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