-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "sbmp_config.h" | ||
#include "payload_builder.h" | ||
#include "type_coerce.h" | ||
|
||
#define check_capacity(pb_p, needed) \ | ||
if ((pb_p)->ptr + (needed) > (pb_p)->capacity) { \ | ||
pb_overflow(); \ | ||
return false; \ | ||
} | ||
|
||
|
||
static FLASH_FN void pb_overflow(void) | ||
{ | ||
sbmp_error("PayloadBuilder overflow."); | ||
} | ||
|
||
/** Write uint8_t to the buffer */ | ||
bool FLASH_FN pb_u8(PayloadBuilder *pb, uint8_t byte) | ||
{ | ||
check_capacity(pb, sizeof(uint8_t)); | ||
pb->buf[pb->ptr++] = byte; | ||
return true; | ||
} | ||
|
||
/** Write uint16_t to the buffer. */ | ||
bool FLASH_FN pb_u16(PayloadBuilder *pb, uint16_t word) | ||
{ | ||
check_capacity(pb, sizeof(uint16_t)); | ||
pb->buf[pb->ptr++] = word & 0xFF; | ||
pb->buf[pb->ptr++] = (word >> 8) & 0xFF; | ||
return true; | ||
} | ||
|
||
/** Write uint32_t to the buffer. */ | ||
bool FLASH_FN pb_u32(PayloadBuilder *pb, uint32_t word) | ||
{ | ||
check_capacity(pb, sizeof(uint32_t)); | ||
pb->buf[pb->ptr++] = word & 0xFF; | ||
pb->buf[pb->ptr++] = (word >> 8) & 0xFF; | ||
pb->buf[pb->ptr++] = (word >> 16) & 0xFF; | ||
pb->buf[pb->ptr++] = (word >> 24) & 0xFF; | ||
return true; | ||
} | ||
|
||
/** Write int8_t to the buffer. */ | ||
bool FLASH_FN pb_i8(PayloadBuilder *pb, int8_t byte) | ||
{ | ||
return pb_u8(pb, ((union conv8){.i8 = byte}).u8); | ||
} | ||
|
||
/** Write int16_t to the buffer. */ | ||
bool FLASH_FN pb_i16(PayloadBuilder *pb, int16_t word) | ||
{ | ||
return pb_u16(pb, ((union conv16){.i16 = word}).u16); | ||
} | ||
|
||
/** Write int32_t to the buffer. */ | ||
bool FLASH_FN pb_i32(PayloadBuilder *pb, int32_t word) | ||
{ | ||
return pb_u32(pb, ((union conv32){.i32 = word}).u32); | ||
} | ||
|
||
/** Write 4-byte float to the buffer. */ | ||
bool FLASH_FN pb_float(PayloadBuilder *pb, float f) | ||
{ | ||
return pb_u32(pb, ((union conv32){.f32 = f}).u32); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef PAYLOAD_BUILDER_H | ||
#define PAYLOAD_BUILDER_H | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
/** | ||
* This module can help you build payload arrays | ||
* | ||
* The builder performs bounds checking and prints error | ||
* message on overflow for easy debugging. | ||
*/ | ||
|
||
typedef struct { | ||
uint8_t *buf; | ||
size_t ptr; | ||
size_t capacity; | ||
} PayloadBuilder; | ||
|
||
#define pb_start(buf, capacity) ((PayloadBuilder){buf, 0, capacity}) | ||
#define pb_length(pb_p) ((pb_p)->ptr) | ||
|
||
/** Write uint8_t to the buffer */ | ||
bool pb_u8(PayloadBuilder *pb, uint8_t byte); | ||
|
||
/** Write uint16_t to the buffer. */ | ||
bool pb_u16(PayloadBuilder *pb, uint16_t word); | ||
|
||
/** Write uint32_t to the buffer. */ | ||
bool pb_u32(PayloadBuilder *pb, uint32_t word); | ||
|
||
/** Write char (int8_t) to the buffer. */ | ||
#define pb_char(pb, c) pb_i8(pb, c) | ||
|
||
/** Write int8_t to the buffer. */ | ||
bool pb_i8(PayloadBuilder *pb, int8_t byte); | ||
|
||
/** Write int16_t to the buffer. */ | ||
bool pb_i16(PayloadBuilder *pb, int16_t word); | ||
|
||
/** Write int32_t to the buffer. */ | ||
bool pb_i32(PayloadBuilder *pb, int32_t word); | ||
|
||
/** Write 4-byte float to the buffer. */ | ||
bool pb_float(PayloadBuilder *pb, float f); | ||
|
||
#endif // PAYLOAD_BUILDER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,6 @@ | |
#include "sbmp_bulk.h" | ||
|
||
#include "payload_parser.h" | ||
#include "payload_builder.h" | ||
|
||
#endif /* SBMP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef TYPE_COERCE_H | ||
#define TYPE_COERCE_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
// Structs for conversion between types | ||
|
||
union conv8 { | ||
uint8_t u8; | ||
int8_t i8; | ||
}; | ||
|
||
union conv16 { | ||
uint16_t u16; | ||
int16_t i16; | ||
}; | ||
|
||
union conv32 { | ||
uint32_t u32; | ||
int32_t i32; | ||
float f32; | ||
}; | ||
|
||
#endif // TYPE_COERCE_H |