mirror of https://github.com/labapart/gattlib
gattlib: Introduce 'gattlib_adapter_get_name()'
parent
884904a3c0
commit
2518348023
|
@ -54,6 +54,11 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
|
||||||
return GATTLIB_SUCCESS;
|
return GATTLIB_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *gattlib_adapter_get_name(void* adapter) {
|
||||||
|
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||||
|
return gattlib_adapter->adapter_name;
|
||||||
|
}
|
||||||
|
|
||||||
struct gattlib_adapter *init_default_adapter(void) {
|
struct gattlib_adapter *init_default_adapter(void) {
|
||||||
struct gattlib_adapter *gattlib_adapter;
|
struct gattlib_adapter *gattlib_adapter;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* SPDX-License-Identifier: BSD-3-Clause
|
* SPDX-License-Identifier: BSD-3-Clause
|
||||||
*
|
*
|
||||||
* Copyright (c) 2016-2021, Olivier Martin <olivier@labapart.org>
|
* Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
@ -149,6 +149,8 @@ static gboolean on_handle_characteristic_indication(
|
||||||
static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection, const uuid_t* uuid, void *callback) {
|
static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection, const uuid_t* uuid, void *callback) {
|
||||||
gattlib_context_t* conn_context = connection->context;
|
gattlib_context_t* conn_context = connection->context;
|
||||||
|
|
||||||
|
assert(callback != NULL);
|
||||||
|
|
||||||
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
|
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
|
||||||
if (dbus_characteristic.type == TYPE_NONE) {
|
if (dbus_characteristic.type == TYPE_NONE) {
|
||||||
char uuid_str[MAX_LEN_UUID_STR + 1];
|
char uuid_str[MAX_LEN_UUID_STR + 1];
|
||||||
|
@ -194,7 +196,6 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
|
||||||
|
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
org_bluez_gatt_characteristic1_call_start_notify_sync(dbus_characteristic.gatt, NULL, &error);
|
org_bluez_gatt_characteristic1_call_start_notify_sync(dbus_characteristic.gatt, NULL, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start DBus GATT notification: %s", error->message);
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start DBus GATT notification: %s", error->message);
|
||||||
g_error_free(error);
|
g_error_free(error);
|
||||||
|
|
|
@ -43,6 +43,12 @@ class Adapter:
|
||||||
self._adapter = c_void_p(None)
|
self._adapter = c_void_p(None)
|
||||||
self._is_opened = False # Note: 'self._adapter != c_void_p(None)' does not seem to return the expected result
|
self._is_opened = False # Note: 'self._adapter != c_void_p(None)' does not seem to return the expected result
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
if self._name:
|
||||||
|
return self._name
|
||||||
|
else:
|
||||||
|
return f"adapter@{self._adapter}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self._name
|
return self._name
|
||||||
|
@ -56,6 +62,8 @@ class Adapter:
|
||||||
ret = gattlib_adapter_open(self._name, byref(self._adapter))
|
ret = gattlib_adapter_open(self._name, byref(self._adapter))
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
self._is_opened = True
|
self._is_opened = True
|
||||||
|
if self._name is None:
|
||||||
|
self._name = gattlib_adapter_get_name(self._adapter)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
from gattlib import *
|
from gattlib import *
|
||||||
from .uuid import gattlib_uuid_to_uuid, gattlib_uuid_to_int
|
from .uuid import gattlib_uuid_to_uuid, gattlib_uuid_to_int
|
||||||
from .exception import handle_return
|
from .exception import handle_return, InvalidParameter
|
||||||
|
|
||||||
|
|
||||||
class GattStream():
|
class GattStream():
|
||||||
|
@ -115,6 +115,9 @@ class GattCharacteristic():
|
||||||
return GattStream(_stream, _mtu.value)
|
return GattStream(_stream, _mtu.value)
|
||||||
|
|
||||||
def register_notification(self, callback, user_data=None):
|
def register_notification(self, callback, user_data=None):
|
||||||
|
if not callable(callback):
|
||||||
|
raise InvalidParameter("Notification callback is not callable.")
|
||||||
|
|
||||||
self._device._notification_add_gatt_characteristic_callback(self, callback, user_data)
|
self._device._notification_add_gatt_characteristic_callback(self, callback, user_data)
|
||||||
|
|
||||||
def unregister_notification(self):
|
def unregister_notification(self):
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ def gattlib_uuid_to_int(gattlib_uuid):
|
||||||
return ValueError("Gattlib UUID not recognized (type:0x%x)" % gattlib_uuid.type)
|
return ValueError("Gattlib UUID not recognized (type:0x%x)" % gattlib_uuid.type)
|
||||||
|
|
||||||
|
|
||||||
def gattlib_uuid_str_to_int(uuid_str):
|
def gattlib_uuid_str_to_int(uuid_str: str) -> int:
|
||||||
# Check if the string could already encode a UUID16 or UUID32
|
# Check if the string could already encode a UUID16 or UUID32
|
||||||
if len(uuid_str) <= 8:
|
if len(uuid_str) <= 8:
|
||||||
return int(uuid_str, 16)
|
return int(uuid_str, 16)
|
||||||
|
@ -50,4 +51,8 @@ def gattlib_uuid_str_to_int(uuid_str):
|
||||||
if match:
|
if match:
|
||||||
return int(match.group(1), 16)
|
return int(match.group(1), 16)
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
return UUID(uuid_str).int
|
return UUID(uuid_str).int
|
||||||
|
except ValueError:
|
||||||
|
logging.error("Could not convert %s to a UUID", uuid_str)
|
||||||
|
raise
|
||||||
|
|
|
@ -219,6 +219,15 @@ extern const char *gattlib_eddystone_url_scheme_prefix[];
|
||||||
*/
|
*/
|
||||||
int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
int gattlib_adapter_open(const char* adapter_name, void** adapter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get adapter name
|
||||||
|
*
|
||||||
|
* @param adapter is the context of the newly opened adapter
|
||||||
|
*
|
||||||
|
* @return Adapter name
|
||||||
|
*/
|
||||||
|
const char *gattlib_adapter_get_name(void* adapter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Enable Bluetooth scanning on a given adapter
|
* @brief Enable Bluetooth scanning on a given adapter
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue