mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-29 04:20:20 +00:00
test/gatt-service-clien: simulate GATT_EVENT_QUERY_COMPLETE event
This commit is contained in:
parent
5fa2c39a49
commit
af7704047a
@ -24,6 +24,36 @@
|
||||
#include "mock_gatt_client.h"
|
||||
|
||||
static const hci_con_handle_t con_handle = 0x01;
|
||||
static bool connected;
|
||||
// temp btstack run loop mock
|
||||
|
||||
static btstack_timer_source_t * btstack_timer = NULL;
|
||||
|
||||
void btstack_run_lopo_deinit(void){
|
||||
btstack_timer = NULL;
|
||||
}
|
||||
|
||||
void btstack_run_loop_add_timer(btstack_timer_source_t * timer){
|
||||
btstack_timer = timer;
|
||||
}
|
||||
|
||||
void * btstack_run_loop_get_timer_context(btstack_timer_source_t * timer){
|
||||
return btstack_timer;
|
||||
}
|
||||
|
||||
int btstack_run_loop_remove_timer(btstack_timer_source_t * timer){
|
||||
btstack_timer = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer(btstack_timer_source_t * timer, uint32_t timeout_in_ms){
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer_context(btstack_timer_source_t * timer, void * context){
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer_handler(btstack_timer_source_t * timer, void (*process)(btstack_timer_source_t * _timer)){
|
||||
}
|
||||
|
||||
// simulate btstack_memory_battery_service_client_get
|
||||
|
||||
@ -31,27 +61,62 @@ static void gatt_client_event_handler(uint8_t packet_type, uint16_t channel, uin
|
||||
UNUSED(packet_type);
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
|
||||
// handle GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED
|
||||
// set flags
|
||||
}
|
||||
|
||||
TEST_GROUP(BATTERY_SERVICE_CLIENT){
|
||||
uint16_t battery_service_cid;
|
||||
|
||||
uint32_t poll_interval_ms;
|
||||
|
||||
void setup(void){
|
||||
uint32_t poll_interval_ms = 2000;
|
||||
battery_service_client_connect(con_handle, &gatt_client_event_handler, poll_interval_ms, &battery_service_cid);
|
||||
|
||||
battery_service_cid = 1;
|
||||
connected = false;
|
||||
poll_interval_ms = 2000;
|
||||
battery_service_client_init();
|
||||
}
|
||||
|
||||
void teardown(){
|
||||
battery_service_client_deinit();
|
||||
// mock().clear();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(BATTERY_SERVICE_CLIENT, lookup_attribute_handles){
|
||||
|
||||
TEST(BATTERY_SERVICE_CLIENT, connect_no_service){
|
||||
uint8_t status = battery_service_client_connect(con_handle, &gatt_client_event_handler, poll_interval_ms, &battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
|
||||
|
||||
mock_gatt_client_run();
|
||||
|
||||
CHECK_EQUAL(false, connected);
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST(BATTERY_SERVICE_CLIENT, disconnect){
|
||||
uint8_t status;
|
||||
|
||||
status = battery_service_client_disconnect(battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
|
||||
|
||||
status = battery_service_client_connect(con_handle, &gatt_client_event_handler, poll_interval_ms, &battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
|
||||
|
||||
status = battery_service_client_disconnect(battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
|
||||
|
||||
status = battery_service_client_disconnect(battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
|
||||
}
|
||||
|
||||
TEST(BATTERY_SERVICE_CLIENT, read_battery_level){
|
||||
uint8_t status = battery_service_client_connect(con_handle, &gatt_client_event_handler, poll_interval_ms, &battery_service_cid);
|
||||
CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
|
||||
|
||||
status = battery_service_client_read_battery_level(battery_service_cid + 1, 0);
|
||||
CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main (int argc, const char * argv[]){
|
||||
return CommandLineTestRunner::RunAllTests(argc, argv);
|
||||
}
|
||||
|
@ -2,50 +2,87 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "btstack_debug.h"
|
||||
#include "mock_gatt_client.h"
|
||||
|
||||
#include "CppUTest/TestHarness.h"
|
||||
#include "CppUTestExt/MockSupport.h"
|
||||
|
||||
static btstack_timer_source_t * btstack_timer = NULL;
|
||||
static enum {
|
||||
MOCK_QUERY_IDLE = 0,
|
||||
MOCK_QUERY_DISCOVER_PRIMARY_SERVICES,
|
||||
} mock_gatt_client_state;
|
||||
|
||||
static uint8_t mock_gatt_client_att_error;
|
||||
static uint16_t mock_gatt_client_uuid;
|
||||
static gatt_client_t gatt_client;
|
||||
|
||||
void mock_gatt_client_reset(void){
|
||||
mock_gatt_client_att_error = 0;
|
||||
mock_gatt_client_state = MOCK_QUERY_IDLE;
|
||||
}
|
||||
|
||||
void mock_gatt_client_add_primary_service(uint16_t service_uuid){
|
||||
}
|
||||
|
||||
void mock_gatt_client_add_characteristic(uint16_t characteristic_uuid){
|
||||
}
|
||||
|
||||
void mock_gatt_client_add_characteristic_descriptor(uint16_t descriptor_uuid){
|
||||
}
|
||||
|
||||
uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
|
||||
return ERROR_CODE_SUCCESS;
|
||||
// simulate erro
|
||||
void mock_gatt_client_simulate_att_error(uint8_t att_error){
|
||||
mock_gatt_client_att_error = att_error;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
|
||||
printf("gatt_client_discover_primary_services_by_uuid16 callback %p\n", callback);
|
||||
|
||||
mock_gatt_client_state = MOCK_QUERY_DISCOVER_PRIMARY_SERVICES;
|
||||
mock_gatt_client_uuid = uuid16;
|
||||
|
||||
gatt_client.callback = callback;
|
||||
gatt_client.con_handle = con_handle;
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
|
||||
btstack_assert(false);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
|
||||
btstack_assert(false);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
|
||||
btstack_assert(false);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_read_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
|
||||
btstack_assert(false);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t gatt_client_write_client_characteristic_configuration(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration){
|
||||
btstack_assert(false);
|
||||
return ERROR_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
|
||||
btstack_assert(false);
|
||||
}
|
||||
|
||||
// copied from gatt_client.c
|
||||
void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
|
||||
btstack_assert(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* copied from gatt_client.c - START
|
||||
*/
|
||||
void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
|
||||
service->start_group_handle = little_endian_read_16(packet, offset);
|
||||
service->end_group_handle = little_endian_read_16(packet, offset + 2);
|
||||
@ -57,7 +94,6 @@ void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_cli
|
||||
}
|
||||
}
|
||||
|
||||
// copied from gatt_client.c
|
||||
void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
|
||||
characteristic->start_handle = little_endian_read_16(packet, offset);
|
||||
characteristic->value_handle = little_endian_read_16(packet, offset + 2);
|
||||
@ -71,7 +107,6 @@ void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset,
|
||||
}
|
||||
}
|
||||
|
||||
// copied from gatt_client.c
|
||||
void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
|
||||
descriptor->handle = little_endian_read_16(packet, offset);
|
||||
reverse_128(&packet[offset+2], descriptor->uuid128);
|
||||
@ -82,28 +117,41 @@ void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, i
|
||||
}
|
||||
}
|
||||
|
||||
void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
|
||||
static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
|
||||
if (!callback) return;
|
||||
(*callback)(HCI_EVENT_PACKET, 0, packet, size);
|
||||
}
|
||||
|
||||
void btstack_run_loop_add_timer(btstack_timer_source_t * timer){
|
||||
btstack_timer = timer;
|
||||
static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){
|
||||
// @format H1
|
||||
uint8_t packet[5];
|
||||
packet[0] = GATT_EVENT_QUERY_COMPLETE;
|
||||
packet[1] = 3;
|
||||
little_endian_store_16(packet, 2, gatt_client->con_handle);
|
||||
packet[4] = att_status;
|
||||
emit_event_new(gatt_client->callback, packet, sizeof(packet));
|
||||
}
|
||||
|
||||
void * btstack_run_loop_get_timer_context(btstack_timer_source_t * timer){
|
||||
return btstack_timer;
|
||||
}
|
||||
/**
|
||||
* copied from gatt_client.c - END
|
||||
*/
|
||||
|
||||
int btstack_run_loop_remove_timer(btstack_timer_source_t * timer){
|
||||
btstack_timer = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer(btstack_timer_source_t * timer, uint32_t timeout_in_ms){
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer_context(btstack_timer_source_t * timer, void * context){
|
||||
}
|
||||
|
||||
void btstack_run_loop_set_timer_handler(btstack_timer_source_t * timer, void (*process)(btstack_timer_source_t * _timer)){
|
||||
}
|
||||
// magic
|
||||
|
||||
void mock_gatt_client_run(void){
|
||||
btstack_assert(mock_gatt_client_state != MOCK_QUERY_IDLE);
|
||||
switch (mock_gatt_client_state){
|
||||
case MOCK_QUERY_DISCOVER_PRIMARY_SERVICES:
|
||||
// emit GATT_EVENT_SERVICE_QUERY_RESULT for each matching service
|
||||
// TODO:
|
||||
// emit GATT_EVENT_QUERY_COMPLETE with status mock_gatt_client_att_error
|
||||
emit_gatt_complete_event(&gatt_client, ERROR_CODE_SUCCESS);
|
||||
// reset status
|
||||
mock_gatt_client_att_error = ERROR_CODE_SUCCESS;
|
||||
break;
|
||||
default:
|
||||
btstack_assert(false);
|
||||
break;
|
||||
}
|
||||
mock_gatt_client_state = MOCK_QUERY_IDLE;
|
||||
}
|
@ -50,7 +50,7 @@ void mock_gatt_client_reset(void);
|
||||
void mock_gatt_client_add_primary_service(uint16_t service_uuid);
|
||||
void mock_gatt_client_add_characteristic(uint16_t characteristic_uuid);
|
||||
void mock_gatt_client_add_characteristic_descriptor(uint16_t descriptor_uuid);
|
||||
|
||||
void mock_gatt_client_run(void);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user