Introduce Gattlib logging backend

pull/238/head
Olivier Martin 2021-10-18 01:25:29 +02:00 committed by Olivier Martin
parent 809a10a289
commit 1afaa8b460
23 changed files with 339 additions and 107 deletions

View File

@ -58,6 +58,11 @@ else()
set(GATTLIB_DBUS TRUE)
endif()
# With 'syslog' backend, we enable all logs (ie: up to level debug) and we leave the
# application to set the level using 'setlogmask()'
set(GATTLIB_LOG_LEVEL 3 CACHE STRING "Define the minimum logging level for Gattlib (0=error, 1=warning, 2=info, 3=debug)")
set(GATTLIB_LOG_BACKEND syslog CACHE STRING "Define logging backend: syslog, printf (default: syslog)")
if (GATTLIB_DBUS)
# Build dbus-based gattlib
add_subdirectory(dbus)

View File

@ -77,7 +77,8 @@ set(gattlib_SRCS gattlib_adapter.c
gattlib_discover.c
gattlib_read_write.c
${CMAKE_SOURCE_DIR}/common/gattlib_common.c
${CMAKE_SOURCE_DIR}/common/gattlib_eddystone.c)
${CMAKE_SOURCE_DIR}/common/gattlib_eddystone.c
${CMAKE_SOURCE_DIR}/common/logging_backend/${GATTLIB_LOG_BACKEND}/gattlib_logging.c)
# Added Glib support
pkg_search_module(GLIB REQUIRED glib-2.0)
@ -99,6 +100,11 @@ else()
endif()
# gattlib
target_compile_definitions(gattlib PUBLIC -DGATTLIB_LOG_LEVEL=${GATTLIB_LOG_LEVEL})
if (GATTLIB_LOG_BACKEND STREQUAL "syslog")
target_compile_definitions(gattlib PUBLIC -DGATTLIB_LOG_BACKEND_SYSLOG)
endif()
include(GNUInstallDirs)
if(GATTLIB_SHARED_LIB)
add_library(gattlib SHARED ${gattlib_SRCS})

View File

@ -84,7 +84,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
gattlib_context_t* conn_context = connection->context;
ret = gatt_discover_primary(conn_context->attrib, NULL, primary_all_cb, &user_data);
if (ret == 0) {
fprintf(stderr, "Fail to discover primary services.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover primary services.");
return GATTLIB_ERROR_BLUEZ;
}
@ -156,7 +156,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
gattlib_context_t* conn_context = connection->context;
ret = gatt_discover_char(conn_context->attrib, start, end, NULL, characteristic_cb, &user_data);
if (ret == 0) {
fprintf(stderr, "Fail to discover characteristics.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristics.");
return GATTLIB_ERROR_BLUEZ;
}

View File

@ -80,7 +80,7 @@ void gattlib_call_notification_handler(struct gattlib_handler *handler, const uu
}
#endif
else {
fprintf(stderr, "Invalid notification handler.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Invalid notification handler.");
}
}
@ -100,7 +100,7 @@ void gattlib_call_disconnection_handler(struct gattlib_handler *handler) {
}
#endif
else {
fprintf(stderr, "Invalid disconnection handler.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Invalid disconnection handler.");
}
}

View File

@ -57,7 +57,7 @@ int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32
ret = gattlib_string_to_uuid(EDDYSTONE_SERVICE_UUID, strlen(EDDYSTONE_SERVICE_UUID) + 1, &eddystone_uuid);
if (ret != 0) {
fprintf(stderr, "Fail to convert characteristic TX to UUID.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to convert characteristic TX to UUID.");
return GATTLIB_ERROR_INTERNAL;
}

View File

@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2021, Olivier Martin <olivier@labapart.org>
*/
#include "gattlib_internal.h"
void gattlib_log(int level, const char *format, ...) {
va_list args;
FILE *stream = stdout;
if (level == GATTLIB_ERROR) {
stream = stderr;
}
va_start(args, format);
vfprintf(stream, format, args);
fprintf(stream, "\n");
va_end(args);
}

View File

@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2021, Olivier Martin <olivier@labapart.org>
*/
#include <syslog.h>
#include "gattlib_internal.h"
static const int m_gattlib_log_level_to_syslog[] = {
LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG
};
void gattlib_log(int level, const char *format, ...) {
va_list args;
va_start(args, format);
vsyslog(m_gattlib_log_level_to_syslog[level], format, args);
va_end(args);
}

View File

