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 #if !defined HAVE_BT_PUT_LE16
110 static inline void bt_put_le16(uint16_t v, uint8_t *p)
112 p[0] = (v >> 0) & 0xff;
113 p[1] = (v >> 8) & 0xff;
117 /* }}} compat decls */
118 /* {{{ Linux socket specific decls */
120 #define BLE_ATT_ERROR_RESP 0x01
121 #define BLE_ATT_EXCHANGE_MTU_REQ 0x02
122 #define BLE_ATT_EXCHANGE_MTU_RESP 0x03
123 #define BLE_ATT_FIND_INFORMATION_REQ 0x04
124 #define BLE_ATT_FIND_INFORMATION_RESP 0x05
125 #define BLE_ATT_FIND_BY_TYPE_REQ 0x06
126 #define BLE_ATT_FIND_BY_TYPE_RESP 0x07
127 #define BLE_ATT_READ_BY_TYPE_REQ 0x08
128 #define BLE_ATT_READ_BY_TYPE_RESP 0x09
129 #define BLE_ATT_READ_REQ 0x0a
130 #define BLE_ATT_READ_RESP 0x0b
131 #define BLE_ATT_READ_BLOB_REQ 0x0c
132 #define BLE_ATT_READ_BLOB_RESP 0x0d
133 #define BLE_ATT_READ_MULTIPLE_REQ 0x0e
134 #define BLE_ATT_READ_MULTIPLE_RESP 0x0f
135 #define BLE_ATT_READ_BY_GROUP_REQ 0x10
136 #define BLE_ATT_READ_BY_GROUP_RESP 0x11
137 #define BLE_ATT_WRITE_REQ 0x12
138 #define BLE_ATT_WRITE_RESP 0x13
139 #define BLE_ATT_WRITE_CMD 0x16
140 #define BLE_ATT_HANDLE_NOTIFICATION 0x1b
141 #define BLE_ATT_HANDLE_INDICATION 0x1d
142 #define BLE_ATT_HANDLE_CONFIRMATION 0x1e
143 #define BLE_ATT_SIGNED_WRITE_CMD 0x52
145 /* }}} Linux socket specific decls */
149 * Convert textual MAC presentation to array of bytes. In contrast to
150 * BlueZ conversion, accept colon or dash separated input as well as a
151 * dense format without separators (001122334455). We expect to use the
152 * library in an environment where colons are not always available as a
153 * separator in user provided specs, while users do want to use some
154 * separator for readability.
156 * TODO Instead of doing the actual conversion here (and dealing with
157 * BlueZ' internal byte order for device address bytes), we might as
158 * well just transform the input string to an output string, and always
159 * use the officially provided str2ba() conversion routine.
161 static int sr_bt_mac_text_to_bytes(const char *text, uint8_t *buf)
169 if (STORE_MAC_REVERSE)
172 while (len && endp && *endp) {
174 if (ACCEPT_NONSEP_MAC) {
176 numbuf[1] = endp[0] ? endp[1] : '\0';
180 v = strtol(ACCEPT_NONSEP_MAC ? numbuf : text, &endp, 16);
183 if (*endp != ':' && *endp != '-' && *endp != '\0')
185 if (v < 0 || v > 255)
187 if (STORE_MAC_REVERSE)
192 if (ACCEPT_NONSEP_MAC)
193 endp = (char *)text + (endp - numbuf);
194 if (*endp == ':' || *endp == '-')
199 sr_err("Failed to parse MAC, too few bytes in '%s'", text);
202 while (isspace(*endp))
205 sr_err("Failed to parse MAC, excess data in '%s'", text);
215 SR_PRIV const char *sr_bt_adapter_get_address(size_t idx)
218 struct hci_dev_info info;
221 rc = hci_devinfo(idx, &info);
222 sr_spew("DIAG: hci_devinfo(%zu) => rc %d", idx, rc);
226 rc = ba2str(&info.bdaddr, addr);
227 sr_spew("DIAG: ba2str() => rc %d", rc);
231 return g_strdup(addr);
238 /* User servicable options. */
239 sr_bt_scan_cb scan_cb;
241 sr_bt_data_cb data_cb;
244 char remote_addr[20];
245 size_t rfcomm_channel;
246 uint16_t read_handle;
247 uint16_t write_handle;
248 uint16_t cccd_handle;
250 /* Internal state. */
253 struct hci_filter orig_filter;
256 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref);
257 static void sr_bt_desc_close(struct sr_bt_desc *desc);
258 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc);
259 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type);
260 #if WITH_WRITE_TYPE_HANDLE
261 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
262 uint8_t type, uint16_t handle);
264 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
265 uint8_t type, uint16_t handle, const uint8_t *data, size_t len);
266 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
267 uint16_t handle, const void *data, size_t len);
269 SR_PRIV struct sr_bt_desc *sr_bt_desc_new(void)
271 struct sr_bt_desc *desc;
273 desc = g_malloc0(sizeof(*desc));
283 SR_PRIV void sr_bt_desc_free(struct sr_bt_desc *desc)
288 sr_bt_desc_close(desc);
292 SR_PRIV int sr_bt_config_cb_scan(struct sr_bt_desc *desc,
293 sr_bt_scan_cb cb, void *cb_data)
299 desc->scan_cb_data = cb_data;
304 SR_PRIV int sr_bt_config_cb_data(struct sr_bt_desc *desc,
305 sr_bt_data_cb cb, void *cb_data)
311 desc->data_cb_data = cb_data;
316 SR_PRIV int sr_bt_config_addr_local(struct sr_bt_desc *desc, const char *addr)
324 if (!addr || !addr[0]) {
325 desc->local_addr[0] = '\0';
329 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
333 rc = ba2str(&mac_bytes, desc->local_addr);
340 SR_PRIV int sr_bt_config_addr_remote(struct sr_bt_desc *desc, const char *addr)
348 if (!addr || !addr[0]) {
349 desc->remote_addr[0] = '\0';
353 rc = sr_bt_mac_text_to_bytes(addr, &mac_bytes.b[0]);
357 rc = ba2str(&mac_bytes, desc->remote_addr);
364 SR_PRIV int sr_bt_config_rfcomm(struct sr_bt_desc *desc, size_t channel)
369 desc->rfcomm_channel = channel;
374 SR_PRIV int sr_bt_config_notify(struct sr_bt_desc *desc,
375 uint16_t read_handle, uint16_t write_handle,
376 uint16_t cccd_handle, uint16_t cccd_value)
382 desc->read_handle = read_handle;
383 desc->write_handle = write_handle;
384 desc->cccd_handle = cccd_handle;
385 desc->cccd_value = cccd_value;
390 static int sr_bt_desc_open(struct sr_bt_desc *desc, int *id_ref)
399 if (desc->local_addr[0]) {
400 id = hci_devid(desc->local_addr);
401 } else if (desc->remote_addr[0]) {
402 str2ba(desc->remote_addr, &mac);
403 id = hci_get_route(&mac);
405 id = hci_get_route(NULL);
408 sr_err("devid failed");
415 sock = hci_open_dev(id);
417 perror("open HCI socket");
425 static void sr_bt_desc_close(struct sr_bt_desc *desc)
432 hci_close_dev(desc->fd);
441 #define EIR_NAME_COMPLETE 9
443 static int sr_bt_scan_prep(struct sr_bt_desc *desc)
446 uint8_t type, owntype, filter;
447 uint16_t ival, window;
451 struct hci_filter scan_filter;
456 /* TODO Replace magic values with symbolic identifiers. */
457 type = 0x01; /* LE public? */
458 ival = htobs(0x0010);
459 window = htobs(0x0010);
460 owntype = 0x00; /* any? */
463 rc = hci_le_set_scan_parameters(desc->fd,
464 type, ival, window, owntype, filter, timeout);
466 perror("set LE scan params");
473 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
475 perror("set LE scan enable");
479 /* Save the current filter. For later restoration. */
480 slen = sizeof(desc->orig_filter);
481 rc = getsockopt(desc->fd, SOL_HCI, HCI_FILTER,
482 &desc->orig_filter, &slen);
484 perror("getsockopt(HCI_FILTER)");
488 hci_filter_clear(&scan_filter);
489 hci_filter_set_ptype(HCI_EVENT_PKT, &scan_filter);
490 hci_filter_set_event(EVT_LE_META_EVENT, &scan_filter);
491 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
492 &scan_filter, sizeof(scan_filter));
494 perror("setsockopt(HCI_FILTER)");
501 static int sr_bt_scan_post(struct sr_bt_desc *desc)
510 /* Restore previous HCI filter. */
511 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
512 &desc->orig_filter, sizeof(desc->orig_filter));
514 perror("setsockopt(HCI_FILTER)");
521 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
528 static int sr_bt_scan_proc(struct sr_bt_desc *desc,
529 sr_bt_scan_cb scan_cb, void *cb_data,
530 uint8_t *data, size_t dlen, le_advertising_info *info)
539 if (type == EIR_NAME_COMPLETE) {
540 ba2str(&info->bdaddr, addr);
541 name = g_strndup((const char *)&data[1], dlen - 1);
543 scan_cb(cb_data, addr, name);
548 /* Unknown or unsupported type, ignore silently. */
552 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration)
556 uint8_t buf[HCI_MAX_EVENT_SIZE];
557 ssize_t rdlen, rdpos;
558 evt_le_meta_event *meta;
559 le_advertising_info *info;
565 sr_dbg("BLE scan (LE)");
567 rc = sr_bt_desc_open(desc, NULL);
571 rc = sr_bt_scan_prep(desc);
575 deadline = time(NULL);
576 deadline += duration;
577 while (time(NULL) <= deadline) {
579 if (sr_bt_check_socket_usable(desc) < 0)
581 rdlen = sr_bt_read(desc, buf, sizeof(buf));
588 if (rdlen < 1 + HCI_EVENT_HDR_SIZE)
590 meta = (void *)&buf[1 + HCI_EVENT_HDR_SIZE];
591 rdlen -= 1 + HCI_EVENT_HDR_SIZE;
592 if (meta->subevent != EVT_LE_ADVERTISING_REPORT)
594 info = (void *)&meta->data[1];
595 sr_spew("evt: type %d, len %d", info->evt_type, info->length);
600 while (rdpos < rdlen) {
601 datalen = info->data[rdpos];
602 dataptr = &info->data[1 + rdpos];
603 if (rdpos + 1 + datalen > info->length)
605 rdpos += 1 + datalen;
606 rc = sr_bt_scan_proc(desc,
607 desc->scan_cb, desc->scan_cb_data,
608 dataptr, datalen, info);
614 rc = sr_bt_scan_post(desc);
618 sr_bt_desc_close(desc);
623 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration)
625 int dev_id, sock, rsp_max;
629 size_t rsp_count, idx;
635 sr_dbg("BLE scan (BT)");
637 sock = sr_bt_desc_open(desc, &dev_id);
642 info = g_malloc0(rsp_max * sizeof(*info));
643 flags = 0 /* | IREQ_CACHE_FLUSH */;
644 inq_rc = hci_inquiry(dev_id, duration, rsp_max, NULL, &info, flags);
646 perror("hci_inquiry");
649 for (idx = 0; idx < rsp_count; idx++) {
650 memset(addr, 0, sizeof(addr));
651 ba2str(&info[idx].bdaddr, addr);
652 memset(name, 0, sizeof(name));
653 if (hci_read_remote_name(sock, &info[idx].bdaddr, sizeof(name), name, 0) < 0)
654 snprintf(name, sizeof(name), "[unknown]");
656 desc->scan_cb(desc->scan_cb_data, addr, name);
660 sr_bt_desc_close(desc);
666 /* {{{ connect/disconnect */
668 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc)
670 struct sockaddr_l2 sl2;
677 if (!desc->remote_addr[0])
679 sr_dbg("BLE connect, remote addr %s", desc->remote_addr);
681 s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, 0);
683 perror("socket create");
688 memset(&sl2, 0, sizeof(sl2));
689 sl2.l2_family = AF_BLUETOOTH;
691 if (desc->local_addr[0])
692 str2ba(desc->local_addr, &mac);
695 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
696 sl2.l2_cid = L2CAP_FC_CONNLESS;
697 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
698 ret = bind(s, (void *)&sl2, sizeof(sl2));
705 struct bt_security buf = {
706 .level = BT_SECURITY_LOW,
709 ret = setsockopt(s, SOL_BLUETOOTH, BT_SECURITY, &buf, sizeof(buf));
711 perror("setsockopt");
716 deadline = g_get_monotonic_time();
717 deadline += CONNECT_BLE_TIMEOUT * 1000 * 1000;
718 str2ba(desc->remote_addr, &mac);
719 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
720 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
721 ret = connect(s, (void *)&sl2, sizeof(sl2));
723 * Cope with "in progress" condition. Keep polling the status
724 * until connect() completes, then get the error by means of
725 * getsockopt(). See the connect(2) manpage for details.
727 if (ret < 0 && errno == EINPROGRESS) {
728 struct pollfd fds[1];
733 * We seem to get here ("connect in progress") even when
734 * the specified peer is not around at all. Which results
735 * in extended periods of time where nothing happens, and
736 * an application timeout seems to be required.
738 sr_spew("in progress ...");
741 memset(fds, 0, sizeof(fds));
743 fds[0].events = POLLOUT;
744 ret = poll(fds, ARRAY_SIZE(fds), -1);
751 if (!(fds[0].revents & POLLOUT))
753 if (g_get_monotonic_time() >= deadline) {
754 sr_warn("Connect attempt timed out");
758 memset(fds, 0, sizeof(fds));
760 fds[0].events = POLLNVAL;
761 ret = poll(fds, 1, 0);
763 perror("poll(INVAL)");
767 /* socket fd is invalid(?) */
772 solen = sizeof(soerror);
773 ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &solen);
775 perror("getsockopt(SO_ERROR)");
779 /* connect(2) failed, SO_ERROR has the error code. */
781 perror("connect(PROGRESS)");
786 * TODO Get the receive MTU here?
787 * getsockopt(SOL_BLUETOOTH, BT_RCVMTU, u16);
798 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc)
800 struct sockaddr_rc addr;
805 if (!desc->remote_addr[0])
807 sr_dbg("RFCOMM connect, remote addr %s, channel %zu",
808 desc->remote_addr, desc->rfcomm_channel);
810 if (!desc->rfcomm_channel)
811 desc->rfcomm_channel = 1;
813 memset(&addr, 0, sizeof(addr));
814 addr.rc_family = AF_BLUETOOTH;
815 str2ba(desc->remote_addr, &addr.rc_bdaddr);
816 addr.rc_channel = desc->rfcomm_channel;
819 * There are cases where connect returns EBUSY if we are re-connecting
820 * to a device. Try multiple times to work around this issue.
822 for (i = 0; i < CONNECT_RFCOMM_TRIES; i++) {
823 fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
829 rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
831 sr_spew("connected");
834 } else if (rc < 0 && errno == EBUSY) {
836 g_usleep(CONNECT_RFCOMM_RETRY_MS * 1000);
844 sr_err("Connect failed, device busy.");
849 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc)
851 sr_dbg("BLE disconnect");
855 sr_bt_desc_close(desc);
858 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc)
860 struct pollfd fds[1];
868 memset(fds, 0, sizeof(fds));
869 fds[0].fd = desc->fd;
870 fds[0].events = POLLERR | POLLHUP;
871 ret = poll(fds, ARRAY_SIZE(fds), 0);
876 if (fds[0].revents & POLLHUP)
878 if (fds[0].revents & POLLERR)
880 if (fds[0].revents & POLLNVAL)
886 /* }}} connect/disconnect */
887 /* {{{ indication/notification */
889 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc)
891 uint8_t buf[sizeof(desc->cccd_value)];
896 sr_dbg("BLE start notify");
898 if (sr_bt_check_socket_usable(desc) < 0)
901 bt_put_le16(desc->cccd_value, buf);
902 wrlen = sr_bt_char_write_req(desc, desc->cccd_handle, buf, sizeof(buf));
903 if (wrlen != sizeof(buf))
909 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc)
914 uint16_t packet_handle;
915 uint8_t *packet_data;
921 if (sr_bt_check_socket_usable(desc) < 0)
924 /* Get another message from the Bluetooth socket. */
925 rdlen = sr_bt_read(desc, buf, sizeof(buf));
931 /* Get header fields and references to the payload data. */
933 packet_handle = 0x0000;
937 packet_type = buf[0];
939 packet_handle = bt_get_le16(&buf[1]);
940 packet_data = &buf[3];
941 packet_dlen = rdlen - 3;
944 /* Dispatch according to the message type. */
945 switch (packet_type) {
946 case BLE_ATT_ERROR_RESP:
947 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "error response");
950 case BLE_ATT_WRITE_RESP:
951 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "write response");
954 case BLE_ATT_HANDLE_INDICATION:
955 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle indication");
956 sr_bt_write_type(desc, BLE_ATT_HANDLE_CONFIRMATION);
957 if (packet_handle != desc->read_handle)
963 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
964 case BLE_ATT_HANDLE_NOTIFICATION:
965 sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle notification");
966 if (packet_handle != desc->read_handle)
972 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
974 sr_spew("unsupported type 0x%02x", packet_type);
981 /* }}} indication/notification */
984 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
985 const void *data, size_t len)
992 if (sr_bt_check_socket_usable(desc) < 0)
995 /* Send TX data to the writable characteristics for BLE UART services. */
996 if (desc->write_handle)
997 return sr_bt_char_write_req(desc, desc->write_handle, data, len);
999 /* Send raw TX data to the RFCOMM socket for BT Classic channels. */
1000 return write(desc->fd, data, len);
1003 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type)
1012 if (sr_bt_check_socket_usable(desc) < 0)
1015 wrlen = write(desc->fd, &type, sizeof(type));
1018 if (wrlen < (ssize_t)sizeof(type))
1024 #if WITH_WRITE_TYPE_HANDLE
1025 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
1026 uint8_t type, uint16_t handle)
1028 return sr_bt_write_type_handle_bytes(desc, type, handle, NULL, 0);
1032 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
1033 uint8_t type, uint16_t handle, const uint8_t *data, size_t len)
1035 uint8_t header[sizeof(uint8_t) + sizeof(uint16_t)];
1036 struct iovec iov[2] = {
1037 { .iov_base = header, .iov_len = sizeof(header), },
1038 { .iov_base = (void *)data, .iov_len = len, },
1047 if (sr_bt_check_socket_usable(desc) < 0)
1051 bt_put_le16(handle, &header[1]);
1054 wrlen = writev(desc->fd, iov, ARRAY_SIZE(iov));
1056 wrlen = write(desc->fd, header, sizeof(header));
1060 if (wrlen < (ssize_t)sizeof(header))
1062 wrlen -= sizeof(header);
1067 /* Returns negative upon error, or returns the number of _payload_ bytes written. */
1068 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
1069 uint16_t handle, const void *data, size_t len)
1071 return sr_bt_write_type_handle_bytes(desc, BLE_ATT_WRITE_REQ,
1075 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc, void *data, size_t len)
1077 struct pollfd fds[1];
1086 if (sr_bt_check_socket_usable(desc) < 0)
1089 memset(fds, 0, sizeof(fds));
1090 fds[0].fd = desc->fd;
1091 fds[0].events = POLLIN;
1092 ret = poll(fds, ARRAY_SIZE(fds), 0);
1097 if (!(fds[0].revents & POLLIN))
1100 rdlen = read(desc->fd, data, len);
1105 /* }}} indication/notification */