mirror of https://github.com/meshcore-dev/MeshCore
13 changed files with 584 additions and 342 deletions
@ -0,0 +1,200 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "BaseSerialInterface.h" |
||||
|
|
||||
|
#ifndef MAX_INTERFACES |
||||
|
// ble, usb, wifi, ethernet
|
||||
|
#define MAX_INTERFACES 4 |
||||
|
#endif |
||||
|
|
||||
|
enum class InterfaceType : uint8_t { |
||||
|
NONE, |
||||
|
Bluetooth, |
||||
|
USB, |
||||
|
WiFi, |
||||
|
Ethernet, |
||||
|
HardwareSerial |
||||
|
}; |
||||
|
|
||||
|
class MultiSerialInterface : public BaseSerialInterface { |
||||
|
private: |
||||
|
|
||||
|
struct RegisteredInterface { |
||||
|
InterfaceType type = InterfaceType::NONE; |
||||
|
BaseSerialInterface* instance = nullptr; |
||||
|
}; |
||||
|
|
||||
|
bool _enabled = false; |
||||
|
RegisteredInterface _interfaces[MAX_INTERFACES] = {}; |
||||
|
|
||||
|
public: |
||||
|
bool addInterface(InterfaceType type, BaseSerialInterface* iface) { |
||||
|
// make sure an interface was provided
|
||||
|
if(iface == nullptr){ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// put it in the first free slot
|
||||
|
for(int i = 0; i < MAX_INTERFACES; i++){ |
||||
|
if(_interfaces[i].instance == nullptr){ |
||||
|
_interfaces[i].instance = iface; |
||||
|
_interfaces[i].type = type; |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// no free slots available
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool removeInterface(BaseSerialInterface* iface) { |
||||
|
// make sure an interface was provided
|
||||
|
if(iface == nullptr){ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// find and remove interface
|
||||
|
for(int i = 0; i < MAX_INTERFACES; i++){ |
||||
|
if(_interfaces[i].instance == iface){ |
||||
|
_interfaces[i] = {}; |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// interface not found
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
void enableBluetooth() { |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.type == InterfaceType::Bluetooth){ |
||||
|
iface.instance->enable(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void disableBluetooth() { |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.type == InterfaceType::Bluetooth){ |
||||
|
iface.instance->disable(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool isBluetoothEnabled() { |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.type == InterfaceType::Bluetooth){ |
||||
|
return iface.instance->isEnabled(); |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// enable all interfaces
|
||||
|
void enable() override { |
||||
|
_enabled = true; |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance){ |
||||
|
iface.instance->enable(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// disable all interfaces
|
||||
|
void disable() override { |
||||
|
_enabled = false; |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance){ |
||||
|
iface.instance->disable(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool isEnabled() const override { |
||||
|
return _enabled; |
||||
|
} |
||||
|
|
||||
|
bool isConnected() const override { |
||||
|
// not connected when disabled
|
||||
|
if(!_enabled){ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// check if any interface is connected
|
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.instance->isConnected()) { |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// nothing connected
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// loop all interfaces
|
||||
|
void loop() override { |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance){ |
||||
|
iface.instance->loop(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool isWriteBusy() const override { |
||||
|
// not busy when disabled
|
||||
|
if(!_enabled){ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// check if any interface is busy
|
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.instance->isEnabled() && iface.instance->isWriteBusy()){ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// nothing busy
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
size_t writeFrame(const uint8_t src[], size_t len) override { |
||||
|
// don't write when disabled or nothing provided
|
||||
|
if(!_enabled || len == 0){ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// write frame to all enabled interfaces
|
||||
|
bool allSuccessful = true; |
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.instance->isEnabled()){ |
||||
|
if(iface.instance->writeFrame(src, len) != len){ |
||||
|
allSuccessful = false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// report success if all writes completed successfully
|
||||
|
return allSuccessful ? len : 0; |
||||
|
} |
||||
|
|
||||
|
size_t checkRecvFrame(uint8_t dest[]) override { |
||||
|
// don't read when disabled
|
||||
|
if(!_enabled){ |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// try to read a frame from any enabled interface
|
||||
|
for(auto iface : _interfaces){ |
||||
|
if(iface.instance && iface.instance->isEnabled()){ |
||||
|
size_t frameSize = iface.instance->checkRecvFrame(dest); |
||||
|
if(frameSize > 0){ |
||||
|
return frameSize; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// no frame received
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
}; |
||||
@ -0,0 +1,140 @@ |
|||||
|
#include "SerialEthernetInterface.h" |
||||
|
|
||||
|
#define RECV_STATE_IDLE 0 |
||||
|
#define RECV_STATE_HDR_FOUND 1 |
||||
|
#define RECV_STATE_LEN1_FOUND 2 |
||||
|
#define RECV_STATE_LEN2_FOUND 3 |
||||
|
|
||||
|
bool SerialEthernetInterface::begin() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
void SerialEthernetInterface::enable() { |
||||
|
if (_isEnabled) return; |
||||
|
_isEnabled = true; |
||||
|
clearBuffers(); |
||||
|
} |
||||
|
|
||||
|
void SerialEthernetInterface::disable() { |
||||
|
_isEnabled = false; |
||||
|
} |
||||
|
|
||||
|
size_t SerialEthernetInterface::writeFrame(const uint8_t src[], size_t len) { |
||||
|
if (len > MAX_FRAME_SIZE) { |
||||
|
ETHERNET_DEBUG_PRINTLN("writeFrame(), frame too big, len=%d\n", len); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
if (isConnected() && len > 0) { |
||||
|
if (send_queue_len >= FRAME_QUEUE_SIZE) { |
||||
|
ETHERNET_DEBUG_PRINTLN("writeFrame(), send_queue is full!"); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
send_queue[send_queue_len].len = len; // add to send queue
|
||||
|
memcpy(send_queue[send_queue_len].buf, src, len); |
||||
|
send_queue_len++; |
||||
|
|
||||
|
return len; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
bool SerialEthernetInterface::isWriteBusy() const { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
void SerialEthernetInterface::onClientConnected() { |
||||
|
_state = RECV_STATE_IDLE; |
||||
|
_frame_len = 0; |
||||
|
_rx_len = 0; |
||||
|
} |
||||
|
|
||||
|
size_t SerialEthernetInterface::checkRecvFrame(uint8_t dest[]) { |
||||
|
|
||||
|
if (isConnected()) { |
||||
|
if (send_queue_len > 0) { // first, check send queue
|
||||
|
|
||||
|
_last_write = millis(); |
||||
|
int len = send_queue[0].len; |
||||
|
|
||||
|
#if ETHERNET_RAW_LINE |
||||
|
ETHERNET_DEBUG_PRINTLN("TX line len=%d", len); |
||||
|
client.write(send_queue[0].buf, len); |
||||
|
client.write("\r\n", 2); |
||||
|
#else |
||||
|
uint8_t pkt[3+len]; // use same header as serial interface so client can delimit frames
|
||||
|
pkt[0] = '>'; |
||||
|
pkt[1] = (len & 0xFF); // LSB
|
||||
|
pkt[2] = (len >> 8); // MSB
|
||||
|
memcpy(&pkt[3], send_queue[0].buf, send_queue[0].len); |
||||
|
ETHERNET_DEBUG_PRINTLN("Sending frame len=%d", len); |
||||
|
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
||||
|
ETHERNET_DEBUG_PRINTLN("TX frame len=%d", len); |
||||
|
#endif |
||||
|
write(pkt, 3 + len); |
||||
|
#endif |
||||
|
send_queue_len--; |
||||
|
for (int i = 0; i < send_queue_len; i++) { // delete top item from queue
|
||||
|
send_queue[i] = send_queue[i + 1]; |
||||
|
} |
||||
|
} else { |
||||
|
while (available()) { |
||||
|
int c = read(); |
||||
|
if (c < 0) break; |
||||
|
|
||||
|
#if ETHERNET_RAW_LINE |
||||
|
if (c == '\r' || c == '\n') { |
||||
|
if (_rx_len == 0) { |
||||
|
continue; |
||||
|
} |
||||
|
uint16_t out_len = _rx_len; |
||||
|
if (out_len > MAX_FRAME_SIZE) out_len = MAX_FRAME_SIZE; |
||||
|
memcpy(dest, _rx_buf, out_len); |
||||
|
_rx_len = 0; |
||||
|
return out_len; |
||||
|
} |
||||
|
if (_rx_len < MAX_FRAME_SIZE) { |
||||
|
_rx_buf[_rx_len] = (uint8_t)c; |
||||
|
_rx_len++; |
||||
|
} |
||||
|
#else |
||||
|
switch (_state) { |
||||
|
case RECV_STATE_IDLE: |
||||
|
if (c == '<') { |
||||
|
_state = RECV_STATE_HDR_FOUND; |
||||
|
} |
||||
|
break; |
||||
|
case RECV_STATE_HDR_FOUND: |
||||
|
_frame_len = (uint8_t)c; |
||||
|
_state = RECV_STATE_LEN1_FOUND; |
||||
|
break; |
||||
|
case RECV_STATE_LEN1_FOUND: |
||||
|
_frame_len |= ((uint16_t)c) << 8; |
||||
|
_rx_len = 0; |
||||
|
_state = _frame_len > 0 ? RECV_STATE_LEN2_FOUND : RECV_STATE_IDLE; |
||||
|
break; |
||||
|
default: |
||||
|
if (_rx_len < MAX_FRAME_SIZE) { |
||||
|
_rx_buf[_rx_len] = (uint8_t)c; |
||||
|
} |
||||
|
_rx_len++; |
||||
|
if (_rx_len >= _frame_len) { |
||||
|
if (_frame_len > MAX_FRAME_SIZE) { |
||||
|
_frame_len = MAX_FRAME_SIZE; |
||||
|
} |
||||
|
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
||||
|
ETHERNET_DEBUG_PRINTLN("RX frame len=%d", _frame_len); |
||||
|
#endif |
||||
|
memcpy(dest, _rx_buf, _frame_len); |
||||
|
_state = RECV_STATE_IDLE; |
||||
|
return _frame_len; |
||||
|
} |
||||
|
} |
||||
|
#endif |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
@ -0,0 +1,75 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "../BaseSerialInterface.h" |
||||
|
|
||||
|
#ifndef ETHERNET_TCP_PORT |
||||
|
#define ETHERNET_TCP_PORT 5000 |
||||
|
#endif |
||||
|
// define ETHERNET_RAW_LINE=1 to use raw line-based CLI instead of framed packets
|
||||
|
|
||||
|
class SerialEthernetInterface : public BaseSerialInterface { |
||||
|
bool _isEnabled; |
||||
|
unsigned long _last_write; |
||||
|
uint8_t _state; |
||||
|
uint16_t _frame_len; |
||||
|
uint16_t _rx_len; |
||||
|
uint8_t _rx_buf[MAX_FRAME_SIZE]; |
||||
|
|
||||
|
struct Frame { |
||||
|
uint8_t len; |
||||
|
uint8_t buf[MAX_FRAME_SIZE]; |
||||
|
}; |
||||
|
|
||||
|
#define FRAME_QUEUE_SIZE 4 |
||||
|
int send_queue_len; |
||||
|
Frame send_queue[FRAME_QUEUE_SIZE]; |
||||
|
|
||||
|
void clearBuffers() { |
||||
|
send_queue_len = 0; |
||||
|
_state = 0; |
||||
|
_frame_len = 0; |
||||
|
_rx_len = 0; |
||||
|
} |
||||
|
|
||||
|
protected: |
||||
|
|
||||
|
public: |
||||
|
SerialEthernetInterface() { |
||||
|
_isEnabled = false; |
||||
|
_last_write = 0; |
||||
|
send_queue_len = 0; |
||||
|
_state = 0; |
||||
|
_frame_len = 0; |
||||
|
_rx_len = 0; |
||||
|
} |
||||
|
bool begin(); |
||||
|
|
||||
|
void onClientConnected(); |
||||
|
|
||||
|
// BaseSerialInterface methods
|
||||
|
void enable() override; |
||||
|
void disable() override; |
||||
|
bool isEnabled() const override { return _isEnabled; } |
||||
|
|
||||
|
bool isConnected() const override; |
||||
|
bool isWriteBusy() const override; |
||||
|
|
||||
|
size_t writeFrame(const uint8_t src[], size_t len) override; |
||||
|
size_t checkRecvFrame(uint8_t dest[]) override; |
||||
|
|
||||
|
virtual int available() = 0; |
||||
|
virtual int read() = 0; |
||||
|
virtual size_t write(const uint8_t *buf, size_t size) = 0; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
||||
|
#include <Arduino.h> |
||||
|
#define ETHERNET_DEBUG_PRINT(F, ...) Serial.printf("ETH: " F, ##__VA_ARGS__) |
||||
|
#define ETHERNET_DEBUG_PRINTLN(F, ...) Serial.printf("ETH: " F "\n", ##__VA_ARGS__) |
||||
|
#define ETHERNET_DEBUG_PRINT_IP(name, ip) Serial.printf("ETH: " name ": %u.%u.%u.%u" "\n", ip[0], ip[1], ip[2], ip[3]) |
||||
|
#else |
||||
|
#define ETHERNET_DEBUG_PRINT(...) {} |
||||
|
#define ETHERNET_DEBUG_PRINTLN(...) {} |
||||
|
#define ETHERNET_DEBUG_PRINT_IP(...) {} |
||||
|
#endif |
||||
Loading…
Reference in new issue