blob: 6856a98fd7c45741fa66417431dbc82b98748929 [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 *
oliverschmidt0dca9282005-05-06 00:05:46 +000035 * $Id: configedit.c,v 1.6 2005/05/06 00:05:46 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
oliverschmidt0dca9282005-05-06 00:05:46 +000051#include "ctk-textentry-checkbox.h"
52
oliverschmidt76872e82004-12-26 14:13:34 +000053#include "config.h"
54
55
56static config_t config;
57static struct ctk_window window;
58
59#ifdef __APPLE2ENH__
60
61static struct ctk_label backgroundlabel =
62 {CTK_LABEL(0, 1, 10, 1, "Background")};
63static char bkgnd[2];
64static struct ctk_textentry backgroundtextentry =
oliverschmidt0dca9282005-05-06 00:05:46 +000065 {CTK_TEXTENTRY_INPUT(11, 1, 1, 1, bkgnd, 1, ctk_textentry_checkbox_input)};
oliverschmidt76872e82004-12-26 14:13:34 +000066static struct ctk_label backgrounddescr =
67 {CTK_LABEL(16, 1, 12, 1, "('x' or ' ')")};
68
69#endif /* __APPLE2ENH__ */
70
71static struct ctk_label slotlabel =
72 {CTK_LABEL(0, 3, 10, 1, "LAN slot")};
73static char slot[2];
74static struct ctk_textentry slottextentry =
75 {CTK_TEXTENTRY(11, 3, 1, 1, slot, 1)};
76static struct ctk_label slotdescr =
77 {CTK_LABEL(16, 3, 12, 1, "('1' to '7')")};
78
oliverschmidt2fa33232005-01-26 21:33:29 +000079static struct ctk_label driverlabel =
80 {CTK_LABEL(0, 5, 10, 1, "LAN driver")};
81static char driver[16];
82static struct ctk_textentry drivertextentry =
83 {CTK_TEXTENTRY(11, 5, 15, 1, driver, 15)};
84
oliverschmidt76872e82004-12-26 14:13:34 +000085static struct ctk_label ipaddrlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +000086 {CTK_LABEL(0, 7, 10, 1, "IP address")};
oliverschmidt76872e82004-12-26 14:13:34 +000087static char ipaddr[16];
88static struct ctk_textentry ipaddrtextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +000089 {CTK_TEXTENTRY(11, 7, 15, 1, ipaddr, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +000090
91#ifdef WITH_ETHERNET
92
93static struct ctk_label netmasklabel =
oliverschmidt2fa33232005-01-26 21:33:29 +000094 {CTK_LABEL(0, 9, 10, 1, "Netmask")};
oliverschmidt76872e82004-12-26 14:13:34 +000095static char netmask[16];
96static struct ctk_textentry netmasktextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +000097 {CTK_TEXTENTRY(11, 9, 15, 1, netmask, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +000098
99static struct ctk_label gatewaylabel =
oliverschmidt2fa33232005-01-26 21:33:29 +0000100 {CTK_LABEL(0, 11, 10, 1, "Gateway")};
oliverschmidt76872e82004-12-26 14:13:34 +0000101static char gateway[16];
102static struct ctk_textentry gatewaytextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000103 {CTK_TEXTENTRY(11, 11, 15, 1, gateway, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000104
105static struct ctk_label dnsserverlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +0000106 {CTK_LABEL(0, 13, 10, 1, "DNS server")};
oliverschmidt76872e82004-12-26 14:13:34 +0000107static char dnsserver[16];
108static struct ctk_textentry dnsservertextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000109 {CTK_TEXTENTRY(11, 13, 15, 1, dnsserver, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000110
111#else /* WITH_ETHERNET */
112
113static struct ctk_label dnsserverlabel =
oliverschmidt2fa33232005-01-26 21:33:29 +0000114 {CTK_LABEL(0, 9, 10, 1, "DNS server")};
oliverschmidt76872e82004-12-26 14:13:34 +0000115static char dnsserver[16];
116static struct ctk_textentry dnsservertextentry =
oliverschmidt2fa33232005-01-26 21:33:29 +0000117 {CTK_TEXTENTRY(11, 9, 15, 1, dnsserver, 15)};
oliverschmidt76872e82004-12-26 14:13:34 +0000118
119#endif /* WITH_ETHERNET */
120
121static struct ctk_button okbutton =
oliverschmidt2fa33232005-01-26 21:33:29 +0000122 {CTK_BUTTON(0, 15, 12, "Save & close")};
oliverschmidt76872e82004-12-26 14:13:34 +0000123static struct ctk_button cancelbutton =
oliverschmidt2fa33232005-01-26 21:33:29 +0000124 {CTK_BUTTON(21, 15, 6, "Cancel")};
oliverschmidt76872e82004-12-26 14:13:34 +0000125
126EK_EVENTHANDLER(config_eventhandler, ev, data);
127EK_PROCESS(p, "Configuration", EK_PRIO_NORMAL,
128 config_eventhandler, NULL, NULL);
129static ek_id_t id = EK_ID_NONE;
oliverschmidt3b308a62005-02-01 00:21:36 +0000130static ek_id_t driverid = EK_ID_NONE;
oliverschmidt76872e82004-12-26 14:13:34 +0000131
132/*-----------------------------------------------------------------------------------*/
133LOADER_INIT_FUNC(config_init, arg)
134{
135 arg_free(arg);
136
137 if(id == EK_ID_NONE) {
138 id = ek_start(&p);
139 }
140}
141/*-----------------------------------------------------------------------------------*/
142static char *
143makebyte(u8_t byte, char *str)
144{
145 if(byte >= 100) {
146 *str++ = (byte / 100 ) % 10 + '0';
147 }
148 if(byte >= 10) {
149 *str++ = (byte / 10) % 10 + '0';
150 }
151 *str++ = (byte % 10) + '0';
152
153 return str;
154}
155/*-----------------------------------------------------------------------------------*/
156static void
157makeaddr(u16_t *addr, char *str)
158{
159 str = makebyte(HTONS(addr[0]) >> 8, str);
160 *str++ = '.';
161 str = makebyte(HTONS(addr[0]) & 0xff, str);
162 *str++ = '.';
163 str = makebyte(HTONS(addr[1]) >> 8, str);
164 *str++ = '.';
165 str = makebyte(HTONS(addr[1]) & 0xff, str);
166 *str++ = 0;
167}
168/*-----------------------------------------------------------------------------------*/
oliverschmidt2fa33232005-01-26 21:33:29 +0000169static int
170makedriver(const char *name, char *str)
171{
172 char *pattern = PACKET_SERVICE_NAME ": ";
173
174 while(*pattern) {
175 if(*name++ != *pattern++) {
176 return 0;
177 }
178 }
179
180 while(*name) {
181 *str++ = *name++;
182 }
183 strcpy(str, ".drv");
184
185 return 1;
186}
187/*-----------------------------------------------------------------------------------*/
oliverschmidt76872e82004-12-26 14:13:34 +0000188static void
189makestrings(void)
190{
191 u16_t addr[2], *addrptr;
oliverschmidt2fa33232005-01-26 21:33:29 +0000192 struct ek_proc *p;
oliverschmidt76872e82004-12-26 14:13:34 +0000193
194#ifdef __APPLE2ENH__
195
196 if(ctk_draw_getbackground()) {
197 *bkgnd = 'x';
198 }
199
200#endif /* __APPLE2ENH__ */
201
oliverschmidt2fa33232005-01-26 21:33:29 +0000202 *slot = config_getlanslot() + '0';
203
204 for(p = EK_PROCS(); p != NULL; p = p->next) {
205 if(makedriver(p->name, driver)) {
oliverschmidt3b308a62005-02-01 00:21:36 +0000206 driverid = p->id;
oliverschmidt2fa33232005-01-26 21:33:29 +0000207 break;
208 }
209 }
210
oliverschmidt76872e82004-12-26 14:13:34 +0000211#ifdef WITH_UIP
212
213 uip_gethostaddr(addr);
214 makeaddr(addr, ipaddr);
215
216#ifdef WITH_ETHERNET
217
218 uip_getnetmask(addr);
219 makeaddr(addr, netmask);
220
221 uip_getdraddr(addr);
222 makeaddr(addr, gateway);
223
224#endif /* WITH_ETHERNET */
225
226 addrptr = resolv_getserver();
227 if(addrptr != NULL) {
228 makeaddr(addrptr, dnsserver);
229 }
230
231#endif /* WITH_UIP */
232
233}
234/*-----------------------------------------------------------------------------------*/
235static void
236nullterminate(char *cptr)
237{
238 /* Find the first space character in the ipaddr and put a zero there
239 to end the string. */
240 for(; *cptr != ' ' && *cptr != 0; ++cptr);
241 *cptr = 0;
242}
243/*-----------------------------------------------------------------------------------*/
244static void
245makeconfig(void)
246{
247 u16_t addr[2];
248
249#ifdef __APPLE2ENH__
250
oliverschmidt0dca9282005-05-06 00:05:46 +0000251 config.bkgnd = (*bkgnd == 'x');
oliverschmidt76872e82004-12-26 14:13:34 +0000252
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/*-----------------------------------------------------------------------------------*/