test/gatt_server: integrate mock TLV

This commit is contained in:
Milanka Ringwald 2021-12-14 18:39:53 +01:00
parent 1e2cd7c0a8
commit 25336c9461
4 changed files with 91 additions and 24 deletions

View File

@ -11,15 +11,18 @@ link_libraries( CppUTest )
link_libraries( CppUTestExt )
include_directories(.)
include_directories(../mock/)
include_directories(../../src)
include_directories(../../3rd-party/rijndael/)
include_directories(../../3rd-party/micro-ecc/)
include_directories(../../platform/posix/)
include_directories( ${CMAKE_CURRENT_BINARY_DIR})
set(SOURCES
../../3rd-party/rijndael/rijndael.c
../../3rd-party/micro-ecc/uECC.c
../../src/ad_parser.c
../mock/mock_btstack_tlv.c
../../3rd-party/rijndael/rijndael.c
../../3rd-party/micro-ecc/uECC.c
../../src/ad_parser.c
../../src/ble/att_db.c
../../src/ble/att_db_util.c
../../src/ble/att_server.c
@ -36,8 +39,9 @@ set(SOURCES
../../src/btstack_linked_list.c
../../src/btstack_memory.c
../../src/btstack_memory_pool.c
../../src/btstack_tlv.c
../../src/btstack_util.c
../../src/btstack_tlv.c
../../platform/posix/btstack_tlv_posix.c
../../src/btstack_util.c
../../src/hci_cmd.c
../../src/hci_dump.c
mock.c
@ -49,7 +53,7 @@ add_library(btstack STATIC ${SOURCES})
# create targets
foreach(EXAMPLE_FILE gatt_server_test.cpp)
get_filename_component(EXAMPLE ${EXAMPLE_FILE} NAME_WE)
set (SOURCE_FILES ${EXAMPLE_FILE} mock.c)
set (SOURCE_FILES ${EXAMPLE_FILE} mock.c ../mock/mock_btstack_tlv.c ../mock/mock_btstack_tlv.h)
# profile.h
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/profile.h

View File

@ -8,6 +8,7 @@ CFLAGS += -I${BTSTACK_ROOT}/3rd-party/micro-ecc
CFLAGS += -I${BTSTACK_ROOT}/platform/embedded
CFLAGS += -I${BTSTACK_ROOT}/platform/posix
CFLAGS += -I${BTSTACK_ROOT}/src
CFLAGS += -I${BTSTACK_ROOT}/test/mock
CFLAGS += -I.
VPATH += ${BTSTACK_ROOT}/src
@ -16,6 +17,7 @@ VPATH += ${BTSTACK_ROOT}/src/ble/gatt-service
VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc
VPATH += ${BTSTACK_ROOT}/3rd-party/rijndael
VPATH += ${BTSTACK_ROOT}/test/mock
COMMON = \
ad_parser.c \
@ -38,6 +40,7 @@ COMMON = \
hids_device.c \
le_device_db_memory.c \
mock.c \
mock_btstack_tlv.c \
nordic_spp_service_server.c \
rijndael.c \
ublox_spp_service_server.c \

View File

@ -20,6 +20,8 @@
#include "ble/att_server.h"
#include "btstack_util.h"
#include "bluetooth.h"
#include "btstack_tlv.h"
#include "mock_btstack_tlv.h"
#include "bluetooth_gatt.h"
@ -28,6 +30,16 @@ static const uint8_t uuid128_with_bluetooth_base[] = { 0x00, 0x00, 0xBB, 0xBB, 0
static const uint8_t uuid128_no_bluetooth_base[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAA, 0xAA, 0x00, 0x00 };
extern "C" void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status);
extern "C" void mock_call_att_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
extern "C" void att_init_connection(uint16_t con_handle);
static uint8_t att_request[255];
static uint16_t att_write_request(uint16_t request_type, uint16_t attribute_handle, uint16_t value_length, const uint8_t * value){
att_request[0] = request_type;
little_endian_store_16(att_request, 1, attribute_handle);
(void)memcpy(&att_request[3], value, value_length);
return 3 + value_length;
}
static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
UNUSED(connection_handle);
@ -53,10 +65,15 @@ static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_h
TEST_GROUP(ATT_SERVER){
uint16_t att_con_handle;
mock_btstack_tlv_t tlv_context;
const btstack_tlv_t * tlv_impl;
void setup(void){
att_con_handle = 0x00;
att_init_connection(att_con_handle);
tlv_impl = mock_btstack_tlv_init_instance(&tlv_context);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
// init att db util and add a service and characteristic
att_db_util_init();
// 0x180F
@ -74,19 +91,25 @@ TEST_GROUP(ATT_SERVER){
att_db_util_add_characteristic_uuid128(uuid128_no_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
// 0x2A38
// 0x2A38btstack_tlv_set_instance
att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
att_db_util_add_characteristic_uuid128(uuid128_with_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
// 0x2AAB
att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
// 0x2A5C
att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CSC_FEATURE, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
// setup ATT server
att_server_init(att_db_util_get_address(), att_read_callback, att_write_callback);
}
void teardown(void) {
mock_btstack_tlv_deinit(&tlv_context);
}
};
TEST(ATT_SERVER, gatt_server_get_value_handle_for_characteristic_with_uuid16){
// att_dump_attributes();
uint16_t value_handle;
@ -129,7 +152,7 @@ TEST(ATT_SERVER, att_server_indicate){
uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
uint8_t status;
// invalid conneciton handle
// invalid connection handle
status = att_server_indicate(0x50, value_handle, &value[0], 0);
CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
@ -153,7 +176,7 @@ TEST(ATT_SERVER, att_server_notify){
uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
uint8_t status;
// invalid conneciton handle
// invalid connection handle
status = att_server_notify(0x50, value_handle, &value[0], 0);
CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
@ -169,19 +192,48 @@ TEST(ATT_SERVER, att_server_notify){
}
TEST(ATT_SERVER, att_server_get_mtu){
// invalid conneciton handle
// invalid connection handle
uint8_t mtu = att_server_get_mtu(0x50);
CHECK_EQUAL(0, mtu);
mtu = att_server_get_mtu(att_con_handle);
CHECK_EQUAL(0, mtu);
CHECK_EQUAL(23, mtu);
}
TEST(ATT_SERVER, att_server_request_can_send_now_event){
att_server_request_can_send_now_event(0x00);
att_server_request_can_send_now_event(att_con_handle);
}
TEST(ATT_SERVER, att_server_can_send_packet_now){
int status = att_server_can_send_packet_now(att_con_handle);
CHECK_EQUAL(1, status);
status = att_server_can_send_packet_now(0x50);
CHECK_EQUAL(0, status);
}
//static btstack_context_callback_registration_t indication_callback;
//
//TEST(ATT_SERVER, att_server_request_to_send_indication){
// int status = att_server_request_to_send_indication(&indication_callback, 0x55);
// CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
//
// status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
// CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
//
// status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
// CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status);
//}
TEST(ATT_SERVER, opcode_ATT_WRITE_REQUEST){
uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, 1, (uint8_t *)"a");
mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
}
// ATT_SIGNED_WRITE_COMMAND
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}

View File

@ -62,12 +62,19 @@ int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){
return 0;
}
static void att_init_connection(att_connection_t * att_connection){
att_connection->mtu = 23;
att_connection->max_mtu = 23;
att_connection->encryption_key_size = 0;
att_connection->authenticated = 0;
att_connection->authorized = 0;
void att_init_connection(uint16_t con_handle){
hci_connection.att_connection.mtu = 23;
hci_connection.att_connection.con_handle = con_handle;
hci_connection.att_connection.max_mtu = 23;
hci_connection.att_connection.encryption_key_size = 0;
hci_connection.att_connection.authenticated = 0;
hci_connection.att_connection.authorized = 0;
hci_connection.att_server.ir_le_device_db_index = 0;
if (btstack_linked_list_empty(&connections)){
btstack_linked_list_add(&connections, (btstack_linked_item_t *)&hci_connection);
}
}
void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
@ -143,7 +150,7 @@ void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t chan
uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
att_connection_t att_connection;
att_init_connection(&att_connection);
att_init_connection(handle);
uint8_t response[max_mtu];
uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
if (response_len){
@ -197,7 +204,7 @@ hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_
return NULL;
}
hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
if (con_handle != 0) return NULL;
if (con_handle != hci_connection.con_handle) return NULL;
return &hci_connection;
}
void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
@ -267,5 +274,6 @@ void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new
}
void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
UNUSED(con_handle);
uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
att_server_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
}