From fa5261b65a696c343e24879601c8dbc82850aa2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 22 Dec 2017 19:17:57 +0100 Subject: [PATCH] some cleaning --- comm/messages.c | 124 -------------------------------- comm/messages.h | 144 +++++--------------------------------- comm/msg_responses.c | 138 ++++++++++++++++++++++++++++++++++++ comm/msg_responses.h | 128 +++++++++++++++++++++++++++++++++ framework/unit_registry.c | 14 ++-- utils/build_parser.sh | 0 6 files changed, 289 insertions(+), 259 deletions(-) create mode 100644 comm/msg_responses.c create mode 100644 comm/msg_responses.h mode change 100755 => 100644 utils/build_parser.sh diff --git a/comm/messages.c b/comm/messages.c index e21d5cf..25fb1aa 100644 --- a/comm/messages.c +++ b/comm/messages.c @@ -32,130 +32,6 @@ void comm_init(void) // --------------------------------------------------------------------------- -void tf_respond_snprintf(TF_TYPE type, TF_ID id, const char *format, ...) -{ -#define ERR_STR_LEN 64 - char buf[ERR_STR_LEN]; - va_list args; - va_start(args, format); - uint32_t len = (uint32_t) fixup_vsnprintf(&buf[0], ERR_STR_LEN, format, args); - va_end(args); - - tf_respond_buf(type, id, (const uint8_t *) buf, len); -} - -void tf_respond_buf(TF_TYPE type, TF_ID id, const uint8_t *buf, uint32_t len) -{ - TF_Msg msg; - TF_ClearMsg(&msg); - { - msg.type = type; - msg.frame_id = id; - msg.data = buf; - msg.len = (TF_LEN) len; - } - TF_Respond(comm, &msg); -} - -void tf_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len) -{ - TF_Msg msg; - TF_ClearMsg(&msg); - { - msg.type = MSG_UNIT_REPORT; - msg.data = buf; - msg.len = (TF_LEN) len; - msg.type = type; - } - TF_Send(comm, &msg); // no listener -} - -// --------------------------------------------------------------------------- - -static void job_respond_err(Job *job) -{ - tf_respond_str(MSG_ERROR, job->frame_id, job->str); -} - -void sched_respond_err(TF_ID frame_id, const char *message) -{ - dbg("ERR: %s", message); - Job job = { - .cb = job_respond_err, - .frame_id = frame_id, - .str = message - }; - scheduleJob(&job, TSK_SCHED_LOW); -} - -void sched_respond_bad_cmd(TF_ID frame_id) -{ - sched_respond_err(frame_id, "BAD COMMAND"); -} - -void sched_respond_malformed_cmd(TF_ID frame_id) -{ - sched_respond_err(frame_id, "MALFORMED PAYLOAD"); -} - -// --------------------------------------------------------------------------- - -static void job_respond_suc(Job *job) -{ - tf_respond_ok(job->frame_id); -} - -void sched_respond_suc(TF_ID frame_id) -{ - Job job = { - .cb = job_respond_suc, - .frame_id = frame_id - }; - scheduleJob(&job, TSK_SCHED_LOW); -} - -// --------------------------------------------------------------------------- - -static void job_respond_uX(Job *job) -{ - tf_respond_buf(MSG_SUCCESS, job->frame_id, (const uint8_t *) &job->d32, job->len); -} - -void sched_respond_u8(TF_ID frame_id, uint8_t d) -{ - Job job = { - .cb = job_respond_uX, - .frame_id = frame_id, - .d32 = d, - .len = 1 - }; - scheduleJob(&job, TSK_SCHED_HIGH); -} - -void sched_respond_u16(TF_ID frame_id, uint16_t d) -{ - Job job = { - .cb = job_respond_uX, - .frame_id = frame_id, - .d32 = d, - .len = 2 - }; - scheduleJob(&job, TSK_SCHED_HIGH); -} - -void sched_respond_u32(TF_ID frame_id, uint32_t d) -{ - Job job = { - .cb = job_respond_uX, - .frame_id = frame_id, - .d32 = d, - .len = 4 - }; - scheduleJob(&job, TSK_SCHED_HIGH); -} - -// --------------------------------------------------------------------------- - static void job_ping_reply(Job *job) { tf_respond_snprintf(MSG_SUCCESS, job->frame_id, "%s/%s", GEX_VERSION, GEX_PLATFORM); diff --git a/comm/messages.h b/comm/messages.h index 963b7fc..355e8aa 100644 --- a/comm/messages.h +++ b/comm/messages.h @@ -9,151 +9,39 @@ #include "task_sched.h" #include "TinyFrame.h" -extern TinyFrame *comm; - -/** - * Initialize TinyFrame and set up listeners - */ -void comm_init(void); - /** * Supported message types (TF_TYPE) */ enum TF_Types_ { // General, low level - MSG_SUCCESS = 0x00, //!< Generic success response; used by default in all responses; payload is transaction-specific - MSG_PING = 0x01, //!< Ping request (or response), used to test connection - MSG_ERROR = 0x02, //!< Generic failure response (when a request fails to execute) + MSG_SUCCESS = 0x00, //!< Generic success response; used by default in all responses; payload is transaction-specific + MSG_PING = 0x01, //!< Ping request (or response), used to test connection + MSG_ERROR = 0x02, //!< Generic failure response (when a request fails to execute) - MSG_BULK_READ_OFFER = 0x03, //!< Offer of data to read. Payload: u32 total len - MSG_BULK_READ_POLL = 0x04, //!< Request to read a previously announced chunk. + MSG_BULK_READ_OFFER = 0x03, //!< Offer of data to read. Payload: u32 total len + MSG_BULK_READ_POLL = 0x04, //!< Request to read a previously announced chunk. MSG_BULK_WRITE_OFFER = 0x05, //!< Offer to receive data in a write transaction. Payload: u32 max size, u32 max chunk - MSG_BULK_DATA = 0x06, //!< Writing a chunk, or sending a chunk to master. - MSG_BULK_END = 0x07, //!< Bulk transfer is done, no more data to read or write. Recipient shall check total len and discard it on mismatch. There could be a checksum ... - MSG_BULK_ABORT = 0x08, //!< Discard the ongoing transfer + MSG_BULK_DATA = 0x06, //!< Writing a chunk, or sending a chunk to master. + MSG_BULK_END = 0x07, //!< Bulk transfer is done, no more data to read or write. Recipient shall check total len and discard it on mismatch. There could be a checksum ... + MSG_BULK_ABORT = 0x08, //!< Discard the ongoing transfer // Unit messages - MSG_UNIT_REQUEST = 0x10, //!< Command addressed to a particular unit - MSG_UNIT_REPORT = 0x11, //!< Spontaneous report from a unit + MSG_UNIT_REQUEST = 0x10, //!< Command addressed to a particular unit + MSG_UNIT_REPORT = 0x11, //!< Spontaneous report from a unit // System messages - MSG_LIST_UNITS = 0x20, //!< Get all unit call-signs and names + MSG_LIST_UNITS = 0x20, //!< Get all unit call-signs and names }; -/** - * Respond to a TF message using printf-like formatting. - * Works synchronously, must be called on a job queue. - * - * @param type - response type byte - * @param frame_id - ID of the original msg - * @param format - printf format - * @param ... - replacements - */ -void __attribute__((format(printf,3,4))) -tf_respond_snprintf(TF_TYPE type, TF_ID frame_id, const char *format, ...); - -/** - * Respond to a TF message with a buffer of fixed length and custom type. - * Works synchronously, must be called on a job queue. - * - * @param type - response type byte - * @param frame_id - ID of the original msg - * @param buf - byte buffer - * @param len - buffer size - */ -void tf_respond_buf(TF_TYPE type, TF_ID frame_id, const uint8_t *buf, uint32_t len); - -/** - * Respond to a TF message with empty body and MSG_SUCCESS type. - * Works synchronously, must be called on a job queue. - * - * @param frame_id - ID of the original msg - */ -static inline void tf_respond_ok(TF_ID frame_id) -{ - tf_respond_buf(MSG_SUCCESS, frame_id, NULL, 0); -} - -/** - * Same like tf_respond_buf(), but used for sending spontaneous reports. - * Works synchronously, must be called on a job queue / timer task etc. - * - * @param type - response type byte - * @param buf - byte buffer - * @param len - buffer size - */ -void tf_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len); - -/** - * Same like tf_respond_buf(), but the buffer length is measured with strlen. - * Used to sending ASCII string responses. - * Works synchronously, must be called on a job queue. - * - * @param type - response type byte - * @param frame_id - ID of the original msg - * @param str - character buffer, zero terminated - */ -static inline void tf_respond_str(TF_TYPE type, TF_ID frame_id, const char *str) -{ - tf_respond_buf(type, frame_id, (const uint8_t *) str, (uint32_t) strlen(str)); -} - -/** - * Schedule sending an ASCII string error response. - * Schedules a low priority job. - * - * @param frame_id - ID of the original msg - * @param str - character buffer, zero terminated - */ -void sched_respond_err(TF_ID frame_id, const char *str); - -/** - * Variant of sched_respond_err() for reporting bad received command code - * - * @param msg_id - ID of the original msg - */ -void sched_respond_bad_cmd(TF_ID frame_id); - -/** - * Variant of sched_respond_err() for reporting malformed commands (e.g. too short payload) - * - * @param msg_id - ID of the original msg - */ -void sched_respond_malformed_cmd(TF_ID frame_id); - -/** - * Schedule sending an empty response with MSG_SUCCESS type. - * Schedules a low priority job. - * - * @param frame_id - ID of the original msg - */ -void sched_respond_suc(TF_ID frame_id); +// Must be after the enum because it's used in the header file. +#include "msg_responses.h" -/** - * Schedule sending a one-byte response with MSG_SUCCESS type. - * Schedules a high priority job. - * - * @param frame_id - ID of the original msg - * @param d - data - */ -void sched_respond_u8(TF_ID frame_id, uint8_t d); -/** - * Schedule sending a two-byte response with MSG_SUCCESS type. - * Schedules a high priority job. - * - * @param frame_id - ID of the original msg - * @param d - data - */ -void sched_respond_u16(TF_ID frame_id, uint16_t d); +extern TinyFrame *comm; /** - * Schedule sending a 4-byte response with MSG_SUCCESS type. - * Schedules a high priority job. - * - * @param frame_id - ID of the original msg - * @param d - data + * Initialize TinyFrame and set up listeners */ -void sched_respond_u32(TF_ID frame_id, uint32_t d); +void comm_init(void); #endif //GEX_MESSAGES_H diff --git a/comm/msg_responses.c b/comm/msg_responses.c new file mode 100644 index 0000000..fdcbff7 --- /dev/null +++ b/comm/msg_responses.c @@ -0,0 +1,138 @@ +// +// Created by MightyPork on 2017/12/22. +// + +#include "messages.h" +#include "msg_responses.h" + +void tf_respond_snprintf(TF_TYPE type, TF_ID id, const char *format, ...) +{ +#define ERR_STR_LEN 64 + char buf[ERR_STR_LEN]; + va_list args; + va_start(args, format); + uint32_t len = (uint32_t) fixup_vsnprintf(&buf[0], ERR_STR_LEN, format, args); + va_end(args); + + tf_respond_buf(type, id, (const uint8_t *) buf, len); +} + + +void tf_respond_buf(TF_TYPE type, TF_ID id, const uint8_t *buf, uint32_t len) +{ + TF_Msg msg; + TF_ClearMsg(&msg); + { + msg.type = type; + msg.frame_id = id; + msg.data = buf; + msg.len = (TF_LEN) len; + } + TF_Respond(comm, &msg); +} + + +void tf_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len) +{ + TF_Msg msg; + TF_ClearMsg(&msg); + { + msg.type = MSG_UNIT_REPORT; + msg.data = buf; + msg.len = (TF_LEN) len; + msg.type = type; + } + TF_Send(comm, &msg); // no listener +} + + +// --------------------------------------------------------------------------- + +static void job_respond_err(Job *job) +{ + tf_respond_str(MSG_ERROR, job->frame_id, job->str); +} + + +void sched_respond_err(TF_ID frame_id, const char *message) +{ + dbg("ERR: %s", message); + Job job = { + .cb = job_respond_err, + .frame_id = frame_id, + .str = message + }; + scheduleJob(&job, TSK_SCHED_LOW); +} + + +void sched_respond_bad_cmd(TF_ID frame_id) +{ + sched_respond_err(frame_id, "BAD COMMAND"); +} + + +void sched_respond_malformed_cmd(TF_ID frame_id) +{ + sched_respond_err(frame_id, "MALFORMED PAYLOAD"); +} + +// --------------------------------------------------------------------------- + +static void job_respond_suc(Job *job) +{ + tf_respond_ok(job->frame_id); +} + + +void sched_respond_suc(TF_ID frame_id) +{ + Job job = { + .cb = job_respond_suc, + .frame_id = frame_id + }; + scheduleJob(&job, TSK_SCHED_LOW); +} + +// --------------------------------------------------------------------------- + +static void job_respond_uX(Job *job) +{ + tf_respond_buf(MSG_SUCCESS, job->frame_id, (const uint8_t *) &job->d32, job->len); +} + + +void sched_respond_u8(TF_ID frame_id, uint8_t d) +{ + Job job = { + .cb = job_respond_uX, + .frame_id = frame_id, + .d32 = d, + .len = 1 + }; + scheduleJob(&job, TSK_SCHED_HIGH); +} + + +void sched_respond_u16(TF_ID frame_id, uint16_t d) +{ + Job job = { + .cb = job_respond_uX, + .frame_id = frame_id, + .d32 = d, + .len = 2 + }; + scheduleJob(&job, TSK_SCHED_HIGH); +} + + +void sched_respond_u32(TF_ID frame_id, uint32_t d) +{ + Job job = { + .cb = job_respond_uX, + .frame_id = frame_id, + .d32 = d, + .len = 4 + }; + scheduleJob(&job, TSK_SCHED_HIGH); +} diff --git a/comm/msg_responses.h b/comm/msg_responses.h new file mode 100644 index 0000000..e1bd35e --- /dev/null +++ b/comm/msg_responses.h @@ -0,0 +1,128 @@ +// +// Created by MightyPork on 2017/12/22. +// + +#ifndef GEX_F072_MSG_RESPONSES_H +#define GEX_F072_MSG_RESPONSES_H + +#ifndef GEX_MESSAGES_H +#error "Include messages.h instead!" +#endif + +/** + * Respond to a TF message using printf-like formatting. + * Works synchronously, must be called on a job queue. + * + * @param type - response type byte + * @param frame_id - ID of the original msg + * @param format - printf format + * @param ... - replacements + */ +void __attribute__((format(printf,3,4))) +tf_respond_snprintf(TF_TYPE type, TF_ID frame_id, const char *format, ...); + +/** + * Respond to a TF message with a buffer of fixed length and custom type. + * Works synchronously, must be called on a job queue. + * + * @param type - response type byte + * @param frame_id - ID of the original msg + * @param buf - byte buffer + * @param len - buffer size + */ +void tf_respond_buf(TF_TYPE type, TF_ID frame_id, const uint8_t *buf, uint32_t len); + +/** + * Respond to a TF message with empty body and MSG_SUCCESS type. + * Works synchronously, must be called on a job queue. + * + * @param frame_id - ID of the original msg + */ +static inline void tf_respond_ok(TF_ID frame_id) +{ + tf_respond_buf(MSG_SUCCESS, frame_id, NULL, 0); +} + +/** + * Same like tf_respond_buf(), but used for sending spontaneous reports. + * Works synchronously, must be called on a job queue / timer task etc. + * + * @param type - response type byte + * @param buf - byte buffer + * @param len - buffer size + */ +void tf_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len); + +/** + * Same like tf_respond_buf(), but the buffer length is measured with strlen. + * Used to sending ASCII string responses. + * Works synchronously, must be called on a job queue. + * + * @param type - response type byte + * @param frame_id - ID of the original msg + * @param str - character buffer, zero terminated + */ +static inline void tf_respond_str(TF_TYPE type, TF_ID frame_id, const char *str) +{ + tf_respond_buf(type, frame_id, (const uint8_t *) str, (uint32_t) strlen(str)); +} + +/** + * Schedule sending an ASCII string error response. + * Schedules a low priority job. + * + * @param frame_id - ID of the original msg + * @param str - character buffer, zero terminated + */ +void sched_respond_err(TF_ID frame_id, const char *str); + +/** + * Variant of sched_respond_err() for reporting bad received command code + * + * @param msg_id - ID of the original msg + */ +void sched_respond_bad_cmd(TF_ID frame_id); + +/** + * Variant of sched_respond_err() for reporting malformed commands (e.g. too short payload) + * + * @param msg_id - ID of the original msg + */ +void sched_respond_malformed_cmd(TF_ID frame_id); + +/** + * Schedule sending an empty response with MSG_SUCCESS type. + * Schedules a low priority job. + * + * @param frame_id - ID of the original msg + */ +void sched_respond_suc(TF_ID frame_id); + +/** + * Schedule sending a one-byte response with MSG_SUCCESS type. + * Schedules a high priority job. + * + * @param frame_id - ID of the original msg + * @param d - data + */ +void sched_respond_u8(TF_ID frame_id, uint8_t d); + +/** + * Schedule sending a two-byte response with MSG_SUCCESS type. + * Schedules a high priority job. + * + * @param frame_id - ID of the original msg + * @param d - data + */ +void sched_respond_u16(TF_ID frame_id, uint16_t d); + +/** + * Schedule sending a 4-byte response with MSG_SUCCESS type. + * Schedules a high priority job. + * + * @param frame_id - ID of the original msg + * @param d - data + */ +void sched_respond_u32(TF_ID frame_id, uint32_t d); + +#endif //GEX_F072_MSG_RESPONSES_H diff --git a/framework/unit_registry.c b/framework/unit_registry.c index 5940c01..7b2b123 100644 --- a/framework/unit_registry.c +++ b/framework/unit_registry.c @@ -543,24 +543,24 @@ void ureg_deliver_unit_request(TF_Msg *msg) void ureg_report_active_units(TF_ID frame_id) { // count bytes needed - uint32_t needed = 1; // + uint32_t msglen = 1; // for the count byte UlistEntry *li = ulist_head; uint32_t count = 0; while (li != NULL) { count++; - needed += strlen(li->unit.name)+1; + msglen += strlen(li->unit.name)+1; li = li->next; } - needed += count; + msglen += count; bool suc = true; - uint8_t *buff = malloc_ck(needed, &suc); + uint8_t *buff = malloc_ck(msglen, &suc); if (!suc) { tf_respond_str(MSG_ERROR, frame_id, "OUT OF MEMORY"); return; } { - PayloadBuilder pb = pb_start(buff, needed, NULL); - pb_u8(&pb, (uint8_t) count); // assume we don't have more than 255 + PayloadBuilder pb = pb_start(buff, msglen, NULL); + pb_u8(&pb, (uint8_t) count); // assume we don't have more than 255 units li = ulist_head; while (li != NULL) { @@ -571,7 +571,7 @@ void ureg_report_active_units(TF_ID frame_id) assert_param(pb.ok); - tf_respond_buf(MSG_SUCCESS, frame_id, buff, needed); + tf_respond_buf(MSG_SUCCESS, frame_id, buff, msglen); } free(buff); diff --git a/utils/build_parser.sh b/utils/build_parser.sh old mode 100755 new mode 100644