Contiki 2.6

testuaodv.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: testuaodv.c,v 1.4 2010/10/19 18:29:05 adamdunkels Exp $
00030  */
00031 
00032 #include <stdlib.h>
00033 #include "net/uip.h"
00034 #include "dev/button-sensor.h"
00035 #include "dev/leds.h"
00036 
00037 #include "net/uaodv.h"
00038 #include "net/uaodv-rt.h"
00039 
00040 #include <stdio.h>
00041 
00042 #define COOJA_PORT 1234
00043 
00044 PROCESS(test_uaodv_process, "uIP uAODV test process");
00045 AUTOSTART_PROCESSES(&uaodv_process, &test_uaodv_process);
00046 
00047 static struct uip_udp_conn *out_conn;
00048 static struct uip_udp_conn *in_conn;
00049 /*---------------------------------------------------------------------*/
00050 PROCESS_THREAD(test_uaodv_process, ev, data)
00051 {
00052   static uip_ipaddr_t addr;
00053 
00054   PROCESS_BEGIN();
00055 
00056   printf("uIP uAODV test process started\n");
00057 
00058   uip_ipaddr(&addr, 0,0,0,0);
00059   in_conn = udp_new(&addr, UIP_HTONS(0), NULL);
00060   uip_udp_bind(in_conn, UIP_HTONS(COOJA_PORT));
00061 
00062   uip_ipaddr(&addr, 10,10,10,4);
00063   out_conn = udp_new(&addr, UIP_HTONS(COOJA_PORT), NULL);
00064 
00065   button_sensor.configure(SENSORS_ACTIVE, 1);
00066 
00067   while(1) {
00068     PROCESS_WAIT_EVENT();
00069 
00070     if(ev == sensors_event && data == &button_sensor) {
00071       struct uaodv_rt_entry *route;
00072 
00073       uip_ipaddr(&addr, 10,10,10,4);
00074       route = uaodv_rt_lookup_any(&addr);
00075       if (route == NULL || route->is_bad) {
00076         printf("%d.%d.%d.%d: lookup %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr));
00077         uaodv_request_route_to(&addr);
00078       } else {
00079         printf("%d.%d.%d.%d: send to %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr));
00080         tcpip_poll_udp(out_conn);
00081         PROCESS_WAIT_UNTIL(ev == tcpip_event && uip_poll());
00082         uip_send("cooyah COOJA", 12);
00083       }
00084     }
00085 
00086     if(ev == tcpip_event && uip_newdata()) {
00087       ((char*) uip_appdata)[uip_datalen()] = 0;
00088       printf("data received from %d.%d.%d.%d: %s\n",
00089           uip_ipaddr_to_quad(&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->srcipaddr),
00090           (char *)uip_appdata);
00091       leds_toggle(LEDS_ALL);
00092     }
00093   }
00094 
00095   PROCESS_END();
00096 }
00097 /*---------------------------------------------------------------------*/