80 lines
2.1 KiB
C++
80 lines
2.1 KiB
C++
/// © MiroZ 2024
|
|
|
|
#include "App.h"
|
|
#include "Settings.h"
|
|
#include <WiFi.h>
|
|
#include "ReaderWriter.h"
|
|
|
|
#include "BleService.h"
|
|
|
|
static const char * TAG = "ble";
|
|
|
|
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
|
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
|
|
|
BleService::BleService(App & app) : m_app(app)
|
|
{
|
|
|
|
}
|
|
|
|
void BleService::start()
|
|
{
|
|
ESP_LOGW(TAG, "Starting ble service...");
|
|
|
|
uint8_t wifi_mac[8];
|
|
esp_read_mac(wifi_mac, ESP_MAC_WIFI_STA);
|
|
|
|
sprintf(m_name, "WP_%02x%02x%02x", wifi_mac[3], wifi_mac[4], wifi_mac[5]);
|
|
|
|
NimBLEDevice::init(m_name);
|
|
NimBLEDevice::setMTU(512);
|
|
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
|
|
// NimBLEDevice::setSecurityAuth(BLE_SM_PAIR_AUTHREQ_SC);
|
|
m_server = NimBLEDevice::createServer();
|
|
|
|
m_server->setCallbacks(this);
|
|
m_service = m_server->createService(SERVICE_UUID);
|
|
m_characteristic = m_service->createCharacteristic(CHARACTERISTIC_UUID,
|
|
NIMBLE_PROPERTY::READ |
|
|
NIMBLE_PROPERTY::WRITE |
|
|
NIMBLE_PROPERTY::NOTIFY);
|
|
|
|
m_characteristic->setCallbacks(this);
|
|
// NimBLE2904 * desc = (NimBLE2904*)m_characteristic->createDescriptor("2904");
|
|
// desc->setFormat(NimBLE2904::FORMAT_UTF8);
|
|
// m_characteristic->addDescriptor(desc);
|
|
|
|
m_service->start();
|
|
|
|
m_advertising = NimBLEDevice::getAdvertising();
|
|
m_advertising->addServiceUUID(m_service->getUUID());
|
|
m_advertising->start();
|
|
}
|
|
|
|
void BleService::onConnect(NimBLEServer* pServer)
|
|
{
|
|
ESP_LOGI(TAG, "connected");
|
|
}
|
|
|
|
void BleService::onDisconnect(NimBLEServer* pServer)
|
|
{
|
|
ESP_LOGI(TAG, "disconnected");
|
|
NimBLEDevice::startAdvertising();
|
|
}
|
|
|
|
void BleService::onRead(NimBLECharacteristic* pCharacteristic)
|
|
{
|
|
// int a = 1234;
|
|
// pCharacteristic->setValue(a);
|
|
// ESP_LOGI(TAG, "onRead hello");
|
|
}
|
|
|
|
void BleService::onWrite(NimBLECharacteristic* pCharacteristic)
|
|
{
|
|
m_app.getCommandProcessor()->take();
|
|
char * buffer = m_app.getCommandProcessor()->process(pCharacteristic->getValue().c_str(), pCharacteristic->getDataLength());
|
|
pCharacteristic->setValue((uint8_t*)buffer, strlen(buffer));
|
|
pCharacteristic->notify();
|
|
m_app.getCommandProcessor()->give();
|
|
}
|