Contiki 2.6

radio-test.c

00001 /*
00002  * Copyright (c) 2006, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * $Id: radio-test.c,v 1.3 2010/10/19 18:29:05 adamdunkels Exp $
00030  *
00031  * -----------------------------------------------------------------
00032  *
00033  * Author  : Adam Dunkels, Joakim Eriksson, Niclas Finne
00034  * Created : 2006-03-07
00035  * Updated : $Date: 2010/10/19 18:29:05 $
00036  *           $Revision: 1.3 $
00037  *
00038  * Simple application to indicate connectivity between two nodes:
00039  *
00040  * - Red led indicates a packet sent via radio (one packet sent each second)
00041  * - Yellow led indicates that this node can hear the other node but not
00042  *   necessary vice versa (unidirectional communication).
00043  * - Green led indicates that both nodes can communicate with each
00044  *   other (bidirectional communication)
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   /* Initialize the indicators */
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         /* send packet */
00124         memcpy(uip_appdata, HEADER, sizeof(HEADER));
00125         ((char *)uip_appdata)[sizeof(HEADER)] = recv.onoff;
00126         /* send arbitrary data to fill the packet size */
00127         uip_send(uip_appdata, PACKET_SIZE);
00128 
00129         set(&flash, ON);
00130       }
00131 
00132       if(uip_newdata()) {
00133         /* packet received */
00134         if(uip_datalen() < PACKET_SIZE
00135            || strncmp((char *)uip_appdata, HEADER, sizeof(HEADER))) {
00136           /* invalid message */
00137           leds_blink();
00138 
00139         } else {
00140           set(&recv, ON);
00141           set(&other, ((char *)uip_appdata)[sizeof(HEADER)] ? ON : OFF);
00142 
00143           /* synchronize the sending to keep the nodes from sending
00144              simultaneously */
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 /*---------------------------------------------------------------------*/