Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
packet.h
Go to the documentation of this file.
1
13#ifndef INFUSE_SDK_INCLUDE_INFUSE_EPACKET_PACKET_H_
14#define INFUSE_SDK_INCLUDE_INFUSE_EPACKET_PACKET_H_
15
16#include <stdint.h>
17
18#include <zephyr/kernel.h>
19#include <zephyr/device.h>
20#include <zephyr/toolchain.h>
21#include <zephyr/net_buf.h>
22#include <zephyr/bluetooth/bluetooth.h>
23
24#include <infuse/types.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
38 /* Packet failed to decrypt */
40 /* Packet is encrypted for remote device */
44} __packed;
45
46/* Interface specific addresses */
48 /* Bluetooth LE address */
49 bt_addr_le_t bluetooth;
50};
51
52/* Empty interface address */
53#define EPACKET_ADDR_ALL ((union epacket_interface_address){0})
54
63typedef void (*epacket_tx_done_cb)(const struct device *dev, struct net_buf *pkt, int result,
64 void *user_data);
65
66/* Metadata for packets that will be transmitted */
68#ifdef CONFIG_EPACKET_BUFFERS_TX_DELAYABLE_WORK
69 struct k_work_delayable dwork;
70#endif
71 /* Callback run when TX completes */
73 /* Context provided to @a tx_done */
75 /* Authentication level of packet */
77 /* Packet type */
79 /* Flags to apply to packet */
80 uint16_t flags;
81 /* Sequence number used for packet */
82 uint16_t sequence;
83 /* Interface specific address */
85};
86
87/* Metadata for packets that have been received */
89 /* Device ID in packet */
91 /* GPS time in packet */
93 /* Key ID used by packet */
95 /* Authentication level of packet */
97 /* Type of packet */
99 /* Flags associated with packet */
100 uint16_t flags;
101 /* ePacket interface packet was received on */
102 const struct device *interface;
103 /* Numerical ID for interface */
105 /* Interface specific address */
107 /* RSSI of packet (0 = 0dBm, 20 = 20dBm, etc) */
108 int16_t rssi;
109 /* Sequence number of packet */
110 uint16_t sequence;
111};
112
113/* Global ePacket flags */
115 /* Bit 15: Encryption Type */
118 /* Bit 14: Transmitting device requests an ACK */
120 /* Bit 13: Device can forward data to the cloud */
122 /* Bit 12: Device sends its own data to the cloud */
124 /* Bits 8-11: Reserved */
125 /* Bits 0-7: Interface specific */
127};
128
132#define EPACKET_KEY_ID_REQ_MAGIC 0x4D
133
136 uint8_t device_key_id[3];
137} __packed;
138
140#define EPACKET_RATE_LIMIT_REQ_MAGIC 0x4E
141
145 uint8_t magic;
147 uint8_t delay_ms;
148} __packed;
149
153 uint8_t magic;
156} __packed;
157
160 uint8_t type;
161 uint8_t addr[6];
162} __packed;
163BUILD_ASSERT(sizeof(struct epacket_interface_address_bt_le) == 7);
164
167 /* Bit 16: 1 when packet is still encrypted
168 * 0 when packet is decrypted
169 * Bits 0-15: Total length of headers + data
170 */
172 /* Received packet signal strength (0 - val) */
173 uint8_t rssi;
174 /* Value from `EPACKET_INTERFACE_*` */
175 uint8_t interface;
176} __packed;
177
180 /* Device ID in the packet */
181 uint64_t device_id;
182 /* GPS time in the packet */
183 uint32_t gps_time;
184 /* Packet type */
185 uint8_t type;
186 /* Packet flags */
187 uint16_t flags;
188 /* Sequence number */
189 uint16_t sequence;
190 /* ID associated with the key */
191 uint8_t key_id[3];
192} __packed;
193
196 /* Total length of this header + payload */
197 uint16_t length;
198 /* Value from `EPACKET_INTERFACE_*` */
199 uint8_t interface;
200 /* Destination interface address + packet bytes */
202} __packed;
203
205 /* Automatically disconnect on the first received INFUSE_RPC_RSP */
207 /* Subscribe to data while connected */
209 /* Send a INFUSE_EPACKET_CONN_TERMINATED on connection terminated */
211 /* Prioritise uplink throughput to the connection associated with this request */
213} __packed;
214
217 /* Total length of this header + payload */
218 uint16_t length;
219 /* Value from `EPACKET_INTERFACE_*` */
220 uint8_t interface;
221 /* Value from `EPACKET_FORWARD_AUTO_CONN_` */
222 uint8_t flags;
223 /* Connection timeout (seconds) */
225 /* Connection idle timeout (seconds) */
227 /* Unconditional connection timeout (seconds) */
229 /* Destination interface address + packet bytes */
231} __packed;
232
235 /* Value from `EPACKET_INTERFACE_*` */
236 uint8_t interface;
237 /* Reason that interface disconnected */
238 int16_t reason;
239 /* Interface address that disconnected */
240 uint8_t address[];
241} __packed;
242
247
254void epacket_rate_limit_tx(k_ticks_t *last_call, uint16_t bytes_transmitted);
255
263void epacket_global_flags_set(uint16_t flags);
264
271
278
285
296struct net_buf *epacket_alloc_tx(k_timeout_t timeout);
297
305struct net_buf *epacket_alloc_rx(k_timeout_t timeout);
306
315static inline struct net_buf *epacket_alloc_tx_for_interface(const struct device *dev,
316 k_timeout_t timeout)
317{
318 const struct epacket_interface_common_config *config = dev->config;
319 struct net_buf *buf = epacket_alloc_tx(timeout);
320 uint16_t size;
321
322 if (buf == NULL) {
323 return NULL;
324 }
325 /* Reserve space for header */
326 net_buf_reserve(buf, config->header_size);
327 /* Limit size based on interface */
329 if (size > (config->header_size + config->footer_size)) {
330 /* Hacky reservation for footer, automatically reversed by epacket_queue */
331 buf->size = size - config->footer_size;
332 } else {
333 /* 0 payload size */
334 buf->size = config->header_size;
335 }
336 return buf;
337}
338
348static inline void epacket_set_tx_metadata(struct net_buf *buf, enum epacket_auth auth,
349 uint16_t flags, enum infuse_type type,
350 union epacket_interface_address dest)
351{
352 struct epacket_tx_metadata *meta = net_buf_user_data(buf);
353
354 meta->auth = auth;
356 meta->type = type;
357 meta->tx_done = NULL;
358 meta->interface_address = dest;
359}
360
368static inline void epacket_set_tx_callback(struct net_buf *buf, epacket_tx_done_cb tx_done,
369 void *user_data)
370{
371 struct epacket_tx_metadata *meta = net_buf_user_data(buf);
372
373 meta->tx_done = tx_done;
374 meta->tx_done_user_data = user_data;
375}
376
386int epacket_received_packet_append(struct net_buf *storage_buf, struct net_buf *received_buf);
387
392#ifdef __cplusplus
393}
394#endif
395
396#endif /* INFUSE_SDK_INCLUDE_INFUSE_EPACKET_PACKET_H_ */
epacket_interface_id
Definition interface.h:55
static uint16_t epacket_interface_max_packet_size(const struct device *dev)
Get current maximum packet size.
Definition interface.h:217
epacket_auth
Definition packet.h:37
struct net_buf * epacket_alloc_tx(k_timeout_t timeout)
Allocate ePacket TX buffer.
int epacket_received_packet_append(struct net_buf *storage_buf, struct net_buf *received_buf)
Append received packet to storage buffer.
struct net_buf * epacket_alloc_rx(k_timeout_t timeout)
Allocate ePacket RX buffer.
void epacket_global_flags_set(uint16_t flags)
Set global flags for all transmitted packets.
void epacket_rate_limit_reset(void)
Reset any active rate limits.
epacket_flags
Definition packet.h:114
void(* epacket_tx_done_cb)(const struct device *dev, struct net_buf *pkt, int result, void *user_data)
Callback run when packet is transmitted.
Definition packet.h:63
int epacket_num_buffers_free_rx(void)
Query the number of free RX buffers.
int epacket_num_buffers_free_tx(void)
Query the number of free TX buffers.
static void epacket_set_tx_metadata(struct net_buf *buf, enum epacket_auth auth, uint16_t flags, enum infuse_type type, union epacket_interface_address dest)
Set metadata on a packet.
Definition packet.h:348
static struct net_buf * epacket_alloc_tx_for_interface(const struct device *dev, k_timeout_t timeout)
Allocate ePacket TX buffer for a specific interface.
Definition packet.h:315
uint16_t epacket_global_flags_get(void)
Get the current global flags value.
static void epacket_set_tx_callback(struct net_buf *buf, epacket_tx_done_cb tx_done, void *user_data)
Set callback to be run after packet sent.
Definition packet.h:368
epacket_forward_auto_conn_flags
Definition packet.h:204
void epacket_rate_limit_tx(k_ticks_t *last_call, uint16_t bytes_transmitted)
Limit the transmission rate of bulk data paths.
@ EPACKET_AUTH_NETWORK
Definition packet.h:42
@ EPACKET_AUTH_DEVICE
Definition packet.h:43
@ EPACKET_AUTH_FAILURE
Definition packet.h:39
@ EPACKET_AUTH_REMOTE_ENCRYPTED
Definition packet.h:41
@ EPACKET_FLAGS_ENCRYPTION_NETWORK
Definition packet.h:117
@ EPACKET_FLAGS_CLOUD_SELF
Definition packet.h:123
@ EPACKET_FLAGS_ENCRYPTION_DEVICE
Definition packet.h:116
@ EPACKET_FLAGS_INTERFACE_MASK
Definition packet.h:126
@ EPACKET_FLAGS_ACK_REQUEST
Definition packet.h:119
@ EPACKET_FLAGS_CLOUD_FORWARDING
Definition packet.h:121
@ EPACKET_FORWARD_AUTO_CONN_DC_NOTIFICATION
Definition packet.h:210
@ EPACKET_FORWARD_AUTO_CONN_SUB_DATA
Definition packet.h:208
@ EPACKET_FORWARD_AUTO_CONN_PRIORITISE_UPLINK
Definition packet.h:212
@ EPACKET_FORWARD_AUTO_CONN_SINGLE_RPC
Definition packet.h:206
infuse_type
Core Infuse Data Types.
Definition types.h:26
Core Infuse-IoT platform types.
ePacket Interface API
Packet for INFUSE_EPACKET_CONN_TERMINATED.
Definition packet.h:234
uint8_t interface
Definition packet.h:236
int16_t reason
Definition packet.h:238
uint8_t address[]
Definition packet.h:240
Common header for INFUSE_EPACKET_FORWARD_AUTO_CONN.
Definition packet.h:216
uint8_t conn_timeout
Definition packet.h:224
uint8_t conn_idle_timeout
Definition packet.h:226
uint8_t destination_payload[]
Definition packet.h:230
uint8_t interface
Definition packet.h:220
uint8_t conn_absolute_timeout
Definition packet.h:228
uint8_t flags
Definition packet.h:222
uint16_t length
Definition packet.h:218
Common header for INFUSE_EPACKET_FORWARD.
Definition packet.h:195
uint8_t destination_payload[]
Definition packet.h:201
uint16_t length
Definition packet.h:197
uint8_t interface
Definition packet.h:199
Format of BLE address in INFUSE_RECEIVED_EPACKET and INFUSE_EPACKET_FORWARD.
Definition packet.h:159
uint8_t type
Definition packet.h:160
uint8_t addr[6]
Definition packet.h:161
Common config struct for all interfaces.
Definition interface.h:165
uint8_t footer_size
Definition interface.h:168
uint8_t header_size
Definition interface.h:167
Format of INFUSE_KEY_IDS packet.
Definition packet.h:135
uint8_t device_key_id[3]
Definition packet.h:136
Magic two byte packet that requests a pause in data transmission.
Definition packet.h:143
uint8_t magic
EPACKET_RATE_LIMIT_REQ_MAGIC
Definition packet.h:145
uint8_t delay_ms
Duration to pause transmission for.
Definition packet.h:147
Magic three byte packet that sets a target data throughput.
Definition packet.h:151
uint16_t target_throughput_kbps
Target data throughput in kilobits/sec.
Definition packet.h:155
uint8_t magic
EPACKET_RATE_LIMIT_REQ_MAGIC
Definition packet.h:153
Common header for INFUSE_RECEIVED_EPACKET.
Definition packet.h:166
uint8_t rssi
Definition packet.h:173
uint8_t interface
Definition packet.h:175
uint16_t len_encrypted
Definition packet.h:171
Header for INFUSE_RECEIVED_EPACKET where packet was decrypted.
Definition packet.h:179
uint16_t sequence
Definition packet.h:189
uint16_t flags
Definition packet.h:187
uint8_t type
Definition packet.h:185
uint64_t device_id
Definition packet.h:181
uint32_t gps_time
Definition packet.h:183
uint8_t key_id[3]
Definition packet.h:191
Definition packet.h:88
enum infuse_type type
Definition packet.h:98
uint16_t flags
Definition packet.h:100
uint32_t key_identifier
Definition packet.h:94
enum epacket_auth auth
Definition packet.h:96
uint32_t packet_gps_time
Definition packet.h:92
uint16_t sequence
Definition packet.h:110
uint64_t packet_device_id
Definition packet.h:90
int16_t rssi
Definition packet.h:108
union epacket_interface_address interface_address
Definition packet.h:106
const struct device * interface
Definition packet.h:102
enum epacket_interface_id interface_id
Definition packet.h:104
Definition packet.h:67
epacket_tx_done_cb tx_done
Definition packet.h:72
enum infuse_type type
Definition packet.h:78
enum epacket_auth auth
Definition packet.h:76
union epacket_interface_address interface_address
Definition packet.h:84
uint16_t flags
Definition packet.h:80
uint16_t sequence
Definition packet.h:82
void * tx_done_user_data
Definition packet.h:74
Definition packet.h:47
bt_addr_le_t bluetooth
Definition packet.h:49