forked from infsoft-locaware/blzlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan-discover.c
More file actions
107 lines (84 loc) · 2.32 KB
/
scan-discover.c
File metadata and controls
107 lines (84 loc) · 2.32 KB
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
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "blzlib.h"
#include "blzlib_log.h"
#include "blzlib_util.h"
#define MAX_SCAN 10
static bool terminate = false;
static uint8_t scanned_macs[MAX_SCAN][6];
static int scan_idx = 0;
static void discover(blz_ctx* blz, const char* mac)
{
LOG_INF("Connecting to %s...", mac);
blz_dev* dev = blz_connect(blz, mac, BLZ_ADDR_UNKNOWN);
if (!dev) {
return;
}
char** uuids = blz_list_service_uuids(dev);
for (int i = 0; uuids != NULL && uuids[i] != NULL; i++) {
LOG_INF("\tService %s", uuids[i]);
blz_serv* srv = blz_get_serv_from_uuid(dev, uuids[i]);
if (srv) {
char** chuuids = blz_list_char_uuids(srv);
for (int i = 0; chuuids != NULL && chuuids[i] != NULL; i++) {
LOG_INF("\t\tCharacteristic %s", chuuids[i]);
}
blz_serv_free(srv);
}
}
blz_disconnect(dev);
}
static void scan_cb(const uint8_t* mac, enum blz_addr_type atype, int8_t rssi,
const uint8_t* data, size_t len, void* user)
{
/* Note: you can't connect in the scan callback! */
LOG_INF("SCAN " MAC_FMT " %d %zd", MAC_PARR(mac), rssi, len);
if (data && len > 0) {
hex_dump("DATA: ", data, len);
}
for (int i = 0; i < MAX_SCAN && scanned_macs[i] != NULL; i++) {
if (memcmp(scanned_macs[i], mac, 6) == 0) {
return; // already in list
}
}
memcpy(scanned_macs[scan_idx++], mac, 6);
if (scan_idx >= MAX_SCAN) {
terminate = true;
}
}
static void signal_handler(__attribute__((unused)) int signo)
{
terminate = true;
}
int main(int argc, char** argv)
{
/* register the signal SIGINT handler */
struct sigaction act;
act.sa_handler = signal_handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
blz_ctx* blz = blz_init("hci0");
if (!blz) {
return EXIT_FAILURE;
}
if (argc > 1) {
for (int i = 1; i < argc; i++) {
discover(blz, argv[i]);
}
} else {
LOG_INF("Cached devices...");
blz_known_devices(blz, scan_cb, NULL);
LOG_INF("Scanning for 10 seconds... press Ctrl-C to cancel...");
blz_scan_start(blz, scan_cb, NULL);
blz_loop_wait(blz, &terminate, 10000);
blz_scan_stop(blz);
/* connect to device to discover services and characteristics */
for (int i = 0; i < MAX_SCAN && MAC_NOT_EMPTY(scanned_macs[i]); i++) {
discover(blz, blz_mac_to_string_s(scanned_macs[i]));
}
}
blz_fini(blz);
return EXIT_SUCCESS;
}