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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
/*
* Copyright (C) 2008 Valmantas Paliksa <walmis at balticum-tv dot lt>
* Copyright (C) 2008 Tadas Dailyda <tadas at dailyda dot com>
*
* Licensed under the GNU General Public License Version 3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/if_bridge.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <errno.h>
#include "libblueman.h"
static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
{
unsigned long long jif;
jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
return jif/10000;
}
int _create_bridge(const char* name) {
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
return -errno;
}
int err;
err = ioctl(sock, SIOCBRADDBR, name);
if (err < 0) {
close(sock);
return -errno;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000000 * (0 - tv.tv_sec);
unsigned long args[5];
struct ifreq ifr;
args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
args[1] = __tv_to_jiffies(&tv);
args[2] = 0;
args[3] = 0;
args[4] = 0;
memcpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_data = (char *) &args;
ioctl(sock, SIOCDEVPRIVATE, &ifr);
close(sock);
return 0;
}
int _destroy_bridge(const char* name) {
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
return -errno;
}
int err;
struct ifreq req;
memset(&req, 0, sizeof (struct ifreq));
strncpy(req.ifr_name, name, IFNAMSIZ);
err = ioctl(sock, SIOCGIFFLAGS, &req);
if (err < 0) {
close(sock);
return -errno;
}
req.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
err = ioctl(sock, SIOCSIFFLAGS, &req);
if (err < 0) {
close(sock);
return -errno;
}
err = ioctl(sock, SIOCBRDELBR, name);
if (err < 0) {
close(sock);
return -errno;
}
close(sock);
return 0;
}
static int find_conn(int s, int dev_id, long arg)
{
struct hci_conn_list_req *cl;
struct hci_conn_info *ci;
int i;
int ret = 0;
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl))))
goto out;
cl->dev_id = dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(s, HCIGETCONNLIST, (void *) cl))
goto out;
for (i = 0; i < cl->conn_num; i++, ci++)
if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
ret = 1;
goto out;
}
out:
free(cl);
return ret;
}
int connection_init(int dev_id, char *addr, struct conn_info_handles *ci)
{
struct hci_conn_info_req *cr = NULL;
bdaddr_t bdaddr;
int dd;
int ret = 1;
str2ba(addr, &bdaddr);
if (dev_id < 0) {
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
if (dev_id < 0) {
ret = ERR_NOT_CONNECTED;
goto out;
}
}
dd = hci_open_dev(dev_id);
if (dd < 0) {
ret = ERR_HCI_DEV_OPEN_FAILED;
goto out;
}
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
if (!cr) {
ret = ERR_CANNOT_ALLOCATE;
goto out;
}
bacpy(&cr->bdaddr, &bdaddr);
cr->type = ACL_LINK;
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
ret = ERR_GET_CONN_INFO_FAILED;
goto out;
}
ci->dd = dd;
ci->handle = cr->conn_info->handle;
out:
if (cr)
free(cr);
return ret;
}
int connection_get_rssi(struct conn_info_handles *ci, int8_t *ret_rssi)
{
int8_t rssi;
if (hci_read_rssi(ci->dd, htobs(ci->handle), &rssi, 1000) < 0) {
return ERR_READ_RSSI_FAILED;
}
*ret_rssi = rssi;
return 1;
}
int connection_get_tpl(struct conn_info_handles *ci, int8_t *ret_tpl, uint8_t type)
{
int8_t level;
if (hci_read_transmit_power_level(ci->dd, htobs(ci->handle), type, &level, 1000) < 0) {
return ERR_READ_TPL_FAILED;
}
*ret_tpl = level;
return 1;
}
int connection_close(struct conn_info_handles *ci)
{
hci_close_dev(ci->dd);
return 1;
}
int
get_rfcomm_channel(uint16_t service_class, char* btd_addr) {
bdaddr_t target;
sdp_session_t *session = 0;
uuid_t service_uuid;
int err;
int port_num = 0;
sdp_list_t *response_list = NULL, *search_list, *attrid_list;
str2ba(btd_addr, &target);
sdp_uuid16_create(&service_uuid, service_class);
session = sdp_connect(BDADDR_ANY, &target, SDP_RETRY_IF_BUSY);
if (!session) {
printf("Failed to connect to sdp\n");
return port_num;
}
search_list = sdp_list_append(NULL, &service_uuid);
uint32_t range = 0x0000ffff;
attrid_list = sdp_list_append(NULL, &range);
err = sdp_service_search_attr_req(session, search_list, SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
if (err) {
printf("Failed to search attributes\n");
sdp_list_free(response_list, 0);
sdp_list_free(search_list, 0);
sdp_list_free(attrid_list, 0);
return port_num;
}
// go through each of the service records
sdp_list_t *r = response_list;
for (; r; r = r->next) {
sdp_record_t *rec = (sdp_record_t*) r->data;
sdp_list_t *proto_list;
// get a list of the protocol sequences
if(sdp_get_access_protos(rec, &proto_list) == 0) {
sdp_list_t *p = proto_list;
// go through each protocol sequence
for(; p ; p = p->next) {
sdp_list_t *pds = (sdp_list_t*)p->data;
// go through each protocol list of the protocol sequence
for(; pds ; pds = pds->next) {
// check the protocol attributes
sdp_data_t *d = (sdp_data_t*)pds->data;
int proto = 0;
for(; d; d = d->next) {
switch(d->dtd) {
case SDP_UUID16:
case SDP_UUID32:
case SDP_UUID128:
proto = sdp_uuid_to_proto(&d->val.uuid);
break;
case SDP_UINT8:
if(proto == RFCOMM_UUID) {
port_num = d->val.int8;
printf("rfcomm channel: %d\n", port_num);
}
break;
}
}
}
sdp_list_free((sdp_list_t*)p->data, 0);
}
sdp_list_free(proto_list, 0);
}
sdp_record_free(rec);
}
sdp_close(session);
return port_num;
}
int
get_rfcomm_list(struct rfcomm_dev_list_req **result)
{
struct rfcomm_dev_list_req *dl;
struct rfcomm_dev_info *di;
int ctl = -1;
int ret = 1;
ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
if (ctl < 0) {
ret = ERR_SOCKET_FAILED; //Can't open RFCOMM control socket
goto out;
}
dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
if (dl == NULL) {
ret = ERR_CANNOT_ALLOCATE;
goto out;
}
dl->dev_num = RFCOMM_MAX_DEV;
di = dl->dev_info;
if (ioctl(ctl, RFCOMMGETDEVLIST, (void *) dl) < 0) {
ret = ERR_GET_RFCOMM_LIST_FAILED;
free(dl);
goto out;
}
*result = dl;
out:
if (ctl >= 0)
close(ctl);
return ret;
}
int create_rfcomm_device(char *local_address, char *remote_address, int channel) {
int sk, dev, ret;
struct sockaddr_rc laddr, raddr;
struct rfcomm_dev_req req;
sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (sk < 0) {
ret = ERR_SOCKET_FAILED;
goto out;
}
laddr.rc_family = AF_BLUETOOTH;
str2ba(local_address, &laddr.rc_bdaddr);
laddr.rc_channel = 0;
if (bind(sk, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) {
ret = ERR_BIND_FAILED;
goto out;
}
raddr.rc_family = AF_BLUETOOTH;
str2ba(remote_address, &raddr.rc_bdaddr);
raddr.rc_channel = channel;
if (connect(sk, (struct sockaddr *) &raddr, sizeof(raddr)) < 0) {
ret = ERR_CONNECT_FAILED;
goto out;
}
memset(&req, 0, sizeof(req));
req.flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP);
bacpy(&req.src, &laddr.rc_bdaddr);
bacpy(&req.dst, &raddr.rc_bdaddr);
req.channel = raddr.rc_channel;
req.dev_id = -1;
dev = ioctl(sk, RFCOMMCREATEDEV, &req);
if (dev < 0) {
ret = ERR_CREATE_DEV_FAILED;
} else {
ret = dev;
}
out:
if (sk >= 0)
close(sk);
return ret;
}
int release_rfcomm_device(int id) {
int sk;
struct rfcomm_dev_req req;
sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
if (sk < 0)
return ERR_SOCKET_FAILED;
memset(&req, 0, sizeof(req));
req.flags = (1 << RFCOMM_HANGUP_NOW);
req.dev_id = id;
if (ioctl(sk, RFCOMMRELEASEDEV, &req) < 0) {
close(sk);
return ERR_RELEASE_DEV_FAILED;
} else {
close(sk);
return 0;
}
}
|