mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-25 17:42:48 +00:00
unify activity detection in PhoneAPI, turn off BLE API while serial API in use
This commit is contained in:
parent
4919129bbc
commit
7473a6c27a
@ -1,5 +1,6 @@
|
|||||||
#include "SerialConsole.h"
|
#include "SerialConsole.h"
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
#include "target_specific.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define Port Serial
|
#define Port Serial
|
||||||
@ -35,3 +36,9 @@ void SerialConsole::handleToRadio(const uint8_t *buf, size_t len)
|
|||||||
|
|
||||||
StreamAPI::handleToRadio(buf, len);
|
StreamAPI::handleToRadio(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Hookable to find out when connection changes
|
||||||
|
void SerialConsole::onConnectionChanged(bool connected)
|
||||||
|
{
|
||||||
|
setBluetoothEnable(!connected); // To prevent user confusion, turn off bluetooth while using the serial port api
|
||||||
|
}
|
@ -26,6 +26,10 @@ class SerialConsole : public StreamAPI, public RedirectablePrint
|
|||||||
RedirectablePrint::write('\r');
|
RedirectablePrint::write('\r');
|
||||||
return RedirectablePrint::write(c);
|
return RedirectablePrint::write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// Hookable to find out when connection changes
|
||||||
|
virtual void onConnectionChanged(bool connected);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern SerialConsole console;
|
extern SerialConsole console;
|
||||||
|
@ -30,8 +30,6 @@ class TotalSizeCharacteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
|
|
||||||
LockGuard g(updateLock);
|
LockGuard g(updateLock);
|
||||||
// Check if there is enough to OTA Update
|
// Check if there is enough to OTA Update
|
||||||
uint32_t len = getValue32(c, 0);
|
uint32_t len = getValue32(c, 0);
|
||||||
@ -67,8 +65,6 @@ class DataCharacteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
|
|
||||||
LockGuard g(updateLock);
|
LockGuard g(updateLock);
|
||||||
std::string value = c->getValue();
|
std::string value = c->getValue();
|
||||||
uint32_t len = value.length();
|
uint32_t len = value.length();
|
||||||
@ -92,8 +88,6 @@ class CRC32Characteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
|
|
||||||
LockGuard g(updateLock);
|
LockGuard g(updateLock);
|
||||||
uint32_t expectedCRC = getValue32(c, 0);
|
uint32_t expectedCRC = getValue32(c, 0);
|
||||||
uint32_t actualCRC = crc.finalize();
|
uint32_t actualCRC = crc.finalize();
|
||||||
|
@ -1,33 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "PowerFSM.h" // FIXME - someday I want to make this OTA thing a separate lb at at that point it can't touch this
|
|
||||||
#include "BLECharacteristic.h"
|
#include "BLECharacteristic.h"
|
||||||
|
#include "PowerFSM.h" // FIXME - someday I want to make this OTA thing a separate lb at at that point it can't touch this
|
||||||
/**
|
|
||||||
* This mixin just lets the power management state machine know the phone is still talking to us
|
|
||||||
*/
|
|
||||||
class BLEKeepAliveCallbacks : public BLECharacteristicCallbacks
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void onRead(BLECharacteristic *c)
|
|
||||||
{
|
|
||||||
powerFSM.trigger(EVENT_CONTACT_FROM_PHONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
|
||||||
{
|
|
||||||
powerFSM.trigger(EVENT_CONTACT_FROM_PHONE);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A characterstic with a set of overridable callbacks
|
* A characterstic with a set of overridable callbacks
|
||||||
*/
|
*/
|
||||||
class CallbackCharacteristic : public BLECharacteristic, public BLEKeepAliveCallbacks
|
class CallbackCharacteristic : public BLECharacteristic, public BLECharacteristicCallbacks
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CallbackCharacteristic(const char *uuid, uint32_t btprops)
|
CallbackCharacteristic(const char *uuid, uint32_t btprops) : BLECharacteristic(uuid, btprops) { setCallbacks(this); }
|
||||||
: BLECharacteristic(uuid, btprops)
|
|
||||||
{
|
|
||||||
setCallbacks(this);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
@ -58,17 +58,12 @@ class ProtobufCharacteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onRead(BLECharacteristic *c)
|
void onRead(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onRead(c);
|
|
||||||
size_t numbytes = pb_encode_to_bytes(trBytes, sizeof(trBytes), fields, my_struct);
|
size_t numbytes = pb_encode_to_bytes(trBytes, sizeof(trBytes), fields, my_struct);
|
||||||
DEBUG_MSG("pbread from %s returns %d bytes\n", c->getUUID().toString().c_str(), numbytes);
|
DEBUG_MSG("pbread from %s returns %d bytes\n", c->getUUID().toString().c_str(), numbytes);
|
||||||
c->setValue(trBytes, numbytes);
|
c->setValue(trBytes, numbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c) { writeToDest(c, my_struct); }
|
||||||
{
|
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
writeToDest(c, my_struct);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// like onWrite, but we provide an different destination to write to, for use by subclasses that
|
/// like onWrite, but we provide an different destination to write to, for use by subclasses that
|
||||||
@ -84,7 +79,7 @@ class ProtobufCharacteristic : public CallbackCharacteristic
|
|||||||
};
|
};
|
||||||
|
|
||||||
#ifdef SUPPORT_OLD_BLE_API
|
#ifdef SUPPORT_OLD_BLE_API
|
||||||
class NodeInfoCharacteristic : public BLECharacteristic, public BLEKeepAliveCallbacks
|
class NodeInfoCharacteristic : public BLECharacteristic, public BLECharacteristicCallbacks
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NodeInfoCharacteristic()
|
NodeInfoCharacteristic()
|
||||||
@ -96,8 +91,6 @@ class NodeInfoCharacteristic : public BLECharacteristic, public BLEKeepAliveCall
|
|||||||
|
|
||||||
void onRead(BLECharacteristic *c)
|
void onRead(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onRead(c);
|
|
||||||
|
|
||||||
const NodeInfo *info = nodeDB.readNextInfo();
|
const NodeInfo *info = nodeDB.readNextInfo();
|
||||||
|
|
||||||
if (info) {
|
if (info) {
|
||||||
@ -113,7 +106,6 @@ class NodeInfoCharacteristic : public BLECharacteristic, public BLEKeepAliveCall
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
DEBUG_MSG("Reset nodeinfo read pointer\n");
|
DEBUG_MSG("Reset nodeinfo read pointer\n");
|
||||||
nodeDB.resetReadPointer();
|
nodeDB.resetReadPointer();
|
||||||
}
|
}
|
||||||
@ -156,8 +148,7 @@ class OwnerCharacteristic : public ProtobufCharacteristic
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(
|
// NOTE: We do not call the standard ProtobufCharacteristic superclass, because we want custom write behavior
|
||||||
c); // NOTE: We do not call the standard ProtobufCharacteristic superclass, because we want custom write behavior
|
|
||||||
|
|
||||||
static User o; // if the phone doesn't set ID we are careful to keep ours, we also always keep our macaddr
|
static User o; // if the phone doesn't set ID we are careful to keep ours, we also always keep our macaddr
|
||||||
if (writeToDest(c, &o)) {
|
if (writeToDest(c, &o)) {
|
||||||
@ -196,7 +187,6 @@ class ToRadioCharacteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onWrite(BLECharacteristic *c)
|
void onWrite(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onWrite(c);
|
|
||||||
DEBUG_MSG("Got on write\n");
|
DEBUG_MSG("Got on write\n");
|
||||||
|
|
||||||
bluetoothPhoneAPI->handleToRadio(c->getData(), c->getValue().length());
|
bluetoothPhoneAPI->handleToRadio(c->getData(), c->getValue().length());
|
||||||
@ -212,7 +202,6 @@ class FromRadioCharacteristic : public CallbackCharacteristic
|
|||||||
|
|
||||||
void onRead(BLECharacteristic *c)
|
void onRead(BLECharacteristic *c)
|
||||||
{
|
{
|
||||||
BLEKeepAliveCallbacks::onRead(c);
|
|
||||||
size_t numBytes = bluetoothPhoneAPI->getFromRadio(trBytes);
|
size_t numBytes = bluetoothPhoneAPI->getFromRadio(trBytes);
|
||||||
|
|
||||||
// Someone is going to read our value as soon as this callback returns. So fill it with the next message in the queue
|
// Someone is going to read our value as soon as this callback returns. So fill it with the next message in the queue
|
||||||
@ -236,11 +225,7 @@ class FromNumCharacteristic : public CallbackCharacteristic
|
|||||||
// observe(&service.fromNumChanged);
|
// observe(&service.fromNumChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRead(BLECharacteristic *c)
|
void onRead(BLECharacteristic *c) { DEBUG_MSG("FIXME implement fromnum read\n"); }
|
||||||
{
|
|
||||||
BLEKeepAliveCallbacks::onRead(c);
|
|
||||||
DEBUG_MSG("FIXME implement fromnum read\n");
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "PhoneAPI.h"
|
#include "PhoneAPI.h"
|
||||||
#include "MeshService.h"
|
#include "MeshService.h"
|
||||||
#include "NodeDB.h"
|
#include "NodeDB.h"
|
||||||
|
#include "PowerFSM.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
PhoneAPI::PhoneAPI()
|
PhoneAPI::PhoneAPI()
|
||||||
@ -14,11 +15,30 @@ void PhoneAPI::init()
|
|||||||
observe(&service.fromNumChanged);
|
observe(&service.fromNumChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhoneAPI::checkConnectionTimeout()
|
||||||
|
{
|
||||||
|
if (isConnected) {
|
||||||
|
bool newConnected = (millis() - lastContactMsec < radioConfig.preferences.phone_timeout_secs);
|
||||||
|
if (!newConnected) {
|
||||||
|
isConnected = false;
|
||||||
|
onConnectionChanged(isConnected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a ToRadio protobuf
|
* Handle a ToRadio protobuf
|
||||||
*/
|
*/
|
||||||
void PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength)
|
void PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength)
|
||||||
{
|
{
|
||||||
|
powerFSM.trigger(EVENT_CONTACT_FROM_PHONE); // As long as the phone keeps talking to us, don't let the radio go to sleep
|
||||||
|
lastContactMsec = millis();
|
||||||
|
if (!isConnected) {
|
||||||
|
isConnected = true;
|
||||||
|
onConnectionChanged(isConnected);
|
||||||
|
}
|
||||||
|
// return (lastContactMsec != 0) &&
|
||||||
|
|
||||||
if (pb_decode_from_bytes(buf, bufLength, ToRadio_fields, &toRadioScratch)) {
|
if (pb_decode_from_bytes(buf, bufLength, ToRadio_fields, &toRadioScratch)) {
|
||||||
switch (toRadioScratch.which_variant) {
|
switch (toRadioScratch.which_variant) {
|
||||||
case ToRadio_packet_tag: {
|
case ToRadio_packet_tag: {
|
||||||
@ -227,6 +247,8 @@ void PhoneAPI::handleToRadioPacket(MeshPacket *p) {}
|
|||||||
/// If the mesh service tells us fromNum has changed, tell the phone
|
/// If the mesh service tells us fromNum has changed, tell the phone
|
||||||
int PhoneAPI::onNotify(uint32_t newValue)
|
int PhoneAPI::onNotify(uint32_t newValue)
|
||||||
{
|
{
|
||||||
|
checkConnectionTimeout(); // a handy place to check if we've heard from the phone (since the BLE version doesn't call this from idle)
|
||||||
|
|
||||||
if (state == STATE_SEND_PACKETS || state == STATE_LEGACY) {
|
if (state == STATE_SEND_PACKETS || state == STATE_LEGACY) {
|
||||||
DEBUG_MSG("Telling client we have new packets %u\n", newValue);
|
DEBUG_MSG("Telling client we have new packets %u\n", newValue);
|
||||||
onNowHasData(newValue);
|
onNowHasData(newValue);
|
||||||
|
@ -50,6 +50,11 @@ class PhoneAPI
|
|||||||
/// Use to ensure that clients don't get confused about old messages from the radio
|
/// Use to ensure that clients don't get confused about old messages from the radio
|
||||||
uint32_t config_nonce = 0;
|
uint32_t config_nonce = 0;
|
||||||
|
|
||||||
|
/** the last msec we heard from the client on the other side of this link */
|
||||||
|
uint32_t lastContactMsec = 0;
|
||||||
|
|
||||||
|
bool isConnected = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PhoneAPI();
|
PhoneAPI();
|
||||||
|
|
||||||
@ -85,6 +90,12 @@ class PhoneAPI
|
|||||||
/// Our fromradio packet while it is being assembled
|
/// Our fromradio packet while it is being assembled
|
||||||
FromRadio fromRadioScratch;
|
FromRadio fromRadioScratch;
|
||||||
|
|
||||||
|
/// Hookable to find out when connection changes
|
||||||
|
virtual void onConnectionChanged(bool connected) {}
|
||||||
|
|
||||||
|
/// If we haven't heard from the other side in a while then say not connected
|
||||||
|
void checkConnectionTimeout();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
||||||
*/
|
*/
|
||||||
|
@ -9,6 +9,7 @@ void StreamAPI::loop()
|
|||||||
{
|
{
|
||||||
writeStream();
|
writeStream();
|
||||||
readStream();
|
readStream();
|
||||||
|
checkConnectionTimeout();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user