1 /* 2 * Copyright (C) 2023 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.bluetooth.content_profiles; 17 18 import android.bluetooth.BluetoothProfile; 19 import android.bluetooth.BluetoothProtoEnums; 20 import android.os.SystemClock; 21 import android.util.Log; 22 23 import com.android.bluetooth.BluetoothStatsLog; 24 import com.android.internal.annotations.VisibleForTesting; 25 26 /** Utility method to report exceptions and error/warn logs in content profiles. */ 27 public class ContentProfileErrorReportUtils { 28 private static final String TAG = ContentProfileErrorReportUtils.class.getSimpleName(); 29 30 /* Minimum period between two error reports */ 31 @VisibleForTesting static final long MIN_PERIOD_BETWEEN_TWO_ERROR_REPORTS_MILLIS = 1_000; 32 33 @VisibleForTesting static long sLastReportTime = 0; 34 35 /** 36 * Report error by writing BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED atom. A 37 * report will be skipped if not enough time has passed from the last report. 38 * 39 * @param profile One of: {@link BluetoothProfile#PBAP}, {@link BluetoothProfile#MAP}, {@link 40 * BluetoothProfile#OPP} 41 * @param fileNameEnum File name enum which is declared in {@link BluetoothProtoEnums} 42 * @param type One of the following: {@link 43 * BluetoothStatsLog#BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__EXCEPTION}, {@link 44 * BluetoothStatsLog#BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__LOG_ERROR}, {@link 45 * BluetoothStatsLog#BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__LOG_WARN} 46 * @param tag A tag which represents the code location of this error. The values are managed per 47 * each java file. 48 * @return true if successfully wrote the error, false otherwise 49 */ report(int profile, int fileNameEnum, int type, int tag)50 public static synchronized boolean report(int profile, int fileNameEnum, int type, int tag) { 51 if (isTooFrequentReport()) { 52 Log.w( 53 TAG, 54 "Skipping reporting this error to prevent flooding." 55 + " fileNameEnum=" 56 + fileNameEnum 57 + ", tag=" 58 + tag); 59 return false; 60 } 61 62 BluetoothStatsLog.write( 63 BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED, 64 profile, 65 fileNameEnum, 66 type, 67 tag); 68 sLastReportTime = SystemClock.uptimeMillis(); 69 return true; 70 } 71 isTooFrequentReport()72 private static boolean isTooFrequentReport() { 73 return SystemClock.uptimeMillis() - sLastReportTime 74 < MIN_PERIOD_BETWEEN_TWO_ERROR_REPORTS_MILLIS; 75 } 76 } 77