summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothLeBroadcastReceiveState.java
blob: 70a2add80e75035711939dbfdce7f1aca4420e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed 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.
 */

package android.bluetooth;

import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * The {@link BluetoothLeBroadcastReceiveState} is used by the BASS server to expose information
 * about a Broadcast Source.
 *
 * It represents the current synchronization state of the server to
 * a PA and/or a BIG containing one or more subgroups containing one or more BISes
 * transmitted by that Broadcast Source. The Broadcast Receive State characteristic is also
 * used to inform clients whether the server has detected that the BIS is encrypted, whether
 * the server requires a Broadcast_Code, and whether the server is decrypting the BIS.
 *
 * @hide
 */
@SystemApi
public final class BluetoothLeBroadcastReceiveState implements Parcelable {
    /**
     * Periodic Advertising Synchronization state.
     *
     * <p>Periodic Advertising (PA) enables the LE Audio Broadcast Assistant to discover broadcast
     * audio streams as well as the audio stream configuration on behalf of an LE Audio Broadcast
     * Sink. This information can then be transferred to the LE Audio Broadcast Sink using the
     * Periodic Advertising Synchronization Transfer (PAST) procedure.
     *
     * @hide
     */
    @IntDef(prefix = "PA_SYNC_STATE_",
            value = {
                    PA_SYNC_STATE_IDLE,
                    PA_SYNC_STATE_SYNCINFO_REQUEST,
                    PA_SYNC_STATE_SYNCHRONIZED,
                    PA_SYNC_STATE_FAILED_TO_SYNCHRONIZE,
                    PA_SYNC_STATE_NO_PAST
            })
    @Retention(RetentionPolicy.SOURCE)
    public @interface PaSyncState {}

    /**
     * Indicates that the Broadcast Sink is not synchronized with the Periodic Advertisements (PA)
     *
     * @hide
     */
    @SystemApi
    public static final int PA_SYNC_STATE_IDLE = 0;

    /**
     * Indicates that the Broadcast Sink requested the Broadcast Assistant to synchronize with the
     * Periodic Advertisements (PA).
     *
     * <p>This is also known as scan delegation or scan offloading.
     *
     * @hide
     */
    @SystemApi
    public static final int PA_SYNC_STATE_SYNCINFO_REQUEST = 1;

    /**
     * Indicates that the Broadcast Sink is synchronized with the Periodic Advertisements (PA).
     *
     * @hide
     */
    @SystemApi
    public static final int PA_SYNC_STATE_SYNCHRONIZED = 2;

    /**
     * Indicates that the Broadcast Sink was unable to synchronize with the Periodic Advertisements
     * (PA).
     *
     * @hide
     */
    @SystemApi
    public static final int PA_SYNC_STATE_FAILED_TO_SYNCHRONIZE = 3;

    /**
     * Indicates that the Broadcast Sink should be synchronized with the Periodic Advertisements
     * (PA) using the Periodic Advertisements Synchronization Transfer (PAST) procedure.
     *
     * @hide
     */
    @SystemApi
    public static final int PA_SYNC_STATE_NO_PAST = 4;

    /**
     * Indicates that the Broadcast Sink synchronization state is invalid.
     *
     * @hide
     */
    public static final int PA_SYNC_STATE_INVALID = 0xFFFF;

    /** @hide */
    @IntDef(
            prefix = "BIG_ENCRYPTION_STATE_",
            value = {
                    BIG_ENCRYPTION_STATE_NOT_ENCRYPTED,
                    BIG_ENCRYPTION_STATE_CODE_REQUIRED,
                    BIG_ENCRYPTION_STATE_DECRYPTING,
                    BIG_ENCRYPTION_STATE_BAD_CODE
            })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BigEncryptionState {}

    /**
     * Indicates that the Broadcast Sink is synchronized with an unencrypted audio stream from a
     * Broadcast Source
     *
     * @hide
     */
    @SystemApi
    public static final int BIG_ENCRYPTION_STATE_NOT_ENCRYPTED = 0;

    /**
     * Indicates that the Broadcast Sink needs a Broadcast Code to synchronize with an audio stream
     * from a Broadcast Source, which was not provided when the audio stream from the Broadcast
     * Source was added.
     *
     * @hide
     */
    @SystemApi
    public static final int BIG_ENCRYPTION_STATE_CODE_REQUIRED = 1;

    /**
     * Indicates that the Broadcast Sink is synchronized with an encrypted audio stream from a
     * Broadcast Source.
     *
     * @hide
     */
    @SystemApi
    public static final int BIG_ENCRYPTION_STATE_DECRYPTING = 2;

