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
12
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/security_ids.h>
25#include <infuse/types.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
37
39 /* Packet failed to decrypt */
41 /* Packet is encrypted for remote device */
45} __packed;
46
47/* Interface specific addresses */
49 /* Bluetooth LE address */
50 bt_addr_le_t bluetooth;
51};
52
53/* Empty interface address */
54#define EPACKET_ADDR_ALL ((union epacket_interface_address){0})
55
64typedef void (*epacket_tx_done_cb)(const struct device *dev, struct net_buf *pkt, int result,
65 void *user_data);
66
67/* Metadata for packets that will be transmitted */
69#ifdef CONFIG_EPACKET_BUFFERS_TX_DELAYABLE_WORK
70 struct k_work_delayable dwork;
71#endif
72 /* Callback run when TX completes */
74 /* Context provided to @a tx_done */
76 /* Authentication level of packet */
78 /* Packet type */
80 /* Key identifier to use to encrypt packet for the auth level */
82 /* Flags to apply to packet */
83 uint16_t flags;
84 /* Sequence number used for packet */
85 uint16_t sequence;
86 /* Interface specific address */
88};
89
90/* Metadata for packets that have been received */
92 /* Device ID in packet */
94 /* Local time packet was received */
95 k_ticks_t rx_timestamp;
96 /* GPS time in packet */
98 /* Key ID used by packet */
100 /* Authentication level of packet */
102 /* Type of packet */
104 /* Flags associated with packet */
105 uint16_t flags;
106 /* ePacket interface packet was received on */
107 const struct device *interface;
108 /* Numerical ID for interface */
110 /* Interface specific address */
112 /* RSSI of packet (0 = 0dBm, 20 = 20dBm, etc) */
113 int16_t rssi;
114 /* Sequence number of packet */
115 uint16_t sequence;
116};
117
118/* Global ePacket flags */
120 /* Bit 15: Encryption Type */
123 /* Bit 14: Transmitting device requests an ACK */
125 /* Bit 13: Device can forward data to the cloud */
127 /* Bit 12: Device sends its own data to the cloud */
129 /* Bits 8-11: Reserved */
130 /* Bits 0-7: Interface specific */
132};
133
137#define EPACKET_KEY_ID_REQ_MAGIC 0x4D
138
141 uint8_t device_key_id[3];
142} __packed;
143
145#define EPACKET_RATE_LIMIT_REQ_MAGIC 0x4E
146
150 uint8_t magic;
152 uint8_t delay_ms;
153} __packed;
154
158 uint8_t magic;
161} __packed;
162
165 uint8_t type;
166 uint8_t addr[6];
167} __packed;
168BUILD_ASSERT(sizeof(struct epacket_interface_address_bt_le) == 7);
169
172 /* Bit 16: 1 when packet is still encrypted
173 * 0 when packet is decrypted
174 * Bits 0-15: Total length of headers + data
175 */
177 /* Received packet signal strength (0 - val) */
178 uint8_t rssi;
179 /* Value from `EPACKET_INTERFACE_*` */
180 uint8_t interface;
181} __packed;
182
185 /* Device ID in the packet */
186 uint64_t device_id;
187 /* GPS time in the packet */
188 uint32_t gps_time;
189 /* Packet type */
190 uint8_t type;
191 /* Packet flags */
192 uint16_t flags;
193 /* Sequence number */
194 uint16_t sequence;
195 /* ID associated with the key */
196 uint8_t key_id[3];
197} __packed;
198
201 /* Total length of this header + payload */
202 uint16_t length;
203 /* Value from `EPACKET_INTERFACE_*` */
204 uint8_t interface;
205 /* Destination interface address + packet bytes */
207} __packed;
208
210 /* Automatically disconnect on the first received INFUSE_RPC_RSP */
212 /* Subscribe to data while connected */
214 /* Send a INFUSE_EPACKET_CONN_TERMINATED on connection terminated */
216 /* Prioritise uplink throughput to the connection associated with this request */
218} __packed;
219
222 /* Total length of this header + payload */
223 uint16_t length;
224 /* Value from `EPACKET_INTERFACE_*` */
225 uint8_t interface;
226 /* Value from `EPACKET_FORWARD_AUTO_CONN_` */
227 uint8_t flags;
228 /* Connection timeout (seconds) */
230 /* Connection idle timeout (seconds) */
232 /* Unconditional connection timeout (seconds) */
234 /* Destination interface address + packet bytes */
236} __packed;
237
240 /* Value from `EPACKET_INTERFACE_*` */
241 uint8_t interface;
242 /* Reason that interface disconnected */
243 int16_t reason;
244 /* Interface address that disconnected */
245 uint8_t address[];
246} __packed;
247
252
259void epacket_rate_limit_tx(k_ticks_t *last_call, uint16_t bytes_transmitted);
260
268void epacket_global_flags_set(uint16_t flags);
269
276
283
290
301struct net_buf *epacket_alloc_tx(k_timeout_t timeout);
302
310struct net_buf *epacket_alloc_rx(k_timeout_t timeout);
311
320static inline struct net_buf *epacket_alloc_tx_for_interface(const struct device *dev,
321 k_timeout_t timeout)
322{
323 const struct epacket_interface_common_config *config = dev->config;
324 struct net_buf *buf = epacket_alloc_tx(timeout);
325 uint16_t size;
326
327 if (buf == NULL) {
328 return NULL;
329 }
330 /* Reserve space for header */
331 net_buf_reserve(buf, config->header_size);
332 /* Limit size based on interface */
334 if (size > (config->header_size + config->footer_size)) {
335 /* Hacky reservation for footer, automatically reversed by epacket_queue */
336 buf->size = size - config->footer_size;
337 } else {
338 /* 0 payload size */
339 buf->size = config->header_size;
340 }
341 return buf;
342}
343
354static inline void epacket_set_tx_metadata_core(struct net_buf *buf, enum epacket_auth auth,
355 uint32_t key_identifier, uint16_t flags,
356 enum infuse_type type,
357 union epacket_interface_address dest)
358{
359 struct epacket_tx_metadata *meta = net_buf_user_data(buf);
360
361 meta->auth = auth;
364 meta->type = type;
365 meta->tx_done = NULL;
366 meta->interface_address = dest;
367}
368
380static inline void epacket_set_tx_metadata(struct net_buf *buf, enum epacket_auth auth,
381 uint16_t flags, enum infuse_type type,
382 union epacket_interface_address dest)
383{
384 uint32_t key_id = 0x00;
385
386 if (auth == EPACKET_AUTH_DEVICE) {
388 } else if (EPACKET_AUTH_NETWORK) {
390 }
391
392 epacket_set_tx_metadata_core(buf, auth, key_id, flags, type, dest);
393}
394
402static inline void epacket_set_tx_callback(struct net_buf *buf, epacket_tx_done_cb tx_done,
403 void *user_data)
404{
405 struct epacket_tx_metadata *meta = net_buf_user_data(buf);
406
407 meta->tx_done = tx_done;
408 meta->tx_done_user_data = user_data;
409}
410
420int epacket_received_packet_append(struct net_buf *storage_buf, struct net_buf *received_buf);
421
425
426#ifdef __cplusplus
427}
428#endif
429
430#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:218
epacket_auth
Definition packet.h:38
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:119
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:64
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:380
static void epacket_set_tx_metadata_core(struct net_buf *buf, enum epacket_auth auth, uint32_t key_identifier, uint16_t flags, enum infuse_type type, union epacket_interface_address dest)
Set all metadata on a packet.
Definition packet.h:354
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:320
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:402
epacket_forward_auto_conn_flags
Definition packet.h:209
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:43
@ EPACKET_AUTH_DEVICE
Definition packet.h:44
@ EPACKET_AUTH_FAILURE
Definition packet.h:40
@ EPACKET_AUTH_REMOTE_ENCRYPTED
Definition packet.h:42
@ EPACKET_FLAGS_ENCRYPTION_NETWORK
Definition packet.h:122
@ EPACKET_FLAGS_CLOUD_SELF
Definition packet.h:128
@ EPACKET_FLAGS_ENCRYPTION_DEVICE
Definition packet.h:121
@ EPACKET_FLAGS_INTERFACE_MASK
Definition packet.h:131
@ EPACKET_FLAGS_ACK_REQUEST
Definition packet.h:124
@ EPACKET_FLAGS_CLOUD_FORWARDING
Definition packet.h:126
@ EPACKET_FORWARD_AUTO_CONN_DC_NOTIFICATION
Definition packet.h:215
@ EPACKET_FORWARD_AUTO_CONN_SUB_DATA
Definition packet.h:213
@ EPACKET_FORWARD_AUTO_CONN_PRIORITISE_UPLINK
Definition packet.h:217
@ EPACKET_FORWARD_AUTO_CONN_SINGLE_RPC
Definition packet.h:211
uint32_t infuse_security_network_key_identifier(void)
Get the current network key identifier.
uint32_t infuse_security_device_key_identifier(void)
Get the current device key identifier.
infuse_type
Core Infuse Data Types.
Definition types.h:26
Core Infuse-IoT platform types.
ePacket Interface API
Infuse Platform Security Identifiers.
Packet for INFUSE_EPACKET_CONN_TERMINATED.
Definition packet.h:239
uint8_t interface
Definition packet.h:241
int16_t reason
Definition packet.h:243
uint8_t address[]
Definition packet.h:245
Common header for INFUSE_EPACKET_FORWARD_AUTO_CONN.
Definition packet.h:221
uint8_t conn_timeout
Definition packet.h:229
uint8_t conn_idle_timeout
Definition packet.h:231
uint8_t destination_payload[]
Definition packet.h:235
uint8_t interface
Definition packet.h:225
uint8_t conn_absolute_timeout
Definition packet.h:233
uint8_t flags
Definition packet.h:227
uint16_t length
Definition packet.h:223
Common header for INFUSE_EPACKET_FORWARD.
Definition packet.h:200
uint8_t destination_payload[]
Definition packet.h:206
uint16_t length
Definition packet.h:202
uint8_t interface
Definition packet.h:204
Format of BLE address in INFUSE_RECEIVED_EPACKET and INFUSE_EPACKET_FORWARD.
Definition packet.h:164
uint8_t type
Definition packet.h:165
uint8_t addr[6]
Definition packet.h:166
Common config struct for all interfaces.
Definition interface.h:166
uint8_t footer_size
Definition interface.h:169
uint8_t header_size
Definition interface.h:168
Format of INFUSE_KEY_IDS packet.
Definition packet.h:140
uint8_t device_key_id[3]
Definition packet.h:141
Magic two byte packet that requests a pause in data transmission.
Definition packet.h:148
uint8_t magic
EPACKET_RATE_LIMIT_REQ_MAGIC
Definition packet.h:150
uint8_t delay_ms
Duration to pause transmission for.
Definition packet.h:152
Magic three byte packet that sets a target data throughput.
Definition packet.h:156
uint16_t target_throughput_kbps
Target data throughput in kilobits/sec.
Definition packet.h:160
uint8_t magic
EPACKET_RATE_LIMIT_REQ_MAGIC
Definition packet.h:158
Common header for INFUSE_RECEIVED_EPACKET.
Definition packet.h:171
uint8_t rssi
Definition packet.h:178
uint8_t interface
Definition packet.h:180
uint16_t len_encrypted
Definition packet.h:176
Header for INFUSE_RECEIVED_EPACKET where packet was decrypted.
Definition packet.h:184
uint16_t sequence
Definition packet.h:194
uint16_t flags
Definition packet.h:192
uint8_t type
Definition packet.h:190
uint64_t device_id
Definition packet.h:186
uint32_t gps_time
Definition packet.h:188
uint8_t key_id[3]
Definition packet.h:196
Definition packet.h:91
enum infuse_type type
Definition packet.h:103
uint16_t flags
Definition packet.h:105
k_ticks_t rx_timestamp
Definition packet.h:95
uint32_t key_identifier
Definition packet.h:99
enum epacket_auth auth
Definition packet.h:101
uint32_t packet_gps_time
Definition packet.h:97
uint16_t sequence
Definition packet.h:115
uint64_t packet_device_id
Definition packet.h:93
int16_t rssi
Definition packet.h:113
union epacket_interface_address interface_address
Definition packet.h:111
const struct device * interface
Definition packet.h:107
enum epacket_interface_id interface_id
Definition packet.h:109
Definition packet.h:68
epacket_tx_done_cb tx_done
Definition packet.h:73
enum infuse_type type
Definition packet.h:79
enum epacket_auth auth
Definition packet.h:77
union epacket_interface_address interface_address
Definition packet.h:87
uint16_t flags
Definition packet.h:83
uint32_t key_identifier
Definition packet.h:81
uint16_t sequence
Definition packet.h:85
void * tx_done_user_data
Definition packet.h:75
Definition packet.h:48
bt_addr_le_t bluetooth
Definition packet.h:50