96 lines
2.6 KiB
C
96 lines
2.6 KiB
C
#include "nh_helper_serial.h"
|
|
|
|
#include <string.h>
|
|
|
|
#define SERIAL_EP_NAME(x) (nh_serial_ep_names[x])
|
|
#define SERIAL_EP_SIZE(x) (strlen(nh_serial_ep_names[x]))
|
|
|
|
static const char *nh_serial_ep_names[] = {
|
|
[NH_SERIAL_EP_RESP] = "ctrlResp",
|
|
[NH_SERIAL_EP_EVENT] = "ctrlEvnt",
|
|
};
|
|
|
|
/* In the SERIAL I/F, a different TLV is used:
|
|
* | 1 | 2 | N |
|
|
* | Tag | Length | Value |
|
|
* Each TLV contains 2 fields, "EP name" and "Data"
|
|
*/
|
|
|
|
nh_ret_t nh_serial_get_type(nh_serial_ep_type_t *type, uint8_t *buf) {
|
|
uint8_t tag = buf[0];
|
|
uint8_t length = (buf[2] << 8U) | buf[1];
|
|
char *value = (char *)&buf[3];
|
|
|
|
if (tag != NH_SERIAL_TAG_EP_NAME) {
|
|
return NH_RET_FAIL;
|
|
}
|
|
|
|
if (length == 0) {
|
|
return NH_RET_FAIL;
|
|
}
|
|
|
|
if (strncmp(value, SERIAL_EP_NAME(NH_SERIAL_EP_RESP), SERIAL_EP_SIZE(NH_SERIAL_EP_RESP)) == 0) {
|
|
*type = NH_SERIAL_EP_RESP;
|
|
|
|
return NH_RET_SUCCESS;
|
|
}
|
|
|
|
if (strncmp(value, SERIAL_EP_NAME(NH_SERIAL_EP_EVENT), SERIAL_EP_SIZE(NH_SERIAL_EP_EVENT)) == 0) {
|
|
*type = NH_SERIAL_EP_EVENT;
|
|
|
|
return NH_RET_SUCCESS;
|
|
}
|
|
|
|
return NH_RET_FAIL;
|
|
}
|
|
|
|
nh_ret_t nh_serial_get_payload(uint8_t *buf, uint8_t **payload, uint16_t *length) {
|
|
uint8_t name_tag = buf[0];
|
|
uint8_t name_len = (buf[2] << 8U) | buf[1];
|
|
|
|
uint16_t data_tag_offset = 3 + name_len;
|
|
|
|
uint8_t data_tag = buf[data_tag_offset];
|
|
uint16_t data_len = (buf[data_tag_offset + 2] << 8U) | buf[data_tag_offset + 1];
|
|
uint8_t *data_value = &buf[data_tag_offset + 3];
|
|
|
|
if (name_tag != NH_SERIAL_TAG_EP_NAME) {
|
|
return NH_RET_FAIL;
|
|
}
|
|
|
|
if (data_tag != NH_SERIAL_TAG_DATA) {
|
|
return NH_RET_FAIL;
|
|
}
|
|
|
|
*payload = data_value;
|
|
*length = data_len;
|
|
|
|
return NH_RET_SUCCESS;
|
|
}
|
|
|
|
nh_ret_t nh_serial_header_length(nh_serial_ep_type_t type, uint16_t *header_length) {
|
|
*header_length = (6 + SERIAL_EP_SIZE(type));
|
|
return NH_RET_SUCCESS;
|
|
}
|
|
|
|
nh_ret_t nh_serial_write_header(nh_serial_ep_type_t type, uint8_t *buf, uint16_t data_len, uint8_t **data_start) {
|
|
uint16_t name_len = SERIAL_EP_SIZE(type);
|
|
uint8_t *name_buf = &buf[3];
|
|
|
|
buf[0] = NH_SERIAL_TAG_EP_NAME;
|
|
buf[1] = name_len & 0xFFU;
|
|
buf[2] = (name_len >> 8U) & 0xFFU;
|
|
|
|
memcpy(name_buf, SERIAL_EP_NAME(type), name_len);
|
|
|
|
uint8_t data_tag_offset = 3 + name_len;
|
|
uint8_t *data_buf = &buf[data_tag_offset + 3];
|
|
|
|
buf[data_tag_offset] = NH_SERIAL_TAG_DATA;
|
|
buf[data_tag_offset + 1] = data_len & 0xFFU;
|
|
buf[data_tag_offset + 2] = (data_len >> 8U) & 0xFFU;
|
|
|
|
*data_start = data_buf;
|
|
|
|
return NH_RET_SUCCESS;
|
|
} |