2 * This file is part of the libsigrok 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/>.
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "serial-bt"
29 #ifdef HAVE_SERIAL_COMM
32 #define SER_BT_CONN_PREFIX "bt"
33 #define SER_BT_CHUNK_SIZE 1200
35 #define SER_BT_PARAM_PREFIX_CHANNEL "channel="
36 #define SER_BT_PARAM_PREFIX_HDL_RX "handle_rx="
37 #define SER_BT_PARAM_PREFIX_HDL_TX "handle_tx="
38 #define SER_BT_PARAM_PREFIX_HDL_CCCD "handle_cccd="
39 #define SER_BT_PARAM_PREFIX_VAL_CCCD "value_cccd="
44 * Serial port handling, wraps the external BT/BLE dependencies.
48 * @defgroup grp_serial_bt Serial port handling, BT/BLE group
50 * Make serial-over-BT communication appear like a regular serial port.
55 /* {{{ support for serial-over-BT channels */
58 * This builtin database of known devices (keyed by their names as
59 * provided during BT/BLE scans) can help improve the presentation of
60 * scan results. Ideally users could take the output and pass it to
61 * subsequent program invocations, not having to "come up with" the
62 * conn= spec, or only having to touch it up minimally. GUI dialogs
63 * could present scan results such that users just need to pick an
64 * item to open a connection.
66 * The current implementation guesses connection types from device
67 * names, and optionally amends them with additional parameters if
68 * experience shows that individual devices need these extra specs.
70 * This database may have to move to a separate source file should
71 * its size grow to amounts that are considered inappropriate here
72 * in the serial transport's BT dispatcher. For now the item count
76 static const struct scan_supported_item {
78 enum ser_bt_conn_t type;
79 const char *add_params;
80 } scan_supported_items[] = {
81 { "121GW", SER_BT_CONN_BLE122, NULL, },
82 { "Adafruit Bluefruit LE 8134", SER_BT_CONN_NRF51, NULL, },
83 { "DL24M_BLE", SER_BT_CONN_AC6328, NULL, },
84 { "DL24M_SPP", SER_BT_CONN_RFCOMM, "/channel=2", },
85 { "HC-05", SER_BT_CONN_RFCOMM, NULL, },
86 { "UC96_BLE", SER_BT_CONN_AC6328, NULL, },
87 { "UC96_SPP", SER_BT_CONN_RFCOMM, "/channel=2", },
88 { "UM25C", SER_BT_CONN_RFCOMM, NULL, },
89 { NULL, SER_BT_CONN_UNKNOWN, NULL, },
92 static const struct scan_supported_item *scan_is_supported(const char *name)
95 const struct scan_supported_item *item;
97 for (idx = 0; idx < ARRAY_SIZE(scan_supported_items); idx++) {
98 item = &scan_supported_items[idx];
101 if (strcmp(name, item->name) != 0)
109 static const char *ser_bt_conn_names[SER_BT_CONN_MAX] = {
110 [SER_BT_CONN_UNKNOWN] = "<type>",
111 [SER_BT_CONN_RFCOMM] = "rfcomm",
112 [SER_BT_CONN_BLE122] = "ble122",
113 [SER_BT_CONN_NRF51] = "nrf51",
114 [SER_BT_CONN_CC254x] = "cc254x",
115 [SER_BT_CONN_AC6328] = "ac6328",
118 static enum ser_bt_conn_t lookup_conn_name(const char *name)
124 return SER_BT_CONN_UNKNOWN;
125 idx = ARRAY_SIZE(ser_bt_conn_names);
127 item = ser_bt_conn_names[idx];
128 if (strcmp(item, name) == 0)
132 return SER_BT_CONN_UNKNOWN;
135 static const char *conn_name_text(enum ser_bt_conn_t type)
137 if (type >= ARRAY_SIZE(ser_bt_conn_names))
138 type = SER_BT_CONN_UNKNOWN;
140 return ser_bt_conn_names[type];
144 * Parse conn= specs for serial over Bluetooth communication.
146 * @param[in] serial The serial port that is about to get opened.
147 * @param[in] spec The caller provided conn= specification.
148 * @param[out] conn_type The type of BT comm (BT RFCOMM, BLE notify).
149 * @param[out] remote_addr The remote device address.
150 * @param[out] rfcomm_channel The RFCOMM channel (if applicable).
151 * @param[out] read_hdl The BLE notify read handle (if applicable).
152 * @param[out] write_hdl The BLE notify write handle (if applicable).
153 * @param[out] cccd_hdl The BLE notify CCCD handle (if applicable).
154 * @param[out] cccd_val The BLE notify CCCD value (if applicable).
156 * @return 0 upon success, non-zero upon failure.
158 * Summary of parsing rules as they are implemented:
159 * - Implementor's note: Automatic scan for available devices is not
160 * yet implemented. So strictly speaking some parts of the input
161 * spec are optional, but fallbacks may not take effect ATM.
162 * - Insist on the "bt" prefix. Accept "bt" alone without any other
164 * - The first field that follows is the connection type. Supported
165 * types are 'rfcomm', 'ble122', 'cc254x', and potentially others
166 * in a future implementation.
167 * - The next field is the remote device's address, either separated
168 * by colons or dashes or spaces, or not separated at all.
169 * - Other parameters (RFCOMM channel, notify handles and write values)
170 * get derived from the connection type.
171 * - More fields after the remote address are options which override
172 * builtin defaults (RFCOMM channels, BLE handles, etc).
174 * Supported formats resulting from these rules:
175 * bt/<conn>/<addr>[/<param>]...
178 * bt/rfcomm/11-22-33-44-55-66
179 * bt/rfcomm/11-22-33-44-55-66/channel=2
180 * bt/ble122/88:6b:12:34:56:78
181 * bt/cc254x/0123456789ab
183 * It's assumed that users easily can create those conn= specs from
184 * available information, or that scan routines will create such specs
185 * that copy'n'paste results (or GUI choices from previous scan results)
186 * can get processed here.
188 static int ser_bt_parse_conn_spec(
189 struct sr_serial_dev_inst *serial, const char *spec,
190 enum ser_bt_conn_t *conn_type, const char **remote_addr,
191 size_t *rfcomm_channel,
192 uint16_t *read_hdl, uint16_t *write_hdl,
193 uint16_t *cccd_hdl, uint16_t *cccd_val)
195 char **fields, *field;
196 enum ser_bt_conn_t type;
199 size_t fields_count, field_idx;
201 unsigned long parm_val;
204 *conn_type = SER_BT_CONN_UNKNOWN;
218 if (!serial || !spec || !spec[0])
221 /* Evaluate the mandatory first three fields. */
222 fields = g_strsplit_set(spec, "/", 0);
225 if (g_strv_length(fields) < 3) {
230 if (strcmp(field, SER_BT_CONN_PREFIX) != 0) {
235 type = lookup_conn_name(field);
243 if (!field || !*field) {
247 addr = g_strdup(field);
251 /* Derive default parameters that match the connection type. */
252 /* TODO Lookup defaults from a table? */
254 case SER_BT_CONN_RFCOMM:
258 case SER_BT_CONN_BLE122:
268 case SER_BT_CONN_NRF51:
270 * Are these values appropriate? Check the learn article at
271 * https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend?view=all
281 /* TODO 'random' type, sec-level=high */
283 case SER_BT_CONN_CC254x:
284 /* TODO Are these values appropriate? Just guessing here. */
294 case SER_BT_CONN_AC6328:
309 * Preset a successful return value for the conn= parse call.
310 * Scan optional additional fields which specify more params.
311 * Update the defaults which were setup above. Pessimize the
312 * routine's return value in error paths.
315 fields_count = g_strv_length(fields);
316 for (field_idx = 3; field_idx < fields_count; field_idx++) {
317 field = fields[field_idx];
318 if (!field || !*field)
320 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_CHANNEL)) {
321 field += strlen(SER_BT_PARAM_PREFIX_CHANNEL);
323 ret = sr_atoul_base(field, &parm_val, &endp, 0);
324 if (ret != SR_OK || !endp || *endp != '\0') {
325 ret_parse = SR_ERR_ARG;
329 *rfcomm_channel = parm_val;
332 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_RX)) {
333 field += strlen(SER_BT_PARAM_PREFIX_HDL_RX);
335 ret = sr_atoul_base(field, &parm_val, &endp, 0);
336 if (ret != SR_OK || !endp || *endp != '\0') {
337 ret_parse = SR_ERR_ARG;
341 *read_hdl = parm_val;
344 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_TX)) {
345 field += strlen(SER_BT_PARAM_PREFIX_HDL_TX);
347 ret = sr_atoul_base(field, &parm_val, &endp, 0);
348 if (ret != SR_OK || !endp || *endp != '\0') {
349 ret_parse = SR_ERR_ARG;
353 *write_hdl = parm_val;
356 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_CCCD)) {
357 field += strlen(SER_BT_PARAM_PREFIX_HDL_CCCD);
359 ret = sr_atoul_base(field, &parm_val, &endp, 0);
360 if (ret != SR_OK || !endp || *endp != '\0') {
361 ret_parse = SR_ERR_ARG;
365 *cccd_hdl = parm_val;
368 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_VAL_CCCD)) {
369 field += strlen(SER_BT_PARAM_PREFIX_VAL_CCCD);
371 ret = sr_atoul_base(field, &parm_val, &endp, 0);
372 if (ret != SR_OK || !endp || *endp != '\0') {
373 ret_parse = SR_ERR_ARG;
377 *cccd_val = parm_val;
387 static void ser_bt_mask_databits(struct sr_serial_dev_inst *serial,
388 uint8_t *data, size_t len)
394 if ((serial->comm_params.data_bits % 8) == 0)
397 mask32 = (1UL << serial->comm_params.data_bits) - 1;
398 mask = mask32 & 0xff;
399 for (idx = 0; idx < len; idx++)
403 static int ser_bt_data_cb(void *cb_data, uint8_t *data, size_t dlen)
405 struct sr_serial_dev_inst *serial;
411 ser_bt_mask_databits(serial, data, dlen);
412 sr_ser_queue_rx_data(serial, data, dlen);
418 /* {{{ wrap serial-over-BT operations in a common serial.c API */
420 /* See if a serial port's name refers to a BT type. */
421 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial)
428 if (!serial->port || !*serial->port)
431 /* Accept either "bt" alone, or "bt/" as a prefix. */
432 if (!g_str_has_prefix(serial->port, SER_BT_CONN_PREFIX))
434 off = strlen(SER_BT_CONN_PREFIX);
435 sep = serial->port[off];
436 if (sep != '\0' && sep != '/')
442 /* The open() wrapper for BT ports. */
443 static int ser_bt_open(struct sr_serial_dev_inst *serial, int flags)
445 enum ser_bt_conn_t conn_type;
446 const char *remote_addr;
447 size_t rfcomm_channel;
448 uint16_t read_hdl, write_hdl, cccd_hdl, cccd_val;
450 struct sr_bt_desc *desc;
454 /* Derive BT specific parameters from the port spec. */
455 rc = ser_bt_parse_conn_spec(serial, serial->port,
456 &conn_type, &remote_addr,
458 &read_hdl, &write_hdl,
459 &cccd_hdl, &cccd_val);
463 if (!conn_type || !remote_addr || !remote_addr[0]) {
464 /* TODO Auto-search for available connections? */
468 /* Create the connection. Only store params after successful use. */
469 desc = sr_bt_desc_new();
472 serial->bt_desc = desc;
473 rc = sr_bt_config_addr_remote(desc, remote_addr);
476 serial->bt_addr_remote = g_strdup(remote_addr);
478 case SER_BT_CONN_RFCOMM:
479 rc = sr_bt_config_rfcomm(desc, rfcomm_channel);
482 serial->bt_rfcomm_channel = rfcomm_channel;
484 case SER_BT_CONN_BLE122:
485 case SER_BT_CONN_NRF51:
486 case SER_BT_CONN_CC254x:
487 case SER_BT_CONN_AC6328:
488 rc = sr_bt_config_notify(desc,
489 read_hdl, write_hdl, cccd_hdl, cccd_val);
492 serial->bt_notify_handle_read = read_hdl;
493 serial->bt_notify_handle_write = write_hdl;
494 serial->bt_notify_handle_cccd = cccd_hdl;
495 serial->bt_notify_value_cccd = cccd_val;
498 /* Unsupported type, or incomplete implementation. */
501 serial->bt_conn_type = conn_type;
503 /* Make sure the receive buffer can accept input data. */
504 if (!serial->rcv_buffer)
505 serial->rcv_buffer = g_string_sized_new(SER_BT_CHUNK_SIZE);
506 rc = sr_bt_config_cb_data(desc, ser_bt_data_cb, serial);
510 /* Open the connection. */
512 case SER_BT_CONN_RFCOMM:
513 rc = sr_bt_connect_rfcomm(desc);
517 case SER_BT_CONN_BLE122:
518 case SER_BT_CONN_NRF51:
519 case SER_BT_CONN_CC254x:
520 case SER_BT_CONN_AC6328:
521 rc = sr_bt_connect_ble(desc);
524 rc = sr_bt_start_notify(desc);
535 static int ser_bt_close(struct sr_serial_dev_inst *serial)
540 if (!serial->bt_desc)
543 sr_bt_disconnect(serial->bt_desc);
544 sr_bt_desc_free(serial->bt_desc);
545 serial->bt_desc = NULL;
547 g_free(serial->bt_addr_local);
548 serial->bt_addr_local = NULL;
549 g_free(serial->bt_addr_remote);
550 serial->bt_addr_remote = NULL;
551 g_slist_free_full(serial->bt_source_args, g_free);
552 serial->bt_source_args = NULL;
557 /* Flush, discards pending RX data, empties buffers. */
558 static int ser_bt_flush(struct sr_serial_dev_inst *serial)
566 /* Drain, waits for completion of pending TX data. */
567 static int ser_bt_drain(struct sr_serial_dev_inst *serial)
570 /* EMPTY */ /* TODO? */
575 static int ser_bt_write(struct sr_serial_dev_inst *serial,
576 const void *buf, size_t count,
577 int nonblocking, unsigned int timeout_ms)
582 * TODO Support chunked transmission when callers' requests
583 * exceed the BT channel's capacity? See ser_hid_write().
586 switch (serial->bt_conn_type) {
587 case SER_BT_CONN_RFCOMM:
590 wrlen = sr_bt_write(serial->bt_desc, buf, count);
594 case SER_BT_CONN_BLE122:
595 case SER_BT_CONN_NRF51:
596 case SER_BT_CONN_CC254x:
597 case SER_BT_CONN_AC6328:
599 * Assume that when applications call the serial layer's
600 * write routine, then the BLE chip/module does support
601 * a TX handle. Just call the serial-BT library's write
606 wrlen = sr_bt_write(serial->bt_desc, buf, count);
616 static int ser_bt_read(struct sr_serial_dev_inst *serial,
617 void *buf, size_t count,
618 int nonblocking, unsigned int timeout_ms)
620 gint64 deadline_us, now_us;
621 uint8_t buffer[SER_BT_CHUNK_SIZE];
627 * Immediately satisfy the caller's request from the RX buffer
628 * if the requested amount of data is available already.
630 if (sr_ser_has_queued_data(serial) >= count)
631 return sr_ser_unqueue_rx_data(serial, buf, count);
634 * When a timeout was specified, then determine the deadline
635 * where to stop reception.
639 now_us = g_get_monotonic_time();
640 deadline_us = now_us + timeout_ms * 1000;
644 * Keep receiving from the port until the caller's requested
645 * amount of data has become available, or the timeout has
646 * expired. In the absence of a timeout, stop reading when an
647 * attempt no longer yields receive data.
650 /* Run another attempt to receive data. */
651 switch (serial->bt_conn_type) {
652 case SER_BT_CONN_RFCOMM:
653 rdlen = sr_bt_read(serial->bt_desc, buffer, sizeof(buffer));
656 rc = ser_bt_data_cb(serial, buffer, rdlen);
660 case SER_BT_CONN_BLE122:
661 case SER_BT_CONN_NRF51:
662 case SER_BT_CONN_CC254x:
663 case SER_BT_CONN_AC6328:
664 dlen = sr_ser_has_queued_data(serial);
665 rc = sr_bt_check_notify(serial->bt_desc);
668 else if (sr_ser_has_queued_data(serial) != dlen)
679 * Stop upon receive errors, or timeout expiration. Only
680 * stop upon empty reception in the absence of a timeout.
684 if (nonblocking && !rdlen)
687 now_us = g_get_monotonic_time();
688 if (now_us > deadline_us)
692 /* Also stop when sufficient data has become available. */
693 if (sr_ser_has_queued_data(serial) >= count)
698 * Satisfy the caller's demand for receive data from previously
699 * queued incoming data.
701 dlen = sr_ser_has_queued_data(serial);
707 return sr_ser_unqueue_rx_data(serial, buf, dlen);
710 struct bt_source_args_t {
711 /* The application callback. */
712 sr_receive_data_callback cb;
714 /* The serial device, to store RX data. */
715 struct sr_serial_dev_inst *serial;
719 * Gets periodically invoked by the glib main loop. "Drives" (checks)
720 * progress of BT communication, and invokes the application's callback
721 * which processes RX data (when some has become available), as well as
722 * handles application level timeouts.
724 static int bt_source_cb(int fd, int revents, void *cb_data)
726 struct bt_source_args_t *args;
727 struct sr_serial_dev_inst *serial;
728 uint8_t rx_buf[SER_BT_CHUNK_SIZE];
736 serial = args->serial;
739 if (!serial->bt_conn_type)
743 * Drain receive data which the channel might have pending.
744 * This is "a copy" of the "background part" of ser_bt_read(),
745 * without the timeout support code, and not knowing how much
746 * data the application is expecting.
749 switch (serial->bt_conn_type) {
750 case SER_BT_CONN_RFCOMM:
751 rdlen = sr_bt_read(serial->bt_desc, rx_buf, sizeof(rx_buf));
754 rc = ser_bt_data_cb(serial, rx_buf, rdlen);
758 case SER_BT_CONN_BLE122:
759 case SER_BT_CONN_NRF51:
760 case SER_BT_CONN_CC254x:
761 case SER_BT_CONN_AC6328:
762 dlen = sr_ser_has_queued_data(serial);
763 rc = sr_bt_check_notify(serial->bt_desc);
766 else if (sr_ser_has_queued_data(serial) != dlen)
778 * When RX data became available (now or earlier), pass this
779 * condition to the application callback. Always periodically
780 * run the application callback, since it handles timeouts and
781 * might carry out other tasks as well like signalling progress.
783 if (sr_ser_has_queued_data(args->serial))
785 rc = args->cb(fd, revents, args->cb_data);
790 /* TODO Can we use the Bluetooth socket's file descriptor? Probably not portably. */
791 #define WITH_MAXIMUM_TIMEOUT_VALUE 0
792 static int ser_bt_setup_source_add(struct sr_session *session,
793 struct sr_serial_dev_inst *serial,
794 int events, int timeout,
795 sr_receive_data_callback cb, void *cb_data)
797 struct bt_source_args_t *args;
802 /* Optionally enforce a minimum poll period. */
803 if (WITH_MAXIMUM_TIMEOUT_VALUE && timeout > WITH_MAXIMUM_TIMEOUT_VALUE)
804 timeout = WITH_MAXIMUM_TIMEOUT_VALUE;
806 /* Allocate status container for background data reception. */
807 args = g_malloc0(sizeof(*args));
809 args->cb_data = cb_data;
810 args->serial = serial;
813 * Have a periodic timer installed. Register the allocated block
814 * with the serial device, since the GSource's finalizer won't
815 * free the memory, and we haven't bothered to create a custom
816 * BT specific GSource.
818 rc = sr_session_source_add(session, -1, events, timeout, bt_source_cb, args);
823 serial->bt_source_args = g_slist_append(serial->bt_source_args, args);
828 static int ser_bt_setup_source_remove(struct sr_session *session,
829 struct sr_serial_dev_inst *serial)
833 (void)sr_session_source_remove(session, -1);
834 /* Release callback args here already? */
839 struct bt_scan_args_t {
841 sr_ser_list_append_t append;
846 static void scan_cb(void *cb_args, const char *addr, const char *name)
848 struct bt_scan_args_t *scan_args;
851 const struct scan_supported_item *item;
852 enum ser_bt_conn_t type;
853 char *port_name, *port_desc;
859 sr_info("BT scan, found: %s - %s\n", addr, name);
861 /* Check whether the device was seen before. */
862 for (l = scan_args->addr_list; l; l = l->next) {
863 if (strcmp(addr, l->data) == 0)
867 /* Substitute colons in the address by dashes. */
870 snprintf(addr_text, sizeof(addr_text), "%s", addr);
871 g_strcanon(addr_text, "0123456789abcdefABCDEF", '-');
873 /* Create a port name, and a description. */
874 item = scan_is_supported(name);
875 type = item ? item->type : SER_BT_CONN_UNKNOWN;
876 port_name = g_strdup_printf("%s/%s/%s%s",
877 SER_BT_CONN_PREFIX, conn_name_text(type), addr_text,
878 (item && item->add_params) ? item->add_params : "");
879 port_desc = g_strdup_printf("%s (%s)", name, scan_args->bt_type);
881 scan_args->port_list = scan_args->append(scan_args->port_list, port_name, port_desc);
885 /* Keep track of the handled address. */
886 addr_copy = g_strdup(addr);
887 scan_args->addr_list = g_slist_append(scan_args->addr_list, addr_copy);
890 static GSList *ser_bt_list(GSList *list, sr_ser_list_append_t append)
892 static const int scan_duration = 3;
894 struct bt_scan_args_t scan_args;
895 struct sr_bt_desc *desc;
898 * Implementor's note: This "list" routine is best-effort. We
899 * assume that registering callbacks always succeeds. Silently
900 * ignore failure to scan for devices. Just return those which
904 desc = sr_bt_desc_new();
908 memset(&scan_args, 0, sizeof(scan_args));
909 scan_args.port_list = list;
910 scan_args.append = append;
912 scan_args.addr_list = NULL;
913 scan_args.bt_type = "BT";
914 (void)sr_bt_config_cb_scan(desc, scan_cb, &scan_args);
915 (void)sr_bt_scan_bt(desc, scan_duration);
916 g_slist_free_full(scan_args.addr_list, g_free);
918 scan_args.addr_list = NULL;
919 scan_args.bt_type = "BLE";
920 (void)sr_bt_config_cb_scan(desc, scan_cb, &scan_args);
921 (void)sr_bt_scan_le(desc, scan_duration);
922 g_slist_free_full(scan_args.addr_list, g_free);
924 sr_bt_desc_free(desc);
926 return scan_args.port_list;
929 static struct ser_lib_functions serlib_bt = {
931 .close = ser_bt_close,
932 .flush = ser_bt_flush,
933 .drain = ser_bt_drain,
934 .write = ser_bt_write,
937 * Bluetooth communication has no concept of bitrate, so ignore
938 * these arguments silently. Neither need we pass the frame format
939 * down to internal BT comm routines, nor need we keep the values
940 * here, since the caller will cache/register them already.
942 .set_params = std_dummy_set_params,
943 .set_handshake = std_dummy_set_handshake,
944 .setup_source_add = ser_bt_setup_source_add,
945 .setup_source_remove = ser_bt_setup_source_remove,
947 .get_frame_format = NULL,
949 SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt = &serlib_bt;
954 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial)
961 SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt = NULL;