1 /* 2 * Copyright (C) 2022 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 android.nearby.multidevices.fastpair.provider 18 19 import android.annotation.TargetApi 20 import android.content.Context 21 import android.nearby.multidevices.fastpair.provider.controller.FastPairProviderSimulatorController 22 import android.nearby.multidevices.fastpair.provider.events.ProviderStatusEvents 23 import android.os.Build 24 import androidx.test.platform.app.InstrumentationRegistry 25 import com.google.android.mobly.snippet.Snippet 26 import com.google.android.mobly.snippet.rpc.AsyncRpc 27 import com.google.android.mobly.snippet.rpc.Rpc 28 29 /** Expose Mobly RPC methods for Python side to simulate fast pair provider role. */ 30 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 31 class FastPairProviderSimulatorSnippet : Snippet { 32 private val context: Context = InstrumentationRegistry.getInstrumentation().context 33 private val fastPairProviderSimulatorController = FastPairProviderSimulatorController(context) 34 35 /** Sets up the Fast Pair provider simulator. */ 36 @AsyncRpc(description = "Sets up FP provider simulator.") setupProviderSimulatornull37 fun setupProviderSimulator(callbackId: String) { 38 fastPairProviderSimulatorController.setupProviderSimulator(ProviderStatusEvents(callbackId)) 39 } 40 41 /** 42 * Starts model id advertising for scanning and initial pairing. 43 * 44 * @param callbackId the callback ID corresponding to the 45 * [FastPairProviderSimulatorSnippet#startProviderSimulator] call that started the scanning. 46 * @param modelId a 3-byte hex string for seeker side to recognize the device (ex: 0x00000C). 47 * @param antiSpoofingKeyString a public key for registered headsets. 48 */ 49 @AsyncRpc(description = "Starts model id advertising for scanning and initial pairing.") startModelIdAdvertisingnull50 fun startModelIdAdvertising( 51 callbackId: String, 52 modelId: String, 53 antiSpoofingKeyString: String 54 ) { 55 fastPairProviderSimulatorController.startModelIdAdvertising( 56 modelId, 57 antiSpoofingKeyString, 58 ProviderStatusEvents(callbackId) 59 ) 60 } 61 62 /** Tears down the Fast Pair provider simulator. */ 63 @Rpc(description = "Tears down FP provider simulator.") teardownProviderSimulatornull64 fun teardownProviderSimulator() { 65 fastPairProviderSimulatorController.teardownProviderSimulator() 66 } 67 68 /** Gets BLE mac address of the Fast Pair provider simulator. */ 69 @Rpc(description = "Gets BLE mac address of the Fast Pair provider simulator.") getBluetoothLeAddressnull70 fun getBluetoothLeAddress(): String { 71 return fastPairProviderSimulatorController.getProviderSimulatorBleAddress() 72 } 73 74 /** Gets the latest account key received on the Fast Pair provider simulator */ 75 @Rpc(description = "Gets the latest account key received on the Fast Pair provider simulator.") getLatestReceivedAccountKeynull76 fun getLatestReceivedAccountKey(): String? { 77 return fastPairProviderSimulatorController.getLatestReceivedAccountKey() 78 } 79 } 80