Contiki 2.5
dtn_network.c
Go to the documentation of this file.
1 /**
2  * \addtogroup dtn_network
3  * @{
4  */
5 
6 /**
7  * \file
8  *
9  * \author Georg von Zengen <vonzeng@ibr.cs.tu-bs.de>
10  * \author Daniel Willmann <daniel@totalueberwachung.de>
11  * \author Wolf-Bastian Pšttner <poettner@ibr.cs.tu-bs.de>
12  */
13 
14 #ifdef CONF_LOGLEVEL
15 #define LOGLEVEL CONF_LOGLEVEL
16 #else
17 #define LOGLEVEL LOGL_INF
18 #endif
19 
20 #include "netstack.h"
21 #include "packetbuf.h"
22 #include "leds.h"
23 #include "logging.h"
24 
25 #include "convergence_layer.h"
26 #include "agent.h"
27 
28 #include "dtn_network.h"
29 
30 /**
31  * Init function called by the Contiki netstack
32  */
33 static void dtn_network_init(void)
34 {
35  /* Set up log domains */
36  logging_init();
37  logging_domain_level_set(LOGD_DTN, LOG_NET, LOGLEVEL);
38  logging_domain_level_set(LOGD_DTN, LOG_BUNDLE, LOGLEVEL);
39  logging_domain_level_set(LOGD_DTN, LOG_ROUTE, LOGLEVEL);
40  logging_domain_level_set(LOGD_DTN, LOG_STORE, LOGLEVEL);
41  logging_domain_level_set(LOGD_DTN, LOG_SDNV, LOGLEVEL);
42  logging_domain_level_set(LOGD_DTN, LOG_SLOTS, LOGLEVEL);
43  logging_domain_level_set(LOGD_DTN, LOG_AGENT, LOGLEVEL);
44  logging_domain_level_set(LOGD_DTN, LOG_CL, LOGLEVEL);
45 
46  /* Clear the packet buffer */
48 
49  /* Initialize logging */
50  LOG(LOGD_DTN, LOG_NET, LOGL_DBG, "init");
51 
52  /* Start the agent */
53  agent_init();
54 }
55 
56 /**
57  * Input callback called by the lower layers to indicate incoming data
58  */
59 static void dtn_network_input(void)
60 {
61  rimeaddr_t source;
62  uint8_t * buffer = NULL;
63  uint8_t length = 0;
64  packetbuf_attr_t rssi = 0;
65 
66  leds_on(LEDS_ALL);
67 
68  /* Create a copy here, because otherwise packetbuf_clear will evaporate the address */
69  rimeaddr_copy(&source, packetbuf_addr(PACKETBUF_ADDR_SENDER));
70  buffer = packetbuf_dataptr();
71  length = packetbuf_datalen();
72  rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
73 
74  convergence_layer_incoming_frame(&source, buffer, length, rssi);
75 
76  leds_off(LEDS_ALL);
77 }
78 
79 /**
80  * Callback function called by the lower layers after a transmission attempt on the radio
81  */
82 static void dtn_network_sent(void * pointer, int status, int num_tx)
83 {
84  int outcome = 0;
85 
86  /* Here we map the return values to the three interesting values needed by the CL */
87  switch(status) {
88  case MAC_TX_DEFERRED:
89  case MAC_TX_COLLISION:
90  case MAC_TX_ERR:
91  default:
92  outcome = CONVERGENCE_LAYER_STATUS_NOSEND;
93  break;
94  case MAC_TX_ERR_FATAL:
95  /* Fatal errors occur when the buffer is too small */
96  outcome = CONVERGENCE_LAYER_STATUS_FATAL;
97  break;
98  case MAC_TX_NOACK:
99  outcome = CONVERGENCE_LAYER_STATUS_NOACK;
100  break;
101  case MAC_TX_OK:
102  outcome = CONVERGENCE_LAYER_STATUS_OK;
103  break;
104  }
105 
106  /* Call the CL */
107  convergence_layer_status(pointer, outcome);
108 }
109 
111 {
112  uint8_t * buffer = NULL;
113 
114  /* We're not going to use packetbuf_copyfrom here but instead assemble the packet
115  * in the buffer ourself */
116  packetbuf_clear();
117 
118  buffer = packetbuf_dataptr();
119 
120  return buffer;
121 }
122 
124  return PACKETBUF_SIZE;
125 }
126 
127 void dtn_network_send(rimeaddr_t * destination, uint8_t length, void * reference)
128 {
129  leds_on(LEDS_YELLOW);
130 
131  /* Set the data length */
132  packetbuf_set_datalen(length);
133 
134  /* Set destination address */
135  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, destination);
136  packetbuf_set_attr(PACKETBUF_ADDRSIZE, 2);
137 
138  /* Send it out via the MAC */
139  NETSTACK_MAC.send(&dtn_network_sent, reference);
140 
141  leds_off(LEDS_YELLOW);
142 }
143 
144 /**
145  * Contiki's network driver interface
146  */
148 {
149  "DTN",
150  dtn_network_init,
151  dtn_network_input
152 };
153 
154 /** @} */