broadcast.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimebroadcast
00003  * @{
00004  */
00005 
00006 /*
00007  * Copyright (c) 2006, Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the Institute nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * This file is part of the Contiki operating system.
00035  *
00036  * $Id: broadcast.c,v 1.3 2010/02/23 18:38:05 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Identified best-effort local area broadcast (broadcast)
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "contiki-net.h"
00047 #include <string.h>
00048 
00049 static const struct packetbuf_attrlist attributes[] =
00050   {
00051     BROADCAST_ATTRIBUTES PACKETBUF_ATTR_LAST
00052   };
00053 
00054 #define DEBUG 0
00055 #if DEBUG
00056 #include <stdio.h>
00057 #define PRINTF(...) printf(__VA_ARGS__)
00058 #else
00059 #define PRINTF(...)
00060 #endif
00061 
00062 /*---------------------------------------------------------------------------*/
00063 static void
00064 recv_from_abc(struct abc_conn *bc)
00065 {
00066   rimeaddr_t sender;
00067   struct broadcast_conn *c = (struct broadcast_conn *)bc;
00068 
00069   rimeaddr_copy(&sender, packetbuf_addr(PACKETBUF_ADDR_SENDER));
00070   
00071   PRINTF("%d.%d: broadcast: from %d.%d\n",
00072          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00073          sender.u8[0], sender.u8[1]);
00074   c->u->recv(c, &sender);
00075 }
00076 /*---------------------------------------------------------------------------*/
00077 static void
00078 sent_by_abc(struct abc_conn *bc, int status, int num_tx)
00079 {
00080   struct broadcast_conn *c = (struct broadcast_conn *)bc;
00081 
00082   PRINTF("%d.%d: sent to %d.%d status %d num_tx %d\n",
00083          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00084          packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
00085          packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
00086          status, num_tx);
00087   if(c->u->sent) {
00088     c->u->sent(c, status, num_tx);
00089   }
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 static const struct abc_callbacks broadcast = {recv_from_abc, sent_by_abc};
00093 /*---------------------------------------------------------------------------*/
00094 void
00095 broadcast_open(struct broadcast_conn *c, uint16_t channel,
00096           const struct broadcast_callbacks *u)
00097 {
00098   abc_open(&c->c, channel, &broadcast);
00099   c->u = u;
00100   channel_set_attributes(channel, attributes);
00101 }
00102 /*---------------------------------------------------------------------------*/
00103 void
00104 broadcast_close(struct broadcast_conn *c)
00105 {
00106   abc_close(&c->c);
00107 }
00108 /*---------------------------------------------------------------------------*/
00109 int
00110 broadcast_send(struct broadcast_conn *c)
00111 {
00112   PRINTF("%d.%d: broadcast_send\n",
00113          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00114   packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
00115   return abc_send(&c->c);
00116 }
00117 /*---------------------------------------------------------------------------*/
00118 /** @} */

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