From 5fba2cfc9959637312ecc74af95b823cda2c7f30 Mon Sep 17 00:00:00 2001 From: Dzulfikar Ahmad Samhan <23522019@std.stei.itb.ac.id> Date: Sun, 28 Apr 2024 04:06:47 +0000 Subject: [PATCH] Feat/ble --- debug-app/app/prj.conf | 6 +- debug-app/app/src/main.cpp | 15 ++++- debug-app/include/ble-scanner.h | 23 +++++++ debug-app/include/debug-handler.h | 1 + debug-app/include/device-parser.h | 26 ++++++++ debug-app/include/utilities.h | 8 ++- debug-app/module/CMakeLists.txt | 3 +- debug-app/module/ble-scanner/CMakeLists.txt | 1 + .../module/ble-scanner/src/ble-scanner.cpp | 64 +++++++++++++++++++ debug-app/module/handler/CMakeLists.txt | 3 +- .../debug-handler/src/debug-handler.cpp | 13 +++- .../handler/device-parser/CMakeLists.txt | 1 + .../device-parser/src/device-parser.cpp | 39 +++++++++++ .../handler/main-handler/src/main-handler.cpp | 2 +- 14 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 debug-app/include/ble-scanner.h create mode 100644 debug-app/include/device-parser.h create mode 100644 debug-app/module/ble-scanner/CMakeLists.txt create mode 100644 debug-app/module/ble-scanner/src/ble-scanner.cpp create mode 100644 debug-app/module/handler/device-parser/CMakeLists.txt create mode 100644 debug-app/module/handler/device-parser/src/device-parser.cpp diff --git a/debug-app/app/prj.conf b/debug-app/app/prj.conf index dc438be..bc5e9b7 100644 --- a/debug-app/app/prj.conf +++ b/debug-app/app/prj.conf @@ -12,7 +12,7 @@ CONFIG_MAIN_STACK_SIZE=2048 CONFIG_HEAP_MEM_POOL_SIZE=512 CONFIG_THREAD_NAME=y CONFIG_ASSERT=y -CONFIG_NO_OPTIMIZATIONS=y +CONFIG_NO_OPTIMIZATIONS=n # Peripheral # CONFIG_ADC=y @@ -31,6 +31,10 @@ CONFIG_ZBUS_LOG_LEVEL_INF=y CONFIG_ZBUS_RUNTIME_OBSERVERS=y CONFIG_ZBUS_CHANNEL_NAME=y +#ble +CONFIG_BT=y +CONFIG_BT_OBSERVER=y + # PM # CONFIG_PM=y # CONFIG_PM_DEVICE=y diff --git a/debug-app/app/src/main.cpp b/debug-app/app/src/main.cpp index b440f77..923ebbe 100644 --- a/debug-app/app/src/main.cpp +++ b/debug-app/app/src/main.cpp @@ -5,6 +5,8 @@ #include "main-handler.h" #include "debug-handler.h" #include "driver-hook.h" +#include "ble-scanner.h" +#include "device-parser.h" #include "utilities.h" @@ -12,6 +14,7 @@ LOG_MODULE_REGISTER(main); DriverMessage_t defaultDriverMsg; StateMessage_t defaultStateMsg; +BleData_t defaultBleData; ZBUS_CHAN_DEFINE(driverRequestChannel, /* Name */ DriverMessage_t, /* Message type */ @@ -29,6 +32,14 @@ ZBUS_CHAN_DEFINE(driverResponseChannel, /* Name */ ZBUS_MSG_INIT(defaultDriverMsg) ); +ZBUS_CHAN_DEFINE(bleScanChannel, /* Name */ + BleData_t, /* Message type */ + NULL, /* Validator */ + NULL, /* User data */ + ZBUS_OBSERVERS_EMPTY, /* observers */ + ZBUS_MSG_INIT(defaultBleData) +); + ZBUS_CHAN_DEFINE(stateChannel, /* Name */ StateMessage_t, /* Message type */ NULL, /* Validator */ @@ -40,8 +51,10 @@ ZBUS_CHAN_DEFINE(stateChannel, /* Name */ int main() { MainHandler mainHandler; - DriverHook DriverHook; + DriverHook driverHook; DebugHandler debugHanddler; + BleScanner bleScanner; + DeviceParser deviceParser; while(true) { // LOG_INF("Hello World! run app\n"); diff --git a/debug-app/include/ble-scanner.h b/debug-app/include/ble-scanner.h new file mode 100644 index 0000000..24742c1 --- /dev/null +++ b/debug-app/include/ble-scanner.h @@ -0,0 +1,23 @@ +#include <zephyr/kernel.h> +#include <zephyr/bluetooth/bluetooth.h> +#include <zephyr/bluetooth/hci.h> + +class BleScanner { +public: + /** Constructor. + * + **/ + BleScanner(); + + /** Destructor. + * + **/ + ~BleScanner(); + + static void DriverRequestListener(const struct zbus_channel *chan); + +private: + static void BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad); + + +}; \ No newline at end of file diff --git a/debug-app/include/debug-handler.h b/debug-app/include/debug-handler.h index 7264d2c..602293c 100644 --- a/debug-app/include/debug-handler.h +++ b/debug-app/include/debug-handler.h @@ -19,6 +19,7 @@ public: static void DriverResponseListener(const struct zbus_channel *chan); static void StateListener(const struct zbus_channel *chan); + static void BleListener(const struct zbus_channel *chan); private: static void DebugThread(DebugHandler *context); diff --git a/debug-app/include/device-parser.h b/debug-app/include/device-parser.h new file mode 100644 index 0000000..eefa340 --- /dev/null +++ b/debug-app/include/device-parser.h @@ -0,0 +1,26 @@ +#include <zephyr/kernel.h> +#include "utilities.h" + +typedef struct BleSensorData { + uint8_t tempSensor; +} BleSensorData_t; + +class DeviceParser { +public: + /** Constructor. + * + **/ + DeviceParser(); + + /** Destructor. + * + **/ + ~DeviceParser(); + + static void BleScanListener(const struct zbus_channel *chan); + bool BleDeviceParser(const BleData_t *data, BleSensorData_t *parsedData); + +private: + + +}; \ No newline at end of file diff --git a/debug-app/include/utilities.h b/debug-app/include/utilities.h index ce70b9e..9c34753 100644 --- a/debug-app/include/utilities.h +++ b/debug-app/include/utilities.h @@ -7,6 +7,7 @@ typedef enum DriverNumb{ SENSOR_1, SENSOR_2, SENSOR_3, + BLUETOOTH, ACTUATOR_1, } DriverNumb_e; @@ -24,4 +25,9 @@ typedef struct DriverMessage { typedef struct StateMessage { StateFlag_e flag; std::vector<uint8_t> value; -} StateMessage_t; \ No newline at end of file +} StateMessage_t; + +typedef struct BleData { + std::string address; + std::vector<uint8_t> data; +} BleData_t; \ No newline at end of file diff --git a/debug-app/module/CMakeLists.txt b/debug-app/module/CMakeLists.txt index 379a5da..6ccbd8e 100644 --- a/debug-app/module/CMakeLists.txt +++ b/debug-app/module/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(driver-hook) -add_subdirectory(handler) \ No newline at end of file +add_subdirectory(handler) +add_subdirectory(ble-scanner) \ No newline at end of file diff --git a/debug-app/module/ble-scanner/CMakeLists.txt b/debug-app/module/ble-scanner/CMakeLists.txt new file mode 100644 index 0000000..13bbb63 --- /dev/null +++ b/debug-app/module/ble-scanner/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/ble-scanner.cpp) \ No newline at end of file diff --git a/debug-app/module/ble-scanner/src/ble-scanner.cpp b/debug-app/module/ble-scanner/src/ble-scanner.cpp new file mode 100644 index 0000000..cfd98b5 --- /dev/null +++ b/debug-app/module/ble-scanner/src/ble-scanner.cpp @@ -0,0 +1,64 @@ +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include <stdio.h> +#include <string.h> + +#include "utilities.h" +#include "ble-scanner.h" + +LOG_MODULE_REGISTER(BleScanner); +ZBUS_CHAN_DECLARE(driverRequestChannel, bleScanChannel); + +static BleScanner *context; + +static struct bt_le_scan_param scan_param = { + .type = BT_LE_SCAN_TYPE_PASSIVE, + .options = BT_LE_SCAN_OPT_NONE, + .interval = BT_GAP_SCAN_FAST_INTERVAL, + .window = BT_GAP_SCAN_FAST_WINDOW, + }; + +// ZBUS_LISTENER_DEFINE(bleScanListener, context->DriverRequestListener); + +BleScanner::BleScanner(){ + context = this; + int err = bt_enable(NULL); + LOG_INF("start bt"); + if (err != 0) { + printk("Bluetooth init failed (err %d)\n", err); + return; + } + + // zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT); + + //TODO: remove when bus communication has fully established + bt_le_scan_start(&scan_param, BleDevicefound); + + +} + +void BleScanner::BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad){ + char addr_str[BT_ADDR_LE_STR_LEN]; + bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); + BleData_t sentMessage = { + .address = std::string(addr_str, sizeof(addr_str)), + .data = std::vector<uint8_t>(ad->data, ad->data + ad->len) + }; + zbus_chan_pub(&bleScanChannel, &sentMessage, K_MSEC(200)); +} + +// void DriverRequestListener(const struct zbus_channel *chan){ +// const DriverMessage_t *msg; +// if (&driverRequestChannel == chan) { +// msg = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access +// if(msg->number == BLUETOOTH){ +// if(msg->value[0]){ +// bt_le_scan_start(&scan_param, bleDevicefound); +// } +// } +// } +// else{ +// return; +// } +// } diff --git a/debug-app/module/handler/CMakeLists.txt b/debug-app/module/handler/CMakeLists.txt index 2ae79be..62ac6d9 100644 --- a/debug-app/module/handler/CMakeLists.txt +++ b/debug-app/module/handler/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(main-handler) -add_subdirectory(debug-handler) \ No newline at end of file +add_subdirectory(debug-handler) +add_subdirectory(device-parser) \ No newline at end of file diff --git a/debug-app/module/handler/debug-handler/src/debug-handler.cpp b/debug-app/module/handler/debug-handler/src/debug-handler.cpp index d7553c9..181ab9b 100644 --- a/debug-app/module/handler/debug-handler/src/debug-handler.cpp +++ b/debug-app/module/handler/debug-handler/src/debug-handler.cpp @@ -12,7 +12,7 @@ #define DEBUG_THREAD_PRIORITY 5 LOG_MODULE_REGISTER(Debug_Handler); -ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel); +ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel, bleScanChannel); K_THREAD_STACK_DEFINE(debugThreadStack, DEBUG_THREAD_STACK_SIZE); @@ -23,6 +23,7 @@ static DebugHandler *context; ZBUS_LISTENER_DEFINE(debugDriverListener, context->DriverResponseListener); ZBUS_LISTENER_DEFINE(stateListener, context->StateListener); +ZBUS_LISTENER_DEFINE(bleListener, context->BleListener); DebugHandler::DebugHandler(){ context = this; @@ -33,6 +34,7 @@ DebugHandler::DebugHandler(){ k_thread_suspend(debugThreadId); zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT); zbus_chan_add_obs(&stateChannel, &stateListener, K_NO_WAIT); + zbus_chan_add_obs(&bleScanChannel, &bleListener, K_NO_WAIT); } void DebugHandler::Init(){ @@ -61,4 +63,13 @@ void DebugHandler::StateListener(const struct zbus_channel *chan){ state = (StateMessage_t *)zbus_chan_const_msg(chan); // Direct message access LOG_INF("From listener -> state =%d, value 0=%d", state->flag, state->value[0]); } +} + +void DebugHandler::BleListener(const struct zbus_channel *chan){ + const BleData_t *ble; + if (&bleScanChannel == chan) { + ble = (BleData_t *)zbus_chan_const_msg(chan); // Direct message access + LOG_INF("Ble -> address =%s", ble->address.c_str()); + LOG_HEXDUMP_INF(ble->data.data(), ble->data.size(), "data raw"); + } } \ No newline at end of file diff --git a/debug-app/module/handler/device-parser/CMakeLists.txt b/debug-app/module/handler/device-parser/CMakeLists.txt new file mode 100644 index 0000000..1d32086 --- /dev/null +++ b/debug-app/module/handler/device-parser/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/device-parser.cpp) \ No newline at end of file diff --git a/debug-app/module/handler/device-parser/src/device-parser.cpp b/debug-app/module/handler/device-parser/src/device-parser.cpp new file mode 100644 index 0000000..1a13531 --- /dev/null +++ b/debug-app/module/handler/device-parser/src/device-parser.cpp @@ -0,0 +1,39 @@ +#include "device-parser.h" +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +LOG_MODULE_REGISTER(DeviceParser); +ZBUS_CHAN_DECLARE(bleScanChannel); + +static DeviceParser *context; + +ZBUS_LISTENER_DEFINE(scanListener, context->BleScanListener); + +DeviceParser::DeviceParser(){ + context = this; + zbus_chan_add_obs(&bleScanChannel, &scanListener, K_NO_WAIT); +} + +void DeviceParser::BleScanListener(const struct zbus_channel *chan){ + const BleData_t *msg; + if (&bleScanChannel == chan) { + msg = (BleData_t *)zbus_chan_const_msg(chan); // Direct message access + BleSensorData_t parsedData; + if(context->BleDeviceParser(msg, &parsedData)){ + LOG_INF("Device %s has data %d", msg->address.c_str(), parsedData.tempSensor); + } + } + else{ + return; + } +} + +bool DeviceParser::BleDeviceParser(const BleData_t *data, BleSensorData_t *parsedData){ + if(data->data.size() >= 8){ + if(data->data[5] == 0x1a && data->data[6] == 0x18){ + parsedData->tempSensor = data->data[7]; + return true; + } + } + return false; +} \ No newline at end of file diff --git a/debug-app/module/handler/main-handler/src/main-handler.cpp b/debug-app/module/handler/main-handler/src/main-handler.cpp index 24dd688..50ed4ee 100644 --- a/debug-app/module/handler/main-handler/src/main-handler.cpp +++ b/debug-app/module/handler/main-handler/src/main-handler.cpp @@ -40,7 +40,7 @@ MainHandler::MainHandler(){ k_sem_init(&responseWait, 0, 1); // zbus_chan_add_obs(&driverResponseChannel, &sensorSubs, K_MSEC(200)); - zbus_chan_add_obs(&driverResponseChannel, &driverListener, K_NO_WAIT); + // zbus_chan_add_obs(&driverResponseChannel, &driverListener, K_NO_WAIT); } void MainHandler::Init(){ -- GitLab