some cleaning
This commit is contained in:
-124
@@ -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);
|
||||
|
||||
+7
-119
@@ -9,13 +9,6 @@
|
||||
#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)
|
||||
*/
|
||||
@@ -40,120 +33,15 @@ enum TF_Types_ {
|
||||
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, ...);
|
||||
// Must be after the enum because it's used in the header file.
|
||||
#include "msg_responses.h"
|
||||
|
||||
|
||||
extern TinyFrame *comm;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Initialize TinyFrame and set up listeners
|
||||
*/
|
||||
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);
|
||||
void comm_init(void);
|
||||
|
||||
#endif //GEX_MESSAGES_H
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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);
|
||||
|
||||
Executable → Regular
Reference in New Issue
Block a user