2 #include <contiki-lib.h>
3 #include <contiki-net.h>
6 #include <dev/button-sensor.h>
9 //#include <net/psock.h>
12 ////////////////// DEBUGGING STUFF
13 #define DEBUG DEBUG_PRINT
14 #include "net/uip-debug.h"
15 //////////////////////////////////
17 //extern struct uip_eth_addr uip_ethaddr;
19 PROCESS(led_remote_control
, "binary LED counter via button over UDP");
20 AUTOSTART_PROCESSES(&led_remote_control
);
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 */
29 #define MAX_PAYLOAD_LEN 40
30 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
32 /*---------------------------------------------------------------------------
34 print_local_addresses(void)
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);
50 //PT_THREAD(recv(struct psock * ps)) {
54 // printf("node %d: recv from %d: %d\n", /* FIXME */ 0, 0, buffer);
57 // leds_toggle(LEDS_GREEN);
59 // leds_toggle(LEDS_YELLOW);
60 // printf("toggled LEDs\n");
64 PROCESS_THREAD(led_remote_control
, ev
, data
) {
70 SENSORS_ACTIVATE(button_sensor
);
73 node_addr
= settings_get_uint16(SETTINGS_KEY_PAN_ADDR
, 0);
74 printf("node address is %d\n", node_addr
);
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));
87 // static struct uip_eth_addr eth_addr = {{0x00, 0x06, 0x98, 0x01, 0x00, 0x00}};
88 // uip_setethaddr(eth_addr);
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
);
95 // uip_gethostaddr(&ipaddr);
96 // PRINTF("IP address is ");
97 // PRINT6ADDR(&ipaddr);
100 udp_conn
= udp_broadcast_new(uip_htons(UDP_PORT
), 0);
101 // udp_bind(udp_conn, UIP_HTONS(UDP_PORT));
103 static struct etimer timer
;
105 PROCESS_WAIT_EVENT_UNTIL(
106 (ev
== sensors_event
&& data
== &button_sensor
) ||
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
));
115 char buf
[MAX_PAYLOAD_LEN
];
117 //uip_ipaddr_copy(&udp_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
118 PRINTF("Sending message from ");
119 PRINT6ADDR(&UIP_IP_BUF
->srcipaddr
);
121 PRINT6ADDR(&udp_conn
->ripaddr
);
122 sprintf(buf
, "Hello! (%d)", ++seq_id
);
123 PRINTF(": %s\n", buf
);
125 uip_udp_packet_send(udp_conn
, buf
, strlen(buf
));
127 /* Restore server connection to allow data from any node */
128 //memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
131 } else if(ev
== tcpip_event
) {
133 ((char *)uip_appdata
)[uip_datalen()] = 0;
134 PRINTF("Received: '%s' from ", (char *)uip_appdata
);
135 PRINT6ADDR(&UIP_IP_BUF
->srcipaddr
);
138 /* Restore server connection to allow data from any node */
139 memset(&udp_conn
->ripaddr
, 0, sizeof(udp_conn
->ripaddr
));
This page took 0.052661 seconds and 5 git commands to generate.