1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.trust;
18 
19 import android.annotation.NonNull;
20 
21 import com.android.car.BLEStreamProtos.BLEOperationProto.OperationType;
22 
23 /**
24  * Handles the streaming of BLE messages to a specific {@link android.bluetooth.BluetoothDevice}.
25  *
26  * <p>This stream will handle if messages to a particular peripheral need to be split into
27  * multiple messages or if the messages can be sent all at once. Internally, it will have its own
28  * protocol for how the split messages are structured.
29  */
30 interface BleMessageStream {
31    /** Registers the given callback to be notified of various events within the stream. */
registerCallback(@onNull BleMessageStreamCallback callback)32     void registerCallback(@NonNull BleMessageStreamCallback callback);
33 
34     /** Unregisters the given callback from being notified of stream events. */
unregisterCallback(@onNull BleMessageStreamCallback callback)35     void unregisterCallback(@NonNull BleMessageStreamCallback callback);
36 
37     /** Sets the maximum size of a message that can be sent. */
setMaxWriteSize(int maxWriteSize)38     void setMaxWriteSize(int maxWriteSize);
39 
40     /** Returns the maximum size of a message that can be sent. */
getMaxWriteSize()41     int getMaxWriteSize();
42 
43     /**
44      * Writes the given message to the write characteristic set on this stream to the
45      * {@code BleutoothDevice} associated with this stream.
46      *
47      * <p>The given message will adhere to the max write size set on this stream. If the message is
48      * larger than this size, then this stream should take the appropriate actions necessary to
49      * chunk the message to the device so that no parts of the message is dropped.
50      *
51      * <p>If there was an error, then this stream will notify the [callback] of this stream via a
52      * call to its {@code onWriteMessageError} method.
53      *
54      * @param message The message to send.
55      * @param operationType The {@link OperationType} of this message.
56      * @param isPayloadEncrypted {@code true} if the message to send has been encrypted.
57      */
writeMessage(@onNull byte[] message, @NonNull OperationType operationType, boolean isPayloadEncrypted)58     void writeMessage(@NonNull byte[] message, @NonNull OperationType operationType,
59             boolean isPayloadEncrypted);
60 }
61