2021-10-18 01:25:29 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* GattLib - GATT Library
|
|
|
|
*
|
2024-03-17 15:24:32 +01:00
|
|
|
* Copyright (C) 2021-2024 Olivier Martin <olivier@labapart.org>
|
2021-10-18 01:25:29 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-02 10:02:39 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
2021-10-18 01:25:29 +02:00
|
|
|
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-02 10:02:39 +02:00
|
|
|
#include "gattlib.h"
|
|
|
|
|
2024-03-17 15:24:32 +01:00
|
|
|
#define BLE_SCAN_TIMEOUT 10
|
|
|
|
|
|
|
|
static const char* adapter_name;
|
2016-05-02 10:02:39 +02:00
|
|
|
|
|
|
|
typedef void (*ble_discovered_device_t)(const char* addr, const char* name);
|
|
|
|
|
|
|
|
// We use a mutex to make the BLE connections synchronous
|
|
|
|
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
LIST_HEAD(listhead, connection_t) g_ble_connections;
|
|
|
|
struct connection_t {
|
|
|
|
pthread_t thread;
|
2024-03-17 15:24:32 +01:00
|
|
|
void *adapter;
|
2016-05-02 10:02:39 +02:00
|
|
|
char* addr;
|
|
|
|
LIST_ENTRY(connection_t) entries;
|
|
|
|
};
|
|
|
|
|
2024-03-17 15:24:32 +01:00
|
|
|
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
2016-05-02 10:02:39 +02:00
|
|
|
gattlib_primary_service_t* services;
|
|
|
|
gattlib_characteristic_t* characteristics;
|
|
|
|
int services_count, characteristics_count;
|
|
|
|
char uuid_str[MAX_LEN_UUID_STR + 1];
|
|
|
|
int ret, i;
|
|
|
|
|
2024-03-17 15:24:32 +01:00
|
|
|
ret = gattlib_discover_primary(connection, &services, &services_count);
|
2016-05-02 10:02:39 +02:00
|
|
|
if (ret != 0) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover primary services.");
|
2017-03-15 23:46:28 +01:00
|
|
|
goto disconnect_exit;
|
2016-05-02 10:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
services[i].attr_handle_start, services[i].attr_handle_end,
|
|
|
|
uuid_str);
|
|
|
|
}
|
|
|
|
free(services);
|
|
|
|
|
2024-03-17 15:24:32 +01:00
|
|
|
ret = gattlib_discover_char(connection, &characteristics, &characteristics_count);
|
2016-05-02 10:02:39 +02:00
|
|
|
if (ret != 0) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristics.");
|
2017-03-15 23:46:28 +01:00
|
|
|
goto disconnect_exit;
|
2016-05-02 10:02:39 +02:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
characteristics[i].properties, characteristics[i].value_handle,
|
|
|
|
uuid_str);
|
|
|
|
}
|
|
|
|
free(characteristics);
|
|
|
|
|
2017-03-15 23:46:28 +01:00
|
|
|
disconnect_exit:
|
2024-03-28 11:52:57 +01:00
|
|
|
gattlib_disconnect(connection, false /* wait_disconnection */);
|
2024-03-17 15:24:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *ble_connect_device(void *arg) {
|
|
|
|
struct connection_t *connection = arg;
|
|
|
|
char* addr = connection->addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_mutex);
|
|
|
|
printf("------------START %s ---------------\n", addr);
|
|
|
|
|
|
|
|
ret = gattlib_connect(connection->adapter, connection->addr, GATTLIB_CONNECTION_OPTIONS_NONE, on_device_connect, NULL);
|
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect to the bluetooth device '%s'", connection->addr);
|
|
|
|
}
|
2016-05-02 10:02:39 +02:00
|
|
|
|
|
|
|
printf("------------DONE %s ---------------\n", addr);
|
|
|
|
pthread_mutex_unlock(&g_mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:49:07 +02:00
|
|
|
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
2016-05-02 10:02:39 +02:00
|
|
|
struct connection_t *connection;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
printf("Discovered %s - '%s'\n", addr, name);
|
|
|
|
} else {
|
|
|
|
printf("Discovered %s\n", addr);
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:26:22 +01:00
|
|
|
connection = calloc(sizeof(struct connection_t), 1);
|
2016-05-02 10:02:39 +02:00
|
|
|
if (connection == NULL) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failt to allocate connection.");
|
2016-05-02 10:02:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
connection->addr = strdup(addr);
|
2024-03-17 15:24:32 +01:00
|
|
|
connection->adapter = adapter;
|
2016-05-02 10:02:39 +02:00
|
|
|
|
|
|
|
ret = pthread_create(&connection->thread, NULL, ble_connect_device, connection);
|
|
|
|
if (ret != 0) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failt to create BLE connection thread.");
|
2016-05-02 10:02:39 +02:00
|
|
|
free(connection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LIST_INSERT_HEAD(&g_ble_connections, connection, entries);
|
|
|
|
}
|
|
|
|
|
2024-03-17 15:24:32 +01:00
|
|
|
static void* ble_task(void* arg) {
|
2017-03-14 18:41:39 +01:00
|
|
|
void* adapter;
|
2016-05-02 10:02:39 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-03-14 18:41:39 +01:00
|
|
|
ret = gattlib_adapter_open(adapter_name, &adapter);
|
|
|
|
if (ret) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open adapter.");
|
2024-03-17 15:24:32 +01:00
|
|
|
return NULL;
|
2016-05-02 10:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_mutex);
|
2019-07-09 18:49:07 +02:00
|
|
|
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT, NULL /* user_data */);
|
2017-03-14 18:41:39 +01:00
|
|
|
if (ret) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to scan.");
|
2019-05-21 21:27:35 +02:00
|
|
|
goto EXIT;
|
2016-05-02 10:02:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 18:41:39 +01:00
|
|
|
gattlib_adapter_scan_disable(adapter);
|
2016-05-02 10:02:39 +02:00
|
|
|
|
|
|
|
puts("Scan completed");
|
|
|
|
pthread_mutex_unlock(&g_mutex);
|
|
|
|
|
|
|
|
// Wait for the thread to complete
|
|
|
|
while (g_ble_connections.lh_first != NULL) {
|
|
|
|
struct connection_t* connection = g_ble_connections.lh_first;
|
|
|
|
pthread_join(connection->thread, NULL);
|
|
|
|
LIST_REMOVE(g_ble_connections.lh_first, entries);
|
|
|
|
free(connection->addr);
|
|
|
|
free(connection);
|
|
|
|
}
|
|
|
|
|
2019-05-21 21:27:35 +02:00
|
|
|
EXIT:
|
2017-03-14 18:41:39 +01:00
|
|
|
gattlib_adapter_close(adapter);
|
2024-03-17 15:24:32 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char *argv[]) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
adapter_name = NULL;
|
|
|
|
} else if (argc == 2) {
|
|
|
|
adapter_name = argv[1];
|
|
|
|
} else {
|
|
|
|
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_mainloop(ble_task, NULL);
|
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to create gattlib mainloop");
|
|
|
|
}
|
|
|
|
|
2019-05-21 21:27:35 +02:00
|
|
|
return ret;
|
2016-05-02 10:02:39 +02:00
|
|
|
}
|