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_dbg("DIAG: hci_devinfo(%zu) => rc %d", idx, rc);
224 rc = ba2str(&info.bdaddr, addr);
225 sr_dbg("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 sr_spew("get devid");
398 if (desc->local_addr[0]) {
399 id = hci_devid(desc->local_addr);
400 } else if (desc->remote_addr[0]) {
401 str2ba(desc->remote_addr, &mac);
402 id = hci_get_route(&mac);
404 id = hci_get_route(NULL);
407 sr_spew("devid failed");
414 sr_spew("open HCI socket");
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;
455 sr_dbg("BLE scan prep");
457 /* TODO Replace magic values with symbolic identifiers. */
458 sr_spew("set LE scan params");
459 type = 0x01; /* LE public? */
460 ival = htobs(0x0010);
461 window = htobs(0x0010);
462 owntype = 0x00; /* any? */
465 rc = hci_le_set_scan_parameters(desc->fd,
466 type, ival, window, owntype, filter, timeout);
468 perror("set LE scan params");
472 sr_spew("set LE scan enable");
476 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
478 perror("set LE scan enable");
482 /* Save the current filter. For later restoration. */
483 sr_spew("get HCI filter");
484 slen = sizeof(desc->orig_filter);
485 rc = getsockopt(desc->fd, SOL_HCI, HCI_FILTER,
486 &desc->orig_filter, &slen);
488 perror("getsockopt(HCI_FILTER)");
492 sr_spew("set HCI filter");
493 hci_filter_clear(&scan_filter);
494 hci_filter_set_ptype(HCI_EVENT_PKT, &scan_filter);
495 hci_filter_set_event(EVT_LE_META_EVENT, &scan_filter);
496 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
497 &scan_filter, sizeof(scan_filter));
499 perror("setsockopt(HCI_FILTER)");
506 static int sr_bt_scan_post(struct sr_bt_desc *desc)
514 sr_dbg("BLE scan post");
516 /* Restore previous HCI filter. */
517 sr_spew("set HCI filter");
518 rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
519 &desc->orig_filter, sizeof(desc->orig_filter));
521 perror("setsockopt(HCI_FILTER)");
525 sr_spew("set LE scan enable");
529 rc = hci_le_set_scan_enable(desc->fd, enable, dup, timeout);
536 static int sr_bt_scan_proc(struct sr_bt_desc *desc,
537 sr_bt_scan_cb scan_cb, void *cb_data,
538 uint8_t *data, size_t dlen, le_advertising_info *info)
547 if (type == EIR_NAME_COMPLETE) {
548 ba2str(&info->bdaddr, addr);
549 name = g_strndup((const char *)&data[1], dlen - 1);
551 scan_cb(cb_data, addr, name);
556 /* Unknown or unsupported type, ignore silently. */
560 SR_PRIV int sr_bt_scan_le(struct sr_bt_desc *desc, int duration)
564 uint8_t buf[HCI_MAX_EVENT_SIZE];
565 ssize_t rdlen, rdpos;
566 evt_le_meta_event *meta;
567 le_advertising_info *info;
573 sr_dbg("BLE scan (LE)");
575 sr_spew("desc open");
576 rc = sr_bt_desc_open(desc, NULL);
580 sr_spew("scan prep");
581 rc = sr_bt_scan_prep(desc);
585 sr_spew("scan loop");
586 deadline = time(NULL);
587 deadline += duration;
588 while (time(NULL) <= deadline) {
590 if (sr_bt_check_socket_usable(desc) < 0)
592 rdlen = sr_bt_read(desc, buf, sizeof(buf));
596 sr_spew("usleep() start");
598 sr_spew("usleep() done");
601 if (rdlen < 1 + HCI_EVENT_HDR_SIZE)
603 meta = (void *)&buf[1 + HCI_EVENT_HDR_SIZE];
604 rdlen -= 1 + HCI_EVENT_HDR_SIZE;
605 if (meta->subevent != EVT_LE_ADVERTISING_REPORT)
607 info = (void *)&meta->data[1];
608 sr_spew("evt: type %d, len %d", info->evt_type, info->length);
613 while (rdpos < rdlen) {
614 datalen = info->data[rdpos];
615 dataptr = &info->data[1 + rdpos];
616 if (rdpos + 1 + datalen > info->length)
618 rdpos += 1 + datalen;
619 rc = sr_bt_scan_proc(desc,
620 desc->scan_cb, desc->scan_cb_data,
621 dataptr, datalen, info);
627 sr_spew("scan post");
628 rc = sr_bt_scan_post(desc);
632 sr_bt_desc_close(desc);
637 SR_PRIV int sr_bt_scan_bt(struct sr_bt_desc *desc, int duration)
639 int dev_id, sock, rsp_max;
643 size_t rsp_count, idx;
649 sr_dbg("BLE scan (BT)");
651 sock = sr_bt_desc_open(desc, &dev_id);
656 info = g_malloc0(rsp_max * sizeof(*info));
657 flags = 0 /* | IREQ_CACHE_FLUSH */;
658 inq_rc = hci_inquiry(dev_id, duration, rsp_max, NULL, &info, flags);
660 perror("hci_inquiry");
663 for (idx = 0; idx < rsp_count; idx++) {
664 memset(addr, 0, sizeof(addr));
665 ba2str(&info[idx].bdaddr, addr);
666 memset(name, 0, sizeof(name));
667 if (hci_read_remote_name(sock, &info[idx].bdaddr, sizeof(name), name, 0) < 0)
668 snprintf(name, sizeof(name), "[unknown]");
670 desc->scan_cb(desc->scan_cb_data, addr, name);
674 sr_bt_desc_close(desc);
680 /* {{{ connect/disconnect */
682 SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc)
684 struct sockaddr_l2 sl2;
690 if (!desc->remote_addr[0])
692 sr_dbg("BLE connect, remote addr %s", desc->remote_addr);
695 s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, 0);
697 perror("socket create");
703 memset(&sl2, 0, sizeof(sl2));
704 sl2.l2_family = AF_BLUETOOTH;
706 if (desc->local_addr[0])
707 str2ba(desc->local_addr, &mac);
710 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
711 sl2.l2_cid = L2CAP_FC_CONNLESS;
712 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
713 ret = bind(s, (void *)&sl2, sizeof(sl2));
720 struct bt_security buf = {
721 .level = BT_SECURITY_LOW,
725 ret = setsockopt(s, SOL_BLUETOOTH, BT_SECURITY, &buf, sizeof(buf));
727 perror("setsockopt");
732 sr_spew("connect()");
733 str2ba(desc->remote_addr, &mac);
734 memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
735 sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
736 ret = connect(s, (void *)&sl2, sizeof(sl2));
738 * Cope with "in progress" condition. Keep polling the status
739 * until connect() completes, then get the error by means of
740 * getsockopt(). See the connect(2) manpage for details.
742 if (ret < 0 && errno == EINPROGRESS) {
743 struct pollfd fds[1];
748 * We seem to get here ("connect in progress") even when
749 * the specified peer is not around at all. Which results
750 * in extended periods of time where nothing happens, and
751 * an application timeout seems to be required.
753 sr_spew("in progress ...");
756 sr_spew("poll(OUT)");
757 memset(fds, 0, sizeof(fds));
759 fds[0].events = POLLOUT;
760 ret = poll(fds, ARRAY_SIZE(fds), -1);
767 if (!(fds[0].revents & POLLOUT))
770 sr_spew("poll(INVAL)");
771 memset(fds, 0, sizeof(fds));
773 fds[0].events = POLLNVAL;
774 ret = poll(fds, 1, 0);
776 perror("poll(INVAL)");
780 /* socket fd is invalid(?) */
785 sr_spew("getsocktop(SO_ERROR)");
786 solen = sizeof(soerror);
787 ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &solen);
789 perror("getsockopt(SO_ERROR)");
793 /* connect(2) failed, SO_ERROR has the error code. */
795 perror("connect(PROGRESS)");
800 * TODO Get the receive MTU here?
801 * getsockopt(SOL_BLUETOOTH, BT_RCVMTU, u16);
804 sr_spew("connect() => rc %d, fd %d", ret, desc->fd);
813 SR_PRIV int sr_bt_connect_rfcomm(struct sr_bt_desc *desc)
815 struct sockaddr_rc addr;
820 if (!desc->remote_addr[0])
822 sr_dbg("RFCOMM connect, remote addr %s, channel %zu",
823 desc->remote_addr, desc->rfcomm_channel);
825 if (!desc->rfcomm_channel)
826 desc->rfcomm_channel = 1;
829 fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
836 sr_spew("connect()");
837 memset(&addr, 0, sizeof(addr));
838 addr.rc_family = AF_BLUETOOTH;
839 str2ba(desc->remote_addr, &addr.rc_bdaddr);
840 addr.rc_channel = desc->rfcomm_channel;
841 rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
846 sr_spew("connected");
851 SR_PRIV void sr_bt_disconnect(struct sr_bt_desc *desc)
853 sr_dbg("BLE disconnect");
857 sr_bt_desc_close(desc);
860 static int sr_bt_check_socket_usable(struct sr_bt_desc *desc)
862 struct pollfd fds[1];
865 sr_spew("socket usability check");
871 memset(fds, 0, sizeof(fds));
872 fds[0].fd = desc->fd;
873 fds[0].events = POLLERR | POLLHUP;
874 ret = poll(fds, ARRAY_SIZE(fds), 0);
879 if (fds[0].revents & POLLHUP)
881 if (fds[0].revents & POLLERR)
883 if (fds[0].revents & POLLNVAL)
889 /* }}} connect/disconnect */
890 /* {{{ indication/notification */
892 SR_PRIV int sr_bt_start_notify(struct sr_bt_desc *desc)
894 uint8_t buf[sizeof(desc->cccd_value)];
899 sr_dbg("BLE start notify");
901 if (sr_bt_check_socket_usable(desc) < 0)
905 bt_put_le16(desc->cccd_value, buf);
906 wrlen = sr_bt_char_write_req(desc, desc->cccd_handle, buf, sizeof(buf));
907 if (wrlen != sizeof(buf))
913 SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc)
918 uint16_t packet_handle;
919 uint8_t *packet_data;
922 sr_dbg("BLE check notify");
926 if (sr_bt_check_socket_usable(desc) < 0)
929 /* Get another message from the Bluetooth socket. */
930 sr_spew("read() non-blocking");
931 rdlen = sr_bt_read(desc, buf, sizeof(buf));
932 sr_spew("read() => %zd", rdlen);
937 sr_spew("read() len %zd, type 0x%02x", rdlen, buf[0]);
939 /* Get header fields and references to the payload data. */
941 packet_handle = 0x0000;
945 packet_type = buf[0];
947 packet_handle = bt_get_le16(&buf[1]);
948 packet_data = &buf[3];
949 packet_dlen = rdlen - 3;
952 /* Dispatch according to the message type. */
953 switch (packet_type) {
954 case BLE_ATT_ERROR_RESP:
955 sr_spew("error response");
958 case BLE_ATT_WRITE_RESP:
959 sr_spew("write response");
962 case BLE_ATT_HANDLE_INDICATION:
963 sr_spew("handle indication");
964 sr_bt_write_type(desc, BLE_ATT_HANDLE_CONFIRMATION);
965 if (packet_handle != desc->read_handle)
971 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
972 case BLE_ATT_HANDLE_NOTIFICATION:
973 sr_spew("handle notification");
974 if (packet_handle != desc->read_handle)
980 return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
982 sr_spew("unsupported type 0x%02x", packet_type);
989 /* }}} indication/notification */
992 SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
993 const void *data, size_t len)
995 sr_dbg("BLE write (raw)");
1001 if (sr_bt_check_socket_usable(desc) < 0)
1004 /* Send TX data to the writable characteristics for BLE UART services. */
1005 if (desc->write_handle)
1006 return sr_bt_char_write_req(desc, desc->write_handle, data, len);
1008 /* Send raw TX data to the RFCOMM socket for BT Classic channels. */
1009 return write(desc->fd, data, len);
1012 static ssize_t sr_bt_write_type(struct sr_bt_desc *desc, uint8_t type)
1016 sr_dbg("BLE write (type)");
1022 if (sr_bt_check_socket_usable(desc) < 0)
1025 wrlen = write(desc->fd, &type, sizeof(type));
1028 if (wrlen < (ssize_t)sizeof(type))
1034 #if WITH_WRITE_TYPE_HANDLE
1035 static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
1036 uint8_t type, uint16_t handle)
1038 sr_dbg("BLE write (type, handle)");
1039 return sr_bt_write_type_handle_bytes(desc, type, handle, NULL, 0);
1043 static ssize_t sr_bt_write_type_handle_bytes(struct sr_bt_desc *desc,
1044 uint8_t type, uint16_t handle, const uint8_t *data, size_t len)
1046 uint8_t header[sizeof(uint8_t) + sizeof(uint16_t)];
1047 struct iovec iov[2] = {
1048 { .iov_base = header, .iov_len = sizeof(header), },
1049 { .iov_base = (void *)data, .iov_len = len, },
1053 sr_dbg("BLE write (type, handle, data)");
1059 if (sr_bt_check_socket_usable(desc) < 0)
1063 bt_put_le16(handle, &header[1]);
1066 wrlen = writev(desc->fd, iov, ARRAY_SIZE(iov));
1068 wrlen = write(desc->fd, header, sizeof(header));
1072 if (wrlen < (ssize_t)sizeof(header))
1074 wrlen -= sizeof(header);
1079 /* Returns negative upon error, or returns the number of _payload_ bytes written. */
1080 static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
1081 uint16_t handle, const void *data, size_t len)
1083 sr_dbg("BLE write-char req");
1085 return sr_bt_write_type_handle_bytes(desc, BLE_ATT_WRITE_REQ,
1089 SR_PRIV ssize_t sr_bt_read(struct sr_bt_desc *desc, void *data, size_t len)
1091 struct pollfd fds[1];
1095 sr_dbg("BLE read (non-blocking)");
1101 if (sr_bt_check_socket_usable(desc) < 0)
1104 sr_spew("poll(POLLIN)");
1105 memset(fds, 0, sizeof(fds));
1106 fds[0].fd = desc->fd;
1107 fds[0].events = POLLIN;
1108 ret = poll(fds, ARRAY_SIZE(fds), 0);
1109 sr_spew("poll(%d, POLLIN) => 0x%x", desc->fd, fds[0].revents);
1114 if (!(fds[0].revents & POLLIN))
1118 rdlen = read(desc->fd, data, len);
1119 sr_spew("read() => %zd", rdlen);
1124 /* }}} indication/notification */