    /**
     * Indicates that the Broadcast Sink is unable to decrypt an audio stream from a Broadcast
     * Source due to an incorrect Broadcast Code.
     *
     * @hide
     */
    @SystemApi
    public static final int BIG_ENCRYPTION_STATE_BAD_CODE = 3;

    /**
     * Indicates that the Broadcast Sink encryption state is invalid.
     *
     * @hide
     */
    public static final int BIG_ENCRYPTION_STATE_INVALID = 0xFFFF;

    private final int mSourceId;
    private final @BluetoothDevice.AddressType int mSourceAddressType;
    private final BluetoothDevice mSourceDevice;
    private final int mSourceAdvertisingSid;
    private final int mBroadcastId;
    private final @PaSyncState int mPaSyncState;
    private final @BigEncryptionState int mBigEncryptionState;
    private final byte[] mBadCode;
    private final int mNumSubgroups;
    private final List<Long> mBisSyncState;
    private final List<BluetoothLeAudioContentMetadata> mSubgroupMetadata;

    /**
     * Constructor to create a read-only {@link BluetoothLeBroadcastReceiveState} instance.
     *
     * @throws NullPointerException if sourceDevice, bisSyncState, or subgroupMetadata is null
     * @throws IllegalArgumentException if sourceID is not [0, 0xFF] or if sourceAddressType
     *      is invalid or if bisSyncState.size() != numSubgroups or if subgroupMetadata.size() !=
     *      numSubgroups or if paSyncState or bigEncryptionState is not recognized bye IntDef
     * @hide
     */
    public BluetoothLeBroadcastReceiveState(@IntRange(from = 0x00, to = 0xFF) int sourceId,
            @BluetoothDevice.AddressType int sourceAddressType,
            @NonNull BluetoothDevice sourceDevice, int sourceAdvertisingSid, int broadcastId,
            @PaSyncState int paSyncState, @BigEncryptionState int bigEncryptionState,
            byte[] badCode, @IntRange(from = 0x00) int numSubgroups,
            @NonNull List<Long> bisSyncState,
            @NonNull List<BluetoothLeAudioContentMetadata> subgroupMetadata) {
        if (sourceId < 0x00 || sourceId > 0xFF) {
            throw new IllegalArgumentException("sourceId " + sourceId
                    + " does not fall between 0x00 and 0xFF");
        }
        Objects.requireNonNull(sourceDevice, "sourceDevice cannot be null");
        if (sourceAddressType == BluetoothDevice.ADDRESS_TYPE_UNKNOWN) {
            throw new IllegalArgumentException("sourceAddressType cannot be ADDRESS_TYPE_UNKNOWN");
        }
        if (sourceAddressType != BluetoothDevice.ADDRESS_TYPE_RANDOM
                && sourceAddressType != BluetoothDevice.ADDRESS_TYPE_PUBLIC) {
            throw new IllegalArgumentException("sourceAddressType " + sourceAddressType
                    + " is invalid");
        }
        Objects.requireNonNull(bisSyncState, "bisSyncState cannot be null");
        if (bisSyncState.size() != numSubgroups) {
            throw new IllegalArgumentException("bisSyncState.size() " + bisSyncState.size()
                    + " must be equal to numSubgroups " + numSubgroups);
        }
        Objects.requireNonNull(subgroupMetadata, "subgroupMetadata cannot be null");
        if (subgroupMetadata.size() != numSubgroups) {
            throw new IllegalArgumentException("subgroupMetadata.size()  "
                    + subgroupMetadata.size() + " must be equal to numSubgroups " + numSubgroups);
        }
        if (paSyncState != PA_SYNC_STATE_IDLE && paSyncState != PA_SYNC_STATE_SYNCINFO_REQUEST
                && paSyncState != PA_SYNC_STATE_FAILED_TO_SYNCHRONIZE
                && paSyncState != PA_SYNC_STATE_NO_PAST && paSyncState != PA_SYNC_STATE_INVALID) {
            throw new IllegalArgumentException("unrecognized paSyncState " + paSyncState);
        }
        if (bigEncryptionState != BIG_ENCRYPTION_STATE_NOT_ENCRYPTED
                && bigEncryptionState != BIG_ENCRYPTION_STATE_CODE_REQUIRED
                && bigEncryptionState != BIG_ENCRYPTION_STATE_DECRYPTING
                && bigEncryptionState != BIG_ENCRYPTION_STATE_BAD_CODE
                && bigEncryptionState != BIG_ENCRYPTION_STATE_INVALID) {
            throw new IllegalArgumentException("unrecognized bigEncryptionState "
                    + bigEncryptionState);
        }
        if (badCode != null && badCode.length != 16) {
            throw new IllegalArgumentException("badCode must be 16 bytes long of null, but is "
                    + badCode.length + " + bytes long");
        }
        mSourceId = sourceId;
        mSourceAddressType = sourceAddressType;
        mSourceDevice = sourceDevice;
        mSourceAdvertisingSid = sourceAdvertisingSid;
        mBroadcastId = broadcastId;
        mPaSyncState = paSyncState;
        mBigEncryptionState = bigEncryptionState;
        mBadCode = badCode;
        mNumSubgroups = numSubgroups;
        mBisSyncState = bisSyncState;
        mSubgroupMetadata = subgroupMetadata;
    }

