some cleaning

sipo
Ondřej Hruška 7 years ago
parent 92c28c5daf
commit 7f682b2dcd
  1. 49
      comm/messages.c
  2. 25
      framework/unit_registry.c

@ -6,40 +6,24 @@
#include "TinyFrame.h" #include "TinyFrame.h"
#include "framework/unit_registry.h" #include "framework/unit_registry.h"
#include "comm/messages.h" #include "comm/messages.h"
#include "task_sched.h"
TinyFrame tf_; static TinyFrame tf_;
TinyFrame *comm = &tf_; TinyFrame *comm = &tf_;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Pre-declaring local functions /**
static TF_Result lst_ping(TinyFrame *tf, TF_Msg *msg); * Ping request listener - returns version string and other info about the build
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);
}
// ---------------------------------------------------------------------------
static TF_Result lst_ping(TinyFrame *tf, TF_Msg *msg) static TF_Result lst_ping(TinyFrame *tf, TF_Msg *msg)
{ {
com_respond_snprintf(msg->frame_id, MSG_SUCCESS, "%s/%s", GEX_VERSION, GEX_PLATFORM); com_respond_snprintf(msg->frame_id, MSG_SUCCESS, "%s/%s", GEX_VERSION, GEX_PLATFORM);
return TF_STAY; return TF_STAY;
} }
// ---------------------------------------------------------------------------- /**
* Default listener, fallback for unhandled messages
*/
static TF_Result lst_default(TinyFrame *tf, TF_Msg *msg) static TF_Result lst_default(TinyFrame *tf, TF_Msg *msg)
{ {
dbg("!! Unhandled msg type %02"PRIx8", frame_id 0x%04"PRIx16, msg->type, msg->frame_id); dbg("!! Unhandled msg type %02"PRIx8", frame_id 0x%04"PRIx16, msg->type, msg->frame_id);
@ -48,18 +32,31 @@ static TF_Result lst_default(TinyFrame *tf, TF_Msg *msg)
return TF_STAY; return TF_STAY;
} }
// ---------------------------------------------------------------------------- /**
* Unit request listener, a message targeted at a particular
*/
static TF_Result lst_unit(TinyFrame *tf, TF_Msg *msg) static TF_Result lst_unit(TinyFrame *tf, TF_Msg *msg)
{ {
ureg_deliver_unit_request(msg); ureg_deliver_unit_request(msg);
return TF_STAY; return TF_STAY;
} }
// ----------------------------------------------------------------------------
static TF_Result lst_list_units(TinyFrame *tf, TF_Msg *msg) static TF_Result lst_list_units(TinyFrame *tf, TF_Msg *msg)
{ {
ureg_report_active_units(msg->frame_id); ureg_report_active_units(msg->frame_id);
return TF_STAY; return TF_STAY;
} }
// ---------------------------------------------------------------------------
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);
}

@ -516,8 +516,11 @@ void ureg_deliver_unit_request(TF_Msg *msg)
UlistEntry *li = ulist_head; UlistEntry *li = ulist_head;
while (li != NULL) { while (li != NULL) {
Unit *const pUnit = &li->unit; Unit *const pUnit = &li->unit;
if (pUnit->callsign == callsign) { if (pUnit->callsign == callsign && pUnit->status == E_SUCCESS) {
bool ok = pUnit->driver->handleRequest(pUnit, msg->frame_id, command, &pp); bool ok = pUnit->driver->handleRequest(pUnit, msg->frame_id, command, &pp);
// send extra SUCCESS confirmation message.
// error is expected to have already been reported.
if (ok && confirmed) { if (ok && confirmed) {
com_respond_ok(msg->frame_id); com_respond_ok(msg->frame_id);
} }
@ -530,7 +533,7 @@ void ureg_deliver_unit_request(TF_Msg *msg)
com_respond_snprintf(msg->frame_id, MSG_ERROR, "NO UNIT @ %"PRIu8, callsign); com_respond_snprintf(msg->frame_id, MSG_ERROR, "NO UNIT @ %"PRIu8, callsign);
} }
/** Send a response for a unit-list request */
void ureg_report_active_units(TF_ID frame_id) void ureg_report_active_units(TF_ID frame_id)
{ {
// count bytes needed // count bytes needed
@ -539,24 +542,29 @@ void ureg_report_active_units(TF_ID frame_id)
UlistEntry *li = ulist_head; UlistEntry *li = ulist_head;
uint32_t count = 0; uint32_t count = 0;
while (li != NULL) { while (li != NULL) {
count++; if (li->unit.status == E_SUCCESS) {
msglen += strlen(li->unit.name)+1; count++;
msglen += strlen(li->unit.name) + 1;
msglen += strlen(li->unit.driver->name) + 1;
}
li = li->next; li = li->next;
} }
msglen += count; msglen += count; // one byte per message for the callsign
bool suc = true; bool suc = true;
uint8_t *buff = malloc_ck(msglen, &suc); uint8_t *buff = malloc_ck(msglen, &suc);
if (!suc) { com_respond_str(MSG_ERROR, frame_id, "OUT OF MEMORY"); return; } if (!suc) { com_respond_str(MSG_ERROR, frame_id, "OUT OF MEMORY"); return; }
{ {
PayloadBuilder pb = pb_start(buff, msglen, NULL); PayloadBuilder pb = pb_start(buff, msglen, NULL);
pb_u8(&pb, (uint8_t) count); // assume we don't have more than 255 units pb_u8(&pb, (uint8_t) count); // assume we don't have more than 255 units
li = ulist_head; li = ulist_head;
while (li != NULL) { while (li != NULL) {
pb_u8(&pb, li->unit.callsign); if (li->unit.status == E_SUCCESS) {
pb_string(&pb, li->unit.name); pb_u8(&pb, li->unit.callsign);
pb_string(&pb, li->unit.name);
pb_string(&pb, li->unit.driver->name);
}
li = li->next; li = li->next;
} }
@ -564,6 +572,5 @@ void ureg_report_active_units(TF_ID frame_id)
com_respond_buf(frame_id, MSG_SUCCESS, buff, msglen); com_respond_buf(frame_id, MSG_SUCCESS, buff, msglen);
} }
free(buff); free(buff);
} }

Loading…
Cancel
Save