forked from labapart/gattlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathble_scan.c
159 lines (130 loc) · 4.2 KB
/
ble_scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/queue.h>
#include "gattlib.h"
#define BLE_SCAN_TIMEOUT 4
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;
char* addr;
LIST_ENTRY(connection_t) entries;
};
static void *ble_connect_device(void *arg) {
struct connection_t *connection = arg;
char* addr = connection->addr;
gatt_connection_t* gatt_connection;
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;
pthread_mutex_lock(&g_mutex);
printf("------------START %s ---------------\n", addr);
gatt_connection = gattlib_connect(NULL, addr, BDADDR_LE_PUBLIC, BT_SEC_LOW, 0, 0);
if (gatt_connection == NULL) {
gatt_connection = gattlib_connect(NULL, addr, BDADDR_LE_RANDOM, BT_SEC_LOW, 0, 0);
if (gatt_connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
goto connection_exit;
} else {
puts("Succeeded to connect to the bluetooth device with random address.");
}
} else {
puts("Succeeded to connect to the bluetooth device.");
}
ret = gattlib_discover_primary(gatt_connection, &services, &services_count);
if (ret != 0) {
fprintf(stderr, "Fail to discover primary services.\n");
goto disconnect_exit;
}
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);
ret = gattlib_discover_char(gatt_connection, &characteristics, &characteristics_count);
if (ret != 0) {
fprintf(stderr, "Fail to discover characteristics.\n");
goto disconnect_exit;
}
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);
disconnect_exit:
gattlib_disconnect(gatt_connection);
connection_exit:
printf("------------DONE %s ---------------\n", addr);
pthread_mutex_unlock(&g_mutex);
return NULL;
}
static void ble_discovered_device(const char* addr, const char* name) {
struct connection_t *connection;
int ret;
if (name) {
printf("Discovered %s - '%s'\n", addr, name);
} else {
printf("Discovered %s\n", addr);
}
connection = malloc(sizeof(struct connection_t));
if (connection == NULL) {
fprintf(stderr, "Failt to allocate connection.\n");
return;
}
connection->addr = strdup(addr);
ret = pthread_create(&connection->thread, NULL, ble_connect_device, connection);
if (ret != 0) {
fprintf(stderr, "Failt to create BLE connection thread.\n");
free(connection);
return;
}
LIST_INSERT_HEAD(&g_ble_connections, connection, entries);
}
int main(int argc, const char *argv[]) {
const char* adapter_name;
void* adapter;
int ret;
if (argc == 1) {
adapter_name = NULL;
} else if (argc == 2) {
adapter_name = argv[1];
} else {
fprintf(stderr, "%s [<bluetooth-adapter>]\n", argv[0]);
return 1;
}
LIST_INIT(&g_ble_connections);
ret = gattlib_adapter_open(adapter_name, &adapter);
if (ret) {
fprintf(stderr, "ERROR: Failed to open adapter.\n");
return 1;
}
pthread_mutex_lock(&g_mutex);
ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, BLE_SCAN_TIMEOUT);
if (ret) {
fprintf(stderr, "ERROR: Failed to scan.\n");
return 1;
}
gattlib_adapter_scan_disable(adapter);
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);
}
gattlib_adapter_close(adapter);
return 0;
}