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 */
57 static const struct scan_supported_item {
59 enum ser_bt_conn_t type;
60 } scan_supported_items[] = {
61 /* Guess connection types from device names (useful for scans). */
62 { "121GW", SER_BT_CONN_BLE122, },
63 { "Adafruit Bluefruit LE 8134", SER_BT_CONN_NRF51, },
64 { "HC-05", SER_BT_CONN_RFCOMM, },
65 { NULL, SER_BT_CONN_UNKNOWN, },
68 static const char *ser_bt_conn_names[SER_BT_CONN_MAX] = {
69 [SER_BT_CONN_UNKNOWN] = "<type>",
70 [SER_BT_CONN_RFCOMM] = "rfcomm",
71 [SER_BT_CONN_BLE122] = "ble122",
72 [SER_BT_CONN_NRF51] = "nrf51",
73 [SER_BT_CONN_CC254x] = "cc254x",
76 static enum ser_bt_conn_t lookup_conn_name(const char *name)
82 return SER_BT_CONN_UNKNOWN;
83 idx = ARRAY_SIZE(ser_bt_conn_names);
85 item = ser_bt_conn_names[idx];
86 if (strcmp(item, name) == 0)
90 return SER_BT_CONN_UNKNOWN;
93 static const char *conn_name_text(enum ser_bt_conn_t type)
95 if (type >= ARRAY_SIZE(ser_bt_conn_names))
96 type = SER_BT_CONN_UNKNOWN;
98 return ser_bt_conn_names[type];
102 * Parse conn= specs for serial over Bluetooth communication.
104 * @param[in] serial The serial port that is about to get opened.
105 * @param[in] spec The caller provided conn= specification.
106 * @param[out] conn_type The type of BT comm (BT RFCOMM, BLE notify).
107 * @param[out] remote_addr The remote device address.
108 * @param[out] rfcomm_channel The RFCOMM channel (if applicable).
109 * @param[out] read_hdl The BLE notify read handle (if applicable).
110 * @param[out] write_hdl The BLE notify write handle (if applicable).
111 * @param[out] cccd_hdl The BLE notify CCCD handle (if applicable).
112 * @param[out] cccd_val The BLE notify CCCD value (if applicable).
114 * @return 0 upon success, non-zero upon failure.
116 * Summary of parsing rules as they are implemented:
117 * - Implementor's note: Automatic scan for available devices is not
118 * yet implemented. So strictly speaking some parts of the input
119 * spec are optional, but fallbacks may not take effect ATM.
120 * - Insist on the "bt" prefix. Accept "bt" alone without any other
122 * - The first field that follows is the connection type. Supported
123 * types are 'rfcomm', 'ble122', 'cc254x', and potentially others
124 * in a future implementation.
125 * - The next field is the remote device's address, either separated
126 * by colons or dashes or spaces, or not separated at all.
127 * - Other parameters (RFCOMM channel, notify handles and write values)
128 * get derived from the connection type.
129 * - More fields after the remote address are options which override
130 * builtin defaults (RFCOMM channels, BLE handles, etc).
132 * Supported formats resulting from these rules:
133 * bt/<conn>/<addr>[/<param>]...
136 * bt/rfcomm/11-22-33-44-55-66
137 * bt/rfcomm/11-22-33-44-55-66/channel=2
138 * bt/ble122/88:6b:12:34:56:78
139 * bt/cc254x/0123456789ab
141 * It's assumed that users easily can create those conn= specs from
142 * available information, or that scan routines will create such specs
143 * that copy'n'paste results (or GUI choices from previous scan results)
144 * can get processed here.
146 static int ser_bt_parse_conn_spec(
147 struct sr_serial_dev_inst *serial, const char *spec,
148 enum ser_bt_conn_t *conn_type, const char **remote_addr,
149 size_t *rfcomm_channel,
150 uint16_t *read_hdl, uint16_t *write_hdl,
151 uint16_t *cccd_hdl, uint16_t *cccd_val)
153 char **fields, *field;
154 enum ser_bt_conn_t type;
157 size_t fields_count, field_idx;
159 unsigned long parm_val;
162 *conn_type = SER_BT_CONN_UNKNOWN;
176 if (!serial || !spec || !spec[0])
179 /* Evaluate the mandatory first three fields. */
180 fields = g_strsplit_set(spec, "/", 0);
183 if (g_strv_length(fields) < 3) {
188 if (strcmp(field, SER_BT_CONN_PREFIX) != 0) {
193 type = lookup_conn_name(field);
201 if (!field || !*field) {
205 addr = g_strdup(field);
209 /* Derive default parameters that match the connection type. */
210 /* TODO Lookup defaults from a table? */
212 case SER_BT_CONN_RFCOMM:
216 case SER_BT_CONN_BLE122:
226 case SER_BT_CONN_NRF51:
228 * Are these values appropriate? Check the learn article at
229 * https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend?view=all
239 /* TODO 'random' type, sec-level=high */
241 case SER_BT_CONN_CC254x:
242 /* TODO Are these values appropriate? Just guessing here. */
257 * Preset a successful return value for the conn= parse call.
258 * Scan optional additional fields which specify more params.
259 * Update the defaults which were setup above. Pessimize the
260 * routine's return value in error paths.
263 fields_count = g_strv_length(fields);
264 for (field_idx = 3; field_idx < fields_count; field_idx++) {
265 field = fields[field_idx];
266 if (!field || !*field)
268 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_CHANNEL)) {
269 field += strlen(SER_BT_PARAM_PREFIX_CHANNEL);
271 ret = sr_atoul_base(field, &parm_val, &endp, 0);
272 if (ret != SR_OK || !endp || *endp != '\0') {
273 ret_parse = SR_ERR_ARG;
277 *rfcomm_channel = parm_val;
280 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_RX)) {
281 field += strlen(SER_BT_PARAM_PREFIX_HDL_RX);
283 ret = sr_atoul_base(field, &parm_val, &endp, 0);
284 if (ret != SR_OK || !endp || *endp != '\0') {
285 ret_parse = SR_ERR_ARG;
289 *read_hdl = parm_val;
292 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_TX)) {
293 field += strlen(SER_BT_PARAM_PREFIX_HDL_TX);
295 ret = sr_atoul_base(field, &parm_val, &endp, 0);
296 if (ret != SR_OK || !endp || *endp != '\0') {
297 ret_parse = SR_ERR_ARG;
301 *write_hdl = parm_val;
304 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_HDL_CCCD)) {
305 field += strlen(SER_BT_PARAM_PREFIX_HDL_CCCD);
307 ret = sr_atoul_base(field, &parm_val, &endp, 0);
308 if (ret != SR_OK || !endp || *endp != '\0') {
309 ret_parse = SR_ERR_ARG;
313 *cccd_hdl = parm_val;
316 if (g_str_has_prefix(field, SER_BT_PARAM_PREFIX_VAL_CCCD)) {
317 field += strlen(SER_BT_PARAM_PREFIX_VAL_CCCD);
319 ret = sr_atoul_base(field, &parm_val, &endp, 0);
320 if (ret != SR_OK || !endp || *endp != '\0') {
321 ret_parse = SR_ERR_ARG;
325 *cccd_val = parm_val;
335 static void ser_bt_mask_databits(struct sr_serial_dev_inst *serial,
336 uint8_t *data, size_t len)
342 if ((serial->comm_params.data_bits % 8) == 0)
345 mask32 = (1UL << serial->comm_params.data_bits) - 1;
346 mask = mask32 & 0xff;
347 for (idx = 0; idx < len; idx++)
351 static int ser_bt_data_cb(void *cb_data, uint8_t *data, size_t dlen)
353 struct sr_serial_dev_inst *serial;
359 ser_bt_mask_databits(serial, data, dlen);
360 sr_ser_queue_rx_data(serial, data, dlen);
366 /* {{{ wrap serial-over-BT operations in a common serial.c API */
368 /* See if a serial port's name refers to a BT type. */
369 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial)
376 if (!serial->port || !*serial->port)
379 /* Accept either "bt" alone, or "bt/" as a prefix. */
380 if (!g_str_has_prefix(serial->port, SER_BT_CONN_PREFIX))
382 off = strlen(SER_BT_CONN_PREFIX);
383 sep = serial->port[off];
384 if (sep != '\0' && sep != '/')
390 /* The open() wrapper for BT ports. */
391 static int ser_bt_open(struct sr_serial_dev_inst *serial, int flags)
393 enum ser_bt_conn_t conn_type;
394 const char *remote_addr;
395 size_t rfcomm_channel;
396 uint16_t read_hdl, write_hdl, cccd_hdl, cccd_val;
398 struct sr_bt_desc *desc;
402 /* Derive BT specific parameters from the port spec. */
403 rc = ser_bt_parse_conn_spec(serial, serial->port,
404 &conn_type, &remote_addr,
406 &read_hdl, &write_hdl,
407 &cccd_hdl, &cccd_val);
411 if (!conn_type || !remote_addr || !remote_addr[0]) {
412 /* TODO Auto-search for available connections? */
416 /* Create the connection. Only store params after successful use. */
417 desc = sr_bt_desc_new();
420 serial->bt_desc = desc;
421 rc = sr_bt_config_addr_remote(desc, remote_addr);
424 serial->bt_addr_remote = g_strdup(remote_addr);
426 case SER_BT_CONN_RFCOMM:
427 rc = sr_bt_config_rfcomm(desc, rfcomm_channel);
430 serial->bt_rfcomm_channel = rfcomm_channel;
432 case SER_BT_CONN_BLE122:
433 case SER_BT_CONN_NRF51:
434 case SER_BT_CONN_CC254x:
435 rc = sr_bt_config_notify(desc,
436 read_hdl, write_hdl, cccd_hdl, cccd_val);
439 serial->bt_notify_handle_read = read_hdl;
440 serial->bt_notify_handle_write = write_hdl;
441 serial->bt_notify_handle_cccd = cccd_hdl;
442 serial->bt_notify_value_cccd = cccd_val;
445 /* Unsupported type, or incomplete implementation. */
448 serial->bt_conn_type = conn_type;
450 /* Make sure the receive buffer can accept input data. */
451 if (!serial->rcv_buffer)
452 serial->rcv_buffer = g_string_sized_new(SER_BT_CHUNK_SIZE);
453 rc = sr_bt_config_cb_data(desc, ser_bt_data_cb, serial);
457 /* Open the connection. */
459 case SER_BT_CONN_RFCOMM:
460 rc = sr_bt_connect_rfcomm(desc);
464 case SER_BT_CONN_BLE122:
465 case SER_BT_CONN_NRF51:
466 case SER_BT_CONN_CC254x:
467 rc = sr_bt_connect_ble(desc);
470 rc = sr_bt_start_notify(desc);
481 static int ser_bt_close(struct sr_serial_dev_inst *serial)
486 if (!serial->bt_desc)
489 sr_bt_disconnect(serial->bt_desc);
490 sr_bt_desc_free(serial->bt_desc);
491 serial->bt_desc = NULL;
493 g_free(serial->bt_addr_local);
494 serial->bt_addr_local = NULL;
495 g_free(serial->bt_addr_remote);
496 serial->bt_addr_remote = NULL;
497 g_slist_free_full(serial->bt_source_args, g_free);
498 serial->bt_source_args = NULL;
503 /* Flush, discards pending RX data, empties buffers. */
504 static int ser_bt_flush(struct sr_serial_dev_inst *serial)
512 /* Drain, waits for completion of pending TX data. */
513 static int ser_bt_drain(struct sr_serial_dev_inst *serial)
516 /* EMPTY */ /* TODO? */
521 static int ser_bt_write(struct sr_serial_dev_inst *serial,
522 const void *buf, size_t count,
523 int nonblocking, unsigned int timeout_ms)
528 * TODO Support chunked transmission when callers' requests
529 * exceed the BT channel's capacity? See ser_hid_write().
532 switch (serial->bt_conn_type) {
533 case SER_BT_CONN_RFCOMM:
536 wrlen = sr_bt_write(serial->bt_desc, buf, count);
540 case SER_BT_CONN_BLE122:
541 case SER_BT_CONN_NRF51:
542 case SER_BT_CONN_CC254x:
544 * Assume that when applications call the serial layer's
545 * write routine, then the BLE chip/module does support
546 * a TX handle. Just call the serial-BT library's write
551 wrlen = sr_bt_write(serial->bt_desc, buf, count);
561 static int ser_bt_read(struct sr_serial_dev_inst *serial,
562 void *buf, size_t count,
563 int nonblocking, unsigned int timeout_ms)
565 gint64 deadline_us, now_us;
566 uint8_t buffer[SER_BT_CHUNK_SIZE];
572 * Immediately satisfy the caller's request from the RX buffer
573 * if the requested amount of data is available already.
575 if (sr_ser_has_queued_data(serial) >= count)
576 return sr_ser_unqueue_rx_data(serial, buf, count);
579 * When a timeout was specified, then determine the deadline
580 * where to stop reception.
584 now_us = g_get_monotonic_time();
585 deadline_us = now_us + timeout_ms * 1000;
589 * Keep receiving from the port until the caller's requested
590 * amount of data has become available, or the timeout has
591 * expired. In the absence of a timeout, stop reading when an
592 * attempt no longer yields receive data.
595 /* Run another attempt to receive data. */
596 switch (serial->bt_conn_type) {
597 case SER_BT_CONN_RFCOMM:
598 rdlen = sr_bt_read(serial->bt_desc, buffer, sizeof(buffer));
601 rc = ser_bt_data_cb(serial, buffer, rdlen);
605 case SER_BT_CONN_BLE122:
606 case SER_BT_CONN_NRF51:
607 case SER_BT_CONN_CC254x:
608 dlen = sr_ser_has_queued_data(serial);
609 rc = sr_bt_check_notify(serial->bt_desc);
612 else if (sr_ser_has_queued_data(serial) != dlen)
623 * Stop upon receive errors, or timeout expiration. Only
624 * stop upon empty reception in the absence of a timeout.
628 if (nonblocking && !rdlen)
631 now_us = g_get_monotonic_time();
632 if (now_us > deadline_us)
636 /* Also stop when sufficient data has become available. */
637 if (sr_ser_has_queued_data(serial) >= count)
642 * Satisfy the caller's demand for receive data from previously
643 * queued incoming data.
645 dlen = sr_ser_has_queued_data(serial);
651 return sr_ser_unqueue_rx_data(serial, buf, dlen);
654 struct bt_source_args_t {
655 /* The application callback. */
656 sr_receive_data_callback cb;
658 /* The serial device, to store RX data. */
659 struct sr_serial_dev_inst *serial;
663 * Gets periodically invoked by the glib main loop. "Drives" (checks)
664 * progress of BT communication, and invokes the application's callback
665 * which processes RX data (when some has become available), as well as
666 * handles application level timeouts.
668 static int bt_source_cb(int fd, int revents, void *cb_data)
670 struct bt_source_args_t *args;
671 struct sr_serial_dev_inst *serial;
672 uint8_t rx_buf[SER_BT_CHUNK_SIZE];
680 serial = args->serial;
683 if (!serial->bt_conn_type)
687 * Drain receive data which the channel might have pending.
688 * This is "a copy" of the "background part" of ser_bt_read(),
689 * without the timeout support code, and not knowing how much
690 * data the application is expecting.
693 switch (serial->bt_conn_type) {
694 case SER_BT_CONN_RFCOMM:
695 rdlen = sr_bt_read(serial->bt_desc, rx_buf, sizeof(rx_buf));
698 rc = ser_bt_data_cb(serial, rx_buf, rdlen);
702 case SER_BT_CONN_BLE122:
703 case SER_BT_CONN_NRF51:
704 case SER_BT_CONN_CC254x:
705 dlen = sr_ser_has_queued_data(serial);
706 rc = sr_bt_check_notify(serial->bt_desc);
709 else if (sr_ser_has_queued_data(serial) != dlen)
721 * When RX data became available (now or earlier), pass this
722 * condition to the application callback. Always periodically
723 * run the application callback, since it handles timeouts and
724 * might carry out other tasks as well like signalling progress.
726 if (sr_ser_has_queued_data(args->serial))
728 rc = args->cb(fd, revents, args->cb_data);
733 /* TODO Can we use the Bluetooth socket's file descriptor? Probably not portably. */
734 #define WITH_MAXIMUM_TIMEOUT_VALUE 0
735 static int ser_bt_setup_source_add(struct sr_session *session,
736 struct sr_serial_dev_inst *serial,
737 int events, int timeout,
738 sr_receive_data_callback cb, void *cb_data)
740 struct bt_source_args_t *args;
745 /* Optionally enforce a minimum poll period. */
746 if (WITH_MAXIMUM_TIMEOUT_VALUE && timeout > WITH_MAXIMUM_TIMEOUT_VALUE)
747 timeout = WITH_MAXIMUM_TIMEOUT_VALUE;
749 /* Allocate status container for background data reception. */
750 args = g_malloc0(sizeof(*args));
752 args->cb_data = cb_data;
753 args->serial = serial;
756 * Have a periodic timer installed. Register the allocated block
757 * with the serial device, since the GSource's finalizer won't
758 * free the memory, and we haven't bothered to create a custom
759 * BT specific GSource.
761 rc = sr_session_source_add(session, -1, events, timeout, bt_source_cb, args);
766 serial->bt_source_args = g_slist_append(serial->bt_source_args, args);
771 static int ser_bt_setup_source_remove(struct sr_session *session,
772 struct sr_serial_dev_inst *serial)
776 (void)sr_session_source_remove(session, -1);
777 /* Release callback args here already? */
782 static enum ser_bt_conn_t scan_is_supported(const char *name)
785 const struct scan_supported_item *item;
787 for (idx = 0; idx < ARRAY_SIZE(scan_supported_items); idx++) {
788 item = &scan_supported_items[idx];
791 if (strcmp(name, item->name) != 0)
796 return SER_BT_CONN_UNKNOWN;
799 struct bt_scan_args_t {
801 sr_ser_list_append_t append;
806 static void scan_cb(void *cb_args, const char *addr, const char *name)
808 struct bt_scan_args_t *scan_args;
811 enum ser_bt_conn_t type;
812 char *port_name, *port_desc;
818 sr_info("BT scan, found: %s - %s\n", addr, name);
820 /* Check whether the device was seen before. */
821 for (l = scan_args->addr_list; l; l = l->next) {
822 if (strcmp(addr, l->data) == 0)
826 /* Substitute colons in the address by dashes. */
829 snprintf(addr_text, sizeof(addr_text), "%s", addr);
830 g_strcanon(addr_text, "0123456789abcdefABCDEF", '-');
832 /* Create a port name, and a description. */
833 type = scan_is_supported(name);
834 port_name = g_strdup_printf("%s/%s/%s",
835 SER_BT_CONN_PREFIX, conn_name_text(type), addr_text);
836 port_desc = g_strdup_printf("%s (%s)", name, scan_args->bt_type);
838 scan_args->port_list = scan_args->append(scan_args->port_list, port_name, port_desc);
842 /* Keep track of the handled address. */
843 addr_copy = g_strdup(addr);
844 scan_args->addr_list = g_slist_append(scan_args->addr_list, addr_copy);
847 static GSList *ser_bt_list(GSList *list, sr_ser_list_append_t append)
849 static const int scan_duration = 2;
851 struct bt_scan_args_t scan_args;
852 struct sr_bt_desc *desc;
855 * Implementor's note: This "list" routine is best-effort. We
856 * assume that registering callbacks always succeeds. Silently
857 * ignore failure to scan for devices. Just return those which
861 desc = sr_bt_desc_new();
865 memset(&scan_args, 0, sizeof(scan_args));
866 scan_args.port_list = list;
867 scan_args.append = append;
869 scan_args.addr_list = NULL;
870 scan_args.bt_type = "BT";
871 (void)sr_bt_config_cb_scan(desc, scan_cb, &scan_args);
872 (void)sr_bt_scan_bt(desc, scan_duration);
873 g_slist_free_full(scan_args.addr_list, g_free);
875 scan_args.addr_list = NULL;
876 scan_args.bt_type = "BLE";
877 (void)sr_bt_config_cb_scan(desc, scan_cb, &scan_args);
878 (void)sr_bt_scan_le(desc, scan_duration);
879 g_slist_free_full(scan_args.addr_list, g_free);
881 sr_bt_desc_free(desc);
883 return scan_args.port_list;
886 static struct ser_lib_functions serlib_bt = {
888 .close = ser_bt_close,
889 .flush = ser_bt_flush,
890 .drain = ser_bt_drain,
891 .write = ser_bt_write,
894 * Bluetooth communication has no concept of bitrate, so ignore
895 * these arguments silently. Neither need we pass the frame format
896 * down to internal BT comm routines, nor need we keep the values
897 * here, since the caller will cache/register them already.
899 .set_params = std_dummy_set_params,
900 .set_handshake = std_dummy_set_handshake,
901 .setup_source_add = ser_bt_setup_source_add,
902 .setup_source_remove = ser_bt_setup_source_remove,
904 .get_frame_format = NULL,
906 SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt = &serlib_bt;
911 SR_PRIV int ser_name_is_bt(struct sr_serial_dev_inst *serial)
918 SR_PRIV struct ser_lib_functions *ser_lib_funcs_bt = NULL;