/
cbeacon.c
282 lines (253 loc) · 6.76 KB
/
cbeacon.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdlib.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <stdio.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include "cbeacon.h"
#define SNAP_LEN HCI_MAX_FRAME_SIZE
struct hcidump_hdr {
uint16_t len;
uint8_t in;
uint8_t pad;
uint32_t ts_sec;
uint32_t ts_usec;
} __attribute__ ((packed));
#define HCIDUMP_HDR_SIZE (sizeof(struct hcidump_hdr))
/*****************************************************************************************************************************
* Global variables
*******************************************************************************************************************************/
static int device_handle=-1;
static cbeaconCallBack cbeacon_cb = NULL; //
static bdaddr_t g_mac_address;
static int g_sd=-1;
/*****************************************************************************************************************************
* Prototypes
*******************************************************************************************************************************/
static int open_socket(int dev);
static int start_lescan(int device_id);
static int stop_lescan(int device_handle);
static int process_frames(int dev, bool nb);
/***********************************************************************************************************/
/**
* @brief Open Socket
*
* @param dev Device handle
*
* @return
*/
static int open_socket(int dev)
{
struct sockaddr_hci addr;
struct hci_filter flt;
struct hci_dev_info di;
int sk, opt;
if (dev != HCI_DEV_NONE) {
int dd = hci_open_dev(dev);
if (dd < 0) {
perror("Can't open device");
return -1;
}
if (hci_devinfo(dev, &di) < 0) {
perror("Can't get device info");
return -1;
}
if (hci_read_bd_addr(dd, &g_mac_address, 1000) < 0 ) {
perror("Can't get device mac address");
return -1;
}
opt = hci_test_bit(HCI_RAW, &di.flags);
if (ioctl(dd, HCISETRAW, opt) < 0) {
if (errno == EACCES) {
perror("Can't access device");
return -1;
}
}
hci_close_dev(dd);
}
/* Create HCI socket */
sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
if (sk < 0) {
perror("Can't create raw socket");
return -1;
}
opt = 1;
if (setsockopt(sk, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
perror("Can't enable data direction info");
return -1;
}
opt = 1;
if (setsockopt(sk, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
perror("Can't enable time stamp");
return -1;
}
/* Setup filter */
hci_filter_clear(&flt);
hci_filter_all_ptypes(&flt);
hci_filter_all_events(&flt);
if (setsockopt(sk, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
perror("Can't set filter");
return -1;
}
/* Bind socket to the HCI device */
memset(&addr, 0, sizeof(addr));
addr.hci_family = AF_BLUETOOTH;
addr.hci_dev = dev;
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "Can't attach to device hci%d. %s(%d)\n",
dev, strerror(errno), errno);
return -1;
}
return sk;
}
/**
* @brief Stop low energy scan
*
* @param device_handle
*
* @return
*/
static int stop_lescan(int device_handle)
{
uint8_t filter_dup = 0;
int err = hci_le_set_scan_enable(device_handle, 0x00, filter_dup, 1000);
if (err < 0) {
perror("Disable scan failed");
return -1;
}
hci_close_dev(device_handle);
return 0;
}
/**
* @brief Start low energy scan
*
* @param device_id
*
* @return
*/
static int start_lescan(int device_id)
{
int device_handle = 0;
if((device_handle = hci_open_dev(device_id)) < 0)
{
perror("Could not open device");
return -1;
}
uint8_t filter_policy = 0x00;
uint16_t interval = htobs(0x0010);
uint16_t window = htobs(0x0010);
int err = hci_le_set_scan_parameters(device_handle, 0x01, interval, window, 0x00, filter_policy, 1000);
if (err < 0) {
perror("Set scan parameters failed");
return -1;
}
err = hci_le_set_scan_enable(device_handle, 0x01, 0, 1000);
if (err < 0) {
perror("Enable scan failed");
return -1;
}
return device_handle;
}
/**
* @brief Process frames
*
* @param dev Device handle
* @param sock Socket number
* @param nb Non blocking flag
*
* @return
*/
static int process_frames(int dev, bool nb)
{
if (g_sd < 0) return -1;
char buf[SNAP_LEN + HCIDUMP_HDR_SIZE];// = malloc(snap_len + HCIDUMP_HDR_SIZE);
while (device_handle!=-1) // Loop in blocking mode
{
void *data = buf + HCIDUMP_HDR_SIZE;
fd_set set;
FD_ZERO(&set);
FD_SET(g_sd, &set);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec=100000;
int rv = select (g_sd+1, &set, NULL, NULL, &timeout);
struct iovec iv;
iv.iov_base = data;
iv.iov_len = SNAP_LEN;
struct msghdr msg;
char ctrl[128];
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = 100;
if (rv > 0)
{
int len = recvmsg(g_sd, &msg, MSG_DONTWAIT);
if (len <= 0)
{
if (errno == EAGAIN || errno == EINTR)
continue;
perror("Receive failed");
return -1;
} else {
if ( cbeacon_cb != NULL ) cbeacon_cb( (int8_t*)data, len);
}
}
if (nb) break;
}
return 0;
}
/**
* @brief
*
* @return
*/
int cbeacon_init()
{
int device_id = hci_get_route(NULL); // Get device
device_handle = start_lescan(device_id);
g_sd = open_socket(0);
return device_handle;
}
/**
* @brief
*
* @param cb
*
* @return
*/
int cbeacon_setcb(cbeaconCallBack cb)
{
cbeacon_cb = cb;
return 0;
}
/**
* @brief Start beacon scan
*
* @return
*/
int cbeacon_start(bool nb)
{
return process_frames(0, nb);
}
/**
* @brief Get mac address of BLE device
*
* @param mac_address[6]
*/
void cbeacon_get_mac_address(int8_t mac_address[6])
{
memcpy(mac_address, (int8_t *)&g_mac_address, 6);
}
/**
* @brief Stop beacon scan
*/
void cbeacon_stop()
{
if (device_handle != -1) stop_lescan(device_handle);
cbeacon_cb = NULL;
}