|
4 | 4 | #ifndef SPACE_PACKET_H |
5 | 5 | #define SPACE_PACKET_H |
6 | 6 |
|
7 | | -#include <stdint.h> |
8 | 7 | #include <stddef.h> |
| 8 | +#include <stdint.h> |
9 | 9 |
|
10 | 10 | /* Primary header is 6 bytes (CCSDS-like): |
11 | 11 | * - bytes 0-1: version(3), type(1), sec_hdr(1), apid(11) |
|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | typedef struct { |
17 | | - /* Primary header represented as bitfields (CCSDS-like) */ |
| 17 | + /* Primary header represented as bitfields (CCSDS-like) */ |
18 | 18 | #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
19 | | - unsigned version:3; |
20 | | - unsigned type:1; |
21 | | - unsigned sec_hdr_flag:1; /* whether secondary header present */ |
22 | | - unsigned apid:11; |
| 19 | + unsigned version : 3; |
| 20 | + unsigned type : 1; |
| 21 | + unsigned sec_hdr_flag : 1; /* whether secondary header present */ |
| 22 | + unsigned apid : 11; |
23 | 23 | #else |
24 | | - unsigned apid:11; |
25 | | - unsigned sec_hdr_flag:1; |
26 | | - unsigned type:1; |
27 | | - unsigned version:3; |
| 24 | + unsigned apid : 11; |
| 25 | + unsigned sec_hdr_flag : 1; |
| 26 | + unsigned type : 1; |
| 27 | + unsigned version : 3; |
28 | 28 | #endif |
29 | | - /* sequence control */ |
| 29 | + /* sequence control */ |
30 | 30 | #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
31 | | - unsigned seq_flags:2; |
32 | | - unsigned seq_count:14; |
| 31 | + unsigned seq_flags : 2; |
| 32 | + unsigned seq_count : 14; |
33 | 33 | #else |
34 | | - unsigned seq_count:14; |
35 | | - unsigned seq_flags:2; |
| 34 | + unsigned seq_count : 14; |
| 35 | + unsigned seq_flags : 2; |
36 | 36 | #endif |
37 | | - uint16_t packet_length; |
| 37 | + uint16_t packet_length; |
38 | 38 | } sp_primary_header_t; |
39 | 39 |
|
40 | 40 | typedef struct { |
41 | | - sp_primary_header_t ph; /* primary header (bitwise representation) */ |
| 41 | + sp_primary_header_t ph; /* primary header (bitwise representation) */ |
42 | 42 |
|
43 | | - const uint8_t *sec_hdr; /* secondary header pointer (if ph.bits.sec_hdr_flag), as provided */ |
44 | | - uint16_t sec_hdr_len; /* total secondary header length in bytes (>=2 when present) |
45 | | - * Layout: byte0 = flags, byte1 = remaining_sec_len (n), followed by n bytes |
46 | | - */ |
47 | | - const uint8_t *payload; /* points into a buffer when parsed */ |
48 | | - uint16_t payload_len; |
49 | | - /* If the secondary header flags (byte0) LSB is 1, a 16-bit CRC (big-endian) |
50 | | - * is appended after payload. The CRC covers the secondary header and payload. |
51 | | - */ |
| 43 | + const uint8_t *sec_hdr; /* secondary header pointer (if ph.bits.sec_hdr_flag), |
| 44 | + as provided */ |
| 45 | + uint16_t sec_hdr_len; /* total secondary header length in bytes (>=2 when |
| 46 | + * present) Layout: byte0 = flags, byte1 = |
| 47 | + * remaining_sec_len (n), followed by n bytes |
| 48 | + */ |
| 49 | + const uint8_t *payload; /* points into a buffer when parsed */ |
| 50 | + uint16_t payload_len; |
| 51 | + /* If the secondary header flags (byte0) LSB is 1, a 16-bit CRC (big-endian) |
| 52 | + * is appended after payload. The CRC covers the secondary header and payload. |
| 53 | + */ |
52 | 54 | } sp_packet_t; |
53 | 55 |
|
54 | 56 | /* Return required buffer size to serialize this packet (header + payload) */ |
55 | 57 | size_t sp_packet_serialize_size(const sp_packet_t *pkt); |
56 | 58 |
|
57 | | -/* Serialize packet into `buf` of length `buf_len`. Returns bytes written or 0 on error. */ |
58 | | -size_t sp_packet_serialize(const sp_packet_t *pkt, uint8_t *buf, size_t buf_len); |
| 59 | +/* Serialize packet into `buf` of length `buf_len`. Returns bytes written or 0 |
| 60 | + * on error. */ |
| 61 | +size_t sp_packet_serialize(const sp_packet_t *pkt, uint8_t *buf, |
| 62 | + size_t buf_len); |
59 | 63 |
|
60 | | -/* Parse buffer into packet fields. Note: this does not allocate payload memory; it sets |
61 | | - * `out->payload` to point into `buf`. Returns 1 on success, 0 on failure. |
| 64 | +/* Parse buffer into packet fields. Note: this does not allocate payload memory; |
| 65 | + * it sets `out->payload` to point into `buf`. Returns 1 on success, 0 on |
| 66 | + * failure. |
62 | 67 | */ |
63 | 68 | int sp_packet_parse(sp_packet_t *out, uint8_t *buf, size_t buf_len); |
64 | 69 |
|
65 | | -/* Utility: compute CRC-16-CCITT (polynomial 0x1021, init 0xFFFF). Returns 16-bit CRC. */ |
| 70 | +/* Utility: compute CRC-16-CCITT (polynomial 0x1021, init 0xFFFF). Returns |
| 71 | + * 16-bit CRC. */ |
66 | 72 | uint16_t sp_crc16_ccitt(const uint8_t *data, size_t len); |
67 | 73 |
|
68 | 74 | #endif /* SPACE_PACKET_H */ |
0 commit comments