Contiki 2.5
pinger.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: pinger.c,v 1.3 2010/10/19 18:29:05 adamdunkels Exp $
32  */
33 
34 #include "contiki-esb.h"
35 
36 #include <stdio.h>
37 
38 PROCESS(pinger, "Pinger");
39 
40 static struct uip_udp_conn *conn;
41 
42 struct data {
43  u8_t dummy_data[20];
44  u16_t id;
45  u16_t seqno;
46  u8_t pingpong;
47 #define PING 0
48 #define PONG 1
49 };
50 
51 static unsigned char pingeron;
52 static struct etimer etimer;
53 
54 static unsigned short sent_seqno, last_seqno;
55 
56 #define PORT 9145
57 
58 static int place_id = 0, packet_count = 0;
59 
60 
61 /*---------------------------------------------------------------------------*/
62 static void
63 quit(void)
64 {
65  process_exit(&pinger);
66  LOADER_UNLOAD();
67 }
68 /*---------------------------------------------------------------------------*/
69 static void
70 udp_appcall(void *arg)
71 {
72  struct data *d;
73  /* char buf[50];*/
74  d = (struct data *)uip_appdata;
75 
76  if(uip_newdata()) {
77  leds_toggle(LEDS_YELLOW);
78  /* beep();*/
79 
80  /* if(uip_htons(d->seqno) != last_seqno + 1) {
81  leds_toggle(LEDS_RED);
82  beep_quick(2);
83  }*/
84  /* last_seqno = uip_htons(d->seqno);*/
85  /* uip_udp_send(sizeof(struct data));*/
86  /* snprintf(buf, sizeof(buf), "Packet received id %d signal %u\n",
87  d->id, tr1001_sstrength());
88 
89  rs232_print(buf);*/
90  /* if(d->pingpong == PING) {
91  d->pingpong = PONG;
92  } else {
93  d->pingpong = PING;
94  d->seqno = uip_htons(uip_htons(d->seqno) + 1);
95  }*/
96  /* uip_udp_send(sizeof(struct data));
97  timer_restart(&timer);*/
98  } else if(uip_poll()) {
99  if(pingeron && etimer_expired(&etimer) && packet_count > 0) {
100  --packet_count;
101  d->id = place_id;
102  d->pingpong = PING;
103  d->seqno = uip_htons(sent_seqno);
104  ++sent_seqno;
105  uip_udp_send(sizeof(struct data));
107  leds_toggle(LEDS_GREEN);
108  }
109 
110  if(packet_count == 0) {
111  pingeron = 0;
112  }
113  }
114 }
115 /*---------------------------------------------------------------------------*/
116 static
117 PT_THREAD(config_thread(struct pt *pt, process_event_t ev, process_data_t data))
118 {
119  static struct etimer pushtimer;
120  static int counter;
121 
122  PT_BEGIN(pt);
123 
124 
125  while(1) {
126 
127  PT_WAIT_UNTIL(pt, ev == sensors_event && data == &button_sensor);
128 
129  beep();
130 
131  leds_on(LEDS_YELLOW);
132 
133  etimer_set(&pushtimer, CLOCK_SECOND);
134  for(counter = 0; !etimer_expired(&pushtimer); ++counter) {
135  etimer_restart(&pushtimer);
136  PT_YIELD_UNTIL(pt, (ev == sensors_event && data == &button_sensor) ||
137  etimer_expired(&pushtimer));
138  }
139 
140  place_id = counter;
141 
142  beep_quick(place_id);
143 
144  pingeron = 1;
145 
146  packet_count = 20;
147 
149 
150  leds_off(LEDS_YELLOW);
151 
152  leds_on(LEDS_RED);
153  PT_WAIT_UNTIL(pt, packet_count == 0);
154 
155  pingeron = 0;
156  leds_off(LEDS_RED);
157  }
158 
159  PT_END(pt);
160 }
161 /*---------------------------------------------------------------------------*/
162 PROCESS_THREAD(pinger, ev, data)
163 {
164  static struct pt config_pt;
165 
166  PROCESS_BEGIN();
167 
168  pingeron = 0;
169 
170  conn = udp_broadcast_new(UIP_HTONS(PORT), NULL);
171 
172  PT_INIT(&config_pt);
173 
174  button_sensor.configure(SENSORS_ACTIVE, 1);
175 
176 
177  while(1) {
178 
179  config_thread(&config_pt, ev, data);
180 
182 
183  printf("Event %d\n", ev);
184 
185  beep();
186 
187  if(ev == tcpip_event) {
188  udp_appcall(data);
189  }
190 
191  if(ev == PROCESS_EVENT_TIMER && etimer_expired(&etimer)) {
192  tcpip_poll_udp(conn);
193  }
194 
195  }
196 
197  PROCESS_END();
198 }
199 /*---------------------------------------------------------------------------*/