2016-04-28 01:22:00 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* GattLib - GATT Library
|
|
|
|
*
|
2024-03-27 22:07:16 +01:00
|
|
|
* Copyright (C) 2016-2024 Olivier Martin <olivier@labapart.org>
|
2016-04-28 01:22:00 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-03-27 22:07:16 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <pthread.h>
|
2016-04-28 01:22:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-10-18 01:25:29 +02:00
|
|
|
#ifdef GATTLIB_LOG_BACKEND_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
|
|
|
|
2016-04-28 01:22:00 +02:00
|
|
|
#include "gattlib.h"
|
|
|
|
|
2024-03-27 22:07:16 +01:00
|
|
|
#define BLE_SCAN_TIMEOUT 10
|
|
|
|
|
|
|
|
static const char* adapter_name = NULL;
|
|
|
|
|
|
|
|
// Declaration of thread condition variable
|
|
|
|
static pthread_cond_t m_connection_terminated = PTHREAD_COND_INITIALIZER;
|
|
|
|
|
|
|
|
// declaring mutex
|
|
|
|
static pthread_mutex_t m_connection_terminated_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
static void on_device_connect(void *adapter, const char *dst, gatt_connection_t* connection, int error, void* user_data) {
|
2016-04-28 01:22:00 +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-27 22:07:16 +01:00
|
|
|
GATTLIB_LOG(GATTLIB_INFO, "Connected to bluetooth device '%s'", dst);
|
2016-04-28 01:22:00 +02:00
|
|
|
|
|
|
|
ret = gattlib_discover_primary(connection, &services, &services_count);
|
2019-06-26 17:34:57 +02:00
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover primary services.");
|
2024-03-27 22:07:16 +01:00
|
|
|
goto EXIT;
|
2016-04-28 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < services_count; i++) {
|
|
|
|
gattlib_uuid_to_string(&services[i].uuid, uuid_str, sizeof(uuid_str));
|
|
|
|
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_INFO, "service[%d] start_handle:%02x end_handle:%02x uuid:%s", i,
|
2016-04-28 01:22:00 +02:00
|
|
|
services[i].attr_handle_start, services[i].attr_handle_end,
|
|
|
|
uuid_str);
|
|
|
|
}
|
|
|
|
free(services);
|
|
|
|
|
|
|
|
ret = gattlib_discover_char(connection, &characteristics, &characteristics_count);
|
2019-06-26 17:34:57 +02:00
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Fail to discover characteristics.");
|
2024-03-27 22:07:16 +01:00
|
|
|
goto EXIT;
|
2016-04-28 01:22:00 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < characteristics_count; i++) {
|
|
|
|
gattlib_uuid_to_string(&characteristics[i].uuid, uuid_str, sizeof(uuid_str));
|
|
|
|
|
2021-10-18 01:25:29 +02:00
|
|
|
GATTLIB_LOG(GATTLIB_INFO, "characteristic[%d] properties:%02x value_handle:%04x uuid:%s", i,
|
2016-04-28 01:22:00 +02:00
|
|
|
characteristics[i].properties, characteristics[i].value_handle,
|
|
|
|
uuid_str);
|
|
|
|
}
|
|
|
|
free(characteristics);
|
|
|
|
|
2024-03-27 22:07:16 +01:00
|
|
|
EXIT:
|
2024-03-28 11:52:57 +01:00
|
|
|
gattlib_disconnect(connection, false /* wait_disconnection */);
|
2024-03-27 22:07:16 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&m_connection_terminated_lock);
|
|
|
|
pthread_cond_signal(&m_connection_terminated);
|
|
|
|
pthread_mutex_unlock(&m_connection_terminated_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stricmp(char const *a, char const *b) {
|
|
|
|
for (;; a++, b++) {
|
|
|
|
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
|
|
|
|
if (d != 0 || !*a)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ble_discovered_device(void *adapter, const char* addr, const char* name, void *user_data) {
|
|
|
|
const char* reference_mac_address = user_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (stricmp(addr, reference_mac_address) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GATTLIB_LOG(GATTLIB_INFO, "Found bluetooth device '%s'", reference_mac_address);
|
|
|
|
|
|
|
|
ret = gattlib_connect(adapter, 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'", addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* ble_task(void* arg) {
|
|
|
|
char* addr = arg;
|
|
|
|
void* adapter;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gattlib_adapter_open(adapter_name, &adapter);
|
|
|
|
if (ret) {
|
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to open adapter.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT, addr);
|
|
|
|
if (ret) {
|
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to scan.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the device to be connected
|
|
|
|
pthread_mutex_lock(&m_connection_terminated_lock);
|
|
|
|
pthread_cond_wait(&m_connection_terminated, &m_connection_terminated_lock);
|
|
|
|
pthread_mutex_unlock(&m_connection_terminated_lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char* device_address;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_address = argv[1];
|
|
|
|
|
|
|
|
ret = gattlib_mainloop(ble_task, device_address);
|
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
|
|
|
GATTLIB_LOG(GATTLIB_ERROR, "Failed to create gattlib mainloop");
|
|
|
|
}
|
|
|
|
|
2016-04-28 01:22:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|