Improved command input

Now handles buffer full condition gracefully. Originally a full buffer
would have prevented any further command processing until reboot

Changed buffer types to minimize the number of explicit cast we are
using
This commit is contained in:
Hogthrob
2019-08-28 17:39:32 +02:00
committed by Martin Jaeger
parent a1098aaf71
commit 4764e0a5d0
7 changed files with 116 additions and 95 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ monitor_speed = 115200
# Compiler settings
build_flags =
-std=gnu++1z
-std=gnu++17
-Wno-register
-fsingle-precision-constant
-Wl,-Map,memory.map
+62 -60
View File
@@ -44,8 +44,7 @@ class ThingSetCAN_Device
ThingSetCAN_Device(PinName rd, PinName td, int hz);
};
CAN can(PIN_CAN_RX, PIN_CAN_TX, CAN_SPEED);
DigitalOut can_disable(PIN_CAN_STB);
//----------------------------------------------------------------------------
// preliminary simple CAN functions to send data to the bus for logging
@@ -57,7 +56,8 @@ DigitalOut can_disable(PIN_CAN_STB);
extern ThingSet ts;
ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c)
ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c), can(PIN_CAN_RX, PIN_CAN_TX, CAN_SPEED),
can_disable(PIN_CAN_STB)
{
can_disable = 1; // we disable the transceiver
can.mode(CAN::Normal);
@@ -69,7 +69,9 @@ ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can
void ThingSetCAN::enable()
{
can_disable = 0; // we enable the transceiver
//can.attach(&can_receive);
#if defined(CAN_RECEIVE)
can.attach([this](){ this->process_input(); } );
#endif
}
void ThingSetCAN::process_1s()
@@ -125,6 +127,9 @@ int ThingSetCAN::pub()
void ThingSetCAN::process_asap()
{
process_outbox();
#if defined(CAN_RECEIVE)
process_inbox();
#endif
}
void ThingSetCAN::process_outbox()
@@ -143,38 +148,28 @@ void ThingSetCAN::process_outbox()
}
}
#if 0
void can_list_object_ids(int category) {
}
void can_send_object_name(int data_obj_id, uint8_t can_dest_id)
#if defined(CAN_RECEIVE)
// TODO: Move encoding to ThingSet class
void ThingSetCAN::send_object_name(int data_obj_id, uint8_t can_dest_id)
{
uint8_t msg_priority = 7; // low priority service message
uint8_t function_id = 0x84;
CANMessage msg;
msg.format = CANExtended;
msg.type = CANData;
msg.id = msg_priority << 26 | function_id << 16 |(can_dest_id << 8)| can_node_id; // TODO: add destination node ID
msg.id = msg_priority << 26 | function_id << 16 |(can_dest_id << 8)| node_id; // TODO: add destination node ID
int arr_id = -1;
for (unsigned int idx = 0; idx < dataObjectsCount; idx++) {
if (dataObjects[idx].id == data_obj_id) {
arr_id = idx;
break; // correct array entry found
}
}
if (arr_id >= 0) {
if (dataObjects[arr_id].access & ACCESS_READ) {
const data_object_t *dop = ts.get_data_object(data_obj_id);
if (dop != NULL) {
if (dop->access & TS_ACCESS_READ) {
msg.data[2] = TS_T_STRING;
int len = strlen(dataObjects[arr_id].name);
int len = strlen(dop->name);
for (int i = 0; i < len && i < (8-3); i++) {
msg.data[i+3] = *(dataObjects[arr_id].name + i);
msg.data[i+3] = *(dop->name + i);
}
msg.len = ((len < 5) ? 3 + len : 8);
serial.printf("TS Send Object Name: %s (id = %d)\n", dataObjects[arr_id].name, data_obj_id);
// serial.printf("TS Send Object Name: %s (id = %d)\n", dataObjects[arr_id].name, data_obj_id);
}
}
else {
@@ -184,79 +179,86 @@ void can_send_object_name(int data_obj_id, uint8_t can_dest_id)
msg.len = 2;
}
can_tx_queue.enqueue(msg);
tx_queue.enqueue(msg);
}
#endif
#if 0
void can_process_inbox()
void ThingSetCAN::process_inbox()
{
int max_attempts = 15;
while (!can_rx_queue.empty() && max_attempts >0) {
while (!rx_queue.empty() && max_attempts >0) {
CANMessage msg;
can_rx_queue.dequeue(msg);
rx_queue.dequeue(msg);
if (!(msg.id & (0x1U<<25))) {
serial.printf("CAN ID bit 25 = 1 --> ignored\n");
// serial.printf("CAN ID bit 25 = 1 --> ignored\n");
continue; // might be SAE J1939 or NMEA 2000 message --> ignore
}
if (msg.id & (0x1U<<24)) {
serial.printf("Data object publication frame\n");
// serial.printf("Data object publication frame\n");
// data object publication frame
} else {
serial.printf("Service frame\n");
// serial.printf("Service frame\n");
// service frame
int function_id = (msg.id >> 16) & (int)0xFF;
uint8_t can_dest_id = msg.id & (int)0xFF;
int data_obj_id;
int value;
switch (function_id) {
case TS_OUTPUT:
{
if (msg.len >= 2) {
data_obj_id = msg.data[1] + (msg.data[2] << 8);
const data_object_t *dop = ts.get_data_object(data_obj_id);
pub_object(*dop);
}
break;
}
case TS_INPUT:
data_obj_id = msg.data[1] + (msg.data[2] << 8);
value = msg.data[6] + (msg.data[7] << 8);
for (unsigned int i = 0; i < dataObjectsCount; ++i) {
if (dataObjects[i].id == data_obj_id) {
if (dataObjects[i].access & TS_ACCESS_WRITE) {
{
if (msg.len >= 8)
{
data_obj_id = msg.data[1] + (msg.data[2] << 8);
// int value = msg.data[6] + (msg.data[7] << 8);
const data_object_t *dop = ts.get_data_object(data_obj_id);
if (dop != NULL)
{
if (dop->access & TS_ACCESS_WRITE)
{
// TODO: write data
serial.printf("ThingSet Write: %d to %s (id = %d)\n", value, dataObjects[i].name, data_obj_id);
} else {
serial.printf("No write allowed to data object %s (id = %d)\n", dataObjects[i].name, data_obj_id);
// serial.printf("ThingSet Write: %d to %s (id = %d)\n", value, dataObjects[i].name, data_obj_id);
}
else
{
// serial.printf("No write allowed to data object %s (id = %d)\n", dataObjects[i].name, data_obj_id);
}
break;
}
}
break;
}
case TS_NAME:
data_obj_id = msg.data[1] + (msg.data[2] << 8);
can_send_object_name(data_obj_id, can_dest_id);
serial.printf("Get Data Object Name: %d\n", data_obj_id);
send_object_name(data_obj_id, can_dest_id);
// serial.printf("Get Data Object Name: %d\n", data_obj_id);
break;
case TS_LIST:
can_list_object_ids(msg.data[1]);
serial.printf("List Data Object IDs: %d\n", msg.data[1]);
break;
}
}
max_attempts--;
}
}
#endif
#if 0
void can_receive() {
void ThingSetCAN::process_input() {
CANMessage msg;
while (can.read(msg)) {
if (!can_rx_queue.full()) {
can_rx_queue.enqueue(msg);
serial.printf("Message received. id: %d, data: %d\n", msg.id, msg.data[0]);
if (!rx_queue.full()) {
rx_queue.enqueue(msg);
// serial.printf("Message received. id: %d, data: %d\n", msg.id, msg.data[0]);
} else {
serial.printf("CAN rx queue full\n");
// serial.printf("CAN rx queue full\n");
}
}
}
#endif
#endif /* CAN_RECEIVE */
#endif /* CAN_ENABLED */
#endif /* UNIT_TEST */
+1 -1
View File
@@ -164,7 +164,7 @@ int main()
control_timer_start(CONTROL_FREQUENCY);
wait(0.1); // necessary to prevent MCU from randomly getting stuck here if PV panel is connected before battery
sleep_manager_lock_deep_sleep();
// sleep_manager_lock_deep_sleep();
// the main loop is suitable for slow tasks like communication (even blocking wait allowed)
time_t last_call = timestamp;
+9 -1
View File
@@ -37,11 +37,19 @@ class ThingSetCAN: public ThingSetDevice
bool pub_object(const data_object_t& data_obj);
int pub();
void process_outbox();
#if defined(CAN_RECEIVE)
void process_inbox();
void process_input();
void send_object_name(int data_obj_id, uint8_t can_dest_id);
CANMsgQueue rx_queue;
#endif
CANMsgQueue tx_queue;
// CANMsgQueue rx_queue;
uint8_t node_id;
const unsigned int channel;
CAN can;
DigitalOut can_disable;
};
#endif /* CAN_ENABLED */
#endif
+4 -8
View File
@@ -60,7 +60,7 @@
// allocated list. Only std::vector works with this code if NO devices are enabled
// std::array and also plain C arrays work if at least one array element is there.
std::vector<ThingSetDevice*> ThingSetDeviceManager::devices[] =
std::vector<ThingSetDevice*> ThingSetDeviceManager::devices =
{
#ifdef UART_SERIAL_ENABLED
&ts_uart,
@@ -76,13 +76,9 @@ std::vector<ThingSetDevice*> ThingSetDeviceManager::devices[] =
// run the respective function on all objects in the "devices" list
// use the c++11 lambda expressions here for the for_each loop, keeps things compact
void ThingSetDeviceManager::process_asap() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev){ dev->process_asap(); }); }
void ThingSetDeviceManager::enable() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev){ dev->enable(); }); }
void ThingSetDeviceManager::process_1s()
{
for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev) { dev->process_1s(); });
fflush(stdout);
}
void ThingSetDeviceManager::process_asap() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev) { dev->process_asap(); }); }
void ThingSetDeviceManager::enable() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev) { dev->enable(); }); }
void ThingSetDeviceManager::process_1s() { for_each(std::begin(devices),std::end(devices), [](ThingSetDevice* dev) { dev->process_1s(); }); }
ThingSetDeviceManager ts_devices;
+36 -21
View File
@@ -21,28 +21,29 @@
#include "thingset.h"
#include "thingset_serial.h"
uint8_t ThingSetStream::buf_resp[1000];
char ThingSetStream::buf_resp[1000];
extern ThingSet ts;
void ThingSetStream::process_1s()
{
if (ts.get_pub_channel(channel)->enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), channel);
stream->puts((const char*)buf_resp);
ts.pub_msg_json(buf_resp, sizeof(buf_resp), channel);
stream->puts(buf_resp);
stream->putc('\n');
}
stream->puts(".\n");
}
void ThingSetStream::process_asap()
{
if (command_flag) {
// commands must have 2 or more characters
if (req_pos > 1) {
stream->printf("Received Request (%d bytes): %s\n", strlen((char *)buf_req), buf_req);
ts.process(buf_req, strlen((char *)buf_req), buf_resp, sizeof(buf_resp));
stream->puts((const char*)buf_resp);
stream->printf("Received Request (%d bytes): %s\n", strlen(buf_req), buf_req);
ts.process((uint8_t *)buf_req, strlen(buf_req), (uint8_t *)buf_resp, sizeof(buf_resp));
stream->puts(buf_resp);
stream->putc('\n');
fflush(*stream);
}
// start listening for new commands
@@ -51,27 +52,41 @@ void ThingSetStream::process_asap()
}
}
/**
* Read characters from stream until line end \n detected, signal command available then
* and wait for processing
*/
void ThingSetStream::process_input()
{
while (stream->readable() && command_flag == false) {
if (req_pos < sizeof(buf_req)) {
buf_req[req_pos] = stream->getc();
while (stream->readable() && command_flag == false) {
if (buf_req[req_pos] == '\n') {
if (req_pos > 0 && buf_req[req_pos-1] == '\r')
buf_req[req_pos-1] = '\0';
else
buf_req[req_pos] = '\0';
int c = stream->getc();
// start processing
command_flag = true;
}
else if (req_pos > 0 && buf_req[req_pos] == '\b') { // backspace
req_pos--;
// \r\n and \n are markers for line end, i.e. command end
// we accept this at any time, even if the buffer is 'full', since
// there is always one last character left for the \0
if (c == '\n') {
if (req_pos > 0 && buf_req[req_pos-1] == '\r') {
buf_req[req_pos-1] = '\0';
}
else {
req_pos++;
buf_req[req_pos] = '\0';
}
// start processing
command_flag = true;
}
// backspace
// can be done always unless there is nothing in the buffer
else if (req_pos > 0 && c == '\b') {
req_pos--;
}
// we fill the buffer up to all but 1 character
// the last character is reserved for '\0'
// more read characters are simply dropped, unless it is \n
// which ends the command input and triggers processing
else if (req_pos < (sizeof(buf_req)-1)) {
buf_req[req_pos++] = c;
}
}
}
+3 -3
View File
@@ -41,8 +41,8 @@ class ThingSetStream: public ThingSetDevice
private:
Stream* stream;
static uint8_t buf_resp[1000]; // only one response buffer needed for all objects
uint8_t buf_req[500];
static char buf_resp[1000]; // only one response buffer needed for all objects
char buf_req[500];
size_t req_pos = 0;
bool command_flag = false;
};
@@ -54,7 +54,7 @@ template<typename T> class ThingSetSerial: public ThingSetStream
virtual void enable() { {
Callback<void()> cb([this]() -> void { this->process_input();});
// ser->attach(cb);
ser->attach(cb);
}
}