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.util.Log;
20 
21 import com.android.car.BLEStreamProtos.VersionExchangeProto.BLEVersionExchange;
22 
23 /**
24  * Resolver of version exchanges between this device and a client device.
25  */
26 class BLEVersionExchangeResolver {
27     private static final String TAG = "BLEVersionExchangeResolver";
28 
29     // Currently, only version 1 of the messaging and security supported.
30     private static final int MESSAGING_VERSION = 1;
31     private static final int SECURITY_VERSION = 1;
32 
33     /**
34      * Return whether or not the given version exchange proto has the a version that is currently
35      * supported by this device.
36      *
37      * @param versionExchange The version exchange proto to resolve
38      * @return {@code true} if there is a supported version.
39      */
hasSupportedVersion(BLEVersionExchange versionExchange)40     static boolean hasSupportedVersion(BLEVersionExchange versionExchange) {
41         int minMessagingVersion = versionExchange.getMinSupportedMessagingVersion();
42         int minSecurityVersion = versionExchange.getMinSupportedSecurityVersion();
43 
44         if (Log.isLoggable(TAG, Log.DEBUG)) {
45             Log.d(TAG, "Checking for supported version on (minMessagingVersion: "
46                     + minMessagingVersion + ", minSecurityVersion: " + minSecurityVersion + ")");
47         }
48 
49         // Only one supported version, so ensure the minimum version matches.
50         return minMessagingVersion == MESSAGING_VERSION && minSecurityVersion == SECURITY_VERSION;
51     }
52 
53     /**
54      * Returns a version exchange proto with the maximum and minimum protocol and security versions
55      * this device currently supports.
56      */
makeVersionExchange()57     static BLEVersionExchange makeVersionExchange() {
58         return BLEVersionExchange.newBuilder()
59                 .setMinSupportedMessagingVersion(MESSAGING_VERSION)
60                 .setMaxSupportedMessagingVersion(MESSAGING_VERSION)
61                 .setMinSupportedSecurityVersion(SECURITY_VERSION)
62                 .setMaxSupportedSecurityVersion(SECURITY_VERSION)
63                 .build();
64     }
65 
BLEVersionExchangeResolver()66     private BLEVersionExchangeResolver() {}
67 }
68