Skip to content

Commit

Permalink
Remove builders in favor of templated approach.
Browse files Browse the repository at this point in the history
This cuts down on code. Future changes will cut down even further as
the process of transforming backend "Base" objects into frontend objects
will be done only at the frontend.
  • Loading branch information
jcarrano committed Nov 26, 2024
1 parent 05cf264 commit 4a1d13b
Show file tree
Hide file tree
Showing 37 changed files with 292 additions and 333 deletions.
8 changes: 1 addition & 7 deletions simpleble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ set(SIMPLEBLE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/src/Logging.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/frontends/safe/AdapterSafe.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/frontends/safe/PeripheralSafe.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/builders/AdapterBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/builders/PeripheralBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/builders/ServiceBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/builders/CharacteristicBuilder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/builders/DescriptorBuilder.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/src/frontends/safe/PeripheralSafe.cpp)

set(SIMPLEBLE_C_SRC
${CMAKE_CURRENT_SOURCE_DIR}/src_c/simpleble.cpp
Expand Down
26 changes: 26 additions & 0 deletions simpleble/src/CommonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,29 @@
SIMPLEBLE_LOG_ERROR("Unknown exception within code block"); \
} \
} while (0)

namespace SimpleBLE::Util {

template <typename MAP>
struct ValueCollector {
public:
template <typename VEC>
operator VEC() {
VEC vec;
vec.reserve(map.size());
std::transform(map.begin(), map.end(), std::back_inserter(vec), [](const auto& pair) { return pair.second; });
return vec;
}

const MAP& map;
};

/**
* Collect the values of a mapping like object into a vector-like object.
*/
template <typename MAP>
auto values(const MAP& map) {
return ValueCollector<MAP>{map};
}

} // namespace SimpleBLE::util
42 changes: 17 additions & 25 deletions simpleble/src/backends/android/AdapterAndroid.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "AdapterAndroid.h"
#include "BuilderBase.h"
#include "CommonUtils.h"
#include "PeripheralAndroid.h"
#include "PeripheralBuilder.h"
#include "simpleble/Peripheral.h"

#include <jni.h>
#include <android/log.h>
#include <thread>
#include <fmt/core.h>
#include <android/BluetoothDevice.h>
#include <android/ScanResult.h>
#include <android/log.h>
#include <fmt/core.h>
#include <jni.h>
#include <thread>

using namespace SimpleBLE;

Expand All @@ -31,13 +32,13 @@ void AdapterBase::initialize() {
}

if (_btAdapter.get() == nullptr) {
_btAdapter = _btAdapterCls.call_static_method( "getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter;");
_btAdapter = _btAdapterCls.call_static_method("getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter;");
}

if (_btScanner.get() == nullptr) {
_btScanner = _btAdapter.call_object_method("getBluetoothLeScanner", "()Landroid/bluetooth/le/BluetoothLeScanner;");
_btScanner = _btAdapter.call_object_method("getBluetoothLeScanner",
"()Landroid/bluetooth/le/BluetoothLeScanner;");
}

}

std::vector<std::shared_ptr<AdapterBase>> AdapterBase::get_adapters() {
Expand All @@ -58,7 +59,7 @@ bool AdapterBase::bluetooth_enabled() {
int bluetoothState = _btAdapter.call_int_method("getState", "()I");
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", fmt::format("Bluetooth state: {}", bluetoothState).c_str());

return isEnabled; //bluetoothState == 12;
return isEnabled; // bluetoothState == 12;
}

AdapterBase::AdapterBase() {
Expand All @@ -76,29 +77,24 @@ AdapterBase::AdapterBase() {
base_peripheral->update_advertising_data(scan_result);

// Convert the base object into an external-facing Peripheral object
PeripheralBuilder peripheral_builder(base_peripheral);
Peripheral peripheral = Factory::build(base_peripheral);

// Check if the device has been seen before, to forward the correct call to the user.
if (this->seen_peripherals_.count(address) == 0) {
// Store it in our table of seen peripherals
this->seen_peripherals_.insert(std::make_pair(address, base_peripheral));
SAFE_CALLBACK_CALL(this->callback_on_scan_found_, peripheral_builder);
SAFE_CALLBACK_CALL(this->callback_on_scan_found_, peripheral);
} else {
SAFE_CALLBACK_CALL(this->callback_on_scan_updated_, peripheral_builder);
SAFE_CALLBACK_CALL(this->callback_on_scan_updated_, peripheral);
}
});

}

