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