rime-udp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, 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: rime-udp.c,v 1.7 2010/10/19 18:29:04 adamdunkels Exp $
00032  */
00033 
00034 /**
00035  * \file
00036  *         A MAC protocol using UDP over IPv6.
00037  * \author
00038  *         Nicolas Tsiftes <nvt@sics.se>
00039  */
00040 
00041 #include <string.h>
00042 
00043 #include "net/uip.h"
00044 #include "net/uip-udp-packet.h"
00045 #include "net/uip-netif.h"
00046 #include "net/rime/rime-udp.h"
00047 #include "net/packetbuf.h"
00048 
00049 #define DEBUG 0
00050 #if DEBUG
00051 #include <stdio.h>
00052 #define PRINTF(...) printf(__VA_ARGS__)
00053 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
00054 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
00055 #else
00056 #define PRINTF(...)
00057 #define PRINT6ADDR(addr)
00058 #define PRINTLLADDR(addr)
00059 #endif
00060 
00061 #ifndef RIME_CONF_UDP_PORT
00062 #define RIME_UDP_PORT           9508
00063 #else
00064 #define RIME_UDP_PORT           RIME_CONF_UDP_PORT
00065 #endif /* RIME_CONF_UDP_PORT */
00066 
00067 static struct uip_udp_conn *broadcast_conn;
00068 static struct uip_udp_conn *unicast_conn;
00069 
00070 static void (* receiver_callback)(const struct mac_driver *);
00071 
00072 PROCESS(rime_udp_process, "Rime over UDP process");
00073 
00074 PROCESS_THREAD(rime_udp_process, ev, data)
00075 {
00076   static uip_ipaddr_t ipaddr;
00077 
00078   PROCESS_BEGIN();
00079 
00080   broadcast_conn = udp_broadcast_new(UIP_HTONS(RIME_UDP_PORT), NULL);
00081   if(broadcast_conn == NULL) {
00082     PRINTF("rime-udp: Failed to allocate a broadcast connection!\n");
00083   }
00084 
00085   uip_create_unspecified(&ipaddr);
00086   unicast_conn = udp_new(&ipaddr, UIP_HTONS(RIME_UDP_PORT), NULL);
00087   if(unicast_conn == NULL) {
00088     PRINTF("rime-udp: Failed to allocate a unicast connection!\n");
00089   }
00090 
00091   udp_bind(unicast_conn, UIP_HTONS(RIME_UDP_PORT));
00092 
00093   while(1) {
00094     PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
00095     if(uip_newdata()) {
00096       packetbuf_clear();
00097       memmove(packetbuf_hdrptr(), uip_appdata, uip_datalen());
00098       PRINTF("rime-udp: received %d bytes\n", uip_datalen());
00099       receiver_callback(&rime_udp_driver);
00100     }
00101   }
00102 
00103   PROCESS_END();
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 static void
00107 send_packet(mac_callback_t sent_callback, void *ptr)
00108 {
00109   const rimeaddr_t *addr;
00110 
00111   addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
00112   PRINTF("rime-udp: Sending %d bytes to %d.%d\n", packetbuf_totlen(),
00113          addr->u8[0], addr->u8[1]);
00114 
00115   if(rimeaddr_cmp(&rimeaddr_null, addr)) {
00116     uip_udp_packet_send(broadcast_conn,
00117                         packetbuf_hdrptr(), packetbuf_totlen());
00118     mac_call_sent_callback(sent_callback, ptr, MAC_TX_OK, 1);
00119   } else {
00120     uip_ip6addr(&unicast_conn->ripaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
00121     uip_netif_addr_autoconf_set(&unicast_conn->ripaddr, (uip_lladdr_t *)addr);
00122     uip_udp_packet_send(unicast_conn,
00123                         packetbuf_hdrptr(), packetbuf_totlen());
00124     uip_create_unspecified(&unicast_conn->ripaddr);
00125   }
00126   return;
00127 }
00128 /*---------------------------------------------------------------------------*/
00129 static int
00130 input_packet(void)
00131 {
00132   packetbuf_set_datalen(uip_datalen());
00133   return uip_datalen();
00134 }
00135 /*---------------------------------------------------------------------------*/
00136 static void
00137 set_receive_function(void (* recv)(const struct mac_driver *))
00138 {
00139   receiver_callback = recv;
00140 }
00141 /*---------------------------------------------------------------------------*/
00142 static int
00143 on(void)
00144 {
00145   return 1;
00146 }
00147 /*---------------------------------------------------------------------------*/
00148 static int
00149 off(int keep_radio_on)
00150 {
00151   return 0;
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 static unsigned short
00155 check_interval(void)
00156 {
00157   return 0;
00158 }
00159 /*---------------------------------------------------------------------------*/
00160 static int
00161 init(void)
00162 {
00163   process_start(&rime_udp_process, NULL);
00164   return 1;
00165 }
00166 /*---------------------------------------------------------------------------*/
00167 const struct mac_driver rime_udp_driver = {
00168   "rime-udp",
00169   init,
00170   send_packet,
00171   input_packet,
00172   on,
00173   off,
00174   check_interval,
00175 };
00176 /*---------------------------------------------------------------------------*/

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