From 2060dd964fb57149834bb225ba4521e1f0582caa Mon Sep 17 00:00:00 2001 From: Bartek Cichocki Date: Fri, 11 Sep 2020 09:52:25 +0200 Subject: [PATCH] [EGD-2625] added BT scan [EGD-2625] moved btstack to mudita_develop branch --- .gitmodules | 4 +- .../ApplicationSettings.cpp | 17 +- .../application-settings/CMakeLists.txt | 1 + .../windows/BtScanWindow.cpp | 106 +++++++++++ .../windows/BtScanWindow.hpp | 39 ++++ .../application-settings/windows/BtWindow.cpp | 10 +- .../Bluetooth/BluetoothWorker.cpp | 31 +++- .../Bluetooth/BluetoothWorker.hpp | 2 + module-bluetooth/Bluetooth/BtCommand.hpp | 19 +- module-bluetooth/Bluetooth/Device.hpp | 31 +++- module-bluetooth/Bluetooth/Error.hpp | 6 +- .../{bluekitchen => profiles}/GAP.cpp | 169 +++++++++--------- .../{bluekitchen => profiles}/PAN.cpp | 0 .../{bluekitchen => profiles}/Worker.cpp | 0 .../btstack_config.h | 0 module-bluetooth/CMakeLists.txt | 2 +- module-bluetooth/lib/btstack | 2 +- module-bluetooth/lib/btstack.cmake | 2 +- module-bsp/bsp/bluetooth/Bluetooth.hpp | 2 +- .../service-bluetooth/ServiceBluetooth.cpp | 7 +- .../messages/BluetoothMessage.hpp | 31 +++- source/MessageType.hpp | 2 + 22 files changed, 369 insertions(+), 114 deletions(-) create mode 100644 module-apps/application-settings/windows/BtScanWindow.cpp create mode 100644 module-apps/application-settings/windows/BtScanWindow.hpp rename module-bluetooth/Bluetooth/interface/{bluekitchen => profiles}/GAP.cpp (59%) rename module-bluetooth/Bluetooth/interface/{bluekitchen => profiles}/PAN.cpp (100%) rename module-bluetooth/Bluetooth/interface/{bluekitchen => profiles}/Worker.cpp (100%) rename module-bluetooth/Bluetooth/interface/{bluekitchen => profiles}/btstack_config.h (100%) diff --git a/.gitmodules b/.gitmodules index 36cb0e900c1158003295a8a2e5f57b39bd58b0c0..2f2c087ef624ade97582b7156f1d2a5763b0bd69 100644 --- a/.gitmodules +++ b/.gitmodules @@ -35,10 +35,10 @@ url = https://github.com/catchorg/Catch2 [submodule "module-utils/date"] path = module-utils/date - url = git@github.com:mudita/date.git + url = ../date.git [submodule "module-audio/Audio/decoder/minimp3"] path = module-audio/Audio/decoder/minimp3 - url = git@github.com:mudita/minimp3.git + url = ../minimp3.git branch = RT1051 [submodule "module-audio/Audio/decoder/taglib"] path = module-audio/Audio/decoder/taglib diff --git a/module-apps/application-settings/ApplicationSettings.cpp b/module-apps/application-settings/ApplicationSettings.cpp index b745194f9ba5d5f7c53bd2fb846198bddc64031b..d48d9d1ff09bd3e2cce136814505f49b2dd45e28 100644 --- a/module-apps/application-settings/ApplicationSettings.cpp +++ b/module-apps/application-settings/ApplicationSettings.cpp @@ -12,6 +12,7 @@ #include "windows/SettingsMainWindow.hpp" #include "windows/LanguageWindow.hpp" #include "windows/BtWindow.hpp" +#include "windows/BtScanWindow.hpp" #include "windows/DateTimeWindow.hpp" #include "windows/FotaWindow.hpp" #include "windows/Info.hpp" @@ -31,7 +32,7 @@ #include "windows/SettingsChange.hpp" #include - +#include #include namespace app @@ -53,7 +54,17 @@ namespace app if (reinterpret_cast(retMsg.get())->retCode == sys::ReturnCodes::Success) { return retMsg; } + if (auto btmsg = dynamic_cast(msgl); btmsg != nullptr) { + auto devices = btmsg->devices; + LOG_INFO("received BT Scan message!"); + auto window = new gui::BtScanWindow(this, devices); + windows.erase(gui::name::window::name_btscan); + windows.insert(std::pair(window->getName(), window)); + setActiveWindow(gui::name::window::name_btscan); + // this->switchWindow("BT_SCAN",nullptr); + render(gui::RefreshModes::GUI_REFRESH_FAST); + } // this variable defines whether message was processed. bool handled = true; @@ -99,6 +110,10 @@ namespace app window = new gui::BtWindow(this); windows.insert(std::pair(window->getName(), window)); + window = new gui::BtScanWindow(this, std::vector()); + window->setVisible(false); + windows.insert(std::pair(window->getName(), window)); + window = new gui::UiTestWindow(this); windows.insert(std::pair(window->getName(), window)); diff --git a/module-apps/application-settings/CMakeLists.txt b/module-apps/application-settings/CMakeLists.txt index 5d993974b035a70d5092ad5a8027250a4f4814bb..0fd63b510c529285b267d74b7135727cc5fe7335 100644 --- a/module-apps/application-settings/CMakeLists.txt +++ b/module-apps/application-settings/CMakeLists.txt @@ -18,6 +18,7 @@ target_sources( ${PROJECT_NAME} windows/SettingsMainWindow.cpp windows/LanguageWindow.cpp windows/BtWindow.cpp + windows/BtScanWindow.cpp windows/UITestWindow.cpp windows/Info.cpp diff --git a/module-apps/application-settings/windows/BtScanWindow.cpp b/module-apps/application-settings/windows/BtScanWindow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1fe5da90052f71073643c060a65474b56250286 --- /dev/null +++ b/module-apps/application-settings/windows/BtScanWindow.cpp @@ -0,0 +1,106 @@ +#include +#include + +#include "service-appmgr/ApplicationManager.hpp" + +#include "../ApplicationSettings.hpp" + +#include "i18/i18.hpp" + +#include "BtScanWindow.hpp" +#include "Label.hpp" +#include "Margins.hpp" + +#include + +namespace gui +{ + + BtScanWindow::BtScanWindow(app::Application *app, std::vector devices) + : AppWindow(app, name::window::name_btscan), devices(devices) + { + buildInterface(); + } + + void BtScanWindow::rebuild() + { + destroyInterface(); + buildInterface(); + } + + void add_box_label2(VBox *layout, UTF8 name, std::function foo) + { + auto el = new gui::Label(nullptr, 0, 0, layout->getWidth(), style::window::label::default_h); + style::window::decorateOption(el); + el->setText(name); + el->activatedCallback = std::move(foo); + layout->addWidget(el); + } + + sys::ReturnCodes message_bt2(app::Application *app, BluetoothMessage::Request req) + { + std::shared_ptr msg = std::make_shared(req); + auto ret = sys::Bus::SendUnicast(msg, "ServiceBluetooth", app); + if (!ret) { + LOG_ERROR("err: %d", static_cast(ret)); + } + return ret ? sys::ReturnCodes::Success : sys::ReturnCodes::Failure; + } + + void BtScanWindow::buildInterface() + { + AppWindow::buildInterface(); + bottomBar->setActive(BottomBar::Side::CENTER, true); + bottomBar->setActive(BottomBar::Side::RIGHT, true); + bottomBar->setText(BottomBar::Side::CENTER, utils::localize.get(style::strings::common::select)); + bottomBar->setText(BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back)); + topBar->setActive(TopBar::Elements::SIGNAL, true); + topBar->setActive(TopBar::Elements::BATTERY, true); + + setTitle(utils::localize.get("BT_scan_results")); + + box = new gui::VBox(this, 0, title->offset_h(), style::window_width, 7 * style::window::label::default_h); + + for (auto device : devices) { + add_box_label2(box, device.name, [=](Item &) { + LOG_DEBUG("Device: %s", device.name.c_str()); + + std::shared_ptr msg = + std::make_shared(bd_addr_to_str(device.address)); + sys::Bus::SendUnicast(msg, "ServiceBluetooth", application, 5000); + + message_bt2(application, BluetoothMessage::Request::StopScan); + // message_bt2(application, BluetoothMessage::Request::Start); + + return true; + }); + } + for (auto &el : box->children) { + el->visible = true; + } + box->setPenWidth(style::window::default_border_no_focus_w); + box->setVisible(true); + setFocusItem(box); + application->render(gui::RefreshModes::GUI_REFRESH_FAST); + } + + bool BtScanWindow::onInput(const InputEvent &inputEvent) + { + return AppWindow::onInput(inputEvent); + } + + void BtScanWindow::destroyInterface() + { + erase(); + invalidate(); + } + + void BtScanWindow::invalidate() noexcept + { + box = nullptr; + } + + void BtScanWindow::onBeforeShow(ShowMode mode, SwitchData *data) + {} + +} // namespace gui diff --git a/module-apps/application-settings/windows/BtScanWindow.hpp b/module-apps/application-settings/windows/BtScanWindow.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d8b15e34d3d68f133d063809d24c5a8c4b67be14 --- /dev/null +++ b/module-apps/application-settings/windows/BtScanWindow.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +#include "AppWindow.hpp" +#include "gui/widgets/Image.hpp" +#include "gui/widgets/Label.hpp" +#include "gui/widgets/Window.hpp" +#include +#include +#include + +namespace gui +{ + namespace name::window + { + inline const std::string name_btscan = "BT_SCAN"; + } + + class BtScanWindow : public AppWindow + { + protected: + VBox *box; + + public: + BtScanWindow(app::Application *app, std::vector devices); + + void onBeforeShow(ShowMode mode, SwitchData *data) override; + void rebuild() override; + void buildInterface() override; + auto onInput(const InputEvent &inputEvent) -> bool override; + void destroyInterface() override; + + private: + void invalidate() noexcept; + std::vector devices; + }; +} // namespace gui diff --git a/module-apps/application-settings/windows/BtWindow.cpp b/module-apps/application-settings/windows/BtWindow.cpp index 8f82385248ffbe5f11ff999ae0a1d18b587dbb57..6756a9dbfaf5dc45065b08fa6c6743a90f9d7865 100644 --- a/module-apps/application-settings/windows/BtWindow.cpp +++ b/module-apps/application-settings/windows/BtWindow.cpp @@ -4,6 +4,7 @@ #include "service-appmgr/ApplicationManager.hpp" #include "../ApplicationSettings.hpp" +#include "../windows/BtScanWindow.hpp" #include "i18/i18.hpp" @@ -41,11 +42,11 @@ namespace gui sys::ReturnCodes message_bt(app::Application *app, BluetoothMessage::Request req) { std::shared_ptr msg = std::make_shared(req); - auto ret = sys::Bus::SendUnicast(msg, "ServiceBluetooth", app, 5000); - if (ret.first != sys::ReturnCodes::Success) { - LOG_ERROR("err: %d", static_cast(ret.first)); + auto ret = sys::Bus::SendUnicast(msg, "ServiceBluetooth", app); + if (!ret) { + LOG_ERROR("err: %d", static_cast(ret)); } - return ret.first; + return ret ? sys::ReturnCodes::Success : sys::ReturnCodes::Failure; } void BtWindow::buildInterface() @@ -80,6 +81,7 @@ namespace gui add_box_label(box, " -> All devices", [=](Item &) { LOG_DEBUG("Callback all devices"); message_bt(application, BluetoothMessage::Request::Scan); + application->setActiveWindow(gui::name::window::name_btscan); return true; }); diff --git a/module-bluetooth/Bluetooth/BluetoothWorker.cpp b/module-bluetooth/Bluetooth/BluetoothWorker.cpp index 8765f2e8822f60642b6d46576ca230b1fe9d5c8c..75df6f4d3b33f05b1ee024b88d8bc27dd4f17930 100644 --- a/module-bluetooth/Bluetooth/BluetoothWorker.cpp +++ b/module-bluetooth/Bluetooth/BluetoothWorker.cpp @@ -2,13 +2,18 @@ #include "log/log.hpp" #include "BtCommand.hpp" +extern "C" +{ +#include "module-bluetooth/lib/btstack/src/btstack_util.h" +}; + using namespace bsp; const char *c_str(Bt::Error::Code code) { switch (code) { - case Bt::Error::Code::Succes: - return "Succes"; + case Bt::Error::Code::Success: + return "Success"; case Bt::Error::Code::NotReady: return "NotReady"; case Bt::Error::Code::SystemError: @@ -49,7 +54,7 @@ bool BluetoothWorker::run() std::string name = "PurePhone"; Bt::set_name(name); // set local namne - // set discoverable (on) + Bt::GAP::set_visibility(true); // Bt::GAP:: Bt::run_stack(&this->bt_worker_task); return true; @@ -62,21 +67,29 @@ bool BluetoothWorker::run() bool BluetoothWorker::scan() { std::vector empty; - + Bt::GAP::setOwnerService(service); auto ret = Bt::GAP::scan(); - if (ret.err != Bt::Error::Succes) { + if (ret.err != Bt::Error::Success) { LOG_ERROR("Cant start scan!: %s %" PRIu32 "", c_str(ret.err), ret.lib_code); return false; } else { LOG_INFO("Scan started!"); + // open new scan window return true; } } +void BluetoothWorker::stop_scan() +{ + Bt::GAP::stop_scan(); +} + bool BluetoothWorker::set_visible() { - LOG_ERROR("TODO"); + static bool visibility = true; + Bt::GAP::set_visibility(visibility); + visibility = !visibility; return false; } @@ -85,7 +98,7 @@ bool BluetoothWorker::start_pan() { Bt::PAN::bnep_setup(); auto err = Bt::PAN::bnep_start(); - if (err.err != Bt::Error::Succes) { + if (err.err != Bt::Error::Success) { LOG_ERROR("PAN setup error: %s %" PRIu32, c_str(err.err), err.lib_code); } return false; @@ -169,3 +182,7 @@ bool BluetoothWorker::handleMessage(uint32_t queueID) return true; } +void BluetoothWorker::set_addr(uint8_t *addr) +{ + LOG_INFO("ADDRESS %s SET!", bd_addr_to_str(addr)); +} diff --git a/module-bluetooth/Bluetooth/BluetoothWorker.hpp b/module-bluetooth/Bluetooth/BluetoothWorker.hpp index 9c313af0d8bc0c5d97e19cdf19d5093e7ed0d4e5..aa511d149600ce99ec215fd2a76fb7f12a00b432 100644 --- a/module-bluetooth/Bluetooth/BluetoothWorker.hpp +++ b/module-bluetooth/Bluetooth/BluetoothWorker.hpp @@ -95,4 +95,6 @@ class BluetoothWorker : private sys::Worker Error aud_init(); /// bluetooth stack id in use unsigned long active_features; + void stop_scan(); + void set_addr(uint8_t *); }; diff --git a/module-bluetooth/Bluetooth/BtCommand.hpp b/module-bluetooth/Bluetooth/BtCommand.hpp index 8e9e39211c8e9780daabbc8272a7b246f062c5e2..99de25627e066a84a13729cd143ddec925a92b41 100644 --- a/module-bluetooth/Bluetooth/BtCommand.hpp +++ b/module-bluetooth/Bluetooth/BtCommand.hpp @@ -8,19 +8,22 @@ namespace Bt { - Error initialize_stack(); - Error register_hw_error_callback(std::function new_callback = nullptr); - Error set_name(std::string &name); - Error run_stack(TaskHandle_t *handle); + auto initialize_stack() -> Error; + auto register_hw_error_callback(std::function new_callback = nullptr) -> Error; + auto set_name(std::string &name) -> Error; + auto run_stack(TaskHandle_t *handle) -> Error; namespace GAP { /// THIS have to be called prior to Bt system start! - Error register_scan(); - Error scan(); + auto register_scan() -> Error; + auto scan() -> Error; + void stop_scan(); + auto set_visibility(bool visibility) -> Error; + void setOwnerService(sys::Service *service); }; // namespace GAP namespace PAN { - Error bnep_start(); - Error bnep_setup(); + auto bnep_start() -> Error; + auto bnep_setup() -> Error; } // namespace PAN }; // namespace Bt diff --git a/module-bluetooth/Bluetooth/Device.hpp b/module-bluetooth/Bluetooth/Device.hpp index b930ad64649c8b381ba6bfe144e72f65db403aeb..1f043ba3d5a94047cbf64f1550b607743736fd2c 100644 --- a/module-bluetooth/Bluetooth/Device.hpp +++ b/module-bluetooth/Bluetooth/Device.hpp @@ -1,11 +1,38 @@ #pragma once #include +#include +#include +#include struct Device { public: - Device(std::string name = "") : name(name) + explicit Device(std::string name = "") : name(std::move(name)) {} - virtual ~Device(){}; + virtual ~Device() = default; std::string name; }; + +enum DEVICE_STATE +{ + REMOTE_NAME_REQUEST, + REMOTE_NAME_INQUIRED, + REMOTE_NAME_FETCHED +}; + +struct Devicei : public Device +{ + public: + bd_addr_t address; + uint8_t pageScanRepetitionMode; + uint16_t clockOffset; + DEVICE_STATE state; + + Devicei(std::string name = "") : Device(std::move(name)) + {} + ~Devicei() override = default; + void address_set(bd_addr_t *addr) + { + memcpy(&address, addr, sizeof address); + } +}; diff --git a/module-bluetooth/Bluetooth/Error.hpp b/module-bluetooth/Bluetooth/Error.hpp index b7f4d1b0ee60bfd80de550001ee86082cb9816d0..a8d19135f8845b972c6b18d31cd74bac8d922a59 100644 --- a/module-bluetooth/Bluetooth/Error.hpp +++ b/module-bluetooth/Bluetooth/Error.hpp @@ -11,13 +11,13 @@ namespace Bt { enum Code { - Succes, + Success, NotReady, SystemError, LibraryError, - } err = Succes; + } err = Success; uint32_t lib_code = 0; - Error(enum Code err = Succes, int lib_code = Succes) : err(err), lib_code(0) + Error(enum Code err = Success, int lib_code = Success) : err(err), lib_code(0) {} }; diff --git a/module-bluetooth/Bluetooth/interface/bluekitchen/GAP.cpp b/module-bluetooth/Bluetooth/interface/profiles/GAP.cpp similarity index 59% rename from module-bluetooth/Bluetooth/interface/bluekitchen/GAP.cpp rename to module-bluetooth/Bluetooth/interface/profiles/GAP.cpp index a60cb00cb7e6e106e5e92cc812bd27078702c35d..c9ac76504b35bb9741adfb5ec87902ce33dda8d5 100644 --- a/module-bluetooth/Bluetooth/interface/bluekitchen/GAP.cpp +++ b/module-bluetooth/Bluetooth/interface/profiles/GAP.cpp @@ -1,43 +1,26 @@ #include #include +#include "Service/Bus.hpp" #include #include +#include +#include "BluetoothWorker.hpp" +#include "Device.hpp" extern "C" { #include "btstack.h" +#include "hci.h" }; btstack_packet_callback_registration_t cb_handler; -enum DEVICE_STATE -{ - REMOTE_NAME_REQUEST, - REMOTE_NAME_INQUIRED, - REMOTE_NAME_FETCHED -}; - -struct Devicei : public Device -{ - public: - bd_addr_t address; - uint8_t pageScanRepetitionMode; - uint16_t clockOffset; - enum DEVICE_STATE state; - - Devicei(std::string name = "") : name(name) - {} - virtual ~Devicei() - {} - void address_set(bd_addr_t *addr) - { - memcpy(&address, addr, sizeof address); - } - std::string name; -}; std::vector devices; -static int getDeviceIndexForAddress(std::vector &devs, bd_addr_t addr) +static auto start_scan() -> int; +static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); + +static auto getDeviceIndexForAddress(std::vector &devs, bd_addr_t addr) -> int { int j; for (j = 0; j < devs.size(); j++) { @@ -56,24 +39,73 @@ enum STATE }; enum STATE state = INIT; +namespace Bt::GAP +{ + static sys::Service *ownerService = nullptr; + + void setOwnerService(sys::Service *service) + { + ownerService = service; + } + + auto register_scan() -> Error + { + LOG_INFO("GAP register scan!"); + /// -> this have to be called prior to power on! + hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR); + cb_handler.callback = &packet_handler; + hci_add_event_handler(&cb_handler); + return Error(); + } + + auto scan() -> Error + { + LOG_INFO("Start scan if active: %d: %d", hci_get_state(), state); + if (hci_get_state() == HCI_STATE_WORKING) { + if (int ret = start_scan(); ret != 0) { + LOG_ERROR("Start scan error!: 0x%X", ret); + return Error(Error::LibraryError, ret); + } + } + else { + return Error(Error::NotReady); + } + return Error(); + } + + void stop_scan() + { + gap_inquiry_force_stop(); + LOG_INFO("Scan stopped!"); + } + + auto set_visibility(bool visibility) -> Error + { + gap_discoverable_control(static_cast(visibility)); + LOG_INFO("Visibility: %s", visibility ? "true" : "false"); + return Error(); + } +} // namespace Bt::GAP + #define INQUIRY_INTERVAL 5 -static int start_scan(void) +static auto start_scan() -> int { LOG_INFO("Starting inquiry scan.."); return gap_inquiry_start(INQUIRY_INTERVAL); } -static int has_more_remote_name_requests(void) +static auto has_more_remote_name_requests() -> int { int i; for (i = 0; i < devices.size(); i++) { - if (devices[i].state == REMOTE_NAME_REQUEST) + if (devices[i].state == REMOTE_NAME_REQUEST) { return 1; + } } return 0; } -static void do_next_remote_name_request(void) +static void do_next_remote_name_request() { int i; for (i = 0; i < devices.size(); i++) { @@ -88,9 +120,9 @@ static void do_next_remote_name_request(void) } } -static void continue_remote_names(void) +static void continue_remote_names() { - if (has_more_remote_name_requests()) { + if (has_more_remote_name_requests() != 0) { do_next_remote_name_request(); return; } @@ -106,15 +138,16 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe int i; int index; - if (packet_type != HCI_EVENT_PACKET) + if (packet_type != HCI_EVENT_PACKET) { return; + } uint8_t event = hci_event_packet_get_type(packet); switch (state) { - /* @text In INIT, an inquiry scan is started, and the application transits to - * ACTIVE state. - */ + /* @text In INIT, an inquiry scan is started, and the application transits to + * ACTIVE state. + */ case INIT: switch (event) { case BTSTACK_EVENT_STATE: @@ -127,23 +160,24 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe } break; - /* @text In ACTIVE, the following events are processed: - * - GAP Inquiry result event: BTstack provides a unified inquiry result that contain - * Class of Device (CoD), page scan mode, clock offset. RSSI and name (from EIR) are optional. - * - Inquiry complete event: the remote name is requested for devices without a fetched - * name. The state of a remote name can be one of the following: - * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED. - * - Remote name request complete event: the remote name is stored in the table and the - * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued. - */ + /* @text In ACTIVE, the following events are processed: + * - GAP Inquiry result event: BTstack provides a unified inquiry result that contain + * Class of Device (CoD), page scan mode, clock offset. RSSI and name (from EIR) are optional. + * - Inquiry complete event: the remote name is requested for devices without a fetched + * name. The state of a remote name can be one of the following: + * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED. + * - Remote name request complete event: the remote name is stored in the table and the + * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued. + */ case ACTIVE: switch (event) { case GAP_EVENT_INQUIRY_RESULT: { gap_event_inquiry_result_get_bd_addr(packet, addr); index = getDeviceIndexForAddress(devices, addr); - if (index >= 0) + if (index >= 0) { break; // already in our list + } Devicei dev; dev.address_set(&addr); @@ -154,22 +188,25 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe LOG_INFO("with COD: 0x%06x, ", (unsigned int)gap_event_inquiry_result_get_class_of_device(packet)); LOG_INFO("pageScan %d, ", dev.pageScanRepetitionMode); LOG_INFO("clock offset 0x%04x", dev.clockOffset); - if (gap_event_inquiry_result_get_rssi_available(packet)) { + if (gap_event_inquiry_result_get_rssi_available(packet) != 0u) { LOG_INFO(", rssi %d dBm", (int8_t)gap_event_inquiry_result_get_rssi(packet)); } - if (gap_event_inquiry_result_get_name_available(packet)) { + if (gap_event_inquiry_result_get_name_available(packet) != 0u) { char name_buffer[240]; int name_len = gap_event_inquiry_result_get_name_len(packet); memcpy(name_buffer, gap_event_inquiry_result_get_name(packet), name_len); name_buffer[name_len] = 0; LOG_INFO(", name '%s'", name_buffer); + dev.name = std::string{name_buffer}; dev.state = REMOTE_NAME_FETCHED; - ; } else { dev.state = REMOTE_NAME_REQUEST; } devices.push_back(dev); + auto msg = std::make_shared(devices); + auto ret = sys::Bus::SendUnicast(msg, "ApplicationSettings", Bt::GAP::ownerService, 5000); + } break; case GAP_EVENT_INQUIRY_COMPLETE: @@ -197,6 +234,8 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe if (index + 1 == devices.size()) { LOG_INFO("Scanned all"); state = DONE; + gap_inquiry_stop(); + break; } continue_remote_names(); } break; @@ -210,35 +249,3 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe break; } } - -namespace Bt -{ - namespace GAP - { - - Error register_scan() - { - LOG_INFO("GAP register scan!"); - /// -> this have to be called prior to power on! - hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR); - cb_handler.callback = &packet_handler; - hci_add_event_handler(&cb_handler); - return Error(); - } - - Error scan() - { - LOG_INFO("Start scan if active: %d: %d", hci_get_state(), state); - if (hci_get_state() == HCI_STATE_WORKING) { - if (int ret = start_scan() != 0) { - LOG_ERROR("Start scan error!: 0x%X", ret); - return Error(Error::LibraryError, ret); - } - } - else { - return Error(Error::NotReady); - } - return Error(); - } - } // namespace GAP -} // namespace Bt diff --git a/module-bluetooth/Bluetooth/interface/bluekitchen/PAN.cpp b/module-bluetooth/Bluetooth/interface/profiles/PAN.cpp similarity index 100% rename from module-bluetooth/Bluetooth/interface/bluekitchen/PAN.cpp rename to module-bluetooth/Bluetooth/interface/profiles/PAN.cpp diff --git a/module-bluetooth/Bluetooth/interface/bluekitchen/Worker.cpp b/module-bluetooth/Bluetooth/interface/profiles/Worker.cpp similarity index 100% rename from module-bluetooth/Bluetooth/interface/bluekitchen/Worker.cpp rename to module-bluetooth/Bluetooth/interface/profiles/Worker.cpp diff --git a/module-bluetooth/Bluetooth/interface/bluekitchen/btstack_config.h b/module-bluetooth/Bluetooth/interface/profiles/btstack_config.h similarity index 100% rename from module-bluetooth/Bluetooth/interface/bluekitchen/btstack_config.h rename to module-bluetooth/Bluetooth/interface/profiles/btstack_config.h diff --git a/module-bluetooth/CMakeLists.txt b/module-bluetooth/CMakeLists.txt index 3e833998338e0db088c88c8fda7821cf666a4266..187c14283e898af559e1b6f76ab852a30620f6af 100644 --- a/module-bluetooth/CMakeLists.txt +++ b/module-bluetooth/CMakeLists.txt @@ -11,7 +11,7 @@ set(SOURCES message("Build with BlueKitchen") include(lib/btstack.cmake) - +message(${CMAKE_CXX_FLAGS_RELWITHDEBINFO}) message("Sources: " ${SOURCES}) add_library(${PROJECT_NAME} STATIC ${SOURCES} ${BOARD_DIR_SOURCES}) diff --git a/module-bluetooth/lib/btstack b/module-bluetooth/lib/btstack index a652416846deeab67f1d4c9295d746db0f19a911..725ab2bc2adc415a0f50c5f34d85e24f5e22922d 160000 --- a/module-bluetooth/lib/btstack +++ b/module-bluetooth/lib/btstack @@ -1 +1 @@ -Subproject commit a652416846deeab67f1d4c9295d746db0f19a911 +Subproject commit 725ab2bc2adc415a0f50c5f34d85e24f5e22922d diff --git a/module-bluetooth/lib/btstack.cmake b/module-bluetooth/lib/btstack.cmake index ec7cee57f5d62343a98026a22b1e8e293f695054..cff9eea15b601646efcc7dad6133aff5212d97e6 100644 --- a/module-bluetooth/lib/btstack.cmake +++ b/module-bluetooth/lib/btstack.cmake @@ -1,5 +1,5 @@ set(BT_GLU "${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/glucode/") -set(BT_INT "${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/interface/bluekitchen/") +set(BT_INT "${CMAKE_CURRENT_SOURCE_DIR}/Bluetooth/interface/profiles/") set(BT_STACK_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/lib/btstack") diff --git a/module-bsp/bsp/bluetooth/Bluetooth.hpp b/module-bsp/bsp/bluetooth/Bluetooth.hpp index bb3f54874d738e0ef90a466da0d6b9bde765d884..3ad7f6b4149865cd89e2dad15c60a4a663e7c8fc 100644 --- a/module-bsp/bsp/bluetooth/Bluetooth.hpp +++ b/module-bsp/bsp/bluetooth/Bluetooth.hpp @@ -100,7 +100,7 @@ namespace bsp { // uart specyfic }; - // Common stuff for Bluetopia and bluekitchen +clean listing for overrrides + // Common stuff for Bluetopia and profiles +clean listing for overrrides class BluetoothCommon : public BTdev { public: diff --git a/module-services/service-bluetooth/ServiceBluetooth.cpp b/module-services/service-bluetooth/ServiceBluetooth.cpp index d244516706858442a29c5b278d0595b136f24c77..cb73b6d4bcdf902f8cf15c4a13739b04b892eb63 100644 --- a/module-services/service-bluetooth/ServiceBluetooth.cpp +++ b/module-services/service-bluetooth/ServiceBluetooth.cpp @@ -58,8 +58,9 @@ sys::Message_t ServiceBluetooth::DataReceivedHandler(sys::DataMessage *msg, sys: else { return std::make_shared(sys::ReturnCodes::Failure); } + case BluetoothMessage::StopScan: + worker->stop_scan(); break; - case BluetoothMessage::PAN: { /// TODO request lwip first... /// because TODO blocking message - wrecks system @@ -91,6 +92,10 @@ sys::Message_t ServiceBluetooth::DataReceivedHandler(sys::DataMessage *msg, sys: catch (std::exception &ex) { LOG_ERROR("Exception on BtService!: %s", ex.what()); } + if (dynamic_cast(msg) != nullptr) { + auto addrMsg = static_cast(msg); + worker->set_addr(addrMsg->addr); + } return std::make_shared(); } diff --git a/module-services/service-bluetooth/messages/BluetoothMessage.hpp b/module-services/service-bluetooth/messages/BluetoothMessage.hpp index 8d9a356b6b92ab7f731ced620f8479191a3a72d6..79dfbaadf48c3c45f0e542bc39c65a3d684cefb2 100644 --- a/module-services/service-bluetooth/messages/BluetoothMessage.hpp +++ b/module-services/service-bluetooth/messages/BluetoothMessage.hpp @@ -1,7 +1,15 @@ #pragma once #include "Service/Message.hpp" + +#include #include "MessageType.hpp" +#include "Bluetooth/Device.hpp" + +extern "C" +{ +#include "module-bluetooth/lib/btstack/src/btstack_util.h" +}; class BluetoothMessage : public sys::DataMessage { @@ -11,10 +19,31 @@ class BluetoothMessage : public sys::DataMessage None, Start, Scan, + StopScan, PAN, Visible, }; enum Request req = Request::None; BluetoothMessage(enum Request req = None) : sys::DataMessage(MessageType::BluetoothRequest), req(req){}; - virtual ~BluetoothMessage() = default; + ~BluetoothMessage() override = default; +}; + +class BluetoothScanMessage : public sys::DataMessage +{ + public: + std::vector devices; + BluetoothScanMessage(std::vector devices) + : sys::DataMessage(MessageType::BluetoothScanResult), devices(std::move(devices)){}; + ~BluetoothScanMessage() override = default; +}; + +class BluetoothAddrMessage : public sys::DataMessage +{ + public: + bd_addr_t addr; + BluetoothAddrMessage(std::string addr) : sys::DataMessage(MessageType::BluetoothAddrResult) + { + sscanf_bd_addr(addr.c_str(), this->addr); + }; + ~BluetoothAddrMessage() override = default; }; diff --git a/source/MessageType.hpp b/source/MessageType.hpp index f9ffa5cc3c4fc5aff49935c8d4315849022257f4..2ca1502bdc524a24653d2f24255e173d54c2034a 100644 --- a/source/MessageType.hpp +++ b/source/MessageType.hpp @@ -192,6 +192,8 @@ enum class MessageType // bluetooth messages BluetoothRequest, + BluetoothScanResult, + BluetoothAddrResult, LwIP_request, EVM_GPIO,