Skip to content

Commit

Permalink
nimble/audio: Add initial audio broadcast sink implementation
Browse files Browse the repository at this point in the history
This adds initial implementation of audio broadcast sink.
  • Loading branch information
MariuszSkamra committed Mar 5, 2024
1 parent 8b7eda2 commit 048498a
Show file tree
Hide file tree
Showing 9 changed files with 934 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/btshell/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pkg.deps:
- "@apache-mynewt-core/sys/shell"
- "@apache-mynewt-core/util/parse_arg"
- nimble/host
- nimble/host/audio
- nimble/host/services/gap
- nimble/host/services/gatt
- nimble/host/store/config
Expand Down
35 changes: 35 additions & 0 deletions nimble/host/audio/include/audio/ble_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
* @endcond
*/

/** Basic Audio Announcement Service UUID. */
#define BLE_BASIC_AUDIO_ANNOUNCEMENT_SVC_UUID 0x1851

/** Broadcast Audio Announcement Service UUID. */
#define BLE_BROADCAST_AUDIO_ANNOUNCEMENT_SVC_UUID 0x1852

Expand Down Expand Up @@ -387,6 +390,10 @@
/** LE Audio Codec Supported Channel Count: 8. */
#define BLE_AUDIO_CODEC_SUPPORTED_CHANNEL_COUNT_8 0x0080

#define BLE_AUDIO_BROADCAST_ID_MASK 0xFFFFFF
#define BLE_AUDIO_BROADCAST_BIS_SYNC_NO_PREF 0xFFFFFFFF
#define BLE_AUDIO_BROADCAST_CODE_SIZE 16

/** @} */

/**
Expand Down Expand Up @@ -516,6 +523,9 @@ struct ble_audio_broadcast_name {
/** BLE Audio event: Codec Unregistered */
#define BLE_AUDIO_EVENT_CODEC_UNREGISTERED 2

/** BLE Audio event: Basic Audio Announcement */
#define BLE_AUDIO_EVENT_BASIC_AUDIO_ANNOUNCEMENT 3

/** @} */

/** @brief Broadcast Announcement */
Expand Down Expand Up @@ -551,6 +561,24 @@ struct ble_audio_event_codec_unregistered {
const struct ble_audio_codec_record *record;
};

/** @brief Basic Audio Announcement */
struct ble_audio_event_basic_audio_announcement {
/** Audio Broadcast Sink instance */
struct ble_audio_bsnk *snk;

/** Advertiser transmit power in dBm (127 if unavailable) */
int8_t tx_power;

/** Received signal strength indication in dBm (127 if unavailable) */
int8_t rssi;

/** Broadcast Audio Source Endpoint (BASE) length */
uint8_t base_len;

/** Broadcast Audio Source Endpoint (BASE) */
const uint8_t *base;
};

/**
* Represents a BLE Audio related event. When such an event occurs, the host
* notifies the application by passing an instance of this structure to an
Expand Down Expand Up @@ -588,6 +616,13 @@ struct ble_audio_event {
* Represents a codec registration.
*/
struct ble_audio_event_codec_unregistered codec_unregistered;

/**
* @ref BLE_AUDIO_EVENT_BASIC_AUDIO_ANNOUNCEMENT
*
* Represents a received Basic Audio Announcement.
*/
struct ble_audio_event_basic_audio_announcement basic_audio_announcement;
};
};

Expand Down
195 changes: 195 additions & 0 deletions nimble/host/audio/include/audio/ble_audio_broadcast_sink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* 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_BLE_AUDIO_BROADCAST_SINK_
#define H_BLE_AUDIO_BROADCAST_SINK_

/**
* @file ble_audio_broadcast_sink.h
*
* @brief Bluetooth Low Energy Audio Broadcast Sink API
*
* @defgroup ble_audio_broadcast_sink Bluetooth LE Audio Broadcast Sink
* @ingroup bt_host
* @{
*/

#include <stdint.h>
#include "host/ble_gap.h"
#include "host/ble_iso.h"
#include "ble_audio.h"

/** @brief Periodic Advertisement Sync parameters */
struct ble_audio_broadcast_sink_pa_sync_params {
/** Advertiser Address for the Broadcast Source */
const ble_addr_t *adv_addr;

/** Advertising Set ID */
uint8_t adv_sid;

/** Periodic advertising interval in 1.25 ms units */
uint16_t pa_interval;

/** Retry count. The parameter is used to calculate the Sync Timeout */
uint8_t retry_count;

/** The maximum number of periodic advertising events that controller can
* skip after a successful receive.
*/
uint16_t skip;
};

