From d7df19ca422794b7bba8cce50f14bbb5e41bb805 Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Thu, 17 Nov 2011 23:11:55 +0100 Subject: [PATCH] old test code: uIP with IPv4, uIP unicast --- exercise_remotecontrol/uip_udp/Makefile | 11 ++ .../uip_udp/led_remote_control.c | 155 ++++++++++++++++++ exercise_remotecontrol/uip_udp_ipv4/Makefile | 11 ++ .../uip_udp_ipv4/led_remote_control.c | 145 ++++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 exercise_remotecontrol/uip_udp/Makefile create mode 100644 exercise_remotecontrol/uip_udp/led_remote_control.c create mode 100644 exercise_remotecontrol/uip_udp_ipv4/Makefile create mode 100644 exercise_remotecontrol/uip_udp_ipv4/led_remote_control.c diff --git a/exercise_remotecontrol/uip_udp/Makefile b/exercise_remotecontrol/uip_udp/Makefile new file mode 100644 index 0000000..1edd8e9 --- /dev/null +++ b/exercise_remotecontrol/uip_udp/Makefile @@ -0,0 +1,11 @@ +include ../../Makefile.properties +CONTIKI_PROJECT=led_remote_control +CFLAGS += -DWITH_UIP=1 -DDEBUG=1 -DCONTIKI_CONF_RANDOM_MAC=1 +UIP_CONF_IPV6=1 + +all: $(CONTIKI_PROJECT) + +upload: $(CONTIKI_PROJECT).upload + +include $(CONTIKI)/Makefile.include + diff --git a/exercise_remotecontrol/uip_udp/led_remote_control.c b/exercise_remotecontrol/uip_udp/led_remote_control.c new file mode 100644 index 0000000..6ecc06e --- /dev/null +++ b/exercise_remotecontrol/uip_udp/led_remote_control.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include +#include +//#include +//#include +#include + +////////////////// DEBUGGING STUFF +#define DEBUG DEBUG_PRINT +#include "net/uip-debug.h" +////////////////////////////////// + +//extern struct uip_eth_addr uip_ethaddr; + +PROCESS(led_remote_control, "binary LED counter via button over UDP"); +AUTOSTART_PROCESSES(&led_remote_control); + +/** node address */ +static uint16_t node_addr; +static uip_ipaddr_t ipaddr; +/** UDP connection object */ +static struct uip_udp_conn * udp_conn; +/** Port used for the connection */ +#define UDP_PORT 1337 +#define MAX_PAYLOAD_LEN 40 +#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) + +/** somehow these need to be defined... */ + +/*---------------------------------------------------------------------------*/ +static void +print_local_addresses(void) +{ + int i; + uint8_t state; + + PRINTF("Server IPv6 addresses: "); + for(i = 0; i < UIP_DS6_ADDR_NB; i++) { + state = uip_ds6_if.addr_list[i].state; + if(uip_ds6_if.addr_list[i].isused && + (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) { + PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr); + PRINTF("\n"); + } + } +} +// +//PT_THREAD(recv(struct psock * ps)) { +// PSOCK_BEGIN(ps); +// +// PSOCK_READBUF(ps); +// printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer); +// // FIXME +// if(buffer % 2) { +// leds_toggle(LEDS_GREEN); +// } +// leds_toggle(LEDS_YELLOW); +// printf("toggled LEDs\n"); +// PSOCK_END(ps); +//} + +PROCESS_THREAD(led_remote_control, ev, data) { + PROCESS_BEGIN(); + + leds_init(); + leds_off(LEDS_ALL); + + SENSORS_ACTIVATE(button_sensor); + + // get node address + node_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0); + printf("node address is %d\n", node_addr); + + + // set MAC address +// uip_lladdr.addr[4] = 0xfe; +// uip_lladdr.addr[5] = 0x33; +// uip_lladdr.addr[6] = (node_addr >> 8) & 0xff; +// uip_lladdr.addr[7] = node_addr & 0xff; +// settings_set(SETTINGS_KEY_EUI64, &uip_lladdr.addr, sizeof(uip_lladdr)); + + + + // set up IP stack +// uip_ds6_init(); + print_local_addresses(); +// static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}}; +// uip_setethaddr(eth_addr); + +// uip_ip6addr(&ipaddr, 0xfe80, 0x1234, 0x5678, 0x90ab, 0xcdef, 0x1357, 0x9ace, +// node_addr); +// uip_sethostaddr(&ipaddr); +// uip_ipaddr(&ipaddr, 255, 255, 0, 0); +// uip_setnetmask(&ipaddr); + +// uip_gethostaddr(&ipaddr); +// PRINTF("IP address is "); +// PRINT6ADDR(&ipaddr); +// PRINTF("\n"); + + // udp_conn = udp_broadcast_new(uip_htons(UDP_PORT), 0); + uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0x1d00, 0x22ff, 0xfe33, 0x001f); + udp_conn = udp_new(&ipaddr, uip_htons(UDP_PORT), 0); + + udp_bind(udp_conn, UIP_HTONS(UDP_PORT)); + + static struct etimer timer; + while(1) { + PROCESS_WAIT_EVENT_UNTIL( + (ev == sensors_event && data == &button_sensor) || + ev == tcpip_event); + + printf("event detected\n"); + if(ev == sensors_event) { + printf("surprise, it's a button event!\n"); + // debounce for 250 ms + etimer_set(&timer, CLOCK_SECOND * 0.25); + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); + + static int seq_id; + char buf[MAX_PAYLOAD_LEN]; + + //uip_create_linklocal_allnodes_mcast(&udp_conn->ripaddr); + //uip_ipaddr_copy(&udp_conn->ripaddr, &ipaddr); + PRINTF("Sending message from "); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF(" to "); + PRINT6ADDR(&udp_conn->ripaddr); + sprintf(buf, "Hello! (%d)", ++seq_id); + PRINTF(": %s\n", buf); + + uip_udp_packet_send(udp_conn, buf, strlen(buf)); + + + + } else if(ev == tcpip_event) { + PRINTF("tcpip_event detected\n"); + if(uip_newdata()) { + ((char *)uip_appdata)[uip_datalen()] = 0; + PRINTF("Received: '%s' from ", (char *)uip_appdata); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF("\n"); + + /* Restore server connection to allow data from any node */ + //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); + } + } + } + + PROCESS_END(); +} diff --git a/exercise_remotecontrol/uip_udp_ipv4/Makefile b/exercise_remotecontrol/uip_udp_ipv4/Makefile new file mode 100644 index 0000000..bd8d6f4 --- /dev/null +++ b/exercise_remotecontrol/uip_udp_ipv4/Makefile @@ -0,0 +1,11 @@ +include ../../Makefile.properties +CONTIKI_PROJECT=led_remote_control +CFLAGS += -DWITH_UIP=1 -DDEBUG=1 -DCONTIKI_CONF_RANDOM_MAC=1 +#UIP_CONF_IPV6=1 + +all: $(CONTIKI_PROJECT) + +upload: $(CONTIKI_PROJECT).upload + +include $(CONTIKI)/Makefile.include + diff --git a/exercise_remotecontrol/uip_udp_ipv4/led_remote_control.c b/exercise_remotecontrol/uip_udp_ipv4/led_remote_control.c new file mode 100644 index 0000000..febcb5d --- /dev/null +++ b/exercise_remotecontrol/uip_udp_ipv4/led_remote_control.c @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +#include +//#include +//#include +#include + +////////////////// DEBUGGING STUFF +#define DEBUG DEBUG_PRINT +#include "net/uip-debug.h" +////////////////////////////////// + +//extern struct uip_eth_addr uip_ethaddr; + +PROCESS(led_remote_control, "binary LED counter via button over UDP"); +AUTOSTART_PROCESSES(&led_remote_control); + +/** node address */ +static uint16_t node_addr; +static uip_ipaddr_t ipaddr; +/** UDP connection object */ +static struct uip_udp_conn * udp_conn; +/** Port used for the connection */ +#define UDP_PORT 1337 +#define MAX_PAYLOAD_LEN 40 +#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) + +/*--------------------------------------------------------------------------- +static void +print_local_addresses(void) +{ + int i; + uint8_t state; + + PRINTF("Server IPv6 addresses: "); + for(i = 0; i < UIP_DS6_ADDR_NB; i++) { + state = uip_ds6_if.addr_list[i].state; + if(uip_ds6_if.addr_list[i].isused && + (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) { + PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr); + PRINTF("\n"); + } + } +}*/ +// +//PT_THREAD(recv(struct psock * ps)) { +// PSOCK_BEGIN(ps); +// +// PSOCK_READBUF(ps); +// printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer); +// // FIXME +// if(buffer % 2) { +// leds_toggle(LEDS_GREEN); +// } +// leds_toggle(LEDS_YELLOW); +// printf("toggled LEDs\n"); +// PSOCK_END(ps); +//} + +PROCESS_THREAD(led_remote_control, ev, data) { + PROCESS_BEGIN(); + + leds_init(); + leds_off(LEDS_ALL); + + SENSORS_ACTIVATE(button_sensor); + + // get node address + node_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0); + printf("node address is %d\n", node_addr); + + + // set MAC address +// uip_lladdr.addr[4] = 0xfe; +// uip_lladdr.addr[5] = 0x33; +// uip_lladdr.addr[6] = (node_addr >> 8) & 0xff; +// uip_lladdr.addr[7] = node_addr & 0xff; +// settings_set(SETTINGS_KEY_EUI64, &uip_lladdr.addr, sizeof(uip_lladdr)); + + + + // set up IP stack +// static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}}; +// uip_setethaddr(eth_addr); + + uip_ipaddr(&ipaddr, 169, 254, (node_addr >> 8) & 0xFF, node_addr & 0xFF); + uip_sethostaddr(&ipaddr); + uip_ipaddr(&ipaddr, 255, 255, 0, 0); + uip_setnetmask(&ipaddr); + +// uip_gethostaddr(&ipaddr); +// PRINTF("IP address is "); +// PRINT6ADDR(&ipaddr); +// PRINTF("\n"); + + udp_conn = udp_broadcast_new(uip_htons(UDP_PORT), 0); +// udp_bind(udp_conn, UIP_HTONS(UDP_PORT)); + + static struct etimer timer; + while(1) { + PROCESS_WAIT_EVENT_UNTIL( + (ev == sensors_event && data == &button_sensor) || + ev == tcpip_event); + + if(ev == sensors_event) { + // debounce for 250 ms + etimer_set(&timer, CLOCK_SECOND * 0.25); + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); + + static int seq_id; + char buf[MAX_PAYLOAD_LEN]; + + //uip_ipaddr_copy(&udp_conn->ripaddr, &UIP_IP_BUF->srcipaddr); + PRINTF("Sending message from "); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF(" to "); + PRINT6ADDR(&udp_conn->ripaddr); + sprintf(buf, "Hello! (%d)", ++seq_id); + PRINTF(": %s\n", buf); + + uip_udp_packet_send(udp_conn, buf, strlen(buf)); + + /* Restore server connection to allow data from any node */ + //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); + + + } else if(ev == tcpip_event) { + if(uip_newdata()) { + ((char *)uip_appdata)[uip_datalen()] = 0; + PRINTF("Received: '%s' from ", (char *)uip_appdata); + PRINT6ADDR(&UIP_IP_BUF->srcipaddr); + PRINTF("\n"); + + /* Restore server connection to allow data from any node */ + memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); + } + } + } + + PROCESS_END(); +} -- 2.20.1