Skip to content

Commit ea8ed39

Browse files
format with clang-format
1 parent e217c9f commit ea8ed39

File tree

7 files changed

+379
-311
lines changed

7 files changed

+379
-311
lines changed

examples/main.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
#include <stdio.h>
1+
#include "../include/space_packet.h"
22
#include <stdint.h>
3+
#include <stdio.h>
34
#include <string.h>
4-
#include "../include/space_packet.h"
55

66
int main(void) {
7-
const uint8_t payload[] = { 'H','e','l','l','o',' ', 'S','P' };
8-
sp_packet_t pkt = {0};
9-
pkt.ph.apid = 0x100;
10-
pkt.ph.seq_count = 1;
11-
/* Example with a minimal secondary header containing flags and zero extra bytes.
12-
* flags byte LSB=1 requests CRC; byte1=0 indicates zero remaining sec header bytes.
13-
*/
14-
const uint8_t sec_hdr[] = { 0x1, 0x0 };
15-
pkt.ph.sec_hdr_flag = 1;
16-
pkt.sec_hdr = sec_hdr;
17-
pkt.sec_hdr_len = sizeof(sec_hdr);
18-
pkt.payload = payload;
19-
pkt.payload_len = sizeof(payload);
7+
const uint8_t payload[] = {'H', 'e', 'l', 'l', 'o', ' ', 'S', 'P'};
8+
sp_packet_t pkt = {0};
9+
pkt.ph.apid = 0x100;
10+
pkt.ph.seq_count = 1;
11+
/* Example with a minimal secondary header containing flags and zero extra
12+
* bytes. flags byte LSB=1 requests CRC; byte1=0 indicates zero remaining sec
13+
* header bytes.
14+
*/
15+
const uint8_t sec_hdr[] = {0x1, 0x0};
16+
pkt.ph.sec_hdr_flag = 1;
17+
pkt.sec_hdr = sec_hdr;
18+
pkt.sec_hdr_len = sizeof(sec_hdr);
19+
pkt.payload = payload;
20+
pkt.payload_len = sizeof(payload);
2021

21-
uint8_t buf[256];
22-
size_t n = sp_packet_serialize(&pkt, buf, sizeof(buf));
23-
if (n == 0) {
24-
printf("serialize failed\n");
25-
return 1;
26-
}
22+
uint8_t buf[256];
23+
size_t n = sp_packet_serialize(&pkt, buf, sizeof(buf));
24+
if (n == 0) {
25+
printf("serialize failed\n");
26+
return 1;
27+
}
2728

28-
printf("Serialized %zu bytes:\n", n);
29-
for (size_t i = 0; i < n; ++i) printf("%02X ", buf[i]);
30-
printf("\n");
29+
printf("Serialized %zu bytes:\n", n);
30+
for (size_t i = 0; i < n; ++i)
31+
printf("%02X ", buf[i]);
32+
printf("\n");
3133

32-
sp_packet_t parsed;
33-
if (!sp_packet_parse(&parsed, buf, n)) {
34-
printf("parse failed\n");
35-
return 2;
36-
}
34+
sp_packet_t parsed;
35+
if (!sp_packet_parse(&parsed, buf, n)) {
36+
printf("parse failed\n");
37+
return 2;
38+
}
3739

38-
printf("Parsed APID=0x%03X seq=%u payload_len=%u\n",
39-
parsed.ph.apid, parsed.ph.seq_count, parsed.payload_len);
40-
printf("Payload as ASCII: ");
41-
fwrite(parsed.payload, 1, parsed.payload_len, stdout);
42-
printf("\n");
40+
printf("Parsed APID=0x%03X seq=%u payload_len=%u\n", parsed.ph.apid,
41+
parsed.ph.seq_count, parsed.payload_len);
42+
printf("Payload as ASCII: ");
43+
fwrite(parsed.payload, 1, parsed.payload_len, stdout);
44+
printf("\n");
4345

44-
return 0;
46+
return 0;
4547
}

examples/spacepacket_example

16 KB
Binary file not shown.

include/space_packet.h

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#ifndef SPACE_PACKET_H
55
#define SPACE_PACKET_H
66

7-
#include <stdint.h>
87
#include <stddef.h>
8+
#include <stdint.h>
99

1010
/* Primary header is 6 bytes (CCSDS-like):
1111
* - bytes 0-1: version(3), type(1), sec_hdr(1), apid(11)
@@ -14,55 +14,61 @@
1414
*/
1515

1616
typedef struct {
17-
/* Primary header represented as bitfields (CCSDS-like) */
17+
/* Primary header represented as bitfields (CCSDS-like) */
1818
#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;
2323
#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;
2828
#endif
29-
/* sequence control */
29+
/* sequence control */
3030
#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;
3333
#else
34-
unsigned seq_count:14;
35-
unsigned seq_flags:2;
34+
unsigned seq_count : 14;
35+
unsigned seq_flags : 2;
3636
#endif
37-
uint16_t packet_length;
37+
uint16_t packet_length;
3838
} sp_primary_header_t;
3939

4040
typedef struct {
41-
sp_primary_header_t ph; /* primary header (bitwise representation) */
41+
sp_primary_header_t ph; /* primary header (bitwise representation) */
4242

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+
*/
5254
} sp_packet_t;
5355

5456
/* Return required buffer size to serialize this packet (header + payload) */
5557
size_t sp_packet_serialize_size(const sp_packet_t *pkt);
5658

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);
5963

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.
6267
*/
6368
int sp_packet_parse(sp_packet_t *out, uint8_t *buf, size_t buf_len);
6469

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. */
6672
uint16_t sp_crc16_ccitt(const uint8_t *data, size_t len);
6773

6874
#endif /* SPACE_PACKET_H */

0 commit comments

Comments
 (0)