Contiki 3.x
ipolite.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 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  * Ipolite Anonymous best effort local area BroadCast (ipolite)
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimeipolite
42  * @{
43  */
44 
45 #include "sys/cc.h"
46 #include "net/rime/rime.h"
47 #include "net/rime/ipolite.h"
48 #include "lib/random.h"
49 
50 #include <string.h>
51 
52 #define DEBUG 0
53 #if DEBUG
54 #include <stdio.h>
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 
60 /*---------------------------------------------------------------------------*/
61 static void
62 recv(struct broadcast_conn *broadcast, const linkaddr_t *from)
63 {
64  struct ipolite_conn *c = (struct ipolite_conn *)broadcast;
65  if(c->q != NULL &&
66  packetbuf_datalen() == queuebuf_datalen(c->q) &&
67  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
68  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
69  /* We received a copy of our own packet, so we increase the
70  duplicate counter. If it reaches its maximum, do not send out
71  our packet. */
72  c->dups++;
73  if(c->dups == c->maxdups) {
74  queuebuf_free(c->q);
75  c->q = NULL;
76  ctimer_stop(&c->t);
77  if(c->cb->dropped) {
78  c->cb->dropped(c);
79  }
80  }
81  }
82  if(c->cb->recv) {
83  c->cb->recv(c, from);
84  }
85 }
86 /*---------------------------------------------------------------------------*/
87 static void
88 sent(struct broadcast_conn *bc, int status, int num_tx)
89 {
90 
91 }
92 /*---------------------------------------------------------------------------*/
93 static void
94 send(void *ptr)
95 {
96  struct ipolite_conn *c = ptr;
97 
98  PRINTF("%d.%d: ipolite: send queuebuf %p\n",
100  c->q);
101 
102  if(c->q != NULL) {
103  queuebuf_to_packetbuf(c->q);
104  queuebuf_free(c->q);
105  c->q = NULL;
106  broadcast_send(&c->c);
107  if(c->cb->sent) {
108  c->cb->sent(c);
109  }
110  }
111 }
112 /*---------------------------------------------------------------------------*/
113 static const struct broadcast_callbacks broadcast = { recv, sent };
114 /*---------------------------------------------------------------------------*/
115 void
116 ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups,
117  const struct ipolite_callbacks *cb)
118 {
119  broadcast_open(&c->c, channel, &broadcast);
120  c->cb = cb;
121  c->maxdups = dups;
122  PRINTF("ipolite open channel %d\n", channel);
123 }
124 /*---------------------------------------------------------------------------*/
125 void
127 {
128  broadcast_close(&c->c);
129  ctimer_stop(&c->t);
130  if(c->q != NULL) {
131  queuebuf_free(c->q);
132  c->q = NULL;
133  }
134 }
135 /*---------------------------------------------------------------------------*/
136 int
137 ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
138 {
139  if(c->q != NULL) {
140  /* If we are already about to send a packet, we cancel the old one. */
141  PRINTF("%d.%d: ipolite_send: cancel old send\n",
143  queuebuf_free(c->q);
144  c->q = NULL;
145  ctimer_stop(&c->t);
146  }
147  c->dups = 0;
148  c->hdrsize = hdrsize;
149  if(interval == 0) {
150  PRINTF("%d.%d: ipolite_send: interval 0\n",
152  if(broadcast_send(&c->c)) {
153  if(c->cb->sent) {
154  c->cb->sent(c);
155  }
156  return 1;
157  }
158 
159  } else {
160  c->q = queuebuf_new_from_packetbuf();
161  if(c->q != NULL) {
162  ctimer_set(&c->t,
163  interval / 2 + (random_rand() % (interval / 2)),
164  send, c);
165  return 1;
166  }
167  PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
169  }
170  return 0;
171 }
172 /*---------------------------------------------------------------------------*/
173 void
175 {
176  ctimer_stop(&c->t);
177  if(c->q != NULL) {
178  queuebuf_free(c->q);
179  c->q = NULL;
180  }
181 }
182 /*---------------------------------------------------------------------------*/
183 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
Default definitions of C compiler quirk work-arounds.
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
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
Definition: broadcast.c:105
Callback structure for broadcast.
Definition: broadcast.h:80
void(* dropped)(struct ipolite_conn *c)
Called when a packet is dropped because a packet was heard from a neighbor.
Definition: ipolite.h:128
void(* recv)(struct ipolite_conn *c, const linkaddr_t *from)
Called when a packet is received on the connection.
Definition: ipolite.h:117
void ipolite_close(struct ipolite_conn *c)
Close an ipolite connection.
Definition: ipolite.c:126
A structure with callback functions for an ipolite connection.
Definition: ipolite.h:113
void(* sent)(struct ipolite_conn *c)
Called when a packet is sent on the connection.
Definition: ipolite.h:122
void(* recv)(struct broadcast_conn *ptr, const linkaddr_t *sender)
Called when a packet has been received by the broadcast module.
Definition: broadcast.h:82
Header file for the Rime stack
#define NULL
The null pointer.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
An opaque structure with no user-visible elements that holds the state of an ipolite connection...
Definition: ipolite.h:135
int broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
Definition: broadcast.c:111
Header file for Ipolite best effort local Broadcast (ipolite)
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
void ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups, const struct ipolite_callbacks *cb)
Open an ipolite connection.
Definition: ipolite.c:116
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ipolite_cancel(struct ipolite_conn *c)
Cancel a pending packet.
Definition: ipolite.c:174
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
int ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on an ipolite connection.
Definition: ipolite.c:137