pinger.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 #include "contiki-esb.h"
00035
00036 #include <stdio.h>
00037
00038 PROCESS(pinger, "Pinger");
00039
00040 static struct uip_udp_conn *conn;
00041
00042 struct data {
00043 u8_t dummy_data[20];
00044 u16_t id;
00045 u16_t seqno;
00046 u8_t pingpong;
00047 #define PING 0
00048 #define PONG 1
00049 };
00050
00051 static unsigned char pingeron;
00052 static struct etimer etimer;
00053
00054 static unsigned short sent_seqno, last_seqno;
00055
00056 #define PORT 9145
00057
00058 static int place_id = 0, packet_count = 0;
00059
00060
00061
00062 static void
00063 quit(void)
00064 {
00065 process_exit(&pinger);
00066 LOADER_UNLOAD();
00067 }
00068
00069 static void
00070 udp_appcall(void *arg)
00071 {
00072 struct data *d;
00073
00074 d = (struct data *)uip_appdata;
00075
00076 if(uip_newdata()) {
00077 leds_toggle(LEDS_YELLOW);
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 } else if(uip_poll()) {
00099 if(pingeron && etimer_expired(&etimer) && packet_count > 0) {
00100 --packet_count;
00101 d->id = place_id;
00102 d->pingpong = PING;
00103 d->seqno = uip_htons(sent_seqno);
00104 ++sent_seqno;
00105 uip_udp_send(sizeof(struct data));
00106 etimer_reset(&etimer);
00107 leds_toggle(LEDS_GREEN);
00108 }
00109
00110 if(packet_count == 0) {
00111 pingeron = 0;
00112 }
00113 }
00114 }
00115
00116 static
00117 PT_THREAD(config_thread(struct pt *pt, process_event_t ev, process_data_t data))
00118 {
00119 static struct etimer pushtimer;
00120 static int counter;
00121
00122 PT_BEGIN(pt);
00123
00124
00125 while(1) {
00126
00127 PT_WAIT_UNTIL(pt, ev == sensors_event && data == &button_sensor);
00128
00129 beep();
00130
00131 leds_on(LEDS_YELLOW);
00132
00133 etimer_set(&pushtimer, CLOCK_SECOND);
00134 for(counter = 0; !etimer_expired(&pushtimer); ++counter) {
00135 etimer_restart(&pushtimer);
00136 PT_YIELD_UNTIL(pt, (ev == sensors_event && data == &button_sensor) ||
00137 etimer_expired(&pushtimer));
00138 }
00139
00140 place_id = counter;
00141
00142 beep_quick(place_id);
00143
00144 pingeron = 1;
00145
00146 packet_count = 20;
00147
00148 etimer_set(&etimer, CLOCK_SECOND / 2);
00149
00150 leds_off(LEDS_YELLOW);
00151
00152 leds_on(LEDS_RED);
00153 PT_WAIT_UNTIL(pt, packet_count == 0);
00154
00155 pingeron = 0;
00156 leds_off(LEDS_RED);
00157 }
00158
00159 PT_END(pt);
00160 }
00161
00162 PROCESS_THREAD(pinger, ev, data)
00163 {
00164 static struct pt config_pt;
00165
00166 PROCESS_BEGIN();
00167
00168 pingeron = 0;
00169
00170 conn = udp_broadcast_new(UIP_HTONS(PORT), NULL);
00171
00172 PT_INIT(&config_pt);
00173
00174 button_sensor.configure(SENSORS_ACTIVE, 1);
00175
00176
00177 while(1) {
00178
00179 config_thread(&config_pt, ev, data);
00180
00181 PROCESS_WAIT_EVENT();
00182
00183 printf("Event %d\n", ev);
00184
00185 beep();
00186
00187 if(ev == tcpip_event) {
00188 udp_appcall(data);
00189 }
00190
00191 if(ev == PROCESS_EVENT_TIMER && etimer_expired(&etimer)) {
00192 tcpip_poll_udp(conn);
00193 }
00194
00195 }
00196
00197 PROCESS_END();
00198 }
00199