mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 22:57:50 +03:00
port to nimble
This commit is contained in:
parent
26b2dc27fa
commit
e45780674d
@ -48,6 +48,7 @@ lib_deps =
|
||||
bblanchon/ArduinoJson @ 7.4.2
|
||||
ricmoo/QRCode @ 0.0.1
|
||||
links2004/WebSockets @ 2.7.3
|
||||
h2zero/NimBLE-Arduino @ 2.3.7
|
||||
|
||||
[env:default]
|
||||
extends = base
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#include "BluetoothActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <BLE2902.h>
|
||||
|
||||
#include "MappedInputManager.h"
|
||||
#include "fontIds.h"
|
||||
@ -29,37 +28,35 @@ void BluetoothActivity::taskTrampoline(void* param) {
|
||||
}
|
||||
|
||||
void BluetoothActivity::startAdvertising() {
|
||||
BLEDevice::startAdvertising();
|
||||
NimBLEDevice::startAdvertising();
|
||||
}
|
||||
|
||||
void BluetoothActivity::stopAdvertising() {
|
||||
BLEDevice::stopAdvertising();
|
||||
NimBLEDevice::stopAdvertising();
|
||||
}
|
||||
|
||||
void BluetoothActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
BLEDevice::init(DEVICE_NAME);
|
||||
pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(&serverCallbacks);
|
||||
NimBLEDevice::init(DEVICE_NAME);
|
||||
pServer = NimBLEDevice::createServer();
|
||||
pServer->setCallbacks(&serverCallbacks, false);
|
||||
pService = pServer->createService(SERVICE_UUID);
|
||||
pRequestChar = pService->createCharacteristic(
|
||||
REQUEST_CHARACTERISTIC_UUID,
|
||||
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR
|
||||
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR
|
||||
);
|
||||
pRequestChar->setCallbacks(&requestCallbacks);
|
||||
pResponseChar = pService->createCharacteristic(
|
||||
RESPONSE_CHARACTERISTIC_UUID,
|
||||
BLECharacteristic::PROPERTY_INDICATE
|
||||
NIMBLE_PROPERTY::INDICATE
|
||||
);
|
||||
pResponseChar->addDescriptor(new BLE2902());
|
||||
pService->start();
|
||||
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
pAdvertising->setScanResponse(true);
|
||||
pAdvertising->setMinPreferred(0x06);
|
||||
pAdvertising->setMinPreferred(0x12);
|
||||
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
||||
pAdvertising->setName(DEVICE_NAME);
|
||||
pAdvertising->addServiceUUID(pService->getUUID());
|
||||
pAdvertising->enableScanResponse(true);
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
@ -94,10 +91,9 @@ void BluetoothActivity::intoState(State newState) {
|
||||
{
|
||||
// caller sets errorMessage
|
||||
file.close();
|
||||
auto connId = pServer->getConnId();
|
||||
if (connId != ESP_GATT_IF_NONE) {
|
||||
if (pServer->getConnectedCount() > 0) {
|
||||
// TODO: send back a response over BLE?
|
||||
pServer->disconnect(connId);
|
||||
pServer->disconnect(pServer->getPeerInfo(0));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -114,8 +110,7 @@ void BluetoothActivity::onExit() {
|
||||
|
||||
stopAdvertising();
|
||||
|
||||
pService->stop();
|
||||
BLEDevice::deinit();
|
||||
NimBLEDevice::deinit(true);
|
||||
|
||||
// Wait until not rendering to delete task
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
@ -219,12 +214,12 @@ void BluetoothActivity::render() const {
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
|
||||
void BluetoothActivity::ServerCallbacks::onConnect(BLEServer* pServer) {
|
||||
void BluetoothActivity::ServerCallbacks::onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) {
|
||||
Serial.printf("BLE connected\n");
|
||||
activity->onConnected(true);
|
||||
}
|
||||
|
||||
void BluetoothActivity::ServerCallbacks::onDisconnect(BLEServer* pServer) {
|
||||
void BluetoothActivity::ServerCallbacks::onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason) {
|
||||
Serial.printf("BLE disconnected\n");
|
||||
activity->onConnected(false);
|
||||
}
|
||||
@ -314,8 +309,8 @@ void BluetoothActivity::onRequest(lfbt_message* msg, size_t msg_len) {
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothActivity::RequestCallbacks::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
|
||||
lfbt_message *msg = (lfbt_message*) param->write.value;
|
||||
Serial.printf("Received BLE message of type %u, txnId %x, length %d\n", msg->type, msg->txnId, param->write.len);
|
||||
activity->onRequest(msg, param->write.len);
|
||||
void BluetoothActivity::RequestCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) {
|
||||
lfbt_message *msg = (lfbt_message*) pCharacteristic->getValue().data();
|
||||
Serial.printf("Received BLE message of type %u, txnId %x, length %d\n", msg->type, msg->txnId, pCharacteristic->getValue().length());
|
||||
activity->onRequest(msg, pCharacteristic->getValue().length());
|
||||
}
|
||||
|
||||
@ -3,9 +3,9 @@
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEServer.h>
|
||||
#include <NimBLEDevice.h>
|
||||
#include <NimBLEUtils.h>
|
||||
#include <NimBLEServer.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
@ -61,12 +61,12 @@ class BluetoothActivity final : public Activity {
|
||||
void onConnected(bool isConnected);
|
||||
void onRequest(lfbt_message *msg, size_t msg_len);
|
||||
|
||||
class ServerCallbacks : public BLEServerCallbacks {
|
||||
class ServerCallbacks : public NimBLEServerCallbacks {
|
||||
friend class BluetoothActivity;
|
||||
BluetoothActivity *activity;
|
||||
|
||||
void onConnect(BLEServer* pServer);
|
||||
void onDisconnect(BLEServer* pServer);
|
||||
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
|
||||
void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason);
|
||||
|
||||
protected:
|
||||
explicit ServerCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
||||
@ -74,11 +74,11 @@ class BluetoothActivity final : public Activity {
|
||||
|
||||
ServerCallbacks serverCallbacks;
|
||||
|
||||
class RequestCallbacks : public BLECharacteristicCallbacks {
|
||||
class RequestCallbacks : public NimBLECharacteristicCallbacks {
|
||||
friend class BluetoothActivity;
|
||||
BluetoothActivity *activity;
|
||||
|
||||
void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param);
|
||||
void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo);
|
||||
|
||||
protected:
|
||||
explicit RequestCallbacks(BluetoothActivity *activity) : activity(activity) {}
|
||||
@ -86,10 +86,10 @@ class BluetoothActivity final : public Activity {
|
||||
|
||||
RequestCallbacks requestCallbacks;
|
||||
|
||||
BLEServer *pServer;
|
||||
BLEService *pService;
|
||||
BLECharacteristic *pRequestChar, *pResponseChar;
|
||||
BLEAdvertising *pAdvertising;
|
||||
NimBLEServer *pServer;
|
||||
NimBLEService *pService;
|
||||
NimBLECharacteristic *pRequestChar, *pResponseChar;
|
||||
NimBLEAdvertising *pAdvertising;
|
||||
void startAdvertising();
|
||||
void stopAdvertising();
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user