diff --git a/simpleble/CMakeLists.txt b/simpleble/CMakeLists.txt index 8c7946ec..71428403 100644 --- a/simpleble/CMakeLists.txt +++ b/simpleble/CMakeLists.txt @@ -150,17 +150,18 @@ list(APPEND PRIVATE_COMPILE_DEFINITIONS SIMPLEBLE_LOG_LEVEL=SIMPLEBLE_LOG_LEVEL_ list(APPEND PRIVATE_COMPILE_DEFINITIONS SIMPLEBLE_VERSION="${PROJECT_VERSION}") # Detect the operating system and load the necessary dependencies -if(SIMPLEBLE_PLAIN) - message(STATUS "Plain Flavor Requested") +message(STATUS "Plain Flavor Requested") - target_sources(simpleble PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/AdapterBase.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/PeripheralBase.cpp) +list(APPEND PRIVATE_COMPILE_DEFINITIONS SIMPLEBLE_BACKEND_PLAIN_ENABLED) - target_include_directories(simpleble PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain) +target_sources(simpleble PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/AdapterPlain.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/PeripheralPlain.cpp) + +target_include_directories(simpleble PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain) -elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") message(STATUS "Linux Host Detected") find_package(DBus1 REQUIRED) diff --git a/simpleble/src/backends/plain/AdapterBase.cpp b/simpleble/src/backends/plain/AdapterBase.cpp deleted file mode 100644 index e7ab8d90..00000000 --- a/simpleble/src/backends/plain/AdapterBase.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "AdapterBase.h" -#include "CommonUtils.h" -#include "PeripheralBase.h" -#include "PeripheralBuilder.h" - -#include - -using namespace SimpleBLE; - -std::vector> AdapterBase::get_adapters() { - std::vector> adapter_list; - adapter_list.push_back(std::make_shared()); - return adapter_list; -} - -bool AdapterBase::bluetooth_enabled() { return true; } - -AdapterBase::AdapterBase() {} - -AdapterBase::~AdapterBase() {} - -void* AdapterBase::underlying() const { return nullptr; } - -std::string AdapterBase::identifier() { return "Plain Adapter"; } - -BluetoothAddress AdapterBase::address() { return "AA:BB:CC:DD:EE:FF"; } - -void AdapterBase::scan_start() { - is_scanning_ = true; - SAFE_CALLBACK_CALL(this->callback_on_scan_start_); - - PeripheralBuilder peripheral_builder(std::make_shared()); - SAFE_CALLBACK_CALL(this->callback_on_scan_found_, peripheral_builder); - SAFE_CALLBACK_CALL(this->callback_on_scan_updated_, peripheral_builder); -} - -void AdapterBase::scan_stop() { - is_scanning_ = false; - SAFE_CALLBACK_CALL(this->callback_on_scan_stop_); -} - -void AdapterBase::scan_for(int timeout_ms) { - scan_start(); - std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms)); - scan_stop(); -} - -bool AdapterBase::scan_is_active() { return is_scanning_; } - -std::vector AdapterBase::scan_get_results() { - std::vector peripherals; - - PeripheralBuilder peripheral_builder(std::make_shared()); - peripherals.push_back(peripheral_builder); - - return peripherals; -} - -std::vector AdapterBase::get_paired_peripherals() { - std::vector peripherals; - - PeripheralBuilder peripheral_builder(std::make_shared()); - peripherals.push_back(peripheral_builder); - - return peripherals; -} - -void AdapterBase::set_callback_on_scan_start(std::function on_scan_start) { - if (on_scan_start) { - callback_on_scan_start_.load(std::move(on_scan_start)); - } else { - callback_on_scan_start_.unload(); - } -} - -void AdapterBase::set_callback_on_scan_stop(std::function on_scan_stop) { - if (on_scan_stop) { - callback_on_scan_stop_.load(std::move(on_scan_stop)); - } else { - callback_on_scan_stop_.unload(); - } -} - -void AdapterBase::set_callback_on_scan_updated(std::function on_scan_updated) { - if (on_scan_updated) { - callback_on_scan_updated_.load(std::move(on_scan_updated)); - } else { - callback_on_scan_updated_.unload(); - } -} - -void AdapterBase::set_callback_on_scan_found(std::function on_scan_found) { - if (on_scan_found) { - callback_on_scan_found_.load(std::move(on_scan_found)); - } else { - callback_on_scan_found_.unload(); - } -} diff --git a/simpleble/src/backends/plain/AdapterPlain.cpp b/simpleble/src/backends/plain/AdapterPlain.cpp new file mode 100644 index 00000000..bee92185 --- /dev/null +++ b/simpleble/src/backends/plain/AdapterPlain.cpp @@ -0,0 +1,98 @@ +#include "AdapterPlain.h" +#include "CommonUtils.h" +#include "PeripheralPlain.h" +#include "PeripheralBuilder.h" + +#include + +using namespace SimpleBLE; + +std::vector> AdapterPlain::get_adapters() { + std::vector> adapter_list; + adapter_list.push_back(std::static_pointer_cast(std::make_shared())); + return adapter_list; +} + +bool AdapterPlain::bluetooth_enabled() { return true; } + +AdapterPlain::AdapterPlain() {} + +AdapterPlain::~AdapterPlain() {} + +void* AdapterPlain::underlying() const { return nullptr; } + +std::string AdapterPlain::identifier() { return "Plain Adapter"; } + +BluetoothAddress AdapterPlain::address() { return "AA:BB:CC:DD:EE:FF"; } + +void AdapterPlain::scan_start() { + is_scanning_ = true; + SAFE_CALLBACK_CALL(callback_on_scan_start_); + + PeripheralBuilder peripheral_builder(std::make_shared()); + SAFE_CALLBACK_CALL(callback_on_scan_found_, peripheral_builder); + SAFE_CALLBACK_CALL(callback_on_scan_updated_, peripheral_builder); +} + +void AdapterPlain::scan_stop() { + is_scanning_ = false; + SAFE_CALLBACK_CALL(callback_on_scan_stop_); +} + +void AdapterPlain::scan_for(int timeout_ms) { + scan_start(); + std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms)); + scan_stop(); +} + +bool AdapterPlain::scan_is_active() { return is_scanning_; } + +std::vector AdapterPlain::scan_get_results() { + std::vector peripherals; + + PeripheralBuilder peripheral_builder(std::static_pointer_cast(std::make_shared())); + peripherals.push_back(peripheral_builder); + + return peripherals; +} + +std::vector AdapterPlain::get_paired_peripherals() { + std::vector peripherals; + + PeripheralBuilder peripheral_builder(std::static_pointer_cast(std::make_shared())); + peripherals.push_back(peripheral_builder); + + return peripherals; +} + +void AdapterPlain::set_callback_on_scan_start(std::function on_scan_start) { + if (on_scan_start) { + callback_on_scan_start_.load(std::move(on_scan_start)); + } else { + callback_on_scan_start_.unload(); + } +} + +void AdapterPlain::set_callback_on_scan_stop(std::function on_scan_stop) { + if (on_scan_stop) { + callback_on_scan_stop_.load(std::move(on_scan_stop)); + } else { + callback_on_scan_stop_.unload(); + } +} + +void AdapterPlain::set_callback_on_scan_updated(std::function on_scan_updated) { + if (on_scan_updated) { + callback_on_scan_updated_.load(std::move(on_scan_updated)); + } else { + callback_on_scan_updated_.unload(); + } +} + +void AdapterPlain::set_callback_on_scan_found(std::function on_scan_found) { + if (on_scan_found) { + callback_on_scan_found_.load(std::move(on_scan_found)); + } else { + callback_on_scan_found_.unload(); + } +} diff --git a/simpleble/src/backends/plain/AdapterBase.h b/simpleble/src/backends/plain/AdapterPlain.h similarity index 91% rename from simpleble/src/backends/plain/AdapterBase.h rename to simpleble/src/backends/plain/AdapterPlain.h index 1261e865..0be497a8 100644 --- a/simpleble/src/backends/plain/AdapterBase.h +++ b/simpleble/src/backends/plain/AdapterPlain.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -15,10 +16,10 @@ namespace SimpleBLE { -class AdapterBase { +class AdapterPlain : public AdapterBase { public: - AdapterBase(); - virtual ~AdapterBase(); + AdapterPlain(); + virtual ~AdapterPlain(); void* underlying() const; diff --git a/simpleble/src/backends/plain/PeripheralBase.cpp b/simpleble/src/backends/plain/PeripheralPlain.cpp similarity index 60% rename from simpleble/src/backends/plain/PeripheralBase.cpp rename to simpleble/src/backends/plain/PeripheralPlain.cpp index 69627180..c6dcbcb8 100644 --- a/simpleble/src/backends/plain/PeripheralBase.cpp +++ b/simpleble/src/backends/plain/PeripheralPlain.cpp @@ -1,4 +1,4 @@ -#include "PeripheralBase.h" +#include "PeripheralPlain.h" #include "CharacteristicBuilder.h" #include "DescriptorBuilder.h" @@ -15,23 +15,23 @@ using namespace std::chrono_literals; const SimpleBLE::BluetoothUUID BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb"; const SimpleBLE::BluetoothUUID BATTERY_CHARACTERISTIC_UUID = "00002a19-0000-1000-8000-00805f9b34fb"; -PeripheralBase::PeripheralBase() {} +PeripheralPlain::PeripheralPlain() {} -PeripheralBase::~PeripheralBase() {} +PeripheralPlain::~PeripheralPlain() {} -void* PeripheralBase::underlying() const { return nullptr; } +void* PeripheralPlain::underlying() const { return nullptr; } -std::string PeripheralBase::identifier() { return "Plain Peripheral"; } +std::string PeripheralPlain::identifier() { return "Plain Peripheral"; } -BluetoothAddress PeripheralBase::address() { return "11:22:33:44:55:66"; } +BluetoothAddress PeripheralPlain::address() { return "11:22:33:44:55:66"; } -BluetoothAddressType PeripheralBase::address_type() { return BluetoothAddressType::PUBLIC; }; +BluetoothAddressType PeripheralPlain::address_type() { return BluetoothAddressType::PUBLIC; }; -int16_t PeripheralBase::rssi() { return -60; } +int16_t PeripheralPlain::rssi() { return -60; } -int16_t PeripheralBase::tx_power() { return 5; } +int16_t PeripheralPlain::tx_power() { return 5; } -uint16_t PeripheralBase::mtu() { +uint16_t PeripheralPlain::mtu() { if (is_connected()) { return 247; } else { @@ -39,25 +39,25 @@ uint16_t PeripheralBase::mtu() { } } -void PeripheralBase::connect() { +void PeripheralPlain::connect() { connected_ = true; paired_ = true; - SAFE_CALLBACK_CALL(this->callback_on_connected_); + SAFE_CALLBACK_CALL(callback_on_connected_); } -void PeripheralBase::disconnect() { +void PeripheralPlain::disconnect() { connected_ = false; - SAFE_CALLBACK_CALL(this->callback_on_disconnected_); + SAFE_CALLBACK_CALL(callback_on_disconnected_); } -bool PeripheralBase::is_connected() { return connected_; } +bool PeripheralPlain::is_connected() { return connected_; } -bool PeripheralBase::is_connectable() { return true; } +bool PeripheralPlain::is_connectable() { return true; } -bool PeripheralBase::is_paired() { return paired_; } +bool PeripheralPlain::is_paired() { return paired_; } -void PeripheralBase::unpair() { paired_ = false; } +void PeripheralPlain::unpair() { paired_ = false; } -std::vector PeripheralBase::services() { +std::vector PeripheralPlain::services() { if (!connected_) return {}; std::vector service_list; @@ -68,19 +68,19 @@ std::vector PeripheralBase::services() { return service_list; } -std::vector PeripheralBase::advertised_services() { return {}; } +std::vector PeripheralPlain::advertised_services() { return {}; } -std::map PeripheralBase::manufacturer_data() { return {{0x004C, "test"}}; } +std::map PeripheralPlain::manufacturer_data() { return {{0x004C, "test"}}; } -ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const& characteristic) { return {}; } +ByteArray PeripheralPlain::read(BluetoothUUID const& service, BluetoothUUID const& characteristic) { return {}; } -void PeripheralBase::write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, +void PeripheralPlain::write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) {} -void PeripheralBase::write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, +void PeripheralPlain::write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) {} -void PeripheralBase::notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, +void PeripheralPlain::notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function callback) { if (callback) { callback_mutex_.lock(); @@ -103,7 +103,7 @@ void PeripheralBase::notify(BluetoothUUID const& service, BluetoothUUID const& c } } -void PeripheralBase::indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, +void PeripheralPlain::indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function callback) { if (callback) { callback_mutex_.lock(); @@ -126,20 +126,20 @@ void PeripheralBase::indicate(BluetoothUUID const& service, BluetoothUUID const& } } -void PeripheralBase::unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) { +void PeripheralPlain::unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) { std::lock_guard lock(callback_mutex_); callbacks_.erase({service, characteristic}); } -ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const& characteristic, +ByteArray PeripheralPlain::read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) { return {}; } -void PeripheralBase::write(BluetoothUUID const& service, BluetoothUUID const& characteristic, +void PeripheralPlain::write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) {} -void PeripheralBase::set_callback_on_connected(std::function on_connected) { +void PeripheralPlain::set_callback_on_connected(std::function on_connected) { if (on_connected) { callback_on_connected_.load(std::move(on_connected)); } else { @@ -147,7 +147,7 @@ void PeripheralBase::set_callback_on_connected(std::function on_connecte } } -void PeripheralBase::set_callback_on_disconnected(std::function on_disconnected) { +void PeripheralPlain::set_callback_on_disconnected(std::function on_disconnected) { if (on_disconnected) { callback_on_disconnected_.load(std::move(on_disconnected)); } else { diff --git a/simpleble/src/backends/plain/PeripheralBase.h b/simpleble/src/backends/plain/PeripheralPlain.h similarity index 94% rename from simpleble/src/backends/plain/PeripheralBase.h rename to simpleble/src/backends/plain/PeripheralPlain.h index e19a1ad8..f4d85e22 100644 --- a/simpleble/src/backends/plain/PeripheralBase.h +++ b/simpleble/src/backends/plain/PeripheralPlain.h @@ -7,6 +7,8 @@ #include #include +#include + #include #include #include @@ -15,10 +17,10 @@ namespace SimpleBLE { -class PeripheralBase { +class PeripheralPlain : public PeripheralBase { public: - PeripheralBase(); - virtual ~PeripheralBase(); + PeripheralPlain(); + virtual ~PeripheralPlain(); void* underlying() const; diff --git a/simpleble/src/frontends/base/Adapter.cpp b/simpleble/src/frontends/base/Adapter.cpp index 4d2b6202..a11527db 100644 --- a/simpleble/src/frontends/base/Adapter.cpp +++ b/simpleble/src/frontends/base/Adapter.cpp @@ -4,6 +4,10 @@ #include "backends/linux/AdapterLinux.h" #endif +#ifdef SIMPLEBLE_BACKEND_PLAIN_ENABLED +#include "backends/plain/AdapterPlain.h" +#endif + #include "AdapterBuilder.h" #include "LoggingInternal.h" @@ -17,6 +21,12 @@ std::vector Adapter::get_adapters() { AdapterBuilder adapter(internal_adapter); available_adapters.push_back(adapter); } +#endif +#ifdef SIMPLEBLE_BACKEND_PLAIN_ENABLED + for (auto& internal_adapter : AdapterPlain::get_adapters()) { + AdapterBuilder adapter(internal_adapter); + available_adapters.push_back(adapter); + } #endif return available_adapters; }