Contiki 3.x
netconf.c
1 /*
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. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki desktop environment
31  *
32  *
33  */
34 
35 #include "contiki-net.h"
36 #include "ctk/ctk.h"
37 
38 
39 /* TCP/IP configuration window. */
40 static struct ctk_window tcpipwindow;
41 
42 static struct ctk_label ipaddrlabel =
43  {CTK_LABEL(0, 1, 10, 1, "IP address")};
44 static char ipaddr[17];
45 static struct ctk_textentry ipaddrtextentry =
46  {CTK_TEXTENTRY(11, 1, 16, 1, ipaddr, 16)};
47 static struct ctk_label netmasklabel =
48  {CTK_LABEL(0, 3, 10, 1, "Netmask")};
49 static char netmask[17];
50 static struct ctk_textentry netmasktextentry =
51  {CTK_TEXTENTRY(11, 3, 16, 1, netmask, 16)};
52 static struct ctk_label gatewaylabel =
53  {CTK_LABEL(0, 5, 10, 1, "Gateway")};
54 static char gateway[17];
55 static struct ctk_textentry gatewaytextentry =
56  {CTK_TEXTENTRY(11, 5, 16, 1, gateway, 16)};
57 #if UIP_UDP
58 static struct ctk_label dnsserverlabel =
59  {CTK_LABEL(0, 7, 10, 1, "DNS server")};
60 static char dnsserver[17];
61 static struct ctk_textentry dnsservertextentry =
62  {CTK_TEXTENTRY(11, 7, 16, 1, dnsserver, 16)};
63 #endif /* UIP_UDP */
64 static struct ctk_button tcpipclosebutton =
65  {CTK_BUTTON(0, 9, 2, "Ok")};
66 
67 PROCESS(netconf_process, "Network configurator");
68 
69 AUTOSTART_PROCESSES(&netconf_process);
70 
71 static void makestrings(void);
72 
73 /*-----------------------------------------------------------------------------------*/
74 static char *
75 makebyte(uint8_t byte, char *str)
76 {
77  if(byte >= 100) {
78  *str++ = (byte / 100 ) % 10 + '0';
79  }
80  if(byte >= 10) {
81  *str++ = (byte / 10) % 10 + '0';
82  }
83  *str++ = (byte % 10) + '0';
84 
85  return str;
86 }
87 /*-----------------------------------------------------------------------------------*/
88 static void
89 makeaddr(uip_ipaddr_t *addr, char *str)
90 {
91  str = makebyte(addr->u8[0], str);
92  *str++ = '.';
93  str = makebyte(addr->u8[1], str);
94  *str++ = '.';
95  str = makebyte(addr->u8[2], str);
96  *str++ = '.';
97  str = makebyte(addr->u8[3], str);
98  *str++ = 0;
99 }
100 /*-----------------------------------------------------------------------------------*/
101 static void
102 makestrings(void)
103 {
104  uip_ipaddr_t addr, *addrptr;
105 
106  uip_gethostaddr(&addr);
107  makeaddr(&addr, ipaddr);
108 
109  uip_getnetmask(&addr);
110  makeaddr(&addr, netmask);
111 
112  uip_getdraddr(&addr);
113  makeaddr(&addr, gateway);
114 
115 #if UIP_UDP
116  addrptr = resolv_getserver();
117  if(addrptr != NULL) {
118  makeaddr(addrptr, dnsserver);
119  }
120 #endif /* UIP_UDP */
121 }
122 /*-----------------------------------------------------------------------------------*/
123 static void
124 nullterminate(char *cptr)
125 {
126  /* Find the first space character in the ipaddr and put a zero there
127  to end the string. */
128  for(; *cptr != ' ' && *cptr != 0; ++cptr);
129  *cptr = 0;
130 }
131 /*-----------------------------------------------------------------------------------*/
132 static void
133 apply_tcpipconfig(void)
134 {
135  uip_ipaddr_t addr;
136 
137  nullterminate(ipaddr);
138  if(uiplib_ipaddrconv(ipaddr, &addr)) {
139  uip_sethostaddr(&addr);
140  }
141 
142  nullterminate(netmask);
143  if(uiplib_ipaddrconv(netmask, &addr)) {
144  uip_setnetmask(&addr);
145  }
146 
147  nullterminate(gateway);
148  if(uiplib_ipaddrconv(gateway, &addr)) {
149  uip_setdraddr(&addr);
150  }
151 
152 #if UIP_UDP
153  nullterminate(dnsserver);
154  if(uiplib_ipaddrconv(dnsserver, &addr)) {
155  resolv_conf(&addr);
156  }
157 #endif /* UIP_UDP */
158 }
159 /*-----------------------------------------------------------------------------------*/
160 static void
161 netconf_quit(void)
162 {
163  process_exit(&netconf_process);
164  LOADER_UNLOAD();
165 }
166 /*-----------------------------------------------------------------------------------*/
167 PROCESS_THREAD(netconf_process, ev, data)
168 {
169 
170  PROCESS_BEGIN();
171 
172  /* Create TCP/IP configuration window. */
173  ctk_window_new(&tcpipwindow, 30, 10, "TCP/IP config");
174 
175  CTK_WIDGET_ADD(&tcpipwindow, &ipaddrlabel);
176  CTK_WIDGET_ADD(&tcpipwindow, &ipaddrtextentry);
177  CTK_WIDGET_ADD(&tcpipwindow, &netmasklabel);
178  CTK_WIDGET_ADD(&tcpipwindow, &netmasktextentry);
179  CTK_WIDGET_ADD(&tcpipwindow, &gatewaylabel);
180  CTK_WIDGET_ADD(&tcpipwindow, &gatewaytextentry);
181 #if UIP_UDP
182  CTK_WIDGET_ADD(&tcpipwindow, &dnsserverlabel);
183  CTK_WIDGET_ADD(&tcpipwindow, &dnsservertextentry);
184 #endif /* UIP_UDP */
185  CTK_WIDGET_ADD(&tcpipwindow, &tcpipclosebutton);
186 
187  CTK_WIDGET_FOCUS(&tcpipwindow, &ipaddrtextentry);
188 
189  /* Fill the configuration strings with values from the current
190  configuration */
191  makestrings();
192 
193  ctk_window_open(&tcpipwindow);
194 
195  while(1) {
197 
198  if(ev == ctk_signal_button_activate) {
199  if(data == (process_data_t)&tcpipclosebutton) {
200  apply_tcpipconfig();
201  ctk_window_close(&tcpipwindow);
202  netconf_quit();
203  }
204  } else if(
205 #if CTK_CONF_WINDOWCLOSE
206  ev == ctk_signal_window_close ||
207 #endif
208  ev == PROCESS_EVENT_EXIT) {
209  ctk_window_close(&tcpipwindow);
210  netconf_quit();
211  }
212  }
213 
214  PROCESS_END();
215 }
216 /*-----------------------------------------------------------------------------------*/
#define CTK_WIDGET_ADD(win, widg)
Add a widget to a window.
Definition: ctk.h:752
process_event_t ctk_signal_window_close
Emitted when a window is closed.
Definition: ctk.c:155
process_event_t ctk_signal_button_activate
Same as ctk_signal_widget_activate.
Definition: ctk.c:126
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:195
#define uip_gethostaddr(addr)
Get the IP address of this host.
Definition: uip.h:215
void ctk_window_open(CC_REGISTER_ARG struct ctk_window *w)
Open a window, or bring window to front if already open.
Definition: ctk.c:347
#define CTK_WIDGET_FOCUS(win, widg)
Set focus to a widget.
Definition: ctk.h:763
void ctk_window_close(struct ctk_window *w)
Close a window if it is open.
Definition: ctk.c:400
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define CTK_TEXTENTRY(x, y, w, h, text, len)
Instantiating macro for the ctk_textentry widget.
Definition: ctk.h:275
#define NULL
The null pointer.
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
#define uip_getdraddr(addr)
Get the default router's IP address.
Definition: uip.h:250
#define uip_setdraddr(addr)
Set the default router's IP address.
Definition: uip.h:227
#define CTK_LABEL(x, y, w, h, text)
Instantiating macro for the ctk_label widget.
Definition: ctk.h:171
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void ctk_window_new(struct ctk_window *window, unsigned char w, unsigned char h, char *title)
Create a new window.
Definition: ctk.c:732
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define uip_getnetmask(addr)
Get the netmask.
Definition: uip.h:260
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define CTK_BUTTON(x, y, w, text)
Instantiating macro for the ctk_button widget.
Definition: ctk.h:140
Representation of a CTK window.
Definition: ctk.h:506
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:239
CTK header file.
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202