gatt_client: support Microphone Control Service Server

This commit is contained in:
Milanka Ringwald 2021-09-21 14:07:55 +02:00
parent 7a38be12a5
commit 5c562e5771
9 changed files with 382 additions and 2 deletions

View File

@ -19,8 +19,9 @@ 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: TX Power Service (TPS 1.0) Server
- GATT Service: Bond Management Service (BMS 1.0) Server
- GATT Service: Microphone Control Service (MICS 1.0) Server
- GATT Service: TX Power Service (TPS 1.0) Server
### Fixed
- A2DP Source: fix reconfigure

View File

@ -14,6 +14,7 @@ SRC_BLE_GATT_SERVICE_FILES = \
hids_device.c \
mesh_provisioning_service_server.c \
mesh_proxy_service_server.c \
microphone_control_service_server.c \
nordic_spp_service_server.c \
scan_parameters_service_client.c \
scan_parameters_service_server.c \

View File

@ -0,0 +1,4 @@
// Bond Management Service 181E
PRIMARY_SERVICE, ORG_BLUETOOTH_SERVICE_MICROPHONE_CONTROL
CHARACTERISTIC, ORG_BLUETOOTH_CHARACTERISTIC_MUTE, DYNAMIC | READ | WRITE| NOTIFY,
CLIENT_CHARACTERISTIC_CONFIGURATION, READ | WRITE,

View File

@ -0,0 +1,151 @@
/*
* 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__ "microphone_control_service_server.c"
/**
* Implementation of the GATT Battery Service Server
* To use with your application, add '#import <microphone_control.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/microphone_control_service_server.h"
static btstack_context_callback_registration_t mc_mute_callback;
static att_service_handler_t microphone_control;
static microphone_control_mute_t mc_mute_value;
static uint16_t mc_mute_value_client_configuration;
static hci_con_handle_t mc_mute_value_client_configuration_connection;
static uint16_t mc_mute_value_handle;
static uint16_t mc_mute_value_handle_client_configuration;
static uint16_t microphone_control_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 == mc_mute_value_handle){
return att_read_callback_handle_byte((uint8_t)mc_mute_value, offset, buffer, buffer_size);
}
if (attribute_handle == mc_mute_value_handle_client_configuration){
return att_read_callback_handle_little_endian_16(mc_mute_value_client_configuration, offset, buffer, buffer_size);
}
return 0;
}
static int microphone_control_service_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
UNUSED(transaction_mode);
UNUSED(offset);
UNUSED(buffer_size);
if (attribute_handle == mc_mute_value_handle){
if (buffer_size != 1){
return ATT_ERROR_VALUE_NOT_ALLOWED;
}
microphone_control_mute_t mute_value = (microphone_control_mute_t)buffer[0];
switch (mute_value){
case MICROPHONE_CONTROL_MUTE_OFF:
case MICROPHONE_CONTROL_MUTE_ON:
if (mc_mute_value == MICROPHONE_CONTROL_MUTE_DISABLED){
return ATT_ERROR_RESPONSE_MICROPHONE_CONTROL_MUTE_DISABLED;
}
mc_mute_value = mute_value;
break;
default:
return ATT_ERROR_VALUE_NOT_ALLOWED;
}
}
if (attribute_handle == mc_mute_value_handle_client_configuration){
if (buffer_size != 2){
return ATT_ERROR_VALUE_NOT_ALLOWED;
}
mc_mute_value_client_configuration = little_endian_read_16(buffer, 0);
mc_mute_value_client_configuration_connection = con_handle;
}
return 0;
}
static void microphone_control_service_can_send_now(void * context){
hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
uint8_t value = (uint8_t) mc_mute_value;
att_server_notify(con_handle, mc_mute_value_handle, &value, 1);
}
void microphone_control_service_server_init(microphone_control_mute_t mute_value){
mc_mute_value = mute_value;
// get service handle range
uint16_t start_handle = 0;
uint16_t end_handle = 0xfff;
int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_MICROPHONE_CONTROL, &start_handle, &end_handle);
btstack_assert(service_found != 0);
UNUSED(service_found);
// get characteristic value handle and client configuration handle
mc_mute_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MUTE);
mc_mute_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_MUTE);
// register service with ATT Server
microphone_control.start_handle = start_handle;
microphone_control.end_handle = end_handle;
microphone_control.read_callback = &microphone_control_service_read_callback;
microphone_control.write_callback = &microphone_control_service_write_callback;
att_server_register_service_handler(&microphone_control);
}
void microphone_control_service_server_set_mute(microphone_control_mute_t mute_value){
if (mc_mute_value == mute_value){
return;
}
mc_mute_value = mute_value;
if (mc_mute_value_client_configuration != 0){
mc_mute_callback.callback = &microphone_control_service_can_send_now;
mc_mute_callback.context = (void*) (uintptr_t) mc_mute_value_client_configuration_connection;
att_server_register_can_send_now_callback(&mc_mute_callback, mc_mute_value_client_configuration_connection);
}
}

View File

@ -0,0 +1,89 @@
/*
* 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 Microphone Control Service Server
*
*/
#ifndef MICROPHONE_CONTROL_SERVICE_SERVER_H
#define MICROPHONE_CONTROL_SERVICE_SERVER_H
#include <stdint.h>
#if defined __cplusplus
extern "C" {
#endif
#define ATT_ERROR_RESPONSE_MICROPHONE_CONTROL_MUTE_DISABLED 0x80
typedef enum {
MICROPHONE_CONTROL_MUTE_OFF = 0x00,
MICROPHONE_CONTROL_MUTE_ON,
MICROPHONE_CONTROL_MUTE_DISABLED
} microphone_control_mute_t;
/**
* @text The Microphone Control Service enables a device to expose the mute control and state of one or more microphones.
* Only server can disable and enable mute. Currently one one client supported.
*
* To use with your application, add `#import <microphone_control_service.gatt>` to your .gatt file.
*/
/* API_START */
/**
* @brief Init Microphone Control Service Server with ATT DB
* @param mute_value
*/
void microphone_control_service_server_init(microphone_control_mute_t mute_value);
/**
* @brief Set mute value.
* @param mute_value
*/
void microphone_control_service_server_set_mute(microphone_control_mute_t mute_value);
/* API_END */
#if defined __cplusplus
}
#endif
#endif

