From 01ec69bb0c777b723a572d25647161033d07c094 Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sat, 19 Aug 2023 18:23:36 +0200 Subject: [PATCH] testing BLE discovery --- platformio.ini | 3 +- src/gfx/rm67162.h | 2 +- src/main.cpp | 16 +++++++- src/main.h | 1 + src/{pins_config.h => pins.h} | 0 src/services/ble/battery_service.cpp | 34 ++++++++++++++++ src/services/ble/battery_service.h | 26 ++++++++++++ src/services/ble/discovery_service.cpp | 56 ++++++++++++++++++++++++++ src/services/ble/discovery_service.h | 24 +++++++++++ src/services/ble/osrw_service.cpp | 42 +++++++++++++++++++ src/services/ble/osrw_service.h | 26 ++++++++++++ src/services/ble/server_callback.cpp | 38 +++++++++++++++++ src/services/ble/server_callback.h | 31 ++++++++++++++ 13 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 src/main.h rename src/{pins_config.h => pins.h} (100%) create mode 100644 src/services/ble/battery_service.cpp create mode 100644 src/services/ble/battery_service.h create mode 100644 src/services/ble/discovery_service.cpp create mode 100644 src/services/ble/discovery_service.h create mode 100644 src/services/ble/osrw_service.cpp create mode 100644 src/services/ble/osrw_service.h create mode 100644 src/services/ble/server_callback.cpp create mode 100644 src/services/ble/server_callback.h diff --git a/platformio.ini b/platformio.ini index 90fc4c6..04417bb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,7 +21,7 @@ monitor_speed = 115200 [env:lilygo-t-amoled] board = lilygo-t-amoled build_flags = - -D CORE_DEBUG_LEVEL=5 + -D CORE_DEBUG_LEVEL=4 -D ESP32S3 -D LILYGOTAMOLED -D BOARD_HAS_PSRAM @@ -33,3 +33,4 @@ lib_deps = me-no-dev/AsyncTCP @ ^1.1.1 lovyan03/LovyanGFX @ ^1.1.8 lvgl/lvgl @ ^8.3.9 + rpolitex/ArduinoNvs @ ^2.5.0 diff --git a/src/gfx/rm67162.h b/src/gfx/rm67162.h index 007eef9..b5e32ff 100644 --- a/src/gfx/rm67162.h +++ b/src/gfx/rm67162.h @@ -19,7 +19,7 @@ #pragma once #include "stdint.h" -#include "pins_config.h" +#include "pins.h" #define TFT_MADCTL 0x36 #define TFT_MAD_MY 0x80 diff --git a/src/main.cpp b/src/main.cpp index d819164..6277b99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,13 +16,22 @@ // along with this program. If not, see . // //---------------------------------------------------------------------------- -#include +#include "main.h" +#include #ifdef LILYGOTAMOLED #include "gfx/lv_setup.h" #include #endif +#include +#include + +void loadSettings() +{ + /// @todo load settings from NVS +} + void setup() { #ifdef LILYGOTAMOLED @@ -43,6 +52,11 @@ void setup() lv_label_set_text(label2, "It is a circularly scrolling text. "); lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40); #endif + + NVS.begin(); + loadSettings(); + + BLE::startAdvertising(); } void loop() diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/main.h @@ -0,0 +1 @@ +#pragma once diff --git a/src/pins_config.h b/src/pins.h similarity index 100% rename from src/pins_config.h rename to src/pins.h diff --git a/src/services/ble/battery_service.cpp b/src/services/ble/battery_service.cpp new file mode 100644 index 0000000..0c4b1a0 --- /dev/null +++ b/src/services/ble/battery_service.cpp @@ -0,0 +1,34 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#include "battery_service.h" + +namespace BLE +{ + BLEService *createBatteryService(BLEServer *pServer) + { + auto *pService = pServer->createService(BLEUUID((uint16_t)0x180F)); + auto *pBatteryLevel = pService->createCharacteristic(BLEUUID((uint16_t)0x2A19), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); + /// TODO: Add battery level + auto value = esp_random() % 100; + pBatteryLevel->setValue(value); + + pService->start(); + return pService; + } +} // namespace BLE diff --git a/src/services/ble/battery_service.h b/src/services/ble/battery_service.h new file mode 100644 index 0000000..83dadf0 --- /dev/null +++ b/src/services/ble/battery_service.h @@ -0,0 +1,26 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#pragma once + +#include + +namespace BLE +{ + BLEService *createBatteryService(BLEServer *pServer); +} // namespace BLE diff --git a/src/services/ble/discovery_service.cpp b/src/services/ble/discovery_service.cpp new file mode 100644 index 0000000..b7d93af --- /dev/null +++ b/src/services/ble/discovery_service.cpp @@ -0,0 +1,56 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#include "discovery_service.h" + +#include +#include +#include +#include +#include +#include + +#include "battery_service.h" +#include "osrw_service.h" +#include "server_callback.h" + +namespace BLE +{ + void startAdvertising() + { + uint8_t mac[6]; + auto pMac = WiFi.macAddress(mac); + char *name = new char[18]; + sprintf(name, "OSRW-%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + BLEDevice::init(name); + + auto *pServer = BLEDevice::createServer(); + pServer->setCallbacks(new ServerCallbacks()); + + auto *pService = createOSRWService(pServer); + auto *pBatteryService = createBatteryService(pServer); + + auto *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(pService->getUUID()); + pAdvertising->addServiceUUID(pBatteryService->getUUID()); + pAdvertising->setScanResponse(true); + pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue + pAdvertising->setMinPreferred(0x12); + BLEDevice::startAdvertising(); + } +} // namespace BLE diff --git a/src/services/ble/discovery_service.h b/src/services/ble/discovery_service.h new file mode 100644 index 0000000..8f09723 --- /dev/null +++ b/src/services/ble/discovery_service.h @@ -0,0 +1,24 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#pragma once + +namespace BLE +{ + void startAdvertising(); +} // namespace BLE diff --git a/src/services/ble/osrw_service.cpp b/src/services/ble/osrw_service.cpp new file mode 100644 index 0000000..b0c8d31 --- /dev/null +++ b/src/services/ble/osrw_service.cpp @@ -0,0 +1,42 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#include "osrw_service.h" + +#include + +#define OSRW_SERVICE_UUID "B7C28FEE-ABD3-4841-ABC8-A686B68C82E2" +#define NAME_CHARACTERISTIC_UUID "5A3973F9-79B5-4D24-A76C-BBA81986A26B" +// 5A3973F9-79B5-4D24-A76C-BBA81986A26B +// 7D616E83-645F-4D2B-B9D5-1151F3CE4133 +// 51B849E4-C15E-4E13-8D42-9A642862DBF4 +// C2690528-B2D2-4E3E-861C-B371D4852FE4 + +namespace BLE +{ + BLEService *createOSRWService(BLEServer *pServer) + { + auto *pService = pServer->createService(OSRW_SERVICE_UUID); + auto *pName = pService->createCharacteristic(NAME_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ); + pName->setValue("ICE 3"); + + pService->start(); + return pService; + } + +} // namespace BLE diff --git a/src/services/ble/osrw_service.h b/src/services/ble/osrw_service.h new file mode 100644 index 0000000..3532bbe --- /dev/null +++ b/src/services/ble/osrw_service.h @@ -0,0 +1,26 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#pragma once + +#include + +namespace BLE +{ + BLEService *createOSRWService(BLEServer *pServer); +} // namespace BLE diff --git a/src/services/ble/server_callback.cpp b/src/services/ble/server_callback.cpp new file mode 100644 index 0000000..c7442f2 --- /dev/null +++ b/src/services/ble/server_callback.cpp @@ -0,0 +1,38 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#include "server_callback.h" + +#include + +namespace BLE +{ + void ServerCallbacks::onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) + { + ESP_LOGD("ble", "BLE client connected"); + } + void ServerCallbacks::onDisconnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) + { + ESP_LOGD("ble", "BLE client disconnected"); + } + + void ServerCallbacks::onMtuChanged(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) + { + ESP_LOGD("ble", "BLE client MTU changed"); + } +} // namespace BLE diff --git a/src/services/ble/server_callback.h b/src/services/ble/server_callback.h new file mode 100644 index 0000000..4f25954 --- /dev/null +++ b/src/services/ble/server_callback.h @@ -0,0 +1,31 @@ +//---------------------------------------------------------------------------- +// OS-Railway - Intercity Express Train +// Copyright (C) 2023 Peter Siegmund (https://mars3142.dev) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +//---------------------------------------------------------------------------- +#pragma once + +#include + +namespace BLE +{ + class ServerCallbacks : public BLEServerCallbacks + { + void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override; + void onDisconnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override; + void onMtuChanged(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override; + }; +} // namespace BLE