blob: d4c67aabb21ca7c01576d706fb5ab2c98439e90d [file] [log] [blame]
adamdunkels4292c862003-04-08 17:56:43 +00001/*
2 * Copyright (c) 2002, 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 *
adamdunkelsc4db2872003-04-16 18:27:33 +000035 * $Id: netconf.c,v 1.5 2003/04/16 18:27:33 adamdunkels Exp $
adamdunkels4292c862003-04-08 17:56:43 +000036 *
37 */
38
39#include "uip_main.h"
40#include "uip.h"
41#include "uip_arp.h"
42#include "ctk.h"
43#include "dispatcher.h"
44
45#include "loader.h"
46
47/* TCP/IP configuration window. */
48static struct ctk_window tcpipwindow;
49
50#ifdef WITH_ETHERNET
51static struct ctk_label ipaddrlabel =
52 {CTK_LABEL(0, 1, 10, 1, "IP address")};
53static char ipaddr[17];
54static struct ctk_textentry ipaddrtextentry =
55 {CTK_TEXTENTRY(11, 1, 16, 1, ipaddr, 16)};
56static struct ctk_label netmasklabel =
57 {CTK_LABEL(0, 3, 10, 1, "Netmask")};
58static char netmask[17];
59static struct ctk_textentry netmasktextentry =
60 {CTK_TEXTENTRY(11, 3, 16, 1, netmask, 16)};
61static struct ctk_label gatewaylabel =
62 {CTK_LABEL(0, 5, 10, 1, "Gateway")};
63static char gateway[17];
64static struct ctk_textentry gatewaytextentry =
65 {CTK_TEXTENTRY(11, 5, 16, 1, gateway, 16)};
66static struct ctk_label dnsserverlabel =
67 {CTK_LABEL(0, 7, 10, 1, "DNS server")};
68static char dnsserver[17];
69static struct ctk_textentry dnsservertextentry =
70 {CTK_TEXTENTRY(11, 7, 16, 1, dnsserver, 16)};
71#else /* WITH_ETHERNET */
72static struct ctk_label ipaddrlabel =
73 {CTK_LABEL(0, 2, 10, 1, "IP address")};
74static char ipaddr[17];
75static struct ctk_textentry ipaddrtextentry =
76 {CTK_TEXTENTRY(11, 2, 16, 1, ipaddr, 16)};
77static struct ctk_label dnsserverlabel =
78 {CTK_LABEL(0, 4, 10, 1, "DNS server")};
79static char dnsserver[17];
80static struct ctk_textentry dnsservertextentry =
81 {CTK_TEXTENTRY(11, 4, 16, 1, dnsserver, 16)};
82#endif /* WITH_ETHERNET */
83
84static struct ctk_button tcpipclosebutton =
85 {CTK_BUTTON(0, 9, 2, "Ok")};
86
adamdunkels78c03dc2003-04-09 13:45:05 +000087static DISPATCHER_SIGHANDLER(netconf_sighandler, s, data);
adamdunkels4292c862003-04-08 17:56:43 +000088static struct dispatcher_proc p =
89 {DISPATCHER_PROC("Network config", NULL, netconf_sighandler, NULL)};
90static ek_id_t id;
91
92
adamdunkelsc4db2872003-04-16 18:27:33 +000093static void makestrings(void);
94
adamdunkels4292c862003-04-08 17:56:43 +000095/*-----------------------------------------------------------------------------------*/
96LOADER_INIT_FUNC(netconf_init)
97{
98 if(id == EK_ID_NONE) {
99 id = dispatcher_start(&p);
100
101 /* Create TCP/IP configuration window. */
102 ctk_window_new(&tcpipwindow, 30, 10, "TCP/IP config");
adamdunkels4b7a7ab2003-04-09 19:22:19 +0000103 if(ctk_draw_width() < 30) {
104 ctk_window_move(&tcpipwindow, 0,
105 (ctk_draw_height() - 10) / 2 - 2);
106 } else {
107 ctk_window_move(&tcpipwindow,
108 (ctk_draw_width() - 30) / 2,
109 (ctk_draw_height() - 10) / 2 - 2);
110 }
111
adamdunkels4292c862003-04-08 17:56:43 +0000112
113
114#ifdef WITH_ETHERNET
115 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel);
116 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry);
117 CTK_WIDGET_ADD(&tcpipwindow, &netmasklabel);
118 CTK_WIDGET_ADD(&tcpipwindow, &netmasktextentry);
119 CTK_WIDGET_ADD(&tcpipwindow, &gatewaylabel);
120 CTK_WIDGET_ADD(&tcpipwindow, &gatewaytextentry);
121 CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel);
122 CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry);
123#else
124 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel);
125 CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry);
126 CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel);
127 CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry);
128#endif /* WITH_ETHERNET */
129
130 CTK_WIDGET_ADD(&tcpipwindow, &tcpipclosebutton);
131
132 CTK_WIDGET_FOCUS(&tcpipwindow, &ipaddrtextentry);
adamdunkelsc4db2872003-04-16 18:27:33 +0000133
134 /* Fill the configuration strings with values from the current
135 configuration */
136 makestrings();
adamdunkels4292c862003-04-08 17:56:43 +0000137
138 dispatcher_listen(ctk_signal_button_activate);
139 dispatcher_listen(ctk_signal_window_close);
140 }
141 ctk_window_open(&tcpipwindow);
142}
143/*-----------------------------------------------------------------------------------*/
adamdunkelsc4db2872003-04-16 18:27:33 +0000144static char *
145makebyte(u8_t byte, char *str)
146{
147 if(byte >= 100) {
148 *str++ = (byte / 100 ) % 10 + '0';
149 }
150 if(byte >= 10) {
151 *str++ = (byte / 10) % 10 + '0';
152 }
153 *str++ = (byte % 10) + '0';
154
155 return str;
156}
157/*-----------------------------------------------------------------------------------*/
158static void
159makeaddr(u16_t *addr, char *str)
160{
161 str = makebyte(HTONS(addr[0]) >> 8, str);
162 *str++ = '.';
163 str = makebyte(HTONS(addr[0]) & 0xff, str);
164 *str++ = '.';
165 str = makebyte(HTONS(addr[1]) >> 8, str);
166 *str++ = '.';
167 str = makebyte(HTONS(addr[1]) & 0xff, str);
168 *str++ = 0;
169}
170/*-----------------------------------------------------------------------------------*/
171static void
172makestrings(void)
173{
174 u16_t addr[2], *addrptr;
175
176 uip_gethostaddr(addr);
177 makeaddr(addr, ipaddr);
178
179#ifdef WITH_ETHERNET
180 uip_getnetmask(addr);
181 makeaddr(addr, netmask);
182
183 uip_getdraddr(addr);
184 makeaddr(addr, gateway);
185#endif /* WITH_ETHERNET */
186
187 addrptr = resolv_getserver();
188 if(addrptr != NULL) {
189 makeaddr(addrptr, dnsserver);
190 }
191
192
193}
194/*-----------------------------------------------------------------------------------*/
adamdunkels4292c862003-04-08 17:56:43 +0000195static void
196nullterminate(char *cptr)
197{
198 /* Find the first space character in the ipaddr and put a zero there
199 to end the string. */
200 for(; *cptr != ' ' && *cptr != 0; ++cptr);
201 *cptr = 0;
202}
203/*-----------------------------------------------------------------------------------*/
204static void
205apply_tcpipconfig(void)
206{
207 u16_t addr[2];
208
209#ifdef WITH_UIP
210 nullterminate(ipaddr);
211 if(uip_main_ipaddrconv(ipaddr, (unsigned char *)addr)) {
212 uip_sethostaddr(addr);
213 }
214
215#ifdef WITH_ETHERNET
216 nullterminate(netmask);
217 if(uip_main_ipaddrconv(netmask, (unsigned char *)addr)) {
218 uip_setnetmask(addr);
219 }
220
221 nullterminate(gateway);
222 if(uip_main_ipaddrconv(gateway, (unsigned char *)addr)) {
223 uip_setdraddr(addr);
224 }
225#endif /* WITH_ETHERNET */
226
227 nullterminate(dnsserver);
228 if(uip_main_ipaddrconv(dnsserver, (unsigned char *)addr)) {
229 resolv_conf(addr);
230 }
231#endif /* WITH_UIP */
232}
233/*-----------------------------------------------------------------------------------*/
234static void
adamdunkels0137b442003-04-08 23:27:33 +0000235netconf_quit(void)
adamdunkels4292c862003-04-08 17:56:43 +0000236{
237 dispatcher_exit(&p);
238 id = EK_ID_NONE;
239 LOADER_UNLOAD();
adamdunkels4292c862003-04-08 17:56:43 +0000240}
241/*-----------------------------------------------------------------------------------*/
adamdunkels78c03dc2003-04-09 13:45:05 +0000242static
243DISPATCHER_SIGHANDLER(netconf_sighandler, s, data)
adamdunkels4292c862003-04-08 17:56:43 +0000244{
adamdunkels78c03dc2003-04-09 13:45:05 +0000245 DISPATCHER_SIGHANDLER_ARGS(s, data);
246
adamdunkels4292c862003-04-08 17:56:43 +0000247 if(s == ctk_signal_button_activate) {
248 if(data == (ek_data_t)&tcpipclosebutton) {
249 apply_tcpipconfig();
250 ctk_window_close(&tcpipwindow);
adamdunkels0137b442003-04-08 23:27:33 +0000251 netconf_quit();
252 ctk_redraw();
adamdunkels4292c862003-04-08 17:56:43 +0000253 }
254 } else if(s == ctk_signal_window_close) {
adamdunkels0137b442003-04-08 23:27:33 +0000255 netconf_quit();
adamdunkels4292c862003-04-08 17:56:43 +0000256 }
257}
258/*-----------------------------------------------------------------------------------*/