    /**
     * Get the source ID assigned by the BASS server
     *
     * Shall be unique for each instance of the Broadcast Receive State characteristic exposed by
     * the server
     *
     * @return source ID assigned by the BASS server
     * @hide
     */
    @SystemApi
    public @IntRange(from = 0x00, to = 0xFF) int getSourceId() {
        return mSourceId;
    }

    /**
     * Get the address type of the Broadcast Source
     *
     * Can be either {@link BluetoothDevice#ADDRESS_TYPE_PUBLIC} or
     * {@link BluetoothDevice#ADDRESS_TYPE_RANDOM
     *
     * @return address type of the Broadcast Source
     * @hide
     */
    @SystemApi
    public @BluetoothDevice.AddressType int getSourceAddressType() {
        return mSourceAddressType;
    }

    /**
     * Get the MAC address of the Broadcast Source, which can be Public Device Address,
     * Random Device Address, Public Identity Address or Random (static) Identity Address
     *
     * @return MAC address of the Broadcast Source
     * @hide
     */
    @SystemApi
    public @NonNull BluetoothDevice getSourceDevice() {
        return mSourceDevice;
    }

    /**
     * Get Advertising_SID subfield of the ADI field of the AUX_ADV_IND PDU or the
     * LL_PERIODIC_SYNC_IND containing the SyncInfo that points to the PA transmitted by the
     * Broadcast Source.
     *
     * @return 1-byte long Advertising_SID of the Broadcast Source
     * @hide
     */
    @SystemApi
    public int getSourceAdvertisingSid() {
        return mSourceAdvertisingSid;
    }

    /**
     * Broadcast_ID of the Broadcast Source
     *
     * @return 3-byte long Broadcast_ID of the Broadcast Source
     * @hide
     */
    @SystemApi
    public int getBroadcastId() {
        return mBroadcastId;
    }

    /**
     * Get the Periodic Advertisement synchronization state between the Broadcast Sink and the
     * Broadcast source
     *
     * Possible values are {@link #PA_SYNC_STATE_IDLE}, {@link #PA_SYNC_STATE_SYNCINFO_REQUEST},
     * {@link #PA_SYNC_STATE_SYNCHRONIZED}, {@link #PA_SYNC_STATE_FAILED_TO_SYNCHRONIZE},
     * {@link #PA_SYNC_STATE_NO_PAST}
     *
     * @return Periodic Advertisement synchronization state
     * @hide
     */
    @SystemApi
    public @PaSyncState int getPaSyncState() {
        return mPaSyncState;
    }

    /**
     * Get the encryption state of a Broadcast Isochronous Group (BIG)
     *
     * Possible values are {@link #BIG_ENCRYPTION_STATE_NOT_ENCRYPTED},
     * {@link #BIG_ENCRYPTION_STATE_CODE_REQUIRED}, {@link #BIG_ENCRYPTION_STATE_DECRYPTING},
     * {@link #BIG_ENCRYPTION_STATE_DECRYPTING}, and {@link #BIG_ENCRYPTION_STATE_BAD_CODE}
     *
     * @return encryption state of a Broadcast Isochronous Group (BIG)
     * @hide
     */
    @SystemApi
    public @BigEncryptionState int getBigEncryptionState() {
        return mBigEncryptionState;
    }

    /**
     * If {@link #getBigEncryptionState()} returns {@link #BIG_ENCRYPTION_STATE_BAD_CODE}, this
     * method returns the value of the incorrect 16-octet Broadcast Code that fails to decrypt
     * an audio stream from a Broadcast Source.
     *
     * @return 16-octet Broadcast Code, or null if {@link #getBigEncryptionState()} does not return
     * {@link #BIG_ENCRYPTION_STATE_BAD_CODE}
     * @hide
     */
    @SystemApi
    public @Nullable byte[] getBadCode() {
        return mBadCode;
    }

