old test code: uIP with IPv4, uIP unicast
[wsn-p.git] / exercise_remotecontrol / uip_udp_ipv4 / 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 /*---------------------------------------------------------------------------
33 static void
34 print_local_addresses(void)
35 {
36 int i;
37 uint8_t state;
38
39 PRINTF("Server IPv6 addresses: ");
40 for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
41 state = uip_ds6_if.addr_list[i].state;
42 if(uip_ds6_if.addr_list[i].isused &&
43 (state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
44 PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
45 PRINTF("\n");
46 }
47 }
48 }*/
49 //
50 //PT_THREAD(recv(struct psock * ps)) {
51 // PSOCK_BEGIN(ps);
52 //
53 // PSOCK_READBUF(ps);
54 // printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer);
55 // // FIXME
56 // if(buffer % 2) {
57 // leds_toggle(LEDS_GREEN);
58 // }
59 // leds_toggle(LEDS_YELLOW);
60 // printf("toggled LEDs\n");
61 // PSOCK_END(ps);
62 //}
63
64 PROCESS_THREAD(led_remote_control, ev, data) {
65 PROCESS_BEGIN();
66
67 leds_init();
68 leds_off(LEDS_ALL);
69
70 SENSORS_ACTIVATE(button_sensor);
71
72 // get node address
73 node_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0);
74 printf("node address is %d\n", node_addr);
75
76
77 // set MAC address
78 // uip_lladdr.addr[4] = 0xfe;
79 // uip_lladdr.addr[5] = 0x33;
80 // uip_lladdr.addr[6] = (node_addr >> 8) & 0xff;
81 // uip_lladdr.addr[7] = node_addr & 0xff;
82 // settings_set(SETTINGS_KEY_EUI64, &uip_lladdr.addr, sizeof(uip_lladdr));
83
84
85
86 // set up IP stack
87 // static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}};
88 // uip_setethaddr(eth_addr);
89
90 uip_ipaddr(&ipaddr, 169, 254, (node_addr >> 8) & 0xFF, node_addr & 0xFF);
91 uip_sethostaddr(&ipaddr);
92 uip_ipaddr(&ipaddr, 255, 255, 0, 0);
93 uip_setnetmask(&ipaddr);
94
95 // uip_gethostaddr(&ipaddr);
96 // PRINTF("IP address is ");
97 // PRINT6ADDR(&ipaddr);
98 // PRINTF("\n");
99
100 udp_conn = udp_broadcast_new(uip_htons(UDP_PORT), 0);
101 // udp_bind(udp_conn, UIP_HTONS(UDP_PORT));
102
103 static struct etimer timer;
104 while(1) {
105 PROCESS_WAIT_EVENT_UNTIL(
106 (ev == sensors_event && data == &button_sensor) ||
107 ev == tcpip_event);
108
109 if(ev == sensors_event) {
110 // debounce for 250 ms
111 etimer_set(&timer, CLOCK_SECOND * 0.25);
112 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
113
114 static int seq_id;
115 char buf[MAX_PAYLOAD_LEN];
116
117 //uip_ipaddr_copy(&udp_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
118 PRINTF("Sending message from ");
119 PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
120 PRINTF(" to ");
121 PRINT6ADDR(&udp_conn->ripaddr);
122 sprintf(buf, "Hello! (%d)", ++seq_id);
123 PRINTF(": %s\n", buf);
124
125 uip_udp_packet_send(udp_conn, buf, strlen(buf));
126
127 /* Restore server connection to allow data from any node */
128 //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
129
130
131 } else if(ev == tcpip_event) {
132 if(uip_newdata()) {
133 ((char *)uip_appdata)[uip_datalen()] = 0;
134 PRINTF("Received: '%s' from ", (char *)uip_appdata);
135 PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
136 PRINTF("\n");
137
138 /* Restore server connection to allow data from any node */
139 memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
140 }
141 }
142 }
143
144 PROCESS_END();
145 }
This page took 0.052661 seconds and 5 git commands to generate.