libschc
fragmenter.h
Go to the documentation of this file.
1 /* (c) 2018 - idlab - UGent - imec
2  *
3  * Bart Moons
4  *
5  * This file is part of the SCHC stack implementation
6  *
7  */
8 
9 #ifndef __SCHCFRAGMENTER_H__
10 #define __SCHCFRAGMENTER_H__
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include "schc.h"
17 #include "schc_config.h"
18 
22 #define SCHC_FRAG_INPUT 2
23 
27 #define SCHC_ACK_INPUT 1
28 
33 #define SCHC_SUCCESS 0
34 
39 #define SCHC_END 3
40 
44 #define SCHC_FAILURE -1
45 
49 #define SCHC_NO_FRAGMENTATION -2
50 
51 
52 typedef enum {
53  INIT_TX = 0, SEND = 1, RESEND = 2, WAIT_BITMAP = 3, END_TX = 4, ERR = 5
54 } tx_state;
55 
56 typedef enum {
58 } rx_state;
59 
60 typedef struct schc_mbuf_t {
61  /* the selected slot */
62  uint32_t slot;
63  /* start of memory block */
64  uint8_t* ptr;
65  /* length of the fragment */
66  uint16_t len;
67  /* the fragment to which the mbuf belongs to */
68  uint8_t frag_cnt;
69  /* the bit offset when formatted */
70  uint8_t offset;
71  /* pointer to the next fragment*/
72  struct schc_mbuf_t *next;
73 } schc_mbuf_t;
74 
75 
76 typedef struct schc_fragmentation_ack_t {
77  /* the rule id included in the ack */
79  /* the encoded bitmap included in the ack */
81  /* the window included in the ack */
82  uint8_t window[1];
83  /* the DTAG received in the ack */
84  uint8_t dtag[1];
85  /* the MIC bit received in the ack */
86  uint8_t mic;
87  /* the fcn value this ack belongs to */
88  uint8_t fcn;
89 
91 
92 typedef struct schc_fragmentation_t {
93  /* the device id of the connection */
94  uint32_t device_id;
95  /* the length of the packet */
96  uint16_t packet_len;
97  /* a pointer to the start of the unfragmented, compressed packet in a bit array */
99  /* the start of the packet + the total length */
100  uint8_t* tail_ptr;
101  /* the rule which will be applied to the header */
103  /* the maximum transfer unit of this connection */
104  uint16_t mtu;
105  /* the duty cycle in ms */
106  uint32_t dc;
107  /* the message integrity check over the full, compressed packet */
108  uint8_t mic[MIC_SIZE_BYTES];
109  /* the fragment counter in the current window
110  * ToDo: we only support fixed FCN length
111  * */
112  uint8_t fcn;
113  /* the current window */
114  uint8_t window;
115  /* the total number of windows transmitted */
116  uint8_t window_cnt;
117  /* the current DTAG */
118  uint8_t dtag;
119  /* the total number of fragments sent */
120  uint8_t frag_cnt;
121  /* the bitmap of the fragments sent */
123  /* the number of transmission attempts */
124  uint8_t attempts;
125  /* the current state for the sending device */
127  /* the current state for the receiving device */
129  /* the function to call when the fragmenter has something to send */
130  uint8_t (*send)(uint8_t* data, uint16_t length, uint32_t device_id);
131  /* the timer task */
132  void (*post_timer_task)(void (*timer_task)(void* conn), uint32_t device_id,
133  uint32_t time_ms, void *arg);
134  /* this function is called when the last rx timer expires */
135  void (*end_rx)(struct schc_fragmentation_t *conn);
136  /* this function is called once the device reaches the END_TX state */
137  void (*end_tx)();
138  /* this callback may be used to remove a timer entry */
139  void (*remove_timer_entry)(uint32_t device_id);
140  /* indicates whether a timer has expired */
141  uint8_t timer_flag;
142  /* indicates if a fragment is received or this is a callback */
143  uint8_t input;
144  /* the last received ack */
146  /* the start of the mbuf chain */
148  /* the rule in use */
150  /* the rule size in bits */
151  uint8_t RULE_SIZE;
153 
155  uint8_t (*send)(uint8_t* data, uint16_t length, uint32_t device_id),
156  void (*end_rx)(schc_fragmentation_t* conn),
157  void (*remove_timer_entry)(uint32_t device_id));
158 int8_t schc_fragment(schc_fragmentation_t *tx_conn);
159 int8_t schc_reassemble(schc_fragmentation_t* rx_conn);
160 void schc_reset(schc_fragmentation_t* conn);
161 
162 schc_fragmentation_t* schc_input(uint8_t* data, uint16_t len,
163  schc_fragmentation_t* rx_conn, uint32_t device_id);
164 void schc_ack_input(uint8_t* data, uint16_t len, schc_fragmentation_t* tx_conn,
165  uint32_t device_id);
166 schc_fragmentation_t* schc_fragment_input(uint8_t* data, uint16_t len,
167  uint32_t device_id);
168 schc_fragmentation_t* schc_get_connection(uint32_t device_id);
169 
170 uint16_t get_mbuf_len(schc_mbuf_t *head);
171 void mbuf_copy(schc_mbuf_t *head, uint8_t* ptr);
172 
173 #ifdef __cplusplus
174 }
175 #endif
176 
177 #endif
schc_reset
void schc_reset(schc_fragmentation_t *conn)
reset a connection
Definition: fragmenter.c:818
INIT_TX
Definition: fragmenter.h:53
RESEND
Definition: fragmenter.h:53
END_TX
Definition: fragmenter.h:53
BITMAP_SIZE_BYTES
#define BITMAP_SIZE_BYTES
Definition: schc_config.h:60
schc_fragmentation_ack_t
Definition: fragmenter.h:76
schc_bitarray_t
Definition: schc.h:32
schc_mbuf_t::frag_cnt
uint8_t frag_cnt
Definition: fragmenter.h:68
schc_fragmentation_t::dtag
uint8_t dtag
Definition: fragmenter.h:118
schc_fragmentation_t::mic
uint8_t mic[MIC_SIZE_BYTES]
Definition: fragmenter.h:108
schc_mbuf_t::len
uint16_t len
Definition: fragmenter.h:66
schc_fragmentation_t::rule_id
uint8_t rule_id[RULE_SIZE_BYTES]
Definition: fragmenter.h:102
schc_fragmentation_t::attempts
uint8_t attempts
Definition: fragmenter.h:124
schc_fragmentation_t::schc_rule
struct schc_rule_t * schc_rule
Definition: fragmenter.h:149
schc_mbuf_t
Definition: fragmenter.h:60
schc_fragmentation_ack_t::window
uint8_t window[1]
Definition: fragmenter.h:82
schc_rule_t
Definition: schc.h:128
schc_fragmentation_t::end_tx
void(* end_tx)()
Definition: fragmenter.h:137
schc_fragment_input
schc_fragmentation_t * schc_fragment_input(uint8_t *data, uint16_t len, uint32_t device_id)
This function should be called whenever a fragment is received an open connection is picked for the d...
Definition: fragmenter.c:2276
schc_fragmentation_t::window
uint8_t window
Definition: fragmenter.h:114
RECV_WINDOW
Definition: fragmenter.h:57
schc_mbuf_t::next
struct schc_mbuf_t * next
Definition: fragmenter.h:72
END_RX
Definition: fragmenter.h:57
schc.h
schc_fragmentation_t::remove_timer_entry
void(* remove_timer_entry)(uint32_t device_id)
Definition: fragmenter.h:139
schc_fragmentation_t::mtu
uint16_t mtu
Definition: fragmenter.h:104
schc_mbuf_t
struct schc_mbuf_t schc_mbuf_t
MIC_SIZE_BYTES
#define MIC_SIZE_BYTES
Definition: schc_config.h:57
schc_config.h
schc_fragmentation_t::window_cnt
uint8_t window_cnt
Definition: fragmenter.h:116
rx_state
rx_state
Definition: fragmenter.h:56
schc_fragmentation_ack_t::bitmap
uint8_t bitmap[BITMAP_SIZE_BYTES]
Definition: fragmenter.h:80
schc_fragmentation_t::dc
uint32_t dc
Definition: fragmenter.h:106
schc_reassemble
int8_t schc_reassemble(schc_fragmentation_t *rx_conn)
the receiver state machine
Definition: fragmenter.c:1454
schc_get_connection
schc_fragmentation_t * schc_get_connection(uint32_t device_id)
find a connection based on a device id or open a new connection if there was no connection for this d...
Definition: fragmenter.c:1322
schc_fragmentation_t::ack
schc_fragmentation_ack_t ack
Definition: fragmenter.h:145
schc_fragmentation_t::bitmap
uint8_t bitmap[BITMAP_SIZE_BYTES]
Definition: fragmenter.h:122
schc_fragmentation_t
Definition: fragmenter.h:92
schc_fragmentation_t::post_timer_task
void(* post_timer_task)(void(*timer_task)(void *conn), uint32_t device_id, uint32_t time_ms, void *arg)
Definition: fragmenter.h:132
schc_fragmentation_t::timer_flag
uint8_t timer_flag
Definition: fragmenter.h:141
schc_fragment
int8_t schc_fragment(schc_fragmentation_t *tx_conn)
the sender state machine
Definition: fragmenter.c:1967
WAIT_BITMAP
Definition: fragmenter.h:53
schc_fragmentation_t::TX_STATE
tx_state TX_STATE
Definition: fragmenter.h:126
ABORT
Definition: fragmenter.h:57
schc_fragmentation_t::RX_STATE
rx_state RX_STATE
Definition: fragmenter.h:128
SEND
Definition: fragmenter.h:53
WAIT_END
Definition: fragmenter.h:57
schc_mbuf_t::slot
uint32_t slot
Definition: fragmenter.h:62
schc_fragmentation_t::packet_len
uint16_t packet_len
Definition: fragmenter.h:96
schc_fragmentation_ack_t::dtag
uint8_t dtag[1]
Definition: fragmenter.h:84
schc_fragmentation_t::tail_ptr
uint8_t * tail_ptr
Definition: fragmenter.h:100
ERR
Definition: fragmenter.h:53
tx_state
tx_state
Definition: fragmenter.h:52
schc_fragmenter_init
int8_t schc_fragmenter_init(schc_fragmentation_t *tx_conn, uint8_t(*send)(uint8_t *data, uint16_t length, uint32_t device_id), void(*end_rx)(schc_fragmentation_t *conn), void(*remove_timer_entry)(uint32_t device_id))
Initializes the SCHC fragmenter.
Definition: fragmenter.c:1799
schc_input
schc_fragmentation_t * schc_input(uint8_t *data, uint16_t len, schc_fragmentation_t *rx_conn, uint32_t device_id)
This function should be called whenever a packet is received.
Definition: fragmenter.c:2194
schc_fragmentation_ack_t
struct schc_fragmentation_ack_t schc_fragmentation_ack_t
schc_fragmentation_ack_t::rule_id
uint8_t rule_id[RULE_SIZE_BYTES]
Definition: fragmenter.h:78
schc_fragmentation_t::bit_arr
schc_bitarray_t * bit_arr
Definition: fragmenter.h:98
schc_fragmentation_ack_t::mic
uint8_t mic
Definition: fragmenter.h:86
schc_fragmentation_t::frag_cnt
uint8_t frag_cnt
Definition: fragmenter.h:120
schc_ack_input
void schc_ack_input(uint8_t *data, uint16_t len, schc_fragmentation_t *tx_conn, uint32_t device_id)
This function should be called whenever an ack is received.
Definition: fragmenter.c:2215
schc_fragmentation_t
struct schc_fragmentation_t schc_fragmentation_t
mbuf_copy
void mbuf_copy(schc_mbuf_t *head, uint8_t *ptr)
copy the byte alligned contents of the mbuf chain to the passed pointer
Definition: fragmenter.c:323
schc_fragmentation_t::RULE_SIZE
uint8_t RULE_SIZE
Definition: fragmenter.h:151
schc_fragmentation_t::fcn
uint8_t fcn
Definition: fragmenter.h:112
schc_fragmentation_t::input
uint8_t input
Definition: fragmenter.h:143
WAIT_NEXT_WINDOW
Definition: fragmenter.h:57
schc_mbuf_t::ptr
uint8_t * ptr
Definition: fragmenter.h:64
schc_fragmentation_t::device_id
uint32_t device_id
Definition: fragmenter.h:94
schc_fragmentation_t::head
schc_mbuf_t * head
Definition: fragmenter.h:147
get_mbuf_len
uint16_t get_mbuf_len(schc_mbuf_t *head)
returns the total length of the mbuf
Definition: fragmenter.c:282
schc_fragmentation_ack_t::fcn
uint8_t fcn
Definition: fragmenter.h:88
RULE_SIZE_BYTES
#define RULE_SIZE_BYTES
Definition: schc.h:15
schc_fragmentation_t::end_rx
void(* end_rx)(struct schc_fragmentation_t *conn)
Definition: fragmenter.h:135
schc_mbuf_t::offset
uint8_t offset
Definition: fragmenter.h:70
WAIT_MISSING_FRAG
Definition: fragmenter.h:57
schc_fragmentation_t::send
uint8_t(* send)(uint8_t *data, uint16_t length, uint32_t device_id)
Definition: fragmenter.h:130