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 "sys/cc.h"
46 #include "net/rime/rime.h"
47 #include "net/rime/polite.h"
48 #include "lib/random.h"
49 
50 #include <string.h>
51 
52 /*---------------------------------------------------------------------------*/
53 static void
54 recv(struct abc_conn *abc)
55 {
56  struct polite_conn *c = (struct polite_conn *)abc;
57  if(c->q != NULL &&
58  packetbuf_datalen() == queuebuf_datalen(c->q) &&
59  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
60  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
61  /* We received a copy of our own packet, so we do not send out
62  packet. */
63  queuebuf_free(c->q);
64  c->q = NULL;
65  ctimer_stop(&c->t);
66  if(c->cb->dropped) {
67  c->cb->dropped(c);
68  }
69  }
70  if(c->cb->recv) {
71  c->cb->recv(c);
72  }
73 }
74 /*---------------------------------------------------------------------------*/
75 static void
76 sent(struct abc_conn *c, int status, int num_tx)
77 {
78 
79 }
80 /*---------------------------------------------------------------------------*/
81 static void
82 send(void *ptr)
83 {
84  struct polite_conn *c = ptr;
85 
86  if(c->q != NULL) {
87  queuebuf_to_packetbuf(c->q);
88  queuebuf_free(c->q);
89  c->q = NULL;
90  abc_send(&c->c);
91  if(c->cb->sent) {
92  c->cb->sent(c);
93  }
94  }
95 }
96 /*---------------------------------------------------------------------------*/
97 static const struct abc_callbacks abc = { recv, sent };
98 /*---------------------------------------------------------------------------*/
99 void
100 polite_open(struct polite_conn *c, uint16_t channel,
101  const struct polite_callbacks *cb)
102 {
103  abc_open(&c->c, channel, &abc);
104  c->cb = cb;
105 }
106 /*---------------------------------------------------------------------------*/
107 void
109 {
110  abc_close(&c->c);
111  ctimer_stop(&c->t);
112  if(c->q != NULL) {
113  queuebuf_free(c->q);
114  c->q = NULL;
115  }
116 }
117 /*---------------------------------------------------------------------------*/
118 int
119 polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
120 {
121  if(c->q != NULL) {
122  /* If we are already about to send a packet, we cancel the old one. */
123  queuebuf_free(c->q);
124  }
125  c->hdrsize = hdrsize;
126  c->q = queuebuf_new_from_packetbuf();
127  if(c->q != NULL) {
128  ctimer_set(&c->t, interval / 2 + (random_rand() % (interval / 2)), send, c);
129  return 1;
130  }
131  return 0;
132 }
133 /*---------------------------------------------------------------------------*/
134 void
136 {
137  ctimer_stop(&c->t);
138  if(c->q != NULL) {
139  queuebuf_free(c->q);
140  c->q = NULL;
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
An opaque structure with no user-visible elements that holds the state of a polite connection...
Definition: polite.h:134
Header file for Polite Anonymous best effort local Broadcast (polite)
Default definitions of C compiler quirk work-arounds.
void(* dropped)(struct polite_conn *c)
Called when a packet is dropped because a packet was heard from a neighbor.
Definition: polite.h:127
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:135
int abc_send(struct abc_conn *c)
Send an anonymous best-effort broadcast packet.
Definition: abc.c:79
void polite_close(struct polite_conn *c)
Close a polite connection.
Definition: polite.c:108
A structure with callback functions for a polite connection.
Definition: polite.h:112
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(* recv)(struct ipolite_conn *c, const linkaddr_t *from)
Called when a packet is received on the connection.
Definition: ipolite.h:117
void(* recv)(struct polite_conn *c)
Called when a packet is received on the connection.
Definition: polite.h:116
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
void(* sent)(struct polite_conn *c)
Called when a packet is sent on the connection.
Definition: polite.h:121
void polite_open(struct polite_conn *c, uint16_t channel, const struct polite_callbacks *cb)
Open a polite connection.
Definition: polite.c:100
Callback structure for abc.
Definition: abc.h:72
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
int polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on a polite connection.
Definition: polite.c:119
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void abc_close(struct abc_conn *c)
Close an abc connection.
Definition: abc.c:73