Code import
This commit is contained in:
+218
@@ -0,0 +1,218 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/11/21.
|
||||
//
|
||||
|
||||
#include "platform.h"
|
||||
#include "TinyFrame.h"
|
||||
#include "framework/unit_registry.h"
|
||||
#include "comm/messages.h"
|
||||
#include "task_sched.h"
|
||||
|
||||
TinyFrame tf_;
|
||||
TinyFrame *comm = &tf_;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Pre-declaring local functions
|
||||
static TF_Result lst_ping(TinyFrame *tf, TF_Msg *msg);
|
||||
static TF_Result lst_unit(TinyFrame *tf, TF_Msg *msg);
|
||||
static TF_Result lst_list_units(TinyFrame *tf, TF_Msg *msg);
|
||||
static TF_Result lst_default(TinyFrame *tf, TF_Msg *msg);
|
||||
|
||||
void comm_init(void)
|
||||
{
|
||||
TF_InitStatic(comm, TF_SLAVE);
|
||||
TF_AddTypeListener(comm, MSG_PING, lst_ping);
|
||||
TF_AddTypeListener(comm, MSG_UNIT_REQUEST, lst_unit);
|
||||
TF_AddTypeListener(comm, MSG_LIST_UNITS, lst_list_units);
|
||||
|
||||
// fall-through
|
||||
TF_AddGenericListener(comm, lst_default);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static TF_Result lst_ping(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
Job job = {
|
||||
.cb = job_ping_reply,
|
||||
.frame_id = msg->frame_id
|
||||
};
|
||||
scheduleJob(&job, TSK_SCHED_LOW);
|
||||
|
||||
return TF_STAY;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void job_unhandled_resp(Job *job)
|
||||
{
|
||||
tf_respond_snprintf(MSG_ERROR, job->frame_id, "UNKNOWN MSG %"PRIu32, job->d32);
|
||||
}
|
||||
|
||||
static TF_Result lst_default(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
Job job = {
|
||||
.cb = job_unhandled_resp,
|
||||
.frame_id = msg->frame_id,
|
||||
.d32 = msg->type
|
||||
};
|
||||
scheduleJob(&job, TSK_SCHED_LOW);
|
||||
|
||||
return TF_STAY;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static TF_Result lst_unit(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
ureg_deliver_unit_request(msg);
|
||||
return TF_STAY;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void job_list_units(Job *job)
|
||||
{
|
||||
ureg_report_active_units(job->frame_id);
|
||||
}
|
||||
|
||||
static TF_Result lst_list_units(TinyFrame *tf, TF_Msg *msg)
|
||||
{
|
||||
Job job = {
|
||||
.cb = job_list_units,
|
||||
.frame_id = msg->frame_id,
|
||||
};
|
||||
scheduleJob(&job, TSK_SCHED_LOW);
|
||||
|
||||
return TF_STAY;
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/11/21.
|
||||
//
|
||||
|
||||
#ifndef GEX_MESSAGES_H
|
||||
#define GEX_MESSAGES_H
|
||||
|
||||
#include "sched_queue.h"
|
||||
#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)
|
||||
// Unit messages
|
||||
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
|
||||
};
|
||||
|
||||
/**
|
||||
* 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_MESSAGES_H
|
||||
Reference in New Issue
Block a user