Contiki 3.x
dhcp.c
1 #include "contiki-net.h"
2 #include "ctk/ctk.h"
3 #include "net/ip/dhcpc.h"
4 
5 
6 
7 PROCESS(dhcp_process, "DHCP");
8 
9 AUTOSTART_PROCESSES(&dhcp_process);
10 
11 static struct ctk_window window;
12 static struct ctk_button getbutton =
13  {CTK_BUTTON(0, 0, 16, "Request address")};
14 static struct ctk_label statuslabel =
15  {CTK_LABEL(0, 1, 16, 1, "")};
16 
17 
18 static struct ctk_label ipaddrlabel =
19  {CTK_LABEL(0, 3, 10, 1, "IP address")};
20 static char ipaddr[17];
21 static struct ctk_textentry ipaddrentry =
22  {CTK_LABEL(11, 3, 16, 1, ipaddr)};
23 static struct ctk_label netmasklabel =
24  {CTK_LABEL(0, 4, 10, 1, "Netmask")};
25 static char netmask[17];
26 static struct ctk_textentry netmaskentry =
27  {CTK_LABEL(11, 4, 16, 1, netmask)};
28 static struct ctk_label gatewaylabel =
29  {CTK_LABEL(0, 5, 10, 1, "Gateway")};
30 static char gateway[17];
31 static struct ctk_textentry gatewayentry =
32  {CTK_LABEL(11, 5, 16, 1, gateway)};
33 static struct ctk_label dnsserverlabel =
34  {CTK_LABEL(0, 6, 10, 1, "DNS server")};
35 static char dnsserver[17];
36 static struct ctk_textentry dnsserverentry =
37  {CTK_LABEL(11, 6, 16, 1, dnsserver)};
38 
39 enum {
40  SHOWCONFIG
41 };
42 /*---------------------------------------------------------------------------*/
43 static void
44 set_statustext(char *text)
45 {
46  ctk_label_set_text(&statuslabel, text);
47  CTK_WIDGET_REDRAW(&statuslabel);
48 }
49 /*---------------------------------------------------------------------------*/
50 static char *
51 makebyte(uint8_t byte, char *str)
52 {
53  if(byte >= 100) {
54  *str++ = (byte / 100 ) % 10 + '0';
55  }
56  if(byte >= 10) {
57  *str++ = (byte / 10) % 10 + '0';
58  }
59  *str++ = (byte % 10) + '0';
60 
61  return str;
62 }
63 /*---------------------------------------------------------------------------*/
64 static void
65 makeaddr(uip_ipaddr_t *addr, char *str)
66 {
67  str = makebyte(addr->u8[0], str);
68  *str++ = '.';
69  str = makebyte(addr->u8[1], str);
70  *str++ = '.';
71  str = makebyte(addr->u8[2], str);
72  *str++ = '.';
73  str = makebyte(addr->u8[3], str);
74  *str++ = 0;
75 }
76 /*---------------------------------------------------------------------------*/
77 static void
78 makestrings(void)
79 {
80  uip_ipaddr_t addr, *addrptr;
81 
82  uip_gethostaddr(&addr);
83  makeaddr(&addr, ipaddr);
84 
85  uip_getnetmask(&addr);
86  makeaddr(&addr, netmask);
87 
88  uip_getdraddr(&addr);
89  makeaddr(&addr, gateway);
90 
91  addrptr = resolv_getserver();
92  if(addrptr != NULL) {
93  makeaddr(addrptr, dnsserver);
94  }
95 }
96 /*---------------------------------------------------------------------------*/
97 PROCESS_THREAD(dhcp_process, ev, data)
98 {
99  PROCESS_BEGIN();
100 
101  ctk_window_new(&window, 28, 7, "DHCP");
102 
103  CTK_WIDGET_ADD(&window, &getbutton);
104  CTK_WIDGET_ADD(&window, &statuslabel);
105  CTK_WIDGET_ADD(&window, &ipaddrlabel);
106  CTK_WIDGET_ADD(&window, &ipaddrentry);
107  CTK_WIDGET_ADD(&window, &netmasklabel);
108  CTK_WIDGET_ADD(&window, &netmaskentry);
109  CTK_WIDGET_ADD(&window, &gatewaylabel);
110  CTK_WIDGET_ADD(&window, &gatewayentry);
111  CTK_WIDGET_ADD(&window, &dnsserverlabel);
112  CTK_WIDGET_ADD(&window, &dnsserverentry);
113 
114  CTK_WIDGET_FOCUS(&window, &getbutton);
115 
116  ctk_window_open(&window);
117  dhcpc_init(uip_lladdr.addr, sizeof(uip_lladdr.addr));
118 
119 
120  while(1) {
122 
123  if(ev == ctk_signal_widget_activate) {
124  if(data == (process_data_t)&getbutton) {
125  dhcpc_request();
126  set_statustext("Requesting...");
127  }
128  } else if(ev == tcpip_event) {
129  dhcpc_appcall(ev, data);
130  } else if(ev == PROCESS_EVENT_EXIT ||
131  ev == ctk_signal_window_close) {
132  ctk_window_close(&window);
133  process_exit(&dhcp_process);
134  LOADER_UNLOAD();
135  } else if(ev == SHOWCONFIG) {
136  makestrings();
137  ctk_window_redraw(&window);
138  }
139  }
140 
141  PROCESS_END();
142 }
143 /*---------------------------------------------------------------------------*/
144 void
145 dhcpc_configured(const struct dhcpc_state *s)
146 {
147  uip_sethostaddr(&s->ipaddr);
148  uip_setnetmask(&s->netmask);
149  uip_setdraddr(&s->default_router);
150  resolv_conf(&s->dnsaddr);
151  set_statustext("Configured.");
152  process_post(PROCESS_CURRENT(), SHOWCONFIG, NULL);
153 }
154 /*---------------------------------------------------------------------------*/
155 void
156 dhcpc_unconfigured(const struct dhcpc_state *s)
157 {
158  set_statustext("Unconfigured.");
159  process_post(PROCESS_CURRENT(), SHOWCONFIG, NULL);
160 }
161 /*---------------------------------------------------------------------------*/
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
#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
#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
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
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_label_set_text(l, t)
Set the text of a label.
Definition: ctk.h:849
#define NULL
The null pointer.
void ctk_window_redraw(struct ctk_window *w)
Redraw a window.
Definition: ctk.c:652
#define uip_getdraddr(addr)
Get the default router's IP address.
Definition: uip.h:250
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
#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
process_event_t ctk_signal_widget_activate
Emitted when a widget is activated (pressed).
Definition: ctk.c:126
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
#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 CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
#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