Contiki 3.x
stunicast.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  * Stubborn unicast
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimestunicast
42  * @{
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 /** @} */
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
#define NULL
The null pointer.
Header file for the Rime stack
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142
Stubborn unicast header file
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82