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; 17 18 import android.util.Log; 19 import android.util.Pair; 20 21 import com.android.server.uwb.data.UwbConfigStatusData; 22 import com.android.server.uwb.data.UwbTlvData; 23 import com.android.server.uwb.data.UwbUciConstants; 24 import com.android.server.uwb.jni.NativeUwbManager; 25 import com.android.server.uwb.params.TlvBuffer; 26 import com.android.server.uwb.params.TlvDecoder; 27 import com.android.server.uwb.params.TlvDecoderBuffer; 28 import com.android.server.uwb.params.TlvEncoder; 29 30 import com.google.uwb.support.base.Params; 31 32 public class UwbConfigurationManager { 33 private static final String TAG = "UwbConfManager"; 34 35 NativeUwbManager mNativeUwbManager; 36 UwbConfigurationManager(NativeUwbManager nativeUwbManager)37 public UwbConfigurationManager(NativeUwbManager nativeUwbManager) { 38 mNativeUwbManager = nativeUwbManager; 39 } 40 41 /** 42 * Set app configurations. 43 */ setAppConfigurations(int sessionId, Params params, String chipId)44 public int setAppConfigurations(int sessionId, Params params, String chipId) { 45 int status = UwbUciConstants.STATUS_CODE_FAILED; 46 TlvBuffer tlvBuffer = null; 47 48 Log.d(TAG, "setAppConfigurations for protocol: " + params.getProtocolName()); 49 TlvEncoder encoder = TlvEncoder.getEncoder(params.getProtocolName()); 50 if (encoder == null) { 51 Log.d(TAG, "unsupported encoder protocol type"); 52 return status; 53 } 54 55 tlvBuffer = encoder.getTlvBuffer(params); 56 57 if (tlvBuffer.getNoOfParams() != 0) { 58 byte[] tlvByteArray = tlvBuffer.getByteArray(); 59 UwbConfigStatusData appConfig = mNativeUwbManager.setAppConfigurations(sessionId, 60 tlvBuffer.getNoOfParams(), 61 tlvByteArray.length, tlvByteArray, chipId); 62 if (appConfig != null) { 63 Log.i(TAG, "setAppConfigurations respData: " + appConfig); 64 status = appConfig.getStatus(); 65 } else { 66 Log.e(TAG, "appConfigList is null or size of appConfigList is zero"); 67 status = UwbUciConstants.STATUS_CODE_FAILED; 68 } 69 } else { 70 // Number of reconfig params FiraRangingReconfigureParams can be null 71 status = UwbUciConstants.STATUS_CODE_OK; 72 } 73 return status; 74 } 75 76 /** 77 * Retrieve app configurations from UWBS. 78 */ getAppConfigurations(int sessionId, String protocolName, byte[] appConfigIds, Class<T> paramType, String chipId)79 public <T extends Params> Pair<Integer, T> getAppConfigurations(int sessionId, 80 String protocolName, byte[] appConfigIds, Class<T> paramType, String chipId) { 81 82 Log.d(TAG, "getAppConfigurations for protocol: " + protocolName); 83 UwbTlvData getAppConfig = mNativeUwbManager.getAppConfigurations(sessionId, 84 appConfigIds.length, appConfigIds.length, appConfigIds, chipId); 85 Log.i(TAG, "getAppConfigurations respData: " 86 + (getAppConfig != null ? getAppConfig.toString() : "null")); 87 return decodeTLV(protocolName, getAppConfig, paramType); 88 } 89 90 /** 91 * Retrieve capability information from UWBS. 92 */ getCapsInfo(String protocolName, Class<T> paramType, String chipId)93 public <T extends Params> Pair<Integer, T> getCapsInfo(String protocolName, 94 Class<T> paramType, String chipId) { 95 96 Log.d(TAG, "getCapsInfo for protocol: " + protocolName); 97 UwbTlvData capsInfo = mNativeUwbManager.getCapsInfo(chipId); 98 Log.i(TAG, "getCapsInfo respData: " 99 + (capsInfo != null ? capsInfo.toString() : "null")); 100 return decodeTLV(protocolName, capsInfo, paramType); 101 } 102 103 /** 104 * Common decode TLV function based on protocol 105 */ decodeTLV(String protocolName, UwbTlvData tlvData, Class<T> paramType)106 public <T extends Params> Pair<Integer, T> decodeTLV(String protocolName, 107 UwbTlvData tlvData, Class<T> paramType) { 108 int status; 109 if (tlvData != null) { 110 status = tlvData.getStatus(); 111 } else { 112 Log.e(TAG, "TlvData is null or size of TlvData is zero"); 113 return Pair.create(UwbUciConstants.STATUS_CODE_FAILED, null); 114 } 115 TlvDecoder decoder = TlvDecoder.getDecoder(protocolName); 116 if (decoder == null) { 117 Log.d(TAG, "unsupported decoder protocol type"); 118 return Pair.create(status, null); 119 } 120 121 int numOfTlvs = tlvData.getLength(); 122 TlvDecoderBuffer tlvs = new TlvDecoderBuffer(tlvData.getTlv(), numOfTlvs); 123 if (!tlvs.parse()) { 124 Log.e(TAG, "Failed to parse tlvs"); 125 return Pair.create(UwbUciConstants.STATUS_CODE_FAILED, null); 126 } 127 T params = null; 128 try { 129 params = decoder.getParams(tlvs, paramType); 130 } catch (IllegalArgumentException e) { 131 Log.e(TAG, "Failed to decode", e); 132 } 133 if (params == null) { 134 Log.d(TAG, "Failed to get params from tlvs"); 135 return Pair.create(UwbUciConstants.STATUS_CODE_FAILED, null); 136 } 137 return Pair.create(UwbUciConstants.STATUS_CODE_OK, params); 138 } 139 } 140