View File

@ -616,6 +616,7 @@ typedef enum {
#define ATT_ERROR_INSUFFICIENT_ENCRYPTION 0x0f
#define ATT_ERROR_UNSUPPORTED_GROUP_TYPE 0x10
#define ATT_ERROR_INSUFFICIENT_RESOURCES 0x11
#define ATT_ERROR_VALUE_NOT_ALLOWED 0x13
// MARK: ATT Error Codes defined by BTstack
#define ATT_ERROR_HCI_DISCONNECT_RECEIVED 0x1f

View File

@ -29,12 +29,13 @@ 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 \
microphone_control_service_server.c \
mock_att_server.c \
nordic_spp_service_server.c \
tx_power_service_server.c \
ublox_spp_service_server.c \
@ -53,6 +54,7 @@ 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-coverage/bond_management_service_test build-asan/bond_management_service_test \
build-coverage/microphone_control_service_test build-asan/microphone_control_service_test \
build-%:
mkdir -p $@
@ -69,6 +71,9 @@ build-%/tx_power_service_profile.h: tx_power_service.gatt| build-%
build-%/bond_management_service_profile.h: bond_management_service.gatt| build-%
python3 ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@
build-%/microphone_control_service_profile.h: microphone_control_service.gatt| build-%
python3 ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@
build-coverage/%.o: %.c | build-coverage
${CC} -c $(CFLAGS_COVERAGE) $< -o $@
@ -99,11 +104,18 @@ build-coverage/bond_management_service_test: build-coverage/bond_management_serv
build-asan/bond_management_service_test: build-asan/bond_management_service_profile.h ${COMMON_OBJ_ASAN} build-asan/bond_management_service_server.o build-asan/bond_management_service_test.o | build-asan
${CC} $(filter-out build-asan/bond_management_service_profile.h,$^) ${LDFLAGS_ASAN} -o $@
build-coverage/microphone_control_service_test: build-coverage/microphone_control_service_profile.h ${COMMON_OBJ_COVERAGE} build-coverage/microphone_control_service_server.o build-coverage/microphone_control_service_test.o | build-coverage
${CC} $(filter-out build-coverage/microphone_control_service_profile.h,$^) ${LDFLAGS_COVERAGE} -o $@
build-asan/microphone_control_service_test: build-asan/microphone_control_service_profile.h ${COMMON_OBJ_ASAN} build-asan/microphone_control_service_server.o build-asan/microphone_control_service_test.o | build-asan
${CC} $(filter-out build-asan/microphone_control_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
build-asan/bond_management_service_test
build-asan/microphone_control_service_test
coverage: all
rm -f build-coverage/*.gcda
@ -111,6 +123,7 @@ coverage: all
build-coverage/device_information_service_test
build-asan/tx_power_service_test
build-asan/bond_management_service_test
build-asan/microphone_control_service_test
clean:
rm -rf build-coverage build-asan

View File

@ -0,0 +1,2 @@
// add Microphone Control Service
#import <microphone_control_service.gatt>

View File

@ -0,0 +1,118 @@
// *****************************************************************************
//
// 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/microphone_control_service_server.h"
#include "microphone_control_service_profile.h"
#include "mock_att_server.h"
TEST_GROUP(MICROPHONE_CONTROL_SERVICE_SERVER){
att_service_handler_t * service;
uint16_t con_handle;
uint16_t mute_value_handle;
uint16_t mute_value_handle_client_configuration;
void setup(void){
// setup database
att_set_db(profile_data);
mute_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_MUTE);
mute_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_MUTE);
// setup battery service
microphone_control_service_server_init(MICROPHONE_CONTROL_MUTE_OFF);
service = mock_att_server_get_service();
con_handle = 0x00;
}
void teardown(){
mock_deinit();
}
};
TEST(MICROPHONE_CONTROL_SERVICE_SERVER, set_mute_value_trigger_can_send_now){
// enable notifications
const uint8_t enable_notify[]= { 0x1, 0x0 };
mock_att_service_write_callback(con_handle, mute_value_handle_client_configuration, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
// set battery to trigger notification
mock().expectOneCall("att_server_register_can_send_now_callback");
microphone_control_service_server_set_mute(MICROPHONE_CONTROL_MUTE_ON);
mock().checkExpectations();
mock().expectOneCall("att_server_notify");
mock_att_service_trigger_can_send_now();
mock().checkExpectations();
}
TEST(MICROPHONE_CONTROL_SERVICE_SERVER, lookup_attribute_handles){
CHECK(mute_value_handle != 0);
CHECK(mute_value_handle_client_configuration != 0);
}
TEST(MICROPHONE_CONTROL_SERVICE_SERVER, set_mute_value){
// mute_value_handle_client_configuration not set
mock().expectNCalls(con_handle, "att_server_register_can_send_now_callback");
microphone_control_service_server_set_mute(MICROPHONE_CONTROL_MUTE_ON);
mock().checkExpectations();
// mute_value_handle_client_configuration set
mock().expectOneCall("att_server_register_can_send_now_callback");
const uint8_t enable_notify[]= { 0x1, 0x0 };
mock_att_service_write_callback(con_handle, mute_value_handle_client_configuration, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
microphone_control_service_server_set_mute(MICROPHONE_CONTROL_MUTE_OFF);
mock().checkExpectations();
}
TEST(MICROPHONE_CONTROL_SERVICE_SERVER, set_wrong_handle_mute_value){
// mute_value_handle_client_configuration not set
mock().expectNCalls(con_handle, "att_server_register_can_send_now_callback");
microphone_control_service_server_set_mute(MICROPHONE_CONTROL_MUTE_ON);
mock().checkExpectations();
// // mute_value_handle_client_configuration set
mock().expectNoCall("att_server_register_can_send_now_callback");
const uint8_t enable_notify[]= { 0x1, 0x0 };
mock_att_service_write_callback(con_handle, mute_value_handle_client_configuration + 10, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
microphone_control_service_server_set_mute(MICROPHONE_CONTROL_MUTE_OFF);
mock().checkExpectations();
}
TEST(MICROPHONE_CONTROL_SERVICE_SERVER, read_mute_value){
uint8_t response[2];
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, mute_value_handle, 0, response, sizeof(response));
CHECK_EQUAL(1, response_len);
response_len = mock_att_service_read_callback(con_handle, mute_value_handle_client_configuration, 0, response, sizeof(response));
CHECK_EQUAL(2, response_len);
}
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}