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 package com.android.server.uwb.indev;
17 
18 import android.annotation.NonNull;
19 import android.util.Log;
20 
21 
22 import com.android.server.uwb.data.UwbRangingData;
23 import com.android.server.uwb.data.UwbVendorUciResponse;
24 import com.android.server.uwb.info.UwbPowerStats;
25 
26 import com.google.uwb.support.base.Params;
27 import com.google.uwb.support.fira.FiraControleeParams;
28 
29 import java.util.Arrays;
30 
31 /**
32  * Uwb Service Core placeholder
33  */
34 public class UwbServiceCore implements IUwbServiceListener {
35     private static final String TAG = UwbServiceCore.class.getSimpleName();
36 
37     protected IUwbServiceListener mUwbServiceListener;
38     private long mUwbServicePtr;
39 
UwbServiceCore()40     public UwbServiceCore() {
41         System.loadLibrary("uwb_adaptation_jni");
42         nativeInit();
43     }
44 
getUwbServicePtr()45     public long getUwbServicePtr() {
46         return mUwbServicePtr;
47     }
48 
setUwbServiceListener(IUwbServiceListener uwbServiceListener)49     public void setUwbServiceListener(IUwbServiceListener uwbServiceListener) {
50         mUwbServiceListener = uwbServiceListener;
51     }
52 
53     /**
54      * Service reset notification
55      *
56      * @param success true if service reset was successful
57      */
onServiceResetReceived(boolean success)58     public void onServiceResetReceived(boolean success) {
59         Log.d(TAG, "onServiceResetReceived(" + success + ")");
60         mUwbServiceListener.onServiceResetReceived(success);
61     }
62 
63     /**
64      * Device status changed notification
65      *
66      * @param deviceState new device state
67      */
onDeviceStatusNotificationReceived(int deviceState)68     public void onDeviceStatusNotificationReceived(int deviceState) {
69         Log.d(TAG, "onDeviceStatusNotificationReceived(" + deviceState + ")");
70         mUwbServiceListener.onDeviceStatusNotificationReceived(deviceState);
71     }
72 
73     /**
74      * Session status notification
75      *
76      * @param sessionId  Session id
77      * @param state      State
78      * @param reasonCode Reason Code
79      */
onSessionStatusNotificationReceived(long sessionId, int state, int reasonCode)80     public void onSessionStatusNotificationReceived(long sessionId, int state, int reasonCode) {
81         Log.d(TAG, "onSessionStatusNotificationReceived(" + sessionId + ", " + state + ", "
82                 + reasonCode
83                 + ")");
84         mUwbServiceListener.onSessionStatusNotificationReceived(sessionId, state, reasonCode);
85     }
86 
87     /**
88      * Range data received notification
89      *
90      * @param sessionId Session id
91      * @param rangeData Range data
92      */
onRangeDataNotificationReceived(long sessionId, UwbRangingData rangeData)93     public void onRangeDataNotificationReceived(long sessionId, UwbRangingData rangeData) {
94         Log.d(TAG, "onRangeDataNotificationReceived(" + sessionId + ", " + rangeData + ")");
95         mUwbServiceListener.onRangeDataNotificationReceived(sessionId, rangeData);
96     }
97 
98     /**
99      * Vendor UCI notification received
100      *
101      * @param gid     Group identifier
102      * @param oid     Opcode identifier
103      * @param payload Payload
104      */
onVendorUciNotificationReceived(int gid, int oid, byte[] payload)105     public void onVendorUciNotificationReceived(int gid, int oid, byte[] payload) {
106         Log.d(TAG, "onVendorUciNotificationReceived(" + gid + ", " + oid + ", " + Arrays.toString(
107                 payload) + ")");
108         mUwbServiceListener.onVendorUciNotificationReceived(gid, oid, payload);
109     }
110 
111     /**
112      * Enable UWB
113      *
114      * @return : If this returns true, UWB is on
115      */
enable()116     public synchronized boolean enable() {
117         if (this.mUwbServicePtr != 0L) {
118             return true;
119         }
120         this.mUwbServicePtr = nativeUwbServiceNew();
121         return nativeEnable();
122     }
123 
124     /**
125      * Disable UWB
126      *
127      * @return : If this returns true, UWB is off
128      */
disable()129     public synchronized boolean disable() {
130         if (this.mUwbServicePtr == 0) {
131             return true;
132         }
133         nativeDisable();
134         nativeUwbServiceDestroy();
135         this.mUwbServicePtr = 0L;
136         return true;
137     }
138 
139     /**
140      * Creates the new ranging session with parameter session ID, type of the session
141      * and given app config parameters.
142      *
143      * @param sessionId       : Session ID is 4 Octets unique random number generated by application
144      * @param sessionType     : Type of session 0x00: Ranging session 0x01: Data transfer 0x02-0x9F:
145      *                        RFU
146      *                        0xA0-0xCF: Reserved for Vendor Specific use case 0xD0: Device Test
147      *                        Mode
148      *                        0xD1-0xDF: RFU 0xE0-0xFF: Vendor Specific use
149      * @param appConfigParams : App configuration parameters
150      * @return : Success or failure
151      */
initSession(int sessionId, byte sessionType, Params appConfigParams)152     public boolean initSession(int sessionId, byte sessionType, Params appConfigParams) {
153         return nativeInitSession(sessionId, sessionType, appConfigParams);
154     }
155 
156     /**
157      * De-initializes the session.
158      *
159      * @param sessionId : Session ID for which session to be de-initialized
160      * @return : Success or failure
161      */
deinitSession(int sessionId)162     public boolean deinitSession(int sessionId) {
163         return nativeDeinitSession(sessionId);
164     }
165 
166     /**
167      * Starts ranging for the session
168      *
169      * @param sessionId : Session ID for which ranging shall start
170      * @return : App config params
171      */
startRanging(int sessionId)172     public boolean startRanging(int sessionId) {
173         return nativeStartRanging(sessionId);
174     }
175 
176     /**
177      * Stops ranging for the session
178      *
179      * @param sessionId : Stop the requested ranging session.
180      * @return : Success or failure
181      */
stopRanging(int sessionId)182     public boolean stopRanging(int sessionId) {
183         return nativeStopRanging(sessionId);
184     }
185 
186     /**
187      * Reconfigure the parameters of the session
188      *
189      * @param sessionId : ID of the Session to be reconfigured
190      * @param params    : New params
191      * @return : Success or failure
192      */
reconfigure(int sessionId, Params params)193     public boolean reconfigure(int sessionId, Params params) {
194         return nativeReconfigure(sessionId, params);
195     }
196 
197     /**
198      * Update Multicast list for the requested UWB session
199      *
200      * @param sessionId  : Session ID to which multicast list to be updated
201      * @param action     : Update the multicast list by adding or removing
202      *                   0x00 - Adding
203      *                   0x01 - removing
204      * @param controlees : Controlees
205      * @return : Success or failure
206      */
updateControllerMulticastList(int sessionId, int action, FiraControleeParams controlees)207     public boolean updateControllerMulticastList(int sessionId, int action,
208             FiraControleeParams controlees) {
209         return nativeUpdateControllerMulticastList(sessionId, (byte) action, controlees);
210     }
211 
212     /**
213      * Set country code.
214      *
215      * @param countryCode : 2 char ISO country code
216      * @return : Success or failure
217      */
setCountryCode(byte[] countryCode)218     public boolean setCountryCode(byte[] countryCode) {
219         Log.i(TAG, "setCountryCode: " + new String(countryCode));
220         return nativeSetCountryCode(countryCode);
221     }
222 
223     /**
224      * Send
225      *
226      * @param gid     : Group Identifier
227      * @param oid     : Opcode Identifier
228      * @param payload : Payload
229      */
230     @NonNull
sendVendorCommand(int gid, int oid, byte[] payload)231     public UwbVendorUciResponse sendVendorCommand(int gid, int oid, byte[] payload) {
232         return nativeSendVendorCmd(gid, oid, payload);
233     }
234 
235     /**
236      * Retrieves power related stats
237      */
getPowerStats()238     public UwbPowerStats getPowerStats() {
239         return nativeGetPowerStats();
240     }
241 
nativeInit()242     private native void nativeInit();
243 
nativeUwbServiceNew()244     private native long nativeUwbServiceNew();
245 
nativeUwbServiceDestroy()246     private native void nativeUwbServiceDestroy();
247 
nativeEnable()248     private native boolean nativeEnable();
249 
nativeDisable()250     private native boolean nativeDisable();
251 
nativeInitSession(int sessionId, byte sessionType, Params appConfigParams)252     private native boolean nativeInitSession(int sessionId, byte sessionType,
253             Params appConfigParams);
254 
nativeDeinitSession(int sessionId)255     private native boolean nativeDeinitSession(int sessionId);
256 
nativeStartRanging(int sessionId)257     private native boolean nativeStartRanging(int sessionId);
258 
nativeStopRanging(int sessionId)259     private native boolean nativeStopRanging(int sessionId);
260 
nativeReconfigure(int sessionId, Params appConfigParams)261     private native boolean nativeReconfigure(int sessionId, Params appConfigParams);
262 
nativeUpdateControllerMulticastList(int sessionId, byte updateMulticastListAction, FiraControleeParams controlees)263     private native boolean nativeUpdateControllerMulticastList(int sessionId,
264             byte updateMulticastListAction, FiraControleeParams controlees);
265 
nativeSetCountryCode(byte[] countryCode)266     private native boolean nativeSetCountryCode(byte[] countryCode);
267 
nativeGetPowerStats()268     private native UwbPowerStats nativeGetPowerStats();
269 
nativeSendVendorCmd(int gid, int oid, byte[] payload)270     private native UwbVendorUciResponse nativeSendVendorCmd(int gid, int oid, byte[] payload);
271 
272 }
273