WIP - add new baseclass for all api endpoints (serial, bluetooth, udp)

https://github.com/meshtastic/Meshtastic-esp32/issues/69
1.2-legacy
geeksville 2020-04-20 18:03:13 -07:00
rodzic 2419ebb04e
commit 9232dfcccf
5 zmienionych plików z 184 dodań i 3 usunięć

2
proto

@ -1 +1 @@
Subproject commit e06645d8db16b9e4f23e74a931b8d5cd07bcbe3c
Subproject commit 0cef75501578a2c4adf63da09fdc34db20b3d862

53
src/PhoneAPI.cpp 100644
Wyświetl plik

@ -0,0 +1,53 @@
#include "PhoneAPI.h"
#include <assert.h>
PhoneAPI::PhoneAPI()
{
// Make sure that we never let our packets grow too large for one BLE packet
assert(FromRadio_size <= 512);
assert(ToRadio_size <= 512);
}
/**
* Handle a ToRadio protobuf
*/
void PhoneAPI::handleToRadio(const char *buf, size_t len)
{
// FIXME
}
/**
* Get the next packet we want to send to the phone, or NULL if no such packet is available.
*
* We assume buf is at least FromRadio_size bytes long.
*/
bool PhoneAPI::getFromRadio(char *buf)
{
return false; // FIXME
}
/**
* Return true if we have data available to send to the phone
*/
bool PhoneAPI::available()
{
return true; // FIXME
}
//
// The following routines are only public for now - until the rev1 bluetooth API is removed
//
void PhoneAPI::handleSetOwner(const User &o) {}
void PhoneAPI::handleSetRadio(const RadioConfig &r) {}
/**
* The client wants to start a new set of config reads
*/
void PhoneAPI::handleWantConfig(uint32_t nonce) {}
/**
* Handle a packet that the phone wants us to send. It is our responsibility to free the packet to the pool
*/
void PhoneAPI::handleToRadioPacket(MeshPacket *p) {}

76
src/PhoneAPI.h 100644
Wyświetl plik

@ -0,0 +1,76 @@
#pragma once
#include "mesh-pb-constants.h"
#include "mesh.pb.h"
#include <string>
/**
* Provides our protobuf based API which phone/PC clients can use to talk to our device
* over UDP, bluetooth or serial.
*
* Eventually there should be once instance of this class for each live connection (because it has a bit of state
* for that connection)
*/
class PhoneAPI
{
enum State {
STATE_SEND_NOTHING, // Initial state, don't send anything until the client starts asking for config
STATE_SEND_MY_NODEINFO,
STATE_SEND_OWNER,
STATE_SEND_RADIO,
STATE_SEND_COMPLETE_ID,
STATE_SEND_PACKETS // send packets or debug strings
};
State state = STATE_SEND_NOTHING;
/**
* Each packet sent to the phone has an incrementing count
*/
uint32_t fromRadioNum = 0;
public:
PhoneAPI();
/**
* Handle a ToRadio protobuf
*/
void handleToRadio(const char *buf, size_t len);
/**
* Get the next packet we want to send to the phone, or NULL if no such packet is available.
*
* We assume buf is at least FromRadio_size bytes long.
*/
bool getFromRadio(char *buf);
/**
* Return true if we have data available to send to the phone
*/
bool available();
//
// The following routines are only public for now - until the rev1 bluetooth API is removed
//
void handleSetOwner(const User &o);
void handleSetRadio(const RadioConfig &r);
protected:
/**
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
*/
void onNowHasData(uint32_t fromRadioNum) {}
private:
/**
* The client wants to start a new set of config reads
*/
void handleWantConfig(uint32_t nonce);
/**
* Handle a packet that the phone wants us to send. It is our responsibility to free the packet to the pool
*/
void handleToRadioPacket(MeshPacket *p);
};

Wyświetl plik

@ -42,6 +42,9 @@ PB_BIND(MyNodeInfo, MyNodeInfo, AUTO)
PB_BIND(DeviceState, DeviceState, 4)
PB_BIND(DebugString, DebugString, 2)
PB_BIND(FromRadio, FromRadio, 2)

Wyświetl plik

