Skip to content

Commit

Permalink
Introduce Gattlib logging backend
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Oct 18, 2021
1 parent 809a10a commit 1afaa8b
Show file tree
Hide file tree
Showing 23 changed files with 339 additions and 107 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion bluez/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions bluez/gattlib_discover.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions common/gattlib_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand All @@ -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.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/gattlib_eddystone.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions common/logging_backend/printf/gattlib_logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2021, Olivier Martin <[email protected]>
*/

#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);
}
21 changes: 21 additions & 0 deletions common/logging_backend/syslog/gattlib_logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2021, Olivier Martin <[email protected]>
*/

#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);
}
6 changes: 6 additions & 0 deletions dbus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand Down
43 changes: 22 additions & 21 deletions dbus/gattlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 17 additions & 8 deletions dbus/gattlib_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions dbus/gattlib_advertisement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 1afaa8b

Please sign in to comment.