blob: 8d5617a1c85e24b541d01a6bed3a829533dad5bd [file] [log] [blame]
adamdunkels42ad15c2004-09-17 20:42:08 +00001#include "contiki.h"
2#include "ctk.h"
3#include "dhcpc.h"
4
5EK_EVENTHANDLER(eventhandler, ev, data);
6EK_PROCESS(p, "DHCP", EK_PRIO_NORMAL,
7 eventhandler, NULL, NULL);
8static ek_id_t id = EK_ID_NONE;
9
10static struct ctk_window window;
11static struct ctk_button getbutton =
12 {CTK_BUTTON(0, 0, 16, "Request address")};
13static struct ctk_label statuslabel =
14 {CTK_LABEL(0, 1, 16, 1, "")};
15
16
17static struct ctk_label ipaddrlabel =
18 {CTK_LABEL(0, 3, 10, 1, "IP address")};
19static char ipaddr[17];
20static struct ctk_textentry ipaddrentry =
21 {CTK_LABEL(11, 3, 16, 1, ipaddr)};
22static struct ctk_label netmasklabel =
23 {CTK_LABEL(0, 4, 10, 1, "Netmask")};
24static char netmask[17];
25static struct ctk_textentry netmaskentry =
26 {CTK_LABEL(11, 4, 16, 1, netmask)};
27static struct ctk_label gatewaylabel =
28 {CTK_LABEL(0, 5, 10, 1, "Gateway")};
29static char gateway[17];
30static struct ctk_textentry gatewayentry =
31 {CTK_LABEL(11, 5, 16, 1, gateway)};
32static struct ctk_label dnsserverlabel =
33 {CTK_LABEL(0, 6, 10, 1, "DNS server")};
34static char dnsserver[17];
35static struct ctk_textentry dnsserverentry =
36 {CTK_LABEL(11, 6, 16, 1, dnsserver)};
37
38enum {
39 SHOWCONFIG
40};
41/*---------------------------------------------------------------------------*/
42LOADER_INIT_FUNC(dhcp_init, arg)
43{
44 arg_free(arg);
45 if(id == EK_ID_NONE) {
46 id = ek_start(&p);
47 }
48}
49/*---------------------------------------------------------------------------*/
50static void
51set_statustext(char *text)
52{
53 ctk_label_set_text(&statuslabel, text);
54 CTK_WIDGET_REDRAW(&statuslabel);
55}
56/*---------------------------------------------------------------------------*/
adamdunkels42ad15c2004-09-17 20:42:08 +000057static char *
58makebyte(u8_t byte, char *str)
59{
60 if(byte >= 100) {
61 *str++ = (byte / 100 ) % 10 + '0';
62 }
63 if(byte >= 10) {
64 *str++ = (byte / 10) % 10 + '0';
65 }
66 *str++ = (byte % 10) + '0';
67
68 return str;
69}
70/*---------------------------------------------------------------------------*/
71static void
72makeaddr(u16_t *addr, char *str)
73{
74 str = makebyte(HTONS(addr[0]) >> 8, str);
75 *str++ = '.';
76 str = makebyte(HTONS(addr[0]) & 0xff, str);
77 *str++ = '.';
78 str = makebyte(HTONS(addr[1]) >> 8, str);
79 *str++ = '.';
80 str = makebyte(HTONS(addr[1]) & 0xff, str);
81 *str++ = 0;
82}
83/*---------------------------------------------------------------------------*/
84static void
85makestrings(void)
86{
87 u16_t addr[2], *addrptr;
88
89 uip_gethostaddr(addr);
90 makeaddr(addr, ipaddr);
91
92 uip_getnetmask(addr);
93 makeaddr(addr, netmask);
94
95 uip_getdraddr(addr);
96 makeaddr(addr, gateway);
97
98 addrptr = resolv_getserver();
99 if(addrptr != NULL) {
100 makeaddr(addrptr, dnsserver);
101 }
102
103}
104/*---------------------------------------------------------------------------*/
adamdunkels2cba96f2004-09-17 21:00:16 +0000105EK_EVENTHANDLER(eventhandler, ev, data)
106{
107 if(ev == EK_EVENT_INIT) {
108 ctk_window_new(&window, 28, 7, "DHCP");
oliverschmidtb86b0652005-04-24 13:37:42 +0000109
adamdunkels2cba96f2004-09-17 21:00:16 +0000110 CTK_WIDGET_ADD(&window, &getbutton);
111 CTK_WIDGET_ADD(&window, &statuslabel);
adamdunkels2cba96f2004-09-17 21:00:16 +0000112 CTK_WIDGET_ADD(&window, &ipaddrlabel);
113 CTK_WIDGET_ADD(&window, &ipaddrentry);
114 CTK_WIDGET_ADD(&window, &netmasklabel);
115 CTK_WIDGET_ADD(&window, &netmaskentry);
116 CTK_WIDGET_ADD(&window, &gatewaylabel);
117 CTK_WIDGET_ADD(&window, &gatewayentry);
118 CTK_WIDGET_ADD(&window, &dnsserverlabel);
119 CTK_WIDGET_ADD(&window, &dnsserverentry);
120
oliverschmidtb86b0652005-04-24 13:37:42 +0000121 CTK_WIDGET_FOCUS(&window, &getbutton);
122
adamdunkels2cba96f2004-09-17 21:00:16 +0000123 ctk_window_open(&window);
124 dhcpc_init();
125 } else if(ev == ctk_signal_widget_activate) {
126 if(data == (ek_data_t)&getbutton) {
127 dhcpc_request();
128 set_statustext("Requesting...");
129 }
130 } else if(ev == tcpip_event) {
131 dhcpc_appcall(data);
132 } else if(ev == EK_EVENT_REQUEST_EXIT ||
133 ev == ctk_signal_window_close) {
134 ctk_window_close(&window);
135 ek_exit();
adamdunkelsec2f4a12004-09-18 20:13:02 +0000136 id = EK_ID_NONE;
adamdunkels2cba96f2004-09-17 21:00:16 +0000137 LOADER_UNLOAD();
138 } else if(ev == SHOWCONFIG) {
139 makestrings();
140 ctk_window_redraw(&window);
141 }
142}
143/*---------------------------------------------------------------------------*/
adamdunkels42ad15c2004-09-17 20:42:08 +0000144void
145dhcpc_configured(void)
146{
147 set_statustext("Configured.");
148 ek_post(EK_PROC_ID(EK_CURRENT()), SHOWCONFIG, NULL);
149}
150/*---------------------------------------------------------------------------*/