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