Contiki 3.x
stbroadcast.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  * Implementation of the Rime module Stubborn Anonymous
36  * BroadCast (stbroadcast)
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 /**
42  * \addtogroup rimestbroadcast
43  * @{
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 /** @} */
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
Definition: broadcast.c:105
#define NULL
The null pointer.
Header file for the Rime stack
Header file for the Rime module Stubborn Anonymous BroadCast (stbroadcast)
void stbroadcast_cancel(struct stbroadcast_conn *c)
Cancel the current stubborn message.
Definition: stbroadcast.c:115
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Callback structure for broadcast.
Definition: broadcast.h:80
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:118
void stbroadcast_open(struct stbroadcast_conn *c, uint16_t channel, const struct stbroadcast_callbacks *u)
Set up a stbroadcast connection.
Definition: stbroadcast.c:64
int broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
Definition: broadcast.c:111
void stbroadcast_set_timer(struct stbroadcast_conn *c, clock_time_t t)
Set the retransmission time of the current stubborn message.
Definition: stbroadcast.c:93
void broadcast_open(struct broadcast_conn *c, uint16_t channel, const struct broadcast_callbacks *u)
Set up an identified best-effort broadcast connection.
Definition: broadcast.c:96
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142
A stbroadcast connection.
Definition: stbroadcast.h:80
int stbroadcast_send_stubborn(struct stbroadcast_conn *c, clock_time_t t)
Send a stubborn message.
Definition: stbroadcast.c:99