old test code: uIP with IPv4, uIP unicast
[wsn-p.git] / exercise_remotecontrol / uip_udp / led_remote_control.c
1 #include <contiki.h>
2 #include <contiki-lib.h>
3 #include <contiki-net.h>
4 #include <stdio.h>
5 #include <dev/leds.h>
6 #include <dev/button-sensor.h>
7 #include <settings.h>
8 //#include <net/uip.h>
9 //#include <net/psock.h>
10 #include <string.h>
11
12 ////////////////// DEBUGGING STUFF
13 #define DEBUG DEBUG_PRINT
14 #include "net/uip-debug.h"
15 //////////////////////////////////
16
17 //extern struct uip_eth_addr uip_ethaddr;
18
19 PROCESS(led_remote_control, "binary LED counter via button over UDP");
20 AUTOSTART_PROCESSES(&led_remote_control);
21
22 /** node address */
23 static uint16_t node_addr;
24 static uip_ipaddr_t ipaddr;
25 /** UDP connection object */
26 static struct uip_udp_conn * udp_conn;
27 /** Port used for the connection */
28 #define UDP_PORT 1337
29 #define MAX_PAYLOAD_LEN 40
30 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
31
32 /** somehow these need to be defined... */
33
34 /*---------------------------------------------------------------------------*/
35 static void
36 print_local_addresses(void)
37 {
38 int i;
39 uint8_t state;
40
41 PRINTF("Server IPv6 addresses: ");
42 for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
43 state = uip_ds6_if.addr_list[i].state;
44 if(uip_ds6_if.addr_list[i].isused &&
45 (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
46 PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
47 PRINTF("\n");
48 }
49 }
50 }
51 //
52 //PT_THREAD(recv(struct psock * ps)) {
53 // PSOCK_BEGIN(ps);
54 //
55 // PSOCK_READBUF(ps);
56 // printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer);
57 // // FIXME
58 // if(buffer % 2) {
59 // leds_toggle(LEDS_GREEN);
60 // }
61 // leds_toggle(LEDS_YELLOW);
62 // printf("toggled LEDs\n");
63 // PSOCK_END(ps);
64 //}
65
66 PROCESS_THREAD(led_remote_control, ev, data) {
67 PROCESS_BEGIN();
68
69 leds_init();
70 leds_off(LEDS_ALL);
71
72 SENSORS_ACTIVATE(button_sensor);
73
74 // get node address
75 node_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0);
76 printf("node address is %d\n", node_addr);
77
78
79 // set MAC address
80 // uip_lladdr.addr[4] = 0xfe;
81 // uip_lladdr.addr[5] = 0x33;
82 // uip_lladdr.addr[6] = (node_addr >> 8) & 0xff;
83 // uip_lladdr.addr[7] = node_addr & 0xff;
84 // settings_set(SETTINGS_KEY_EUI64, &uip_lladdr.addr, sizeof(uip_lladdr));
85
86
87
88 // set up IP stack
89 // uip_ds6_init();
90 print_local_addresses();
91 // static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}};
92 // uip_setethaddr(eth_addr);
93
94 // uip_ip6addr(&ipaddr, 0xfe80, 0x1234, 0x5678, 0x90ab, 0xcdef, 0x1357, 0x9ace,
95 // node_addr);
96 // uip_sethostaddr(&ipaddr);
97 // uip_ipaddr(&ipaddr, 255, 255, 0, 0);
98 // uip_setnetmask(&ipaddr);
99
100 // uip_gethostaddr(&ipaddr);
101 // PRINTF("IP address is ");
102 // PRINT6ADDR(&ipaddr);
103 // PRINTF("\n");
104
105 // udp_conn = udp_broadcast_new(uip_htons(UDP_PORT), 0);
106 uip_ip6addr(&ipaddr, 0xfe80, 0, 0, 0, 0x1d00, 0x22ff, 0xfe33, 0x001f);
107 udp_conn = udp_new(&ipaddr, uip_htons(UDP_PORT), 0);
108
109 udp_bind(udp_conn, UIP_HTONS(UDP_PORT));
110
111 static struct etimer timer;
112 while(1) {
113 PROCESS_WAIT_EVENT_UNTIL(
114 (ev == sensors_event && data == &button_sensor) ||
115 ev == tcpip_event);
116
117 printf("event detected\n");
118 if(ev == sensors_event) {
119 printf("surprise, it's a button event!\n");
120 // debounce for 250 ms
121 etimer_set(&timer, CLOCK_SECOND * 0.25);
122 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
123
124 static int seq_id;
125 char buf[MAX_PAYLOAD_LEN];
126
127 //uip_create_linklocal_allnodes_mcast(&udp_conn->ripaddr);
128 //uip_ipaddr_copy(&udp_conn->ripaddr, &ipaddr);
129 PRINTF("Sending message from ");
130 PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
131 PRINTF(" to ");
132 PRINT6ADDR(&udp_conn->ripaddr);
133 sprintf(buf, "Hello! (%d)", ++seq_id);
134 PRINTF(": %s\n", buf);
135
136 uip_udp_packet_send(udp_conn, buf, strlen(buf));
137
138
139
140 } else if(ev == tcpip_event) {
141 PRINTF("tcpip_event detected\n");
142 if(uip_newdata()) {
143 ((char *)uip_appdata)[uip_datalen()] = 0;
144 PRINTF("Received: '%s' from ", (char *)uip_appdata);
145 PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
146 PRINTF("\n");
147
148 /* Restore server connection to allow data from any node */
149 //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
150 }
151 }
152 }
153
154 PROCESS_END();
155 }
This page took 0.052984 seconds and 5 git commands to generate.