Contiki-Inga 3.x
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  */
37 
38 /**
39  * \file
40  * Polite Anonymous best effort local area BroadCast (polite)
41  * \author
42  * Adam Dunkels <adam@sics.se>
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 /** @} */