AdapterBase::~AdapterBase() {

}
AdapterBase::~AdapterBase() {}

void* AdapterBase::underlying() const { return nullptr; }

std::string AdapterBase::identifier() {
return _btAdapter.call_string_method("getName", "()Ljava/lang/String;");
}
std::string AdapterBase::identifier() { return _btAdapter.call_string_method("getName", "()Ljava/lang/String;"); }

BluetoothAddress AdapterBase::address() {
return BluetoothAddress(_btAdapter.call_string_method("getAddress", "()Ljava/lang/String;"));
Expand All @@ -125,13 +121,9 @@ void AdapterBase::scan_for(int timeout_ms) {

bool AdapterBase::scan_is_active() { return scanning_; }

std::vector<Peripheral> AdapterBase::scan_get_results() {
return std::vector<Peripheral>();
}
std::vector<Peripheral> AdapterBase::scan_get_results() { return std::vector<Peripheral>(); }

std::vector<Peripheral> AdapterBase::get_paired_peripherals() {
return std::vector<Peripheral>();
}
std::vector<Peripheral> AdapterBase::get_paired_peripherals() { return std::vector<Peripheral>(); }

void AdapterBase::set_callback_on_scan_start(std::function<void()> on_scan_start) {
if (on_scan_start) {
Expand Down
12 changes: 5 additions & 7 deletions simpleble/src/backends/android/AdapterAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <string>
#include <vector>

#include "jni/Common.hpp"
#include "bridge/ScanCallback.h"
#include "jni/Common.hpp"

namespace SimpleBLE {

Expand Down Expand Up @@ -47,11 +47,11 @@ class AdapterBase {
// NOTE: The following methods have been made public to allow the JNI layer to call them, but
// should not be called directly by the user.

void onScanResultCallback(JNIEnv *env, jobject thiz, jint callback_type, jobject result);
void onBatchScanResultsCallback(JNIEnv *env, jobject thiz, jobject results);
void onScanFailedCallback(JNIEnv *env, jobject thiz, jint error_code);
void onScanResultCallback(JNIEnv* env, jobject thiz, jint callback_type, jobject result);
void onBatchScanResultsCallback(JNIEnv* env, jobject thiz, jobject results);
void onScanFailedCallback(JNIEnv* env, jobject thiz, jint error_code);

//static std::map<jobject, AdapterBase*, JNI::JObjectComparator> _scanCallbackMap;
// static std::map<jobject, AdapterBase*, JNI::JObjectComparator> _scanCallbackMap;

private:
// NOTE: The correct way to request a BluetoothAdapter is to go though the BluetoothManager,
Expand Down Expand Up @@ -79,8 +79,6 @@ class AdapterBase {
kvn::safe_callback<void(Peripheral)> callback_on_scan_found_;

std::atomic<bool> scanning_{false};


};

} // namespace SimpleBLE
18 changes: 10 additions & 8 deletions simpleble/src/backends/android/PeripheralAndroid.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "PeripheralAndroid.h"

#include "CharacteristicBuilder.h"
#include "DescriptorBuilder.h"
#include "ServiceBuilder.h"
#include "BuilderBase.h"
#include "CharacteristicBase.h"
#include "DescriptorBase.h"
#include "ServiceBase.h"

#include <simpleble/Exceptions.h>
#include <algorithm>
#include "CommonUtils.h"
#include "LoggingInternal.h"
#include "simpleble/Descriptor.h"

using namespace SimpleBLE;
using namespace std::chrono_literals;
Expand Down Expand Up @@ -71,7 +73,7 @@ std::vector<Service> PeripheralBase::services() {
// Build the list of descriptors for the characteristic.
std::vector<Descriptor> descriptor_list;
for (auto descriptor : characteristic.getDescriptors()) {
descriptor_list.push_back(DescriptorBuilder(descriptor.getUuid()));
descriptor_list.push_back(Factory::Builder<Descriptor>(descriptor.getUuid()));
}

int flags = characteristic.getProperties();
Expand All @@ -82,12 +84,12 @@ std::vector<Service> PeripheralBase::services() {
bool can_notify = flags & Android::BluetoothGattCharacteristic::PROPERTY_NOTIFY;
bool can_indicate = flags & Android::BluetoothGattCharacteristic::PROPERTY_INDICATE;

characteristic_list.push_back(CharacteristicBuilder(characteristic.getUuid(), descriptor_list, can_read,
can_write_request, can_write_command, can_notify,
can_indicate));
characteristic_list.push_back(
Factory::Builder<Characteristic>(characteristic.getUuid(), descriptor_list, can_read, can_write_request,
can_write_command, can_notify, can_indicate));
}

service_list.push_back(ServiceBuilder(service.getUuid(), characteristic_list));
service_list.push_back(Factory::Builder<Service>(service.getUuid(), characteristic_list));
}

return service_list;
Expand Down
2 changes: 0 additions & 2 deletions simpleble/src/backends/android/PeripheralAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class PeripheralBase {
void update_advertising_data(Android::ScanResult scan_result);

private:

Android::Bridge::BluetoothGattCallback _btGattCallback;
Android::BluetoothDevice _device;
Android::BluetoothGatt _gatt;
Expand All @@ -68,7 +67,6 @@ class PeripheralBase {
Android::BluetoothGattDescriptor _fetch_descriptor(const BluetoothUUID& service_uuid,
const BluetoothUUID& characteristic_uuid,
const BluetoothUUID& descriptor_uuid);

};

} // namespace SimpleBLE
3 changes: 1 addition & 2 deletions simpleble/src/backends/common/CharacteristicBase.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <simpleble/Characteristic.h>

#include "CharacteristicBase.h"
#include "CharacteristicBuilder.h"

using namespace SimpleBLE;

Expand All @@ -24,4 +23,4 @@ bool CharacteristicBase::can_read() { return can_read_; }
bool CharacteristicBase::can_write_request() { return can_write_request_; }
bool CharacteristicBase::can_write_command() { return can_write_command_; }
bool CharacteristicBase::can_notify() { return can_notify_; }
bool CharacteristicBase::can_indicate() { return can_indicate_; }
bool CharacteristicBase::can_indicate() { return can_indicate_; }
3 changes: 1 addition & 2 deletions simpleble/src/backends/common/DescriptorBase.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <simpleble/Descriptor.h>

#include "DescriptorBase.h"
#include "DescriptorBuilder.h"

using namespace SimpleBLE;

DescriptorBase::DescriptorBase(const BluetoothUUID& uuid) : uuid_(uuid) {}

BluetoothUUID DescriptorBase::uuid() { return uuid_; }
BluetoothUUID DescriptorBase::uuid() { return uuid_; }
1 change: 0 additions & 1 deletion simpleble/src/backends/common/ServiceBase.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <simpleble/Service.h>

#include "ServiceBase.h"
#include "ServiceBuilder.h"

using namespace SimpleBLE;

Expand Down
20 changes: 8 additions & 12 deletions simpleble/src/backends/linux/AdapterLinux.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "AdapterLinux.h"
#include "BackendBluez.h"
#include "BuildVec.h"
#include "BuilderBase.h"
#include "CommonUtils.h"
#include "PeripheralLinux.h"
#include "PeripheralBuilder.h"

using namespace SimpleBLE;

Expand Down Expand Up @@ -57,15 +58,15 @@ void AdapterBase::scan_start() {
auto base_peripheral = this->peripherals_.at(device->address());

// Convert the base object into an external-facing Peripheral object
PeripheralBuilder peripheral_builder(base_peripheral);
Peripheral peripheral = Factory::build(base_peripheral);

// Check if the device has been seen before, to forward the correct call to the user.
if (this->seen_peripherals_.count(device->address()) == 0) {
// Store it in our table of seen peripherals
this->seen_peripherals_.insert(std::make_pair(device->address(), base_peripheral));
SAFE_CALLBACK_CALL(this->callback_on_scan_found_, peripheral_builder);
SAFE_CALLBACK_CALL(this->callback_on_scan_found_, peripheral);
} else {
SAFE_CALLBACK_CALL(this->callback_on_scan_updated_, peripheral_builder);
SAFE_CALLBACK_CALL(this->callback_on_scan_updated_, peripheral);
}
});

Expand Down Expand Up @@ -97,21 +98,16 @@ void AdapterBase::scan_for(int timeout_ms) {
bool AdapterBase::scan_is_active() { return is_scanning_ && adapter_->discovering(); }

std::vector<Peripheral> AdapterBase::scan_get_results() {
std::vector<Peripheral> peripherals;
for (auto& [address, peripheral] : this->seen_peripherals_) {
PeripheralBuilder peripheral_builder(peripheral);
peripherals.push_back(peripheral_builder);
}
return peripherals;
std::vector<std::shared_ptr<PeripheralBase>> peripherals = Util::values(seen_peripherals_);
return Factory::vector(peripherals);
}

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

auto paired_list = adapter_->device_paired_get();
for (auto& device : paired_list) {
PeripheralBuilder peripheral_builder(std::make_shared<PeripheralBase>(device, this->adapter_));
peripherals.push_back(peripheral_builder);
peripherals.push_back(Factory::Builder<Peripheral>(device, this->adapter_));
}

return peripherals;
Expand Down
32 changes: 19 additions & 13 deletions simpleble/src/backends/linux/PeripheralLinux.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "PeripheralLinux.h"

#include "CharacteristicBuilder.h"
#include "DescriptorBuilder.h"
#include "ServiceBuilder.h"

#include "BuildVec.h"
#include "BuilderBase.h"
#include "CharacteristicBase.h"
#include "DescriptorBase.h"
#include "ServiceBase.h"

#include <simpleble/Characteristic.h>
#include <simpleble/Descriptor.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Service.h>
#include <simplebluez/Exceptions.h>
#include <algorithm>
#include "CommonUtils.h"
Expand Down Expand Up @@ -139,7 +144,7 @@ std::vector<Service> PeripheralBase::services() {
// Build the list of descriptors for the characteristic.
std::vector<Descriptor> descriptor_list;
for (auto bluez_descriptor : bluez_characteristic->descriptors()) {
descriptor_list.push_back(DescriptorBuilder(bluez_descriptor->uuid()));
descriptor_list.push_back(Factory::Builder<Descriptor>(bluez_descriptor->uuid()));
}

std::vector<std::string> flags = bluez_characteristic->flags();
Expand All @@ -150,20 +155,21 @@ std::vector<Service> PeripheralBase::services() {
bool can_notify = std::find(flags.begin(), flags.end(), "notify") != flags.end();
bool can_indicate = std::find(flags.begin(), flags.end(), "indicate") != flags.end();

characteristic_list.push_back(CharacteristicBuilder(bluez_characteristic->uuid(), descriptor_list, can_read,
can_write_request, can_write_command, can_notify,
can_indicate));
characteristic_list.push_back(
Factory::Builder<Characteristic>(bluez_characteristic->uuid(), descriptor_list, can_read,
can_write_request, can_write_command, can_notify, can_indicate));
}

service_list.push_back(ServiceBuilder(bluez_service->uuid(), characteristic_list));
service_list.push_back(Factory::Builder<Service>(bluez_service->uuid(), characteristic_list));
}

// If the battery service is not available, and the device has the appropriate interface, add it.
if (!is_battery_service_available && device_->has_battery_interface()) {
// Emulate the battery service through the Battery1 interface.
service_list.push_back(
ServiceBuilder(BATTERY_SERVICE_UUID,
{CharacteristicBuilder(BATTERY_CHARACTERISTIC_UUID, {}, true, false, false, true, false)}));
std::vector<Descriptor> descriptor_list;
std::vector<Characteristic> battery_characteristics = {Factory::Builder<Characteristic>(
BATTERY_CHARACTERISTIC_UUID, descriptor_list, true, false, false, true, false)};
service_list.push_back(Factory::Builder<Service>(BATTERY_SERVICE_UUID, battery_characteristics));
}

return service_list;
Expand All @@ -172,7 +178,7 @@ std::vector<Service> PeripheralBase::services() {
std::vector<Service> PeripheralBase::advertised_services() {
std::vector<Service> service_list;
for (auto& service_uuid : device_->uuids()) {
service_list.push_back(ServiceBuilder(service_uuid));
service_list.push_back(Factory::Builder<Service>(service_uuid));
}

return service_list;
Expand Down
Loading

0 comments on commit 4a1d13b

Please sign in to comment.