Contiki 2.5
radio-test.c
1 /*
2  * Copyright (c) 2006, 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  * $Id: radio-test.c,v 1.3 2010/10/19 18:29:05 adamdunkels Exp $
30  *
31  * -----------------------------------------------------------------
32  *
33  * Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
34  * Created : 2006-03-07
35  * Updated : $Date: 2010/10/19 18:29:05 $
36  * $Revision: 1.3 $
37  *
38  * Simple application to indicate connectivity between two nodes:
39  *
40  * - Red led indicates a packet sent via radio (one packet sent each second)
41  * - Yellow led indicates that this node can hear the other node but not
42  * necessary vice versa (unidirectional communication).
43  * - Green led indicates that both nodes can communicate with each
44  * other (bidirectional communication)
45  */
46 
47 #include "contiki-esb.h"
48 #include <string.h>
49 
50 PROCESS(radio_test_process, "Radio test");
51 AUTOSTART_PROCESSES(&radio_test_process);
52 
53 #define ON 1
54 #define OFF 0
55 
56 #define HEADER "RTST"
57 #define PACKET_SIZE 20
58 #define PORT 2345
59 
60 struct indicator {
61  int onoff;
62  int led;
63  clock_time_t interval;
64  struct etimer timer;
65 };
66 
67 /*---------------------------------------------------------------------*/
68 static void
69 set(struct indicator *indicator, int onoff) {
70  if(indicator->onoff ^ onoff) {
71  indicator->onoff = onoff;
72  if(onoff) {
73  leds_on(indicator->led);
74  } else {
75  leds_off(indicator->led);
76  }
77  }
78  if(onoff) {
79  etimer_set(&indicator->timer, indicator->interval);
80  }
81 }
82 /*---------------------------------------------------------------------*/
83 PROCESS_THREAD(radio_test_process, ev, data)
84 {
85  static struct etimer send_timer;
86  static struct uip_udp_conn *conn;
87  static struct indicator recv, other, flash;
88 
89  PROCESS_BEGIN();
90 
91  /* Initialize the indicators */
92  recv.onoff = other.onoff = flash.onoff = OFF;
93  recv.interval = other.interval = CLOCK_SECOND;
94  flash.interval = 1;
95  recv.led = LEDS_YELLOW;
96  other.led = LEDS_GREEN;
97  flash.led = LEDS_RED;
98 
99  conn = udp_broadcast_new(UIP_HTONS(PORT), NULL);
100  etimer_set(&send_timer, CLOCK_SECOND);
101 
102  while(1) {
104 
105  if(ev == PROCESS_EVENT_TIMER) {
106  if(data == &send_timer) {
107  etimer_reset(&send_timer);
108  tcpip_poll_udp(conn);
109 
110  } else if(data == &other.timer) {
111  set(&other, OFF);
112 
113  } else if(data == &recv.timer) {
114  set(&recv, OFF);
115 
116  } else if(data == &flash.timer) {
117  set(&flash, OFF);
118  }
119 
120  } else if(ev == tcpip_event) {
121 
122  if(uip_poll()) {
123  /* send packet */
124  memcpy(uip_appdata, HEADER, sizeof(HEADER));
125  ((char *)uip_appdata)[sizeof(HEADER)] = recv.onoff;
126  /* send arbitrary data to fill the packet size */
127  uip_send(uip_appdata, PACKET_SIZE);
128 
129  set(&flash, ON);
130  }
131 
132  if(uip_newdata()) {
133  /* packet received */
134  if(uip_datalen() < PACKET_SIZE
135  || strncmp((char *)uip_appdata, HEADER, sizeof(HEADER))) {
136  /* invalid message */
137  leds_blink();
138 
139  } else {
140  set(&recv, ON);
141  set(&other, ((char *)uip_appdata)[sizeof(HEADER)] ? ON : OFF);
142 
143  /* synchronize the sending to keep the nodes from sending
144  simultaneously */
145  etimer_set(&send_timer, CLOCK_SECOND);
146  etimer_adjust(&send_timer, - (int) (CLOCK_SECOND >> 1));
147 
148  beep();
149  }
150  }
151  }
152  }
153  PROCESS_END();
154 }
155 /*---------------------------------------------------------------------*/