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