blob: 50487ff08178594f1c7a7bd138108f3c4025feb6 [file] [log] [blame]
oliverschmidt76872e82004-12-26 14:13:34 +00001/*
2 * Copyright (c) 2004, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
oliverschmidtbc1dc142005-04-12 21:54:08 +000035 * $Id: configedit.c,v 1.5 2005/04/12 21:54:08 oliverschmidt Exp $
oliverschmidt76872e82004-12-26 14:13:34 +000036 *
37 */
38
39
40#include <string.h>
oliverschmidt76872e82004-12-26 14:13:34 +000041
42#include "uiplib.h"
43#include "resolv.h"
44#include "ctk.h"
45
oliverschmidt2fa33232005-01-26 21:33:29 +000046#include "program-handler.h"
47#include "packet-service.h"
oliverschmidtbc1dc142005-04-12 21:54:08 +000048#include "kfs.h"
oliverschmidt4c19ccb2005-03-20 00:23:28 +000049#include "cfs.h"
oliverschmidt2fa33232005-01-26 21:33:29 +000050
oliverschmidt76872e82004-12-26 14:13:34 +000051#include "config.h"
52
53
54static config_t config;
55static struct ctk_window window;
56
57#ifdef __APPLE2ENH__
58
59static struct ctk_label backgroundlabel =
60 {CTK_LABEL(0, 1, 10, 1, "Background")};
61static char bkgnd[2];
62static struct ctk_textentry backgroundtextentry =
63 {CTK_TEXTENTRY(11, 1, 1, 1, bkgnd, 1)};
64static struct ctk_label backgrounddescr =
65 {CTK_LABEL(16, 1, 12, 1, "('x' or ' ')")};
66
67#endif /* __APPLE2ENH__ */
68
69static struct ctk_label slotlabel =
70 {CTK_LABEL(0, 3, 10, 1, "LAN slot")};
71static char slot[2];
72static struct ctk_textentry slottextentry =
73 {CTK_TEXTENTRY(11, 3, 1, 1, slot, 1)};
74static struct ctk_label slotdescr =
75 {CTK_LABEL(16, 3, 12, 1, "('1' to '7')")};
76
oliverschmidt2fa33232005-01-26 21:33:29 +000077static struct ctk_label driverlabel =
78 {CTK_LABEL(0, 5, 10, 1, "LAN driver")};
79static char driver[16];
80static struct ctk_textentry drivertextentry =
81 {CTK_TEXTENTRY(11, 5, 15, 1, driver, 15)};
82
oliverschmidt76872e82004-12-26 14:13:34 +000083static struct ctk_label ipaddrlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +000084 {CTK_LABEL(0, 7, 10, 1, "IP address")};
oliverschmidt76872e82004-12-26 14:13:34 +000085static char ipaddr[16];
86static struct ctk_textentry ipaddrtextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +000087 {CTK_TEXTENTRY(11, 7, 15, 1, ipaddr, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +000088
89#ifdef WITH_ETHERNET
90
91static struct ctk_label netmasklabel =
oliverschmidt2fa33232005-01-26 21:33:29 +000092 {CTK_LABEL(0, 9, 10, 1, "Netmask")};
oliverschmidt76872e82004-12-26 14:13:34 +000093static char netmask[16];
94static struct ctk_textentry netmasktextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +000095 {CTK_TEXTENTRY(11, 9, 15, 1, netmask, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +000096
97static struct ctk_label gatewaylabel =
oliverschmidt2fa33232005-01-26 21:33:29 +000098 {CTK_LABEL(0, 11, 10, 1, "Gateway")};
oliverschmidt76872e82004-12-26 14:13:34 +000099static char gateway[16];
100static struct ctk_textentry gatewaytextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000101 {CTK_TEXTENTRY(11, 11, 15, 1, gateway, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000102
103static struct ctk_label dnsserverlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +0000104 {CTK_LABEL(0, 13, 10, 1, "DNS server")};
oliverschmidt76872e82004-12-26 14:13:34 +0000105static char dnsserver[16];
106static struct ctk_textentry dnsservertextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000107 {CTK_TEXTENTRY(11, 13, 15, 1, dnsserver, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000108
109#else /* WITH_ETHERNET */
110
111static struct ctk_label dnsserverlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +0000112 {CTK_LABEL(0, 9, 10, 1, "DNS server")};
oliverschmidt76872e82004-12-26 14:13:34 +0000113static char dnsserver[16];
114static struct ctk_textentry dnsservertextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000115 {CTK_TEXTENTRY(11, 9, 15, 1, dnsserver, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000116
117#endif /* WITH_ETHERNET */
118
119static struct ctk_button okbutton =
oliverschmidt2fa33232005-01-26 21:33:29 +0000120 {CTK_BUTTON(0, 15, 12, "Save & close")};
oliverschmidt76872e82004-12-26 14:13:34 +0000121static struct ctk_button cancelbutton =
oliverschmidt2fa33232005-01-26 21:33:29 +0000122 {CTK_BUTTON(21, 15, 6, "Cancel")};
oliverschmidt76872e82004-12-26 14:13:34 +0000123
124EK_EVENTHANDLER(config_eventhandler, ev, data);
125EK_PROCESS(p, "Configuration", EK_PRIO_NORMAL,
126 config_eventhandler, NULL, NULL);
127static ek_id_t id = EK_ID_NONE;
oliverschmidt3b308a62005-02-01 00:21:36 +0000128static ek_id_t driverid = EK_ID_NONE;
oliverschmidt76872e82004-12-26 14:13:34 +0000129
130/*-----------------------------------------------------------------------------------*/
131LOADER_INIT_FUNC(config_init, arg)
132{
133 arg_free(arg);
134
135 if(id == EK_ID_NONE) {
136 id = ek_start(&p);
137 }
138}
139/*-----------------------------------------------------------------------------------*/
140static char *
141makebyte(u8_t byte, char *str)
142{
143 if(byte >= 100) {
144 *str++ = (byte / 100 ) % 10 + '0';
145 }
146 if(byte >= 10) {
147 *str++ = (byte / 10) % 10 + '0';
148 }
149 *str++ = (byte % 10) + '0';
150
151 return str;
152}
153/*-----------------------------------------------------------------------------------*/
154static void
155makeaddr(u16_t *addr, char *str)
156{
157 str = makebyte(HTONS(addr[0]) >> 8, str);
158 *str++ = '.';
159 str = makebyte(HTONS(addr[0]) & 0xff, str);
160 *str++ = '.';
161 str = makebyte(HTONS(addr[1]) >> 8, str);
162 *str++ = '.';
163 str = makebyte(HTONS(addr[1]) & 0xff, str);
164 *str++ = 0;
165}
166/*-----------------------------------------------------------------------------------*/
oliverschmidt2fa33232005-01-26 21:33:29 +0000167static int
168makedriver(const char *name, char *str)
169{
170 char *pattern = PACKET_SERVICE_NAME ": ";
171
172 while(*pattern) {
173 if(*name++ != *pattern++) {
174 return 0;
175 }
176 }
177
178 while(*name) {
179 *str++ = *name++;
180 }
181 strcpy(str, ".drv");
182
183 return 1;
184}
185/*-----------------------------------------------------------------------------------*/
oliverschmidt76872e82004-12-26 14:13:34 +0000186static void
187makestrings(void)
188{
189 u16_t addr[2], *addrptr;
oliverschmidt2fa33232005-01-26 21:33:29 +0000190 struct ek_proc *p;
oliverschmidt76872e82004-12-26 14:13:34 +0000191
192#ifdef __APPLE2ENH__
193
194 if(ctk_draw_getbackground()) {
195 *bkgnd = 'x';
196 }
197
198#endif /* __APPLE2ENH__ */
199
oliverschmidt2fa33232005-01-26 21:33:29 +0000200 *slot = config_getlanslot() + '0';
201
202 for(p = EK_PROCS(); p != NULL; p = p->next) {
203 if(makedriver(p->name, driver)) {
oliverschmidt3b308a62005-02-01 00:21:36 +0000204 driverid = p->id;
oliverschmidt2fa33232005-01-26 21:33:29 +0000205 break;
206 }
207 }
208
oliverschmidt76872e82004-12-26 14:13:34 +0000209#ifdef WITH_UIP
210
211 uip_gethostaddr(addr);
212 makeaddr(addr, ipaddr);
213
214#ifdef WITH_ETHERNET
215
216 uip_getnetmask(addr);
217 makeaddr(addr, netmask);
218
219 uip_getdraddr(addr);
220 makeaddr(addr, gateway);
221
222#endif /* WITH_ETHERNET */
223
224 addrptr = resolv_getserver();
225 if(addrptr != NULL) {
226 makeaddr(addrptr, dnsserver);
227 }
228
229#endif /* WITH_UIP */
230
231}
232/*-----------------------------------------------------------------------------------*/
233static void
234nullterminate(char *cptr)
235{
236 /* Find the first space character in the ipaddr and put a zero there
237 to end the string. */
238 for(; *cptr != ' ' && *cptr != 0; ++cptr);
239 *cptr = 0;
240}
241/*-----------------------------------------------------------------------------------*/
242static void
243makeconfig(void)
244{
245 u16_t addr[2];
246
247#ifdef __APPLE2ENH__
248
249 if(*bkgnd == 'x' || *bkgnd == 'X') {
250 config.bkgnd = 1;
251 }
252
253#endif /* __APPLE2ENH__ */
254
255 if(*slot >= '1' && *slot <= '7') {
256 config.slot = *slot - '0';
257 }
258
oliverschmidt2fa33232005-01-26 21:33:29 +0000259 nullterminate(driver);
260 strcpy(config.driver, driver);
261
oliverschmidt76872e82004-12-26 14:13:34 +0000262#ifdef WITH_UIP
263
264 nullterminate(ipaddr);
265 if(uiplib_ipaddrconv(ipaddr, (unsigned char *)addr)) {
266 config.ipaddr[0] = addr[0];
267 config.ipaddr[1] = addr[1];
268 }
269
270#ifdef WITH_ETHERNET
271
272 nullterminate(netmask);
273 if(uiplib_ipaddrconv(netmask, (unsigned char *)addr)) {
274 config.netmask[0] = addr[0];
275 config.netmask[1] = addr[1];
276 }
277
278 nullterminate(gateway);
279 if(uiplib_ipaddrconv(gateway, (unsigned char *)addr)) {
280 config.gateway[0] = addr[0];
281 config.gateway[1] = addr[1];
282 }
283
284#endif /* WITH_ETHERNET */
285
286 nullterminate(dnsserver);
287 if(uiplib_ipaddrconv(dnsserver, (unsigned char *)addr)) {
288 config.dnsserver[0] = addr[0];
289 config.dnsserver[1] = addr[1];
290 }
291
292#endif /* WITH_UIP */
293}
294/*-----------------------------------------------------------------------------------*/
295static void
296config_apply(void)
297{
298#ifdef __APPLE2ENH__
299
300 ctk_draw_setbackground(config.bkgnd);
301
302#endif /* __APPLE2ENH__ */
303
oliverschmidt3b308a62005-02-01 00:21:36 +0000304 if(driverid != EK_ID_NONE) {
305 ek_post(driverid, EK_EVENT_REQUEST_EXIT, NULL);
306 }
307
oliverschmidt2fa33232005-01-26 21:33:29 +0000308 config_setlanslot(config.slot);
309
310 if(*config.driver) {
311 program_handler_load(config.driver, NULL);
312 }
313
oliverschmidt76872e82004-12-26 14:13:34 +0000314#ifdef WITH_UIP
315
316 uip_sethostaddr(config.ipaddr);
317
318#ifdef WITH_ETHERNET
319
320 uip_setnetmask(config.netmask);
321 uip_setdraddr(config.gateway);
322
323#endif /* WITH_ETHERNET */
324
325 resolv_conf(config.dnsserver);
326
327#endif /* WITH_UIP */
328}
329/*-----------------------------------------------------------------------------------*/
330static void
331config_save(void)
332{
333 int fd;
334
oliverschmidtbc1dc142005-04-12 21:54:08 +0000335 fd = cfs_open(strcat(kfs_getdir(), "contiki.cfg"), CFS_WRITE);
oliverschmidt76872e82004-12-26 14:13:34 +0000336 if(fd == -1) {
337 return;
338 }
oliverschmidt4c19ccb2005-03-20 00:23:28 +0000339 cfs_write(fd, &config, sizeof(config));
340 cfs_close(fd);
oliverschmidt76872e82004-12-26 14:13:34 +0000341}
342/*-----------------------------------------------------------------------------------*/
343EK_EVENTHANDLER(config_eventhandler, ev, data)
344{
345 EK_EVENTHANDLER_ARGS(ev, data);
346
347 if(ev == EK_EVENT_INIT) {
oliverschmidt2fa33232005-01-26 21:33:29 +0000348 ctk_window_new(&window, 29, 16, "Config editor");
oliverschmidt76872e82004-12-26 14:13:34 +0000349#ifdef __APPLE2ENH__
350 CTK_WIDGET_ADD(&window, &backgroundlabel);
351 CTK_WIDGET_ADD(&window, &backgroundtextentry);
352 CTK_WIDGET_ADD(&window, &backgrounddescr);
353#endif /* __APPLE2ENH__ */
354 CTK_WIDGET_ADD(&window, &slotlabel);
355 CTK_WIDGET_ADD(&window, &slottextentry);
356 CTK_WIDGET_ADD(&window, &slotdescr);
oliverschmidt2fa33232005-01-26 21:33:29 +0000357 CTK_WIDGET_ADD(&window, &driverlabel);
358 CTK_WIDGET_ADD(&window, &drivertextentry);
359 CTK_WIDGET_ADD(&window, &ipaddrlabel);
oliverschmidt76872e82004-12-26 14:13:34 +0000360 CTK_WIDGET_ADD(&window, &ipaddrtextentry);
361#ifdef WITH_ETHERNET
362 CTK_WIDGET_ADD(&window, &netmasklabel);
363 CTK_WIDGET_ADD(&window, &netmasktextentry);
364 CTK_WIDGET_ADD(&window, &gatewaylabel);
365 CTK_WIDGET_ADD(&window, &gatewaytextentry);
366#endif /* WITH_ETHERNET */
367 CTK_WIDGET_ADD(&window, &dnsserverlabel);
368 CTK_WIDGET_ADD(&window, &dnsservertextentry);
369 CTK_WIDGET_ADD(&window, &okbutton);
370 CTK_WIDGET_ADD(&window, &cancelbutton);
371#ifdef __APPLE2ENH__
372 CTK_WIDGET_FOCUS(&window, &backgroundtextentry);
373#else /* __APPLE2ENH__ */
374 CTK_WIDGET_FOCUS(&window, &slottextentry);
375#endif /* __APPLE2ENH__ */
376
377 /* Fill the configuration strings with values from the current
378 configuration */
379 makestrings();
380 ctk_window_open(&window);
381 } else if(ev == ctk_signal_button_activate) {
382 if(data == (ek_data_t)&okbutton) {
383
384 /* Fill the configuration with values from the current
385 configuration strings */
386 makeconfig();
387 config_apply();
388 config_save();
389 goto quit;
390 } else if(data == (ek_data_t)&cancelbutton) {
391 goto quit;
392 }
393
394 } else if(ev == ctk_signal_window_close ||
395 ev == EK_EVENT_REQUEST_EXIT) {
396 goto quit;
397 }
398 return;
399
400quit:
401 ctk_window_close(&window);
402 ek_exit();
403 id = EK_ID_NONE;
404 LOADER_UNLOAD();
405}
406/*-----------------------------------------------------------------------------------*/