adamdunkels | 42ad15c | 2004-09-17 20:42:08 +0000 | [diff] [blame] | 1 | #include "contiki.h" |
| 2 | #include "ctk.h" |
| 3 | #include "dhcpc.h" |
| 4 | |
| 5 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 6 | EK_PROCESS(p, "DHCP", EK_PRIO_NORMAL, |
| 7 | eventhandler, NULL, NULL); |
| 8 | static ek_id_t id = EK_ID_NONE; |
| 9 | |
| 10 | static struct ctk_window window; |
| 11 | static struct ctk_button getbutton = |
| 12 | {CTK_BUTTON(0, 0, 16, "Request address")}; |
| 13 | static struct ctk_label statuslabel = |
| 14 | {CTK_LABEL(0, 1, 16, 1, "")}; |
| 15 | |
| 16 | |
| 17 | static struct ctk_label ipaddrlabel = |
| 18 | {CTK_LABEL(0, 3, 10, 1, "IP address")}; |
| 19 | static char ipaddr[17]; |
| 20 | static struct ctk_textentry ipaddrentry = |
| 21 | {CTK_LABEL(11, 3, 16, 1, ipaddr)}; |
| 22 | static struct ctk_label netmasklabel = |
| 23 | {CTK_LABEL(0, 4, 10, 1, "Netmask")}; |
| 24 | static char netmask[17]; |
| 25 | static struct ctk_textentry netmaskentry = |
| 26 | {CTK_LABEL(11, 4, 16, 1, netmask)}; |
| 27 | static struct ctk_label gatewaylabel = |
| 28 | {CTK_LABEL(0, 5, 10, 1, "Gateway")}; |
| 29 | static char gateway[17]; |
| 30 | static struct ctk_textentry gatewayentry = |
| 31 | {CTK_LABEL(11, 5, 16, 1, gateway)}; |
| 32 | static struct ctk_label dnsserverlabel = |
| 33 | {CTK_LABEL(0, 6, 10, 1, "DNS server")}; |
| 34 | static char dnsserver[17]; |
| 35 | static struct ctk_textentry dnsserverentry = |
| 36 | {CTK_LABEL(11, 6, 16, 1, dnsserver)}; |
| 37 | |
| 38 | enum { |
| 39 | SHOWCONFIG |
| 40 | }; |
| 41 | /*---------------------------------------------------------------------------*/ |
| 42 | LOADER_INIT_FUNC(dhcp_init, arg) |
| 43 | { |
| 44 | arg_free(arg); |
| 45 | if(id == EK_ID_NONE) { |
| 46 | id = ek_start(&p); |
| 47 | } |
| 48 | } |
| 49 | /*---------------------------------------------------------------------------*/ |
| 50 | static void |
| 51 | set_statustext(char *text) |
| 52 | { |
| 53 | ctk_label_set_text(&statuslabel, text); |
| 54 | CTK_WIDGET_REDRAW(&statuslabel); |
| 55 | } |
| 56 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 42ad15c | 2004-09-17 20:42:08 +0000 | [diff] [blame] | 57 | static char * |
| 58 | makebyte(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 | /*---------------------------------------------------------------------------*/ |
| 71 | static void |
| 72 | makeaddr(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 | /*---------------------------------------------------------------------------*/ |
| 84 | static void |
| 85 | makestrings(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 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 2cba96f | 2004-09-17 21:00:16 +0000 | [diff] [blame] | 105 | EK_EVENTHANDLER(eventhandler, ev, data) |
| 106 | { |
| 107 | if(ev == EK_EVENT_INIT) { |
| 108 | ctk_window_new(&window, 28, 7, "DHCP"); |
oliverschmidt | b86b065 | 2005-04-24 13:37:42 +0000 | [diff] [blame] | 109 | |
adamdunkels | 2cba96f | 2004-09-17 21:00:16 +0000 | [diff] [blame] | 110 | CTK_WIDGET_ADD(&window, &getbutton); |
| 111 | CTK_WIDGET_ADD(&window, &statuslabel); |
adamdunkels | 2cba96f | 2004-09-17 21:00:16 +0000 | [diff] [blame] | 112 | 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 | |
oliverschmidt | b86b065 | 2005-04-24 13:37:42 +0000 | [diff] [blame] | 121 | CTK_WIDGET_FOCUS(&window, &getbutton); |
| 122 | |
adamdunkels | 2cba96f | 2004-09-17 21:00:16 +0000 | [diff] [blame] | 123 | 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(); |
adamdunkels | ec2f4a1 | 2004-09-18 20:13:02 +0000 | [diff] [blame] | 136 | id = EK_ID_NONE; |
adamdunkels | 2cba96f | 2004-09-17 21:00:16 +0000 | [diff] [blame] | 137 | LOADER_UNLOAD(); |
| 138 | } else if(ev == SHOWCONFIG) { |
| 139 | makestrings(); |
| 140 | ctk_window_redraw(&window); |
| 141 | } |
| 142 | } |
| 143 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 42ad15c | 2004-09-17 20:42:08 +0000 | [diff] [blame] | 144 | void |
| 145 | dhcpc_configured(void) |
| 146 | { |
| 147 | set_statustext("Configured."); |
| 148 | ek_post(EK_PROC_ID(EK_CURRENT()), SHOWCONFIG, NULL); |
| 149 | } |
| 150 | /*---------------------------------------------------------------------------*/ |