1 /* 2 * Copyright (C) 2021 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.jni; 17 18 import android.annotation.NonNull; 19 import android.util.Log; 20 21 import com.android.server.uwb.UwbInjector; 22 import com.android.server.uwb.data.UwbConfigStatusData; 23 import com.android.server.uwb.data.UwbMulticastListUpdateStatus; 24 import com.android.server.uwb.data.UwbRangingData; 25 import com.android.server.uwb.data.UwbTlvData; 26 import com.android.server.uwb.data.UwbUciConstants; 27 import com.android.server.uwb.data.UwbVendorUciResponse; 28 import com.android.server.uwb.info.UwbPowerStats; 29 30 public class NativeUwbManager { 31 private static final String TAG = NativeUwbManager.class.getSimpleName(); 32 33 public final Object mSessionFnLock = new Object(); 34 public final Object mSessionCountFnLock = new Object(); 35 public final Object mGlobalStateFnLock = new Object(); 36 public final Object mGetSessionStatusFnLock = new Object(); 37 public final Object mSetAppConfigFnLock = new Object(); 38 private final UwbInjector mUwbInjector; 39 protected INativeUwbManager.DeviceNotification mDeviceListener; 40 protected INativeUwbManager.SessionNotification mSessionListener; 41 private long mDispatcherPointer; 42 protected INativeUwbManager.VendorNotification mVendorListener; 43 NativeUwbManager(@onNull UwbInjector uwbInjector)44 public NativeUwbManager(@NonNull UwbInjector uwbInjector) { 45 mUwbInjector = uwbInjector; 46 loadLibrary(); 47 } 48 loadLibrary()49 protected void loadLibrary() { 50 System.loadLibrary("uwb_uci_jni_rust"); 51 nativeInit(); 52 } 53 setDeviceListener(INativeUwbManager.DeviceNotification deviceListener)54 public void setDeviceListener(INativeUwbManager.DeviceNotification deviceListener) { 55 mDeviceListener = deviceListener; 56 } 57 setSessionListener(INativeUwbManager.SessionNotification sessionListener)58 public void setSessionListener(INativeUwbManager.SessionNotification sessionListener) { 59 mSessionListener = sessionListener; 60 } 61 setVendorListener(INativeUwbManager.VendorNotification vendorListener)62 public void setVendorListener(INativeUwbManager.VendorNotification vendorListener) { 63 mVendorListener = vendorListener; 64 } 65 onDeviceStatusNotificationReceived(int deviceState)66 public void onDeviceStatusNotificationReceived(int deviceState) { 67 Log.d(TAG, "onDeviceStatusNotificationReceived(" + deviceState + ")"); 68 mDeviceListener.onDeviceStatusNotificationReceived(deviceState); 69 } 70 onCoreGenericErrorNotificationReceived(int status)71 public void onCoreGenericErrorNotificationReceived(int status) { 72 Log.d(TAG, "onCoreGenericErrorNotificationReceived(" + status + ")"); 73 mDeviceListener.onCoreGenericErrorNotificationReceived(status); 74 } 75 onSessionStatusNotificationReceived(long id, int state, int reasonCode)76 public void onSessionStatusNotificationReceived(long id, int state, int reasonCode) { 77 Log.d(TAG, "onSessionStatusNotificationReceived(" + id + ", " + state + ", " + reasonCode 78 + ")"); 79 mSessionListener.onSessionStatusNotificationReceived(id, state, reasonCode); 80 } 81 onRangeDataNotificationReceived(UwbRangingData rangeData)82 public void onRangeDataNotificationReceived(UwbRangingData rangeData) { 83 Log.d(TAG, "onRangeDataNotificationReceived : " + rangeData); 84 mSessionListener.onRangeDataNotificationReceived(rangeData); 85 } 86 onMulticastListUpdateNotificationReceived( UwbMulticastListUpdateStatus multicastListUpdateData)87 public void onMulticastListUpdateNotificationReceived( 88 UwbMulticastListUpdateStatus multicastListUpdateData) { 89 Log.d(TAG, "onMulticastListUpdateNotificationReceived : " + multicastListUpdateData); 90 mSessionListener.onMulticastListUpdateNotificationReceived(multicastListUpdateData); 91 } 92 93 /** 94 * Enable UWB hardware. 95 * 96 * @return : If this returns true, UWB is on 97 */ doInitialize()98 public synchronized boolean doInitialize() { 99 if (this.mDispatcherPointer == 0L) { 100 this.mDispatcherPointer = nativeDispatcherNew(); 101 } 102 return nativeDoInitialize(); 103 } 104 105 /** 106 * Disable UWB hardware. 107 * 108 * @return : If this returns true, UWB is off 109 */ doDeinitialize()110 public synchronized boolean doDeinitialize() { 111 nativeDoDeinitialize(); 112 nativeDispatcherDestroy(); 113 this.mDispatcherPointer = 0L; 114 return true; 115 } 116 getTimestampResolutionNanos()117 public synchronized long getTimestampResolutionNanos() { 118 return 0L; 119 /* TODO: Not Implemented in native stack 120 return nativeGetTimestampResolutionNanos(); */ 121 } 122 123 /** 124 * Retrieves maximum number of UWB sessions concurrently 125 * 126 * @return : Retrieves maximum number of UWB sessions concurrently 127 */ getMaxSessionNumber()128 public int getMaxSessionNumber() { 129 return nativeGetMaxSessionNumber(); 130 } 131 132 /** 133 * Retrieves power related stats 134 * 135 */ getPowerStats()136 public UwbPowerStats getPowerStats() { 137 return nativeGetPowerStats(); 138 } 139 140 /** 141 * Creates the new UWB session with parameter session ID and type of the session. 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: RFU 145 * 0xA0-0xCF: Reserved for Vendor Specific use case 0xD0: Device Test Mode 146 * 0xD1-0xDF: RFU 0xE0-0xFF: Vendor Specific use 147 * @return : {@link UwbUciConstants} Status code 148 */ initSession(int sessionId, byte sessionType)149 public byte initSession(int sessionId, byte sessionType) { 150 synchronized (mSessionFnLock) { 151 return nativeSessionInit(sessionId, sessionType); 152 } 153 } 154 155 /** 156 * De-initializes the session. 157 * 158 * @param sessionId : Session ID for which session to be de-initialized 159 * @return : {@link UwbUciConstants} Status code 160 */ deInitSession(int sessionId)161 public byte deInitSession(int sessionId) { 162 synchronized (mSessionFnLock) { 163 return nativeSessionDeInit(sessionId); 164 } 165 } 166 167 /** 168 * reset the UWBs 169 * 170 * @param resetConfig : Reset config 171 * @return : {@link UwbUciConstants} Status code 172 */ resetDevice(byte resetConfig)173 public byte resetDevice(byte resetConfig) { 174 return nativeResetDevice(resetConfig); 175 } 176 177 /** 178 * Retrieves number of UWB sessions in the UWBS. 179 * 180 * @return : Number of UWB sessions present in the UWBS. 181 */ getSessionCount()182 public byte getSessionCount() { 183 synchronized (mSessionCountFnLock) { 184 return nativeGetSessionCount(); 185 } 186 } 187 188 /** 189 * Queries the current state of the UWB session. 190 * 191 * @param sessionId : Session of the UWB session for which current session state to be queried 192 * @return : {@link UwbUciConstants} Session State 193 */ getSessionState(int sessionId)194 public byte getSessionState(int sessionId) { 195 synchronized (mGetSessionStatusFnLock) { 196 return nativeGetSessionState(sessionId); 197 } 198 } 199 200 /** 201 * Starts a UWB session. 202 * 203 * @param sessionId : Session ID for which ranging shall start 204 * @return : {@link UwbUciConstants} Status code 205 */ startRanging(int sessionId)206 public byte startRanging(int sessionId) { 207 synchronized (mSessionFnLock) { 208 return nativeRangingStart(sessionId); 209 } 210 } 211 212 /** 213 * Stops the ongoing UWB session. 214 * 215 * @param sessionId : Stop the requested ranging session. 216 * @return : {@link UwbUciConstants} Status code 217 */ stopRanging(int sessionId)218 public byte stopRanging(int sessionId) { 219 synchronized (mSessionFnLock) { 220 return nativeRangingStop(sessionId); 221 } 222 } 223 224 /** 225 * set APP Configuration Parameters for the requested UWB session 226 * 227 * @param noOfParams : The number (n) of APP Configuration Parameters 228 * @param appConfigParamLen : The length of APP Configuration Parameters 229 * @param appConfigParams : APP Configuration Parameter 230 * @return : {@link UwbConfigStatusData} : Contains statuses for all cfg_id 231 */ setAppConfigurations(int sessionId, int noOfParams, int appConfigParamLen, byte[] appConfigParams)232 public UwbConfigStatusData setAppConfigurations(int sessionId, int noOfParams, 233 int appConfigParamLen, byte[] appConfigParams) { 234 synchronized (mSetAppConfigFnLock) { 235 return nativeSetAppConfigurations(sessionId, noOfParams, appConfigParamLen, 236 appConfigParams); 237 } 238 } 239 240 /** 241 * Get APP Configuration Parameters for the requested UWB session 242 * 243 * @param noOfParams : The number (n) of APP Configuration Parameters 244 * @param appConfigParamLen : The length of APP Configuration Parameters 245 * @param appConfigIds : APP Configuration Parameter 246 * @return : {@link UwbTlvData} : All tlvs that are to be decoded 247 */ getAppConfigurations(int sessionId, int noOfParams, int appConfigParamLen, byte[] appConfigIds)248 public UwbTlvData getAppConfigurations(int sessionId, int noOfParams, int appConfigParamLen, 249 byte[] appConfigIds) { 250 synchronized (mSetAppConfigFnLock) { 251 return nativeGetAppConfigurations(sessionId, noOfParams, appConfigParamLen, 252 appConfigIds); 253 } 254 } 255 256 /** 257 * Get Core Capabilities information 258 * 259 * @return : {@link UwbTlvData} : All tlvs that are to be decoded 260 */ getCapsInfo()261 public UwbTlvData getCapsInfo() { 262 synchronized (mGlobalStateFnLock) { 263 return nativeGetCapsInfo(); 264 } 265 } 266 267 /** 268 * Update Multicast list for the requested UWB session 269 * 270 * @param sessionId : Session ID to which multicast list to be updated 271 * @param action : Update the multicast list by adding or removing 272 * 0x00 - Adding 273 * 0x01 - removing 274 * @param noOfControlee : The number(n) of Controlees 275 * @param addresses : address list of Controlees 276 * @param subSessionIds : Specific sub-session ID list of Controlees 277 * @return : refer to SESSION_SET_APP_CONFIG_RSP 278 * in the Table 16: Control messages to set Application configurations 279 */ controllerMulticastListUpdate(int sessionId, int action, int noOfControlee, short[] addresses, int[]subSessionIds)280 public byte controllerMulticastListUpdate(int sessionId, int action, int noOfControlee, 281 short[] addresses, int[]subSessionIds) { 282 synchronized (mSessionFnLock) { 283 return nativeControllerMulticastListUpdate(sessionId, (byte) action, 284 (byte) noOfControlee, addresses, subSessionIds); 285 } 286 } 287 288 /** 289 * Set country code. 290 * 291 * @param countryCode 2 char ISO country code 292 */ setCountryCode(byte[] countryCode)293 public byte setCountryCode(byte[] countryCode) { 294 Log.i(TAG, "setCountryCode: " + new String(countryCode)); 295 synchronized (mGlobalStateFnLock) { 296 return nativeSetCountryCode(countryCode); 297 } 298 } 299 300 @NonNull sendRawVendorCmd(int gid, int oid, byte[] payload)301 public UwbVendorUciResponse sendRawVendorCmd(int gid, int oid, byte[] payload) { 302 synchronized (mGlobalStateFnLock) { 303 return nativeSendRawVendorCmd(gid, oid, payload); 304 } 305 } 306 nativeDispatcherNew()307 private native long nativeDispatcherNew(); 308 nativeDispatcherDestroy()309 private native void nativeDispatcherDestroy(); 310 nativeInit()311 private native boolean nativeInit(); 312 nativeDoInitialize()313 private native boolean nativeDoInitialize(); 314 nativeDoDeinitialize()315 private native boolean nativeDoDeinitialize(); 316 nativeGetTimestampResolutionNanos()317 private native long nativeGetTimestampResolutionNanos(); 318 nativeGetPowerStats()319 private native UwbPowerStats nativeGetPowerStats(); 320 nativeGetMaxSessionNumber()321 private native int nativeGetMaxSessionNumber(); 322 nativeResetDevice(byte resetConfig)323 private native byte nativeResetDevice(byte resetConfig); 324 nativeSessionInit(int sessionId, byte sessionType)325 private native byte nativeSessionInit(int sessionId, byte sessionType); 326 nativeSessionDeInit(int sessionId)327 private native byte nativeSessionDeInit(int sessionId); 328 nativeGetSessionCount()329 private native byte nativeGetSessionCount(); 330 nativeRangingStart(int sessionId)331 private native byte nativeRangingStart(int sessionId); 332 nativeRangingStop(int sessionId)333 private native byte nativeRangingStop(int sessionId); 334 nativeGetSessionState(int sessionId)335 private native byte nativeGetSessionState(int sessionId); 336 nativeSetAppConfigurations(int sessionId, int noOfParams, int appConfigParamLen, byte[] appConfigParams)337 private native UwbConfigStatusData nativeSetAppConfigurations(int sessionId, int noOfParams, 338 int appConfigParamLen, byte[] appConfigParams); 339 nativeGetAppConfigurations(int sessionId, int noOfParams, int appConfigParamLen, byte[] appConfigParams)340 private native UwbTlvData nativeGetAppConfigurations(int sessionId, int noOfParams, 341 int appConfigParamLen, byte[] appConfigParams); 342 nativeGetCapsInfo()343 private native UwbTlvData nativeGetCapsInfo(); 344 nativeControllerMulticastListUpdate(int sessionId, byte action, byte noOfControlee, short[] address, int[]subSessionId)345 private native byte nativeControllerMulticastListUpdate(int sessionId, byte action, 346 byte noOfControlee, short[] address, int[]subSessionId); 347 nativeSetCountryCode(byte[] countryCode)348 private native byte nativeSetCountryCode(byte[] countryCode); 349 nativeSendRawVendorCmd(int gid, int oid, byte[] payload)350 private native UwbVendorUciResponse nativeSendRawVendorCmd(int gid, int oid, byte[] payload); 351 } 352