2 * This file is part of the sigrok project.
4 * Copyright (C) 2018-2019 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Scan support for Bluetooth LE devices is modelled after the MIT licensed
22 * https://github.com/carsonmcdonald/bluez-experiments experiments/scantest.c
23 * example source code which is:
25 * The MIT License (MIT)
27 * Copyright (c) 2013 Carson McDonald
29 * Permission is hereby granted, free of charge, to any person obtaining a copy of
30 * this software and associated documentation files (the "Software"), to deal in
31 * the Software without restriction, including without limitation the rights to
32 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
33 * the Software, and to permit persons to whom the Software is furnished to do so,
34 * subject to the following conditions:
36 * The above copyright notice and this permission notice shall be included in all
37 * copies or substantial portions of the Software.
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
41 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
42 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
43 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 * This file implements an internal platform agnostic API of libsigrok
49 * for Bluetooth communication, as well as the first implementation on a
50 * specific platform which is based on the BlueZ library and got tested
54 * - Separate the "common" from the "bluez specific" parts. The current
55 * implementation uses the fact that HAVE_BLUETOOTH exclusively depends
56 * on HAVE_LIBBLUEZ, and thus both are identical.
57 * - Add missing features to the Linux platform support: Scan without
58 * root privileges, UUID to handle translation.
59 * - Add support for other platforms.
64 /* Unconditionally compile the source, optionally end up empty. */
68 #include <bluetooth/bluetooth.h>
69 #include <bluetooth/hci.h>
70 #include <bluetooth/hci_lib.h>
71 #include <bluetooth/l2cap.h>
72 #include <bluetooth/rfcomm.h>
83 #include <sys/socket.h>
87 #include <libsigrok/libsigrok.h>
88 #include "libsigrok-internal.h"
91 #define LOG_PREFIX "bt-bluez"
94 #define STORE_MAC_REVERSE 1
95 #define ACCEPT_NONSEP_MAC 1
97 /* Silence warning about (currently) unused routine. */
98 #define WITH_WRITE_TYPE_HANDLE 0
100 /* {{{ compat decls */
102 * The availability of conversion helpers in <bluetooth/bluetooth.h>
103 * appears to be version dependent. Let's provide the helper here if
104 * the header doesn't.
107 #if !defined HAVE_BT_PUT_LE16
108 static inline void bt_put_le16(uint16_t v, uint8_t *p)
110 p[0] = (v >> 0) & 0xff;
111 p[1] = (v >> 8) & 0xff;
115 /* }}} compat decls */
116 /* {{{ Linux socket specific decls */
118 #define BLE_ATT_ERROR_RESP 0x01
119 #define BLE_ATT_EXCHANGE_MTU_REQ 0x02
120 #define BLE_ATT_EXCHANGE_MTU_RESP 0x03
121 #define BLE_ATT_FIND_INFORMATION_REQ 0x04
122 #define BLE_ATT_FIND_INFORMATION_RESP 0x05
123 #define BLE_ATT_FIND_BY_TYPE_REQ 0x06
124 #define BLE_ATT_FIND_BY_TYPE_RESP 0x07
125 #define BLE_ATT_READ_BY_TYPE_REQ 0x08
126 #define BLE_ATT_READ_BY_TYPE_RESP 0x09
127 #define BLE_ATT_READ_REQ 0x0a
128 #define BLE_ATT_READ_RESP 0x0b
129 #define BLE_ATT_READ_BLOB_REQ 0x0c
130 #define BLE_ATT_READ_BLOB_RESP 0x0d
131 #define BLE_ATT_READ_MULTIPLE_REQ 0x0e
132 #define BLE_ATT_READ_MULTIPLE_RESP 0x0f
133 #define BLE_ATT_READ_BY_GROUP_REQ 0x10
134 #define BLE_ATT_READ_BY_GROUP_RESP 0x11
135 #define BLE_ATT_WRITE_REQ 0x12
136 #define BLE_ATT_WRITE_RESP 0x13
137 #define BLE_ATT_WRITE_CMD 0x16
138 #define BLE_ATT_HANDLE_NOTIFICATION 0x1b
139 #define BLE_ATT_HANDLE_INDICATION 0x1d
140 #define BLE_ATT_HANDLE_CONFIRMATION 0x1e
141 #define BLE_ATT_SIGNED_WRITE_CMD 0x52
143 /* }}} Linux socket specific decls */
147 * Convert textual MAC presentation to array of bytes. In contrast to
148 * BlueZ conversion, accept colon or dash separated input as well as a
149 * dense format without separators (001122334455). We expect to use the
150 * library in an environment where colons are not always available as a
151 * separator in user provided specs, while users do want to use some
152 * separator for readability.
154 * TODO Instead of doing the actual conversion here (and dealing with
155 * BlueZ' internal byte order for device address bytes), we might as
156 * well just transform the input string to an output string, and always
157 * use the officially provided str2ba() conversion routine.
159 static int sr_bt_mac_text_to_bytes(const char *text, uint8_t *buf)
167 if (STORE_MAC_REVERSE)
170 while (len && endp && *endp) {
172 if (ACCEPT_NONSEP_MAC) {
174 numbuf[1] = endp[0] ? endp[1] : '\0';
178 v = strtol(ACCEPT_NONSEP_MAC ? numbuf : text, &endp, 16);
181 if (*endp != ':' && *endp != '-' && *endp != '\0')
183 if (v < 0 || v > 255)
185 if (STORE_MAC_REVERSE)
190 if (ACCEPT_NONSEP_MAC)
191 endp = (char *)text + (endp - numbuf);
192 if (*endp == ':' || *endp == '-')
197 sr_err("Failed to parse MAC, too few bytes in '%s'", text);
200 while (isspace(*endp))
203 sr_err("Failed to parse MAC, excess data in '%s'", text);
213 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx)
216 struct hci_dev_info info;
219 rc = hci_devinfo(idx, &info);
220 sr_spew("DIAG: hci_devinfo(%zu) => rc %d", idx, rc);
224 rc = ba2str(&info.bdaddr, addr);
225 sr_spew("DIAG: ba2str() => rc %d", rc);
229 return g_strdup(addr);
236 /* User servicable options. */
237 sr_bt_scan_cb scan_cb;
239 sr_bt_data_cb data_cb;
242 char remote_addr[20];
243 size_t rfcomm_channel;
244 uint16_t read_handle;
245 uint16_t write_handle;
246 uint16_t cccd_handle;
248 /* Internal state. */
251 struct hci_filter orig_filter;
254 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref);
255 static void sr_bt_desc_close(struct sr_bt_desc *desc);
256 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc);
257 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type);
258 #if WITH_WRITE_TYPE_HANDLE
259 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
260 uint8_t type, uint16_t handle);
262 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
263 uint8_t type, uint16_t handle, const uint8_t *data, size_t len);
264 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
265 uint16_t handle, const void *data, size_t len);
267 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void)
269 struct sr_bt_desc *desc;
271 desc = g_malloc0(sizeof(*desc));
281 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc)
286 sr_bt_desc_close(desc);
290 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
291 sr_bt_scan_cb cb, void *cb_data)
297 desc->scan_cb_data = cb_data;
302 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
303 sr_bt_data_cb cb, void *cb_data)
309 desc->data_cb_data = cb_data;
314 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr)
322 if (!addr || !addr[0]) {
323 desc->local_addr[0] = '\0';
327 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
331 rc = ba2str(&mac_bytes, desc->local_addr);
338 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr)
346 if (!addr || !addr[0]) {
347 desc->remote_addr[0] = '\0';
351 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
355 rc = ba2str(&mac_bytes, desc->remote_addr);
362 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel)
367 desc->rfcomm_channel = channel;
372 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
373 uint16_t read_handle, uint16_t write_handle,
374 uint16_t cccd_handle, uint16_t cccd_value)
380 desc->read_handle = read_handle;
381 desc->write_handle = write_handle;
382 desc->cccd_handle = cccd_handle;
383 desc->cccd_value = cccd_value;
388 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref)
397 if (desc->local_addr[0]) {
398 id = hci_devid(desc->local_addr);
399 } else if (desc->remote_addr[0]) {
400 str2ba(desc->remote_addr, &mac);
401 id = hci_get_route(&mac);
403 id = hci_get_route(NULL);
406 sr_err("devid failed");
413 sock = hci_open_dev(id);
415 perror("open HCI socket");
423 static void sr_bt_desc_close(struct sr_bt_desc *desc)
430 hci_close_dev(desc->fd);
439 #define EIR_NAME_COMPLETE 9
441 static int sr_bt_scan_prep(struct sr_bt_desc *desc)
444 uint8_t type, owntype, filter;
445 uint16_t ival, window;
449 struct hci_filter scan_filter;
454 /* TODO Replace magic values with symbolic identifiers. */
455 type = 0x01; /* LE public? */
456 ival = htobs(0x0010);
457 window = htobs(0x0010);
458 owntype = 0x00; /* any? */
461 rc = hci_le_set_scan_parameters(desc->fd,
462 type, ival, window, owntype, filter, timeout);
464 perror("set LE scan params");
471 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
473 perror("set LE scan enable");
477 /* Save the current filter. For later restoration. */
478 slen = sizeof(desc->orig_filter);
479 rc = getsockopt(desc->fd, SOL_HCI, HCI_FILTER,
480 &desc->orig_filter, &slen);
482 perror("getsockopt(HCI_FILTER)");
486 hci_filter_clear(&scan_filter);
487 hci_filter_set_ptype(HCI_EVENT_PKT, &scan_filter);
488 hci_filter_set_event(EVT_LE_META_EVENT, &scan_filter);
489 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
490 &scan_filter, sizeof(scan_filter));
492 perror("setsockopt(HCI_FILTER)");
499 static int sr_bt_scan_post(struct sr_bt_desc *desc)
508 /* Restore previous HCI filter. */
509 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
510 &desc->orig_filter, sizeof(desc->orig_filter));
512 perror("setsockopt(HCI_FILTER)");
519 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
526 static int sr_bt_scan_proc(struct sr_bt_desc *desc,
527 sr_bt_scan_cb scan_cb, void *cb_data,
528 uint8_t *data, size_t dlen, le_advertising_info *info)
537 if (type == EIR_NAME_COMPLETE) {
538 ba2str(&info->bdaddr, addr);
539 name = g_strndup((const char *)&data[1], dlen - 1);
541 scan_cb(cb_data, addr, name);
546 /* Unknown or unsupported type, ignore silently. */
550 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration)
554 uint8_t buf[HCI_MAX_EVENT_SIZE];
555 ssize_t rdlen, rdpos;
556 evt_le_meta_event *meta;
557 le_advertising_info *info;
563 sr_dbg("BLE scan (LE)");
565 rc = sr_bt_desc_open(desc, NULL);
569 rc = sr_bt_scan_prep(desc);
573 deadline = time(NULL);
574 deadline += duration;
575 while (time(NULL) <= deadline) {
577 if (sr_bt_check_socket_usable(desc) < 0)
579 rdlen = sr_bt_read(desc, buf, sizeof(buf));
586 if (rdlen < 1 + HCI_EVENT_HDR_SIZE)
588 meta = (void *)&buf[1 + HCI_EVENT_HDR_SIZE];
589 rdlen -= 1 + HCI_EVENT_HDR_SIZE;
590 if (meta->subevent != EVT_LE_ADVERTISING_REPORT)
592 info = (void *)&meta->data[1];
593 sr_spew("evt: type %d, len %d", info->evt_type, info->length);
598 while (rdpos < rdlen) {
599 datalen = info->data[rdpos];
600 dataptr = &info->data[1 + rdpos];
601 if (rdpos + 1 + datalen > info->length)
603 rdpos += 1 + datalen;
604 rc = sr_bt_scan_proc(desc,
605 desc->scan_cb, desc->scan_cb_data,
606 dataptr, datalen, info);
612 rc = sr_bt_scan_post(desc);
616 sr_bt_desc_close(desc);
621 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration)
623 int dev_id, sock, rsp_max;
627 size_t rsp_count, idx;
633 sr_dbg("BLE scan (BT)");
635 sock = sr_bt_desc_open(desc, &dev_id);
640 info = g_malloc0(rsp_max * sizeof(*info));
641 flags = 0 /* | IREQ_CACHE_FLUSH */;
642 inq_rc = hci_inquiry(dev_id, duration, rsp_max, NULL, &info, flags);
644 perror("hci_inquiry");
647 for (idx = 0; idx < rsp_count; idx++) {
648 memset(addr, 0, sizeof(addr));
649 ba2str(&info[idx].bdaddr, addr);
650 memset(name, 0, sizeof(name));
651 if (hci_read_remote_name(sock, &info[idx].bdaddr, sizeof(name), name, 0) < 0)
652 snprintf(name, sizeof(name), "[unknown]");
654 desc->scan_cb(desc->scan_cb_data, addr, name);
658 sr_bt_desc_close(desc);
664 /* {{{ connect/disconnect */
666 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc)
668 struct sockaddr_l2 sl2;
674 if (!desc->remote_addr[0])
676 sr_dbg("BLE connect, remote addr %s", desc->remote_addr);
678 s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, 0);
680 perror("socket create");
685 memset(&sl2, 0, sizeof(sl2));
686 sl2.l2_family = AF_BLUETOOTH;
688 if (desc->local_addr[0])
689 str2ba(desc->local_addr, &mac);
692 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
693 sl2.l2_cid = L2CAP_FC_CONNLESS;
694 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
695 ret = bind(s, (void *)&sl2, sizeof(sl2));
702 struct bt_security buf = {
703 .level = BT_SECURITY_LOW,
706 ret = setsockopt(s, SOL_BLUETOOTH, BT_SECURITY, &buf, sizeof(buf));
708 perror("setsockopt");
713 str2ba(desc->remote_addr, &mac);
714 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
715 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
716 ret = connect(s, (void *)&sl2, sizeof(sl2));
718 * Cope with "in progress" condition. Keep polling the status
719 * until connect() completes, then get the error by means of
720 * getsockopt(). See the connect(2) manpage for details.
722 if (ret < 0 && errno == EINPROGRESS) {
723 struct pollfd fds[1];
728 * We seem to get here ("connect in progress") even when
729 * the specified peer is not around at all. Which results
730 * in extended periods of time where nothing happens, and
731 * an application timeout seems to be required.
733 sr_spew("in progress ...");
736 memset(fds, 0, sizeof(fds));
738 fds[0].events = POLLOUT;
739 ret = poll(fds, ARRAY_SIZE(fds), -1);
746 if (!(fds[0].revents & POLLOUT))
749 memset(fds, 0, sizeof(fds));
751 fds[0].events = POLLNVAL;
752 ret = poll(fds, 1, 0);
754 perror("poll(INVAL)");
758 /* socket fd is invalid(?) */
763 solen = sizeof(soerror);
764 ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &solen);
766 perror("getsockopt(SO_ERROR)");
770 /* connect(2) failed, SO_ERROR has the error code. */
772 perror("connect(PROGRESS)");
777 * TODO Get the receive MTU here?
778 * getsockopt(SOL_BLUETOOTH, BT_RCVMTU, u16);
789 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc)
791 struct sockaddr_rc addr;
796 if (!desc->remote_addr[0])
798 sr_dbg("RFCOMM connect, remote addr %s, channel %zu",
799 desc->remote_addr, desc->rfcomm_channel);
801 if (!desc->rfcomm_channel)
802 desc->rfcomm_channel = 1;
804 fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
811 memset(&addr, 0, sizeof(addr));
812 addr.rc_family = AF_BLUETOOTH;
813 str2ba(desc->remote_addr, &addr.rc_bdaddr);
814 addr.rc_channel = desc->rfcomm_channel;
815 rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
820 sr_spew("connected");
825 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc)
827 sr_dbg("BLE disconnect");
831 sr_bt_desc_close(desc);
834 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc)
836 struct pollfd fds[1];
844 memset(fds, 0, sizeof(fds));
845 fds[0].fd = desc->fd;
846 fds[0].events = POLLERR | POLLHUP;
847 ret = poll(fds, ARRAY_SIZE(fds), 0);
852 if (fds[0].revents & POLLHUP)
854 if (fds[0].revents & POLLERR)
856 if (fds[0].revents & POLLNVAL)
862 /* }}} connect/disconnect */
863 /* {{{ indication/notification */
865 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc)
867 uint8_t buf[sizeof(desc->cccd_value)];
872 sr_dbg("BLE start notify");
874 if (sr_bt_check_socket_usable(desc) < 0)
877 bt_put_le16(desc->cccd_value, buf);
878 wrlen = sr_bt_char_write_req(desc, desc->cccd_handle, buf, sizeof(buf));
879 if (wrlen != sizeof(buf))
885 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc)
890 uint16_t packet_handle;
891 uint8_t *packet_data;
897 if (sr_bt_check_socket_usable(desc) < 0)
900 /* Get another message from the Bluetooth socket. */
901 rdlen = sr_bt_read(desc, buf, sizeof(buf));
907 /* Get header fields and references to the payload data. */
909 packet_handle = 0x0000;
913 packet_type = buf[0];
915 packet_handle = bt_get_le16(&buf[1]);
916 packet_data = &buf[3];
917 packet_dlen = rdlen - 3;
920 /* Dispatch according to the message type. */
921 switch (packet_type) {
922 case BLE_ATT_ERROR_RESP:
923 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "error response");
926 case BLE_ATT_WRITE_RESP:
927 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "write response");
930 case BLE_ATT_HANDLE_INDICATION:
931 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle indication");
932 sr_bt_write_type(desc, BLE_ATT_HANDLE_CONFIRMATION);
933 if (packet_handle != desc->read_handle)
939 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
940 case BLE_ATT_HANDLE_NOTIFICATION:
941 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle notification");
942 if (packet_handle != desc->read_handle)
948 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
950 sr_spew("unsupported type 0x%02x", packet_type);
957 /* }}} indication/notification */
960 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
961 const void *data, size_t len)
968 if (sr_bt_check_socket_usable(desc) < 0)
971 /* Send TX data to the writable characteristics for BLE UART services. */
972 if (desc->write_handle)
973 return sr_bt_char_write_req(desc, desc->write_handle, data, len);
975 /* Send raw TX data to the RFCOMM socket for BT Classic channels. */
976 return write(desc->fd, data, len);
979 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type)
988 if (sr_bt_check_socket_usable(desc) < 0)
991 wrlen = write(desc->fd, &type, sizeof(type));
994 if (wrlen < (ssize_t)sizeof(type))
1000 #if WITH_WRITE_TYPE_HANDLE
1001 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
1002 uint8_t type, uint16_t handle)
1004 return sr_bt_write_type_handle_bytes(desc, type, handle, NULL, 0);
1008 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
1009 uint8_t type, uint16_t handle, const uint8_t *data, size_t len)
1011 uint8_t header[sizeof(uint8_t) + sizeof(uint16_t)];
1012 struct iovec iov[2] = {
1013 { .iov_base = header, .iov_len = sizeof(header), },
1014 { .iov_base = (void *)data, .iov_len = len, },
1023 if (sr_bt_check_socket_usable(desc) < 0)
1027 bt_put_le16(handle, &header[1]);
1030 wrlen = writev(desc->fd, iov, ARRAY_SIZE(iov));
1032 wrlen = write(desc->fd, header, sizeof(header));
1036 if (wrlen < (ssize_t)sizeof(header))
1038 wrlen -= sizeof(header);
1043 /* Returns negative upon error, or returns the number of _payload_ bytes written. */
1044 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
1045 uint16_t handle, const void *data, size_t len)
1047 return sr_bt_write_type_handle_bytes(desc, BLE_ATT_WRITE_REQ,
1051 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc, void *data, size_t len)
1053 struct pollfd fds[1];
1062 if (sr_bt_check_socket_usable(desc) < 0)
1065 memset(fds, 0, sizeof(fds));
1066 fds[0].fd = desc->fd;
1067 fds[0].events = POLLIN;
1068 ret = poll(fds, ARRAY_SIZE(fds), 0);
1073 if (!(fds[0].revents & POLLIN))
1076 rdlen = read(desc->fd, data, len);
1081 /* }}} indication/notification */