Contiki 3.x
polite.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  * Polite Anonymous best effort local area BroadCast (polite)
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimepolite
42  * @{
43  */
44 
45 #include "net/rime/rime.h"
46 #include "net/rime/polite.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 
60 /*---------------------------------------------------------------------------*/
61 static void
62 recv(struct abc_conn *abc)
63 {
64  struct polite_conn *c = (struct polite_conn *)abc;
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 do not send out
70  packet. */
71  queuebuf_free(c->q);
72  c->q = NULL;
73  ctimer_stop(&c->t);
74  if(c->cb->dropped) {
75  c->cb->dropped(c);
76  }
77  }
78  if(c->cb->recv) {
79  c->cb->recv(c);
80  }
81 }
82 /*---------------------------------------------------------------------------*/
83 static void
84 sent(struct abc_conn *c, int status, int num_tx)
85 {
86 
87 }
88 /*---------------------------------------------------------------------------*/
89 static void
90 send(void *ptr)
91 {
92  struct polite_conn *c = ptr;
93 
94  if(c->q != NULL) {
95  queuebuf_to_packetbuf(c->q);
96  queuebuf_free(c->q);
97  c->q = NULL;
98  abc_send(&c->c);
99  if(c->cb->sent) {
100  c->cb->sent(c);
101  }
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 static const struct abc_callbacks abc = { recv, sent };
106 /*---------------------------------------------------------------------------*/
107 void
108 polite_open(struct polite_conn *c, uint16_t channel,
109  const struct polite_callbacks *cb)
110 {
111  abc_open(&c->c, channel, &abc);
112  c->cb = cb;
113 }
114 /*---------------------------------------------------------------------------*/
115 void
117 {
118  abc_close(&c->c);
119  ctimer_stop(&c->t);
120  if(c->q != NULL) {
121  queuebuf_free(c->q);
122  c->q = NULL;
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 int
127 polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
128 {
129  if(c->q != NULL) {
130  /* If we are already about to send a packet, we cancel the old one. */
131  queuebuf_free(c->q);
132  }
133  c->hdrsize = hdrsize;
134  c->q = queuebuf_new_from_packetbuf();
135  if(c->q != NULL) {
136  ctimer_set(&c->t, interval / 2 + (random_rand() % (interval / 2)), send, c);
137  return 1;
138  }
139  return 0;
140 }
141 /*---------------------------------------------------------------------------*/
142 void
144 {
145  ctimer_stop(&c->t);
146  if(c->q != NULL) {
147  queuebuf_free(c->q);
148  c->q = NULL;
149  }
150 }
151 /*---------------------------------------------------------------------------*/
152 /** @} */
int abc_send(struct abc_conn *c)
Send an anonymous best-effort broadcast packet.
Definition: abc.c:79
int polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on a polite connection.
Definition: polite.c:127
void(* dropped)(struct polite_conn *c)
Called when a packet is dropped because a packet was heard from a neighbor.
Definition: polite.h:127
#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
Header file for Polite Anonymous best effort local Broadcast (polite)
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void polite_open(struct polite_conn *c, uint16_t channel, const struct polite_callbacks *cb)
Open a polite connection.
Definition: polite.c:108
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void(* sent)(struct polite_conn *c)
Called when a packet is sent on the connection.
Definition: polite.h:121
An opaque structure with no user-visible elements that holds the state of a polite connection...
Definition: polite.h:134
void(* recv)(struct abc_conn *ptr)
Called when a packet has been received by the abc module.
Definition: abc.h:74
void polite_cancel(struct polite_conn *c)
Cancel a pending packet.
Definition: polite.c:143
void abc_close(struct abc_conn *c)
Close an abc connection.
Definition: abc.c:73
void abc_open(struct abc_conn *c, uint16_t channelno, const struct abc_callbacks *callbacks)
Set up an anonymous best-effort broadcast connection.
Definition: abc.c:64
void polite_close(struct polite_conn *c)
Close a polite connection.
Definition: polite.c:116
void(* recv)(struct polite_conn *c)
Called when a packet is received on the connection.
Definition: polite.h:116
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
A structure with callback functions for a polite connection.
Definition: polite.h:112
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
Callback structure for abc.
Definition: abc.h:72