Initial commit

This commit is contained in:
Robert Goodall
2026-07-29 00:39:00 -04:00
commit 346229dd64
30 changed files with 15309 additions and 0 deletions
@@ -0,0 +1,43 @@
#ifndef SIMPLEBLE_EXPORT_H
#define SIMPLEBLE_EXPORT_H
#ifdef SIMPLEBLE_STATIC_DEFINE
# define SIMPLEBLE_EXPORT
# define SIMPLEBLE_NO_EXPORT
#else
# ifndef SIMPLEBLE_EXPORT
# ifdef simpleble_EXPORTS
/* We are building this library */
# define SIMPLEBLE_EXPORT
# else
/* We are using this library */
# define SIMPLEBLE_EXPORT
# endif
# endif
# ifndef SIMPLEBLE_NO_EXPORT
# define SIMPLEBLE_NO_EXPORT
# endif
#endif
#ifndef SIMPLEBLE_DEPRECATED
# define SIMPLEBLE_DEPRECATED __attribute__ ((__deprecated__))
#endif
#ifndef SIMPLEBLE_DEPRECATED_EXPORT
# define SIMPLEBLE_DEPRECATED_EXPORT SIMPLEBLE_EXPORT SIMPLEBLE_DEPRECATED
#endif
#ifndef SIMPLEBLE_DEPRECATED_NO_EXPORT
# define SIMPLEBLE_DEPRECATED_NO_EXPORT SIMPLEBLE_NO_EXPORT SIMPLEBLE_DEPRECATED
#endif
/* NOLINTNEXTLINE(readability-avoid-unconditional-preprocessor-if) */
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef SIMPLEBLE_NO_DEPRECATED
# define SIMPLEBLE_NO_DEPRECATED
# endif
#endif
#endif /* SIMPLEBLE_EXPORT_H */
Binary file not shown.
@@ -0,0 +1,105 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <simpleble/export.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Peripheral.h>
#include <simpleble/Types.h>
namespace SimpleBLE {
class AdapterBase;
/**
* Bluetooth Adapter.
*
* A default-constructed Adapter object is not initialized. Calling any method
* other than `initialized()` on an uninitialized Adapter will throw an exception
* of type `SimpleBLE::NotInitialized`.
*
* NOTE: This class is intended to be used by the user only. Library developers
* should use shared pointers to `AdapterBase` instead.
*/
class SIMPLEBLE_EXPORT Adapter {
public:
Adapter() = default;
virtual ~Adapter() = default;
bool initialized() const;
/**
* Retrieve the underlying OS object/handle.
*
* For certain compatibility with external libraries, we sometimes need to
* expose the actual OS handle to the user. This is particularly important
* for MacOS right now.
*/
void* underlying() const;
std::string identifier();
BluetoothAddress address();
/**
* Control the power state of the adapter.
*
* NOTE: Power control support depends on the backend and platform.
* Unsupported backends may do nothing.
* NOTE: Power callbacks are supported by backends that provide power state change events.
*/
void power_on();
void power_off();
bool is_powered();
void set_callback_on_power_on(std::function<void()> on_power_on);
void set_callback_on_power_off(std::function<void()> on_power_off);
void scan_start();
void scan_stop();
void scan_for(int timeout_ms);
bool scan_is_active();
std::vector<Peripheral> scan_get_results();
void set_callback_on_scan_start(std::function<void()> on_scan_start);
void set_callback_on_scan_stop(std::function<void()> on_scan_stop);
void set_callback_on_scan_updated(std::function<void(Peripheral)> on_scan_updated);
void set_callback_on_scan_found(std::function<void(Peripheral)> on_scan_found);
/**
* Retrieve a list of all paired peripherals.
*
* NOTE:This method is currently only supported by the Linux, Windows and Android backends.
*/
std::vector<Peripheral> get_paired_peripherals();
/**
* Retrieve a list of all connected peripherals.
*
* NOTE: This method is currently only supported by the Windows backend. (More backends coming soon.)
*/
std::vector<Peripheral> get_connected_peripherals();
static bool bluetooth_enabled();
/**
* Fetches a list of all available adapters from all available backends.
*
* This will cause backends to be instantiated/initialized and adapters
* too.
*
* @note All configuration values must be set prior to calling this function.
* Please refer to the SimpleBLE::Config documentation for more details.
*/
static std::vector<Adapter> get_adapters();
protected:
AdapterBase* operator->();
const AdapterBase* operator->() const;
std::shared_ptr<AdapterBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,58 @@
#pragma once
#include <simpleble/export.h>
#include <simpleble/Adapter.h>
#include <simpleble/PeripheralSafe.h>
#include <memory>
#include <optional>
#include <vector>
namespace SimpleBLE {
namespace Safe {
/**
* Wrapper around the Adapter class that provides a noexcept interface.
*
* We use instances of this class directly and not through shared_ptr because
* this is just a wrapper around the Adapter class, which is already managed by
* shared_ptr.
*/
class SIMPLEBLE_EXPORT Adapter {
public:
Adapter(SimpleBLE::Adapter& adapter);
Adapter(SimpleBLE::Adapter&& adapter);
virtual ~Adapter() = default;
std::optional<std::string> identifier() noexcept;
std::optional<BluetoothAddress> address() noexcept;
bool scan_start() noexcept;
bool scan_stop() noexcept;
bool scan_for(int timeout_ms) noexcept;
std::optional<bool> scan_is_active() noexcept;
std::optional<std::vector<SimpleBLE::Safe::Peripheral>> scan_get_results() noexcept;
bool set_callback_on_scan_start(std::function<void()> on_scan_start) noexcept;
bool set_callback_on_scan_stop(std::function<void()> on_scan_stop) noexcept;
bool set_callback_on_scan_updated(std::function<void(SimpleBLE::Safe::Peripheral)> on_scan_updated) noexcept;
bool set_callback_on_scan_found(std::function<void(SimpleBLE::Safe::Peripheral)> on_scan_found) noexcept;
std::optional<std::vector<SimpleBLE::Safe::Peripheral>> get_paired_peripherals() noexcept;
static std::optional<bool> bluetooth_enabled() noexcept;
static std::optional<std::vector<SimpleBLE::Safe::Adapter>> get_adapters() noexcept;
/**
* Cast to the underlying adapter object.
*/
operator SimpleBLE::Adapter() const noexcept;
protected:
SimpleBLE::Adapter internal_;
};
} // namespace Safe
} // namespace SimpleBLE
@@ -0,0 +1,50 @@
#pragma once
#include <simpleble/export.h>
#if __APPLE__
#include "TargetConditionals.h"
#endif
/**
* Advanced Features
*
* The functions presented in this namespace are OS-specific backdoors that are
* not part of the standard SimpleBLE API, which allow the user to access
* low-level details of the implementation for advanced use cases.
*
* These functions should be used with caution.
*/
#if defined(_WIN32)
namespace SimpleBLE::Advanced::Windows {}
#endif
#if TARGET_OS_OSX
namespace SimpleBLE::Advanced::MacOS {}
#endif
#if TARGET_OS_IOS
namespace SimpleBLE::Advanced::iOS {}
#endif
#if defined(__ANDROID__)
#include <jni.h>
namespace SimpleBLE::Advanced::Android {
JavaVM* SIMPLEBLE_EXPORT get_jvm();
void SIMPLEBLE_EXPORT set_jvm(JavaVM* jvm);
} // namespace SimpleBLE::Advanced::Android
#endif
#if defined(__linux__) && !defined(__ANDROID__)
namespace SimpleBLE::Advanced::Linux {}
#endif
@@ -0,0 +1,51 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <simpleble/Adapter.h>
#include <simpleble/export.h>
namespace SimpleBLE {
class BackendBase;
class SIMPLEBLE_EXPORT Backend {
friend class Adapter;
public:
Backend() = default;
virtual ~Backend() = default;
bool initialized() const;
/**
* Get a list of all available adapters from this backend.
*/
std::vector<Adapter> adapters();
/**
* Check if Bluetooth is enabled for this backend.
*/
bool bluetooth_enabled();
/**
* Get the identifier of the backend.
*/
std::string identifier() const noexcept;
/**
* Get all available backends.
*
* This will cause backends to be instantiated/initialized.
*/
static std::vector<Backend> get_backends();
protected:
BackendBase* operator->();
const BackendBase* operator->() const;
std::shared_ptr<BackendBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <simpleble/export.h>
#include <simpleble/Descriptor.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Types.h>
namespace SimpleBLE {
class CharacteristicBase;
class SIMPLEBLE_EXPORT Characteristic {
public:
Characteristic() = default;
virtual ~Characteristic() = default;
bool initialized() const;
BluetoothUUID uuid();
std::vector<Descriptor> descriptors();
std::vector<std::string> capabilities();
bool can_read();
bool can_write_request();
bool can_write_command();
bool can_notify();
bool can_indicate();
protected:
CharacteristicBase* operator->();
const CharacteristicBase* operator->() const;
std::shared_ptr<CharacteristicBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,78 @@
#pragma once
#include <chrono>
#include <simpleble/export.h>
//clang-format off
namespace SimpleBLE {
/**
* @namespace SimpleBLE::Config
* @brief Configuration options for SimpleBLE.
*
* @note All configuration values must be set prior to any other interaction with a SimpleBLE component.
* Changes made after an adapter has been retrieved may not take effect or could lead to undefined behavior.
*/
namespace Config {
namespace SimpleBluez {
extern SIMPLEBLE_EXPORT bool use_legacy_bluez_backend;
extern SIMPLEBLE_EXPORT bool use_system_bus; // NOTE: This is only available in the new Bluez backend.
extern SIMPLEBLE_EXPORT std::chrono::steady_clock::duration connection_timeout;
extern SIMPLEBLE_EXPORT std::chrono::steady_clock::duration disconnection_timeout;
static void reset() {
use_legacy_bluez_backend = false;
use_system_bus = true;
connection_timeout = std::chrono::seconds(2);
disconnection_timeout = std::chrono::seconds(1);
}
} // namespace SimpleBluez
namespace WinRT {
/**
* @deprecated SimpleBLE uses its own MTA apartment by default. This compatibility flag will be removed in a future release.
*/
extern SIMPLEBLE_EXPORT bool experimental_use_own_mta_apartment;
extern SIMPLEBLE_EXPORT bool experimental_reinitialize_winrt_apartment_on_main_thread;
extern SIMPLEBLE_EXPORT bool use_deferred_disconnect;
static void reset() {
experimental_use_own_mta_apartment = true;
experimental_reinitialize_winrt_apartment_on_main_thread = false;
use_deferred_disconnect = true;
}
} // namespace WinRT
namespace CoreBluetooth {
static void reset() {}
} // namespace CoreBluetooth
namespace Android {
enum class ConnectionPriorityRequest { DISABLED = -1, BALANCED = 0, HIGH = 1, LOW_POWER = 2, DCK = 3 };
extern SIMPLEBLE_EXPORT ConnectionPriorityRequest connection_priority_request;
static void reset() { connection_priority_request = ConnectionPriorityRequest::DISABLED; }
} // namespace Android
namespace Dongl {
extern SIMPLEBLE_EXPORT bool use_dongl_backend;
extern SIMPLEBLE_EXPORT bool auto_update;
extern SIMPLEBLE_EXPORT bool force_update;
static void reset() {
use_dongl_backend = false;
auto_update = false;
force_update = false;
}
} // namespace Dongl
namespace Base {
static void reset_all() {
SimpleBluez::reset();
WinRT::reset();
CoreBluetooth::reset();
Android::reset();
Dongl::reset();
}
} // namespace Base
} // namespace Config
} // namespace SimpleBLE
//clang-format on
@@ -0,0 +1,30 @@
#pragma once
#include <memory>
#include <simpleble/export.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Types.h>
namespace SimpleBLE {
class DescriptorBase;
class SIMPLEBLE_EXPORT Descriptor {
public:
Descriptor() = default;
virtual ~Descriptor() = default;
bool initialized() const;
BluetoothUUID uuid();
protected:
DescriptorBase* operator->();
const DescriptorBase* operator->() const;
std::shared_ptr<DescriptorBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,73 @@
#pragma once
#include <stdexcept>
#include <string>
#include <simpleble/export.h>
#include "Types.h"
namespace SimpleBLE {
namespace Exception {
class SIMPLEBLE_EXPORT BaseException : public std::runtime_error {
public:
BaseException(const std::string& __arg) : std::runtime_error(__arg) {}
};
class SIMPLEBLE_EXPORT NotInitialized : public BaseException {
public:
NotInitialized();
};
class SIMPLEBLE_EXPORT NotConnected : public BaseException {
public:
NotConnected();
};
class SIMPLEBLE_EXPORT InvalidReference : public BaseException {
public:
InvalidReference();
};
class SIMPLEBLE_EXPORT ServiceNotFound : public BaseException {
public:
ServiceNotFound(BluetoothUUID uuid);
};
class SIMPLEBLE_EXPORT CharacteristicNotFound : public BaseException {
public:
CharacteristicNotFound(BluetoothUUID uuid);
};
class SIMPLEBLE_EXPORT DescriptorNotFound : public BaseException {
public:
DescriptorNotFound(BluetoothUUID uuid);
};
class SIMPLEBLE_EXPORT OperationNotSupported : public BaseException {
public:
OperationNotSupported();
OperationNotSupported(const std::string& operation, const BluetoothUUID& characteristic_uuid);
};
class SIMPLEBLE_EXPORT OperationFailed : public BaseException {
public:
OperationFailed();
OperationFailed(const std::string& err_msg);
};
class SIMPLEBLE_EXPORT WinRTException : public BaseException {
public:
WinRTException(int32_t err_code, const std::string& err_msg);
};
class SIMPLEBLE_EXPORT CoreBluetoothException : public BaseException {
public:
CoreBluetoothException(const std::string& err_msg);
};
} // namespace Exception
} // namespace SimpleBLE
@@ -0,0 +1,73 @@
#pragma once
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <simpleble/export.h>
namespace SimpleBLE {
namespace Logging {
enum Level : int {
None = 0,
Fatal,
Error,
Warn,
Info,
Debug,
Verbose,
};
// clang-format off
using Callback = std::function<void(
Level,
const std::string& module,
const std::string& file,
uint32_t line,
const std::string& function,
const std::string& message)>;
// clang-format on
class SIMPLEBLE_EXPORT Logger {
public:
static Logger* get();
void set_level(Level level);
Level get_level();
void set_callback(Callback callback);
bool has_callback();
void log_default_stdout();
void log_default_file();
void log_default_file(const std::string path);
// clang-format off
void log(
Level level,
const std::string& module,
const std::string& file,
uint32_t line,
const std::string& function,
const std::string& message);
// clang-format on
private:
Logger();
~Logger();
Logger(Logger& other) = delete; // Remove copy constructor
void operator=(const Logger&) = delete; // Remove copy assignment
static std::string level_to_str(Level level);
Level level_{Level::Info};
Callback callback_{nullptr};
std::recursive_mutex mutex_;
};
} // namespace Logging
} // namespace SimpleBLE
@@ -0,0 +1,82 @@
#pragma once
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <simpleble/export.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Service.h>
#include <simpleble/Types.h>
namespace SimpleBLE {
class PeripheralBase;
class SIMPLEBLE_EXPORT Peripheral {
public:
Peripheral() = default;
virtual ~Peripheral() = default;
bool initialized() const;
void* underlying() const;
std::string identifier();
BluetoothAddress address();
BluetoothAddressType address_type();
int16_t rssi();
/**
* @brief Provides the advertised transmit power in dBm.
*
* @note If the field has not been advertised by the peripheral,
* the returned value will be -32768.
*/
int16_t tx_power();
uint16_t mtu();
void connect();
void disconnect();
bool is_connected();
bool is_connectable();
bool is_paired();
void unpair();
/**
* @brief Provides a list of all services that are available on the peripheral.
*
* @note If the peripheral is not connected, it will return a list of services
* that were advertised by the device.
*/
std::vector<Service> services();
std::map<uint16_t, ByteArray> manufacturer_data();
/* Calling any of the methods below when the device is not connected will throw
Exception::NotConnected */
// clang-format off
ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic);
void write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data);
void write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data);
void notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback);
void indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback);
void unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic);
ByteArray read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor);
void write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data);
// clang-format on
void set_callback_on_connected(std::function<void()> on_connected);
void set_callback_on_disconnected(std::function<void()> on_disconnected);
protected:
PeripheralBase* operator->();
const PeripheralBase* operator->() const;
std::shared_ptr<PeripheralBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,64 @@
#pragma once
#include <memory>
#include <optional>
#include <simpleble/export.h>
#include <simpleble/Peripheral.h>
#include <simpleble/Service.h>
namespace SimpleBLE {
namespace Safe {
class SIMPLEBLE_EXPORT Peripheral {
public:
Peripheral(SimpleBLE::Peripheral& peripheral);
Peripheral(SimpleBLE::Peripheral&& peripheral);
virtual ~Peripheral() = default;
std::optional<std::string> identifier() noexcept;
std::optional<BluetoothAddress> address() noexcept;
std::optional<BluetoothAddressType> address_type() noexcept;
std::optional<int16_t> rssi() noexcept;
std::optional<int16_t> tx_power() noexcept;
std::optional<uint16_t> mtu() noexcept;
bool connect() noexcept;
bool disconnect() noexcept;
std::optional<bool> is_connected() noexcept;
std::optional<bool> is_connectable() noexcept;
std::optional<bool> is_paired() noexcept;
bool unpair() noexcept;
std::optional<std::vector<Service>> services() noexcept;
std::optional<std::map<uint16_t, ByteArray>> manufacturer_data() noexcept;
// clang-format off
std::optional<ByteArray> read(BluetoothUUID const& service, BluetoothUUID const& characteristic) noexcept;
bool write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) noexcept;
bool write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic, ByteArray const& data) noexcept;
bool notify(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) noexcept;
bool indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic, std::function<void(ByteArray payload)> callback) noexcept;
bool unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) noexcept;
std::optional<ByteArray> read(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor) noexcept;
bool write(BluetoothUUID const& service, BluetoothUUID const& characteristic, BluetoothUUID const& descriptor, ByteArray const& data) noexcept;
// clang-format on
bool set_callback_on_connected(std::function<void()> on_connected) noexcept;
bool set_callback_on_disconnected(std::function<void()> on_disconnected) noexcept;
/**
* Get the underlying peripheral object.
*/
operator SimpleBLE::Peripheral() const noexcept;
protected:
SimpleBLE::Peripheral internal_;
};
} // namespace Safe
} // namespace SimpleBLE
@@ -0,0 +1,34 @@
#pragma once
#include <memory>
#include <vector>
#include <simpleble/export.h>
#include <simpleble/Exceptions.h>
#include <simpleble/Types.h>
#include "simpleble/Characteristic.h"
namespace SimpleBLE {
class ServiceBase;
class SIMPLEBLE_EXPORT Service {
public:
Service() = default;
virtual ~Service() = default;
bool initialized() const;
BluetoothUUID uuid();
ByteArray data();
std::vector<Characteristic> characteristics();
protected:
const ServiceBase* operator->() const;
ServiceBase* operator->();
std::shared_ptr<ServiceBase> internal_;
};
} // namespace SimpleBLE
@@ -0,0 +1,8 @@
#pragma once
#include <simpleble/Config.h>
#include <simpleble/Adapter.h>
#include <simpleble/AdapterSafe.h>
#include <simpleble/Peripheral.h>
#include <simpleble/PeripheralSafe.h>
#include <simpleble/Utils.h>
@@ -0,0 +1,49 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "kvn/kvn_bytearray.h"
/**
* @file Types.h
* @brief Defines types and enumerations used throughout the SimpleBLE library.
*/
namespace SimpleBLE {
using BluetoothAddress = std::string;
// IDEA: Extend BluetoothUUID to include a `uuid` function that
// returns the same string, but provides a homogeneous interface.
using BluetoothUUID = std::string;
/**
* @typedef ByteArray
* @brief Represents a byte array using kvn::bytearray from the external library.
*/
using ByteArray = kvn::bytearray;
#ifdef ANDROID
#pragma push_macro("ANDROID")
#undef ANDROID
#define ANDROID_WAS_DEFINED
#endif
enum class OperatingSystem {
WINDOWS,
MACOS,
IOS,
LINUX,
ANDROID,
};
#ifdef ANDROID_WAS_DEFINED
#pragma pop_macro("ANDROID")
#undef ANDROID_WAS_DEFINED
#endif
// TODO: Add to_string functions for all enums.
enum BluetoothAddressType : int32_t { PUBLIC = 0, RANDOM = 1, UNSPECIFIED = 2 };
} // namespace SimpleBLE
@@ -0,0 +1,13 @@
#pragma once
#include <simpleble/export.h>
#include <simpleble/Types.h>
namespace SimpleBLE {
OperatingSystem SIMPLEBLE_EXPORT get_operating_system();
std::string SIMPLEBLE_EXPORT get_simpleble_version();
} // namespace SimpleBLE