mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-30 16:20:24 +00:00
hids_client: skeleton code for connect
This commit is contained in:
parent
fc975d0e06
commit
cf26c8fbbf
@ -86,7 +86,8 @@ GATT_CLIENT += \
|
||||
gatt_client.c \
|
||||
battery_service_client.c \
|
||||
device_information_service_client.c \
|
||||
scan_parameters_service_client.c \
|
||||
scan_parameters_service_client.c \
|
||||
hids_client.c \
|
||||
|
||||
PAN += \
|
||||
pan.c \
|
||||
@ -226,6 +227,7 @@ EXAMPLES_LE_ONLY= \
|
||||
gatt_heart_rate_client \
|
||||
gatt_streamer_server \
|
||||
hog_boot_host_demo \
|
||||
hog_host_demo \
|
||||
hog_keyboard_demo \
|
||||
hog_mouse_demo \
|
||||
le_data_channel_client \
|
||||
@ -326,6 +328,9 @@ hog_mouse_demo: hog_mouse_demo.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SER
|
||||
hog_boot_host_demo: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_CLIENT_OBJ} hog_boot_host_demo.c
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
hog_host_demo: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_CLIENT_OBJ} hog_host_demo.c
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
sm_pairing_peripheral: sm_pairing_peripheral.h ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_SERVER_OBJ} ${GATT_CLIENT_OBJ} sm_pairing_peripheral.c
|
||||
${CC} $(filter-out sm_pairing_peripheral.h,$^) ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
|
@ -42,33 +42,180 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hids_client.h"
|
||||
#include "ble/gatt-service/hids_client.h"
|
||||
|
||||
#include "btstack_memory.h"
|
||||
#include "ble/att_db.h"
|
||||
#include "ble/core.h"
|
||||
#include "ble/gatt_client.h"
|
||||
#include "ble/sm.h"
|
||||
#include "bluetooth_gatt.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_event.h"
|
||||
#include "btstack_run_loop.h"
|
||||
#include "classic/sdp_util.h"
|
||||
#include "gap.h"
|
||||
|
||||
static btstack_packet_handler_t client_handler;
|
||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
static btstack_linked_list_t clients;
|
||||
static uint16_t hids_cid_counter = 0;
|
||||
|
||||
void hids_client_register_callback(btstack_packet_handler_t handler){
|
||||
client_handler = handler;
|
||||
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
|
||||
static uint16_t hids_get_next_cid(void){
|
||||
if (hids_cid_counter == 0xffff) {
|
||||
hids_cid_counter = 1;
|
||||
} else {
|
||||
hids_cid_counter++;
|
||||
}
|
||||
return hids_cid_counter;
|
||||
}
|
||||
|
||||
static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(packet_type); // ok: only hci events
|
||||
UNUSED(channel); // ok: there is no channel
|
||||
UNUSED(size); // ok: fixed format events read from HCI buffer
|
||||
|
||||
static hids_client_t * hids_create_client(hci_con_handle_t con_handle, uint16_t cid){
|
||||
hids_client_t * client = btstack_memory_hids_client_get();
|
||||
if (!client){
|
||||
log_error("Not enough memory to create client");
|
||||
return NULL;
|
||||
}
|
||||
client->state = HIDS_CLIENT_STATE_IDLE;
|
||||
client->cid = cid;
|
||||
client->con_handle = con_handle;
|
||||
|
||||
client->num_instances = 0;
|
||||
btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
|
||||
return client;
|
||||
}
|
||||
|
||||
void hids_client_init(void){
|
||||
hci_event_callback_registration.callback = &handle_hci_event;
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
static void hids_finalize_client(hids_client_t * client){
|
||||
btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
|
||||
btstack_memory_hids_client_free(client);
|
||||
}
|
||||
|
||||
static hids_client_t * hids_get_client_for_con_handle(hci_con_handle_t con_handle){
|
||||
btstack_linked_list_iterator_t it;
|
||||
btstack_linked_list_iterator_init(&it, &clients);
|
||||
while (btstack_linked_list_iterator_has_next(&it)){
|
||||
hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
|
||||
if (client->con_handle != con_handle) continue;
|
||||
return client;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static hids_client_t * hids_get_client_for_cid(uint16_t hids_cid){
|
||||
btstack_linked_list_iterator_t it;
|
||||
btstack_linked_list_iterator_init(&it, &clients);
|
||||
while (btstack_linked_list_iterator_has_next(&it)){
|
||||
hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
|
||||
if (client->cid != hids_cid) continue;
|
||||
return client;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void hids_emit_connection_established(hids_client_t * client, uint8_t status){
|
||||
uint8_t event[7];
|
||||
int pos = 0;
|
||||
event[pos++] = HCI_EVENT_GATTSERVICE_META;
|
||||
event[pos++] = sizeof(event) - 2;
|
||||
event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED;
|
||||
little_endian_store_16(event, pos, client->cid);
|
||||
pos += 2;
|
||||
event[pos++] = status;
|
||||
event[pos++] = client->num_instances;
|
||||
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
||||
}
|
||||
|
||||
|
||||
static void hids_run_for_client(hids_client_t * client){
|
||||
uint8_t att_status;
|
||||
|
||||
switch (client->state){
|
||||
case HIDS_CLIENT_STATE_W2_QUERY_SERVICE:
|
||||
client->state = HIDS_CLIENT_STATE_W4_SERVICE_RESULT;
|
||||
att_status = gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE);
|
||||
// TODO handle status
|
||||
UNUSED(att_status);
|
||||
break;
|
||||
|
||||
case HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC:
|
||||
break;
|
||||
|
||||
case HIDS_CLIENT_STATE_CONNECTED:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
UNUSED(packet_type);
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
|
||||
hids_client_t * client;
|
||||
uint8_t att_status;
|
||||
|
||||
switch(hci_event_packet_get_type(packet)){
|
||||
case GATT_EVENT_SERVICE_QUERY_RESULT:
|
||||
client = hids_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet));
|
||||
btstack_assert(client != NULL);
|
||||
|
||||
break;
|
||||
|
||||
case GATT_EVENT_QUERY_COMPLETE:
|
||||
client = hids_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet));
|
||||
btstack_assert(client != NULL);
|
||||
|
||||
att_status = gatt_event_query_complete_get_att_status(packet);
|
||||
|
||||
switch (client->state){
|
||||
case HIDS_CLIENT_STATE_W4_SERVICE_RESULT:
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, uint16_t * hids_cid){
|
||||
btstack_assert(packet_handler != NULL);
|
||||
|
||||
hids_client_t * client = hids_get_client_for_con_handle(con_handle);
|
||||
if (client != NULL){
|
||||
return ERROR_CODE_COMMAND_DISALLOWED;
|
||||
}
|
||||
|
||||
uint16_t cid = hids_get_next_cid();
|
||||
if (hids_cid != NULL) {
|
||||
*hids_cid = cid;
|
||||
}
|
||||
|
||||
client = hids_create_client(con_handle, cid);
|
||||
if (client == NULL) {
|
||||
return BTSTACK_MEMORY_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
client->client_handler = packet_handler;
|
||||
client->state = HIDS_CLIENT_STATE_W2_QUERY_SERVICE;
|
||||
|
||||
hids_run_for_client(client);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t hids_client_disconnect(uint16_t hids_cid){
|
||||
hids_client_t * client = hids_get_client_for_cid(hids_cid);
|
||||
if (client == NULL){
|
||||
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
|
||||
}
|
||||
// finalize connection
|
||||
hids_finalize_client(client);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
void hids_client_init(void){}
|
||||
|
||||
void hids_client_deinit(void){}
|
||||
|
@ -45,12 +45,71 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
#include "hid.h"
|
||||
#include "btstack_defines.h"
|
||||
#include "bluetooth.h"
|
||||
#include "btstack_linked_list.h"
|
||||
#include "ble/gatt_client.h"
|
||||
|
||||
#ifndef MAX_NUM_HID_SERVICES
|
||||
#define MAX_NUM_HID_SERVICES 3
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
HIDS_CLIENT_STATE_IDLE,
|
||||
HIDS_CLIENT_STATE_W2_QUERY_SERVICE,
|
||||
HIDS_CLIENT_STATE_W4_SERVICE_RESULT,
|
||||
HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC,
|
||||
HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
|
||||
HIDS_CLIENT_STATE_CONNECTED
|
||||
} hid_service_client_state_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
// service
|
||||
uint16_t start_handle;
|
||||
uint16_t end_handle;
|
||||
|
||||
} hid_service_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
btstack_linked_item_t item;
|
||||
|
||||
hci_con_handle_t con_handle;
|
||||
uint16_t cid;
|
||||
hid_service_client_state_t state;
|
||||
btstack_packet_handler_t client_handler;
|
||||
|
||||
uint8_t num_instances;
|
||||
hid_service_t services[MAX_NUM_HID_SERVICES];
|
||||
} hids_client_t;
|
||||
|
||||
/* API_START */
|
||||
|
||||
/**
|
||||
* @brief Initialize Battery Service.
|
||||
*/
|
||||
void hids_client_init(void);
|
||||
void hids_client_register_callback(btstack_packet_handler_t callback);
|
||||
const char * hids_client_attribute_name_for_id(int id);
|
||||
|
||||
/* @brief Connect to HID Services of remote device.
|
||||
*
|
||||
* @param con_handle
|
||||
* @param packet_handler
|
||||
* @param hids_cid
|
||||
* @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED
|
||||
*/
|
||||
uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, uint16_t * hids_cid);
|
||||
|
||||
/**
|
||||
* @brief Disconnect from Battery Service.
|
||||
* @param hids_cid
|
||||
* @return status
|
||||
*/
|
||||
uint8_t hids_client_disconnect(uint16_t hids_cid);
|
||||
|
||||
/**
|
||||
* @brief De-initialize Battery Service.
|
||||
*/
|
||||
void hids_client_deinit(void);
|
||||
|
||||
/* API_END */
|
||||
|
||||
|
@ -3211,6 +3211,14 @@ typedef uint8_t sm_key_t[16];
|
||||
*/
|
||||
#define GATTSERVICE_SUBEVENT_SPP_SERVICE_DISCONNECTED 0x12
|
||||
|
||||
/**
|
||||
* @format 1211
|
||||
* @param subevent_code
|
||||
* @param cid
|
||||
* @param status
|
||||
* @param num_instances
|
||||
*/
|
||||
#define GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED 0x13
|
||||
|
||||
// MAP Meta Event Group
|
||||
|
||||
|
@ -3949,6 +3949,7 @@ static inline const uint8_t * hsp_subevent_ag_indication_get_value(const uint8_t
|
||||
return &event[4];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get field status from event HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED
|
||||
* @param event packet
|
||||
@ -9513,6 +9514,34 @@ static inline hci_con_handle_t gattservice_subevent_spp_service_disconnected_get
|
||||
return little_endian_read_16(event, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get field cid from event GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED
|
||||
* @param event packet
|
||||
* @return cid
|
||||
* @note: btstack_type 2
|
||||
*/
|
||||
static inline uint16_t gattservice_subevent_hid_service_connected_get_cid(const uint8_t * event){
|
||||
return little_endian_read_16(event, 3);
|
||||
}
|
||||
/**
|
||||
* @brief Get field status from event GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED
|
||||
* @param event packet
|
||||
* @return status
|
||||
* @note: btstack_type 1
|
||||
*/
|
||||
static inline uint8_t gattservice_subevent_hid_service_connected_get_status(const uint8_t * event){
|
||||
return event[5];
|
||||
}
|
||||
/**
|
||||
* @brief Get field num_instances from event GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED
|
||||
* @param event packet
|
||||
* @return num_instances
|
||||
* @note: btstack_type 1
|
||||
*/
|
||||
static inline uint8_t gattservice_subevent_hid_service_connected_get_num_instances(const uint8_t * event){
|
||||
return event[6];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get field map_cid from event MAP_SUBEVENT_CONNECTION_OPENED
|
||||
* @param event packet
|
||||
|
@ -55,6 +55,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef ENABLE_MALLOC_TEST
|
||||
extern "C" void * test_malloc(size_t size);
|
||||
#define malloc test_malloc
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MALLOC
|
||||
typedef struct btstack_memory_buffer {
|
||||
struct btstack_memory_buffer * next;
|
||||
@ -1094,63 +1099,6 @@ void btstack_memory_battery_service_client_free(battery_service_client_t *batter
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: scan_parameters_service_client_t
|
||||
#if !defined(HAVE_MALLOC) && !defined(MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS)
|
||||
#if defined(MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS)
|
||||
#error "Deprecated MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS defined instead of MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS. Please update your btstack_config.h to use MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS."
|
||||
#else
|
||||
#define MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
#if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
|
||||
static scan_parameters_service_client_t scan_parameters_service_client_storage[MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS];
|
||||
static btstack_memory_pool_t scan_parameters_service_client_pool;
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
void * buffer = btstack_memory_pool_get(&scan_parameters_service_client_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(scan_parameters_service_client_t));
|
||||
}
|
||||
return (scan_parameters_service_client_t *) buffer;
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
btstack_memory_pool_free(&scan_parameters_service_client_pool, scan_parameters_service_client);
|
||||
}
|
||||
#else
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
return NULL;
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
UNUSED(scan_parameters_service_client);
|
||||
};
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
|
||||
typedef struct {
|
||||
btstack_memory_buffer_t tracking;
|
||||
scan_parameters_service_client_t data;
|
||||
} btstack_memory_scan_parameters_service_client_t;
|
||||
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
btstack_memory_scan_parameters_service_client_t * buffer = (btstack_memory_scan_parameters_service_client_t *) malloc(sizeof(btstack_memory_scan_parameters_service_client_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(btstack_memory_scan_parameters_service_client_t));
|
||||
btstack_memory_tracking_add(&buffer->tracking);
|
||||
return &buffer->data;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
// reconstruct buffer start
|
||||
btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) scan_parameters_service_client)[-1];
|
||||
btstack_memory_tracking_remove(buffer);
|
||||
free(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: gatt_client_t
|
||||
#if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
|
||||
#if defined(MAX_NO_GATT_CLIENTS)
|
||||
@ -1208,6 +1156,120 @@ void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: hids_client_t
|
||||
#if !defined(HAVE_MALLOC) && !defined(MAX_NR_HIDS_CLIENTS)
|
||||
#if defined(MAX_NO_HIDS_CLIENTS)
|
||||
#error "Deprecated MAX_NO_HIDS_CLIENTS defined instead of MAX_NR_HIDS_CLIENTS. Please update your btstack_config.h to use MAX_NR_HIDS_CLIENTS."
|
||||
#else
|
||||
#define MAX_NR_HIDS_CLIENTS 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef MAX_NR_HIDS_CLIENTS
|
||||
#if MAX_NR_HIDS_CLIENTS > 0
|
||||
static hids_client_t hids_client_storage[MAX_NR_HIDS_CLIENTS];
|
||||
static btstack_memory_pool_t hids_client_pool;
|
||||
hids_client_t * btstack_memory_hids_client_get(void){
|
||||
void * buffer = btstack_memory_pool_get(&hids_client_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(hids_client_t));
|
||||
}
|
||||
return (hids_client_t *) buffer;
|
||||
}
|
||||
void btstack_memory_hids_client_free(hids_client_t *hids_client){
|
||||
btstack_memory_pool_free(&hids_client_pool, hids_client);
|
||||
}
|
||||
#else
|
||||
hids_client_t * btstack_memory_hids_client_get(void){
|
||||
return NULL;
|
||||
}
|
||||
void btstack_memory_hids_client_free(hids_client_t *hids_client){
|
||||
UNUSED(hids_client);
|
||||
};
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
|
||||
typedef struct {
|
||||
btstack_memory_buffer_t tracking;
|
||||
hids_client_t data;
|
||||
} btstack_memory_hids_client_t;
|
||||
|
||||
hids_client_t * btstack_memory_hids_client_get(void){
|
||||
btstack_memory_hids_client_t * buffer = (btstack_memory_hids_client_t *) malloc(sizeof(btstack_memory_hids_client_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(btstack_memory_hids_client_t));
|
||||
btstack_memory_tracking_add(&buffer->tracking);
|
||||
return &buffer->data;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
void btstack_memory_hids_client_free(hids_client_t *hids_client){
|
||||
// reconstruct buffer start
|
||||
btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hids_client)[-1];
|
||||
btstack_memory_tracking_remove(buffer);
|
||||
free(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: scan_parameters_service_client_t
|
||||
#if !defined(HAVE_MALLOC) && !defined(MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS)
|
||||
#if defined(MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS)
|
||||
#error "Deprecated MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS defined instead of MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS. Please update your btstack_config.h to use MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS."
|
||||
#else
|
||||
#define MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
#if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
|
||||
static scan_parameters_service_client_t scan_parameters_service_client_storage[MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS];
|
||||
static btstack_memory_pool_t scan_parameters_service_client_pool;
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
void * buffer = btstack_memory_pool_get(&scan_parameters_service_client_pool);
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(scan_parameters_service_client_t));
|
||||
}
|
||||
return (scan_parameters_service_client_t *) buffer;
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
btstack_memory_pool_free(&scan_parameters_service_client_pool, scan_parameters_service_client);
|
||||
}
|
||||
#else
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
return NULL;
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
UNUSED(scan_parameters_service_client);
|
||||
};
|
||||
#endif
|
||||
#elif defined(HAVE_MALLOC)
|
||||
|
||||
typedef struct {
|
||||
btstack_memory_buffer_t tracking;
|
||||
scan_parameters_service_client_t data;
|
||||
} btstack_memory_scan_parameters_service_client_t;
|
||||
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
|
||||
btstack_memory_scan_parameters_service_client_t * buffer = (btstack_memory_scan_parameters_service_client_t *) malloc(sizeof(btstack_memory_scan_parameters_service_client_t));
|
||||
if (buffer){
|
||||
memset(buffer, 0, sizeof(btstack_memory_scan_parameters_service_client_t));
|
||||
btstack_memory_tracking_add(&buffer->tracking);
|
||||
return &buffer->data;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
|
||||
// reconstruct buffer start
|
||||
btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) scan_parameters_service_client)[-1];
|
||||
btstack_memory_tracking_remove(buffer);
|
||||
free(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// MARK: sm_lookup_entry_t
|
||||
#if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
|
||||
#if defined(MAX_NO_SM_LOOKUP_ENTRIES)
|
||||
@ -1787,12 +1849,15 @@ void btstack_memory_init(void){
|
||||
#if MAX_NR_BATTERY_SERVICE_CLIENTS > 0
|
||||
btstack_memory_pool_create(&battery_service_client_pool, battery_service_client_storage, MAX_NR_BATTERY_SERVICE_CLIENTS, sizeof(battery_service_client_t));
|
||||
#endif
|
||||
#if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
|
||||
btstack_memory_pool_create(&scan_parameters_service_client_pool, scan_parameters_service_client_storage, MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS, sizeof(scan_parameters_service_client_t));
|
||||
#endif
|
||||
#if MAX_NR_GATT_CLIENTS > 0
|
||||
btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
|
||||
#endif
|
||||
#if MAX_NR_HIDS_CLIENTS > 0
|
||||
btstack_memory_pool_create(&hids_client_pool, hids_client_storage, MAX_NR_HIDS_CLIENTS, sizeof(hids_client_t));
|
||||
#endif
|
||||
#if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
|
||||
btstack_memory_pool_create(&scan_parameters_service_client_pool, scan_parameters_service_client_storage, MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS, sizeof(scan_parameters_service_client_t));
|
||||
#endif
|
||||
#if MAX_NR_SM_LOOKUP_ENTRIES > 0
|
||||
btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
|
||||
#endif
|
||||
|
@ -71,10 +71,11 @@ extern "C" {
|
||||
|
||||
// BLE
|
||||
#ifdef ENABLE_BLE
|
||||
#include "ble/gatt_client.h"
|
||||
#include "ble/gatt-service/battery_service_client.h"
|
||||
#include "ble/sm.h"
|
||||
#include "ble/gatt-service/hids_client.h"
|
||||
#include "ble/gatt-service/scan_parameters_service_client.h"
|
||||
#include "ble/gatt_client.h"
|
||||
#include "ble/sm.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MESH
|
||||
@ -157,13 +158,15 @@ void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t
|
||||
|
||||
#endif
|
||||
#ifdef ENABLE_BLE
|
||||
// battery_service_client, scan_parameters_service_client, gatt_client, sm_lookup_entry, whitelist_entry
|
||||
// battery_service_client, gatt_client, hids_client, scan_parameters_service_client, sm_lookup_entry, whitelist_entry
|
||||
battery_service_client_t * btstack_memory_battery_service_client_get(void);
|
||||
void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client);
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void);
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client);
|
||||
gatt_client_t * btstack_memory_gatt_client_get(void);
|
||||
void btstack_memory_gatt_client_free(gatt_client_t *gatt_client);
|
||||
hids_client_t * btstack_memory_hids_client_get(void);
|
||||
void btstack_memory_hids_client_free(hids_client_t *hids_client);
|
||||
scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void);
|
||||
void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client);
|
||||
sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void);
|
||||
void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry);
|
||||
whitelist_entry_t * btstack_memory_whitelist_entry_get(void);
|
||||
|
@ -802,48 +802,6 @@ TEST(btstack_memory, battery_service_client_NotEnoughBuffers){
|
||||
|
||||
|
||||
|
||||
TEST(btstack_memory, scan_parameters_service_client_GetAndFree){
|
||||
scan_parameters_service_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#else
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
// single
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#else
|
||||
// none
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context == NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(btstack_memory, scan_parameters_service_client_NotEnoughBuffers){
|
||||
scan_parameters_service_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
simulate_no_memory = 1;
|
||||
#else
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
int i;
|
||||
// alloc all static buffers
|
||||
for (i = 0; i < MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS; i++){
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
// get one more
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context == NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(btstack_memory, gatt_client_GetAndFree){
|
||||
gatt_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
@ -886,6 +844,90 @@ TEST(btstack_memory, gatt_client_NotEnoughBuffers){
|
||||
|
||||
|
||||
|
||||
TEST(btstack_memory, hids_client_GetAndFree){
|
||||
hids_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
context = btstack_memory_hids_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_hids_client_free(context);
|
||||
#else
|
||||
#ifdef MAX_NR_HIDS_CLIENTS
|
||||
// single
|
||||
context = btstack_memory_hids_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_hids_client_free(context);
|
||||
#else
|
||||
// none
|
||||
context = btstack_memory_hids_client_get();
|
||||
CHECK(context == NULL);
|
||||
btstack_memory_hids_client_free(context);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(btstack_memory, hids_client_NotEnoughBuffers){
|
||||
hids_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
simulate_no_memory = 1;
|
||||
#else
|
||||
#ifdef MAX_NR_HIDS_CLIENTS
|
||||
int i;
|
||||
// alloc all static buffers
|
||||
for (i = 0; i < MAX_NR_HIDS_CLIENTS; i++){
|
||||
context = btstack_memory_hids_client_get();
|
||||
CHECK(context != NULL);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
// get one more
|
||||
context = btstack_memory_hids_client_get();
|
||||
CHECK(context == NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(btstack_memory, scan_parameters_service_client_GetAndFree){
|
||||
scan_parameters_service_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#else
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
// single
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#else
|
||||
// none
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context == NULL);
|
||||
btstack_memory_scan_parameters_service_client_free(context);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(btstack_memory, scan_parameters_service_client_NotEnoughBuffers){
|
||||
scan_parameters_service_client_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
simulate_no_memory = 1;
|
||||
#else
|
||||
#ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
|
||||
int i;
|
||||
// alloc all static buffers
|
||||
for (i = 0; i < MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS; i++){
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context != NULL);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
// get one more
|
||||
context = btstack_memory_scan_parameters_service_client_get();
|
||||
CHECK(context == NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(btstack_memory, sm_lookup_entry_GetAndFree){
|
||||
sm_lookup_entry_t * context;
|
||||
#ifdef HAVE_MALLOC
|
||||
|
@ -79,10 +79,11 @@ extern "C" {
|
||||
|
||||
// BLE
|
||||
#ifdef ENABLE_BLE
|
||||
#include "ble/gatt_client.h"
|
||||
#include "ble/gatt-service/battery_service_client.h"
|
||||
#include "ble/sm.h"
|
||||
#include "ble/gatt-service/hids_client.h"
|
||||
#include "ble/gatt-service/scan_parameters_service_client.h"
|
||||
#include "ble/gatt_client.h"
|
||||
#include "ble/sm.h"
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MESH
|
||||
@ -299,7 +300,7 @@ list_of_classic_structs = [
|
||||
["avrcp_browsing_connection"],
|
||||
]
|
||||
list_of_le_structs = [
|
||||
["battery_service_client", "scan_parameters_service_client", "gatt_client", "sm_lookup_entry", "whitelist_entry"],
|
||||
["battery_service_client", "gatt_client", "hids_client", "scan_parameters_service_client", "sm_lookup_entry", "whitelist_entry"],
|
||||
]
|
||||
list_of_mesh_structs = [
|
||||
['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
|
||||
|
Loading…
x
Reference in New Issue
Block a user