51#define HF_BLUETOOTH_ERR_LIST(X) \
53 X(BLUETOOTH_SUCCESS, 0, "Success") \
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")
103#define X(name, value, desc) name = value,
115#define X(NAME, VALUE, DESC) \
116 case hf_bluetooth_err_t::NAME: \
499 uint32_t duration_ms = 0,
537 uint32_t timeout_ms = 0) noexcept = 0;
570 const std::
string& pin = "") noexcept = 0;
602 const std::vector<uint8_t>& data) noexcept = 0;
619 std::vector<uint8_t>& data,
size_t max_bytes = 0) noexcept = 0;
653 const std::
string& service_uuid,
654 const std::
string& characteristic_uuid,
655 std::vector<uint8_t>& value) noexcept = 0;
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;
681 const std::
string& service_uuid,
682 const std::
string& characteristic_uuid,
683 bool enable) noexcept = 0;
759#define X(name, value, desc) \
760 case hf_bluetooth_err_t::name: \
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);
782 if (addr_str.length() != 17)
786 for (
size_t i = 0; i < addr_str.length(); ++i) {
788 if (addr_str[i] !=
':')
791 if (!std::isxdigit(addr_str[i]))
797 for (
int i = 0; i < 6; i++) {
799 const char* hex_start = addr_str.c_str() + pos;
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);
818 addr[i] =
static_cast<uint8_t
>(value);
825 for (
int i = 0; i < 6; i++) {
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_CONNECTION_TYPE_CLASSIC
@ HF_BLUETOOTH_CONNECTION_TYPE_AUTO
@ HF_BLUETOOTH_CONNECTION_TYPE_BLE
@ HF_BLUETOOTH_MODE_CLASSIC
@ HF_BLUETOOTH_MODE_DISABLED
@ HF_BLUETOOTH_CONNECTION_STATE_CONNECTED
@ HF_BLUETOOTH_CONNECTION_STATE_ADVERTISING
@ HF_BLUETOOTH_CONNECTION_STATE_CONNECTING
@ HF_BLUETOOTH_CONNECTION_STATE_DISCONNECTED
@ HF_BLUETOOTH_CONNECTION_STATE_SCANNING
@ HF_BLUETOOTH_STATE_DISCONNECTING
@ HF_BLUETOOTH_STATE_CONNECTED
@ HF_BLUETOOTH_STATE_PAIRING
@ HF_BLUETOOTH_STATE_ENABLED
@ HF_BLUETOOTH_STATE_CONNECTING
@ HF_BLUETOOTH_STATE_PAIRED
@ HF_BLUETOOTH_STATE_DISABLED
@ HF_BLUETOOTH_STATE_SCANNING
@ HF_BLUETOOTH_SCAN_MODE_LE_LIMITED
@ HF_BLUETOOTH_SCAN_MODE_LE_GENERAL
@ HF_BLUETOOTH_SCAN_MODE_GENERAL_INQUIRY
@ HF_BLUETOOTH_EVENT_UNPAIR_SUCCESS
@ HF_BLUETOOTH_EVENT_DISABLED
@ HF_BLUETOOTH_EVENT_SCAN_STOP
@ HF_BLUETOOTH_EVENT_PAIR_REQUEST
@ HF_BLUETOOTH_EVENT_DEVICE_FOUND
@ HF_BLUETOOTH_EVENT_SCAN_START
@ HF_BLUETOOTH_EVENT_CONNECT_SUCCESS
@ HF_BLUETOOTH_EVENT_PAIR_FAILED
@ HF_BLUETOOTH_EVENT_DATA_RECEIVED
@ HF_BLUETOOTH_EVENT_CONNECT_FAILED
@ HF_BLUETOOTH_EVENT_GATT_SERVICE_DISCOVERED
@ HF_BLUETOOTH_EVENT_DATA_SENT
@ HF_BLUETOOTH_EVENT_DISCONNECT
@ HF_BLUETOOTH_EVENT_GATT_CHARACTERISTIC_WRITE
@ HF_BLUETOOTH_EVENT_GATT_NOTIFICATION
@ HF_BLUETOOTH_EVENT_PAIR_SUCCESS
@ HF_BLUETOOTH_EVENT_GATT_CHARACTERISTIC_READ
@ HF_BLUETOOTH_EVENT_ENABLED
@ HF_BLUETOOTH_SCAN_TYPE_PASSIVE
@ HF_BLUETOOTH_SCAN_TYPE_ACTIVE
@ HF_BLUETOOTH_SECURITY_AUTHENTICATED_SC
@ HF_BLUETOOTH_SECURITY_AUTHORIZED
@ HF_BLUETOOTH_SECURITY_AUTHENTICATED
@ HF_BLUETOOTH_SECURITY_UNAUTHENTICATED
@ HF_BLUETOOTH_SECURITY_NONE
@ HF_BLUETOOTH_SECURITY_ENCRYPTED
@ HF_BLUETOOTH_DEVICE_TYPE_CLASSIC
@ HF_BLUETOOTH_DEVICE_TYPE_DUAL
@ HF_BLUETOOTH_DEVICE_TYPE_BLE
@ HF_BLUETOOTH_DEVICE_TYPE_UNKNOWN
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