I am currently implementing a BLE server using GATT
api from bluez5
in C. I need to use my own service with custom characteristics.
Problem is that bluez5
does not install all header of GATT
api. Same problem in libbluetooth
with does not provide all external GATT
api.
Am I using the wrong api ? What is the tips to compile my code ?
Current dirty solution is to replace btgatt-server.c
in tools directory of bluez
source by my own code to be able to dev/test
my implementation.
EDIT :
I am using the last stable version of bluez
: 5.32
headers from bluez
that I need to compile my code :
#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"
#include "lib/l2cap.h"
#include "lib/uuid.h"
#include "src/shared/mainloop.h"
#include "src/shared/util.h"
#include "src/shared/att.h"
#include "src/shared/queue.h"
#include "src/shared/timeout.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-server.h"
functions :
[arthur ] make 2>&1 | grep gatt_ | grep implicit
tools/btgatt-server.c:32:2: error: implicit declaration of function ‘gatt_db_attribute_read_result’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:60:2: error: implicit declaration of function ‘gatt_db_add_service’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:67:2: error: implicit declaration of function ‘gatt_db_service_add_characteristic’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:94:2: error: implicit declaration of function ‘gatt_db_service_set_active’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:110:2: error: implicit declaration of function ‘bt_gatt_server_unref’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:111:2: error: implicit declaration of function ‘gatt_db_unref’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:186:2: error: implicit declaration of function ‘gatt_db_new’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:192:2: error: implicit declaration of function ‘bt_gatt_server_new’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:202:2: error: implicit declaration of function ‘bt_gatt_server_set_debug’ [-Werror=implicit-function-declaration]
These includes are not install by the Makefile of bluez
on my system. And the library file does not include the function that I need either.
I will probably used dbus
API later. But right now I am always using low c API from bluez5
(path of example from bluez5 : bluez5_utils-5.31/tools/btgatt-server.c). To compile this standalone code outside bluez5 source directory I am using some include/library from bluez5 :
In my case I am working with buildroot. So I add some piece of code in bluez5 mk file to get this missing library :
define BLUEZ5_UTILS_EXTERNALIZE_GATT
mkdir -p $(STAGING_DIR)/usr/local/bluez5/gatt
cp $(BLUEZ5_UTILS_SRCDIR)/src/uuid-helper.o $(STAGING_DIR)/usr/local/bluez5/gatt/.
cp $(BLUEZ5_UTILS_SRCDIR)/src/.libs/libshared-mainloop.a $(STAGING_DIR)/usr/local/bluez5/gatt/.
cp $(BLUEZ5_UTILS_SRCDIR)/lib/.libs/libbluetooth-internal.a $(STAGING_DIR)/usr/local/bluez5/gatt/.
$(INSTALL) -D -m 0755 $(@D)/tools/btmgmt $(STAGING_DIR)/usr/bin/btmgmt
$(INSTALL) -D -m 0755 $(@D)/tools/btmgmt $(TARGET_DIR)/usr/bin/btmgmt
endef
Then in my Makefile I just need to link with these libraries + add -I to find missing header file which are in bluez5 source directory :
# find bluez5 header
HEADER += -I$(BLUEZ_PATH)
# link bluez5 libraries
BLUEZ5_LIB = $(STAGING_DIR)/usr/local/bluez5/gatt/libshared-mainloop.a BLUEZ5_LIB += $(STAGING_DIR)/usr/local/bluez5/gatt/libbluetooth-internal.a
Anyway this is probably not the best way to do it but it is working great. Moreover you don't need to do anything if you upgrade your bluez5 version
. I find some other solution like fork bluez5 and put some modification in Makefile to build a static library which contain all stuff that I need. But this solution is really painfully and you need to update your work at each new bluez5 release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With