1 /*
2  * Copyright (C) 2020 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.internal.telephony.d2d;
18 
19 import android.annotation.NonNull;
20 
21 import java.util.Set;
22 
23 /**
24  * Base definition for a device-to-device communication protocol.
25  */
26 public interface TransportProtocol {
27 
28     /**
29      * Callbacks from the {@link TransportProtocol} to the {@link Communicator} which indicates
30      * important events like protocol negotiation status as well as incoming messages.
31      */
32     public interface Callback {
33         /**
34          * The {@link TransportProtocol} calls this method when protocol negotiation has completed
35          * successfully.
36          * @param protocol The protocol which succeeded (should be {@code this}).
37          */
onNegotiationSuccess(@onNull TransportProtocol protocol)38         void onNegotiationSuccess(@NonNull TransportProtocol protocol);
39 
40         /**
41          * The {@link TransportProtocol} calls this method when protocol negotiation has failed.
42          * @param protocol The protocol which failed (should be {@code this}).
43          */
onNegotiationFailed(@onNull TransportProtocol protocol)44         void onNegotiationFailed(@NonNull TransportProtocol protocol);
45 
46         /**
47          * The {@link TransportProtocol} calls this method when the protocol has received incoming
48          * messages.
49          * @param messages The received messages.
50          */
onMessagesReceived(@onNull Set<Communicator.Message> messages)51         void onMessagesReceived(@NonNull Set<Communicator.Message> messages);
52     }
53 
54     /**
55      * Called by the {@link Communicator} to register for callbacks regarding the transport's
56      * progress.
57      * @param callback the callback to use.
58      */
setCallback(Callback callback)59     void setCallback(Callback callback);
60 
61     /**
62      * Called by {@link Communicator} when negotiation of device-to-device communication using a
63      * protocol should be started.
64      */
startNegotiation()65     void startNegotiation();
66 
67     /**
68      * Called by {@link Communicator} when a message should be sent using device-to-device
69      * communication.
70      * @param messages the messages to send via the transport.
71      */
sendMessages(Set<Communicator.Message> messages)72     void sendMessages(Set<Communicator.Message> messages);
73 
74     /**
75      * Forces this transport to be in a negotiated state.
76      */
forceNegotiated()77     void forceNegotiated();
78 
79     /**
80      * Forces this transport to be in a non-negotiated state.
81      */
forceNotNegotiated()82     void forceNotNegotiated();
83 }
84