Debian Codegraph @

module/libblueman.c

raw
1 /*
2 * Copyright (C) 2008 Valmantas Paliksa <walmis at balticum-tv dot lt>
3 * Copyright (C) 2008 Tadas Dailyda <tadas at dailyda dot com>
4 *
5 * Licensed under the GNU General Public License Version 3
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <unistd.h>
33 #include <linux/sockios.h>
34 #include <linux/if.h>
35 #include <linux/if_bridge.h>
36
37 #include <bluetooth/bluetooth.h>
38 #include <bluetooth/hci.h>
39 #include <bluetooth/hci_lib.h>
40 #include <bluetooth/rfcomm.h>
41 #include <bluetooth/sdp.h>
42 #include <bluetooth/sdp_lib.h>
43 #include <errno.h>
44
45 #include "libblueman.h"
46
47 static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
48 {
49 unsigned long long jif;
50
51 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
52
53 return jif/10000;
54 }
55
56 int _create_bridge(const char* name) {
57 int sock;
58 sock = socket(AF_INET, SOCK_STREAM, 0);
59 if (sock < 0) {
60 return -errno;
61 }
62
63 int err;
64
65 err = ioctl(sock, SIOCBRADDBR, name);
66 if (err < 0) {
67 close(sock);
68 return -errno;
69 }
70
71
72 struct timeval tv;
73 tv.tv_sec = 0;
74 tv.tv_usec = 1000000 * (0 - tv.tv_sec);
75
76
77 unsigned long args[5];
78 struct ifreq ifr;
79
80 args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
81 args[1] = __tv_to_jiffies(&tv);
82 args[2] = 0;
83 args[3] = 0;
84 args[4] = 0;
85
86 memcpy(ifr.ifr_name, name, IFNAMSIZ);
87 ifr.ifr_data = (char *) &args;
88
89 ioctl(sock, SIOCDEVPRIVATE, &ifr);
90
91 close(sock);
92 return 0;
93 }
94
95
96 int _destroy_bridge(const char* name) {
97 int sock;
98 sock = socket(AF_INET, SOCK_STREAM, 0);
99 if (sock < 0) {
100 return -errno;
101 }
102
103 int err;
104
105 struct ifreq req;
106 memset(&req, 0, sizeof (struct ifreq));
107 strncpy(req.ifr_name, name, IFNAMSIZ);
108
109 err = ioctl(sock, SIOCGIFFLAGS, &req);
110 if (err < 0) {
111 close(sock);
112 return -errno;
113 }
114
115 req.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
116
117 err = ioctl(sock, SIOCSIFFLAGS, &req);
118
119 if (err < 0) {
120 close(sock);
121 return -errno;
122 }
123
124 err = ioctl(sock, SIOCBRDELBR, name);
125 if (err < 0) {
126 close(sock);
127 return -errno;
128 }
129
130 close(sock);
131
132 return 0;
133 }
134
135 static int find_conn(int s, int dev_id, long arg)
136 {
137 struct hci_conn_list_req *cl;
138 struct hci_conn_info *ci;
139 int i;
140 int ret = 0;
141
142 if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl))))
143 goto out;
144
145 cl->dev_id = dev_id;
146 cl->conn_num = 10;
147 ci = cl->conn_info;
148
149 if (ioctl(s, HCIGETCONNLIST, (void *) cl))
150 goto out;
151
152 for (i = 0; i < cl->conn_num; i++, ci++)
153 if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
154 ret = 1;
155 goto out;
156 }
157
158 out:
159 free(cl);
160 return ret;
161 }
162
163
164
165 int connection_init(int dev_id, char *addr, struct conn_info_handles *ci)
166 {
167 struct hci_conn_info_req *cr = NULL;
168 bdaddr_t bdaddr;
169
170 int dd;
171 int ret = 1;
172
173 str2ba(addr, &bdaddr);
174
175 if (dev_id < 0) {
176 dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
177 if (dev_id < 0) {
178 ret = ERR_NOT_CONNECTED;
179 goto out;
180 }
181 }
182
183 dd = hci_open_dev(dev_id);
184 if (dd < 0) {
185 ret = ERR_HCI_DEV_OPEN_FAILED;
186 goto out;
187 }
188
189 cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
190 if (!cr) {
191 ret = ERR_CANNOT_ALLOCATE;
192 goto out;
193 }
194
195 bacpy(&cr->bdaddr, &bdaddr);
196 cr->type = ACL_LINK;
197 if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
198 ret = ERR_GET_CONN_INFO_FAILED;
199 goto out;
200 }
201
202 ci->dd = dd;
203 ci->handle = cr->conn_info->handle;
204
205 out:
206 if (cr)
207 free(cr);
208
209 return ret;
210 }
211
212 int connection_get_rssi(struct conn_info_handles *ci, int8_t *ret_rssi)
213 {
214 int8_t rssi;
215 if (hci_read_rssi(ci->dd, htobs(ci->handle), &rssi, 1000) < 0) {
216 return ERR_READ_RSSI_FAILED;
217 }
218 *ret_rssi = rssi;
219 return 1;
220
221 }
222
223 int connection_get_tpl(struct conn_info_handles *ci, int8_t *ret_tpl, uint8_t type)
224 {
225 int8_t level;
226 if (hci_read_transmit_power_level(ci->dd, htobs(ci->handle), type, &level, 1000) < 0) {
227 return ERR_READ_TPL_FAILED;
228 }
229 *ret_tpl = level;
230 return 1;
231 }
232
233 int connection_close(struct conn_info_handles *ci)
234 {
235 hci_close_dev(ci->dd);
236 return 1;
237 }
238
239 int
240 get_rfcomm_channel(uint16_t service_class, char* btd_addr) {
241 bdaddr_t target;
242 sdp_session_t *session = 0;
243 uuid_t service_uuid;
244 int err;
245 int port_num = 0;
246 sdp_list_t *response_list = NULL, *search_list, *attrid_list;
247
248 str2ba(btd_addr, &target);
249 sdp_uuid16_create(&service_uuid, service_class);
250
251 session = sdp_connect(BDADDR_ANY, &target, SDP_RETRY_IF_BUSY);
252
253 if (!session) {
254 printf("Failed to connect to sdp\n");
255 return port_num;
256 }
257
258 search_list = sdp_list_append(NULL, &service_uuid);
259
260 uint32_t range = 0x0000ffff;
261 attrid_list = sdp_list_append(NULL, &range);
262
263 err = sdp_service_search_attr_req(session, search_list, SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
264
265 if (err) {
266 printf("Failed to search attributes\n");
267 sdp_list_free(response_list, 0);
268 sdp_list_free(search_list, 0);
269 sdp_list_free(attrid_list, 0);
270 return port_num;
271 }
272 // go through each of the service records
273 sdp_list_t *r = response_list;
274 for (; r; r = r->next) {
275 sdp_record_t *rec = (sdp_record_t*) r->data;
276 sdp_list_t *proto_list;
277
278 // get a list of the protocol sequences
279 if(sdp_get_access_protos(rec, &proto_list) == 0) {
280 sdp_list_t *p = proto_list;
281
282 // go through each protocol sequence
283 for(; p ; p = p->next) {
284 sdp_list_t *pds = (sdp_list_t*)p->data;
285
286 // go through each protocol list of the protocol sequence
287 for(; pds ; pds = pds->next) {
288
289 // check the protocol attributes
290 sdp_data_t *d = (sdp_data_t*)pds->data;
291 int proto = 0;
292 for(; d; d = d->next) {
293 switch(d->dtd) {
294 case SDP_UUID16:
295 case SDP_UUID32:
296 case SDP_UUID128:
297 proto = sdp_uuid_to_proto(&d->val.uuid);
298 break;
299 case SDP_UINT8:
300 if(proto == RFCOMM_UUID) {
301 port_num = d->val.int8;
302 printf("rfcomm channel: %d\n", port_num);
303 }
304 break;
305 }
306 }
307 }
308 sdp_list_free((sdp_list_t*)p->data, 0);
309 }
310 sdp_list_free(proto_list, 0);
311 }
312 sdp_record_free(rec);
313 }
314 sdp_close(session);
315 return port_num;
316 }
317
318 int
319 get_rfcomm_list(struct rfcomm_dev_list_req **result)
320 {
321 struct rfcomm_dev_list_req *dl;
322 struct rfcomm_dev_info *di;
323 int ctl = -1;
324 int ret = 1;
325
326 ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
327 if (ctl < 0) {
328 ret = ERR_SOCKET_FAILED; //Can't open RFCOMM control socket
329 goto out;
330 }
331
332 dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
333 if (dl == NULL) {
334 ret = ERR_CANNOT_ALLOCATE;
335 goto out;
336 }
337
338 dl->dev_num = RFCOMM_MAX_DEV;
339 di = dl->dev_info;
340
341 if (ioctl(ctl, RFCOMMGETDEVLIST, (void *) dl) < 0) {
342 ret = ERR_GET_RFCOMM_LIST_FAILED;
343 free(dl);
344 goto out;
345 }
346
347 *result = dl;
348
349 out:
350 if (ctl >= 0)
351 close(ctl);
352 return ret;
353 }
354
355 int create_rfcomm_device(char *local_address, char *remote_address, int channel) {
356 int sk, dev, ret;
357 struct sockaddr_rc laddr, raddr;
358 struct rfcomm_dev_req req;
359
360 sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
361 if (sk < 0) {
362 ret = ERR_SOCKET_FAILED;
363 goto out;
364 }
365
366 laddr.rc_family = AF_BLUETOOTH;
367 str2ba(local_address, &laddr.rc_bdaddr);
368 laddr.rc_channel = 0;
369
370 if (bind(sk, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) {
371 ret = ERR_BIND_FAILED;
372 goto out;
373 }
374
375 raddr.rc_family = AF_BLUETOOTH;
376 str2ba(remote_address, &raddr.rc_bdaddr);
377 raddr.rc_channel = channel;
378
379 if (connect(sk, (struct sockaddr *) &raddr, sizeof(raddr)) < 0) {
380 ret = ERR_CONNECT_FAILED;
381 goto out;
382 }
383
384 memset(&req, 0, sizeof(req));
385 req.flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP);
386 bacpy(&req.src, &laddr.rc_bdaddr);
387 bacpy(&req.dst, &raddr.rc_bdaddr);
388 req.channel = raddr.rc_channel;
389 req.dev_id = -1;
390
391 dev = ioctl(sk, RFCOMMCREATEDEV, &req);
392 if (dev < 0) {
393 ret = ERR_CREATE_DEV_FAILED;
394 } else {
395 ret = dev;
396 }
397
398 out:
399 if (sk >= 0)
400 close(sk);
401 return ret;
402 }
403
404 int release_rfcomm_device(int id) {
405 int sk;
406 struct rfcomm_dev_req req;
407
408 sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
409 if (sk < 0)
410 return ERR_SOCKET_FAILED;
411
412 memset(&req, 0, sizeof(req));
413 req.flags = (1 << RFCOMM_HANGUP_NOW);
414 req.dev_id = id;
415
416 if (ioctl(sk, RFCOMMRELEASEDEV, &req) < 0) {
417 close(sk);
418 return ERR_RELEASE_DEV_FAILED;
419 } else {
420 close(sk);
421 return 0;
422 }
423 }