Contiki 3.x
unicast.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  * \file
35  * Single-hop unicast
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimeuc
42  * @{
43  */
44 
45 #include "net/rime/rime.h"
46 #include "net/rime/unicast.h"
47 #include <string.h>
48 
49 static const struct packetbuf_attrlist attributes[] =
50  {
51  UNICAST_ATTRIBUTES
52  PACKETBUF_ATTR_LAST
53  };
54 
55 #define DEBUG 0
56 #if DEBUG
57 #include <stdio.h>
58 #define PRINTF(...) printf(__VA_ARGS__)
59 #else
60 #define PRINTF(...)
61 #endif
62 
63 /*---------------------------------------------------------------------------*/
64 static void
65 recv_from_broadcast(struct broadcast_conn *broadcast, const linkaddr_t *from)
66 {
67  struct unicast_conn *c = (struct unicast_conn *)broadcast;
68 
69  PRINTF("%d.%d: uc: recv_from_broadcast, receiver %d.%d\n",
71  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
72  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]);
73  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &linkaddr_node_addr)) {
74  if(c->u->recv) {
75  c->u->recv(c, from);
76  }
77  }
78 }
79 /*---------------------------------------------------------------------------*/
80 static void
81 sent_by_broadcast(struct broadcast_conn *broadcast, int status, int num_tx)
82 {
83  struct unicast_conn *c = (struct unicast_conn *)broadcast;
84 
85  PRINTF("%d.%d: uc: sent_by_broadcast, receiver %d.%d\n",
87  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
88  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]);
89 
90  if(c->u->sent) {
91  c->u->sent(c, status, num_tx);
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 static const struct broadcast_callbacks uc = {recv_from_broadcast,
96  sent_by_broadcast};
97 /*---------------------------------------------------------------------------*/
98 void
99 unicast_open(struct unicast_conn *c, uint16_t channel,
100  const struct unicast_callbacks *u)
101 {
102  broadcast_open(&c->c, channel, &uc);
103  c->u = u;
104  channel_set_attributes(channel, attributes);
105 }
106 /*---------------------------------------------------------------------------*/
107 void
108 unicast_close(struct unicast_conn *c)
109 {
110  broadcast_close(&c->c);
111 }
112 /*---------------------------------------------------------------------------*/
113 int
114 unicast_send(struct unicast_conn *c, const linkaddr_t *receiver)
115 {
116  PRINTF("%d.%d: unicast_send to %d.%d\n",
118  receiver->u8[0], receiver->u8[1]);
119  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, receiver);
120  return broadcast_send(&c->c);
121 }
122 /*---------------------------------------------------------------------------*/
123 /** @} */
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Header file for Rime&#39;s single-hop unicast
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
Definition: broadcast.c:105
Header file for the Rime stack
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
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
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