circuitpython/devices/ble_hci/common-hal/_bleio/att.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1796 lines
62 KiB
C
Raw Permalink Normal View History

2020-07-23 18:54:26 -04:00
// Derived from ArduinoBLE.
// Copyright 2020 Dan Halbert for Adafruit Industries
// Some functions here are unused now, but may be used in the future.
// Don't warn or error about this, and depend on the compiler & linker to
// eliminate the associated code.
#pragma GCC diagnostic ignored "-Wunused"
#pragma GCC diagnostic ignored "-Wunused-function"
2020-07-23 18:54:26 -04:00
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hci.h"
#include "att.h"
// Zephyr include files to define HCI communication values and structs.
2021-03-15 09:57:36 -04:00
// #include "hci_include/hci.h"
// #include "hci_include/hci_err.h"
#include "hci_include/att_internal.h"
2020-07-23 18:54:26 -04:00
#include "hci_include/l2cap_internal.h"
#include "py/obj.h"
#include "common-hal/_bleio/Adapter.h"
2020-07-30 22:07:55 -04:00
#include "common-hal/_bleio/Attribute.h"
2020-08-02 11:36:38 -04:00
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Characteristic.h"
#include "shared-bindings/_bleio/Descriptor.h"
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"
2020-07-23 18:54:26 -04:00
#include "supervisor/shared/tick.h"
STATIC uint16_t max_mtu = BT_ATT_DEFAULT_LE_MTU; // 23
STATIC unsigned long timeout = 5000;
2020-07-28 11:56:00 -04:00
STATIC volatile bool confirm;
2020-07-23 18:54:26 -04:00
2020-07-30 22:07:55 -04:00
STATIC uint16_t long_write_handle = BLE_GATT_HANDLE_INVALID;
2021-03-15 09:57:36 -04:00
STATIC uint8_t *long_write_value = NULL;
2020-07-23 18:54:26 -04:00
STATIC uint16_t long_write_value_length = 0;
// When we send a request, fill this struct with info about the expected response.
// We check this against the actual received response.
STATIC struct {
uint16_t conn_handle; // Expected handle.
uint8_t opcode; // Expected RSP opcode.
2021-03-15 09:57:36 -04:00
uint8_t *buffer; // Pointer to response packet
2020-07-23 18:54:26 -04:00
uint8_t length; // Length of response packet.
} expected_rsp;
// A characteristic declaration has this data, in this order:
// See Bluetooth v5.1 spec, section 3.3.1 Characteristic declaration.
typedef struct __packed {
uint8_t properties;
uint16_t value_handle;
2020-12-15 10:58:47 -05:00
uint8_t uuid[]; // 2 or 16 bytes
} characteristic_declaration_t;
STATIC uint8_t bleio_properties_to_ble_spec_properties(uint8_t bleio_properties) {
uint8_t ble_spec_properties = 0;
if (bleio_properties & CHAR_PROP_BROADCAST) {
ble_spec_properties |= BT_GATT_CHRC_BROADCAST;
}
if (bleio_properties & CHAR_PROP_INDICATE) {
ble_spec_properties |= BT_GATT_CHRC_INDICATE;
}
if (bleio_properties & CHAR_PROP_NOTIFY) {
ble_spec_properties |= BT_GATT_CHRC_NOTIFY;
}
if (bleio_properties & CHAR_PROP_READ) {
ble_spec_properties |= BT_GATT_CHRC_READ;
}
if (bleio_properties & CHAR_PROP_WRITE) {
ble_spec_properties |= BT_GATT_CHRC_WRITE;
}
if (bleio_properties & CHAR_PROP_WRITE_NO_RESPONSE) {
ble_spec_properties |= BT_GATT_CHRC_WRITE_WITHOUT_RESP;
}
return ble_spec_properties;
}
// FIX not currently used; re-enable when used.
2020-08-10 19:46:18 -04:00
#if 0
STATIC uint8_t ble_spec_properties_to_bleio_properties(uint8_t ble_spec_properties) {
uint8_t bleio_properties = 0;
if (ble_spec_properties & BT_GATT_CHRC_BROADCAST) {
bleio_properties |= CHAR_PROP_BROADCAST;
}
if (ble_spec_properties & BT_GATT_CHRC_INDICATE) {
bleio_properties |= CHAR_PROP_INDICATE;
}
if (ble_spec_properties & BT_GATT_CHRC_NOTIFY) {
bleio_properties |= CHAR_PROP_NOTIFY;
}
if (ble_spec_properties & BT_GATT_CHRC_READ) {
bleio_properties |= CHAR_PROP_READ;
}
if (ble_spec_properties & BT_GATT_CHRC_WRITE) {
bleio_properties |= CHAR_PROP_WRITE;
}
if (ble_spec_properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) {
bleio_properties |= CHAR_PROP_WRITE_NO_RESPONSE;
}
2020-08-10 19:46:18 -04:00
return bleio_properties;
}
#endif // #if 0
2020-07-23 18:54:26 -04:00
STATIC void send_error(uint16_t conn_handle, uint8_t opcode, uint16_t handle, uint8_t code) {
struct __packed {
struct bt_att_hdr h;
struct bt_att_error_rsp r;
} rsp = { {
2021-03-15 09:57:36 -04:00
.code = BT_ATT_OP_ERROR_RSP,
}, {
.request = opcode,
}};
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
STATIC void send_req(uint16_t conn_handle, size_t request_length, uint8_t *request_buffer) {
2020-07-28 11:56:00 -04:00
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, request_length, request_buffer);
}
2021-03-15 09:57:36 -04:00
STATIC int send_req_wait_for_rsp(uint16_t conn_handle, size_t request_length, uint8_t *request_buffer, uint8_t response_buffer[]) {
2020-07-23 18:54:26 -04:00
// We expect a particular kind of response after this request.
expected_rsp.conn_handle = conn_handle;
// The response opcode is the request opcode + 1.
expected_rsp.opcode = request_buffer[0] + 1;
expected_rsp.buffer = response_buffer;
expected_rsp.length = 0;
2020-07-28 11:56:00 -04:00
send_req(conn_handle, request_length, request_buffer);
2020-07-23 18:54:26 -04:00
if (response_buffer == NULL) {
// not expecting a response.
return 0;
}
for (uint64_t start = supervisor_ticks_ms64(); supervisor_ticks_ms64() - start < timeout;) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
2020-07-23 18:54:26 -04:00
if (!att_handle_is_connected(conn_handle)) {
break;
}
if (expected_rsp.length != 0) {
expected_rsp.conn_handle = 0xffff;
return expected_rsp.length;
}
}
expected_rsp.conn_handle = 0xffff;
return 0;
}
// If a response matches what is in expected_rsp, copy the rest of it into the buffer.
STATIC void check_and_save_expected_rsp(uint16_t conn_handle, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
if (conn_handle == expected_rsp.conn_handle && expected_rsp.opcode == opcode) {
expected_rsp.buffer[0] = opcode;
memcpy(&expected_rsp.buffer[1], data, dlen);
expected_rsp.length = dlen + 1;
}
}
void bleio_att_reset(void) {
2020-07-23 18:54:26 -04:00
max_mtu = BT_ATT_DEFAULT_LE_MTU;
timeout = 5000;
2020-07-30 22:07:55 -04:00
long_write_handle = BLE_GATT_HANDLE_INVALID;
2020-07-23 18:54:26 -04:00
long_write_value = NULL;
long_write_value_length = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
bleio_connections[i].conn_handle = BLE_CONN_HANDLE_INVALID;
bleio_connections[i].role = 0x00;
bleio_connections[i].addr.type = 0;
memset(bleio_connections[i].addr.a.val, 0, sizeof_field(bt_addr_t, val));
bleio_connections[i].mtu = BT_ATT_DEFAULT_LE_MTU;
}
}
bool att_connect_to_address(bt_addr_le_t *addr) {
2021-03-15 09:57:36 -04:00
// FIX
2020-07-23 18:54:26 -04:00
if (hci_le_create_conn(0x0060, 0x0030, 0x00, addr, 0x00,
2021-03-15 09:57:36 -04:00
0x0006, 0x000c, 0x0000, 0x00c8, 0x0004, 0x0006) != 0) {
2020-07-23 18:54:26 -04:00
return false;
}
bool is_connected = false;
for (uint64_t start = supervisor_ticks_ms64(); supervisor_ticks_ms64() - start < timeout;) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
2020-07-23 18:54:26 -04:00
is_connected = att_address_is_connected(addr);
if (is_connected) {
break;
}
}
if (!is_connected) {
hci_le_cancel_conn();
}
return is_connected;
}
2020-08-13 00:03:39 -04:00
bool att_disconnect(uint16_t conn_handle) {
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
2020-07-23 18:54:26 -04:00
return false;
}
hci_disconnect(conn_handle);
2020-08-13 00:03:39 -04:00
// Confirm we're now disconnected.
return !att_handle_is_connected(conn_handle);
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
// FIX
2020-07-23 18:54:26 -04:00
// STATIC bool discover_services(uint16_t conn_handle, BLERemoteDevice* device, const char* serviceUuidFilter) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// BLEUuid serviceUuid(serviceUuidFilter);
// while (reqEnd_handle == 0xffff) {
2020-08-10 19:46:18 -04:00
// int respLength = readByGroupReq(conn_handle, reqStart_handle, reqEnd_handle, BLE_UUID_SERVICE_PRIMARY, response_buffer);
2020-07-23 18:54:26 -04:00
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_READ_GROUP_RSP) {
2020-07-23 18:54:26 -04:00
// uint16_t lengthPerService = response_buffer[1];
// uint8_t uuidLen = lengthPerService - 4;
// for (size_t i = 2; i < respLength; i += lengthPerService) {
// struct __attribute__ ((packed)) RawService {
// uint16_t start_handle;
// uint16_t end_handle;
// uint8_t uuid[16];
// } *rawService = (RawService*)&response_buffer[i];
// if (serviceUuidFilter == NULL ||
// (uuidLen == serviceUuid.length() && memcmp(rawService->uuid, serviceUuid.data(), uuidLen) == 0)) {
// BLERemoteService* service = new BLERemoteService(rawService->uuid, uuidLen,
// rawService->start_handle,
// rawService->end_handle);
// if (service == NULL) {
// return false;
// }
// device->addService(service);
// }
// reqStart_handle = rawService->end_handle + 1;
2020-07-30 22:07:55 -04:00
// if (reqStart_handle == BLE_GATT_HANDLE_INVALID) {
// reqEnd_handle = BLE_GATT_HANDLE_INVALID;
2020-07-23 18:54:26 -04:00
// }
// }
// } else {
2020-07-30 22:07:55 -04:00
// reqEnd_handle = BLE_GATT_HANDLE_INVALID;
2020-07-23 18:54:26 -04:00
// }
// }
// return true;
// }
// STATIC bool discover_characteristics(uint16_t conn_handle, BLERemoteDevice* device) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// int serviceCount = device->serviceCount();
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* service = device->service(i);
// reqStart_handle = service->start_handle();
// reqEnd_handle = service->end_handle();
// while (1) {
2020-08-10 19:46:18 -04:00
// int respLength = readByTypeReq(conn_handle, reqStart_handle, reqEnd_handle, BLE_UUID_CHARACTERISTIC, response_buffer);
2020-07-23 18:54:26 -04:00
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_READ_TYPE_RSP) {
2020-07-23 18:54:26 -04:00
// uint16_t lengthPerCharacteristic = response_buffer[1];
// uint8_t uuidLen = lengthPerCharacteristic - 5;
// for (size_t i = 2; i < respLength; i += lengthPerCharacteristic) {
// struct __attribute__ ((packed)) RawCharacteristic {
// uint16_t start_handle;
// uint8_t properties;
// uint16_t value_handle;
// uint8_t uuid[16];
// } *rawCharacteristic = (RawCharacteristic*)&response_buffer[i];
// BLERemoteCharacteristic* characteristic = new BLERemoteCharacteristic(rawCharacteristic->uuid, uuidLen,
// conn_handle,
// rawCharacteristic->start_handle,
// rawCharacteristic->properties,
// rawCharacteristic->value_handle);
// if (characteristic == NULL) {
// return false;
// }
// service->addCharacteristic(characteristic);
// reqStart_handle = rawCharacteristic->value_handle + 1;
// }
// } else {
// break;
// }
// }
// }
// return true;
// }
// STATIC bool discover_descriptors(uint16_t conn_handle, BLERemoteDevice* device) {
// uint16_t reqStart_handle = 0x0001;
// uint16_t reqEnd_handle = 0xffff;
// uint8_t response_buffer[max_mtu];
// int serviceCount = device->serviceCount();
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* service = device->service(i);
// uint16_t serviceEnd_handle = service->end_handle();
// int characteristicCount = service->characteristicCount();
// for (int j = 0; j < characteristicCount; j++) {
// BLERemoteCharacteristic* characteristic = service->characteristic(j);
// BLERemoteCharacteristic* nextCharacteristic = (j == (characteristicCount - 1)) ? NULL : service->characteristic(j);
// reqStart_handle = characteristic->value_handle() + 1;
// reqEnd_handle = nextCharacteristic ? nextCharacteristic->value_handle() : serviceEnd_handle;
// if (reqStart_handle > reqEnd_handle) {
// continue;
// }
// while (1) {
// int respLength = findInfoReq(conn_handle, reqStart_handle, reqEnd_handle, response_buffer);
// if (respLength == 0) {
// return false;
// }
// if (response_buffer[0] == BT_ATT_OP_FIND_INFO_RSP) {
// uint16_t lengthPerDescriptor = response_buffer[1] * 4;
// uint8_t uuidLen = 2;
// for (size_t i = 2; i < respLength; i += lengthPerDescriptor) {
// struct __attribute__ ((packed)) RawDescriptor {
// uint16_t handle;
// uint8_t uuid[16];
// } *rawDescriptor = (RawDescriptor*)&response_buffer[i];
// BLERemoteDescriptor* descriptor = new BLERemoteDescriptor(rawDescriptor->uuid, uuidLen,
// conn_handle,
// rawDescriptor->handle);
// if (descriptor == NULL) {
// return false;
// }
// characteristic->addDescriptor(descriptor);
// reqStart_handle = rawDescriptor->handle + 1;
// }
// } else {
// break;
// }
// }
// }
// }
// return true;
// }
2021-03-15 09:57:36 -04:00
bool att_discover_attributes(bt_addr_le_t *addr, const char *service_uuid_filter) {
2020-07-23 18:54:26 -04:00
uint16_t conn_handle = att_conn_handle(addr);
if (conn_handle == 0xffff) {
return false;
}
// send MTU request
if (!att_exchange_mtu(conn_handle)) {
return false;
}
// find the device entry for the peeer
2021-03-15 09:57:36 -04:00
// FIX BLERemoteDevice* device = NULL;
2020-07-23 18:54:26 -04:00
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
// if (bleio_connections[i].conn_handle == conn_handle) {
// //FIX if (bleio_connections[i].device == NULL) {
// //FIX
// //bleio_connections[i].device = new BLERemoteDevice();
// //}
2020-07-23 18:54:26 -04:00
// //device = bleio_connections[i].device;
2020-07-23 18:54:26 -04:00
// break;
// }
// }
2020-07-23 18:54:26 -04:00
// //FIX if (device == NULL) {
// // return false;
// // }
2020-07-23 18:54:26 -04:00
// if (service_uuid_filter == NULL) {
// // clear existing services
// //FIX device->clear_services();
// } else {
// //FIX int service_count = device->service_count();
2020-07-23 18:54:26 -04:00
// for (size_t i = 0; i < service_count; i++) {
// //FIX BLERemoteService* service = device->service(i);
2020-07-23 18:54:26 -04:00
// if (strcasecmp(service->uuid(), service_uuid_filter) == 0) {
// // found an existing service with same UUID
// return true;
// }
// }
2020-07-23 18:54:26 -04:00
}
// discover services
2021-03-15 09:57:36 -04:00
// FIX
2020-07-23 18:54:26 -04:00
// if (!att_discover_services(conn_handle, device, service_uuid_filter)) {
// return false;
// }
// // discover characteristics
// if (!discover_characteristics(conn_handle, device)) {
// return false;
// }
// // discover descriptors396
// if (!discover_descriptors(conn_handle, device)) {
// return false;
// }
return true;
}
void att_set_max_mtu(uint16_t max_mtu_in) {
max_mtu = max_mtu_in;
}
void att_set_timeout(unsigned long timeout_in) {
timeout = timeout_in;
}
void att_add_connection(uint16_t handle, uint8_t role, bt_addr_le_t *peer_addr, uint16_t interval, uint16_t latency, uint16_t supervision_timeout, uint8_t master_clock_accuracy) {
2021-03-15 09:57:36 -04:00
(void)interval;
(void)latency;
(void)supervision_timeout;
(void)master_clock_accuracy;
2020-07-23 18:54:26 -04:00
int peer_index = -1;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
peer_index = i;
break;
}
}
if (peer_index == -1) {
// bail, no space
return;
}
bleio_connections[peer_index].conn_handle = handle;
bleio_connections[peer_index].role = role;
2020-07-28 11:56:00 -04:00
bleio_connections[peer_index].mtu = BT_ATT_DEFAULT_LE_MTU;
2020-07-23 18:54:26 -04:00
memcpy(&bleio_connections[peer_index].addr, peer_addr, sizeof(bleio_connections[peer_index].addr));
}
2020-08-18 16:10:09 -04:00
void att_remove_connection(uint16_t conn_handle, uint8_t reason) {
2021-03-15 09:57:36 -04:00
(void)reason;
2020-07-23 18:54:26 -04:00
int peer_index = -1;
int peer_count = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
2020-08-18 16:10:09 -04:00
if (bleio_connections[i].conn_handle == conn_handle) {
2020-07-23 18:54:26 -04:00
peer_index = i;
}
if (bleio_connections[i].conn_handle != 0xffff) {
peer_count++;
}
}
if (peer_index == -1) {
2020-08-10 19:46:18 -04:00
// Peer not found
2020-07-23 18:54:26 -04:00
return;
}
if (peer_count == 1) {
2020-08-10 19:46:18 -04:00
// Clear CCCD values on disconnect.
size_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
2020-08-18 16:10:09 -04:00
for (size_t handle = 1; handle <= max_attribute_handle; handle++) {
2020-08-10 19:46:18 -04:00
mp_obj_t attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
uint16_t zero = 0;
mp_buffer_info_t zero_cccd_value = {
.buf = &zero,
.len = sizeof(zero),
};
if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
2020-08-10 19:46:18 -04:00
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) == BLE_UUID_CCCD) {
common_hal_bleio_descriptor_set_value(descriptor, &zero_cccd_value);
}
}
}
2020-07-23 18:54:26 -04:00
2020-07-30 22:07:55 -04:00
long_write_handle = BLE_GATT_HANDLE_INVALID;
2020-07-23 18:54:26 -04:00
long_write_value_length = 0;
}
bleio_connections[peer_index].conn_handle = 0xffff;
bleio_connections[peer_index].role = 0x00;
memset(&bleio_connections[peer_index].addr, 0x00, sizeof(bleio_connections[peer_index].addr));
2020-07-28 11:56:00 -04:00
bleio_connections[peer_index].mtu = BT_ATT_DEFAULT_LE_MTU;
2020-07-23 18:54:26 -04:00
}
uint16_t att_conn_handle(bt_addr_le_t *addr) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].addr.type == addr->type &&
memcmp(&bleio_connections[i].addr.a.val, addr->a.val, sizeof(addr->a.val)) == 0) {
return bleio_connections[i].conn_handle;
}
}
return 0xffff;
}
bool att_is_connected(void) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle != 0xffff) {
return true;
}
}
return false;
}
bool att_address_is_connected(bt_addr_le_t *addr) {
2021-03-15 09:57:36 -04:00
return att_conn_handle(addr) != 0xffff;
2020-07-23 18:54:26 -04:00
}
bool att_handle_is_connected(uint16_t handle) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == handle) {
return true;
}
}
return false;
}
uint16_t att_mtu(uint16_t handle) {
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == handle) {
return bleio_connections[i].mtu;
}
}
2020-07-28 11:56:00 -04:00
return BT_ATT_DEFAULT_LE_MTU;
2020-07-23 18:54:26 -04:00
}
bool att_disconnect_all(void) {
int num_disconnects = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
2020-08-13 00:03:39 -04:00
if (att_disconnect(bleio_connections[i].conn_handle) != 0) {
2020-07-23 18:54:26 -04:00
continue;
}
num_disconnects++;
bleio_connections[i].conn_handle = 0xffff;
bleio_connections[i].role = 0x00;
bleio_connections[i].addr.type = 0;
memset(bleio_connections[i].addr.a.val, 0, sizeof(bleio_connections[i].addr.a.val));
2020-07-28 11:56:00 -04:00
bleio_connections[i].mtu = BT_ATT_DEFAULT_LE_MTU;
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
return num_disconnects > 0;
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
bool att_notify(uint16_t handle, const uint8_t *value, int length) {
2020-07-23 18:54:26 -04:00
int num_notifications = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
2020-08-02 11:36:38 -04:00
typedef struct __packed {
2020-07-30 22:07:55 -04:00
struct bt_att_hdr hdr;
struct bt_att_notify ntf;
2020-08-02 11:36:38 -04:00
} notify_t;
2020-07-23 18:54:26 -04:00
2020-07-30 22:07:55 -04:00
size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(notify_t)), (uint16_t)length);
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
uint8_t notify_bytes[sizeof(notify_t) + allowed_length];
2021-03-15 09:57:36 -04:00
notify_t *notify = (notify_t *)notify_bytes;
notify->hdr.code = BT_ATT_OP_NOTIFY;
;
2020-08-02 11:36:38 -04:00
notify->ntf.handle = handle;
memcpy(notify->ntf.value, value, allowed_length);
2020-07-30 22:07:55 -04:00
hci_send_acl_pkt(bleio_connections[i].conn_handle, BT_L2CAP_CID_ATT,
2021-03-15 09:57:36 -04:00
sizeof(notify_bytes), notify_bytes);
2020-07-23 18:54:26 -04:00
num_notifications++;
}
2021-03-15 09:57:36 -04:00
return num_notifications > 0;
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
bool att_indicate(uint16_t handle, const uint8_t *value, int length) {
2020-07-23 18:54:26 -04:00
int num_indications = 0;
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == 0xffff) {
continue;
}
2020-08-02 11:36:38 -04:00
typedef struct __packed {
2020-07-30 22:07:55 -04:00
struct bt_att_hdr hdr;
struct bt_att_indicate ind;
2020-08-02 11:36:38 -04:00
} indicate_t;
2020-07-23 18:54:26 -04:00
2020-07-30 22:07:55 -04:00
size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(indicate_t)), (uint16_t)length);
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
uint8_t indicate_bytes[sizeof(indicate_t) + allowed_length];
2021-03-15 09:57:36 -04:00
indicate_t *indicate = (indicate_t *)indicate_bytes;
indicate->hdr.code = BT_ATT_OP_INDICATE;
;
2020-08-02 11:36:38 -04:00
indicate->ind.handle = handle;
memcpy(indicate->ind.value, value, allowed_length);
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
confirm = false;
2020-07-23 18:54:26 -04:00
2020-07-30 22:07:55 -04:00
hci_send_acl_pkt(bleio_connections[i].conn_handle, BT_L2CAP_CID_ATT,
2021-03-15 09:57:36 -04:00
sizeof(indicate_bytes), indicate_bytes);
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
while (!confirm) {
// RUN_BACKGROUND_TASKS includes hci_poll_for_incoming_pkt();
RUN_BACKGROUND_TASKS;
2020-07-23 18:54:26 -04:00
if (!att_address_is_connected(&bleio_connections[i].addr)) {
break;
}
}
num_indications++;
}
2021-03-15 09:57:36 -04:00
return num_indications > 0;
2020-07-23 18:54:26 -04:00
}
STATIC void process_error(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_error_rsp *rsp = (struct bt_att_error_rsp *)data;
2020-07-23 18:54:26 -04:00
if (dlen != sizeof(struct bt_att_error_rsp)) {
// Incorrect size; ignore.
return;
}
// expected_rsp.opcode is an RSP opcode. Does it match the REQ opcode in this response?
if (expected_rsp.conn_handle == conn_handle && (expected_rsp.opcode - 1) == rsp->request) {
expected_rsp.buffer[0] = BT_ATT_OP_ERROR_RSP;
memcpy(&expected_rsp.buffer[1], data, dlen);
expected_rsp.length = dlen + 1;
}
}
STATIC void process_mtu_req(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_exchange_mtu_req *req = (struct bt_att_exchange_mtu_req *)data;
if (dlen != sizeof(struct bt_att_exchange_mtu_req)) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_MTU_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
return;
}
uint16_t mtu = req->mtu;
if (mtu > max_mtu) {
mtu = max_mtu;
}
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
bleio_connections[i].mtu = mtu;
break;
}
}
struct __packed {
struct bt_att_hdr h;
struct bt_att_exchange_mtu_rsp r;
2020-07-23 18:54:26 -04:00
} rsp = { {
2021-03-15 09:57:36 -04:00
.code = BT_ATT_OP_MTU_RSP,
}, {
.mtu = mtu,
}};
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
2020-07-23 18:54:26 -04:00
}
STATIC void process_mtu_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_exchange_mtu_rsp *rsp = (struct bt_att_exchange_mtu_rsp *)data;
2020-07-23 18:54:26 -04:00
if (dlen != sizeof(struct bt_att_exchange_mtu_rsp)) {
return;
}
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
bleio_connections[i].mtu = rsp->mtu;
break;
}
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_MTU_RSP, dlen, data);
}
STATIC void process_find_info_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_find_info_req *req = (struct bt_att_find_info_req *)data;
2020-07-23 18:54:26 -04:00
if (dlen != sizeof(struct bt_att_find_info_req)) {
send_error(conn_handle, BT_ATT_OP_FIND_INFO_REQ, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_find_info_rsp r;
} rsp_t;
2020-07-23 18:54:26 -04:00
uint8_t rsp_bytes[mtu];
2021-03-15 09:57:36 -04:00
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_FIND_INFO_RSP;
2020-07-23 18:54:26 -04:00
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
2020-07-23 18:54:26 -04:00
bool no_data = true;
2020-07-23 18:54:26 -04:00
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_uuid = 0;
2020-07-23 18:54:26 -04:00
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
2020-07-23 18:54:26 -04:00
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
2020-07-23 18:54:26 -04:00
// Fetch the uuid for the given attribute, which might be a characteristic or a descriptor.
bleio_uuid_obj_t *uuid;
2020-07-23 18:54:26 -04:00
if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->handle != handle) {
// If the handles don't match, this is the characteristic definition attribute.
// Skip it. We want the characteristic value attribute.
continue;
}
uuid = characteristic->uuid;
2020-07-23 18:54:26 -04:00
} else {
uuid = bleio_attribute_get_uuid(attribute_obj);
}
const uint32_t sizeof_uuid = common_hal_bleio_uuid_get_size(uuid) / 8;
if (sizeof_first_uuid == 0) {
sizeof_first_uuid = sizeof_uuid;
// All the uuids in the response will be the same size.
rsp->r.format = sizeof_uuid == 2 ? BT_ATT_INFO_16 : BT_ATT_INFO_128;
}
if (sizeof_uuid != sizeof_first_uuid) {
// Previous UUID was a different size. We can't mix sizes.
// Stop and send what we have so far.
break;
}
if (rsp_length + sizeof_uuid > mtu) {
// No remaining room in response for this uuid.
break;
}
if (sizeof_uuid == 2) {
2021-03-15 09:57:36 -04:00
struct bt_att_info_16 *info_16 = (struct bt_att_info_16 *)&rsp_bytes[rsp_length];
info_16->handle = handle;
info_16->uuid = common_hal_bleio_uuid_get_uuid16(uuid);
rsp_length += sizeof(struct bt_att_info_16);
} else {
2021-03-15 09:57:36 -04:00
struct bt_att_info_128 *info_128 = (struct bt_att_info_128 *)&rsp_bytes[rsp_length];
info_128->handle = handle;
common_hal_bleio_uuid_get_uuid128(uuid, info_128->uuid);
rsp_length += sizeof(struct bt_att_info_128);
}
2021-03-15 09:57:36 -04:00
no_data = false;
} // end for
if (no_data) {
send_error(conn_handle, BT_ATT_OP_FIND_INFO_REQ, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
2020-07-23 18:54:26 -04:00
}
static int att_find_info_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint8_t response_buffer[]) {
struct __packed req {
struct bt_att_hdr h;
struct bt_att_find_info_req r;
} req = { {
.code = BT_ATT_OP_FIND_INFO_REQ,
}, {
.start_handle = start_handle,
.end_handle = end_handle,
}};
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
}
2020-07-23 18:54:26 -04:00
STATIC void process_find_info_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen < 2) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_FIND_INFO_RSP, dlen, data);
}
STATIC void process_find_type_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_find_type_req *req = (struct bt_att_find_type_req *)data;
2020-07-23 18:54:26 -04:00
if (dlen < sizeof(struct bt_att_find_type_req)) {
send_error(conn_handle, BT_ATT_OP_FIND_TYPE_RSP, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
uint8_t response[mtu];
uint16_t response_length;
response[0] = BT_ATT_OP_FIND_TYPE_RSP;
response_length = 1;
2021-03-15 09:57:36 -04:00
// FIX
2020-08-10 19:46:18 -04:00
// if (find_type_req->type == BLE_UUID_SERVICE_PRIMARY) {
// for (uint16_t i = (find_type_req->start_handle - 1); i < GATT.attributeCount() && i <= (find_type_req->end_handle - 1); i++) {
2020-07-23 18:54:26 -04:00
// BLELocalAttribute* attribute = GATT.attribute(i);
// if ((attribute->type() == find_type_req->type) && (attribute->uuidLength() == value_length) && memcmp(attribute->uuidData(), value, value_length) == 0) {
2020-07-23 18:54:26 -04:00
// BLELocalService* service = (BLELocalService*)attribute;
// // add the start handle
// uint16_t start_handle = service->start_handle();
// memcpy(&response[response_length], &start_handle, sizeof(start_handle));
// response_length += sizeof(start_handle);
// // add the end handle
// uint16_t end_handle = service->end_handle();
// memcpy(&response[response_length], &end_handle, sizeof(end_handle));
// response_length += sizeof(end_handle);
// }
// if ((response_length + 4) > mtu) {
// break;
// }
// }
// }
if (response_length == 1) {
send_error(conn_handle, BT_ATT_OP_FIND_TYPE_RSP, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
}
}
static void process_read_group_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_read_group_req *req = (struct bt_att_read_group_req *)data;
uint16_t type_uuid = req->uuid[0] | (req->uuid[1] << 8);
2020-07-23 18:54:26 -04:00
// We only support returning services for BT_ATT_OP_READ_GROUP_REQ, which is typically used
// for service discovery.
if (dlen != sizeof(struct bt_att_read_group_req) + sizeof(type_uuid) ||
2020-08-10 19:46:18 -04:00
(type_uuid != BLE_UUID_SERVICE_PRIMARY &&
type_uuid != BLE_UUID_SERVICE_SECONDARY)) {
2020-07-23 18:54:26 -04:00
send_error(conn_handle, BT_ATT_OP_READ_GROUP_REQ, req->start_handle, BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_group_rsp r;
} rsp_t;
uint8_t rsp_bytes[mtu];
2021-03-15 09:57:36 -04:00
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_READ_GROUP_RSP;
rsp->r.len = 0;
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
bool no_data = true;
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_service_uuid = 0;
// Size of a single bt_att_group_data chunk. Start with the initial size, and
// add the uuid size in the loop below.
size_t data_length = sizeof(struct bt_att_group_data);
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
if (rsp_length + data_length > mtu) {
// The next possible bt_att_group_data chunk won't fit. The response is full.
break;
}
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (mp_obj_is_type(attribute_obj, &bleio_service_type)) {
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute_obj);
// Is this a 16-bit or a 128-bit uuid? It must match in size with any previous attribute
// in this transmission.
const uint32_t sizeof_service_uuid = common_hal_bleio_uuid_get_size(service->uuid) / 8;
if (sizeof_first_service_uuid == 0) {
sizeof_first_service_uuid = sizeof_service_uuid;
data_length += sizeof_service_uuid;
} else if (sizeof_first_service_uuid != sizeof_service_uuid) {
// Mismatched sizes, which can't be in the same batch.
// Transmit just what we have so far in this batch.
break;
}
2020-07-23 18:54:26 -04:00
// Pass the length of ONE bt_att_group_data chunk.
// There may be multiple chunks in this transmission.
rsp->r.len = data_length;
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
struct bt_att_group_data *group_data = (struct bt_att_group_data *)&rsp_bytes[rsp_length];
2020-07-23 18:54:26 -04:00
group_data->start_handle = service->start_handle;
group_data->end_handle = service->end_handle;
common_hal_bleio_uuid_pack_into(service->uuid, group_data->value);
2020-07-23 18:54:26 -04:00
rsp_length += data_length;
no_data = false;
}
}
2020-07-23 18:54:26 -04:00
if (no_data) {
2020-07-23 18:54:26 -04:00
send_error(conn_handle, BT_ATT_OP_READ_GROUP_REQ, req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
2020-07-23 18:54:26 -04:00
}
}
static int att_read_group_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid, uint8_t response_buffer[]) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_group_req r;
} req_t;
uint8_t req_bytes[sizeof(req_t) + sizeof(uuid)];
req_t *req = (req_t *)req_bytes;
req->h.code = BT_ATT_OP_READ_GROUP_REQ;
req->r.start_handle = start_handle;
req->r.end_handle = end_handle;
req->r.uuid[0] = uuid & 0xff;
req->r.uuid[1] = uuid >> 8;
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
}
STATIC void process_read_group_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2020-07-23 18:54:26 -04:00
if (dlen < 2) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_GROUP_RSP, dlen, data);
}
STATIC void process_read_or_read_blob_req(uint16_t conn_handle, uint16_t mtu, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
uint16_t handle;
uint16_t offset = 0;
uint8_t response_opcode;
if (opcode == BT_ATT_OP_READ_REQ) {
if (dlen != sizeof(struct bt_att_read_req)) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_READ_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
return;
}
2021-03-15 09:57:36 -04:00
struct bt_att_read_req *req = (struct bt_att_read_req *)data;
2020-07-23 18:54:26 -04:00
handle = req->handle;
response_opcode = BT_ATT_OP_READ_RSP;
} else if (opcode == BT_ATT_OP_READ_BLOB_REQ) {
2020-07-23 18:54:26 -04:00
if (dlen != sizeof(struct bt_att_read_blob_req)) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_READ_BLOB_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
return;
}
2021-03-15 09:57:36 -04:00
struct bt_att_read_blob_req *req = (struct bt_att_read_blob_req *)data;
2020-07-23 18:54:26 -04:00
handle = req->handle;
offset = req->offset;
response_opcode = BT_ATT_OP_READ_BLOB_RSP;
} else {
return;
2020-07-23 18:54:26 -04:00
}
2020-08-10 19:46:18 -04:00
if (handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_rsp r; // Same as bt_att_read_blob_rsp.
} rsp_t;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
uint8_t rsp_bytes[mtu];
2021-03-15 09:57:36 -04:00
rsp_t *rsp = (rsp_t *)rsp_bytes;
2020-08-10 19:46:18 -04:00
rsp->h.code = response_opcode;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
if (mp_obj_is_type(attribute_obj, &bleio_service_type)) {
2020-08-10 19:46:18 -04:00
if (offset) {
send_error(conn_handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG, handle, BT_ATT_ERR_INVALID_PDU);
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute_obj);
const uint32_t sizeof_service_uuid = common_hal_bleio_uuid_get_size(service->uuid) / 8;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
common_hal_bleio_uuid_pack_into(service->uuid, rsp->r.value);
rsp_length += sizeof_service_uuid;
2020-07-23 18:54:26 -04:00
} else if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
2020-08-10 19:46:18 -04:00
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->decl_handle == handle) {
// Read characteristic declaration. Return properties, value handle, and uuid.
if (offset) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
return;
}
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
characteristic_declaration_t *char_decl = (characteristic_declaration_t *)rsp->r.value;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
// Convert from the bleio properties bit values to the BLE spec properties bit values.
// They are not the same :(.
char_decl->properties = bleio_properties_to_ble_spec_properties(characteristic->props);
char_decl->value_handle = characteristic->handle;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
const uint32_t sizeof_char_uuid = common_hal_bleio_uuid_get_size(characteristic->uuid) / 8;
common_hal_bleio_uuid_pack_into(characteristic->uuid, char_decl->uuid);
rsp_length += sizeof_char_uuid;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
} else {
// Read characteristic value.
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
if ((characteristic->props & CHAR_PROP_READ) == 0) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_READ_NOT_PERMITTED);
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
mp_buffer_info_t bufinfo;
mp_get_buffer(characteristic->value, &bufinfo, MP_BUFFER_READ);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
if (offset >= bufinfo.len) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_INVALID_OFFSET);
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
size_t value_length = MIN(mtu - rsp_length, bufinfo.len - offset);
memcpy(rsp->r.value, bufinfo.buf + offset, value_length);
rsp_length += value_length;
}
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
2020-08-10 19:46:18 -04:00
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
mp_buffer_info_t bufinfo;
mp_get_buffer(descriptor->value, &bufinfo, MP_BUFFER_READ);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
if (offset >= bufinfo.len) {
send_error(conn_handle, opcode, handle, BT_ATT_ERR_INVALID_OFFSET);
return;
}
size_t value_length = MIN(mtu - rsp_length, bufinfo.len - offset);
memcpy(rsp->r.value, bufinfo.buf + offset, value_length);
rsp_length += value_length;
}
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
2020-07-23 18:54:26 -04:00
}
STATIC void process_read_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_RSP, dlen, data);
}
STATIC void process_read_type_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_read_type_req *req = (struct bt_att_read_type_req *)data;
uint16_t type_uuid = req->uuid[0] | (req->uuid[1] << 8);
2020-07-23 18:54:26 -04:00
if (dlen != sizeof(struct bt_att_read_type_req) + sizeof(type_uuid)) {
2020-07-23 18:54:26 -04:00
send_error(conn_handle, BT_ATT_OP_READ_TYPE_REQ, req->start_handle, BT_ATT_ERR_INVALID_PDU);
return;
}
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_type_rsp r;
} rsp_t;
2020-07-23 18:54:26 -04:00
uint8_t rsp_bytes[mtu];
2021-03-15 09:57:36 -04:00
rsp_t *rsp = (rsp_t *)rsp_bytes;
rsp->h.code = BT_ATT_OP_READ_TYPE_RSP;
rsp->r.len = 0;
2020-07-23 18:54:26 -04:00
// Keeps track of total length of the response.
size_t rsp_length = sizeof(rsp_t);
2020-07-23 18:54:26 -04:00
bool no_data = true;
2020-07-23 18:54:26 -04:00
// All the data chunks must have uuid's that are the same size.
// Keep track of the first one to make sure.
size_t sizeof_first_uuid = 0;
2020-07-23 18:54:26 -04:00
// Size of a single bt_att_data chunk. Start with the initial size, and
// add the uuid size and other data sizes in the loop below.
size_t data_length = sizeof(struct bt_att_data);
2020-07-23 18:54:26 -04:00
const uint16_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
for (uint16_t handle = req->start_handle;
handle <= max_attribute_handle && handle <= req->end_handle;
handle++) {
2020-07-23 18:54:26 -04:00
if (rsp_length + data_length > mtu) {
// The next possible bt_att_data chunk won't fit. The response is full.
break;
}
2020-07-23 18:54:26 -04:00
mp_obj_t *attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
if (type_uuid == BLE_UUID_CHARACTERISTIC &&
mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
// Request is for characteristic declarations.
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
if (characteristic->handle == handle) {
// If the characteristic's handle is this attribute's handle, skip it:
// it's the attribute for characteristic value. We want to return the declaration
// handle attribute instead. (It will probably get skipped below, by the
// handle++).
continue;
}
2020-07-23 18:54:26 -04:00
// Is this a 16-bit or a 128-bit uuid? It must match in size with any previous attribute
// in this transmission.
const uint32_t sizeof_uuid = common_hal_bleio_uuid_get_size(characteristic->uuid) / 8;
if (sizeof_first_uuid == 0) {
sizeof_first_uuid = sizeof_uuid;
data_length += sizeof_uuid;
data_length += sizeof(characteristic_declaration_t);
} else if (sizeof_first_uuid != sizeof_uuid) {
// Mismatched sizes, which can't be in the same batch.
// Transmit just what we have so far in this batch.
break;
}
2020-07-23 18:54:26 -04:00
// Pass the length of ONE bt_att_data chunk.
// There may be multiple chunks in this transmission.
rsp->r.len = data_length;
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
2020-07-23 18:54:26 -04:00
att_data->handle = characteristic->decl_handle;
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
characteristic_declaration_t *char_decl = (characteristic_declaration_t *)att_data->value;
2020-07-23 18:54:26 -04:00
// Convert from the bleio properties bit values to the BLE spec properties bit values.
// They are not the same :(.
char_decl->properties = bleio_properties_to_ble_spec_properties(characteristic->props);
char_decl->value_handle = characteristic->handle;
common_hal_bleio_uuid_pack_into(characteristic->uuid, char_decl->uuid);
2020-07-23 18:54:26 -04:00
// We know the next handle will be the characteristic value handle, so skip it.
handle++;
2020-07-23 18:54:26 -04:00
rsp_length += data_length;
no_data = false;
2020-07-23 18:54:26 -04:00
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
// See if request is for a descriptor value with a 16-bit UUID, such as the CCCD.
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
2020-08-10 19:46:18 -04:00
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) == type_uuid) {
2021-03-15 09:57:36 -04:00
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
2020-07-23 18:54:26 -04:00
att_data->handle = handle;
2020-07-23 18:54:26 -04:00
mp_buffer_info_t bufinfo;
if (!mp_get_buffer(descriptor->value, &bufinfo, MP_BUFFER_READ)) {
break;
}
uint16_t value_size = MIN(mtu - rsp_length, bufinfo.len);
memcpy(att_data->value, bufinfo.buf, value_size);
rsp_length += value_size;
2020-07-23 18:54:26 -04:00
// Only return one descriptor value.
no_data = false;
break;
}
} else if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
// See if request is for a characteristic value with a 16-bit UUID.
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
2020-08-10 19:46:18 -04:00
if (bleio_uuid_get_uuid16_or_unknown(characteristic->uuid) == type_uuid) {
2021-03-15 09:57:36 -04:00
struct bt_att_data *att_data = (struct bt_att_data *)&rsp_bytes[rsp_length];
att_data->handle = handle;
mp_buffer_info_t bufinfo;
if (!mp_get_buffer(characteristic->value, &bufinfo, MP_BUFFER_READ)) {
// This shouldn't happen. There should be a buf in characteristic->value.
break;
}
uint16_t value_size = MIN(mtu - rsp_length, bufinfo.len);
memcpy(att_data->value, bufinfo.buf, value_size);
rsp_length += value_size;
// Only return one characteristic value.
no_data = false;
break;
}
}
} // end for loop
if (no_data) {
send_error(conn_handle, BT_ATT_OP_READ_TYPE_REQ,
2021-03-15 09:57:36 -04:00
req->start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
} else {
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, rsp_length, rsp_bytes);
}
2020-07-23 18:54:26 -04:00
}
static int att_read_type_req(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, uint16_t type, uint8_t response_buffer[]) {
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_read_type_req r;
} req_t;
uint8_t req_bytes[sizeof(req_t) + sizeof(type)];
req_t *req = (req_t *)req_bytes;
req->h.code = BT_ATT_OP_READ_TYPE_REQ;
req->r.start_handle = start_handle;
req->r.end_handle = end_handle;
req->r.uuid[0] = type & 0xff;
req->r.uuid[1] = type >> 8;
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
}
STATIC void process_read_type_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2020-07-23 18:54:26 -04:00
if (dlen < 1) {
return; // invalid, drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_READ_TYPE_RSP, dlen, data);
}
// Handles BT_ATT_OP_WRITE_REQ or BT_ATT_OP_WRITE_
2020-07-28 11:56:00 -04:00
STATIC void process_write_req_or_cmd(uint16_t conn_handle, uint16_t mtu, uint8_t op, uint8_t dlen, uint8_t data[]) {
// struct bt_att_write_cmd is identical, so don't bother to split code paths based on opcode.
2021-03-15 09:57:36 -04:00
struct bt_att_write_req *req = (struct bt_att_write_req *)data;
2020-08-10 19:46:18 -04:00
2020-07-28 11:56:00 -04:00
bool with_response = (op == BT_ATT_OP_WRITE_REQ);
2020-07-23 18:54:26 -04:00
if (dlen < sizeof(struct bt_att_write_req)) {
if (with_response) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
}
return;
}
2020-08-10 19:46:18 -04:00
if (req->handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
}
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
size_t value_length = dlen - sizeof(struct bt_att_write_req);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
mp_buffer_info_t bufinfo;
bufinfo.buf = req->value;
bufinfo.len = value_length;
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
mp_obj_t attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, req->handle);
2020-07-23 18:54:26 -04:00
if (mp_obj_is_type(attribute_obj, &bleio_characteristic_type)) {
2020-08-10 19:46:18 -04:00
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute_obj);
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
// Don't write the characteristic declaration.
// Also, this must be a writable characteristic.
if (req->handle != characteristic->handle ||
(with_response
? (characteristic->props & CHAR_PROP_WRITE) == 0
: (characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE) == 0)) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
}
return;
}
2020-07-23 18:54:26 -04:00
2020-08-13 00:03:39 -04:00
// Just change the local value. Don't fire off notifications, etc.
bleio_characteristic_set_local_value(characteristic, &bufinfo);
2020-07-23 18:54:26 -04:00
} else if (mp_obj_is_type(attribute_obj, &bleio_descriptor_type)) {
2020-08-10 19:46:18 -04:00
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute_obj);
// Only CCCD's are writable.
if (bleio_uuid_get_uuid16_or_unknown(descriptor->uuid) != BLE_UUID_CCCD) {
if (with_response) {
send_error(conn_handle, BT_ATT_OP_WRITE_REQ, req->handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
}
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
common_hal_bleio_descriptor_set_value(descriptor, &bufinfo);
}
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
if (with_response) {
2020-08-10 19:46:18 -04:00
// There's no data in the response. We just indicate the write happened.
struct bt_att_hdr rsp = {
.code = BT_ATT_OP_WRITE_RSP,
2020-08-10 19:46:18 -04:00
};
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(rsp), (uint8_t *)&rsp);
2020-07-23 18:54:26 -04:00
}
}
STATIC void process_write_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
if (dlen != 0) {
return; // drop
}
check_and_save_expected_rsp(conn_handle, BT_ATT_OP_WRITE_RSP, dlen, data);
}
2020-07-28 11:56:00 -04:00
STATIC void process_prepare_write_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_prepare_write_req *req = (struct bt_att_prepare_write_req *)data;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
if (dlen < sizeof(struct bt_att_prepare_write_req)) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
return;
}
2020-07-30 22:07:55 -04:00
uint16_t handle = req->handle;
uint16_t offset = req->offset;
2021-03-15 09:57:36 -04:00
(void)offset;
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
if (handle > bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj)) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
2020-07-30 22:07:55 -04:00
return;
}
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
mp_obj_t *attribute = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
2020-07-23 18:54:26 -04:00
if (!mp_obj_is_type(attribute, &bleio_characteristic_type)) {
2020-08-02 11:36:38 -04:00
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
2020-07-30 22:07:55 -04:00
return;
}
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute);
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
if (handle != characteristic->handle) {
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG);
2020-07-30 22:07:55 -04:00
return;
}
2020-07-23 18:54:26 -04:00
2020-08-10 19:46:18 -04:00
if ((characteristic->props & CHAR_PROP_WRITE) == 0) {
2020-08-02 11:36:38 -04:00
send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_WRITE_NOT_PERMITTED);
2020-07-30 22:07:55 -04:00
return;
}
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
// FIX if (long_write_handle == BLE_GATT_HANDLE_INVALID)
2020-07-28 11:56:00 -04:00
// int valueSize = characteristic->valueSize();
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// long_write_value = (uint8_t*)realloc(long_write_value, valueSize);
// long_write_value_length = 0;
// long_write_handle = handle;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// memset(long_write_value, 0x00, valueSize);
// } else if (long_write_handle != handle) {
// send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_UNLIKELY);
// return;
// }
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// uint8_t value_length = dlen - sizeof(struct bt_att_prepare_write_req);
// uint8_t* value = &data[sizeof(struct bt_att_prepare_write_req)];
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// if ((offset != long_write_value_length) || ((offset + value_length) > (uint16_t)characteristic->valueSize())) {
// send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_INVALID_OFFSET);
// return;
// }
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// memcpy(long_write_value + offset, value, value_length);
// long_write_value_length += value_length;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// uint8_t response[mtu];
// uint16_t response_length;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// response[0] = BT_ATT_OP_PREP_WRITE_RSP;
// memcpy(&response[1], data, dlen);
// response_length = dlen + 1;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
2020-07-23 18:54:26 -04:00
}
STATIC void process_exec_write_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
struct bt_att_exec_write_req *req = (struct bt_att_exec_write_req *)data;
2020-07-28 11:56:00 -04:00
if (dlen != sizeof(struct bt_att_exec_write_req)) {
2020-07-30 22:07:55 -04:00
send_error(conn_handle, BT_ATT_OP_EXEC_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU);
2020-07-23 18:54:26 -04:00
return;
}
2020-07-28 11:56:00 -04:00
if (long_write_handle && (req->flags & 0x01)) {
2021-03-15 09:57:36 -04:00
// FIX BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)GATT.attribute(long_write_handle - 1);
2020-07-23 18:54:26 -04:00
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle == conn_handle) {
2021-03-15 09:57:36 -04:00
// FIX characteristic->writeValue(BLEDevice(bleio_connections[i].address_type, bleio_connections[i].address), long_write_value, long_write_value_length);
2020-07-23 18:54:26 -04:00
break;
}
}
}
2020-07-30 22:07:55 -04:00
long_write_handle = BLE_GATT_HANDLE_INVALID;
2020-07-23 18:54:26 -04:00
long_write_value_length = 0;
uint8_t response[mtu];
uint16_t response_length;
response[0] = BT_ATT_OP_EXEC_WRITE_RSP;
response_length = 1;
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, response_length, response);
}
2020-08-10 19:46:18 -04:00
STATIC void process_notify_or_indicate(uint16_t conn_handle, uint8_t opcode, uint8_t dlen, uint8_t data[]) {
2020-07-23 18:54:26 -04:00
if (dlen < 2) {
return; // drop
}
2020-07-28 11:56:00 -04:00
// struct bt_att_notify and bt_att_indicate are identical.
2021-03-15 09:57:36 -04:00
// FIXunused struct bt_att_notify *req = (struct bt_att_notify *) data;
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
// FIXunused uint8_t handle = req->handle;
2020-07-23 18:54:26 -04:00
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
if (bleio_connections[i].conn_handle != conn_handle) {
continue;
}
2021-03-15 09:57:36 -04:00
// FIX BLERemoteDevice* device = bleio_connections[i].device;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// if (!device) {
// break;
// }
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// int serviceCount = device->serviceCount();
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// for (size_t i = 0; i < serviceCount; i++) {
// BLERemoteService* s = device->service(i);
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// if (s->start_handle() < handle && s->end_handle() >= handle) {
// int characteristicCount = s->characteristicCount();
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// for (int j = 0; j < characteristicCount; j++) {
// BLERemoteCharacteristic* c = s->characteristic(j);
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// if (c->value_handle() == handle) {
// //FIX c->writeValue(BLEDevice(bleio_connections[i].address_type, bleio_connections[i].address), &data[2], dlen - 2);
// }
// }
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
// break;
// }
// }
2020-07-23 18:54:26 -04:00
}
2020-07-28 11:56:00 -04:00
if (opcode == BT_ATT_OP_INDICATE) {
// send CONFIRM for INDICATE
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
uint8_t op_confirm = BT_ATT_OP_CONFIRM;
2020-07-23 18:54:26 -04:00
2020-07-28 11:56:00 -04:00
hci_send_acl_pkt(conn_handle, BT_L2CAP_CID_ATT, sizeof(op_confirm), &op_confirm);
2020-07-23 18:54:26 -04:00
}
}
2020-08-10 19:46:18 -04:00
STATIC void process_confirm(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
2021-03-15 09:57:36 -04:00
(void)conn_handle;
(void)dlen;
(void)data;
2020-07-28 11:56:00 -04:00
confirm = true;
2020-07-23 18:54:26 -04:00
}
bool att_exchange_mtu(uint16_t conn_handle) {
uint8_t response_buffer[max_mtu];
2020-07-28 11:56:00 -04:00
struct bt_att_exchange_mtu_req req = {
.mtu = max_mtu,
};
2021-03-15 09:57:36 -04:00
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
2020-07-23 18:54:26 -04:00
}
int att_read_req(uint16_t conn_handle, uint16_t handle, uint8_t response_buffer[]) {
2020-07-28 11:56:00 -04:00
struct __packed {
struct bt_att_hdr h;
struct bt_att_read_req r;
} req = { {
2021-03-15 09:57:36 -04:00
.code = BT_ATT_OP_READ_REQ,
}, {
.handle = handle,
}};
2020-07-23 18:54:26 -04:00
2021-03-15 09:57:36 -04:00
return send_req_wait_for_rsp(conn_handle, sizeof(req), (uint8_t *)&req, response_buffer);
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
int att_write_req(uint16_t conn_handle, uint16_t handle, const uint8_t *data, uint8_t data_len, uint8_t response_buffer[]) {
2020-08-02 11:36:38 -04:00
typedef struct __packed {
struct bt_att_hdr h;
struct bt_att_write_req r;
} req_t;
2020-07-30 22:07:55 -04:00
2020-08-02 11:36:38 -04:00
uint8_t req_bytes[sizeof(req_t) + data_len];
2021-03-15 09:57:36 -04:00
req_t *req = (req_t *)req_bytes;
2020-08-02 11:36:38 -04:00
req->h.code = BT_ATT_OP_WRITE_REQ;
req->r.handle = handle;
memcpy(req->r.value, data, data_len);
2020-07-30 22:07:55 -04:00
2020-08-02 11:36:38 -04:00
return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer);
2020-07-23 18:54:26 -04:00
}
2021-03-15 09:57:36 -04:00
void att_write_cmd(uint16_t conn_handle, uint16_t handle, const uint8_t *data, uint8_t data_len) {
2020-08-02 11:36:38 -04:00
typedef struct __packed {
2020-07-28 11:56:00 -04:00
struct bt_att_hdr h;
struct bt_att_write_cmd r;
2020-08-02 11:36:38 -04:00
} cmd_t;
2020-07-23 18:54:26 -04:00
2020-08-02 11:36:38 -04:00
uint8_t cmd_bytes[sizeof(cmd_t) + data_len];
2021-03-15 09:57:36 -04:00
cmd_t *cmd = (cmd_t *)cmd_bytes;
2020-08-02 11:36:38 -04:00
cmd->h.code = BT_ATT_OP_WRITE_CMD;
cmd->r.handle = handle;
memcpy(cmd->r.value, data, data_len);
2020-07-30 22:07:55 -04:00
2020-08-13 00:03:39 -04:00
send_req(conn_handle, sizeof(cmd_bytes), cmd_bytes);
2020-07-23 18:54:26 -04:00
}
void att_process_data(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) {
// Opcode is a single byte at the front of the data.
uint8_t opcode = data[0];
// Skip over opcode.
dlen--;
data++;
2020-07-28 11:56:00 -04:00
uint16_t mtu = att_mtu(conn_handle);
2020-07-23 18:54:26 -04:00
switch (opcode) {
case BT_ATT_OP_ERROR_RSP:
process_error(conn_handle, dlen, data);
break;
case BT_ATT_OP_MTU_REQ:
process_mtu_req(conn_handle, dlen, data);
break;
case BT_ATT_OP_MTU_RSP:
process_mtu_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_FIND_INFO_REQ:
process_find_info_req(conn_handle, mtu, dlen, data);
break;
case BT_ATT_OP_FIND_INFO_RSP:
process_find_info_rsp(conn_handle, dlen, data);
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_FIND_TYPE_REQ:
process_find_type_req(conn_handle, mtu, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_READ_TYPE_REQ:
process_read_type_req(conn_handle, mtu, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_READ_TYPE_RSP:
process_read_type_rsp(conn_handle, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_READ_GROUP_REQ:
process_read_group_req(conn_handle, mtu, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_READ_GROUP_RSP:
process_read_group_rsp(conn_handle, dlen, data);
2020-07-23 18:54:26 -04:00
break;
case BT_ATT_OP_READ_REQ:
case BT_ATT_OP_READ_BLOB_REQ:
process_read_or_read_blob_req(conn_handle, mtu, opcode, dlen, data);
break;
case BT_ATT_OP_READ_RSP:
process_read_rsp(conn_handle, dlen, data);
break;
case BT_ATT_OP_WRITE_REQ:
case BT_ATT_OP_WRITE_CMD:
process_write_req_or_cmd(conn_handle, mtu, opcode, dlen, data);
break;
case BT_ATT_OP_WRITE_RSP:
process_write_rsp(conn_handle, dlen, data);
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_PREPARE_WRITE_REQ:
process_prepare_write_req(conn_handle, mtu, dlen, data);
2020-07-23 18:54:26 -04:00
break;
case BT_ATT_OP_EXEC_WRITE_REQ:
process_exec_write_req(conn_handle, mtu, dlen, data);
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_NOTIFY:
case BT_ATT_OP_INDICATE:
2020-08-10 19:46:18 -04:00
process_notify_or_indicate(conn_handle, opcode, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_CONFIRM:
2020-08-10 19:46:18 -04:00
process_confirm(conn_handle, dlen, data);
2020-07-23 18:54:26 -04:00
break;
2020-07-28 11:56:00 -04:00
case BT_ATT_OP_READ_MULT_REQ:
2020-07-23 18:54:26 -04:00
case BT_ATT_OP_SIGNED_WRITE_CMD:
default:
2020-07-28 11:56:00 -04:00
send_error(conn_handle, opcode, 0x00, BT_ATT_ERR_NOT_SUPPORTED);
2020-07-23 18:54:26 -04:00
break;
}
}
// FIX Do we need all of these?
static void check_att_err(uint8_t err) {
mp_rom_error_text_t msg = NULL;
switch (err) {
case 0:
return;
case BT_ATT_ERR_INVALID_HANDLE:
msg = MP_ERROR_TEXT("Invalid handle");
break;
case BT_ATT_ERR_READ_NOT_PERMITTED:
msg = MP_ERROR_TEXT("Read not permitted");
break;
case BT_ATT_ERR_WRITE_NOT_PERMITTED:
msg = MP_ERROR_TEXT("Write not permitted");
break;
case BT_ATT_ERR_INVALID_PDU:
msg = MP_ERROR_TEXT("Invalid PDU");
break;
case BT_ATT_ERR_NOT_SUPPORTED:
msg = MP_ERROR_TEXT("Not supported");
break;
case BT_ATT_ERR_INVALID_OFFSET:
msg = MP_ERROR_TEXT("Invalid offset");
break;
case BT_ATT_ERR_PREPARE_QUEUE_FULL:
msg = MP_ERROR_TEXT("Prepare queue full");
break;
case BT_ATT_ERR_ATTRIBUTE_NOT_FOUND:
msg = MP_ERROR_TEXT("Attribute not found");
break;
case BT_ATT_ERR_ATTRIBUTE_NOT_LONG:
msg = MP_ERROR_TEXT("Attribute not long");
break;
case BT_ATT_ERR_ENCRYPTION_KEY_SIZE:
msg = MP_ERROR_TEXT("Encryption key size");
break;
case BT_ATT_ERR_INVALID_ATTRIBUTE_LEN:
msg = MP_ERROR_TEXT("Invalid attribute length");
break;
case BT_ATT_ERR_UNLIKELY:
msg = MP_ERROR_TEXT("Unlikely");
break;
case BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE:
msg = MP_ERROR_TEXT("Unsupported group type");
break;
case BT_ATT_ERR_INSUFFICIENT_RESOURCES:
msg = MP_ERROR_TEXT("Insufficient resources");
break;
case BT_ATT_ERR_DB_OUT_OF_SYNC:
msg = MP_ERROR_TEXT("DB out of sync");
break;
case BT_ATT_ERR_VALUE_NOT_ALLOWED:
msg = MP_ERROR_TEXT("Value not allowed");
break;
}
if (msg) {
mp_raise_bleio_BluetoothError(msg);
}
switch (err) {
case BT_ATT_ERR_AUTHENTICATION:
msg = MP_ERROR_TEXT("Insufficient authentication");
break;
case BT_ATT_ERR_INSUFFICIENT_ENCRYPTION:
msg = MP_ERROR_TEXT("Insufficient encryption");
break;
}
if (msg) {
mp_raise_bleio_SecurityError(msg);
}
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Unknown ATT error: 0x%02x"), err);
}