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"
90 #define LOG_PREFIX "bt-bluez"
92 #define CONNECT_BLE_TIMEOUT 20 /* Connect timeout in seconds. */
93 #define STORE_MAC_REVERSE 1
94 #define ACCEPT_NONSEP_MAC 1
96 #define CONNECT_RFCOMM_TRIES 3
97 #define CONNECT_RFCOMM_RETRY_MS 100
99 /* Silence warning about (currently) unused routine. */
100 #define WITH_WRITE_TYPE_HANDLE 0
102 /* {{{ compat decls */
104 * The availability of conversion helpers in <bluetooth/bluetooth.h>
105 * appears to be version dependent. Let's provide the helper here if
106 * the header doesn't.
109 /* }}} compat decls */
110 /* {{{ Linux socket specific decls */
112 #define BLE_ATT_ERROR_RESP 0x01
113 #define BLE_ATT_EXCHANGE_MTU_REQ 0x02
114 #define BLE_ATT_EXCHANGE_MTU_RESP 0x03
115 #define BLE_ATT_FIND_INFORMATION_REQ 0x04
116 #define BLE_ATT_FIND_INFORMATION_RESP 0x05
117 #define BLE_ATT_FIND_BY_TYPE_REQ 0x06
118 #define BLE_ATT_FIND_BY_TYPE_RESP 0x07
119 #define BLE_ATT_READ_BY_TYPE_REQ 0x08
120 #define BLE_ATT_READ_BY_TYPE_RESP 0x09
121 #define BLE_ATT_READ_REQ 0x0a
122 #define BLE_ATT_READ_RESP 0x0b
123 #define BLE_ATT_READ_BLOB_REQ 0x0c
124 #define BLE_ATT_READ_BLOB_RESP 0x0d
125 #define BLE_ATT_READ_MULTIPLE_REQ 0x0e
126 #define BLE_ATT_READ_MULTIPLE_RESP 0x0f
127 #define BLE_ATT_READ_BY_GROUP_REQ 0x10
128 #define BLE_ATT_READ_BY_GROUP_RESP 0x11
129 #define BLE_ATT_WRITE_REQ 0x12
130 #define BLE_ATT_WRITE_RESP 0x13
131 #define BLE_ATT_WRITE_CMD 0x16
132 #define BLE_ATT_HANDLE_NOTIFICATION 0x1b
133 #define BLE_ATT_HANDLE_INDICATION 0x1d
134 #define BLE_ATT_HANDLE_CONFIRMATION 0x1e
135 #define BLE_ATT_SIGNED_WRITE_CMD 0x52
137 /* }}} Linux socket specific decls */
141 * Convert textual MAC presentation to array of bytes. In contrast to
142 * BlueZ conversion, accept colon or dash separated input as well as a
143 * dense format without separators (001122334455). We expect to use the
144 * library in an environment where colons are not always available as a
145 * separator in user provided specs, while users do want to use some
146 * separator for readability.
148 * TODO Instead of doing the actual conversion here (and dealing with
149 * BlueZ' internal byte order for device address bytes), we might as
150 * well just transform the input string to an output string, and always
151 * use the officially provided str2ba() conversion routine.
153 static int sr_bt_mac_text_to_bytes(const char *text, uint8_t *buf)
161 if (STORE_MAC_REVERSE)
164 while (len && endp && *endp) {
166 if (ACCEPT_NONSEP_MAC) {
168 numbuf[1] = endp[0] ? endp[1] : '\0';
172 v = strtol(ACCEPT_NONSEP_MAC ? numbuf : text, &endp, 16);
175 if (*endp != ':' && *endp != '-' && *endp != '\0')
177 if (v < 0 || v > 255)
179 if (STORE_MAC_REVERSE)
184 if (ACCEPT_NONSEP_MAC)
185 endp = (char *)text + (endp - numbuf);
186 if (*endp == ':' || *endp == '-')
191 sr_err("Failed to parse MAC, too few bytes in '%s'", text);
194 while (isspace(*endp))
197 sr_err("Failed to parse MAC, excess data in '%s'", text);
207 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx)
210 struct hci_dev_info info;
213 rc = hci_devinfo(idx, &info);
214 sr_spew("DIAG: hci_devinfo(%zu) => rc %d", idx, rc);
218 rc = ba2str(&info.bdaddr, addr);
219 sr_spew("DIAG: ba2str() => rc %d", rc);
223 return g_strdup(addr);
230 /* User servicable options. */
231 sr_bt_scan_cb scan_cb;
233 sr_bt_data_cb data_cb;
236 char remote_addr[20];
237 size_t rfcomm_channel;
238 uint16_t read_handle;
239 uint16_t write_handle;
240 uint16_t cccd_handle;
242 /* Internal state. */
245 struct hci_filter orig_filter;
248 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref);
249 static void sr_bt_desc_close(struct sr_bt_desc *desc);
250 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc);
251 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type);
252 #if WITH_WRITE_TYPE_HANDLE
253 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
254 uint8_t type, uint16_t handle);
256 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
257 uint8_t type, uint16_t handle, const uint8_t *data, size_t len);
258 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
259 uint16_t handle, const void *data, size_t len);
261 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void)
263 struct sr_bt_desc *desc;
265 desc = g_malloc0(sizeof(*desc));
275 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc)
280 sr_bt_desc_close(desc);
284 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
285 sr_bt_scan_cb cb, void *cb_data)
291 desc->scan_cb_data = cb_data;
296 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
297 sr_bt_data_cb cb, void *cb_data)
303 desc->data_cb_data = cb_data;
308 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr)
316 if (!addr || !addr[0]) {
317 desc->local_addr[0] = '\0';
321 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
325 rc = ba2str(&mac_bytes, desc->local_addr);
332 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr)
340 if (!addr || !addr[0]) {
341 desc->remote_addr[0] = '\0';
345 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
349 rc = ba2str(&mac_bytes, desc->remote_addr);
356 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel)
361 desc->rfcomm_channel = channel;
366 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
367 uint16_t read_handle, uint16_t write_handle,
368 uint16_t cccd_handle, uint16_t cccd_value)
374 desc->read_handle = read_handle;
375 desc->write_handle = write_handle;
376 desc->cccd_handle = cccd_handle;
377 desc->cccd_value = cccd_value;
382 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref)
391 if (desc->local_addr[0]) {
392 id = hci_devid(desc->local_addr);
393 } else if (desc->remote_addr[0]) {
394 str2ba(desc->remote_addr, &mac);
395 id = hci_get_route(&mac);
397 id = hci_get_route(NULL);
400 sr_err("devid failed");
407 sock = hci_open_dev(id);
409 perror("open HCI socket");
417 static void sr_bt_desc_close(struct sr_bt_desc *desc)
424 hci_close_dev(desc->fd);
433 #define EIR_NAME_COMPLETE 9
435 static int sr_bt_scan_prep(struct sr_bt_desc *desc)
438 uint8_t type, owntype, filter;
439 uint16_t ival, window;
443 struct hci_filter scan_filter;
448 /* TODO Replace magic values with symbolic identifiers. */
449 type = 0x01; /* LE public? */
450 ival = htobs(0x0010);
451 window = htobs(0x0010);
452 owntype = 0x00; /* any? */
455 rc = hci_le_set_scan_parameters(desc->fd,
456 type, ival, window, owntype, filter, timeout);
458 perror("set LE scan params");
465 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
467 perror("set LE scan enable");
471 /* Save the current filter. For later restoration. */
472 slen = sizeof(desc->orig_filter);
473 rc = getsockopt(desc->fd, SOL_HCI, HCI_FILTER,
474 &desc->orig_filter, &slen);
476 perror("getsockopt(HCI_FILTER)");
480 hci_filter_clear(&scan_filter);
481 hci_filter_set_ptype(HCI_EVENT_PKT, &scan_filter);
482 hci_filter_set_event(EVT_LE_META_EVENT, &scan_filter);
483 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
484 &scan_filter, sizeof(scan_filter));
486 perror("setsockopt(HCI_FILTER)");
493 static int sr_bt_scan_post(struct sr_bt_desc *desc)
502 /* Restore previous HCI filter. */
503 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
504 &desc->orig_filter, sizeof(desc->orig_filter));
506 perror("setsockopt(HCI_FILTER)");
513 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
520 static int sr_bt_scan_proc(struct sr_bt_desc *desc,
521 sr_bt_scan_cb scan_cb, void *cb_data,
522 uint8_t *data, size_t dlen, le_advertising_info *info)
531 if (type == EIR_NAME_COMPLETE) {
532 ba2str(&info->bdaddr, addr);
533 name = g_strndup((const char *)&data[1], dlen - 1);
535 scan_cb(cb_data, addr, name);
540 /* Unknown or unsupported type, ignore silently. */
544 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration)
548 uint8_t buf[HCI_MAX_EVENT_SIZE];
549 ssize_t rdlen, rdpos;
550 evt_le_meta_event *meta;
551 le_advertising_info *info;
557 sr_dbg("BLE scan (LE)");
559 rc = sr_bt_desc_open(desc, NULL);
563 rc = sr_bt_scan_prep(desc);
567 deadline = time(NULL);
568 deadline += duration;
569 while (time(NULL) <= deadline) {
571 if (sr_bt_check_socket_usable(desc) < 0)
573 rdlen = sr_bt_read(desc, buf, sizeof(buf));
580 if (rdlen < 1 + HCI_EVENT_HDR_SIZE)
582 meta = (void *)&buf[1 + HCI_EVENT_HDR_SIZE];
583 rdlen -= 1 + HCI_EVENT_HDR_SIZE;
584 if (meta->subevent != EVT_LE_ADVERTISING_REPORT)
586 info = (void *)&meta->data[1];
587 sr_spew("evt: type %d, len %d", info->evt_type, info->length);
592 while (rdpos < rdlen) {
593 datalen = info->data[rdpos];
594 dataptr = &info->data[1 + rdpos];
595 if (rdpos + 1 + datalen > info->length)
597 rdpos += 1 + datalen;
598 rc = sr_bt_scan_proc(desc,
599 desc->scan_cb, desc->scan_cb_data,
600 dataptr, datalen, info);
606 rc = sr_bt_scan_post(desc);
610 sr_bt_desc_close(desc);
615 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration)
617 int dev_id, sock, rsp_max;
621 size_t rsp_count, idx;
627 sr_dbg("BLE scan (BT)");
629 sock = sr_bt_desc_open(desc, &dev_id);
634 info = g_malloc0(rsp_max * sizeof(*info));
635 flags = 0 /* | IREQ_CACHE_FLUSH */;
636 inq_rc = hci_inquiry(dev_id, duration, rsp_max, NULL, &info, flags);
638 perror("hci_inquiry");
641 for (idx = 0; idx < rsp_count; idx++) {
642 memset(addr, 0, sizeof(addr));
643 ba2str(&info[idx].bdaddr, addr);
644 memset(name, 0, sizeof(name));
645 if (hci_read_remote_name(sock, &info[idx].bdaddr, sizeof(name), name, 0) < 0)
646 snprintf(name, sizeof(name), "[unknown]");
648 desc->scan_cb(desc->scan_cb_data, addr, name);
652 sr_bt_desc_close(desc);
658 /* {{{ connect/disconnect */
660 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc)
662 struct sockaddr_l2 sl2;
669 if (!desc->remote_addr[0])
671 sr_dbg("BLE connect, remote addr %s", desc->remote_addr);
673 s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, 0);
675 perror("socket create");
680 memset(&sl2, 0, sizeof(sl2));
681 sl2.l2_family = AF_BLUETOOTH;
683 if (desc->local_addr[0])
684 str2ba(desc->local_addr, &mac);
687 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
688 sl2.l2_cid = L2CAP_FC_CONNLESS;
689 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
690 ret = bind(s, (void *)&sl2, sizeof(sl2));
697 struct bt_security buf = {
698 .level = BT_SECURITY_LOW,
701 ret = setsockopt(s, SOL_BLUETOOTH, BT_SECURITY, &buf, sizeof(buf));
703 perror("setsockopt");
708 deadline = g_get_monotonic_time();
709 deadline += CONNECT_BLE_TIMEOUT * 1000 * 1000;
710 str2ba(desc->remote_addr, &mac);
711 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
712 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
713 ret = connect(s, (void *)&sl2, sizeof(sl2));
715 * Cope with "in progress" condition. Keep polling the status
716 * until connect() completes, then get the error by means of
717 * getsockopt(). See the connect(2) manpage for details.
719 if (ret < 0 && errno == EINPROGRESS) {
720 struct pollfd fds[1];
725 * We seem to get here ("connect in progress") even when
726 * the specified peer is not around at all. Which results
727 * in extended periods of time where nothing happens, and
728 * an application timeout seems to be required.
730 sr_spew("in progress ...");
733 memset(fds, 0, sizeof(fds));
735 fds[0].events = POLLOUT;
736 ret = poll(fds, ARRAY_SIZE(fds), -1);
743 if (!(fds[0].revents & POLLOUT))
745 if (g_get_monotonic_time() >= deadline) {
746 sr_warn("Connect attempt timed out");
750 memset(fds, 0, sizeof(fds));
752 fds[0].events = POLLNVAL;
753 ret = poll(fds, 1, 0);
755 perror("poll(INVAL)");
759 /* socket fd is invalid(?) */
764 solen = sizeof(soerror);
765 ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &solen);
767 perror("getsockopt(SO_ERROR)");
771 /* connect(2) failed, SO_ERROR has the error code. */
773 perror("connect(PROGRESS)");
778 * TODO Get the receive MTU here?
779 * getsockopt(SOL_BLUETOOTH, BT_RCVMTU, u16);
790 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc)
792 struct sockaddr_rc addr;
797 if (!desc->remote_addr[0])
799 sr_dbg("RFCOMM connect, remote addr %s, channel %zu",
800 desc->remote_addr, desc->rfcomm_channel);
802 if (!desc->rfcomm_channel)
803 desc->rfcomm_channel = 1;
805 memset(&addr, 0, sizeof(addr));
806 addr.rc_family = AF_BLUETOOTH;
807 str2ba(desc->remote_addr, &addr.rc_bdaddr);
808 addr.rc_channel = desc->rfcomm_channel;
811 * There are cases where connect returns EBUSY if we are re-connecting
812 * to a device. Try multiple times to work around this issue.
814 for (i = 0; i < CONNECT_RFCOMM_TRIES; i++) {
815 fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
821 rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
823 sr_spew("connected");
826 } else if (rc < 0 && errno == EBUSY) {
828 g_usleep(CONNECT_RFCOMM_RETRY_MS * 1000);
836 sr_err("Connect failed, device busy.");
841 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc)
843 sr_dbg("BLE disconnect");
847 sr_bt_desc_close(desc);
850 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc)
852 struct pollfd fds[1];
860 memset(fds, 0, sizeof(fds));
861 fds[0].fd = desc->fd;
862 fds[0].events = POLLERR | POLLHUP;
863 ret = poll(fds, ARRAY_SIZE(fds), 0);
868 if (fds[0].revents & POLLHUP)
870 if (fds[0].revents & POLLERR)
872 if (fds[0].revents & POLLNVAL)
878 /* }}} connect/disconnect */
879 /* {{{ indication/notification */
881 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc)
883 uint8_t buf[sizeof(desc->cccd_value)];
888 sr_dbg("BLE start notify");
890 if (sr_bt_check_socket_usable(desc) < 0)
893 write_u16le(buf, desc->cccd_value);
894 wrlen = sr_bt_char_write_req(desc, desc->cccd_handle, buf, sizeof(buf));
895 if (wrlen != sizeof(buf))
901 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc)
906 uint16_t packet_handle;
907 uint8_t *packet_data;
913 if (sr_bt_check_socket_usable(desc) < 0)
916 /* Get another message from the Bluetooth socket. */
917 rdlen = sr_bt_read(desc, buf, sizeof(buf));
923 /* Get header fields and references to the payload data. */
925 packet_handle = 0x0000;
929 packet_type = buf[0];
931 packet_handle = bt_get_le16(&buf[1]);
932 packet_data = &buf[3];
933 packet_dlen = rdlen - 3;
936 /* Dispatch according to the message type. */
937 switch (packet_type) {
938 case BLE_ATT_ERROR_RESP:
939 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "error response");
942 case BLE_ATT_WRITE_RESP:
943 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "write response");
946 case BLE_ATT_HANDLE_INDICATION:
947 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle indication");
948 sr_bt_write_type(desc, BLE_ATT_HANDLE_CONFIRMATION);
949 if (packet_handle != desc->read_handle)
955 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
956 case BLE_ATT_HANDLE_NOTIFICATION:
957 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle notification");
958 if (packet_handle != desc->read_handle)
964 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
966 sr_spew("unsupported type 0x%02x", packet_type);
973 /* }}} indication/notification */
976 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
977 const void *data, size_t len)
984 if (sr_bt_check_socket_usable(desc) < 0)
987 /* Send TX data to the writable characteristics for BLE UART services. */
988 if (desc->write_handle)
989 return sr_bt_char_write_req(desc, desc->write_handle, data, len);
991 /* Send raw TX data to the RFCOMM socket for BT Classic channels. */
992 return write(desc->fd, data, len);
995 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type)
1004 if (sr_bt_check_socket_usable(desc) < 0)
1007 wrlen = write(desc->fd, &type, sizeof(type));
1010 if (wrlen < (ssize_t)sizeof(type))
1016 #if WITH_WRITE_TYPE_HANDLE
1017 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
1018 uint8_t type, uint16_t handle)
1020 return sr_bt_write_type_handle_bytes(desc, type, handle, NULL, 0);
1024 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
1025 uint8_t type, uint16_t handle, const uint8_t *data, size_t len)
1027 uint8_t header[sizeof(uint8_t) + sizeof(uint16_t)];
1028 struct iovec iov[2] = {
1029 { .iov_base = header, .iov_len = sizeof(header), },
1030 { .iov_base = (void *)data, .iov_len = len, },
1039 if (sr_bt_check_socket_usable(desc) < 0)
1043 write_u16le(&header[1], handle);
1046 wrlen = writev(desc->fd, iov, ARRAY_SIZE(iov));
1048 wrlen = write(desc->fd, header, sizeof(header));
1052 if (wrlen < (ssize_t)sizeof(header))
1054 wrlen -= sizeof(header);
1059 /* Returns negative upon error, or returns the number of _payload_ bytes written. */
1060 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
1061 uint16_t handle, const void *data, size_t len)
1063 return sr_bt_write_type_handle_bytes(desc, BLE_ATT_WRITE_REQ,
1067 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc, void *data, size_t len)
1069 struct pollfd fds[1];
1078 if (sr_bt_check_socket_usable(desc) < 0)
1081 memset(fds, 0, sizeof(fds));
1082 fds[0].fd = desc->fd;
1083 fds[0].events = POLLIN;
1084 ret = poll(fds, ARRAY_SIZE(fds), 0);
1089 if (!(fds[0].revents & POLLIN))
1092 rdlen = read(desc->fd, data, len);
1097 /* }}} indication/notification */