Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build #270

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright (c) 2016-2024, Olivier Martin <[email protected]>
#

cmake_minimum_required(VERSION 3.4)
cmake_minimum_required(VERSION 3.25.1)

# Add Cross-Compilation support when the environment variables
# CROSS_COMPILE and SYSROOT are defined
Expand Down Expand Up @@ -61,7 +61,7 @@ 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)")
set(GATTLIB_LOG_BACKEND syslog CACHE STRING "Define logging backend: syslog, printf, python (default: syslog)")

if (GATTLIB_DBUS)
# Build dbus-based gattlib
Expand Down
10 changes: 5 additions & 5 deletions bluez/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,14 +19,14 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

message("Build gattlib for Bluez v${BLUEZ_VERSION_MAJOR}.${BLUEZ_VERSION_MINOR}")

set(bluez4_DIR bluez4)
set(bluez5_DIR bluez5)
set(bluez5_DIR bluez5)

# Bluez specific files
set(bluez4_SRCS ${bluez4_DIR}/attrib/att.c
Expand Down Expand Up @@ -109,12 +109,12 @@ include(GNUInstallDirs)
if(GATTLIB_SHARED_LIB)
add_library(gattlib SHARED ${gattlib_SRCS})
target_link_libraries(gattlib ${gattlib_LIBS})

install(TARGETS gattlib LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
else()
add_library(gattlib ${gattlib_SRCS})
target_include_directories(gattlib INTERFACE ../include)
target_link_libraries(gattlib ${gattlib_LIBS})

install(TARGETS gattlib LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
56 changes: 56 additions & 0 deletions common/logging_backend/python/gattlib_logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
*
* Copyright (c) 2024, Olivier Martin <[email protected]>
*/

#include <syslog.h>

#include "gattlib_internal.h"

static PyObject* m_logging_func;

void gattlib_log_init(PyObject* logging_func) {
m_logging_func = logging_func;
}

void gattlib_log(int level, const char *format, ...) {
va_list args;
va_start(args, format);

if (m_logging_func == NULL) {
FILE *stream = stdout;

if (level == GATTLIB_ERROR) {
stream = stderr;
}

vfprintf(stream, format, args);
fprintf(stream, "\n");
} else {
PyGILState_STATE d_gstate;
PyObject *result;
char string[400];

vsnprintf(string, sizeof(string), format, args);

d_gstate = PyGILState_Ensure();

PyObject *arglist = Py_BuildValue("Is", level, string);
#if PYTHON_VERSION >= PYTHON_VERSIONS(3, 9)
result = PyObject_Call(m_logging_func, arglist, NULL);
#else
result = PyEval_CallObject(m_logging_func, arglist);
#endif
Py_DECREF(arglist);

if (result == NULL) {
GATTLIB_LOG(GATTLIB_ERROR, "Python notification handler has raised an exception.");
PyErr_Print();
}

PyGILState_Release(d_gstate);
}

va_end(args);
}
17 changes: 11 additions & 6 deletions dbus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright (c) 2016-2024, Olivier Martin <[email protected]>
#

cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down Expand Up @@ -103,12 +103,17 @@ if(GATTLIB_PYTHON_INTERFACE)
set(Python_USE_STATIC_LIBS TRUE)
endif()
find_package(Python COMPONENTS Interpreter Development)
if (Python_Development_FOUND)
include_directories(${Python_INCLUDE_DIRS})
list(APPEND gattlib_LIBS ${Python_LIBRARIES})

add_definitions(-DWITH_PYTHON -DPYTHON_VERSION_MAJOR=${Python_VERSION_MAJOR} -DPYTHON_VERSION_MINOR=${Python_VERSION_MINOR})
if (NOT Python_Development_FOUND)
find_package(Python COMPONENTS Development.Module)
if (NOT Python_Development.Module_FOUND)
message(FATAL_ERROR "Could not find Python developer package")
endif()
endif()

include_directories(${Python_INCLUDE_DIRS})
list(APPEND gattlib_LIBS ${Python_LIBRARIES})

add_definitions(-DWITH_PYTHON -DPYTHON_VERSION_MAJOR=${Python_VERSION_MAJOR} -DPYTHON_VERSION_MINOR=${Python_VERSION_MINOR})
endif()

# Gattlib
Expand Down
4 changes: 2 additions & 2 deletions examples/advertisement_data/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/ble_scan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/discover/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/find_eddystone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/gatttool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/nordic_uart/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/notification/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/read_write/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions examples/read_write_memory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2021 Olivier Martin <[email protected]>
# Copyright (C) 2016-2024 Olivier Martin <[email protected]>
#
#
# This program is free software; you can redistribute it and/or modify
Expand All @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.25.1)

find_package(PkgConfig REQUIRED)

Expand Down
22 changes: 22 additions & 0 deletions gattlib-py/gattlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@

gattlib = CDLL("libgattlib.so")

def native_logging(level: int, string: str):
if level == 3:
logger.debug(string)
elif level == 2:
logger.info(string)
elif level == 1:
logger.warning(string)
elif level == 0:
logger.error(string)
else:
logger.debug(string)

try:
# void gattlib_log_init(PyObject* logging_func)
gattlib_log_init = gattlib.gattlib_log_init
gattlib_log_init.argtypes = [py_object]

# Declare Python function for logging native string
gattlib_log_init(native_logging)
except AttributeError:
# Excepted when using a Gattlib logging backend without 'gattlib_log_init'
pass

# typedef struct {
# uint8_t data[16];
Expand Down
5 changes: 4 additions & 1 deletion gattlib-py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def build_extension(self, ext: CMakeExtension) -> None:
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]

# In this example, we pass in the version to C++. You might not need to.
cmake_args += [f"-DEXAMPLE_VERSION_INFO={self.distribution.get_version()}", "-DGATTLIB_BUILD_EXAMPLES=NO"]
cmake_args += [
f"-DEXAMPLE_VERSION_INFO={self.distribution.get_version()}",
"-DGATTLIB_BUILD_EXAMPLES=NO",
"-DGATTLIB_LOG_BACKEND=python"]

if self.compiler.compiler_type != "msvc":
# Using Ninja-build since it a) is available as a wheel and b)
Expand Down
Loading