From 6d9284d5873eeccff86a6a4c6bcd0678ef7ae075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Thu, 6 Jul 2023 08:34:02 +0200 Subject: [PATCH 01/12] apps/bttester: split bttester.h and add btp_ prefixes Splits `bttester.h` into seperate files, one for each BTP service. Adds BTP_ prefix for defines and btp_ prefix for structs. --- apps/bttester/src/btp/btp.h | 88 ++ apps/bttester/src/btp/btp_core.h | 51 + apps/bttester/src/btp/btp_gap.h | 343 +++++ apps/bttester/src/btp/btp_gatt.h | 347 +++++ apps/bttester/src/btp/btp_gattc.h | 266 ++++ apps/bttester/src/btp/btp_l2cap.h | 141 ++ apps/bttester/src/btp/btp_mesh.h | 205 +++ apps/bttester/src/btp/bttester.h | 124 ++ apps/bttester/src/btp_core.c | 167 ++ apps/bttester/src/{gap.c => btp_gap.c} | 408 ++--- apps/bttester/src/{gatt.c => btp_gatt.c} | 318 ++-- .../bttester/src/{gatt_cl.c => btp_gatt_cl.c} | 272 ++-- apps/bttester/src/{l2cap.c => btp_l2cap.c} | 120 +- apps/bttester/src/{mesh.c => btp_mesh.c} | 206 +-- apps/bttester/src/bttester.c | 145 +- apps/bttester/src/bttester.h | 1361 ----------------- apps/bttester/src/bttester_pipe.h | 2 +- apps/bttester/src/main.c | 2 +- 18 files changed, 2399 insertions(+), 2167 deletions(-) create mode 100644 apps/bttester/src/btp/btp.h create mode 100644 apps/bttester/src/btp/btp_core.h create mode 100644 apps/bttester/src/btp/btp_gap.h create mode 100644 apps/bttester/src/btp/btp_gatt.h create mode 100644 apps/bttester/src/btp/btp_gattc.h create mode 100644 apps/bttester/src/btp/btp_l2cap.h create mode 100644 apps/bttester/src/btp/btp_mesh.h create mode 100644 apps/bttester/src/btp/bttester.h create mode 100644 apps/bttester/src/btp_core.c rename apps/bttester/src/{gap.c => btp_gap.c} (79%) rename apps/bttester/src/{gatt.c => btp_gatt.c} (85%) rename apps/bttester/src/{gatt_cl.c => btp_gatt_cl.c} (82%) rename apps/bttester/src/{l2cap.c => btp_l2cap.c} (84%) rename apps/bttester/src/{mesh.c => btp_mesh.c} (78%) delete mode 100644 apps/bttester/src/bttester.h diff --git a/apps/bttester/src/btp/btp.h b/apps/bttester/src/btp/btp.h new file mode 100644 index 0000000000..3cf172f9b5 --- /dev/null +++ b/apps/bttester/src/btp/btp.h @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp.h - Bluetooth tester btp headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "bttester.h" +#include "btp_core.h" +#include "btp_gap.h" +#include "btp_gatt.h" +#include "btp_gattc.h" +#include "btp_l2cap.h" +#include "btp_mesh.h" + +#if MYNEWT_VAL(BLE_MESH) +#include "mesh/glue.h" +#else +#include "../glue.h" +#endif + +#define BTP_MTU MYNEWT_VAL(BTTESTER_BTP_DATA_SIZE_MAX) +#define BTP_DATA_MAX_SIZE (BTP_MTU - sizeof(struct btp_hdr)) + +#define BTP_INDEX_NONE 0xff + +#define BTP_SERVICE_ID_CORE 0 +#define BTP_SERVICE_ID_GAP 1 +#define BTP_SERVICE_ID_GATT 2 +#define BTP_SERVICE_ID_L2CAP 3 +#define BTP_SERVICE_ID_MESH 4 +#define BTP_SERVICE_ID_GATTC 6 + +#define BTP_STATUS_SUCCESS 0x00 +#define BTP_STATUS_FAILED 0x01 +#define BTP_STATUS_UNKNOWN_CMD 0x02 +#define BTP_STATUS_NOT_READY 0x03 + +#define SYS_LOG_DBG(fmt, ...) \ + if (MYNEWT_VAL(BTTESTER_DEBUG)) { \ + console_printf("[DBG] %s: " fmt "\n", \ + __func__, ## __VA_ARGS__); \ + } +#define SYS_LOG_INF(fmt, ...) console_printf("[INF] %s: " fmt "\n", \ + __func__, ## __VA_ARGS__); +#define SYS_LOG_ERR(fmt, ...) console_printf("[WRN] %s: " fmt "\n", \ + __func__, ## __VA_ARGS__); + +#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG +#define SYS_LOG_DOMAIN "bttester" + +#define sys_cpu_to_le32 htole32 +#define sys_le32_to_cpu le32toh +#define sys_cpu_to_le16 htole16 + +struct btp_hdr { + uint8_t service; + uint8_t opcode; + uint8_t index; + uint16_t len; + uint8_t data[0]; +} __packed; + +#define BTP_STATUS 0x00 +struct btp_status { + uint8_t code; +} __packed; diff --git a/apps/bttester/src/btp/btp_core.h b/apps/bttester/src/btp/btp_core.h new file mode 100644 index 0000000000..cbf309ce88 --- /dev/null +++ b/apps/bttester/src/btp/btp_core.h @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_core.h - Bluetooth tester Core service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* Core Service */ +#define BTP_CORE_READ_SUPPORTED_COMMANDS 0x01 +struct btp_core_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_CORE_READ_SUPPORTED_SERVICES 0x02 +struct btp_core_read_supported_services_rp { + uint8_t data[0]; +} __packed; + +#define BTP_CORE_REGISTER_SERVICE 0x03 +struct btp_core_register_service_cmd { + uint8_t id; +} __packed; + +#define BTP_CORE_UNREGISTER_SERVICE 0x04 +struct btp_core_unregister_service_cmd { + uint8_t id; +} __packed; + +/* events */ +#define BTP_CORE_EV_IUT_READY 0x80 \ No newline at end of file diff --git a/apps/bttester/src/btp/btp_gap.h b/apps/bttester/src/btp/btp_gap.h new file mode 100644 index 0000000000..4a24df678d --- /dev/null +++ b/apps/bttester/src/btp/btp_gap.h @@ -0,0 +1,343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_gap.h - Bluetooth tester GAP service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* GAP Service */ +/* commands */ +#define BTP_GAP_READ_SUPPORTED_COMMANDS 0x01 +struct btp_gap_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_GAP_READ_CONTROLLER_INDEX_LIST 0x02 +struct btp_gap_read_controller_index_list_rp { + uint8_t num; + uint8_t index[0]; +} __packed; + +#define BTP_GAP_SETTINGS_POWERED 0 +#define BTP_GAP_SETTINGS_CONNECTABLE 1 +#define BTP_GAP_SETTINGS_FAST_CONNECTABLE 2 +#define BTP_GAP_SETTINGS_DISCOVERABLE 3 +#define BTP_GAP_SETTINGS_BONDABLE 4 +#define BTP_GAP_SETTINGS_LINK_SEC_3 5 +#define BTP_GAP_SETTINGS_SSP 6 +#define BTP_GAP_SETTINGS_BREDR 7 +#define BTP_GAP_SETTINGS_HS 8 +#define BTP_GAP_SETTINGS_LE 9 +#define BTP_GAP_SETTINGS_ADVERTISING 10 +#define BTP_GAP_SETTINGS_SC 11 +#define BTP_GAP_SETTINGS_DEBUG_KEYS 12 +#define BTP_GAP_SETTINGS_PRIVACY 13 +#define BTP_GAP_SETTINGS_CONTROLLER_CONFIG 14 +#define BTP_GAP_SETTINGS_STATIC_ADDRESS 15 + +#define BTP_GAP_READ_CONTROLLER_INFO 0x03 +struct btp_gap_read_controller_info_rp { + uint8_t address[6]; + uint32_t supported_settings; + uint32_t current_settings; + uint8_t cod[3]; + uint8_t name[249]; + uint8_t short_name[11]; +} __packed; + +#define BTP_GAP_RESET 0x04 +struct btp_gap_reset_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_SET_POWERED 0x05 +struct btp_gap_set_powered_cmd { + uint8_t powered; +} __packed; +struct btp_gap_set_powered_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_SET_CONNECTABLE 0x06 +struct btp_gap_set_connectable_cmd { + uint8_t connectable; +} __packed; +struct btp_gap_set_connectable_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_SET_FAST_CONNECTABLE 0x07 +struct btp_gap_set_fast_connectable_cmd { + uint8_t fast_connectable; +} __packed; +struct btp_gap_set_fast_connectable_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_NON_DISCOVERABLE 0x00 +#define BTP_GAP_GENERAL_DISCOVERABLE 0x01 +#define BTP_GAP_LIMITED_DISCOVERABLE 0x02 + +#define BTP_GAP_SET_DISCOVERABLE 0x08 +struct btp_gap_set_discoverable_cmd { + uint8_t discoverable; +} __packed; +struct btp_gap_set_discoverable_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_SET_BONDABLE 0x09 +struct btp_gap_set_bondable_cmd { + uint8_t bondable; +} __packed; +struct btp_gap_set_bondable_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_START_ADVERTISING 0x0a +struct btp_gap_start_advertising_cmd { + uint8_t adv_data_len; + uint8_t scan_rsp_len; + uint8_t adv_data[0]; + uint8_t scan_rsp[0]; +} __packed; +struct btp_gap_start_advertising_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_STOP_ADVERTISING 0x0b +struct btp_gap_stop_advertising_rp { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_DISCOVERY_FLAG_LE 0x01 +#define BTP_GAP_DISCOVERY_FLAG_BREDR 0x02 +#define BTP_GAP_DISCOVERY_FLAG_LIMITED 0x04 +#define BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN 0x08 +#define BTP_GAP_DISCOVERY_FLAG_LE_OBSERVE 0x10 + +#define BTP_GAP_START_DISCOVERY 0x0c +struct btp_gap_start_discovery_cmd { + uint8_t flags; +} __packed; + +#define BTP_GAP_STOP_DISCOVERY 0x0d + +#define BTP_GAP_CONNECT 0x0e +struct btp_gap_connect_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_DISCONNECT 0x0f +struct btp_gap_disconnect_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_IO_CAP_DISPLAY_ONLY 0 +#define BTP_GAP_IO_CAP_DISPLAY_YESNO 1 +#define BTP_GAP_IO_CAP_KEYBOARD_ONLY 2 +#define BTP_GAP_IO_CAP_NO_INPUT_OUTPUT 3 +#define BTP_GAP_IO_CAP_KEYBOARD_DISPLAY 4 + +#define BTP_GAP_SET_IO_CAP 0x10 +struct btp_gap_set_io_cap_cmd { + uint8_t io_cap; +} __packed; + +#define BTP_GAP_PAIR 0x11 +struct btp_gap_pair_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_UNPAIR 0x12 +struct btp_gap_unpair_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_PASSKEY_ENTRY 0x13 +struct btp_gap_passkey_entry_cmd { + uint8_t address_type; + uint8_t address[6]; + uint32_t passkey; +} __packed; + +#define BTP_GAP_PASSKEY_CONFIRM 0x14 +struct btp_gap_passkey_confirm_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t match; +} __packed; + +#define BTP_GAP_START_DIRECT_ADV 0x15 +struct btp_gap_start_direct_adv_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t high_duty; +} __packed; + +#define BTP_GAP_CONN_PARAM_UPDATE 0x16 +struct btp_gap_conn_param_update_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t conn_itvl_min; + uint16_t conn_itvl_max; + uint16_t conn_latency; + uint16_t supervision_timeout; +} __packed; + +#define BTP_GAP_PAIRING_CONSENT_RSP 0x17 +struct btp_gap_pairing_consent_rsp_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t consent; +} __packed; + +#define BTP_GAP_OOB_LEGACY_SET_DATA 0x18 +struct btp_gap_oob_legacy_set_data_cmd { + uint8_t oob_data[16]; +} __packed; + +#define BTP_GAP_OOB_SC_GET_LOCAL_DATA 0x19 +struct btp_gap_oob_sc_get_local_data_rp { + uint8_t r[16]; + uint8_t c[16]; +} __packed; + +#define BTP_GAP_OOB_SC_SET_REMOTE_DATA 0x1a +struct btp_gap_oob_sc_set_remote_data_cmd { + uint8_t r[16]; + uint8_t c[16]; +} __packed; + +#define BTP_GAP_SET_MITM 0x1b +struct btp_gap_set_mitm_cmd { + uint8_t mitm; +} __packed; + +#define BTP_GAP_SET_FILTER_ACCEPT_LIST 0x1c +struct btp_gap_set_filter_accept_list_cmd { + uint8_t list_len; + ble_addr_t addrs[]; +} __packed; +/* events */ +#define BTP_GAP_EV_NEW_SETTINGS 0x80 +struct btp_gap_new_settings_ev { + uint32_t current_settings; +} __packed; + +#define BTP_GAP_DEVICE_FOUND_FLAG_RSSI 0x01 +#define BTP_GAP_DEVICE_FOUND_FLAG_AD 0x02 +#define BTP_GAP_DEVICE_FOUND_FLAG_SD 0x04 + +#define BTP_GAP_EV_DEVICE_FOUND 0x81 +struct btp_gap_device_found_ev { + uint8_t address_type; + uint8_t address[6]; + int8_t rssi; + uint8_t flags; + uint16_t eir_data_len; + uint8_t eir_data[0]; +} __packed; + +#define BTP_GAP_EV_DEVICE_CONNECTED 0x82 +struct btp_gap_device_connected_ev { + uint8_t address_type; + uint8_t address[6]; + uint16_t conn_itvl; + uint16_t conn_latency; + uint16_t supervision_timeout; +} __packed; + +#define BTP_GAP_EV_DEVICE_DISCONNECTED 0x83 +struct btp_gap_device_disconnected_ev { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_EV_PASSKEY_DISPLAY 0x84 +struct btp_gap_passkey_display_ev { + uint8_t address_type; + uint8_t address[6]; + uint32_t passkey; +} __packed; + +#define BTP_GAP_EV_PASSKEY_ENTRY_REQ 0x85 +struct btp_gap_passkey_entry_req_ev { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_EV_PASSKEY_CONFIRM_REQ 0x86 +struct btp_gap_passkey_confirm_req_ev { + uint8_t address_type; + uint8_t address[6]; + uint32_t passkey; +} __packed; + +#define BTP_GAP_EV_IDENTITY_RESOLVED 0x87 +struct btp_gap_identity_resolved_ev { + uint8_t address_type; + uint8_t address[6]; + uint8_t identity_address_type; + uint8_t identity_address[6]; +} __packed; + +#define BTP_GAP_EV_CONN_PARAM_UPDATE 0x88 +struct btp_gap_conn_param_update_ev { + uint8_t address_type; + uint8_t address[6]; + uint16_t conn_itvl; + uint16_t conn_latency; + uint16_t supervision_timeout; +} __packed; + +#define BTP_GAP_EV_SEC_LEVEL_CHANGED 0x89 +struct btp_gap_sec_level_changed_ev { + uint8_t address_type; + uint8_t address[6]; + uint8_t level; +} __packed; + +#define BTP_GAP_EV_PAIRING_CONSENT_REQ 0x8a +struct btp_gap_pairing_consent_req_ev { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_EV_BOND_LOST 0x8b +struct btp_gap_bond_lost_ev { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GAP_EV_SEC_PAIRING_FAILED 0x8c +struct btp_gap_sec_pairing_failed_ev { + uint8_t address_type; + uint8_t address[6]; + uint8_t reason; +} __packed; diff --git a/apps/bttester/src/btp/btp_gatt.h b/apps/bttester/src/btp/btp_gatt.h new file mode 100644 index 0000000000..6e23366460 --- /dev/null +++ b/apps/bttester/src/btp/btp_gatt.h @@ -0,0 +1,347 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_gatt.h - Bluetooth tester GATT service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* GATT Service */ +/* commands */ +#define BTP_GATT_READ_SUPPORTED_COMMANDS 0x01 +struct btp_gatt_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_GATT_SERVICE_PRIMARY 0x00 +#define BTP_GATT_SERVICE_SECONDARY 0x01 + +#define BTP_GATT_ADD_SERVICE 0x02 +struct btp_gatt_add_service_cmd { + uint8_t type; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; +struct btp_gatt_add_service_rp { + uint16_t svc_id; +} __packed; + +#define BTP_GATT_ADD_CHARACTERISTIC 0x03 +struct btp_gatt_add_characteristic_cmd { + uint16_t svc_id; + uint8_t properties; + uint8_t permissions; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; +struct btp_gatt_add_characteristic_rp { + uint16_t char_id; +} __packed; + +#define BTP_GATT_ADD_DESCRIPTOR 0x04 +struct btp_gatt_add_descriptor_cmd { + uint16_t char_id; + uint8_t permissions; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; +struct btp_gatt_add_descriptor_rp { + uint16_t desc_id; +} __packed; + +#define BTP_GATT_ADD_INCLUDED_SERVICE 0x05 +struct btp_gatt_add_included_service_cmd { + uint16_t svc_id; +} __packed; +struct btp_gatt_add_included_service_rp { + uint16_t included_service_id; +} __packed; + +#define BTP_GATT_SET_VALUE 0x06 +struct btp_gatt_set_value_cmd { + uint16_t attr_id; + uint16_t len; + uint8_t value[0]; +} __packed; + +#define BTP_GATT_START_SERVER 0x07 +struct btp_gatt_start_server_rp { + uint16_t db_attr_off; + uint8_t db_attr_cnt; +} __packed; + +#define BTP_GATT_SET_ENC_KEY_SIZE 0x09 +struct btp_gatt_set_enc_key_size_cmd { + uint16_t attr_id; + uint8_t key_size; +} __packed; + +struct btp_gatt_service { + uint16_t start_handle; + uint16_t end_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +struct btp_gatt_included { + uint16_t included_handle; + struct btp_gatt_service service; +} __packed; + +struct btp_gatt_read_uuid_chr { + uint16_t handle; + uint8_t data[0]; +} __packed; + +struct btp_gatt_characteristic { + uint16_t characteristic_handle; + uint16_t value_handle; + uint8_t properties; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +struct btp_gatt_descriptor { + uint16_t descriptor_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +#define BTP_GATT_EXCHANGE_MTU 0x0a + +#define BTP_GATT_DISC_ALL_PRIM_SVCS 0x0b +struct btp_gatt_disc_all_prim_svcs_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; +struct btp_gatt_disc_all_prim_svcs_rp { + uint8_t services_count; + struct btp_gatt_service services[0]; +} __packed; + +#define BTP_GATT_DISC_PRIM_UUID 0x0c +struct btp_gatt_disc_prim_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; +struct btp_gatt_disc_prim_uuid_rp { + uint8_t services_count; + struct btp_gatt_service services[0]; +} __packed; + +#define BTP_GATT_FIND_INCLUDED 0x0d +struct btp_gatt_find_included_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; +struct btp_gatt_find_included_rp { + uint8_t services_count; + struct btp_gatt_included included[0]; +} __packed; + +#define BTP_GATT_DISC_ALL_CHRC 0x0e +struct btp_gatt_disc_all_chrc_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; +struct btp_gatt_disc_chrc_rp { + uint8_t characteristics_count; + struct btp_gatt_characteristic characteristics[0]; +} __packed; + +#define BTP_GATT_DISC_CHRC_UUID 0x0f +struct btp_gatt_disc_chrc_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +#define BTP_GATT_DISC_ALL_DESC 0x10 +struct btp_gatt_disc_all_desc_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; +struct btp_gatt_disc_all_desc_rp { + uint8_t descriptors_count; + struct btp_gatt_descriptor descriptors[0]; +} __packed; + +#define BTP_GATT_READ 0x11 +struct btp_gatt_read_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; +} __packed; +struct btp_gatt_read_rp { + uint8_t att_response; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_READ_UUID 0x12 +struct btp_gatt_read_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +#define BTP_GATT_READ_LONG 0x13 +struct btp_gatt_read_long_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; +} __packed; + +#define BTP_GATT_READ_MULTIPLE 0x14 +struct btp_gatt_read_multiple_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t handles_count; + uint16_t handles[0]; +} __packed; + +#define BTP_GATT_WRITE_WITHOUT_RSP 0x15 +struct btp_gatt_write_without_rsp_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_SIGNED_WRITE_WITHOUT_RSP 0x16 +struct btp_gatt_signed_write_without_rsp_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_WRITE 0x17 +struct btp_gatt_write_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_WRITE_LONG 0x18 +struct btp_gatt_write_long_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_RELIABLE_WRITE 0x19 +struct btp_gatt_reliable_write_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_CFG_NOTIFY 0x1a +#define BTP_GATT_CFG_INDICATE 0x1b +struct btp_gatt_cfg_notify_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t enable; + uint16_t ccc_handle; +} __packed; + +#define BTP_GATT_GET_ATTRIBUTES 0x1c +struct btp_gatt_get_attributes_cmd { + uint16_t start_handle; + uint16_t end_handle; + uint8_t type_length; + uint8_t type[0]; +} __packed; +struct btp_gatt_get_attributes_rp { + uint8_t attrs_count; + uint8_t attrs[0]; +} __packed; +struct btp_gatt_attr { + uint16_t handle; + uint8_t permission; + uint8_t type_length; + uint8_t type[0]; +} __packed; + +#define BTP_GATT_GET_ATTRIBUTE_VALUE 0x1d +struct btp_gatt_get_attribute_value_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; +} __packed; +struct btp_gatt_get_attribute_value_rp { + uint8_t att_response; + uint16_t value_length; + uint8_t value[0]; +} __packed; + +#define BTP_GATT_CHANGE_DATABASE 0x1e +struct btp_gatt_change_database { + uint16_t start_handle; + uint16_t end_handle; + uint8_t visibility; +} __packed; + +/* GATT events */ +#define BTP_GATT_EV_NOTIFICATION 0x80 +struct btp_gatt_notification_ev { + uint8_t address_type; + uint8_t address[6]; + uint8_t type; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATT_EV_ATTR_VALUE_CHANGED 0x81 +struct btp_gatt_attr_value_changed_ev { + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; diff --git a/apps/bttester/src/btp/btp_gattc.h b/apps/bttester/src/btp/btp_gattc.h new file mode 100644 index 0000000000..0b946a52b1 --- /dev/null +++ b/apps/bttester/src/btp/btp_gattc.h @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_gattc.h - Bluetooth tester GATT Client service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* GATT Client Service */ +/* commands */ +#define BTP_GATTC_READ_SUPPORTED_COMMANDS 0x01 +struct btp_gattc_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_EXCHANGE_MTU 0x02 + +#define BTP_GATTC_DISC_ALL_PRIM_SVCS 0x03 +struct btp_gattc_disc_all_prim_svcs_cmd { + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_GATTC_DISC_PRIM_UUID 0x04 +struct btp_gattc_disc_prim_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +#define BTP_GATTC_FIND_INCLUDED 0x05 +struct btp_gattc_find_included_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; + +#define BTP_GATTC_DISC_ALL_CHRC 0x06 +struct btp_gattc_disc_all_chrc_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; + +#define BTP_GATTC_DISC_CHRC_UUID 0x07 +struct btp_gattc_disc_chrc_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; + +#define BTP_GATTC_DISC_ALL_DESC 0x08 +struct btp_gattc_disc_all_desc_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; +} __packed; + +#define BTP_GATTC_READ 0x09 +struct btp_gattc_read_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; +} __packed; + +#define BTP_GATTC_READ_UUID 0x0a +struct btp_gattc_read_uuid_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t start_handle; + uint16_t end_handle; + uint8_t uuid_length; + uint8_t uuid[0]; +} __packed; +struct btp_gattc_read_uuid_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint16_t data_length; + uint8_t value_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_READ_LONG 0x0b +struct btp_gattc_read_long_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; +} __packed; + +#define BTP_GATTC_READ_MULTIPLE 0x0c +struct btp_gattc_read_multiple_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t handles_count; + uint16_t handles[0]; +} __packed; + +#define BTP_GATTC_WRITE_WITHOUT_RSP 0x0d +struct btp_gattc_write_without_rsp_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP 0x0e +struct btp_gattc_signed_write_without_rsp_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_WRITE 0x0f +struct btp_gattc_write_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_WRITE_LONG 0x10 +struct btp_gattc_write_long_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_RELIABLE_WRITE 0x11 +struct btp_gattc_reliable_write_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t handle; + uint16_t offset; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_CFG_NOTIFY 0x12 +#define BTP_GATTC_CFG_INDICATE 0x13 +struct btp_gattc_cfg_notify_cmd { + uint8_t address_type; + uint8_t address[6]; + uint8_t enable; + uint16_t ccc_handle; +} __packed; + +/* events */ +#define BTP_GATTC_EV_MTU_EXCHANGED 0x80 +struct btp_gattc_exchange_mtu_ev { + uint8_t address_type; + uint8_t address[6]; + uint16_t mtu; +} __packed; + +#define BTP_GATTC_DISC_ALL_PRIM_RP 0x81 +struct btp_gattc_disc_prim_svcs_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint8_t services_count; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_DISC_PRIM_UUID_RP 0x82 + +#define BTP_GATTC_FIND_INCLUDED_RP 0x83 +struct btp_gattc_find_included_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint8_t services_count; + struct btp_gatt_included included[0]; +} __packed; + +#define BTP_GATTC_DISC_ALL_CHRC_RP 0x84 +#define BTP_GATTC_DISC_CHRC_UUID_RP 0x85 +struct btp_gattc_disc_chrc_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint8_t characteristics_count; + struct btp_gatt_characteristic characteristics[0]; +} __packed; + +#define BTP_GATTC_DISC_ALL_DESC_RP 0x86 +struct btp_gattc_disc_all_desc_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint8_t descriptors_count; + struct btp_gatt_descriptor descriptors[0]; +} __packed; + +#define BTP_GATTC_READ_RP 0x87 +#define BTP_GATTC_READ_UUID_RP 0x88 +#define BTP_GATTC_READ_LONG_RP 0x89 +#define BTP_GATTC_READ_MULTIPLE_RP 0x8a +struct btp_gattc_read_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_GATTC_WRITE_RP 0x8b +struct btp_gattc_write_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; +} __packed; +#define BTP_GATTC_WRITE_LONG_RP 0x8c +#define BTP_GATTC_RELIABLE_WRITE_RP 0x8d +#define BTP_GATTC_CFG_NOTIFY_RP 0x8e +#define BTP_GATTC_CFG_INDICATE_RP 0x8f +struct btp_subscribe_rp { + uint8_t address_type; + uint8_t address[6]; + uint8_t status; +} __packed; + +#define BTP_GATTC_EV_NOTIFICATION_RXED 0x90 +struct btp_gattc_notification_ev { + uint8_t address_type; + uint8_t address[6]; + uint8_t type; + uint16_t handle; + uint16_t data_length; + uint8_t data[0]; +} __packed; diff --git a/apps/bttester/src/btp/btp_l2cap.h b/apps/bttester/src/btp/btp_l2cap.h new file mode 100644 index 0000000000..5916345531 --- /dev/null +++ b/apps/bttester/src/btp/btp_l2cap.h @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_l2cap.h - Bluetooth tester L2CAP service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* L2CAP Service */ +/* commands */ +#define BTP_L2CAP_READ_SUPPORTED_COMMANDS 0x01 +struct btp_l2cap_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_L2CAP_CONNECT_OPT_ECFC 0x01 +#define BTP_L2CAP_CONNECT_OPT_HOLD_CREDIT 0x02 + +#define BTP_L2CAP_CONNECT 0x02 +struct btp_l2cap_connect_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t psm; + uint16_t mtu; + uint8_t num; + uint8_t options; +} __packed; + +struct btp_l2cap_connect_rp { + uint8_t num; + uint8_t chan_ids[0]; +} __packed; + +#define BTP_L2CAP_DISCONNECT 0x03 +struct btp_l2cap_disconnect_cmd { + uint8_t chan_id; +} __packed; + +#define BTP_L2CAP_SEND_DATA 0x04 +struct btp_l2cap_send_data_cmd { + uint8_t chan_id; + uint16_t data_len; + uint8_t data[]; +} __packed; + +#define BTP_L2CAP_TRANSPORT_BREDR 0x00 +#define BTP_L2CAP_TRANSPORT_LE 0x01 + +#define BTP_L2CAP_LISTEN 0x05 +struct btp_l2cap_listen_cmd { + uint16_t psm; + uint8_t transport; + uint16_t mtu; + uint16_t response; +} __packed; + +#define BTP_L2CAP_ACCEPT_CONNECTION 0x06 +struct l2cap_accept_connection_cmd { + uint8_t chan_id; + uint16_t result; +} __packed; + +#define BTP_L2CAP_RECONFIGURE 0x07 +struct btp_l2cap_reconfigure_cmd { + uint8_t address_type; + uint8_t address[6]; + uint16_t mtu; + uint8_t num; + uint8_t idxs[]; +} __packed; + +#define BTP_L2CAP_CREDITS 0x08 +struct btp_l2cap_credits_cmd { + uint8_t chan_id; +} __packed; + +/* events */ +#define BTP_L2CAP_EV_CONNECTION_REQ 0x80 +struct btp_l2cap_connection_req_ev { + uint8_t chan_id; + uint16_t psm; + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_L2CAP_EV_CONNECTED 0x81 +struct btp_l2cap_connected_ev { + uint8_t chan_id; + uint16_t psm; + uint16_t peer_mtu; + uint16_t peer_mps; + uint16_t our_mtu; + uint16_t our_mps; + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_L2CAP_EV_DISCONNECTED 0x82 +struct btp_l2cap_disconnected_ev { + uint16_t result; + uint8_t chan_id; + uint16_t psm; + uint8_t address_type; + uint8_t address[6]; +} __packed; + +#define BTP_L2CAP_EV_DATA_RECEIVED 0x83 +struct btp_l2cap_data_received_ev { + uint8_t chan_id; + uint16_t data_length; + uint8_t data[0]; +} __packed; + +#define BTP_L2CAP_EV_RECONFIGURED 0x84 +struct btp_l2cap_reconfigured_ev { + uint8_t chan_id; + uint16_t peer_mtu; + uint16_t peer_mps; + uint16_t our_mtu; + uint16_t our_mps; +} __packed; diff --git a/apps/bttester/src/btp/btp_mesh.h b/apps/bttester/src/btp/btp_mesh.h new file mode 100644 index 0000000000..0a356e40ff --- /dev/null +++ b/apps/bttester/src/btp/btp_mesh.h @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_mesh.h - Bluetooth tester MESH service headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* MESH Service */ +/* commands */ +#define BTP_MESH_READ_SUPPORTED_COMMANDS 0x01 +struct btp_mesh_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#define BTP_MESH_OUT_BLINK BIT(0) +#define BTP_MESH_OUT_BEEP BIT(1) +#define BTP_MESH_OUT_VIBRATE BIT(2) +#define BTP_MESH_OUT_DISPLAY_NUMBER BIT(3) +#define BTP_MESH_OUT_DISPLAY_STRING BIT(4) + +#define BTP_MESH_IN_PUSH BIT(0) +#define BTP_MESH_IN_TWIST BIT(1) +#define BTP_MESH_IN_ENTER_NUMBER BIT(2) +#define BTP_MESH_IN_ENTER_STRING BIT(3) + +#define BTP_MESH_CONFIG_PROVISIONING 0x02 +struct btp_mesh_config_provisioning_cmd { + uint8_t uuid[16]; + uint8_t static_auth[16]; + uint8_t out_size; + uint16_t out_actions; + uint8_t in_size; + uint16_t in_actions; +} __packed; + +#define BTP_MESH_PROVISION_NODE 0x03 +struct btp_mesh_provision_node_cmd { + uint8_t net_key[16]; + uint16_t net_key_idx; + uint8_t flags; + uint32_t iv_index; + uint32_t seq_num; + uint16_t addr; + uint8_t dev_key[16]; +} __packed; + +#define BTP_MESH_INIT 0x04 +#define BTP_MESH_RESET 0x05 +#define BTP_MESH_INPUT_NUMBER 0x06 +struct btp_mesh_input_number_cmd { + uint32_t number; +} __packed; + +#define BTP_MESH_INPUT_STRING 0x07 +struct btp_mesh_input_string_cmd { + uint8_t string_len; + uint8_t string[0]; +} __packed; + +#define BTP_MESH_IVU_TEST_MODE 0x08 +struct btp_mesh_ivu_test_mode_cmd { + uint8_t enable; +} __packed; + +#define BTP_MESH_IVU_TOGGLE_STATE 0x09 + +#define BTP_MESH_NET_SEND 0x0a +struct btp_mesh_net_send_cmd { + uint8_t ttl; + uint16_t src; + uint16_t dst; + uint8_t payload_len; + uint8_t payload[0]; +} __packed; + +#define BTP_MESH_HEALTH_GENERATE_FAULTS 0x0b +struct btp_mesh_health_generate_faults_rp { + uint8_t test_id; + uint8_t cur_faults_count; + uint8_t reg_faults_count; + uint8_t current_faults[0]; + uint8_t registered_faults[0]; +} __packed; + +#define BTP_MESH_HEALTH_CLEAR_FAULTS 0x0c + +#define BTP_MESH_LPN 0x0d +struct btp_mesh_lpn_set_cmd { + uint8_t enable; +} __packed; + +#define BTP_MESH_LPN_POLL 0x0e + +#define BTP_MESH_MODEL_SEND 0x0f +struct btp_mesh_model_send_cmd { + uint16_t src; + uint16_t dst; + uint8_t payload_len; + uint8_t payload[0]; +} __packed; + +#define BTP_MESH_LPN_SUBSCRIBE 0x10 +struct btp_mesh_lpn_subscribe_cmd { + uint16_t address; +} __packed; + +#define BTP_MESH_LPN_UNSUBSCRIBE 0x11 +struct btp_mesh_lpn_unsubscribe_cmd { + uint16_t address; +} __packed; + +#define BTP_MESH_RPL_CLEAR 0x12 +#define BTP_MESH_PROXY_IDENTITY 0x13 + +/* events */ +#define BTP_MESH_EV_OUT_NUMBER_ACTION 0x80 +struct btp_mesh_out_number_action_ev { + uint16_t action; + uint32_t number; +} __packed; + +#define BTP_MESH_EV_OUT_STRING_ACTION 0x81 +struct btp_mesh_out_string_action_ev { + uint8_t string_len; + uint8_t string[0]; +} __packed; + +#define BTP_MESH_EV_IN_ACTION 0x82 +struct btp_mesh_in_action_ev { + uint16_t action; + uint8_t size; +} __packed; + +#define BTP_MESH_EV_PROVISIONED 0x83 + +#define BTP_MESH_PROV_BEARER_PB_ADV 0x00 +#define BTP_MESH_PROV_BEARER_PB_GATT 0x01 +#define BTP_MESH_EV_PROV_LINK_OPEN 0x84 +struct btp_mesh_prov_link_open_ev { + uint8_t bearer; +} __packed; + +#define BTP_MESH_EV_PROV_LINK_CLOSED 0x85 +struct btp_mesh_prov_link_closed_ev { + uint8_t bearer; +} __packed; + +#define BTP_MESH_EV_NET_RECV 0x86 +struct btp_mesh_net_recv_ev { + uint8_t ttl; + uint8_t ctl; + uint16_t src; + uint16_t dst; + uint8_t payload_len; + uint8_t payload[0]; +} __packed; + +#define BTP_MESH_EV_INVALID_BEARER 0x87 +struct btp_mesh_invalid_bearer_ev { + uint8_t opcode; +} __packed; + +#define BTP_MESH_EV_INCOMP_TIMER_EXP 0x88 + +#define BTP_MESH_EV_LPN_ESTABLISHED 0x8b +struct btp_mesh_lpn_established_ev { + uint16_t net_idx; + uint16_t friend_addr; + uint8_t queue_size; + uint8_t recv_win; +} __packed; + +#define BTP_MESH_EV_LPN_TERMINATED 0x8c +struct btp_mesh_lpn_terminated_ev { + uint16_t net_idx; + uint16_t friend_addr; +} __packed; + +#define BTP_MESH_EV_LPN_POLLED 0x8d +struct btp_mesh_lpn_polled_ev { + uint16_t net_idx; + uint16_t friend_addr; + uint8_t retry; +} __packed; diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h new file mode 100644 index 0000000000..06ea5833e1 --- /dev/null +++ b/apps/bttester/src/btp/bttester.h @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* bttester.h - Bluetooth tester headers */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef __BTTESTER_H__ +#define __BTTESTER_H__ + +#include "syscfg/syscfg.h" +#include "host/ble_gatt.h" +#if MYNEWT_VAL(BLE_MESH) +#include "mesh/glue.h" +#else +#include "glue.h" +#endif + +static inline void +tester_set_bit(uint8_t *addr, unsigned int bit) +{ + uint8_t *p = addr + (bit / 8); + + *p |= BIT(bit % 8); +} + +static inline uint8_t +tester_test_bit(const uint8_t *addr, unsigned int bit) +{ + const uint8_t *p = addr + (bit / 8); + + return *p & BIT(bit % 8); +} + + +void +tester_init(void); +void +tester_rsp(uint8_t service, uint8_t opcode, uint8_t index, uint8_t status); +void +tester_send(uint8_t service, uint8_t opcode, uint8_t index, uint8_t *data, + size_t len); +void +tester_send_buf(uint8_t service, uint8_t opcode, uint8_t index, + struct os_mbuf *buf); + +uint8_t +tester_init_gap(void); +uint8_t +tester_unregister_gap(void); +void +tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len); +void +tester_handle_core(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len); +uint8_t +tester_init_gatt(void); +uint8_t +tester_unregister_gatt(void); +void +tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len); +void +tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len); +int +tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, + uint8_t indication, struct os_mbuf *om); +int +tester_gatt_subscribe_ev(uint16_t conn_handle, + uint16_t attr_handle, + uint8_t reason, + uint8_t prev_notify, + uint8_t cur_notify, + uint8_t prev_indicate, + uint8_t cur_indicate); + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) +uint8_t +tester_init_l2cap(void); +uint8_t +tester_unregister_l2cap(void); +void +tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len); +#endif + +#if MYNEWT_VAL(BLE_MESH) +uint8_t +tester_init_mesh(void); +uint8_t +tester_unregister_mesh(void); +void +tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len); +#endif /* MYNEWT_VAL(BLE_MESH) */ + +void +gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); + +int +gatt_svr_init(void); +#endif /* __BTTESTER_H__ */ \ No newline at end of file diff --git a/apps/bttester/src/btp_core.c b/apps/bttester/src/btp_core.c new file mode 100644 index 0000000000..afcac7c52b --- /dev/null +++ b/apps/bttester/src/btp_core.c @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_core.c - Bluetooth BTP Core service */ + +/* + * Copyright (C) 2023 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "btp/btp.h" + +static void +supported_commands(uint8_t *data, uint16_t len) +{ + uint8_t buf[1]; + struct core_read_supported_commands_rp *rp = (void *) buf; + + memset(buf, 0, sizeof(buf)); + + tester_set_bit(buf, BTP_CORE_READ_SUPPORTED_COMMANDS); + tester_set_bit(buf, BTP_CORE_READ_SUPPORTED_SERVICES); + tester_set_bit(buf, BTP_CORE_REGISTER_SERVICE); + tester_set_bit(buf, BTP_CORE_UNREGISTER_SERVICE); + + tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_READ_SUPPORTED_COMMANDS, + BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); +} + +static void +supported_services(uint8_t *data, uint16_t len) +{ + uint8_t buf[1]; + struct core_read_supported_services_rp *rp = (void *) buf; + + memset(buf, 0, sizeof(buf)); + + tester_set_bit(buf, BTP_SERVICE_ID_CORE); + tester_set_bit(buf, BTP_SERVICE_ID_GAP); + tester_set_bit(buf, BTP_SERVICE_ID_GATT); +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) + tester_set_bit(buf, BTP_SERVICE_ID_L2CAP); +#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ +#if MYNEWT_VAL(BLE_MESH) + tester_set_bit(buf, BTP_SERVICE_ID_MESH); +#endif /* MYNEWT_VAL(BLE_MESH) */ + tester_set_bit(buf, BTP_SERVICE_ID_GATTC); + + tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_READ_SUPPORTED_SERVICES, + BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); +} + +static void +register_service(uint8_t *data, uint16_t len) +{ + struct core_register_service_cmd *cmd = (void *) data; + uint8_t status; + + switch (cmd->id) { + case BTP_SERVICE_ID_GAP: + status = tester_init_gap(); + /* Rsp with success status will be handled by bt enable cb */ + if (status == BTP_STATUS_FAILED) { + goto rsp; + } + return; + case BTP_SERVICE_ID_GATT: + status = tester_init_gatt(); + break; +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) + case BTP_SERVICE_ID_L2CAP: + status = tester_init_l2cap(); + break; +#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ +#if MYNEWT_VAL(BLE_MESH) + case BTP_SERVICE_ID_MESH: + status = tester_init_mesh(); + break; +#endif /* MYNEWT_VAL(BLE_MESH) */ + default: + status = BTP_STATUS_FAILED; + break; + } + +rsp: + tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, BTP_INDEX_NONE, + status); +} + +static void +unregister_service(uint8_t *data, uint16_t len) +{ + struct core_unregister_service_cmd *cmd = (void *) data; + uint8_t status; + + switch (cmd->id) { + case BTP_SERVICE_ID_GAP: + status = tester_unregister_gap(); + break; + case BTP_SERVICE_ID_GATT: + status = tester_unregister_gatt(); + break; +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) + case BTP_SERVICE_ID_L2CAP: + status = tester_unregister_l2cap(); + break; +#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ +#if MYNEWT_VAL(BLE_MESH) + case BTP_SERVICE_ID_MESH: + status = tester_unregister_mesh(); + break; +#endif /* MYNEWT_VAL(BLE_MESH) */ + default: + status = BTP_STATUS_FAILED; + break; + } + + tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_UNREGISTER_SERVICE, BTP_INDEX_NONE, + status); +} + +void +tester_handle_core(uint8_t opcode, uint8_t index, uint8_t *data, + uint16_t len) +{ + if (index != BTP_INDEX_NONE) { + tester_rsp(BTP_SERVICE_ID_CORE, opcode, index, + BTP_STATUS_FAILED); + return; + } + + switch (opcode) { + case BTP_CORE_READ_SUPPORTED_COMMANDS: + supported_commands(data, len); + return; + case BTP_CORE_READ_SUPPORTED_SERVICES: + supported_services(data, len); + return; + case BTP_CORE_REGISTER_SERVICE: + register_service(data, len); + return; + case BTP_CORE_UNREGISTER_SERVICE: + unregister_service(data, len); + return; + default: + tester_rsp(BTP_SERVICE_ID_CORE, opcode, BTP_INDEX_NONE, + BTP_STATUS_UNKNOWN_CMD); + return; + } +} diff --git a/apps/bttester/src/gap.c b/apps/bttester/src/btp_gap.c similarity index 79% rename from apps/bttester/src/gap.c rename to apps/bttester/src/btp_gap.c index c28853a0e3..24f7f82dcc 100644 --- a/apps/bttester/src/gap.c +++ b/apps/bttester/src/btp_gap.c @@ -33,13 +33,13 @@ #include "../../../nimble/host/src/ble_hs_hci_priv.h" #include "../../../nimble/host/src/ble_sm_priv.h" -#include "bttester.h" +#include "btp/btp.h" #define CONTROLLER_INDEX 0 #define CONTROLLER_NAME "btp_tester" #define BLE_AD_DISCOV_MASK (BLE_HS_ADV_F_DISC_LTD | BLE_HS_ADV_F_DISC_GEN) -#define ADV_BUF_LEN (sizeof(struct gap_device_found_ev) + 2 * 31) +#define ADV_BUF_LEN (sizeof(struct btp_gap_device_found_ev) + 2 * 31) /* parameter values to reject in CPUP if all match the pattern */ #define REJECT_INTERVAL_MIN 0x0C80 @@ -62,11 +62,11 @@ static ble_addr_t peer_id_addr; static ble_addr_t peer_ota_addr; static bool encrypted = false; -static struct os_callout update_params_co; -static struct gap_conn_param_update_cmd update_params; +static struct os_callout update_params_co; +static struct btp_gap_conn_param_update_cmd update_params; -static struct os_callout connected_ev_co; -static struct gap_device_connected_ev connected_ev; +static struct os_callout connected_ev_co; +static struct btp_gap_device_connected_ev connected_ev; #define CONNECTED_EV_DELAY_MS(itvl) 8 * BLE_HCI_CONN_ITVL * itvl / 1000 static int connection_attempts; #if MYNEWT_VAL(BTTESTER_PRIVACY_MODE) && MYNEWT_VAL(BTTESTER_USE_NRPA) @@ -124,46 +124,46 @@ gap_event_cb(struct ble_gap_event *event, void *arg); static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[3]; - struct gap_read_supported_commands_rp *rp = (void *) &cmds; + uint8_t cmds[3]; + struct btp_gap_read_supported_commands_rp *rp = (void *) &cmds; SYS_LOG_DBG(""); memset(cmds, 0, sizeof(cmds)); - tester_set_bit(cmds, GAP_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, GAP_READ_CONTROLLER_INDEX_LIST); - tester_set_bit(cmds, GAP_READ_CONTROLLER_INFO); - tester_set_bit(cmds, GAP_SET_CONNECTABLE); - tester_set_bit(cmds, GAP_SET_DISCOVERABLE); - tester_set_bit(cmds, GAP_SET_BONDABLE); - tester_set_bit(cmds, GAP_START_ADVERTISING); - tester_set_bit(cmds, GAP_STOP_ADVERTISING); - tester_set_bit(cmds, GAP_START_DISCOVERY); - tester_set_bit(cmds, GAP_STOP_DISCOVERY); - tester_set_bit(cmds, GAP_CONNECT); - tester_set_bit(cmds, GAP_DISCONNECT); - tester_set_bit(cmds, GAP_SET_IO_CAP); - tester_set_bit(cmds, GAP_PAIR); - tester_set_bit(cmds, GAP_UNPAIR); - tester_set_bit(cmds, GAP_PASSKEY_ENTRY); - tester_set_bit(cmds, GAP_PASSKEY_CONFIRM); - tester_set_bit(cmds, GAP_START_DIRECT_ADV); - tester_set_bit(cmds, GAP_CONN_PARAM_UPDATE); - tester_set_bit(cmds, GAP_OOB_LEGACY_SET_DATA); - tester_set_bit(cmds, GAP_OOB_SC_GET_LOCAL_DATA); - tester_set_bit(cmds, GAP_OOB_SC_SET_REMOTE_DATA); - tester_set_bit(cmds, GAP_SET_MITM); - - tester_send(BTP_SERVICE_ID_GAP, GAP_READ_SUPPORTED_COMMANDS, + tester_set_bit(cmds, BTP_GAP_READ_SUPPORTED_COMMANDS); + tester_set_bit(cmds, BTP_GAP_READ_CONTROLLER_INDEX_LIST); + tester_set_bit(cmds, BTP_GAP_READ_CONTROLLER_INFO); + tester_set_bit(cmds, BTP_GAP_SET_CONNECTABLE); + tester_set_bit(cmds, BTP_GAP_SET_DISCOVERABLE); + tester_set_bit(cmds, BTP_GAP_SET_BONDABLE); + tester_set_bit(cmds, BTP_GAP_START_ADVERTISING); + tester_set_bit(cmds, BTP_GAP_STOP_ADVERTISING); + tester_set_bit(cmds, BTP_GAP_START_DISCOVERY); + tester_set_bit(cmds, BTP_GAP_STOP_DISCOVERY); + tester_set_bit(cmds, BTP_GAP_CONNECT); + tester_set_bit(cmds, BTP_GAP_DISCONNECT); + tester_set_bit(cmds, BTP_GAP_SET_IO_CAP); + tester_set_bit(cmds, BTP_GAP_PAIR); + tester_set_bit(cmds, BTP_GAP_UNPAIR); + tester_set_bit(cmds, BTP_GAP_PASSKEY_ENTRY); + tester_set_bit(cmds, BTP_GAP_PASSKEY_CONFIRM); + tester_set_bit(cmds, BTP_GAP_START_DIRECT_ADV); + tester_set_bit(cmds, BTP_GAP_CONN_PARAM_UPDATE); + tester_set_bit(cmds, BTP_GAP_OOB_LEGACY_SET_DATA); + tester_set_bit(cmds, BTP_GAP_OOB_SC_GET_LOCAL_DATA); + tester_set_bit(cmds, BTP_GAP_OOB_SC_SET_REMOTE_DATA); + tester_set_bit(cmds, BTP_GAP_SET_MITM); + + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_SUPPORTED_COMMANDS, CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); } static void controller_index_list(uint8_t *data, uint16_t len) { - struct gap_read_controller_index_list_rp *rp; - uint8_t buf[sizeof(*rp) + 1]; + struct btp_gap_read_controller_index_list_rp *rp; + uint8_t buf[sizeof(*rp) + 1]; SYS_LOG_DBG(""); @@ -172,15 +172,15 @@ controller_index_list(uint8_t *data, uint16_t len) rp->num = 1; rp->index[0] = CONTROLLER_INDEX; - tester_send(BTP_SERVICE_ID_GAP, GAP_READ_CONTROLLER_INDEX_LIST, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_CONTROLLER_INDEX_LIST, BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); } static void controller_info(uint8_t *data, uint16_t len) { - struct gap_read_controller_info_rp rp; - uint32_t supported_settings = 0; + struct btp_gap_read_controller_info_rp rp; + uint32_t supported_settings = 0; ble_addr_t addr; int rc; @@ -207,33 +207,33 @@ controller_info(uint8_t *data, uint16_t len) } else { own_addr_type = BLE_OWN_ADDR_RPA_RANDOM_DEFAULT; } - current_settings |= BIT(GAP_SETTINGS_PRIVACY); - supported_settings |= BIT(GAP_SETTINGS_PRIVACY); + current_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); + supported_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); memcpy(rp.address, addr.val, sizeof(rp.address)); } else { rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, rp.address, NULL); if (rc) { own_addr_type = BLE_OWN_ADDR_RANDOM; memcpy(rp.address, addr.val, sizeof(rp.address)); - supported_settings |= BIT(GAP_SETTINGS_STATIC_ADDRESS); - current_settings |= BIT(GAP_SETTINGS_STATIC_ADDRESS); + supported_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS); + current_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS); } else { own_addr_type = BLE_OWN_ADDR_PUBLIC; } } - supported_settings |= BIT(GAP_SETTINGS_POWERED); - supported_settings |= BIT(GAP_SETTINGS_CONNECTABLE); - supported_settings |= BIT(GAP_SETTINGS_BONDABLE); - supported_settings |= BIT(GAP_SETTINGS_LE); - supported_settings |= BIT(GAP_SETTINGS_ADVERTISING); - supported_settings |= BIT(GAP_SETTINGS_SC); + supported_settings |= BIT(BTP_GAP_SETTINGS_POWERED); + supported_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE); + supported_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE); + supported_settings |= BIT(BTP_GAP_SETTINGS_LE); + supported_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); + supported_settings |= BIT(BTP_GAP_SETTINGS_SC); if (ble_hs_cfg.sm_bonding) { - current_settings |= BIT(GAP_SETTINGS_BONDABLE); + current_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE); } if (ble_hs_cfg.sm_sc) { - current_settings |= BIT(GAP_SETTINGS_SC); + current_settings |= BIT(BTP_GAP_SETTINGS_SC); } rp.supported_settings = sys_cpu_to_le32(supported_settings); @@ -241,7 +241,7 @@ controller_info(uint8_t *data, uint16_t len) memcpy(rp.name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME)); - tester_send(BTP_SERVICE_ID_GAP, GAP_READ_CONTROLLER_INFO, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_CONTROLLER_INFO, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } @@ -285,22 +285,22 @@ static void rotate_nrpa_cb(struct os_event *ev) static void set_connectable(uint8_t *data, uint16_t len) { - const struct gap_set_connectable_cmd *cmd = (void *) data; - struct gap_set_connectable_rp rp; + const struct btp_gap_set_connectable_cmd *cmd = (void *) data; + struct btp_gap_set_connectable_rp rp; SYS_LOG_DBG(""); if (cmd->connectable) { - current_settings |= BIT(GAP_SETTINGS_CONNECTABLE); + current_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE); adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; } else { - current_settings &= ~BIT(GAP_SETTINGS_CONNECTABLE); + current_settings &= ~BIT(BTP_GAP_SETTINGS_CONNECTABLE); adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; } rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_SET_CONNECTABLE, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_CONNECTABLE, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } @@ -309,59 +309,59 @@ static uint8_t ad_flags = BLE_HS_ADV_F_BREDR_UNSUP; static void set_discoverable(uint8_t *data, uint16_t len) { - const struct gap_set_discoverable_cmd *cmd = (void *) data; - struct gap_set_discoverable_rp rp; + const struct btp_gap_set_discoverable_cmd *cmd = (void *) data; + struct btp_gap_set_discoverable_rp rp; SYS_LOG_DBG(""); switch (cmd->discoverable) { - case GAP_NON_DISCOVERABLE: + case BTP_GAP_NON_DISCOVERABLE: ad_flags &= ~(BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_DISC_LTD); adv_params.disc_mode = BLE_GAP_DISC_MODE_NON; - current_settings &= ~BIT(GAP_SETTINGS_DISCOVERABLE); + current_settings &= ~BIT(BTP_GAP_SETTINGS_DISCOVERABLE); break; - case GAP_GENERAL_DISCOVERABLE: + case BTP_GAP_GENERAL_DISCOVERABLE: ad_flags &= ~BLE_HS_ADV_F_DISC_LTD; ad_flags |= BLE_HS_ADV_F_DISC_GEN; adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; - current_settings |= BIT(GAP_SETTINGS_DISCOVERABLE); + current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE); break; - case GAP_LIMITED_DISCOVERABLE: + case BTP_GAP_LIMITED_DISCOVERABLE: ad_flags &= ~BLE_HS_ADV_F_DISC_GEN; ad_flags |= BLE_HS_ADV_F_DISC_LTD; adv_params.disc_mode = BLE_GAP_DISC_MODE_LTD; - current_settings |= BIT(GAP_SETTINGS_DISCOVERABLE); + current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE); break; default: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_SET_DISCOVERABLE, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_DISCOVERABLE, CONTROLLER_INDEX, BTP_STATUS_FAILED); return; } rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_SET_DISCOVERABLE, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_DISCOVERABLE, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } static void set_bondable(const uint8_t *data, uint16_t len) { - const struct gap_set_bondable_cmd *cmd = (void *) data; - struct gap_set_bondable_rp rp; + const struct btp_gap_set_bondable_cmd *cmd = (void *) data; + struct btp_gap_set_bondable_rp rp; SYS_LOG_DBG(""); ble_hs_cfg.sm_bonding = cmd->bondable; if (ble_hs_cfg.sm_bonding) { - current_settings |= BIT(GAP_SETTINGS_BONDABLE); + current_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE); } else { - current_settings &= ~BIT(GAP_SETTINGS_BONDABLE); + current_settings &= ~BIT(BTP_GAP_SETTINGS_BONDABLE); } rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_SET_BONDABLE, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_BONDABLE, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } @@ -391,9 +391,9 @@ set_ad(const struct bt_data *ad, size_t ad_len, static void start_advertising(const uint8_t *data, uint16_t len) { - const struct gap_start_advertising_cmd *cmd = (void *) data; - struct gap_start_advertising_rp rp; - int32_t duration_ms = BLE_HS_FOREVER; + const struct btp_gap_start_advertising_cmd *cmd = (void *) data; + struct btp_gap_start_advertising_rp rp; + int32_t duration_ms = BLE_HS_FOREVER; uint8_t buf[BLE_HS_ADV_MAX_SZ]; uint8_t buf_len = 0; uint8_t adv_len, sd_len; @@ -471,34 +471,34 @@ start_advertising(const uint8_t *data, uint16_t len) goto fail; } - current_settings |= BIT(GAP_SETTINGS_ADVERTISING); + current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_START_ADVERTISING, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_START_ADVERTISING, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); return; fail: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_START_ADVERTISING, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_ADVERTISING, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void stop_advertising(const uint8_t *data, uint16_t len) { - struct gap_stop_advertising_rp rp; + struct btp_gap_stop_advertising_rp rp; SYS_LOG_DBG(""); if (ble_gap_adv_stop() != 0) { - tester_rsp(BTP_SERVICE_ID_GAP, GAP_STOP_ADVERTISING, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_ADVERTISING, CONTROLLER_INDEX, BTP_STATUS_FAILED); return; } - current_settings &= ~BIT(GAP_SETTINGS_ADVERTISING); + current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_STOP_ADVERTISING, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_ADVERTISING, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } @@ -537,7 +537,7 @@ static void store_adv(const ble_addr_t *addr, int8_t rssi, const uint8_t *data, uint8_t len) { - struct gap_device_found_ev *ev; + struct btp_gap_device_found_ev *ev; /* cleanup */ net_buf_simple_init(adv_buf, 0); @@ -547,7 +547,7 @@ store_adv(const ble_addr_t *addr, int8_t rssi, memcpy(ev->address, addr->val, sizeof(ev->address)); ev->address_type = addr->type; ev->rssi = rssi; - ev->flags = GAP_DEVICE_FOUND_FLAG_AD | GAP_DEVICE_FOUND_FLAG_RSSI; + ev->flags = BTP_GAP_DEVICE_FOUND_FLAG_AD | BTP_GAP_DEVICE_FOUND_FLAG_RSSI; ev->eir_data_len = len; memcpy(net_buf_simple_add(adv_buf, len), data, len); } @@ -556,11 +556,11 @@ static void device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, const uint8_t *data, uint8_t len) { - struct gap_device_found_ev *ev; - ble_addr_t a; + struct btp_gap_device_found_ev *ev; + ble_addr_t a; /* if General/Limited Discovery - parse Advertising data to get flags */ - if (!(discovery_flags & GAP_DISCOVERY_FLAG_LE_OBSERVE) && + if (!(discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_OBSERVE) && (evtype != BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP)) { uint8_t flags = get_ad_flags(data, len); @@ -571,7 +571,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, } /* if Limited Discovery - ignore general discoverable devices */ - if ((discovery_flags & GAP_DISCOVERY_FLAG_LIMITED) && + if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) && !(flags & BLE_HS_ADV_F_DISC_LTD)) { SYS_LOG_DBG("General discoverable, skipping"); return; @@ -601,7 +601,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, } ev->eir_data_len += len; - ev->flags |= GAP_DEVICE_FOUND_FLAG_SD; + ev->flags |= BTP_GAP_DEVICE_FOUND_FLAG_SD; memcpy(net_buf_simple_add(adv_buf, len), data, len); @@ -613,7 +613,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, * current one */ if (adv_buf->om_len) { - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_FOUND, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, CONTROLLER_INDEX, adv_buf->om_data, adv_buf->om_len); } @@ -621,14 +621,14 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, store_adv(addr, rssi, data, len); /* if Active Scan and scannable event - wait for Scan Response */ - if ((discovery_flags & GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) && + if ((discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) && (evtype == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND || evtype == BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND)) { SYS_LOG_DBG("Waiting for scan response"); return; } done: - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_FOUND, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, CONTROLLER_INDEX, adv_buf->om_data, adv_buf->om_len); } @@ -647,20 +647,20 @@ discovery_cb(struct ble_gap_event *event, void *arg) static void start_discovery(const uint8_t *data, uint16_t len) { - const struct gap_start_discovery_cmd *cmd = (void *) data; - struct ble_gap_disc_params params = {0}; + const struct btp_gap_start_discovery_cmd *cmd = (void *) data; + struct ble_gap_disc_params params = {0}; uint8_t status; SYS_LOG_DBG(""); /* only LE scan is supported */ - if (cmd->flags & GAP_DISCOVERY_FLAG_BREDR) { + if (cmd->flags & BTP_GAP_DISCOVERY_FLAG_BREDR) { status = BTP_STATUS_FAILED; goto reply; } - params.passive = (cmd->flags & GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) == 0; - params.limited = (cmd->flags & GAP_DISCOVERY_FLAG_LIMITED) > 0; + params.passive = (cmd->flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) == 0; + params.limited = (cmd->flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) > 0; params.filter_duplicates = 1; if (ble_gap_disc(own_addr_type, BLE_HS_FOREVER, @@ -674,7 +674,7 @@ start_discovery(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; reply: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_START_DISCOVERY, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_DISCOVERY, CONTROLLER_INDEX, status); } @@ -689,7 +689,7 @@ stop_discovery(const uint8_t *data, uint16_t len) status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, GAP_STOP_DISCOVERY, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_DISCOVERY, CONTROLLER_INDEX, status); } @@ -728,12 +728,12 @@ device_connected_ev_send(struct os_event *ev) rc = gap_conn_find_by_addr((ble_addr_t *) &connected_ev, &desc); if (rc) { - tester_rsp(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_CONNECTED, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, CONTROLLER_INDEX, BTP_STATUS_FAILED); return; } - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_CONNECTED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, CONTROLLER_INDEX, (uint8_t *) &connected_ev, sizeof(connected_ev)); @@ -774,7 +774,7 @@ le_connected(uint16_t conn_handle, int status) os_time_ms_to_ticks32( CONNECTED_EV_DELAY_MS(desc.conn_itvl))); #else - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_CONNECTED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, CONTROLLER_INDEX, (uint8_t *) &connected_ev, sizeof(connected_ev)); #endif @@ -783,8 +783,8 @@ le_connected(uint16_t conn_handle, int status) static void le_disconnected(struct ble_gap_conn_desc *conn, int reason) { - struct gap_device_disconnected_ev ev; - ble_addr_t *addr = &conn->peer_ota_addr; + struct btp_gap_device_disconnected_ev ev; + ble_addr_t *addr = &conn->peer_ota_addr; SYS_LOG_DBG(""); @@ -818,7 +818,7 @@ le_disconnected(struct ble_gap_conn_desc *conn, int reason) memcpy(ev.address, addr->val, sizeof(ev.address)); ev.address_type = addr->type; - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_DISCONNECTED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -846,9 +846,9 @@ auth_passkey_oob(uint16_t conn_handle) static void auth_passkey_display(uint16_t conn_handle, unsigned int passkey) { - struct ble_gap_conn_desc desc; - struct gap_passkey_display_ev ev; - ble_addr_t *addr; + struct ble_gap_conn_desc desc; + struct btp_gap_passkey_display_ev ev; + ble_addr_t *addr; struct ble_sm_io pk; int rc; @@ -874,16 +874,16 @@ auth_passkey_display(uint16_t conn_handle, unsigned int passkey) ev.address_type = addr->type; ev.passkey = sys_cpu_to_le32(pk.passkey); - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_PASSKEY_DISPLAY, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void auth_passkey_entry(uint16_t conn_handle) { - struct ble_gap_conn_desc desc; - struct gap_passkey_entry_req_ev ev; - ble_addr_t *addr; + struct ble_gap_conn_desc desc; + struct btp_gap_passkey_entry_req_ev ev; + ble_addr_t *addr; int rc; SYS_LOG_DBG(""); @@ -898,16 +898,16 @@ auth_passkey_entry(uint16_t conn_handle) memcpy(ev.address, addr->val, sizeof(ev.address)); ev.address_type = addr->type; - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_PASSKEY_ENTRY_REQ, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) { - struct ble_gap_conn_desc desc; - struct gap_passkey_confirm_req_ev ev; - ble_addr_t *addr; + struct ble_gap_conn_desc desc; + struct btp_gap_passkey_confirm_req_ev ev; + ble_addr_t *addr; int rc; SYS_LOG_DBG(""); @@ -923,7 +923,7 @@ auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) ev.address_type = addr->type; ev.passkey = sys_cpu_to_le32(passkey); - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_PASSKEY_CONFIRM_REQ, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -984,9 +984,9 @@ le_passkey_action(uint16_t conn_handle, static void le_identity_resolved(uint16_t conn_handle) { - struct ble_gap_conn_desc desc; - struct gap_identity_resolved_ev ev; - int rc; + struct ble_gap_conn_desc desc; + struct btp_gap_identity_resolved_ev ev; + int rc; SYS_LOG_DBG(""); @@ -1005,16 +1005,16 @@ le_identity_resolved(uint16_t conn_handle) memcpy(ev.identity_address, desc.peer_id_addr.val, sizeof(ev.identity_address)); - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_IDENTITY_RESOLVED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void le_pairing_failed(uint16_t conn_handle, int reason) { - struct ble_gap_conn_desc desc; - struct gap_sec_pairing_failed_ev ev; - int rc; + struct ble_gap_conn_desc desc; + struct btp_gap_sec_pairing_failed_ev ev; + int rc; SYS_LOG_DBG(""); @@ -1031,14 +1031,14 @@ le_pairing_failed(uint16_t conn_handle, int reason) ev.reason = reason; - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_SEC_PAIRING_FAILED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_PAIRING_FAILED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void le_conn_param_update(struct ble_gap_conn_desc *desc) { - struct gap_conn_param_update_ev ev; + struct btp_gap_conn_param_update_ev ev; SYS_LOG_DBG(""); @@ -1049,14 +1049,14 @@ le_conn_param_update(struct ble_gap_conn_desc *desc) ev.conn_latency = desc->conn_latency; ev.supervision_timeout = desc->supervision_timeout; - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_CONN_PARAM_UPDATE, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_CONN_PARAM_UPDATE, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void le_encryption_changed(struct ble_gap_conn_desc *desc) { - struct gap_sec_level_changed_ev ev; + struct btp_gap_sec_level_changed_ev ev; SYS_LOG_DBG(""); @@ -1078,15 +1078,15 @@ le_encryption_changed(struct ble_gap_conn_desc *desc) } } - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_SEC_LEVEL_CHANGED, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_LEVEL_CHANGED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void bond_lost(uint16_t conn_handle) { - struct gap_bond_lost_ev ev; - struct ble_gap_conn_desc desc; + struct btp_gap_bond_lost_ev ev; + struct ble_gap_conn_desc desc; int rc; rc = ble_gap_conn_find(conn_handle, &desc); @@ -1095,7 +1095,7 @@ bond_lost(uint16_t conn_handle) memcpy(ev.address, &desc.peer_id_addr, sizeof(ev.address)); ev.address_type = desc.peer_id_addr.type; tester_send(BTP_SERVICE_ID_GAP, - GAP_EV_BOND_LOST, + BTP_GAP_EV_BOND_LOST, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); @@ -1166,12 +1166,12 @@ print_conn_desc(const struct ble_gap_conn_desc *desc) static void adv_complete(void) { - struct gap_new_settings_ev ev; + struct btp_gap_new_settings_ev ev; - current_settings &= ~BIT(GAP_SETTINGS_ADVERTISING); + current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); ev.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_NEW_SETTINGS, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -1372,7 +1372,7 @@ connect(const uint8_t *data, uint16_t len) status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, GAP_CONNECT, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_CONNECT, CONTROLLER_INDEX, status); } static void @@ -1397,36 +1397,36 @@ disconnect(const uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_DISCONNECT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_DISCONNECT, CONTROLLER_INDEX, status); } static void set_io_cap(const uint8_t *data, uint16_t len) { - const struct gap_set_io_cap_cmd *cmd = (void *) data; - uint8_t status; + const struct btp_gap_set_io_cap_cmd *cmd = (void *) data; + uint8_t status; SYS_LOG_DBG(""); switch (cmd->io_cap) { - case GAP_IO_CAP_DISPLAY_ONLY: + case BTP_GAP_IO_CAP_DISPLAY_ONLY: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; ble_hs_cfg.sm_mitm = 1; break; - case GAP_IO_CAP_KEYBOARD_DISPLAY: + case BTP_GAP_IO_CAP_KEYBOARD_DISPLAY: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_KEYBOARD_DISP; ble_hs_cfg.sm_mitm = 1; break; - case GAP_IO_CAP_NO_INPUT_OUTPUT: + case BTP_GAP_IO_CAP_NO_INPUT_OUTPUT: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO; ble_hs_cfg.sm_mitm = 0; break; - case GAP_IO_CAP_KEYBOARD_ONLY: + case BTP_GAP_IO_CAP_KEYBOARD_ONLY: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_KEYBOARD_ONLY; ble_hs_cfg.sm_mitm = 1; break; - case GAP_IO_CAP_DISPLAY_YESNO: + case BTP_GAP_IO_CAP_DISPLAY_YESNO: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_YES_NO; ble_hs_cfg.sm_mitm = 1; break; @@ -1438,7 +1438,7 @@ set_io_cap(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_SET_IO_CAP, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_IO_CAP, CONTROLLER_INDEX, status); } @@ -1466,7 +1466,7 @@ pair(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_PAIR, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PAIR, CONTROLLER_INDEX, status); } static void @@ -1479,14 +1479,14 @@ unpair(const uint8_t *data, uint16_t len) err = ble_gap_unpair((ble_addr_t *) data); status = (uint8_t) (err != 0 ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); - tester_rsp(BTP_SERVICE_ID_GAP, GAP_UNPAIR, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_UNPAIR, CONTROLLER_INDEX, status); } static void passkey_entry(const uint8_t *data, uint16_t len) { - const struct gap_passkey_entry_cmd *cmd = (void *) data; - struct ble_gap_conn_desc desc; + const struct btp_gap_passkey_entry_cmd *cmd = (void *) data; + struct ble_gap_conn_desc desc; struct ble_sm_io pk; uint8_t status; int rc; @@ -1511,15 +1511,15 @@ passkey_entry(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_PASSKEY_ENTRY, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PASSKEY_ENTRY, CONTROLLER_INDEX, status); } static void passkey_confirm(const uint8_t *data, uint16_t len) { - const struct gap_passkey_confirm_cmd *cmd = (void *) data; - struct ble_gap_conn_desc desc; + const struct btp_gap_passkey_confirm_cmd *cmd = (void *) data; + struct ble_gap_conn_desc desc; struct ble_sm_io pk; uint8_t status; int rc; @@ -1545,16 +1545,16 @@ passkey_confirm(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_PASSKEY_CONFIRM, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PASSKEY_CONFIRM, CONTROLLER_INDEX, status); } static void start_direct_adv(const uint8_t *data, uint16_t len) { - const struct gap_start_direct_adv_cmd *cmd = (void *) data; - struct gap_start_advertising_rp rp; - static struct ble_gap_adv_params adv_params = { + const struct btp_gap_start_direct_adv_cmd *cmd = (void *) data; + struct btp_gap_start_advertising_rp rp; + static struct ble_gap_adv_params adv_params = { .conn_mode = BLE_GAP_CONN_MODE_DIR, }; int err; @@ -1571,14 +1571,14 @@ start_direct_adv(const uint8_t *data, uint16_t len) goto fail; } - current_settings |= BIT(GAP_SETTINGS_ADVERTISING); + current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); rp.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, GAP_START_DIRECT_ADV, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_START_DIRECT_ADV, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); return; fail: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_START_DIRECT_ADV, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_DIRECT_ADV, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1591,7 +1591,7 @@ conn_param_update_cb(uint16_t conn_handle, int status, void *arg) static int conn_param_update_slave(uint16_t conn_handle, - const struct gap_conn_param_update_cmd *cmd) + const struct btp_gap_conn_param_update_cmd *cmd) { int rc; struct ble_l2cap_sig_update_params params; @@ -1612,7 +1612,7 @@ conn_param_update_slave(uint16_t conn_handle, static int conn_param_update_master(uint16_t conn_handle, - const struct gap_conn_param_update_cmd *cmd) + const struct btp_gap_conn_param_update_cmd *cmd) { int rc; struct ble_gap_upd_params params; @@ -1668,69 +1668,69 @@ conn_param_update(struct os_event *ev) static void conn_param_update_async(const uint8_t *data, uint16_t len) { - const struct gap_conn_param_update_cmd *cmd = (void *) data; + const struct btp_gap_conn_param_update_cmd *cmd = (void *) data; update_params = *cmd; os_callout_reset(&update_params_co, 0); - tester_rsp(BTP_SERVICE_ID_GAP, GAP_CONN_PARAM_UPDATE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_CONN_PARAM_UPDATE, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void oob_legacy_set_data(const uint8_t *data, uint16_t len) { - const struct gap_oob_legacy_set_data_cmd *cmd = (void *) data; + const struct btp_gap_oob_legacy_set_data_cmd *cmd = (void *) data; ble_hs_cfg.sm_oob_data_flag = 1; memcpy(oob, cmd->oob_data, sizeof(oob)); - tester_rsp(BTP_SERVICE_ID_GAP, GAP_OOB_LEGACY_SET_DATA, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_LEGACY_SET_DATA, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void oob_sc_get_local_data(const uint8_t *data, uint16_t len) { - struct gap_oob_sc_get_local_data_rp rp; + struct btp_gap_oob_sc_get_local_data_rp rp; memcpy(rp.r, oob_data_local.r, 16); memcpy(rp.c, oob_data_local.c, 16); - tester_send(BTP_SERVICE_ID_GAP, GAP_OOB_SC_GET_LOCAL_DATA, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_SC_GET_LOCAL_DATA, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } static void oob_sc_set_remote_data(const uint8_t *data, uint16_t len) { - const struct gap_oob_sc_set_remote_data_cmd *cmd = (void *) data; + const struct btp_gap_oob_sc_set_remote_data_cmd *cmd = (void *) data; ble_hs_cfg.sm_oob_data_flag = 1; memcpy(oob_data_remote.r, cmd->r, 16); memcpy(oob_data_remote.c, cmd->c, 16); - tester_rsp(BTP_SERVICE_ID_GAP, GAP_OOB_SC_SET_REMOTE_DATA, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_SC_SET_REMOTE_DATA, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void set_mitm(const uint8_t *data, uint16_t len) { - const struct gap_set_mitm_cmd *cmd = (void *) data; + const struct btp_gap_set_mitm_cmd *cmd = (void *) data; ble_hs_cfg.sm_mitm = cmd->mitm; - tester_rsp(BTP_SERVICE_ID_GAP, GAP_SET_MITM, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_MITM, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void set_filter_accept_list(const uint8_t *data, uint16_t len) { - uint8_t status = BTP_STATUS_SUCCESS; - struct gap_set_filter_accept_list_cmd *tmp = - (struct gap_set_filter_accept_list_cmd *) data; + uint8_t status = BTP_STATUS_SUCCESS; + struct btp_gap_set_filter_accept_list_cmd *tmp = + (struct btp_gap_set_filter_accept_list_cmd *) data; SYS_LOG_DBG(""); @@ -1743,7 +1743,7 @@ set_filter_accept_list(const uint8_t *data, uint16_t len) status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, GAP_SET_FILTER_ACCEPT_LIST, + tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_FILTER_ACCEPT_LIST, CONTROLLER_INDEX, status); } @@ -1752,8 +1752,8 @@ tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) { switch (opcode) { - case GAP_READ_SUPPORTED_COMMANDS: - case GAP_READ_CONTROLLER_INDEX_LIST: + case BTP_GAP_READ_SUPPORTED_COMMANDS: + case BTP_GAP_READ_CONTROLLER_INDEX_LIST: if (index != BTP_INDEX_NONE) { tester_rsp(BTP_SERVICE_ID_GAP, opcode, index, BTP_STATUS_FAILED); @@ -1770,76 +1770,76 @@ tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, } switch (opcode) { - case GAP_READ_SUPPORTED_COMMANDS: + case BTP_GAP_READ_SUPPORTED_COMMANDS: supported_commands(data, len); return; - case GAP_READ_CONTROLLER_INDEX_LIST: + case BTP_GAP_READ_CONTROLLER_INDEX_LIST: controller_index_list(data, len); return; - case GAP_READ_CONTROLLER_INFO: + case BTP_GAP_READ_CONTROLLER_INFO: controller_info(data, len); return; - case GAP_SET_CONNECTABLE: + case BTP_GAP_SET_CONNECTABLE: set_connectable(data, len); return; - case GAP_SET_DISCOVERABLE: + case BTP_GAP_SET_DISCOVERABLE: set_discoverable(data, len); return; - case GAP_SET_BONDABLE: + case BTP_GAP_SET_BONDABLE: set_bondable(data, len); return; - case GAP_START_ADVERTISING: + case BTP_GAP_START_ADVERTISING: start_advertising(data, len); return; - case GAP_STOP_ADVERTISING: + case BTP_GAP_STOP_ADVERTISING: stop_advertising(data, len); return; - case GAP_START_DISCOVERY: + case BTP_GAP_START_DISCOVERY: start_discovery(data, len); return; - case GAP_STOP_DISCOVERY: + case BTP_GAP_STOP_DISCOVERY: stop_discovery(data, len); return; - case GAP_CONNECT: + case BTP_GAP_CONNECT: connect(data, len); return; - case GAP_DISCONNECT: + case BTP_GAP_DISCONNECT: disconnect(data, len); return; - case GAP_SET_IO_CAP: + case BTP_GAP_SET_IO_CAP: set_io_cap(data, len); return; - case GAP_PAIR: + case BTP_GAP_PAIR: pair(data, len); return; - case GAP_UNPAIR: + case BTP_GAP_UNPAIR: unpair(data, len); return; - case GAP_PASSKEY_ENTRY: + case BTP_GAP_PASSKEY_ENTRY: passkey_entry(data, len); return; - case GAP_PASSKEY_CONFIRM: + case BTP_GAP_PASSKEY_CONFIRM: passkey_confirm(data, len); return; - case GAP_START_DIRECT_ADV: + case BTP_GAP_START_DIRECT_ADV: start_direct_adv(data, len); return; - case GAP_CONN_PARAM_UPDATE: + case BTP_GAP_CONN_PARAM_UPDATE: conn_param_update_async(data, len); return; - case GAP_OOB_LEGACY_SET_DATA: + case BTP_GAP_OOB_LEGACY_SET_DATA: oob_legacy_set_data(data, len); return; - case GAP_OOB_SC_GET_LOCAL_DATA: + case BTP_GAP_OOB_SC_GET_LOCAL_DATA: oob_sc_get_local_data(data, len); return; - case GAP_OOB_SC_SET_REMOTE_DATA: + case BTP_GAP_OOB_SC_SET_REMOTE_DATA: oob_sc_set_remote_data(data, len); return; - case GAP_SET_MITM: + case BTP_GAP_SET_MITM: set_mitm(data, len); return; - case GAP_SET_FILTER_ACCEPT_LIST: + case BTP_GAP_SET_FILTER_ACCEPT_LIST: set_filter_accept_list(data, len); return; default: @@ -1853,14 +1853,14 @@ static void tester_init_gap_cb(int err) { if (err) { - tester_rsp(BTP_SERVICE_ID_CORE, CORE_REGISTER_SERVICE, + tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, BTP_INDEX_NONE, BTP_STATUS_FAILED); return; } current_settings = 0; - current_settings |= BIT(GAP_SETTINGS_POWERED); - current_settings |= BIT(GAP_SETTINGS_LE); + current_settings |= BIT(BTP_GAP_SETTINGS_POWERED); + current_settings |= BIT(BTP_GAP_SETTINGS_LE); os_callout_init(&update_params_co, os_eventq_dflt_get(), conn_param_update, NULL); @@ -1868,7 +1868,7 @@ tester_init_gap_cb(int err) os_callout_init(&connected_ev_co, os_eventq_dflt_get(), device_connected_ev_send, NULL); - tester_rsp(BTP_SERVICE_ID_CORE, CORE_REGISTER_SERVICE, BTP_INDEX_NONE, + tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, BTP_INDEX_NONE, BTP_STATUS_SUCCESS); } diff --git a/apps/bttester/src/gatt.c b/apps/bttester/src/btp_gatt.c similarity index 85% rename from apps/bttester/src/gatt.c rename to apps/bttester/src/btp_gatt.c index dd7dadb4a5..d3b826f53b 100644 --- a/apps/bttester/src/gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -36,7 +36,7 @@ #include "../../../nimble/host/src/ble_att_priv.h" #include "../../../nimble/host/src/ble_gatt_priv.h" -#include "bttester.h" +#include "btp/btp.h" #define CONTROLLER_INDEX 0 #define MAX_BUFFER_SIZE 2048 @@ -255,8 +255,8 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = { static void attr_value_changed_ev(uint16_t handle, struct os_mbuf *data) { - struct gatt_attr_value_changed_ev *ev; - struct os_mbuf *buf = os_msys_get(0, 0); + struct btp_gatt_attr_value_changed_ev *ev; + struct os_mbuf *buf = os_msys_get(0, 0); SYS_LOG_DBG(""); @@ -267,7 +267,7 @@ attr_value_changed_ev(uint16_t handle, struct os_mbuf *data) ev->data_length = sys_cpu_to_le16(os_mbuf_len(data)); os_mbuf_appendfrom(buf, data, 0, os_mbuf_len(data)); - tester_send_buf(BTP_SERVICE_ID_GATT, GATT_EV_ATTR_VALUE_CHANGED, + tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_ATTR_VALUE_CHANGED, CONTROLLER_INDEX, buf); } @@ -572,7 +572,7 @@ gatt_svr_rel_write_test(uint16_t conn_handle, uint16_t attr_handle, static void start_server(uint8_t *data, uint16_t len) { - struct gatt_start_server_rp rp; + struct btp_gatt_start_server_rp rp; SYS_LOG_DBG(""); @@ -583,7 +583,7 @@ start_server(uint8_t *data, uint16_t len) rp.db_attr_off = 0; rp.db_attr_cnt = 0; - tester_send(BTP_SERVICE_ID_GATT, GATT_START_SERVER, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_START_SERVER, CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); } @@ -672,8 +672,8 @@ read_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct gatt_read_rp *rp = (void *) gatt_buf.buf; - uint8_t btp_opcode = (uint8_t) (int) arg; + struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; + uint8_t btp_opcode = (uint8_t) (int) arg; SYS_LOG_DBG("status=%d", error->status); @@ -703,8 +703,8 @@ read_cb(uint16_t conn_handle, static void read(uint8_t *data, uint16_t len) { - const struct gatt_read_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_read_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -717,12 +717,12 @@ read(uint8_t *data, uint16_t len) /* Clear buffer */ read_destroy(); - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { goto fail; } if (ble_gattc_read(conn.conn_handle, sys_le16_to_cpu(cmd->handle), - read_cb, (void *) GATT_READ)) { + read_cb, (void *) BTP_GATT_READ)) { read_destroy(); goto fail; } @@ -730,7 +730,7 @@ read(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -740,8 +740,8 @@ read_long_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct gatt_read_rp *rp = (void *) gatt_buf.buf; - uint8_t btp_opcode = (uint8_t) (int) arg; + struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; + uint8_t btp_opcode = (uint8_t) (int) arg; SYS_LOG_DBG("status=%d", error->status); @@ -775,8 +775,8 @@ read_long_cb(uint16_t conn_handle, static void read_long(uint8_t *data, uint16_t len) { - const struct gatt_read_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_read_long_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -789,14 +789,14 @@ read_long(uint8_t *data, uint16_t len) /* Clear buffer */ read_destroy(); - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { goto fail; } if (ble_gattc_read_long(conn.conn_handle, sys_le16_to_cpu(cmd->handle), sys_le16_to_cpu(cmd->offset), - read_long_cb, (void *) GATT_READ_LONG)) { + read_long_cb, (void *) BTP_GATT_READ_LONG)) { read_destroy(); goto fail; } @@ -804,15 +804,15 @@ read_long(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_LONG, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void read_multiple(uint8_t *data, uint16_t len) { - const struct gatt_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + const struct btp_gatt_read_multiple_cmd *cmd = (void *) data; + uint16_t handles[cmd->handles_count]; struct ble_gap_conn_desc conn; int rc, i; @@ -830,13 +830,13 @@ read_multiple(uint8_t *data, uint16_t len) /* Clear buffer */ read_destroy(); - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { goto fail; } if (ble_gattc_read_mult(conn.conn_handle, handles, cmd->handles_count, read_cb, - (void *) GATT_READ_MULTIPLE)) { + (void *) BTP_GATT_READ_MULTIPLE)) { read_destroy(); goto fail; } @@ -844,15 +844,15 @@ read_multiple(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ_MULTIPLE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_MULTIPLE, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) { - const struct gatt_write_without_rsp_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_write_without_rsp_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -892,8 +892,8 @@ write_rsp(uint16_t conn_handle, const struct ble_gatt_error *error, static void write(uint8_t *data, uint16_t len) { - const struct gatt_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_write_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -905,22 +905,22 @@ write(uint8_t *data, uint16_t len) if (ble_gattc_write_flat(conn.conn_handle, sys_le16_to_cpu(cmd->handle), cmd->data, sys_le16_to_cpu(cmd->data_length), - write_rsp, (void *) GATT_WRITE)) { + write_rsp, (void *) BTP_GATT_WRITE)) { goto fail; } return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void write_long(uint8_t *data, uint16_t len) { - const struct gatt_write_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_write_long_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; int rc = 0; @@ -941,7 +941,7 @@ write_long(uint8_t *data, uint16_t len) sys_le16_to_cpu(cmd->handle), sys_le16_to_cpu(cmd->offset), om, write_rsp, - (void *) GATT_WRITE_LONG); + (void *) BTP_GATT_WRITE_LONG); if (!rc) { return; } @@ -949,7 +949,7 @@ write_long(uint8_t *data, uint16_t len) fail: SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -964,7 +964,7 @@ reliable_write_rsp(uint16_t conn_handle, SYS_LOG_DBG("Reliable write status %d", err); - tester_send(BTP_SERVICE_ID_GATT, GATT_RELIABLE_WRITE, + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_RELIABLE_WRITE, CONTROLLER_INDEX, &err, sizeof(err)); return 0; } @@ -972,8 +972,8 @@ reliable_write_rsp(uint16_t conn_handle, static void reliable_write(uint8_t *data, uint16_t len) { - const struct gatt_reliable_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_reliable_write_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; int rc; @@ -1007,7 +1007,7 @@ reliable_write(uint8_t *data, uint16_t len) fail: os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1020,8 +1020,8 @@ static struct bt_gatt_subscribe_params { static void read_uuid(uint8_t *data, uint16_t len) { - const struct gatt_read_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_read_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; @@ -1039,14 +1039,14 @@ read_uuid(uint8_t *data, uint16_t len) /* Clear buffer */ read_destroy(); - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { goto fail; } if (ble_gattc_read_by_uuid(conn.conn_handle, sys_le16_to_cpu(cmd->start_handle), sys_le16_to_cpu(cmd->end_handle), &uuid.u, - read_long_cb, (void *) GATT_READ_UUID)) { + read_long_cb, (void *) BTP_GATT_READ_UUID)) { read_destroy(); goto fail; } @@ -1054,7 +1054,7 @@ read_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1063,9 +1063,9 @@ disc_prim_uuid_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *gatt_svc, void *arg) { - struct gatt_disc_prim_uuid_rp *rp = (void *) gatt_buf.buf; - struct gatt_service *service; - const ble_uuid_any_t *uuid; + struct btp_gatt_disc_prim_uuid_rp *rp = (void *) gatt_buf.buf; + struct btp_gatt_service *service; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t opcode = (uint8_t) (int) arg; @@ -1120,22 +1120,22 @@ disc_all_desc_cb(uint16_t conn_handle, const struct ble_gatt_dsc *gatt_dsc, void *arg) { - struct gatt_disc_all_desc_rp *rp = (void *) gatt_buf.buf; - struct gatt_descriptor *dsc; - const ble_uuid_any_t *uuid; + struct btp_gatt_disc_all_desc_rp *rp = (void *) gatt_buf.buf; + struct btp_gatt_descriptor *dsc; + const ble_uuid_any_t *uuid; uint8_t uuid_length; SYS_LOG_DBG(""); if (error->status != 0 && error->status != BLE_HS_EDONE) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, CONTROLLER_INDEX, BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; @@ -1146,7 +1146,7 @@ disc_all_desc_cb(uint16_t conn_handle, dsc = gatt_buf_reserve(sizeof(*dsc) + uuid_length); if (!dsc) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, CONTROLLER_INDEX, BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; @@ -1180,12 +1180,12 @@ disc_all_prim_svcs(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_disc_all_prim_svcs_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_all_prim_svcs_rp))) { goto fail; } if (ble_gattc_disc_all_svcs(conn.conn_handle, disc_prim_uuid_cb, - (void *) GATT_DISC_ALL_PRIM_SVCS)) { + (void *) BTP_GATT_DISC_ALL_PRIM_SVCS)) { discover_destroy(); goto fail; } @@ -1193,15 +1193,15 @@ disc_all_prim_svcs(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_PRIM_SVCS, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_PRIM_SVCS, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void disc_all_desc(uint8_t *data, uint16_t len) { - const struct gatt_disc_all_desc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_disc_all_desc_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int rc; @@ -1212,7 +1212,7 @@ disc_all_desc(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_disc_all_desc_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_all_desc_rp))) { goto fail; } @@ -1232,7 +1232,7 @@ disc_all_desc(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1241,23 +1241,23 @@ find_included_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *gatt_svc, void *arg) { - struct gatt_find_included_rp *rp = (void *) gatt_buf.buf; - struct gatt_included *included; - const ble_uuid_any_t *uuid; + struct btp_gatt_find_included_rp *rp = (void *) gatt_buf.buf; + struct btp_gatt_included *included; + const ble_uuid_any_t *uuid; int service_handle = (int) arg; uint8_t uuid_length; SYS_LOG_DBG(""); if (error->status != 0 && error->status != BLE_HS_EDONE) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, CONTROLLER_INDEX, BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; @@ -1268,7 +1268,7 @@ find_included_cb(uint16_t conn_handle, included = gatt_buf_reserve(sizeof(*included) + uuid_length); if (!included) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, CONTROLLER_INDEX, BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; @@ -1299,9 +1299,9 @@ disc_chrc_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *gatt_chr, void *arg) { - struct gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; - struct gatt_characteristic *chrc; - const ble_uuid_any_t *uuid; + struct btp_gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; + struct btp_gatt_characteristic *chrc; + const ble_uuid_any_t *uuid; uint8_t btp_opcode = (uint8_t) (int) arg; uint8_t uuid_length; @@ -1353,8 +1353,8 @@ disc_chrc_cb(uint16_t conn_handle, static void disc_chrc_uuid(uint8_t *data, uint16_t len) { - const struct gatt_disc_chrc_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_disc_chrc_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; int rc; @@ -1370,7 +1370,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_disc_chrc_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_chrc_rp))) { goto fail; } @@ -1379,7 +1379,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) if (ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, - (void *) GATT_DISC_CHRC_UUID)) { + (void *) BTP_GATT_DISC_CHRC_UUID)) { discover_destroy(); goto fail; } @@ -1387,15 +1387,15 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_CHRC_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_CHRC_UUID, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void disc_prim_uuid(uint8_t *data, uint16_t len) { - const struct gatt_disc_prim_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_disc_prim_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; @@ -1410,13 +1410,13 @@ disc_prim_uuid(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_disc_prim_uuid_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_prim_uuid_rp))) { goto fail; } if (ble_gattc_disc_svc_by_uuid(conn.conn_handle, &uuid.u, disc_prim_uuid_cb, - (void *) GATT_DISC_PRIM_UUID)) { + (void *) BTP_GATT_DISC_PRIM_UUID)) { discover_destroy(); goto fail; } @@ -1424,15 +1424,15 @@ disc_prim_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_PRIM_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_PRIM_UUID, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void disc_all_chrc(uint8_t *data, uint16_t len) { - const struct gatt_disc_all_chrc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_disc_all_chrc_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int rc; @@ -1444,7 +1444,7 @@ disc_all_chrc(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_disc_chrc_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_chrc_rp))) { SYS_LOG_DBG("Buf reserve failed"); goto fail; } @@ -1453,7 +1453,7 @@ disc_all_chrc(uint8_t *data, uint16_t len) end_handle = sys_le16_to_cpu(cmd->end_handle); rc = ble_gattc_disc_all_chrs(conn.conn_handle, start_handle, end_handle, - disc_chrc_cb, (void *) GATT_DISC_ALL_CHRC); + disc_chrc_cb, (void *) BTP_GATT_DISC_ALL_CHRC); if (rc) { discover_destroy(); goto fail; @@ -1462,15 +1462,15 @@ disc_all_chrc(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_CHRC, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_CHRC, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void find_included(uint8_t *data, uint16_t len) { - const struct gatt_find_included_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_find_included_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; int rc; @@ -1482,7 +1482,7 @@ find_included(uint8_t *data, uint16_t len) goto fail; } - if (!gatt_buf_reserve(sizeof(struct gatt_find_included_rp))) { + if (!gatt_buf_reserve(sizeof(struct btp_gatt_find_included_rp))) { goto fail; } @@ -1500,7 +1500,7 @@ find_included(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1512,13 +1512,13 @@ exchange_func(uint16_t conn_handle, SYS_LOG_DBG(""); if (error->status) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, CONTROLLER_INDEX, BTP_STATUS_FAILED); return 0; } - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return 0; @@ -1543,7 +1543,7 @@ exchange_mtu(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, CONTROLLER_INDEX, BTP_STATUS_FAILED); } @@ -1555,7 +1555,7 @@ enable_subscription(uint16_t conn_handle, uint16_t ccc_handle, SYS_LOG_DBG(""); - op = (uint8_t) (value == 0x0001 ? GATT_CFG_NOTIFY : GATT_CFG_INDICATE); + op = (uint8_t) (value == 0x0001 ? BTP_GATT_CFG_NOTIFY : BTP_GATT_CFG_INDICATE); if (ble_gattc_write_flat(conn_handle, ccc_handle, &value, sizeof(value), NULL, NULL)) { @@ -1594,8 +1594,8 @@ disable_subscription(uint16_t conn_handle, uint16_t ccc_handle) static void config_subscription(uint8_t *data, uint16_t len, uint8_t op) { - const struct gatt_cfg_notify_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_cfg_notify_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); uint8_t status; int rc; @@ -1612,7 +1612,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) if (cmd->enable) { uint16_t value; - if (op == GATT_CFG_NOTIFY) { + if (op == BTP_GATT_CFG_NOTIFY) { value = 0x0001; } else { value = 0x0002; @@ -1676,10 +1676,10 @@ flags_hs2btp(uint8_t flags) static void get_attrs(uint8_t *data, uint16_t len) { - const struct gatt_get_attributes_cmd *cmd = (void *) data; - struct gatt_get_attributes_rp *rp; - struct gatt_attr *gatt_attr; - struct os_mbuf *buf = os_msys_get(0, 0); + const struct btp_gatt_get_attributes_cmd *cmd = (void *) data; + struct btp_gatt_get_attributes_rp *rp; + struct btp_gatt_attr *gatt_attr; + struct os_mbuf *buf = os_msys_get(0, 0); uint16_t start_handle, end_handle; struct ble_att_svr_entry *entry = NULL; ble_uuid_any_t uuid; @@ -1742,12 +1742,12 @@ get_attrs(uint8_t *data, uint16_t len) rp->attrs_count = count; - tester_send_buf(BTP_SERVICE_ID_GATT, GATT_GET_ATTRIBUTES, + tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, CONTROLLER_INDEX, buf); goto free; fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_GET_ATTRIBUTES, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, CONTROLLER_INDEX, BTP_STATUS_FAILED); free: os_mbuf_free_chain(buf); @@ -1756,9 +1756,9 @@ get_attrs(uint8_t *data, uint16_t len) static void get_attr_val(uint8_t *data, uint16_t len) { - const struct gatt_get_attribute_value_cmd *cmd = (void *) data; - struct gatt_get_attribute_value_rp *rp; - struct ble_gap_conn_desc conn; + const struct btp_gatt_get_attribute_value_cmd *cmd = (void *) data; + struct btp_gatt_get_attribute_value_rp *rp; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); uint16_t handle = sys_cpu_to_le16(cmd->handle); uint8_t out_att_err = 0; @@ -1777,7 +1777,7 @@ get_attr_val(uint8_t *data, uint16_t len) rp->att_response = out_att_err; rp->value_length = os_mbuf_len(buf) - sizeof(*rp); - tester_send_buf(BTP_SERVICE_ID_GATT, GATT_GET_ATTRIBUTE_VALUE, + tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTE_VALUE, CONTROLLER_INDEX, buf); goto free; @@ -1792,7 +1792,7 @@ get_attr_val(uint8_t *data, uint16_t len) rp->att_response = out_att_err; rp->value_length = os_mbuf_len(buf) - sizeof(*rp); - tester_send_buf(BTP_SERVICE_ID_GATT, GATT_GET_ATTRIBUTE_VALUE, + tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTE_VALUE, CONTROLLER_INDEX, buf); goto free; @@ -1805,7 +1805,7 @@ get_attr_val(uint8_t *data, uint16_t len) static void change_database(uint8_t *data, uint16_t len) { - const struct gatt_change_database *cmd = (void *) data; + const struct btp_gatt_change_database *cmd = (void *) data; SYS_LOG_DBG("") @@ -1813,7 +1813,7 @@ change_database(uint8_t *data, uint16_t len) ble_svc_gatt_changed(cmd->start_handle, cmd->end_handle); - tester_rsp(BTP_SERVICE_ID_GATT, GATT_CHANGE_DATABASE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_CHANGE_DATABASE, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return; @@ -1822,38 +1822,38 @@ change_database(uint8_t *data, uint16_t len) static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[4]; - struct gatt_read_supported_commands_rp *rp = (void *) cmds; + uint8_t cmds[4]; + struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; SYS_LOG_DBG(""); memset(cmds, 0, sizeof(cmds)); - tester_set_bit(cmds, GATT_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, GATT_START_SERVER); - tester_set_bit(cmds, GATT_EXCHANGE_MTU); - tester_set_bit(cmds, GATT_DISC_ALL_PRIM_SVCS); - tester_set_bit(cmds, GATT_DISC_PRIM_UUID); - tester_set_bit(cmds, GATT_FIND_INCLUDED); - tester_set_bit(cmds, GATT_DISC_ALL_CHRC); - tester_set_bit(cmds, GATT_DISC_CHRC_UUID); - tester_set_bit(cmds, GATT_DISC_ALL_DESC); - tester_set_bit(cmds, GATT_READ); - tester_set_bit(cmds, GATT_READ_LONG); - tester_set_bit(cmds, GATT_READ_MULTIPLE); - tester_set_bit(cmds, GATT_WRITE_WITHOUT_RSP); + tester_set_bit(cmds, BTP_GATT_READ_SUPPORTED_COMMANDS); + tester_set_bit(cmds, BTP_GATT_START_SERVER); + tester_set_bit(cmds, BTP_GATT_EXCHANGE_MTU); + tester_set_bit(cmds, BTP_GATT_DISC_ALL_PRIM_SVCS); + tester_set_bit(cmds, BTP_GATT_DISC_PRIM_UUID); + tester_set_bit(cmds, BTP_GATT_FIND_INCLUDED); + tester_set_bit(cmds, BTP_GATT_DISC_ALL_CHRC); + tester_set_bit(cmds, BTP_GATT_DISC_CHRC_UUID); + tester_set_bit(cmds, BTP_GATT_DISC_ALL_DESC); + tester_set_bit(cmds, BTP_GATT_READ); + tester_set_bit(cmds, BTP_GATT_READ_LONG); + tester_set_bit(cmds, BTP_GATT_READ_MULTIPLE); + tester_set_bit(cmds, BTP_GATT_WRITE_WITHOUT_RSP); #if 0 - tester_set_bit(cmds, GATT_SIGNED_WRITE_WITHOUT_RSP); + tester_set_bit(cmds, BTP_GATT_SIGNED_WRITE_WITHOUT_RSP); #endif - tester_set_bit(cmds, GATT_WRITE); - tester_set_bit(cmds, GATT_WRITE_LONG); - tester_set_bit(cmds, GATT_CFG_NOTIFY); - tester_set_bit(cmds, GATT_CFG_INDICATE); - tester_set_bit(cmds, GATT_GET_ATTRIBUTES); - tester_set_bit(cmds, GATT_GET_ATTRIBUTE_VALUE); - tester_set_bit(cmds, GATT_CHANGE_DATABASE); - - tester_send(BTP_SERVICE_ID_GATT, GATT_READ_SUPPORTED_COMMANDS, + tester_set_bit(cmds, BTP_GATT_WRITE); + tester_set_bit(cmds, BTP_GATT_WRITE_LONG); + tester_set_bit(cmds, BTP_GATT_CFG_NOTIFY); + tester_set_bit(cmds, BTP_GATT_CFG_INDICATE); + tester_set_bit(cmds, BTP_GATT_GET_ATTRIBUTES); + tester_set_bit(cmds, BTP_GATT_GET_ATTRIBUTE_VALUE); + tester_set_bit(cmds, BTP_GATT_CHANGE_DATABASE); + + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_READ_SUPPORTED_COMMANDS, CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); } @@ -1868,76 +1868,76 @@ tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) { switch (opcode) { - case GATT_READ_SUPPORTED_COMMANDS: + case BTP_GATT_READ_SUPPORTED_COMMANDS: supported_commands(data, len); return; - case GATT_START_SERVER: + case BTP_GATT_START_SERVER: start_server(data, len); return; - case GATT_EXCHANGE_MTU: + case BTP_GATT_EXCHANGE_MTU: exchange_mtu(data, len); return; - case GATT_DISC_ALL_PRIM_SVCS: + case BTP_GATT_DISC_ALL_PRIM_SVCS: disc_all_prim_svcs(data, len); return; - case GATT_DISC_PRIM_UUID: + case BTP_GATT_DISC_PRIM_UUID: disc_prim_uuid(data, len); return; - case GATT_FIND_INCLUDED: + case BTP_GATT_FIND_INCLUDED: find_included(data, len); return; - case GATT_DISC_ALL_CHRC: + case BTP_GATT_DISC_ALL_CHRC: disc_all_chrc(data, len); return; - case GATT_DISC_CHRC_UUID: + case BTP_GATT_DISC_CHRC_UUID: disc_chrc_uuid(data, len); return; - case GATT_DISC_ALL_DESC: + case BTP_GATT_DISC_ALL_DESC: disc_all_desc(data, len); return; - case GATT_CHANGE_DATABASE: + case BTP_GATT_CHANGE_DATABASE: change_database(data, len); return; - case GATT_READ: + case BTP_GATT_READ: read(data, len); return; - case GATT_READ_UUID: + case BTP_GATT_READ_UUID: read_uuid(data, len); return; - case GATT_READ_LONG: + case BTP_GATT_READ_LONG: read_long(data, len); return; - case GATT_READ_MULTIPLE: + case BTP_GATT_READ_MULTIPLE: read_multiple(data, len); return; - case GATT_WRITE_WITHOUT_RSP: + case BTP_GATT_WRITE_WITHOUT_RSP: write_without_rsp(data, len, opcode, false); return; #if 0 - case GATT_SIGNED_WRITE_WITHOUT_RSP: + case BTP_GATT_SIGNED_WRITE_WITHOUT_RSP: write_without_rsp(data, len, opcode, true); return; #endif - case GATT_WRITE: + case BTP_GATT_WRITE: write(data, len); return; - case GATT_WRITE_LONG: + case BTP_GATT_WRITE_LONG: write_long(data, len); return; - case GATT_RELIABLE_WRITE: + case BTP_GATT_RELIABLE_WRITE: reliable_write(data, len); return; - case GATT_CFG_NOTIFY: - case GATT_CFG_INDICATE: + case BTP_GATT_CFG_NOTIFY: + case BTP_GATT_CFG_INDICATE: config_subscription(data, len, opcode); return; - case GATT_GET_ATTRIBUTES: + case BTP_GATT_GET_ATTRIBUTES: get_attrs(data, len); return; - case GATT_GET_ATTRIBUTE_VALUE: + case BTP_GATT_GET_ATTRIBUTE_VALUE: get_attr_val(data, len); return; default: @@ -1951,8 +1951,8 @@ int tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, uint8_t indication, struct os_mbuf *om) { - struct gatt_notification_ev *ev; - struct ble_gap_conn_desc conn; + struct btp_gatt_notification_ev *ev; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1978,7 +1978,7 @@ tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, ev->data_length = sys_cpu_to_le16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); - tester_send_buf(BTP_SERVICE_ID_GATT, GATT_EV_NOTIFICATION, + tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_NOTIFICATION, CONTROLLER_INDEX, buf); fail: diff --git a/apps/bttester/src/gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c similarity index 82% rename from apps/bttester/src/gatt_cl.c rename to apps/bttester/src/btp_gatt_cl.c index af08764292..af33f17ee4 100644 --- a/apps/bttester/src/gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -28,7 +28,7 @@ #include "../../../nimble/host/src/ble_att_priv.h" #include "../../../nimble/host/src/ble_gatt_priv.h" -#include "bttester.h" +#include "btp/btp.h" #define CONTROLLER_INDEX 0 #define MAX_BUFFER_SIZE 2048 @@ -123,8 +123,8 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, const struct ble_gatt_error *error, uint16_t mtu, void *arg) { - struct gattc_exchange_mtu_ev *ev; - struct ble_gap_conn_desc conn; + struct btp_gattc_exchange_mtu_ev *ev; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -144,7 +144,7 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, ev->mtu = mtu; - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_EV_MTU_EXCHANGED, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_MTU_EXCHANGED, CONTROLLER_INDEX, buf); fail: os_mbuf_free_chain(buf); @@ -174,7 +174,7 @@ exchange_mtu(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_EXCHANGE_MTU, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_EXCHANGE_MTU, CONTROLLER_INDEX, status); } @@ -183,10 +183,10 @@ disc_prim_svcs_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *gatt_svc, void *arg) { - struct gattc_disc_prim_svcs_rp *rp; - struct ble_gap_conn_desc conn; - struct gatt_service *service; - const ble_uuid_any_t *uuid; + struct btp_gattc_disc_prim_svcs_rp *rp; + struct ble_gap_conn_desc conn; + struct btp_gatt_service *service; + const ble_uuid_any_t *uuid; const ble_addr_t *addr; uint8_t uuid_length; struct os_mbuf *buf = os_msys_get(0, 0); @@ -272,22 +272,22 @@ disc_all_prim_svcs(uint8_t *data, uint16_t len) } if (ble_gattc_disc_all_svcs(conn.conn_handle, disc_prim_svcs_cb, - (void *) GATTC_DISC_ALL_PRIM_RP)) { + (void *) BTP_GATTC_DISC_ALL_PRIM_RP)) { discover_destroy(); status = BTP_STATUS_FAILED; goto rsp; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_DISC_ALL_PRIM_SVCS, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_PRIM_SVCS, CONTROLLER_INDEX, status); } static void disc_prim_uuid(uint8_t *data, uint16_t len) { - const struct gattc_disc_prim_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_disc_prim_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -307,14 +307,14 @@ disc_prim_uuid(uint8_t *data, uint16_t len) if (ble_gattc_disc_svc_by_uuid(conn.conn_handle, &uuid.u, disc_prim_svcs_cb, - (void *) GATTC_DISC_PRIM_UUID_RP)) { + (void *) BTP_GATTC_DISC_PRIM_UUID_RP)) { discover_destroy(); status = BTP_STATUS_FAILED; goto rsp; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_DISC_PRIM_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_PRIM_UUID, CONTROLLER_INDEX, status); } @@ -323,9 +323,9 @@ find_included_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *gatt_svc, void *arg) { - struct gattc_find_included_rp *rp; - struct gatt_included *included; - const ble_uuid_any_t *uuid; + struct btp_gattc_find_included_rp *rp; + struct btp_gatt_included *included; + const ble_uuid_any_t *uuid; int service_handle = (int) arg; uint8_t uuid_length; uint8_t err = (uint8_t) error->status; @@ -354,7 +354,7 @@ find_included_cb(uint16_t conn_handle, SYS_LOG_DBG(""); if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->services_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_FIND_INCLUDED_RP, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, CONTROLLER_INDEX, buf); discover_destroy(); goto free; @@ -364,7 +364,7 @@ find_included_cb(uint16_t conn_handle, rp->status = 0; rp->services_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_FIND_INCLUDED_RP, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, CONTROLLER_INDEX, buf); discover_destroy(); goto free; @@ -404,8 +404,8 @@ find_included_cb(uint16_t conn_handle, static void find_included(uint8_t *data, uint16_t len) { - const struct gattc_find_included_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_find_included_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; uint8_t status = BTP_STATUS_SUCCESS; @@ -432,7 +432,7 @@ find_included(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_FIND_INCLUDED, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED, CONTROLLER_INDEX, status); } @@ -441,9 +441,9 @@ disc_chrc_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *gatt_chr, void *arg) { - struct gattc_disc_chrc_rp *rp; - struct gatt_characteristic *chrc; - const ble_uuid_any_t *uuid; + struct btp_gattc_disc_chrc_rp *rp; + struct btp_gatt_characteristic *chrc; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; @@ -522,8 +522,8 @@ disc_chrc_cb(uint16_t conn_handle, static void disc_all_chrc(uint8_t *data, uint16_t len) { - const struct gattc_disc_all_chrc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_disc_all_chrc_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -544,7 +544,7 @@ disc_all_chrc(uint8_t *data, uint16_t len) start_handle, end_handle, disc_chrc_cb, - (void *) GATTC_DISC_ALL_CHRC_RP); + (void *) BTP_GATTC_DISC_ALL_CHRC_RP); if (rc) { discover_destroy(); status = BTP_STATUS_FAILED; @@ -552,15 +552,15 @@ disc_all_chrc(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_DISC_ALL_CHRC, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_CHRC, CONTROLLER_INDEX, status); } static void disc_chrc_uuid(uint8_t *data, uint16_t len) { - const struct gattc_disc_chrc_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_disc_chrc_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; @@ -584,7 +584,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) rc = ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, - (void *) GATTC_DISC_CHRC_UUID_RP); + (void *) BTP_GATTC_DISC_CHRC_UUID_RP); if (rc) { discover_destroy(); status = BTP_STATUS_FAILED; @@ -592,7 +592,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_DISC_CHRC_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_CHRC_UUID, CONTROLLER_INDEX, status); } @@ -603,9 +603,9 @@ disc_all_desc_cb(uint16_t conn_handle, const struct ble_gatt_dsc *gatt_dsc, void *arg) { - struct gattc_disc_all_desc_rp *rp; - struct gatt_descriptor *dsc; - const ble_uuid_any_t *uuid; + struct btp_gattc_disc_all_desc_rp *rp; + struct btp_gatt_descriptor *dsc; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); @@ -632,7 +632,7 @@ disc_all_desc_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->descriptors_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_DISC_ALL_DESC_RP, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, CONTROLLER_INDEX, buf); discover_destroy(); goto free; @@ -642,7 +642,7 @@ disc_all_desc_cb(uint16_t conn_handle, rp->status = 0; rp->descriptors_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_DISC_ALL_DESC_RP, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, CONTROLLER_INDEX, buf); discover_destroy(); goto free; @@ -678,8 +678,8 @@ disc_all_desc_cb(uint16_t conn_handle, static void disc_all_desc(uint8_t *data, uint16_t len) { - const struct gattc_disc_all_desc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_disc_all_desc_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -699,7 +699,7 @@ disc_all_desc(uint8_t *data, uint16_t len) start_handle, end_handle, disc_all_desc_cb, - (void *) GATTC_DISC_ALL_DESC); + (void *) BTP_GATTC_DISC_ALL_DESC); SYS_LOG_DBG("rc=%d", rc); @@ -710,7 +710,7 @@ disc_all_desc(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_DISC_ALL_DESC, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC, CONTROLLER_INDEX, status); } @@ -720,8 +720,8 @@ read_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct gattc_read_rp *rp; - uint8_t opcode = (uint8_t) (int) arg; + struct btp_gattc_read_rp *rp; + uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; struct ble_gap_conn_desc conn; @@ -771,8 +771,8 @@ read_cb(uint16_t conn_handle, static void read(uint8_t *data, uint16_t len) { - const struct gattc_read_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_read_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -788,14 +788,14 @@ read(uint8_t *data, uint16_t len) read_destroy(); if (ble_gattc_read(conn.conn_handle, sys_le16_to_cpu(cmd->handle), - read_cb, (void *) GATTC_READ_RP)) { + read_cb, (void *) BTP_GATTC_READ_RP)) { read_destroy(); status = BTP_STATUS_FAILED; goto rsp; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_READ, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ, CONTROLLER_INDEX, status); } @@ -805,9 +805,9 @@ read_uuid_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct gattc_read_uuid_rp *rp; - struct gatt_read_uuid_chr *chr; - uint8_t opcode = (uint8_t) (int) arg; + struct btp_gattc_read_uuid_rp *rp; + struct btp_gatt_read_uuid_chr *chr; + uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -874,8 +874,8 @@ read_uuid_cb(uint16_t conn_handle, static void read_uuid(uint8_t *data, uint16_t len) { - const struct gattc_read_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_read_uuid_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -899,13 +899,13 @@ read_uuid(uint8_t *data, uint16_t len) if (ble_gattc_read_by_uuid(conn.conn_handle, sys_le16_to_cpu(cmd->start_handle), sys_le16_to_cpu(cmd->end_handle), &uuid.u, - read_uuid_cb, (void *) GATTC_READ_UUID_RP)) { + read_uuid_cb, (void *) BTP_GATTC_READ_UUID_RP)) { read_destroy(); status = BTP_STATUS_FAILED; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_READ_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_UUID, CONTROLLER_INDEX, status); } @@ -915,7 +915,7 @@ read_long_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct gattc_read_rp *rp;; + struct btp_gattc_read_rp *rp;; uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); @@ -974,8 +974,8 @@ read_long_cb(uint16_t conn_handle, static void read_long(uint8_t *data, uint16_t len) { - const struct gattc_read_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_read_long_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -993,21 +993,21 @@ read_long(uint8_t *data, uint16_t len) if (ble_gattc_read_long(conn.conn_handle, sys_le16_to_cpu(cmd->handle), sys_le16_to_cpu(cmd->offset), - read_long_cb, (void *) GATTC_READ_LONG_RP)) { + read_long_cb, (void *) BTP_GATTC_READ_LONG_RP)) { read_destroy(); status = BTP_STATUS_FAILED; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_READ_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_LONG, CONTROLLER_INDEX, status); } static void read_multiple(uint8_t *data, uint16_t len) { - const struct gattc_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + const struct btp_gattc_read_multiple_cmd *cmd = (void *) data; + uint16_t handles[cmd->handles_count]; struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc, i; @@ -1031,21 +1031,21 @@ read_multiple(uint8_t *data, uint16_t len) handles, cmd->handles_count, read_cb, - (void *) GATTC_READ_MULTIPLE_RP)) { + (void *) BTP_GATTC_READ_MULTIPLE_RP)) { read_destroy(); status = BTP_STATUS_FAILED; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_READ_MULTIPLE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_MULTIPLE, CONTROLLER_INDEX, status); } static void write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) { - const struct gattc_write_without_rsp_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_write_without_rsp_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1072,8 +1072,8 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { - struct gattc_write_rp *rp; - uint8_t err = (uint8_t) error->status; + struct btp_gattc_write_rp *rp; + uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1106,8 +1106,8 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, static void write(uint8_t *data, uint16_t len) { - const struct gattc_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_write_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1121,20 +1121,20 @@ write(uint8_t *data, uint16_t len) if (ble_gattc_write_flat(conn.conn_handle, sys_le16_to_cpu(cmd->handle), cmd->data, sys_le16_to_cpu(cmd->data_length), - write_cb, (void *) GATTC_WRITE_RP)) { + write_cb, (void *) BTP_GATTC_WRITE_RP)) { status = BTP_STATUS_FAILED; } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_WRITE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE, CONTROLLER_INDEX, status); } static void write_long(uint8_t *data, uint16_t len) { - const struct gattc_write_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_write_long_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; uint8_t status = BTP_STATUS_SUCCESS; int rc = 0; @@ -1157,7 +1157,7 @@ write_long(uint8_t *data, uint16_t len) sys_le16_to_cpu(cmd->handle), sys_le16_to_cpu(cmd->offset), om, write_cb, - (void *) GATTC_WRITE_LONG_RP); + (void *) BTP_GATTC_WRITE_LONG_RP); if (rc) { status = BTP_STATUS_FAILED; } else { @@ -1168,7 +1168,7 @@ write_long(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_WRITE_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, CONTROLLER_INDEX, status); } @@ -1179,8 +1179,8 @@ reliable_write_cb(uint16_t conn_handle, uint8_t num_attrs, void *arg) { - struct gattc_write_rp *rp; - uint8_t err = (uint8_t) error->status; + struct btp_gattc_write_rp *rp; + uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; struct ble_gap_conn_desc conn; @@ -1202,7 +1202,7 @@ reliable_write_cb(uint16_t conn_handle, memcpy(rp->address, addr->val, sizeof(rp->address)); rp->status = err; - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_RELIABLE_WRITE_RP, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_RELIABLE_WRITE_RP, CONTROLLER_INDEX, buf); free: os_mbuf_free_chain(buf); @@ -1212,8 +1212,8 @@ reliable_write_cb(uint16_t conn_handle, static void reliable_write(uint8_t *data, uint16_t len) { - const struct gattc_reliable_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_reliable_write_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; uint8_t status = BTP_STATUS_SUCCESS; @@ -1251,7 +1251,7 @@ reliable_write(uint8_t *data, uint16_t len) fail: os_mbuf_free_chain(om); rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, GATTC_WRITE_LONG, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, CONTROLLER_INDEX, status); } @@ -1261,8 +1261,8 @@ subscribe_cb(uint16_t conn_handle, struct ble_gatt_attr *attrs, void *arg) { - struct subscribe_rp *rp; - uint8_t err = (uint8_t) error->status; + struct btp_subscribe_rp *rp; + uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1300,8 +1300,8 @@ enable_subscription(uint16_t conn_handle, uint16_t ccc_handle, SYS_LOG_DBG(""); - opcode = (uint32_t) (value == 0x0001 ? GATTC_CFG_NOTIFY_RP - : GATTC_CFG_INDICATE_RP); + opcode = (uint32_t) (value == 0x0001 ? BTP_GATTC_CFG_NOTIFY_RP + : BTP_GATTC_CFG_INDICATE_RP); if (ble_gattc_write_flat(conn_handle, ccc_handle, @@ -1325,8 +1325,8 @@ disable_subscription(uint16_t conn_handle, uint16_t ccc_handle) SYS_LOG_DBG(""); - opcode = (uint32_t) (value == 0x0001 ? GATTC_CFG_NOTIFY_RP - : GATTC_CFG_INDICATE_RP); + opcode = (uint32_t) (value == 0x0001 ? BTP_GATTC_CFG_NOTIFY_RP + : BTP_GATTC_CFG_INDICATE_RP); /* Fail if CCC handle doesn't match */ if (ccc_handle != subscribe_params.ccc_handle) { @@ -1350,8 +1350,8 @@ disable_subscription(uint16_t conn_handle, uint16_t ccc_handle) static void config_subscription(uint8_t *data, uint16_t len, uint8_t op) { - const struct gattc_cfg_notify_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gattc_cfg_notify_cmd *cmd = (void *) data; + struct ble_gap_conn_desc conn; uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1367,7 +1367,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) if (cmd->enable) { uint16_t value; - if (op == GATTC_CFG_NOTIFY) { + if (op == BTP_GATTC_CFG_NOTIFY) { value = 0x0001; } else { value = 0x0002; @@ -1396,8 +1396,8 @@ int tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, uint8_t indication, struct os_mbuf *om) { - struct gattc_notification_ev *ev; - struct ble_gap_conn_desc conn; + struct btp_gattc_notification_ev *ev; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1423,7 +1423,7 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, ev->data_length = sys_cpu_to_le16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); - tester_send_buf(BTP_SERVICE_ID_GATTC, GATTC_EV_NOTIFICATION_RXED, + tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_NOTIFICATION_RXED, CONTROLLER_INDEX, buf); fail: @@ -1434,36 +1434,36 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[3]; - struct gatt_read_supported_commands_rp *rp = (void *) cmds; + uint8_t cmds[3]; + struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; SYS_LOG_DBG(""); memset(cmds, 0, sizeof(cmds)); - tester_set_bit(cmds, GATTC_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, GATTC_EXCHANGE_MTU); - tester_set_bit(cmds, GATTC_DISC_ALL_PRIM_SVCS); - tester_set_bit(cmds, GATTC_DISC_PRIM_UUID); - tester_set_bit(cmds, GATTC_FIND_INCLUDED); - tester_set_bit(cmds, GATTC_DISC_ALL_CHRC); - tester_set_bit(cmds, GATTC_DISC_CHRC_UUID); - tester_set_bit(cmds, GATTC_DISC_ALL_DESC); - tester_set_bit(cmds, GATTC_READ); - tester_set_bit(cmds, GATTC_READ_UUID); - tester_set_bit(cmds, GATTC_READ_LONG); - tester_set_bit(cmds, GATTC_READ_MULTIPLE); - tester_set_bit(cmds, GATTC_WRITE_WITHOUT_RSP); + tester_set_bit(cmds, BTP_GATTC_READ_SUPPORTED_COMMANDS); + tester_set_bit(cmds, BTP_GATTC_EXCHANGE_MTU); + tester_set_bit(cmds, BTP_GATTC_DISC_ALL_PRIM_SVCS); + tester_set_bit(cmds, BTP_GATTC_DISC_PRIM_UUID); + tester_set_bit(cmds, BTP_GATTC_FIND_INCLUDED); + tester_set_bit(cmds, BTP_GATTC_DISC_ALL_CHRC); + tester_set_bit(cmds, BTP_GATTC_DISC_CHRC_UUID); + tester_set_bit(cmds, BTP_GATTC_DISC_ALL_DESC); + tester_set_bit(cmds, BTP_GATTC_READ); + tester_set_bit(cmds, BTP_GATTC_READ_UUID); + tester_set_bit(cmds, BTP_GATTC_READ_LONG); + tester_set_bit(cmds, BTP_GATTC_READ_MULTIPLE); + tester_set_bit(cmds, BTP_GATTC_WRITE_WITHOUT_RSP); #if 0 - tester_set_bit(cmds, GATTC_SIGNED_WRITE_WITHOUT_RSP); + tester_set_bit(cmds, BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP); #endif - tester_set_bit(cmds, GATTC_WRITE); - tester_set_bit(cmds, GATTC_WRITE_LONG); - tester_set_bit(cmds, GATTC_RELIABLE_WRITE); - tester_set_bit(cmds, GATTC_CFG_NOTIFY); - tester_set_bit(cmds, GATTC_CFG_INDICATE); + tester_set_bit(cmds, BTP_GATTC_WRITE); + tester_set_bit(cmds, BTP_GATTC_WRITE_LONG); + tester_set_bit(cmds, BTP_GATTC_RELIABLE_WRITE); + tester_set_bit(cmds, BTP_GATTC_CFG_NOTIFY); + tester_set_bit(cmds, BTP_GATTC_CFG_INDICATE); - tester_send(BTP_SERVICE_ID_GATTC, GATTC_READ_SUPPORTED_COMMANDS, + tester_send(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_SUPPORTED_COMMANDS, CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); } @@ -1472,64 +1472,64 @@ tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) { switch (opcode) { - case GATTC_READ_SUPPORTED_COMMANDS: + case BTP_GATTC_READ_SUPPORTED_COMMANDS: supported_commands(data, len); return; - case GATTC_EXCHANGE_MTU: + case BTP_GATTC_EXCHANGE_MTU: exchange_mtu(data, len); return; - case GATTC_DISC_ALL_PRIM_SVCS: + case BTP_GATTC_DISC_ALL_PRIM_SVCS: disc_all_prim_svcs(data, len); return; - case GATTC_DISC_PRIM_UUID: + case BTP_GATTC_DISC_PRIM_UUID: disc_prim_uuid(data, len); return; - case GATTC_FIND_INCLUDED: + case BTP_GATTC_FIND_INCLUDED: find_included(data, len); return; - case GATTC_DISC_ALL_CHRC: + case BTP_GATTC_DISC_ALL_CHRC: disc_all_chrc(data, len); return; - case GATTC_DISC_CHRC_UUID: + case BTP_GATTC_DISC_CHRC_UUID: disc_chrc_uuid(data, len); return; - case GATTC_DISC_ALL_DESC: + case BTP_GATTC_DISC_ALL_DESC: disc_all_desc(data, len); return; - case GATTC_READ: + case BTP_GATTC_READ: read(data, len); return; - case GATTC_READ_UUID: + case BTP_GATTC_READ_UUID: read_uuid(data, len); return; - case GATTC_READ_LONG: + case BTP_GATTC_READ_LONG: read_long(data, len); return; - case GATTC_READ_MULTIPLE: + case BTP_GATTC_READ_MULTIPLE: read_multiple(data, len); return; - case GATTC_WRITE_WITHOUT_RSP: + case BTP_GATTC_WRITE_WITHOUT_RSP: write_without_rsp(data, len, opcode, false); return; #if 0 - case GATTC_SIGNED_WRITE_WITHOUT_RSP: + case BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP: write_without_rsp(data, len, opcode, true); return; #endif - case GATTC_WRITE: + case BTP_GATTC_WRITE: write(data, len); return; - case GATTC_WRITE_LONG: + case BTP_GATTC_WRITE_LONG: write_long(data, len); return; - case GATTC_RELIABLE_WRITE: + case BTP_GATTC_RELIABLE_WRITE: reliable_write(data, len); return; - case GATTC_CFG_NOTIFY: - case GATTC_CFG_INDICATE: + case BTP_GATTC_CFG_NOTIFY: + case BTP_GATTC_CFG_INDICATE: config_subscription(data, len, opcode); return; default: diff --git a/apps/bttester/src/l2cap.c b/apps/bttester/src/btp_l2cap.c similarity index 84% rename from apps/bttester/src/l2cap.c rename to apps/bttester/src/btp_l2cap.c index 768dd0ed0a..93435943a4 100644 --- a/apps/bttester/src/l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -35,7 +35,7 @@ #include "../../../nimble/host/src/ble_l2cap_priv.h" -#include "bttester.h" +#include "btp/btp.h" #define CONTROLLER_INDEX 0 #define CHANNELS MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) @@ -57,7 +57,7 @@ static struct channel { } channels[CHANNELS]; static uint8_t - recv_cb_buf[TESTER_COC_MTU + sizeof(struct l2cap_data_received_ev)]; + recv_cb_buf[TESTER_COC_MTU + sizeof(struct btp_l2cap_data_received_ev)]; static struct channel * get_free_channel(void) @@ -122,8 +122,8 @@ static void recv_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct os_mbuf *buf, void *arg) { - struct l2cap_data_received_ev *ev = (void *) recv_cb_buf; - struct channel *channel = find_channel(chan); + struct btp_l2cap_data_received_ev *ev = (void *) recv_cb_buf; + struct channel *channel = find_channel(chan); assert(channel != NULL); ev->chan_id = channel->chan_id; @@ -135,7 +135,7 @@ recv_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, } os_mbuf_copydata(buf, 0, ev->data_length, ev->data); - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_EV_DATA_RECEIVED, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DATA_RECEIVED, CONTROLLER_INDEX, recv_cb_buf, sizeof(*ev) + ev->data_length); tester_l2cap_coc_recv(chan, buf); @@ -146,10 +146,10 @@ unstalled_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, int status, void *arg) { if (status) { - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_SEND_DATA, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, BTP_STATUS_FAILED); } else { - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_SEND_DATA, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } } @@ -159,8 +159,8 @@ reconfigured_ev(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info, int status) { - struct l2cap_reconfigured_ev ev; - struct channel *channel; + struct btp_l2cap_reconfigured_ev ev; + struct channel *channel; if (status != 0) { return; @@ -175,7 +175,7 @@ reconfigured_ev(uint16_t conn_handle, struct ble_l2cap_chan *chan, ev.our_mtu = chan_info->our_coc_mtu; ev.our_mps = chan_info->our_l2cap_mtu; - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_EV_RECONFIGURED, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_RECONFIGURED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -183,8 +183,8 @@ static void connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info, void *arg) { - struct l2cap_connected_ev ev; - struct ble_gap_conn_desc desc; + struct btp_l2cap_connected_ev ev; + struct ble_gap_conn_desc desc; struct channel *channel = find_channel(chan); if (channel == NULL) { @@ -206,7 +206,7 @@ connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, sizeof(ev.address)); } - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_EV_CONNECTED, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -214,11 +214,11 @@ static void disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info, void *arg) { - struct l2cap_disconnected_ev ev; - struct ble_gap_conn_desc desc; + struct btp_l2cap_disconnected_ev ev; + struct ble_gap_conn_desc desc; struct channel *channel; - memset(&ev, 0, sizeof(struct l2cap_disconnected_ev)); + memset(&ev, 0, sizeof(struct btp_l2cap_disconnected_ev)); channel = find_channel(chan); assert(channel != NULL); @@ -234,7 +234,7 @@ disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, sizeof(ev.address)); } - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_EV_DISCONNECTED, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DISCONNECTED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -406,18 +406,18 @@ tester_l2cap_event(struct ble_l2cap_event *event, void *arg) static void connect(uint8_t *data, uint16_t len) { - const struct l2cap_connect_cmd *cmd = (void *) data; - uint8_t rp_buf[sizeof(struct l2cap_connect_rp) + cmd->num]; - struct l2cap_connect_rp *rp = (void *) rp_buf; - struct ble_gap_conn_desc desc; + const struct btp_l2cap_connect_cmd *cmd = (void *) data; + uint8_t rp_buf[sizeof(struct btp_l2cap_connect_rp) + cmd->num]; + struct btp_l2cap_connect_rp *rp = (void *) rp_buf; + struct ble_gap_conn_desc desc; struct channel *chan; struct os_mbuf *sdu_rx[cmd->num]; ble_addr_t *addr = (void *) data; uint16_t mtu = htole16(cmd->mtu); int rc; int i, j; - bool ecfc = cmd->options & L2CAP_CONNECT_OPT_ECFC; - hold_credit = cmd->options & L2CAP_CONNECT_OPT_HOLD_CREDIT; + bool ecfc = cmd->options & BTP_L2CAP_CONNECT_OPT_ECFC; + hold_credit = cmd->options & BTP_L2CAP_CONNECT_OPT_HOLD_CREDIT; SYS_LOG_DBG("connect: type: %d addr: %s", addr->type, @@ -481,21 +481,21 @@ connect(uint8_t *data, uint16_t len) goto fail; } - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_CONNECT, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, CONTROLLER_INDEX, (uint8_t *) rp, sizeof(rp_buf)); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_CONNECT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void disconnect(const uint8_t *data, uint16_t len) { - const struct l2cap_disconnect_cmd *cmd = (void *) data; - struct channel *chan; + const struct btp_l2cap_disconnect_cmd *cmd = (void *) data; + struct channel *chan; uint8_t status; int err; @@ -513,15 +513,15 @@ disconnect(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_DISCONNECT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_DISCONNECT, CONTROLLER_INDEX, status); } static void send_data(const uint8_t *data, uint16_t len) { - const struct l2cap_send_data_cmd *cmd = (void *) data; - struct os_mbuf *sdu_tx = NULL; + const struct btp_l2cap_send_data_cmd *cmd = (void *) data; + struct os_mbuf *sdu_tx = NULL; int rc; uint16_t data_len = sys_le16_to_cpu(cmd->data_len); struct channel *chan = get_channel(cmd->chan_id); @@ -550,7 +550,7 @@ send_data(const uint8_t *data, uint16_t len) /* ble_l2cap_send takes ownership of the sdu */ rc = ble_l2cap_send(chan->chan, sdu_tx); if (rc == 0 || rc == BLE_HS_ESTALLED) { - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_SEND_DATA, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return; } @@ -559,15 +559,15 @@ send_data(const uint8_t *data, uint16_t len) os_mbuf_free_chain(sdu_tx); fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_SEND_DATA, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void listen(const uint8_t *data, uint16_t len) { - const struct l2cap_listen_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + const struct btp_l2cap_listen_cmd *cmd = (void *) data; + uint16_t mtu = htole16(cmd->mtu); int rc; SYS_LOG_DBG(""); @@ -582,20 +582,20 @@ listen(const uint8_t *data, uint16_t len) goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_LISTEN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_LISTEN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void credits(uint8_t *data, uint16_t len) { - const struct l2cap_credits_cmd *cmd = (void *) data; - struct os_mbuf *sdu; + const struct btp_l2cap_credits_cmd *cmd = (void *) data; + struct os_mbuf *sdu; int rc; struct channel *channel = get_channel(cmd->chan_id); @@ -613,19 +613,19 @@ credits(uint8_t *data, uint16_t len) if (rc != 0) { goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_CREDITS, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_CREDITS, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void reconfigure(const uint8_t *data, uint16_t len) { - const struct l2cap_reconfigure_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + const struct btp_l2cap_reconfigure_cmd *cmd = (void *) data; + uint16_t mtu = htole16(cmd->mtu); struct ble_gap_conn_desc desc; ble_addr_t *addr = (void *) data; struct ble_l2cap_chan *chans[cmd->num]; @@ -658,31 +658,31 @@ reconfigure(const uint8_t *data, uint16_t len) goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_RECONFIGURE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_RECONFIGURE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, CONTROLLER_INDEX, BTP_STATUS_FAILED); } static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[1]; - struct l2cap_read_supported_commands_rp *rp = (void *) cmds; + uint8_t cmds[1]; + struct btp_l2cap_read_supported_commands_rp *rp = (void *) cmds; memset(cmds, 0, sizeof(cmds)); - tester_set_bit(cmds, L2CAP_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, L2CAP_CONNECT); - tester_set_bit(cmds, L2CAP_DISCONNECT); - tester_set_bit(cmds, L2CAP_LISTEN); - tester_set_bit(cmds, L2CAP_SEND_DATA); - tester_set_bit(cmds, L2CAP_RECONFIGURE); + tester_set_bit(cmds, BTP_L2CAP_READ_SUPPORTED_COMMANDS); + tester_set_bit(cmds, BTP_L2CAP_CONNECT); + tester_set_bit(cmds, BTP_L2CAP_DISCONNECT); + tester_set_bit(cmds, BTP_L2CAP_LISTEN); + tester_set_bit(cmds, BTP_L2CAP_SEND_DATA); + tester_set_bit(cmds, BTP_L2CAP_RECONFIGURE); - tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_READ_SUPPORTED_COMMANDS, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_READ_SUPPORTED_COMMANDS, CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); } @@ -691,25 +691,25 @@ tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) { switch (opcode) { - case L2CAP_READ_SUPPORTED_COMMANDS: + case BTP_L2CAP_READ_SUPPORTED_COMMANDS: supported_commands(data, len); return; - case L2CAP_CONNECT: + case BTP_L2CAP_CONNECT: connect(data, len); return; - case L2CAP_DISCONNECT: + case BTP_L2CAP_DISCONNECT: disconnect(data, len); return; - case L2CAP_SEND_DATA: + case BTP_L2CAP_SEND_DATA: send_data(data, len); return; - case L2CAP_LISTEN: + case BTP_L2CAP_LISTEN: listen(data, len); return; - case L2CAP_RECONFIGURE: + case BTP_L2CAP_RECONFIGURE: reconfigure(data, len); return; - case L2CAP_CREDITS: + case BTP_L2CAP_CREDITS: credits(data, len); return; default: diff --git a/apps/bttester/src/mesh.c b/apps/bttester/src/btp_mesh.c similarity index 78% rename from apps/bttester/src/mesh.c rename to apps/bttester/src/btp_mesh.c index 26093f755d..cc36515a4e 100644 --- a/apps/bttester/src/mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -36,7 +36,7 @@ #include "mesh/testing.h" #include "console/console.h" -#include "bttester.h" +#include "btp/btp.h" extern uint8_t own_addr_type; @@ -93,32 +93,32 @@ supported_commands(uint8_t *data, uint16_t len) /* 1st octet */ memset(net_buf_simple_add(buf, 1), 0, 1); - tester_set_bit(buf->om_data, MESH_READ_SUPPORTED_COMMANDS); - tester_set_bit(buf->om_data, MESH_CONFIG_PROVISIONING); - tester_set_bit(buf->om_data, MESH_PROVISION_NODE); - tester_set_bit(buf->om_data, MESH_INIT); - tester_set_bit(buf->om_data, MESH_RESET); - tester_set_bit(buf->om_data, MESH_INPUT_NUMBER); - tester_set_bit(buf->om_data, MESH_INPUT_STRING); + tester_set_bit(buf->om_data, BTP_MESH_READ_SUPPORTED_COMMANDS); + tester_set_bit(buf->om_data, BTP_MESH_CONFIG_PROVISIONING); + tester_set_bit(buf->om_data, BTP_MESH_PROVISION_NODE); + tester_set_bit(buf->om_data, BTP_MESH_INIT); + tester_set_bit(buf->om_data, BTP_MESH_RESET); + tester_set_bit(buf->om_data, BTP_MESH_INPUT_NUMBER); + tester_set_bit(buf->om_data, BTP_MESH_INPUT_STRING); /* 2nd octet */ - tester_set_bit(buf->om_data, MESH_IVU_TEST_MODE); - tester_set_bit(buf->om_data, MESH_IVU_TOGGLE_STATE); - tester_set_bit(buf->om_data, MESH_NET_SEND); - tester_set_bit(buf->om_data, MESH_HEALTH_GENERATE_FAULTS); - tester_set_bit(buf->om_data, MESH_HEALTH_CLEAR_FAULTS); - tester_set_bit(buf->om_data, MESH_LPN); - tester_set_bit(buf->om_data, MESH_LPN_POLL); - tester_set_bit(buf->om_data, MESH_MODEL_SEND); + tester_set_bit(buf->om_data, BTP_MESH_IVU_TEST_MODE); + tester_set_bit(buf->om_data, BTP_MESH_IVU_TOGGLE_STATE); + tester_set_bit(buf->om_data, BTP_MESH_NET_SEND); + tester_set_bit(buf->om_data, BTP_MESH_HEALTH_GENERATE_FAULTS); + tester_set_bit(buf->om_data, BTP_MESH_HEALTH_CLEAR_FAULTS); + tester_set_bit(buf->om_data, BTP_MESH_LPN); + tester_set_bit(buf->om_data, BTP_MESH_LPN_POLL); + tester_set_bit(buf->om_data, BTP_MESH_MODEL_SEND); /* 3rd octet */ memset(net_buf_simple_add(buf, 1), 0, 1); #if MYNEWT_VAL(BLE_MESH_TESTING) - tester_set_bit(buf->om_data, MESH_LPN_SUBSCRIBE); - tester_set_bit(buf->om_data, MESH_LPN_UNSUBSCRIBE); - tester_set_bit(buf->om_data, MESH_RPL_CLEAR); + tester_set_bit(buf->om_data, BTP_MESH_LPN_SUBSCRIBE); + tester_set_bit(buf->om_data, BTP_MESH_LPN_UNSUBSCRIBE); + tester_set_bit(buf->om_data, BTP_MESH_RPL_CLEAR); #endif /* CONFIG_BT_TESTING */ - tester_set_bit(buf->om_data, MESH_PROXY_IDENTITY); + tester_set_bit(buf->om_data, BTP_MESH_PROXY_IDENTITY); - tester_send_buf(BTP_SERVICE_ID_MESH, MESH_READ_SUPPORTED_COMMANDS, + tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_READ_SUPPORTED_COMMANDS, CONTROLLER_INDEX, buf); } @@ -266,16 +266,16 @@ static struct bt_mesh_elem elements[] = { static void link_open(bt_mesh_prov_bearer_t bearer) { - struct mesh_prov_link_open_ev ev; + struct btp_mesh_prov_link_open_ev ev; SYS_LOG_DBG("bearer 0x%02x", bearer); switch (bearer) { case BT_MESH_PROV_ADV: - ev.bearer = MESH_PROV_BEARER_PB_ADV; + ev.bearer = BTP_MESH_PROV_BEARER_PB_ADV; break; case BT_MESH_PROV_GATT: - ev.bearer = MESH_PROV_BEARER_PB_GATT; + ev.bearer = BTP_MESH_PROV_BEARER_PB_GATT; break; default: SYS_LOG_ERR("Invalid bearer"); @@ -283,23 +283,23 @@ link_open(bt_mesh_prov_bearer_t bearer) return; } - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROV_LINK_OPEN, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_OPEN, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void link_close(bt_mesh_prov_bearer_t bearer) { - struct mesh_prov_link_closed_ev ev; + struct btp_mesh_prov_link_closed_ev ev; SYS_LOG_DBG("bearer 0x%02x", bearer); switch (bearer) { case BT_MESH_PROV_ADV: - ev.bearer = MESH_PROV_BEARER_PB_ADV; + ev.bearer = BTP_MESH_PROV_BEARER_PB_ADV; break; case BT_MESH_PROV_GATT: - ev.bearer = MESH_PROV_BEARER_PB_GATT; + ev.bearer = BTP_MESH_PROV_BEARER_PB_GATT; break; default: SYS_LOG_ERR("Invalid bearer"); @@ -307,21 +307,21 @@ link_close(bt_mesh_prov_bearer_t bearer) return; } - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROV_LINK_CLOSED, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_CLOSED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static int output_number(bt_mesh_output_action_t action, uint32_t number) { - struct mesh_out_number_action_ev ev; + struct btp_mesh_out_number_action_ev ev; SYS_LOG_DBG("action 0x%04x number 0x%08lx", action, number); ev.action = sys_cpu_to_le16(action); ev.number = sys_cpu_to_le32(number); - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_OUT_NUMBER_ACTION, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); return 0; @@ -330,8 +330,8 @@ output_number(bt_mesh_output_action_t action, uint32_t number) static int output_string(const char *str) { - struct mesh_out_string_action_ev *ev; - struct os_mbuf *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE); + struct btp_mesh_out_string_action_ev *ev; + struct os_mbuf *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE); SYS_LOG_DBG("str %s", str); @@ -342,7 +342,7 @@ output_string(const char *str) net_buf_simple_add_mem(buf, str, ev->string_len); - tester_send_buf(BTP_SERVICE_ID_MESH, MESH_EV_OUT_STRING_ACTION, + tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_STRING_ACTION, CONTROLLER_INDEX, buf); os_mbuf_free_chain(buf); @@ -352,7 +352,7 @@ output_string(const char *str) static int input(bt_mesh_input_action_t action, uint8_t size) { - struct mesh_in_action_ev ev; + struct btp_mesh_in_action_ev ev; SYS_LOG_DBG("action 0x%04x number 0x%02x", action, size); @@ -361,7 +361,7 @@ input(bt_mesh_input_action_t action, uint8_t size) ev.action = sys_cpu_to_le16(action); ev.size = size; - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_IN_ACTION, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); return 0; @@ -379,7 +379,7 @@ prov_complete(uint16_t net_idx, uint16_t addr) net.local = addr; net.dst = addr; - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROVISIONED, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, CONTROLLER_INDEX, NULL, 0); } @@ -413,7 +413,7 @@ static struct bt_mesh_prov prov = { static void config_prov(uint8_t *data, uint16_t len) { - const struct mesh_config_provisioning_cmd *cmd = (void *) data; + const struct btp_mesh_config_provisioning_cmd *cmd = (void *) data; SYS_LOG_DBG(""); @@ -425,14 +425,14 @@ config_prov(uint8_t *data, uint16_t len) prov.input_size = cmd->in_size; prov.input_actions = sys_le16_to_cpu(cmd->in_actions); - tester_rsp(BTP_SERVICE_ID_MESH, MESH_CONFIG_PROVISIONING, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_CONFIG_PROVISIONING, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void provision_node(uint8_t *data, uint16_t len) { - const struct mesh_provision_node_cmd *cmd = (void *) data; + const struct btp_mesh_provision_node_cmd *cmd = (void *) data; SYS_LOG_DBG(""); @@ -444,7 +444,7 @@ provision_node(uint8_t *data, uint16_t len) iv_index = sys_le32_to_cpu(cmd->iv_index); net_key_idx = sys_le16_to_cpu(cmd->net_key_idx); - tester_rsp(BTP_SERVICE_ID_MESH, MESH_PROVISION_NODE, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROVISION_NODE, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } @@ -477,7 +477,7 @@ init(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_MESH, MESH_INIT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INIT, CONTROLLER_INDEX, status); } @@ -488,15 +488,15 @@ reset(uint8_t *data, uint16_t len) bt_mesh_reset(); - tester_rsp(BTP_SERVICE_ID_MESH, MESH_RESET, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RESET, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void input_number(uint8_t *data, uint16_t len) { - const struct mesh_input_number_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; + const struct btp_mesh_input_number_cmd *cmd = (void *) data; + uint8_t status = BTP_STATUS_SUCCESS; uint32_t number; int err; @@ -509,15 +509,15 @@ input_number(uint8_t *data, uint16_t len) status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_INPUT_NUMBER, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_NUMBER, CONTROLLER_INDEX, status); } static void input_string(uint8_t *data, uint16_t len) { - const struct mesh_input_string_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; + const struct btp_mesh_input_string_cmd *cmd = (void *) data; + uint8_t status = BTP_STATUS_SUCCESS; uint8_t str_auth[16]; int err; @@ -541,20 +541,20 @@ input_string(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_MESH, MESH_INPUT_STRING, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_STRING, CONTROLLER_INDEX, status); } static void ivu_test_mode(uint8_t *data, uint16_t len) { - const struct mesh_ivu_test_mode_cmd *cmd = (void *) data; + const struct btp_mesh_ivu_test_mode_cmd *cmd = (void *) data; SYS_LOG_DBG("enable 0x%02x", cmd->enable); bt_mesh_iv_update_test(cmd->enable ? true : false); - tester_rsp(BTP_SERVICE_ID_MESH, MESH_IVU_TEST_MODE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TEST_MODE, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } @@ -570,14 +570,14 @@ ivu_toggle_state(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to toggle the IV Update state"); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_IVU_TOGGLE_STATE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TOGGLE_STATE, CONTROLLER_INDEX, result ? BTP_STATUS_SUCCESS : BTP_STATUS_FAILED); } static void lpn(uint8_t *data, uint16_t len) { - struct mesh_lpn_set_cmd *cmd = (void *) data; + struct btp_mesh_lpn_set_cmd *cmd = (void *) data; bool enable; int err; @@ -589,7 +589,7 @@ lpn(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to toggle LPN (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -605,15 +605,15 @@ lpn_poll(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send poll msg (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_POLL, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_POLL, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } static void net_send(uint8_t *data, uint16_t len) { - struct mesh_net_send_cmd *cmd = (void *) data; - struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); + struct btp_mesh_net_send_cmd *cmd = (void *) data; + struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = vnd_app_key_idx, @@ -638,7 +638,7 @@ net_send(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_NET_SEND, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_NET_SEND, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); os_mbuf_free_chain(msg); @@ -647,8 +647,8 @@ net_send(uint8_t *data, uint16_t len) static void health_generate_faults(uint8_t *data, uint16_t len) { - struct mesh_health_generate_faults_rp *rp; - struct os_mbuf *buf = NET_BUF_SIMPLE(sizeof(*rp) + sizeof(cur_faults) + + struct btp_mesh_health_generate_faults_rp *rp; + struct os_mbuf *buf = NET_BUF_SIMPLE(sizeof(*rp) + sizeof(cur_faults) + sizeof(reg_faults)); uint8_t some_faults[] = {0x01, 0x02, 0x03, 0xff, 0x06}; uint8_t cur_faults_count, reg_faults_count; @@ -667,7 +667,7 @@ health_generate_faults(uint8_t *data, uint16_t len) bt_mesh_fault_update(&elements[0]); - tester_send_buf(BTP_SERVICE_ID_MESH, MESH_HEALTH_GENERATE_FAULTS, + tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_HEALTH_GENERATE_FAULTS, CONTROLLER_INDEX, buf); } @@ -681,15 +681,15 @@ health_clear_faults(uint8_t *data, uint16_t len) bt_mesh_fault_update(&elements[0]); - tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_CLEAR_FAULTS, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_HEALTH_CLEAR_FAULTS, CONTROLLER_INDEX, BTP_STATUS_SUCCESS); } static void model_send(uint8_t *data, uint16_t len) { - struct mesh_model_send_cmd *cmd = (void *) data; - struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); + struct btp_mesh_model_send_cmd *cmd = (void *) data; + struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = BT_MESH_KEY_DEV, @@ -728,7 +728,7 @@ model_send(uint8_t *data, uint16_t len) } fail: - tester_rsp(BTP_SERVICE_ID_MESH, MESH_MODEL_SEND, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_MODEL_SEND, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); os_mbuf_free_chain(msg); @@ -739,8 +739,8 @@ model_send(uint8_t *data, uint16_t len) static void lpn_subscribe(uint8_t *data, uint16_t len) { - struct mesh_lpn_subscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + struct btp_mesh_lpn_subscribe_cmd *cmd = (void *) data; + uint16_t address = sys_le16_to_cpu(cmd->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -750,15 +750,15 @@ lpn_subscribe(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to subscribe (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_SUBSCRIBE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_SUBSCRIBE, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } static void lpn_unsubscribe(uint8_t *data, uint16_t len) { - struct mesh_lpn_unsubscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + struct btp_mesh_lpn_unsubscribe_cmd *cmd = (void *) data; + uint16_t address = sys_le16_to_cpu(cmd->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -768,7 +768,7 @@ lpn_unsubscribe(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to unsubscribe (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_UNSUBSCRIBE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_UNSUBSCRIBE, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -784,7 +784,7 @@ rpl_clear(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to clear RPL (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_RPL_CLEAR, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RPL_CLEAR, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -802,7 +802,7 @@ proxy_identity_enable(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to enable proxy identity (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, MESH_PROXY_IDENTITY, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROXY_IDENTITY, CONTROLLER_INDEX, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -810,63 +810,63 @@ void tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) { switch (opcode) { - case MESH_READ_SUPPORTED_COMMANDS: + case BTP_MESH_READ_SUPPORTED_COMMANDS: supported_commands(data, len); break; - case MESH_CONFIG_PROVISIONING: + case BTP_MESH_CONFIG_PROVISIONING: config_prov(data, len); break; - case MESH_PROVISION_NODE: + case BTP_MESH_PROVISION_NODE: provision_node(data, len); break; - case MESH_INIT: + case BTP_MESH_INIT: init(data, len); break; - case MESH_RESET: + case BTP_MESH_RESET: reset(data, len); break; - case MESH_INPUT_NUMBER: + case BTP_MESH_INPUT_NUMBER: input_number(data, len); break; - case MESH_INPUT_STRING: + case BTP_MESH_INPUT_STRING: input_string(data, len); break; - case MESH_IVU_TEST_MODE: + case BTP_MESH_IVU_TEST_MODE: ivu_test_mode(data, len); break; - case MESH_IVU_TOGGLE_STATE: + case BTP_MESH_IVU_TOGGLE_STATE: ivu_toggle_state(data, len); break; - case MESH_LPN: + case BTP_MESH_LPN: lpn(data, len); break; - case MESH_LPN_POLL: + case BTP_MESH_LPN_POLL: lpn_poll(data, len); break; - case MESH_NET_SEND: + case BTP_MESH_NET_SEND: net_send(data, len); break; - case MESH_HEALTH_GENERATE_FAULTS: + case BTP_MESH_HEALTH_GENERATE_FAULTS: health_generate_faults(data, len); break; - case MESH_HEALTH_CLEAR_FAULTS: + case BTP_MESH_HEALTH_CLEAR_FAULTS: health_clear_faults(data, len); break; - case MESH_MODEL_SEND: + case BTP_MESH_MODEL_SEND: model_send(data, len); break; #if MYNEWT_VAL(BLE_MESH_TESTING) - case MESH_LPN_SUBSCRIBE: + case BTP_MESH_LPN_SUBSCRIBE: lpn_subscribe(data, len); break; - case MESH_LPN_UNSUBSCRIBE: + case BTP_MESH_LPN_UNSUBSCRIBE: lpn_unsubscribe(data, len); break; - case MESH_RPL_CLEAR: + case BTP_MESH_RPL_CLEAR: rpl_clear(data, len); break; #endif /* MYNEWT_VAL(BLE_MESH_TESTING) */ - case MESH_PROXY_IDENTITY: + case BTP_MESH_PROXY_IDENTITY: proxy_identity_enable(data, len); break; default: @@ -884,8 +884,8 @@ net_recv_ev(uint8_t ttl, const void *payload, size_t payload_len) { - struct os_mbuf *buf = NET_BUF_SIMPLE(UINT8_MAX); - struct mesh_net_recv_ev *ev; + struct os_mbuf *buf = NET_BUF_SIMPLE(UINT8_MAX); + struct btp_mesh_net_recv_ev *ev; SYS_LOG_DBG("ttl 0x%02x ctl 0x%02x src 0x%04x dst 0x%04x " "payload_len %d", ttl, ctl, src, dst, payload_len); @@ -904,7 +904,7 @@ net_recv_ev(uint8_t ttl, ev->payload_len = payload_len; net_buf_simple_add_mem(buf, payload, payload_len); - tester_send_buf(BTP_SERVICE_ID_MESH, MESH_EV_NET_RECV, CONTROLLER_INDEX, + tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_EV_NET_RECV, CONTROLLER_INDEX, buf); done: os_mbuf_free_chain(buf); @@ -957,20 +957,20 @@ model_unbound_cb(uint16_t addr, struct bt_mesh_model *model, static void invalid_bearer_cb(uint8_t opcode) { - struct mesh_invalid_bearer_ev ev = { + struct btp_mesh_invalid_bearer_ev ev = { .opcode = opcode, }; SYS_LOG_DBG("opcode 0x%02x", opcode); - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_INVALID_BEARER, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INVALID_BEARER, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void incomp_timer_exp_cb(void) { - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_INCOMP_TIMER_EXP, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INCOMP_TIMER_EXP, CONTROLLER_INDEX, NULL, 0); } @@ -987,7 +987,7 @@ lpn_established(uint16_t friend_addr) { struct bt_mesh_lpn *lpn = &bt_mesh.lpn; - struct mesh_lpn_established_ev + struct btp_mesh_lpn_established_ev ev = {lpn->sub->net_idx, friend_addr, lpn->queue_size, lpn->recv_win}; @@ -995,20 +995,20 @@ lpn_established(uint16_t friend_addr) "Friend 0x%04x Queue Size %d Receive Window %d", friend_addr, lpn->queue_size, lpn->recv_win); - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_LPN_ESTABLISHED, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_ESTABLISHED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } static void lpn_terminated(uint16_t friend_addr) { - struct bt_mesh_lpn *lpn = &bt_mesh.lpn; - struct mesh_lpn_terminated_ev ev = {lpn->sub->net_idx, friend_addr}; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + struct btp_mesh_lpn_terminated_ev ev = {lpn->sub->net_idx, friend_addr}; SYS_LOG_DBG("Friendship (as LPN) lost with Friend " "0x%04x", friend_addr); - tester_send(BTP_SERVICE_ID_MESH, MESH_EV_LPN_TERMINATED, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_TERMINATED, CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } diff --git a/apps/bttester/src/bttester.c b/apps/bttester/src/bttester.c index 62219f565a..1bb22e7cfc 100644 --- a/apps/bttester/src/bttester.c +++ b/apps/bttester/src/bttester.c @@ -33,7 +33,7 @@ #include "console/console.h" #include "bttester_pipe.h" -#include "bttester.h" +#include "btp/btp.h" #define CMD_QUEUED 2 @@ -51,145 +51,6 @@ struct btp_buf { static struct btp_buf cmd_buf[CMD_QUEUED]; -static void -supported_commands(uint8_t *data, uint16_t len) -{ - uint8_t buf[1]; - struct core_read_supported_commands_rp *rp = (void *) buf; - - memset(buf, 0, sizeof(buf)); - - tester_set_bit(buf, CORE_READ_SUPPORTED_COMMANDS); - tester_set_bit(buf, CORE_READ_SUPPORTED_SERVICES); - tester_set_bit(buf, CORE_REGISTER_SERVICE); - tester_set_bit(buf, CORE_UNREGISTER_SERVICE); - - tester_send(BTP_SERVICE_ID_CORE, CORE_READ_SUPPORTED_COMMANDS, - BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); -} - -static void -supported_services(uint8_t *data, uint16_t len) -{ - uint8_t buf[1]; - struct core_read_supported_services_rp *rp = (void *) buf; - - memset(buf, 0, sizeof(buf)); - - tester_set_bit(buf, BTP_SERVICE_ID_CORE); - tester_set_bit(buf, BTP_SERVICE_ID_GAP); - tester_set_bit(buf, BTP_SERVICE_ID_GATT); -#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - tester_set_bit(buf, BTP_SERVICE_ID_L2CAP); -#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ -#if MYNEWT_VAL(BLE_MESH) - tester_set_bit(buf, BTP_SERVICE_ID_MESH); -#endif /* MYNEWT_VAL(BLE_MESH) */ - tester_set_bit(buf, BTP_SERVICE_ID_GATTC); - - tester_send(BTP_SERVICE_ID_CORE, CORE_READ_SUPPORTED_SERVICES, - BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); -} - -static void -register_service(uint8_t *data, uint16_t len) -{ - struct core_register_service_cmd *cmd = (void *) data; - uint8_t status; - - switch (cmd->id) { - case BTP_SERVICE_ID_GAP: - status = tester_init_gap(); - /* Rsp with success status will be handled by bt enable cb */ - if (status == BTP_STATUS_FAILED) { - goto rsp; - } - return; - case BTP_SERVICE_ID_GATT: - status = tester_init_gatt(); - break; -#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - case BTP_SERVICE_ID_L2CAP: - status = tester_init_l2cap(); - break; -#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ -#if MYNEWT_VAL(BLE_MESH) - case BTP_SERVICE_ID_MESH: - status = tester_init_mesh(); - break; -#endif /* MYNEWT_VAL(BLE_MESH) */ - default: - status = BTP_STATUS_FAILED; - break; - } - -rsp: - tester_rsp(BTP_SERVICE_ID_CORE, CORE_REGISTER_SERVICE, BTP_INDEX_NONE, - status); -} - -static void -unregister_service(uint8_t *data, uint16_t len) -{ - struct core_unregister_service_cmd *cmd = (void *) data; - uint8_t status; - - switch (cmd->id) { - case BTP_SERVICE_ID_GAP: - status = tester_unregister_gap(); - break; - case BTP_SERVICE_ID_GATT: - status = tester_unregister_gatt(); - break; -#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - case BTP_SERVICE_ID_L2CAP: - status = tester_unregister_l2cap(); - break; -#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ -#if MYNEWT_VAL(BLE_MESH) - case BTP_SERVICE_ID_MESH: - status = tester_unregister_mesh(); - break; -#endif /* MYNEWT_VAL(BLE_MESH) */ - default: - status = BTP_STATUS_FAILED; - break; - } - - tester_rsp(BTP_SERVICE_ID_CORE, CORE_UNREGISTER_SERVICE, BTP_INDEX_NONE, - status); -} - -static void -handle_core(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len) -{ - if (index != BTP_INDEX_NONE) { - tester_rsp(BTP_SERVICE_ID_CORE, opcode, index, - BTP_STATUS_FAILED); - return; - } - - switch (opcode) { - case CORE_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case CORE_READ_SUPPORTED_SERVICES: - supported_services(data, len); - return; - case CORE_REGISTER_SERVICE: - register_service(data, len); - return; - case CORE_UNREGISTER_SERVICE: - unregister_service(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_CORE, opcode, BTP_INDEX_NONE, - BTP_STATUS_UNKNOWN_CMD); - return; - } -} - static void cmd_handler(struct os_event *ev) { @@ -216,7 +77,7 @@ cmd_handler(struct os_event *ev) switch (cmd->hdr.service) { case BTP_SERVICE_ID_CORE: - handle_core(cmd->hdr.opcode, cmd->hdr.index, + tester_handle_core(cmd->hdr.opcode, cmd->hdr.index, cmd->hdr.data, len); break; case BTP_SERVICE_ID_GAP: @@ -330,7 +191,7 @@ tester_init(void) bttester_pipe_register(buf->data, BTP_MTU, recv_cb); - tester_send(BTP_SERVICE_ID_CORE, CORE_EV_IUT_READY, BTP_INDEX_NONE, + tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_EV_IUT_READY, BTP_INDEX_NONE, NULL, 0); } diff --git a/apps/bttester/src/bttester.h b/apps/bttester/src/bttester.h deleted file mode 100644 index 87bb275b89..0000000000 --- a/apps/bttester/src/bttester.h +++ /dev/null @@ -1,1361 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* bttester.h - Bluetooth tester headers */ - -/* - * Copyright (C) 2021 Codecoup - * Copyright (c) 2015-2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef __BTTESTER_H__ -#define __BTTESTER_H__ - -#include "syscfg/syscfg.h" -#include "host/ble_gatt.h" - -#if MYNEWT_VAL(BLE_MESH) -#include "mesh/glue.h" -#else -#include "glue.h" -#endif - -#define BTP_MTU MYNEWT_VAL(BTTESTER_BTP_DATA_SIZE_MAX) -#define BTP_DATA_MAX_SIZE (BTP_MTU - sizeof(struct btp_hdr)) - -#define BTP_INDEX_NONE 0xff - -#define BTP_SERVICE_ID_CORE 0 -#define BTP_SERVICE_ID_GAP 1 -#define BTP_SERVICE_ID_GATT 2 -#define BTP_SERVICE_ID_L2CAP 3 -#define BTP_SERVICE_ID_MESH 4 -#define BTP_SERVICE_ID_GATTC 6 - -#define BTP_STATUS_SUCCESS 0x00 -#define BTP_STATUS_FAILED 0x01 -#define BTP_STATUS_UNKNOWN_CMD 0x02 -#define BTP_STATUS_NOT_READY 0x03 - -#define SYS_LOG_DBG(fmt, ...) \ - if (MYNEWT_VAL(BTTESTER_DEBUG)) { \ - console_printf("[DBG] %s: " fmt "\n", \ - __func__, ## __VA_ARGS__); \ - } -#define SYS_LOG_INF(fmt, ...) console_printf("[INF] %s: " fmt "\n", \ - __func__, ## __VA_ARGS__); -#define SYS_LOG_ERR(fmt, ...) console_printf("[WRN] %s: " fmt "\n", \ - __func__, ## __VA_ARGS__); - -#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG -#define SYS_LOG_DOMAIN "bttester" - -#define sys_cpu_to_le32 htole32 -#define sys_le32_to_cpu le32toh -#define sys_cpu_to_le16 htole16 - -struct btp_hdr { - uint8_t service; - uint8_t opcode; - uint8_t index; - uint16_t len; - uint8_t data[0]; -} __packed; - -#define BTP_STATUS 0x00 -struct btp_status { - uint8_t code; -} __packed; - -/* Core Service */ -#define CORE_READ_SUPPORTED_COMMANDS 0x01 -struct core_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define CORE_READ_SUPPORTED_SERVICES 0x02 -struct core_read_supported_services_rp { - uint8_t data[0]; -} __packed; - -#define CORE_REGISTER_SERVICE 0x03 -struct core_register_service_cmd { - uint8_t id; -} __packed; - -#define CORE_UNREGISTER_SERVICE 0x04 -struct core_unregister_service_cmd { - uint8_t id; -} __packed; - -/* events */ -#define CORE_EV_IUT_READY 0x80 - -/* GAP Service */ -/* commands */ -#define GAP_READ_SUPPORTED_COMMANDS 0x01 -struct gap_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define GAP_READ_CONTROLLER_INDEX_LIST 0x02 -struct gap_read_controller_index_list_rp { - uint8_t num; - uint8_t index[0]; -} __packed; - -#define GAP_SETTINGS_POWERED 0 -#define GAP_SETTINGS_CONNECTABLE 1 -#define GAP_SETTINGS_FAST_CONNECTABLE 2 -#define GAP_SETTINGS_DISCOVERABLE 3 -#define GAP_SETTINGS_BONDABLE 4 -#define GAP_SETTINGS_LINK_SEC_3 5 -#define GAP_SETTINGS_SSP 6 -#define GAP_SETTINGS_BREDR 7 -#define GAP_SETTINGS_HS 8 -#define GAP_SETTINGS_LE 9 -#define GAP_SETTINGS_ADVERTISING 10 -#define GAP_SETTINGS_SC 11 -#define GAP_SETTINGS_DEBUG_KEYS 12 -#define GAP_SETTINGS_PRIVACY 13 -#define GAP_SETTINGS_CONTROLLER_CONFIG 14 -#define GAP_SETTINGS_STATIC_ADDRESS 15 - -#define GAP_READ_CONTROLLER_INFO 0x03 -struct gap_read_controller_info_rp { - uint8_t address[6]; - uint32_t supported_settings; - uint32_t current_settings; - uint8_t cod[3]; - uint8_t name[249]; - uint8_t short_name[11]; -} __packed; - -#define GAP_RESET 0x04 -struct gap_reset_rp { - uint32_t current_settings; -} __packed; - -#define GAP_SET_POWERED 0x05 -struct gap_set_powered_cmd { - uint8_t powered; -} __packed; -struct gap_set_powered_rp { - uint32_t current_settings; -} __packed; - -#define GAP_SET_CONNECTABLE 0x06 -struct gap_set_connectable_cmd { - uint8_t connectable; -} __packed; -struct gap_set_connectable_rp { - uint32_t current_settings; -} __packed; - -#define GAP_SET_FAST_CONNECTABLE 0x07 -struct gap_set_fast_connectable_cmd { - uint8_t fast_connectable; -} __packed; -struct gap_set_fast_connectable_rp { - uint32_t current_settings; -} __packed; - -#define GAP_NON_DISCOVERABLE 0x00 -#define GAP_GENERAL_DISCOVERABLE 0x01 -#define GAP_LIMITED_DISCOVERABLE 0x02 - -#define GAP_SET_DISCOVERABLE 0x08 -struct gap_set_discoverable_cmd { - uint8_t discoverable; -} __packed; -struct gap_set_discoverable_rp { - uint32_t current_settings; -} __packed; - -#define GAP_SET_BONDABLE 0x09 -struct gap_set_bondable_cmd { - uint8_t bondable; -} __packed; -struct gap_set_bondable_rp { - uint32_t current_settings; -} __packed; - -#define GAP_START_ADVERTISING 0x0a -struct gap_start_advertising_cmd { - uint8_t adv_data_len; - uint8_t scan_rsp_len; - uint8_t adv_data[0]; - uint8_t scan_rsp[0]; -} __packed; -struct gap_start_advertising_rp { - uint32_t current_settings; -} __packed; - -#define GAP_STOP_ADVERTISING 0x0b -struct gap_stop_advertising_rp { - uint32_t current_settings; -} __packed; - -#define GAP_DISCOVERY_FLAG_LE 0x01 -#define GAP_DISCOVERY_FLAG_BREDR 0x02 -#define GAP_DISCOVERY_FLAG_LIMITED 0x04 -#define GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN 0x08 -#define GAP_DISCOVERY_FLAG_LE_OBSERVE 0x10 - -#define GAP_START_DISCOVERY 0x0c -struct gap_start_discovery_cmd { - uint8_t flags; -} __packed; - -#define GAP_STOP_DISCOVERY 0x0d - -#define GAP_CONNECT 0x0e -struct gap_connect_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_DISCONNECT 0x0f -struct gap_disconnect_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_IO_CAP_DISPLAY_ONLY 0 -#define GAP_IO_CAP_DISPLAY_YESNO 1 -#define GAP_IO_CAP_KEYBOARD_ONLY 2 -#define GAP_IO_CAP_NO_INPUT_OUTPUT 3 -#define GAP_IO_CAP_KEYBOARD_DISPLAY 4 - -#define GAP_SET_IO_CAP 0x10 -struct gap_set_io_cap_cmd { - uint8_t io_cap; -} __packed; - -#define GAP_PAIR 0x11 -struct gap_pair_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_UNPAIR 0x12 -struct gap_unpair_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_PASSKEY_ENTRY 0x13 -struct gap_passkey_entry_cmd { - uint8_t address_type; - uint8_t address[6]; - uint32_t passkey; -} __packed; - -#define GAP_PASSKEY_CONFIRM 0x14 -struct gap_passkey_confirm_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t match; -} __packed; - -#define GAP_START_DIRECT_ADV 0x15 -struct gap_start_direct_adv_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t high_duty; -} __packed; - -#define GAP_CONN_PARAM_UPDATE 0x16 -struct gap_conn_param_update_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t conn_itvl_min; - uint16_t conn_itvl_max; - uint16_t conn_latency; - uint16_t supervision_timeout; -} __packed; - -#define GAP_PAIRING_CONSENT_RSP 0x17 -struct gap_pairing_consent_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t consent; -} __packed; - -#define GAP_OOB_LEGACY_SET_DATA 0x18 -struct gap_oob_legacy_set_data_cmd { - uint8_t oob_data[16]; -} __packed; - -#define GAP_OOB_SC_GET_LOCAL_DATA 0x19 -struct gap_oob_sc_get_local_data_rp { - uint8_t r[16]; - uint8_t c[16]; -} __packed; - -#define GAP_OOB_SC_SET_REMOTE_DATA 0x1a -struct gap_oob_sc_set_remote_data_cmd { - uint8_t r[16]; - uint8_t c[16]; -} __packed; - -#define GAP_SET_MITM 0x1b -struct gap_set_mitm_cmd { - uint8_t mitm; -} __packed; - -#define GAP_SET_FILTER_ACCEPT_LIST 0x1c -struct gap_set_filter_accept_list_cmd { - uint8_t list_len; - ble_addr_t addrs[]; -} __packed; -/* events */ -#define GAP_EV_NEW_SETTINGS 0x80 -struct gap_new_settings_ev { - uint32_t current_settings; -} __packed; - -#define GAP_DEVICE_FOUND_FLAG_RSSI 0x01 -#define GAP_DEVICE_FOUND_FLAG_AD 0x02 -#define GAP_DEVICE_FOUND_FLAG_SD 0x04 - -#define GAP_EV_DEVICE_FOUND 0x81 -struct gap_device_found_ev { - uint8_t address_type; - uint8_t address[6]; - int8_t rssi; - uint8_t flags; - uint16_t eir_data_len; - uint8_t eir_data[0]; -} __packed; - -#define GAP_EV_DEVICE_CONNECTED 0x82 -struct gap_device_connected_ev { - uint8_t address_type; - uint8_t address[6]; - uint16_t conn_itvl; - uint16_t conn_latency; - uint16_t supervision_timeout; -} __packed; - -#define GAP_EV_DEVICE_DISCONNECTED 0x83 -struct gap_device_disconnected_ev { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_EV_PASSKEY_DISPLAY 0x84 -struct gap_passkey_display_ev { - uint8_t address_type; - uint8_t address[6]; - uint32_t passkey; -} __packed; - -#define GAP_EV_PASSKEY_ENTRY_REQ 0x85 -struct gap_passkey_entry_req_ev { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_EV_PASSKEY_CONFIRM_REQ 0x86 -struct gap_passkey_confirm_req_ev { - uint8_t address_type; - uint8_t address[6]; - uint32_t passkey; -} __packed; - -#define GAP_EV_IDENTITY_RESOLVED 0x87 -struct gap_identity_resolved_ev { - uint8_t address_type; - uint8_t address[6]; - uint8_t identity_address_type; - uint8_t identity_address[6]; -} __packed; - -#define GAP_EV_CONN_PARAM_UPDATE 0x88 -struct gap_conn_param_update_ev { - uint8_t address_type; - uint8_t address[6]; - uint16_t conn_itvl; - uint16_t conn_latency; - uint16_t supervision_timeout; -} __packed; - -#define GAP_EV_SEC_LEVEL_CHANGED 0x89 -struct gap_sec_level_changed_ev { - uint8_t address_type; - uint8_t address[6]; - uint8_t level; -} __packed; - -#define GAP_EV_PAIRING_CONSENT_REQ 0x8a -struct gap_pairing_consent_req_ev { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_EV_BOND_LOST 0x8b -struct gap_bond_lost_ev { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GAP_EV_SEC_PAIRING_FAILED 0x8c -struct gap_sec_pairing_failed_ev { - uint8_t address_type; - uint8_t address[6]; - uint8_t reason; -} __packed; - -/* GATT Service */ -/* commands */ -#define GATT_READ_SUPPORTED_COMMANDS 0x01 -struct gatt_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define GATT_SERVICE_PRIMARY 0x00 -#define GATT_SERVICE_SECONDARY 0x01 - -#define GATT_ADD_SERVICE 0x02 -struct gatt_add_service_cmd { - uint8_t type; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; -struct gatt_add_service_rp { - uint16_t svc_id; -} __packed; - -#define GATT_ADD_CHARACTERISTIC 0x03 -struct gatt_add_characteristic_cmd { - uint16_t svc_id; - uint8_t properties; - uint8_t permissions; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; -struct gatt_add_characteristic_rp { - uint16_t char_id; -} __packed; - -#define GATT_ADD_DESCRIPTOR 0x04 -struct gatt_add_descriptor_cmd { - uint16_t char_id; - uint8_t permissions; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; -struct gatt_add_descriptor_rp { - uint16_t desc_id; -} __packed; - -#define GATT_ADD_INCLUDED_SERVICE 0x05 -struct gatt_add_included_service_cmd { - uint16_t svc_id; -} __packed; -struct gatt_add_included_service_rp { - uint16_t included_service_id; -} __packed; - -#define GATT_SET_VALUE 0x06 -struct gatt_set_value_cmd { - uint16_t attr_id; - uint16_t len; - uint8_t value[0]; -} __packed; - -#define GATT_START_SERVER 0x07 -struct gatt_start_server_rp { - uint16_t db_attr_off; - uint8_t db_attr_cnt; -} __packed; - -#define GATT_SET_ENC_KEY_SIZE 0x09 -struct gatt_set_enc_key_size_cmd { - uint16_t attr_id; - uint8_t key_size; -} __packed; - -/* Gatt Client */ -struct gatt_service { - uint16_t start_handle; - uint16_t end_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -struct gatt_included { - uint16_t included_handle; - struct gatt_service service; -} __packed; - -struct gatt_read_uuid_chr { - uint16_t handle; - uint8_t data[0]; -} __packed; - -struct gatt_characteristic { - uint16_t characteristic_handle; - uint16_t value_handle; - uint8_t properties; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -struct gatt_descriptor { - uint16_t descriptor_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -#define GATT_EXCHANGE_MTU 0x0a - -#define GATT_DISC_ALL_PRIM_SVCS 0x0b -struct gatt_disc_all_prim_svcs_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; -struct gatt_disc_all_prim_svcs_rp { - uint8_t services_count; - struct gatt_service services[0]; -} __packed; - -#define GATT_DISC_PRIM_UUID 0x0c -struct gatt_disc_prim_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; -struct gatt_disc_prim_uuid_rp { - uint8_t services_count; - struct gatt_service services[0]; -} __packed; - -#define GATT_FIND_INCLUDED 0x0d -struct gatt_find_included_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; -struct gatt_find_included_rp { - uint8_t services_count; - struct gatt_included included[0]; -} __packed; - -#define GATT_DISC_ALL_CHRC 0x0e -struct gatt_disc_all_chrc_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; -struct gatt_disc_chrc_rp { - uint8_t characteristics_count; - struct gatt_characteristic characteristics[0]; -} __packed; - -#define GATT_DISC_CHRC_UUID 0x0f -struct gatt_disc_chrc_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -#define GATT_DISC_ALL_DESC 0x10 -struct gatt_disc_all_desc_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; -struct gatt_disc_all_desc_rp { - uint8_t descriptors_count; - struct gatt_descriptor descriptors[0]; -} __packed; - -#define GATT_READ 0x11 -struct gatt_read_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; -} __packed; -struct gatt_read_rp { - uint8_t att_response; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_READ_UUID 0x12 -struct gatt_read_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -#define GATT_READ_LONG 0x13 -struct gatt_read_long_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; -} __packed; - -#define GATT_READ_MULTIPLE 0x14 -struct gatt_read_multiple_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t handles_count; - uint16_t handles[0]; -} __packed; - -#define GATT_WRITE_WITHOUT_RSP 0x15 -struct gatt_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_SIGNED_WRITE_WITHOUT_RSP 0x16 -struct gatt_signed_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_WRITE 0x17 -struct gatt_write_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_WRITE_LONG 0x18 -struct gatt_write_long_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_RELIABLE_WRITE 0x19 -struct gatt_reliable_write_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_CFG_NOTIFY 0x1a -#define GATT_CFG_INDICATE 0x1b -struct gatt_cfg_notify_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t enable; - uint16_t ccc_handle; -} __packed; - -#define GATT_GET_ATTRIBUTES 0x1c -struct gatt_get_attributes_cmd { - uint16_t start_handle; - uint16_t end_handle; - uint8_t type_length; - uint8_t type[0]; -} __packed; -struct gatt_get_attributes_rp { - uint8_t attrs_count; - uint8_t attrs[0]; -} __packed; -struct gatt_attr { - uint16_t handle; - uint8_t permission; - uint8_t type_length; - uint8_t type[0]; -} __packed; - -#define GATT_GET_ATTRIBUTE_VALUE 0x1d -struct gatt_get_attribute_value_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; -} __packed; -struct gatt_get_attribute_value_rp { - uint8_t att_response; - uint16_t value_length; - uint8_t value[0]; -} __packed; - -#define GATT_CHANGE_DATABASE 0x1e -struct gatt_change_database { - uint16_t start_handle; - uint16_t end_handle; - uint8_t visibility; -} __packed; - -/* GATT events */ -#define GATT_EV_NOTIFICATION 0x80 -struct gatt_notification_ev { - uint8_t address_type; - uint8_t address[6]; - uint8_t type; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATT_EV_ATTR_VALUE_CHANGED 0x81 -struct gatt_attr_value_changed_ev { - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -static inline void -tester_set_bit(uint8_t *addr, unsigned int bit) -{ - uint8_t *p = addr + (bit / 8); - - *p |= BIT(bit % 8); -} - -static inline uint8_t -tester_test_bit(const uint8_t *addr, unsigned int bit) -{ - const uint8_t *p = addr + (bit / 8); - - return *p & BIT(bit % 8); -} - -/* L2CAP Service */ -/* commands */ -#define L2CAP_READ_SUPPORTED_COMMANDS 0x01 -struct l2cap_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define L2CAP_CONNECT_OPT_ECFC 0x01 -#define L2CAP_CONNECT_OPT_HOLD_CREDIT 0x02 - -#define L2CAP_CONNECT 0x02 -struct l2cap_connect_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t psm; - uint16_t mtu; - uint8_t num; - uint8_t options; -} __packed; - -struct l2cap_connect_rp { - uint8_t num; - uint8_t chan_ids[0]; -} __packed; - -#define L2CAP_DISCONNECT 0x03 -struct l2cap_disconnect_cmd { - uint8_t chan_id; -} __packed; - -#define L2CAP_SEND_DATA 0x04 -struct l2cap_send_data_cmd { - uint8_t chan_id; - uint16_t data_len; - uint8_t data[]; -} __packed; - -#define L2CAP_TRANSPORT_BREDR 0x00 -#define L2CAP_TRANSPORT_LE 0x01 - -#define L2CAP_LISTEN 0x05 -struct l2cap_listen_cmd { - uint16_t psm; - uint8_t transport; - uint16_t mtu; - uint16_t response; -} __packed; - -#define L2CAP_ACCEPT_CONNECTION 0x06 -struct l2cap_accept_connection_cmd { - uint8_t chan_id; - uint16_t result; -} __packed; - -#define L2CAP_RECONFIGURE 0x07 -struct l2cap_reconfigure_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t mtu; - uint8_t num; - uint8_t idxs[]; -} __packed; - -#define L2CAP_CREDITS 0x08 -struct l2cap_credits_cmd { - uint8_t chan_id; -} __packed; - -/* events */ -#define L2CAP_EV_CONNECTION_REQ 0x80 -struct l2cap_connection_req_ev { - uint8_t chan_id; - uint16_t psm; - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define L2CAP_EV_CONNECTED 0x81 -struct l2cap_connected_ev { - uint8_t chan_id; - uint16_t psm; - uint16_t peer_mtu; - uint16_t peer_mps; - uint16_t our_mtu; - uint16_t our_mps; - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define L2CAP_EV_DISCONNECTED 0x82 -struct l2cap_disconnected_ev { - uint16_t result; - uint8_t chan_id; - uint16_t psm; - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define L2CAP_EV_DATA_RECEIVED 0x83 -struct l2cap_data_received_ev { - uint8_t chan_id; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define L2CAP_EV_RECONFIGURED 0x84 -struct l2cap_reconfigured_ev { - uint8_t chan_id; - uint16_t peer_mtu; - uint16_t peer_mps; - uint16_t our_mtu; - uint16_t our_mps; -} __packed; - -/* MESH Service */ -/* commands */ -#define MESH_READ_SUPPORTED_COMMANDS 0x01 -struct mesh_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define MESH_OUT_BLINK BIT(0) -#define MESH_OUT_BEEP BIT(1) -#define MESH_OUT_VIBRATE BIT(2) -#define MESH_OUT_DISPLAY_NUMBER BIT(3) -#define MESH_OUT_DISPLAY_STRING BIT(4) - -#define MESH_IN_PUSH BIT(0) -#define MESH_IN_TWIST BIT(1) -#define MESH_IN_ENTER_NUMBER BIT(2) -#define MESH_IN_ENTER_STRING BIT(3) - -#define MESH_CONFIG_PROVISIONING 0x02 -struct mesh_config_provisioning_cmd { - uint8_t uuid[16]; - uint8_t static_auth[16]; - uint8_t out_size; - uint16_t out_actions; - uint8_t in_size; - uint16_t in_actions; -} __packed; - -#define MESH_PROVISION_NODE 0x03 -struct mesh_provision_node_cmd { - uint8_t net_key[16]; - uint16_t net_key_idx; - uint8_t flags; - uint32_t iv_index; - uint32_t seq_num; - uint16_t addr; - uint8_t dev_key[16]; -} __packed; - -#define MESH_INIT 0x04 -#define MESH_RESET 0x05 -#define MESH_INPUT_NUMBER 0x06 -struct mesh_input_number_cmd { - uint32_t number; -} __packed; - -#define MESH_INPUT_STRING 0x07 -struct mesh_input_string_cmd { - uint8_t string_len; - uint8_t string[0]; -} __packed; - -#define MESH_IVU_TEST_MODE 0x08 -struct mesh_ivu_test_mode_cmd { - uint8_t enable; -} __packed; - -#define MESH_IVU_TOGGLE_STATE 0x09 - -#define MESH_NET_SEND 0x0a -struct mesh_net_send_cmd { - uint8_t ttl; - uint16_t src; - uint16_t dst; - uint8_t payload_len; - uint8_t payload[0]; -} __packed; - -#define MESH_HEALTH_GENERATE_FAULTS 0x0b -struct mesh_health_generate_faults_rp { - uint8_t test_id; - uint8_t cur_faults_count; - uint8_t reg_faults_count; - uint8_t current_faults[0]; - uint8_t registered_faults[0]; -} __packed; - -#define MESH_HEALTH_CLEAR_FAULTS 0x0c - -#define MESH_LPN 0x0d -struct mesh_lpn_set_cmd { - uint8_t enable; -} __packed; - -#define MESH_LPN_POLL 0x0e - -#define MESH_MODEL_SEND 0x0f -struct mesh_model_send_cmd { - uint16_t src; - uint16_t dst; - uint8_t payload_len; - uint8_t payload[0]; -} __packed; - -#define MESH_LPN_SUBSCRIBE 0x10 -struct mesh_lpn_subscribe_cmd { - uint16_t address; -} __packed; - -#define MESH_LPN_UNSUBSCRIBE 0x11 -struct mesh_lpn_unsubscribe_cmd { - uint16_t address; -} __packed; - -#define MESH_RPL_CLEAR 0x12 -#define MESH_PROXY_IDENTITY 0x13 - -/* events */ -#define MESH_EV_OUT_NUMBER_ACTION 0x80 -struct mesh_out_number_action_ev { - uint16_t action; - uint32_t number; -} __packed; - -#define MESH_EV_OUT_STRING_ACTION 0x81 -struct mesh_out_string_action_ev { - uint8_t string_len; - uint8_t string[0]; -} __packed; - -#define MESH_EV_IN_ACTION 0x82 -struct mesh_in_action_ev { - uint16_t action; - uint8_t size; -} __packed; - -#define MESH_EV_PROVISIONED 0x83 - -#define MESH_PROV_BEARER_PB_ADV 0x00 -#define MESH_PROV_BEARER_PB_GATT 0x01 -#define MESH_EV_PROV_LINK_OPEN 0x84 -struct mesh_prov_link_open_ev { - uint8_t bearer; -} __packed; - -#define MESH_EV_PROV_LINK_CLOSED 0x85 -struct mesh_prov_link_closed_ev { - uint8_t bearer; -} __packed; - -#define MESH_EV_NET_RECV 0x86 -struct mesh_net_recv_ev { - uint8_t ttl; - uint8_t ctl; - uint16_t src; - uint16_t dst; - uint8_t payload_len; - uint8_t payload[0]; -} __packed; - -#define MESH_EV_INVALID_BEARER 0x87 -struct mesh_invalid_bearer_ev { - uint8_t opcode; -} __packed; - -#define MESH_EV_INCOMP_TIMER_EXP 0x88 - -#define MESH_EV_LPN_ESTABLISHED 0x8b -struct mesh_lpn_established_ev { - uint16_t net_idx; - uint16_t friend_addr; - uint8_t queue_size; - uint8_t recv_win; -} __packed; - -#define MESH_EV_LPN_TERMINATED 0x8c -struct mesh_lpn_terminated_ev { - uint16_t net_idx; - uint16_t friend_addr; -} __packed; - -#define MESH_EV_LPN_POLLED 0x8d -struct mesh_lpn_polled_ev { - uint16_t net_idx; - uint16_t friend_addr; - uint8_t retry; -} __packed; - -void -tester_init(void); -void -tester_rsp(uint8_t service, uint8_t opcode, uint8_t index, uint8_t status); -void -tester_send(uint8_t service, uint8_t opcode, uint8_t index, uint8_t *data, - size_t len); -void -tester_send_buf(uint8_t service, uint8_t opcode, uint8_t index, - struct os_mbuf *buf); - -uint8_t -tester_init_gap(void); -uint8_t -tester_unregister_gap(void); -void -tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -uint8_t -tester_init_gatt(void); -uint8_t -tester_unregister_gatt(void); -void -tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -void -tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -int -tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, - uint8_t indication, struct os_mbuf *om); -int -tester_gatt_subscribe_ev(uint16_t conn_handle, - uint16_t attr_handle, - uint8_t reason, - uint8_t prev_notify, - uint8_t cur_notify, - uint8_t prev_indicate, - uint8_t cur_indicate); - -#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) -uint8_t -tester_init_l2cap(void); -uint8_t -tester_unregister_l2cap(void); -void -tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -#endif - -#if MYNEWT_VAL(BLE_MESH) -uint8_t -tester_init_mesh(void); -uint8_t -tester_unregister_mesh(void); -void -tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len); -#endif /* MYNEWT_VAL(BLE_MESH) */ - -void -gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); -int -gatt_svr_init(void); - -/* GATT Client Service */ -/* commands */ -#define GATTC_READ_SUPPORTED_COMMANDS 0x01 -struct gattc_read_supported_commands_rp { - uint8_t data[0]; -} __packed; - -#define GATTC_EXCHANGE_MTU 0x02 - -#define GATTC_DISC_ALL_PRIM_SVCS 0x03 -struct gattc_disc_all_prim_svcs_cmd { - uint8_t address_type; - uint8_t address[6]; -} __packed; - -#define GATTC_DISC_PRIM_UUID 0x04 -struct gattc_disc_prim_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -#define GATTC_FIND_INCLUDED 0x05 -struct gattc_find_included_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; - -#define GATTC_DISC_ALL_CHRC 0x06 -struct gattc_disc_all_chrc_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; - -#define GATTC_DISC_CHRC_UUID 0x07 -struct gattc_disc_chrc_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; - -#define GATTC_DISC_ALL_DESC 0x08 -struct gattc_disc_all_desc_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; -} __packed; - -#define GATTC_READ 0x09 -struct gattc_read_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; -} __packed; - -#define GATTC_READ_UUID 0x0a -struct gattc_read_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t start_handle; - uint16_t end_handle; - uint8_t uuid_length; - uint8_t uuid[0]; -} __packed; -struct gattc_read_uuid_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint16_t data_length; - uint8_t value_length; - uint8_t data[0]; -} __packed; - -#define GATTC_READ_LONG 0x0b -struct gattc_read_long_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; -} __packed; - -#define GATTC_READ_MULTIPLE 0x0c -struct gattc_read_multiple_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t handles_count; - uint16_t handles[0]; -} __packed; - -#define GATTC_WRITE_WITHOUT_RSP 0x0d -struct gattc_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_SIGNED_WRITE_WITHOUT_RSP 0x0e -struct gattc_signed_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_WRITE 0x0f -struct gattc_write_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_WRITE_LONG 0x10 -struct gattc_write_long_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_RELIABLE_WRITE 0x11 -struct gattc_reliable_write_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_CFG_NOTIFY 0x12 -#define GATTC_CFG_INDICATE 0x13 -struct gattc_cfg_notify_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t enable; - uint16_t ccc_handle; -} __packed; - -/* events */ -#define GATTC_EV_MTU_EXCHANGED 0x80 -struct gattc_exchange_mtu_ev { - uint8_t address_type; - uint8_t address[6]; - uint16_t mtu; -} __packed; - -#define GATTC_DISC_ALL_PRIM_RP 0x81 -struct gattc_disc_prim_svcs_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint8_t services_count; - uint8_t data[0]; -} __packed; - -#define GATTC_DISC_PRIM_UUID_RP 0x82 - -#define GATTC_FIND_INCLUDED_RP 0x83 -struct gattc_find_included_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint8_t services_count; - struct gatt_included included[0]; -} __packed; - -#define GATTC_DISC_ALL_CHRC_RP 0x84 -#define GATTC_DISC_CHRC_UUID_RP 0x85 -struct gattc_disc_chrc_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint8_t characteristics_count; - struct gatt_characteristic characteristics[0]; -} __packed; - -#define GATTC_DISC_ALL_DESC_RP 0x86 -struct gattc_disc_all_desc_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint8_t descriptors_count; - struct gatt_descriptor descriptors[0]; -} __packed; - -#define GATTC_READ_RP 0x87 -#define GATTC_READ_UUID_RP 0x88 -#define GATTC_READ_LONG_RP 0x89 -#define GATTC_READ_MULTIPLE_RP 0x8a -struct gattc_read_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#define GATTC_WRITE_RP 0x8b -struct gattc_write_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; -} __packed; -#define GATTC_WRITE_LONG_RP 0x8c -#define GATTC_RELIABLE_WRITE_RP 0x8d -#define GATTC_RELIABLE_WRITE_RP 0x8d -#define GATTC_CFG_NOTIFY_RP 0x8e -#define GATTC_CFG_INDICATE_RP 0x8f -struct subscribe_rp { - uint8_t address_type; - uint8_t address[6]; - uint8_t status; -} __packed; - -#define GATTC_EV_NOTIFICATION_RXED 0x90 -struct gattc_notification_ev { - uint8_t address_type; - uint8_t address[6]; - uint8_t type; - uint16_t handle; - uint16_t data_length; - uint8_t data[0]; -} __packed; - -#endif /* __BTTESTER_H__ */ diff --git a/apps/bttester/src/bttester_pipe.h b/apps/bttester/src/bttester_pipe.h index 5f27a41c2c..ac56c575ac 100644 --- a/apps/bttester/src/bttester_pipe.h +++ b/apps/bttester/src/bttester_pipe.h @@ -21,7 +21,7 @@ #define __BTTESTER_PIPE_H__ #include -#include "bttester.h" +#include "btp/bttester.h" #ifdef __cplusplus extern "C" { diff --git a/apps/bttester/src/main.c b/apps/bttester/src/main.c index 8cc48661ae..99dff7e3de 100644 --- a/apps/bttester/src/main.c +++ b/apps/bttester/src/main.c @@ -31,7 +31,7 @@ #include "host/ble_uuid.h" #include "host/ble_hs.h" -#include "bttester.h" +#include "btp/btp.h" static void on_reset(int reason) From f9acbd519c449729fb00a15ef3f770c4bd14576c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Fri, 7 Jul 2023 14:33:08 +0200 Subject: [PATCH 02/12] apps/bttester: refactor BTP handling This commit refactors BTP handling to new model, where API is provided for registration of handlers for BTP commands as callbacks. New API provides also size check and common buffer for response. Removes Controlle Index validation from BTP handlers, as these are always 0. Corrected some formatting for variable declarations. This refactors only core and GAP service into new API, rest of services will not work but are compiled. They will be refactored in following commits. --- apps/bttester/src/btp/btp.h | 8 + apps/bttester/src/btp/btp_gap.h | 7 + apps/bttester/src/btp/bttester.h | 32 +- apps/bttester/src/btp_core.c | 176 +++--- apps/bttester/src/btp_gap.c | 900 ++++++++++++++++--------------- apps/bttester/src/btp_gatt.c | 181 +++---- apps/bttester/src/btp_gatt_cl.c | 111 ++-- apps/bttester/src/btp_l2cap.c | 66 +-- apps/bttester/src/btp_mesh.c | 91 ++-- apps/bttester/src/bttester.c | 142 +++-- apps/bttester/src/glue.h | 19 + 11 files changed, 929 insertions(+), 804 deletions(-) diff --git a/apps/bttester/src/btp/btp.h b/apps/bttester/src/btp/btp.h index 3cf172f9b5..996ab7912b 100644 --- a/apps/bttester/src/btp/btp.h +++ b/apps/bttester/src/btp/btp.h @@ -44,6 +44,7 @@ #define BTP_DATA_MAX_SIZE (BTP_MTU - sizeof(struct btp_hdr)) #define BTP_INDEX_NONE 0xff +#define BTP_INDEX 0x00 #define BTP_SERVICE_ID_CORE 0 #define BTP_SERVICE_ID_GAP 1 @@ -52,11 +53,18 @@ #define BTP_SERVICE_ID_MESH 4 #define BTP_SERVICE_ID_GATTC 6 +#define BTP_SERVICE_ID_MAX BTP_SERVICE_ID_GATTC + #define BTP_STATUS_SUCCESS 0x00 #define BTP_STATUS_FAILED 0x01 #define BTP_STATUS_UNKNOWN_CMD 0x02 #define BTP_STATUS_NOT_READY 0x03 +/* TODO indicate delay response, should be removed when all commands are + * converted to cmd+status+ev pattern + */ +#define BTP_STATUS_DELAY_REPLY 0xFF + #define SYS_LOG_DBG(fmt, ...) \ if (MYNEWT_VAL(BTTESTER_DEBUG)) { \ console_printf("[DBG] %s: " fmt "\n", \ diff --git a/apps/bttester/src/btp/btp_gap.h b/apps/bttester/src/btp/btp_gap.h index 4a24df678d..5ef756113f 100644 --- a/apps/bttester/src/btp/btp_gap.h +++ b/apps/bttester/src/btp/btp_gap.h @@ -121,6 +121,12 @@ struct btp_gap_start_advertising_cmd { uint8_t scan_rsp_len; uint8_t adv_data[0]; uint8_t scan_rsp[0]; +/* + * This command is very unfortunate because it has two fields after variable + * data. Those needs to be handled explicitly by handler. + * uint32_t duration; + * uint8_t own_addr_type; + */ } __packed; struct btp_gap_start_advertising_rp { uint32_t current_settings; @@ -148,6 +154,7 @@ struct btp_gap_start_discovery_cmd { struct btp_gap_connect_cmd { uint8_t address_type; uint8_t address[6]; + uint8_t own_addr_type; } __packed; #define BTP_GAP_DISCONNECT 0x0f diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 06ea5833e1..e13400ae96 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -36,6 +36,7 @@ #else #include "glue.h" #endif +#include static inline void tester_set_bit(uint8_t *addr, unsigned int bit) @@ -57,10 +58,21 @@ tester_test_bit(const uint8_t *addr, unsigned int bit) void tester_init(void); void -tester_rsp(uint8_t service, uint8_t opcode, uint8_t index, uint8_t status); +tester_rsp(uint8_t service, uint8_t opcode, uint8_t status); void -tester_send(uint8_t service, uint8_t opcode, uint8_t index, uint8_t *data, - size_t len); +tester_send(uint8_t service, uint8_t opcode, uint8_t *data, size_t len); + +struct btp_handler { + uint8_t opcode; + uint8_t index; + ssize_t expect_len; + uint8_t (*func)(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len); +}; + +void tester_register_command_handlers(uint8_t service, + const struct btp_handler *handlers, + size_t num); void tester_send_buf(uint8_t service, uint8_t opcode, uint8_t index, struct os_mbuf *buf); @@ -70,20 +82,16 @@ tester_init_gap(void); uint8_t tester_unregister_gap(void); void -tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -void -tester_handle_core(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); +tester_init_core(void); uint8_t tester_init_gatt(void); uint8_t tester_unregister_gatt(void); void -tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_gatt(uint8_t opcode, uint8_t *data, uint16_t len); void -tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_gattc(uint8_t opcode, uint8_t *data, uint16_t len); int tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, @@ -103,7 +111,7 @@ tester_init_l2cap(void); uint8_t tester_unregister_l2cap(void); void -tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_l2cap(uint8_t opcode, uint8_t *data, uint16_t len); #endif @@ -113,7 +121,7 @@ tester_init_mesh(void); uint8_t tester_unregister_mesh(void); void -tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len); +tester_handle_mesh(uint8_t opcode, uint8_t *data, uint16_t len); #endif /* MYNEWT_VAL(BLE_MESH) */ void diff --git a/apps/bttester/src/btp_core.c b/apps/bttester/src/btp_core.c index afcac7c52b..4aee11d347 100644 --- a/apps/bttester/src/btp_core.c +++ b/apps/bttester/src/btp_core.c @@ -26,71 +26,80 @@ */ #include "btp/btp.h" +#include "atomic.h" -static void -supported_commands(uint8_t *data, uint16_t len) +static ATOMIC_DEFINE(registered_services, BTP_SERVICE_ID_MAX); + +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t buf[1]; - struct core_read_supported_commands_rp *rp = (void *) buf; + struct btp_core_read_supported_commands_rp *rp = rsp; - memset(buf, 0, sizeof(buf)); + tester_set_bit(rp->data, BTP_CORE_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_CORE_READ_SUPPORTED_SERVICES); + tester_set_bit(rp->data, BTP_CORE_REGISTER_SERVICE); + tester_set_bit(rp->data, BTP_CORE_UNREGISTER_SERVICE); - tester_set_bit(buf, BTP_CORE_READ_SUPPORTED_COMMANDS); - tester_set_bit(buf, BTP_CORE_READ_SUPPORTED_SERVICES); - tester_set_bit(buf, BTP_CORE_REGISTER_SERVICE); - tester_set_bit(buf, BTP_CORE_UNREGISTER_SERVICE); + *rsp_len = sizeof(*rp) + 1; - tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_READ_SUPPORTED_COMMANDS, - BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); + return BTP_STATUS_SUCCESS; } -static void -supported_services(uint8_t *data, uint16_t len) +static uint8_t +supported_services(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t buf[1]; - struct core_read_supported_services_rp *rp = (void *) buf; - - memset(buf, 0, sizeof(buf)); + struct btp_core_read_supported_services_rp *rp = rsp; - tester_set_bit(buf, BTP_SERVICE_ID_CORE); - tester_set_bit(buf, BTP_SERVICE_ID_GAP); - tester_set_bit(buf, BTP_SERVICE_ID_GATT); + /* octet 0 */ + tester_set_bit(rp->data, BTP_SERVICE_ID_CORE); + tester_set_bit(rp->data, BTP_SERVICE_ID_GAP); + tester_set_bit(rp->data, BTP_SERVICE_ID_GATT); #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - tester_set_bit(buf, BTP_SERVICE_ID_L2CAP); + tester_set_bit(rp->data, BTP_SERVICE_ID_L2CAP); #endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ #if MYNEWT_VAL(BLE_MESH) - tester_set_bit(buf, BTP_SERVICE_ID_MESH); + tester_set_bit(rp->data, BTP_SERVICE_ID_MESH); #endif /* MYNEWT_VAL(BLE_MESH) */ - tester_set_bit(buf, BTP_SERVICE_ID_GATTC); + tester_set_bit(rp->data, BTP_SERVICE_ID_GATTC); + + *rsp_len = sizeof(*rp) + 2; - tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_READ_SUPPORTED_SERVICES, - BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); + return BTP_STATUS_SUCCESS; } -static void -register_service(uint8_t *data, uint16_t len) +static uint8_t +register_service(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct core_register_service_cmd *cmd = (void *) data; + const struct btp_core_register_service_cmd *cp = cmd; uint8_t status; - switch (cmd->id) { + /* invalid service */ + if ((cp->id == BTP_SERVICE_ID_CORE) || (cp->id > BTP_SERVICE_ID_MAX)) { + return BTP_STATUS_FAILED; + } + + /* already registered */ + if (atomic_test_bit(registered_services, cp->id)) { + return BTP_STATUS_FAILED; + } + + switch (cp->id) { case BTP_SERVICE_ID_GAP: status = tester_init_gap(); - /* Rsp with success status will be handled by bt enable cb */ - if (status == BTP_STATUS_FAILED) { - goto rsp; - } - return; + break; case BTP_SERVICE_ID_GATT: status = tester_init_gatt(); break; #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - case BTP_SERVICE_ID_L2CAP: + case BTP_SERVICE_ID_L2CAP: status = tester_init_l2cap(); break; #endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ #if MYNEWT_VAL(BLE_MESH) - case BTP_SERVICE_ID_MESH: + case BTP_SERVICE_ID_MESH: status = tester_init_mesh(); break; #endif /* MYNEWT_VAL(BLE_MESH) */ @@ -99,18 +108,31 @@ register_service(uint8_t *data, uint16_t len) break; } -rsp: - tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, BTP_INDEX_NONE, - status); + if (status == BTP_STATUS_SUCCESS) { + atomic_set_bit(registered_services, cp->id); + } + + return status; } -static void -unregister_service(uint8_t *data, uint16_t len) +static uint8_t +unregister_service(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct core_unregister_service_cmd *cmd = (void *) data; + const struct btp_core_unregister_service_cmd *cp = cmd; uint8_t status; - switch (cmd->id) { + /* invalid service ID */ + if ((cp->id == BTP_SERVICE_ID_CORE) || (cp->id > BTP_SERVICE_ID_MAX)) { + return BTP_STATUS_FAILED; + } + + /* not registered */ + if (!atomic_test_bit(registered_services, cp->id)) { + return BTP_STATUS_FAILED; + } + + switch (cp->id) { case BTP_SERVICE_ID_GAP: status = tester_unregister_gap(); break; @@ -118,12 +140,12 @@ unregister_service(uint8_t *data, uint16_t len) status = tester_unregister_gatt(); break; #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - case BTP_SERVICE_ID_L2CAP: + case BTP_SERVICE_ID_L2CAP: status = tester_unregister_l2cap(); break; #endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ #if MYNEWT_VAL(BLE_MESH) - case BTP_SERVICE_ID_MESH: + case BTP_SERVICE_ID_MESH: status = tester_unregister_mesh(); break; #endif /* MYNEWT_VAL(BLE_MESH) */ @@ -132,36 +154,44 @@ unregister_service(uint8_t *data, uint16_t len) break; } - tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_UNREGISTER_SERVICE, BTP_INDEX_NONE, - status); + if (status == BTP_STATUS_SUCCESS) { + atomic_clear_bit(registered_services, cp->id); + } + + return status; } +static const struct btp_handler handlers[] = { + { + .opcode = BTP_CORE_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_CORE_READ_SUPPORTED_SERVICES, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_services, + }, + { + .opcode = BTP_CORE_REGISTER_SERVICE, + .index = BTP_INDEX_NONE, + .expect_len = sizeof(struct btp_core_register_service_cmd), + .func = register_service, + }, + { + .opcode = BTP_CORE_UNREGISTER_SERVICE, + .index = BTP_INDEX_NONE, + .expect_len = sizeof(struct btp_core_unregister_service_cmd), + .func = unregister_service, + }, +}; + void -tester_handle_core(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len) +tester_init_core(void) { - if (index != BTP_INDEX_NONE) { - tester_rsp(BTP_SERVICE_ID_CORE, opcode, index, - BTP_STATUS_FAILED); - return; - } - - switch (opcode) { - case BTP_CORE_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case BTP_CORE_READ_SUPPORTED_SERVICES: - supported_services(data, len); - return; - case BTP_CORE_REGISTER_SERVICE: - register_service(data, len); - return; - case BTP_CORE_UNREGISTER_SERVICE: - unregister_service(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_CORE, opcode, BTP_INDEX_NONE, - BTP_STATUS_UNKNOWN_CMD); - return; - } + tester_register_command_handlers(BTP_SERVICE_ID_CORE, handlers, + ARRAY_SIZE(handlers)); + atomic_set_bit(registered_services, BTP_SERVICE_ID_CORE); } diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index 24f7f82dcc..66b88a6802 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -85,9 +85,6 @@ static const struct ble_gap_conn_params dflt_conn_params = { .max_ce_len = 0x0300, }; -static void -conn_param_update(struct os_event *ev); - static int gap_conn_find_by_addr(const ble_addr_t *dev_addr, struct ble_gap_conn_desc *out_desc) @@ -121,66 +118,70 @@ gap_conn_find_by_addr(const ble_addr_t *dev_addr, static int gap_event_cb(struct ble_gap_event *event, void *arg); -static void -supported_commands(uint8_t *data, uint16_t len) +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t cmds[3]; - struct btp_gap_read_supported_commands_rp *rp = (void *) &cmds; + struct btp_gap_read_supported_commands_rp *rp = rsp; + + /* octet 0 */ + tester_set_bit(rp->data, BTP_GAP_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_GAP_READ_CONTROLLER_INDEX_LIST); + tester_set_bit(rp->data, BTP_GAP_READ_CONTROLLER_INFO); + tester_set_bit(rp->data, BTP_GAP_SET_CONNECTABLE); + + /* octet 1 */ + tester_set_bit(rp->data, BTP_GAP_SET_DISCOVERABLE); + tester_set_bit(rp->data, BTP_GAP_SET_BONDABLE); + tester_set_bit(rp->data, BTP_GAP_START_ADVERTISING); + tester_set_bit(rp->data, BTP_GAP_STOP_ADVERTISING); + tester_set_bit(rp->data, BTP_GAP_START_DISCOVERY); + tester_set_bit(rp->data, BTP_GAP_STOP_DISCOVERY); + tester_set_bit(rp->data, BTP_GAP_CONNECT); + tester_set_bit(rp->data, BTP_GAP_DISCONNECT); + + /* octet 2 */ + tester_set_bit(rp->data, BTP_GAP_SET_IO_CAP); + tester_set_bit(rp->data, BTP_GAP_PAIR); + tester_set_bit(rp->data, BTP_GAP_UNPAIR); + tester_set_bit(rp->data, BTP_GAP_PASSKEY_ENTRY); + tester_set_bit(rp->data, BTP_GAP_PASSKEY_CONFIRM); + tester_set_bit(rp->data, BTP_GAP_START_DIRECT_ADV); + tester_set_bit(rp->data, BTP_GAP_CONN_PARAM_UPDATE); + + /* octet 3 */ + tester_set_bit(rp->data, BTP_GAP_OOB_LEGACY_SET_DATA); + tester_set_bit(rp->data, BTP_GAP_OOB_SC_GET_LOCAL_DATA); + tester_set_bit(rp->data, BTP_GAP_OOB_SC_SET_REMOTE_DATA); + tester_set_bit(rp->data, BTP_GAP_SET_MITM); + + *rsp_len = sizeof(*rp) + 4; - SYS_LOG_DBG(""); - - memset(cmds, 0, sizeof(cmds)); - - tester_set_bit(cmds, BTP_GAP_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, BTP_GAP_READ_CONTROLLER_INDEX_LIST); - tester_set_bit(cmds, BTP_GAP_READ_CONTROLLER_INFO); - tester_set_bit(cmds, BTP_GAP_SET_CONNECTABLE); - tester_set_bit(cmds, BTP_GAP_SET_DISCOVERABLE); - tester_set_bit(cmds, BTP_GAP_SET_BONDABLE); - tester_set_bit(cmds, BTP_GAP_START_ADVERTISING); - tester_set_bit(cmds, BTP_GAP_STOP_ADVERTISING); - tester_set_bit(cmds, BTP_GAP_START_DISCOVERY); - tester_set_bit(cmds, BTP_GAP_STOP_DISCOVERY); - tester_set_bit(cmds, BTP_GAP_CONNECT); - tester_set_bit(cmds, BTP_GAP_DISCONNECT); - tester_set_bit(cmds, BTP_GAP_SET_IO_CAP); - tester_set_bit(cmds, BTP_GAP_PAIR); - tester_set_bit(cmds, BTP_GAP_UNPAIR); - tester_set_bit(cmds, BTP_GAP_PASSKEY_ENTRY); - tester_set_bit(cmds, BTP_GAP_PASSKEY_CONFIRM); - tester_set_bit(cmds, BTP_GAP_START_DIRECT_ADV); - tester_set_bit(cmds, BTP_GAP_CONN_PARAM_UPDATE); - tester_set_bit(cmds, BTP_GAP_OOB_LEGACY_SET_DATA); - tester_set_bit(cmds, BTP_GAP_OOB_SC_GET_LOCAL_DATA); - tester_set_bit(cmds, BTP_GAP_OOB_SC_SET_REMOTE_DATA); - tester_set_bit(cmds, BTP_GAP_SET_MITM); - - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); + return BTP_STATUS_SUCCESS; } -static void -controller_index_list(uint8_t *data, uint16_t len) +static uint8_t +controller_index_list(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_gap_read_controller_index_list_rp *rp; - uint8_t buf[sizeof(*rp) + 1]; + struct btp_gap_read_controller_index_list_rp *rp = rsp; SYS_LOG_DBG(""); - rp = (void *) buf; - rp->num = 1; rp->index[0] = CONTROLLER_INDEX; - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_CONTROLLER_INDEX_LIST, - BTP_INDEX_NONE, (uint8_t *) rp, sizeof(buf)); + *rsp_len = sizeof(*rp) + 1; + + return BTP_STATUS_SUCCESS; } -static void -controller_info(uint8_t *data, uint16_t len) +static uint8_t +controller_info(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_gap_read_controller_info_rp rp; - uint32_t supported_settings = 0; + struct btp_gap_read_controller_info_rp *rp = rsp; + uint32_t supported_settings = 0; ble_addr_t addr; int rc; @@ -189,8 +190,6 @@ controller_info(uint8_t *data, uint16_t len) rc = ble_hs_pvcy_set_our_irk(irk); assert(rc == 0); - memset(&rp, 0, sizeof(rp)); - /* Make sure we have proper identity address set (public preferred) */ rc = ble_hs_util_ensure_addr(1); assert(rc == 0); @@ -209,12 +208,12 @@ controller_info(uint8_t *data, uint16_t len) } current_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); supported_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); - memcpy(rp.address, addr.val, sizeof(rp.address)); + memcpy(rp->address, addr.val, sizeof(rp->address)); } else { - rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, rp.address, NULL); + rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, rp->address, NULL); if (rc) { own_addr_type = BLE_OWN_ADDR_RANDOM; - memcpy(rp.address, addr.val, sizeof(rp.address)); + memcpy(rp->address, addr.val, sizeof(rp->address)); supported_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS); current_settings |= BIT(BTP_GAP_SETTINGS_STATIC_ADDRESS); } else { @@ -236,13 +235,14 @@ controller_info(uint8_t *data, uint16_t len) current_settings |= BIT(BTP_GAP_SETTINGS_SC); } - rp.supported_settings = sys_cpu_to_le32(supported_settings); - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->supported_settings = sys_cpu_to_le32(supported_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); + + memcpy(rp->name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME)); - memcpy(rp.name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME)); + *rsp_len = sizeof(*rp); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_READ_CONTROLLER_INFO, - CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); + return BTP_STATUS_SUCCESS; } static struct ble_gap_adv_params adv_params = { @@ -282,15 +282,16 @@ static void rotate_nrpa_cb(struct os_event *ev) } #endif -static void -set_connectable(uint8_t *data, uint16_t len) +static uint8_t +set_connectable(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_set_connectable_cmd *cmd = (void *) data; - struct btp_gap_set_connectable_rp rp; + const struct btp_gap_set_connectable_cmd *cp = cmd; + struct btp_gap_set_connectable_rp *rp = rsp; SYS_LOG_DBG(""); - if (cmd->connectable) { + if (cp->connectable) { current_settings |= BIT(BTP_GAP_SETTINGS_CONNECTABLE); adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; } else { @@ -298,23 +299,25 @@ set_connectable(uint8_t *data, uint16_t len) adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; } - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); + + *rsp_len = sizeof(*rp); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_CONNECTABLE, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); + return BTP_STATUS_SUCCESS; } static uint8_t ad_flags = BLE_HS_ADV_F_BREDR_UNSUP; -static void -set_discoverable(uint8_t *data, uint16_t len) +static uint8_t +set_discoverable(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_set_discoverable_cmd *cmd = (void *) data; - struct btp_gap_set_discoverable_rp rp; + const struct btp_gap_set_discoverable_cmd *cp = cmd; + struct btp_gap_set_discoverable_rp *rp = rsp; SYS_LOG_DBG(""); - switch (cmd->discoverable) { + switch (cp->discoverable) { case BTP_GAP_NON_DISCOVERABLE: ad_flags &= ~(BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_DISC_LTD); adv_params.disc_mode = BLE_GAP_DISC_MODE_NON; @@ -333,36 +336,35 @@ set_discoverable(uint8_t *data, uint16_t len) current_settings |= BIT(BTP_GAP_SETTINGS_DISCOVERABLE); break; default: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_DISCOVERABLE, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - return; + return BTP_STATUS_FAILED; } - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_DISCOVERABLE, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); + *rsp_len = sizeof(*rp); + + return BTP_STATUS_SUCCESS; } -static void -set_bondable(const uint8_t *data, uint16_t len) +static uint8_t +set_bondable(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_set_bondable_cmd *cmd = (void *) data; - struct btp_gap_set_bondable_rp rp; + const struct btp_gap_set_bondable_cmd *cp = cmd; + struct btp_gap_set_bondable_rp *rp = rsp; - SYS_LOG_DBG(""); + SYS_LOG_DBG("bondable: %d", cp->bondable); - ble_hs_cfg.sm_bonding = cmd->bondable; + ble_hs_cfg.sm_bonding = cp->bondable; if (ble_hs_cfg.sm_bonding) { current_settings |= BIT(BTP_GAP_SETTINGS_BONDABLE); } else { current_settings &= ~BIT(BTP_GAP_SETTINGS_BONDABLE); } - rp.current_settings = sys_cpu_to_le32(current_settings); - - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_SET_BONDABLE, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); + rp->current_settings = sys_cpu_to_le32(current_settings); + *rsp_len = sizeof(*rp); + return BTP_STATUS_SUCCESS; } static struct bt_data ad[10] = { @@ -371,70 +373,91 @@ static struct bt_data ad[10] = { static struct bt_data sd[10]; static int -set_ad(const struct bt_data *ad, size_t ad_len, +set_ad(const struct bt_data *ad_data, size_t ad_len, uint8_t *buf, uint8_t *buf_len) { int i; for (i = 0; i < ad_len; i++) { - buf[(*buf_len)++] = ad[i].data_len + 1; - buf[(*buf_len)++] = ad[i].type; + buf[(*buf_len)++] = ad_data[i].data_len + 1; + buf[(*buf_len)++] = ad_data[i].type; - memcpy(&buf[*buf_len], ad[i].data, - ad[i].data_len); - *buf_len += ad[i].data_len; + memcpy(&buf[*buf_len], ad_data[i].data, + ad_data[i].data_len); + *buf_len += ad_data[i].data_len; } return 0; } -static void -start_advertising(const uint8_t *data, uint16_t len) +static uint8_t +start_advertising(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_start_advertising_cmd *cmd = (void *) data; - struct btp_gap_start_advertising_rp rp; - int32_t duration_ms = BLE_HS_FOREVER; + const struct btp_gap_start_advertising_cmd *cp = cmd; + struct btp_gap_start_advertising_rp *rp = rsp; + int32_t duration_ms = BLE_HS_FOREVER; uint8_t buf[BLE_HS_ADV_MAX_SZ]; uint8_t buf_len = 0; uint8_t adv_len, sd_len; + uint8_t addr_type; + uint32_t duration; + int err; int i; SYS_LOG_DBG(""); - for (i = 0, adv_len = 1; i < cmd->adv_data_len; adv_len++) { + /* This command is very unfortunate since after variable data there is + * additional 5 bytes (4 bytes for duration, 1 byte for own address + * type. + */ + if ((cmd_len < sizeof(*cp)) || + (cmd_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len + + sizeof(duration) + sizeof(own_addr_type))) { + return BTP_STATUS_FAILED; + } + + /* currently ignored */ + duration = get_le32(cp->adv_data + cp->adv_data_len + cp->scan_rsp_len); + (void)duration; + addr_type = cp->adv_data[cp->adv_data_len + + cp->scan_rsp_len + + sizeof(duration)]; + + for (i = 0, adv_len = 1U; i < cp->adv_data_len; adv_len++) { if (adv_len >= ARRAY_SIZE(ad)) { SYS_LOG_ERR("ad[] Out of memory"); - goto fail; + return BTP_STATUS_FAILED; } - ad[adv_len].type = cmd->adv_data[i++]; - ad[adv_len].data_len = cmd->adv_data[i++]; - ad[adv_len].data = &cmd->adv_data[i]; + ad[adv_len].type = cp->scan_rsp[i++]; + ad[adv_len].data_len = cp->scan_rsp[i++]; + ad[adv_len].data = &cp->scan_rsp[i]; i += ad[adv_len].data_len; } - for (i = 0, sd_len = 0; i < cmd->scan_rsp_len; sd_len++) { + for (sd_len = 0U; i < cp->scan_rsp_len; sd_len++) { if (sd_len >= ARRAY_SIZE(sd)) { SYS_LOG_ERR("sd[] Out of memory"); - goto fail; + return BTP_STATUS_FAILED; } - sd[sd_len].type = cmd->scan_rsp[i++]; - sd[sd_len].data_len = cmd->scan_rsp[i++]; - sd[sd_len].data = &cmd->scan_rsp[i]; + sd[sd_len].type = cp->scan_rsp[i++]; + sd[sd_len].data_len = cp->scan_rsp[i++]; + sd[sd_len].data = &cp->scan_rsp[i]; i += sd[sd_len].data_len; } err = set_ad(ad, adv_len, buf, &buf_len); if (err) { - goto fail; + return BTP_STATUS_FAILED; } err = ble_gap_adv_set_data(buf, buf_len); if (err != 0) { - goto fail; + return BTP_STATUS_FAILED; } if (sd_len) { @@ -443,13 +466,13 @@ start_advertising(const uint8_t *data, uint16_t len) err = set_ad(sd, sd_len, buf, &buf_len); if (err) { SYS_LOG_ERR("Advertising failed: err %d", err); - goto fail; + return BTP_STATUS_FAILED; } err = ble_gap_adv_rsp_set_data(buf, buf_len); if (err != 0) { SYS_LOG_ERR("Advertising failed: err %d", err); - goto fail; + return BTP_STATUS_FAILED; } } @@ -457,6 +480,30 @@ start_advertising(const uint8_t *data, uint16_t len) duration_ms = MYNEWT_VAL(BTTESTER_LTD_ADV_TIMEOUT); } + /* In NimBLE, own_addr_type is configured in `controller_info` function. + * Let's just verify restrictions for Privacy options. + */ + switch (addr_type) { + case 0x00: + break; +#if defined(CONFIG_BT_PRIVACY) + case 0x01: + /* RPA usage is is controlled via privacy settings */ + if (!atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_PRIVACY)) { + return BTP_STATUS_FAILED; + } + break; + case 0x02: + /* NRPA is used only for non-connectable advertising */ + if (atomic_test_bit(¤t_settings, BTP_GAP_SETTINGS_CONNECTABLE)) { + return BTP_STATUS_FAILED; + } + break; +#endif + default: + return BTP_STATUS_FAILED; + } + #if MYNEWT_VAL(BTTESTER_PRIVACY_MODE) && MYNEWT_VAL(BTTESTER_USE_NRPA) if (MYNEWT_VAL(BTTESTER_NRPA_TIMEOUT) < duration_ms / 1000) { advertising_start = os_get_uptime_usec(); @@ -468,38 +515,37 @@ start_advertising(const uint8_t *data, uint16_t len) &adv_params, gap_event_cb, NULL); if (err) { SYS_LOG_ERR("Advertising failed: err %d", err); - goto fail; + return BTP_STATUS_FAILED; } current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_START_ADVERTISING, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); - return; -fail: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_ADVERTISING, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + *rsp_len = sizeof(*rp); + + return BTP_STATUS_SUCCESS; } -static void -stop_advertising(const uint8_t *data, uint16_t len) +static uint8_t +stop_advertising(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_gap_stop_advertising_rp rp; + struct btp_gap_stop_advertising_rp *rp = rsp; + int err; SYS_LOG_DBG(""); - if (ble_gap_adv_stop() != 0) { - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_ADVERTISING, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - return; + err = ble_gap_adv_stop(); + if (err != 0) { + return BTP_STATUS_FAILED; } current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); + + *rsp_len = sizeof(*rp); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_ADVERTISING, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); + return BTP_STATUS_SUCCESS; } static uint8_t @@ -557,7 +603,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, const uint8_t *data, uint8_t len) { struct btp_gap_device_found_ev *ev; - ble_addr_t a; + ble_addr_t a; /* if General/Limited Discovery - parse Advertising data to get flags */ if (!(discovery_flags & BTP_GAP_DISCOVERY_FLAG_LE_OBSERVE) && @@ -614,8 +660,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, */ if (adv_buf->om_len) { tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, - CONTROLLER_INDEX, adv_buf->om_data, - adv_buf->om_len); + adv_buf->om_data, adv_buf->om_len); } store_adv(addr, rssi, data, len); @@ -629,7 +674,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, } done: tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, - CONTROLLER_INDEX, adv_buf->om_data, adv_buf->om_len); + adv_buf->om_data, adv_buf->om_len); } static int @@ -644,53 +689,46 @@ discovery_cb(struct ble_gap_event *event, void *arg) return 0; } -static void -start_discovery(const uint8_t *data, uint16_t len) +static uint8_t +start_discovery(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_start_discovery_cmd *cmd = (void *) data; - struct ble_gap_disc_params params = {0}; - uint8_t status; + const struct btp_gap_start_discovery_cmd *cp = cmd; + struct ble_gap_disc_params params = {0}; SYS_LOG_DBG(""); /* only LE scan is supported */ - if (cmd->flags & BTP_GAP_DISCOVERY_FLAG_BREDR) { - status = BTP_STATUS_FAILED; - goto reply; + if (cp->flags & BTP_GAP_DISCOVERY_FLAG_BREDR) { + return BTP_STATUS_FAILED; } - params.passive = (cmd->flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) == 0; - params.limited = (cmd->flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) > 0; + params.passive = (cp->flags & BTP_GAP_DISCOVERY_FLAG_LE_ACTIVE_SCAN) == 0; + params.limited = (cp->flags & BTP_GAP_DISCOVERY_FLAG_LIMITED) > 0; params.filter_duplicates = 1; if (ble_gap_disc(own_addr_type, BLE_HS_FOREVER, ¶ms, discovery_cb, NULL) != 0) { - status = BTP_STATUS_FAILED; - goto reply; + return BTP_STATUS_FAILED; } net_buf_simple_init(adv_buf, 0); - discovery_flags = cmd->flags; + discovery_flags = cp->flags; - status = BTP_STATUS_SUCCESS; -reply: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_DISCOVERY, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } -static void -stop_discovery(const uint8_t *data, uint16_t len) +static uint8_t +stop_discovery(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t status = BTP_STATUS_SUCCESS; - SYS_LOG_DBG(""); if (ble_gap_disc_cancel() != 0) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_STOP_DISCOVERY, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } /* Bluetooth Core Spec v5.1 | Section 10.7.1 @@ -729,12 +767,12 @@ device_connected_ev_send(struct os_event *ev) rc = gap_conn_find_by_addr((ble_addr_t *) &connected_ev, &desc); if (rc) { tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); return; } tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, - CONTROLLER_INDEX, (uint8_t *) &connected_ev, + (uint8_t *) &connected_ev, sizeof(connected_ev)); periph_privacy(desc); @@ -775,7 +813,7 @@ le_connected(uint16_t conn_handle, int status) CONNECTED_EV_DELAY_MS(desc.conn_itvl))); #else tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, - CONTROLLER_INDEX, (uint8_t *) &connected_ev, + (uint8_t *) &connected_ev, sizeof(connected_ev)); #endif } @@ -784,7 +822,7 @@ static void le_disconnected(struct ble_gap_conn_desc *conn, int reason) { struct btp_gap_device_disconnected_ev ev; - ble_addr_t *addr = &conn->peer_ota_addr; + ble_addr_t *addr = &conn->peer_ota_addr; SYS_LOG_DBG(""); @@ -819,7 +857,7 @@ le_disconnected(struct ble_gap_conn_desc *conn, int reason) ev.address_type = addr->type; tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -846,9 +884,9 @@ auth_passkey_oob(uint16_t conn_handle) static void auth_passkey_display(uint16_t conn_handle, unsigned int passkey) { - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct btp_gap_passkey_display_ev ev; - ble_addr_t *addr; + ble_addr_t *addr; struct ble_sm_io pk; int rc; @@ -875,15 +913,15 @@ auth_passkey_display(uint16_t conn_handle, unsigned int passkey) ev.passkey = sys_cpu_to_le32(pk.passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void auth_passkey_entry(uint16_t conn_handle) { - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct btp_gap_passkey_entry_req_ev ev; - ble_addr_t *addr; + ble_addr_t *addr; int rc; SYS_LOG_DBG(""); @@ -899,15 +937,15 @@ auth_passkey_entry(uint16_t conn_handle) ev.address_type = addr->type; tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) { - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct btp_gap_passkey_confirm_req_ev ev; - ble_addr_t *addr; + ble_addr_t *addr; int rc; SYS_LOG_DBG(""); @@ -924,7 +962,7 @@ auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) ev.passkey = sys_cpu_to_le32(passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -984,9 +1022,9 @@ le_passkey_action(uint16_t conn_handle, static void le_identity_resolved(uint16_t conn_handle) { - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct btp_gap_identity_resolved_ev ev; - int rc; + int rc; SYS_LOG_DBG(""); @@ -1006,15 +1044,15 @@ le_identity_resolved(uint16_t conn_handle) sizeof(ev.identity_address)); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void le_pairing_failed(uint16_t conn_handle, int reason) { - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct btp_gap_sec_pairing_failed_ev ev; - int rc; + int rc; SYS_LOG_DBG(""); @@ -1032,7 +1070,7 @@ le_pairing_failed(uint16_t conn_handle, int reason) ev.reason = reason; tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_PAIRING_FAILED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1050,7 +1088,7 @@ le_conn_param_update(struct ble_gap_conn_desc *desc) ev.supervision_timeout = desc->supervision_timeout; tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_CONN_PARAM_UPDATE, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1079,14 +1117,14 @@ le_encryption_changed(struct ble_gap_conn_desc *desc) } tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_LEVEL_CHANGED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void bond_lost(uint16_t conn_handle) { struct btp_gap_bond_lost_ev ev; - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; int rc; rc = ble_gap_conn_find(conn_handle, &desc); @@ -1096,7 +1134,6 @@ bond_lost(uint16_t conn_handle) ev.address_type = desc.peer_id_addr.type; tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); } @@ -1171,7 +1208,7 @@ adv_complete(void) current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); ev.current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, (uint8_t *) &ev, sizeof(ev)); } @@ -1355,11 +1392,13 @@ gap_event_cb(struct ble_gap_event *event, void *arg) return 0; } -static void -connect(const uint8_t *data, uint16_t len) +static uint8_t +connect(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t status = BTP_STATUS_SUCCESS; - ble_addr_t *addr = (ble_addr_t *) data; + const struct btp_gap_connect_cmd *cp = cmd; + + ble_addr_t *addr = (ble_addr_t *)&cp->address_type; SYS_LOG_DBG(""); @@ -1369,47 +1408,43 @@ connect(const uint8_t *data, uint16_t len) if (ble_gap_connect(own_addr_type, addr, 0, &dflt_conn_params, gap_event_cb, NULL)) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_CONNECT, CONTROLLER_INDEX, status); + return BTP_STATUS_SUCCESS; } -static void -disconnect(const uint8_t *data, uint16_t len) +static uint8_t +disconnect(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gap_disconnect_cmd *cp = cmd; struct ble_gap_conn_desc desc; - uint8_t status; int rc; SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *) data, &desc); + rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } if (ble_gap_terminate(desc.conn_handle, BLE_ERR_REM_USER_CONN_TERM)) { - status = BTP_STATUS_FAILED; - } else { - status = BTP_STATUS_SUCCESS; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_DISCONNECT, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } -static void -set_io_cap(const uint8_t *data, uint16_t len) +static uint8_t +set_io_cap(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_set_io_cap_cmd *cmd = (void *) data; - uint8_t status; + const struct btp_gap_set_io_cap_cmd *cp = cmd; SYS_LOG_DBG(""); - switch (cmd->io_cap) { + switch (cp->io_cap) { case BTP_GAP_IO_CAP_DISPLAY_ONLY: ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; ble_hs_cfg.sm_mitm = 1; @@ -1431,155 +1466,132 @@ set_io_cap(const uint8_t *data, uint16_t len) ble_hs_cfg.sm_mitm = 1; break; default: - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_IO_CAP, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } -static void -pair(const uint8_t *data, uint16_t len) +static uint8_t +pair(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gap_pair_cmd *cp = cmd; struct ble_gap_conn_desc desc; - uint8_t status; int rc; SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *) data, &desc); + rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } rc = ble_gap_security_initiate(desc.conn_handle); if (rc != 0 && rc != BLE_HS_EALREADY) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PAIR, CONTROLLER_INDEX, status); + return BTP_STATUS_SUCCESS; } -static void -unpair(const uint8_t *data, uint16_t len) +static uint8_t +unpair(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t status; + const struct btp_gap_unpair_cmd *cp = cmd; int err; SYS_LOG_DBG(""); - err = ble_gap_unpair((ble_addr_t *) data); - status = (uint8_t) (err != 0 ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_UNPAIR, CONTROLLER_INDEX, status); + err = ble_gap_unpair((ble_addr_t *)&cp->address_type); + return err != 0 ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS; } -static void -passkey_entry(const uint8_t *data, uint16_t len) +static uint8_t +passkey_entry(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_passkey_entry_cmd *cmd = (void *) data; - struct ble_gap_conn_desc desc; + const struct btp_gap_passkey_entry_cmd *cp = cmd; + struct ble_gap_conn_desc desc; struct ble_sm_io pk; - uint8_t status; int rc; SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *) data, &desc); + rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } pk.action = BLE_SM_IOACT_INPUT; - pk.passkey = sys_le32_to_cpu(cmd->passkey); + pk.passkey = sys_le32_to_cpu(cp->passkey); rc = ble_sm_inject_io(desc.conn_handle, &pk); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PASSKEY_ENTRY, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } -static void -passkey_confirm(const uint8_t *data, uint16_t len) +static uint8_t +passkey_confirm(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_passkey_confirm_cmd *cmd = (void *) data; - struct ble_gap_conn_desc desc; + const struct btp_gap_passkey_confirm_cmd *cp = cmd; + struct ble_gap_conn_desc desc; struct ble_sm_io pk; - uint8_t status; int rc; SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *) data, &desc); + rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } pk.action = BLE_SM_IOACT_NUMCMP; - pk.numcmp_accept = cmd->match; + pk.numcmp_accept = cp->match; rc = ble_sm_inject_io(desc.conn_handle, &pk); if (rc) { console_printf("sm inject io failed"); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_PASSKEY_CONFIRM, CONTROLLER_INDEX, - status); + return BTP_STATUS_SUCCESS; } -static void -start_direct_adv(const uint8_t *data, uint16_t len) +static uint8_t +start_direct_adv(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_start_direct_adv_cmd *cmd = (void *) data; - struct btp_gap_start_advertising_rp rp; - static struct ble_gap_adv_params adv_params = { + const struct btp_gap_start_direct_adv_cmd *cp = cmd; + struct btp_gap_start_advertising_rp *rp = rsp; + static struct ble_gap_adv_params adv_params = { .conn_mode = BLE_GAP_CONN_MODE_DIR, }; int err; SYS_LOG_DBG(""); - adv_params.high_duty_cycle = cmd->high_duty; + adv_params.high_duty_cycle = cp->high_duty; - err = ble_gap_adv_start(own_addr_type, (ble_addr_t *) data, + err = ble_gap_adv_start(own_addr_type, (ble_addr_t *)&cp->address_type, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); if (err) { SYS_LOG_ERR("Advertising failed: err %d", err); - goto fail; + return BTP_STATUS_FAILED; } current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp.current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = sys_cpu_to_le32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_START_DIRECT_ADV, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); - return; -fail: - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_START_DIRECT_ADV, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + *rsp_len = sizeof(*rp); + + return BTP_STATUS_SUCCESS; } static void @@ -1665,72 +1677,74 @@ conn_param_update(struct os_event *ev) SYS_LOG_ERR("Conn param update fail; rc=%d", rc); } -static void -conn_param_update_async(const uint8_t *data, uint16_t len) +static uint8_t +conn_param_update_async(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_conn_param_update_cmd *cmd = (void *) data; - update_params = *cmd; + const struct btp_gap_conn_param_update_cmd *cp = cmd; + update_params = *cp; os_callout_reset(&update_params_co, 0); - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_CONN_PARAM_UPDATE, CONTROLLER_INDEX, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -oob_legacy_set_data(const uint8_t *data, uint16_t len) +static uint8_t +set_oob_legacy_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_oob_legacy_set_data_cmd *cmd = (void *) data; + const struct btp_gap_oob_legacy_set_data_cmd *cp = cmd; ble_hs_cfg.sm_oob_data_flag = 1; - memcpy(oob, cmd->oob_data, sizeof(oob)); + memcpy(oob, cp->oob_data, sizeof(oob)); - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_LEGACY_SET_DATA, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -oob_sc_get_local_data(const uint8_t *data, uint16_t len) +static uint8_t +get_oob_sc_local_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_gap_oob_sc_get_local_data_rp rp; + struct btp_gap_oob_sc_get_local_data_rp *rp = rsp; + + memcpy(rp->r, oob_data_local.r, 16); + memcpy(rp->c, oob_data_local.c, 16); - memcpy(rp.r, oob_data_local.r, 16); - memcpy(rp.c, oob_data_local.c, 16); + *rsp_len = sizeof(*rp); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_SC_GET_LOCAL_DATA, - CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); + return BTP_STATUS_SUCCESS; } -static void -oob_sc_set_remote_data(const uint8_t *data, uint16_t len) +static uint8_t +set_oob_sc_remote_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_oob_sc_set_remote_data_cmd *cmd = (void *) data; + const struct btp_gap_oob_sc_set_remote_data_cmd *cp = cmd; ble_hs_cfg.sm_oob_data_flag = 1; - memcpy(oob_data_remote.r, cmd->r, 16); - memcpy(oob_data_remote.c, cmd->c, 16); + memcpy(oob_data_remote.r, cp->r, 16); + memcpy(oob_data_remote.c, cp->c, 16); - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_OOB_SC_SET_REMOTE_DATA, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -set_mitm(const uint8_t *data, uint16_t len) +static uint8_t +set_mitm(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gap_set_mitm_cmd *cmd = (void *) data; + const struct btp_gap_set_mitm_cmd *cp = cmd; - ble_hs_cfg.sm_mitm = cmd->mitm; + ble_hs_cfg.sm_mitm = cp->mitm; - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_MITM, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -set_filter_accept_list(const uint8_t *data, uint16_t len) +static uint8_t +set_filter_accept_list(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t status = BTP_STATUS_SUCCESS; - struct btp_gap_set_filter_accept_list_cmd *tmp = - (struct btp_gap_set_filter_accept_list_cmd *) data; + const struct btp_gap_set_filter_accept_list_cmd *cp = cmd; + int err; SYS_LOG_DBG(""); @@ -1738,126 +1752,147 @@ set_filter_accept_list(const uint8_t *data, uint16_t len) * Check if the nb of bytes received matches the len of addrs list. * Then set the filter accept list. */ - if (((len - sizeof(tmp->list_len)) / sizeof(ble_addr_t) != - tmp->list_len) || ble_gap_wl_set(tmp->addrs, tmp->list_len)) { - status = BTP_STATUS_FAILED; + if ((cmd_len < sizeof(*cp)) || + (cmd_len != sizeof(*cp) + (cp->list_len * sizeof(cp->addrs[0])))) { + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_GAP, BTP_GAP_SET_FILTER_ACCEPT_LIST, - CONTROLLER_INDEX, status); -} - -void -tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len) -{ - switch (opcode) { - case BTP_GAP_READ_SUPPORTED_COMMANDS: - case BTP_GAP_READ_CONTROLLER_INDEX_LIST: - if (index != BTP_INDEX_NONE) { - tester_rsp(BTP_SERVICE_ID_GAP, opcode, index, - BTP_STATUS_FAILED); - return; - } - break; - default: - if (index != CONTROLLER_INDEX) { - tester_rsp(BTP_SERVICE_ID_GAP, opcode, index, - BTP_STATUS_FAILED); - return; - } - break; + err = ble_gap_wl_set(cp->addrs, cp->list_len); + if (err != 0) { + return BTP_STATUS_FAILED; } - switch (opcode) { - case BTP_GAP_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case BTP_GAP_READ_CONTROLLER_INDEX_LIST: - controller_index_list(data, len); - return; - case BTP_GAP_READ_CONTROLLER_INFO: - controller_info(data, len); - return; - case BTP_GAP_SET_CONNECTABLE: - set_connectable(data, len); - return; - case BTP_GAP_SET_DISCOVERABLE: - set_discoverable(data, len); - return; - case BTP_GAP_SET_BONDABLE: - set_bondable(data, len); - return; - case BTP_GAP_START_ADVERTISING: - start_advertising(data, len); - return; - case BTP_GAP_STOP_ADVERTISING: - stop_advertising(data, len); - return; - case BTP_GAP_START_DISCOVERY: - start_discovery(data, len); - return; - case BTP_GAP_STOP_DISCOVERY: - stop_discovery(data, len); - return; - case BTP_GAP_CONNECT: - connect(data, len); - return; - case BTP_GAP_DISCONNECT: - disconnect(data, len); - return; - case BTP_GAP_SET_IO_CAP: - set_io_cap(data, len); - return; - case BTP_GAP_PAIR: - pair(data, len); - return; - case BTP_GAP_UNPAIR: - unpair(data, len); - return; - case BTP_GAP_PASSKEY_ENTRY: - passkey_entry(data, len); - return; - case BTP_GAP_PASSKEY_CONFIRM: - passkey_confirm(data, len); - return; - case BTP_GAP_START_DIRECT_ADV: - start_direct_adv(data, len); - return; - case BTP_GAP_CONN_PARAM_UPDATE: - conn_param_update_async(data, len); - return; - case BTP_GAP_OOB_LEGACY_SET_DATA: - oob_legacy_set_data(data, len); - return; - case BTP_GAP_OOB_SC_GET_LOCAL_DATA: - oob_sc_get_local_data(data, len); - return; - case BTP_GAP_OOB_SC_SET_REMOTE_DATA: - oob_sc_set_remote_data(data, len); - return; - case BTP_GAP_SET_MITM: - set_mitm(data, len); - return; - case BTP_GAP_SET_FILTER_ACCEPT_LIST: - set_filter_accept_list(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_GAP, opcode, index, - BTP_STATUS_UNKNOWN_CMD); - return; - } + return BTP_STATUS_SUCCESS; } +static const struct btp_handler handlers[] = { + { + .opcode = BTP_GAP_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_GAP_READ_CONTROLLER_INDEX_LIST, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = controller_index_list, + }, + { + .opcode = BTP_GAP_READ_CONTROLLER_INFO, + .expect_len = 0, + .func = controller_info, + }, + { + .opcode = BTP_GAP_SET_CONNECTABLE, + .expect_len = sizeof(struct btp_gap_set_connectable_cmd), + .func = set_connectable, + }, + { + .opcode = BTP_GAP_SET_DISCOVERABLE, + .expect_len = sizeof(struct btp_gap_set_discoverable_cmd), + .func = set_discoverable, + }, + { + .opcode = BTP_GAP_SET_BONDABLE, + .expect_len = sizeof(struct btp_gap_set_bondable_cmd), + .func = set_bondable, + }, + { + .opcode = BTP_GAP_START_ADVERTISING, + .expect_len = -1, + .func = start_advertising, + }, + { + .opcode = BTP_GAP_START_DIRECT_ADV, + .expect_len = sizeof(struct btp_gap_start_direct_adv_cmd), + .func = start_direct_adv, + }, + { + .opcode = BTP_GAP_STOP_ADVERTISING, + .expect_len = 0, + .func = stop_advertising, + }, + { + .opcode = BTP_GAP_START_DISCOVERY, + .expect_len = sizeof(struct btp_gap_start_discovery_cmd), + .func = start_discovery, + }, + { + .opcode = BTP_GAP_STOP_DISCOVERY, + .expect_len = 0, + .func = stop_discovery, + }, + { + .opcode = BTP_GAP_CONNECT, + .expect_len = sizeof(struct btp_gap_connect_cmd), + .func = connect, + }, + { + .opcode = BTP_GAP_DISCONNECT, + .expect_len = sizeof(struct btp_gap_disconnect_cmd), + .func = disconnect, + }, + { + .opcode = BTP_GAP_SET_IO_CAP, + .expect_len = sizeof(struct btp_gap_set_io_cap_cmd), + .func = set_io_cap, + }, + { + .opcode = BTP_GAP_PAIR, + .expect_len = sizeof(struct btp_gap_pair_cmd), + .func = pair, + }, + { + .opcode = BTP_GAP_UNPAIR, + .expect_len = sizeof(struct btp_gap_unpair_cmd), + .func = unpair, + }, + { + .opcode = BTP_GAP_PASSKEY_ENTRY, + .expect_len = sizeof(struct btp_gap_passkey_entry_cmd), + .func = passkey_entry, + }, + { + .opcode = BTP_GAP_PASSKEY_CONFIRM, + .expect_len = sizeof(struct btp_gap_passkey_confirm_cmd), + .func = passkey_confirm, + }, + { + .opcode = BTP_GAP_CONN_PARAM_UPDATE, + .expect_len = sizeof(struct btp_gap_conn_param_update_cmd), + .func = conn_param_update_async, + }, + { + .opcode = BTP_GAP_OOB_LEGACY_SET_DATA, + .expect_len = sizeof(struct btp_gap_oob_legacy_set_data_cmd), + .func = set_oob_legacy_data, + }, + { + .opcode = BTP_GAP_OOB_SC_GET_LOCAL_DATA, + .expect_len = 0, + .func = get_oob_sc_local_data, + }, + { + .opcode = BTP_GAP_OOB_SC_SET_REMOTE_DATA, + .expect_len = sizeof(struct btp_gap_oob_sc_set_remote_data_cmd), + .func = set_oob_sc_remote_data, + }, + { + .opcode = BTP_GAP_SET_MITM, + .expect_len = sizeof(struct btp_gap_set_mitm_cmd), + .func = set_mitm, + }, + { + .opcode = BTP_GAP_SET_FILTER_ACCEPT_LIST, + .expect_len = -1, + .func = set_filter_accept_list, + }, +}; + static void -tester_init_gap_cb(int err) +tester_init_gap_cb() { - if (err) { - tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, - BTP_INDEX_NONE, BTP_STATUS_FAILED); - return; - } - current_settings = 0; current_settings |= BIT(BTP_GAP_SETTINGS_POWERED); current_settings |= BIT(BTP_GAP_SETTINGS_LE); @@ -1867,9 +1902,6 @@ tester_init_gap_cb(int err) os_callout_init(&connected_ev_co, os_eventq_dflt_get(), device_connected_ev_send, NULL); - - tester_rsp(BTP_SERVICE_ID_CORE, BTP_CORE_REGISTER_SERVICE, BTP_INDEX_NONE, - BTP_STATUS_SUCCESS); } uint8_t @@ -1890,7 +1922,11 @@ tester_init_gap(void) #endif adv_buf = NET_BUF_SIMPLE(ADV_BUF_LEN); - tester_init_gap_cb(BTP_STATUS_SUCCESS); + tester_init_gap_cb(); + + tester_register_command_handlers(BTP_SERVICE_ID_GAP, handlers, + ARRAY_SIZE(handlers)); + return BTP_STATUS_SUCCESS; } diff --git a/apps/bttester/src/btp_gatt.c b/apps/bttester/src/btp_gatt.c index d3b826f53b..5d62a677ad 100644 --- a/apps/bttester/src/btp_gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -256,7 +256,7 @@ static void attr_value_changed_ev(uint16_t handle, struct os_mbuf *data) { struct btp_gatt_attr_value_changed_ev *ev; - struct os_mbuf *buf = os_msys_get(0, 0); + struct os_mbuf *buf = os_msys_get(0, 0); SYS_LOG_DBG(""); @@ -583,7 +583,7 @@ start_server(uint8_t *data, uint16_t len) rp.db_attr_off = 0; rp.db_attr_cnt = 0; - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_START_SERVER, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_START_SERVER, (uint8_t *) &rp, sizeof(rp)); } @@ -672,29 +672,29 @@ read_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; - uint8_t btp_opcode = (uint8_t) (int) arg; + struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; + uint8_t btp_opcode = (uint8_t) (int) arg; SYS_LOG_DBG("status=%d", error->status); if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->att_response = (uint8_t) BLE_HS_ATT_ERR(error->status); - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf + .len); read_destroy(); return 0; } if (!gatt_buf_add(attr->om->om_data, attr->om->om_len)) { tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); read_destroy(); return 0; } rp->data_length += attr->om->om_len; tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + gatt_buf.buf, gatt_buf.len); read_destroy(); return 0; @@ -704,7 +704,7 @@ static void read(uint8_t *data, uint16_t len) { const struct btp_gatt_read_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -730,8 +730,7 @@ read(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, BTP_STATUS_FAILED); } static int @@ -740,29 +739,29 @@ read_long_cb(uint16_t conn_handle, struct ble_gatt_attr *attr, void *arg) { - struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; - uint8_t btp_opcode = (uint8_t) (int) arg; + struct btp_gatt_read_rp *rp = (void *) gatt_buf.buf; + uint8_t btp_opcode = (uint8_t) (int) arg; SYS_LOG_DBG("status=%d", error->status); if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->att_response = (uint8_t) BLE_HS_ATT_ERR(error->status); - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf + .len); read_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf + .len); read_destroy(); return 0; } if (gatt_buf_add(attr->om->om_data, attr->om->om_len) == NULL) { tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); read_destroy(); return BLE_HS_ENOMEM; } @@ -776,7 +775,7 @@ static void read_long(uint8_t *data, uint16_t len) { const struct btp_gatt_read_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -804,15 +803,14 @@ read_long(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_LONG, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_LONG, BTP_STATUS_FAILED); } static void read_multiple(uint8_t *data, uint16_t len) { const struct btp_gatt_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + uint16_t handles[cmd->handles_count]; struct ble_gap_conn_desc conn; int rc, i; @@ -844,15 +842,14 @@ read_multiple(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_MULTIPLE, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_MULTIPLE, BTP_STATUS_FAILED); } static void write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) { const struct btp_gatt_write_without_rsp_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -871,7 +868,7 @@ write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) } rsp: - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GATT, op, status); } static int @@ -885,7 +882,7 @@ write_rsp(uint16_t conn_handle, const struct ble_gatt_error *error, SYS_LOG_DBG(""); tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, &err, sizeof(err)); + &err, sizeof(err)); return 0; } @@ -893,7 +890,7 @@ static void write(uint8_t *data, uint16_t len) { const struct btp_gatt_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); @@ -912,15 +909,14 @@ write(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE, BTP_STATUS_FAILED); } static void write_long(uint8_t *data, uint16_t len) { const struct btp_gatt_write_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; int rc = 0; @@ -949,8 +945,7 @@ write_long(uint8_t *data, uint16_t len) fail: SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, BTP_STATUS_FAILED); } static int @@ -965,7 +960,7 @@ reliable_write_rsp(uint16_t conn_handle, SYS_LOG_DBG("Reliable write status %d", err); tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_RELIABLE_WRITE, - CONTROLLER_INDEX, &err, sizeof(err)); + &err, sizeof(err)); return 0; } @@ -973,7 +968,7 @@ static void reliable_write(uint8_t *data, uint16_t len) { const struct btp_gatt_reliable_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; int rc; @@ -1007,8 +1002,7 @@ reliable_write(uint8_t *data, uint16_t len) fail: os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, BTP_STATUS_FAILED); } static struct bt_gatt_subscribe_params { @@ -1021,7 +1015,7 @@ static void read_uuid(uint8_t *data, uint16_t len) { const struct btp_gatt_read_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; @@ -1054,8 +1048,7 @@ read_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, BTP_STATUS_FAILED); } static int @@ -1064,8 +1057,8 @@ disc_prim_uuid_cb(uint16_t conn_handle, const struct ble_gatt_svc *gatt_svc, void *arg) { struct btp_gatt_disc_prim_uuid_rp *rp = (void *) gatt_buf.buf; - struct btp_gatt_service *service; - const ble_uuid_any_t *uuid; + struct btp_gatt_service *service; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t opcode = (uint8_t) (int) arg; @@ -1073,14 +1066,13 @@ disc_prim_uuid_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { tester_rsp(BTP_SERVICE_ID_GATT, opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, opcode, gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1091,7 +1083,7 @@ disc_prim_uuid_cb(uint16_t conn_handle, service = gatt_buf_reserve(sizeof(*service) + uuid_length); if (!service) { tester_rsp(BTP_SERVICE_ID_GATT, opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; } @@ -1121,22 +1113,22 @@ disc_all_desc_cb(uint16_t conn_handle, void *arg) { struct btp_gatt_disc_all_desc_rp *rp = (void *) gatt_buf.buf; - struct btp_gatt_descriptor *dsc; - const ble_uuid_any_t *uuid; + struct btp_gatt_descriptor *dsc; + const ble_uuid_any_t *uuid; uint8_t uuid_length; SYS_LOG_DBG(""); if (error->status != 0 && error->status != BLE_HS_EDONE) { tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, gatt_buf + .buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1147,7 +1139,7 @@ disc_all_desc_cb(uint16_t conn_handle, dsc = gatt_buf_reserve(sizeof(*dsc) + uuid_length); if (!dsc) { tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; } @@ -1194,14 +1186,14 @@ disc_all_prim_svcs(uint8_t *data, uint16_t len) fail: tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_PRIM_SVCS, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); } static void disc_all_desc(uint8_t *data, uint16_t len) { const struct btp_gatt_disc_all_desc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int rc; @@ -1232,8 +1224,7 @@ disc_all_desc(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, BTP_STATUS_FAILED); } static int @@ -1242,8 +1233,8 @@ find_included_cb(uint16_t conn_handle, const struct ble_gatt_svc *gatt_svc, void *arg) { struct btp_gatt_find_included_rp *rp = (void *) gatt_buf.buf; - struct btp_gatt_included *included; - const ble_uuid_any_t *uuid; + struct btp_gatt_included *included; + const ble_uuid_any_t *uuid; int service_handle = (int) arg; uint8_t uuid_length; @@ -1251,14 +1242,14 @@ find_included_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, gatt_buf + .buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1269,7 +1260,7 @@ find_included_cb(uint16_t conn_handle, included = gatt_buf_reserve(sizeof(*included) + uuid_length); if (!included) { tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; } @@ -1301,7 +1292,7 @@ disc_chrc_cb(uint16_t conn_handle, { struct btp_gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; struct btp_gatt_characteristic *chrc; - const ble_uuid_any_t *uuid; + const ble_uuid_any_t *uuid; uint8_t btp_opcode = (uint8_t) (int) arg; uint8_t uuid_length; @@ -1309,14 +1300,14 @@ disc_chrc_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); + tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf + .len); discover_destroy(); return 0; } @@ -1327,7 +1318,7 @@ disc_chrc_cb(uint16_t conn_handle, chrc = gatt_buf_reserve(sizeof(*chrc) + uuid_length); if (!chrc) { tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); discover_destroy(); return BLE_HS_ENOMEM; } @@ -1354,7 +1345,7 @@ static void disc_chrc_uuid(uint8_t *data, uint16_t len) { const struct btp_gatt_disc_chrc_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; int rc; @@ -1387,7 +1378,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_CHRC_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_CHRC_UUID, BTP_STATUS_FAILED); } @@ -1395,7 +1386,7 @@ static void disc_prim_uuid(uint8_t *data, uint16_t len) { const struct btp_gatt_disc_prim_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; @@ -1424,7 +1415,7 @@ disc_prim_uuid(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_PRIM_UUID, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_PRIM_UUID, BTP_STATUS_FAILED); } @@ -1462,15 +1453,14 @@ disc_all_chrc(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_CHRC, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_CHRC, BTP_STATUS_FAILED); } static void find_included(uint8_t *data, uint16_t len) { const struct btp_gatt_find_included_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; int rc; @@ -1500,8 +1490,7 @@ find_included(uint8_t *data, uint16_t len) return; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, BTP_STATUS_FAILED); } static int @@ -1513,13 +1502,12 @@ exchange_func(uint16_t conn_handle, if (error->status) { tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); return 0; } - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, CONTROLLER_INDEX, - BTP_STATUS_SUCCESS); + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, BTP_STATUS_SUCCESS); return 0; } @@ -1544,7 +1532,7 @@ exchange_mtu(uint8_t *data, uint16_t len) return; fail: tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); } static int @@ -1564,8 +1552,7 @@ enable_subscription(uint16_t conn_handle, uint16_t ccc_handle, subscribe_params.ccc_handle = value; - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, - BTP_STATUS_SUCCESS); + tester_rsp(BTP_SERVICE_ID_GATT, op, BTP_STATUS_SUCCESS); return 0; } @@ -1595,7 +1582,7 @@ static void config_subscription(uint8_t *data, uint16_t len, uint8_t op) { const struct btp_gatt_cfg_notify_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); uint8_t status; int rc; @@ -1604,8 +1591,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); if (rc) { - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, - BTP_STATUS_FAILED); + tester_rsp(BTP_SERVICE_ID_GATT, op, BTP_STATUS_FAILED); return; } @@ -1635,7 +1621,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) SYS_LOG_DBG("Config subscription (op %u) status %u", op, status); - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GATT, op, status); } #define BTP_PERM_F_READ 0x01 @@ -1678,8 +1664,8 @@ get_attrs(uint8_t *data, uint16_t len) { const struct btp_gatt_get_attributes_cmd *cmd = (void *) data; struct btp_gatt_get_attributes_rp *rp; - struct btp_gatt_attr *gatt_attr; - struct os_mbuf *buf = os_msys_get(0, 0); + struct btp_gatt_attr *gatt_attr; + struct os_mbuf *buf = os_msys_get(0, 0); uint16_t start_handle, end_handle; struct ble_att_svr_entry *entry = NULL; ble_uuid_any_t uuid; @@ -1747,7 +1733,7 @@ get_attrs(uint8_t *data, uint16_t len) goto free; fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, BTP_STATUS_FAILED); free: os_mbuf_free_chain(buf); @@ -1757,8 +1743,8 @@ static void get_attr_val(uint8_t *data, uint16_t len) { const struct btp_gatt_get_attribute_value_cmd *cmd = (void *) data; - struct btp_gatt_get_attribute_value_rp *rp; - struct ble_gap_conn_desc conn; + struct btp_gatt_get_attribute_value_rp *rp; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); uint16_t handle = sys_cpu_to_le16(cmd->handle); uint8_t out_att_err = 0; @@ -1813,7 +1799,7 @@ change_database(uint8_t *data, uint16_t len) ble_svc_gatt_changed(cmd->start_handle, cmd->end_handle); - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_CHANGE_DATABASE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_CHANGE_DATABASE, BTP_STATUS_SUCCESS); return; @@ -1822,7 +1808,7 @@ change_database(uint8_t *data, uint16_t len) static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[4]; + uint8_t cmds[4]; struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; SYS_LOG_DBG(""); @@ -1854,7 +1840,7 @@ supported_commands(uint8_t *data, uint16_t len) tester_set_bit(cmds, BTP_GATT_CHANGE_DATABASE); tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); + (uint8_t *) rp, sizeof(cmds)); } enum attr_type { @@ -1864,7 +1850,7 @@ enum attr_type { }; void -tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_gatt(uint8_t opcode, uint8_t *data, uint16_t len) { switch (opcode) { @@ -1941,8 +1927,7 @@ tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, get_attr_val(data, len); return; default: - tester_rsp(BTP_SERVICE_ID_GATT, opcode, index, - BTP_STATUS_UNKNOWN_CMD); + tester_rsp(BTP_SERVICE_ID_GATT, opcode, BTP_STATUS_UNKNOWN_CMD); return; } } @@ -1952,7 +1937,7 @@ tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, uint8_t indication, struct os_mbuf *om) { struct btp_gatt_notification_ev *ev; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index af33f17ee4..f734c9c4e4 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -124,7 +124,7 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, uint16_t mtu, void *arg) { struct btp_gattc_exchange_mtu_ev *ev; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -175,7 +175,7 @@ exchange_mtu(uint8_t *data, uint16_t len) rsp: tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_EXCHANGE_MTU, - CONTROLLER_INDEX, status); + status); } static int @@ -184,9 +184,9 @@ disc_prim_svcs_cb(uint16_t conn_handle, const struct ble_gatt_svc *gatt_svc, void *arg) { struct btp_gattc_disc_prim_svcs_rp *rp; - struct ble_gap_conn_desc conn; - struct btp_gatt_service *service; - const ble_uuid_any_t *uuid; + struct ble_gap_conn_desc conn; + struct btp_gatt_service *service; + const ble_uuid_any_t *uuid; const ble_addr_t *addr; uint8_t uuid_length; struct os_mbuf *buf = os_msys_get(0, 0); @@ -280,14 +280,14 @@ disc_all_prim_svcs(uint8_t *data, uint16_t len) rsp: tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_PRIM_SVCS, - CONTROLLER_INDEX, status); + status); } static void disc_prim_uuid(uint8_t *data, uint16_t len) { const struct btp_gattc_disc_prim_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -314,8 +314,7 @@ disc_prim_uuid(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_PRIM_UUID, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_PRIM_UUID, status); } static int @@ -324,8 +323,8 @@ find_included_cb(uint16_t conn_handle, const struct ble_gatt_svc *gatt_svc, void *arg) { struct btp_gattc_find_included_rp *rp; - struct btp_gatt_included *included; - const ble_uuid_any_t *uuid; + struct btp_gatt_included *included; + const ble_uuid_any_t *uuid; int service_handle = (int) arg; uint8_t uuid_length; uint8_t err = (uint8_t) error->status; @@ -405,7 +404,7 @@ static void find_included(uint8_t *data, uint16_t len) { const struct btp_gattc_find_included_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; uint8_t status = BTP_STATUS_SUCCESS; @@ -432,8 +431,7 @@ find_included(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED, status); } static int @@ -443,7 +441,7 @@ disc_chrc_cb(uint16_t conn_handle, { struct btp_gattc_disc_chrc_rp *rp; struct btp_gatt_characteristic *chrc; - const ble_uuid_any_t *uuid; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; @@ -523,7 +521,7 @@ static void disc_all_chrc(uint8_t *data, uint16_t len) { const struct btp_gattc_disc_all_chrc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -552,15 +550,14 @@ disc_all_chrc(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_CHRC, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_CHRC, status); } static void disc_chrc_uuid(uint8_t *data, uint16_t len) { const struct btp_gattc_disc_chrc_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; @@ -592,8 +589,7 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_CHRC_UUID, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_CHRC_UUID, status); } static int @@ -604,8 +600,8 @@ disc_all_desc_cb(uint16_t conn_handle, void *arg) { struct btp_gattc_disc_all_desc_rp *rp; - struct btp_gatt_descriptor *dsc; - const ble_uuid_any_t *uuid; + struct btp_gatt_descriptor *dsc; + const ble_uuid_any_t *uuid; uint8_t uuid_length; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); @@ -679,7 +675,7 @@ static void disc_all_desc(uint8_t *data, uint16_t len) { const struct btp_gattc_disc_all_desc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -710,8 +706,7 @@ disc_all_desc(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC, status); } static int @@ -721,7 +716,7 @@ read_cb(uint16_t conn_handle, void *arg) { struct btp_gattc_read_rp *rp; - uint8_t opcode = (uint8_t) (int) arg; + uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; struct ble_gap_conn_desc conn; @@ -772,7 +767,7 @@ static void read(uint8_t *data, uint16_t len) { const struct btp_gattc_read_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -795,8 +790,7 @@ read(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ, status); } static int @@ -807,7 +801,7 @@ read_uuid_cb(uint16_t conn_handle, { struct btp_gattc_read_uuid_rp *rp; struct btp_gatt_read_uuid_chr *chr; - uint8_t opcode = (uint8_t) (int) arg; + uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -875,7 +869,7 @@ static void read_uuid(uint8_t *data, uint16_t len) { const struct btp_gattc_read_uuid_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -905,8 +899,7 @@ read_uuid(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_UUID, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_UUID, status); } static int @@ -975,7 +968,7 @@ static void read_long(uint8_t *data, uint16_t len) { const struct btp_gattc_read_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -999,15 +992,14 @@ read_long(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_LONG, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_LONG, status); } static void read_multiple(uint8_t *data, uint16_t len) { const struct btp_gattc_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + uint16_t handles[cmd->handles_count]; struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc, i; @@ -1037,15 +1029,14 @@ read_multiple(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_MULTIPLE, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_MULTIPLE, status); } static void write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) { const struct btp_gattc_write_without_rsp_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1064,7 +1055,7 @@ write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, op, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GATTC, op, status); } static int @@ -1073,7 +1064,7 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, void *arg) { struct btp_gattc_write_rp *rp; - uint8_t err = (uint8_t) error->status; + uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1107,7 +1098,7 @@ static void write(uint8_t *data, uint16_t len) { const struct btp_gattc_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1126,15 +1117,14 @@ write(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE, status); } static void write_long(uint8_t *data, uint16_t len) { const struct btp_gattc_write_long_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; uint8_t status = BTP_STATUS_SUCCESS; int rc = 0; @@ -1168,8 +1158,7 @@ write_long(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, status); } static int @@ -1180,7 +1169,7 @@ reliable_write_cb(uint16_t conn_handle, void *arg) { struct btp_gattc_write_rp *rp; - uint8_t err = (uint8_t) error->status; + uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; struct ble_gap_conn_desc conn; @@ -1213,7 +1202,7 @@ static void reliable_write(uint8_t *data, uint16_t len) { const struct btp_gattc_reliable_write_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; uint8_t status = BTP_STATUS_SUCCESS; @@ -1251,8 +1240,7 @@ reliable_write(uint8_t *data, uint16_t len) fail: os_mbuf_free_chain(om); rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, status); } static int @@ -1262,7 +1250,7 @@ subscribe_cb(uint16_t conn_handle, void *arg) { struct btp_subscribe_rp *rp; - uint8_t err = (uint8_t) error->status; + uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1351,7 +1339,7 @@ static void config_subscription(uint8_t *data, uint16_t len, uint8_t op) { const struct btp_gattc_cfg_notify_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1389,7 +1377,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) rsp: SYS_LOG_DBG("Config subscription (op %u) status %u", op, status); - tester_rsp(BTP_SERVICE_ID_GATTC, op, CONTROLLER_INDEX, status); + tester_rsp(BTP_SERVICE_ID_GATTC, op, status); } int @@ -1397,7 +1385,7 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, uint8_t indication, struct os_mbuf *om) { struct btp_gattc_notification_ev *ev; - struct ble_gap_conn_desc conn; + struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); const ble_addr_t *addr; @@ -1434,7 +1422,7 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[3]; + uint8_t cmds[3]; struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; SYS_LOG_DBG(""); @@ -1464,11 +1452,11 @@ supported_commands(uint8_t *data, uint16_t len) tester_set_bit(cmds, BTP_GATTC_CFG_INDICATE); tester_send(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); + (uint8_t *) rp, sizeof(cmds)); } void -tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_gattc(uint8_t opcode, uint8_t *data, uint16_t len) { switch (opcode) { @@ -1533,8 +1521,7 @@ tester_handle_gattc(uint8_t opcode, uint8_t index, uint8_t *data, config_subscription(data, len, opcode); return; default: - tester_rsp(BTP_SERVICE_ID_GATTC, opcode, index, - BTP_STATUS_UNKNOWN_CMD); + tester_rsp(BTP_SERVICE_ID_GATTC, opcode, BTP_STATUS_UNKNOWN_CMD); return; } } \ No newline at end of file diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index 93435943a4..03d70688a2 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -122,8 +122,8 @@ static void recv_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct os_mbuf *buf, void *arg) { - struct btp_l2cap_data_received_ev *ev = (void *) recv_cb_buf; - struct channel *channel = find_channel(chan); + struct btp_l2cap_data_received_ev *ev = (void *) recv_cb_buf; + struct channel *channel = find_channel(chan); assert(channel != NULL); ev->chan_id = channel->chan_id; @@ -136,7 +136,7 @@ recv_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, os_mbuf_copydata(buf, 0, ev->data_length, ev->data); tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DATA_RECEIVED, - CONTROLLER_INDEX, recv_cb_buf, sizeof(*ev) + ev->data_length); + recv_cb_buf, sizeof(*ev) + ev->data_length); tester_l2cap_coc_recv(chan, buf); } @@ -147,10 +147,10 @@ unstalled_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, { if (status) { tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, - CONTROLLER_INDEX, BTP_STATUS_FAILED); + BTP_STATUS_FAILED); } else { tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + BTP_STATUS_SUCCESS); } } @@ -160,7 +160,7 @@ reconfigured_ev(uint16_t conn_handle, struct ble_l2cap_chan *chan, int status) { struct btp_l2cap_reconfigured_ev ev; - struct channel *channel; + struct channel *channel; if (status != 0) { return; @@ -176,7 +176,7 @@ reconfigured_ev(uint16_t conn_handle, struct ble_l2cap_chan *chan, ev.our_mps = chan_info->our_l2cap_mtu; tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_RECONFIGURED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -184,7 +184,7 @@ connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info, void *arg) { struct btp_l2cap_connected_ev ev; - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct channel *channel = find_channel(chan); if (channel == NULL) { @@ -206,7 +206,7 @@ connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, sizeof(ev.address)); } - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, (uint8_t *) &ev, sizeof(ev)); } @@ -215,7 +215,7 @@ disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, struct ble_l2cap_chan_info *chan_info, void *arg) { struct btp_l2cap_disconnected_ev ev; - struct ble_gap_conn_desc desc; + struct ble_gap_conn_desc desc; struct channel *channel; memset(&ev, 0, sizeof(struct btp_l2cap_disconnected_ev)); @@ -235,7 +235,7 @@ disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, } tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DISCONNECTED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static int @@ -407,9 +407,9 @@ static void connect(uint8_t *data, uint16_t len) { const struct btp_l2cap_connect_cmd *cmd = (void *) data; - uint8_t rp_buf[sizeof(struct btp_l2cap_connect_rp) + cmd->num]; - struct btp_l2cap_connect_rp *rp = (void *) rp_buf; - struct ble_gap_conn_desc desc; + uint8_t rp_buf[sizeof(struct btp_l2cap_connect_rp) + cmd->num]; + struct btp_l2cap_connect_rp *rp = (void *) rp_buf; + struct ble_gap_conn_desc desc; struct channel *chan; struct os_mbuf *sdu_rx[cmd->num]; ble_addr_t *addr = (void *) data; @@ -481,13 +481,13 @@ connect(uint8_t *data, uint16_t len) goto fail; } - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, (uint8_t *) rp, sizeof(rp_buf)); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, BTP_STATUS_FAILED); } @@ -495,7 +495,7 @@ static void disconnect(const uint8_t *data, uint16_t len) { const struct btp_l2cap_disconnect_cmd *cmd = (void *) data; - struct channel *chan; + struct channel *chan; uint8_t status; int err; @@ -513,15 +513,15 @@ disconnect(const uint8_t *data, uint16_t len) status = BTP_STATUS_SUCCESS; rsp: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_DISCONNECT, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_DISCONNECT, status); } static void send_data(const uint8_t *data, uint16_t len) { - const struct btp_l2cap_send_data_cmd *cmd = (void *) data; - struct os_mbuf *sdu_tx = NULL; + const struct btp_l2cap_send_data_cmd *cmd = (void *) data; + struct os_mbuf *sdu_tx = NULL; int rc; uint16_t data_len = sys_le16_to_cpu(cmd->data_len); struct channel *chan = get_channel(cmd->chan_id); @@ -550,7 +550,7 @@ send_data(const uint8_t *data, uint16_t len) /* ble_l2cap_send takes ownership of the sdu */ rc = ble_l2cap_send(chan->chan, sdu_tx); if (rc == 0 || rc == BLE_HS_ESTALLED) { - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, BTP_STATUS_SUCCESS); return; } @@ -559,7 +559,7 @@ send_data(const uint8_t *data, uint16_t len) os_mbuf_free_chain(sdu_tx); fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, BTP_STATUS_FAILED); } @@ -582,12 +582,12 @@ listen(const uint8_t *data, uint16_t len) goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, BTP_STATUS_FAILED); } @@ -595,7 +595,7 @@ static void credits(uint8_t *data, uint16_t len) { const struct btp_l2cap_credits_cmd *cmd = (void *) data; - struct os_mbuf *sdu; + struct os_mbuf *sdu; int rc; struct channel *channel = get_channel(cmd->chan_id); @@ -613,11 +613,11 @@ credits(uint8_t *data, uint16_t len) if (rc != 0) { goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, BTP_STATUS_FAILED); } @@ -658,19 +658,19 @@ reconfigure(const uint8_t *data, uint16_t len) goto fail; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, BTP_STATUS_SUCCESS); return; fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, BTP_STATUS_FAILED); } static void supported_commands(uint8_t *data, uint16_t len) { - uint8_t cmds[1]; + uint8_t cmds[1]; struct btp_l2cap_read_supported_commands_rp *rp = (void *) cmds; memset(cmds, 0, sizeof(cmds)); @@ -683,11 +683,11 @@ supported_commands(uint8_t *data, uint16_t len) tester_set_bit(cmds, BTP_L2CAP_RECONFIGURE); tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); + (uint8_t *) rp, sizeof(cmds)); } void -tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, +tester_handle_l2cap(uint8_t opcode, uint8_t *data, uint16_t len) { switch (opcode) { @@ -713,7 +713,7 @@ tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data, credits(data, len); return; default: - tester_rsp(BTP_SERVICE_ID_L2CAP, opcode, index, + tester_rsp(BTP_SERVICE_ID_L2CAP, opcode, BTP_STATUS_UNKNOWN_CMD); return; } diff --git a/apps/bttester/src/btp_mesh.c b/apps/bttester/src/btp_mesh.c index cc36515a4e..3e0668ea74 100644 --- a/apps/bttester/src/btp_mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -284,7 +284,7 @@ link_open(bt_mesh_prov_bearer_t bearer) } tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_OPEN, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void @@ -308,7 +308,7 @@ link_close(bt_mesh_prov_bearer_t bearer) } tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_CLOSED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static int @@ -322,7 +322,7 @@ output_number(bt_mesh_output_action_t action, uint32_t number) ev.number = sys_cpu_to_le32(number); tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); return 0; } @@ -331,7 +331,7 @@ static int output_string(const char *str) { struct btp_mesh_out_string_action_ev *ev; - struct os_mbuf *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE); + struct os_mbuf *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE); SYS_LOG_DBG("str %s", str); @@ -361,7 +361,7 @@ input(bt_mesh_input_action_t action, uint8_t size) ev.action = sys_cpu_to_le16(action); ev.size = size; - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, (uint8_t *) &ev, sizeof(ev)); return 0; @@ -379,7 +379,7 @@ prov_complete(uint16_t net_idx, uint16_t addr) net.local = addr; net.dst = addr; - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, CONTROLLER_INDEX, + tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, NULL, 0); } @@ -426,7 +426,7 @@ config_prov(uint8_t *data, uint16_t len) prov.input_actions = sys_le16_to_cpu(cmd->in_actions); tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_CONFIG_PROVISIONING, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + BTP_STATUS_SUCCESS); } static void @@ -445,7 +445,7 @@ provision_node(uint8_t *data, uint16_t len) net_key_idx = sys_le16_to_cpu(cmd->net_key_idx); tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROVISION_NODE, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + BTP_STATUS_SUCCESS); } static void @@ -477,8 +477,7 @@ init(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INIT, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INIT, status); } static void @@ -488,15 +487,15 @@ reset(uint8_t *data, uint16_t len) bt_mesh_reset(); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RESET, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RESET, BTP_STATUS_SUCCESS); } static void input_number(uint8_t *data, uint16_t len) { - const struct btp_mesh_input_number_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; + const struct btp_mesh_input_number_cmd *cmd = (void *) data; + uint8_t status = BTP_STATUS_SUCCESS; uint32_t number; int err; @@ -509,15 +508,14 @@ input_number(uint8_t *data, uint16_t len) status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_NUMBER, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_NUMBER, status); } static void input_string(uint8_t *data, uint16_t len) { - const struct btp_mesh_input_string_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; + const struct btp_mesh_input_string_cmd *cmd = (void *) data; + uint8_t status = BTP_STATUS_SUCCESS; uint8_t str_auth[16]; int err; @@ -541,8 +539,7 @@ input_string(uint8_t *data, uint16_t len) } rsp: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_STRING, CONTROLLER_INDEX, - status); + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_STRING, status); } static void @@ -554,7 +551,7 @@ ivu_test_mode(uint8_t *data, uint16_t len) bt_mesh_iv_update_test(cmd->enable ? true : false); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TEST_MODE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TEST_MODE, BTP_STATUS_SUCCESS); } @@ -570,7 +567,7 @@ ivu_toggle_state(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to toggle the IV Update state"); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TOGGLE_STATE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TOGGLE_STATE, result ? BTP_STATUS_SUCCESS : BTP_STATUS_FAILED); } @@ -589,7 +586,7 @@ lpn(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to toggle LPN (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -605,7 +602,7 @@ lpn_poll(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send poll msg (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_POLL, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_POLL, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -613,7 +610,7 @@ static void net_send(uint8_t *data, uint16_t len) { struct btp_mesh_net_send_cmd *cmd = (void *) data; - struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); + struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = vnd_app_key_idx, @@ -638,7 +635,7 @@ net_send(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to send (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_NET_SEND, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_NET_SEND, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); os_mbuf_free_chain(msg); @@ -648,8 +645,8 @@ static void health_generate_faults(uint8_t *data, uint16_t len) { struct btp_mesh_health_generate_faults_rp *rp; - struct os_mbuf *buf = NET_BUF_SIMPLE(sizeof(*rp) + sizeof(cur_faults) + - sizeof(reg_faults)); + struct os_mbuf *buf = NET_BUF_SIMPLE(sizeof(*rp) + sizeof(cur_faults) + + sizeof(reg_faults)); uint8_t some_faults[] = {0x01, 0x02, 0x03, 0xff, 0x06}; uint8_t cur_faults_count, reg_faults_count; @@ -682,14 +679,14 @@ health_clear_faults(uint8_t *data, uint16_t len) bt_mesh_fault_update(&elements[0]); tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_HEALTH_CLEAR_FAULTS, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); + BTP_STATUS_SUCCESS); } static void model_send(uint8_t *data, uint16_t len) { struct btp_mesh_model_send_cmd *cmd = (void *) data; - struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); + struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = BT_MESH_KEY_DEV, @@ -728,7 +725,7 @@ model_send(uint8_t *data, uint16_t len) } fail: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_MODEL_SEND, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_MODEL_SEND, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); os_mbuf_free_chain(msg); @@ -739,8 +736,8 @@ model_send(uint8_t *data, uint16_t len) static void lpn_subscribe(uint8_t *data, uint16_t len) { - struct btp_mesh_lpn_subscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + struct btp_mesh_lpn_subscribe_cmd *cmd = (void *) data; + uint16_t address = sys_le16_to_cpu(cmd->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -750,15 +747,15 @@ lpn_subscribe(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to subscribe (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_SUBSCRIBE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_SUBSCRIBE, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } static void lpn_unsubscribe(uint8_t *data, uint16_t len) { - struct btp_mesh_lpn_unsubscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + struct btp_mesh_lpn_unsubscribe_cmd *cmd = (void *) data; + uint16_t address = sys_le16_to_cpu(cmd->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -768,7 +765,7 @@ lpn_unsubscribe(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to unsubscribe (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_UNSUBSCRIBE, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_UNSUBSCRIBE, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -784,7 +781,7 @@ rpl_clear(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to clear RPL (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RPL_CLEAR, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RPL_CLEAR, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } @@ -802,12 +799,12 @@ proxy_identity_enable(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to enable proxy identity (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROXY_IDENTITY, CONTROLLER_INDEX, + tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROXY_IDENTITY, err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); } void -tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) +tester_handle_mesh(uint8_t opcode, uint8_t *data, uint16_t len) { switch (opcode) { case BTP_MESH_READ_SUPPORTED_COMMANDS: @@ -870,7 +867,7 @@ tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len) proxy_identity_enable(data, len); break; default: - tester_rsp(BTP_SERVICE_ID_MESH, opcode, index, + tester_rsp(BTP_SERVICE_ID_MESH, opcode, BTP_STATUS_UNKNOWN_CMD); break; } @@ -884,7 +881,7 @@ net_recv_ev(uint8_t ttl, const void *payload, size_t payload_len) { - struct os_mbuf *buf = NET_BUF_SIMPLE(UINT8_MAX); + struct os_mbuf *buf = NET_BUF_SIMPLE(UINT8_MAX); struct btp_mesh_net_recv_ev *ev; SYS_LOG_DBG("ttl 0x%02x ctl 0x%02x src 0x%04x dst 0x%04x " @@ -964,14 +961,14 @@ invalid_bearer_cb(uint8_t opcode) SYS_LOG_DBG("opcode 0x%02x", opcode); tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INVALID_BEARER, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void incomp_timer_exp_cb(void) { tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INCOMP_TIMER_EXP, - CONTROLLER_INDEX, NULL, 0); + NULL, 0); } static struct bt_test_cb bt_test_cb = { @@ -996,20 +993,20 @@ lpn_established(uint16_t friend_addr) friend_addr, lpn->queue_size, lpn->recv_win); tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_ESTABLISHED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } static void lpn_terminated(uint16_t friend_addr) { - struct bt_mesh_lpn *lpn = &bt_mesh.lpn; - struct btp_mesh_lpn_terminated_ev ev = {lpn->sub->net_idx, friend_addr}; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + struct btp_mesh_lpn_terminated_ev ev = {lpn->sub->net_idx, friend_addr}; SYS_LOG_DBG("Friendship (as LPN) lost with Friend " "0x%04x", friend_addr); tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_TERMINATED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); + (uint8_t *) &ev, sizeof(ev)); } void diff --git a/apps/bttester/src/bttester.c b/apps/bttester/src/bttester.c index 1bb22e7cfc..f0d0886302 100644 --- a/apps/bttester/src/bttester.c +++ b/apps/bttester/src/bttester.c @@ -47,15 +47,60 @@ struct btp_buf { uint8_t data[BTP_MTU]; struct btp_hdr hdr; }; + uint8_t rsp[BTP_MTU]; }; static struct btp_buf cmd_buf[CMD_QUEUED]; +static struct { + const struct btp_handler *handlers; + uint8_t num; +} service_handler[BTP_SERVICE_ID_MAX + 1]; + +static void +tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, + uint8_t *data, size_t len); +static void +tester_rsp_with_index(uint8_t service, uint8_t opcode, uint8_t index, + uint8_t status); + +void +tester_register_command_handlers(uint8_t service, + const struct btp_handler *handlers, + size_t num) +{ + __ASSERT_NO_MSG(service <= BTP_SERVICE_ID_MAX); + __ASSERT_NO_MSG(service_handler[service].handlers == NULL); + + service_handler[service].handlers = handlers; + service_handler[service].num = num; +} + +static const struct btp_handler * +find_btp_handler(uint8_t service, uint8_t opcode) +{ + if ((service > BTP_SERVICE_ID_MAX) || + (service_handler[service].handlers == NULL)) { + return NULL; + } + + for (uint8_t i = 0; i < service_handler[service].num; i++) { + if (service_handler[service].handlers[i].opcode == opcode) { + return &service_handler[service].handlers[i]; + } + } + + return NULL; +} + static void cmd_handler(struct os_event *ev) { + const struct btp_handler *btp; uint16_t len; struct btp_buf *cmd; + uint8_t status; + uint16_t rsp_len = 0; if (!ev || !ev->ev_arg) { return; @@ -71,43 +116,30 @@ cmd_handler(struct os_event *ev) sizeof(cmd->hdr) + len)); } - /* TODO - * verify if service is registered before calling handler - */ - - switch (cmd->hdr.service) { - case BTP_SERVICE_ID_CORE: - tester_handle_core(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; - case BTP_SERVICE_ID_GAP: - tester_handle_gap(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; - case BTP_SERVICE_ID_GATT: - tester_handle_gatt(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; -#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) - case BTP_SERVICE_ID_L2CAP: - tester_handle_l2cap(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; -#endif /* MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) */ -#if MYNEWT_VAL(BLE_MESH) - case BTP_SERVICE_ID_MESH: - tester_handle_mesh(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; -#endif /* MYNEWT_VAL(BLE_MESH) */ - case BTP_SERVICE_ID_GATTC: - tester_handle_gattc(cmd->hdr.opcode, cmd->hdr.index, - cmd->hdr.data, len); - break; - default: - tester_rsp(cmd->hdr.service, cmd->hdr.opcode, - cmd->hdr.index, BTP_STATUS_FAILED); - break; + btp = find_btp_handler(cmd->hdr.service, cmd->hdr.opcode); + if (btp) { + if (btp->index != cmd->hdr.index) { + status = BTP_STATUS_FAILED; + } else if ((btp->expect_len >= 0) && (btp->expect_len != len)) { + status = BTP_STATUS_FAILED; + } else { + status = btp->func(cmd->hdr.data, len, + cmd->rsp, &rsp_len); + } + + __ASSERT_NO_MSG((rsp_len + sizeof(struct btp_hdr)) <= BTP_MTU); + } else { + status = BTP_STATUS_UNKNOWN_CMD; + } + + if (status != BTP_STATUS_DELAY_REPLY) { + if ((status == BTP_STATUS_SUCCESS) && rsp_len > 0) { + tester_send_with_index(cmd->hdr.service, cmd->hdr.opcode, + cmd->hdr.index, cmd->rsp, rsp_len); + } else { + tester_rsp_with_index(cmd->hdr.service, cmd->hdr.opcode, + cmd->hdr.index, status); + } } os_eventq_put(&avail_queue, ev); @@ -191,20 +223,23 @@ tester_init(void) bttester_pipe_register(buf->data, BTP_MTU, recv_cb); - tester_send(BTP_SERVICE_ID_CORE, BTP_CORE_EV_IUT_READY, BTP_INDEX_NONE, - NULL, 0); + /* core service is always available */ + tester_init_core(); + + tester_send_with_index(BTP_SERVICE_ID_CORE, BTP_CORE_EV_IUT_READY, + BTP_INDEX_NONE, NULL, 0); } -void -tester_send(uint8_t service, uint8_t opcode, uint8_t index, uint8_t *data, - size_t len) +static void +tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, + uint8_t *data, size_t len) { struct btp_hdr msg; msg.service = service; msg.opcode = opcode; msg.index = index; - msg.len = len; + msg.len = sys_cpu_to_le16(len); bttester_pipe_send((uint8_t *) &msg, sizeof(msg)); if (data && len) { @@ -238,16 +273,29 @@ tester_send_buf(uint8_t service, uint8_t opcode, uint8_t index, } } -void -tester_rsp(uint8_t service, uint8_t opcode, uint8_t index, uint8_t status) +static void +tester_rsp_with_index(uint8_t service, uint8_t opcode, uint8_t index, + uint8_t status) { struct btp_status s; if (status == BTP_STATUS_SUCCESS) { - tester_send(service, opcode, index, NULL, 0); + tester_send_with_index(service, opcode, index, NULL, 0); return; } s.code = status; - tester_send(service, BTP_STATUS, index, (uint8_t *) &s, sizeof(s)); + tester_send_with_index(service, BTP_STATUS, index, (uint8_t *) &s, sizeof(s)); +} + +void +tester_send(uint8_t service, uint8_t opcode, uint8_t *data, size_t len) +{ + tester_send_with_index(service, opcode, BTP_INDEX, data, len); +} + +void +tester_rsp(uint8_t service, uint8_t opcode, uint8_t status) +{ + tester_rsp_with_index(service, opcode, BTP_INDEX, status); } diff --git a/apps/bttester/src/glue.h b/apps/bttester/src/glue.h index 0372d757fe..024f795643 100644 --- a/apps/bttester/src/glue.h +++ b/apps/bttester/src/glue.h @@ -67,4 +67,23 @@ net_buf_simple_push(struct os_mbuf *om, uint8_t len); const char * bt_hex(const void *buf, size_t len); +/** + * INTERNAL_HIDDEN @endcond + */ + +/** + * @brief Define an array of atomic variables. + * + * This macro defines an array of atomic variables containing at least + * @a num_bits bits. + * + * @note + * If used from file scope, the bits of the array are initialized to zero; + * if used from within a function, the bits are left uninitialized. + * + * @param name Name of array of atomic variables. + * @param num_bits Number of bits needed. + */ +#define ATOMIC_DEFINE(name, num_bits) \ + atomic_t name[1 + ((num_bits) - 1) / ATOMIC_BITS] #endif /* __GLUE_H__ */ From f69eb886818c91249bf501a7e430a302d1c1312c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Mon, 10 Jul 2023 13:26:50 +0200 Subject: [PATCH 03/12] apps/bttester: drop `glue` module There is no need to be dependent on `glue` module and it can be dropped from application. Mesh part of bttester can still use one from `"mesh/glue.h"` --- apps/bttester/src/btp/btp.h | 10 -- apps/bttester/src/btp/btp_gap.h | 15 +++ apps/bttester/src/btp/bttester.h | 15 ++- apps/bttester/src/btp_gap.c | 64 +++++++----- apps/bttester/src/btp_gatt.c | 143 ++++++++++++++------------ apps/bttester/src/btp_gatt_cl.c | 166 +++++++++++++++++++------------ apps/bttester/src/btp_l2cap.c | 8 +- apps/bttester/src/btp_mesh.c | 14 +-- apps/bttester/src/bttester.c | 53 ++++++++-- apps/bttester/src/glue.c | 129 ------------------------ apps/bttester/src/glue.h | 89 ----------------- 11 files changed, 298 insertions(+), 408 deletions(-) delete mode 100644 apps/bttester/src/glue.c delete mode 100644 apps/bttester/src/glue.h diff --git a/apps/bttester/src/btp/btp.h b/apps/bttester/src/btp/btp.h index 996ab7912b..5e1a172e9f 100644 --- a/apps/bttester/src/btp/btp.h +++ b/apps/bttester/src/btp/btp.h @@ -34,12 +34,6 @@ #include "btp_l2cap.h" #include "btp_mesh.h" -#if MYNEWT_VAL(BLE_MESH) -#include "mesh/glue.h" -#else -#include "../glue.h" -#endif - #define BTP_MTU MYNEWT_VAL(BTTESTER_BTP_DATA_SIZE_MAX) #define BTP_DATA_MAX_SIZE (BTP_MTU - sizeof(struct btp_hdr)) @@ -78,10 +72,6 @@ #define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG #define SYS_LOG_DOMAIN "bttester" -#define sys_cpu_to_le32 htole32 -#define sys_le32_to_cpu le32toh -#define sys_cpu_to_le16 htole16 - struct btp_hdr { uint8_t service; uint8_t opcode; diff --git a/apps/bttester/src/btp/btp_gap.h b/apps/bttester/src/btp/btp_gap.h index 5ef756113f..ca60b81037 100644 --- a/apps/bttester/src/btp/btp_gap.h +++ b/apps/bttester/src/btp/btp_gap.h @@ -26,6 +26,21 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "nimble/ble.h" + +struct adv_data { + uint8_t type; + uint8_t data_len; + const uint8_t *data; +}; + +#define ADV_DATA(_type, _data, _data_len) \ + { \ + .type = (_type), \ + .data_len = (_data_len), \ + .data = (const uint8_t *)(_data), \ + } + /* GAP Service */ /* commands */ #define BTP_GAP_READ_SUPPORTED_COMMANDS 0x01 diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index e13400ae96..3aed917796 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -31,13 +31,18 @@ #include "syscfg/syscfg.h" #include "host/ble_gatt.h" -#if MYNEWT_VAL(BLE_MESH) -#include "mesh/glue.h" -#else -#include "glue.h" -#endif +#include "os/os_mbuf.h" #include +#define BIT(n) (1UL << (n)) + +/* Reset os_mbuf to reusable state */ +void +tester_mbuf_reset(struct os_mbuf *buf); + +const char * +string_from_bytes(const void *buf, size_t len); + static inline void tester_set_bit(uint8_t *addr, unsigned int bit) { diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index 66b88a6802..378e3ae5b2 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -35,6 +35,8 @@ #include "btp/btp.h" +#include + #define CONTROLLER_INDEX 0 #define CONTROLLER_NAME "btp_tester" @@ -235,8 +237,8 @@ controller_info(const void *cmd, uint16_t cmd_len, current_settings |= BIT(BTP_GAP_SETTINGS_SC); } - rp->supported_settings = sys_cpu_to_le32(supported_settings); - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->supported_settings = htole32(supported_settings); + rp->current_settings = htole32(current_settings); memcpy(rp->name, CONTROLLER_NAME, sizeof(CONTROLLER_NAME)); @@ -299,7 +301,7 @@ set_connectable(const void *cmd, uint16_t cmd_len, adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; } - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); @@ -339,7 +341,7 @@ set_discoverable(const void *cmd, uint16_t cmd_len, return BTP_STATUS_FAILED; } - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); @@ -362,18 +364,18 @@ set_bondable(const void *cmd, uint16_t cmd_len, current_settings &= ~BIT(BTP_GAP_SETTINGS_BONDABLE); } - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); return BTP_STATUS_SUCCESS; } -static struct bt_data ad[10] = { - BT_DATA(BLE_HS_ADV_TYPE_FLAGS, &ad_flags, sizeof(ad_flags)), +static struct adv_data ad[10] = { + ADV_DATA(BLE_HS_ADV_TYPE_FLAGS, &ad_flags, sizeof(ad_flags)), }; -static struct bt_data sd[10]; +static struct adv_data sd[10]; static int -set_ad(const struct bt_data *ad_data, size_t ad_len, +set_ad(const struct adv_data *ad_data, size_t ad_len, uint8_t *buf, uint8_t *buf_len) { int i; @@ -519,7 +521,7 @@ start_advertising(const void *cmd, uint16_t cmd_len, } current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); @@ -541,7 +543,7 @@ stop_advertising(const void *cmd, uint16_t cmd_len, } current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); @@ -584,18 +586,27 @@ store_adv(const ble_addr_t *addr, int8_t rssi, const uint8_t *data, uint8_t len) { struct btp_gap_device_found_ev *ev; - + void *adv_data; /* cleanup */ - net_buf_simple_init(adv_buf, 0); + tester_mbuf_reset(adv_buf); - ev = net_buf_simple_add(adv_buf, sizeof(*ev)); + ev = os_mbuf_extend(adv_buf, sizeof(*ev)); + if (!ev) { + return; + } memcpy(ev->address, addr->val, sizeof(ev->address)); ev->address_type = addr->type; ev->rssi = rssi; ev->flags = BTP_GAP_DEVICE_FOUND_FLAG_AD | BTP_GAP_DEVICE_FOUND_FLAG_RSSI; ev->eir_data_len = len; - memcpy(net_buf_simple_add(adv_buf, len), data, len); + + adv_data = os_mbuf_extend(adv_buf, len); + if (!adv_data) { + return; + } + + memcpy(adv_data, data, len); } static void @@ -603,6 +614,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, const uint8_t *data, uint8_t len) { struct btp_gap_device_found_ev *ev; + void *adv_data; ble_addr_t a; /* if General/Limited Discovery - parse Advertising data to get flags */ @@ -649,7 +661,12 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, ev->eir_data_len += len; ev->flags |= BTP_GAP_DEVICE_FOUND_FLAG_SD; - memcpy(net_buf_simple_add(adv_buf, len), data, len); + adv_data = os_mbuf_extend(adv_buf, len); + if (!adv_data) { + return; + } + + memcpy(adv_data, data, len); goto done; } @@ -712,7 +729,7 @@ start_discovery(const void *cmd, uint16_t cmd_len, return BTP_STATUS_FAILED; } - net_buf_simple_init(adv_buf, 0); + tester_mbuf_reset(adv_buf); discovery_flags = cp->flags; return BTP_STATUS_SUCCESS; @@ -910,7 +927,7 @@ auth_passkey_display(uint16_t conn_handle, unsigned int passkey) memcpy(ev.address, addr->val, sizeof(ev.address)); ev.address_type = addr->type; - ev.passkey = sys_cpu_to_le32(pk.passkey); + ev.passkey = htole32(pk.passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, (uint8_t *) &ev, sizeof(ev)); @@ -959,7 +976,7 @@ auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) memcpy(ev.address, addr->val, sizeof(ev.address)); ev.address_type = addr->type; - ev.passkey = sys_cpu_to_le32(passkey); + ev.passkey = htole32(passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, (uint8_t *) &ev, sizeof(ev)); @@ -1206,7 +1223,7 @@ adv_complete(void) struct btp_gap_new_settings_ev ev; current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); - ev.current_settings = sys_cpu_to_le32(current_settings); + ev.current_settings = htole32(current_settings); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, (uint8_t *) &ev, sizeof(ev)); @@ -1525,7 +1542,7 @@ passkey_entry(const void *cmd, uint16_t cmd_len, } pk.action = BLE_SM_IOACT_INPUT; - pk.passkey = sys_le32_to_cpu(cp->passkey); + pk.passkey = le32toh(cp->passkey); rc = ble_sm_inject_io(desc.conn_handle, &pk); if (rc) { @@ -1587,7 +1604,7 @@ start_direct_adv(const void *cmd, uint16_t cmd_len, } current_settings |= BIT(BTP_GAP_SETTINGS_ADVERTISING); - rp->current_settings = sys_cpu_to_le32(current_settings); + rp->current_settings = htole32(current_settings); *rsp_len = sizeof(*rp); @@ -1920,7 +1937,8 @@ tester_init_gap(void) os_callout_init(&bttester_nrpa_rotate_timer, os_eventq_dflt_get(), rotate_nrpa_cb, NULL); #endif - adv_buf = NET_BUF_SIMPLE(ADV_BUF_LEN); + adv_buf = os_msys_get(ADV_BUF_LEN, 0); + assert(adv_buf); tester_init_gap_cb(); diff --git a/apps/bttester/src/btp_gatt.c b/apps/bttester/src/btp_gatt.c index 5d62a677ad..ef6a805b75 100644 --- a/apps/bttester/src/btp_gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -260,11 +260,13 @@ attr_value_changed_ev(uint16_t handle, struct os_mbuf *data) SYS_LOG_DBG(""); - net_buf_simple_init(buf, 0); - ev = net_buf_simple_add(buf, sizeof(*ev)); + ev = os_mbuf_extend(buf, sizeof(*ev)); + if (!ev) { + return; + } - ev->handle = sys_cpu_to_le16(handle); - ev->data_length = sys_cpu_to_le16(os_mbuf_len(data)); + ev->handle = htole16(handle); + ev->data_length = htole16(os_mbuf_len(data)); os_mbuf_appendfrom(buf, data, 0, os_mbuf_len(data)); tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_ATTR_VALUE_CHANGED, @@ -598,7 +600,7 @@ btp2bt_uuid(const uint8_t *uuid, uint8_t len, case 0x02: /* UUID 16 */ bt_uuid->u.type = BLE_UUID_TYPE_16; memcpy(&le16, uuid, sizeof(le16)); - BLE_UUID16(bt_uuid)->value = sys_le16_to_cpu(le16); + BLE_UUID16(bt_uuid)->value = le16toh(le16); break; case 0x10: /* UUID 128*/ bt_uuid->u.type = BLE_UUID_TYPE_128; @@ -721,7 +723,7 @@ read(uint8_t *data, uint16_t len) goto fail; } - if (ble_gattc_read(conn.conn_handle, sys_le16_to_cpu(cmd->handle), + if (ble_gattc_read(conn.conn_handle, le16toh(cmd->handle), read_cb, (void *) BTP_GATT_READ)) { read_destroy(); goto fail; @@ -793,8 +795,8 @@ read_long(uint8_t *data, uint16_t len) } if (ble_gattc_read_long(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), - sys_le16_to_cpu(cmd->offset), + le16toh(cmd->handle), + le16toh(cmd->offset), read_long_cb, (void *) BTP_GATT_READ_LONG)) { read_destroy(); goto fail; @@ -817,7 +819,7 @@ read_multiple(uint8_t *data, uint16_t len) SYS_LOG_DBG(""); for (i = 0; i < ARRAY_SIZE(handles); i++) { - handles[i] = sys_le16_to_cpu(cmd->handles[i]); + handles[i] = le16toh(cmd->handles[i]); } rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); @@ -862,8 +864,8 @@ write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) } if (ble_gattc_write_no_rsp_flat(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), cmd->data, - sys_le16_to_cpu(cmd->data_length))) { + le16toh(cmd->handle), cmd->data, + le16toh(cmd->data_length))) { status = BTP_STATUS_FAILED; } @@ -900,8 +902,8 @@ write(uint8_t *data, uint16_t len) goto fail; } - if (ble_gattc_write_flat(conn.conn_handle, sys_le16_to_cpu(cmd->handle), - cmd->data, sys_le16_to_cpu(cmd->data_length), + if (ble_gattc_write_flat(conn.conn_handle, le16toh(cmd->handle), + cmd->data, le16toh(cmd->data_length), write_rsp, (void *) BTP_GATT_WRITE)) { goto fail; } @@ -927,15 +929,15 @@ write_long(uint8_t *data, uint16_t len) goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, sys_le16_to_cpu(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); if (!om) { SYS_LOG_ERR("Insufficient resources"); goto fail; } rc = ble_gattc_write_long(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), - sys_le16_to_cpu(cmd->offset), + le16toh(cmd->handle), + le16toh(cmd->offset), om, write_rsp, (void *) BTP_GATT_WRITE_LONG); if (!rc) { @@ -980,16 +982,16 @@ reliable_write(uint8_t *data, uint16_t len) goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, sys_le16_to_cpu(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); /* This is required, because Nimble checks if * the data is longer than offset */ - if (os_mbuf_extend(om, sys_le16_to_cpu(cmd->offset) + 1) == NULL) { + if (os_mbuf_extend(om, le16toh(cmd->offset) + 1) == NULL) { goto fail; } - attr.handle = sys_le16_to_cpu(cmd->handle); - attr.offset = sys_le16_to_cpu(cmd->offset); + attr.handle = le16toh(cmd->handle); + attr.offset = le16toh(cmd->offset); attr.om = om; if (ble_gattc_write_reliable(conn.conn_handle, &attr, 1, @@ -1038,8 +1040,8 @@ read_uuid(uint8_t *data, uint16_t len) } if (ble_gattc_read_by_uuid(conn.conn_handle, - sys_le16_to_cpu(cmd->start_handle), - sys_le16_to_cpu(cmd->end_handle), &uuid.u, + le16toh(cmd->start_handle), + le16toh(cmd->end_handle), &uuid.u, read_long_cb, (void *) BTP_GATT_READ_UUID)) { read_destroy(); goto fail; @@ -1088,12 +1090,12 @@ disc_prim_uuid_cb(uint16_t conn_handle, return BLE_HS_ENOMEM; } - service->start_handle = sys_cpu_to_le16(gatt_svc->start_handle); - service->end_handle = sys_cpu_to_le16(gatt_svc->end_handle); + service->start_handle = htole16(gatt_svc->start_handle); + service->end_handle = htole16(gatt_svc->end_handle); service->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(service->uuid, &u16, uuid_length); } else { memcpy(service->uuid, BLE_UUID128(uuid)->value, @@ -1144,11 +1146,11 @@ disc_all_desc_cb(uint16_t conn_handle, return BLE_HS_ENOMEM; } - dsc->descriptor_handle = sys_cpu_to_le16(gatt_dsc->handle); + dsc->descriptor_handle = htole16(gatt_dsc->handle); dsc->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(dsc->uuid, &u16, uuid_length); } else { memcpy(dsc->uuid, BLE_UUID128(uuid)->value, uuid_length); @@ -1208,8 +1210,8 @@ disc_all_desc(uint8_t *data, uint16_t len) goto fail; } - start_handle = sys_le16_to_cpu(cmd->start_handle) - 1; - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle) - 1; + end_handle = le16toh(cmd->end_handle); rc = ble_gattc_disc_all_dscs(conn.conn_handle, start_handle, end_handle, disc_all_desc_cb, NULL); @@ -1266,14 +1268,14 @@ find_included_cb(uint16_t conn_handle, } /* FIXME */ - included->included_handle = sys_cpu_to_le16(service_handle + 1 + + included->included_handle = htole16(service_handle + 1 + rp->services_count); - included->service.start_handle = sys_cpu_to_le16(gatt_svc->start_handle); - included->service.end_handle = sys_cpu_to_le16(gatt_svc->end_handle); + included->service.start_handle = htole16(gatt_svc->start_handle); + included->service.end_handle = htole16(gatt_svc->end_handle); included->service.uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(included->service.uuid, &u16, uuid_length); } else { memcpy(included->service.uuid, BLE_UUID128(uuid)->value, @@ -1323,13 +1325,13 @@ disc_chrc_cb(uint16_t conn_handle, return BLE_HS_ENOMEM; } - chrc->characteristic_handle = sys_cpu_to_le16(gatt_chr->def_handle); + chrc->characteristic_handle = htole16(gatt_chr->def_handle); chrc->properties = gatt_chr->properties; - chrc->value_handle = sys_cpu_to_le16(gatt_chr->val_handle); + chrc->value_handle = htole16(gatt_chr->val_handle); chrc->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(chrc->uuid, &u16, uuid_length); } else { memcpy(chrc->uuid, BLE_UUID128(uuid)->value, @@ -1365,8 +1367,8 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) goto fail; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); if (ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, @@ -1440,8 +1442,8 @@ disc_all_chrc(uint8_t *data, uint16_t len) goto fail; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); rc = ble_gattc_disc_all_chrs(conn.conn_handle, start_handle, end_handle, disc_chrc_cb, (void *) BTP_GATT_DISC_ALL_CHRC); @@ -1476,8 +1478,8 @@ find_included(uint8_t *data, uint16_t len) goto fail; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); service_handle_arg = start_handle; if (ble_gattc_find_inc_svcs(conn.conn_handle, start_handle, end_handle, @@ -1583,7 +1585,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) { const struct btp_gatt_cfg_notify_cmd *cmd = (void *) data; struct ble_gap_conn_desc conn; - uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); + uint16_t ccc_handle = le16toh(cmd->ccc_handle); uint8_t status; int rc; @@ -1677,8 +1679,8 @@ get_attrs(uint8_t *data, uint16_t len) memset(str, 0, sizeof(str)); memset(&uuid, 0, sizeof(uuid)); - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); if (cmd->type_length) { if (btp2bt_uuid(cmd->type, cmd->type_length, &uuid)) { @@ -1694,8 +1696,10 @@ get_attrs(uint8_t *data, uint16_t len) SYS_LOG_DBG("start 0x%04x end 0x%04x", start_handle, end_handle); } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + goto fail; + } entry = ble_att_svr_find_by_uuid(entry, uuid_ptr, end_handle); while (entry) { @@ -1706,19 +1710,23 @@ get_attrs(uint8_t *data, uint16_t len) continue; } - gatt_attr = net_buf_simple_add(buf, sizeof(*gatt_attr)); - gatt_attr->handle = sys_cpu_to_le16(entry->ha_handle_id); + gatt_attr = os_mbuf_extend(buf, sizeof(*gatt_attr)); + if (!gatt_attr) { + goto fail; + } + gatt_attr->handle = htole16(entry->ha_handle_id); gatt_attr->permission = flags_hs2btp(entry->ha_flags); if (entry->ha_uuid->type == BLE_UUID_TYPE_16) { + uint16_t uuid_val; + gatt_attr->type_length = 2; - net_buf_simple_add_le16(buf, - BLE_UUID16(entry->ha_uuid)->value); + uuid_val = htole16(BLE_UUID16(entry->ha_uuid)->value); + os_mbuf_append(buf, &uuid_val, sizeof(uuid_val)); } else { gatt_attr->type_length = 16; - net_buf_simple_add_mem(buf, - BLE_UUID128(entry->ha_uuid)->value, - gatt_attr->type_length); + os_mbuf_append(buf, BLE_UUID128(entry->ha_uuid)->value, + gatt_attr->type_length); } count++; @@ -1746,15 +1754,17 @@ get_attr_val(uint8_t *data, uint16_t len) struct btp_gatt_get_attribute_value_rp *rp; struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); - uint16_t handle = sys_cpu_to_le16(cmd->handle); + uint16_t handle = le16toh(cmd->handle); uint8_t out_att_err = 0; int conn_status; conn_status = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); if (conn_status) { - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + goto free; + } ble_att_svr_read_handle(BLE_HS_CONN_HANDLE_NONE, handle, 0, buf, @@ -1768,8 +1778,10 @@ get_attr_val(uint8_t *data, uint16_t len) goto free; } else { - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + goto free; + } ble_att_svr_read_handle(conn.conn_handle, handle, 0, buf, @@ -1951,16 +1963,17 @@ tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, goto fail; } - net_buf_simple_init(buf, 0); - ev = net_buf_simple_add(buf, sizeof(*ev)); + ev = os_mbuf_extend(buf, sizeof(*ev)); + if (!ev) { + goto fail; + } addr = &conn.peer_ota_addr; - ev->address_type = addr->type; - memcpy(ev->address, addr->val, sizeof(ev->address)); + memcpy(&ev->address, addr, sizeof(ev->address)); ev->type = (uint8_t) (indication ? 0x02 : 0x01); - ev->handle = sys_cpu_to_le16(attr_handle); - ev->data_length = sys_cpu_to_le16(os_mbuf_len(om)); + ev->handle = htole16(attr_handle); + ev->data_length = htole16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_NOTIFICATION, diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index f734c9c4e4..3f7a09f2f8 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -44,7 +44,7 @@ btp2bt_uuid(const uint8_t *uuid, uint8_t len, case 0x02: /* UUID 16 */ bt_uuid->u.type = BLE_UUID_TYPE_16; memcpy(&le16, uuid, sizeof(le16)); - BLE_UUID16(bt_uuid)->value = sys_le16_to_cpu(le16); + BLE_UUID16(bt_uuid)->value = le16toh(le16); break; case 0x10: /* UUID 128*/ bt_uuid->u.type = BLE_UUID_TYPE_128; @@ -134,8 +134,10 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, goto fail; } - net_buf_simple_init(buf, 0); - ev = net_buf_simple_add(buf, sizeof(*ev)); + ev = os_mbuf_extend(buf, sizeof(*ev)); + if (!ev) { + return 0; + } addr = &conn.peer_ota_addr; @@ -200,8 +202,11 @@ disc_prim_svcs_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -237,12 +242,12 @@ disc_prim_svcs_cb(uint16_t conn_handle, goto free; } - service->start_handle = sys_cpu_to_le16(gatt_svc->start_handle); - service->end_handle = sys_cpu_to_le16(gatt_svc->end_handle); + service->start_handle = htole16(gatt_svc->start_handle); + service->end_handle = htole16(gatt_svc->end_handle); service->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(service->uuid, &u16, uuid_length); } else { memcpy(service->uuid, BLE_UUID128(uuid)->value, @@ -340,8 +345,11 @@ find_included_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -379,14 +387,14 @@ find_included_cb(uint16_t conn_handle, goto free; } - included->included_handle = sys_cpu_to_le16(service_handle + 1 + + included->included_handle = htole16(service_handle + 1 + rp->services_count); - included->service.start_handle = sys_cpu_to_le16(gatt_svc->start_handle); - included->service.end_handle = sys_cpu_to_le16(gatt_svc->end_handle); + included->service.start_handle = htole16(gatt_svc->start_handle); + included->service.end_handle = htole16(gatt_svc->end_handle); included->service.uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(included->service.uuid, &u16, uuid_length); } else { memcpy(included->service.uuid, BLE_UUID128(uuid)->value, @@ -418,8 +426,8 @@ find_included(uint8_t *data, uint16_t len) goto rsp; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); service_handle_arg = start_handle; if (ble_gattc_find_inc_svcs(conn.conn_handle, start_handle, end_handle, @@ -455,8 +463,11 @@ disc_chrc_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -498,13 +509,13 @@ disc_chrc_cb(uint16_t conn_handle, goto free; } - chrc->characteristic_handle = sys_cpu_to_le16(gatt_chr->def_handle); + chrc->characteristic_handle = htole16(gatt_chr->def_handle); chrc->properties = gatt_chr->properties; - chrc->value_handle = sys_cpu_to_le16(gatt_chr->val_handle); + chrc->value_handle = htole16(gatt_chr->val_handle); chrc->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(chrc->uuid, &u16, uuid_length); } else { memcpy(chrc->uuid, BLE_UUID128(uuid)->value, @@ -535,8 +546,8 @@ disc_all_chrc(uint8_t *data, uint16_t len) goto rsp; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); rc = ble_gattc_disc_all_chrs(conn.conn_handle, start_handle, @@ -576,8 +587,8 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) goto rsp; } - start_handle = sys_le16_to_cpu(cmd->start_handle); - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle); + end_handle = le16toh(cmd->end_handle); rc = ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, @@ -616,8 +627,11 @@ disc_all_desc_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -654,11 +668,11 @@ disc_all_desc_cb(uint16_t conn_handle, goto free; } - dsc->descriptor_handle = sys_cpu_to_le16(gatt_dsc->handle); + dsc->descriptor_handle = htole16(gatt_dsc->handle); dsc->uuid_length = uuid_length; if (uuid->u.type == BLE_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BLE_UUID16(uuid)->value); + uint16_t u16 = htole16(BLE_UUID16(uuid)->value); memcpy(dsc->uuid, &u16, uuid_length); } else { memcpy(dsc->uuid, BLE_UUID128(uuid)->value, uuid_length); @@ -688,8 +702,8 @@ disc_all_desc(uint8_t *data, uint16_t len) goto rsp; } - start_handle = sys_le16_to_cpu(cmd->start_handle) - 1; - end_handle = sys_le16_to_cpu(cmd->end_handle); + start_handle = le16toh(cmd->start_handle) - 1; + end_handle = le16toh(cmd->end_handle); rc = ble_gattc_disc_all_dscs(conn.conn_handle, start_handle, @@ -727,8 +741,11 @@ read_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -782,7 +799,7 @@ read(uint8_t *data, uint16_t len) /* Clear buffer */ read_destroy(); - if (ble_gattc_read(conn.conn_handle, sys_le16_to_cpu(cmd->handle), + if (ble_gattc_read(conn.conn_handle, le16toh(cmd->handle), read_cb, (void *) BTP_GATTC_READ_RP)) { read_destroy(); status = BTP_STATUS_FAILED; @@ -816,8 +833,11 @@ read_uuid_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -857,7 +877,7 @@ read_uuid_cb(uint16_t conn_handle, goto free; } - chr->handle = sys_cpu_to_be16(attr->handle); + chr->handle = htobe16(attr->handle); memcpy(chr->data, attr->om->om_data, attr->om->om_len); free: @@ -891,8 +911,8 @@ read_uuid(uint8_t *data, uint16_t len) read_destroy(); if (ble_gattc_read_by_uuid(conn.conn_handle, - sys_le16_to_cpu(cmd->start_handle), - sys_le16_to_cpu(cmd->end_handle), &uuid.u, + le16toh(cmd->start_handle), + le16toh(cmd->end_handle), &uuid.u, read_uuid_cb, (void *) BTP_GATTC_READ_UUID_RP)) { read_destroy(); status = BTP_STATUS_FAILED; @@ -923,8 +943,11 @@ read_long_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -984,8 +1007,8 @@ read_long(uint8_t *data, uint16_t len) read_destroy(); if (ble_gattc_read_long(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), - sys_le16_to_cpu(cmd->offset), + le16toh(cmd->handle), + le16toh(cmd->offset), read_long_cb, (void *) BTP_GATTC_READ_LONG_RP)) { read_destroy(); status = BTP_STATUS_FAILED; @@ -1007,7 +1030,7 @@ read_multiple(uint8_t *data, uint16_t len) SYS_LOG_DBG(""); for (i = 0; i < ARRAY_SIZE(handles); i++) { - handles[i] = sys_le16_to_cpu(cmd->handles[i]); + handles[i] = le16toh(cmd->handles[i]); } rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); @@ -1049,8 +1072,8 @@ write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) } if (ble_gattc_write_no_rsp_flat(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), cmd->data, - sys_le16_to_cpu(cmd->data_length))) { + le16toh(cmd->handle), cmd->data, + le16toh(cmd->data_length))) { status = BTP_STATUS_FAILED; } @@ -1078,8 +1101,11 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -1110,8 +1136,8 @@ write(uint8_t *data, uint16_t len) goto rsp; } - if (ble_gattc_write_flat(conn.conn_handle, sys_le16_to_cpu(cmd->handle), - cmd->data, sys_le16_to_cpu(cmd->data_length), + if (ble_gattc_write_flat(conn.conn_handle, le16toh(cmd->handle), + cmd->data, le16toh(cmd->data_length), write_cb, (void *) BTP_GATTC_WRITE_RP)) { status = BTP_STATUS_FAILED; } @@ -1136,7 +1162,7 @@ write_long(uint8_t *data, uint16_t len) goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, sys_le16_to_cpu(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); if (!om) { SYS_LOG_ERR("Insufficient resources"); status = BTP_STATUS_FAILED; @@ -1144,8 +1170,8 @@ write_long(uint8_t *data, uint16_t len) } rc = ble_gattc_write_long(conn.conn_handle, - sys_le16_to_cpu(cmd->handle), - sys_le16_to_cpu(cmd->offset), + le16toh(cmd->handle), + le16toh(cmd->offset), om, write_cb, (void *) BTP_GATTC_WRITE_LONG_RP); if (rc) { @@ -1182,8 +1208,11 @@ reliable_write_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -1216,17 +1245,17 @@ reliable_write(uint8_t *data, uint16_t len) goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, sys_le16_to_cpu(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); /* This is required, because Nimble checks if * the data is longer than offset */ - if (os_mbuf_extend(om, sys_le16_to_cpu(cmd->offset) + 1) == NULL) { + if (os_mbuf_extend(om, le16toh(cmd->offset) + 1) == NULL) { status = BTP_STATUS_FAILED; goto fail; } - attr.handle = sys_le16_to_cpu(cmd->handle); - attr.offset = sys_le16_to_cpu(cmd->offset); + attr.handle = le16toh(cmd->handle); + attr.offset = le16toh(cmd->offset); attr.om = om; if (ble_gattc_write_reliable(conn.conn_handle, &attr, 1, @@ -1264,8 +1293,11 @@ subscribe_cb(uint16_t conn_handle, goto free; } - net_buf_simple_init(buf, 0); - rp = net_buf_simple_add(buf, sizeof(*rp)); + rp = os_mbuf_extend(buf, sizeof(*rp)); + if (!rp) { + rc = BLE_HS_ENOMEM; + goto free; + } addr = &conn.peer_ota_addr; @@ -1340,7 +1372,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) { const struct btp_gattc_cfg_notify_cmd *cmd = (void *) data; struct ble_gap_conn_desc conn; - uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); + uint16_t ccc_handle = le16toh(cmd->ccc_handle); uint8_t status = BTP_STATUS_SUCCESS; int rc; @@ -1399,16 +1431,18 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, goto fail; } - net_buf_simple_init(buf, 0); - ev = net_buf_simple_add(buf, sizeof(*ev)); + ev = os_mbuf_extend(buf, sizeof(*ev)); + if (!ev) { + return 0; + } addr = &conn.peer_ota_addr; ev->address_type = addr->type; memcpy(ev->address, addr->val, sizeof(ev->address)); ev->type = (uint8_t) (indication ? 0x02 : 0x01); - ev->handle = sys_cpu_to_le16(attr_handle); - ev->data_length = sys_cpu_to_le16(os_mbuf_len(om)); + ev->handle = htole16(attr_handle); + ev->data_length = htole16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_NOTIFICATION_RXED, diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index 03d70688a2..498d22604b 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -421,7 +421,7 @@ connect(uint8_t *data, uint16_t len) SYS_LOG_DBG("connect: type: %d addr: %s", addr->type, - bt_hex(addr->val, 6)); + string_from_bytes(addr->val, 6)); if (mtu == 0 || mtu > TESTER_COC_MTU) { mtu = TESTER_COC_MTU; @@ -523,7 +523,7 @@ send_data(const uint8_t *data, uint16_t len) const struct btp_l2cap_send_data_cmd *cmd = (void *) data; struct os_mbuf *sdu_tx = NULL; int rc; - uint16_t data_len = sys_le16_to_cpu(cmd->data_len); + uint16_t data_len = le16toh(cmd->data_len); struct channel *chan = get_channel(cmd->chan_id); SYS_LOG_DBG("cmd->chan_id=%d", cmd->chan_id); @@ -567,7 +567,7 @@ static void listen(const uint8_t *data, uint16_t len) { const struct btp_l2cap_listen_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + uint16_t mtu = htole16(cmd->mtu); int rc; SYS_LOG_DBG(""); @@ -625,7 +625,7 @@ static void reconfigure(const uint8_t *data, uint16_t len) { const struct btp_l2cap_reconfigure_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + uint16_t mtu = htole16(cmd->mtu); struct ble_gap_conn_desc desc; ble_addr_t *addr = (void *) data; struct ble_l2cap_chan *chans[cmd->num]; diff --git a/apps/bttester/src/btp_mesh.c b/apps/bttester/src/btp_mesh.c index 3e0668ea74..7642f8ea7f 100644 --- a/apps/bttester/src/btp_mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -318,8 +318,8 @@ output_number(bt_mesh_output_action_t action, uint32_t number) SYS_LOG_DBG("action 0x%04x number 0x%08lx", action, number); - ev.action = sys_cpu_to_le16(action); - ev.number = sys_cpu_to_le32(number); + ev.action = htole16(action); + ev.number = htole32(number); tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, (uint8_t *) &ev, sizeof(ev)); @@ -358,7 +358,7 @@ input(bt_mesh_input_action_t action, uint8_t size) input_size = size; - ev.action = sys_cpu_to_le16(action); + ev.action = htole16(action); ev.size = size; tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, @@ -441,7 +441,7 @@ provision_node(uint8_t *data, uint16_t len) addr = sys_le16_to_cpu(cmd->addr); flags = cmd->flags; - iv_index = sys_le32_to_cpu(cmd->iv_index); + iv_index = le32toh(cmd->iv_index); net_key_idx = sys_le16_to_cpu(cmd->net_key_idx); tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROVISION_NODE, @@ -499,7 +499,7 @@ input_number(uint8_t *data, uint16_t len) uint32_t number; int err; - number = sys_le32_to_cpu(cmd->number); + number = le32toh(cmd->number); SYS_LOG_DBG("number 0x%04lx", number); @@ -896,8 +896,8 @@ net_recv_ev(uint8_t ttl, ev = net_buf_simple_add(buf, sizeof(*ev)); ev->ttl = ttl; ev->ctl = ctl; - ev->src = sys_cpu_to_le16(src); - ev->dst = sys_cpu_to_le16(dst); + ev->src = htole16(src); + ev->dst = htole16(dst); ev->payload_len = payload_len; net_buf_simple_add_mem(buf, payload, payload_len); diff --git a/apps/bttester/src/bttester.c b/apps/bttester/src/bttester.c index f0d0886302..7245bcb461 100644 --- a/apps/bttester/src/bttester.c +++ b/apps/bttester/src/bttester.c @@ -57,6 +57,14 @@ static struct { uint8_t num; } service_handler[BTP_SERVICE_ID_MAX + 1]; + +void +tester_mbuf_reset(struct os_mbuf *buf) +{ + buf->om_data = &buf->om_databuf[buf->om_pkthdr_len]; + buf->om_len = 0; +} + static void tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, uint8_t *data, size_t len); @@ -69,13 +77,38 @@ tester_register_command_handlers(uint8_t service, const struct btp_handler *handlers, size_t num) { - __ASSERT_NO_MSG(service <= BTP_SERVICE_ID_MAX); - __ASSERT_NO_MSG(service_handler[service].handlers == NULL); + assert(service <= BTP_SERVICE_ID_MAX); + assert(service_handler[service].handlers == NULL); service_handler[service].handlers = handlers; service_handler[service].num = num; } +const char * +string_from_bytes(const void *buf, size_t len) +{ + static const char hex[] = "0123456789abcdef"; + static char hexbufs[4][137]; + static uint8_t curbuf; + const uint8_t *b = buf; + char *str; + int i; + + str = hexbufs[curbuf++]; + curbuf %= ARRAY_SIZE(hexbufs); + + len = min(len, (sizeof(hexbufs[0]) - 1) / 2); + + for (i = 0; i < len; i++) { + str[i * 2] = hex[b[i] >> 4]; + str[i * 2 + 1] = hex[b[i] & 0xf]; + } + + str[i * 2] = '\0'; + + return str; +} + static const struct btp_handler * find_btp_handler(uint8_t service, uint8_t opcode) { @@ -108,12 +141,12 @@ cmd_handler(struct os_event *ev) cmd = ev->ev_arg; - len = sys_le16_to_cpu(cmd->hdr.len); + len = le16toh(cmd->hdr.len); if (MYNEWT_VAL(BTTESTER_BTP_LOG)) { console_printf("[DBG] received %d bytes: %s\n", sizeof(cmd->hdr) + len, - bt_hex(cmd->data, - sizeof(cmd->hdr) + len)); + string_from_bytes(cmd->data, + sizeof(cmd->hdr) + len)); } btp = find_btp_handler(cmd->hdr.service, cmd->hdr.opcode); @@ -127,7 +160,7 @@ cmd_handler(struct os_event *ev) cmd->rsp, &rsp_len); } - __ASSERT_NO_MSG((rsp_len + sizeof(struct btp_hdr)) <= BTP_MTU); + assert((rsp_len + sizeof(struct btp_hdr)) <= BTP_MTU); } else { status = BTP_STATUS_UNKNOWN_CMD; } @@ -157,7 +190,7 @@ recv_cb(uint8_t *buf, size_t *off) return buf; } - len = sys_le16_to_cpu(cmd->len); + len = le16toh(cmd->len); if (len > BTP_MTU - sizeof(*cmd)) { *off = 0; return buf; @@ -239,7 +272,7 @@ tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, msg.service = service; msg.opcode = opcode; msg.index = index; - msg.len = sys_cpu_to_le16(len); + msg.len = htole16(len); bttester_pipe_send((uint8_t *) &msg, sizeof(msg)); if (data && len) { @@ -248,10 +281,10 @@ tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, if (MYNEWT_VAL(BTTESTER_BTP_LOG)) { console_printf("[DBG] send %d bytes hdr: %s\n", sizeof(msg), - bt_hex((char *) &msg, sizeof(msg))); + string_from_bytes((char *) &msg, sizeof(msg))); if (data && len) { console_printf("[DBG] send %d bytes data: %s\n", len, - bt_hex((char *) data, len)); + string_from_bytes((char *) data, len)); } } } diff --git a/apps/bttester/src/glue.c b/apps/bttester/src/glue.c deleted file mode 100644 index f4c838184b..0000000000 --- a/apps/bttester/src/glue.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "syscfg/syscfg.h" - -#if !MYNEWT_VAL(BLE_MESH) -#include -#include -#include "os/os.h" -#include "os/os_mbuf.h" -#include "glue.h" - - -#define ASSERT_NOT_CHAIN(om) assert(SLIST_NEXT(om, om_next) == NULL) - -const char *bt_hex(const void *buf, size_t len) -{ - static const char hex[] = "0123456789abcdef"; - static char hexbufs[4][137]; - static uint8_t curbuf; - const uint8_t *b = buf; - char *str; - int i; - - str = hexbufs[curbuf++]; - curbuf %= ARRAY_SIZE(hexbufs); - - len = min(len, (sizeof(hexbufs[0]) - 1) / 2); - - for (i = 0; i < len; i++) { - str[i * 2] = hex[b[i] >> 4]; - str[i * 2 + 1] = hex[b[i] & 0xf]; - } - - str[i * 2] = '\0'; - - return str; -} - -struct os_mbuf * NET_BUF_SIMPLE(uint16_t size) -{ - struct os_mbuf *buf; - - buf = os_msys_get(size, 0); - assert(buf); - - return buf; -} - -/* This is by purpose */ -void net_buf_simple_init(struct os_mbuf *buf, - size_t reserve_head) -{ - /* This is called in Zephyr after init. - * Note in Mynewt case we don't care abour reserved head*/ - buf->om_data = &buf->om_databuf[buf->om_pkthdr_len] + reserve_head; - buf->om_len = 0; -} - -void -net_buf_simple_add_le16(struct os_mbuf *om, uint16_t val) -{ - val = htole16(val); - os_mbuf_append(om, &val, sizeof(val)); - ASSERT_NOT_CHAIN(om); -} - -void -net_buf_simple_add_be16(struct os_mbuf *om, uint16_t val) -{ - val = htobe16(val); - os_mbuf_append(om, &val, sizeof(val)); - ASSERT_NOT_CHAIN(om); -} - -void -net_buf_simple_add_be32(struct os_mbuf *om, uint32_t val) -{ - val = htobe32(val); - os_mbuf_append(om, &val, sizeof(val)); - ASSERT_NOT_CHAIN(om); -} - -void -net_buf_simple_add_u8(struct os_mbuf *om, uint8_t val) -{ - os_mbuf_append(om, &val, 1); - ASSERT_NOT_CHAIN(om); -} - -void* -net_buf_simple_add(struct os_mbuf *om, uint8_t len) -{ - void * tmp; - - tmp = os_mbuf_extend(om, len); - ASSERT_NOT_CHAIN(om); - - return tmp; -} - -uint8_t * -net_buf_simple_push(struct os_mbuf *om, uint8_t len) -{ - uint8_t headroom = om->om_data - &om->om_databuf[om->om_pkthdr_len]; - - assert(headroom >= len); - om->om_data -= len; - om->om_len += len; - - return om->om_data; -} -#endif diff --git a/apps/bttester/src/glue.h b/apps/bttester/src/glue.h deleted file mode 100644 index 024f795643..0000000000 --- a/apps/bttester/src/glue.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef __GLUE_H__ -#define __GLUE_H__ - -#include "os/endian.h" - -#define uint8_t uint8_t -#define int8_t int8_t -#define uint16_t uint16_t -#define uint32_t uint32_t -#define int32_t int32_t - -#ifndef BIT -#define BIT(n) (1UL << (n)) -#endif - -#define __packed __attribute__((__packed__)) - -#define sys_le16_to_cpu le16toh - -struct bt_data { - uint8_t type; - uint8_t data_len; - const uint8_t *data; -}; - -#define BT_DATA(_type, _data, _data_len) \ - { \ - .type = (_type), \ - .data_len = (_data_len), \ - .data = (const uint8_t *)(_data), \ - } - -struct os_mbuf * -NET_BUF_SIMPLE(uint16_t size); -void -net_buf_simple_init(struct os_mbuf *buf, size_t reserve_head); -void -net_buf_simple_add_le16(struct os_mbuf *om, uint16_t val); -void -net_buf_simple_add_u8(struct os_mbuf *om, uint8_t val); -void * -net_buf_simple_add(struct os_mbuf *om, uint8_t len); -uint8_t * -net_buf_simple_push(struct os_mbuf *om, uint8_t len); - -#define net_buf_simple_add_mem(a, b, c) os_mbuf_append(a,b,c) - -const char * -bt_hex(const void *buf, size_t len); - -/** - * INTERNAL_HIDDEN @endcond - */ - -/** - * @brief Define an array of atomic variables. - * - * This macro defines an array of atomic variables containing at least - * @a num_bits bits. - * - * @note - * If used from file scope, the bits of the array are initialized to zero; - * if used from within a function, the bits are left uninitialized. - * - * @param name Name of array of atomic variables. - * @param num_bits Number of bits needed. - */ -#define ATOMIC_DEFINE(name, num_bits) \ - atomic_t name[1 + ((num_bits) - 1) / ATOMIC_BITS] -#endif /* __GLUE_H__ */ From 741a3be285196f7bd2200ec236bdf6e40cbfdcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Mon, 10 Jul 2023 14:10:11 +0200 Subject: [PATCH 04/12] apps/bttester: drop atomic.h There is no need of porting extra atomic API in tester. --- apps/bttester/src/atomic.h | 417 ------------------------------- apps/bttester/src/btp/bttester.h | 8 + apps/bttester/src/btp_core.c | 13 +- 3 files changed, 14 insertions(+), 424 deletions(-) delete mode 100644 apps/bttester/src/atomic.h diff --git a/apps/bttester/src/atomic.h b/apps/bttester/src/atomic.h deleted file mode 100644 index 4376ad5bb7..0000000000 --- a/apps/bttester/src/atomic.h +++ /dev/null @@ -1,417 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* atomic operations */ - -/* - * Copyright (c) 1997-2015, Wind River Systems, Inc. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef __ATOMIC_H__ -#define __ATOMIC_H__ - -#ifdef __cplusplus -extern "C" -{ -#endif - -typedef int atomic_t; -typedef atomic_t atomic_val_t; - -/** - * @defgroup atomic_apis Atomic Services APIs - * @ingroup kernel_apis - * @{ - */ - -/** - * @brief Atomic compare-and-set. - * - * This routine performs an atomic compare-and-set on @a target. If the current - * value of @a target equals @a old_value, @a target is set to @a new_value. - * If the current value of @a target does not equal @a old_value, @a target - * is left unchanged. - * - * @param target Address of atomic variable. - * @param old_value Original value to compare against. - * @param new_value New value to store. - * @return 1 if @a new_value is written, 0 otherwise. - */ -static inline int -atomic_cas(atomic_t *target, atomic_val_t old_value, - atomic_val_t new_value) -{ - return __atomic_compare_exchange_n(target, &old_value, new_value, - 0, __ATOMIC_SEQ_CST, - __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic addition. - * - * This routine performs an atomic addition on @a target. - * - * @param target Address of atomic variable. - * @param value Value to add. - * - * @return Previous value of @a target. - */ -static inline atomic_val_t -atomic_add(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic subtraction. - * - * This routine performs an atomic subtraction on @a target. - * - * @param target Address of atomic variable. - * @param value Value to subtract. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_sub(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic increment. - * - * This routine performs an atomic increment by 1 on @a target. - * - * @param target Address of atomic variable. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_inc(atomic_t *target) -{ - return atomic_add(target, 1); -} - -/** - * - * @brief Atomic decrement. - * - * This routine performs an atomic decrement by 1 on @a target. - * - * @param target Address of atomic variable. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_dec(atomic_t *target) -{ - return atomic_sub(target, 1); -} - -/** - * - * @brief Atomic get. - * - * This routine performs an atomic read on @a target. - * - * @param target Address of atomic variable. - * - * @return Value of @a target. - */ - -static inline atomic_val_t -atomic_get(const atomic_t *target) -{ - return __atomic_load_n(target, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic get-and-set. - * - * This routine atomically sets @a target to @a value and returns - * the previous value of @a target. - * - * @param target Address of atomic variable. - * @param value Value to write to @a target. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_set(atomic_t *target, atomic_val_t value) -{ - /* This builtin, as described by Intel, is not a traditional - * test-and-set operation, but rather an atomic exchange operation. It - * writes value into *ptr, and returns the previous contents of *ptr. - */ - return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic clear. - * - * This routine atomically sets @a target to zero and returns its previous - * value. (Hence, it is equivalent to atomic_set(target, 0).) - * - * @param target Address of atomic variable. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_clear(atomic_t *target) -{ - return atomic_set(target, 0); -} - -/** - * - * @brief Atomic bitwise inclusive OR. - * - * This routine atomically sets @a target to the bitwise inclusive OR of - * @a target and @a value. - * - * @param target Address of atomic variable. - * @param value Value to OR. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_or(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic bitwise exclusive OR (XOR). - * - * This routine atomically sets @a target to the bitwise exclusive OR (XOR) of - * @a target and @a value. - * - * @param target Address of atomic variable. - * @param value Value to XOR - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_xor(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic bitwise AND. - * - * This routine atomically sets @a target to the bitwise AND of @a target - * and @a value. - * - * @param target Address of atomic variable. - * @param value Value to AND. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_and(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); -} - -/** - * - * @brief Atomic bitwise NAND. - * - * This routine atomically sets @a target to the bitwise NAND of @a target - * and @a value. (This operation is equivalent to target = ~(target & value).) - * - * @param target Address of atomic variable. - * @param value Value to NAND. - * - * @return Previous value of @a target. - */ - -static inline atomic_val_t -atomic_nand(atomic_t *target, atomic_val_t value) -{ - return __atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST); -} - -/** - * @brief Initialize an atomic variable. - * - * This macro can be used to initialize an atomic variable. For example, - * @code atomic_t my_var = ATOMIC_INIT(75); @endcode - * - * @param i Value to assign to atomic variable. - */ -#define ATOMIC_INIT(i) (i) - -/** - * @cond INTERNAL_HIDDEN - */ - -#define ATOMIC_BITS (sizeof(atomic_val_t) * 8) -#define ATOMIC_MASK(bit) (1 << ((bit) & (ATOMIC_BITS - 1))) -#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS)) - -/** - * INTERNAL_HIDDEN @endcond - */ - -/** - * @brief Define an array of atomic variables. - * - * This macro defines an array of atomic variables containing at least - * @a num_bits bits. - * - * @note - * If used from file scope, the bits of the array are initialized to zero; - * if used from within a function, the bits are left uninitialized. - * - * @param name Name of array of atomic variables. - * @param num_bits Number of bits needed. - */ -#define ATOMIC_DEFINE(name, num_bits) \ - atomic_t name[1 + ((num_bits) - 1) / ATOMIC_BITS] - -/** - * @brief Atomically test a bit. - * - * This routine tests whether bit number @a bit of @a target is set or not. - * The target may be a single atomic variable or an array of them. - * - * @param target Address of atomic variable or array. - * @param bit Bit number (starting from 0). - * - * @return 1 if the bit was set, 0 if it wasn't. - */ -static inline int -atomic_test_bit(const atomic_t *target, int bit) -{ - atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit)); - - return (1 & (val >> (bit & (ATOMIC_BITS - 1)))); -} - -/** - * @brief Atomically test and clear a bit. - * - * Atomically clear bit number @a bit of @a target and return its old value. - * The target may be a single atomic variable or an array of them. - * - * @param target Address of atomic variable or array. - * @param bit Bit number (starting from 0). - * - * @return 1 if the bit was set, 0 if it wasn't. - */ -static inline int -atomic_test_and_clear_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - atomic_val_t old; - - old = atomic_and(ATOMIC_ELEM(target, bit), ~mask); - - return (old & mask) != 0; -} - -/** - * @brief Atomically set a bit. - * - * Atomically set bit number @a bit of @a target and return its old value. - * The target may be a single atomic variable or an array of them. - * - * @param target Address of atomic variable or array. - * @param bit Bit number (starting from 0). - * - * @return 1 if the bit was set, 0 if it wasn't. - */ -static inline int -atomic_test_and_set_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - atomic_val_t old; - - old = atomic_or(ATOMIC_ELEM(target, bit), mask); - - return (old & mask) != 0; -} - -/** - * @brief Atomically clear a bit. - * - * Atomically clear bit number @a bit of @a target. - * The target may be a single atomic variable or an array of them. - * - * @param target Address of atomic variable or array. - * @param bit Bit number (starting from 0). - * - * @return N/A - */ -static inline void -atomic_clear_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - - atomic_and(ATOMIC_ELEM(target, bit), ~mask); -} - -/** - * @brief Atomically set a bit. - * - * Atomically set bit number @a bit of @a target. - * The target may be a single atomic variable or an array of them. - * - * @param target Address of atomic variable or array. - * @param bit Bit number (starting from 0). - * - * @return N/A - */ -static inline void -atomic_set_bit(atomic_t *target, int bit) -{ - atomic_val_t mask = ATOMIC_MASK(bit); - - atomic_or(ATOMIC_ELEM(target, bit), mask); -} - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __ATOMIC_H__ */ diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 3aed917796..ea24600c19 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -59,6 +59,14 @@ tester_test_bit(const uint8_t *addr, unsigned int bit) return *p & BIT(bit % 8); } +static inline void +tester_clear_bit(uint8_t *addr, unsigned int bit) +{ + uint8_t *p = addr + (bit / 8); + + *p &= ~BIT(bit % 8); +} + void tester_init(void); diff --git a/apps/bttester/src/btp_core.c b/apps/bttester/src/btp_core.c index 4aee11d347..fe1b8d4450 100644 --- a/apps/bttester/src/btp_core.c +++ b/apps/bttester/src/btp_core.c @@ -26,9 +26,8 @@ */ #include "btp/btp.h" -#include "atomic.h" -static ATOMIC_DEFINE(registered_services, BTP_SERVICE_ID_MAX); +static uint8_t registered_services[((BTP_SERVICE_ID_MAX - 1) / 8) + 1]; static uint8_t supported_commands(const void *cmd, uint16_t cmd_len, @@ -82,7 +81,7 @@ register_service(const void *cmd, uint16_t cmd_len, } /* already registered */ - if (atomic_test_bit(registered_services, cp->id)) { + if (tester_test_bit(registered_services, cp->id)) { return BTP_STATUS_FAILED; } @@ -109,7 +108,7 @@ register_service(const void *cmd, uint16_t cmd_len, } if (status == BTP_STATUS_SUCCESS) { - atomic_set_bit(registered_services, cp->id); + tester_set_bit(registered_services, cp->id); } return status; @@ -128,7 +127,7 @@ unregister_service(const void *cmd, uint16_t cmd_len, } /* not registered */ - if (!atomic_test_bit(registered_services, cp->id)) { + if (!tester_test_bit(registered_services, cp->id)) { return BTP_STATUS_FAILED; } @@ -155,7 +154,7 @@ unregister_service(const void *cmd, uint16_t cmd_len, } if (status == BTP_STATUS_SUCCESS) { - atomic_clear_bit(registered_services, cp->id); + tester_clear_bit(registered_services, cp->id); } return status; @@ -193,5 +192,5 @@ tester_init_core(void) { tester_register_command_handlers(BTP_SERVICE_ID_CORE, handlers, ARRAY_SIZE(handlers)); - atomic_set_bit(registered_services, BTP_SERVICE_ID_CORE); + tester_set_bit(registered_services, BTP_SERVICE_ID_CORE); } From 7bccd88f86bfaa993a5fd82cc9fab3a1e7172e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Tue, 11 Jul 2023 08:36:47 +0200 Subject: [PATCH 05/12] apps/bttester: use ble_addr_t whenever possible We should use ble_addr_t instead of pair {address_type, address} whenever possible, as suggested in 35c71a4dcd2ad859e66746088042c50b8a0add39 This simplifies code to single memcpy. --- apps/bttester/src/btp/btp_gap.h | 65 +++++++++----------------- apps/bttester/src/btp/btp_gatt.h | 54 +++++++-------------- apps/bttester/src/btp/btp_gattc.h | 78 +++++++++++-------------------- apps/bttester/src/btp/btp_l2cap.h | 15 ++---- apps/bttester/src/btp_gap.c | 55 +++++++++------------- apps/bttester/src/btp_gatt_cl.c | 72 +++++----------------------- apps/bttester/src/btp_l2cap.c | 8 +--- 7 files changed, 106 insertions(+), 241 deletions(-) diff --git a/apps/bttester/src/btp/btp_gap.h b/apps/bttester/src/btp/btp_gap.h index ca60b81037..5e3e1b8372 100644 --- a/apps/bttester/src/btp/btp_gap.h +++ b/apps/bttester/src/btp/btp_gap.h @@ -167,15 +167,13 @@ struct btp_gap_start_discovery_cmd { #define BTP_GAP_CONNECT 0x0e struct btp_gap_connect_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t own_addr_type; } __packed; #define BTP_GAP_DISCONNECT 0x0f struct btp_gap_disconnect_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_IO_CAP_DISPLAY_ONLY 0 @@ -191,41 +189,35 @@ struct btp_gap_set_io_cap_cmd { #define BTP_GAP_PAIR 0x11 struct btp_gap_pair_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_UNPAIR 0x12 struct btp_gap_unpair_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_PASSKEY_ENTRY 0x13 struct btp_gap_passkey_entry_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint32_t passkey; } __packed; #define BTP_GAP_PASSKEY_CONFIRM 0x14 struct btp_gap_passkey_confirm_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t match; } __packed; #define BTP_GAP_START_DIRECT_ADV 0x15 struct btp_gap_start_direct_adv_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t high_duty; } __packed; #define BTP_GAP_CONN_PARAM_UPDATE 0x16 struct btp_gap_conn_param_update_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t conn_itvl_min; uint16_t conn_itvl_max; uint16_t conn_latency; @@ -234,8 +226,7 @@ struct btp_gap_conn_param_update_cmd { #define BTP_GAP_PAIRING_CONSENT_RSP 0x17 struct btp_gap_pairing_consent_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t consent; } __packed; @@ -278,8 +269,7 @@ struct btp_gap_new_settings_ev { #define BTP_GAP_EV_DEVICE_FOUND 0x81 struct btp_gap_device_found_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; int8_t rssi; uint8_t flags; uint16_t eir_data_len; @@ -288,8 +278,7 @@ struct btp_gap_device_found_ev { #define BTP_GAP_EV_DEVICE_CONNECTED 0x82 struct btp_gap_device_connected_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t conn_itvl; uint16_t conn_latency; uint16_t supervision_timeout; @@ -297,42 +286,36 @@ struct btp_gap_device_connected_ev { #define BTP_GAP_EV_DEVICE_DISCONNECTED 0x83 struct btp_gap_device_disconnected_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_EV_PASSKEY_DISPLAY 0x84 struct btp_gap_passkey_display_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint32_t passkey; } __packed; #define BTP_GAP_EV_PASSKEY_ENTRY_REQ 0x85 struct btp_gap_passkey_entry_req_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_EV_PASSKEY_CONFIRM_REQ 0x86 struct btp_gap_passkey_confirm_req_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint32_t passkey; } __packed; #define BTP_GAP_EV_IDENTITY_RESOLVED 0x87 struct btp_gap_identity_resolved_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t identity_address_type; - uint8_t identity_address[6]; + uint8_t identity_address; } __packed; #define BTP_GAP_EV_CONN_PARAM_UPDATE 0x88 struct btp_gap_conn_param_update_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t conn_itvl; uint16_t conn_latency; uint16_t supervision_timeout; @@ -340,26 +323,22 @@ struct btp_gap_conn_param_update_ev { #define BTP_GAP_EV_SEC_LEVEL_CHANGED 0x89 struct btp_gap_sec_level_changed_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t level; } __packed; #define BTP_GAP_EV_PAIRING_CONSENT_REQ 0x8a struct btp_gap_pairing_consent_req_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_EV_BOND_LOST 0x8b struct btp_gap_bond_lost_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GAP_EV_SEC_PAIRING_FAILED 0x8c struct btp_gap_sec_pairing_failed_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t reason; } __packed; diff --git a/apps/bttester/src/btp/btp_gatt.h b/apps/bttester/src/btp/btp_gatt.h index 6e23366460..41f5ae7ec2 100644 --- a/apps/bttester/src/btp/btp_gatt.h +++ b/apps/bttester/src/btp/btp_gatt.h @@ -131,8 +131,7 @@ struct btp_gatt_descriptor { #define BTP_GATT_DISC_ALL_PRIM_SVCS 0x0b struct btp_gatt_disc_all_prim_svcs_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; struct btp_gatt_disc_all_prim_svcs_rp { uint8_t services_count; @@ -141,8 +140,7 @@ struct btp_gatt_disc_all_prim_svcs_rp { #define BTP_GATT_DISC_PRIM_UUID 0x0c struct btp_gatt_disc_prim_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t uuid_length; uint8_t uuid[0]; } __packed; @@ -153,8 +151,7 @@ struct btp_gatt_disc_prim_uuid_rp { #define BTP_GATT_FIND_INCLUDED 0x0d struct btp_gatt_find_included_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; @@ -165,8 +162,7 @@ struct btp_gatt_find_included_rp { #define BTP_GATT_DISC_ALL_CHRC 0x0e struct btp_gatt_disc_all_chrc_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; @@ -177,8 +173,7 @@ struct btp_gatt_disc_chrc_rp { #define BTP_GATT_DISC_CHRC_UUID 0x0f struct btp_gatt_disc_chrc_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; uint8_t uuid_length; @@ -187,8 +182,7 @@ struct btp_gatt_disc_chrc_uuid_cmd { #define BTP_GATT_DISC_ALL_DESC 0x10 struct btp_gatt_disc_all_desc_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; @@ -199,8 +193,7 @@ struct btp_gatt_disc_all_desc_rp { #define BTP_GATT_READ 0x11 struct btp_gatt_read_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; } __packed; struct btp_gatt_read_rp { @@ -211,8 +204,7 @@ struct btp_gatt_read_rp { #define BTP_GATT_READ_UUID 0x12 struct btp_gatt_read_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; uint8_t uuid_length; @@ -221,24 +213,21 @@ struct btp_gatt_read_uuid_cmd { #define BTP_GATT_READ_LONG 0x13 struct btp_gatt_read_long_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; } __packed; #define BTP_GATT_READ_MULTIPLE 0x14 struct btp_gatt_read_multiple_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t handles_count; uint16_t handles[0]; } __packed; #define BTP_GATT_WRITE_WITHOUT_RSP 0x15 struct btp_gatt_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -246,8 +235,7 @@ struct btp_gatt_write_without_rsp_cmd { #define BTP_GATT_SIGNED_WRITE_WITHOUT_RSP 0x16 struct btp_gatt_signed_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -255,8 +243,7 @@ struct btp_gatt_signed_write_without_rsp_cmd { #define BTP_GATT_WRITE 0x17 struct btp_gatt_write_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -264,8 +251,7 @@ struct btp_gatt_write_cmd { #define BTP_GATT_WRITE_LONG 0x18 struct btp_gatt_write_long_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; uint16_t data_length; @@ -274,8 +260,7 @@ struct btp_gatt_write_long_cmd { #define BTP_GATT_RELIABLE_WRITE 0x19 struct btp_gatt_reliable_write_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; uint16_t data_length; @@ -285,8 +270,7 @@ struct btp_gatt_reliable_write_cmd { #define BTP_GATT_CFG_NOTIFY 0x1a #define BTP_GATT_CFG_INDICATE 0x1b struct btp_gatt_cfg_notify_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t enable; uint16_t ccc_handle; } __packed; @@ -311,8 +295,7 @@ struct btp_gatt_attr { #define BTP_GATT_GET_ATTRIBUTE_VALUE 0x1d struct btp_gatt_get_attribute_value_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; } __packed; struct btp_gatt_get_attribute_value_rp { @@ -331,8 +314,7 @@ struct btp_gatt_change_database { /* GATT events */ #define BTP_GATT_EV_NOTIFICATION 0x80 struct btp_gatt_notification_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t type; uint16_t handle; uint16_t data_length; diff --git a/apps/bttester/src/btp/btp_gattc.h b/apps/bttester/src/btp/btp_gattc.h index 0b946a52b1..7c642fdd67 100644 --- a/apps/bttester/src/btp/btp_gattc.h +++ b/apps/bttester/src/btp/btp_gattc.h @@ -37,38 +37,33 @@ struct btp_gattc_read_supported_commands_rp { #define BTP_GATTC_DISC_ALL_PRIM_SVCS 0x03 struct btp_gattc_disc_all_prim_svcs_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_GATTC_DISC_PRIM_UUID 0x04 struct btp_gattc_disc_prim_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t uuid_length; uint8_t uuid[0]; } __packed; #define BTP_GATTC_FIND_INCLUDED 0x05 struct btp_gattc_find_included_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; #define BTP_GATTC_DISC_ALL_CHRC 0x06 struct btp_gattc_disc_all_chrc_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; #define BTP_GATTC_DISC_CHRC_UUID 0x07 struct btp_gattc_disc_chrc_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; uint8_t uuid_length; @@ -77,31 +72,27 @@ struct btp_gattc_disc_chrc_uuid_cmd { #define BTP_GATTC_DISC_ALL_DESC 0x08 struct btp_gattc_disc_all_desc_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; } __packed; #define BTP_GATTC_READ 0x09 struct btp_gattc_read_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; } __packed; #define BTP_GATTC_READ_UUID 0x0a struct btp_gattc_read_uuid_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t start_handle; uint16_t end_handle; uint8_t uuid_length; uint8_t uuid[0]; } __packed; struct btp_gattc_read_uuid_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint16_t data_length; uint8_t value_length; @@ -110,24 +101,21 @@ struct btp_gattc_read_uuid_rp { #define BTP_GATTC_READ_LONG 0x0b struct btp_gattc_read_long_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; } __packed; #define BTP_GATTC_READ_MULTIPLE 0x0c struct btp_gattc_read_multiple_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t handles_count; uint16_t handles[0]; } __packed; #define BTP_GATTC_WRITE_WITHOUT_RSP 0x0d struct btp_gattc_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -135,8 +123,7 @@ struct btp_gattc_write_without_rsp_cmd { #define BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP 0x0e struct btp_gattc_signed_write_without_rsp_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -144,8 +131,7 @@ struct btp_gattc_signed_write_without_rsp_cmd { #define BTP_GATTC_WRITE 0x0f struct btp_gattc_write_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t data_length; uint8_t data[0]; @@ -153,8 +139,7 @@ struct btp_gattc_write_cmd { #define BTP_GATTC_WRITE_LONG 0x10 struct btp_gattc_write_long_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; uint16_t data_length; @@ -163,8 +148,7 @@ struct btp_gattc_write_long_cmd { #define BTP_GATTC_RELIABLE_WRITE 0x11 struct btp_gattc_reliable_write_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t handle; uint16_t offset; uint16_t data_length; @@ -174,8 +158,7 @@ struct btp_gattc_reliable_write_cmd { #define BTP_GATTC_CFG_NOTIFY 0x12 #define BTP_GATTC_CFG_INDICATE 0x13 struct btp_gattc_cfg_notify_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t enable; uint16_t ccc_handle; } __packed; @@ -183,15 +166,13 @@ struct btp_gattc_cfg_notify_cmd { /* events */ #define BTP_GATTC_EV_MTU_EXCHANGED 0x80 struct btp_gattc_exchange_mtu_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t mtu; } __packed; #define BTP_GATTC_DISC_ALL_PRIM_RP 0x81 struct btp_gattc_disc_prim_svcs_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint8_t services_count; uint8_t data[0]; @@ -201,8 +182,7 @@ struct btp_gattc_disc_prim_svcs_rp { #define BTP_GATTC_FIND_INCLUDED_RP 0x83 struct btp_gattc_find_included_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint8_t services_count; struct btp_gatt_included included[0]; @@ -211,8 +191,7 @@ struct btp_gattc_find_included_rp { #define BTP_GATTC_DISC_ALL_CHRC_RP 0x84 #define BTP_GATTC_DISC_CHRC_UUID_RP 0x85 struct btp_gattc_disc_chrc_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint8_t characteristics_count; struct btp_gatt_characteristic characteristics[0]; @@ -220,8 +199,7 @@ struct btp_gattc_disc_chrc_rp { #define BTP_GATTC_DISC_ALL_DESC_RP 0x86 struct btp_gattc_disc_all_desc_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint8_t descriptors_count; struct btp_gatt_descriptor descriptors[0]; @@ -232,8 +210,7 @@ struct btp_gattc_disc_all_desc_rp { #define BTP_GATTC_READ_LONG_RP 0x89 #define BTP_GATTC_READ_MULTIPLE_RP 0x8a struct btp_gattc_read_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; uint16_t data_length; uint8_t data[0]; @@ -241,8 +218,7 @@ struct btp_gattc_read_rp { #define BTP_GATTC_WRITE_RP 0x8b struct btp_gattc_write_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; } __packed; #define BTP_GATTC_WRITE_LONG_RP 0x8c @@ -250,15 +226,13 @@ struct btp_gattc_write_rp { #define BTP_GATTC_CFG_NOTIFY_RP 0x8e #define BTP_GATTC_CFG_INDICATE_RP 0x8f struct btp_subscribe_rp { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t status; } __packed; #define BTP_GATTC_EV_NOTIFICATION_RXED 0x90 struct btp_gattc_notification_ev { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint8_t type; uint16_t handle; uint16_t data_length; diff --git a/apps/bttester/src/btp/btp_l2cap.h b/apps/bttester/src/btp/btp_l2cap.h index 5916345531..7d2adcaea6 100644 --- a/apps/bttester/src/btp/btp_l2cap.h +++ b/apps/bttester/src/btp/btp_l2cap.h @@ -38,8 +38,7 @@ struct btp_l2cap_read_supported_commands_rp { #define BTP_L2CAP_CONNECT 0x02 struct btp_l2cap_connect_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t psm; uint16_t mtu; uint8_t num; @@ -82,8 +81,7 @@ struct l2cap_accept_connection_cmd { #define BTP_L2CAP_RECONFIGURE 0x07 struct btp_l2cap_reconfigure_cmd { - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; uint16_t mtu; uint8_t num; uint8_t idxs[]; @@ -99,8 +97,7 @@ struct btp_l2cap_credits_cmd { struct btp_l2cap_connection_req_ev { uint8_t chan_id; uint16_t psm; - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_L2CAP_EV_CONNECTED 0x81 @@ -111,8 +108,7 @@ struct btp_l2cap_connected_ev { uint16_t peer_mps; uint16_t our_mtu; uint16_t our_mps; - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_L2CAP_EV_DISCONNECTED 0x82 @@ -120,8 +116,7 @@ struct btp_l2cap_disconnected_ev { uint16_t result; uint8_t chan_id; uint16_t psm; - uint8_t address_type; - uint8_t address[6]; + ble_addr_t address; } __packed; #define BTP_L2CAP_EV_DATA_RECEIVED 0x83 diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index 378e3ae5b2..946bda75e2 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -210,7 +210,7 @@ controller_info(const void *cmd, uint16_t cmd_len, } current_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); supported_settings |= BIT(BTP_GAP_SETTINGS_PRIVACY); - memcpy(rp->address, addr.val, sizeof(rp->address)); + memcpy(&rp->address, &addr, sizeof(rp->address)); } else { rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, rp->address, NULL); if (rc) { @@ -595,8 +595,7 @@ store_adv(const ble_addr_t *addr, int8_t rssi, return; } - memcpy(ev->address, addr->val, sizeof(ev->address)); - ev->address_type = addr->type; + memcpy(&ev->address, addr, sizeof(ev->address)); ev->rssi = rssi; ev->flags = BTP_GAP_DEVICE_FOUND_FLAG_AD | BTP_GAP_DEVICE_FOUND_FLAG_RSSI; ev->eir_data_len = len; @@ -645,8 +644,7 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, } ev = (void *) adv_buf->om_data; - a.type = ev->address_type; - memcpy(a.val, ev->address, sizeof(a.val)); + memcpy(&a, &ev->address, sizeof(a)); /* * in general, the Scan Response comes right after the @@ -818,8 +816,7 @@ le_connected(uint16_t conn_handle, int status) addr = &desc.peer_id_addr; - memcpy(connected_ev.address, addr->val, sizeof(connected_ev.address)); - connected_ev.address_type = addr->type; + memcpy(&connected_ev.address, addr, sizeof(connected_ev.address)); connected_ev.conn_itvl = desc.conn_itvl; connected_ev.conn_latency = desc.conn_latency; connected_ev.supervision_timeout = desc.supervision_timeout; @@ -870,8 +867,7 @@ le_disconnected(struct ble_gap_conn_desc *conn, int reason) connection_attempts = 0; memset(&connected_ev, 0, sizeof(connected_ev)); - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; + memcpy(&ev.address, addr, sizeof(ev.address)); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, (uint8_t *) &ev, sizeof(ev)); @@ -925,8 +921,7 @@ auth_passkey_display(uint16_t conn_handle, unsigned int passkey) addr = &desc.peer_ota_addr; - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; + memcpy(&ev.address, addr, sizeof(ev.address)); ev.passkey = htole32(pk.passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, @@ -950,8 +945,7 @@ auth_passkey_entry(uint16_t conn_handle) addr = &desc.peer_ota_addr; - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; + memcpy(&ev.address, addr, sizeof(ev.address)); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, (uint8_t *) &ev, sizeof(ev)); @@ -974,8 +968,7 @@ auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) addr = &desc.peer_ota_addr; - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; + memcpy(&ev.address, addr, sizeof(ev.address)); ev.passkey = htole32(passkey); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, @@ -1053,11 +1046,9 @@ le_identity_resolved(uint16_t conn_handle) peer_id_addr = desc.peer_id_addr; peer_ota_addr = desc.peer_ota_addr; - ev.address_type = desc.peer_ota_addr.type; - memcpy(ev.address, desc.peer_ota_addr.val, sizeof(ev.address)); + memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); - ev.identity_address_type = desc.peer_id_addr.type; - memcpy(ev.identity_address, desc.peer_id_addr.val, + memcpy(&ev.identity_address, &desc.peer_id_addr, sizeof(ev.identity_address)); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, @@ -1081,8 +1072,7 @@ le_pairing_failed(uint16_t conn_handle, int reason) peer_id_addr = desc.peer_id_addr; peer_ota_addr = desc.peer_ota_addr; - ev.address_type = desc.peer_ota_addr.type; - memcpy(ev.address, desc.peer_ota_addr.val, sizeof(ev.address)); + memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); ev.reason = reason; @@ -1097,8 +1087,7 @@ le_conn_param_update(struct ble_gap_conn_desc *desc) SYS_LOG_DBG(""); - ev.address_type = desc->peer_ota_addr.type; - memcpy(ev.address, desc->peer_ota_addr.val, sizeof(ev.address)); + memcpy(&ev.address, &desc->peer_ota_addr, sizeof(ev.address)); ev.conn_itvl = desc->conn_itvl; ev.conn_latency = desc->conn_latency; @@ -1117,8 +1106,7 @@ le_encryption_changed(struct ble_gap_conn_desc *desc) encrypted = (bool) desc->sec_state.encrypted; - ev.address_type = desc->peer_ota_addr.type; - memcpy(ev.address, desc->peer_ota_addr.val, sizeof(ev.address)); + memcpy(&ev.address, &desc->peer_ota_addr, sizeof(ev.address)); ev.level = 0; if (desc->sec_state.encrypted) { @@ -1147,8 +1135,7 @@ bond_lost(uint16_t conn_handle) rc = ble_gap_conn_find(conn_handle, &desc); assert(rc == 0); - memcpy(ev.address, &desc.peer_id_addr, sizeof(ev.address)); - ev.address_type = desc.peer_id_addr.type; + memcpy(&ev.address, &desc.peer_id_addr, sizeof(ev.address)); tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST, (uint8_t *) &ev, @@ -1415,7 +1402,7 @@ connect(const void *cmd, uint16_t cmd_len, { const struct btp_gap_connect_cmd *cp = cmd; - ble_addr_t *addr = (ble_addr_t *)&cp->address_type; + ble_addr_t *addr = (ble_addr_t *)&cp->address; SYS_LOG_DBG(""); @@ -1441,7 +1428,7 @@ disconnect(const void *cmd, uint16_t cmd_len, SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); + rc = gap_conn_find_by_addr(&cp->address, &desc); if (rc) { return BTP_STATUS_FAILED; } @@ -1499,7 +1486,7 @@ pair(const void *cmd, uint16_t cmd_len, SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); + rc = gap_conn_find_by_addr(&cp->address, &desc); if (rc) { return BTP_STATUS_FAILED; } @@ -1521,7 +1508,7 @@ unpair(const void *cmd, uint16_t cmd_len, SYS_LOG_DBG(""); - err = ble_gap_unpair((ble_addr_t *)&cp->address_type); + err = ble_gap_unpair(&cp->address); return err != 0 ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS; } @@ -1536,7 +1523,7 @@ passkey_entry(const void *cmd, uint16_t cmd_len, SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); + rc = gap_conn_find_by_addr(&cp->address, &desc); if (rc) { return BTP_STATUS_FAILED; } @@ -1563,7 +1550,7 @@ passkey_confirm(const void *cmd, uint16_t cmd_len, SYS_LOG_DBG(""); - rc = gap_conn_find_by_addr((ble_addr_t *)&cp->address_type, &desc); + rc = gap_conn_find_by_addr(&cp->address, &desc); if (rc) { return BTP_STATUS_FAILED; } @@ -1595,7 +1582,7 @@ start_direct_adv(const void *cmd, uint16_t cmd_len, adv_params.high_duty_cycle = cp->high_duty; - err = ble_gap_adv_start(own_addr_type, (ble_addr_t *)&cp->address_type, + err = ble_gap_adv_start(own_addr_type, &cp->address, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL); if (err) { diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index 3f7a09f2f8..0ba494a10c 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -126,7 +126,6 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, struct btp_gattc_exchange_mtu_ev *ev; struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; SYS_LOG_DBG(""); @@ -139,10 +138,7 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, return 0; } - addr = &conn.peer_ota_addr; - - ev->address_type = addr->type; - memcpy(ev->address, addr->val, sizeof(ev->address)); + memcpy(&ev->address, &conn.peer_ota_addr, sizeof(ev->address)); ev->mtu = mtu; @@ -189,7 +185,6 @@ disc_prim_svcs_cb(uint16_t conn_handle, struct ble_gap_conn_desc conn; struct btp_gatt_service *service; const ble_uuid_any_t *uuid; - const ble_addr_t *addr; uint8_t uuid_length; struct os_mbuf *buf = os_msys_get(0, 0); uint8_t opcode = (uint8_t) (int) arg; @@ -208,10 +203,7 @@ disc_prim_svcs_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; if (error->status != 0 && error->status != BLE_HS_EDONE) { @@ -334,7 +326,6 @@ find_included_cb(uint16_t conn_handle, uint8_t uuid_length; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -351,10 +342,7 @@ find_included_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; @@ -454,7 +442,6 @@ disc_chrc_cb(uint16_t conn_handle, uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -469,10 +456,7 @@ disc_chrc_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; @@ -616,7 +600,6 @@ disc_all_desc_cb(uint16_t conn_handle, uint8_t uuid_length; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -633,10 +616,7 @@ disc_all_desc_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; @@ -732,7 +712,6 @@ read_cb(uint16_t conn_handle, struct btp_gattc_read_rp *rp; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -747,10 +726,7 @@ read_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); SYS_LOG_DBG("status=%d", error->status); @@ -821,7 +797,6 @@ read_uuid_cb(uint16_t conn_handle, uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; static uint16_t attr_len; @@ -839,10 +814,7 @@ read_uuid_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; @@ -932,7 +904,6 @@ read_long_cb(uint16_t conn_handle, uint8_t opcode = (uint8_t) (int) arg; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -949,10 +920,7 @@ read_long_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; @@ -1090,7 +1058,6 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -1107,10 +1074,7 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, @@ -1197,7 +1161,6 @@ reliable_write_cb(uint16_t conn_handle, struct btp_gattc_write_rp *rp; uint8_t err = (uint8_t) error->status; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -1214,10 +1177,7 @@ reliable_write_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_RELIABLE_WRITE_RP, @@ -1282,7 +1242,6 @@ subscribe_cb(uint16_t conn_handle, uint8_t err = (uint8_t) error->status; uint8_t opcode = (uint8_t) (int) arg; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; struct ble_gap_conn_desc conn; int rc = 0; @@ -1299,10 +1258,7 @@ subscribe_cb(uint16_t conn_handle, goto free; } - addr = &conn.peer_ota_addr; - - rp->address_type = addr->type; - memcpy(rp->address, addr->val, sizeof(rp->address)); + memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, @@ -1419,7 +1375,6 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, struct btp_gattc_notification_ev *ev; struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); - const ble_addr_t *addr; SYS_LOG_DBG(""); @@ -1436,10 +1391,7 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, return 0; } - addr = &conn.peer_ota_addr; - - ev->address_type = addr->type; - memcpy(ev->address, addr->val, sizeof(ev->address)); + memcpy(&ev->address, &conn.peer_ota_addr, sizeof(ev->address)); ev->type = (uint8_t) (indication ? 0x02 : 0x01); ev->handle = htole16(attr_handle); ev->data_length = htole16(os_mbuf_len(om)); diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index 498d22604b..389bb8fd79 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -201,9 +201,7 @@ connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, channel->chan = chan; if (!ble_gap_conn_find(conn_handle, &desc)) { - ev.address_type = desc.peer_ota_addr.type; - memcpy(ev.address, desc.peer_ota_addr.val, - sizeof(ev.address)); + memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); } tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, @@ -229,9 +227,7 @@ disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, ev.psm = chan_info->psm; if (!ble_gap_conn_find(conn_handle, &desc)) { - ev.address_type = desc.peer_ota_addr.type; - memcpy(ev.address, desc.peer_ota_addr.val, - sizeof(ev.address)); + memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); } tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DISCONNECTED, From f0249715183ee50880176b42c4ab18cca914ba28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Tue, 11 Jul 2023 13:45:55 +0200 Subject: [PATCH 06/12] apps/bttester: refactor GATT service Accomodated to new API. --- apps/bttester/src/btp/btp_gatt.h | 5 +- apps/bttester/src/btp/bttester.h | 3 - apps/bttester/src/btp_gatt.c | 805 +++++++++++++++++-------------- 3 files changed, 443 insertions(+), 370 deletions(-) diff --git a/apps/bttester/src/btp/btp_gatt.h b/apps/bttester/src/btp/btp_gatt.h index 41f5ae7ec2..88f2e62a9a 100644 --- a/apps/bttester/src/btp/btp_gatt.h +++ b/apps/bttester/src/btp/btp_gatt.h @@ -128,6 +128,9 @@ struct btp_gatt_descriptor { } __packed; #define BTP_GATT_EXCHANGE_MTU 0x0a +struct btp_gatt_exchange_mtu_cmd { + ble_addr_t address; +} __packed; #define BTP_GATT_DISC_ALL_PRIM_SVCS 0x0b struct btp_gatt_disc_all_prim_svcs_cmd { @@ -305,7 +308,7 @@ struct btp_gatt_get_attribute_value_rp { } __packed; #define BTP_GATT_CHANGE_DATABASE 0x1e -struct btp_gatt_change_database { +struct btp_gatt_change_database_cmd { uint16_t start_handle; uint16_t end_handle; uint8_t visibility; diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index ea24600c19..9d66cd946b 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -101,9 +101,6 @@ tester_init_gatt(void); uint8_t tester_unregister_gatt(void); void -tester_handle_gatt(uint8_t opcode, uint8_t *data, - uint16_t len); -void tester_handle_gattc(uint8_t opcode, uint8_t *data, uint16_t len); int diff --git a/apps/bttester/src/btp_gatt.c b/apps/bttester/src/btp_gatt.c index ef6a805b75..09bd8d7236 100644 --- a/apps/bttester/src/btp_gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -571,10 +571,11 @@ gatt_svr_rel_write_test(uint16_t conn_handle, uint16_t attr_handle, } } -static void -start_server(uint8_t *data, uint16_t len) +static uint8_t +start_server(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_gatt_start_server_rp rp; + struct btp_gatt_start_server_rp *rp = rsp; SYS_LOG_DBG(""); @@ -582,11 +583,12 @@ start_server(uint8_t *data, uint16_t len) ble_svc_gatt_changed(0x0001, 0xffff); - rp.db_attr_off = 0; - rp.db_attr_cnt = 0; + rp->db_attr_off = 0; + rp->db_attr_cnt = 0; + + *rsp_len = sizeof(*rp); - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_START_SERVER, - (uint8_t *) &rp, sizeof(rp)); + return BTP_STATUS_SUCCESS; } /* Convert UUID from BTP command to bt_uuid */ @@ -702,37 +704,35 @@ read_cb(uint16_t conn_handle, return 0; } -static void -read(uint8_t *data, uint16_t len) +static uint8_t +read_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_read_cmd *cmd = (void *) data; + const struct btp_gatt_read_cmd *cp = cmd; struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { - goto fail; + return BTP_STATUS_FAILED; } - if (ble_gattc_read(conn.conn_handle, le16toh(cmd->handle), + if (ble_gattc_read(conn.conn_handle, le16toh(cp->handle), read_cb, (void *) BTP_GATT_READ)) { read_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } static int @@ -773,104 +773,103 @@ read_long_cb(uint16_t conn_handle, return 0; } -static void -read_long(uint8_t *data, uint16_t len) +static uint8_t +read_long(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_read_long_cmd *cmd = (void *) data; + const struct btp_gatt_read_long_cmd *cp = cmd; struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_read_long(conn.conn_handle, - le16toh(cmd->handle), - le16toh(cmd->offset), + le16toh(cp->handle), + le16toh(cp->offset), read_long_cb, (void *) BTP_GATT_READ_LONG)) { read_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_LONG, BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } -static void -read_multiple(uint8_t *data, uint16_t len) +static uint8_t +read_multiple(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + const struct btp_gatt_read_multiple_cmd *cp = cmd; + uint16_t handles[cp->handles_count]; struct ble_gap_conn_desc conn; int rc, i; SYS_LOG_DBG(""); for (i = 0; i < ARRAY_SIZE(handles); i++) { - handles[i] = le16toh(cmd->handles[i]); + handles[i] = le16toh(cp->handles[i]); } - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_read_mult(conn.conn_handle, handles, - cmd->handles_count, read_cb, + cp->handles_count, read_cb, (void *) BTP_GATT_READ_MULTIPLE)) { read_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ_MULTIPLE, BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } -static void -write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) +static uint8_t +write_without_rsp(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_write_without_rsp_cmd *cmd = (void *) data; + const struct btp_gatt_write_without_rsp_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + return BTP_STATUS_FAILED; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } if (ble_gattc_write_no_rsp_flat(conn.conn_handle, - le16toh(cmd->handle), cmd->data, - le16toh(cmd->data_length))) { - status = BTP_STATUS_FAILED; + le16toh(cp->handle), cp->data, + le16toh(cp->data_length))) { + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATT, op, status); + return BTP_STATUS_SUCCESS; } static int @@ -888,66 +887,75 @@ write_rsp(uint16_t conn_handle, const struct ble_gatt_error *error, return 0; } -static void -write(uint8_t *data, uint16_t len) +static uint8_t +write_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_write_cmd *cmd = (void *) data; + const struct btp_gatt_write_cmd *cp = cmd; struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + return BTP_STATUS_FAILED; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } - if (ble_gattc_write_flat(conn.conn_handle, le16toh(cmd->handle), - cmd->data, le16toh(cmd->data_length), + if (ble_gattc_write_flat(conn.conn_handle, le16toh(cp->handle), + cp->data, le16toh(cp->data_length), write_rsp, (void *) BTP_GATT_WRITE)) { - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE, BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } -static void -write_long(uint8_t *data, uint16_t len) +static uint8_t +write_long(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_write_long_cmd *cmd = (void *) data; + const struct btp_gatt_write_long_cmd *cp = cmd; struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; int rc = 0; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + goto fail; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cp->data, le16toh(cp->data_length)); if (!om) { SYS_LOG_ERR("Insufficient resources"); goto fail; } rc = ble_gattc_write_long(conn.conn_handle, - le16toh(cmd->handle), - le16toh(cmd->offset), + le16toh(cp->handle), + le16toh(cp->offset), om, write_rsp, (void *) BTP_GATT_WRITE_LONG); if (!rc) { - return; + return BTP_STATUS_DELAY_REPLY; } fail: SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, BTP_STATUS_FAILED); + return BTP_STATUS_FAILED; } static int @@ -966,10 +974,11 @@ reliable_write_rsp(uint16_t conn_handle, return 0; } -static void -reliable_write(uint8_t *data, uint16_t len) +static uint8_t +reliable_write(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_reliable_write_cmd *cmd = (void *) data; + const struct btp_gatt_reliable_write_cmd *cp = cmd; struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; @@ -977,21 +986,21 @@ reliable_write(uint8_t *data, uint16_t len) SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cp->data, le16toh(cp->data_length)); /* This is required, because Nimble checks if * the data is longer than offset */ - if (os_mbuf_extend(om, le16toh(cmd->offset) + 1) == NULL) { + if (os_mbuf_extend(om, le16toh(cp->offset) + 1) == NULL) { goto fail; } - attr.handle = le16toh(cmd->handle); - attr.offset = le16toh(cmd->offset); + attr.handle = le16toh(cp->handle); + attr.offset = le16toh(cp->offset); attr.om = om; if (ble_gattc_write_reliable(conn.conn_handle, &attr, 1, @@ -999,12 +1008,12 @@ reliable_write(uint8_t *data, uint16_t len) goto fail; } - return; + return BTP_STATUS_SUCCESS; fail: os_mbuf_free_chain(om); - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_WRITE_LONG, BTP_STATUS_FAILED); + return BTP_STATUS_FAILED; } static struct bt_gatt_subscribe_params { @@ -1013,44 +1022,42 @@ static struct bt_gatt_subscribe_params { uint16_t value_handle; } subscribe_params; -static void -read_uuid(uint8_t *data, uint16_t len) +static uint8_t +read_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_read_uuid_cmd *cmd = (void *) data; + const struct btp_gatt_read_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - goto fail; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (!gatt_buf_reserve(sizeof(struct btp_gatt_read_rp))) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_read_by_uuid(conn.conn_handle, - le16toh(cmd->start_handle), - le16toh(cmd->end_handle), &uuid.u, + le16toh(cp->start_handle), + le16toh(cp->end_handle), &uuid.u, read_long_cb, (void *) BTP_GATT_READ_UUID)) { read_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_READ, BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } static int @@ -1161,57 +1168,56 @@ disc_all_desc_cb(uint16_t conn_handle, return 0; } -static void -disc_all_prim_svcs(uint8_t *data, uint16_t len) +static uint8_t +disc_all_prim_svcs(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gatt_disc_all_prim_svcs_cmd *cp = cmd; struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_all_prim_svcs_rp))) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_disc_all_svcs(conn.conn_handle, disc_prim_uuid_cb, (void *) BTP_GATT_DISC_ALL_PRIM_SVCS)) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_PRIM_SVCS, - BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } -static void -disc_all_desc(uint8_t *data, uint16_t len) +static uint8_t +disc_all_desc(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_disc_all_desc_cmd *cmd = (void *) data; + const struct btp_gatt_disc_all_desc_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_all_desc_rp))) { - goto fail; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle) - 1; - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle) - 1; + end_handle = le16toh(cp->end_handle); rc = ble_gattc_disc_all_dscs(conn.conn_handle, start_handle, end_handle, disc_all_desc_cb, NULL); @@ -1220,13 +1226,10 @@ disc_all_desc(uint8_t *data, uint16_t len) if (rc) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } static int @@ -1251,7 +1254,7 @@ find_included_cb(uint16_t conn_handle, if (error->status == BLE_HS_EDONE) { tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, gatt_buf - .buf, gatt_buf.len); + .buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1267,9 +1270,8 @@ find_included_cb(uint16_t conn_handle, return BLE_HS_ENOMEM; } - /* FIXME */ included->included_handle = htole16(service_handle + 1 + - rp->services_count); + rp->services_count); included->service.start_handle = htole16(gatt_svc->start_handle); included->service.end_handle = htole16(gatt_svc->end_handle); included->service.uuid_length = uuid_length; @@ -1292,7 +1294,7 @@ disc_chrc_cb(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_chr *gatt_chr, void *arg) { - struct btp_gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; + struct btp_gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; struct btp_gatt_characteristic *chrc; const ble_uuid_any_t *uuid; uint8_t btp_opcode = (uint8_t) (int) arg; @@ -1309,7 +1311,7 @@ disc_chrc_cb(uint16_t conn_handle, if (error->status == BLE_HS_EDONE) { tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf - .len); + .len); discover_destroy(); return 0; } @@ -1343,10 +1345,11 @@ disc_chrc_cb(uint16_t conn_handle, return 0; } -static void -disc_chrc_uuid(uint8_t *data, uint16_t len) +static uint8_t +disc_chrc_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_disc_chrc_uuid_cmd *cmd = (void *) data; + const struct btp_gatt_disc_chrc_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; @@ -1354,114 +1357,111 @@ disc_chrc_uuid(uint8_t *data, uint16_t len) SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - goto fail; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_chrc_rp))) { - goto fail; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); if (ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, (void *) BTP_GATT_DISC_CHRC_UUID)) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_CHRC_UUID, - BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } -static void -disc_prim_uuid(uint8_t *data, uint16_t len) +static uint8_t +disc_prim_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_disc_prim_uuid_cmd *cmd = (void *) data; + const struct btp_gatt_disc_prim_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if ((cmd_len < sizeof(*cp)) || + (cmd_len != sizeof(*cp) + cp->uuid_length)) { + return BTP_STATUS_FAILED; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - goto fail; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_prim_uuid_rp))) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_disc_svc_by_uuid(conn.conn_handle, &uuid.u, disc_prim_uuid_cb, (void *) BTP_GATT_DISC_PRIM_UUID)) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_PRIM_UUID, - BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } -static void -disc_all_chrc(uint8_t *data, uint16_t len) +static uint8_t +disc_all_chrc(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_disc_all_chrc_cmd *cmd = (void *) data; - struct ble_gap_conn_desc conn; + const struct btp_gatt_disc_all_chrc_cmd *cp = cmd; + struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { SYS_LOG_DBG("Conn find failed"); - goto fail; + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_disc_chrc_rp))) { SYS_LOG_DBG("Buf reserve failed"); - goto fail; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); rc = ble_gattc_disc_all_chrs(conn.conn_handle, start_handle, end_handle, disc_chrc_cb, (void *) BTP_GATT_DISC_ALL_CHRC); if (rc) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_CHRC, BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } -static void -find_included(uint8_t *data, uint16_t len) +static uint8_t +find_included(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_find_included_cmd *cmd = (void *) data; + const struct btp_gatt_find_included_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; @@ -1469,30 +1469,27 @@ find_included(uint8_t *data, uint16_t len) SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } if (!gatt_buf_reserve(sizeof(struct btp_gatt_find_included_rp))) { - goto fail; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); service_handle_arg = start_handle; if (ble_gattc_find_inc_svcs(conn.conn_handle, start_handle, end_handle, find_included_cb, (void *) service_handle_arg)) { discover_destroy(); - goto fail; + return BTP_STATUS_FAILED; } - return; - -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, BTP_STATUS_FAILED); + return BTP_STATUS_DELAY_REPLY; } static int @@ -1503,38 +1500,39 @@ exchange_func(uint16_t conn_handle, SYS_LOG_DBG(""); if (error->status) { - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, - BTP_STATUS_FAILED); + SYS_LOG_DBG("MTU exchange failed"); return 0; } - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, BTP_STATUS_SUCCESS); + SYS_LOG_DBG("MTU exchange succeed"); return 0; } -static void -exchange_mtu(uint8_t *data, uint16_t len) +static uint8_t +exchange_mtu(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gatt_exchange_mtu_cmd *cp = cmd; struct ble_gap_conn_desc conn; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } if (ble_gattc_exchange_mtu(conn.conn_handle, exchange_func, NULL)) { - goto fail; + return BTP_STATUS_FAILED; } - return; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_EXCHANGE_MTU, - BTP_STATUS_FAILED); + /* this BTP command is about initiating MTU exchange, no need to wait + * for procedure to complete. + */ + return BTP_STATUS_SUCCESS; } static int @@ -1580,36 +1578,64 @@ disable_subscription(uint16_t conn_handle, uint16_t ccc_handle) return 0; } -static void -config_subscription(uint8_t *data, uint16_t len, uint8_t op) +static uint8_t +config_subscription_notif(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_cfg_notify_cmd *cmd = (void *) data; + const struct btp_gatt_cfg_notify_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint16_t ccc_handle = le16toh(cmd->ccc_handle); + uint16_t ccc_handle = le16toh(cp->ccc_handle); uint8_t status; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - tester_rsp(BTP_SERVICE_ID_GATT, op, BTP_STATUS_FAILED); - return; + return BTP_STATUS_FAILED; } - if (cmd->enable) { - uint16_t value; + if (cp->enable) { + /* on success response will be sent from callback */ + if (enable_subscription(conn.conn_handle, + ccc_handle, 0x0001) == 0) { + return BTP_STATUS_DELAY_REPLY; + } - if (op == BTP_GATT_CFG_NOTIFY) { - value = 0x0001; + status = BTP_STATUS_FAILED; + } else { + if (disable_subscription(conn.conn_handle, ccc_handle) < 0) { + status = BTP_STATUS_FAILED; } else { - value = 0x0002; + status = BTP_STATUS_SUCCESS; } + } + + return status; +} + +static uint8_t +config_subscription_ind(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) +{ + const struct btp_gatt_cfg_notify_cmd *cp = cmd; + struct ble_gap_conn_desc conn; + uint16_t ccc_handle = le16toh(cp->ccc_handle); + uint8_t status; + int rc; + + SYS_LOG_DBG(""); + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); + if (rc) { + return BTP_STATUS_FAILED; + } + if (cp->enable) { /* on success response will be sent from callback */ if (enable_subscription(conn.conn_handle, - ccc_handle, value) == 0) { - return; + ccc_handle, 0x0002) == 0) { + return BTP_STATUS_DELAY_REPLY; } status = BTP_STATUS_FAILED; @@ -1621,9 +1647,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) } } - SYS_LOG_DBG("Config subscription (op %u) status %u", op, status); - - tester_rsp(BTP_SERVICE_ID_GATT, op, status); + return status; } #define BTP_PERM_F_READ 0x01 @@ -1661,11 +1685,12 @@ flags_hs2btp(uint8_t flags) return ret; } -static void -get_attrs(uint8_t *data, uint16_t len) +static uint8_t +get_attrs(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_get_attributes_cmd *cmd = (void *) data; - struct btp_gatt_get_attributes_rp *rp; + const struct btp_gatt_get_attributes_cmd *cp = cmd; + struct btp_gatt_get_attributes_rp *rp = rsp; struct btp_gatt_attr *gatt_attr; struct os_mbuf *buf = os_msys_get(0, 0); uint16_t start_handle, end_handle; @@ -1674,17 +1699,19 @@ get_attrs(uint8_t *data, uint16_t len) ble_uuid_t *uuid_ptr = NULL; uint8_t count = 0; char str[BLE_UUID_STR_LEN]; + uint8_t status = BTP_STATUS_SUCCESS; SYS_LOG_DBG(""); memset(str, 0, sizeof(str)); memset(&uuid, 0, sizeof(uuid)); - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); - if (cmd->type_length) { - if (btp2bt_uuid(cmd->type, cmd->type_length, &uuid)) { - goto fail; + if (cp->type_length) { + if (btp2bt_uuid(cp->type, cp->type_length, &uuid)) { + status = BTP_STATUS_FAILED; + goto free; } ble_uuid_to_str(&uuid.u, str); @@ -1698,7 +1725,8 @@ get_attrs(uint8_t *data, uint16_t len) rp = os_mbuf_extend(buf, sizeof(*rp)); if (!rp) { - goto fail; + status = BTP_STATUS_FAILED; + goto free; } entry = ble_att_svr_find_by_uuid(entry, uuid_ptr, end_handle); @@ -1712,7 +1740,8 @@ get_attrs(uint8_t *data, uint16_t len) gatt_attr = os_mbuf_extend(buf, sizeof(*gatt_attr)); if (!gatt_attr) { - goto fail; + status = BTP_STATUS_FAILED; + goto free; } gatt_attr->handle = htole16(entry->ha_handle_id); gatt_attr->permission = flags_hs2btp(entry->ha_flags); @@ -1736,33 +1765,32 @@ get_attrs(uint8_t *data, uint16_t len) rp->attrs_count = count; - tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, - CONTROLLER_INDEX, buf); + *rsp_len = sizeof(*rp) + buf->om_len; - goto free; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTES, - BTP_STATUS_FAILED); free: os_mbuf_free_chain(buf); + return status; } -static void -get_attr_val(uint8_t *data, uint16_t len) +static uint8_t +get_attr_val(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_get_attribute_value_cmd *cmd = (void *) data; + const struct btp_gatt_get_attribute_value_cmd *cp = cmd; struct btp_gatt_get_attribute_value_rp *rp; struct ble_gap_conn_desc conn; struct os_mbuf *buf = os_msys_get(0, 0); - uint16_t handle = le16toh(cmd->handle); + uint16_t handle = le16toh(cp->handle); uint8_t out_att_err = 0; int conn_status; + uint8_t status = BTP_STATUS_SUCCESS; - conn_status = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + conn_status = ble_gap_conn_find_by_addr(&cp->address, &conn); if (conn_status) { rp = os_mbuf_extend(buf, sizeof(*rp)); if (!rp) { + status = BTP_STATUS_FAILED; goto free; } @@ -1773,13 +1801,14 @@ get_attr_val(uint8_t *data, uint16_t len) rp->att_response = out_att_err; rp->value_length = os_mbuf_len(buf) - sizeof(*rp); - tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTE_VALUE, - CONTROLLER_INDEX, buf); + (void)memcpy(rsp, buf->om_data, buf->om_len); + *rsp_len = buf->om_len; goto free; } else { rp = os_mbuf_extend(buf, sizeof(*rp)); if (!rp) { + status = BTP_STATUS_FAILED; goto free; } @@ -1790,69 +1819,72 @@ get_attr_val(uint8_t *data, uint16_t len) rp->att_response = out_att_err; rp->value_length = os_mbuf_len(buf) - sizeof(*rp); - tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_GET_ATTRIBUTE_VALUE, - CONTROLLER_INDEX, buf); + (void)memcpy(rsp, buf->om_data, buf->om_len); + *rsp_len = buf->om_len; goto free; } free: os_mbuf_free_chain(buf); + return status; } -static void -change_database(uint8_t *data, uint16_t len) +static uint8_t +change_database(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gatt_change_database *cmd = (void *) data; + const struct btp_gatt_change_database_cmd *cp = cmd; SYS_LOG_DBG("") ble_gatts_show_local(); - ble_svc_gatt_changed(cmd->start_handle, cmd->end_handle); - - tester_rsp(BTP_SERVICE_ID_GATT, BTP_GATT_CHANGE_DATABASE, - BTP_STATUS_SUCCESS); + ble_svc_gatt_changed(cp->start_handle, cp->end_handle); - return; + return BTP_STATUS_SUCCESS; } -static void -supported_commands(uint8_t *data, uint16_t len) +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t cmds[4]; - struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; - - SYS_LOG_DBG(""); - - memset(cmds, 0, sizeof(cmds)); - - tester_set_bit(cmds, BTP_GATT_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, BTP_GATT_START_SERVER); - tester_set_bit(cmds, BTP_GATT_EXCHANGE_MTU); - tester_set_bit(cmds, BTP_GATT_DISC_ALL_PRIM_SVCS); - tester_set_bit(cmds, BTP_GATT_DISC_PRIM_UUID); - tester_set_bit(cmds, BTP_GATT_FIND_INCLUDED); - tester_set_bit(cmds, BTP_GATT_DISC_ALL_CHRC); - tester_set_bit(cmds, BTP_GATT_DISC_CHRC_UUID); - tester_set_bit(cmds, BTP_GATT_DISC_ALL_DESC); - tester_set_bit(cmds, BTP_GATT_READ); - tester_set_bit(cmds, BTP_GATT_READ_LONG); - tester_set_bit(cmds, BTP_GATT_READ_MULTIPLE); - tester_set_bit(cmds, BTP_GATT_WRITE_WITHOUT_RSP); + struct btp_gatt_read_supported_commands_rp *rp = rsp; + + /* octet 0 */ + tester_set_bit(rp->data, BTP_GATT_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_GATT_START_SERVER); + + /* octet 1 */ + tester_set_bit(rp->data, BTP_GATT_EXCHANGE_MTU); + tester_set_bit(rp->data, BTP_GATT_DISC_ALL_PRIM_SVCS); + tester_set_bit(rp->data, BTP_GATT_DISC_PRIM_UUID); + tester_set_bit(rp->data, BTP_GATT_FIND_INCLUDED); + tester_set_bit(rp->data, BTP_GATT_DISC_ALL_CHRC); + tester_set_bit(rp->data, BTP_GATT_DISC_CHRC_UUID); + + /* octet 2 */ + tester_set_bit(rp->data, BTP_GATT_DISC_ALL_DESC); + tester_set_bit(rp->data, BTP_GATT_READ); + tester_set_bit(rp->data, BTP_GATT_READ_LONG); + tester_set_bit(rp->data, BTP_GATT_READ_MULTIPLE); + tester_set_bit(rp->data, BTP_GATT_WRITE_WITHOUT_RSP); #if 0 - tester_set_bit(cmds, BTP_GATT_SIGNED_WRITE_WITHOUT_RSP); + tester_set_bit(rp->data, BTP_GATT_SIGNED_WRITE_WITHOUT_RSP); #endif - tester_set_bit(cmds, BTP_GATT_WRITE); - tester_set_bit(cmds, BTP_GATT_WRITE_LONG); - tester_set_bit(cmds, BTP_GATT_CFG_NOTIFY); - tester_set_bit(cmds, BTP_GATT_CFG_INDICATE); - tester_set_bit(cmds, BTP_GATT_GET_ATTRIBUTES); - tester_set_bit(cmds, BTP_GATT_GET_ATTRIBUTE_VALUE); - tester_set_bit(cmds, BTP_GATT_CHANGE_DATABASE); - - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_READ_SUPPORTED_COMMANDS, - (uint8_t *) rp, sizeof(cmds)); + tester_set_bit(rp->data, BTP_GATT_WRITE); + + /* octet 3 */ + tester_set_bit(rp->data, BTP_GATT_WRITE_LONG); + tester_set_bit(rp->data, BTP_GATT_CFG_NOTIFY); + tester_set_bit(rp->data, BTP_GATT_CFG_INDICATE); + tester_set_bit(rp->data, BTP_GATT_GET_ATTRIBUTES); + tester_set_bit(rp->data, BTP_GATT_GET_ATTRIBUTE_VALUE); + tester_set_bit(rp->data, BTP_GATT_CHANGE_DATABASE); + + *rsp_len = sizeof(*rp) + 4; + + return BTP_STATUS_SUCCESS; } enum attr_type { @@ -1861,88 +1893,126 @@ enum attr_type { BLE_GATT_ATTR_DSC, }; -void -tester_handle_gatt(uint8_t opcode, uint8_t *data, - uint16_t len) -{ - switch (opcode) { - case BTP_GATT_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case BTP_GATT_START_SERVER: - start_server(data, len); - return; - case BTP_GATT_EXCHANGE_MTU: - exchange_mtu(data, len); - return; - case BTP_GATT_DISC_ALL_PRIM_SVCS: - disc_all_prim_svcs(data, len); - return; - case BTP_GATT_DISC_PRIM_UUID: - disc_prim_uuid(data, len); - return; - case BTP_GATT_FIND_INCLUDED: - find_included(data, len); - return; - case BTP_GATT_DISC_ALL_CHRC: - disc_all_chrc(data, len); - return; - case BTP_GATT_DISC_CHRC_UUID: - disc_chrc_uuid(data, len); - return; - case BTP_GATT_DISC_ALL_DESC: - disc_all_desc(data, len); - return; - case BTP_GATT_CHANGE_DATABASE: - change_database(data, len); - return; - case BTP_GATT_READ: - read(data, len); - return; - case BTP_GATT_READ_UUID: - read_uuid(data, len); - return; - case BTP_GATT_READ_LONG: - read_long(data, len); - return; - case BTP_GATT_READ_MULTIPLE: - read_multiple(data, len); - return; - case BTP_GATT_WRITE_WITHOUT_RSP: - write_without_rsp(data, - len, - opcode, - false); - return; +static const struct btp_handler handlers[] = { + { + .opcode = BTP_GATT_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_GATT_START_SERVER, + .expect_len = 0, + .func = start_server, + }, + { + .opcode = BTP_GATT_EXCHANGE_MTU, + .expect_len = sizeof(struct btp_gatt_exchange_mtu_cmd), + .func = exchange_mtu, + }, + { + .opcode = BTP_GATT_DISC_ALL_PRIM_SVCS, + .expect_len = sizeof(struct btp_gatt_disc_all_prim_svcs_cmd), + .func = disc_all_prim_svcs, + }, + { + .opcode = BTP_GATT_DISC_PRIM_UUID, + .expect_len = -1, + .func = disc_prim_uuid, + }, + { + .opcode = BTP_GATT_FIND_INCLUDED, + .expect_len = sizeof(struct btp_gatt_find_included_cmd), + .func = find_included, + }, + { + .opcode = BTP_GATT_DISC_ALL_CHRC, + .expect_len = sizeof(struct btp_gatt_disc_all_chrc_cmd), + .func = disc_all_chrc, + }, + { + .opcode = BTP_GATT_DISC_CHRC_UUID, + .expect_len = -1, + .func = disc_chrc_uuid, + }, + { + .opcode = BTP_GATT_DISC_ALL_DESC, + .expect_len = sizeof(struct btp_gatt_disc_all_desc_cmd), + .func = disc_all_desc, + }, + { + .opcode = BTP_GATT_CHANGE_DATABASE, + .expect_len = sizeof(struct btp_gatt_change_database_cmd), + .func = change_database, + }, + { + .opcode = BTP_GATT_READ, + .expect_len = sizeof(struct btp_gatt_read_cmd), + .func = read_data, + }, + { + .opcode = BTP_GATT_READ_UUID, + .expect_len = -1, + .func = read_uuid, + }, + { + .opcode = BTP_GATT_READ_LONG, + .expect_len = sizeof(struct btp_gatt_read_long_cmd), + .func = read_long, + }, + { + .opcode = BTP_GATT_READ_MULTIPLE, + .expect_len = -1, + .func = read_multiple, + }, + { + .opcode = BTP_GATT_WRITE_WITHOUT_RSP, + .expect_len = -1, + .func = write_without_rsp, + }, #if 0 - case BTP_GATT_SIGNED_WRITE_WITHOUT_RSP: - write_without_rsp(data, len, opcode, true); - return; + { + .opcode = BTP_GATT_SIGNED_WRITE_WITHOUT_RSP, + .expect_len = -1, + .func = write_signed_without_rsp, + }, #endif - case BTP_GATT_WRITE: - write(data, len); - return; - case BTP_GATT_WRITE_LONG: - write_long(data, len); - return; - case BTP_GATT_RELIABLE_WRITE: - reliable_write(data, len); - return; - case BTP_GATT_CFG_NOTIFY: - case BTP_GATT_CFG_INDICATE: - config_subscription(data, len, opcode); - return; - case BTP_GATT_GET_ATTRIBUTES: - get_attrs(data, len); - return; - case BTP_GATT_GET_ATTRIBUTE_VALUE: - get_attr_val(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_GATT, opcode, BTP_STATUS_UNKNOWN_CMD); - return; - } -} + { + .opcode = BTP_GATT_WRITE, + .expect_len = -1, + .func = write_data, + }, + { + .opcode = BTP_GATT_WRITE_LONG, + .expect_len = -1, + .func = write_long, + }, + { + .opcode = BTP_GATT_RELIABLE_WRITE, + .expect_len = -1, + .func = reliable_write, + }, + { + .opcode = BTP_GATT_CFG_NOTIFY, + .expect_len = sizeof(struct btp_gatt_cfg_notify_cmd), + .func = config_subscription_notif, + }, + { + .opcode = BTP_GATT_CFG_INDICATE, + .expect_len = sizeof(struct btp_gatt_cfg_notify_cmd), + .func = config_subscription_ind, + }, + { + .opcode = BTP_GATT_GET_ATTRIBUTES, + .expect_len = -1, + .func = get_attrs, + }, + { + .opcode = BTP_GATT_GET_ATTRIBUTE_VALUE, + .expect_len = sizeof(struct btp_gatt_get_attribute_value_cmd), + .func = get_attr_val, + }, +}; int tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, @@ -2148,6 +2218,9 @@ tester_init_gatt(void) os_callout_init(¬ify_tx_timer, os_eventq_dflt_get(), notify_test, NULL); + tester_register_command_handlers(BTP_SERVICE_ID_GATT, handlers, + ARRAY_SIZE(handlers)); + return BTP_STATUS_SUCCESS; } From 4dc2cb4ae64109e14f1d4bf83bc8b5e2dc900ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Wed, 12 Jul 2023 10:24:06 +0200 Subject: [PATCH 07/12] apps/bttester: refactor L2CAP service Accomodated to new API. --- apps/bttester/src/btp/bttester.h | 3 - apps/bttester/src/btp_l2cap.c | 361 +++++++++++++++++-------------- 2 files changed, 198 insertions(+), 166 deletions(-) diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 9d66cd946b..4112a51307 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -120,9 +120,6 @@ uint8_t tester_init_l2cap(void); uint8_t tester_unregister_l2cap(void); -void -tester_handle_l2cap(uint8_t opcode, uint8_t *data, - uint16_t len); #endif #if MYNEWT_VAL(BLE_MESH) diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index 389bb8fd79..aa6e158d32 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -72,7 +72,6 @@ get_free_channel(void) chan = &channels[i]; chan->chan_id = i; - return chan; } @@ -399,43 +398,45 @@ tester_l2cap_event(struct ble_l2cap_event *event, void *arg) } } -static void -connect(uint8_t *data, uint16_t len) +static uint8_t +connect(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_connect_cmd *cmd = (void *) data; - uint8_t rp_buf[sizeof(struct btp_l2cap_connect_rp) + cmd->num]; - struct btp_l2cap_connect_rp *rp = (void *) rp_buf; + const struct btp_l2cap_connect_cmd *cp = cmd; + struct btp_l2cap_connect_rp *rp = rsp; struct ble_gap_conn_desc desc; struct channel *chan; - struct os_mbuf *sdu_rx[cmd->num]; - ble_addr_t *addr = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + struct os_mbuf *sdu_rx[cp->num]; + ble_addr_t *addr = (void *)&cp->address; + uint16_t mtu = le16toh(cp->mtu); + uint16_t psm = le16toh(cp->psm); int rc; int i, j; - bool ecfc = cmd->options & BTP_L2CAP_CONNECT_OPT_ECFC; - hold_credit = cmd->options & BTP_L2CAP_CONNECT_OPT_HOLD_CREDIT; + uint8_t status = BTP_STATUS_SUCCESS; + bool ecfc = cp->options & BTP_L2CAP_CONNECT_OPT_ECFC; + hold_credit = cp->options & BTP_L2CAP_CONNECT_OPT_HOLD_CREDIT; SYS_LOG_DBG("connect: type: %d addr: %s", addr->type, string_from_bytes(addr->val, 6)); - if (mtu == 0 || mtu > TESTER_COC_MTU) { - mtu = TESTER_COC_MTU; + rc = ble_gap_conn_find_by_addr(addr, &desc); + if (cp->num == 0 || cp->num > CHANNELS || + mtu > TESTER_COC_MTU || mtu == 0) { + return BTP_STATUS_FAILED; } - rc = ble_gap_conn_find_by_addr(addr, &desc); if (rc) { SYS_LOG_ERR("GAP conn find failed"); - goto fail; + return BTP_STATUS_FAILED; } - rp->num = cmd->num; - - for (i = 0; i < cmd->num; i++) { + for (i = 0; i < cp->num; i++) { chan = get_free_channel(); if (!chan) { SYS_LOG_ERR("No free channels"); - goto fail; + status = BTP_STATUS_FAILED; + goto done; } /* temporarily mark channel as used to select next one */ chan->state = 1; @@ -445,186 +446,203 @@ connect(uint8_t *data, uint16_t len) sdu_rx[i] = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); if (sdu_rx[i] == NULL) { SYS_LOG_ERR("Failed to alloc buf"); - goto fail; - } - } - - /* mark selected channels as unused again */ - for (i = 0; i < cmd->num; i++) { - for (j = 0; j < CHANNELS; j++) { - if (rp->chan_ids[i] == channels[j].chan_id) { - channels[j].state = 0; - } + status = BTP_STATUS_FAILED; + goto done; } } - if (cmd->num == 1 && !ecfc) { - rc = ble_l2cap_connect(desc.conn_handle, htole16(cmd->psm), + if (cp->num == 1 && !ecfc) { + rc = ble_l2cap_connect(desc.conn_handle, psm, mtu, sdu_rx[0], tester_l2cap_event, NULL); } else if (ecfc) { rc = ble_l2cap_enhanced_connect(desc.conn_handle, - htole16(cmd->psm), mtu, - cmd->num, sdu_rx, + psm, mtu, + cp->num, sdu_rx, tester_l2cap_event, NULL); } else { SYS_LOG_ERR("Invalid 'num' parameter value"); - goto fail; + status = BTP_STATUS_FAILED; + goto done; } if (rc) { SYS_LOG_ERR("L2CAP connect failed\n"); - goto fail; + status = BTP_STATUS_FAILED; + goto done; } - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, - (uint8_t *) rp, sizeof(rp_buf)); + rp->num = cp->num; - return; + *rsp_len = sizeof(*rp) + (rp->num * sizeof(rp->chan_ids[0])); +done: + /* mark selected channels as unused again */ + for (i = 0; i < cp->num; i++) { + for (j = 0; j < CHANNELS; j++) { + if (rp->chan_ids[i] == channels[j].chan_id) { + channels[j].state = 0; + } + } + } -fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CONNECT, - BTP_STATUS_FAILED); + return status; } -static void -disconnect(const uint8_t *data, uint16_t len) +static uint8_t +disconnect(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_disconnect_cmd *cmd = (void *) data; + const struct btp_l2cap_disconnect_cmd *cp = cmd; struct channel *chan; - uint8_t status; int err; SYS_LOG_DBG(""); - chan = get_channel(cmd->chan_id); + if (cp->chan_id >= CHANNELS) { + return BTP_STATUS_FAILED; + } + + chan = get_channel(cp->chan_id); assert(chan != NULL); err = ble_l2cap_disconnect(chan->chan); if (err) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_DISCONNECT, - status); + return BTP_STATUS_SUCCESS; } -static void -send_data(const uint8_t *data, uint16_t len) +static uint8_t +send_data(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_send_data_cmd *cmd = (void *) data; + const struct btp_l2cap_send_data_cmd *cp = cmd; + struct channel *chan; struct os_mbuf *sdu_tx = NULL; int rc; - uint16_t data_len = le16toh(cmd->data_len); - struct channel *chan = get_channel(cmd->chan_id); + uint16_t data_len; - SYS_LOG_DBG("cmd->chan_id=%d", cmd->chan_id); + SYS_LOG_DBG("cmd->chan_id=%d", cp->chan_id); + + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_len)) { + return BTP_STATUS_FAILED; + } + + if (cp->chan_id >= CHANNELS) { + return BTP_STATUS_FAILED; + } + + chan = get_channel(cp->chan_id); + data_len = le16toh(cp->data_len); if (!chan) { SYS_LOG_ERR("Invalid channel\n"); - goto fail; + return BTP_STATUS_FAILED; } /* FIXME: For now, fail if data length exceeds buffer length */ if (data_len > TESTER_COC_MTU) { SYS_LOG_ERR("Data length exceeds buffer length"); - goto fail; + return BTP_STATUS_FAILED; } sdu_tx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); if (sdu_tx == NULL) { SYS_LOG_ERR("No memory in the test sdu pool\n"); + rc = BLE_HS_ENOMEM; goto fail; } - os_mbuf_append(sdu_tx, cmd->data, data_len); + os_mbuf_append(sdu_tx, cp->data, data_len); /* ble_l2cap_send takes ownership of the sdu */ rc = ble_l2cap_send(chan->chan, sdu_tx); if (rc == 0 || rc == BLE_HS_ESTALLED) { - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, - BTP_STATUS_SUCCESS); - return; + return BTP_STATUS_SUCCESS; } +fail: SYS_LOG_ERR("Unable to send data: %d", rc); os_mbuf_free_chain(sdu_tx); -fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_SEND_DATA, - BTP_STATUS_FAILED); + return BTP_STATUS_FAILED; } -static void -listen(const uint8_t *data, uint16_t len) +static uint8_t +listen(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_listen_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + const struct btp_l2cap_listen_cmd *cp = cmd; + uint16_t mtu = htole16(cp->mtu); + uint16_t psm = htole16(cp->psm); int rc; SYS_LOG_DBG(""); + if (psm == 0) { + return BTP_STATUS_FAILED; + } + if (mtu == 0 || mtu > TESTER_COC_MTU) { mtu = TESTER_COC_MTU; } - /* TODO: Handle cmd->transport flag */ - rc = ble_l2cap_create_server(cmd->psm, mtu, tester_l2cap_event, NULL); - if (rc) { - goto fail; + /* We do not support BR/EDR transport */ + if (cp->transport == 0) { + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, - BTP_STATUS_SUCCESS); - return; + rc = ble_l2cap_create_server(psm, mtu, tester_l2cap_event, NULL); + if (rc) { + return BTP_STATUS_FAILED; + } -fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_LISTEN, - BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } -static void -credits(uint8_t *data, uint16_t len) +static uint8_t +credits(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_credits_cmd *cmd = (void *) data; + const struct btp_l2cap_credits_cmd *cp = cmd; + struct channel *chan; struct os_mbuf *sdu; int rc; - struct channel *channel = get_channel(cmd->chan_id); - if (channel == NULL) { - goto fail; + if (cp->chan_id >= CHANNELS) { + return BTP_STATUS_FAILED; + } + + chan = get_channel(cp->chan_id); + if (chan == NULL) { + return BTP_STATUS_FAILED; } sdu = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); if (sdu == NULL) { os_mbuf_free_chain(sdu); - goto fail; + return BTP_STATUS_FAILED; } - rc = ble_l2cap_recv_ready(channel->chan, sdu); + rc = ble_l2cap_recv_ready(chan->chan, sdu); if (rc != 0) { - goto fail; + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, - BTP_STATUS_SUCCESS); - return; -fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_CREDITS, - BTP_STATUS_FAILED); + + return BTP_STATUS_SUCCESS; } -static void -reconfigure(const uint8_t *data, uint16_t len) +static uint8_t +reconfigure(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_l2cap_reconfigure_cmd *cmd = (void *) data; - uint16_t mtu = htole16(cmd->mtu); + const struct btp_l2cap_reconfigure_cmd *cp = cmd; + uint16_t mtu = le16toh(cp->mtu); struct ble_gap_conn_desc desc; - ble_addr_t *addr = (void *) data; - struct ble_l2cap_chan *chans[cmd->num]; + ble_addr_t *addr = (ble_addr_t *)&cp->address; + struct ble_l2cap_chan *chans[cp->num]; struct channel *channel; int rc; int i; @@ -638,82 +656,96 @@ reconfigure(const uint8_t *data, uint16_t len) rc = ble_gap_conn_find_by_addr(addr, &desc); if (rc) { SYS_LOG_ERR("GAP conn find failed"); - goto fail; + return BTP_STATUS_FAILED; + } + + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + cp->num) { + return BTP_STATUS_FAILED; } - for (i = 0; i < cmd->num; ++i) { - channel = get_channel(cmd->idxs[i]); + if (cp->num > CHANNELS) { + return BTP_STATUS_FAILED; + } + + mtu = le16toh(cp->mtu); + if (mtu > TESTER_COC_MTU) { + return BTP_STATUS_FAILED; + } + + for (i = 0; i < cp->num; ++i) { + channel = get_channel(cp->idxs[i]); if (channel == NULL) { - goto fail; + return BTP_STATUS_FAILED; } chans[i] = channel->chan; } - rc = ble_l2cap_reconfig(chans, cmd->num, mtu); + rc = ble_l2cap_reconfig(chans, cp->num, mtu); if (rc) { - goto fail; + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, - BTP_STATUS_SUCCESS); - return; - -fail: - tester_rsp(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_RECONFIGURE, - BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } -static void -supported_commands(uint8_t *data, uint16_t len) +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t cmds[1]; - struct btp_l2cap_read_supported_commands_rp *rp = (void *) cmds; + struct btp_l2cap_read_supported_commands_rp *rp = rsp; + + /* octet 0 */ + tester_set_bit(rp->data, BTP_L2CAP_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_L2CAP_CONNECT); + tester_set_bit(rp->data, BTP_L2CAP_DISCONNECT); + tester_set_bit(rp->data, BTP_L2CAP_SEND_DATA); + tester_set_bit(rp->data, BTP_L2CAP_LISTEN); + /* octet 1 */ + tester_set_bit(rp->data, BTP_L2CAP_CREDITS); + *rsp_len = sizeof(*rp) + 2; - memset(cmds, 0, sizeof(cmds)); - - tester_set_bit(cmds, BTP_L2CAP_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, BTP_L2CAP_CONNECT); - tester_set_bit(cmds, BTP_L2CAP_DISCONNECT); - tester_set_bit(cmds, BTP_L2CAP_LISTEN); - tester_set_bit(cmds, BTP_L2CAP_SEND_DATA); - tester_set_bit(cmds, BTP_L2CAP_RECONFIGURE); - - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_READ_SUPPORTED_COMMANDS, - (uint8_t *) rp, sizeof(cmds)); + return BTP_STATUS_SUCCESS; } -void -tester_handle_l2cap(uint8_t opcode, uint8_t *data, - uint16_t len) -{ - switch (opcode) { - case BTP_L2CAP_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case BTP_L2CAP_CONNECT: - connect(data, len); - return; - case BTP_L2CAP_DISCONNECT: - disconnect(data, len); - return; - case BTP_L2CAP_SEND_DATA: - send_data(data, len); - return; - case BTP_L2CAP_LISTEN: - listen(data, len); - return; - case BTP_L2CAP_RECONFIGURE: - reconfigure(data, len); - return; - case BTP_L2CAP_CREDITS: - credits(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_L2CAP, opcode, - BTP_STATUS_UNKNOWN_CMD); - return; - } -} +static const struct btp_handler handlers[] = { + { + .opcode = BTP_L2CAP_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_L2CAP_CONNECT, + .expect_len = sizeof(struct btp_l2cap_connect_cmd), + .func = connect, + }, + { + .opcode = BTP_L2CAP_DISCONNECT, + .expect_len = sizeof(struct btp_l2cap_disconnect_cmd), + .func = disconnect, + }, + { + .opcode = BTP_L2CAP_SEND_DATA, + .expect_len = -1, + .func = send_data, + }, + { + .opcode = BTP_L2CAP_LISTEN, + .expect_len = sizeof(struct btp_l2cap_listen_cmd), + .func = listen, + }, + { + .opcode = BTP_L2CAP_RECONFIGURE, + .expect_len = -1, + .func = reconfigure, + }, + { + .opcode = BTP_L2CAP_CREDITS, + .expect_len = sizeof(struct btp_l2cap_credits_cmd), + .func = credits, + }, +}; uint8_t tester_init_l2cap(void) @@ -730,6 +762,9 @@ tester_init_l2cap(void) TESTER_COC_MTU, TESTER_COC_BUF_COUNT); assert(rc == 0); + tester_register_command_handlers(BTP_SERVICE_ID_L2CAP, handlers, + ARRAY_SIZE(handlers)); + return BTP_STATUS_SUCCESS; } From e783ab029215c8fa70e71573a4a23131e31404a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Wed, 12 Jul 2023 12:21:16 +0200 Subject: [PATCH 08/12] apps/bttester: refactor Mesh service Accomodate to new API. --- apps/bttester/src/btp/bttester.h | 2 - apps/bttester/src/btp_mesh.c | 545 +++++++++++++++++-------------- 2 files changed, 301 insertions(+), 246 deletions(-) diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 4112a51307..0c8db52a46 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -127,8 +127,6 @@ uint8_t tester_init_mesh(void); uint8_t tester_unregister_mesh(void); -void -tester_handle_mesh(uint8_t opcode, uint8_t *data, uint16_t len); #endif /* MYNEWT_VAL(BLE_MESH) */ void diff --git a/apps/bttester/src/btp_mesh.c b/apps/bttester/src/btp_mesh.c index 7642f8ea7f..3b9054a813 100644 --- a/apps/bttester/src/btp_mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -84,42 +84,40 @@ static struct { .dst = BT_MESH_ADDR_UNASSIGNED, }; -static void -supported_commands(uint8_t *data, uint16_t len) +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct os_mbuf *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE); - - net_buf_simple_init(buf, 0); - - /* 1st octet */ - memset(net_buf_simple_add(buf, 1), 0, 1); - tester_set_bit(buf->om_data, BTP_MESH_READ_SUPPORTED_COMMANDS); - tester_set_bit(buf->om_data, BTP_MESH_CONFIG_PROVISIONING); - tester_set_bit(buf->om_data, BTP_MESH_PROVISION_NODE); - tester_set_bit(buf->om_data, BTP_MESH_INIT); - tester_set_bit(buf->om_data, BTP_MESH_RESET); - tester_set_bit(buf->om_data, BTP_MESH_INPUT_NUMBER); - tester_set_bit(buf->om_data, BTP_MESH_INPUT_STRING); - /* 2nd octet */ - tester_set_bit(buf->om_data, BTP_MESH_IVU_TEST_MODE); - tester_set_bit(buf->om_data, BTP_MESH_IVU_TOGGLE_STATE); - tester_set_bit(buf->om_data, BTP_MESH_NET_SEND); - tester_set_bit(buf->om_data, BTP_MESH_HEALTH_GENERATE_FAULTS); - tester_set_bit(buf->om_data, BTP_MESH_HEALTH_CLEAR_FAULTS); - tester_set_bit(buf->om_data, BTP_MESH_LPN); - tester_set_bit(buf->om_data, BTP_MESH_LPN_POLL); - tester_set_bit(buf->om_data, BTP_MESH_MODEL_SEND); - /* 3rd octet */ - memset(net_buf_simple_add(buf, 1), 0, 1); + struct btp_mesh_read_supported_commands_rp *rp = rsp; + + /* octet 0 */ + tester_set_bit(rp->data, BTP_MESH_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_MESH_CONFIG_PROVISIONING); + tester_set_bit(rp->data, BTP_MESH_PROVISION_NODE); + tester_set_bit(rp->data, BTP_MESH_INIT); + tester_set_bit(rp->data, BTP_MESH_RESET); + tester_set_bit(rp->data, BTP_MESH_INPUT_NUMBER); + tester_set_bit(rp->data, BTP_MESH_INPUT_STRING); + /* octet 1 */ + tester_set_bit(rp->data, BTP_MESH_IVU_TEST_MODE); + tester_set_bit(rp->data, BTP_MESH_IVU_TOGGLE_STATE); + tester_set_bit(rp->data, BTP_MESH_NET_SEND); + tester_set_bit(rp->data, BTP_MESH_HEALTH_GENERATE_FAULTS); + tester_set_bit(rp->data, BTP_MESH_HEALTH_CLEAR_FAULTS); + tester_set_bit(rp->data, BTP_MESH_LPN); + tester_set_bit(rp->data, BTP_MESH_LPN_POLL); + tester_set_bit(rp->data, BTP_MESH_MODEL_SEND); + /* octet 2 */ #if MYNEWT_VAL(BLE_MESH_TESTING) - tester_set_bit(buf->om_data, BTP_MESH_LPN_SUBSCRIBE); - tester_set_bit(buf->om_data, BTP_MESH_LPN_UNSUBSCRIBE); - tester_set_bit(buf->om_data, BTP_MESH_RPL_CLEAR); + tester_set_bit(rp->data, BTP_MESH_LPN_SUBSCRIBE); + tester_set_bit(rp->data, BTP_MESH_LPN_UNSUBSCRIBE); + tester_set_bit(rp->data, BTP_MESH_RPL_CLEAR); #endif /* CONFIG_BT_TESTING */ - tester_set_bit(buf->om_data, BTP_MESH_PROXY_IDENTITY); + tester_set_bit(rp->data, BTP_MESH_PROXY_IDENTITY); - tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, buf); + *rsp_len = sizeof(*rp) + 3; + + return BTP_STATUS_SUCCESS; } static void @@ -410,153 +408,161 @@ static struct bt_mesh_prov prov = { .reset = prov_reset, }; -static void -config_prov(uint8_t *data, uint16_t len) +static uint8_t +config_prov(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_mesh_config_provisioning_cmd *cmd = (void *) data; + const struct btp_mesh_config_provisioning_cmd *cp = cmd; SYS_LOG_DBG(""); - memcpy(dev_uuid, cmd->uuid, sizeof(dev_uuid)); - memcpy(static_auth, cmd->static_auth, sizeof(static_auth)); + if (cmd_len < sizeof(*cp)) { + return BTP_STATUS_FAILED; + } + + memcpy(dev_uuid, cp->uuid, sizeof(dev_uuid)); + memcpy(static_auth, cp->static_auth, sizeof(static_auth)); - prov.output_size = cmd->out_size; - prov.output_actions = sys_le16_to_cpu(cmd->out_actions); - prov.input_size = cmd->in_size; - prov.input_actions = sys_le16_to_cpu(cmd->in_actions); + prov.output_size = cp->out_size; + prov.output_actions = sys_le16_to_cpu(cp->out_actions); + prov.input_size = cp->in_size; + prov.input_actions = sys_le16_to_cpu(cp->in_actions); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_CONFIG_PROVISIONING, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -provision_node(uint8_t *data, uint16_t len) +static uint8_t +provision_node(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_mesh_provision_node_cmd *cmd = (void *) data; + const struct btp_mesh_provision_node_cmd *cp = cmd; SYS_LOG_DBG(""); - memcpy(dev_key, cmd->dev_key, sizeof(dev_key)); - memcpy(net_key, cmd->net_key, sizeof(net_key)); + if (cmd_len != sizeof(*cp)) { + return BTP_STATUS_FAILED; + } + + memcpy(dev_key, cp->dev_key, sizeof(dev_key)); + memcpy(net_key, cp->net_key, sizeof(net_key)); - addr = sys_le16_to_cpu(cmd->addr); - flags = cmd->flags; - iv_index = le32toh(cmd->iv_index); - net_key_idx = sys_le16_to_cpu(cmd->net_key_idx); + addr = sys_le16_to_cpu(cp->addr); + flags = cp->flags; + iv_index = le32toh(cp->iv_index); + net_key_idx = sys_le16_to_cpu(cp->net_key_idx); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROVISION_NODE, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -init(uint8_t *data, uint16_t len) +static uint8_t +init(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t status = BTP_STATUS_SUCCESS; int err; SYS_LOG_DBG(""); err = bt_mesh_init(own_addr_type, &prov, &comp); if (err) { - status = BTP_STATUS_FAILED; - - goto rsp; + return BTP_STATUS_FAILED; } if (addr) { err = bt_mesh_provision(net_key, net_key_idx, flags, iv_index, addr, dev_key); if (err) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } } else { err = bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT); if (err) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } } -rsp: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INIT, status); + return BTP_STATUS_SUCCESS; } -static void -reset(uint8_t *data, uint16_t len) +static uint8_t +reset(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { SYS_LOG_DBG(""); bt_mesh_reset(); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RESET, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -input_number(uint8_t *data, uint16_t len) +static uint8_t +input_number(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_mesh_input_number_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; + const struct btp_mesh_input_number_cmd *cp = cmd; uint32_t number; int err; - number = le32toh(cmd->number); + number = le32toh(cp->number); SYS_LOG_DBG("number 0x%04lx", number); err = bt_mesh_input_number(number); if (err) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_NUMBER, status); + return BTP_STATUS_SUCCESS; } -static void -input_string(uint8_t *data, uint16_t len) +static uint8_t +input_string(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_mesh_input_string_cmd *cmd = (void *) data; - uint8_t status = BTP_STATUS_SUCCESS; - uint8_t str_auth[16]; + const struct btp_mesh_input_string_cmd *cp = cmd; int err; SYS_LOG_DBG(""); - if (cmd->string_len > sizeof(str_auth)) { - SYS_LOG_ERR("Too long input (%u chars required)", input_size); - status = BTP_STATUS_FAILED; - goto rsp; - } else if (cmd->string_len < input_size) { - SYS_LOG_ERR("Too short input (%u chars required)", input_size); - status = BTP_STATUS_FAILED; - goto rsp; + if (cmd_len < sizeof(*cp) && + cmd_len != (sizeof(*cp) + cp->string_len)) { + return BTP_STATUS_FAILED; } - strncpy((char *) str_auth, (char *) cmd->string, cmd->string_len); + /* for historical reasons this commands must send NULL terminated + * string + */ + if (cp->string[cp->string_len] != '\0') { + return BTP_STATUS_FAILED; + } - err = bt_mesh_input_string((char *) str_auth); + if (strlen((char *)cp->string) < input_size) { + SYS_LOG_ERR("Too short input (%u chars required)", input_size); + return BTP_STATUS_FAILED; + } + err = bt_mesh_input_string((char *)cp->string); if (err) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_INPUT_STRING, status); + return BTP_STATUS_SUCCESS; } -static void -ivu_test_mode(uint8_t *data, uint16_t len) +static uint8_t +ivu_test_mode(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_mesh_ivu_test_mode_cmd *cmd = (void *) data; + const struct btp_mesh_ivu_test_mode_cmd *cp = cmd; - SYS_LOG_DBG("enable 0x%02x", cmd->enable); + SYS_LOG_DBG("enable 0x%02x", cp->enable); - bt_mesh_iv_update_test(cmd->enable ? true : false); + bt_mesh_iv_update_test(cp->enable ? true : false); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TEST_MODE, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -ivu_toggle_state(uint8_t *data, uint16_t len) +static uint8_t +ivu_toggle_state(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { bool result; @@ -565,33 +571,35 @@ ivu_toggle_state(uint8_t *data, uint16_t len) result = bt_mesh_iv_update(); if (!result) { SYS_LOG_ERR("Failed to toggle the IV Update state"); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_IVU_TOGGLE_STATE, - result ? BTP_STATUS_SUCCESS : BTP_STATUS_FAILED); + return BTP_STATUS_SUCCESS; } -static void -lpn(uint8_t *data, uint16_t len) +static uint8_t +lpn(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_lpn_set_cmd *cmd = (void *) data; + const struct btp_mesh_lpn_set_cmd *cp = cmd; bool enable; int err; - SYS_LOG_DBG("enable 0x%02x", cmd->enable); + SYS_LOG_DBG("enable 0x%02x", cp->enable); - enable = cmd->enable ? true : false; + enable = cp->enable ? true : false; err = bt_mesh_lpn_set(enable); if (err) { SYS_LOG_ERR("Failed to toggle LPN (err %d)", err); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -lpn_poll(uint8_t *data, uint16_t len) +static uint8_t +lpn_poll(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { int err; @@ -600,27 +608,34 @@ lpn_poll(uint8_t *data, uint16_t len) err = bt_mesh_lpn_poll(); if (err) { SYS_LOG_ERR("Failed to send poll msg (err %d)", err); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_POLL, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -net_send(uint8_t *data, uint16_t len) +static uint8_t +net_send(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_net_send_cmd *cmd = (void *) data; + const struct btp_mesh_net_send_cmd *cp = cmd; struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = vnd_app_key_idx, - .addr = sys_le16_to_cpu(cmd->dst), - .send_ttl = cmd->ttl, + .addr = sys_le16_to_cpu(cp->dst), + .send_ttl = cp->ttl, }; int err; + int status = BTP_STATUS_SUCCESS; + + if (cmd_len < sizeof(*cp) && + cmd_len != (sizeof(*cp) + cp->payload_len)) { + return BTP_STATUS_FAILED; + } SYS_LOG_DBG("ttl 0x%02x dst 0x%04x payload_len %d", ctx.send_ttl, - ctx.addr, cmd->payload_len); + ctx.addr, cp->payload_len); if (!bt_mesh_app_key_exists(vnd_app_key_idx)) { (void) bt_mesh_app_key_add(vnd_app_key_idx, net.net_idx, @@ -628,48 +643,47 @@ net_send(uint8_t *data, uint16_t len) vnd_models[0].keys[0] = vnd_app_key_idx; } - net_buf_simple_add_mem(msg, cmd->payload, cmd->payload_len); + net_buf_simple_add_mem(msg, cp->payload, cp->payload_len); err = bt_mesh_model_send(&vnd_models[0], &ctx, msg, NULL, NULL); if (err) { SYS_LOG_ERR("Failed to send (err %d)", err); + status = BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_NET_SEND, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); - os_mbuf_free_chain(msg); + + return status; } -static void -health_generate_faults(uint8_t *data, uint16_t len) +static uint8_t +health_generate_faults(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_health_generate_faults_rp *rp; - struct os_mbuf *buf = NET_BUF_SIMPLE(sizeof(*rp) + sizeof(cur_faults) + - sizeof(reg_faults)); + struct btp_mesh_health_generate_faults_rp *rp = rsp; uint8_t some_faults[] = {0x01, 0x02, 0x03, 0xff, 0x06}; uint8_t cur_faults_count, reg_faults_count; - rp = net_buf_simple_add(buf, sizeof(*rp)); - cur_faults_count = min(sizeof(cur_faults), sizeof(some_faults)); memcpy(cur_faults, some_faults, cur_faults_count); - net_buf_simple_add_mem(buf, cur_faults, cur_faults_count); + memcpy(rp->current_faults, cur_faults, cur_faults_count); rp->cur_faults_count = cur_faults_count; reg_faults_count = min(sizeof(reg_faults), sizeof(some_faults)); memcpy(reg_faults, some_faults, reg_faults_count); - net_buf_simple_add_mem(buf, reg_faults, reg_faults_count); + memcpy(rp->registered_faults + cur_faults_count, reg_faults, reg_faults_count); rp->reg_faults_count = reg_faults_count; bt_mesh_fault_update(&elements[0]); - tester_send_buf(BTP_SERVICE_ID_MESH, BTP_MESH_HEALTH_GENERATE_FAULTS, - CONTROLLER_INDEX, buf); + *rsp_len = sizeof(*rp) + cur_faults_count + reg_faults_count; + + return BTP_STATUS_SUCCESS; } -static void -health_clear_faults(uint8_t *data, uint16_t len) +static uint8_t +health_clear_faults(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { SYS_LOG_DBG(""); @@ -678,24 +692,33 @@ health_clear_faults(uint8_t *data, uint16_t len) bt_mesh_fault_update(&elements[0]); - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_HEALTH_CLEAR_FAULTS, - BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -model_send(uint8_t *data, uint16_t len) +static uint8_t +model_send(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_model_send_cmd *cmd = (void *) data; + const struct btp_mesh_model_send_cmd *cp = cmd; struct os_mbuf *msg = NET_BUF_SIMPLE(UINT8_MAX); + struct bt_mesh_model *model = NULL; + int err, i; + uint16_t src; + int status = BTP_STATUS_SUCCESS; + + if (cmd_len < sizeof(*cp) && + cmd_len != (sizeof(*cp) + cp->payload_len)) { + return BTP_STATUS_FAILED; + } + struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, .app_idx = BT_MESH_KEY_DEV, - .addr = sys_le16_to_cpu(cmd->dst), + .addr = sys_le16_to_cpu(cp->dst), .send_ttl = BT_MESH_TTL_DEFAULT, }; - struct bt_mesh_model *model = NULL; - int err, i; - uint16_t src = sys_le16_to_cpu(cmd->src); + + src = sys_le16_to_cpu(cp->src); /* Lookup source address */ for (i = 0; i < ARRAY_SIZE(model_bound); i++) { @@ -709,35 +732,35 @@ model_send(uint8_t *data, uint16_t len) if (!model) { SYS_LOG_ERR("Model not found"); - err = -EINVAL; + status = BTP_STATUS_FAILED; - goto fail; + goto rsp; } SYS_LOG_DBG("src 0x%04x dst 0x%04x model %p payload_len %d", src, - ctx.addr, model, cmd->payload_len); + ctx.addr, model, cp->payload_len); - net_buf_simple_add_mem(msg, cmd->payload, cmd->payload_len); + net_buf_simple_add_mem(msg, cp->payload, cp->payload_len); err = bt_mesh_model_send(model, &ctx, msg, NULL, NULL); if (err) { SYS_LOG_ERR("Failed to send (err %d)", err); + status = BTP_STATUS_FAILED; } -fail: - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_MODEL_SEND, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); - +rsp: os_mbuf_free_chain(msg); + return status; } #if MYNEWT_VAL(BLE_MESH_TESTING) -static void -lpn_subscribe(uint8_t *data, uint16_t len) +static uint8_t +lpn_subscribe(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_lpn_subscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + const struct btp_mesh_lpn_subscribe_cmd *cp = cmd; + uint16_t address = sys_le16_to_cpu(cp->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -747,15 +770,15 @@ lpn_subscribe(uint8_t *data, uint16_t len) SYS_LOG_ERR("Failed to subscribe (err %d)", err); } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_SUBSCRIBE, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -lpn_unsubscribe(uint8_t *data, uint16_t len) +static uint8_t +lpn_unsubscribe(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - struct btp_mesh_lpn_unsubscribe_cmd *cmd = (void *) data; - uint16_t address = sys_le16_to_cpu(cmd->address); + const struct btp_mesh_lpn_unsubscribe_cmd *cp = cmd; + uint16_t address = sys_le16_to_cpu(cp->address); int err; SYS_LOG_DBG("address 0x%04x", address); @@ -763,14 +786,15 @@ lpn_unsubscribe(uint8_t *data, uint16_t len) err = bt_test_mesh_lpn_group_remove(&address, 1); if (err) { SYS_LOG_ERR("Failed to unsubscribe (err %d)", err); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_LPN_UNSUBSCRIBE, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -static void -rpl_clear(uint8_t *data, uint16_t len) +static uint8_t +rpl_clear(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { int err; @@ -779,16 +803,17 @@ rpl_clear(uint8_t *data, uint16_t len) err = bt_test_mesh_rpl_clear(); if (err) { SYS_LOG_ERR("Failed to clear RPL (err %d)", err); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_RPL_CLEAR, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } #endif /* MYNEWT_VAL(BLE_MESH_TESTING) */ -static void -proxy_identity_enable(uint8_t *data, uint16_t len) +static uint8_t +proxy_identity_enable(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { int err; @@ -797,81 +822,110 @@ proxy_identity_enable(uint8_t *data, uint16_t len) err = bt_mesh_proxy_identity_enable(); if (err) { SYS_LOG_ERR("Failed to enable proxy identity (err %d)", err); + return BTP_STATUS_FAILED; } - tester_rsp(BTP_SERVICE_ID_MESH, BTP_MESH_PROXY_IDENTITY, - err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS); + return BTP_STATUS_SUCCESS; } -void -tester_handle_mesh(uint8_t opcode, uint8_t *data, uint16_t len) -{ - switch (opcode) { - case BTP_MESH_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - break; - case BTP_MESH_CONFIG_PROVISIONING: - config_prov(data, len); - break; - case BTP_MESH_PROVISION_NODE: - provision_node(data, len); - break; - case BTP_MESH_INIT: - init(data, len); - break; - case BTP_MESH_RESET: - reset(data, len); - break; - case BTP_MESH_INPUT_NUMBER: - input_number(data, len); - break; - case BTP_MESH_INPUT_STRING: - input_string(data, len); - break; - case BTP_MESH_IVU_TEST_MODE: - ivu_test_mode(data, len); - break; - case BTP_MESH_IVU_TOGGLE_STATE: - ivu_toggle_state(data, len); - break; - case BTP_MESH_LPN: - lpn(data, len); - break; - case BTP_MESH_LPN_POLL: - lpn_poll(data, len); - break; - case BTP_MESH_NET_SEND: - net_send(data, len); - break; - case BTP_MESH_HEALTH_GENERATE_FAULTS: - health_generate_faults(data, len); - break; - case BTP_MESH_HEALTH_CLEAR_FAULTS: - health_clear_faults(data, len); - break; - case BTP_MESH_MODEL_SEND: - model_send(data, len); - break; -#if MYNEWT_VAL(BLE_MESH_TESTING) - case BTP_MESH_LPN_SUBSCRIBE: - lpn_subscribe(data, len); - break; - case BTP_MESH_LPN_UNSUBSCRIBE: - lpn_unsubscribe(data, len); - break; - case BTP_MESH_RPL_CLEAR: - rpl_clear(data, len); - break; -#endif /* MYNEWT_VAL(BLE_MESH_TESTING) */ - case BTP_MESH_PROXY_IDENTITY: - proxy_identity_enable(data, len); - break; - default: - tester_rsp(BTP_SERVICE_ID_MESH, opcode, - BTP_STATUS_UNKNOWN_CMD); - break; - } -} +static const struct btp_handler handlers[] = { + { + .opcode = BTP_MESH_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_MESH_CONFIG_PROVISIONING, + .expect_len = -1, + .func = config_prov, + }, + { + .opcode = BTP_MESH_PROVISION_NODE, + .expect_len = -1, + .func = provision_node, + }, + { + .opcode = BTP_MESH_INIT, + .expect_len = 0, + .func = init, + }, + { + .opcode = BTP_MESH_RESET, + .expect_len = 0, + .func = reset, + }, + { + .opcode = BTP_MESH_INPUT_NUMBER, + .expect_len = sizeof(struct btp_mesh_input_number_cmd), + .func = input_number, + }, + { + .opcode = BTP_MESH_INPUT_STRING, + .expect_len = -1, + .func = input_string, + }, + { + .opcode = BTP_MESH_IVU_TEST_MODE, + .expect_len = sizeof(struct btp_mesh_ivu_test_mode_cmd), + .func = ivu_test_mode, + }, + { + .opcode = BTP_MESH_IVU_TOGGLE_STATE, + .expect_len = 0, + .func = ivu_toggle_state, + }, + { + .opcode = BTP_MESH_LPN, + .expect_len = sizeof(struct btp_mesh_lpn_set_cmd), + .func = lpn, + }, + { + .opcode = BTP_MESH_LPN_POLL, + .expect_len = 0, + .func = lpn_poll, + }, + { + .opcode = BTP_MESH_NET_SEND, + .expect_len = -1, + .func = net_send, + }, + { + .opcode = BTP_MESH_HEALTH_GENERATE_FAULTS, + .expect_len = 0, + .func = health_generate_faults, + }, + { + .opcode = BTP_MESH_HEALTH_CLEAR_FAULTS, + .expect_len = 0, + .func = health_clear_faults, + }, + { + .opcode = BTP_MESH_MODEL_SEND, + .expect_len = -1, + .func = model_send, + }, + { + .opcode = BTP_MESH_LPN_SUBSCRIBE, + .expect_len = sizeof(struct btp_mesh_lpn_subscribe_cmd), + .func = lpn_subscribe, + }, + { + .opcode = BTP_MESH_LPN_UNSUBSCRIBE, + .expect_len = sizeof(struct btp_mesh_lpn_unsubscribe_cmd), + .func = lpn_unsubscribe, + }, + { + .opcode = BTP_MESH_RPL_CLEAR, + .expect_len = 0, + .func = rpl_clear, + }, + { + .opcode = BTP_MESH_PROXY_IDENTITY, + .expect_len = 0, + .func = proxy_identity_enable, + }, +}; void net_recv_ev(uint8_t ttl, @@ -1029,6 +1083,9 @@ tester_init_mesh(void) bt_test_cb_register(&bt_test_cb); } + tester_register_command_handlers(BTP_SERVICE_ID_MESH, handlers, + ARRAY_SIZE(handlers)); + return BTP_STATUS_SUCCESS; } From d882f43e42b2ba404a2e18b396d5c97959789b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Thu, 13 Jul 2023 09:09:38 +0200 Subject: [PATCH 09/12] apps/bttester: refactor GATT Client service Accomodated to new API. --- apps/bttester/src/btp/btp_gattc.h | 3 + apps/bttester/src/btp/bttester.h | 8 +- apps/bttester/src/btp_core.c | 6 + apps/bttester/src/btp_gatt_cl.c | 676 ++++++++++++++++-------------- 4 files changed, 373 insertions(+), 320 deletions(-) diff --git a/apps/bttester/src/btp/btp_gattc.h b/apps/bttester/src/btp/btp_gattc.h index 7c642fdd67..76510b47af 100644 --- a/apps/bttester/src/btp/btp_gattc.h +++ b/apps/bttester/src/btp/btp_gattc.h @@ -34,6 +34,9 @@ struct btp_gattc_read_supported_commands_rp { } __packed; #define BTP_GATTC_EXCHANGE_MTU 0x02 +struct btp_gattc_exchange_mtu_cmd { + ble_addr_t address; +} __packed; #define BTP_GATTC_DISC_ALL_PRIM_SVCS 0x03 struct btp_gattc_disc_all_prim_svcs_cmd { diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 0c8db52a46..c986a7e97e 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -100,9 +100,6 @@ uint8_t tester_init_gatt(void); uint8_t tester_unregister_gatt(void); -void -tester_handle_gattc(uint8_t opcode, uint8_t *data, - uint16_t len); int tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, uint8_t indication, struct os_mbuf *om); @@ -128,7 +125,10 @@ tester_init_mesh(void); uint8_t tester_unregister_mesh(void); #endif /* MYNEWT_VAL(BLE_MESH) */ - +uint8_t +tester_init_gatt_cl(void); +uint8_t +tester_unregister_gatt_cl(void); void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); diff --git a/apps/bttester/src/btp_core.c b/apps/bttester/src/btp_core.c index fe1b8d4450..35dcd824b7 100644 --- a/apps/bttester/src/btp_core.c +++ b/apps/bttester/src/btp_core.c @@ -101,6 +101,9 @@ register_service(const void *cmd, uint16_t cmd_len, case BTP_SERVICE_ID_MESH: status = tester_init_mesh(); break; + case BTP_SERVICE_ID_GATTC: + status = tester_init_gatt_cl(); + break; #endif /* MYNEWT_VAL(BLE_MESH) */ default: status = BTP_STATUS_FAILED; @@ -148,6 +151,9 @@ unregister_service(const void *cmd, uint16_t cmd_len, status = tester_unregister_mesh(); break; #endif /* MYNEWT_VAL(BLE_MESH) */ + case BTP_SERVICE_ID_GATTC: + status = tester_unregister_gatt_cl(); + break; default: status = BTP_STATUS_FAILED; break; diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index 0ba494a10c..501c46030e 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -149,31 +149,28 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, return 0; } -static void -exchange_mtu(uint8_t *data, uint16_t len) +static uint8_t +exchange_mtu(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gattc_exchange_mtu_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } if (ble_gattc_exchange_mtu(conn.conn_handle, tester_mtu_exchanged_ev, NULL)) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_EXCHANGE_MTU, - status); + return BTP_STATUS_SUCCESS; } static int @@ -253,65 +250,58 @@ disc_prim_svcs_cb(uint16_t conn_handle, return rc; } -static void -disc_all_prim_svcs(uint8_t *data, uint16_t len) +static uint8_t +disc_all_prim_svcs(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { + const struct btp_gattc_disc_all_prim_svcs_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } if (ble_gattc_disc_all_svcs(conn.conn_handle, disc_prim_svcs_cb, (void *) BTP_GATTC_DISC_ALL_PRIM_RP)) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_PRIM_SVCS, - status); + return BTP_STATUS_SUCCESS; } -static void -disc_prim_uuid(uint8_t *data, uint16_t len) +static uint8_t +disc_prim_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_disc_prim_uuid_cmd *cmd = (void *) data; + const struct btp_gattc_disc_prim_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - status = BTP_STATUS_FAILED; - goto rsp; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } if (ble_gattc_disc_svc_by_uuid(conn.conn_handle, &uuid.u, disc_prim_svcs_cb, (void *) BTP_GATTC_DISC_PRIM_UUID_RP)) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_PRIM_UUID, status); + return BTP_STATUS_SUCCESS; } static int @@ -396,38 +386,35 @@ find_included_cb(uint16_t conn_handle, return rc; } -static void -find_included(uint8_t *data, uint16_t len) +static uint8_t +find_included(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_find_included_cmd *cmd = (void *) data; + const struct btp_gattc_find_included_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; int service_handle_arg; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); service_handle_arg = start_handle; if (ble_gattc_find_inc_svcs(conn.conn_handle, start_handle, end_handle, find_included_cb, (void *) service_handle_arg)) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED, status); + return BTP_STATUS_SUCCESS; } static int @@ -512,26 +499,25 @@ disc_chrc_cb(uint16_t conn_handle, return rc; } -static void -disc_all_chrc(uint8_t *data, uint16_t len) +static uint8_t +disc_all_chrc(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_disc_all_chrc_cmd *cmd = (void *) data; + const struct btp_gattc_disc_all_chrc_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { SYS_LOG_DBG("Conn find rsped"); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); rc = ble_gattc_disc_all_chrs(conn.conn_handle, start_handle, @@ -540,51 +526,45 @@ disc_all_chrc(uint8_t *data, uint16_t len) (void *) BTP_GATTC_DISC_ALL_CHRC_RP); if (rc) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_CHRC, status); + return BTP_STATUS_SUCCESS; } -static void -disc_chrc_uuid(uint8_t *data, uint16_t len) +static uint8_t +disc_chrc_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_disc_chrc_uuid_cmd *cmd = (void *) data; + const struct btp_gattc_disc_chrc_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; ble_uuid_any_t uuid; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - status = BTP_STATUS_FAILED; - goto rsp; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle); - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle); + end_handle = le16toh(cp->end_handle); rc = ble_gattc_disc_chrs_by_uuid(conn.conn_handle, start_handle, end_handle, &uuid.u, disc_chrc_cb, (void *) BTP_GATTC_DISC_CHRC_UUID_RP); if (rc) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_CHRC_UUID, status); + return BTP_STATUS_SUCCESS; } static int @@ -665,25 +645,24 @@ disc_all_desc_cb(uint16_t conn_handle, return rc; } -static void -disc_all_desc(uint8_t *data, uint16_t len) +static uint8_t +disc_all_desc(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_disc_all_desc_cmd *cmd = (void *) data; + const struct btp_gattc_disc_all_desc_cmd *cp = cmd; struct ble_gap_conn_desc conn; uint16_t start_handle, end_handle; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - start_handle = le16toh(cmd->start_handle) - 1; - end_handle = le16toh(cmd->end_handle); + start_handle = le16toh(cp->start_handle) - 1; + end_handle = le16toh(cp->end_handle); rc = ble_gattc_disc_all_dscs(conn.conn_handle, start_handle, @@ -695,12 +674,10 @@ disc_all_desc(uint8_t *data, uint16_t len) if (rc) { discover_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC, status); + return BTP_STATUS_SUCCESS; } static int @@ -756,34 +733,31 @@ read_cb(uint16_t conn_handle, return rc; } -static void -read(uint8_t *data, uint16_t len) +static uint8_t +read(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_read_cmd *cmd = (void *) data; + const struct btp_gattc_read_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); - if (ble_gattc_read(conn.conn_handle, le16toh(cmd->handle), + if (ble_gattc_read(conn.conn_handle, le16toh(cp->handle), read_cb, (void *) BTP_GATTC_READ_RP)) { read_destroy(); - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ, status); + return BTP_STATUS_SUCCESS; } static int @@ -857,41 +831,38 @@ read_uuid_cb(uint16_t conn_handle, return rc; } -static void -read_uuid(uint8_t *data, uint16_t len) +static uint8_t +read_uuid(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_read_uuid_cmd *cmd = (void *) data; + const struct btp_gattc_read_uuid_cmd *cp = cmd; struct ble_gap_conn_desc conn; ble_uuid_any_t uuid; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid)) { - status = BTP_STATUS_FAILED; - goto rsp; + if (btp2bt_uuid(cp->uuid, cp->uuid_length, &uuid)) { + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (ble_gattc_read_by_uuid(conn.conn_handle, - le16toh(cmd->start_handle), - le16toh(cmd->end_handle), &uuid.u, + le16toh(cp->start_handle), + le16toh(cp->end_handle), &uuid.u, read_uuid_cb, (void *) BTP_GATTC_READ_UUID_RP)) { read_destroy(); - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_UUID, status); + return BTP_STATUS_SUCCESS; } static int @@ -955,56 +926,53 @@ read_long_cb(uint16_t conn_handle, return rc; } -static void -read_long(uint8_t *data, uint16_t len) +static uint8_t +read_long(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_read_long_cmd *cmd = (void *) data; + const struct btp_gattc_read_long_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } /* Clear buffer */ read_destroy(); if (ble_gattc_read_long(conn.conn_handle, - le16toh(cmd->handle), - le16toh(cmd->offset), + le16toh(cp->handle), + le16toh(cp->offset), read_long_cb, (void *) BTP_GATTC_READ_LONG_RP)) { read_destroy(); - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_LONG, status); + return BTP_STATUS_SUCCESS; } -static void -read_multiple(uint8_t *data, uint16_t len) +static uint8_t +read_multiple(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_read_multiple_cmd *cmd = (void *) data; - uint16_t handles[cmd->handles_count]; + const struct btp_gattc_read_multiple_cmd *cp = cmd; + uint16_t handles[cp->handles_count]; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc, i; SYS_LOG_DBG(""); for (i = 0; i < ARRAY_SIZE(handles); i++) { - handles[i] = le16toh(cmd->handles[i]); + handles[i] = le16toh(cp->handles[i]); } - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } /* Clear buffer */ @@ -1012,41 +980,43 @@ read_multiple(uint8_t *data, uint16_t len) if (ble_gattc_read_mult(conn.conn_handle, handles, - cmd->handles_count, + cp->handles_count, read_cb, (void *) BTP_GATTC_READ_MULTIPLE_RP)) { read_destroy(); - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_MULTIPLE, status); + return BTP_STATUS_SUCCESS; } -static void -write_without_rsp(uint8_t *data, uint16_t len, uint8_t op, bool sign) +static uint8_t +write_without_rsp(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_write_without_rsp_cmd *cmd = (void *) data; + const struct btp_gattc_write_without_rsp_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + return BTP_STATUS_FAILED; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } if (ble_gattc_write_no_rsp_flat(conn.conn_handle, - le16toh(cmd->handle), cmd->data, - le16toh(cmd->data_length))) { - status = BTP_STATUS_FAILED; + le16toh(cp->handle), cp->data, + le16toh(cp->data_length))) { + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, op, status); + return BTP_STATUS_SUCCESS; } static int @@ -1084,71 +1054,75 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, return rc; } -static void -write(uint8_t *data, uint16_t len) +static uint8_t +write(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_write_cmd *cmd = (void *) data; + const struct btp_gattc_write_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + return BTP_STATUS_FAILED; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - if (ble_gattc_write_flat(conn.conn_handle, le16toh(cmd->handle), - cmd->data, le16toh(cmd->data_length), + if (ble_gattc_write_flat(conn.conn_handle, le16toh(cp->handle), + cp->data, le16toh(cp->data_length), write_cb, (void *) BTP_GATTC_WRITE_RP)) { - status = BTP_STATUS_FAILED; + return BTP_STATUS_FAILED; } -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE, status); + return BTP_STATUS_SUCCESS; } -static void -write_long(uint8_t *data, uint16_t len) +static uint8_t +write_long(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_write_long_cmd *cmd = (void *) data; + const struct btp_gattc_write_long_cmd *cp = cmd; struct ble_gap_conn_desc conn; struct os_mbuf *om = NULL; - uint8_t status = BTP_STATUS_SUCCESS; int rc = 0; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + if (cmd_len < sizeof(*cp) || + cmd_len != sizeof(*cp) + le16toh(cp->data_length)) { + goto fail; + } + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { goto fail; } - om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cp->data, le16toh(cp->data_length)); if (!om) { SYS_LOG_ERR("Insufficient resources"); - status = BTP_STATUS_FAILED; goto fail; } rc = ble_gattc_write_long(conn.conn_handle, - le16toh(cmd->handle), - le16toh(cmd->offset), + le16toh(cp->handle), + le16toh(cp->offset), om, write_cb, (void *) BTP_GATTC_WRITE_LONG_RP); - if (rc) { - status = BTP_STATUS_FAILED; - } else { - goto rsp; + if (!rc) { + return BTP_STATUS_DELAY_REPLY; } fail: SYS_LOG_ERR("Failed to send Write Long request, rc=%d", rc); os_mbuf_free_chain(om); -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, status); + return BTP_STATUS_FAILED; } static int @@ -1187,49 +1161,46 @@ reliable_write_cb(uint16_t conn_handle, return rc; } -static void -reliable_write(uint8_t *data, uint16_t len) +static uint8_t +reliable_write(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_reliable_write_cmd *cmd = (void *) data; + const struct btp_gattc_reliable_write_cmd *cp = cmd; struct ble_gap_conn_desc conn; struct ble_gatt_attr attr; struct os_mbuf *om = NULL; - uint8_t status = BTP_STATUS_SUCCESS; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto fail; + return BTP_STATUS_SUCCESS; } - om = ble_hs_mbuf_from_flat(cmd->data, le16toh(cmd->data_length)); + om = ble_hs_mbuf_from_flat(cp->data, le16toh(cp->data_length)); /* This is required, because Nimble checks if * the data is longer than offset */ - if (os_mbuf_extend(om, le16toh(cmd->offset) + 1) == NULL) { - status = BTP_STATUS_FAILED; - goto fail; + if (os_mbuf_extend(om, le16toh(cp->offset) + 1) == NULL) { + return BTP_STATUS_SUCCESS; } - attr.handle = le16toh(cmd->handle); - attr.offset = le16toh(cmd->offset); + attr.handle = le16toh(cp->handle); + attr.offset = le16toh(cp->offset); attr.om = om; if (ble_gattc_write_reliable(conn.conn_handle, &attr, 1, reliable_write_cb, NULL)) { - status = BTP_STATUS_FAILED; goto fail; - } else { - goto rsp; } + return BTP_STATUS_SUCCESS; + fail: os_mbuf_free_chain(om); -rsp: - tester_rsp(BTP_SERVICE_ID_GATTC, BTP_GATTC_WRITE_LONG, status); + + return BTP_STATUS_FAILED; } static int @@ -1323,37 +1294,65 @@ disable_subscription(uint16_t conn_handle, uint16_t ccc_handle) return 0; } -static void -config_subscription(uint8_t *data, uint16_t len, uint8_t op) +static uint8_t +config_subscription_notif(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - const struct btp_gattc_cfg_notify_cmd *cmd = (void *) data; + const struct btp_gattc_cfg_notify_cmd *cp = cmd; struct ble_gap_conn_desc conn; - uint16_t ccc_handle = le16toh(cmd->ccc_handle); - uint8_t status = BTP_STATUS_SUCCESS; + uint16_t ccc_handle = le16toh(cp->ccc_handle); + uint8_t status; int rc; SYS_LOG_DBG(""); - rc = ble_gap_conn_find_by_addr((ble_addr_t *) data, &conn); + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); if (rc) { - status = BTP_STATUS_FAILED; - goto rsp; + return BTP_STATUS_FAILED; } - if (cmd->enable) { - uint16_t value; + if (cp->enable) { + if (enable_subscription(conn.conn_handle, + ccc_handle, 0x0001) == 0) { + return BTP_STATUS_SUCCESS; + } - if (op == BTP_GATTC_CFG_NOTIFY) { - value = 0x0001; + status = BTP_STATUS_FAILED; + } else { + if (disable_subscription(conn.conn_handle, ccc_handle) < 0) { + status = BTP_STATUS_FAILED; } else { - value = 0x0002; + status = BTP_STATUS_SUCCESS; } + } + + return status; +} + +static uint8_t +config_subscription_ind(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) +{ + const struct btp_gattc_cfg_notify_cmd *cp = cmd; + struct ble_gap_conn_desc conn; + uint16_t ccc_handle = le16toh(cp->ccc_handle); + uint8_t status; + int rc; + + SYS_LOG_DBG(""); + + rc = ble_gap_conn_find_by_addr(&cp->address, &conn); + if (rc) { + return BTP_STATUS_FAILED; + } + if (cp->enable) { if (enable_subscription(conn.conn_handle, - ccc_handle, value) != 0) { - status = BTP_STATUS_FAILED; - goto rsp; + ccc_handle, 0x0002) == 0) { + return BTP_STATUS_SUCCESS; } + + status = BTP_STATUS_FAILED; } else { if (disable_subscription(conn.conn_handle, ccc_handle) < 0) { status = BTP_STATUS_FAILED; @@ -1362,10 +1361,7 @@ config_subscription(uint8_t *data, uint16_t len, uint8_t op) } } -rsp: - SYS_LOG_DBG("Config subscription (op %u) status %u", op, status); - - tester_rsp(BTP_SERVICE_ID_GATTC, op, status); + return status; } int @@ -1405,109 +1401,157 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, return 0; } -static void -supported_commands(uint8_t *data, uint16_t len) +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) { - uint8_t cmds[3]; - struct btp_gatt_read_supported_commands_rp *rp = (void *) cmds; + struct btp_gattc_read_supported_commands_rp *rp = rsp; SYS_LOG_DBG(""); - memset(cmds, 0, sizeof(cmds)); - - tester_set_bit(cmds, BTP_GATTC_READ_SUPPORTED_COMMANDS); - tester_set_bit(cmds, BTP_GATTC_EXCHANGE_MTU); - tester_set_bit(cmds, BTP_GATTC_DISC_ALL_PRIM_SVCS); - tester_set_bit(cmds, BTP_GATTC_DISC_PRIM_UUID); - tester_set_bit(cmds, BTP_GATTC_FIND_INCLUDED); - tester_set_bit(cmds, BTP_GATTC_DISC_ALL_CHRC); - tester_set_bit(cmds, BTP_GATTC_DISC_CHRC_UUID); - tester_set_bit(cmds, BTP_GATTC_DISC_ALL_DESC); - tester_set_bit(cmds, BTP_GATTC_READ); - tester_set_bit(cmds, BTP_GATTC_READ_UUID); - tester_set_bit(cmds, BTP_GATTC_READ_LONG); - tester_set_bit(cmds, BTP_GATTC_READ_MULTIPLE); - tester_set_bit(cmds, BTP_GATTC_WRITE_WITHOUT_RSP); + /* octet 0 */ + tester_set_bit(rp->data, BTP_GATTC_READ_SUPPORTED_COMMANDS); + tester_set_bit(rp->data, BTP_GATTC_EXCHANGE_MTU); + tester_set_bit(rp->data, BTP_GATTC_DISC_ALL_PRIM_SVCS); + tester_set_bit(rp->data, BTP_GATTC_DISC_PRIM_UUID); + tester_set_bit(rp->data, BTP_GATTC_FIND_INCLUDED); + tester_set_bit(rp->data, BTP_GATTC_DISC_ALL_CHRC); + tester_set_bit(rp->data, BTP_GATTC_DISC_CHRC_UUID); + /* octet 1 */ + tester_set_bit(rp->data, BTP_GATTC_DISC_ALL_DESC); + tester_set_bit(rp->data, BTP_GATTC_READ); + tester_set_bit(rp->data, BTP_GATTC_READ_UUID); + tester_set_bit(rp->data, BTP_GATTC_READ_LONG); + tester_set_bit(rp->data, BTP_GATTC_READ_MULTIPLE); + tester_set_bit(rp->data, BTP_GATTC_WRITE_WITHOUT_RSP); #if 0 - tester_set_bit(cmds, BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP); + tester_set_bit(rp->data, BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP); #endif - tester_set_bit(cmds, BTP_GATTC_WRITE); - tester_set_bit(cmds, BTP_GATTC_WRITE_LONG); - tester_set_bit(cmds, BTP_GATTC_RELIABLE_WRITE); - tester_set_bit(cmds, BTP_GATTC_CFG_NOTIFY); - tester_set_bit(cmds, BTP_GATTC_CFG_INDICATE); - - tester_send(BTP_SERVICE_ID_GATTC, BTP_GATTC_READ_SUPPORTED_COMMANDS, - (uint8_t *) rp, sizeof(cmds)); + tester_set_bit(rp->data, BTP_GATTC_WRITE); + /* octet 2 */ + tester_set_bit(rp->data, BTP_GATTC_WRITE_LONG); + tester_set_bit(rp->data, BTP_GATTC_RELIABLE_WRITE); + tester_set_bit(rp->data, BTP_GATTC_CFG_NOTIFY); + tester_set_bit(rp->data, BTP_GATTC_CFG_INDICATE); + + *rsp_len = sizeof(*rp) + 3; + + return BTP_STATUS_SUCCESS; } -void -tester_handle_gattc(uint8_t opcode, uint8_t *data, - uint16_t len) -{ - switch (opcode) { - case BTP_GATTC_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case BTP_GATTC_EXCHANGE_MTU: - exchange_mtu(data, len); - return; - case BTP_GATTC_DISC_ALL_PRIM_SVCS: - disc_all_prim_svcs(data, len); - return; - case BTP_GATTC_DISC_PRIM_UUID: - disc_prim_uuid(data, len); - return; - case BTP_GATTC_FIND_INCLUDED: - find_included(data, len); - return; - case BTP_GATTC_DISC_ALL_CHRC: - disc_all_chrc(data, len); - return; - case BTP_GATTC_DISC_CHRC_UUID: - disc_chrc_uuid(data, len); - return; - case BTP_GATTC_DISC_ALL_DESC: - disc_all_desc(data, len); - return; - case BTP_GATTC_READ: - read(data, len); - return; - case BTP_GATTC_READ_UUID: - read_uuid(data, len); - return; - case BTP_GATTC_READ_LONG: - read_long(data, len); - return; - case BTP_GATTC_READ_MULTIPLE: - read_multiple(data, len); - return; - case BTP_GATTC_WRITE_WITHOUT_RSP: - write_without_rsp(data, - len, - opcode, - false); - return; + +static const struct btp_handler handlers[] = { + { + .opcode = BTP_GATTC_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, + { + .opcode = BTP_GATTC_EXCHANGE_MTU, + .expect_len = sizeof(struct btp_gattc_exchange_mtu_cmd), + .func = exchange_mtu, + }, + { + .opcode = BTP_GATTC_DISC_ALL_PRIM_SVCS, + .expect_len = sizeof(struct btp_gattc_disc_all_prim_svcs_cmd), + .func = disc_all_prim_svcs, + }, + { + .opcode = BTP_GATTC_DISC_PRIM_UUID, + .expect_len = -1, + .func = disc_prim_uuid, + }, + { + .opcode = BTP_GATTC_FIND_INCLUDED, + .expect_len = sizeof(struct btp_gattc_find_included_cmd), + .func = find_included, + }, + { + .opcode = BTP_GATTC_DISC_ALL_CHRC, + .expect_len = sizeof(struct btp_gattc_disc_all_chrc_cmd), + .func = disc_all_chrc, + }, + { + .opcode = BTP_GATTC_DISC_CHRC_UUID, + .expect_len = -1, + .func = disc_chrc_uuid, + }, + { + .opcode = BTP_GATTC_DISC_ALL_DESC, + .expect_len = sizeof(struct btp_gattc_disc_all_desc_cmd), + .func = disc_all_desc, + }, + { + .opcode = BTP_GATTC_READ, + .expect_len = sizeof(struct btp_gattc_read_cmd), + .func = read, + }, + { + .opcode = BTP_GATTC_READ_UUID, + .expect_len = -1, + .func = read_uuid, + }, + { + .opcode = BTP_GATTC_READ_LONG, + .expect_len = sizeof(struct btp_gattc_read_long_cmd), + .func = read_long, + }, + { + .opcode = BTP_GATTC_READ_MULTIPLE, + .expect_len = -1, + .func = read_multiple, + }, + { + .opcode = BTP_GATTC_WRITE_WITHOUT_RSP, + .expect_len = -1, + .func = write_without_rsp, + }, #if 0 - case BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP: - write_without_rsp(data, len, opcode, true); - return; + { + .opcode = BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP, + .expect_len = -1, + .func = write_signed_without_rsp, + }, #endif - case BTP_GATTC_WRITE: - write(data, len); - return; - case BTP_GATTC_WRITE_LONG: - write_long(data, len); - return; - case BTP_GATTC_RELIABLE_WRITE: - reliable_write(data, len); - return; - case BTP_GATTC_CFG_NOTIFY: - case BTP_GATTC_CFG_INDICATE: - config_subscription(data, len, opcode); - return; - default: - tester_rsp(BTP_SERVICE_ID_GATTC, opcode, BTP_STATUS_UNKNOWN_CMD); - return; - } -} \ No newline at end of file + { + .opcode = BTP_GATTC_WRITE, + .expect_len = -1, + .func = write, + }, + { + .opcode = BTP_GATTC_WRITE_LONG, + .expect_len = -1, + .func = write_long, + }, + { + .opcode = BTP_GATTC_RELIABLE_WRITE, + .expect_len = -1, + .func = reliable_write, + }, + { + .opcode = BTP_GATTC_CFG_NOTIFY, + .expect_len = sizeof(struct btp_gattc_cfg_notify_cmd), + .func = config_subscription_notif, + }, + { + .opcode = BTP_GATTC_CFG_INDICATE, + .expect_len = sizeof(struct btp_gattc_cfg_notify_cmd), + .func = config_subscription_ind, + }, +}; + +uint8_t +tester_init_gatt_cl(void) +{ + tester_register_command_handlers(BTP_SERVICE_ID_GATTC, handlers, + ARRAY_SIZE(handlers)); + + return BTP_STATUS_SUCCESS; +} + +uint8_t +tester_unregister_gatt_cl(void) +{ + return BTP_STATUS_SUCCESS; +} From e8222722b6977b66d1bdf06c6e6cbc41a32d8c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Wed, 12 Jul 2023 13:49:28 +0200 Subject: [PATCH 10/12] apps/bttester: Keep BTP command in buffer for delayed response This allows to avoid copying data for async callbacks. --- apps/bttester/src/bttester.c | 50 ++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/apps/bttester/src/bttester.c b/apps/bttester/src/bttester.c index 7245bcb461..b07661d781 100644 --- a/apps/bttester/src/bttester.c +++ b/apps/bttester/src/bttester.c @@ -40,6 +40,7 @@ static struct os_eventq avail_queue; static struct os_eventq *cmds_queue; static struct os_event bttester_ev[CMD_QUEUED]; +static struct btp_buf *delayed_cmd; struct btp_buf { struct os_event *ev; @@ -165,14 +166,22 @@ cmd_handler(struct os_event *ev) status = BTP_STATUS_UNKNOWN_CMD; } - if (status != BTP_STATUS_DELAY_REPLY) { - if ((status == BTP_STATUS_SUCCESS) && rsp_len > 0) { - tester_send_with_index(cmd->hdr.service, cmd->hdr.opcode, - cmd->hdr.index, cmd->rsp, rsp_len); - } else { - tester_rsp_with_index(cmd->hdr.service, cmd->hdr.opcode, - cmd->hdr.index, status); - } + /* Allow to delay only 1 command. This is for convenience only + * of using cmd data without need of copying those in async + * functions. Should be not needed eventually. + */ + if (status == BTP_STATUS_DELAY_REPLY) { + assert(delayed_cmd == NULL); + delayed_cmd = cmd; + return; + } + + if ((status == BTP_STATUS_SUCCESS) && rsp_len > 0) { + tester_send_with_index(cmd->hdr.service, cmd->hdr.opcode, + cmd->hdr.index, cmd->rsp, rsp_len); + } else { + tester_rsp_with_index(cmd->hdr.service, cmd->hdr.opcode, + cmd->hdr.index, status); } os_eventq_put(&avail_queue, ev); @@ -325,10 +334,35 @@ void tester_send(uint8_t service, uint8_t opcode, uint8_t *data, size_t len) { tester_send_with_index(service, opcode, BTP_INDEX, data, len); + + /* async response to command */ + if (opcode < 0x80) { + struct btp_buf *cmd; + + assert(delayed_cmd != NULL); + + cmd = delayed_cmd; + delayed_cmd = NULL; + + (void)memset(cmd, 0, sizeof(*cmd)); + os_eventq_put(&avail_queue, + CONTAINER_OF(cmd, struct btp_buf, data)->ev); + } } void tester_rsp(uint8_t service, uint8_t opcode, uint8_t status) { + struct btp_buf *cmd; + tester_rsp_with_index(service, opcode, BTP_INDEX, status); + + assert(delayed_cmd != NULL); + + cmd = delayed_cmd; + delayed_cmd = NULL; + + (void)memset(cmd, 0, sizeof(*cmd)); + os_eventq_put(&avail_queue, + CONTAINER_OF(cmd, struct btp_buf, data)->ev); } From ffdb1f02fe3ef56b2cc89d726f6a830955463755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Thu, 13 Jul 2023 10:04:56 +0200 Subject: [PATCH 11/12] bluetooth: tester: Add explicit functions for event and response This make is easier to verify if proper use of BTP is maintained. tester_rsp and tester_rsp_full will be removed eventually when BTP is fully async. --- apps/bttester/src/btp/bttester.h | 4 +- apps/bttester/src/btp_gap.c | 61 ++++++++++++------------- apps/bttester/src/btp_gatt.c | 47 +++++++++---------- apps/bttester/src/btp_gatt_cl.c | 78 ++++++++++++++++---------------- apps/bttester/src/btp_l2cap.c | 16 +++---- apps/bttester/src/btp_mesh.c | 36 +++++++-------- apps/bttester/src/bttester.c | 37 +++++++++------ 7 files changed, 143 insertions(+), 136 deletions(-) diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index c986a7e97e..120e623db3 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -73,7 +73,9 @@ tester_init(void); void tester_rsp(uint8_t service, uint8_t opcode, uint8_t status); void -tester_send(uint8_t service, uint8_t opcode, uint8_t *data, size_t len); +tester_rsp_full(uint8_t service, uint8_t opcode, const void *rsp, size_t len); +void +tester_event(uint8_t service, uint8_t opcode, const void *data, size_t len); struct btp_handler { uint8_t opcode; diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index 946bda75e2..30c36e2291 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -674,8 +674,8 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, * current one */ if (adv_buf->om_len) { - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, - adv_buf->om_data, adv_buf->om_len); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, + adv_buf->om_data, adv_buf->om_len); } store_adv(addr, rssi, data, len); @@ -688,8 +688,8 @@ device_found(ble_addr_t *addr, int8_t rssi, uint8_t evtype, return; } done: - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, - adv_buf->om_data, adv_buf->om_len); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_FOUND, + adv_buf->om_data, adv_buf->om_len); } static int @@ -786,9 +786,8 @@ device_connected_ev_send(struct os_event *ev) return; } - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, - (uint8_t *) &connected_ev, - sizeof(connected_ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, + (uint8_t *) &connected_ev, sizeof(connected_ev)); periph_privacy(desc); } @@ -826,9 +825,9 @@ le_connected(uint16_t conn_handle, int status) os_time_ms_to_ticks32( CONNECTED_EV_DELAY_MS(desc.conn_itvl))); #else - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, - (uint8_t *) &connected_ev, - sizeof(connected_ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_CONNECTED, + (uint8_t *) &connected_ev, + sizeof(connected_ev)); #endif } @@ -869,8 +868,8 @@ le_disconnected(struct ble_gap_conn_desc *conn, int reason) memcpy(&ev.address, addr, sizeof(ev.address)); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_DEVICE_DISCONNECTED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -924,8 +923,8 @@ auth_passkey_display(uint16_t conn_handle, unsigned int passkey) memcpy(&ev.address, addr, sizeof(ev.address)); ev.passkey = htole32(pk.passkey); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_DISPLAY, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -947,8 +946,8 @@ auth_passkey_entry(uint16_t conn_handle) memcpy(&ev.address, addr, sizeof(ev.address)); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_ENTRY_REQ, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -971,8 +970,8 @@ auth_passkey_numcmp(uint16_t conn_handle, unsigned int passkey) memcpy(&ev.address, addr, sizeof(ev.address)); ev.passkey = htole32(passkey); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_PASSKEY_CONFIRM_REQ, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1051,8 +1050,8 @@ le_identity_resolved(uint16_t conn_handle) memcpy(&ev.identity_address, &desc.peer_id_addr, sizeof(ev.identity_address)); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_IDENTITY_RESOLVED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1076,8 +1075,8 @@ le_pairing_failed(uint16_t conn_handle, int reason) ev.reason = reason; - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_PAIRING_FAILED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_PAIRING_FAILED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1093,8 +1092,8 @@ le_conn_param_update(struct ble_gap_conn_desc *desc) ev.conn_latency = desc->conn_latency; ev.supervision_timeout = desc->supervision_timeout; - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_CONN_PARAM_UPDATE, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_CONN_PARAM_UPDATE, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1121,8 +1120,8 @@ le_encryption_changed(struct ble_gap_conn_desc *desc) } } - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_LEVEL_CHANGED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_SEC_LEVEL_CHANGED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1136,10 +1135,8 @@ bond_lost(uint16_t conn_handle) assert(rc == 0); memcpy(&ev.address, &desc.peer_id_addr, sizeof(ev.address)); - tester_send(BTP_SERVICE_ID_GAP, - BTP_GAP_EV_BOND_LOST, - (uint8_t *) &ev, - sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1212,8 +1209,8 @@ adv_complete(void) current_settings &= ~BIT(BTP_GAP_SETTINGS_ADVERTISING); ev.current_settings = htole32(current_settings); - tester_send(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_NEW_SETTINGS, + (uint8_t *) &ev, sizeof(ev)); } static int diff --git a/apps/bttester/src/btp_gatt.c b/apps/bttester/src/btp_gatt.c index 09bd8d7236..b77919466f 100644 --- a/apps/bttester/src/btp_gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -269,8 +269,8 @@ attr_value_changed_ev(uint16_t handle, struct os_mbuf *data) ev->data_length = htole16(os_mbuf_len(data)); os_mbuf_appendfrom(buf, data, 0, os_mbuf_len(data)); - tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_ATTR_VALUE_CHANGED, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATT, BTP_GATT_EV_ATTR_VALUE_CHANGED, + buf->om_data, buf->om_len); } static int @@ -683,8 +683,8 @@ read_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->att_response = (uint8_t) BLE_HS_ATT_ERR(error->status); - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf - .len); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + gatt_buf.buf, gatt_buf.len); read_destroy(); return 0; } @@ -697,8 +697,8 @@ read_cb(uint16_t conn_handle, } rp->data_length += attr->om->om_len; - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - gatt_buf.buf, gatt_buf.len); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + gatt_buf.buf, gatt_buf.len); read_destroy(); return 0; @@ -748,15 +748,15 @@ read_long_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->att_response = (uint8_t) BLE_HS_ATT_ERR(error->status); - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf - .len); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + gatt_buf.buf, gatt_buf.len); read_destroy(); return 0; } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf - .len); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + gatt_buf.buf, gatt_buf.len); read_destroy(); return 0; } @@ -882,8 +882,8 @@ write_rsp(uint16_t conn_handle, const struct ble_gatt_error *error, SYS_LOG_DBG(""); - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - &err, sizeof(err)); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + &err, sizeof(err)); return 0; } @@ -969,8 +969,8 @@ reliable_write_rsp(uint16_t conn_handle, SYS_LOG_DBG("Reliable write status %d", err); - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_RELIABLE_WRITE, - &err, sizeof(err)); + tester_rsp_full(BTP_SERVICE_ID_GATT, BTP_GATT_RELIABLE_WRITE, + &err, sizeof(err)); return 0; } @@ -1081,7 +1081,8 @@ disc_prim_uuid_cb(uint16_t conn_handle, } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, opcode, gatt_buf.buf, gatt_buf.len); + tester_rsp_full(BTP_SERVICE_ID_GATT, opcode, + gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1136,8 +1137,8 @@ disc_all_desc_cb(uint16_t conn_handle, } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, gatt_buf - .buf, gatt_buf.len); + tester_rsp_full(BTP_SERVICE_ID_GATT, BTP_GATT_DISC_ALL_DESC, + gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1253,8 +1254,8 @@ find_included_cb(uint16_t conn_handle, } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, gatt_buf - .buf, gatt_buf.len); + tester_rsp_full(BTP_SERVICE_ID_GATT, BTP_GATT_FIND_INCLUDED, + gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; } @@ -1310,8 +1311,8 @@ disc_chrc_cb(uint16_t conn_handle, } if (error->status == BLE_HS_EDONE) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, gatt_buf.buf, gatt_buf - .len); + tester_rsp_full(BTP_SERVICE_ID_GATT, btp_opcode, + gatt_buf.buf, gatt_buf.len); discover_destroy(); return 0; } @@ -2046,8 +2047,8 @@ tester_gatt_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, ev->data_length = htole16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); - tester_send_buf(BTP_SERVICE_ID_GATT, BTP_GATT_EV_NOTIFICATION, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATT, BTP_GATT_EV_NOTIFICATION, + buf->om_data, buf->om_len); fail: os_mbuf_free_chain(buf); diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index 501c46030e..63f6575626 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -142,8 +142,8 @@ tester_mtu_exchanged_ev(uint16_t conn_handle, ev->mtu = mtu; - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_MTU_EXCHANGED, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_MTU_EXCHANGED, + buf->om_data, buf->om_len); fail: os_mbuf_free_chain(buf); return 0; @@ -205,8 +205,8 @@ disc_prim_svcs_cb(uint16_t conn_handle, rp->status = err; if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->services_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -215,8 +215,8 @@ disc_prim_svcs_cb(uint16_t conn_handle, rp->status = 0; rp->services_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -339,8 +339,8 @@ find_included_cb(uint16_t conn_handle, SYS_LOG_DBG(""); if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->services_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -349,8 +349,8 @@ find_included_cb(uint16_t conn_handle, rp->status = 0; rp->services_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_FIND_INCLUDED_RP, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -454,8 +454,8 @@ disc_chrc_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->characteristics_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -464,8 +464,8 @@ disc_chrc_cb(uint16_t conn_handle, rp->status = 0; rp->characteristics_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -602,8 +602,8 @@ disc_all_desc_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->descriptors_count = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -612,8 +612,8 @@ disc_all_desc_cb(uint16_t conn_handle, rp->status = 0; rp->descriptors_count = gatt_buf.cnt; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_DISC_ALL_DESC_RP, + buf->om_data, buf->om_len); discover_destroy(); goto free; } @@ -710,8 +710,8 @@ read_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->status = (uint8_t) BLE_HS_ATT_ERR(error->status); rp->data_length = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); goto free; } @@ -725,8 +725,8 @@ read_cb(uint16_t conn_handle, rp->status = 0; rp->data_length = attr->om->om_len; os_mbuf_appendfrom(buf, attr->om, 0, os_mbuf_len(attr->om)); - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); free: os_mbuf_free_chain(buf); @@ -794,8 +794,8 @@ read_uuid_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->data_length = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); goto free; } @@ -806,8 +806,8 @@ read_uuid_cb(uint16_t conn_handle, rp->status = 0; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); goto free; } @@ -897,8 +897,8 @@ read_long_cb(uint16_t conn_handle, if (error->status != 0 && error->status != BLE_HS_EDONE) { rp->data_length = 0; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); goto free; } @@ -907,8 +907,8 @@ read_long_cb(uint16_t conn_handle, rp->status = 0; rp->data_length = gatt_buf.len; os_mbuf_append(buf, gatt_buf.buf, gatt_buf.len); - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); read_destroy(); goto free; } @@ -1047,8 +1047,8 @@ write_cb(uint16_t conn_handle, const struct ble_gatt_error *error, memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); free: os_mbuf_free_chain(buf); return rc; @@ -1116,7 +1116,7 @@ write_long(const void *cmd, uint16_t cmd_len, om, write_cb, (void *) BTP_GATTC_WRITE_LONG_RP); if (!rc) { - return BTP_STATUS_DELAY_REPLY; + return BTP_STATUS_SUCCESS; } fail: @@ -1154,8 +1154,8 @@ reliable_write_cb(uint16_t conn_handle, memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_RELIABLE_WRITE_RP, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_RELIABLE_WRITE_RP, + buf->om_data, buf->om_len); free: os_mbuf_free_chain(buf); return rc; @@ -1232,8 +1232,8 @@ subscribe_cb(uint16_t conn_handle, memcpy(&rp->address, &conn.peer_ota_addr, sizeof(rp->address)); rp->status = err; - tester_send_buf(BTP_SERVICE_ID_GATTC, opcode, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, opcode, + buf->om_data, buf->om_len); free: os_mbuf_free_chain(buf); return rc; @@ -1393,8 +1393,8 @@ tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle, ev->data_length = htole16(os_mbuf_len(om)); os_mbuf_appendfrom(buf, om, 0, os_mbuf_len(om)); - tester_send_buf(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_NOTIFICATION_RXED, - CONTROLLER_INDEX, buf); + tester_event(BTP_SERVICE_ID_GATTC, BTP_GATTC_EV_NOTIFICATION_RXED, + buf->om_data, buf->om_len); fail: os_mbuf_free_chain(buf); diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index aa6e158d32..f8450760b1 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -134,8 +134,8 @@ recv_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, } os_mbuf_copydata(buf, 0, ev->data_length, ev->data); - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DATA_RECEIVED, - recv_cb_buf, sizeof(*ev) + ev->data_length); + tester_event(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DATA_RECEIVED, + recv_cb_buf, sizeof(*ev) + ev->data_length); tester_l2cap_coc_recv(chan, buf); } @@ -174,8 +174,8 @@ reconfigured_ev(uint16_t conn_handle, struct ble_l2cap_chan *chan, ev.our_mtu = chan_info->our_coc_mtu; ev.our_mps = chan_info->our_l2cap_mtu; - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_RECONFIGURED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_RECONFIGURED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -203,8 +203,8 @@ connected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); } - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_CONNECTED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -229,8 +229,8 @@ disconnected_cb(uint16_t conn_handle, struct ble_l2cap_chan *chan, memcpy(&ev.address, &desc.peer_ota_addr, sizeof(ev.address)); } - tester_send(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DISCONNECTED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_L2CAP, BTP_L2CAP_EV_DISCONNECTED, + (uint8_t *) &ev, sizeof(ev)); } static int diff --git a/apps/bttester/src/btp_mesh.c b/apps/bttester/src/btp_mesh.c index 3b9054a813..8d557e2af9 100644 --- a/apps/bttester/src/btp_mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -281,8 +281,8 @@ link_open(bt_mesh_prov_bearer_t bearer) return; } - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_OPEN, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_OPEN, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -305,8 +305,8 @@ link_close(bt_mesh_prov_bearer_t bearer) return; } - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_CLOSED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_CLOSED, + (uint8_t *) &ev, sizeof(ev)); } static int @@ -319,8 +319,8 @@ output_number(bt_mesh_output_action_t action, uint32_t number) ev.action = htole16(action); ev.number = htole32(number); - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, + (uint8_t *) &ev, sizeof(ev)); return 0; } @@ -359,8 +359,8 @@ input(bt_mesh_input_action_t action, uint8_t size) ev.action = htole16(action); ev.size = size; - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, + (uint8_t *) &ev, sizeof(ev)); return 0; } @@ -377,8 +377,8 @@ prov_complete(uint16_t net_idx, uint16_t addr) net.local = addr; net.dst = addr; - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, - NULL, 0); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, + NULL, 0); } static void @@ -1014,15 +1014,15 @@ invalid_bearer_cb(uint8_t opcode) SYS_LOG_DBG("opcode 0x%02x", opcode); - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INVALID_BEARER, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INVALID_BEARER, + (uint8_t *) &ev, sizeof(ev)); } static void incomp_timer_exp_cb(void) { - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INCOMP_TIMER_EXP, - NULL, 0); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INCOMP_TIMER_EXP, + NULL, 0); } static struct bt_test_cb bt_test_cb = { @@ -1046,8 +1046,8 @@ lpn_established(uint16_t friend_addr) "Friend 0x%04x Queue Size %d Receive Window %d", friend_addr, lpn->queue_size, lpn->recv_win); - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_ESTABLISHED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_ESTABLISHED, + (uint8_t *) &ev, sizeof(ev)); } static void @@ -1059,8 +1059,8 @@ lpn_terminated(uint16_t friend_addr) SYS_LOG_DBG("Friendship (as LPN) lost with Friend " "0x%04x", friend_addr); - tester_send(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_TERMINATED, - (uint8_t *) &ev, sizeof(ev)); + tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_TERMINATED, + (uint8_t *) &ev, sizeof(ev)); } void diff --git a/apps/bttester/src/bttester.c b/apps/bttester/src/bttester.c index b07661d781..d5e0399a49 100644 --- a/apps/bttester/src/bttester.c +++ b/apps/bttester/src/bttester.c @@ -68,7 +68,7 @@ tester_mbuf_reset(struct os_mbuf *buf) static void tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, - uint8_t *data, size_t len); + const uint8_t *data, size_t len); static void tester_rsp_with_index(uint8_t service, uint8_t opcode, uint8_t index, uint8_t status); @@ -274,7 +274,7 @@ tester_init(void) static void tester_send_with_index(uint8_t service, uint8_t opcode, uint8_t index, - uint8_t *data, size_t len) + const uint8_t *data, size_t len) { struct btp_hdr msg; @@ -331,23 +331,29 @@ tester_rsp_with_index(uint8_t service, uint8_t opcode, uint8_t index, } void -tester_send(uint8_t service, uint8_t opcode, uint8_t *data, size_t len) +tester_event(uint8_t service, uint8_t opcode, const void *data, size_t len) { + assert(opcode >= 0x80); tester_send_with_index(service, opcode, BTP_INDEX, data, len); +} - /* async response to command */ - if (opcode < 0x80) { - struct btp_buf *cmd; +void +tester_rsp_full(uint8_t service, uint8_t opcode, const void *rsp, size_t len) +{ + struct btp_buf *cmd; - assert(delayed_cmd != NULL); + assert(opcode < 0x80); + assert(delayed_cmd != NULL); - cmd = delayed_cmd; - delayed_cmd = NULL; + tester_send_with_index(service, opcode, BTP_INDEX, rsp, len); - (void)memset(cmd, 0, sizeof(*cmd)); - os_eventq_put(&avail_queue, - CONTAINER_OF(cmd, struct btp_buf, data)->ev); - } + cmd = delayed_cmd; + delayed_cmd = NULL; + + (void)memset(cmd, 0, sizeof(*cmd)); + + os_eventq_put(&avail_queue, + CONTAINER_OF(cmd, struct btp_buf, data)->ev); } void @@ -355,10 +361,11 @@ tester_rsp(uint8_t service, uint8_t opcode, uint8_t status) { struct btp_buf *cmd; - tester_rsp_with_index(service, opcode, BTP_INDEX, status); - + assert(opcode < 0x80); assert(delayed_cmd != NULL); + tester_rsp_with_index(service, opcode, BTP_INDEX, status); + cmd = delayed_cmd; delayed_cmd = NULL; From aa409e7831d6742c213a6c3188394a9e7a6e2cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Kopy=C5=9Bci=C5=84ski?= Date: Thu, 13 Jul 2023 10:09:14 +0200 Subject: [PATCH 12/12] bluetooth: tester: Use define for variable length BTP commands This makes code a bit easier to follow. --- apps/bttester/src/btp/bttester.h | 5 +++++ apps/bttester/src/btp_gap.c | 4 ++-- apps/bttester/src/btp_gatt.c | 20 ++++++++++---------- apps/bttester/src/btp_gatt_cl.c | 18 +++++++++--------- apps/bttester/src/btp_l2cap.c | 4 ++-- apps/bttester/src/btp_mesh.c | 10 +++++----- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 120e623db3..8e8b98b5ad 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -77,6 +77,11 @@ tester_rsp_full(uint8_t service, uint8_t opcode, const void *rsp, size_t len); void tester_event(uint8_t service, uint8_t opcode, const void *data, size_t len); +/* Used to indicate that command length is variable and that validation will + * be done in handler. + */ +#define BTP_HANDLER_LENGTH_VARIABLE (-1) + struct btp_handler { uint8_t opcode; uint8_t index; diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index 30c36e2291..ebd076315c 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -1801,7 +1801,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GAP_START_ADVERTISING, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = start_advertising, }, { @@ -1886,7 +1886,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GAP_SET_FILTER_ACCEPT_LIST, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = set_filter_accept_list, }, }; diff --git a/apps/bttester/src/btp_gatt.c b/apps/bttester/src/btp_gatt.c index b77919466f..72115e21ad 100644 --- a/apps/bttester/src/btp_gatt.c +++ b/apps/bttester/src/btp_gatt.c @@ -1918,7 +1918,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATT_DISC_PRIM_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = disc_prim_uuid, }, { @@ -1933,7 +1933,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATT_DISC_CHRC_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = disc_chrc_uuid, }, { @@ -1953,7 +1953,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATT_READ_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = read_uuid, }, { @@ -1963,34 +1963,34 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATT_READ_MULTIPLE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = read_multiple, }, { .opcode = BTP_GATT_WRITE_WITHOUT_RSP, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_without_rsp, }, #if 0 { .opcode = BTP_GATT_SIGNED_WRITE_WITHOUT_RSP, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_signed_without_rsp, }, #endif { .opcode = BTP_GATT_WRITE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_data, }, { .opcode = BTP_GATT_WRITE_LONG, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_long, }, { .opcode = BTP_GATT_RELIABLE_WRITE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = reliable_write, }, { @@ -2005,7 +2005,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATT_GET_ATTRIBUTES, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = get_attrs, }, { diff --git a/apps/bttester/src/btp_gatt_cl.c b/apps/bttester/src/btp_gatt_cl.c index 63f6575626..ff76d7dd7f 100644 --- a/apps/bttester/src/btp_gatt_cl.c +++ b/apps/bttester/src/btp_gatt_cl.c @@ -1459,7 +1459,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATTC_DISC_PRIM_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = disc_prim_uuid, }, { @@ -1474,7 +1474,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATTC_DISC_CHRC_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = disc_chrc_uuid, }, { @@ -1489,7 +1489,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATTC_READ_UUID, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = read_uuid, }, { @@ -1499,34 +1499,34 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_GATTC_READ_MULTIPLE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = read_multiple, }, { .opcode = BTP_GATTC_WRITE_WITHOUT_RSP, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_without_rsp, }, #if 0 { .opcode = BTP_GATTC_SIGNED_WRITE_WITHOUT_RSP, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_signed_without_rsp, }, #endif { .opcode = BTP_GATTC_WRITE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write, }, { .opcode = BTP_GATTC_WRITE_LONG, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = write_long, }, { .opcode = BTP_GATTC_RELIABLE_WRITE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = reliable_write, }, { diff --git a/apps/bttester/src/btp_l2cap.c b/apps/bttester/src/btp_l2cap.c index f8450760b1..34749af7fb 100644 --- a/apps/bttester/src/btp_l2cap.c +++ b/apps/bttester/src/btp_l2cap.c @@ -727,7 +727,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_L2CAP_SEND_DATA, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = send_data, }, { @@ -737,7 +737,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_L2CAP_RECONFIGURE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = reconfigure, }, { diff --git a/apps/bttester/src/btp_mesh.c b/apps/bttester/src/btp_mesh.c index 8d557e2af9..96eaaf81f5 100644 --- a/apps/bttester/src/btp_mesh.c +++ b/apps/bttester/src/btp_mesh.c @@ -837,12 +837,12 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_MESH_CONFIG_PROVISIONING, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = config_prov, }, { .opcode = BTP_MESH_PROVISION_NODE, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = provision_node, }, { @@ -862,7 +862,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_MESH_INPUT_STRING, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = input_string, }, { @@ -887,7 +887,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_MESH_NET_SEND, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = net_send, }, { @@ -902,7 +902,7 @@ static const struct btp_handler handlers[] = { }, { .opcode = BTP_MESH_MODEL_SEND, - .expect_len = -1, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = model_send, }, {