HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BaseBluetooth.h
Go to the documentation of this file.
1
21#pragma once
22
23#include "HardwareTypes.h"
24#include <cctype>
25#include <cstdint>
26#include <functional>
27#include <string>
28#include <string_view>
29#include <vector>
30
51#define HF_BLUETOOTH_ERR_LIST(X) \
52 /* Success codes */ \
53 X(BLUETOOTH_SUCCESS, 0, "Success") \
54 \
55 /* General errors */ \
56 X(BLUETOOTH_ERR_FAILURE, 1, "General failure") \
57 X(BLUETOOTH_ERR_INVALID_PARAM, 2, "Invalid parameter") \
58 X(BLUETOOTH_ERR_NOT_INITIALIZED, 3, "Bluetooth not initialized") \
59 X(BLUETOOTH_ERR_ALREADY_INITIALIZED, 4, "Bluetooth already initialized") \
60 X(BLUETOOTH_ERR_NOT_ENABLED, 5, "Bluetooth not enabled") \
61 X(BLUETOOTH_ERR_ALREADY_ENABLED, 6, "Bluetooth already enabled") \
62 X(BLUETOOTH_ERR_NOT_CONNECTED, 7, "Bluetooth not connected") \
63 X(BLUETOOTH_ERR_ALREADY_CONNECTED, 8, "Bluetooth already connected") \
64 X(BLUETOOTH_ERR_CONNECTION_FAILED, 9, "Connection failed") \
65 X(BLUETOOTH_ERR_DISCONNECTION_FAILED, 10, "Disconnection failed") \
66 X(BLUETOOTH_ERR_SCAN_FAILED, 11, "Device scan failed") \
67 X(BLUETOOTH_ERR_PAIR_FAILED, 12, "Pairing failed") \
68 X(BLUETOOTH_ERR_UNPAIR_FAILED, 13, "Unpairing failed") \
69 X(BLUETOOTH_ERR_TIMEOUT, 14, "Operation timeout") \
70 X(BLUETOOTH_ERR_NO_MEMORY, 15, "Insufficient memory") \
71 X(BLUETOOTH_ERR_INVALID_ADDRESS, 16, "Invalid Bluetooth address") \
72 X(BLUETOOTH_ERR_DEVICE_NOT_FOUND, 17, "Device not found") \
73 X(BLUETOOTH_ERR_SERVICE_NOT_FOUND, 18, "Service not found") \
74 X(BLUETOOTH_ERR_CHARACTERISTIC_NOT_FOUND, 19, "Characteristic not found") \
75 X(BLUETOOTH_ERR_AUTHENTICATION_FAILED, 20, "Authentication failed") \
76 X(BLUETOOTH_ERR_AUTHORIZATION_FAILED, 21, "Authorization failed") \
77 X(BLUETOOTH_ERR_ENCRYPTION_FAILED, 22, "Encryption failed") \
78 X(BLUETOOTH_ERR_OPERATION_NOT_SUPPORTED, 23, "Operation not supported") \
79 X(BLUETOOTH_ERR_GATT_ERROR, 24, "GATT operation error") \
80 X(BLUETOOTH_ERR_INVALID_STATE, 25, "Invalid state for operation") \
81 X(BLUETOOTH_ERR_PERMISSION_DENIED, 26, "Permission denied") \
82 X(BLUETOOTH_ERR_BUSY, 27, "Device or operation busy") \
83 X(BLUETOOTH_ERR_SECURITY_ERROR, 28, "Security error") \
84 X(BLUETOOTH_ERR_INIT_FAILED, 29, "Initialization failed") \
85 X(BLUETOOTH_ERR_NOT_SUPPORTED, 30, "Operation not supported") \
86 X(BLUETOOTH_ERR_ALREADY_STARTED, 31, "Operation already started") \
87 X(BLUETOOTH_ERR_CONFIG_FAILED, 32, "Configuration failed") \
88 X(BLUETOOTH_ERR_START_FAILED, 33, "Start operation failed") \
89 X(BLUETOOTH_ERR_STOP_FAILED, 34, "Stop operation failed") \
90 X(BLUETOOTH_ERR_CONNECTION_NOT_FOUND, 35, "Connection not found") \
91 X(BLUETOOTH_ERR_MAX_CONNECTIONS_REACHED, 36, "Maximum connections reached") \
92 X(BLUETOOTH_ERR_SEND_FAILED, 37, "Send operation failed") \
93 X(BLUETOOTH_ERR_GATT_SERVICE_ADD_FAILED, 38, "GATT service add failed") \
94 X(BLUETOOTH_ERR_GATT_CHAR_ADD_FAILED, 39, "GATT characteristic add failed") \
95 X(BLUETOOTH_ERR_GATT_SERVICE_NOT_FOUND, 40, "GATT service not found") \
96 X(BLUETOOTH_ERR_GATT_SERVICE_START_FAILED, 41, "GATT service start failed") \
97 X(BLUETOOTH_ERR_UNKNOWN, 42, "Unknown error")
98
103#define X(name, value, desc) name = value,
105#undef X
106
113constexpr std::string_view HfBluetoothErrToString(hf_bluetooth_err_t err) noexcept {
114 switch (err) {
115#define X(NAME, VALUE, DESC) \
116 case hf_bluetooth_err_t::NAME: \
117 return DESC;
119#undef X
120 default:
122 }
123}
124
135
146
161
174
183
193
203
215
240
252 std::string ToString() const;
253
259 bool FromString(const std::string& addr_str);
260
265 bool IsValid() const;
266};
267
282
294
310
316 std::string uuid;
318 uint16_t handle;
319};
320
326 std::string uuid;
327 uint16_t handle;
328 uint8_t properties;
329 std::vector<uint8_t> value;
330};
331
337 std::function<void(hf_bluetooth_event_t event, void* event_data)>;
338
344 std::function<void(const hf_bluetooth_address_t& address, const std::vector<uint8_t>& data)>;
345
360public:
364 virtual ~BaseBluetooth() noexcept = default;
365
366 // ========== Initialization and Configuration ==========
367
374
379 virtual hf_bluetooth_err_t Deinitialize() noexcept = 0;
380
385 virtual bool IsInitialized() const noexcept = 0;
386
391 virtual hf_bluetooth_err_t Enable() noexcept = 0;
392
397 virtual hf_bluetooth_err_t Disable() noexcept = 0;
398
403 virtual bool IsEnabled() const noexcept = 0;
404
411
416 virtual hf_bluetooth_mode_t GetMode() const noexcept = 0;
417
418 // ========== Device Management ==========
419
425 virtual hf_bluetooth_err_t GetLocalAddress(hf_bluetooth_address_t& address) const noexcept = 0;
426
432 virtual hf_bluetooth_err_t SetDeviceName(const std::string& name) noexcept = 0;
433
438 virtual std::string GetDeviceName() const noexcept = 0;
439
440 // ========== Classic Bluetooth Operations ==========
441
448
455 virtual hf_bluetooth_err_t SetDiscoverable(bool discoverable, uint32_t timeout_ms = 0) noexcept = 0;
456
461 virtual bool IsDiscoverable() const noexcept = 0;
462
463 // ========== BLE Operations ==========
464
470 virtual hf_bluetooth_err_t ConfigureBle(const hf_bluetooth_ble_config_t& config) noexcept = 0;
471
476 virtual hf_bluetooth_err_t StartAdvertising() noexcept = 0;
477
482 virtual hf_bluetooth_err_t StopAdvertising() noexcept = 0;
483
488 virtual bool IsAdvertising() const noexcept = 0;
489
490 // ========== Device Discovery ==========
491
499 uint32_t duration_ms = 0,
501
506 virtual hf_bluetooth_err_t StopScan() noexcept = 0;
507
512 virtual bool IsScanning() const noexcept = 0;
513
520 std::vector<hf_bluetooth_device_info_t>& devices) noexcept = 0;
521
527
528 // ========== Connection Management ==========
529
537 uint32_t timeout_ms = 0) noexcept = 0;
538
544 virtual hf_bluetooth_err_t Disconnect(const hf_bluetooth_address_t& address) noexcept = 0;
545
551 virtual bool IsConnected(const hf_bluetooth_address_t& address) const noexcept = 0;
552
559 std::vector<hf_bluetooth_device_info_t>& devices) noexcept = 0;
560
561 // ========== Pairing and Bonding ==========
562
570 const std::string& pin = "") noexcept = 0;
571
577 virtual hf_bluetooth_err_t Unpair(const hf_bluetooth_address_t& address) noexcept = 0;
578
584 virtual bool IsPaired(const hf_bluetooth_address_t& address) const noexcept = 0;
585
591 virtual hf_bluetooth_err_t GetPairedDevices(std::vector<hf_bluetooth_device_info_t>& devices) noexcept = 0;
592
593 // ========== Data Transmission ==========
594
602 const std::vector<uint8_t>& data) noexcept = 0;
603
609 virtual int GetAvailableData(const hf_bluetooth_address_t& address) const noexcept = 0;
610
619 std::vector<uint8_t>& data, size_t max_bytes = 0) noexcept = 0;
620
621 // ========== GATT Operations (BLE) ==========
622
630 const hf_bluetooth_address_t& address,
631 std::vector<hf_bluetooth_gatt_service_t>& services) noexcept = 0;
632
641 const hf_bluetooth_address_t& address, const std::string& service_uuid,
642 std::vector<hf_bluetooth_gatt_characteristic_t>& characteristics) noexcept = 0;
643
653 const std::string& service_uuid,
654 const std::string& characteristic_uuid,
655 std::vector<uint8_t>& value) noexcept = 0;
656
667 const std::string& service_uuid,
668 const std::string& characteristic_uuid,
669 const std::vector<uint8_t>& value,
670 bool with_response = true) noexcept = 0;
671
681 const std::string& service_uuid,
682 const std::string& characteristic_uuid,
683 bool enable) noexcept = 0;
684
685 // ========== State and Status ==========
686
691 virtual hf_bluetooth_state_t GetState() const noexcept = 0;
692
698 virtual int8_t GetRssi(const hf_bluetooth_address_t& address) const noexcept = 0;
699
700 // ========== Event Handling ==========
701
708
714
721
727
728 // ========== Utility Functions ==========
729
735 static std::string_view GetErrorString(hf_bluetooth_err_t error);
736
737protected:
741 BaseBluetooth() = default;
742
746 BaseBluetooth(const BaseBluetooth&) = delete;
747
751 BaseBluetooth& operator=(const BaseBluetooth&) = delete;
752};
753
758inline std::string_view BaseBluetooth::GetErrorString(hf_bluetooth_err_t error) {
759#define X(name, value, desc) \
760 case hf_bluetooth_err_t::name: \
761 return desc;
762 switch (error) {
764 default:
766 }
767#undef X
768}
769
774inline std::string hf_bluetooth_address_t::ToString() const {
775 char buffer[18];
776 snprintf(buffer, sizeof(buffer), "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2],
777 addr[3], addr[4], addr[5]);
778 return std::string(buffer);
779}
780
781inline bool hf_bluetooth_address_t::FromString(const std::string& addr_str) {
782 if (addr_str.length() != 17)
783 return false;
784
785 // Validate format: XX:XX:XX:XX:XX:XX
786 for (size_t i = 0; i < addr_str.length(); ++i) {
787 if (i % 3 == 2) {
788 if (addr_str[i] != ':')
789 return false;
790 } else {
791 if (!std::isxdigit(addr_str[i]))
792 return false;
793 }
794 }
795
796 // Parse using no-exceptions approach with manual hex conversion
797 for (int i = 0; i < 6; i++) {
798 size_t pos = i * 3;
799 const char* hex_start = addr_str.c_str() + pos;
800
801 // Manual hex conversion without exceptions
802 unsigned int value = 0;
803 for (int j = 0; j < 2; j++) {
804 char c = hex_start[j];
805 if (c >= '0' && c <= '9') {
806 value = (value << 4) + (c - '0');
807 } else if (c >= 'A' && c <= 'F') {
808 value = (value << 4) + (c - 'A' + 10);
809 } else if (c >= 'a' && c <= 'f') {
810 value = (value << 4) + (c - 'a' + 10);
811 } else {
812 return false; // Invalid hex character
813 }
814 }
815
816 if (value > 255)
817 return false;
818 addr[i] = static_cast<uint8_t>(value);
819 }
820
821 return true;
822}
823
825 for (int i = 0; i < 6; i++) {
826 if (addr[i] != 0)
827 return true;
828 }
829 return false;
830}
hf_bluetooth_err_t
Definition BaseBluetooth.h:104
Platform-agnostic hardware type definitions for the HardFOC system.
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
Abstract base class for Bluetooth functionality.
Definition BaseBluetooth.h:359
virtual std::string GetDeviceName() const noexcept=0
Get local device name.
virtual hf_bluetooth_err_t ConfigureClassic(const hf_bluetooth_classic_config_t &config) noexcept=0
Configure Bluetooth Classic parameters.
virtual hf_bluetooth_err_t ClearDiscoveredDevices() noexcept=0
Clear discovered devices list.
virtual hf_bluetooth_err_t DiscoverServices(const hf_bluetooth_address_t &address, std::vector< hf_bluetooth_gatt_service_t > &services) noexcept=0
Discover GATT services on a connected device.
virtual hf_bluetooth_err_t SetMode(hf_bluetooth_mode_t mode) noexcept=0
Set Bluetooth operating mode.
virtual hf_bluetooth_err_t SubscribeCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, bool enable) noexcept=0
Subscribe to GATT characteristic notifications.
virtual hf_bluetooth_err_t Disable() noexcept=0
Disable Bluetooth.
virtual hf_bluetooth_err_t WriteCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, const std::vector< uint8_t > &value, bool with_response=true) noexcept=0
Write GATT characteristic value.
virtual hf_bluetooth_err_t StartAdvertising() noexcept=0
Start BLE advertising.
virtual bool IsAdvertising() const noexcept=0
Check if BLE advertising is active.
virtual bool IsEnabled() const noexcept=0
Check if Bluetooth is enabled.
virtual hf_bluetooth_err_t SetDeviceName(const std::string &name) noexcept=0
Set local device name.
virtual hf_bluetooth_err_t Pair(const hf_bluetooth_address_t &address, const std::string &pin="") noexcept=0
Pair with a remote device.
virtual hf_bluetooth_err_t RegisterEventCallback(hf_bluetooth_event_callback_t callback) noexcept=0
Register event callback.
virtual hf_bluetooth_err_t UnregisterDataCallback() noexcept=0
Unregister data received callback.
virtual hf_bluetooth_err_t GetConnectedDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept=0
Get list of connected devices.
virtual hf_bluetooth_err_t Connect(const hf_bluetooth_address_t &address, uint32_t timeout_ms=0) noexcept=0
Connect to a remote device.
virtual hf_bluetooth_err_t Initialize(hf_bluetooth_mode_t mode) noexcept=0
Initialize the Bluetooth subsystem.
virtual hf_bluetooth_err_t GetDiscoveredDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept=0
Get discovered devices.
virtual hf_bluetooth_err_t DiscoverCharacteristics(const hf_bluetooth_address_t &address, const std::string &service_uuid, std::vector< hf_bluetooth_gatt_characteristic_t > &characteristics) noexcept=0
Discover GATT characteristics for a service.
virtual hf_bluetooth_err_t ReadCharacteristic(const hf_bluetooth_address_t &address, const std::string &service_uuid, const std::string &characteristic_uuid, std::vector< uint8_t > &value) noexcept=0
Read GATT characteristic value.
virtual hf_bluetooth_err_t Deinitialize() noexcept=0
Deinitialize the Bluetooth subsystem.
virtual hf_bluetooth_err_t Disconnect(const hf_bluetooth_address_t &address) noexcept=0
Disconnect from a remote device.
virtual hf_bluetooth_err_t StartScan(uint32_t duration_ms=0, hf_bluetooth_scan_type_t type=hf_bluetooth_scan_type_t::HF_BLUETOOTH_SCAN_TYPE_ACTIVE) noexcept=0
Start device discovery/scanning.
virtual int GetAvailableData(const hf_bluetooth_address_t &address) const noexcept=0
Check if data is available to read.
virtual hf_bluetooth_err_t GetPairedDevices(std::vector< hf_bluetooth_device_info_t > &devices) noexcept=0
Get list of paired devices.
virtual ~BaseBluetooth() noexcept=default
Virtual destructor for proper cleanup of derived classes.
virtual hf_bluetooth_mode_t GetMode() const noexcept=0
Get current Bluetooth operating mode.
virtual bool IsConnected(const hf_bluetooth_address_t &address) const noexcept=0
Check if connected to a device.
virtual hf_bluetooth_err_t ReadData(const hf_bluetooth_address_t &address, std::vector< uint8_t > &data, size_t max_bytes=0) noexcept=0
Read available data from a connected device.
virtual bool IsInitialized() const noexcept=0
Check if Bluetooth is initialized.
virtual hf_bluetooth_state_t GetState() const noexcept=0
Get current Bluetooth state.
virtual hf_bluetooth_err_t RegisterDataCallback(hf_bluetooth_data_callback_t callback) noexcept=0
Register data received callback.
virtual hf_bluetooth_err_t Enable() noexcept=0
Enable Bluetooth.
virtual hf_bluetooth_err_t ConfigureBle(const hf_bluetooth_ble_config_t &config) noexcept=0
Configure Bluetooth Low Energy parameters.
virtual hf_bluetooth_err_t Unpair(const hf_bluetooth_address_t &address) noexcept=0
Unpair from a remote device.
virtual hf_bluetooth_err_t StopAdvertising() noexcept=0
Stop BLE advertising.
virtual hf_bluetooth_err_t StopScan() noexcept=0
Stop device discovery/scanning.
virtual bool IsDiscoverable() const noexcept=0
Check if device is discoverable.
virtual hf_bluetooth_err_t GetLocalAddress(hf_bluetooth_address_t &address) const noexcept=0
Get local Bluetooth address.
virtual int8_t GetRssi(const hf_bluetooth_address_t &address) const noexcept=0
Get signal strength for a connected device.
virtual hf_bluetooth_err_t SendData(const hf_bluetooth_address_t &address, const std::vector< uint8_t > &data) noexcept=0
Send data to a connected device.
virtual hf_bluetooth_err_t SetDiscoverable(bool discoverable, uint32_t timeout_ms=0) noexcept=0
Make device discoverable.
virtual bool IsPaired(const hf_bluetooth_address_t &address) const noexcept=0
Check if paired with a device.
virtual hf_bluetooth_err_t UnregisterEventCallback() noexcept=0
Unregister event callback.
virtual bool IsScanning() const noexcept=0
Check if scanning is in progress.
std::string ToString() const
Convert address to string format.
Definition BaseBluetooth.h:774
hf_bluetooth_connection_type_t
Bluetooth connection types.
Definition BaseBluetooth.h:198
constexpr std::string_view HfBluetoothErrToString(hf_bluetooth_err_t err) noexcept
Convert hf_bluetooth_err_t to human-readable string.
Definition BaseBluetooth.h:113
std::function< void(const hf_bluetooth_address_t &address, const std::vector< uint8_t > &data)> hf_bluetooth_data_callback_t
Data received callback function type.
Definition BaseBluetooth.h:343
hf_bluetooth_mode_t
Bluetooth operating modes.
Definition BaseBluetooth.h:129
hf_bluetooth_connection_state_t
Bluetooth connection states.
Definition BaseBluetooth.h:208
#define HF_BLUETOOTH_ERR_LIST(X)
HardFOC Bluetooth error codes macro list.
Definition BaseBluetooth.h:51
static std::string_view GetErrorString(hf_bluetooth_err_t error)
Get error description string.
Definition BaseBluetooth.h:758
hf_bluetooth_state_t
Bluetooth connection states.
Definition BaseBluetooth.h:151
hf_bluetooth_scan_mode_t
Bluetooth scan modes.
Definition BaseBluetooth.h:188
std::function< void(hf_bluetooth_event_t event, void *event_data)> hf_bluetooth_event_callback_t
Bluetooth event callback function type.
Definition BaseBluetooth.h:336
hf_bluetooth_event_t
Bluetooth event types for callback functions.
Definition BaseBluetooth.h:220
hf_bluetooth_scan_type_t
Bluetooth scan types.
Definition BaseBluetooth.h:179
#define X(name, value, desc)
Generate Bluetooth error enumeration using X-macro pattern.
Definition BaseBluetooth.h:103
hf_bluetooth_security_t
Bluetooth security levels.
Definition BaseBluetooth.h:166
hf_bluetooth_device_type_t
Bluetooth device types.
Definition BaseBluetooth.h:140
@ HF_BLUETOOTH_EVENT_GATT_SERVICE_DISCOVERED
@ HF_BLUETOOTH_EVENT_GATT_CHARACTERISTIC_WRITE
@ HF_BLUETOOTH_EVENT_GATT_CHARACTERISTIC_READ
Bluetooth address structure (6 bytes)
Definition BaseBluetooth.h:245
bool IsValid() const
Check if address is valid (not all zeros)
Definition BaseBluetooth.h:824
bool FromString(const std::string &addr_str)
Parse address from string.
Definition BaseBluetooth.h:781
hf_u8_t addr[6]
Definition BaseBluetooth.h:246
Bluetooth Low Energy configuration structure.
Definition BaseBluetooth.h:299
hf_bluetooth_scan_type_t scan_type
Definition BaseBluetooth.h:307
bool scannable
Definition BaseBluetooth.h:302
uint16_t scan_interval_ms
Definition BaseBluetooth.h:305
bool connectable
Definition BaseBluetooth.h:303
bool advertising
Definition BaseBluetooth.h:301
std::string device_name
Definition BaseBluetooth.h:300
hf_bluetooth_security_t security
Definition BaseBluetooth.h:308
uint16_t scan_window_ms
Definition BaseBluetooth.h:306
uint16_t advertising_interval_ms
Definition BaseBluetooth.h:304
Bluetooth Classic configuration structure.
Definition BaseBluetooth.h:287
hf_bluetooth_security_t security
Definition BaseBluetooth.h:292
std::string device_name
Definition BaseBluetooth.h:288
bool discoverable
Definition BaseBluetooth.h:289
uint32_t discovery_timeout_ms
Definition BaseBluetooth.h:291
bool connectable
Definition BaseBluetooth.h:290
Bluetooth device information structure.
Definition BaseBluetooth.h:272
uint32_t class_of_device
Definition BaseBluetooth.h:277
std::string name
Definition BaseBluetooth.h:274
bool is_connected
Definition BaseBluetooth.h:280
hf_bluetooth_device_type_t type
Definition BaseBluetooth.h:275
hf_bluetooth_address_t address
Definition BaseBluetooth.h:273
std::vector< std::string > uuids
Definition BaseBluetooth.h:278
int8_t rssi
Definition BaseBluetooth.h:276
bool is_bonded
Definition BaseBluetooth.h:279
GATT characteristic structure.
Definition BaseBluetooth.h:325
uint8_t properties
Definition BaseBluetooth.h:328
std::string uuid
Definition BaseBluetooth.h:326
uint16_t handle
Definition BaseBluetooth.h:327
std::vector< uint8_t > value
Definition BaseBluetooth.h:329
GATT service structure.
Definition BaseBluetooth.h:315
bool is_primary
Definition BaseBluetooth.h:317
uint16_t handle
Definition BaseBluetooth.h:318
std::string uuid
Definition BaseBluetooth.h:316