Contiki 2.5
bundle.h
Go to the documentation of this file.
1 /**
2  * \addtogroup agent
3  * @{
4  */
5 
6 /**
7  * \defgroup bundle Bundle Memory Representation
8  *
9  * @{
10  */
11 
12 /**
13  * \file
14  * \brief this file defines the bundle memory representation
15  *
16  * \author Georg von Zengen (vonzeng@ibr.cs.tu-bs.de)
17  * \author Daniel Willmann <daniel@totalueberwachung.de>
18  * \author Wolf-Bastian Poettner <poettner@ibr.cs.tu-bs.de>
19  */
20 
21 #include <stdint.h>
22 
23 #include "contiki.h"
24 #include "mmem.h"
25 #include "net/rime/rimeaddr.h"
26 #include "process.h"
27 #include "packetbuf.h"
28 
29 #ifndef __BUNDLE_H__
30 #define __BUNDLE_H__
31 
32 
33 //primary block defines
34 #define VERSION 0
35 #define FLAGS 1
36 #define LENGTH 2
37 #define DEST_NODE 3
38 #define DEST_SERV 4
39 #define SRC_NODE 5
40 #define SRC_SERV 6
41 #define REP_NODE 7
42 #define REP_SERV 8
43 #define CUST_NODE 9
44 #define CUST_SERV 10
45 #define TIME_STAMP 11
46 #define TIME_STAMP_SEQ_NR 12
47 #define LIFE_TIME 13
48 #define DIRECTORY_LEN 14
49 #define FRAG_OFFSET 15
50 #define APP_DATA_LEN 16
51 
52 /* Bundle Block Types */
53 #define BUNDLE_BLOCK_TYPE_PAYLOAD 0x01
54 
55 /* Bundle deletion reasons */
56 #define REASON_NO_INFORMATION 0x00
57 #define REASON_LIFETIME_EXPIRED 0x01
58 #define REASON_UNIDIRECTIONAL_LINK 0x02
59 #define REASON_TRANSMISSION_CANCELED 0x03
60 #define REASON_DEPLETED_STORAGE 0x04
61 #define REASON_DEST_EID_UNINTELLIGBLE 0x05
62 #define REASON_NO_ROUTE 0x06
63 #define REASON_NO_TIMELY_CONTACT 0x07
64 #define REASON_BLOCK_UNINTELLIGBLE 0x08
65 #define REASON_DELIVERED 0xFF
66 
67 /* Bundle Flag defines */
68 #define BUNDLE_FLAG_FRAGMENT 0x0001
69 #define BUNDLE_FLAG_ADM_REC 0x0002
70 #define BUNDLE_FLAG_DONT_FRAG 0x0004
71 #define BUNDLE_FLAG_CUST_REQ 0x0008
72 #define BUNDLE_FLAG_SINGLETON 0x0010
73 #define BUNDLE_FLAG_ACK_REQ 0x0020
74 /* Bit 6 reserved */
75 #define BUNDLE_FLAG_PRIOL 0x0080
76 #define BUNDLE_FLAG_PRIOH 0x0100
77 /* Bit 9 - 13 reserved */
78 #define BUNDLE_FLAG_REP_RECV 0x4000
79 #define BUNDLE_FLAG_REP_CUST 0x8000
80 #define BUNDLE_FLAG_REP_FWD 0x10000
81 #define BUNDLE_FLAG_REP_DELIV 0x20000
82 #define BUNDLE_FLAG_REP_DELETE 0x40000
83 /* Bit 19 and 20 reserved */
84 
85 #define BUNDLE_FLAG_REPORT (BUNDLE_FLAG_REP_RECV | BUNDLE_FLAG_REP_CUST | BUNDLE_FLAG_REP_FWD | BUNDLE_FLAG_REP_DELIV | BUNDLE_FLAG_REP_DELETE)
86 
87 /* Bundle Priorities */
88 #define BUNDLE_PRIORITY_BULK 0x0000
89 #define BUNDLE_PRIORITY_NORMAL 0x0080
90 #define BUNDLE_PRIORITY_EXPEDITED 0x0100
91 #define BUNDLE_PRIORITY_RESERVED 0x0180
92 
93 #define BUNDLE_PRIORITY_MASK 0x0180
94 
95 /* Block Flag defines */
96 #define BUNDLE_BLOCK_FLAG_NULL 0x00
97 #define BUNDLE_BLOCK_FLAG_REPL 0x01
98 #define BUNDLE_BLOCK_FLAG_STAT 0x02
99 #define BUNDLE_BLOCK_FLAG_DEL 0x04
100 #define BUNDLE_BLOCK_FLAG_LAST 0x08
101 #define BUNDLE_BLOCK_FLAG_DISC 0x10
102 #define BUNDLE_BLOCK_FLAG_NOTPR 0x20
103 #define BUNDLE_BLOCK_FLAG_EID 0x40
104 
105 //payload block defines
106 #define DATA 17
107 
108 // Enable Timing debug output
109 #define DEBUG_H 0
110 
111 struct bundle_block_t {
112  uint8_t type;
113  uint32_t flags;
114 
115  /* FIXME: EID References are unsupported */
116 
117  /* Variable array at the end to hold the payload
118  * Size is uint8 despite being an SDNV because
119  * IEEE-805.15.4 limits the size here. */
120  uint8_t block_size;
121  uint8_t payload[];
122 } __attribute__((packed));
123 
124 /**
125 * \brief this struct defines the bundle for internal processing
126 */
127 struct bundle_t{
128  uint8_t custody;
129  uint8_t del_reason;
130 
131  uint32_t bundle_num;
132 
133  uint32_t rec_time;
134 
135  uint32_t flags;
136  uint32_t dst_node;
137  uint32_t dst_srv;
138  uint32_t src_node;
139  uint32_t src_srv;
140  uint32_t rep_node;
141  uint32_t rep_srv;
142  uint32_t cust_node;
143  uint32_t cust_srv;
144  uint32_t tstamp;
145  uint32_t tstamp_seq;
146 
147  uint32_t lifetime;
148 
149  uint32_t frag_offs;
150  uint32_t app_len;
151 
152  packetbuf_attr_t rssi;
153  struct process * source_process;
154  rimeaddr_t msrc;
155 #if DEBUG_H
156  uint16_t debug_time;
157 #endif
158  uint8_t num_blocks;
159 
160  uint8_t block_data[];
161 } __attribute__ ((packed));
162 
163 /**
164  * \brief generates the bundle struct from raw data
165  * \param buffer pointer to the buffer with raw data
166  * \param size size of raw data
167  * \return Pointer to the MMEM struct containing the bundle
168  */
169 struct mmem * bundle_recover_bundle(uint8_t * buffer, int size);
170 
171 /**
172  * \brief Encodes the bundle to raw data
173  * \param bundlemem pointer to the MMEM struct containing the bundle
174  * \param buffer pointer to a buffer
175  * \param max_len Size of the buffer
176  * \return The number of bytes that were written to buf
177  */
178 int bundle_encode_bundle(struct mmem * bundlemem, uint8_t * buffer, int max_len);
179 
180 /**
181  * \brief sets an attribute of a bundle
182  * \param bundlemem pointer to the MMEM struct containing bundle
183  * \param attr attribute to be set
184  * \param val pointer to the value to be set
185  * \return length of the set value on success or 0 on error
186  */
187 uint8_t bundle_set_attr(struct mmem *bundlemem, uint8_t attr, uint32_t *val);
188 
189 /**
190  * \brief Gets an attribute of a bundle
191  * \param bundlemem pointer to the MMEM struct containing the bundle
192  * \param attr attribute to get
193  * \param val pointer to the variable where the value will be written
194  * \return 1 on success or 0 on error
195  */
196 uint8_t bundle_get_attr(struct mmem *bundlemem, uint8_t attr, uint32_t *val);
197 
198 /**
199  * \brief Get a new bundle structure allocated
200  * \return MMEM allocation of the bundle, NULL in case of an error
201  */
202 struct mmem * bundle_create_bundle();
203 
204 /**
205  * \brief free a given MMEM allocation of a bundle struct
206  * \param bundlemem the MMEM allocation to free
207  *
208  * A bit of magic is involved here because we want to also free
209  * the bundleslot that this bundle belongs to.
210  */
211 uint16_t bundle_delete_bundle(struct mmem * bundlemem);
212 
213 /**
214  * \brief Add a block to a bundle
215  * \param bundlemem pointer to the MMEM allocation of the bundle
216  * \param type type of the block
217  * \param flags processing flags of the block
218  * \param data pointer to the block payload
219  * \param d_len length of the block payload
220  * \return 1 on success or 0 on error
221  */
222 int bundle_add_block(struct mmem * bundlemem, uint8_t type, uint8_t flags, uint8_t * data, uint8_t d_len);
223 
224 /**
225  * \brief Returns a pointer a bundle block
226  * \param bundlemem MMEM allocation of the bundle
227  * \param i index of the block. Starts at 0
228  * \return the block or NULL on error
229  */
230 struct bundle_block_t * bundle_get_block(struct mmem * bundlemem, uint8_t i);
231 
232 /**
233  * \brief Returns pointer to first bundle block of specific type
234  * \param bundlemem MMEM allocation of the bundle
235  * \param type type of the block (see bundle.h)
236  * \return the block of NULL on error
237  */
238 struct bundle_block_t * bundle_get_block_by_type(struct mmem * bundlemem, uint8_t type);
239 
240 /**
241  * \brief Returns pointer to bundle payload block
242  * \param bundlemem MMEM allocation of the bundle
243  * \return the block of NULL on error
244  */
245 struct bundle_block_t * bundle_get_payload_block(struct mmem * bundlemem);
246 
247 /**
248  * \brief converts IPN EIDs (uint32_t) into the RIME address
249  */
250 rimeaddr_t convert_eid_to_rime(uint32_t eid);
251 
252 /**
253  * \brief converts a RIME address into an IPN EID
254  */
255 uint32_t convert_rime_to_eid(rimeaddr_t * dest);
256 
257 /**
258  * \brief Decrements the reference counter for a MMEM struct containing a bundle
259  * Frees the struct when reference counter is down to 0
260  * \return Returns the number of remaining references on the bundle or -1 on error
261  */
262 int bundle_decrement(struct mmem *bundlemem);
263 
264 /**
265  * \brief Increments the reference counter for a MMEM struct containing a bundle
266  * \return Returns the number of references on the bundle or -1 on error
267  */
268 int bundle_increment(struct mmem *bundlemem);
269 
270 #endif
271 /** @} */
272 /** @} */