• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.bluetooth.btservice;
18 
19 import com.android.internal.annotations.GuardedBy;
20 import com.android.internal.annotations.VisibleForTesting;
21 
22 public class BluetoothHciVendorSpecificNativeInterface {
23     private static final String TAG =
24             BluetoothHciVendorSpecificNativeInterface.class.getSimpleName();
25 
26     @GuardedBy("INSTANCE_LOCK")
27     private static BluetoothHciVendorSpecificNativeInterface sInstance;
28 
29     private static final Object INSTANCE_LOCK = new Object();
30 
31     /** Get singleton instance. */
getInstance()32     public static BluetoothHciVendorSpecificNativeInterface getInstance() {
33         synchronized (INSTANCE_LOCK) {
34             if (sInstance == null) {
35                 sInstance = new BluetoothHciVendorSpecificNativeInterface();
36             }
37             return sInstance;
38         }
39     }
40 
41     /** Set singleton instance. */
42     @VisibleForTesting
setInstance(BluetoothHciVendorSpecificNativeInterface instance)43     static void setInstance(BluetoothHciVendorSpecificNativeInterface instance) {
44         synchronized (INSTANCE_LOCK) {
45             sInstance = instance;
46         }
47     }
48 
49     private BluetoothHciVendorSpecificDispatcher mDispatcher;
50 
init(BluetoothHciVendorSpecificDispatcher dispatcher)51     void init(BluetoothHciVendorSpecificDispatcher dispatcher) {
52         mDispatcher = dispatcher;
53         initNative();
54     }
55 
cleanup()56     void cleanup() {
57         cleanupNative();
58     }
59 
sendCommand(int ocf, byte[] parameters, byte[] cookie)60     void sendCommand(int ocf, byte[] parameters, byte[] cookie) {
61         sendCommandNative(ocf, parameters, cookie);
62     }
63 
64     // Callbacks from the native stack
65 
onCommandStatus(int ocf, int status, byte[] cookie)66     private void onCommandStatus(int ocf, int status, byte[] cookie) {
67         mDispatcher.dispatchCommandStatusOrComplete(
68                 cookie, (cb) -> cb.onCommandStatus(ocf, status));
69     }
70 
onCommandComplete(int ocf, byte[] returnParameters, byte[] cookie)71     private void onCommandComplete(int ocf, byte[] returnParameters, byte[] cookie) {
72         mDispatcher.dispatchCommandStatusOrComplete(
73                 cookie, (cb) -> cb.onCommandComplete(ocf, returnParameters));
74     }
75 
onEvent(int code, byte[] data)76     private void onEvent(int code, byte[] data) {
77         mDispatcher.broadcastEvent(code, (cb) -> cb.onEvent(code, data));
78     }
79 
80     // Native methods that call into the JNI interface
81 
initNative()82     private native void initNative();
83 
cleanupNative()84     private native void cleanupNative();
85 
sendCommandNative(int ocf, byte[] parameters, byte[] cookie)86     private native void sendCommandNative(int ocf, byte[] parameters, byte[] cookie);
87 }
88