From 2e4de760be33b3293c6af8ae2b6e9cba7bcd3d76 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Fri, 29 Jan 2016 09:41:04 -0500 Subject: tests: move bluetooth tests to tests/ Move all bluetooth testcases to tests/ and change Makefiles accordingly where applicable. Also fix Makefile to make some variables overridable and reduce level of whitelisting in testcase.ini. Change-Id: Ia71ee1fbbb238c45280b1e3c4747ca2f748f5263 Signed-off-by: Anas Nashif --- samples/bluetooth/beacon/Makefile | 2 +- samples/bluetooth/beacon/testcase.ini | 1 - samples/bluetooth/peripheral_sc_only/Makefile | 2 +- samples/bluetooth/shell/testcase.ini | 1 + samples/bluetooth/test_bluetooth/Makefile | 3 - samples/bluetooth/test_bluetooth/bluetooth.c | 73 - samples/bluetooth/tester/Makefile | 37 - samples/bluetooth/tester/README | 2 - samples/bluetooth/tester/btp_spec.txt | 1071 ------------ samples/bluetooth/tester/prj.conf | 20 - samples/bluetooth/tester/prj.mdef | 5 - samples/bluetooth/tester/prj_nble.conf | 5 - samples/bluetooth/tester/src/Makefile | 3 - samples/bluetooth/tester/src/bttester.c | 229 --- samples/bluetooth/tester/src/bttester.h | 529 ------ samples/bluetooth/tester/src/gap.c | 558 ------- samples/bluetooth/tester/src/gatt.c | 1711 -------------------- samples/bluetooth/tester/src/main.c | 32 - samples/bluetooth/tester/testcase.ini | 12 - samples/microkernel/test/test_bluetooth/Makefile | 7 - samples/microkernel/test/test_bluetooth/prj.mdef | 5 - .../microkernel/test/test_bluetooth/prj_arm.conf | 8 - .../microkernel/test/test_bluetooth/prj_x86.conf | 8 - .../microkernel/test/test_bluetooth/testcase.ini | 3 - samples/nanokernel/test/test_bluetooth/Makefile | 6 - .../nanokernel/test/test_bluetooth/prj_arm.conf | 8 - .../nanokernel/test/test_bluetooth/prj_x86.conf | 8 - .../nanokernel/test/test_bluetooth/testcase.ini | 4 - 28 files changed, 3 insertions(+), 4350 deletions(-) delete mode 100644 samples/bluetooth/test_bluetooth/Makefile delete mode 100644 samples/bluetooth/test_bluetooth/bluetooth.c delete mode 100644 samples/bluetooth/tester/Makefile delete mode 100644 samples/bluetooth/tester/README delete mode 100644 samples/bluetooth/tester/btp_spec.txt delete mode 100644 samples/bluetooth/tester/prj.conf delete mode 100644 samples/bluetooth/tester/prj.mdef delete mode 100644 samples/bluetooth/tester/prj_nble.conf delete mode 100644 samples/bluetooth/tester/src/Makefile delete mode 100644 samples/bluetooth/tester/src/bttester.c delete mode 100644 samples/bluetooth/tester/src/bttester.h delete mode 100644 samples/bluetooth/tester/src/gap.c delete mode 100644 samples/bluetooth/tester/src/gatt.c delete mode 100644 samples/bluetooth/tester/src/main.c delete mode 100644 samples/bluetooth/tester/testcase.ini delete mode 100644 samples/microkernel/test/test_bluetooth/Makefile delete mode 100644 samples/microkernel/test/test_bluetooth/prj.mdef delete mode 100644 samples/microkernel/test/test_bluetooth/prj_arm.conf delete mode 100644 samples/microkernel/test/test_bluetooth/prj_x86.conf delete mode 100644 samples/microkernel/test/test_bluetooth/testcase.ini delete mode 100644 samples/nanokernel/test/test_bluetooth/Makefile delete mode 100644 samples/nanokernel/test/test_bluetooth/prj_arm.conf delete mode 100644 samples/nanokernel/test/test_bluetooth/prj_x86.conf delete mode 100644 samples/nanokernel/test/test_bluetooth/testcase.ini (limited to 'samples') diff --git a/samples/bluetooth/beacon/Makefile b/samples/bluetooth/beacon/Makefile index fdc0101e3..01f9c3088 100644 --- a/samples/bluetooth/beacon/Makefile +++ b/samples/bluetooth/beacon/Makefile @@ -1,7 +1,7 @@ BOARD ?= qemu_x86 MDEF_FILE = prj.mdef KERNEL_TYPE = micro -CONF_FILE ?= prj.conf +CONF_FILE = prj.conf QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/bluetooth/beacon/testcase.ini b/samples/bluetooth/beacon/testcase.ini index 53b82525e..98d06cdc6 100644 --- a/samples/bluetooth/beacon/testcase.ini +++ b/samples/bluetooth/beacon/testcase.ini @@ -16,5 +16,4 @@ tags = bluetooth build_only = true extra_args = CONF_FILE="prj_nble.conf" arch_whitelist = x86 -config_whitelist = CONFIG_SOC_QUARK_SE platform_whitelist = arduino_101 diff --git a/samples/bluetooth/peripheral_sc_only/Makefile b/samples/bluetooth/peripheral_sc_only/Makefile index 01f9c3088..fdc0101e3 100644 --- a/samples/bluetooth/peripheral_sc_only/Makefile +++ b/samples/bluetooth/peripheral_sc_only/Makefile @@ -1,7 +1,7 @@ BOARD ?= qemu_x86 MDEF_FILE = prj.mdef KERNEL_TYPE = micro -CONF_FILE = prj.conf +CONF_FILE ?= prj.conf QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/bluetooth/shell/testcase.ini b/samples/bluetooth/shell/testcase.ini index 98d06cdc6..53b82525e 100644 --- a/samples/bluetooth/shell/testcase.ini +++ b/samples/bluetooth/shell/testcase.ini @@ -16,4 +16,5 @@ tags = bluetooth build_only = true extra_args = CONF_FILE="prj_nble.conf" arch_whitelist = x86 +config_whitelist = CONFIG_SOC_QUARK_SE platform_whitelist = arduino_101 diff --git a/samples/bluetooth/test_bluetooth/Makefile b/samples/bluetooth/test_bluetooth/Makefile deleted file mode 100644 index da8e477a0..000000000 --- a/samples/bluetooth/test_bluetooth/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -ccflags-y += -I${srctree}/samples/include - -obj-y = bluetooth.o diff --git a/samples/bluetooth/test_bluetooth/bluetooth.c b/samples/bluetooth/test_bluetooth/bluetooth.c deleted file mode 100644 index 555716bc7..000000000 --- a/samples/bluetooth/test_bluetooth/bluetooth.c +++ /dev/null @@ -1,73 +0,0 @@ -/* bluetooth.c - Bluetooth smoke test */ - -/* - * Copyright (c) 2015 Intel Corporation. - * - * Licensed 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 - -#include -#include - -#include -#include - -#define EXPECTED_ERROR -ENOSYS - -static int driver_open(void) -{ - TC_PRINT("driver: %s\n", __func__); - - /* Indicate that there is no real Bluetooth device */ - return EXPECTED_ERROR; -} - -static int driver_send(enum bt_buf_type type, struct net_buf *buf) -{ - return 0; -} - -static struct bt_driver drv = { - .open = driver_open, - .send = driver_send, -}; - -static void driver_init(void) -{ - bt_driver_register(&drv); -} - -#if defined(CONFIG_MICROKERNEL) -void mainloop(void) -#else -void main(void) -#endif -{ - int ret, ret_code; - - driver_init(); - - ret = bt_enable(NULL); - if (ret == EXPECTED_ERROR) { - ret_code = TC_PASS; - } else { - ret_code = TC_FAIL; - } - - TC_END(ret_code, "%s - %s.\n", ret_code == TC_PASS ? PASS : FAIL, - __func__); - - TC_END_REPORT(ret_code); -} diff --git a/samples/bluetooth/tester/Makefile b/samples/bluetooth/tester/Makefile deleted file mode 100644 index 1ca5ccf23..000000000 --- a/samples/bluetooth/tester/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -# Makefile - Bluetooth tester Makefile for microkernel - -# -# Copyright (c) 2015 Intel Corporation -# -# Licensed 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. -# - -# DESCRIPTION -# Makefile for the Bluetooth tester application -# default for qemu_cortex_m3 as only this currently supports enought UARTs -BOARD ?= qemu_cortex_m3 -ARCH ?= arm - -MDEF_FILE = prj.mdef -KERNEL_TYPE = micro -CONF_FILE ?= prj.conf - -# UART for Bluetooth -QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr - -# UART for Tester -QEMU_EXTRA_FLAGS += -serial pipe:/tmp/bt-stack-tester - -PRJ_INCPATH += ${ZEPHYR_BASE}/include/drivers - -include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/bluetooth/tester/README b/samples/bluetooth/tester/README deleted file mode 100644 index a3a1296ce..000000000 --- a/samples/bluetooth/tester/README +++ /dev/null @@ -1,2 +0,0 @@ -Tester application uses binary protocol for control and is aimed at automated -testing. diff --git a/samples/bluetooth/tester/btp_spec.txt b/samples/bluetooth/tester/btp_spec.txt deleted file mode 100644 index 731cf709b..000000000 --- a/samples/bluetooth/tester/btp_spec.txt +++ /dev/null @@ -1,1071 +0,0 @@ -Tester protocol for Bluetooth stack -*********************************** - -Copyright (C) 2015 Intel Corporation - - -Overview -======== - -This document describes the format of data used for communicating between tester -and implementation under test (IUT). - -The protocol is SOCK_STREAM based and follows a strict PDU specification -with a generic header and initial registration exchange. The communication is -driver from tester with commands/response exchange. The protocol is single PDU -exchanged based, meaning every command requires a response. IUT will use events -to signal notifications. - -Commands and events use single socket. All services are multi-plexed over same -socket. - - .-- IUT --. .--Tester--. - | | | | - | | Command | | - | | <-------------------------- | | - | | | | - | | Response | | - | | --------------------------> | | - | | | | - | | Event | | - | | --------------------------> | | - | | | | - '-----------' '----------' - - -Packet Structures -================= - -Every packet will follow the basic header to support simple multi-plexing -over the same socket. It will also support a basic control channel with service -id 0. Due to use of single socket for command/response and events it is -possible that event(s) will be received before response to command. - - 0 8 16 24 40 - +------------+--------+------------------+-------------+ - | Service ID | Opcode | Controller Index | Data Length | - +------------+--------+------------------+-------------+ - | | - -The unique service ID is assigned by this specification for each service -supported by tester. - -As general rule of thumb, the opcode for command matches the opcode for a -response. Or the opcode 0x00 for an error is returned. - -Events opcodes start from 0x80. - -All fields are in little-endian byte order (least significant byte first). - -Controller Index can have a special value to indicate that -command or event is not related to any controller. Possible values: - - 0x00 to 0xFE - 0xFF - -Error response is common for all services and has fixed structure: - - Opcode 0x00 - Error response - - Response parameters: Status (1 octet) - - Valid status values: 0x01 = Fail - 0x02 = Unknown Command - 0x03 = Not ready - 0x04 = Invalid Index - -Core Service (ID 0) -=================== - -Commands and responses: - - Opcode 0x00 - Error response - - Opcode 0x01 - Read Supported Commands command/response - - Controller Index: - Command parameters: - Response parameters: (variable) - - Each bit in response is a flag indicating if command with - opcode matching bit number is supported. Bit set to 1 means - that command is supported. Bit 0 is reserved and shall always - be set to 0. If specific bit is not present in response (less - than required bytes received) it shall be assumed that command - is not supported. - - In case of an error, the error response will be returned. - - Opcode 0x02 - Read Supported Services command/response - - Controller Index: - Command parameters: - Response parameters: (variable) - - Each bit in response is a flag indicating if service with ID - matching bit number is supported. Bit set to 1 means that - service is supported. If specific bit is not present in response - (less than required bytes received) it shall be assumed that - service is not supported. - - In case of an error, the error response will be returned. - - Opcode 0x03 - Register Service command/response - - Controller Index: - Command parameters: Service ID (1 octet) - Response parameters: - - In case a command is sent for an undeclared service ID, it will - be rejected. Also there will be no events for undeclared - service ID. - - In case of an error, the error response will be returned. - - Opcode 0x04 - Unregister Service command/response - - Controller Index: - Command parameters: Service ID (1 octet) - Response parameters: - - In case of an error, the error response will be returned. - -GAP Service (ID 1) -================== - -Commands and responses: - - Opcode 0x00 - Error response - - Opcode 0x01 - Read Supported Commands command/response - - Controller Index: - Command parameters: - Response parameters: (variable) - - Each bit in response is a flag indicating if command with - opcode matching bit number is supported. Bit set to 1 means - that command is supported. Bit 0 is reserved and shall always - be set to 0. If specific bit is not present in response (less - than required bytes received) it shall be assumed that command - is not supported. - - In case of an error, the error response will be returned. - - Opcode 0x02 - Read Controller Index List command/response - - Controller Index: - Command parameters: - Response parameters: Number of Controllers (1 octet) - Controller Index[i] (1 octet) - - This command returns the list of controllers. - - In case of an error, the error response will be returned. - - Opcode 0x03 - Read Controller Information command/response - - Controller Index: - Command parameters: - Response parameters: Address (6 Octets) - Supported_Settings (4 Octets) - Current_Settings (4 Octets) - Class_Of_Device (3 Octets) - Name (249 Octets) - Short_Name (11 Octets) - - This command is used to retrieve the current state and basic - information of a controller. It is typically used right after - getting the response to the Read Controller Index List command - - Current_Settings and Supported_Settings is a bitmask with - currently the following available bits: - - 0 Powered - 1 Connectable - 2 Fast Connectable - 3 Discoverable - 4 Bondable - 5 Link Level Security (Sec. mode 3) - 6 Secure Simple Pairing - 7 Basic Rate/Enhanced Data Rate - 8 High Speed - 9 Low Energy - 10 Advertising - 11 Secure Connections - 12 Debug Keys - 13 Privacy - 14 Controller Configuration - 15 Static Address - - In case of an error, the error response will be returned. - - Opcode 0x04 - Reset command/response - - Controller Index: - Command parameters: - Response parameters: Current_Settings (4 Octets) - - This allows to clean up any state data (eg. keys) and restore - controller to its default system state. - - In case of an error, the error response will be returned. - - Opcode 0x05 - Set Powered command/response - - Controller Index: - Command parameters: Powered (1 octet) - Response parameters: Current_Settings (4 Octets) - - Valid Powered values: 0x00 = Off - 0x01 = On - - This command is used to power on or off a controller. - - In case of an error, the error response will be returned. - - Opcode 0x06 - Set Connectable command/response - - Controller Index: - Command parameters: Connectable (1 octet) - Response parameters: Current_Settings (4 Octets) - - Valid Connectable values: 0x00 = Off - 0x01 = On - - This command is used to set controller connectable. - - In case of an error, the error response will be returned. - - Opcode 0x07 - Set Fast Connectable command/response - - Controller Index: - Command parameters: Fast Connectable (1 octet) - Response parameters: Current_Settings (4 Octets) - - Valid Fast Connectable values: 0x00 = Off - 0x01 = On - - This command is used to set controller fast connectable. - This command is only available for BR/EDR capable controllers. - - In case of an error, the error response will be returned. - - Opcode 0x08 - Set Discoverable command/response - - Controller Index: - Command parameters: Discoverable (1 octet) - Response parameters: Current_Settings (4 Octets) - - Valid Discoverable values: 0x00 = Off - 0x01 = General Discoverable - 0x02 = Limited Discoverable - - This command is used to set controller discoverable. - - In case of an error, the error response will be returned. - - Opcode 0x09 - Set Bondable command/response - - Controller Index: - Command parameters: Bondable (1 octet) - Response parameters: Current_Settings (4 Octets) - - Valid Bondable values: 0x00 = Off - 0x01 = On - - This command is used to set controller bondable. - - In case of an error, the error response will be returned. - - Opcode 0x0a - Start Advertising command/response - - Controller Index: - Command parameters: Adv_Data_Len (1 octet) - Scan_Rsp_len (1 octet) - Adv_Data (0-255 octets) - Scan_Rsp (0-255 octets) - Return Parameters: Current_Settings (4 Octets) - - This command is used to start advertising. - - In case of an error, the error response will be returned. - - Opcode 0x0b - Stop Advertising command/response - - Controller Index: - Command parameters: - Return Parameters: Current_Settings (4 Octets) - - This command is used to stop advertising. - - In case of an error, the error response will be returned. - - Opcode 0x0c - Start Discovery command/response - - Controller Index: - Command parameters: Flags (1 octet) - Return Parameters: - - Possible values for the Flags parameter are a bit-wise or - of the following bits: - 0 = LE scan - 1 = BR/EDR scan - 2 = Use limited discovery procedure - - This command is used to start discovery. - - In case of an error, the error response will be returned. - - Opcode 0x0d - Stop Discovery command/response - - Controller Index: - Command parameters: - Return Parameters: - - This command is used to stop discovery. - - In case of an error, the error response will be returned. - - Opcode 0x0e - Connect command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Return Parameters: - - Valid Address_Type parameter values: - 0x00 = Public - 0x01 = Random - - This command is used to create a Link Layer connection with - remote device. - - In case of an error, the error response will be returned. - - Opcode 0x0f - Disconnect command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Return Parameters: - - Valid Address_Type parameter values: - 0x00 = Public - 0x01 = Random - - This command is used to terminate an existing connection or - to cancel pending connection attempt. - - In case of an error, the error response will be returned. - - Opcode 0x10 - Set IO Capability command/response - - Controller Index: - Command parameters: IO_Capability (1 octet) - Return Parameters: - - Valid IO_Capabilities parameter values: - 0x00 = Display Only - 0x01 = Display Yes/No - 0x02 = Keyboard Only - 0x03 = No Input, No Output - 0x04 = Keyboard Display - - This command is used to set IO capability. - - In case of an error, the error response will be returned. - - Opcode 0x11 - Pair command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Return Parameters: - - This command is used to initiate pairing with remote. - - In case of an error, the error response will be returned. - - Opcode 0x12 - Unpair command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Return Parameters: - - This command is used to unpair with remote. - - In case of an error, the error response will be returned. - - Opcode 0x13 - Passkey Entry Response command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Passkey (4 octets) - Return Parameters: - - This command is used to response with passkey for pairing - request. - - In case of an error, the error response will be returned. - - Opcode 0x14 - Passkey Confirmation Response command/response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Match (1 octet) - Return Parameters: - - This command is used to response for pairing request with - confirmation in accordance with initiator and responder - passkey. - - In case of an error, the error response will be returned. - -Events: - Opcode 0x80 - New Settings event - - Controller Index: - Event parameters: Current_Settings (4 octets) - - This event indicates that one or more of the settings for a - controller has changed. - - Opcode 0x81 - Device Found event - - Controller Index: - Event parameters: Address (6 octets) - Address_Type (1 octet) - RSSI (1 octet) - Flags (1 octet) - EIR_Data_Length (2 Octets) - EIR_Data (0-65535 Octets) - - Possible values for the Flags parameter are a bit-wise or - of the following bits: - 0 = RSSI valid - 1 = Adv_Data included - 2 = Scan_Rsp included - - This event indicates that a device was found during device - discovery. - - Opcode 0x82 - Device Connected event - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - - This event indicates that a device was connected. - - Opcode 0x83 - Device Disconnected event - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - - This event indicates that a device was disconnected. - - Opcode 0x84 - Passkey Display event - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - Passkey (4 octets) - - This event indicates that passkey was received and it needs to - be confirmed on remote side. - - Opcode 0x85 - Passkey Enter Request event - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - - This event indicates that remote requests for passkey enter. - - Opcode 0x86 - Passkey Confirm Request event - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - Passkey (4 octets) - - This event indicates that passkey needs to be confirmed. - -GATT Service (ID 2) -=================== - -Commands and responses: - - Opcode 0x00 - Error response - - Opcode 0x01 - Read Supported Commands command/response - - Controller Index: - Command parameters: - Response parameters: (variable) - - Each bit in response is a flag indicating if command with - opcode matching bit number is supported. Bit set to 1 means - that command is supported. Bit 0 is reserved and shall always - be set to 0. If specific bit is not present in response (less - than required bytes received) it shall be assumed that command - is not supported. - - In case of an error, the error response will be returned. - - Opcode 0x02 - Add Service - - Controller Index: - Command parameters: Type (1 octet) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: Service_ID (2 octets) - - Valid Type parameter values: - 0x00 = Primary - 0x01 = Secondary - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This command is used to add new service to GATT Server. - Service ID of service declaration is returned in the response. - Attribute database shall be initiated sequentially. - After this issuing this command tester shall add characteristics - or included services this service contains. - - In case of an error, the error response will be returned. - - Opcode 0x03 - Add Characteristic - - Controller Index: - Command parameters: Service_ID (2 octets) - Properties (1 octet) - Permissions (1 octet) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: Characteristic_ID (2 octets) - - Possible values for the Properties parameter are a bit-wise - of the following bits: - - 0 Broadcast - 1 Read - 2 Write Without Response - 3 Write - 4 Notify - 5 Indicate - 6 Authenticated Signed Writes - 7 Extended Properties - - Possible values for the Permissions parameter are a bit-wise - of the following bits: - - 0 Read - 1 Write - 2 Read with Encryption - 3 Write with Encryption - 4 Read with Authentication - 5 Write with Authentication - 6 Authorization - - This command is used to add new characteristic to GATT Server. - Characteristic ID of characteristic declaration is returned in - the response. - Attribute's database shall be initiated sequentially. - After issuing this command tester can add descriptors to this - characteristic. - - In case of an error, the error response will be returned. - - Opcode 0x04 - Add Descriptor - - Controller Index: - Command parameters: Characteristic_ID (2 octets) - Permissions (1 octet) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: Descriptor_ID (2 octets) - - This command is used to add new characteristic descriptor - to GATT Server. The command shall be used right after - Add Characteristic or previous Add Descriptor command. - - In case of an error, the error response will be returned. - - Opcode 0x05 - Add Included Service - - Controller Index: - Command parameters: Service_ID (2 octets) - Response parameters: Included_Service_ID (2 octets) - - This command is used to add new included service declaration - to GATT Server. Service that is going to be included has to be - already added to the server. Attribute_ID of include - declaration is returned in the response. - - In case of an error, the error response will be returned. - - Opcode 0x06 - Set Characteristic/Descriptor Value - - Controller Index: - Command parameters: Attribute_ID (2 octets) - Value_Length (2 octet) - Value (1-512 octets) - Response parameters: - - This command is used to set the value of characteristic - or descriptor. - - In case of an error, the error response will be returned. - - Opcode 0x07 - Start Server - - Controller Index: - Command parameters: - Response parameters: - - This command is used to start server with previously prepared - attributes database. - Subsequent calls of this command shall return an error. - - In case of an error, the error response will be returned. - - Opcode 0x08 - Reset Server - - Controller Index: - Command parameters: - Response parameters: - - This command is used to clear the server from attributes. - - In case of an error, the error response will be returned. - - Opcode 0x09 - Set Required Encryption Key Size - - Controller Index: - Command parameters: Attribute_ID (2 octets) - Encryption_Key_Size (1 octet) - Response parameters: - - This command is used to set required Encryption Key Size of an - attribute. It shall be used only if encryption or authentication - is needed to have access to this attribute. Otherwise an error - shall be returned. - - Possible values for Encryption_Key_Size parameter are: - <0x07, 0x0f> - - In case of an error, the error response will be returned. - - Opcode 0x0a -> 0x3f - Reserved for future use - - Opcode 0x40 - Exchange MTU - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Response parameters: - - This command is used by GATT Client to configure ATT protocol. - IUT is expected to send Exchange MTU Request to negotiate - MTU size. - - In case of an error, the error response will be returned. - - Opcode 0x41 - Discover All Primary Services - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Response parameters: Services_Count (1 octet) - [array] Service (variable) - - Object Service is defined as: - Start_Handle (2 octets) - End_Group_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover all primary - services on a server. - Services found during discovery are returned in the response. - - In case of an error, the error response will be returned. - - Opcode 0x42 - Discover Primary Service by UUID - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: Services_Count (1 octet) - [array] Service (variable) - - Object Service is defined as: - Start_Handle (2 octets) - End_Group_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover primary services - with specific UUID on a server. - Services found during discovery are returned in the response. - - In case of an error, the error response will be returned. - - Opcode 0x43 - Find Included Services - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Service_Start_Handle (2 octets) - Service_End_Handle (2 octets) - Response parameters: Services_Count (1 octet) - [array] Included_Service (variable) - - Object Included_Service is defined as: - Included_Handle (2 octets) - Type (1 octet) - Service (7 or 21 octets) - - Valid Type parameter values: - 0x00 = Primary - 0x01 = Secondary - - Object Service is defined as: - Start_Handle (2 octets) - End_Group_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover service - relationships to other services. - Services found during discovery are returned in the response. - - Opcode 0x44 - Discover All Characteristics of a Service - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Service_Start_Handle (2 octets) - Service_End_Handle (2 octets) - Response parameters: Characteristics_Count (1 octet) - [array] Characteristic (variable) - - Object Characteristic is defined as: - Characteristic_Handle (2 octets) - Value_Handle (2 octets) - Properties (1 octet) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover all - characteristics within specified service range. - Characteristics found during discovery are returned in the - response. - - In case of an error, the error response will be returned. - - Opcode 0x45 - Discover Characteristics by UUID - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Start_Handle (2 octets) - End_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: Characteristics_Count (1 octet) - [array] Characteristic (variable) - - Object Characteristic is defined as: - Characteristic_Handle (2 octets) - Value_Handle (2 octets) - Properties (1 octet) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover characteristic - declarations with given UUID on a server. - Characteristics found during discovery are returned in the - response. - - In case of an error, the error response will be returned. - - Opcode 0x46 - Discover All Characteristic Descriptors - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Start_Handle (2 octets) - End_Handle (2 octets) - Response parameters: Descriptors_Count (1 octet) - [array] Descriptor (variable) - - Object Descriptor is defined as: - Descriptor_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used by a client to discover all the - characteristic descriptors contained within characteristic. - Descriptors found during discovery are returned in the - response. - - In case of an error, the error response will be returned. - - Opcode 0x47 - Read Characteristic Value/Descriptor - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Response parameters: ATT_Response (1 octet) - Data_Length (2 octets) - Data (variable) - - This procedure is used to read a Characteristic Value or - Characteristic Descriptor from a server. - Read results are returned in the response to this command. - ATT_Response shall be set to 0x00, if Read has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x48 - Read Using Characteristic UUID - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Start_Handle (2 octets) - End_Handle (2 octets) - UUID_Length (1 octet) - UUID (2 or 16 octets) - Response parameters: ATT_Response (1 octet)s - Data_Length (2 octets) - Data (variable) - - Valid UUID_Length parameter values: - 0x02 = UUID16 - 0x10 = UUID128 - - This procedure is used to read a Characteristic Value from a - server when characteristic UUID is known. - Read results are returned in the response to this command. - ATT_Response shall be set to 0x00, if Read has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x49 - Read Long Characteristic Value/Descriptor - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Offset (2 octets) - Response parameters: ATT_Response (1 octet) - Data_Length (2 octets) - Data (variable) - - This procedure is used to read Long Characteristic Value or - Long Characteristic Descriptor from a server. - Read results are returned in the response to this command. - ATT_Response shall be set to 0x00, if Read has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x4a - Read Multiple Characteristic Values - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handles_Count (1 octet) - Handles (variable) - Response parameters: ATT_Response (1 octet) - Data_Length (2 octets) - Data (variable) - - This procedure is used to read multiple Characteristic Values - from a server. - Read results are returned in the response to this command. - ATT_Response shall be set to 0x00, if Read has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x4b - Write Without Response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Data_Length (2 octets) - Data (variable) - Response parameters: - - This procedure is used to write a Characteristic Value to a - server. There is no acknowledgment that the write was - successfully performed. - - In case of an error, the error response will be returned. - - Opcode 0x4c - Signed Write Without Response - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Data_Length (2 octets) - Data (variable) - Response parameters: - - This procedure is used to write a Characteristic Value to a - server. There is no acknowledgment that the write was - successfully performed. This procedure is intended to be used - if client and server are bonded, and connected using - non-encrypted link. - - In case of an error, the error response will be returned. - - Opcode 0x4d - Write Characteristic Value/Descriptor - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Data_Length (2 octets) - Data (variable) - Response parameters: ATT_Response (1 octet) - - This procedure is used to write a Characteristic Value or - Characteristic Descriptor to a server. - ATT_Response shall be set to 0x00, if Write has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x4e - Write Long Characteristic Value/Descriptor - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Offset (2 octets) - Data_Length (2 octets) - Data (variable) - Response parameters: ATT_Response (1 octet) - - This procedure is used to write a Long Characteristic Value or - Long Characteristic Descriptor to a server. - ATT_Response shall be set to 0x00, if Write has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x4f - Reliable Write - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Handle (2 octets) - Offset (2 octets) - Data_Length (2 octets) - Data (variable) - Response parameters: ATT_Response (1 octet) - - This procedure is used to write a Characteristic Value to - a server and assurance is required that the correct - Characteristic Value is going to be written. - ATT_Response shall be set to 0x00, if Write has been completed - successfully. Otherwise it shall be set to ATT error code - received. - - In case of an error, the error response will be returned. - - Opcode 0x50 - Configure Notifications - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Enable (1 octet) - CCC_Handle (2 octets) - Response parameters: - - This procedure is used to configure server to notify - characteristic value to a client. - - In case of an error, the error response will be returned. - - Opcode 0x51 - Configure Indications - - Controller Index: - Command parameters: Address_Type (1 octet) - Address (6 octets) - Enable (1 octet) - CCC_Handle (2 octets) - Response parameters: - - This procedure is used to configure server to indicate - characteristic value to a client. - - In case of an error, the error response will be returned. - -Events: - Opcode 0x80 -> 0xbf - Reserved for future use - - Opcode 0xc0 - Notification/Indication Received - - Controller Index: - Event parameters: Address_Type (1 octet) - Address (6 octets) - Type (1 octet) - Handle (2 octets) - Data_Length (2 octets) - Data (variable) - - Valid Type parameter values: - 0x01 = Notification - 0x02 = Indication - - This event indicates that IUT has received notification - or notification. diff --git a/samples/bluetooth/tester/prj.conf b/samples/bluetooth/tester/prj.conf deleted file mode 100644 index 617e07ccc..000000000 --- a/samples/bluetooth/tester/prj.conf +++ /dev/null @@ -1,20 +0,0 @@ -CONFIG_UART_PIPE=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_BLUETOOTH=y -CONFIG_BLUETOOTH_LE=y -CONFIG_BLUETOOTH_CENTRAL=y -CONFIG_BLUETOOTH_PERIPHERAL=y -CONFIG_BLUETOOTH_SMP=y -CONFIG_BLUETOOTH_SIGNING=y -CONFIG_BLUETOOTH_GATT_DYNAMIC_DB=y -CONFIG_BLUETOOTH_GATT_CLIENT=y -CONFIG_BLUETOOTH_DEBUG=y -CONFIG_BLUETOOTH_DEBUG_HCI_CORE=y -CONFIG_BLUETOOTH_DEBUG_BUF=y -CONFIG_BLUETOOTH_DEBUG_CONN=y -CONFIG_BLUETOOTH_DEBUG_L2CAP=y -CONFIG_BLUETOOTH_DEBUG_SMP=y -CONFIG_BLUETOOTH_DEBUG_ATT=y -CONFIG_BLUETOOTH_DEBUG_GATT=y -CONFIG_INIT_STACKS=y -CONFIG_PRINTK=y diff --git a/samples/bluetooth/tester/prj.mdef b/samples/bluetooth/tester/prj.mdef deleted file mode 100644 index dd08b4bd3..000000000 --- a/samples/bluetooth/tester/prj.mdef +++ /dev/null @@ -1,5 +0,0 @@ -% Application : Bluetooth Tester - -% TASK NAME PRIO ENTRY STACK GROUPS -% =================================================== - TASK MAIN 7 mainloop 2048 [EXE] diff --git a/samples/bluetooth/tester/prj_nble.conf b/samples/bluetooth/tester/prj_nble.conf deleted file mode 100644 index 03112f7a4..000000000 --- a/samples/bluetooth/tester/prj_nble.conf +++ /dev/null @@ -1,5 +0,0 @@ -CONFIG_UART_PIPE=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_NBLE=y -CONFIG_ARC_INIT=n -CONFIG_UART_CONSOLE=n diff --git a/samples/bluetooth/tester/src/Makefile b/samples/bluetooth/tester/src/Makefile deleted file mode 100644 index 5640f877a..000000000 --- a/samples/bluetooth/tester/src/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -ccflags-y +=-I$(srctree)/include/drivers - -obj-y = bttester.o gap.o gatt.o main.o diff --git a/samples/bluetooth/tester/src/bttester.c b/samples/bluetooth/tester/src/bttester.c deleted file mode 100644 index de4d5aaec..000000000 --- a/samples/bluetooth/tester/src/bttester.c +++ /dev/null @@ -1,229 +0,0 @@ -/* bttester.c - Bluetooth Tester */ - -/* - * Copyright (c) 2015 Intel Corporation - * - * Licensed 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 -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "bttester.h" - -#define STACKSIZE 2048 -static char __stack stack[STACKSIZE]; - -#define CMD_QUEUED 2 -static uint8_t cmd_buf[CMD_QUEUED * BTP_MTU]; - -static struct nano_fifo cmds_queue; -static struct nano_fifo avail_queue; - -static void supported_commands(uint8_t *data, uint16_t len) -{ - uint8_t buf[1]; - struct core_read_supported_commands_rp *rp = (void *) buf; - - buf[0] = 1 << CORE_READ_SUPPORTED_COMMANDS; - buf[0] |= 1 << CORE_READ_SUPPORTED_SERVICES; - buf[0] |= 1 << CORE_REGISTER_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; - - buf[0] = 1 << BTP_SERVICE_ID_CORE; - buf[0] |= 1 << BTP_SERVICE_ID_GAP; - buf[0] |= 1 << BTP_SERVICE_ID_GATT; - - 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(); - break; - case BTP_SERVICE_ID_GATT: - status = tester_init_gatt(); - break; - default: - status = BTP_STATUS_FAILED; - break; - } - - tester_rsp(BTP_SERVICE_ID_CORE, CORE_REGISTER_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; - default: - tester_rsp(BTP_SERVICE_ID_CORE, opcode, BTP_INDEX_NONE, - BTP_STATUS_UNKNOWN_CMD); - return; - } -} - -static void cmd_handler(int arg1, int arg2) -{ - while (1) { - struct btp_hdr *cmd; - uint16_t len; - - cmd = nano_fiber_fifo_get(&cmds_queue, TICKS_UNLIMITED); - - len = sys_le16_to_cpu(cmd->len); - - /* TODO - * verify if service is registered before calling handler - */ - - switch (cmd->service) { - case BTP_SERVICE_ID_CORE: - handle_core(cmd->opcode, cmd->index, cmd->data, len); - break; - case BTP_SERVICE_ID_GAP: - tester_handle_gap(cmd->opcode, cmd->index, cmd->data, - len); - break; - case BTP_SERVICE_ID_GATT: - tester_handle_gatt(cmd->opcode, cmd->index, cmd->data, - len); - break; - default: - tester_rsp(cmd->service, cmd->opcode, cmd->index, - BTP_STATUS_FAILED); - break; - } - - nano_fiber_fifo_put(&avail_queue, cmd); - } -} - -static uint8_t *recv_cb(uint8_t *buf, size_t *off) -{ - struct btp_hdr *cmd = (void *) buf; - uint8_t *new_buf; - uint16_t len; - - if (*off < sizeof(*cmd)) { - return buf; - } - - len = sys_le16_to_cpu(cmd->len); - if (len > BTP_MTU - sizeof(*cmd)) { - printk("BT tester: invalid packet length\n"); - *off = 0; - return buf; - } - - if (*off < sizeof(*cmd) + len) { - return buf; - } - - new_buf = nano_fifo_get(&avail_queue, TICKS_NONE); - if (!new_buf) { - printk("BT tester: RX overflow\n"); - *off = 0; - return buf; - } - - nano_fifo_put(&cmds_queue, buf); - - *off = 0; - return new_buf; -} - -void tester_init(void) -{ - int i; - - nano_fifo_init(&cmds_queue); - nano_fifo_init(&avail_queue); - - for (i = 0; i < CMD_QUEUED; i++) { - nano_fifo_put(&avail_queue, &cmd_buf[i * BTP_MTU]); - } - - task_fiber_start(stack, STACKSIZE, cmd_handler, 0, 0, 7, 0); - - uart_pipe_register(nano_fifo_get(&avail_queue, TICKS_NONE), - BTP_MTU, recv_cb); - - printk("BT tester initialized\n"); -} - -void tester_send(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; - - uart_pipe_send((uint8_t *)&msg, sizeof(msg)); - if (data && len) { - uart_pipe_send(data, len); - } -} - -void tester_rsp(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); - return; - } - - s.code = status; - tester_send(service, BTP_STATUS, index, (uint8_t *) &s, sizeof(s)); -} diff --git a/samples/bluetooth/tester/src/bttester.h b/samples/bluetooth/tester/src/bttester.h deleted file mode 100644 index 89815b9a3..000000000 --- a/samples/bluetooth/tester/src/bttester.h +++ /dev/null @@ -1,529 +0,0 @@ -/* bttester.h - Bluetooth tester headers */ - -/* - * Copyright (c) 2015 Intel Corporation - * - * Licensed 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. - */ - -#define BTP_MTU 1024 - -#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_STATUS_SUCCESS 0x00 -#define BTP_STATUS_FAILED 0x01 -#define BTP_STATUS_UNKNOWN_CMD 0x02 -#define BTP_STATUS_NOT_READY 0x03 - -#define BTTESTER_DBG(fmt, ...) printf("bttester: %s: " fmt "\n", __func__, \ - ##__VA_ARGS__) - -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; - -/* 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_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 gap_set_bondable_cmd; -} __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_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; - -/* 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[6]; - uint8_t address_type; - 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[6]; - uint8_t address_type; -} __packed; - -#define GAP_EV_DEVICE_DISCONNECTED 0x83 -struct gap_device_disconnected_ev { - uint8_t address[6]; - uint8_t address_type; -} __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; - -/* 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 - -#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 */ -#define GATT_CLIENT_OP_OFFSET 0x40 - -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_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 0x40 - -#define GATT_DISC_PRIM_UUID 0x42 -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 0x43 -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 0x44 -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 0x45 -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 0x46 -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 0x47 -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_LONG 0x49 -struct gatt_read_long_cmd { - uint8_t address_type; - uint8_t address[6]; - uint16_t handle; - uint16_t offset; -} __packed; - -#define GATT_READ_MULTIPLE 0x4a -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 0x4b -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 0x4c -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 0x4d -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 0x4e -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_CFG_NOTIFY 0x50 -#define GATT_CFG_INDICATE 0x51 -struct gatt_cfg_notify_cmd { - uint8_t address_type; - uint8_t address[6]; - uint8_t enable; - uint16_t ccc_handle; -} __packed; - -/* GATT events */ -#define GATT_EV_NOTIFICATION 0xc0 -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; - -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); - -uint8_t tester_init_gap(void); -void tester_handle_gap(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); -uint8_t tester_init_gatt(void); -void tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len); diff --git a/samples/bluetooth/tester/src/gap.c b/samples/bluetooth/tester/src/gap.c deleted file mode 100644 index ae4bb7c97..000000000 --- a/samples/bluetooth/tester/src/gap.c +++ /dev/null @@ -1,558 +0,0 @@ -/* gap.c - Bluetooth GAP Tester */ - -/* - * Copyright (c) 2015 Intel Corporation - * - * Licensed 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 -#include -#include - -#include -#include -#include - -#include -#include - -#include "bttester.h" - -#define CONTROLLER_INDEX 0 -#define CONTROLLER_NAME "btp_tester" - -/* TODO add api for reading real address */ -#define CONTROLLER_ADDR (&(bt_addr_t) {{1, 2, 3, 4, 5, 6}}) - -static atomic_t current_settings; -struct bt_conn_auth_cb cb; - -static void le_connected(struct bt_conn *conn, uint8_t err) -{ - struct gap_device_connected_ev ev; - const bt_addr_le_t *addr = bt_conn_get_dst(conn); - - if (err) { - return; - } - - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; - - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_CONNECTED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); -} - -static void le_disconnected(struct bt_conn *conn, uint8_t reason) -{ - struct gap_device_disconnected_ev ev; - const bt_addr_le_t *addr = bt_conn_get_dst(conn); - - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; - - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_DISCONNECTED, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); -} - -static struct bt_conn_cb conn_callbacks = { - .connected = le_connected, - .disconnected = le_disconnected, -}; - -static void supported_commands(uint8_t *data, uint16_t len) -{ - uint16_t cmds; - struct gap_read_supported_commands_rp *rp = (void *) &cmds; - - cmds = 1 << GAP_READ_SUPPORTED_COMMANDS; - cmds |= 1 << GAP_READ_CONTROLLER_INDEX_LIST; - cmds |= 1 << GAP_READ_CONTROLLER_INFO; - cmds |= 1 << GAP_SET_CONNECTABLE; - cmds |= 1 << GAP_SET_DISCOVERABLE; - cmds |= 1 << GAP_START_ADVERTISING; - cmds |= 1 << GAP_STOP_ADVERTISING; - cmds |= 1 << GAP_START_DISCOVERY; - cmds |= 1 << GAP_STOP_DISCOVERY; - cmds |= 1 << GAP_DISCONNECT; - cmds |= 1 << GAP_SET_IO_CAP; - cmds |= 1 << GAP_PAIR; - cmds |= 1 << GAP_PASSKEY_ENTRY; - - tester_send(BTP_SERVICE_ID_GAP, 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]; - - rp = (void *) buf; - - rp->num = 1; - rp->index[0] = CONTROLLER_INDEX; - - tester_send(BTP_SERVICE_ID_GAP, 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; - - memset(&rp, 0, sizeof(rp)); - memcpy(rp.address, CONTROLLER_ADDR, sizeof(bt_addr_t)); - - supported_settings = 1 << GAP_SETTINGS_POWERED; - supported_settings |= 1 << GAP_SETTINGS_CONNECTABLE; - supported_settings |= 1 << GAP_SETTINGS_BONDABLE; - supported_settings |= 1 << GAP_SETTINGS_LE; - supported_settings |= 1 << GAP_SETTINGS_ADVERTISING; - - 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)); - - tester_send(BTP_SERVICE_ID_GAP, GAP_READ_CONTROLLER_INFO, - CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); -} - -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; - - if (cmd->connectable) { - atomic_set_bit(¤t_settings, GAP_SETTINGS_CONNECTABLE); - } else { - atomic_clear_bit(¤t_settings, GAP_SETTINGS_CONNECTABLE); - } - - rp.current_settings = sys_cpu_to_le32(current_settings); - - tester_send(BTP_SERVICE_ID_GAP, GAP_SET_CONNECTABLE, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); -} - -static uint8_t ad_flags = BT_LE_AD_NO_BREDR; -static struct bt_data ad[10] = { - BT_DATA(BT_DATA_FLAGS, &ad_flags, sizeof(ad_flags)), -}; - -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; - - switch (cmd->discoverable) { - case GAP_NON_DISCOVERABLE: - ad_flags &= ~(BT_LE_AD_GENERAL | BT_LE_AD_LIMITED); - atomic_clear_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); - break; - case GAP_GENERAL_DISCOVERABLE: - ad_flags &= ~BT_LE_AD_LIMITED; - ad_flags |= BT_LE_AD_GENERAL; - atomic_set_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); - break; - case GAP_LIMITED_DISCOVERABLE: - ad_flags &= ~BT_LE_AD_GENERAL; - ad_flags |= BT_LE_AD_LIMITED; - atomic_set_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); - break; - default: - tester_rsp(BTP_SERVICE_ID_GAP, 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, - (uint8_t *) &rp, sizeof(rp)); -} - -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; - uint8_t adv_type, adv_len; - int i; - - if (atomic_test_bit(¤t_settings, GAP_SETTINGS_CONNECTABLE)) { - adv_type = BT_LE_ADV_IND; - } else { - adv_type = BT_LE_ADV_NONCONN_IND; - } - - for (i = 0, adv_len = 1; i < cmd->adv_data_len; adv_len++) { - if (adv_len >= ARRAY_SIZE(ad)) { - BTTESTER_DBG("ad[] Out of memory"); - goto fail; - } - - 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]; - i += ad[adv_len].data_len; - } - - if (bt_le_adv_start(BT_LE_ADV(adv_type), ad, adv_len, NULL, 0) < 0) { - BTTESTER_DBG("Failed to start advertising"); - goto fail; - } - - atomic_set_bit(¤t_settings, GAP_SETTINGS_ADVERTISING); - rp.current_settings = sys_cpu_to_le32(current_settings); - - tester_send(BTP_SERVICE_ID_GAP, GAP_START_ADVERTISING, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); - return; -fail: - tester_rsp(BTP_SERVICE_ID_GAP, 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; - - if (bt_le_adv_stop() < 0) { - tester_rsp(BTP_SERVICE_ID_GAP, GAP_STOP_ADVERTISING, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - return; - } - - atomic_clear_bit(¤t_settings, GAP_SETTINGS_ADVERTISING); - rp.current_settings = sys_cpu_to_le32(current_settings); - - tester_send(BTP_SERVICE_ID_GAP, GAP_STOP_ADVERTISING, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); -} - -static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t evtype, - const uint8_t *ad, uint8_t len) -{ - struct gap_device_found_ev *ev; - uint8_t buf[sizeof(*ev) + len]; - - ev = (void*) buf; - - memcpy(ev->address, addr->val, sizeof(ev->address)); - ev->address_type = addr->type; - - ev->flags = GAP_DEVICE_FOUND_FLAG_RSSI; - ev->rssi = rssi; - - if (evtype == BT_LE_ADV_SCAN_RSP) { - ev->flags |= GAP_DEVICE_FOUND_FLAG_SD; - } else { - ev->flags |= GAP_DEVICE_FOUND_FLAG_AD; - } - - ev->eir_data_len = len; - if (len) { - memcpy(ev->eir_data, ad, len); - } - - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_DEVICE_FOUND, CONTROLLER_INDEX, - buf, sizeof(buf)); -} - -static void start_discovery(const uint8_t *data, uint16_t len) -{ - const struct gap_start_discovery_cmd *cmd = (void *) data; - uint8_t status; - - /* only LE scan is supported */ - if (cmd->flags & (~GAP_DISCOVERY_FLAG_LE)) { - status = BTP_STATUS_FAILED; - goto reply; - } - - if (bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found) < 0) { - status = BTP_STATUS_FAILED; - goto reply; - } - - status = BTP_STATUS_SUCCESS; -reply: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_START_DISCOVERY, CONTROLLER_INDEX, - status); -} - -static void stop_discovery(const uint8_t *data, uint16_t len) -{ - uint8_t status = BTP_STATUS_SUCCESS; - - if (bt_le_scan_stop() < 0) { - status = BTP_STATUS_FAILED; - } - - tester_rsp(BTP_SERVICE_ID_GAP, GAP_STOP_DISCOVERY, CONTROLLER_INDEX, - status); -} - -static void connect(const uint8_t *data, uint16_t len) -{ - struct bt_conn *conn; - uint8_t status; - - conn = bt_conn_create_le((bt_addr_le_t *) data, - BT_LE_CONN_PARAM_DEFAULT); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - bt_conn_unref(conn); - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_CONNECT, CONTROLLER_INDEX, status); -} - -static void disconnect(const uint8_t *data, uint16_t len) -{ - struct bt_conn *conn; - uint8_t status; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - if (bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN)) { - status = BTP_STATUS_FAILED; - } else { - status = BTP_STATUS_SUCCESS; - } - - bt_conn_unref(conn); - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_DISCONNECT, CONTROLLER_INDEX, - status); -} - -static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) -{ - struct gap_passkey_display_ev ev; - const bt_addr_le_t *addr = bt_conn_get_dst(conn); - - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; - ev.passkey = sys_cpu_to_le32(passkey); - - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_PASSKEY_DISPLAY, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); -} - -static void auth_passkey_entry(struct bt_conn *conn) -{ - struct gap_passkey_entry_req_ev ev; - const bt_addr_le_t *addr = bt_conn_get_dst(conn); - - memcpy(ev.address, addr->val, sizeof(ev.address)); - ev.address_type = addr->type; - - tester_send(BTP_SERVICE_ID_GAP, GAP_EV_PASSKEY_ENTRY_REQ, - CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev)); -} - -static void auth_cancel(struct bt_conn *conn) -{ - /* TODO */ -} - -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; - - /* Reset io cap requirements */ - memset(&cb, 0, sizeof(cb)); - bt_conn_auth_cb_register(NULL); - - switch (cmd->io_cap) { - case GAP_IO_CAP_DISPLAY_ONLY: - cb.cancel = auth_cancel; - cb.passkey_display = auth_passkey_display; - break; - case GAP_IO_CAP_KEYBOARD_DISPLAY: - cb.cancel = auth_cancel; - cb.passkey_display = auth_passkey_display; - cb.passkey_entry = auth_passkey_entry; - break; - case GAP_IO_CAP_NO_INPUT_OUTPUT: - cb.cancel = auth_cancel; - break; - case GAP_IO_CAP_KEYBOARD_ONLY: - cb.cancel = auth_cancel; - cb.passkey_entry = auth_passkey_entry; - break; - case GAP_IO_CAP_DISPLAY_YESNO: - default: - status = BTP_STATUS_FAILED; - goto rsp; - } - - if (bt_conn_auth_cb_register(&cb)) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_SET_IO_CAP, CONTROLLER_INDEX, - status); -} - -static void pair(const uint8_t *data, uint16_t len) -{ - struct bt_conn *conn; - uint8_t status; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - if (bt_conn_security(conn, BT_SECURITY_MEDIUM)) { - status = BTP_STATUS_FAILED; - bt_conn_unref(conn); - goto rsp; - } - - bt_conn_unref(conn); - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_PAIR, CONTROLLER_INDEX, status); -} - -static void passkey_entry(const uint8_t *data, uint16_t len) -{ - const struct gap_passkey_entry_cmd *cmd = (void *) data; - struct bt_conn *conn; - uint8_t status; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - bt_conn_auth_passkey_entry(conn, sys_le32_to_cpu(cmd->passkey)); - - bt_conn_unref(conn); - status = BTP_STATUS_SUCCESS; - -rsp: - tester_rsp(BTP_SERVICE_ID_GAP, GAP_PASSKEY_ENTRY, CONTROLLER_INDEX, - status); -} - -void 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: - 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; - } - - switch (opcode) { - case GAP_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case GAP_READ_CONTROLLER_INDEX_LIST: - controller_index_list(data, len); - return; - case GAP_READ_CONTROLLER_INFO: - controller_info(data, len); - return; - case GAP_SET_CONNECTABLE: - set_connectable(data, len); - return; - case GAP_SET_DISCOVERABLE: - set_discoverable(data, len); - return; - case GAP_START_ADVERTISING: - start_advertising(data, len); - return; - case GAP_STOP_ADVERTISING: - stop_advertising(data, len); - return; - case GAP_START_DISCOVERY: - start_discovery(data, len); - return; - case GAP_STOP_DISCOVERY: - stop_discovery(data, len); - return; - case GAP_CONNECT: - connect(data, len); - return; - case GAP_DISCONNECT: - disconnect(data, len); - return; - case GAP_SET_IO_CAP: - set_io_cap(data, len); - return; - case GAP_PAIR: - pair(data, len); - return; - case GAP_PASSKEY_ENTRY: - passkey_entry(data, len); - return; - default: - tester_rsp(BTP_SERVICE_ID_GAP, opcode, index, - BTP_STATUS_UNKNOWN_CMD); - return; - } -} - -uint8_t tester_init_gap(void) -{ - if (bt_enable(NULL) < 0) { - return BTP_STATUS_FAILED; - } - - atomic_clear(¤t_settings); - atomic_set_bit(¤t_settings, GAP_SETTINGS_POWERED); - atomic_set_bit(¤t_settings, GAP_SETTINGS_CONNECTABLE); - atomic_set_bit(¤t_settings, GAP_SETTINGS_BONDABLE); - atomic_set_bit(¤t_settings, GAP_SETTINGS_LE); - - bt_conn_cb_register(&conn_callbacks); - - return BTP_STATUS_SUCCESS; -} diff --git a/samples/bluetooth/tester/src/gatt.c b/samples/bluetooth/tester/src/gatt.c deleted file mode 100644 index 35f743403..000000000 --- a/samples/bluetooth/tester/src/gatt.c +++ /dev/null @@ -1,1711 +0,0 @@ -/* gatt.c - Bluetooth GATT Server Tester */ - -/* - * Copyright (c) 2015 Intel Corporation - * - * Licensed 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 -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "bttester.h" - -#define CONTROLLER_INDEX 0 -#define MAX_ATTRIBUTES 50 -#define MAX_BUFFER_SIZE 2048 - -#define GATT_PERM_ENC_READ_MASK (BT_GATT_PERM_READ_ENCRYPT | \ - BT_GATT_PERM_READ_AUTHEN) -#define GATT_PERM_ENC_WRITE_MASK (BT_GATT_PERM_WRITE_ENCRYPT | \ - BT_GATT_PERM_WRITE_AUTHEN) - -static struct bt_gatt_attr gatt_db[MAX_ATTRIBUTES]; - -/* - * gatt_buf - cache used by a gatt client (to cache data read/discovered) - * and gatt server (to store attribute user_data). - * It is not intended to be used by client and server at the same time. - */ -static struct { - uint16_t len; - uint8_t buf[MAX_BUFFER_SIZE]; -} gatt_buf; - -static void *gatt_buf_reserve(size_t len) -{ - void *ptr; - - if ((len + gatt_buf.len) > ARRAY_SIZE(gatt_buf.buf)) { - return NULL; - } - - ptr = memset(gatt_buf.buf + gatt_buf.len, 0, len); - gatt_buf.len += len; - - BTTESTER_DBG("%d/%d used", gatt_buf.len, MAX_BUFFER_SIZE); - - return ptr; -} - -static void *gatt_buf_add(const void *data, size_t len) -{ - void *ptr; - - if ((len + gatt_buf.len) > ARRAY_SIZE(gatt_buf.buf)) { - return NULL; - } - - ptr = memcpy(gatt_buf.buf + gatt_buf.len, data, len); - gatt_buf.len += len; - - BTTESTER_DBG("%d/%d used", gatt_buf.len, MAX_BUFFER_SIZE); - - return ptr; -} - -static void gatt_buf_clear(void) -{ - memset(&gatt_buf, 0, sizeof(gatt_buf)); -} - -static struct bt_gatt_attr *gatt_db_add(const struct bt_gatt_attr *pattern) -{ - static struct bt_gatt_attr *attr = gatt_db; - - /* Return NULL if gatt_db is full */ - if (attr == &gatt_db[ARRAY_SIZE(gatt_db)]) { - return NULL; - } - - memcpy(attr, pattern, sizeof(*attr)); - - /* Register attribute in GATT database, this will assign it a handle */ - if (bt_gatt_register(attr, 1)) { - return NULL; - } - - BTTESTER_DBG("handle 0x%04x", attr->handle); - - return attr++; -} - -/* Convert UUID from BTP command to bt_uuid */ -static uint8_t btp2bt_uuid(const uint8_t *uuid, uint8_t len, - struct bt_uuid *bt_uuid) -{ - uint16_t le16; - - switch (len) { - case 0x02: /* UUID 16 */ - bt_uuid->type = BT_UUID_TYPE_16; - memcpy(&le16, uuid, sizeof(le16)); - BT_UUID_16(bt_uuid)->val = sys_le16_to_cpu(le16); - break; - case 0x10: /* UUID 128*/ - bt_uuid->type = BT_UUID_TYPE_128; - memcpy(BT_UUID_128(bt_uuid)->val, uuid, 16); - break; - default: - return BTP_STATUS_FAILED; - } - - return BTP_STATUS_SUCCESS; -} - -static void supported_commands(uint8_t *data, uint16_t len) -{ - uint64_t cmds[2]; - struct gatt_read_supported_commands_rp *rp = (void *) cmds; - - cmds[0] = 1 << GATT_READ_SUPPORTED_COMMANDS; - cmds[0] |= 1 << GATT_ADD_SERVICE; - cmds[0] |= 1 << GATT_ADD_CHARACTERISTIC; - cmds[0] |= 1 << GATT_ADD_DESCRIPTOR; - cmds[0] |= 1 << GATT_ADD_INCLUDED_SERVICE; - cmds[0] |= 1 << GATT_SET_VALUE; - cmds[0] |= 1 << GATT_START_SERVER; - cmds[0] |= 1 << GATT_SET_ENC_KEY_SIZE; - cmds[1] = 1 << (GATT_EXCHANGE_MTU - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_DISC_PRIM_UUID - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_FIND_INCLUDED - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_DISC_ALL_CHRC - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_DISC_CHRC_UUID - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_DISC_ALL_DESC - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_READ - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_READ_LONG - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_READ_MULTIPLE - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_WRITE_WITHOUT_RSP - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_SIGNED_WRITE_WITHOUT_RSP - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_WRITE - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_WRITE_LONG - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_CFG_NOTIFY - GATT_CLIENT_OP_OFFSET); - cmds[1] |= 1 << (GATT_CFG_INDICATE - GATT_CLIENT_OP_OFFSET); - - tester_send(BTP_SERVICE_ID_GATT, GATT_READ_SUPPORTED_COMMANDS, - CONTROLLER_INDEX, (uint8_t *) rp, sizeof(cmds)); -} - -static struct bt_gatt_attr svc_pri = BT_GATT_PRIMARY_SERVICE(NULL); -static struct bt_gatt_attr svc_sec = BT_GATT_SECONDARY_SERVICE(NULL); - -union uuid { - struct bt_uuid uuid; - struct bt_uuid_16 u16; - struct bt_uuid_128 u128; -}; - -static void add_service(uint8_t *data, uint16_t len) -{ - const struct gatt_add_service_cmd *cmd = (void *) data; - struct gatt_add_service_rp rp; - struct bt_gatt_attr *attr_svc; - union uuid uuid; - - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid.uuid)) { - goto fail; - } - - switch (cmd->type) { - case GATT_SERVICE_PRIMARY: - attr_svc = gatt_db_add(&svc_pri); - break; - case GATT_SERVICE_SECONDARY: - attr_svc = gatt_db_add(&svc_sec); - break; - default: - goto fail; - } - - if (!attr_svc) { - goto fail; - } - - attr_svc->user_data = gatt_buf_add(&uuid, sizeof(uuid)); - if (!attr_svc->user_data) { - goto fail; - } - - rp.svc_id = sys_cpu_to_le16(attr_svc->handle); - - tester_send(BTP_SERVICE_ID_GATT, GATT_ADD_SERVICE, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); - - return; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_ADD_SERVICE, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -struct gatt_value { - uint16_t len; - uint8_t *data; - uint8_t *prep_data; - uint8_t enc_key_size; - bool has_ccc; -}; - -static int read_value(struct bt_conn *conn, const struct bt_gatt_attr *attr, - void *buf, uint16_t len, uint16_t offset) -{ - const struct gatt_value *value = attr->user_data; - - if ((attr->perm & GATT_PERM_ENC_READ_MASK) && - (value->enc_key_size > bt_conn_enc_key_size(conn))) { - return -EACCES; - } - - return bt_gatt_attr_read(conn, attr, buf, len, offset, value->data, - value->len); -} - -static int write_value(struct bt_conn *conn, const struct bt_gatt_attr *attr, - const void *buf, uint16_t len, uint16_t offset) -{ - struct gatt_value *value = attr->user_data; - - if ((attr->perm & GATT_PERM_ENC_WRITE_MASK) && - (value->enc_key_size > bt_conn_enc_key_size(conn))) { - return -EACCES; - } - - /* - * If the prepare Value Offset is greater than the current length of - * the attribute value Error Response shall be sent with the - * «Invalid Offset». - */ - if (offset > value->len) { - return -EINVAL; - } - - if (offset + len > value->len) { - return -EFBIG; - } - - memcpy(value->prep_data + offset, buf, len); - - return len; -} - -static int flush_value(struct bt_conn *conn, - const struct bt_gatt_attr *attr, uint8_t flags) -{ - struct gatt_value *value = attr->user_data; - - switch (flags) { - case BT_GATT_FLUSH_SYNC: - /* Sync buffer to data */ - memcpy(value->data, value->prep_data, value->len); - /* Fallthrough */ - case BT_GATT_FLUSH_DISCARD: - memset(value->prep_data, 0, value->len); - return 0; - } - - return -EINVAL; -} - -static struct bt_gatt_attr chr = BT_GATT_CHARACTERISTIC(NULL, 0); -static struct bt_gatt_attr chr_val = BT_GATT_LONG_DESCRIPTOR(NULL, 0, - read_value, - write_value, - flush_value, NULL); - -static uint8_t add_characteristic_cb(const struct bt_gatt_attr *attr, - void *user_data) -{ - const struct gatt_add_characteristic_cmd *cmd = user_data; - struct gatt_add_characteristic_rp rp; - struct bt_gatt_attr *attr_chrc, *attr_value; - struct bt_gatt_chrc chrc; - union uuid uuid; - - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid.uuid)) { - goto fail; - } - - attr_chrc = gatt_db_add(&chr); - if (!attr_chrc) { - goto fail; - } - - attr_value = gatt_db_add(&chr_val); - if (!attr_value) { - goto fail; - } - - chrc.properties = cmd->properties; - chrc.uuid = gatt_buf_add(&uuid, sizeof(uuid)); - if (!chrc.uuid) { - goto fail; - } - - attr_chrc->user_data = gatt_buf_add(&chrc, sizeof(chrc)); - if (!attr_chrc->user_data) { - goto fail; - } - - attr_value->uuid = chrc.uuid; - attr_value->perm = cmd->permissions; - - rp.char_id = sys_cpu_to_le16(attr_chrc->handle); - tester_send(BTP_SERVICE_ID_GATT, GATT_ADD_CHARACTERISTIC, - CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); - - return BT_GATT_ITER_STOP; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_ADD_CHARACTERISTIC, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - - return BT_GATT_ITER_STOP; -} - -static void add_characteristic(uint8_t *data, uint16_t len) -{ - const struct gatt_add_characteristic_cmd *cmd = (void *) data; - uint16_t handle = sys_le16_to_cpu(cmd->svc_id); - - /* TODO Return error if no attribute found */ - bt_gatt_foreach_attr(handle, handle, add_characteristic_cb, data); -} - -static bool ccc_added; - -static struct bt_gatt_ccc_cfg ccc_cfg[CONFIG_BLUETOOTH_MAX_PAIRED] = {}; - -static void ccc_cfg_changed(uint16_t value) -{ - /* NOP */ -} - -static struct bt_gatt_attr ccc = BT_GATT_CCC(ccc_cfg, ccc_cfg_changed); - -static struct bt_gatt_attr *add_ccc(const struct bt_gatt_attr *attr_chrc) -{ - struct bt_gatt_attr *attr_desc, *attr_value; - struct bt_gatt_chrc *chrc = attr_chrc->user_data; - struct gatt_value *value; - - /* Fail if another CCC already exist on server */ - if (ccc_added) { - return NULL; - } - - /* Check characteristic properties */ - if (!(chrc->properties & - (BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE))) { - return NULL; - } - - /* - * Look for characteristic value (stored under next handle) to set - * 'has_ccc' flag - */ - attr_value = bt_gatt_attr_next(attr_chrc); - if (!attr_value) { - return NULL; - } - - value = attr_value->user_data; - if (!value) { - return NULL; - } - - /* Add CCC descriptor to GATT database */ - attr_desc = gatt_db_add(&ccc); - if (!attr_desc) { - return NULL; - } - - value->has_ccc = true; - ccc_added = true; - - return attr_desc; -} - -static struct bt_gatt_attr cep = BT_GATT_CEP(NULL); - -static struct bt_gatt_attr *add_cep(const struct bt_gatt_attr *attr_chrc) -{ - struct bt_gatt_attr *attr_desc; - struct bt_gatt_chrc *chrc = attr_chrc->user_data; - struct bt_gatt_cep cep_value; - - /* Extended Properties bit shall be set */ - if (!(chrc->properties & BT_GATT_CHRC_EXT_PROP)) { - return NULL; - } - - /* Add CEP descriptor to GATT database */ - attr_desc = gatt_db_add(&cep); - if (!attr_desc) { - return NULL; - } - - attr_desc->user_data = gatt_buf_add(&cep_value, sizeof(cep_value)); - if (!attr_desc->user_data) { - return NULL; - } - - return attr_desc; -} - -static struct bt_gatt_attr *dsc = &chr_val; - -static uint8_t add_descriptor_cb(const struct bt_gatt_attr *attr, - void *user_data) -{ - const struct gatt_add_descriptor_cmd *cmd = user_data; - struct gatt_add_descriptor_rp rp; - struct bt_gatt_attr *attr_desc; - union uuid uuid; - - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid.uuid)) { - goto fail; - } - - if (!bt_uuid_cmp(&uuid.uuid, cep.uuid)) { - attr_desc = add_cep(attr); - } else if (!bt_uuid_cmp(&uuid.uuid, ccc.uuid)) { - attr_desc = add_ccc(attr); - } else { - attr_desc = gatt_db_add(dsc); - } - - if (!attr_desc) { - goto fail; - } - - /* CCC and CEP have permissions already set */ - if (!attr_desc->perm) { - attr_desc->perm = cmd->permissions; - } - - /* CCC and CEP have UUID already set */ - if (!attr_desc->uuid) { - attr_desc->uuid = gatt_buf_add(&uuid, sizeof(uuid)); - if (!attr_desc->uuid) { - goto fail; - } - } - - rp.desc_id = sys_cpu_to_le16(attr_desc->handle); - - tester_send(BTP_SERVICE_ID_GATT, GATT_ADD_DESCRIPTOR, CONTROLLER_INDEX, - (uint8_t *) &rp, sizeof(rp)); - - return BT_GATT_ITER_STOP; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_ADD_DESCRIPTOR, CONTROLLER_INDEX, - BTP_STATUS_FAILED); - - return BT_GATT_ITER_STOP; -} - -static void add_descriptor(uint8_t *data, uint16_t len) -{ - const struct gatt_add_descriptor_cmd *cmd = (void *) data; - uint16_t handle = sys_le16_to_cpu(cmd->char_id); - - /* TODO Return error if no attribute found */ - bt_gatt_foreach_attr(handle, handle, add_descriptor_cb, data); -} - -static uint8_t get_service_handles(const struct bt_gatt_attr *attr, - void *user_data) -{ - struct bt_gatt_include *include = user_data; - - /* - * The first attribute found is service declaration. - * Preset end handle - next attribute can be a service. - */ - if (!include->start_handle) { - include->start_handle = attr->handle; - include->end_handle = attr->handle; - - return BT_GATT_ITER_CONTINUE; - } - - /* Stop if attribute is a service */ - if (!bt_uuid_cmp(attr->uuid, svc_pri.uuid) || - !bt_uuid_cmp(attr->uuid, svc_sec.uuid)) { - return BT_GATT_ITER_STOP; - } - - include->end_handle = attr->handle; - - return BT_GATT_ITER_CONTINUE; -} - -static struct bt_gatt_attr svc_inc = BT_GATT_INCLUDE_SERVICE(NULL); - -static uint8_t add_included_cb(const struct bt_gatt_attr *attr, void *user_data) -{ - struct gatt_add_included_service_rp rp; - struct bt_gatt_attr *attr_incl; - struct bt_gatt_include include; - - /* Fail if attribute stored under requested handle is not a service */ - if (bt_uuid_cmp(attr->uuid, svc_pri.uuid) && - bt_uuid_cmp(attr->uuid, svc_sec.uuid)) { - goto fail; - } - - attr_incl = gatt_db_add(&svc_inc); - if (!attr_incl) { - goto fail; - } - - include.uuid = attr->user_data; - include.start_handle = 0; - - attr_incl->user_data = gatt_buf_add(&include, sizeof(include)); - if (!attr_incl->user_data) { - goto fail; - } - - /* Lookup for service end handle */ - bt_gatt_foreach_attr(attr->handle, 0xffff, get_service_handles, - attr_incl->user_data); - - rp.included_service_id = sys_cpu_to_le16(attr_incl->handle); - - tester_send(BTP_SERVICE_ID_GATT, GATT_ADD_CHARACTERISTIC, - CONTROLLER_INDEX, (uint8_t *) &rp, sizeof(rp)); - - return BT_GATT_ITER_STOP; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_ADD_CHARACTERISTIC, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - - return BT_GATT_ITER_STOP; -} - -static void add_included(uint8_t *data, uint16_t len) -{ - const struct gatt_add_included_service_cmd *cmd = (void *) data; - uint16_t handle = sys_le16_to_cpu(cmd->svc_id); - - /* TODO Return error if no attribute found */ - bt_gatt_foreach_attr(handle, handle, add_included_cb, data); -} - -static uint8_t set_ccc_value(struct bt_gatt_attr *attr, const void *value, - const uint16_t len) -{ - uint16_t ccc_val; - - if (len != sizeof(ccc_val)) { - return BTP_STATUS_FAILED; - } - - memcpy(&ccc_val, value, sizeof(ccc_val)); - - /* - * CCC Data has been already set, so we can only verify if the - * requested data is correct - */ - if (sys_le16_to_cpu(ccc_val) != 0) { - return BTP_STATUS_FAILED; - } - - return BTP_STATUS_SUCCESS; -} - -static uint8_t set_cep_value(struct bt_gatt_attr *attr, const void *value, - const uint16_t len) -{ - struct bt_gatt_cep *cep_value = attr->user_data; - - if (len != sizeof(cep_value->properties)) { - return BTP_STATUS_FAILED; - } - - memcpy(&cep_value->properties, value, len); - - return BTP_STATUS_SUCCESS; -} - -static uint8_t set_value_cb(struct bt_gatt_attr *attr, void *user_data) -{ - const struct gatt_set_value_cmd *cmd = user_data; - struct gatt_value value; - uint8_t status; - - /* Handle CCC value */ - if (!bt_uuid_cmp(attr->uuid, ccc.uuid)) { - status = set_ccc_value(attr, cmd->value, - sys_le16_to_cpu(cmd->len)); - goto rsp; - } - - /* Set CEP value */ - if (!bt_uuid_cmp(attr->uuid, cep.uuid)) { - status = set_cep_value(attr, cmd->value, - sys_le16_to_cpu(cmd->len)); - goto rsp; - } - - if (!bt_uuid_cmp(attr->uuid, chr.uuid)) { - attr = bt_gatt_attr_next(attr); - if (!attr) { - status = BTP_STATUS_FAILED; - goto rsp; - } - } - - value.len = sys_le16_to_cpu(cmd->len); - - /* Check if attribute value has been already set */ - if (attr->user_data) { - struct gatt_value *gatt_value = attr->user_data; - - /* Fail if value length doesn't match */ - if (value.len != gatt_value->len) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - memcpy(gatt_value->data, cmd->value, gatt_value->len); - - if (gatt_value->has_ccc) { - bt_gatt_notify(NULL, attr, gatt_value->data, - gatt_value->len); - } - - status = BTP_STATUS_SUCCESS; - goto rsp; - } - - value.data = gatt_buf_add(cmd->value, value.len); - if (!value.data) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - value.prep_data = gatt_buf_reserve(value.len); - if (!value.prep_data) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - value.has_ccc = false; - value.enc_key_size = 0x00; - - attr->user_data = gatt_buf_add(&value, sizeof(value)); - if (!attr->user_data) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - status = BTP_STATUS_SUCCESS; -rsp: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_SET_VALUE, CONTROLLER_INDEX, - status); - - return BT_GATT_ITER_STOP; -} - -static void set_value(uint8_t *data, uint16_t len) -{ - const struct gatt_set_value_cmd *cmd = (void *) data; - uint16_t handle = sys_le16_to_cpu(cmd->attr_id); - - /* TODO Return error if no attribute found */ - bt_gatt_foreach_attr(handle, handle, (bt_gatt_attr_func_t) set_value_cb, - data); -} - -static void start_server(uint8_t *data, uint16_t len) -{ - tester_rsp(BTP_SERVICE_ID_GATT, GATT_START_SERVER, - CONTROLLER_INDEX, BTP_STATUS_SUCCESS); -} - -static uint8_t set_enc_key_size_cb(const struct bt_gatt_attr *attr, - void *user_data) -{ - const struct gatt_set_enc_key_size_cmd *cmd = user_data; - struct gatt_value *value; - uint8_t status; - - /* Fail if requested key size is invalid */ - if (cmd->key_size < 0x07 || cmd->key_size > 0x0f) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - /* Fail if requested attribute is a service */ - if (!bt_uuid_cmp(attr->uuid, svc_pri.uuid) || - !bt_uuid_cmp(attr->uuid, svc_sec.uuid) || - !bt_uuid_cmp(attr->uuid, svc_inc.uuid)) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - /* Lookup for characteristic value attribute */ - if (!bt_uuid_cmp(attr->uuid, chr.uuid)) { - attr = bt_gatt_attr_next(attr); - if (!attr) { - status = BTP_STATUS_FAILED; - goto rsp; - } - } - - /* Fail if permissions are not set */ - if (!(attr->perm & (GATT_PERM_ENC_READ_MASK | - GATT_PERM_ENC_WRITE_MASK))) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - /* Fail if there is no attribute value */ - if (!attr->user_data) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - value = attr->user_data; - value->enc_key_size = cmd->key_size; - - status = BTP_STATUS_SUCCESS; -rsp: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_SET_ENC_KEY_SIZE, CONTROLLER_INDEX, - status); - - return BT_GATT_ITER_STOP; -} - -static void set_enc_key_size(uint8_t *data, uint16_t len) -{ - const struct gatt_set_enc_key_size_cmd *cmd = (void *) data; - uint16_t handle = sys_le16_to_cpu(cmd->attr_id); - - /* TODO Return error if no attribute found */ - bt_gatt_foreach_attr(handle, handle, set_enc_key_size_cb, data); -} - -static void exchange_mtu_rsp(struct bt_conn *conn, uint8_t err) -{ - if (err) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - - return; - } - - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, CONTROLLER_INDEX, - BTP_STATUS_SUCCESS); -} - -static void exchange_mtu(uint8_t *data, uint16_t len) -{ - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail; - } - - if (bt_gatt_exchange_mtu(conn, exchange_mtu_rsp) < 0) { - bt_conn_unref(conn); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_EXCHANGE_MTU, - CONTROLLER_INDEX, BTP_STATUS_FAILED); -} - -static struct bt_gatt_discover_params discover_params; -static union uuid uuid; -static uint8_t btp_opcode; - -static void discover_destroy(struct bt_gatt_discover_params *params) -{ - memset(params, 0, sizeof(*params)); - gatt_buf_clear(); -} - -static uint8_t disc_prim_uuid_cb(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - struct bt_gatt_service *data; - struct gatt_disc_prim_uuid_rp *rp = (void *) gatt_buf.buf; - struct gatt_service *service; - uint8_t uuid_length; - - if (!attr) { - tester_send(BTP_SERVICE_ID_GATT, GATT_DISC_PRIM_UUID, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - data = attr->user_data; - - uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; - - service = gatt_buf_reserve(sizeof(*service) + uuid_length); - if (!service) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_PRIM_UUID, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - service->start_handle = sys_cpu_to_le16(attr->handle); - service->end_handle = sys_cpu_to_le16(data->end_handle); - service->uuid_length = uuid_length; - - if (data->uuid->type == BT_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); - - memcpy(service->uuid, &u16, uuid_length); - } else { - memcpy(service->uuid, BT_UUID_128(data->uuid)->val, - uuid_length); - } - - rp->services_count++; - - return BT_GATT_ITER_CONTINUE; -} - -static void disc_prim_uuid(uint8_t *data, uint16_t len) -{ - const struct gatt_disc_prim_uuid_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid.uuid)) { - goto fail; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_disc_prim_uuid_rp))) { - goto fail; - } - - discover_params.uuid = &uuid.uuid; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; - discover_params.func = disc_prim_uuid_cb; - - if (bt_gatt_discover(conn, &discover_params) < 0) { - discover_destroy(&discover_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_PRIM_UUID, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static uint8_t find_included_cb(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - struct bt_gatt_include *data; - struct gatt_find_included_rp *rp = (void *) gatt_buf.buf; - struct gatt_included *included; - uint8_t uuid_length; - - if (!attr) { - tester_send(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - data = attr->user_data; - - uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; - - included = gatt_buf_reserve(sizeof(*included) + uuid_length); - if (!included) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - included->included_handle = attr->handle; - included->service.start_handle = sys_cpu_to_le16(data->start_handle); - included->service.end_handle = sys_cpu_to_le16(data->end_handle); - included->service.uuid_length = uuid_length; - - if (data->uuid->type == BT_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); - - memcpy(included->service.uuid, &u16, uuid_length); - } else { - /* TODO Read this 128bit UUID */ - memset(included->service.uuid, 0, uuid_length); - } - - rp->services_count++; - - return BT_GATT_ITER_CONTINUE; -} - -static void find_included(uint8_t *data, uint16_t len) -{ - const struct gatt_find_included_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_find_included_rp))) { - goto fail; - } - - discover_params.start_handle = sys_le16_to_cpu(cmd->start_handle); - discover_params.end_handle = sys_le16_to_cpu(cmd->end_handle); - discover_params.type = BT_GATT_DISCOVER_INCLUDE; - discover_params.func = find_included_cb; - - if (bt_gatt_discover(conn, &discover_params) < 0) { - discover_destroy(&discover_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_FIND_INCLUDED, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static uint8_t disc_chrc_cb(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - struct bt_gatt_chrc *data; - struct gatt_disc_chrc_rp *rp = (void *) gatt_buf.buf; - struct gatt_characteristic *chrc; - uint8_t uuid_length; - - if (!attr) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - data = attr->user_data; - - uuid_length = data->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; - - chrc = gatt_buf_reserve(sizeof(*chrc) + uuid_length); - if (!chrc) { - tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - chrc->characteristic_handle = sys_cpu_to_le16(attr->handle); - chrc->properties = data->properties; - chrc->value_handle = sys_cpu_to_le16(attr->handle + 1); - chrc->uuid_length = uuid_length; - - if (data->uuid->type == BT_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(data->uuid)->val); - - memcpy(chrc->uuid, &u16, uuid_length); - } else { - memcpy(chrc->uuid, BT_UUID_128(data->uuid)->val, uuid_length); - } - - rp->characteristics_count++; - - return BT_GATT_ITER_CONTINUE; -} - -static void disc_all_chrc(uint8_t *data, uint16_t len) -{ - const struct gatt_disc_all_chrc_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_disc_chrc_rp))) { - goto fail; - } - - discover_params.start_handle = sys_le16_to_cpu(cmd->start_handle); - discover_params.end_handle = sys_le16_to_cpu(cmd->end_handle); - discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; - discover_params.func = disc_chrc_cb; - - /* TODO should be handled as user_data via CONTAINER_OF macro */ - btp_opcode = GATT_DISC_ALL_CHRC; - - if (bt_gatt_discover(conn, &discover_params) < 0) { - discover_destroy(&discover_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_CHRC, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static void disc_chrc_uuid(uint8_t *data, uint16_t len) -{ - const struct gatt_disc_chrc_uuid_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (btp2bt_uuid(cmd->uuid, cmd->uuid_length, &uuid.uuid)) { - goto fail; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_disc_chrc_rp))) { - goto fail; - } - - discover_params.uuid = &uuid.uuid; - discover_params.start_handle = sys_le16_to_cpu(cmd->start_handle); - discover_params.end_handle = sys_le16_to_cpu(cmd->end_handle); - discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; - discover_params.func = disc_chrc_cb; - - /* TODO should be handled as user_data via CONTAINER_OF macro */ - btp_opcode = GATT_DISC_CHRC_UUID; - - if (bt_gatt_discover(conn, &discover_params) < 0) { - discover_destroy(&discover_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_CHRC_UUID, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static uint8_t disc_all_desc_cb(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - struct gatt_disc_all_desc_rp *rp = (void *) gatt_buf.buf; - struct gatt_descriptor *descriptor; - uint8_t uuid_length; - - if (!attr) { - tester_send(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, - CONTROLLER_INDEX, gatt_buf.buf, gatt_buf.len); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - uuid_length = attr->uuid->type == BT_UUID_TYPE_16 ? 2 : 16; - - descriptor = gatt_buf_reserve(sizeof(*descriptor) + uuid_length); - if (!descriptor) { - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - discover_destroy(params); - return BT_GATT_ITER_STOP; - } - - descriptor->descriptor_handle = sys_cpu_to_le16(attr->handle); - descriptor->uuid_length = uuid_length; - - if (attr->uuid->type == BT_UUID_TYPE_16) { - uint16_t u16 = sys_cpu_to_le16(BT_UUID_16(attr->uuid)->val); - - memcpy(descriptor->uuid, &u16, uuid_length); - } else { - memcpy(descriptor->uuid, BT_UUID_128(attr->uuid)->val, - uuid_length); - } - - rp->descriptors_count++; - - return BT_GATT_ITER_CONTINUE; -} - -static void disc_all_desc(uint8_t *data, uint16_t len) -{ - const struct gatt_disc_all_desc_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_disc_all_desc_rp))) { - goto fail; - } - - discover_params.start_handle = sys_le16_to_cpu(cmd->start_handle); - discover_params.end_handle = sys_le16_to_cpu(cmd->end_handle); - discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; - discover_params.func = disc_all_desc_cb; - - if (bt_gatt_discover(conn, &discover_params) < 0) { - discover_destroy(&discover_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_DISC_ALL_DESC, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static struct bt_gatt_read_params read_params; - -static void read_destroy(struct bt_gatt_read_params *params) -{ - memset(params, 0, sizeof(*params)); - gatt_buf_clear(); -} - -static uint8_t read_cb(struct bt_conn *conn, int err, - struct bt_gatt_read_params *params, const void *data, - uint16_t length) -{ - struct gatt_read_rp *rp = (void *) gatt_buf.buf; - - /* Respond to the Lower Tester with ATT Error received */ - if (err) { - rp->att_response = err; - } - - /* read complete */ - if (!data) { - tester_send(BTP_SERVICE_ID_GATT, btp_opcode, CONTROLLER_INDEX, - gatt_buf.buf, gatt_buf.len); - read_destroy(params); - return BT_GATT_ITER_STOP; - } - - if (!gatt_buf_add(data, length)) { - tester_rsp(BTP_SERVICE_ID_GATT, btp_opcode, - CONTROLLER_INDEX, BTP_STATUS_FAILED); - read_destroy(params); - return BT_GATT_ITER_STOP; - } - - rp->data_length += length; - - return BT_GATT_ITER_CONTINUE; -} - -static void read(uint8_t *data, uint16_t len) -{ - const struct gatt_read_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { - goto fail; - } - - read_params.handle_count = 1; - read_params.single.handle = sys_le16_to_cpu(cmd->handle); - read_params.single.offset = 0x0000; - read_params.func = read_cb; - - /* TODO should be handled as user_data via CONTAINER_OF macro */ - btp_opcode = GATT_READ; - - if (bt_gatt_read(conn, &read_params) < 0) { - read_destroy(&read_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static void read_long(uint8_t *data, uint16_t len) -{ - const struct gatt_read_long_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { - goto fail; - } - - read_params.handle_count = 1; - read_params.single.handle = sys_le16_to_cpu(cmd->handle); - read_params.single.offset = sys_le16_to_cpu(cmd->offset); - read_params.func = read_cb; - - /* TODO should be handled as user_data via CONTAINER_OF macro */ - btp_opcode = GATT_READ_LONG; - - if (bt_gatt_read(conn, &read_params) < 0) { - read_destroy(&read_params); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, 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]; - struct bt_conn *conn; - int i; - - for (i = 0; i < ARRAY_SIZE(handles); i++) { - handles[i] = sys_le16_to_cpu(cmd->handles[i]); - } - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail_conn; - } - - if (!gatt_buf_reserve(sizeof(struct gatt_read_rp))) { - goto fail; - } - - read_params.func = read_cb; - read_params.handle_count = i; - read_params.handles = handles; /* not used in read func */ - - /* TODO should be handled as user_data via CONTAINER_OF macro */ - btp_opcode = GATT_READ_MULTIPLE; - - if (bt_gatt_read(conn, &read_params) < 0) { - gatt_buf_clear(); - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - bt_conn_unref(conn); - -fail_conn: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_READ_MULTIPLE, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static void write_without_rsp(uint8_t *data, uint16_t len) -{ - const struct gatt_write_without_rsp_cmd *cmd = (void *) data; - struct bt_conn *conn; - uint8_t status = BTP_STATUS_SUCCESS; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - if (bt_gatt_write_without_response(conn, sys_le16_to_cpu(cmd->handle), - cmd->data, - sys_le16_to_cpu(cmd->data_length), - false) < 0) { - status = BTP_STATUS_FAILED; - } - - bt_conn_unref(conn); -rsp: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE_WITHOUT_RSP, - CONTROLLER_INDEX, status); -} - -static void signed_write_without_rsp(uint8_t *data, uint16_t len) -{ - const struct gatt_write_without_rsp_cmd *cmd = (void *) data; - struct bt_conn *conn; - uint8_t status = BTP_STATUS_SUCCESS; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - status = BTP_STATUS_FAILED; - goto rsp; - } - - if (bt_gatt_write_without_response(conn, sys_le16_to_cpu(cmd->handle), - cmd->data, - sys_le16_to_cpu(cmd->data_length), - true) < 0) { - status = BTP_STATUS_FAILED; - } - - bt_conn_unref(conn); -rsp: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_SIGNED_WRITE_WITHOUT_RSP, - CONTROLLER_INDEX, status); -} - -static void write_rsp(struct bt_conn *conn, uint8_t err) -{ - tester_send(BTP_SERVICE_ID_GATT, GATT_WRITE, CONTROLLER_INDEX, &err, - sizeof(err)); -} - -static void write(uint8_t *data, uint16_t len) -{ - const struct gatt_write_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail; - } - - if (bt_gatt_write(conn, sys_le16_to_cpu(cmd->handle), 0, cmd->data, - sys_le16_to_cpu(cmd->data_length), write_rsp) < 0) { - bt_conn_unref(conn); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static void write_long_rsp(struct bt_conn *conn, uint8_t err) -{ - tester_send(BTP_SERVICE_ID_GATT, GATT_WRITE_LONG, CONTROLLER_INDEX, - &err, sizeof(err)); -} - -static void write_long(uint8_t *data, uint16_t len) -{ - const struct gatt_write_long_cmd *cmd = (void *) data; - struct bt_conn *conn; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - goto fail; - } - - if (bt_gatt_write(conn, sys_le16_to_cpu(cmd->handle), - sys_le16_to_cpu(cmd->offset), cmd->data, - sys_le16_to_cpu(cmd->data_length), - write_long_rsp) < 0) { - bt_conn_unref(conn); - - goto fail; - } - - bt_conn_unref(conn); - - return; -fail: - tester_rsp(BTP_SERVICE_ID_GATT, GATT_WRITE_LONG, CONTROLLER_INDEX, - BTP_STATUS_FAILED); -} - -static struct bt_gatt_subscribe_params subscribe_params; - -/* ev header + default MTU_ATT-3 */ -static uint8_t ev_buf[33]; - -static uint8_t notify_func(struct bt_conn *conn, - struct bt_gatt_subscribe_params *params, - const void *data, uint16_t length) -{ - struct gatt_notification_ev *ev = (void *) ev_buf; - const bt_addr_le_t *addr = bt_conn_get_dst(conn); - - if (!data) { - BTTESTER_DBG("Unsubscribed"); - memset(params, 0, sizeof(*params)); - return BT_GATT_ITER_STOP; - } - - ev->type = (uint8_t) subscribe_params.value; - ev->handle = sys_cpu_to_le16(subscribe_params.value_handle); - ev->data_length = sys_cpu_to_le16(length); - memcpy(ev->data, data, length); - memcpy(ev->address, addr->val, sizeof(ev->address)); - ev->address_type = addr->type; - - tester_send(BTP_SERVICE_ID_GATT, GATT_EV_NOTIFICATION, - CONTROLLER_INDEX, ev_buf, sizeof(*ev) + length); - - return BT_GATT_ITER_CONTINUE; -} - -static void discover_complete(struct bt_conn *conn, - struct bt_gatt_discover_params *params) -{ - uint8_t op, status; - - /* If no value handle it means that chrc has not been found */ - if (!subscribe_params.value_handle) { - status = BTP_STATUS_FAILED; - goto fail; - } - - if (bt_gatt_subscribe(conn, &subscribe_params) < 0) { - status = BTP_STATUS_FAILED; - goto fail; - } - - status = BTP_STATUS_SUCCESS; -fail: - op = subscribe_params.value == BT_GATT_CCC_NOTIFY ? GATT_CFG_NOTIFY : - GATT_CFG_INDICATE; - - if (status == BTP_STATUS_FAILED) { - memset(&subscribe_params, 0, sizeof(subscribe_params)); - } - - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, status); -} - -static uint8_t discover_func(struct bt_conn *conn, - const struct bt_gatt_attr *attr, - struct bt_gatt_discover_params *params) -{ - if (!attr) { - discover_complete(conn, params); - return BT_GATT_ITER_STOP; - } - - /* Characteristic Value Handle is the next handle beyond declaration */ - subscribe_params.value_handle = attr->handle + 1; - - /* - * Continue characteristic discovery to get last characteristic - * preceding this CCC descriptor - */ - return BT_GATT_ITER_CONTINUE; -} - -static int enable_subscription(struct bt_conn *conn, uint16_t ccc_handle, - uint16_t value) -{ - /* Fail if there is another subscription enabled */ - if (subscribe_params.ccc_handle) { - BTTESTER_DBG("Another subscription already enabled"); - return -EEXIST; - } - - /* Discover Characteristic Value this CCC Descriptor refers to */ - discover_params.start_handle = 0x0001; - discover_params.end_handle = ccc_handle; - discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; - discover_params.func = discover_func; - - subscribe_params.ccc_handle = ccc_handle; - subscribe_params.value = value; - subscribe_params.notify = notify_func; - - return bt_gatt_discover(conn, &discover_params); -} - -static int disable_subscription(struct bt_conn *conn, uint16_t ccc_handle) -{ - /* Fail if CCC handle doesn't match */ - if (ccc_handle != subscribe_params.ccc_handle) { - BTTESTER_DBG("CCC handle doesn't match"); - return -EINVAL; - } - - if (bt_gatt_unsubscribe(conn, &subscribe_params) < 0) { - return -EBUSY; - } - - subscribe_params.ccc_handle = 0; - - return 0; -} - -static void config_subscription(uint8_t *data, uint16_t len, uint16_t op) -{ - const struct gatt_cfg_notify_cmd *cmd = (void *) data; - struct bt_conn *conn; - uint16_t ccc_handle = sys_le16_to_cpu(cmd->ccc_handle); - uint8_t status; - - conn = bt_conn_lookup_addr_le((bt_addr_le_t *) data); - if (!conn) { - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, - BTP_STATUS_FAILED); - return; - } - - if (cmd->enable) { - uint16_t value; - - if (op == GATT_CFG_NOTIFY) { - value = BT_GATT_CCC_NOTIFY; - } else { - value = BT_GATT_CCC_INDICATE; - } - - /* on success response will be sent from callback */ - if (enable_subscription(conn, ccc_handle, value) == 0) { - bt_conn_unref(conn); - return; - } - - status = BTP_STATUS_FAILED; - } else { - if (disable_subscription(conn, ccc_handle) < 0) { - status = BTP_STATUS_FAILED; - } else { - status = BTP_STATUS_SUCCESS; - } - } - - BTTESTER_DBG("Config subscription (op %u) status %u", op, status); - - bt_conn_unref(conn); - tester_rsp(BTP_SERVICE_ID_GATT, op, CONTROLLER_INDEX, status); -} - -void tester_handle_gatt(uint8_t opcode, uint8_t index, uint8_t *data, - uint16_t len) -{ - switch (opcode) { - case GATT_READ_SUPPORTED_COMMANDS: - supported_commands(data, len); - return; - case GATT_ADD_SERVICE: - add_service(data, len); - return; - case GATT_ADD_CHARACTERISTIC: - add_characteristic(data, len); - return; - case GATT_ADD_DESCRIPTOR: - add_descriptor(data, len); - return; - case GATT_ADD_INCLUDED_SERVICE: - add_included(data, len); - return; - case GATT_SET_VALUE: - set_value(data, len); - return; - case GATT_START_SERVER: - start_server(data, len); - return; - case GATT_SET_ENC_KEY_SIZE: - set_enc_key_size(data, len); - return; - case GATT_EXCHANGE_MTU: - exchange_mtu(data, len); - return; - case GATT_DISC_PRIM_UUID: - disc_prim_uuid(data, len); - return; - case GATT_FIND_INCLUDED: - find_included(data, len); - return; - case GATT_DISC_ALL_CHRC: - disc_all_chrc(data, len); - return; - case GATT_DISC_CHRC_UUID: - disc_chrc_uuid(data, len); - return; - case GATT_DISC_ALL_DESC: - disc_all_desc(data, len); - return; - case GATT_READ: - read(data, len); - return; - case GATT_READ_LONG: - read_long(data, len); - return; - case GATT_READ_MULTIPLE: - read_multiple(data, len); - return; - case GATT_WRITE_WITHOUT_RSP: - write_without_rsp(data, len); - return; - case GATT_SIGNED_WRITE_WITHOUT_RSP: - signed_write_without_rsp(data, len); - return; - case GATT_WRITE: - write(data, len); - return; - case GATT_WRITE_LONG: - write_long(data, len); - return; - case GATT_CFG_NOTIFY: - case GATT_CFG_INDICATE: - config_subscription(data, len, opcode); - return; - default: - tester_rsp(BTP_SERVICE_ID_GATT, opcode, index, - BTP_STATUS_UNKNOWN_CMD); - return; - } -} - -uint8_t tester_init_gatt(void) -{ - ccc_added = false; - gatt_buf_clear(); - memset(&gatt_db, 0, sizeof(gatt_db)); - - return BTP_STATUS_SUCCESS; -} diff --git a/samples/bluetooth/tester/src/main.c b/samples/bluetooth/tester/src/main.c deleted file mode 100644 index 1488c642c..000000000 --- a/samples/bluetooth/tester/src/main.c +++ /dev/null @@ -1,32 +0,0 @@ -/* main.c - Application main entry point */ - -/* - * Copyright (c) 2015 Intel Corporation - * - * Licensed 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 -#include -#include - -#include "bttester.h" - -#ifdef CONFIG_MICROKERNEL -void mainloop(void) -#else -void main(void) -#endif -{ - tester_init(); -} diff --git a/samples/bluetooth/tester/testcase.ini b/samples/bluetooth/tester/testcase.ini deleted file mode 100644 index 840b944c3..000000000 --- a/samples/bluetooth/tester/testcase.ini +++ /dev/null @@ -1,12 +0,0 @@ -[test] -tags = bluetooth -build_only = true -platform_whitelist = basic_cortex_m3 minnowboard - -[tesr_nble] -tags = bluetooth -build_only = true -extra_args = CONF_FILE="prj_nble.conf" -arch_whitelist = x86 -config_whitelist = CONFIG_SOC_QUARK_SE -platform_whitelist = arduino_101 diff --git a/samples/microkernel/test/test_bluetooth/Makefile b/samples/microkernel/test/test_bluetooth/Makefile deleted file mode 100644 index 4f982bb26..000000000 --- a/samples/microkernel/test/test_bluetooth/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -MDEF_FILE = prj.mdef -KERNEL_TYPE = micro -BOARD ?= qemu_x86 -CONF_FILE = prj_$(ARCH).conf -SOURCE_DIR = $(ZEPHYR_BASE)/samples/bluetooth/test_bluetooth/ - -include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/microkernel/test/test_bluetooth/prj.mdef b/samples/microkernel/test/test_bluetooth/prj.mdef deleted file mode 100644 index a3ad4a5e6..000000000 --- a/samples/microkernel/test/test_bluetooth/prj.mdef +++ /dev/null @@ -1,5 +0,0 @@ -% Application : Bluetooth test - -% TASK NAME PRIO ENTRY STACK GROUPS -% =================================================== - TASK MAIN 7 mainloop 2048 [EXE] diff --git a/samples/microkernel/test/test_bluetooth/prj_arm.conf b/samples/microkernel/test/test_bluetooth/prj_arm.conf deleted file mode 100644 index 67e07cfcf..000000000 --- a/samples/microkernel/test/test_bluetooth/prj_arm.conf +++ /dev/null @@ -1,8 +0,0 @@ -# Let stack canaries use non-random number generator. -# This option is NOT to be used in production code. -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_BLUETOOTH=y -CONFIG_BLUETOOTH_LE=y -CONFIG_BLUETOOTH_NO_DRIVER=y -CONFIG_BLUETOOTH_DEBUG=y -CONFIG_UART_INTERRUPT_DRIVEN=n diff --git a/samples/microkernel/test/test_bluetooth/prj_x86.conf b/samples/microkernel/test/test_bluetooth/prj_x86.conf deleted file mode 100644 index 67e07cfcf..000000000 --- a/samples/microkernel/test/test_bluetooth/prj_x86.conf +++ /dev/null @@ -1,8 +0,0 @@ -# Let stack canaries use non-random number generator. -# This option is NOT to be used in production code. -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_BLUETOOTH=y -CONFIG_BLUETOOTH_LE=y -CONFIG_BLUETOOTH_NO_DRIVER=y -CONFIG_BLUETOOTH_DEBUG=y -CONFIG_UART_INTERRUPT_DRIVEN=n diff --git a/samples/microkernel/test/test_bluetooth/testcase.ini b/samples/microkernel/test/test_bluetooth/testcase.ini deleted file mode 100644 index ee3f7b9a0..000000000 --- a/samples/microkernel/test/test_bluetooth/testcase.ini +++ /dev/null @@ -1,3 +0,0 @@ -[test-bluetooth] -tags = bluetooth - diff --git a/samples/nanokernel/test/test_bluetooth/Makefile b/samples/nanokernel/test/test_bluetooth/Makefile deleted file mode 100644 index 4c22b722d..000000000 --- a/samples/nanokernel/test/test_bluetooth/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -KERNEL_TYPE = nano -BOARD ?= qemu_x86 -CONF_FILE = prj_$(ARCH).conf -SOURCE_DIR = $(ZEPHYR_BASE)/samples/bluetooth/test_bluetooth/ - -include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/nanokernel/test/test_bluetooth/prj_arm.conf b/samples/nanokernel/test/test_bluetooth/prj_arm.conf deleted file mode 100644 index 67e07cfcf..000000000 --- a/samples/nanokernel/test/test_bluetooth/prj_arm.conf +++ /dev/null @@ -1,8 +0,0 @@ -# Let stack canaries use non-random number generator. -# This option is NOT to be used in production code. -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_BLUETOOTH=y -CONFIG_BLUETOOTH_LE=y -CONFIG_BLUETOOTH_NO_DRIVER=y -CONFIG_BLUETOOTH_DEBUG=y -CONFIG_UART_INTERRUPT_DRIVEN=n diff --git a/samples/nanokernel/test/test_bluetooth/prj_x86.conf b/samples/nanokernel/test/test_bluetooth/prj_x86.conf deleted file mode 100644 index 67e07cfcf..000000000 --- a/samples/nanokernel/test/test_bluetooth/prj_x86.conf +++ /dev/null @@ -1,8 +0,0 @@ -# Let stack canaries use non-random number generator. -# This option is NOT to be used in production code. -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_BLUETOOTH=y -CONFIG_BLUETOOTH_LE=y -CONFIG_BLUETOOTH_NO_DRIVER=y -CONFIG_BLUETOOTH_DEBUG=y -CONFIG_UART_INTERRUPT_DRIVEN=n diff --git a/samples/nanokernel/test/test_bluetooth/testcase.ini b/samples/nanokernel/test/test_bluetooth/testcase.ini deleted file mode 100644 index 8b8817f45..000000000 --- a/samples/nanokernel/test/test_bluetooth/testcase.ini +++ /dev/null @@ -1,4 +0,0 @@ -[test-bluetooth] -tags = bluetooth -arch_whitelist = x86 arm -platform_whitelist = minnowboard basic_minuteia arduino_101 cortex_m3 frdm_k64f -- cgit v1.2.3