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