    /**
     * Get number of Broadcast subgroups being added to this sink
     *
     * @return number of Broadcast subgroups being added to this sink
     */
    public int getNumSubgroups() {
        return mNumSubgroups;
    }

    /**
     * Get a list of bitfield on whether a Broadcast Isochronous Stream (BIS) is synchronized
     * between the sink and source
     *
     * The number of items in the returned list is the same as {@link #getNumSubgroups()}. For each
     * subgroup, at most 31 BISes are available and their synchronization state is indicated by its
     * bit value at the particular offset (i.e. Bit 0-30 = BIS_index[1-31])
     *
     * For example, if (BisSyncState & 0b1 << 5) != 0, BIS 5 is synchronized between source and sync
     *
     * There is a special case, 0xFFFFFFFF to indicate Broadcast Sink failed to synchronize to
     * a particular subgroup
     *
     * @return a list of bitfield on whether a Broadcast Isochronous Stream (BIS) is synchronized
     * between the sink and source
     * @hide
     */
    @SystemApi
    public @NonNull List<Long> getBisSyncState() {
        return mBisSyncState;
    }

    /**
     * Get metadata for every subgroup added to this Broadcast Sink
     *
     * The number of items in the returned list is the same as {@link #getNumSubgroups()}.
     *
     * @return metadata for every subgroup added to this Broadcast Sink
     * @hide
     */
    @SystemApi
    public @NonNull List<BluetoothLeAudioContentMetadata> getSubgroupMetadata() {
        return mSubgroupMetadata;
    }

    /**
     * {@inheritDoc}
     * @hide
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * {@inheritDoc}
     * @hide
     */
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mSourceId);
        out.writeInt(mSourceAddressType);
        out.writeTypedObject(mSourceDevice, 0);
        out.writeInt(mSourceAdvertisingSid);
        out.writeInt(mBroadcastId);
        out.writeInt(mPaSyncState);
        out.writeInt(mBigEncryptionState);

        if (mBadCode != null) {
            out.writeInt(mBadCode.length);
            out.writeByteArray(mBadCode);
        } else {
            // -1 indicates that there is no "bad broadcast code"
            out.writeInt(-1);
        }
        out.writeInt(mNumSubgroups);
        out.writeList(mBisSyncState);
        out.writeTypedList(mSubgroupMetadata);
    }

    /**
     * A {@link Parcelable.Creator} to create {@link BluetoothLeBroadcastReceiveState} from parcel.
     * @hide
     */
    @SystemApi
    public static final @NonNull Parcelable.Creator<BluetoothLeBroadcastReceiveState> CREATOR =
            new Parcelable.Creator<BluetoothLeBroadcastReceiveState>() {
                public @NonNull BluetoothLeBroadcastReceiveState createFromParcel(
                        @NonNull Parcel in) {
                    final int sourceId = in.readInt();
                    final int sourceAddressType = in.readInt();
                    final BluetoothDevice sourceDevice =
                            in.readTypedObject(BluetoothDevice.CREATOR);
                    final int sourceAdvertisingSid = in.readInt();
                    final int broadcastId = in.readInt();
                    final int paSyncState = in.readInt();
                    final int bigEncryptionState = in.readInt();
                    final int badCodeLen = in.readInt();
                    byte[] badCode = null;

                    if (badCodeLen != -1) {
                        badCode = new byte[badCodeLen];
                        if (badCodeLen > 0) {
                            in.readByteArray(badCode);
                        }
                    }
                    final byte numSubGroups = in.readByte();
                    final List<Long> bisSyncState =
                            in.readArrayList(Long.class.getClassLoader(), Long.class);
                    final List<BluetoothLeAudioContentMetadata> subgroupMetadata =
                            new ArrayList<>();
                    in.readTypedList(subgroupMetadata, BluetoothLeAudioContentMetadata.CREATOR);

                    return new BluetoothLeBroadcastReceiveState(
                            sourceId,
                            sourceAddressType,
                            sourceDevice,
                            sourceAdvertisingSid,
                            broadcastId,
                            paSyncState,
                            bigEncryptionState,
                            badCode,
                            numSubGroups,
                            bisSyncState,
                            subgroupMetadata);
                }

                public @NonNull BluetoothLeBroadcastReceiveState[] newArray(int size) {
                    return new BluetoothLeBroadcastReceiveState[size];
                }
            };
}