1 /* 2 * Copyright (C) 2019 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.wifi.util; 17 18 import android.annotation.NonNull; 19 import android.content.Context; 20 import android.net.wifi.WifiInfo; 21 22 import com.android.wifi.resources.R; 23 24 /** Utilities for computations involving RSSI. */ 25 public class RssiUtil { RssiUtil()26 private RssiUtil() {} 27 28 /** 29 * Apply known fixes to a RSSi from signal poll and return INVALID_RSSI to indicate the data 30 * is invalid. 31 */ calculateAdjustedRssi(int rssi)32 public static int calculateAdjustedRssi(int rssi) { 33 if (rssi > WifiInfo.INVALID_RSSI && rssi < WifiInfo.MAX_RSSI) { 34 /* 35 * Positive RSSI is possible when devices are close(~0m apart) to each other. 36 * And there are some driver/firmware implementation, where they avoid 37 * reporting large negative rssi values by adding 256. 38 * so adjust the valid rssi reports for such implementations. 39 */ 40 if (rssi > (WifiInfo.INVALID_RSSI + 256)) { 41 return rssi - 256; 42 } 43 return rssi; 44 } 45 return WifiInfo.INVALID_RSSI; 46 } 47 48 /** Calculate RSSI level from RSSI using overlaid RSSI level thresholds. */ calculateSignalLevel(Context context, int rssi)49 public static int calculateSignalLevel(Context context, int rssi) { 50 int[] thresholds = getRssiLevelThresholds(context); 51 52 for (int level = 0; level < thresholds.length; level++) { 53 if (rssi < thresholds[level]) { 54 return level; 55 } 56 } 57 return thresholds.length; 58 } 59 60 @NonNull getRssiLevelThresholds(Context context)61 private static int[] getRssiLevelThresholds(Context context) { 62 // getIntArray() will never return null, it will throw instead 63 return context.getResources().getIntArray(R.array.config_wifiRssiLevelThresholds); 64 } 65 } 66