Contiki-Inga 3.x
stbroadcast.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimestbroadcast
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, 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  * Implementation of the Rime module Stubborn Anonymous
41  * BroadCast (stbroadcast)
42  * \author
43  * Adam Dunkels <adam@sics.se>
44  */
45 
46 #include "net/rime/stbroadcast.h"
47 #include "net/rime/rime.h"
48 #include <string.h>
49 
50 /*---------------------------------------------------------------------------*/
51 static void
52 recv_from_broadcast(struct broadcast_conn *broadcast, const linkaddr_t *sender)
53 {
54  register struct stbroadcast_conn *c = (struct stbroadcast_conn *)broadcast;
55  /* DEBUGF(3, "stbroadcast: recv_from_broadcast from %d\n", from_id);*/
56  if(c->u->recv != NULL) {
57  c->u->recv(c);
58  }
59 }
60 /*---------------------------------------------------------------------------*/
61 static const struct broadcast_callbacks stbroadcast = {recv_from_broadcast};
62 /*---------------------------------------------------------------------------*/
63 void
64 stbroadcast_open(struct stbroadcast_conn *c, uint16_t channel,
65  const struct stbroadcast_callbacks *u)
66 {
67  broadcast_open(&c->c, channel, &stbroadcast);
68  c->u = u;
69 }
70 /*---------------------------------------------------------------------------*/
71 void
72 stbroadcast_close(struct stbroadcast_conn *c)
73 {
74  broadcast_close(&c->c);
75  ctimer_stop(&c->t);
76 }
77 /*---------------------------------------------------------------------------*/
78 static void
79 send(void *ptr)
80 {
81  struct stbroadcast_conn *c = ptr;
82 
83  /* DEBUGF(3, "stbroadcast: send()\n");*/
84  queuebuf_to_packetbuf(c->buf);
85  broadcast_send(&c->c);
86  ctimer_reset(&c->t);
87  if(c->u->sent != NULL) {
88  c->u->sent(c);
89  }
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 stbroadcast_set_timer(struct stbroadcast_conn *c, clock_time_t t)
94 {
95  ctimer_set(&c->t, t, send, c);
96 }
97 /*---------------------------------------------------------------------------*/
98 int
99 stbroadcast_send_stubborn(struct stbroadcast_conn *c, clock_time_t t)
100 {
101  if(c->buf != NULL) {
102  queuebuf_free(c->buf);
103  }
104  c->buf = queuebuf_new_from_packetbuf();
105  if(c->buf == NULL) {
106  return 0;
107  }
108  send(c);
109  stbroadcast_set_timer(c, t);
110  return 1;
111 
112 }
113 /*---------------------------------------------------------------------------*/
114 void
116 {
117  ctimer_stop(&c->t);
118 }
119 /*---------------------------------------------------------------------------*/
120 /** @} */