gattlib: Introduce 'gattlib_adapter_get_name()'

to-push
Olivier Martin 2024-02-12 22:33:16 +01:00 committed by Olivier Martin
parent 884904a3c0
commit 2518348023
6 changed files with 36 additions and 5 deletions

View File

@ -54,6 +54,11 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
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 *gattlib_adapter;
int ret;

View File

@ -1,7 +1,7 @@
/*
* 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>
@ -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) {
gattlib_context_t* conn_context = connection->context;
assert(callback != NULL);
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
if (dbus_characteristic.type == TYPE_NONE) {
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;
org_bluez_gatt_characteristic1_call_start_notify_sync(dbus_characteristic.gatt, NULL, &error);
if (error) {
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start DBus GATT notification: %s", error->message);
g_error_free(error);

View File

@ -43,6 +43,12 @@ class Adapter:
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
def __str__(self) -> str:
if self._name:
return self._name
else:
return f"adapter@{self._adapter}"
@property
def name(self):
return self._name
@ -56,6 +62,8 @@ class Adapter:
ret = gattlib_adapter_open(self._name, byref(self._adapter))
if ret == 0:
self._is_opened = True
if self._name is None:
self._name = gattlib_adapter_get_name(self._adapter)
return ret
def close(self):

View File

@ -6,7 +6,7 @@
from gattlib import *
from .uuid import gattlib_uuid_to_uuid, gattlib_uuid_to_int
from .exception import handle_return
from .exception import handle_return, InvalidParameter
class GattStream():
@ -115,6 +115,9 @@ class GattCharacteristic():
return GattStream(_stream, _mtu.value)
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)
def unregister_notification(self):

View File

@ -4,6 +4,7 @@
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
#
import logging
import re
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)
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
if len(uuid_str) <= 8:
return int(uuid_str, 16)
@ -50,4 +51,8 @@ def gattlib_uuid_str_to_int(uuid_str):
if match:
return int(match.group(1), 16)
else:
return UUID(uuid_str).int
try:
return UUID(uuid_str).int
except ValueError:
logging.error("Could not convert %s to a UUID", uuid_str)
raise

View File

@ -219,6 +219,15 @@ extern const char *gattlib_eddystone_url_scheme_prefix[];
*/
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
*