Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 26 additions & 85 deletions APP/CDH_App/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,97 +14,19 @@
#define SYSID_COMMS 3
#define ENDPOINT_COMMS_TEST 154



void simple_example(void) {
while (1) {
const uint8_t payload[10] = "Meow meow";
uint8_t buf[LFP_HEADER_SIZE + lfp_encoded_body_length(payload, sizeof(payload))];

if (lfp_encode_to_buf(SYSID_CDH, SYSID_COMMS, ENDPOINT_COMMS_TEST, LFP_DIRECTION_REQUEST,
payload,sizeof(payload),
buf,sizeof(buf)
) < 0) {
ULOG_ERROR("Failed to encode LFP packet!");
}

ULOG_INFO("LFP encoding successful: {}", ULOG_SLICE(buf));

lfp_header_t header;
if (lfp_parse_header(buf, &header) != LFP_EOK) {
ULOG_ERROR("Failed to decode LFP header!");
}

ULOG_INFO(
"LFP header decoded! \n\tInitiator: {} \n\tTarget: {} \n\tEndpoint: {} \n\tDirection: {} \n\tLen: {}",
header.initiator, header.target, header.endpoint, (uint8_t)header.direction, header.encoded_length,
);

uint8_t * p_body_buf = buf + LFP_HEADER_SIZE;
if (lfp_body_decoder(&header, p_body_buf, p_body_buf, sizeof(buf) - LFP_HEADER_SIZE) == LFP_FAIL) {
ULOG_ERROR("Failed to decode buffer");
}

ULOG_INFO("Packet decoded successfully: {}", (char *)p_body_buf);

k_msleep(2000);
}
void asn1_example_bms_system_status_req(const BMSSystemStatusRequest * p_payload, const asn1_lfp_decode_data_t * p_data) {
ULOG_INFO("Received system status request");
}

void on_msg(const lfp_header_t * p_header, const uint8_t * p_body, uint16_t body_length, void * p_ctx) {
ARG_UNUSED(p_ctx);
ARG_UNUSED(body_length);

ULOG_INFO("Decoded packet on endpoint {}: {}", p_header->endpoint, (char *)p_body);
void asn1_example_bms_system_status_res(const BMSSystemStatusResponse * p_payload, const asn1_lfp_decode_data_t * p_data) {
ULOG_INFO("Received system status response: version {}, uptime {}", p_payload->version, p_payload->uptime);
}

void on_error(const lfp_code_t reason, void * p_ctx) {
void asn1_example_stream_error_handler(lfp_code_t reason, void * p_ctx) {
ARG_UNUSED(p_ctx);

ULOG_ERROR("Stream error: {}", reason);
}

void stream_example(void) {
const uint8_t payload[10] = "Woof woof";
uint8_t buf[LFP_HEADER_SIZE + lfp_encoded_body_length(payload, sizeof(payload))];

if (lfp_encode_to_buf(SYSID_CDH, SYSID_COMMS, ENDPOINT_COMMS_TEST, LFP_DIRECTION_REQUEST,
payload,sizeof(payload),
buf,sizeof(buf)
) < 0) {
ULOG_ERROR("Failed to encode LFP packet!");
}

ULOG_INFO("LFP encoding successful: {}", ULOG_SLICE(buf));

lfp_stream_ctx_t ctx;
lfp_stream_init(&ctx, (uint8_t[1024]){}, 1024, NULL, on_msg, on_error, NULL);
lfp_stream_update_buf(&ctx, buf, sizeof(buf));

ULOG_INFO("LFP Packet ingested");
}

//static const struct device * dev_demo_uart = DEVICE_DT_GET(DT_NODELABEL(demo_serial));
void asn1_example_cb(uint8_t chr, void * p_ctx) {
//uart_poll_out(dev_demo_uart, chr);
}

void asn1_example(void) {
if (!ASN1_LFP_SERIALIZE(cdhSystemId, bmsSystemId, BMSSystemStatusResponse, asn1_example_cb, NULL, {
.uptime = 0xDEADBEEF,
.version = 12
})) {
ULOG_ERROR("LFP encoding failed! {} {}", ASN1_LFP_ERROR_CODE.asn1, ASN1_LFP_ERROR_CODE.lfp);
}
}

void asn1_example_bms_system_status_req(const BMSSystemStatusRequest * p_payload, const asn1_lfp_decode_data_t * p_data) {
ULOG_INFO("Received system status request");
}
void asn1_example_bms_system_status_res(const BMSSystemStatusResponse * p_payload, const asn1_lfp_decode_data_t * p_data) {
ULOG_INFO("Received system status response: version {}, uptime {}", p_payload->version, p_payload->uptime);
}

void asn1_example_error_handler(const int asn1_error, const asn1_lfp_decode_data_t * p_data) {
ULOG_ERROR("Failed to decode message {} (error {})", lfp_composite_id(p_data->p_header), asn1_error);
}
Expand All @@ -113,7 +35,7 @@ void asn1_example_on_msg(const lfp_header_t * p_header, const uint8_t * p_body,
ARG_UNUSED(p_ctx);
ARG_UNUSED(body_length);

ULOG_INFO("Decoded packet on endpoint {}: {}", p_header->endpoint, (char *)p_body);
ULOG_INFO("Decoded packet on endpoint {}: {}", p_header->endpoint, ((struct ulog_slice){.data=(void *)p_body,.size=body_length}));

const asn1_lfp_decode_data_t data = {
.p_header = p_header,
Expand All @@ -133,7 +55,26 @@ void asn1_example_on_msg(const lfp_header_t * p_header, const uint8_t * p_body,
ULOG_WARN("Received unknown message {}", ULOG_SLICE_PTR((lfp_header_t *)p_header));
}

void asn1_example(void) {
uint8_t payload[ASN1_LFP_SEND_BUF_SIZE(BMSSystemStatusResponse)];

const uint32_t size = ASN1_LFP_SERIALIZE(cdhSystemId, bmsSystemId, BMSSystemStatusResponse, payload, sizeof(payload), {
.uptime = 0xDEADBEEF,
.version = 12
});
if (!size) {
ULOG_ERROR("LFP encoding failed! {} {}", ASN1_LFP_ERROR_CODE.asn1, ASN1_LFP_ERROR_CODE.lfp);
}

ULOG_INFO("Sending: {}", ((struct ulog_slice) {.data = payload, .size = size}));

uint8_t payload2[ASN1_LFP_RECV_BUF_SIZE(BMSSystemStatusResponse)];
lfp_stream_ctx_t stream;
lfp_stream_init(&stream, payload2, sizeof(payload2), NULL, asn1_example_on_msg, asn1_example_stream_error_handler, NULL);
lfp_stream_update_buf(&stream, payload, size);
}

int main(void)
{
stream_example();
asn1_example();
}
Loading