radio-test.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "contiki-esb.h"
00048 #include <string.h>
00049
00050 PROCESS(radio_test_process, "Radio test");
00051 AUTOSTART_PROCESSES(&radio_test_process);
00052
00053 #define ON 1
00054 #define OFF 0
00055
00056 #define HEADER "RTST"
00057 #define PACKET_SIZE 20
00058 #define PORT 2345
00059
00060 struct indicator {
00061 int onoff;
00062 int led;
00063 clock_time_t interval;
00064 struct etimer timer;
00065 };
00066
00067
00068 static void
00069 set(struct indicator *indicator, int onoff) {
00070 if(indicator->onoff ^ onoff) {
00071 indicator->onoff = onoff;
00072 if(onoff) {
00073 leds_on(indicator->led);
00074 } else {
00075 leds_off(indicator->led);
00076 }
00077 }
00078 if(onoff) {
00079 etimer_set(&indicator->timer, indicator->interval);
00080 }
00081 }
00082
00083 PROCESS_THREAD(radio_test_process, ev, data)
00084 {
00085 static struct etimer send_timer;
00086 static struct uip_udp_conn *conn;
00087 static struct indicator recv, other, flash;
00088
00089 PROCESS_BEGIN();
00090
00091
00092 recv.onoff = other.onoff = flash.onoff = OFF;
00093 recv.interval = other.interval = CLOCK_SECOND;
00094 flash.interval = 1;
00095 recv.led = LEDS_YELLOW;
00096 other.led = LEDS_GREEN;
00097 flash.led = LEDS_RED;
00098
00099 conn = udp_broadcast_new(UIP_HTONS(PORT), NULL);
00100 etimer_set(&send_timer, CLOCK_SECOND);
00101
00102 while(1) {
00103 PROCESS_WAIT_EVENT();
00104
00105 if(ev == PROCESS_EVENT_TIMER) {
00106 if(data == &send_timer) {
00107 etimer_reset(&send_timer);
00108 tcpip_poll_udp(conn);
00109
00110 } else if(data == &other.timer) {
00111 set(&other, OFF);
00112
00113 } else if(data == &recv.timer) {
00114 set(&recv, OFF);
00115
00116 } else if(data == &flash.timer) {
00117 set(&flash, OFF);
00118 }
00119
00120 } else if(ev == tcpip_event) {
00121
00122 if(uip_poll()) {
00123
00124 memcpy(uip_appdata, HEADER, sizeof(HEADER));
00125 ((char *)uip_appdata)[sizeof(HEADER)] = recv.onoff;
00126
00127 uip_send(uip_appdata, PACKET_SIZE);
00128
00129 set(&flash, ON);
00130 }
00131
00132 if(uip_newdata()) {
00133
00134 if(uip_datalen() < PACKET_SIZE
00135 || strncmp((char *)uip_appdata, HEADER, sizeof(HEADER))) {
00136
00137 leds_blink();
00138
00139 } else {
00140 set(&recv, ON);
00141 set(&other, ((char *)uip_appdata)[sizeof(HEADER)] ? ON : OFF);
00142
00143
00144
00145 etimer_set(&send_timer, CLOCK_SECOND);
00146 etimer_adjust(&send_timer, - (int) (CLOCK_SECOND >> 1));
00147
00148 beep();
00149 }
00150 }
00151 }
00152 }
00153 PROCESS_END();
00154 }
00155