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 17 package com.android.server.uwb; 18 19 import android.util.Log; 20 21 import static com.android.server.uwb.UwbSettingsStore.SETTINGS_LOG_MODE; 22 23 import java.util.Locale; 24 import java.util.Optional; 25 import java.util.stream.Stream; 26 27 /** 28 * Provide functions for setting Log mode for capturing UCI messages as packets. 29 * Log mode defaults to disabled if was never set. However, log mode setting persists after reboot. 30 * The log mode parameter is sent to native stack through JNI where logging is implemented. 31 */ 32 public class UciLogModeStore { 33 private static final String TAG = "UciLogModeStore"; 34 35 /** 36 * ModeName represents the log mode as a string. 37 */ 38 public enum Mode { 39 DISABLED("disabled"), 40 FILTERED("filtered"), 41 UNFILTERED("unfiltered"); 42 43 private final String mMode; 44 Mode(String mode)45 Mode(String mode) { 46 this.mMode = mode; 47 } 48 getMode()49 public String getMode() { 50 return mMode; 51 } 52 53 /** 54 * Attempts to parse a string to corresponding LogMode 55 * @param modeNameStr is one of Disabled, Filtered, or Unfiltered (case insensitive). 56 * @return enum ModeName if successful, empty if failed. 57 */ fromName(String modeNameStr)58 public static Optional<Mode> fromName(String modeNameStr) { 59 return Stream.of(values()) 60 .filter(mode -> mode.getMode().equals( 61 modeNameStr.toLowerCase(Locale.US))) 62 .findFirst(); 63 } 64 } 65 66 private Mode mLogMode; 67 private final UwbSettingsStore mUwbSettingsStore; 68 UciLogModeStore(UwbSettingsStore uwbSettingsStore)69 public UciLogModeStore(UwbSettingsStore uwbSettingsStore) { 70 mUwbSettingsStore = uwbSettingsStore; 71 mLogMode = Mode.FILTERED; 72 } 73 74 /** 75 * Initialize the module. 76 */ initialize()77 public void initialize() { 78 Optional<Mode> logModeOption = Mode.fromName( 79 mUwbSettingsStore.get(SETTINGS_LOG_MODE)); 80 if (logModeOption.isPresent()) { 81 mLogMode = logModeOption.get(); 82 } 83 } 84 85 /** 86 * Is this a valid log mode 87 * 88 * @param logMode is one of Disabled, Filtered, or Unfiltered (case insensitive). 89 * @return true if the logMode is valid, false otherwise. 90 */ isValid(String logMode)91 public static boolean isValid(String logMode) { 92 return Mode.fromName(logMode).isPresent(); 93 } 94 95 /** 96 * Sets the log mode for current session, and store for future UWB UCI messages. 97 * 98 * @param modeStr is one of Disabled, Filtered, or Unfiltered (case insensitive). 99 * @return true if the log mode is set successfully, false otherwise. 100 */ storeMode(String modeStr)101 public boolean storeMode(String modeStr) { 102 Optional<Mode> logModeOption = Mode.fromName(modeStr); 103 if (logModeOption.isPresent()) { 104 mLogMode = logModeOption.get(); 105 mUwbSettingsStore.put(SETTINGS_LOG_MODE, mLogMode.getMode()); 106 Log.d(TAG, " set UCI log mode to " + mLogMode.getMode()); 107 return true; 108 } else { 109 return false; 110 } 111 } 112 113 /** 114 * Gets the log mode. 115 * 116 * @return the log mode as a String 117 */ getMode()118 public String getMode() { 119 return mLogMode.getMode(); 120 } 121 } 122