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 import android.annotation.Nullable;
21 import android.bluetooth.BluetoothDevice;
22 import android.bluetooth.BluetoothGattCharacteristic;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.util.Log;
26 
27 import com.android.car.BLEStreamProtos.VersionExchangeProto.BLEVersionExchange;
28 
29 /**
30  * Resolver of version exchanges between this device and a client device.
31  */
32 class BLEVersionExchangeResolver {
33     private static final String TAG = "BLEVersionExchangeResolver";
34 
35     // Currently, only version 1 of the messaging and security supported.
36     private static final int MESSAGING_VERSION = 1;
37     private static final int SECURITY_VERSION = 1;
38 
39     /**
40      * Returns a message stream that can be used to send messages to the given
41      * {@link BluetoothDevice} based on the version exchange proto.
42      *
43      * @param versionExchange The version exchange proto to resolve
44      * @param device The remote device to send messages to.
45      * @param readCharacteristic The characteristic the remote device will use to write messages to.
46      *                           This is the characteristic this IHU will read from.
47      * @param writeCharacteristic The characteristic on the remote device that this IHU can write
48      *                            messages to.
49      * @return A stream that can send message or {@code null} if resolution was not possible.
50      */
51     @Nullable
resolveToStream( @onNull BLEVersionExchange versionExchange, @NonNull BluetoothDevice device, @NonNull BlePeripheralManager blePeripheralManager, @NonNull BluetoothGattCharacteristic writeCharacteristic, @NonNull BluetoothGattCharacteristic readCharacteristic)52     static BleMessageStream resolveToStream(
53             @NonNull BLEVersionExchange versionExchange,
54             @NonNull BluetoothDevice device, @NonNull BlePeripheralManager blePeripheralManager,
55             @NonNull BluetoothGattCharacteristic writeCharacteristic,
56             @NonNull BluetoothGattCharacteristic readCharacteristic) {
57         int minMessagingVersion = versionExchange.getMinSupportedMessagingVersion();
58         int minSecurityVersion = versionExchange.getMinSupportedSecurityVersion();
59 
60         if (Log.isLoggable(TAG, Log.DEBUG)) {
61             Log.d(TAG, "Checking for supported version on (minMessagingVersion: "
62                     + minMessagingVersion + ", minSecurityVersion: " + minSecurityVersion + ")");
63         }
64 
65         // Only one supported version, so ensure the minimum version matches.
66         if (minMessagingVersion == MESSAGING_VERSION && minSecurityVersion == SECURITY_VERSION) {
67             return new BleMessageStreamV1(
68                     new Handler(Looper.getMainLooper()),
69                     blePeripheralManager,
70                     device,
71                     writeCharacteristic,
72                     readCharacteristic);
73         }
74 
75         return null;
76     }
77 
78     /**
79      * Returns a version exchange proto with the maximum and minimum protocol and security versions
80      * this device currently supports.
81      */
82     @NonNull
makeVersionExchange()83     static BLEVersionExchange makeVersionExchange() {
84         return BLEVersionExchange.newBuilder()
85                 .setMinSupportedMessagingVersion(MESSAGING_VERSION)
86                 .setMaxSupportedMessagingVersion(MESSAGING_VERSION)
87                 .setMinSupportedSecurityVersion(SECURITY_VERSION)
88                 .setMaxSupportedSecurityVersion(SECURITY_VERSION)
89                 .build();
90     }
91 
BLEVersionExchangeResolver()92     private BLEVersionExchangeResolver() {}
93 }
94