/**
* @brief Synchronize to Periodic Advertisement
*
* Start receiving the BIGInfo and Broadcast Audio Source Endpoint (BASE)
* reports.
*
* This function initiates Periodic Advertisement Synchronization Procedure.
* The function requires extended scan to be enabled.
*
* @param[in] source_id Audio Broadcast Source ID.
* @param[in] params Periodic Advertisement Sync parameters to use.
*
* @return 0 on success;
* A non-zero value on failure.
*/
int ble_audio_broadcast_sink_pa_sync(
uint8_t source_id,
const struct ble_audio_broadcast_sink_pa_sync_params *params);

/** @brief Periodic Advertisement Sync Transfer parameters */
struct ble_audio_broadcast_sink_pa_sync_recv_params {
/** Connection handle */
uint16_t conn_handle;

/** Periodic advertising interval in 1.25 ms units */
uint16_t pa_interval;

/** Retry count. The parameter is used to calculate the Sync Timeout */
uint8_t retry_count;

/** The maximum number of periodic advertising events that controller can
* skip after a successful receive.
*/
uint16_t skip;
};

/**
* @brief Synchronize to Periodic Advertisement with PAST
*
* Start receiving the BIGInfo and Broadcast Audio Source Endpoint (BASE)
* reports.
*
* This function initiates Periodic Advertisement Sync Transfer (PAST) procedure
* to offload the scanning.
*
* @param[in] source_id Audio Broadcast Source ID.
* @param[in] params Periodic Advertisement Sync parameters to use.
*
* @return 0 on success;
* A non-zero value on failure.
*/
int ble_audio_broadcast_sink_pa_sync_recv(
uint8_t source_id,
const struct ble_audio_broadcast_sink_pa_sync_recv_params *params);

/**
* @brief Terminate or cancel pending Periodic Advertisement Sync
*
* This function terminates active Periodic Advertisement Sync or cancels the
* pending sync.
*
* @param[in] source_id Audio Broadcast Source ID.
*
* @return 0 on success;
* A non-zero value on failure.
*/
int ble_audio_broadcast_sink_pa_sync_term(uint8_t source_id);

/** @brief BIS Sync parameters */
struct ble_audio_broadcast_sink_bis_sync_params {
/** BIS index */
uint8_t bis_index;

/** ISO data received callback */
ble_iso_event_fn *cb;

/** Callback argument */
void *cb_arg;
};

/** @brief Broadcast Sink Sync parameters */
struct ble_audio_broadcast_sink_sync_params {
/** NULL-terminated broadcast code */
const char *broadcast_code;

/** Number of BISes */
uint8_t num_bis;

/** BIS Sync parameters */
struct ble_audio_broadcast_sink_bis_sync_params *params;
};

/**
* @brief Synchronize to Audio Broadcast
*
* This function is used to synchronize to Audio Broadcast to start
* reception of audio data.
*
* @param[in] source_id Audio Broadcast Source ID.
* @param[in] params Broadcast Sink Sync parameters to use.
*
* @return 0 on success;
* BLE_HS_EINVAL if the parameters are invalid;
* BLE_HS_ENOENT if missing PA synchronization;
* BLE_HS_EAGAIN if PA Sync is in progress;
* BLE_HS_EENCRYPT if Broadcast Code is missing;
* BLE_HS_ENOMEM if memory capacity exceeded;
* Other nonzero on error.
*/
int ble_audio_broadcast_sink_sync(
uint8_t source_id,
const struct ble_audio_broadcast_sink_sync_params *params);

/**
* @brief Terminate or cancel pending Audio Broadcast synchronization
*
* This function terminates active Audio Broadcast sync or cancels the
* pending sync.
*
* @param[in] source_id Audio Broadcast Source ID.
*
* @return 0 on success;
* A non-zero value on failure.
*/
int ble_audio_broadcast_sink_sync_term(uint8_t source_id);

/**
* @brief Initialize Broadcast Sink role
*
* This function is restricted to be called by sysinit.
*/
int ble_audio_broadcast_sink_init(void);

/**
* @}
*/

#endif /* H_BLE_AUDIO_BROADCAST_SINK_*/
3 changes: 3 additions & 0 deletions nimble/host/audio/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ pkg.keywords:
pkg.deps:
- nimble
- nimble/host

pkg.init.BLE_AUDIO_BROADCAST_SINK:
ble_audio_broadcast_sink_init: 'MYNEWT_VAL(BLE_AUDIO_BROADCAST_SINK_SYSINIT_STAGE)'
Loading

0 comments on commit 048498a

Please sign in to comment.