diff --git a/epicardium/ble/epic_att_api.c b/epicardium/ble/epic_att_api.c index d89a0f699bb72f4b0be6e76c2e3b3c9d4eba4102..62f51847a8139dafd3975ec9dd33b1358855ca0b 100644 --- a/epicardium/ble/epic_att_api.c +++ b/epicardium/ble/epic_att_api.c @@ -15,6 +15,14 @@ #include <stdio.h> #include <string.h> +/* We allow up to 10 dynamically defined services */ +#define ATTS_DYN_GROUP_COUNT 10 +#define ATTS_DYN_START_HANDLE 0x200 + +static void *dyn_groups[ATTS_DYN_GROUP_COUNT] = {}; +static int next_dyn_group = 0; +static int next_handle = ATTS_DYN_START_HANDLE; + void ble_epic_att_api_event(attEvt_t *att_event) { attEvt_t *e = WsfBufAlloc(sizeof(*e)); @@ -70,25 +78,16 @@ static uint8_t DynAttsWriteCback( return AttsSetAttr(handle, len, pValue); } -/* We allow up to 10 dynamically defined services */ -#define ATTS_DYN_GROUP_COUNT 10 -#define ATTS_DYN_START_HANDLE 0x200 -static int next_start_handle = ATTS_DYN_START_HANDLE; -static void *dyn_groups[ATTS_DYN_GROUP_COUNT] = {}; -static int next_dyn_group = 0; - int epic_atts_dyn_create_service( const uint8_t *uuid, uint8_t uuid_len, uint16_t group_size, - void **pSvcHandle, - uint16_t *start_handle + void **pSvcHandle ) { - *start_handle = next_start_handle; - int end_handle = *start_handle + group_size - 1; - next_start_handle += group_size; + uint16_t start_handle = next_handle; + uint16_t end_handle = start_handle + group_size - 1; - *pSvcHandle = AttsDynCreateGroup(*start_handle, end_handle); + *pSvcHandle = AttsDynCreateGroup(start_handle, end_handle); dyn_groups[next_dyn_group++] = *pSvcHandle; AttsDynRegister(*pSvcHandle, NULL, DynAttsWriteCback); @@ -102,6 +101,7 @@ int epic_atts_dyn_create_service( 0, ATTS_PERMIT_READ ); + next_handle++; // TODO: validate parameters and pointer and current service count return 0; @@ -113,7 +113,7 @@ int epic_atts_dyn_add_characteristic( uint8_t uuid_len, uint8_t flags, uint16_t maxLen, - uint16_t current_handle + uint16_t *value_handle ) { uint8_t settings = ATTS_SET_VARIABLE_LEN; if (flags & ATT_PROP_WRITE) { @@ -129,7 +129,7 @@ int epic_atts_dyn_add_characteristic( } uint8_t characteristic[1 + 2 + 16] = { - flags, UINT16_TO_BYTES(current_handle + 1) + flags, UINT16_TO_BYTES(next_handle + 1) }; memcpy(characteristic + 3, uuid, uuid_len); uint8_t characteristic_len = 1 + 2 + uuid_len; @@ -144,8 +144,10 @@ int epic_atts_dyn_add_characteristic( 0, ATTS_PERMIT_READ ); + next_handle++; /* Value */ + *value_handle = next_handle; if ((flags & ATT_PROP_READ) == 0) { AttsDynAddAttrDynConst( pSvcHandle, @@ -169,6 +171,7 @@ int epic_atts_dyn_add_characteristic( permissions ); } + next_handle++; return 0; } @@ -179,7 +182,8 @@ int epic_ble_atts_dyn_add_descriptor( uint8_t flags, const uint8_t *value, uint16_t value_len, - uint16_t maxLen + uint16_t maxLen, + uint16_t *descriptor_handle ) { uint8_t settings = 0; if (flags & ATT_PROP_WRITE) { @@ -194,6 +198,7 @@ int epic_ble_atts_dyn_add_descriptor( permissions |= ATTS_PERMIT_WRITE; } + *descriptor_handle = next_handle; AttsDynAddAttrDyn( pSvcHandle, uuid, @@ -204,6 +209,7 @@ int epic_ble_atts_dyn_add_descriptor( settings, permissions ); + next_handle++; return 0; } @@ -227,8 +233,8 @@ int epic_ble_atts_dyn_delete_groups(void) dyn_groups[i] = NULL; } } - next_dyn_group = 0; - next_start_handle = ATTS_DYN_START_HANDLE; + next_dyn_group = 0; + next_handle = ATTS_DYN_START_HANDLE; return 0; } diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index b38b103400877a78b468efc43cae2a0cbeb74ffb..8beed551c434d16092a108f493e5aac1f3c78fd6 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -2640,12 +2640,12 @@ API(API_BLE_HID_SEND_REPORT, int epic_ble_hid_send_report(uint8_t report_id, uin * * The MicroPython Bluetooth module is still in flux so this API will continue to change as well. */ -API(API_BLE_ATTS_DYN_CREATE_GROUP, int epic_atts_dyn_create_service(const uint8_t *uuid, uint8_t uuid_len, uint16_t group_size, void **pSvcHandle, uint16_t *start_handle)); +API(API_BLE_ATTS_DYN_CREATE_GROUP, int epic_atts_dyn_create_service(const uint8_t *uuid, uint8_t uuid_len, uint16_t group_size, void **pSvcHandle)); //API(API_BLE_ATTS_DYN_DELETE_GROUP, void AttsDynDeleteGroup(void *pSvcHandle)); API(API_BLE_ATTS_DYN_DELETE_GROUPS, int epic_ble_atts_dyn_delete_groups(void)); -API(API_BLE_ATTS_DYN_ADD_DESCRIPTOR, int epic_ble_atts_dyn_add_descriptor(void *pSvcHandle, const uint8_t *uuid, uint8_t uuid_len, uint8_t flags, const uint8_t *value, uint16_t value_len, uint16_t maxLen)); -API(API_BLE_ATTS_DYN_ADD_CHARACTERISTIC, int epic_atts_dyn_add_characteristic(void *pSvcHandle, const uint8_t *uuid, uint8_t uuid_len, uint8_t flags, uint16_t maxLen, uint16_t current_handle)); +API(API_BLE_ATTS_DYN_ADD_CHARACTERISTIC, int epic_atts_dyn_add_characteristic(void *pSvcHandle, const uint8_t *uuid, uint8_t uuid_len, uint8_t flags, uint16_t maxLen, uint16_t *value_handle)); +API(API_BLE_ATTS_DYN_ADD_DESCRIPTOR, int epic_ble_atts_dyn_add_descriptor(void *pSvcHandle, const uint8_t *uuid, uint8_t uuid_len, uint8_t flags, const uint8_t *value, uint16_t value_len, uint16_t maxLen, uint16_t *descriptor_handle)); API(API_BLE_ATTS_SEND_SERVICE_CHANGED_IND, int epic_atts_dyn_send_service_changed_ind(void)); diff --git a/pycardium/modules/modbluetooth_card10.c b/pycardium/modules/modbluetooth_card10.c index 5914dda3b277d4bd8fc7a2fa3189be2670391910..e6b2f77879b9896e6633bd44d0ac44dbaa9420f9 100644 --- a/pycardium/modules/modbluetooth_card10.c +++ b/pycardium/modules/modbluetooth_card10.c @@ -395,21 +395,14 @@ int mp_bluetooth_gatts_register_service( const uint16_t numHandles = 1 + num_characteristics * 2 + num_ccds + num_descriptors_total; - uint16_t startHandle; uint8_t uuid_len = service_uuid->type == MP_BLUETOOTH_UUID_TYPE_16 ? 2 : 16; // TODO: handle error epic_atts_dyn_create_service( - service_uuid->data, - uuid_len, - numHandles, - &pSvcHandle, - &startHandle + service_uuid->data, uuid_len, numHandles, &pSvcHandle ); - uint16_t currentHandle = startHandle; - size_t descriptor_index = 0; size_t handle_index = 0; @@ -418,28 +411,26 @@ int mp_bluetooth_gatts_register_service( mp_obj_bluetooth_uuid_t *uuid = characteristic_uuids[i]; uuid_len = uuid->type == MP_BLUETOOTH_UUID_TYPE_16 ? 2 : 16; - currentHandle++; + uint16_t value_handle; epic_atts_dyn_add_characteristic( pSvcHandle, uuid->data, uuid_len, flags, MP_BLUETOOTH_DEFAULT_ATTR_LEN, - currentHandle + &value_handle ); - currentHandle++; ; - handles[handle_index] = currentHandle; + handles[handle_index] = value_handle; mp_bluetooth_gatts_db_create_entry( - GATTS_DB, currentHandle, MP_BLUETOOTH_DEFAULT_ATTR_LEN + GATTS_DB, value_handle, MP_BLUETOOTH_DEFAULT_ATTR_LEN ); - gatts_status_create_entry(GATTS_STATUS, currentHandle); + gatts_status_create_entry(GATTS_STATUS, value_handle); if ((flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) || flags & (MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE)) { /* CCCD */ - currentHandle++; uint8_t cccd_buf[2] = {}; // TODO: Handle CCC data. // Until then: activate notification/indications by default. @@ -450,6 +441,7 @@ int mp_bluetooth_gatts_register_service( cccd_buf[0] |= 0x02; } + uint16_t cccd_handle; uint8_t ccc_uuid[] = { 0x02, 0x29 }; uint8_t flags = MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ | MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE; @@ -460,17 +452,16 @@ int mp_bluetooth_gatts_register_service( flags, cccd_buf, sizeof(cccd_buf), - sizeof(cccd_buf) - ); - epic_ble_atts_set_attr( - currentHandle, cccd_buf, sizeof(cccd_buf) + sizeof(cccd_buf), + &cccd_handle ); + mp_bluetooth_gatts_db_create_entry( - GATTS_DB, currentHandle, sizeof(cccd_buf) + GATTS_DB, cccd_handle, sizeof(cccd_buf) ); int ret = mp_bluetooth_gatts_db_write( GATTS_DB, - currentHandle, + cccd_handle, cccd_buf, sizeof(cccd_buf) ); @@ -488,6 +479,7 @@ int mp_bluetooth_gatts_register_service( uuid_len = uuid->type == MP_BLUETOOTH_UUID_TYPE_16 ? 2 : 16; + uint16_t descriptor_handle; epic_ble_atts_dyn_add_descriptor( pSvcHandle, uuid->data, @@ -495,12 +487,14 @@ int mp_bluetooth_gatts_register_service( flags, NULL, 0, - MP_BLUETOOTH_DEFAULT_ATTR_LEN + MP_BLUETOOTH_DEFAULT_ATTR_LEN, + &descriptor_handle ); + handles[handle_index] = descriptor_handle; mp_bluetooth_gatts_db_create_entry( GATTS_DB, - handles[handle_index], + descriptor_handle, MP_BLUETOOTH_DEFAULT_ATTR_LEN );