Skip to content

Commit

Permalink
Backends tidyup (#365)
Browse files Browse the repository at this point in the history
* Rename backend-specific files.

Instead of having each backend's files named AdapterBase/PeripheralBase, rename
them with unique names. This is to prepare the codebase for having multiple enabled
backends.

* Remove builders in favor of templated approach.

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 authored Nov 28, 2024
1 parent a4294ed commit 0ec3925
Show file tree
Hide file tree
Showing 50 changed files with 390 additions and 374 deletions.
82 changes: 54 additions & 28 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 Expand Up @@ -149,18 +143,51 @@ endif()
list(APPEND PRIVATE_COMPILE_DEFINITIONS SIMPLEBLE_LOG_LEVEL=SIMPLEBLE_LOG_LEVEL_${SIMPLEBLE_LOG_LEVEL})
list(APPEND PRIVATE_COMPILE_DEFINITIONS SIMPLEBLE_VERSION="${PROJECT_VERSION}")

if(NOT SIMPLEBLE_TEST)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(SIMPLEBLE_BACKEND_LINUX ON)
else()
set(SIMPLEBLE_BACKEND_LINUX OFF)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(SIMPLEBLE_BACKEND_WINDOWS ON)
else()
set(SIMPLEBLE_BACKEND_WINDOWS OFF)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
set(SIMPLEBLE_BACKEND_MACOS ON)
else()
set(SIMPLEBLE_BACKEND_MACOS OFF)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Android")
set(SIMPLEBLE_BACKEND_ANDROID ON)
else()
set(SIMPLEBLE_BACKEND_ANDROID OFF)
endif()
else()
set(SIMPLEBLE_BACKEND_LINUX OFF)
set(SIMPLEBLE_BACKEND_WINDOWS OFF)
set(SIMPLEBLE_BACKEND_MACOS OFF)
set(SIMPLEBLE_BACKEND_ANDROID OFF)
endif()

add_compile_definitions(SIMPLEBLE_BACKEND_PLAIN=$<BOOL:${SIMPLEBLE_PLAIN}>
SIMPLEBLE_BACKEND_LINUX=$<BOOL:${SIMPLEBLE_BACKEND_LINUX}>
SIMPLEBLE_BACKEND_WINDOWS=$<BOOL:${SIMPLEBLE_BACKEND_WINDOWS}>
SIMPLEBLE_BACKEND_MACOS=$<BOOL:${SIMPLEBLE_BACKEND_MACOS}>
SIMPLEBLE_BACKEND_ANDROID=$<BOOL:${SIMPLEBLE_BACKEND_ANDROID}>)

# Detect the operating system and load the necessary dependencies
if(SIMPLEBLE_PLAIN)
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)

target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/AdapterPlain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/PeripheralPlain.cpp)
elseif(SIMPLEBLE_BACKEND_LINUX)
message(STATUS "Linux Host Detected")

find_package(DBus1 REQUIRED)
Expand All @@ -181,9 +208,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif()

target_sources(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/AdapterBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/PeripheralBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/Bluez.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/AdapterLinux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/PeripheralLinux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/BackendBluez.cpp)

target_sources(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../simplebluez/src/ProxyOrg.cpp
Expand Down Expand Up @@ -224,13 +251,12 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")

target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../simplebluez/include
${CMAKE_CURRENT_SOURCE_DIR}/../simpledbus/include
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux)
${CMAKE_CURRENT_SOURCE_DIR}/../simpledbus/include)

set_property(TARGET simpleble PROPERTY INSTALL_RPATH $ORIGIN)
set_property(TARGET simpleble-c PROPERTY INSTALL_RPATH $ORIGIN)

elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
elseif(SIMPLEBLE_BACKEND_WINDOWS)
message(STATUS "Windows Host Detected")

set(WINVERSION_CODE 0x0A00) # Selected Windows 10 based on https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt
Expand Down Expand Up @@ -259,11 +285,11 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows)
target_sources(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/AdapterBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/PeripheralBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/AdapterWindows.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/PeripheralWindows.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/Utils.cpp)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
elseif(SIMPLEBLE_BACKEND_MACOS)
message(STATUS "Darwin / iOS Host Detected")

list(APPEND PRIVATE_COMPILE_OPTIONS -fobjc-arc)
Expand All @@ -273,15 +299,15 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos)
target_sources(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/Utils.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/AdapterBase.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/AdapterMac.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/AdapterBaseMacOS.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/PeripheralBase.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/PeripheralMac.mm
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/PeripheralBaseMacOS.mm)

set_property(TARGET simpleble PROPERTY INSTALL_RPATH @loader_path)
set_property(TARGET simpleble-c PROPERTY INSTALL_RPATH @loader_path)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
elseif(SIMPLEBLE_BACKEND_ANDROID)
message(STATUS "Configuring for Android")

# Set the include directories for the Android NDK headers and other necessary directories
Expand All @@ -296,8 +322,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
target_include_directories(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android)
target_sources(simpleble PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/AdapterBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/PeripheralBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/AdapterAndroid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/PeripheralAndroid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/android/BluetoothDevice.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/android/BluetoothGatt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/android/BluetoothGattService.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
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "AdapterBase.h"
#include "AdapterAndroid.h"
#include "BuilderBase.h"
#include "CommonUtils.h"
#include "PeripheralBase.h"
#include "PeripheralBuilder.h"
#include "PeripheralAndroid.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 Expand Up @@ -164,4 +156,3 @@ void AdapterBase::set_callback_on_scan_found(std::function<void(Peripheral)> on_
callback_on_scan_found_.unload();
}
}

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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "PeripheralBase.h"
#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 Expand Up @@ -299,4 +301,4 @@ Android::BluetoothGattDescriptor PeripheralBase::_fetch_descriptor(const Bluetoo
}
}
throw SimpleBLE::Exception::ServiceNotFound(service_uuid);
}
}
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
Loading

0 comments on commit 0ec3925

Please sign in to comment.