-
Notifications
You must be signed in to change notification settings - Fork 9
/
bt_utils.c
223 lines (175 loc) · 3.55 KB
/
bt_utils.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
/*
Copyright (c) 2010 Mathieu Laurendeau <[email protected]>
License: GPLv3
*/
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#define HCI_REQ_TIMEOUT 1000
/*
* \brief This function gets the bluetooth device address for a given device number.
*
* \param device_number the device number
* \param bdaddr the buffer to store the bluetooth device address
*
* \return 0 if successful, -1 otherwise
*/
int bt_get_device_bdaddr(int device_number, char bdaddr[18])
{
int ret = 0;
bdaddr_t bda;
int s = hci_open_dev (device_number);
if(hci_read_bd_addr(s, &bda, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
else
{
ba2str(&bda, bdaddr);
}
close(s);
return ret;
}
int get_device_id(char* bdaddr)
{
int id = 0;
if(bdaddr)
{
id = hci_devid(bdaddr);
}
else
{
id = hci_get_route(NULL);
}
return id;
}
/*
* \brief This function writes the bluetooth device class for a given device number.
*
* \param device_number the device number
*
* \return 0 if successful, -1 otherwise
*/
int bt_write_device_class(char* bdaddr, uint32_t class)
{
int ret = 0;
int id = get_device_id(bdaddr);
if(id < 0)
{
return -1;
}
int s = hci_open_dev (id);
if(hci_write_class_of_dev(s, class, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
close(s);
return ret;
}
int delete_stored_link_key(char* bdaddr, char* bdaddr_dest)
{
int ret = 0;
int id = get_device_id(bdaddr);
if(id < 0)
{
return -1;
}
int s = hci_open_dev (id);
bdaddr_t bda;
str2ba(bdaddr_dest, &bda);
if(hci_delete_stored_link_key(s, &bda, 0, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
close(s);
return ret;
}
int write_stored_link_key(char* bdaddr, char* bdaddr_dest, unsigned char* key)
{
int ret = 0;
int id = get_device_id(bdaddr);
if(id < 0)
{
return -1;
}
int s = hci_open_dev (id);
bdaddr_t bda;
str2ba(bdaddr_dest, &bda);
if(hci_write_stored_link_key(s, &bda, key, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
close(s);
return ret;
}
int authenticate_link(char* bdaddr_dest)
{
int ret = 0;
int err = 0, dd;
struct hci_conn_info_req *cr = 0;
// find the connection handle to the specified bluetooth device
cr = (struct hci_conn_info_req*) malloc(
sizeof(struct hci_conn_info_req) +
sizeof(struct hci_conn_info));
str2ba(bdaddr_dest, &cr->bdaddr);
cr->type = ACL_LINK;
dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );
if( dd < 0 ) {
ret = -1;
}
else
{
err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr );
if( !err )
{
if(hci_authenticate_link(dd, cr->conn_info->handle, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
}
else
{
ret = -1;
}
}
free(cr);
close(dd);
return ret;
}
int encrypt_link(char* bdaddr_dest)
{
int ret = 0;
int err = 0, dd;
struct hci_conn_info_req *cr = 0;
// find the connection handle to the specified bluetooth device
cr = (struct hci_conn_info_req*) malloc(
sizeof(struct hci_conn_info_req) +
sizeof(struct hci_conn_info));
str2ba(bdaddr_dest, &cr->bdaddr);
cr->type = ACL_LINK;
dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );
if( dd < 0 ) {
ret = -1;
}
else
{
err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr );
if( !err )
{
if(hci_encrypt_link(dd, cr->conn_info->handle, 0x01, HCI_REQ_TIMEOUT) < 0)
{
ret = -1;
}
}
else
{
ret = -1;
}
}
free(cr);
close(dd);
return ret;
}