diff --git a/apps/bttester/pkg.yml b/apps/bttester/pkg.yml index e763185fe5..0972cb7a2b 100644 --- a/apps/bttester/pkg.yml +++ b/apps/bttester/pkg.yml @@ -32,10 +32,12 @@ pkg.deps: - "@apache-mynewt-core/sys/stats" - "@apache-mynewt-core/sys/shell" - "@apache-mynewt-nimble/nimble/host" + - "@apache-mynewt-nimble/nimble/host/audio" - "@apache-mynewt-nimble/nimble/host/util" - "@apache-mynewt-nimble/nimble/host/services/gap" - "@apache-mynewt-nimble/nimble/host/services/gatt" - "@apache-mynewt-nimble/nimble/host/services/dis" + - "@apache-mynewt-nimble/nimble/host/audio/services/bass" - "@apache-mynewt-nimble/nimble/host/store/config" - "@apache-mynewt-core/hw/drivers/uart" - "@apache-mynewt-core/hw/drivers/rtt" diff --git a/apps/bttester/src/btp/btp.h b/apps/bttester/src/btp/btp.h index 4403a16038..a4ae5225a2 100644 --- a/apps/bttester/src/btp/btp.h +++ b/apps/bttester/src/btp/btp.h @@ -48,6 +48,7 @@ #define BTP_SERVICE_ID_MESH 4 #define BTP_SERVICE_ID_GATTC 6 #define BTP_SERVICE_ID_BAP 14 +#define BTP_SERVICE_ID_BASS 14 #define BTP_SERVICE_ID_MAX BTP_SERVICE_ID_BAP diff --git a/apps/bttester/src/btp/btp_bap.h b/apps/bttester/src/btp/btp_bap.h index f25ab14748..844495f1ae 100644 --- a/apps/bttester/src/btp/btp_bap.h +++ b/apps/bttester/src/btp/btp_bap.h @@ -83,12 +83,27 @@ struct bap_bap_broadcast_source_stop_cmd { uint8_t broadcast_id[3]; } __packed; -#define BTP_BAP_BROADCAST_SINK_SETUP 0x0a +#define BTP_BAP_BROADCAST_SINK_SETUP 0xa +struct btp_bap_broadcast_sink_setup_cmd { +} __packed; + +#define BTP_BAP_BROADCAST_SINK_STOP 0x0f +struct btp_bap_broadcast_sink_stop_cmd { + ble_addr_t address; + uint8_t broadcast_id[3]; +} __packed; + +#define BTP_BAP_SET_BROADCAST_CODE 0x17 +struct btp_bap_set_broadcast_code_cmd { + ble_addr_t addr; + uint8_t source_id; + uint8_t broadcast_code[16]; +} __packed; + #define BTP_BAP_BROADCAST_SINK_RELEASE 0x0b #define BTP_BAP_BROADCAST_SCAN_START 0x0c #define BTP_BAP_BROADCAST_SCAN_STOP 0x0d #define BTP_BAP_BROADCAST_SINK_SYNC 0x0e -#define BTP_BAP_BROADCAST_SINK_STOP 0x0f #define BTP_BAP_BROADCAST_SINK_BIS_SYNC 0x10 #define BTP_BAP_DISCOVER_SCAN_DELEGATOR 0x11 #define BTP_BAP_BROADCAST_ASSISTANT_SCAN_START 0x12 diff --git a/apps/bttester/src/btp/btp_bass.h b/apps/bttester/src/btp/btp_bass.h new file mode 100644 index 0000000000..8e840037e3 --- /dev/null +++ b/apps/bttester/src/btp/btp_bass.h @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BTP_BASS_ +#define H_BTP_BASS_ + +#include "nimble/ble.h" +#include + +#ifndef __packed +#define __packed __attribute__((__packed__)) +#endif + +/* BAS Service */ +/* commands */ +#define BTP_BASS_READ_SUPPORTED_COMMANDS 0x01 +struct btp_bass_read_supported_commands_rp { + uint8_t data[0]; +} __packed; + +#endif /* H_BTP_BASS_ */ \ No newline at end of file diff --git a/apps/bttester/src/btp/btp_gap.h b/apps/bttester/src/btp/btp_gap.h index 2a87b12930..7af0a42f92 100644 --- a/apps/bttester/src/btp/btp_gap.h +++ b/apps/bttester/src/btp/btp_gap.h @@ -70,6 +70,7 @@ struct btp_gap_read_controller_index_list_rp { #define BTP_GAP_SETTINGS_PRIVACY 13 #define BTP_GAP_SETTINGS_CONTROLLER_CONFIG 14 #define BTP_GAP_SETTINGS_STATIC_ADDRESS 15 +#define BTP_GAP_SETTINGS_EXTENDED_ADVERTISING 17 #define BTP_GAP_SETTINGS_PERIODIC_ADVERTISING 18 #define BTP_GAP_READ_CONTROLLER_INFO 0x03 @@ -258,6 +259,15 @@ struct btp_gap_set_filter_accept_list_cmd { ble_addr_t addrs[]; } __packed; +#define GAP_SET_EXT_ADV 0x21 +struct btp_gap_set_ext_advertising_cmd { + uint8_t flags; +} __packed; + +struct btp_gap_set_ext_advertising_rp { + uint32_t current_settings; +} __packed; + #define GAP_PADV_CONFIGURE 0x22 struct gap_periodic_adv_configure_cmd { uint8_t flags; diff --git a/apps/bttester/src/btp/bttester.h b/apps/bttester/src/btp/bttester.h index 7cb82b5c92..873a0803a0 100644 --- a/apps/bttester/src/btp/bttester.h +++ b/apps/bttester/src/btp/bttester.h @@ -148,6 +148,9 @@ tester_init_bap(void); uint8_t tester_unregister_bap(void); #endif /* MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE) */ - -#endif /* __BTTESTER_H__ */ +uint8_t +tester_init_bass(void); +uint8_t +tester_unregister_bass(void); +#endif /* MYNEWT_VAL(BLE_ISO_BROADCASTER) */ diff --git a/apps/bttester/src/btp_bap.c b/apps/bttester/src/btp_bap.c index 5a2d06c811..b72f714a89 100644 --- a/apps/bttester/src/btp_bap.c +++ b/apps/bttester/src/btp_bap.c @@ -19,22 +19,24 @@ /* btp_bap.c - Bluetooth Basic Audio Profile Tester */ +#include "btp/bttester.h" #include "syscfg/syscfg.h" +#include + #if MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE) #include "btp/btp_bap.h" - #include "btp/btp.h" #include "console/console.h" -#include "nimble/ble.h" #include "host/ble_hs.h" #include "host/util/util.h" #include "math.h" #include "audio/ble_audio_broadcast_source.h" +#include "audio/ble_audio_broadcast_sink.h" #include "audio/ble_audio.h" #include "host/ble_iso.h" @@ -319,6 +321,32 @@ broadcast_source_start(const void *cmd, uint16_t cmd_len, void *rsp, return BTP_STATUS_SUCCESS; } +static uint8_t broadcast_code_set(const void *cmd, uint16_t cmd_len, void *rsp, + uint16_t *rsp_len) +{ + return BTP_STATUS_SUCCESS; +} + +static uint8_t broadcast_sink_setup(const void *cmd, uint16_t cmd_len, void *rsp, + uint16_t *rsp_len) +{ + return BTP_STATUS_SUCCESS; +} + +static uint8_t broadcast_sink_stop(const void *cmd, uint16_t cmd_len, void *rsp, + uint16_t *rsp_len) +{ + int rc; + //const struct btp_bap_broadcast_sink_stop_cmd *cp = cmd; + + rc = ble_audio_broadcast_sink_stop(0); + if (rc) { + return BTP_STATUS_FAILED; + } + + return rc; +} + static uint8_t broadcast_source_stop(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len) @@ -371,8 +399,171 @@ static const struct btp_handler handlers[] = { .expect_len = sizeof(struct bap_bap_broadcast_source_stop_cmd), .func = broadcast_source_stop, }, + { + .opcode = BTP_BAP_SET_BROADCAST_CODE, + .index = BTP_INDEX, + .expect_len = sizeof(struct btp_bap_set_broadcast_code_cmd), + .func = broadcast_code_set, + }, + { + .opcode = BTP_BAP_BROADCAST_SINK_SETUP, + .index = BTP_INDEX, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, + .func = broadcast_sink_setup, + }, + { + .opcode = BTP_BAP_BROADCAST_SINK_STOP, + .index = BTP_INDEX, + .expect_len = BTP_HANDLER_LENGTH_VARIABLE, + .func = broadcast_sink_stop, + }, }; +#define BROADCAST_SINK_PA_SYNC_TIMEOUT_DEFAULT 0x07D0 + +static int +broadcast_sink_pa_sync_params_get(struct ble_gap_periodic_sync_params *params) +{ + params->skip = 0; + params->sync_timeout = BROADCAST_SINK_PA_SYNC_TIMEOUT_DEFAULT; + params->reports_disabled = false; + + return 0; +} + +static int +broadcast_sink_disc_start(const struct ble_gap_ext_disc_params *params) +{ + uint8_t own_addr_type; + int rc; + + /* Figure out address to use while scanning. */ + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) { + console_printf("determining own address type failed (%d)", rc); + assert(0); + } + + rc = ble_gap_ext_disc(own_addr_type, 0, 0, 0, 0, 0, params, NULL, NULL, NULL); + if (rc != 0) { + console_printf("ext disc failed (%d)", rc); + } + + return rc; +} + +static int +broadcast_sink_disc_stop(void) +{ + int rc; + + rc = ble_gap_disc_cancel(); + if (rc != 0) { + console_printf("disc cancel failed (%d)", rc); + } + + return rc; +} + +static int +broadcast_sink_action_fn(struct ble_audio_broadcast_sink_action *action, void *arg) +{ + switch (action->type) { + case BLE_AUDIO_BROADCAST_SINK_ACTION_PA_SYNC: + return broadcast_sink_pa_sync_params_get(action->pa_sync.out_params); + case BLE_AUDIO_BROADCAST_SINK_ACTION_BIG_SYNC: + break; + case BLE_AUDIO_BROADCAST_SINK_ACTION_BIS_SYNC: + return 0; + case BLE_AUDIO_BROADCAST_SINK_ACTION_DISC_START: + return broadcast_sink_disc_start(action->disc_start.params_preferred); + case BLE_AUDIO_BROADCAST_SINK_ACTION_DISC_STOP: + return broadcast_sink_disc_stop(); + default: + assert(false); + return BLE_HS_ENOTSUP; + } + + return 0; +} + +static int +broadcast_sink_audio_event_handler(struct ble_audio_event *event, void *arg) +{ + switch (event->type) { + case BLE_AUDIO_EVENT_BROADCAST_SINK_PA_SYNC_STATE: + console_printf("source_id=0x%02x PA sync: %s\n", + event->broadcast_sink_pa_sync_state.source_id, + ble_audio_broadcast_sink_sync_state_str( + event->broadcast_sink_pa_sync_state.state)); + break; + case BLE_AUDIO_EVENT_BROADCAST_SINK_BIS_SYNC_STATE: + console_printf("source_id=0x%02x bis_index=0x%02x BIS sync: %s\n", + event->broadcast_sink_bis_sync_state.source_id, + event->broadcast_sink_bis_sync_state.bis_index, + ble_audio_broadcast_sink_sync_state_str( + event->broadcast_sink_bis_sync_state.state)); + if (event->broadcast_sink_bis_sync_state.state == + BLE_AUDIO_BROADCAST_SINK_SYNC_STATE_ESTABLISHED) { + console_printf("conn_handle=0x%04x\n", + event->broadcast_sink_bis_sync_state.conn_handle); + } + break; + default: + break; + } + + return 0; +} + +static int +scan_delegator_pick_source_id_to_swap(uint8_t *out_source_id_to_swap) +{ + /* TODO: Add some logic here */ + *out_source_id_to_swap = 0; + + return 0; +} + +static int +scan_delegator_action_fn(struct ble_audio_scan_delegator_action *action, void *arg) +{ + switch (action->type) { + case BLE_AUDIO_SCAN_DELEGATOR_ACTION_SOURCE_ADD: + console_printf("Source Add:\nsource_id=%u\n", action->source_add.source_id); + if (action->source_add.out_source_id_to_swap == NULL) { + return 0; + } else { + return scan_delegator_pick_source_id_to_swap(action->source_add.out_source_id_to_swap); + } + case BLE_AUDIO_SCAN_DELEGATOR_ACTION_SOURCE_MODIFY: + console_printf("Source Modify:\nsource_id=%u\n", action->source_modify.source_id); + break; + case BLE_AUDIO_SCAN_DELEGATOR_ACTION_SOURCE_REMOVE: + console_printf("Source Remove:\nsource_id=%u\n", action->source_remove.source_id); + break; + default: + assert(false); + return BLE_HS_ENOTSUP; + } + + return 0; +} + +static int +scan_delegator_audio_event_handler(struct ble_audio_event *event, void *arg) +{ + switch (event->type) { + case BLE_AUDIO_EVENT_BROADCAST_ANNOUNCEMENT: + console_printf("\n"); + break; + default: + break; + } + + return 0; +} + uint8_t tester_init_bap(void) { @@ -405,6 +596,23 @@ tester_init_bap(void) return BTP_STATUS_FAILED; } + static struct ble_audio_event_listener broadcast_sink_listener; + + rc = ble_audio_broadcast_sink_cb_set(broadcast_sink_action_fn, NULL); + assert(rc == 0); + + rc = ble_audio_event_listener_register(&broadcast_sink_listener, + broadcast_sink_audio_event_handler, NULL); + + static struct ble_audio_event_listener scan_delegator_listener; + + rc = ble_audio_scan_delegator_action_fn_set(scan_delegator_action_fn, NULL); + assert(rc == 0); + + rc = ble_audio_event_listener_register(&scan_delegator_listener, + scan_delegator_audio_event_handler, NULL); + assert(rc == 0); + tester_register_command_handlers(BTP_SERVICE_ID_BAP, handlers, ARRAY_SIZE(handlers)); diff --git a/apps/bttester/src/btp_bass.c b/apps/bttester/src/btp_bass.c new file mode 100644 index 0000000000..8947232634 --- /dev/null +++ b/apps/bttester/src/btp_bass.c @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* btp_bass.c - Bluetooth Broadcast Audio Stream Service Tester */ + +#include "syscfg/syscfg.h" +#include + +#if MYNEWT_VAL(BLE_AUDIO) + +#include "btp/btp_bass.h" + + +#include "btp/btp.h" +#include "console/console.h" + +#include "nimble/ble.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "math.h" + +#include "audio/ble_audio_broadcast_source.h" +#include "services/bass/ble_audio_svc_bass.h" +#include "audio/ble_audio.h" +#include "host/ble_iso.h" + +#include "bsp/bsp.h" + +static uint8_t +supported_commands(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) +{ + return BTP_STATUS_SUCCESS; +} + +static const struct btp_handler handlers[] = { + { + .opcode = BTP_BASS_READ_SUPPORTED_COMMANDS, + .index = BTP_INDEX_NONE, + .expect_len = 0, + .func = supported_commands, + }, +}; + +uint8_t +tester_init_bass(void) +{ + tester_register_command_handlers(BTP_SERVICE_ID_BASS, handlers, + ARRAY_SIZE(handlers)); + + return BTP_STATUS_SUCCESS; +} + +uint8_t +tester_unregister_bass(void) +{ + return BTP_STATUS_SUCCESS; +} + +#endif /* MYNEWT_VAL(BLE_AUDIO) */ + diff --git a/apps/bttester/src/btp_core.c b/apps/bttester/src/btp_core.c index a7b1878322..eeae816a9e 100644 --- a/apps/bttester/src/btp_core.c +++ b/apps/bttester/src/btp_core.c @@ -106,7 +106,7 @@ register_service(const void *cmd, uint16_t cmd_len, case BTP_SERVICE_ID_BAP: status = tester_init_bap(); break; -#endif /* MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE) */ +#endif /* MYNEWT_VAL(BLE_ISO_BROADCASTER) */ case BTP_SERVICE_ID_GATTC: status = tester_init_gatt_cl(); break; diff --git a/apps/bttester/src/btp_gap.c b/apps/bttester/src/btp_gap.c index d0aa7ce06e..71062f8946 100644 --- a/apps/bttester/src/btp_gap.c +++ b/apps/bttester/src/btp_gap.c @@ -160,6 +160,7 @@ supported_commands(const void *cmd, uint16_t cmd_len, /* octet 4 */ #if MYNEWT_VAL(BLE_PERIODIC_ADV) + tester_set_bit(rp->data, GAP_SET_EXT_ADV); tester_set_bit(rp->data, GAP_PADV_CONFIGURE); tester_set_bit(rp->data, GAP_PADV_START); tester_set_bit(rp->data, GAP_PADV_SET_DATA); @@ -2000,6 +2001,44 @@ set_filter_accept_list(const void *cmd, uint16_t cmd_len, return BTP_STATUS_SUCCESS; } +#if MYNEWT_VAL(BLE_EXT_ADV) +static uint8_t set_ext_advertising(const void *cmd, uint16_t cmd_len, + void *rsp, uint16_t *rsp_len) +{ + int rc; + //const struct btp_gap_set_ext_advertising_cmd *cp = cmd; + struct ble_gap_ext_adv_params ext_params = {0}; + struct btp_gap_set_ext_advertising_rp *rp = rsp; + + ext_params.connectable = 0; + ext_params.scannable = 0; + ext_params.legacy_pdu = 0; + ext_params.anonymous = 0; + ext_params.own_addr_type = own_addr_type; + ext_params.primary_phy = BLE_HCI_LE_PHY_1M; + ext_params.secondary_phy = BLE_HCI_LE_PHY_1M; + ext_params.sid = 1; + + rc = ble_gap_ext_adv_configure(1, &ext_params, NULL, gap_event_cb, NULL); + if (rc) { + SYS_LOG_ERR("Failed to configure extended advertiser; rc=%d", rc); + return BTP_STATUS_FAILED; + } + + rc = ble_gap_ext_adv_start(1,0,0); + if (rc) { + SYS_LOG_ERR("Failed to start extended advertiser; rc=%d", rc); + return BTP_STATUS_FAILED; + } + + current_settings |= BIT(BTP_GAP_SETTINGS_EXTENDED_ADVERTISING); + + rp->current_settings = htole32(current_settings); + *rsp_len = sizeof(*rp); + return BTP_STATUS_SUCCESS; +} +#endif + #if MYNEWT_VAL(BLE_PERIODIC_ADV) static uint8_t periodic_adv_configure(const void *cmd, uint16_t cmd_len, @@ -2330,6 +2369,13 @@ static const struct btp_handler handlers[] = { .expect_len = BTP_HANDLER_LENGTH_VARIABLE, .func = set_filter_accept_list, }, +#if MYNEWT_VAL(BLE_EXT_ADV) + { + .opcode = GAP_SET_EXT_ADV, + .expect_len = sizeof(struct btp_gap_set_ext_advertising_cmd), + .func = set_ext_advertising, + }, +#endif /* BLE_EXT_ADV*/ #if MYNEWT_VAL(BLE_PERIODIC_ADV) { .opcode = GAP_PADV_CONFIGURE, diff --git a/apps/bttester/syscfg.yml b/apps/bttester/syscfg.yml index 80c377a7c1..da517fecf7 100644 --- a/apps/bttester/syscfg.yml +++ b/apps/bttester/syscfg.yml @@ -93,15 +93,23 @@ syscfg.vals: BLE_ISO: 1 BLE_AUDIO: 1 + BLE_AUDIO_BROADCAST_SINK: 1 + BLE_AUDIO_BROADCAST_SINK_MAX: 2 + BLE_PERIODIC_ADV_SYNC_TRANSFER: 1 + BLE_PERIODIC_ADV_SYNC_BIGINFO_REPORTS: 1 BLE_ROLE_BROADCASTER: 1 - BLE_ISO_MAX_BISES: 1 - BLE_ISO_MAX_BIGS: 1 + BLE_ISO_BROADCAST_SOURCE: 1 + BLE_ISO_BROADCAST_SINK: 1 + BLE_ISO_MAX_BISES: 3 + BLE_ISO_MAX_BIGS: 3 BLE_EXT_ADV: 1 BLE_PHY_2M: 1 BLE_EXT_ADV_MAX_SIZE: 40 BLE_PERIODIC_ADV: 1 BLE_ISO_BROADCAST_SOURCE: 1 - BLE_MULTI_ADV_INSTANCES: 1 + BLE_MULTI_ADV_INSTANCES: 3 + BLE_SVC_AUDIO_BASS_METADATA_MAX_SZ: 256 + BLE_SVC_AUDIO_BASS_SUB_NUM_MAX: 10 OS_MAIN_STACK_SIZE: 512 SHELL_TASK: 0 @@ -110,11 +118,12 @@ syscfg.vals: MSYS_1_BLOCK_COUNT: 100 BLE_MONITOR_RTT: 1 - CONSOLE_RTT: 0 + BLE_MONITOR_RTT_BUFFER_SIZE: 4096 + CONSOLE_RTT: 1 CONSOLE_UART: 0 CONSOLE_UART_FLOW_CONTROL: UART_FLOW_CTL_RTS_CTS - RTT_NUM_BUFFERS_UP: 0 - RTT_NUM_BUFFERS_DOWN: 0 + RTT_NUM_BUFFERS_UP: 1 + RTT_NUM_BUFFERS_DOWN: 1 BLE_L2CAP_COC_MAX_NUM: 5 BLE_L2CAP_SIG_MAX_PROCS: 2 @@ -162,6 +171,6 @@ syscfg.vals: BLE_MESH_RX_SEG_MAX: 13 BLE_MESH_TX_SEG_MSG_COUNT: 2 BLE_MAX_CONNECTIONS: 8 - - - + NRF5340_EMBED_NET_CORE: 1 + BSP_NRF5340_NET_ENABLE: 1 + MYNEWT_DOWNLOADER: nrfjprog diff --git a/nimble/host/audio/services/bass/include/services/bass/ble_audio_svc_bass.h b/nimble/host/audio/services/bass/include/services/bass/ble_audio_svc_bass.h index 431ecb968c..f0949a2e1a 100644 --- a/nimble/host/audio/services/bass/include/services/bass/ble_audio_svc_bass.h +++ b/nimble/host/audio/services/bass/include/services/bass/ble_audio_svc_bass.h @@ -286,6 +286,9 @@ struct ble_svc_audio_bass_operation { /** Number of subgroups */ uint8_t num_subgroups; + /** BIS Synchronisation of subgroups */ + uint32_t bis_sync[BLE_SVC_AUDIO_BASS_SUB_NUM_MAX]; + /** Subgroup entries */ struct ble_svc_audio_bass_subgroup subgroups[BLE_SVC_AUDIO_BASS_SUB_NUM_MAX]; @@ -326,6 +329,10 @@ struct ble_svc_audio_bass_operation { /** BIS Synchronisation of subgroups */ uint32_t bis_sync[BLE_SVC_AUDIO_BASS_SUB_NUM_MAX]; + + /** Subgroup entries */ + struct ble_svc_audio_bass_subgroup + subgroups[BLE_SVC_AUDIO_BASS_SUB_NUM_MAX]; } modify_source; /** diff --git a/nimble/host/audio/services/bass/src/ble_audio_svc_bass.c b/nimble/host/audio/services/bass/src/ble_audio_svc_bass.c index 1c4ff639e1..fe7f6ff5f1 100644 --- a/nimble/host/audio/services/bass/src/ble_audio_svc_bass.c +++ b/nimble/host/audio/services/bass/src/ble_audio_svc_bass.c @@ -23,6 +23,9 @@ #include "../../host/src/ble_att_priv.h" #include "services/bass/ble_audio_svc_bass.h" #include "../../../src/ble_audio_priv.h" +#include "os/os_mbuf.h" +#include "os/util.h" +#include #define BLE_SVC_AUDIO_BASS_CHR_LEN_UNLIMITED (-1) #define BLE_SVC_AUDIO_BASS_RECEIVE_STATE_SRC_ID_NONE 0xFF @@ -201,7 +204,7 @@ ble_svc_audio_bass_receive_state_notify(struct ble_svc_audio_bass_rcv_state_entr { int i; - for (i = 0; i < sizeof(ble_svc_audio_bass_chrs); i++) { + for (i = 0; i < ARRAY_SIZE(ble_svc_audio_bass_chrs); i++) { if (ble_svc_audio_bass_chrs[i].arg == state) { ble_gatts_chr_updated(*ble_svc_audio_bass_chrs[i].val_handle); return 0; @@ -217,7 +220,7 @@ ble_svc_audio_bass_receive_state_find_by_source_id(struct ble_svc_audio_bass_rcv { int i; - for (i = 0; i < sizeof(receiver_states); i++) { + for (i = 0; i < ARRAY_SIZE(receiver_states); i++) { if (receiver_states[i].source_id == source_id) { *out_state = &receiver_states[i]; return 0; @@ -232,7 +235,7 @@ ble_svc_audio_bass_receive_state_find_free(struct ble_svc_audio_bass_rcv_state_e { int i; - for (i = 0; i < sizeof(receiver_states); i++) { + for (i = 0; i < ARRAY_SIZE(receiver_states); i++) { if (receiver_states[i].source_id == BLE_SVC_AUDIO_BASS_RECEIVE_STATE_SRC_ID_NONE) { *out_state = &receiver_states[i]; return 0; @@ -251,6 +254,10 @@ ble_svc_audio_bass_receive_state_free(struct ble_svc_audio_bass_rcv_state_entry static int ble_svc_audio_bass_remote_scan_stopped(uint8_t *data, uint16_t data_len, uint16_t conn_handle) { + if (data_len > 1) { + return BLE_ATT_ERR_WRITE_REQ_REJECTED; + } + struct ble_audio_event ev = { .type = BLE_AUDIO_EVENT_BASS_REMOTE_SCAN_STOPPED }; @@ -264,6 +271,10 @@ ble_svc_audio_bass_remote_scan_stopped(uint8_t *data, uint16_t data_len, uint16_ static int ble_svc_audio_bass_remote_scan_started(uint8_t *data, uint16_t data_len, uint16_t conn_handle) { + if (data_len > 1) { + return BLE_ATT_ERR_WRITE_REQ_REJECTED; + } + struct ble_audio_event ev = { .type = BLE_AUDIO_EVENT_BASS_REMOTE_SCAN_STARTED }; @@ -274,6 +285,28 @@ ble_svc_audio_bass_remote_scan_started(uint8_t *data, uint16_t data_len, uint16_ return 0; } +static int +check_bis_sync(uint16_t num_subgroups, const uint32_t *bis_sync_list) +{ + uint32_t bis_sync_mask = 0; + int i; + int j; + + for (i = 0; i < num_subgroups; i++) { + if (bis_sync_list[i] != 0xFFFFFFFF) { + for (j = 0; j < num_subgroups; j++) { + if (bis_sync_list[i] & bis_sync_mask) { + return BLE_HS_EINVAL; + } + + bis_sync_mask |= bis_sync_list[i]; + } + } + } + + return 0; +} + static int ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_handle) { @@ -298,10 +331,23 @@ ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_ha operation.op = BLE_SVC_AUDIO_BASS_OPERATION_ADD_SOURCE; operation.conn_handle = conn_handle; + offset++; operation.add_source.adv_addr.type = data[offset++]; + if (operation.add_source.adv_addr.type < 0 || + operation.add_source.adv_addr.type > 3) { + rc = BLE_HS_EINVAL; + ev.bass_operation_status.status = BLE_HS_EINVAL; + goto done; + } memcpy(operation.add_source.adv_addr.val, &data[offset], 6); offset += 6; operation.add_source.adv_sid = data[offset++]; + if (operation.add_source.adv_sid < 0 || + operation.add_source.adv_sid > 0xF) { + rc = BLE_HS_EINVAL; + ev.bass_operation_status.status = BLE_HS_EINVAL; + goto done; + } operation.add_source.broadcast_id = get_le24(&data[offset]); offset += 3; operation.add_source.pa_sync = data[offset++]; @@ -326,6 +372,7 @@ ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_ha ev.bass_operation_status.status = BLE_HS_EREJECT; goto done; } + operation.add_source.bis_sync[i] = get_le32(&data[offset]); operation.add_source.subgroups[i].bis_sync_state = get_le32(&data[offset]); offset += 4; operation.add_source.subgroups[i].metadata_length = data[offset++]; @@ -339,6 +386,18 @@ ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_ha offset += operation.add_source.subgroups[i].metadata_length; data_len -= operation.add_source.subgroups[i].metadata_length; } + if (check_bis_sync(operation.add_source.num_subgroups, + operation.add_source.bis_sync)) { + rc = BLE_HS_EINVAL; + ev.bass_operation_status.status = BLE_HS_EREJECT; + goto done; + } + + if (data_len != 0) { + rc = BLE_ATT_ERR_WRITE_REQ_REJECTED; + ev.bass_operation_status.status = BLE_HS_EREJECT; + goto done; + } source_id_new = ble_svc_audio_bass_get_new_source_id(); operation.add_source.source_id = source_id_new; @@ -385,9 +444,11 @@ ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_ha ev.bass_operation_status.source_id = rcv_state->source_id; rcv_state->state.source_addr.type = operation.add_source.adv_addr.type; - memcpy(&rcv_state->state.source_addr.type, operation.add_source.adv_addr.val, 6); + memcpy(&rcv_state->state.source_addr.val, operation.add_source.adv_addr.val, 6); rcv_state->state.source_adv_sid = operation.add_source.adv_sid; rcv_state->state.broadcast_id = operation.add_source.broadcast_id; + rcv_state->state.pa_sync_state = operation.add_source.pa_sync; + rcv_state->state.num_subgroups = operation.add_source.num_subgroups; for (i = 0; i < operation.add_source.num_subgroups; i++) { metadata_ptr = os_memblock_get(&ble_audio_svc_bass_metadata_pool); @@ -415,28 +476,6 @@ ble_svc_audio_bass_add_source(uint8_t *data, uint16_t data_len, uint16_t conn_ha return rc; } -static int -check_bis_sync(uint16_t num_subgroups, const uint32_t *bis_sync_list) -{ - uint32_t bis_sync_mask = 0; - int i; - int j; - - for (i = 0; i < num_subgroups; i++) { - if (bis_sync_list[i] != 0xFFFFFFFF) { - for (j = 0; j < num_subgroups; j++) { - if (bis_sync_list[i] & bis_sync_mask) { - return BLE_HS_EINVAL; - } - - bis_sync_mask |= bis_sync_list[i]; - } - } - } - - return 0; -} - static int ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn_handle) { @@ -449,6 +488,7 @@ ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn .status = 0 } }; + uint8_t *metadata_ptr; uint8_t offset = 0; int rc = 0; int i; @@ -458,6 +498,7 @@ ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn operation.op = BLE_SVC_AUDIO_BASS_OPERATION_MODIFY_SOURCE; operation.conn_handle = conn_handle; + offset++; operation.modify_source.source_id = data[offset++]; ble_svc_audio_bass_receive_state_find_by_source_id(&rcv_state, @@ -477,8 +518,7 @@ ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn operation.modify_source.pa_interval = get_le16(&data[offset]); offset += 2; - operation.modify_source.num_subgroups = get_le16(&data[offset]); - offset += 2; + operation.modify_source.num_subgroups = data[offset++]; data_len -= offset; if (data_len < operation.modify_source.num_subgroups * sizeof(uint32_t)) { @@ -489,7 +529,18 @@ ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn for (i = 0; i < operation.modify_source.num_subgroups; i++) { operation.modify_source.bis_sync[i] = get_le32(&data[offset]); + rcv_state->state.subgroups[i].bis_sync_state = operation.modify_source.bis_sync[i]; offset += 4; + operation.modify_source.subgroups[i].metadata_length = data[offset++]; + data_len -= 5; + if (data_len < operation.modify_source.subgroups[i].metadata_length) { + rc = BLE_ATT_ERR_WRITE_REQ_REJECTED; + ev.bass_operation_status.status = BLE_HS_EREJECT; + goto done; + } + operation.modify_source.subgroups[i].metadata = &data[offset]; + offset += operation.modify_source.subgroups[i].metadata_length; + data_len -= operation.modify_source.subgroups[i].metadata_length; } if (check_bis_sync(operation.modify_source.num_subgroups, @@ -509,12 +560,28 @@ ble_svc_audio_bass_modify_source(uint8_t *data, uint16_t data_len, uint16_t conn } ev.bass_operation_status.source_id = operation.modify_source.source_id; + rcv_state->source_id = operation.modify_source.source_id; + rcv_state->state.pa_sync_state = operation.modify_source.pa_sync; + + for (i = 0; i < operation.modify_source.num_subgroups; i++) { + metadata_ptr = os_memblock_get(&ble_audio_svc_bass_metadata_pool); + if (!metadata_ptr) { + rc = BLE_HS_ENOMEM; + ev.bass_operation_status.status = BLE_HS_ENOMEM; + goto done; + } + rcv_state->state.subgroups[i].metadata_length = operation.modify_source.subgroups[i].metadata_length; + memcpy(metadata_ptr, operation.modify_source.subgroups[i].metadata, + min(operation.modify_source.subgroups[i].metadata_length, + MYNEWT_VAL(BLE_SVC_AUDIO_BASS_METADATA_MAX_SZ))); + + rcv_state->state.subgroups[i].metadata = metadata_ptr; + } done: if (!rc) { rc = ble_svc_audio_bass_receive_state_notify(rcv_state); ev.bass_operation_status.status = rc; - goto done; } ble_audio_event_listener_call(&ev); @@ -530,7 +597,11 @@ ble_svc_audio_bass_set_broadcast_code(uint8_t *data, uint16_t data_len, uint16_t .type = BLE_AUDIO_EVENT_BASS_BROADCAST_CODE_SET, }; - ev.bass_set_broadcast_code.source_id = data[0]; + if (data_len != 18) { + return BLE_ATT_ERR_WRITE_REQ_REJECTED; + } + + ev.bass_set_broadcast_code.source_id = data[1]; ble_svc_audio_bass_receive_state_find_by_source_id(&rcv_state, ev.bass_set_broadcast_code.source_id); @@ -538,7 +609,10 @@ ble_svc_audio_bass_set_broadcast_code(uint8_t *data, uint16_t data_len, uint16_t return BLE_SVC_AUDIO_BASS_ERR_INVALID_SOURCE_ID; } - memcpy(ev.bass_set_broadcast_code.broadcast_code, &data[1], BLE_AUDIO_BROADCAST_CODE_SIZE); + memcpy(ev.bass_set_broadcast_code.broadcast_code, &data[2], BLE_AUDIO_BROADCAST_CODE_SIZE); + rcv_state->state.big_encryption = BLE_SVC_AUDIO_BASS_BIG_ENC_DECRYPTING; + + ble_svc_audio_bass_receive_state_notify(rcv_state); ble_audio_event_listener_call(&ev); @@ -549,7 +623,7 @@ static int ble_svc_audio_bass_remove_source(uint8_t *data, uint16_t data_len, uint16_t conn_handle) { struct ble_audio_event ev = { - .type = BLE_AUDIO_EVENT_BASS_BROADCAST_CODE_SET, + .type = BLE_AUDIO_EVENT_BASS_OPERATION_STATUS, .bass_operation_status = { .op = BLE_AUDIO_EVENT_BASS_SOURCE_REMOVED, .status = 0 @@ -557,10 +631,12 @@ ble_svc_audio_bass_remove_source(uint8_t *data, uint16_t data_len, uint16_t conn }; struct ble_svc_audio_bass_rcv_state_entry *rcv_state = NULL; struct ble_svc_audio_bass_operation operation; + uint16_t chr_val; int rc = 0; int i; - ev.bass_set_broadcast_code.source_id = data[0]; + ev.bass_operation_status.source_id = data[1]; + operation.op = BLE_SVC_AUDIO_BASS_OPERATION_REMOVE_SOURCE; ble_svc_audio_bass_receive_state_find_by_source_id(&rcv_state, ev.bass_operation_status.source_id); @@ -570,6 +646,12 @@ ble_svc_audio_bass_remove_source(uint8_t *data, uint16_t data_len, uint16_t conn goto done; } + if (rcv_state->state.pa_sync_state == BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_SYNCED) { + return rc; + } + + chr_val = rcv_state->chr_val; + operation.remove_source.source_id = ev.bass_operation_status.source_id; operation.conn_handle = conn_handle; if (accept_fn.ctrl_point_ev_fn) { @@ -587,12 +669,12 @@ ble_svc_audio_bass_remove_source(uint8_t *data, uint16_t data_len, uint16_t conn memset(rcv_state, 0, sizeof(*rcv_state)); rcv_state->source_id = BLE_SVC_AUDIO_BASS_RECEIVE_STATE_SRC_ID_NONE; + rcv_state->chr_val = chr_val; done: if (!rc) { rc = ble_svc_audio_bass_receive_state_notify(rcv_state); ev.bass_operation_status.status = rc; - goto done; } ble_audio_event_listener_call(&ev); @@ -604,8 +686,9 @@ static struct ble_svc_audio_bass_ctrl_point_handler * ble_svc_audio_bass_find_handler(uint8_t opcode) { int i; + uint8_t handlers_size = ARRAY_SIZE(ble_svc_audio_bass_ctrl_point_handlers); - for (i = 0; i < sizeof(ble_svc_audio_bass_ctrl_point_handlers); i++) { + for (i = 0; i < handlers_size; i++) { if (ble_svc_audio_bass_ctrl_point_handlers[i].op_code == opcode) { return &ble_svc_audio_bass_ctrl_point_handlers[i]; } @@ -618,12 +701,16 @@ static int ble_svc_audio_bass_ctrl_point_write_access(struct ble_gatt_access_ctxt *ctxt, uint16_t conn_handle) { struct ble_svc_audio_bass_ctrl_point_handler *handler; + uint16_t mbuf_len = OS_MBUF_PKTLEN(ctxt->om); + uint8_t opcode; + uint8_t data[MYNEWT_VAL(BLE_SVC_AUDIO_BASS_METADATA_MAX_SZ)]; - uint8_t opcode = ctxt->om->om_data[0]; + os_mbuf_copydata(ctxt->om, 0, mbuf_len, data); + opcode = data[0]; handler = ble_svc_audio_bass_find_handler(opcode); - if (!handler) { + if (handler == NULL) { return BLE_SVC_AUDIO_BASS_ERR_OPCODE_NOT_SUPPORTED; } @@ -633,7 +720,7 @@ ble_svc_audio_bass_ctrl_point_write_access(struct ble_gatt_access_ctxt *ctxt, ui return BLE_ATT_ERR_WRITE_REQ_REJECTED; } - return handler->handler_cb(&ctxt->om->om_data[1], ctxt->om->om_len - 1, conn_handle); + return handler->handler_cb(data, mbuf_len, conn_handle); } static int diff --git a/targets/nordic_pca10095_net-blehci/syscfg.yml b/targets/nordic_pca10095_net-blehci/syscfg.yml index 30f9bcc985..1ed5c3d18b 100644 --- a/targets/nordic_pca10095_net-blehci/syscfg.yml +++ b/targets/nordic_pca10095_net-blehci/syscfg.yml @@ -39,4 +39,7 @@ syscfg.vals: BLE_MULTI_ADV_INSTANCES: 5 BLE_PERIODIC_ADV: 1 BLE_PERIODIC_ADV_SYNC_TRANSFER: 1 - BLE_VERSION: 51 + BLE_PERIODIC_ADV_SYNC_BIGINFO_REPORTS: 1 + BLE_LL_ISO_BROADCASTER: 1 + BLE_LL_ISO: 1 + BLE_VERSION: 54