gatt_service: implement TX Power Service Server

This commit is contained in:
Milanka Ringwald 2021-09-14 14:39:08 +02:00
parent 9e81dea27f
commit 44bb45f308
8 changed files with 278 additions and 6 deletions

View File

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `a2dp_source_register_media_config_validator`
- A2DP Sink: allow accept or reject of stream start on A2DP_SUBEVENT_START_STREAM_REQUESTED when ENABLE_AVDTP_ACCEPTOR_EXPLICIT_START_STREAM_CONFIRMATION is defined
- SM: Support Cross-Transport Key-Derivation (CTKD) of LE LTK from BR/EDR SC Link Key
- GATT Service: support TX Power Service Server
### Fixed
- A2DP Source: fix reconfigure

View File

@ -16,5 +16,6 @@ SRC_BLE_GATT_SERVICE_FILES = \
nordic_spp_service_server.c \
scan_parameters_service_client.c \
scan_parameters_service_server.c \
tx_power_service_server.c \
ublox_spp_service_server.c \

View File

@ -0,0 +1,6 @@
// Specification Type org.bluetooth.service.tx_power
// https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Services/org.bluetooth.service.tx_power.xml
// Tx Power 1804
PRIMARY_SERVICE, ORG_BLUETOOTH_SERVICE_TX_POWER
CHARACTERISTIC, ORG_BLUETOOTH_CHARACTERISTIC_TX_POWER_LEVEL, DYNAMIC | READ,

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "tx_power_service_server.c"
/**
* Implementation of the GATT TX Power Service Server
* To use with your application, add '#import <tx_power_service.gatt' to your .gatt file
*/
#include "btstack_defines.h"
#include "ble/att_db.h"
#include "ble/att_server.h"
#include "btstack_util.h"
#include "bluetooth_gatt.h"
#include "btstack_debug.h"
#include "ble/gatt-service/tx_power_service_server.h"
static att_service_handler_t tx_power_service_handler;
static int8_t tx_power_level_value;
static uint16_t tx_power_level_value_handle;
static uint16_t tx_power_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
UNUSED(con_handle);
if (attribute_handle == tx_power_level_value_handle){
return att_read_callback_handle_byte((uint8_t)tx_power_level_value, offset, buffer, buffer_size);
}
return 0;
}
void tx_power_service_server_init(int8_t tx_power_level){
tx_power_level_value = tx_power_level;
// get service handle range
uint16_t start_handle = 0;
uint16_t end_handle = 0xfff;
bool service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_TX_POWER, &start_handle, &end_handle);
btstack_assert(service_found);
UNUSED(service_found);
// get characteristic value handle and client configuration handle
tx_power_level_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_TX_POWER_LEVEL);
// register service with ATT Server
tx_power_service_handler.start_handle = start_handle;
tx_power_service_handler.end_handle = end_handle;
tx_power_service_handler.read_callback = &tx_power_service_read_callback;
tx_power_service_handler.write_callback = NULL;
att_server_register_service_handler(&tx_power_service_handler);
}
void tx_power_service_server_set_level(int8_t tx_power_level){
tx_power_level_value = tx_power_level;
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/**
* @title TX Power Service Server
*
*/
#ifndef TX_POWER_SERVICE_SERVER_H
#define TX_POWER_SERVICE_SERVER_H
#include <stdint.h>
#if defined __cplusplus
extern "C" {
#endif
/**
* @text The TX Power service exposes a devices current transmit power level when in a connection.
* There shall only be one instance of the Tx Power service on a device.
*
* To use with your application, add `#import <tx_power_service.gatt>` to your .gatt file.
* After adding it to your .gatt file, you call *tx_power_service_server_init(value)* with the
* devices current transmit power level value. The valid range for the power level is 0-100.
*
* If the power level value changes, you can call *tx_power_service_server_set_level(tx_power_level)*.
* The service does not support sending Notifications.
*/
/* API_START */
/**
* @brief Init TX Power Service Server with ATT DB
* @param tx_power_level
*/
void tx_power_service_server_init(int8_t tx_power_level);
/**
* @brief Update TX power level
* @param tx_power_level
*/
void tx_power_service_server_set_level(int8_t tx_power_level);
/* API_END */
#if defined __cplusplus
}
#endif
#endif

View File

@ -29,6 +29,7 @@ COMMON = \
cycling_power_service_server.c \
cycling_speed_and_cadence_service_server.c \
device_information_service_server.c \
tx_power_service_server.c \
hci_dump.c \
heart_rate_service_server.c \
hids_device.c \
@ -48,9 +49,9 @@ COMMON_OBJ_COVERAGE = $(addprefix build-coverage/,$(COMMON:.c=.o))
COMMON_OBJ_ASAN = $(addprefix build-asan/, $(COMMON:.c=.o))
all: build-coverage/battery_service_test build-coverage/device_information_service_test \
build-asan/battery_service_test build-asan/device_information_service_test
all: build-coverage/battery_service_test build-asan/battery_service_test \
build-coverage/device_information_service_test build-asan/device_information_service_test \
build-coverage/tx_power_service_test build-asan/tx_power_service_test \
build-%:
mkdir -p $@
@ -61,6 +62,9 @@ build-%/battery_service_profile.h: battery_service_profile.gatt | build-%
build-%/device_information_service_profile.h: device_information_service.gatt| build-%
python3 ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@
build-%/tx_power_service_profile.h: tx_power_service.gatt| build-%
python3 ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@
build-coverage/%.o: %.c | build-coverage
${CC} -c $(CFLAGS_COVERAGE) $< -o $@
@ -70,23 +74,31 @@ build-asan/%.o: %.c | build-asan
build-coverage/battery_service_test: build-coverage/battery_service_profile.h ${COMMON_OBJ_COVERAGE} build-coverage/battery_service_test.o | build-coverage
${CC} $(filter-out build-coverage/battery_service_profile.h,$^) ${LDFLAGS_COVERAGE} -o $@
build-coverage/device_information_service_test: build-coverage/device_information_service_profile.h ${COMMON_OBJ_COVERAGE} build-coverage/device_information_service_test.o | build-coverage
${CC} $(filter-out build-coverage/device_information_service_profile.h,$^) ${LDFLAGS_COVERAGE} -o $@
build-asan/battery_service_test: build-asan/battery_service_profile.h ${COMMON_OBJ_ASAN} build-asan/battery_service_test.o | build-asan
${CC} $(filter-out build-asan/battery_service_profile.h,$^) ${LDFLAGS_ASAN} -o $@
build-coverage/device_information_service_test: build-coverage/device_information_service_profile.h ${COMMON_OBJ_COVERAGE} build-coverage/device_information_service_test.o | build-coverage
${CC} $(filter-out build-coverage/device_information_service_profile.h,$^) ${LDFLAGS_COVERAGE} -o $@
build-asan/device_information_service_test: build-asan/device_information_service_profile.h ${COMMON_OBJ_ASAN} build-asan/device_information_service_test.o | build-asan
${CC} $(filter-out build-asan/device_information_service_profile.h,$^) ${LDFLAGS_ASAN} -o $@
build-coverage/tx_power_service_test: build-coverage/tx_power_service_profile.h ${COMMON_OBJ_COVERAGE} build-coverage/tx_power_service_test.o | build-coverage
${CC} $(filter-out build-coverage/tx_power_service_profile.h,$^) ${LDFLAGS_COVERAGE} -o $@
build-asan/tx_power_service_test: build-asan/tx_power_service_profile.h ${COMMON_OBJ_ASAN} build-asan/tx_power_service_test.o | build-asan
${CC} $(filter-out build-asan/tx_power_service_profile.h,$^) ${LDFLAGS_ASAN} -o $@
test: all
build-asan/battery_service_test
build-asan/device_information_service_test
build-asan/tx_power_service_test
coverage: all
rm -f build-coverage/*.gcda
build-coverage/battery_service_test
build-coverage/device_information_service_test
build-asan/tx_power_service_test
clean:
rm -rf build-coverage build-asan

View File

@ -0,0 +1,2 @@
// add TX Power Service
#import <tx_power_service.gatt>

View File

@ -0,0 +1,71 @@
// *****************************************************************************
//
// test battery service
//
// *****************************************************************************
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTestExt/MockSupport.h"
#include "hci.h"
#include "btstack_util.h"
#include "bluetooth.h"
#include "bluetooth_gatt.h"
#include "ble/gatt-service/tx_power_service_server.h"
#include "tx_power_service_profile.h"
#include "mock_att_server.h"
static int8_t tx_power_level = 100;
TEST_GROUP(TX_POWER_SERVICE_SERVER){
att_service_handler_t * service;
uint16_t con_handle;
uint16_t tx_power_level_value_handle;
void setup(void){
// setup database
att_set_db(profile_data);
tx_power_level_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_TX_POWER_LEVEL);
// setup tx power service
tx_power_service_server_init(tx_power_level);
service = mock_att_server_get_service();
con_handle = 0x00;
}
void teardown(){
mock_deinit();
}
};
TEST(TX_POWER_SERVICE_SERVER, lookup_attribute_handles){
CHECK(tx_power_level_value_handle != 0);
}
TEST(TX_POWER_SERVICE_SERVER, read_tx_power_level_value){
uint8_t response[1];
uint16_t response_len;
// invalid attribute handle
response_len = mock_att_service_read_callback(con_handle, 0xffff, 0xffff, response, sizeof(response));
CHECK_EQUAL(0, response_len);
response_len = mock_att_service_read_callback(con_handle, tx_power_level_value_handle, 0, response, sizeof(response));
CHECK_EQUAL(1, response_len);
}
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}