diff options
| author | Dan Halbert <halbert@halwitz.org> | 2020-08-02 11:36:38 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2020-08-02 11:36:38 -0400 |
| commit | 0a60aee3e4270d1da42d67f933578d60d56b8b52 (patch) | |
| tree | 43beede01b4f2d94863f979365c08db64c6be882 /devices/ble_hci | |
| parent | a76ad3415c6aeae7bb9c915f20220d32001e8094 (diff) | |
| parent | e7a78e08479e412dda16076986924e3b8cb19b75 (diff) | |
wip: compiles
Diffstat (limited to 'devices/ble_hci')
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/Adapter.c | 2 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/Attribute.h | 4 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/Characteristic.c | 21 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/Characteristic.h | 4 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/Service.c | 21 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/__init__.c | 2 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/att.c | 91 | ||||
| -rw-r--r-- | devices/ble_hci/common-hal/_bleio/hci.c | 18 |
8 files changed, 87 insertions, 76 deletions
diff --git a/devices/ble_hci/common-hal/_bleio/Adapter.c b/devices/ble_hci/common-hal/_bleio/Adapter.c index 2e1baea04..3acae9beb 100644 --- a/devices/ble_hci/common-hal/_bleio/Adapter.c +++ b/devices/ble_hci/common-hal/_bleio/Adapter.c @@ -243,7 +243,7 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable // Indices into the list are handles. Handle 0x0000 designates an invalid handle, // so store None there to skip it. self->attributes = mp_obj_new_list(0, NULL); - bleio_adapter_add_attribute(mp_const_none); + bleio_adapter_add_attribute(self, mp_const_none); self->last_added_service_handle = BLE_GATT_HANDLE_INVALID; self->last_added_characteristic_handle = BLE_GATT_HANDLE_INVALID; } diff --git a/devices/ble_hci/common-hal/_bleio/Attribute.h b/devices/ble_hci/common-hal/_bleio/Attribute.h index 5ad2142d3..4301614fc 100644 --- a/devices/ble_hci/common-hal/_bleio/Attribute.h +++ b/devices/ble_hci/common-hal/_bleio/Attribute.h @@ -30,13 +30,13 @@ #include "shared-module/_bleio/Attribute.h" // Types returned by attribute table lookups. These are UUIDs. -enum ble_attribute_type { +typedef enum { BLE_TYPE_UNKNOWN = 0x0000, BLE_TYPE_PRIMARY_SERVICE = 0x2800, BLE_TYPE_SECONDARY_SERVICE = 0x2801, BLE_TYPE_CHARACTERISTIC = 0x2803, BLE_TYPE_DESCRIPTOR = 0x2900 -}; +} ble_attribute_type; // typedef struct // { diff --git a/devices/ble_hci/common-hal/_bleio/Characteristic.c b/devices/ble_hci/common-hal/_bleio/Characteristic.c index 1c678b761..e2cdac08c 100644 --- a/devices/ble_hci/common-hal/_bleio/Characteristic.c +++ b/devices/ble_hci/common-hal/_bleio/Characteristic.c @@ -33,10 +33,16 @@ #include "shared-bindings/_bleio/Service.h" #include "common-hal/_bleio/Adapter.h" +#include "common-hal/_bleio/att.h" + +#define CCCD_NOTIFY 0x1 +#define CCCD_INDICATE 0x2 + void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_service_obj_t *service, uint16_t handle, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_buffer_info_t *initial_value_bufinfo) { self->service = service; self->uuid = uuid; + self->decl_handle = BLE_GATT_HANDLE_INVALID; self->handle = BLE_GATT_HANDLE_INVALID; self->props = props; self->read_perm = read_perm; @@ -112,14 +118,15 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, const bool indicate = self->props & CHAR_PROP_INDICATE; // Read the CCCD value, if there is one. if ((notify | indicate) && self->cccd_handle != BLE_GATT_HANDLE_INVALID) { - common_hal_bleio_gatts_read(self->cccd_handle, conn_handle, &cccd, sizeof(cccd)); + common_hal_bleio_gatts_read(self->cccd_handle, BLE_CONN_HANDLE_INVALID, + (uint8_t *) &cccd, sizeof(cccd)); } // It's possible that both notify and indicate are set. - if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) { + if (notify && (cccd & CCCD_NOTIFY)) { att_notify(self->handle, bufinfo->buf, MIN(bufinfo->len, self->max_length)); } - if (indicate && (cccd & BLE_GATT_HVX_INDICATION)) { + if (indicate && (cccd & CCCD_INDICATE)) { att_indicate(self->handle, bufinfo->buf, MIN(bufinfo->len, self->max_length)); } @@ -136,12 +143,12 @@ bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties } void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *self, bleio_descriptor_obj_t *descriptor) { - if (self->handle != common_hal_bleio_adapter_obj->last_added_characteristic_handle) { + if (self->handle != common_hal_bleio_adapter_obj.last_added_characteristic_handle) { mp_raise_bleio_BluetoothError( translate("Descriptor can only be added to most recently added characteristic")); } - descriptor->handle = bleio_adapter_add_attribute(common_hal_bleio_adapter_obj, descriptor); + descriptor->handle = bleio_adapter_add_attribute(&common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(descriptor)); // Link together all the descriptors for this characteristic. descriptor->next = self->descriptor_list; @@ -161,8 +168,8 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, common_hal_bleio_check_connected(conn_handle); uint16_t cccd_value = - (notify ? BLE_GATT_HVX_NOTIFICATION : 0) | - (indicate ? BLE_GATT_HVX_INDICATION : 0); + (notify ? CCCD_NOTIFY : 0) | + (indicate ? CCCD_INDICATE : 0); (void) cccd_value; //FIX call att_something to set remote CCCD diff --git a/devices/ble_hci/common-hal/_bleio/Characteristic.h b/devices/ble_hci/common-hal/_bleio/Characteristic.h index 7887afdfb..ce8302fcf 100644 --- a/devices/ble_hci/common-hal/_bleio/Characteristic.h +++ b/devices/ble_hci/common-hal/_bleio/Characteristic.h @@ -42,8 +42,8 @@ typedef struct _bleio_characteristic_obj { mp_obj_t value; uint16_t max_length; bool fixed_length; - uint16_t handle; - uint16_t value_handle; // Should be handle+1. + uint16_t decl_handle; + uint16_t handle; // Should be decl_handle+1. bleio_characteristic_properties_t props; bleio_attribute_security_mode_t read_perm; bleio_attribute_security_mode_t write_perm; diff --git a/devices/ble_hci/common-hal/_bleio/Service.c b/devices/ble_hci/common-hal/_bleio/Service.c index 696b8b943..c68355791 100644 --- a/devices/ble_hci/common-hal/_bleio/Service.c +++ b/devices/ble_hci/common-hal/_bleio/Service.c @@ -26,7 +26,7 @@ */ #include "py/runtime.h" -#include "common-hal/_bleio/__init__.h" +#include "shared-bindings/_bleio/__init__.h" #include "shared-bindings/_bleio/Characteristic.h" #include "shared-bindings/_bleio/Descriptor.h" #include "shared-bindings/_bleio/Service.h" @@ -41,8 +41,8 @@ uint32_t _common_hal_bleio_service_construct(bleio_service_obj_t *self, bleio_uu vm_used_ble = true; - self->handle = bleio_adapter_add_attribute(common_hal_bleio_adapter_obj, self); - if (self->handle = BLE_GATT_HANDLE_INVALID) { + self->handle = bleio_adapter_add_attribute(&common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(self)); + if (self->handle == BLE_GATT_HANDLE_INVALID) { return 1; } return 0; @@ -84,13 +84,15 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *initial_value_bufinfo) { - if (self->handle != common_hal_bleio_adapter_obj->last_added_service_handle) { + if (self->handle != common_hal_bleio_adapter_obj.last_added_service_handle) { mp_raise_bleio_BluetoothError( translate("Characteristic can only be added to most recently added service")); } - characteristic->decl_handle = bleio_adapter_add_attribute(common_hal_bleio_adapter_obj, characteristic); + characteristic->decl_handle = bleio_adapter_add_attribute( + &common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(characteristic)); // This is the value handle - characteristic->value_handle = bleio_adapter_add_attribute(common_hal_bleio_adapter_obj, characteristic); + characteristic->handle = bleio_adapter_add_attribute( + &common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(characteristic)); if (characteristic->props & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE)) { // We need a CCCD. @@ -99,7 +101,12 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, cccd->read_perm = SECURITY_MODE_OPEN; // Make CCCD write permission match characteristic read permission. cccd->write_perm = characteristic->read_perm; - characteristic->cccd_handle = common_hal_bleio_characteristic_add_descriptor(characteristic, cccd); + + const uint16_t cccd_handle = bleio_adapter_add_attribute( + &common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(cccd)); + cccd->handle = cccd_handle; + characteristic->cccd_handle = cccd_handle; + common_hal_bleio_characteristic_add_descriptor(characteristic, cccd); } // #if CIRCUITPY_VERBOSE_BLE diff --git a/devices/ble_hci/common-hal/_bleio/__init__.c b/devices/ble_hci/common-hal/_bleio/__init__.c index c64719099..7139e6193 100644 --- a/devices/ble_hci/common-hal/_bleio/__init__.c +++ b/devices/ble_hci/common-hal/_bleio/__init__.c @@ -270,7 +270,5 @@ void common_hal_bleio_gc_collect(void) { void bleio_background(void) { - supervisor_bluetooth_background(); bleio_adapter_background(&common_hal_bleio_adapter_obj); - //FIX bonding_background(); } diff --git a/devices/ble_hci/common-hal/_bleio/att.c b/devices/ble_hci/common-hal/_bleio/att.c index 013a51737..2982861f7 100644 --- a/devices/ble_hci/common-hal/_bleio/att.c +++ b/devices/ble_hci/common-hal/_bleio/att.c @@ -31,6 +31,8 @@ #include "py/obj.h" #include "common-hal/_bleio/Adapter.h" #include "common-hal/_bleio/Attribute.h" +#include "shared-bindings/_bleio/__init__.h" +#include "shared-bindings/_bleio/Characteristic.h" #include "supervisor/shared/tick.h" STATIC uint16_t max_mtu = BT_ATT_DEFAULT_LE_MTU; // 23 @@ -620,20 +622,20 @@ bool att_notify(uint16_t handle, const uint8_t* value, int length) { continue; } - typedef struct notify_t __packed { + typedef struct __packed { struct bt_att_hdr hdr; struct bt_att_notify ntf; - }; + } notify_t; size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(notify_t)), (uint16_t)length); - uint8_t notify_bytes[sizeof(cmd_s) + allowed_length]; - notify_t *notify_p = (notify_t *) notify_bytes; - notify_p->hdr.code = BT_ATT_OP_NOTIFY;; - notify_p->ntf.handle = handle; - memcpy(notify_p->ntf.value, data, allowed_length); + uint8_t notify_bytes[sizeof(notify_t) + allowed_length]; + notify_t *notify = (notify_t *) notify_bytes; + notify->hdr.code = BT_ATT_OP_NOTIFY;; + notify->ntf.handle = handle; + memcpy(notify->ntf.value, value, allowed_length); hci_send_acl_pkt(bleio_connections[i].conn_handle, BT_L2CAP_CID_ATT, - size_of(notify_bytes), notify_bytes); + sizeof(notify_bytes), notify_bytes); num_notifications++; } @@ -641,7 +643,7 @@ bool att_notify(uint16_t handle, const uint8_t* value, int length) { return (num_notifications > 0); } -bool att_indicate(conn_handle, uint16_t handle, const uint8_t* value, int length) { +bool att_indicate(uint16_t handle, const uint8_t* value, int length) { int num_indications = 0; for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) { @@ -649,18 +651,18 @@ bool att_indicate(conn_handle, uint16_t handle, const uint8_t* value, int length continue; } - typedef struct indicate_t __packed { + typedef struct __packed { struct bt_att_hdr hdr; struct bt_att_indicate ind; - }; + } indicate_t; size_t allowed_length = MIN((uint16_t)(bleio_connections[i].mtu - sizeof(indicate_t)), (uint16_t)length); - uint8_t indicate_bytes[sizeof(cmd_s) + allowed_length]; - struct indicate_s *indicate_p = (indicate_s *) indicate_bytes; - indicate_p->hdr.code = BT_ATT_OP_INDICATE;; - indicate_p->ind.handle = handle; - memcpy(indicate->ind.value, data, allowed_length); + uint8_t indicate_bytes[sizeof(indicate_t) + allowed_length]; + indicate_t *indicate = (indicate_t *) indicate_bytes; + indicate->hdr.code = BT_ATT_OP_INDICATE;; + indicate->ind.handle = handle; + memcpy(indicate->ind.value, value, allowed_length); confirm = false; @@ -1319,7 +1321,7 @@ STATIC void process_write_rsp(uint16_t conn_handle, uint8_t dlen, uint8_t data[] } STATIC void process_prepare_write_req(uint16_t conn_handle, uint16_t mtu, uint8_t dlen, uint8_t data[]) { - FIX struct bt_att_prepare_write_req *req = (struct bt_att_prepare_write_req *) data; + struct bt_att_prepare_write_req *req = (struct bt_att_prepare_write_req *) data; if (dlen < sizeof(struct bt_att_prepare_write_req)) { send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, BLE_GATT_HANDLE_INVALID, BT_ATT_ERR_INVALID_PDU); @@ -1328,32 +1330,33 @@ STATIC void process_prepare_write_req(uint16_t conn_handle, uint16_t mtu, uint8_ uint16_t handle = req->handle; uint16_t offset = req->offset; + (void) offset; - 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_ATTR_NOT_FOUND); + 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); return; } - mp_obj_t *attribute = bleio_adapter_get_attribute(common_hal_bleio_adapter_obj, handle); + mp_obj_t *attribute = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle); if (!MP_OBJ_IS_TYPE(attribute, &bleio_characteristic_type)) { - send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTR_NOT_LONG); + send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG); return; } bleio_characteristic_obj_t* characteristic = MP_OBJ_TO_PTR(attribute); - if (handle != characteristic->value_handle) { - send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTR_NOT_LONG); + if (handle != characteristic->handle) { + send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_ATTRIBUTE_NOT_LONG); return; } if (characteristic->props & CHAR_PROP_WRITE) { - send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_WRITE_NOT_PERM); + send_error(conn_handle, BT_ATT_OP_PREPARE_WRITE_REQ, handle, BT_ATT_ERR_WRITE_NOT_PERMITTED); return; } - if (long_write_handle == BLE_GATT_HANDLE_INVALID) + //FIX if (long_write_handle == BLE_GATT_HANDLE_INVALID) // int valueSize = characteristic->valueSize(); // long_write_value = (uint8_t*)realloc(long_write_value, valueSize); @@ -1508,35 +1511,33 @@ int att_read_req(uint16_t conn_handle, uint16_t handle, uint8_t response_buffer[ } int att_write_req(uint16_t conn_handle, uint16_t handle, const uint8_t* data, uint8_t data_len, uint8_t response_buffer[]) { - typedef struct write_req_t __packed { - struct bt_att_hdr hdr; - struct bt_att_write_req req; - }; - - uint8_t req_bytes[sizeof(write_req_t) + data_len]; - struct write_req_t *write_req_P = (write_req_t *) req_bytes; - req_p->hdr.code = BT_ATT_OP_WRITE_REQ; - req_p->req.handle = handle; - memcpy(req_p->req.value, data, data_len); + typedef struct __packed { + struct bt_att_hdr h; + struct bt_att_write_req r; + } req_t; - memcpy(req.req.value, data, data_len); + uint8_t req_bytes[sizeof(req_t) + data_len]; + req_t *req = (req_t *) req_bytes; + req->h.code = BT_ATT_OP_WRITE_REQ; + req->r.handle = handle; + memcpy(req->r.value, data, data_len); - return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req, response_buffer); + return send_req_wait_for_rsp(conn_handle, sizeof(req_bytes), req_bytes, response_buffer); } void att_write_cmd(uint16_t conn_handle, uint16_t handle, const uint8_t* data, uint8_t data_len) { - struct cmd_s __packed { + typedef struct __packed { struct bt_att_hdr h; struct bt_att_write_cmd r; - }; + } cmd_t; - uint8_t cmd_bytes[sizeof(cmd_s) + data_len]; - struct cmd_s *cmd_p = (cmd_s *) cmd_bytes; - cmd_p->h.code = BT_ATT_OP_WRITE_CMD; - cmd_p->r.handle = handle; - memcpy(cmd_p->r.value, data, data_len); + uint8_t cmd_bytes[sizeof(cmd_t) + data_len]; + cmd_t *cmd = (cmd_t *) cmd_bytes; + cmd->h.code = BT_ATT_OP_WRITE_CMD; + cmd->r.handle = handle; + memcpy(cmd->r.value, data, data_len); - return send_cmd(conn_handle, sizeof(cmd_bytes), cmd_bytes); + return send_req(conn_handle, sizeof(cmd_bytes), cmd_bytes); } void att_process_data(uint16_t conn_handle, uint8_t dlen, uint8_t data[]) { diff --git a/devices/ble_hci/common-hal/_bleio/hci.c b/devices/ble_hci/common-hal/_bleio/hci.c index 83aba883a..9c8d7f692 100644 --- a/devices/ble_hci/common-hal/_bleio/hci.c +++ b/devices/ble_hci/common-hal/_bleio/hci.c @@ -40,8 +40,6 @@ #define CTS_TIMEOUT_MSECS (1000) #define RESPONSE_TIMEOUT_MSECS (1000) -#define adapter (&common_hal_bleio_adapter_obj) - // These are the headers of the full packets that are sent over the serial interface. // They all have a one-byte type-field at the front, one of the H4_xxx packet types. @@ -370,14 +368,14 @@ hci_result_t hci_poll_for_incoming_pkt(void) { common_hal_mcu_enable_interrupts(); // Assert RTS low to say we're ready to read data. - common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, false); + common_hal_digitalio_digitalinout_set_value(common_hal_bleio_adapter_obj.rts_digitalinout, false); int errcode = 0; bool packet_is_complete = false; // Read bytes until we run out, or accumulate a complete packet. - while (common_hal_busio_uart_rx_characters_available(adapter->hci_uart)) { - common_hal_busio_uart_read(adapter->hci_uart, rx_buffer + rx_idx, 1, &errcode); + while (common_hal_busio_uart_rx_characters_available(common_hal_bleio_adapter_obj.hci_uart)) { + common_hal_busio_uart_read(common_hal_bleio_adapter_obj.hci_uart, rx_buffer + rx_idx, 1, &errcode); if (errcode) { hci_poll_in_progress = false; return HCI_READ_ERROR; @@ -412,7 +410,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) { } // Stop incoming data while processing packet. - common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true); + common_hal_digitalio_digitalinout_set_value(common_hal_bleio_adapter_obj.rts_digitalinout, true); size_t pkt_len = rx_idx; // Reset for next packet. rx_idx = 0; @@ -441,7 +439,7 @@ hci_result_t hci_poll_for_incoming_pkt(void) { break; } - common_hal_digitalio_digitalinout_set_value(adapter->rts_digitalinout, true); + common_hal_digitalio_digitalinout_set_value(common_hal_bleio_adapter_obj.rts_digitalinout, true); hci_poll_in_progress = false; return HCI_OK; @@ -452,7 +450,7 @@ STATIC hci_result_t write_pkt(uint8_t *buffer, size_t len) { // Wait for CTS to go low before writing to HCI adapter. uint64_t start = supervisor_ticks_ms64(); - while (common_hal_digitalio_digitalinout_get_value(adapter->cts_digitalinout)) { + while (common_hal_digitalio_digitalinout_get_value(common_hal_bleio_adapter_obj.cts_digitalinout)) { RUN_BACKGROUND_TASKS; if (supervisor_ticks_ms64() - start > CTS_TIMEOUT_MSECS) { return HCI_WRITE_TIMEOUT; @@ -460,7 +458,7 @@ STATIC hci_result_t write_pkt(uint8_t *buffer, size_t len) { } int errcode = 0; - common_hal_busio_uart_write(adapter->hci_uart, buffer, len, &errcode); + common_hal_busio_uart_write(common_hal_bleio_adapter_obj.hci_uart, buffer, len, &errcode); if (errcode) { return HCI_WRITE_ERROR; } @@ -551,7 +549,7 @@ hci_result_t hci_send_acl_pkt(uint16_t handle, uint8_t cid, uint8_t data_len, ui pending_pkt++; int errcode = 0; - common_hal_busio_uart_write(adapter->hci_uart, tx_buffer, buf_len, &errcode); + common_hal_busio_uart_write(common_hal_bleio_adapter_obj.hci_uart, tx_buffer, buf_len, &errcode); if (errcode) { return HCI_WRITE_ERROR; } |
