pinger.c

00001 /*
00002  * Copyright (c) 2005, 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  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: pinger.c,v 1.3 2010/10/19 18:29:05 adamdunkels Exp $
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   /*  char buf[50];*/
00074   d = (struct data *)uip_appdata;
00075   
00076   if(uip_newdata()) {
00077     leds_toggle(LEDS_YELLOW);
00078     /*    beep();*/
00079 
00080     /*    if(uip_htons(d->seqno) != last_seqno + 1) {
00081       leds_toggle(LEDS_RED);
00082       beep_quick(2);
00083       }*/    
00084     /*    last_seqno = uip_htons(d->seqno);*/
00085     /*    uip_udp_send(sizeof(struct data));*/
00086     /*    snprintf(buf, sizeof(buf), "Packet received id %d signal %u\n",
00087              d->id, tr1001_sstrength());
00088 
00089              rs232_print(buf);*/
00090     /*    if(d->pingpong == PING) {
00091       d->pingpong = PONG;
00092     } else {
00093       d->pingpong = PING;
00094       d->seqno = uip_htons(uip_htons(d->seqno) + 1);
00095       }*/
00096     /*    uip_udp_send(sizeof(struct data));
00097           timer_restart(&timer);*/
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 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:43 2011 for Contiki 2.5 by  doxygen 1.6.1