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