return -1;
sr_dbg("BLE open");
- sr_spew("get devid");
if (desc->local_addr[0]) {
id = hci_devid(desc->local_addr);
} else if (desc->remote_addr[0]) {
if (id_ref)
*id_ref = id;
- sr_spew("open HCI socket");
sock = hci_open_dev(id);
if (sock < 0) {
perror("open HCI socket");
if (!desc)
return -1;
- sr_dbg("BLE scan prep");
/* TODO Replace magic values with symbolic identifiers. */
- sr_spew("set LE scan params");
type = 0x01; /* LE public? */
ival = htobs(0x0010);
window = htobs(0x0010);
return -1;
}
- sr_spew("set LE scan enable");
enable = 1;
dup = 1;
timeout = 1000;
}
/* Save the current filter. For later restoration. */
- sr_spew("get HCI filter");
slen = sizeof(desc->orig_filter);
rc = getsockopt(desc->fd, SOL_HCI, HCI_FILTER,
&desc->orig_filter, &slen);
return -1;
}
- sr_spew("set HCI filter");
hci_filter_clear(&scan_filter);
hci_filter_set_ptype(HCI_EVENT_PKT, &scan_filter);
hci_filter_set_event(EVT_LE_META_EVENT, &scan_filter);
if (!desc)
return -1;
- sr_dbg("BLE scan post");
/* Restore previous HCI filter. */
- sr_spew("set HCI filter");
rc = setsockopt(desc->fd, SOL_HCI, HCI_FILTER,
&desc->orig_filter, sizeof(desc->orig_filter));
if (rc < 0) {
return -1;
}
- sr_spew("set LE scan enable");
enable = 0;
dup = 1;
timeout = 1000;
return -1;
sr_dbg("BLE scan (LE)");
- sr_spew("desc open");
rc = sr_bt_desc_open(desc, NULL);
if (rc < 0)
return -1;
- sr_spew("scan prep");
rc = sr_bt_scan_prep(desc);
if (rc < 0)
return -1;
- sr_spew("scan loop");
deadline = time(NULL);
deadline += duration;
while (time(NULL) <= deadline) {
if (rdlen < 0)
break;
if (!rdlen) {
- sr_spew("usleep() start");
g_usleep(50000);
- sr_spew("usleep() done");
continue;
}
if (rdlen < 1 + HCI_EVENT_HDR_SIZE)
}
}
- sr_spew("scan post");
rc = sr_bt_scan_post(desc);
if (rc < 0)
return -1;
return -1;
sr_dbg("BLE connect, remote addr %s", desc->remote_addr);
- sr_spew("socket()");
s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, 0);
if (s < 0) {
perror("socket create");
}
desc->fd = s;
- sr_spew("bind()");
memset(&sl2, 0, sizeof(sl2));
sl2.l2_family = AF_BLUETOOTH;
sl2.l2_psm = 0;
.level = BT_SECURITY_LOW,
.key_size = 0,
};
- sr_spew("security");
ret = setsockopt(s, SOL_BLUETOOTH, BT_SECURITY, &buf, sizeof(buf));
if (ret < 0) {
perror("setsockopt");
}
}
- sr_spew("connect()");
str2ba(desc->remote_addr, &mac);
memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
sr_spew("in progress ...");
do {
- sr_spew("poll(OUT)");
memset(fds, 0, sizeof(fds));
fds[0].fd = s;
fds[0].events = POLLOUT;
if (!(fds[0].revents & POLLOUT))
continue;
} while (1);
- sr_spew("poll(INVAL)");
memset(fds, 0, sizeof(fds));
fds[0].fd = s;
fds[0].events = POLLNVAL;
close(s);
return -1;
}
- sr_spew("getsocktop(SO_ERROR)");
solen = sizeof(soerror);
ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &solen);
if (ret < 0) {
* getsockopt(SOL_BLUETOOTH, BT_RCVMTU, u16);
*/
}
- sr_spew("connect() => rc %d, fd %d", ret, desc->fd);
if (ret < 0) {
perror("connect");
return ret;
if (!desc->rfcomm_channel)
desc->rfcomm_channel = 1;
- sr_spew("socket()");
fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (fd < 0) {
perror("socket");
}
desc->fd = fd;
- sr_spew("connect()");
memset(&addr, 0, sizeof(addr));
addr.rc_family = AF_BLUETOOTH;
str2ba(desc->remote_addr, &addr.rc_bdaddr);
struct pollfd fds[1];
int ret;
- sr_spew("socket usability check");
if (!desc)
return -1;
if (desc->fd < 0)
if (sr_bt_check_socket_usable(desc) < 0)
return -2;
- sr_spew("write()");
bt_put_le16(desc->cccd_value, buf);
wrlen = sr_bt_char_write_req(desc, desc->cccd_handle, buf, sizeof(buf));
if (wrlen != sizeof(buf))
uint8_t *packet_data;
size_t packet_dlen;
- sr_dbg("BLE check notify");
if (!desc)
return -1;
return -2;
/* Get another message from the Bluetooth socket. */
- sr_spew("read() non-blocking");
rdlen = sr_bt_read(desc, buf, sizeof(buf));
- sr_spew("read() => %zd", rdlen);
if (rdlen < 0)
return -2;
if (!rdlen)
return 0;
- sr_spew("read() len %zd, type 0x%02x", rdlen, buf[0]);
/* Get header fields and references to the payload data. */
packet_type = 0x00;
/* Dispatch according to the message type. */
switch (packet_type) {
case BLE_ATT_ERROR_RESP:
- sr_spew("error response");
+ sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "error response");
/* EMPTY */
break;
case BLE_ATT_WRITE_RESP:
- sr_spew("write response");
+ sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "write response");
/* EMPTY */
break;
case BLE_ATT_HANDLE_INDICATION:
- sr_spew("handle indication");
+ sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle indication");
sr_bt_write_type(desc, BLE_ATT_HANDLE_CONFIRMATION);
if (packet_handle != desc->read_handle)
return -4;
return 0;
return desc->data_cb(desc->data_cb_data, packet_data, packet_dlen);
case BLE_ATT_HANDLE_NOTIFICATION:
- sr_spew("handle notification");
+ sr_spew("read() len %zd, type 0x%02x (%s)", rdlen, buf[0], "handle notification");
if (packet_handle != desc->read_handle)
return -4;
if (!packet_data)
SR_PRIV ssize_t sr_bt_write(struct sr_bt_desc *desc,
const void *data, size_t len)
{
- sr_dbg("BLE write (raw)");
if (!desc)
return -1;
if (desc->fd < 0)
{
ssize_t wrlen;
- sr_dbg("BLE write (type)");
if (!desc)
return -1;
if (desc->fd < 0)
static ssize_t sr_bt_write_type_handle(struct sr_bt_desc *desc,
uint8_t type, uint16_t handle)
{
- sr_dbg("BLE write (type, handle)");
return sr_bt_write_type_handle_bytes(desc, type, handle, NULL, 0);
}
#endif
};
ssize_t wrlen;
- sr_dbg("BLE write (type, handle, data)");
if (!desc)
return -1;
if (desc->fd < 0)
static ssize_t sr_bt_char_write_req(struct sr_bt_desc *desc,
uint16_t handle, const void *data, size_t len)
{
- sr_dbg("BLE write-char req");
-
return sr_bt_write_type_handle_bytes(desc, BLE_ATT_WRITE_REQ,
handle, data, len);
}
int ret;
ssize_t rdlen;
- sr_dbg("BLE read (non-blocking)");
if (!desc)
return -1;
if (desc->fd < 0)
if (sr_bt_check_socket_usable(desc) < 0)
return -2;
- sr_spew("poll(POLLIN)");
memset(fds, 0, sizeof(fds));
fds[0].fd = desc->fd;
fds[0].events = POLLIN;
ret = poll(fds, ARRAY_SIZE(fds), 0);
- sr_spew("poll(%d, POLLIN) => 0x%x", desc->fd, fds[0].revents);
if (ret < 0)
return ret;
if (!ret)
if (!(fds[0].revents & POLLIN))
return 0;
- sr_spew("read()");
rdlen = read(desc->fd, data, len);
- sr_spew("read() => %zd", rdlen);
return rdlen;
}