Skip to content

Commit

Permalink
Ported Plain to multibackend arch
Browse files Browse the repository at this point in the history
  • Loading branch information
kdewald committed Oct 21, 2024
1 parent ae58631 commit 062be41
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 143 deletions.
17 changes: 9 additions & 8 deletions simpleble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
98 changes: 0 additions & 98 deletions simpleble/src/backends/plain/AdapterBase.cpp

This file was deleted.

98 changes: 98 additions & 0 deletions simpleble/src/backends/plain/AdapterPlain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "AdapterPlain.h"
#include "CommonUtils.h"
#include "PeripheralPlain.h"
#include "PeripheralBuilder.h"

#include <thread>

using namespace SimpleBLE;

std::vector<std::shared_ptr<AdapterBase>> AdapterPlain::get_adapters() {
std::vector<std::shared_ptr<AdapterBase>> adapter_list;
adapter_list.push_back(std::static_pointer_cast<AdapterBase>(std::make_shared<AdapterPlain>()));
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<PeripheralPlain>());
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<Peripheral> AdapterPlain::scan_get_results() {
std::vector<Peripheral> peripherals;

PeripheralBuilder peripheral_builder(std::static_pointer_cast<PeripheralBase>(std::make_shared<PeripheralPlain>()));
peripherals.push_back(peripheral_builder);

return peripherals;
}

std::vector<Peripheral> AdapterPlain::get_paired_peripherals() {
std::vector<Peripheral> peripherals;

PeripheralBuilder peripheral_builder(std::static_pointer_cast<PeripheralBase>(std::make_shared<PeripheralPlain>()));
peripherals.push_back(peripheral_builder);

return peripherals;
}

void AdapterPlain::set_callback_on_scan_start(std::function<void()> 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<void()> 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<void(Peripheral)> 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<void(Peripheral)> on_scan_found) {
if (on_scan_found) {
callback_on_scan_found_.load(std::move(on_scan_found));
} else {
callback_on_scan_found_.unload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <kvn_safe_callback.hpp>

#include <backends/AdapterBase.h>
#include <atomic>
#include <functional>
#include <memory>
Expand All @@ -15,10 +16,10 @@

namespace SimpleBLE {

class AdapterBase {
class AdapterPlain : public AdapterBase {
public:
AdapterBase();
virtual ~AdapterBase();
AdapterPlain();
virtual ~AdapterPlain();

void* underlying() const;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "PeripheralBase.h"
#include "PeripheralPlain.h"

#include "CharacteristicBuilder.h"
#include "DescriptorBuilder.h"
Expand All @@ -15,49 +15,49 @@ 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 {
return 0;
}
}

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<Service> PeripheralBase::services() {
std::vector<Service> PeripheralPlain::services() {
if (!connected_) return {};

std::vector<Service> service_list;
Expand All @@ -68,19 +68,19 @@ std::vector<Service> PeripheralBase::services() {
return service_list;
}

std::vector<Service> PeripheralBase::advertised_services() { return {}; }
std::vector<Service> PeripheralPlain::advertised_services() { return {}; }

std::map<uint16_t, ByteArray> PeripheralBase::manufacturer_data() { return {{0x004C, "test"}}; }
std::map<uint16_t, ByteArray> 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<void(ByteArray payload)> callback) {
if (callback) {
callback_mutex_.lock();
Expand All @@ -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<void(ByteArray payload)> callback) {
if (callback) {
callback_mutex_.lock();
Expand All @@ -126,28 +126,28 @@ 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<std::mutex> 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<void()> on_connected) {
void PeripheralPlain::set_callback_on_connected(std::function<void()> on_connected) {
if (on_connected) {
callback_on_connected_.load(std::move(on_connected));
} else {
callback_on_connected_.unload();
}
}

void PeripheralBase::set_callback_on_disconnected(std::function<void()> on_disconnected) {
void PeripheralPlain::set_callback_on_disconnected(std::function<void()> on_disconnected) {
if (on_disconnected) {
callback_on_disconnected_.load(std::move(on_disconnected));
} else {
Expand Down
Loading

0 comments on commit 062be41

Please sign in to comment.