feat(setprotocol): define system payload schemas

This commit is contained in:
2026-09-01 11:47:08 +03:00
parent 19becd7b8c
commit 9b636a9f40
5 changed files with 279 additions and 3 deletions

View File

@@ -15,6 +15,12 @@ uint32_t setp_get_u32(const uint8_t *data)
| ((uint32_t)data[3] << 24);
}
uint64_t setp_get_u64(const uint8_t *data)
{
return (uint64_t)setp_get_u32(data)
| ((uint64_t)setp_get_u32(&data[4]) << 32);
}
void setp_put_u16(uint8_t *data, uint16_t value)
{
data[0] = (uint8_t)(value & 0xFFU);
@@ -29,6 +35,12 @@ void setp_put_u32(uint8_t *data, uint32_t value)
data[3] = (uint8_t)(value >> 24);
}
void setp_put_u64(uint8_t *data, uint64_t value)
{
setp_put_u32(data, (uint32_t)(value & 0xFFFFFFFFULL));
setp_put_u32(&data[4], (uint32_t)(value >> 32));
}
uint32_t setp_crc32(const uint8_t *data, size_t length)
{
uint32_t crc = 0xFFFFFFFFUL;
@@ -278,3 +290,96 @@ bool setp_status_decode(const setp_frame_t *frame, uint16_t *status,
}
return true;
}
size_t setp_device_info_encode(const setp_device_info_t *info,
uint8_t *out, size_t capacity)
{
size_t total;
if ((info == NULL) || (out == NULL)
|| (info->schema_version != SETP_DEVICE_INFO_SCHEMA_VERSION)
|| (info->model_length > SETP_DEVICE_MODEL_MAX)
|| ((info->model_length != 0U) && (info->model == NULL))) {
return 0U;
}
total = (size_t)SETP_DEVICE_INFO_FIXED_SIZE + info->model_length;
if (capacity < total) {
return 0U;
}
setp_put_u16(&out[0], info->schema_version);
setp_put_u16(&out[2], info->device_class);
setp_put_u32(&out[4], info->hardware_version);
setp_put_u32(&out[8], info->firmware_version);
setp_put_u32(&out[12], info->dictionary_version);
setp_put_u64(&out[16], info->serial_number);
out[24] = info->model_length;
if (info->model_length != 0U) {
(void)memcpy(&out[SETP_DEVICE_INFO_FIXED_SIZE],
info->model, info->model_length);
}
return total;
}
bool setp_device_info_decode(const uint8_t *data, uint16_t length,
setp_device_info_t *out)
{
uint8_t model_length;
if ((data == NULL) || (out == NULL) || (length < SETP_DEVICE_INFO_FIXED_SIZE)) {
return false;
}
model_length = data[24];
if ((setp_get_u16(&data[0]) != SETP_DEVICE_INFO_SCHEMA_VERSION)
|| (model_length > SETP_DEVICE_MODEL_MAX)
|| (length != (uint16_t)(SETP_DEVICE_INFO_FIXED_SIZE + model_length))) {
return false;
}
out->schema_version = setp_get_u16(&data[0]);
out->device_class = setp_get_u16(&data[2]);
out->hardware_version = setp_get_u32(&data[4]);
out->firmware_version = setp_get_u32(&data[8]);
out->dictionary_version = setp_get_u32(&data[12]);
out->serial_number = setp_get_u64(&data[16]);
out->model_length = model_length;
out->model = &data[SETP_DEVICE_INFO_FIXED_SIZE];
return true;
}
size_t setp_capabilities_encode(const setp_capabilities_t *capabilities,
uint8_t *out, size_t capacity)
{
if ((capabilities == NULL) || (out == NULL)
|| (capacity < SETP_CAPABILITIES_SIZE)
|| (capabilities->schema_version != SETP_CAPABILITIES_SCHEMA_VERSION)
|| (capabilities->max_payload == 0U)
|| (capabilities->max_payload > SETP_MAX_PAYLOAD)) {
return 0U;
}
setp_put_u16(&out[0], capabilities->schema_version);
setp_put_u16(&out[2], capabilities->max_payload);
setp_put_u32(&out[4], capabilities->interface_mask);
setp_put_u32(&out[8], capabilities->feature_flags);
setp_put_u16(&out[12], capabilities->max_read_items);
setp_put_u16(&out[14], capabilities->max_write_items);
setp_put_u16(&out[16], capabilities->max_subscriptions);
setp_put_u16(&out[18], capabilities->max_publish_items);
return SETP_CAPABILITIES_SIZE;
}
bool setp_capabilities_decode(const uint8_t *data, uint16_t length,
setp_capabilities_t *out)
{
if ((data == NULL) || (out == NULL) || (length != SETP_CAPABILITIES_SIZE)
|| (setp_get_u16(&data[0]) != SETP_CAPABILITIES_SCHEMA_VERSION)
|| (setp_get_u16(&data[2]) == 0U)
|| (setp_get_u16(&data[2]) > SETP_MAX_PAYLOAD)) {
return false;
}
out->schema_version = setp_get_u16(&data[0]);
out->max_payload = setp_get_u16(&data[2]);
out->interface_mask = setp_get_u32(&data[4]);
out->feature_flags = setp_get_u32(&data[8]);
out->max_read_items = setp_get_u16(&data[12]);
out->max_write_items = setp_get_u16(&data[14]);
out->max_subscriptions = setp_get_u16(&data[16]);
out->max_publish_items = setp_get_u16(&data[18]);
return true;
}