begin moving comms glue from the old crufty BLE code to the new cleaner PhoneAPI class

This commit is contained in:
geeksville
2020-04-22 14:55:36 -07:00
parent 31f735ae1f
commit e40524baf0
6 changed files with 168 additions and 100 deletions

View File

@@ -1,4 +1,6 @@
#include "PhoneAPI.h"
#include "MeshService.h"
#include "NodeDB.h"
#include <assert.h>
PhoneAPI::PhoneAPI()
@@ -8,12 +10,31 @@ PhoneAPI::PhoneAPI()
assert(ToRadio_size <= 512);
}
void PhoneAPI::init()
{
observe(&service.fromNumChanged);
}
/**
* Handle a ToRadio protobuf
*/
void PhoneAPI::handleToRadio(const char *buf, size_t len)
void PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength)
{
// FIXME
if (pb_decode_from_bytes(buf, bufLength, ToRadio_fields, &toRadioScratch)) {
switch (toRadioScratch.which_variant) {
case ToRadio_packet_tag: {
// If our phone is sending a position, see if we can use it to set our RTC
MeshPacket &p = toRadioScratch.variant.packet;
service.handleToRadio(p);
break;
}
default:
DEBUG_MSG("Error: unexpected ToRadio variant\n");
break;
}
} else {
DEBUG_MSG("Error: ignoring malformed toradio\n");
}
}
/**
@@ -21,9 +42,28 @@ void PhoneAPI::handleToRadio(const char *buf, size_t len)
*
* We assume buf is at least FromRadio_size bytes long.
*/
bool PhoneAPI::getFromRadio(char *buf)
size_t PhoneAPI::getFromRadio(uint8_t *buf)
{
return false; // FIXME
if (!available())
return false;
// Do we have a message from the mesh?
if (packetForPhone) {
// Encapsulate as a FromRadio packet
memset(&fromRadioScratch, 0, sizeof(fromRadioScratch));
fromRadioScratch.which_variant = FromRadio_packet_tag;
fromRadioScratch.variant.packet = *packetForPhone;
size_t numbytes = pb_encode_to_bytes(buf, sizeof(FromRadio_size), FromRadio_fields, &fromRadioScratch);
DEBUG_MSG("delivering toPhone packet to phone %d bytes\n", numbytes);
service.releaseToPool(packetForPhone); // we just copied the bytes, so don't need this buffer anymore
packetForPhone = NULL;
return numbytes;
}
DEBUG_MSG("toPhone queue is empty\n");
return 0;
}
/**
@@ -31,6 +71,8 @@ bool PhoneAPI::getFromRadio(char *buf)
*/
bool PhoneAPI::available()
{
packetForPhone = service.getForPhone();
return true; // FIXME
}
@@ -38,9 +80,33 @@ bool PhoneAPI::available()
// The following routines are only public for now - until the rev1 bluetooth API is removed
//
void PhoneAPI::handleSetOwner(const User &o) {}
void PhoneAPI::handleSetOwner(const User &o)
{
int changed = 0;
void PhoneAPI::handleSetRadio(const RadioConfig &r) {}
if (*o.long_name) {
changed |= strcmp(owner.long_name, o.long_name);
strcpy(owner.long_name, o.long_name);
}
if (*o.short_name) {
changed |= strcmp(owner.short_name, o.short_name);
strcpy(owner.short_name, o.short_name);
}
if (*o.id) {
changed |= strcmp(owner.id, o.id);
strcpy(owner.id, o.id);
}
if (changed) // If nothing really changed, don't broadcast on the network or write to flash
service.reloadOwner();
}
void PhoneAPI::handleSetRadio(const RadioConfig &r)
{
radioConfig = r;
service.reloadConfig();
}
/**
* The client wants to start a new set of config reads
@@ -50,4 +116,11 @@ 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) {}
void PhoneAPI::handleToRadioPacket(MeshPacket *p) {}
/// If the mesh service tells us fromNum has changed, tell the phone
int PhoneAPI::onNotify(uint32_t newValue)
{
onNowHasData(newValue);
return 0;
}