Loading...
Searching...
No Matches
packet.c
1/***********************************************************************************
2 * @file packet.c *
3 * @brief *
4 * *
5 * @{ *
6 ***********************************************************************************/
7
8#include "packet.h"
9
10#include "stdbool.h"
11#include "string.h"
12
13static size_t _Packet_getSize(Packet *packet);
14
15/* =============================================================================== */
24bool Packet_asBytes(Packet *packet, uint8_t *out, uint8_t size) {
25 // Early exit with false if output array size is too small
26 if (size < _Packet_getSize(packet))
27 return false;
28
29 // First byte is always packet identifier
30 out[0] = packet->id;
31
32 // Iterate through each field, copy data to output array
33 for (int i = 0, j = 1; i < packet->length; i++) {
34 memcpy(&out[j], packet->fields[i].data, packet->fields[i].size);
35 j += packet->fields[i].size;
36 }
37
38 return true;
39}
40
41/* =============================================================================== */
50static size_t _Packet_getSize(Packet *packet) {
51 size_t size = 1;
52
53 for (int i = 0; i < packet->length; i++)
54 size += packet->fields[i].size;
55
56 return size;
57}
58
bool Packet_asBytes(Packet *packet, uint8_t *out, uint8_t size)
Definition packet.c:24