Contiki 2.5
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  * $Id: unicast.c,v 1.4 2010/02/23 18:38:05 adamdunkels Exp $
38  */
39 
40 /**
41  * \file
42  * Single-hop unicast
43  * \author
44  * Adam Dunkels <adam@sics.se>
45  */
46 
47 #include "net/rime.h"
48 #include "net/rime/unicast.h"
49 #include <string.h>
50 
51 static const struct packetbuf_attrlist attributes[] =
52  {
53  UNICAST_ATTRIBUTES
54  PACKETBUF_ATTR_LAST
55  };
56 
57 #define DEBUG 0
58 #if DEBUG
59 #include <stdio.h>
60 #define PRINTF(...) printf(__VA_ARGS__)
61 #else
62 #define PRINTF(...)
63 #endif
64 
65 /*---------------------------------------------------------------------------*/
66 static void
67 recv_from_broadcast(struct broadcast_conn *broadcast, const rimeaddr_t *from)
68 {
69  struct unicast_conn *c = (struct unicast_conn *)broadcast;
70 
71  PRINTF("%d.%d: uc: recv_from_broadcast, receiver %d.%d\n",
73  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[0],
74  packetbuf_addr(PACKETBUF_ADDR_RECEIVER)->u8[1]);
75  if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_node_addr)) {
76  c->u->recv(c, from);
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: recv_from_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 rimeaddr_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 /** @} */