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 "net/rime/rime.h"
46 #include "net/rime/ipolite.h"
47 #include "lib/random.h"
48 
49 #include <string.h>
50 
51 #ifndef MAX
52 #define MAX(a, b) ((a) > (b)? (a) : (b))
53 #endif /* MAX */
54 
55 #ifndef MIN
56 #define MIN(a, b) ((a) < (b)? (a) : (b))
57 #endif /* MIN */
58 
59 #define DEBUG 0
60 #if DEBUG
61 #include <stdio.h>
62 #define PRINTF(...) printf(__VA_ARGS__)
63 #else
64 #define PRINTF(...)
65 #endif
66 
67 /*---------------------------------------------------------------------------*/
68 static void
69 recv(struct broadcast_conn *broadcast, const linkaddr_t *from)
70 {
71  struct ipolite_conn *c = (struct ipolite_conn *)broadcast;
72  if(c->q != NULL &&
73  packetbuf_datalen() == queuebuf_datalen(c->q) &&
74  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
75  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
76  /* We received a copy of our own packet, so we increase the
77  duplicate counter. If it reaches its maximum, do not send out
78  our packet. */
79  c->dups++;
80  if(c->dups == c->maxdups) {
81  queuebuf_free(c->q);
82  c->q = NULL;
83  ctimer_stop(&c->t);
84  if(c->cb->dropped) {
85  c->cb->dropped(c);
86  }
87  }
88  }
89  if(c->cb->recv) {
90  c->cb->recv(c, from);
91  }
92 }
93 /*---------------------------------------------------------------------------*/
94 static void
95 sent(struct broadcast_conn *bc, int status, int num_tx)
96 {
97 
98 }
99 /*---------------------------------------------------------------------------*/
100 static void
101 send(void *ptr)
102 {
103  struct ipolite_conn *c = ptr;
104 
105  PRINTF("%d.%d: ipolite: send queuebuf %p\n",
107  c->q);
108 
109  if(c->q != NULL) {
110  queuebuf_to_packetbuf(c->q);
111  queuebuf_free(c->q);
112  c->q = NULL;
113  broadcast_send(&c->c);
114  if(c->cb->sent) {
115  c->cb->sent(c);
116  }
117  }
118 }
119 /*---------------------------------------------------------------------------*/
120 static const struct broadcast_callbacks broadcast = { recv, sent };
121 /*---------------------------------------------------------------------------*/
122 void
123 ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups,
124  const struct ipolite_callbacks *cb)
125 {
126  broadcast_open(&c->c, channel, &broadcast);
127  c->cb = cb;
128  c->maxdups = dups;
129  PRINTF("ipolite open channel %d\n", channel);
130 }
131 /*---------------------------------------------------------------------------*/
132 void
134 {
135  broadcast_close(&c->c);
136  ctimer_stop(&c->t);
137  if(c->q != NULL) {
138  queuebuf_free(c->q);
139  c->q = NULL;
140  }
141 }
142 /*---------------------------------------------------------------------------*/
143 int
144 ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
145 {
146  if(c->q != NULL) {
147  /* If we are already about to send a packet, we cancel the old one. */
148  PRINTF("%d.%d: ipolite_send: cancel old send\n",
150  queuebuf_free(c->q);
151  c->q = NULL;
152  ctimer_stop(&c->t);
153  }
154  c->dups = 0;
155  c->hdrsize = hdrsize;
156  if(interval == 0) {
157  PRINTF("%d.%d: ipolite_send: interval 0\n",
159  if(broadcast_send(&c->c)) {
160  if(c->cb->sent) {
161  c->cb->sent(c);
162  }
163  return 1;
164  }
165 
166  } else {
167  c->q = queuebuf_new_from_packetbuf();
168  if(c->q != NULL) {
169  ctimer_set(&c->t,
170  interval / 2 + (random_rand() % (interval / 2)),
171  send, c);
172  return 1;
173  }
174  PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
176  }
177  return 0;
178 }
179 /*---------------------------------------------------------------------------*/
180 void
182 {
183  ctimer_stop(&c->t);
184  if(c->q != NULL) {
185  queuebuf_free(c->q);
186  c->q = NULL;
187  }
188 }
189 /*---------------------------------------------------------------------------*/
190 /** @} */
A structure with callback functions for an ipolite connection.
Definition: ipolite.h:113
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
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
void ipolite_cancel(struct ipolite_conn *c)
Cancel a pending packet.
Definition: ipolite.c:181
#define NULL
The null pointer.
void(* recv)(struct ipolite_conn *c, const linkaddr_t *from)
Called when a packet is received on the connection.
Definition: ipolite.h:117
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void ipolite_close(struct ipolite_conn *c)
Close an ipolite connection.
Definition: ipolite.c:133
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:123
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void(* dropped)(struct ipolite_conn *c)
Called when a packet is dropped because a packet was heard from a neighbor.
Definition: ipolite.h:128
Callback structure for broadcast.
Definition: broadcast.h:80
Header file for Ipolite best effort local Broadcast (ipolite)
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
void(* sent)(struct ipolite_conn *c)
Called when a packet is sent on the connection.
Definition: ipolite.h:122
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
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
int ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on an ipolite connection.
Definition: ipolite.c:144
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47