Contiki 2.5
broadcast.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimebroadcast
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: broadcast.c,v 1.3 2010/02/23 18:38:05 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Identified best-effort local area broadcast (broadcast)
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "contiki-net.h"
47 #include <string.h>
48 
49 static const struct packetbuf_attrlist attributes[] =
50  {
51  BROADCAST_ATTRIBUTES PACKETBUF_ATTR_LAST
52  };
53 
54 #define DEBUG 0
55 #if DEBUG
56 #include <stdio.h>
57 #define PRINTF(...) printf(__VA_ARGS__)
58 #else
59 #define PRINTF(...)
60 #endif
61 
62 /*---------------------------------------------------------------------------*/
63 static void
64 recv_from_abc(struct abc_conn *bc)
65 {
66  rimeaddr_t sender;
67  struct broadcast_conn *c = (struct broadcast_conn *)bc;
68 
69  rimeaddr_copy(&sender, packetbuf_addr(PACKETBUF_ADDR_SENDER));
70 
71  PRINTF("%d.%d: broadcast: from %d.%d\n",
73  sender.u8[0], sender.u8[1]);
74  c->u->recv(c, &sender);
75 }
76 /*---------------------------------------------------------------------------*/
77 static void
78 sent_by_abc(struct abc_conn *bc, int status, int num_tx)
79 {
80  struct broadcast_conn *c = (struct broadcast_conn *)bc;
81 
82  PRINTF("%d.%d: sent to %d.%d status %d num_tx %d\n",
84  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
85  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
86  status, num_tx);
87  if(c->u->sent) {
88  c->u->sent(c, status, num_tx);
89  }
90 }
91 /*---------------------------------------------------------------------------*/
92 static const struct abc_callbacks broadcast = {recv_from_abc, sent_by_abc};
93 /*---------------------------------------------------------------------------*/
94 void
95 broadcast_open(struct broadcast_conn *c, uint16_t channel,
96  const struct broadcast_callbacks *u)
97 {
98  abc_open(&c->c, channel, &broadcast);
99  c->u = u;
100  channel_set_attributes(channel, attributes);
101 }
102 /*---------------------------------------------------------------------------*/
103 void
104 broadcast_close(struct broadcast_conn *c)
105 {
106  abc_close(&c->c);
107 }
108 /*---------------------------------------------------------------------------*/
109 int
110 broadcast_send(struct broadcast_conn *c)
111 {
112  PRINTF("%d.%d: broadcast_send\n",
114  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &rimeaddr_node_addr);
115  return abc_send(&c->c);
116 }
117 /*---------------------------------------------------------------------------*/
118 /** @} */