implemented keep alive (clients that have not sent the KeepAlive command for more than ten seconds will be disconnected)
This commit is contained in:
parent
1be53b8925
commit
20fc2784db
5 changed files with 53 additions and 7 deletions
|
@ -60,10 +60,11 @@ public:
|
|||
void sendData(String data);
|
||||
|
||||
bool getDeviceConnected();
|
||||
bool disconnectCurrentDevice();
|
||||
|
||||
protected:
|
||||
// callbacks for BLEServer
|
||||
void onConnect(BLEServer *pServer) override;
|
||||
void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;
|
||||
void onDisconnect(BLEServer *pServer) override;
|
||||
|
||||
// callback for BLECharacteristic
|
||||
|
@ -75,6 +76,9 @@ private:
|
|||
const char *rxUUID;
|
||||
const char *txUUID;
|
||||
|
||||
|
||||
esp_gatt_perm_t permissions;
|
||||
|
||||
// BLE Objects
|
||||
BLEServer *bleServer;
|
||||
BLEService *bleService;
|
||||
|
@ -82,7 +86,8 @@ private:
|
|||
BLECharacteristic *rxCharacteristic;
|
||||
|
||||
// helpers
|
||||
bool deviceConnected = false;
|
||||
bool deviceConnected;
|
||||
uint16_t deviceConnectionId;
|
||||
BluetoothLeUartServerCallbacks *callbacks;
|
||||
};
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ public:
|
|||
// befriend for callbacks
|
||||
friend class BluetoothLeUartServer;
|
||||
|
||||
void loop();
|
||||
|
||||
protected:
|
||||
// calbacks for BluetoothLeUartServerCallbacks
|
||||
void onDeviceConnectedChanged(bool deviceConnected) override;
|
||||
|
@ -23,6 +25,8 @@ protected:
|
|||
private:
|
||||
enum OmobiDisplayCommand
|
||||
{
|
||||
AuthorizeSessionCommand = 0,
|
||||
KeepAliveCommand = 1,
|
||||
GetAllTextSetsCommand = 10,
|
||||
GetTextSetParameterCommand = 11,
|
||||
GetDisplayBrightnessCommand = 12,
|
||||
|
@ -37,6 +41,9 @@ private:
|
|||
DisplayControllerError = 501
|
||||
};
|
||||
|
||||
unsigned long lastKeepAlive;
|
||||
const int maximumKeepAliveDelay = 10000;
|
||||
|
||||
LedDisplayController *ledDisplayController;
|
||||
BluetoothLeUartServer *bleServer;
|
||||
};
|
||||
|
|
|
@ -7,6 +7,8 @@ BluetoothLeUartServer::BluetoothLeUartServer(String deviceName, const char uartS
|
|||
this->txUUID = txUUID;
|
||||
|
||||
this->callbacks = nullptr;
|
||||
this->deviceConnected = false;
|
||||
this->deviceConnectionId = -1;
|
||||
|
||||
// Create the BLE Device
|
||||
BLEDevice::init(deviceName.c_str()); // Give it a name
|
||||
|
@ -54,9 +56,11 @@ void BluetoothLeUartServer::sendData(String data)
|
|||
txCharacteristic->notify();
|
||||
}
|
||||
|
||||
void BluetoothLeUartServer::onConnect(BLEServer *pServer)
|
||||
void BluetoothLeUartServer::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param)
|
||||
{
|
||||
this->deviceConnected = true;
|
||||
this->deviceConnectionId = param->connect.conn_id;
|
||||
|
||||
if (this->callbacks != nullptr)
|
||||
{
|
||||
this->callbacks->onDeviceConnectedChanged(this->deviceConnected);
|
||||
|
@ -66,6 +70,8 @@ void BluetoothLeUartServer::onConnect(BLEServer *pServer)
|
|||
void BluetoothLeUartServer::onDisconnect(BLEServer *pServer)
|
||||
{
|
||||
this->deviceConnected = false;
|
||||
this->deviceConnectionId = -1;
|
||||
|
||||
if (this->callbacks != nullptr)
|
||||
this->callbacks->onDeviceConnectedChanged(this->deviceConnected);
|
||||
}
|
||||
|
@ -80,3 +86,11 @@ bool BluetoothLeUartServer::getDeviceConnected()
|
|||
{
|
||||
return this->deviceConnected;
|
||||
}
|
||||
|
||||
bool BluetoothLeUartServer::disconnectCurrentDevice() {
|
||||
if(!this->getDeviceConnected())
|
||||
return false;
|
||||
|
||||
this->bleServer->disconnect(this->deviceConnectionId);
|
||||
return true;
|
||||
}
|
|
@ -1,22 +1,34 @@
|
|||
#include "OmobiLedDisplay.h"
|
||||
#include "esp_gatts_api.h"
|
||||
|
||||
OmobiLedDisplay::OmobiLedDisplay(String deviceName, Adafruit_NeoMatrix *ledDisplayMatrix)
|
||||
{
|
||||
this->lastKeepAlive = -1;
|
||||
|
||||
// init led display controller
|
||||
this->ledDisplayController = new LedDisplayController(ledDisplayMatrix);
|
||||
this->ledDisplayController->setTextSetParameter(0, LedDisplayController::ActiveParameter, "true");
|
||||
|
||||
// init ble server
|
||||
this->bleServer = new BluetoothLeUartServer(deviceName, "6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e");
|
||||
this->bleServer = new BluetoothLeUartServer(deviceName, "92fecb20-1406-426a-afa5-cd5c1f306462", "92fecb21-1406-426a-afa5-cd5c1f306462", "92fecb22-1406-426a-afa5-cd5c1f306462");
|
||||
this->bleServer->setCallbacks(this);
|
||||
}
|
||||
|
||||
void OmobiLedDisplay::loop() {
|
||||
if(millis() - lastKeepAlive > this->maximumKeepAliveDelay)
|
||||
this->bleServer->disconnectCurrentDevice();
|
||||
}
|
||||
|
||||
void OmobiLedDisplay::onDeviceConnectedChanged(bool deviceConnected)
|
||||
{
|
||||
if (deviceConnected)
|
||||
if (deviceConnected) {
|
||||
Serial.println("Device connected");
|
||||
else
|
||||
this->lastKeepAlive = millis();
|
||||
}
|
||||
else {
|
||||
Serial.println("Device disconnected");
|
||||
this->lastKeepAlive = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void OmobiLedDisplay::onDataReceived(String dataString)
|
||||
|
@ -42,6 +54,13 @@ void OmobiLedDisplay::onDataReceived(String dataString)
|
|||
|
||||
switch (requestHeader)
|
||||
{
|
||||
case KeepAliveCommand:
|
||||
{
|
||||
replyStatus = Success;
|
||||
this->lastKeepAlive = millis();
|
||||
break;
|
||||
}
|
||||
|
||||
case GetAllTextSetsCommand:
|
||||
{
|
||||
// cycle through all text sets
|
||||
|
|
|
@ -27,5 +27,6 @@ void setup()
|
|||
void loop()
|
||||
{
|
||||
// nothing to do in loop
|
||||
delay(1000);
|
||||
display->loop();
|
||||
delay(1);
|
||||
}
|
Loading…
Reference in a new issue