@ -74,6 +74,7 @@ set(gattlib_SRCS gattlib.c
bluez5/lib/uuid.c
${CMAKE_CURRENT_LIST_DIR}/../common/gattlib_common.c
${CMAKE_CURRENT_LIST_DIR}/../common/gattlib_eddystone.c
${CMAKE_CURRENT_LIST_DIR}/../common/logging_backend/${GATTLIB_LOG_BACKEND}/gattlib_logging.c
${CMAKE_CURRENT_BINARY_DIR}/org-bluez-adaptater1.c
${CMAKE_CURRENT_BINARY_DIR}/org-bluez-device1.c
${CMAKE_CURRENT_BINARY_DIR}/org-bluez-gattcharacteristic1.c
@ -111,6 +112,11 @@ else()
add_library(gattlib ${gattlib_SRCS})
endif()
target_compile_definitions(gattlib PUBLIC -DGATTLIB_LOG_LEVEL=${GATTLIB_LOG_LEVEL})
if (GATTLIB_LOG_BACKEND STREQUAL "syslog")
target_compile_definitions(gattlib PUBLIC -DGATTLIB_LOG_BACKEND_SYSLOG)
endif()
target_include_directories(gattlib PUBLIC ../include)
target_link_libraries(gattlib ${gattlib_LIBS})

View File

@ -38,6 +38,7 @@ gboolean on_handle_device_property_change(
g_variant_get (arg_changed_properties, "a{sv}", &iter);
while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: device_property_change: %s: %s", key, g_variant_print(value, TRUE));
if (strcmp(key, "Connected") == 0) {
if (!g_variant_get_boolean(value)) {
// Disconnection case
@ -154,7 +155,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
&error);
if (device == NULL) {
if (error) {
fprintf(stderr, "Failed to connect to DBus Bluez Device: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect to DBus Bluez Device: %s", error->message);
g_error_free(error);
}
goto FREE_CONNECTION;
@ -175,9 +176,9 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
if (strncmp(error->message, m_dbus_error_unknown_object, strlen(m_dbus_error_unknown_object)) == 0) {
// You might have this error if the computer has not scanned or has not already had
// pairing information about the targetted device.
fprintf(stderr, "Device '%s' cannot be found\n", dst);
GATTLIB_LOG(GATTLIB_ERROR, "Device '%s' cannot be found", dst);
} else {
fprintf(stderr, "Device connected error (device:%s): %s\n",
GATTLIB_LOG(GATTLIB_ERROR, "Device connected error (device:%s): %s",
conn_context->device_object_path,
error->message);
}
@ -239,7 +240,7 @@ int gattlib_disconnect(gatt_connection_t* connection) {
org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error);
if (error) {
fprintf(stderr, "Failed to disconnect DBus Bluez Device: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message);
g_error_free(error);
}
@ -301,10 +302,10 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
&error);
if (service_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open service '%s': %s\n", *service_str, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s': %s", *service_str, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open service '%s'.\n", *service_str);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s'.", *service_str);
}
continue;
}
@ -343,7 +344,7 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
const gchar* const* service_strs = org_bluez_device1_get_uuids(device);
if (device_manager == NULL) {
fprintf(stderr, "Gattlib context not initialized.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib context not initialized.");
return GATTLIB_INVALID_PARAMETER;
}
@ -390,10 +391,10 @@ int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_serv
&error);
if (service_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open service '%s': %s\n", object_path, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s': %s", object_path, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open service '%s'.\n", object_path);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s'.", object_path);
}
continue;
}
@ -492,10 +493,10 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
&error);
if (service_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open services '%s': %s\n", *service_str, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open services '%s': %s", *service_str, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open services '%s'.\n", *service_str);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open services '%s'.", *service_str);
}
continue;
}
@ -539,10 +540,10 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
&error);
if (service_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open service '%s': %s\n", *service_str, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s': %s", *service_str, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open service '%s'.\n", *service_str);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s'.", *service_str);
}
continue;
}
@ -572,10 +573,10 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
&error);
if (characteristic_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open characteristic '%s': %s\n", characteristic_str, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open characteristic '%s': %s", characteristic_str, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open characteristic '%s'.\n", characteristic_str);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open characteristic '%s'.", characteristic_str);
}
continue;
} else {
@ -642,10 +643,10 @@ static void add_characteristics_from_service(gattlib_context_t* conn_context, GD
&error);
if (characteristic == NULL) {
if (error) {
fprintf(stderr, "Failed to open characteristic '%s': %s\n", object_path, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open characteristic '%s': %s", object_path, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open characteristic '%s'.\n", object_path);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open characteristic '%s'.", object_path);
}
continue;
}
@ -704,7 +705,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
GList *l;
if (device_manager == NULL) {
fprintf(stderr, "Gattlib context not initialized.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib context not initialized.");
return GATTLIB_INVALID_PARAMETER;
}
@ -750,10 +751,10 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
&error);
if (service_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to open service '%s': %s\n", object_path, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s': %s", object_path, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to open service '%s'.\n", object_path);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open service '%s'.", object_path);
}
continue;
}
@ -808,7 +809,7 @@ int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_a
NULL,
&error);
if (error) {
fprintf(stderr, "Failed to connection to new DBus Bluez Device: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connection to new DBus Bluez Device: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}

View File

@ -30,10 +30,10 @@ int gattlib_adapter_open(const char* adapter_name, void** adapter) {
NULL, &error);
if (adapter_proxy == NULL) {
if (error) {
fprintf(stderr, "Failed to get adapter %s: %s\n", object_path, error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to get adapter %s: %s", object_path, error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to get adapter %s\n", object_path);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to get adapter %s", object_path);
}
return GATTLIB_ERROR_DBUS;
}
@ -87,10 +87,10 @@ GDBusObjectManager *get_device_manager_from_adapter(struct gattlib_adapter *gatt
&error);
if (gattlib_adapter->device_manager == NULL) {
if (error) {
fprintf(stderr, "Failed to get Bluez Device Manager: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to get Bluez Device Manager: %s", error->message);
g_error_free(error);
} else {
fprintf(stderr, "Failed to get Bluez Device Manager.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to get Bluez Device Manager.");
}
return NULL;
}
@ -120,7 +120,7 @@ static void device_manager_on_device1_signal(const char* device1_path, struct di
NULL,
&error);
if (error) {
fprintf(stderr, "Failed to connection to new DBus Bluez Device: %s\n",
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connection to new DBus Bluez Device: %s",
error->message);
g_error_free(error);
}
@ -159,11 +159,15 @@ static void on_dbus_object_added(GDBusObjectManager *device_manager,
gpointer user_data)
{
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));
GDBusInterface *interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.Device1");
if (!interface) {
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_object_added: %s (not 'org.bluez.Device1')", object_path);
return;
}
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_object_added: %s (has 'org.bluez.Device1')", object_path);
// It is a 'org.bluez.Device1'
device_manager_on_device1_signal(object_path, user_data);
@ -178,6 +182,11 @@ on_interface_proxy_properties_changed (GDBusObjectManagerClient *device_manager,
const gchar *const *invalidated_properties,
gpointer user_data)
{
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_interface_proxy_properties_changed: interface:%s changed_properties:%s invalidated_properties:%s",
g_dbus_proxy_get_interface_name(interface_proxy),
g_variant_print(changed_properties, TRUE),
invalidated_properties);
// Check if the object is a 'org.bluez.Device1'
if (strcmp(g_dbus_proxy_get_interface_name(interface_proxy), "org.bluez.Device1") != 0) {
return;
@ -228,8 +237,8 @@ int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, i
}
if (error) {
printf("error: %d.%d\n", error->domain, error->code);
fprintf(stderr, "Failed to set discovery filter: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to set discovery filter: %s (%d.%d)",
error->message, error->domain, error->code);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}
@ -267,7 +276,7 @@ int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list, i
// Now, start BLE discovery
org_bluez_adapter1_call_start_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
if (error) {
fprintf(stderr, "Failed to start discovery: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start discovery: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}

View File

@ -40,8 +40,8 @@ int get_advertisement_data_from_device(OrgBluezDevice1 *bluez_device1,
manufacturer_data_variant = org_bluez_device1_get_manufacturer_data(bluez_device1);
if (manufacturer_data_variant != NULL) {
if (g_variant_n_children(manufacturer_data_variant) != 1) {
fprintf(stderr, "Warning: Manufacturer Data with multiple children: %s\n",
g_variant_print(manufacturer_data_variant, TRUE));
GATTLIB_LOG(GATTLIB_WARNING, "Manufacturer Data with multiple children: %s",
g_variant_print(manufacturer_data_variant, TRUE));
return GATTLIB_NOT_SUPPORTED;
}
GVariant* manufacturer_data_dict = g_variant_get_child_value(manufacturer_data_variant, 0);

View File

@ -37,7 +37,7 @@ static bool handle_dbus_gattcharacteristic_from_path(gattlib_context_t* conn_con
const gchar *characteristic_uuid_str = org_bluez_gatt_characteristic1_get_uuid(characteristic);
if (characteristic_uuid_str == NULL) {
// It should not be expected to get NULL from GATT characteristic UUID but we still test it
fprintf(stderr, "Error: %s path unexpectly returns a NULL UUID.\n", object_path);
GATTLIB_LOG(GATTLIB_ERROR, "Error: %s path unexpectly returns a NULL UUID.", object_path);
g_object_unref(characteristic);
return false;
}
@ -112,7 +112,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
};
if (device_manager == NULL) {
fprintf(stderr, "Gattlib Context not initialized.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib Context not initialized.");
return dbus_characteristic; // Return characteristic of type TYPE_NONE
}
@ -120,7 +120,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
if (gattlib_uuid_cmp(uuid, &m_battery_level_uuid) == 0) {
is_battery_level_uuid = true;
} else if (gattlib_uuid_cmp(uuid, &m_ccc_uuid) == 0) {
fprintf(stderr, "Error: Bluez v5.42+ does not expose Client Characteristic Configuration Descriptor through DBUS interface\n");
GATTLIB_LOG(GATTLIB_ERROR, "Error: Bluez v5.42+ does not expose Client Characteristic Configuration Descriptor through DBUS interface");
return dbus_characteristic;
}
@ -153,7 +153,7 @@ struct dbus_characteristic get_characteristic_from_uuid(gatt_connection_t* conne
}
}
#else
fprintf(stderr, "You might use Bluez v5.48 with gattlib built for pre-v5.40\n");
GATTLIB_LOG(GATTLIB_ERROR, "You might use Bluez v5.48 with gattlib built for pre-v5.40");
#endif
}
}
@ -172,7 +172,7 @@ static struct dbus_characteristic get_characteristic_from_handle(gatt_connection
};
if (device_manager == NULL) {
fprintf(stderr, "Gattlib context not initialized.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Gattlib context not initialized.");
return dbus_characteristic;
}
@ -219,7 +219,7 @@ static int read_gatt_characteristic(struct dbus_characteristic *dbus_characteris
g_variant_builder_unref(options);
#endif
if (error != NULL) {
fprintf(stderr, "Failed to read DBus GATT characteristic: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to read DBus GATT characteristic: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}
@ -314,7 +314,7 @@ int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid,
g_variant_builder_unref(options);
#endif
if (error != NULL) {
fprintf(stderr, "Failed to read DBus GATT characteristic: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to read DBus GATT characteristic: %s", error->message);
g_error_free(error);
ret = GATTLIB_ERROR_DBUS;
goto EXIT;
@ -353,7 +353,7 @@ static int write_char(struct dbus_characteristic *dbus_characteristic, const voi
#endif
if (error != NULL) {
fprintf(stderr, "Failed to write DBus GATT characteristic: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to write DBus GATT characteristic: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}

View File

@ -27,6 +27,10 @@ gboolean on_handle_battery_level_property_change(
static guint8 percentage;
gatt_connection_t* connection = user_data;
GATTLIB_LOG(GATTLIB_DEBUG, "DBUS: on_handle_battery_level_property_change: changed_properties:%s invalidated_properties:%s",
g_variant_print(arg_changed_properties, TRUE),
arg_invalidated_properties);
if (gattlib_has_valid_handler(&connection->notification)) {
// Retrieve 'Value' from 'arg_changed_properties'
if (g_variant_n_children (arg_changed_properties) > 0) {
@ -71,6 +75,9 @@ static gboolean on_handle_characteristic_property_change(
g_variant_get (arg_changed_properties, "a{sv}", &iter);
while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
GATTLIB_LOG(GATTLIB_DEBUG, "on_handle_characteristic_property_change: %s:%s",
key, g_variant_print(value, TRUE));
if (strcmp(key, "Value") == 0) {
uuid_t uuid;
size_t data_length;
@ -92,6 +99,8 @@ static gboolean on_handle_characteristic_property_change(
g_variant_iter_free(iter);
}
} else {
GATTLIB_LOG(GATTLIB_DEBUG, "on_handle_characteristic_property_change: not a notification handler");
}
return TRUE;
}
@ -113,6 +122,9 @@ static gboolean on_handle_characteristic_indication(
g_variant_get (arg_changed_properties, "a{sv}", &iter);
while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
GATTLIB_LOG(GATTLIB_DEBUG, "on_handle_indication_property_change: %s:%s",
key, g_variant_print(value, TRUE));
if (strcmp(key, "Value") == 0) {
uuid_t uuid;
size_t data_length;
@ -130,6 +142,8 @@ static gboolean on_handle_characteristic_indication(
}
g_variant_iter_free(iter);
}
} else {
GATTLIB_LOG(GATTLIB_DEBUG, "on_handle_indication_property_change: Not a valid indication handler");
}
return TRUE;
}
@ -143,7 +157,7 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
gattlib_uuid_to_string(uuid, uuid_str, sizeof(uuid_str));
fprintf(stderr, "GATT characteristic '%s' not found\n", uuid_str);
GATTLIB_LOG(GATTLIB_ERROR, "GATT characteristic '%s' not found", uuid_str);
return GATTLIB_NOT_FOUND;
}
#if BLUEZ_VERSION > BLUEZ_VERSIONS(5, 40)
@ -166,7 +180,7 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
G_CALLBACK(callback),
connection);
if (signal_id == 0) {
fprintf(stderr, "Failed to connect signal to DBus GATT notification\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect signal to DBus GATT notification");
return GATTLIB_ERROR_DBUS;
}
@ -184,7 +198,7 @@ static int connect_signal_to_characteristic_uuid(gatt_connection_t* connection,
org_bluez_gatt_characteristic1_call_start_notify_sync(dbus_characteristic.gatt, NULL, &error);
if (error) {
fprintf(stderr, "Failed to start DBus GATT notification: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to start DBus GATT notification: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
} else {
@ -220,7 +234,7 @@ static int disconnect_signal_to_characteristic_uuid(gatt_connection_t* connectio
free(notification_handle);
if (error) {
fprintf(stderr, "Failed to stop DBus GATT notification: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to stop DBus GATT notification: %s", error->message);
g_error_free(error);
return GATTLIB_NOT_FOUND;
} else {

View File

@ -48,7 +48,7 @@ int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t
g_variant_builder_unref(variant_options);
if (error != NULL) {
fprintf(stderr, "Failed to acquired write DBus GATT characteristic: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to acquired write DBus GATT characteristic: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}
@ -56,7 +56,7 @@ int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t
error = NULL;
fd = g_unix_fd_list_get(fd_list, g_variant_get_handle(out_fd), &error);
if (error != NULL) {
fprintf(stderr, "Failed to retrieve Unix File Descriptor: %s\n", error->message);
GATTLIB_LOG(GATTLIB_ERROR, "Failed to retrieve Unix File Descriptor: %s", error->message);
g_error_free(error);
return GATTLIB_ERROR_DBUS;
}

View File

@ -1,7 +1,34 @@
/*
*
* GattLib - GATT Library
*
* Copyright (C) 2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
#define BLE_SCAN_TIMEOUT 60
@ -43,13 +70,18 @@ int main(int argc, const char *argv[]) {
} else if (argc == 2) {
adapter_name = argv[1];
} else {
fprintf(stderr, "%s [<bluetooth-adapter>]\n", argv[0]);
GATTLIB_LOG(GATTLIB_ERROR, "%s [<bluetooth-adapter>]", argv[0]);
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_advertisement_dat", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
ret = gattlib_adapter_open(adapter_name, &adapter);
if (ret) {
fprintf(stderr, "ERROR: Failed to open adapter.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open adapter.");
return 1;
}
@ -61,7 +93,7 @@ int main(int argc, const char *argv[]) {
0, /* timeout=0 means infinite loop */
NULL /* user_data */);
if (ret) {
fprintf(stderr, "ERROR: Failed to scan.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to scan.");
goto EXIT;
}

View File

@ -1,9 +1,36 @@
/*
*
* GattLib - GATT Library
*
* Copyright (C) 2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/queue.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
#define BLE_SCAN_TIMEOUT 4
@ -36,7 +63,7 @@ static void *ble_connect_device(void *arg) {
gatt_connection = gattlib_connect(NULL, addr, GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
if (gatt_connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
goto connection_exit;
} else {
puts("Succeeded to connect to the bluetooth device.");
@ -44,7 +71,7 @@ static void *ble_connect_device(void *arg) {
ret = gattlib_discover_primary(gatt_connection, &services, &services_count);
if (ret != 0) {
fprintf(stderr, "Fail to discover primary services.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover primary services.");
goto disconnect_exit;
}
@ -59,7 +86,7 @@ static void *ble_connect_device(void *arg) {
ret = gattlib_discover_char(gatt_connection, &characteristics, &characteristics_count);
if (ret != 0) {
fprintf(stderr, "Fail to discover characteristics.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristics.");
goto disconnect_exit;
}
for (i = 0; i < characteristics_count; i++) {
@ -92,14 +119,14 @@ static void ble_discovered_device(void *adapter, const char* addr, const char* n
connection = malloc(sizeof(struct connection_t));
if (connection == NULL) {
fprintf(stderr, "Failt to allocate connection.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failt to allocate connection.");
return;
}
connection->addr = strdup(addr);
ret = pthread_create(&connection->thread, NULL, ble_connect_device, connection);
if (ret != 0) {
fprintf(stderr, "Failt to create BLE connection thread.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failt to create BLE connection thread.");
free(connection);
return;
}
@ -116,22 +143,27 @@ int main(int argc, const char *argv[]) {
} else if (argc == 2) {
adapter_name = argv[1];
} else {
fprintf(stderr, "%s [<bluetooth-adapter>]\n", argv[0]);
printf("%s [<bluetooth-adapter>]\n", argv[0]);
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_ble_scan", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
LIST_INIT(&g_ble_connections);
ret = gattlib_adapter_open(adapter_name, &adapter);
if (ret) {
fprintf(stderr, "ERROR: Failed to open adapter.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open adapter.");
return 1;
}
pthread_mutex_lock(&g_mutex);
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT, NULL /* user_data */);
if (ret) {
fprintf(stderr, "ERROR: Failed to scan.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to scan.");
goto EXIT;
}

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -24,6 +24,10 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
int main(int argc, char *argv[])
@ -35,6 +39,11 @@ int main(int argc, char *argv[])
char uuid_str[MAX_LEN_UUID_STR + 1];
int ret, i;
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_discover", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
if (argc != 2) {
printf("%s <device_address>\n", argv[0]);
return 1;
@ -42,20 +51,20 @@ int main(int argc, char *argv[])
connection = gattlib_connect(NULL, argv[1], GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
return 1;
}
ret = gattlib_discover_primary(connection, &services, &services_count);
if (ret != GATTLIB_SUCCESS) {
fprintf(stderr, "Fail to discover primary services.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover primary services.");
return 1;
}
for (i = 0; i < services_count; i++) {
gattlib_uuid_to_string(&services[i].uuid, uuid_str, sizeof(uuid_str));
printf("service[%d] start_handle:%02x end_handle:%02x uuid:%s\n", i,
GATTLIB_LOG(GATTLIB_INFO, "service[%d] start_handle:%02x end_handle:%02x uuid:%s", i,
services[i].attr_handle_start, services[i].attr_handle_end,
uuid_str);
}
@ -63,13 +72,13 @@ int main(int argc, char *argv[])
ret = gattlib_discover_char(connection, &characteristics, &characteristics_count);
if (ret != GATTLIB_SUCCESS) {
fprintf(stderr, "Fail to discover characteristics.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristics.");
return 1;
}
for (i = 0; i < characteristics_count; i++) {
gattlib_uuid_to_string(&characteristics[i].uuid, uuid_str, sizeof(uuid_str));
printf("characteristic[%d] properties:%02x value_handle:%04x uuid:%s\n", i,
GATTLIB_LOG(GATTLIB_INFO, "characteristic[%d] properties:%02x value_handle:%04x uuid:%s", i,
characteristics[i].properties, characteristics[i].value_handle,
uuid_str);
}

View File

@ -1,8 +1,29 @@
//#include <pthread.h>
//#include <stdio.h>
//#include <stdint.h>
//#include <stdlib.h>
//#include <sys/queue.h>
/*
*
* GattLib - GATT Library
*
* Copyright (C) 2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
@ -49,7 +70,7 @@ void on_eddystone_found(void *adapter, const char* addr, const char* name,
puts("\tEddystone EID");
break;
default:
fprintf(stderr, "\tEddystone ID %d not supported\n", advertisement_data_ptr->data[0]);
printf("\tEddystone ID %d not supported\n", advertisement_data_ptr->data[0]);
}
}
}
@ -68,13 +89,18 @@ int main(int argc, const char *argv[]) {
} else if (argc == 2) {
adapter_name = argv[1];
} else {
fprintf(stderr, "%s [<bluetooth-adapter>]\n", argv[0]);
printf("%s [<bluetooth-adapter>]\n", argv[0]);
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_find_eddystone", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
ret = gattlib_adapter_open(adapter_name, &adapter);
if (ret) {
fprintf(stderr, "ERROR: Failed to open adapter.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open adapter.");
return 1;
}
@ -83,7 +109,7 @@ int main(int argc, const char *argv[]) {
GATTLIB_EDDYSTONE_TYPE_URL,
on_eddystone_found, BLE_SCAN_EDDYSTONE_TIMEOUT, NULL);
if (ret) {
fprintf(stderr, "ERROR: Failed to scan.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Failed to scan.");
goto EXIT;
}

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -26,6 +26,10 @@
#include <stdlib.h>
#include <signal.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
@ -63,23 +67,28 @@ int main(int argc, char *argv[]) {
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_nordic_uart", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
m_connection = gattlib_connect(NULL, argv[1],
GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM |
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW);
if (m_connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
return 1;
}
// Convert characteristics to their respective UUIDs
ret = gattlib_string_to_uuid(NUS_CHARACTERISTIC_TX_UUID, strlen(NUS_CHARACTERISTIC_TX_UUID) + 1, &nus_characteristic_tx_uuid);
if (ret) {
fprintf(stderr, "Fail to convert characteristic TX to UUID.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to convert characteristic TX to UUID.");
return 1;
}
ret = gattlib_string_to_uuid(NUS_CHARACTERISTIC_RX_UUID, strlen(NUS_CHARACTERISTIC_RX_UUID) + 1, &nus_characteristic_rx_uuid);
if (ret) {
fprintf(stderr, "Fail to convert characteristic RX to UUID.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to convert characteristic RX to UUID.");
return 1;
}
@ -88,7 +97,7 @@ int main(int argc, char *argv[]) {
int characteristic_count;
ret = gattlib_discover_char(m_connection, &characteristics, &characteristic_count);
if (ret) {
fprintf(stderr, "Fail to discover characteristic.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristic.");
return 1;
}
@ -101,10 +110,10 @@ int main(int argc, char *argv[]) {
}
}
if (tx_handle == 0) {
fprintf(stderr, "Fail to find NUS TX characteristic.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to find NUS TX characteristic.");
return 1;
} else if (rx_handle == 0) {
fprintf(stderr, "Fail to find NUS RX characteristic.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to find NUS RX characteristic.");
return 1;
}
free(characteristics);
@ -114,7 +123,7 @@ int main(int argc, char *argv[]) {
ret = gattlib_notification_start(m_connection, &nus_characteristic_rx_uuid);
if (ret) {
fprintf(stderr, "Fail to start notification.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to start notification.");
return 2;
}
@ -130,7 +139,7 @@ int main(int argc, char *argv[]) {
length = MIN(total_length, 20);
ret = gattlib_write_without_response_char_by_handle(m_connection, tx_handle, input_ptr, length);
if (ret) {
fprintf(stderr, "Fail to send data to NUS TX characteristic.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to send data to NUS TX characteristic.");
return 1;
}
input_ptr += length;

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2017 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -27,6 +27,10 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
static uuid_t g_notify_uuid;
@ -76,17 +80,27 @@ int main(int argc, char *argv[]) {
}
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_notification", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
setlogmask(LOG_UPTO(LOG_DEBUG));
#endif
connection = gattlib_connect(NULL, argv[1], GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
return 1;
}
gattlib_register_notification(connection, notification_handler, NULL);
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_notification", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_DEBUG));
#endif
ret = gattlib_notification_start(connection, &g_notify_uuid);
if (ret) {
fprintf(stderr, "Fail to start notification.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to start notification.");
goto DISCONNECT;
}
@ -102,9 +116,9 @@ int main(int argc, char *argv[]) {
if (ret != GATTLIB_SUCCESS) {
if (ret == GATTLIB_NOT_FOUND) {
fprintf(stderr, "Could not find GATT Characteristic with UUID %s.\n", argv[3]);
GATTLIB_LOG(GATTLIB_ERROR, "Could not find GATT Characteristic with UUID %s.", argv[3]);
} else {
fprintf(stderr, "Error while writing GATT Characteristic with UUID %s (ret:%d)\n",
GATTLIB_LOG(GATTLIB_ERROR, "Error while writing GATT Characteristic with UUID %s (ret:%d)",
argv[3], ret);
}
goto DISCONNECT;

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -25,6 +25,10 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
typedef enum { READ, WRITE} operation_t;
@ -47,6 +51,11 @@ int main(int argc, char *argv[]) {
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_read_write", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
if (strcmp(argv[2], "read") == 0) {
g_operation = READ;
} else if ((strcmp(argv[2], "write") == 0) && (argc == 5)) {
@ -70,7 +79,7 @@ int main(int argc, char *argv[]) {
connection = gattlib_connect(NULL, argv[1], GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
return 1;
}
@ -84,10 +93,10 @@ int main(int argc, char *argv[]) {
gattlib_uuid_to_string(&g_uuid, uuid_str, sizeof(uuid_str));
if (ret == GATTLIB_NOT_FOUND) {
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.\n", uuid_str);
GATTLIB_LOG(GATTLIB_ERROR, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.", uuid_str);
} else {
fprintf(stderr, "Error while reading GATT Characteristic with UUID %s (ret:%d)\n", uuid_str, ret);
GATTLIB_LOG(GATTLIB_ERROR, "Error while reading GATT Characteristic with UUID %s (ret:%d)", uuid_str, ret);
}
goto EXIT;
}
@ -107,10 +116,10 @@ int main(int argc, char *argv[]) {
gattlib_uuid_to_string(&g_uuid, uuid_str, sizeof(uuid_str));
if (ret == GATTLIB_NOT_FOUND) {
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.\n", uuid_str);
GATTLIB_LOG(GATTLIB_ERROR, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.", uuid_str);
} else {
fprintf(stderr, "Error while writing GATT Characteristic with UUID %s (ret:%d)\n",
GATTLIB_LOG(GATTLIB_ERROR, "Error while writing GATT Characteristic with UUID %s (ret:%d)",
uuid_str, ret);
}
goto EXIT;

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2021 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -27,6 +27,10 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
#include <syslog.h>
#endif
#include "gattlib.h"
static uuid_t m_uuid;
@ -50,7 +54,7 @@ void *connect_ble(void *arg) {
connection = gattlib_connect(NULL, params->mac_address, GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
GATTLIB_LOG(GATTLIB_ERROR, "Fail to connect to the bluetooth device.");
return NULL;
}
@ -65,10 +69,10 @@ void *connect_ble(void *arg) {
gattlib_uuid_to_string(&m_uuid, uuid_str, sizeof(uuid_str));
if (ret == GATTLIB_NOT_FOUND) {
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.\n", uuid_str);
GATTLIB_LOG(GATTLIB_ERROR, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.", uuid_str);
} else {
fprintf(stderr, "Error while reading GATT Characteristic with UUID %s (ret:%d)\n", uuid_str, ret);
GATTLIB_LOG(GATTLIB_ERROR, "Error while reading GATT Characteristic with UUID %s (ret:%d)", uuid_str, ret);
}
goto EXIT;
}
@ -89,10 +93,10 @@ void *connect_ble(void *arg) {
gattlib_uuid_to_string(&m_uuid, uuid_str, sizeof(uuid_str));
if (ret == GATTLIB_NOT_FOUND) {
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.\n", uuid_str);
GATTLIB_LOG(GATTLIB_ERROR, "Could not find GATT Characteristic with UUID %s. "
"You might call the program with '--gatt-discovery'.", uuid_str);
} else {
fprintf(stderr, "Error while writing GATT Characteristic with UUID %s (ret:%d)\n",
GATTLIB_LOG(GATTLIB_ERROR, "Error while writing GATT Characteristic with UUID %s (ret:%d)",
uuid_str, ret);
}
goto EXIT;
@ -114,6 +118,11 @@ int main(int argc, char *argv[]) {
return 1;
}
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
openlog("gattlib_read_write_memory", LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
struct connect_ble_params params = {
.mac_address = argv[1],
};

View File

@ -119,6 +119,13 @@ extern "C" {
//@}
#define GATTLIB_ERROR 0
#define GATTLIB_WARNING 1
#define GATTLIB_INFO 2
#define GATTLIB_DEBUG 3
#define GATTLIB_LOG(level, args...) if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
typedef struct _gatt_connection_t gatt_connection_t;
typedef struct _gatt_stream_t gatt_stream_t;
@ -670,6 +677,8 @@ int gattlib_string_to_uuid(const char *str, size_t size, uuid_t *uuid);
*/
int gattlib_uuid_cmp(const uuid_t *uuid1, const uuid_t *uuid2);
void gattlib_log(int level, const char *format, ...);
#ifdef __cplusplus
}
#endif