New ThingSet features pub and auth included

This commit is contained in:
Martin Jaeger
2019-06-18 14:20:14 +02:00
parent ead6f2bffb
commit 375fcee514
11 changed files with 158 additions and 138 deletions
+31 -33
View File
@@ -25,7 +25,8 @@
#ifdef USB_SERIAL_ENABLED
#include "USBSerial.h"
USBSerial ser_usb(0x1f00, 0x2012, 0x0001, false); // connection is not blocked when USB is not plugged in
uint8_t buf_req_usb[500;
uint8_t buf_req_usb[500];
size_t req_usb_pos = 0;
#endif
#ifdef UART_SERIAL_ENABLED
@@ -34,16 +35,11 @@ uint8_t buf_req_uart[500];
size_t req_uart_pos = 0;
#endif
uint8_t buf_resp[1000]; // only one response buffer needed for USB and UART
static uint8_t buf_resp[1000]; // only one response buffer needed for USB and UART
extern ThingSet ts;
bool uart_serial_command_flag = false;
bool pub_uart_enabled = true; // start with sending data disabled
bool usb_serial_command_flag = false;
bool pub_usb_enabled = true;
extern ts_pub_channel_t pub_channels[];
extern const int pub_channel_serial;
void thingset_serial_init(Serial* s)
{
@@ -79,6 +75,8 @@ void thingset_serial_process_1s()
#ifdef UART_SERIAL_ENABLED
static bool uart_serial_command_flag = false;
void uart_serial_isr()
{
while (ser_uart->readable() && uart_serial_command_flag == false) {
@@ -110,12 +108,10 @@ void uart_serial_init(Serial* s)
s->attach(uart_serial_isr);
}
extern const int PUB_CHANNEL_SERIAL;
void uart_serial_pub()
{
if (pub_uart_enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), PUB_CHANNEL_SERIAL);
if (pub_channels[pub_channel_serial].enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), 0);
ser_uart->printf("%s\n", buf_resp);
}
}
@@ -140,25 +136,27 @@ void uart_serial_process()
#ifdef USB_SERIAL_ENABLED
static bool usb_serial_command_flag = false;
void usb_serial_isr()
{
while (ser_usb.readable() && usb_serial_command_flag == false) {
if (req_usb.pos < 100) {
req_usb.data.bin[req_usb.pos] = ser_usb.getc();
//ser_usb->putc(req_usb.data[req_usb.pos]); // echo back the character
if (req_usb.data.bin[req_usb.pos] == '\n') {
if (req_usb.pos > 0 && req_usb.data.bin[req_usb.pos-1] == '\r')
req_usb.data.bin[req_usb.pos-1] = '\0';
if (req_usb_pos < 100) {
buf_req_usb[req_usb_pos] = ser_usb.getc();
//ser_usb->putc(req_usb.data[req_usb_pos]); // echo back the character
if (buf_req_usb[req_usb_pos] == '\n') {
if (req_usb_pos > 0 && buf_req_usb[req_usb_pos-1] == '\r')
buf_req_usb[req_usb_pos-1] = '\0';
else
req_usb.data.bin[req_usb.pos] = '\0';
req_usb.pos = 0;
buf_req_usb[req_usb_pos] = '\0';
req_usb_pos = 0;
usb_serial_command_flag = true;
}
else if (req_usb.pos > 0 && req_usb.data.bin[req_usb.pos] == '\b') { // backspace
req_usb.pos--;
else if (req_usb_pos > 0 && buf_req_usb[req_usb_pos] == '\b') { // backspace
req_usb_pos--;
}
else {
req_usb.pos++;
req_usb_pos++;
}
}
}
@@ -167,8 +165,7 @@ void usb_serial_isr()
void usb_serial_init()
{
ser_usb.attach(usb_serial_isr);
req_usb.data.bin = buf_req_uart;
req_usb.size = sizeof(buf_req_uart);
// buf_req_usb = buf_req_uart;
}
void usb_serial_process()
@@ -176,19 +173,20 @@ void usb_serial_process()
if (usb_serial_command_flag) {
//ser->printf("Received Command: %s\n", buf_serial);
//int len_req = sizeof(req_usb.data);
if (req_usb.pos > 0) {
//int len_resp = thingset_request(req_usb.data, len_req, buf_resp, TS_RESP_BUFFER_LEN);
thingset_process(&req_usb, &resp, &ts_data);
ser_usb.printf("%s\n", resp.data);
if (req_usb_pos > 1) {
ser_usb.printf("Received Request (%d bytes): %s\n", strlen((char *)buf_req_usb), buf_req_usb);
ts.process(buf_req_usb, strlen((char *)buf_req_usb), buf_resp, sizeof(buf_resp));
ser_usb.printf("%s\n", buf_resp);
}
usb_serial_command_flag = false;
req_usb_pos = 0;
}
}
void usb_serial_pub() {
if (pub_usb_enabled) {
thingset_pub_msg_json(&resp, &ts_data, pub_serial_data_objects, sizeof(pub_serial_data_objects)/sizeof(uint16_t));
ser_usb.printf("%s\n", resp.data.str);
if (pub_channels[pub_channel_serial].enabled) {
ts.pub_msg_json((char *)buf_resp, sizeof(buf_resp), pub_channel_serial);
ser_usb.printf("%s\n", buf_resp);
}
}