• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
6  *  Copyright (C) 2009 The Android Open Source Project
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 #include <unistd.h>
25 #include <errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 
30 #include <private/android_filesystem_config.h>
31 #include <sys/prctl.h>
32 #include <linux/capability.h>
33 
34 #include <bluetooth/bluetooth.h>
35 #include <bluetooth/hci.h>
36 
37 /* Set UID to bluetooth w/ CAP_NET_RAW, CAP_NET_ADMIN and CAP_NET_BIND_SERVICE
38  * (Android's init.rc does not yet support applying linux capabilities) */
android_set_aid_and_cap()39 void android_set_aid_and_cap() {
40 	prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
41 	setuid(AID_BLUETOOTH);
42 
43 	struct __user_cap_header_struct header;
44 	struct __user_cap_data_struct cap;
45 	header.version = _LINUX_CAPABILITY_VERSION;
46 	header.pid = 0;
47 	cap.effective = cap.permitted = 1 << CAP_NET_RAW |
48 					1 << CAP_NET_ADMIN |
49 					1 << CAP_NET_BIND_SERVICE;
50 	cap.inheritable = 0;
51 	capset(&header, &cap);
52 }
53 
54 #ifdef BOARD_HAVE_BLUETOOTH_BCM
vendor_high_priority(int fd,uint16_t handle)55 static int vendor_high_priority(int fd, uint16_t handle) {
56     unsigned char hci_sleep_cmd[] = {
57         0x01,               // HCI command packet
58         0x57, 0xfc,         // HCI_Write_High_Priority_Connection
59         0x02,               // Length
60         0x00, 0x00          // Handle
61     };
62 
63     hci_sleep_cmd[4] = (uint8_t)handle;
64     hci_sleep_cmd[5] = (uint8_t)(handle >> 8);
65 
66     int ret = write(fd, hci_sleep_cmd, sizeof(hci_sleep_cmd));
67     if (ret < 0) {
68         error("write(): %s (%d)]", strerror(errno), errno);
69         return -1;
70     } else if (ret != sizeof(hci_sleep_cmd)) {
71         error("write(): unexpected length %d", ret);
72         return -1;
73     }
74     return 0;
75 }
76 
get_hci_sock()77 static int get_hci_sock() {
78     int sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
79     struct sockaddr_hci addr;
80     int opt;
81 
82     if(sock < 0) {
83         error("Can't create raw HCI socket!");
84         return -1;
85     }
86 
87     opt = 1;
88     if (setsockopt(sock, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
89         error("Error setting data direction\n");
90         return -1;
91     }
92 
93     /* Bind socket to the HCI device */
94     addr.hci_family = AF_BLUETOOTH;
95     addr.hci_dev = 0;  // hci0
96     if(bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
97         error("Can't attach to device hci0. %s(%d)\n",
98              strerror(errno),
99              errno);
100         return -1;
101     }
102     return sock;
103 }
104 
get_acl_handle(int fd,bdaddr_t * bdaddr)105 static int get_acl_handle(int fd, bdaddr_t *bdaddr) {
106     int i;
107     int ret = -1;
108     struct hci_conn_list_req *conn_list;
109     struct hci_conn_info *conn_info;
110     int max_conn = 10;
111 
112     conn_list = malloc(max_conn * (
113             sizeof(struct hci_conn_list_req) + sizeof(struct hci_conn_info)));
114     if (!conn_list) {
115         error("Out of memory in %s\n", __FUNCTION__);
116         return -1;
117     }
118 
119     conn_list->dev_id = 0;  /* hardcoded to HCI device 0 */
120     conn_list->conn_num = max_conn;
121 
122     if (ioctl(fd, HCIGETCONNLIST, (void *)conn_list)) {
123         error("Failed to get connection list\n");
124         goto out;
125     }
126 
127     for (i=0; i < conn_list->conn_num; i++) {
128         conn_info = &conn_list->conn_info[i];
129         if (conn_info->type == ACL_LINK &&
130                 !memcmp((void *)&conn_info->bdaddr, (void *)bdaddr,
131                 sizeof(bdaddr_t))) {
132             ret = conn_info->handle;
133             goto out;
134         }
135     }
136     ret = 0;
137 
138 out:
139     free(conn_list);
140     return ret;
141 }
142 
143 /* Request that the ACL link to a given Bluetooth connection be high priority,
144  * for improved coexistance support
145  */
android_set_high_priority(bdaddr_t * ba)146 int android_set_high_priority(bdaddr_t *ba) {
147     int ret;
148     int fd = get_hci_sock();
149     int acl_handle;
150 
151     if (fd < 0)
152         return fd;
153 
154     acl_handle = get_acl_handle(fd, ba);
155     if (acl_handle < 0) {
156         ret = acl_handle;
157         goto out;
158     }
159 
160     ret = vendor_high_priority(fd, acl_handle);
161 
162 out:
163     close(fd);
164 
165     return ret;
166 }
167 
168 #else
169 
android_set_high_priority(bdaddr_t * ba)170 int android_set_high_priority(bdaddr_t *ba) {
171     return 0;
172 }
173 
174 #endif
175