Skip to content

Commit

Permalink
nimble/host: Add Broadcast Audio Scan Service
Browse files Browse the repository at this point in the history
BASS initial implementation
  • Loading branch information
KKopyscinski committed Mar 1, 2024
1 parent a04a6d2 commit 313ce36
Show file tree
Hide file tree
Showing 4 changed files with 1,159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* 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_SVC_BASS_
#define H_BLE_AUDIO_SVC_BASS_

#include <stdint.h>
#include "host/audio/ble_audio_bsnk.h"
#include "syscfg/syscfg.h"

#define BLE_SVC_AUDIO_BASS_UUID16 0x184F
#define BLE_SVC_AUDIO_BASS_CHR_UUID16_BAS_CONTROL_POINT 0x2BC7
#define BLE_SVC_AUDIO_BASS_CHR_UUID16_BROADCAST_RECEIVE_STATE 0x2BC8

#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_REMOTE_SCAN_STOPPED 0x00
#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_REMOTE_SCAN_STARTED 0x01
#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_ADD_SOURCE 0x02
#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_MODIFY_SOURCE 0x03
#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_SET_BROADCAST_CODE 0x04
#define BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_REMOVE_SOURCE 0x05

enum ble_svc_audio_bass_big_enc {
BLE_SVC_AUDIO_BASS_BIG_ENC_NOT_ENCRYPTED,
BLE_SVC_AUDIO_BASS_BIG_ENC_BROADCAST_CODE_REQ,
BLE_SVC_AUDIO_BASS_BIG_ENC_DECRYPTING,
BLE_SVC_AUDIO_BASS_BIG_ENC_BAD_CODE
};

enum ble_svc_audio_bass_pa_sync {
BLE_SVC_AUDIO_BASS_PA_SYNC_DO_NOT_SYNC,
BLE_SVC_AUDIO_BASS_PA_SYNC_SYNC_PAST_AVAILABLE,
BLE_SVC_AUDIO_BASS_PA_SYNC_SYNC_PAST_NOT_AVAILABLE
};

enum ble_svc_audio_bass_pa_sync_state {
BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_NOT_SYNCED,
BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_SYNC_INFO_REQ,
BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_SYNCED,
BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_SYNCED_FAILED,
BLE_SVC_AUDIO_BASS_PA_SYNC_STATE_NO_PAST
};

struct ble_svc_audio_bass_subgroup {
uint32_t bis_sync_state;
uint8_t metadata_length;
uint8_t *metadata;
};

struct ble_svc_audio_bass_receiver_state_params {
ble_addr_t source_addr;
uint8_t source_adv_sid;
uint32_t broadcast_id;
uint8_t pa_sync_state;
uint8_t big_encryption;
uint8_t bad_code[BLE_AUDIO_BROADCAST_CODE_SIZE];
uint8_t num_subgroups;
struct ble_svc_audio_bass_subgroup
subgroups[MYNEWT_VAL(BLE_SVC_AUDIO_BASS_MAX_SUBGROUPS_PER_RECV_STATE)];
};

struct ble_svc_audio_bass_receiver_state {
uint8_t source_id;
struct ble_audio_bsnk *bsnk;
struct ble_svc_audio_bass_receiver_state_params params;
};

struct ble_svc_audio_bass_ctrl_point_event {
/**
* Indicates the type of BASS event that occurred. This is one of the
* BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT codes.
*/
uint8_t op;

/**
* A discriminated union containing additional details concerning the BASS Control Point
* event. The 'type' field indicates which member of the union is valid.
*/
union {
/**
* Represents Add Source operation. Valid for the following event
* types:
* o BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_ADD_SOURCE
* Application can accept or reject Add Source operation. If no application callback is set
* and free Receive State characteristic exists operation is automatically accepted.
* If application callback exists and returns 0 accepted. Otherwise, Operation is rejected.
* If operation is accepted by application callback, this callback may select receiver
* state to be filled. If application doesnt select characteristic, BASS Server falls back
* to searching free one. If none is found, operation is rejected.
*/
struct {
/**
* Advertiser Address
*/
ble_addr_t adv_addr;
uint8_t adv_sid;
uint32_t broadcast_id : 24;
uint8_t pa_sync;
uint16_t pa_interval;
uint16_t num_subgroups;
struct ble_svc_audio_bass_subgroup
subgroups[MYNEWT_VAL(BLE_SVC_AUDIO_BASS_MAX_SUBGROUPS_PER_RECV_STATE)];
uint8_t *source_id;
} add_source;

/**newt
* Represents Modify Source operation. Valid for the following event
* types:
* o BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_MODIFY_SOURCE
* Application can accept or reject Add Source operation. If no application callback is set
* or application callback returns 0 operation is automatically accepted.
* If application callback returns non-zero value operation is rejected.
*/
struct {
uint8_t source_id;
uint8_t pa_sync;
uint16_t pa_interval;
uint16_t num_subgroups;
uint32_t bis_sync[MYNEWT_VAL(BLE_SVC_AUDIO_BASS_MAX_SUBGROUPS_PER_RECV_STATE)];
} modify_source;

/**
* Represents Set Broadcast_Code operation. Valid for the following event
* types:
* o BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_SET_BROADCAST_CODE
*/
struct {
uint8_t source_id;
uint8_t broadcast_code[BLE_AUDIO_BROADCAST_CODE_SIZE];
} set_broadcast_code;

/**
* Represents Remove Source operation. Valid for the following event
* types:
* o BLE_SVC_AUDIO_BASS_CTRL_POINT_EVENT_REMOVE_SOURCE
*/
struct {
uint8_t source_id;
} remove_source;
};
};

typedef int ble_svc_audio_bass_ctrl_point_ev_fn(struct ble_svc_audio_bass_ctrl_point_event *event,
void *arg);

int
ble_svc_audio_bass_ctrl_point_cb_set(ble_svc_audio_bass_ctrl_point_ev_fn *cb, void *arg);

int
ble_svc_audio_bass_receive_state_find_free(struct ble_svc_audio_bass_receiver_state **out_state);

int
ble_svc_audio_bass_receive_state_add(const struct ble_svc_audio_bass_receiver_state_params *params,
struct ble_svc_audio_bass_receiver_state **out_state);

int
ble_svc_audio_bass_receive_state_remove(struct ble_svc_audio_bass_receiver_state *state);

#endif /* H_BLE_AUDIO_SVC_BASS_ */
32 changes: 32 additions & 0 deletions nimble/host/audio/services/bass/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

pkg.name: nimble/host/services/audio/bass
pkg.description: Broadcast Audio Scan Service
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- ble
- bluetooth
- pacs
- nimble

pkg.deps:
- nimble/host

pkg.init:
ble_svc_audio_bass_init: 'MYNEWT_VAL(BLE_SVC_AUDIO_BASS_SYSINIT_STAGE)'
Loading

0 comments on commit 313ce36

Please sign in to comment.