Contiki-Inga 3.x
stunicast.c
Go to the documentation of this file.
1 /**
2  * \addtogroup rimestunicast
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  * Stubborn unicast
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "net/rime/stunicast.h"
46 #include "net/rime/rime.h"
47 #include <string.h>
48 
49 #define DEBUG 0
50 #if DEBUG
51 #include <stdio.h>
52 #define PRINTF(...) printf(__VA_ARGS__)
53 #else
54 #define PRINTF(...)
55 #endif
56 
57 /*---------------------------------------------------------------------------*/
58 static void
59 recv_from_uc(struct unicast_conn *uc, const linkaddr_t *from)
60 {
61  register struct stunicast_conn *c = (struct stunicast_conn *)uc;
62  PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
64  from->u8[0], from->u8[1]);
65  if(c->u->recv != NULL) {
66  c->u->recv(c, from);
67  }
68 }
69 /*---------------------------------------------------------------------------*/
70 static void
71 sent_by_uc(struct unicast_conn *uc, int status, int num_tx)
72 {
73  register struct stunicast_conn *c = (struct stunicast_conn *)uc;
74  PRINTF("%d.%d: stunicast: recv_from_uc from %d.%d\n",
76  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
77  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
78  if(c->u->sent != NULL) {
79  c->u->sent(c, status, num_tx);
80  }
81 }
82 /*---------------------------------------------------------------------------*/
83 static const struct unicast_callbacks stunicast = {recv_from_uc,
84  sent_by_uc};
85 /*---------------------------------------------------------------------------*/
86 void
87 stunicast_open(struct stunicast_conn *c, uint16_t channel,
88  const struct stunicast_callbacks *u)
89 {
90  unicast_open(&c->c, channel, &stunicast);
91  c->u = u;
92 }
93 /*---------------------------------------------------------------------------*/
94 void
95 stunicast_close(struct stunicast_conn *c)
96 {
97  unicast_close(&c->c);
98  stunicast_cancel(c);
99 }
100 /*---------------------------------------------------------------------------*/
101 linkaddr_t *
102 stunicast_receiver(struct stunicast_conn *c)
103 {
104  return &c->receiver;
105 }
106 /*---------------------------------------------------------------------------*/
107 static void
108 send(void *ptr)
109 {
110  struct stunicast_conn *c = ptr;
111 
112  PRINTF("%d.%d: stunicast: resend to %d.%d\n",
114  c->receiver.u8[0], c->receiver.u8[1]);
115  if(c->buf) {
116  queuebuf_to_packetbuf(c->buf);
117  unicast_send(&c->c, &c->receiver);
118  stunicast_set_timer(c, CLOCK_SECOND);
119  }
120  /* if(c->u->sent != NULL) {
121  c->u->sent(c);
122  }*/
123 }
124 /*---------------------------------------------------------------------------*/
125 void
126 stunicast_set_timer(struct stunicast_conn *c, clock_time_t t)
127 {
128  ctimer_set(&c->t, t, send, c);
129 }
130 /*---------------------------------------------------------------------------*/
131 int
132 stunicast_send_stubborn(struct stunicast_conn *c, const linkaddr_t *receiver,
133  clock_time_t rxmittime)
134 {
135  if(c->buf != NULL) {
136  queuebuf_free(c->buf);
137  }
138  c->buf = queuebuf_new_from_packetbuf();
139  if(c->buf == NULL) {
140  return 0;
141  }
142  linkaddr_copy(&c->receiver, receiver);
143  ctimer_set(&c->t, rxmittime, send, c);
144 
145  PRINTF("%d.%d: stunicast_send_stubborn to %d.%d\n",
147  c->receiver.u8[0],c->receiver.u8[1]);
148  unicast_send(&c->c, &c->receiver);
149  /* if(c->u->sent != NULL) {
150  c->u->sent(c);
151  }*/
152 
153  return 1;
154 
155 }
156 /*---------------------------------------------------------------------------*/
157 int
158 stunicast_send(struct stunicast_conn *c, const linkaddr_t *receiver)
159 {
160  PRINTF("%d.%d: stunicast_send to %d.%d\n",
162  receiver->u8[0], receiver->u8[1]);
163  return unicast_send(&c->c, receiver);
164 }
165 /*---------------------------------------------------------------------------*/
166 void
167 stunicast_cancel(struct stunicast_conn *c)
168 {
169  ctimer_stop(&c->t);
170  if(c->buf != NULL) {
171  queuebuf_free(c->buf);
172  c->buf = NULL;
173  }
174 }
175 /*---------------------------------------------------------------------------*/
176 /** @} */