2006-06-18 00:41:10 +02:00
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "ctk/ctk.h"
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/dhcpc.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PROCESS(dhcp_process, "DHCP");
|
|
|
|
|
2008-02-07 17:28:11 +01:00
|
|
|
AUTOSTART_PROCESSES(&dhcp_process);
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
static struct ctk_window window;
|
|
|
|
static struct ctk_button getbutton =
|
|
|
|
{CTK_BUTTON(0, 0, 16, "Request address")};
|
|
|
|
static struct ctk_label statuslabel =
|
|
|
|
{CTK_LABEL(0, 1, 16, 1, "")};
|
|
|
|
|
|
|
|
|
|
|
|
static struct ctk_label ipaddrlabel =
|
|
|
|
{CTK_LABEL(0, 3, 10, 1, "IP address")};
|
|
|
|
static char ipaddr[17];
|
|
|
|
static struct ctk_textentry ipaddrentry =
|
|
|
|
{CTK_LABEL(11, 3, 16, 1, ipaddr)};
|
|
|
|
static struct ctk_label netmasklabel =
|
|
|
|
{CTK_LABEL(0, 4, 10, 1, "Netmask")};
|
|
|
|
static char netmask[17];
|
|
|
|
static struct ctk_textentry netmaskentry =
|
|
|
|
{CTK_LABEL(11, 4, 16, 1, netmask)};
|
|
|
|
static struct ctk_label gatewaylabel =
|
|
|
|
{CTK_LABEL(0, 5, 10, 1, "Gateway")};
|
|
|
|
static char gateway[17];
|
|
|
|
static struct ctk_textentry gatewayentry =
|
|
|
|
{CTK_LABEL(11, 5, 16, 1, gateway)};
|
|
|
|
static struct ctk_label dnsserverlabel =
|
|
|
|
{CTK_LABEL(0, 6, 10, 1, "DNS server")};
|
|
|
|
static char dnsserver[17];
|
|
|
|
static struct ctk_textentry dnsserverentry =
|
|
|
|
{CTK_LABEL(11, 6, 16, 1, dnsserver)};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SHOWCONFIG
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_statustext(char *text)
|
|
|
|
{
|
|
|
|
ctk_label_set_text(&statuslabel, text);
|
|
|
|
CTK_WIDGET_REDRAW(&statuslabel);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static char *
|
2012-02-20 20:13:50 +01:00
|
|
|
makebyte(uint8_t byte, char *str)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
if(byte >= 100) {
|
|
|
|
*str++ = (byte / 100 ) % 10 + '0';
|
|
|
|
}
|
|
|
|
if(byte >= 10) {
|
|
|
|
*str++ = (byte / 10) % 10 + '0';
|
|
|
|
}
|
|
|
|
*str++ = (byte % 10) + '0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2006-09-19 01:27:42 +02:00
|
|
|
makeaddr(uip_ipaddr_t *addr, char *str)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2006-09-19 01:27:42 +02:00
|
|
|
str = makebyte(addr->u8[0], str);
|
2006-06-18 00:41:10 +02:00
|
|
|
*str++ = '.';
|
2006-09-19 01:27:42 +02:00
|
|
|
str = makebyte(addr->u8[1], str);
|
2006-06-18 00:41:10 +02:00
|
|
|
*str++ = '.';
|
2006-09-19 01:27:42 +02:00
|
|
|
str = makebyte(addr->u8[2], str);
|
2006-06-18 00:41:10 +02:00
|
|
|
*str++ = '.';
|
2006-09-19 01:27:42 +02:00
|
|
|
str = makebyte(addr->u8[3], str);
|
2006-06-18 00:41:10 +02:00
|
|
|
*str++ = 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
makestrings(void)
|
|
|
|
{
|
2006-09-19 01:27:42 +02:00
|
|
|
uip_ipaddr_t addr, *addrptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2006-09-19 01:27:42 +02:00
|
|
|
uip_gethostaddr(&addr);
|
|
|
|
makeaddr(&addr, ipaddr);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2006-09-19 01:27:42 +02:00
|
|
|
uip_getnetmask(&addr);
|
|
|
|
makeaddr(&addr, netmask);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2006-09-19 01:27:42 +02:00
|
|
|
uip_getdraddr(&addr);
|
|
|
|
makeaddr(&addr, gateway);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2014-04-16 15:52:22 +02:00
|
|
|
addrptr = uip_nameserver_get(0);
|
2006-06-18 00:41:10 +02:00
|
|
|
if(addrptr != NULL) {
|
|
|
|
makeaddr(addrptr, dnsserver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(dhcp_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
2015-06-21 14:25:52 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
ctk_window_new(&window, 28, 7, "DHCP");
|
2015-06-21 14:25:52 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
CTK_WIDGET_ADD(&window, &getbutton);
|
|
|
|
CTK_WIDGET_ADD(&window, &statuslabel);
|
|
|
|
CTK_WIDGET_ADD(&window, &ipaddrlabel);
|
|
|
|
CTK_WIDGET_ADD(&window, &ipaddrentry);
|
|
|
|
CTK_WIDGET_ADD(&window, &netmasklabel);
|
|
|
|
CTK_WIDGET_ADD(&window, &netmaskentry);
|
|
|
|
CTK_WIDGET_ADD(&window, &gatewaylabel);
|
|
|
|
CTK_WIDGET_ADD(&window, &gatewayentry);
|
|
|
|
CTK_WIDGET_ADD(&window, &dnsserverlabel);
|
|
|
|
CTK_WIDGET_ADD(&window, &dnsserverentry);
|
2015-06-21 14:25:52 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
CTK_WIDGET_FOCUS(&window, &getbutton);
|
|
|
|
|
|
|
|
ctk_window_open(&window);
|
2013-01-09 17:03:57 +01:00
|
|
|
dhcpc_init(uip_lladdr.addr, sizeof(uip_lladdr.addr));
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT();
|
2015-06-21 14:25:52 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
if(ev == ctk_signal_widget_activate) {
|
|
|
|
if(data == (process_data_t)&getbutton) {
|
|
|
|
dhcpc_request();
|
|
|
|
set_statustext("Requesting...");
|
|
|
|
}
|
2015-06-21 14:25:52 +02:00
|
|
|
} else if(ev == tcpip_event || ev == PROCESS_EVENT_TIMER) {
|
2006-06-18 00:41:10 +02:00
|
|
|
dhcpc_appcall(ev, data);
|
|
|
|
} else if(ev == PROCESS_EVENT_EXIT ||
|
|
|
|
ev == ctk_signal_window_close) {
|
|
|
|
ctk_window_close(&window);
|
|
|
|
process_exit(&dhcp_process);
|
|
|
|
LOADER_UNLOAD();
|
|
|
|
} else if(ev == SHOWCONFIG) {
|
|
|
|
makestrings();
|
|
|
|
ctk_window_redraw(&window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
dhcpc_configured(const struct dhcpc_state *s)
|
|
|
|
{
|
2006-08-27 01:08:32 +02:00
|
|
|
uip_sethostaddr(&s->ipaddr);
|
|
|
|
uip_setnetmask(&s->netmask);
|
|
|
|
uip_setdraddr(&s->default_router);
|
2014-04-16 15:52:22 +02:00
|
|
|
uip_nameserver_update(&s->dnsaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
|
2006-06-18 00:41:10 +02:00
|
|
|
set_statustext("Configured.");
|
|
|
|
process_post(PROCESS_CURRENT(), SHOWCONFIG, NULL);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 01:08:16 +02:00
|
|
|
void
|
|
|
|
dhcpc_unconfigured(const struct dhcpc_state *s)
|
|
|
|
{
|
|
|
|
set_statustext("Unconfigured.");
|
|
|
|
process_post(PROCESS_CURRENT(), SHOWCONFIG, NULL);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|