@ -49,6 +49,10 @@ typedef struct _Data {
Data_payload_t payload;
} Data;
typedef struct _DebugString {
char message[256];
} DebugString;
typedef struct _MyNodeInfo {
int32_t my_node_num;
bool has_gps;
@ -150,6 +154,12 @@ typedef struct _FromRadio {
pb_size_t which_variant;
union {
MeshPacket packet;
MyNodeInfo my_info;
NodeInfo node_info;
User owner;
RadioConfig radio;
DebugString debug_string;
uint32_t config_complete_id;
} variant;
} FromRadio;
@ -157,6 +167,9 @@ typedef struct _ToRadio {
pb_size_t which_variant;
union {
MeshPacket packet;
uint32_t want_config_id;
RadioConfig set_radio;
User set_owner;
} variant;
} ToRadio;
@ -188,6 +201,7 @@ typedef struct _ToRadio {
#define NodeInfo_init_default {0, false, User_init_default, false, Position_init_default, 0, 0}
#define MyNodeInfo_init_default {0, 0, 0, "", "", "", 0, 0, 0}
#define DeviceState_init_default {false, RadioConfig_init_default, false, MyNodeInfo_init_default, false, User_init_default, 0, {NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default, NodeInfo_init_default}, 0, {MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default, MeshPacket_init_default}, false, MeshPacket_init_default, 0}
#define DebugString_init_default {""}
#define FromRadio_init_default {0, 0, {MeshPacket_init_default}}
#define ToRadio_init_default {0, {MeshPacket_init_default}}
#define Position_init_zero {0, 0, 0, 0, 0}
@ -202,6 +216,7 @@ typedef struct _ToRadio {
#define NodeInfo_init_zero {0, false, User_init_zero, false, Position_init_zero, 0, 0}
#define MyNodeInfo_init_zero {0, 0, 0, "", "", "", 0, 0, 0}
#define DeviceState_init_zero {false, RadioConfig_init_zero, false, MyNodeInfo_init_zero, false, User_init_zero, 0, {NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero, NodeInfo_init_zero}, 0, {MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero, MeshPacket_init_zero}, false, MeshPacket_init_zero, 0}
#define DebugString_init_zero {""}
#define FromRadio_init_zero {0, 0, {MeshPacket_init_zero}}
#define ToRadio_init_zero {0, {MeshPacket_init_zero}}
@ -213,6 +228,7 @@ typedef struct _ToRadio {
#define ChannelSettings_name_tag 5
#define Data_typ_tag 1
#define Data_payload_tag 2
#define DebugString_message_tag 1
#define MyNodeInfo_my_node_num_tag 1
#define MyNodeInfo_has_gps_tag 2
#define MyNodeInfo_num_channels_tag 3
@ -269,8 +285,17 @@ typedef struct _ToRadio {
#define DeviceState_version_tag 8
#define DeviceState_rx_text_message_tag 7
#define FromRadio_packet_tag 2
#define FromRadio_my_info_tag 3
#define FromRadio_node_info_tag 4
#define FromRadio_owner_tag 5
#define FromRadio_radio_tag 6
#define FromRadio_debug_string_tag 7
#define FromRadio_config_complete_id_tag 8
#define FromRadio_num_tag 1
#define ToRadio_packet_tag 1
#define ToRadio_want_config_id_tag 100
#define ToRadio_set_radio_tag 101
#define ToRadio_set_owner_tag 102
/* Struct field encoding specification for nanopb */
#define Position_FIELDLIST(X, a) \
@ -397,18 +422,39 @@ X(a, STATIC, SINGULAR, UINT32, version, 8)
#define DeviceState_receive_queue_MSGTYPE MeshPacket
#define DeviceState_rx_text_message_MSGTYPE MeshPacket
#define DebugString_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, STRING, message, 1)
#define DebugString_CALLBACK NULL
#define DebugString_DEFAULT NULL
#define FromRadio_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, num, 1) \
X(a, STATIC, ONEOF, MESSAGE, (variant,packet,variant.packet), 2)
X(a, STATIC, ONEOF, MESSAGE, (variant,packet,variant.packet), 2) \
X(a, STATIC, ONEOF, MESSAGE, (variant,my_info,variant.my_info), 3) \
X(a, STATIC, ONEOF, MESSAGE, (variant,node_info,variant.node_info), 4) \
X(a, STATIC, ONEOF, MESSAGE, (variant,owner,variant.owner), 5) \
X(a, STATIC, ONEOF, MESSAGE, (variant,radio,variant.radio), 6) \
X(a, STATIC, ONEOF, MESSAGE, (variant,debug_string,variant.debug_string), 7) \
X(a, STATIC, ONEOF, UINT32, (variant,config_complete_id,variant.config_complete_id), 8)
#define FromRadio_CALLBACK NULL
#define FromRadio_DEFAULT NULL
#define FromRadio_variant_packet_MSGTYPE MeshPacket
#define FromRadio_variant_my_info_MSGTYPE MyNodeInfo
#define FromRadio_variant_node_info_MSGTYPE NodeInfo
#define FromRadio_variant_owner_MSGTYPE User
#define FromRadio_variant_radio_MSGTYPE RadioConfig
#define FromRadio_variant_debug_string_MSGTYPE DebugString
#define ToRadio_FIELDLIST(X, a) \
X(a, STATIC, ONEOF, MESSAGE, (variant,packet,variant.packet), 1)
X(a, STATIC, ONEOF, MESSAGE, (variant,packet,variant.packet), 1) \
X(a, STATIC, ONEOF, UINT32, (variant,want_config_id,variant.want_config_id), 100) \
X(a, STATIC, ONEOF, MESSAGE, (variant,set_radio,variant.set_radio), 101) \
X(a, STATIC, ONEOF, MESSAGE, (variant,set_owner,variant.set_owner), 102)
#define ToRadio_CALLBACK NULL
#define ToRadio_DEFAULT NULL
#define ToRadio_variant_packet_MSGTYPE MeshPacket
#define ToRadio_variant_set_radio_MSGTYPE RadioConfig
#define ToRadio_variant_set_owner_MSGTYPE User
extern const pb_msgdesc_t Position_msg;
extern const pb_msgdesc_t Data_msg;
@ -422,6 +468,7 @@ extern const pb_msgdesc_t RadioConfig_UserPreferences_msg;
extern const pb_msgdesc_t NodeInfo_msg;
extern const pb_msgdesc_t MyNodeInfo_msg;
extern const pb_msgdesc_t DeviceState_msg;
extern const pb_msgdesc_t DebugString_msg;
extern const pb_msgdesc_t FromRadio_msg;
extern const pb_msgdesc_t ToRadio_msg;
@ -438,6 +485,7 @@ extern const pb_msgdesc_t ToRadio_msg;
#define NodeInfo_fields &NodeInfo_msg
#define MyNodeInfo_fields &MyNodeInfo_msg
#define DeviceState_fields &DeviceState_msg
#define DebugString_fields &DebugString_msg
#define FromRadio_fields &FromRadio_msg
#define ToRadio_fields &ToRadio_msg
@ -454,6 +502,7 @@ extern const pb_msgdesc_t ToRadio_msg;
#define NodeInfo_size 155
#define MyNodeInfo_size 85
#define DeviceState_size 19502
#define DebugString_size 258
#define FromRadio_size 435
#define ToRadio_size 429