Contiki 2.5
polite.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimepolite
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2007, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  * $Id: polite.c,v 1.10 2010/02/23 18:38:05 adamdunkels Exp $
37  */
38 
39 /**
40  * \file
41  * Polite Anonymous best effort local area BroadCast (polite)
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "net/rime.h"
47 #include "net/rime/polite.h"
48 #include "lib/random.h"
49 
50 #include <string.h>
51 
52 #ifndef MAX
53 #define MAX(a,b) ((a) > (b)? (a) : (b))
54 #endif /* MAX */
55 
56 #ifndef MIN
57 #define MIN(a, b) ((a) < (b)? (a) : (b))
58 #endif /* MIN */
59 
60 
61 /*---------------------------------------------------------------------------*/
62 static void
63 recv(struct abc_conn *abc)
64 {
65  struct polite_conn *c = (struct polite_conn *)abc;
66  if(c->q != NULL &&
67  packetbuf_datalen() == queuebuf_datalen(c->q) &&
68  memcmp(packetbuf_dataptr(), queuebuf_dataptr(c->q),
69  MIN(c->hdrsize, packetbuf_datalen())) == 0) {
70  /* We received a copy of our own packet, so we do not send out
71  packet. */
72  queuebuf_free(c->q);
73  c->q = NULL;
74  ctimer_stop(&c->t);
75  if(c->cb->dropped) {
76  c->cb->dropped(c);
77  }
78  }
79  if(c->cb->recv) {
80  c->cb->recv(c);
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 static void
85 sent(struct abc_conn *c, int status, int num_tx)
86 {
87 
88 }
89 /*---------------------------------------------------------------------------*/
90 static void
91 send(void *ptr)
92 {
93  struct polite_conn *c = ptr;
94 
95  if(c->q != NULL) {
96  queuebuf_to_packetbuf(c->q);
97  queuebuf_free(c->q);
98  c->q = NULL;
99  abc_send(&c->c);
100  if(c->cb->sent) {
101  c->cb->sent(c);
102  }
103  }
104 }
105 /*---------------------------------------------------------------------------*/
106 static const struct abc_callbacks abc = { recv, sent };
107 /*---------------------------------------------------------------------------*/
108 void
109 polite_open(struct polite_conn *c, uint16_t channel,
110  const struct polite_callbacks *cb)
111 {
112  abc_open(&c->c, channel, &abc);
113  c->cb = cb;
114 }
115 /*---------------------------------------------------------------------------*/
116 void
118 {
119  abc_close(&c->c);
120  ctimer_stop(&c->t);
121  if(c->q != NULL) {
122  queuebuf_free(c->q);
123  c->q = NULL;
124  }
125 }
126 /*---------------------------------------------------------------------------*/
127 int
128 polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
129 {
130  if(c->q != NULL) {
131  /* If we are already about to send a packet, we cancel the old one. */
132  queuebuf_free(c->q);
133  }
134  c->hdrsize = hdrsize;
135  c->q = queuebuf_new_from_packetbuf();
136  if(c->q != NULL) {
137  ctimer_set(&c->t, interval / 2 + (random_rand() % (interval / 2)), send, c);
138  return 1;
139  }
140  return 0;
141 }
142 /*---------------------------------------------------------------------------*/
143 void
145 {
146  ctimer_stop(&c->t);
147  if(c->q != NULL) {
148  queuebuf_free(c->q);
149  c->q = NULL;
150  }
151 }
152 /*---------------------------------------------------------------------------*/
153 /** @} */