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.telephony.qns;
17 
18 import static android.telephony.SignalStrength.INVALID;
19 
20 import android.telephony.SignalThresholdInfo;
21 
22 import java.util.Objects;
23 import java.util.concurrent.atomic.AtomicInteger;
24 
25 /**
26  * This class defines the threshold information that is processed in QualityMonitor to match the
27  * thresholds criteria for different access networks and measurement types.
28  */
29 class Threshold {
30     private static final AtomicInteger sTid = new AtomicInteger();
31 
32     private int mThresholdId;
33     private int mGroupId;
34     private int mAccessNetwork;
35     private int mMeasurementType;
36     private int mThreshold;
37     private int mMatchType;
38     private int mWaitTime;
39 
Threshold(int accessNetwork, int measurementType, int threshold, int matchType, int waitTime)40     Threshold(int accessNetwork, int measurementType, int threshold, int matchType, int waitTime) {
41         this.mThresholdId = sTid.getAndIncrement();
42         this.mGroupId = QnsConstants.INVALID_ID;
43         this.mAccessNetwork = accessNetwork;
44         this.mMeasurementType = measurementType;
45         this.mThreshold = threshold;
46         this.mMatchType = matchType;
47         this.mWaitTime = waitTime;
48     }
49 
Threshold(Threshold t)50     Threshold(Threshold t) {
51         this.mThresholdId = sTid.getAndIncrement();
52         this.mGroupId = QnsConstants.INVALID_ID;
53         this.mAccessNetwork = t.mAccessNetwork;
54         this.mMeasurementType = t.mMeasurementType;
55         this.mThreshold = t.mThreshold;
56         this.mMatchType = t.mMatchType;
57         this.mWaitTime = t.mWaitTime;
58     }
59 
Threshold(int tid, int gid, Threshold t)60     private Threshold(int tid, int gid, Threshold t) {
61         this.mThresholdId = tid;
62         this.mGroupId = gid;
63         this.mAccessNetwork = t.mAccessNetwork;
64         this.mMeasurementType = t.mMeasurementType;
65         this.mThreshold = t.mThreshold;
66         this.mMatchType = t.mMatchType;
67         this.mWaitTime = t.mWaitTime;
68     }
69 
copy()70     Threshold copy() {
71         return new Threshold(mThresholdId, mGroupId, this);
72     }
73 
getThresholdId()74     int getThresholdId() {
75         return mThresholdId;
76     }
77 
setThresholdId(int thresholdId)78     void setThresholdId(int thresholdId) {
79         this.mThresholdId = thresholdId;
80     }
81 
getGroupId()82     int getGroupId() {
83         return mGroupId;
84     }
85 
setGroupId(int groupId)86     void setGroupId(int groupId) {
87         this.mGroupId = groupId;
88     }
89 
getAccessNetwork()90     int getAccessNetwork() {
91         return mAccessNetwork;
92     }
93 
setAccessNetwork(int accessNetwork)94     void setAccessNetwork(int accessNetwork) {
95         this.mAccessNetwork = accessNetwork;
96     }
97 
getMeasurementType()98     int getMeasurementType() {
99         return mMeasurementType;
100     }
101 
setMeasurementType(int measurementType)102     void setMeasurementType(int measurementType) {
103         this.mMeasurementType = measurementType;
104     }
105 
getThreshold()106     int getThreshold() {
107         return mThreshold;
108     }
109 
setThreshold(int threshold)110     void setThreshold(int threshold) {
111         this.mThreshold = threshold;
112     }
113 
getMatchType()114     int getMatchType() {
115         return mMatchType;
116     }
117 
setMatchType(int matchType)118     void setMatchType(int matchType) {
119         this.mMatchType = matchType;
120     }
121 
getWaitTime()122     int getWaitTime() {
123         return mWaitTime;
124     }
125 
setWaitTime(int waitTime)126     void setWaitTime(int waitTime) {
127         mWaitTime = waitTime;
128     }
129 
130     /** Method is used to match the Threshold Criteria before Notify to ANE. */
isMatching(int threshold)131     boolean isMatching(int threshold) {
132         if (threshold != INVALID) {
133             switch (this.mMatchType) {
134                 case QnsConstants.THRESHOLD_MATCH_TYPE_EQUAL_TO:
135                     return this.mThreshold == threshold;
136                 case QnsConstants.THRESHOLD_EQUAL_OR_LARGER:
137                     return this.mThreshold <= threshold;
138                 case QnsConstants.THRESHOLD_EQUAL_OR_SMALLER:
139                     return this.mThreshold >= threshold;
140             }
141         }
142         return false;
143     }
144 
145     @Override
equals(Object o)146     public boolean equals(Object o) {
147         if (!(o instanceof Threshold)) return false;
148         Threshold th = (Threshold) o;
149         return this.mThresholdId == th.mThresholdId
150                 && this.mGroupId == th.mGroupId
151                 && this.mAccessNetwork == th.mAccessNetwork
152                 && this.mMeasurementType == th.mMeasurementType
153                 && this.mThreshold == th.mThreshold
154                 && this.mMatchType == th.mMatchType
155                 && this.mWaitTime == th.mWaitTime;
156     }
157 
158     @Override
hashCode()159     public int hashCode() {
160         return Objects.hash(
161                 mThresholdId,
162                 mGroupId,
163                 mAccessNetwork,
164                 mMeasurementType,
165                 mThreshold,
166                 mMatchType,
167                 mWaitTime);
168     }
169 
170     @Override
toString()171     public String toString() {
172         return "Threshold {"
173                 + "thresholdId="
174                 + mThresholdId
175                 + ", groupId="
176                 + mGroupId
177                 + ", accessNetwork="
178                 + mAccessNetwork
179                 + ", measurementType="
180                 + mMeasurementType
181                 + ", threshold="
182                 + mThreshold
183                 + ", matchType="
184                 + mMatchType
185                 + ", waitTime="
186                 + mWaitTime
187                 + '}';
188     }
189 
toShortString()190     String toShortString() {
191         StringBuilder sb = new StringBuilder();
192         sb.append(QnsConstants.accessNetworkTypeToString(mAccessNetwork)).append(".");
193         switch (mMeasurementType) {
194             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_UNKNOWN:
195                 sb.append("UNKNOWN");
196                 break;
197             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSI:
198                 sb.append("RSSI");
199                 break;
200             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSCP:
201                 sb.append("RSSCP");
202                 break;
203             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRP:
204                 sb.append("RSRP");
205                 break;
206             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSRQ:
207                 sb.append("RSRQ");
208                 break;
209             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_RSSNR:
210                 sb.append("RSSNR");
211                 break;
212             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRP:
213                 sb.append("SSRSRP");
214                 break;
215             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSRSRQ:
216                 sb.append("SSRSRQ");
217                 break;
218             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_SSSINR:
219                 sb.append("SSSINR");
220                 break;
221             case SignalThresholdInfo.SIGNAL_MEASUREMENT_TYPE_ECNO:
222                 sb.append("ECNO");
223                 break;
224             case QnsConstants.SIGNAL_MEASUREMENT_AVAILABILITY:
225                 sb.append("AVAIL");
226         }
227         if (mMatchType == QnsConstants.THRESHOLD_EQUAL_OR_LARGER) {
228             sb.append(">=");
229         } else if (mMatchType == QnsConstants.THRESHOLD_EQUAL_OR_SMALLER) {
230             sb.append("<=");
231         } else if (mMatchType == QnsConstants.THRESHOLD_MATCH_TYPE_EQUAL_TO) {
232             sb.append("==");
233         }
234         sb.append(mThreshold);
235         return sb.toString();
236     }
237 
identicalThreshold(Threshold o)238     boolean identicalThreshold(Threshold o) {
239         if (this == o) return true;
240         if (o == null) return false;
241         return this.mAccessNetwork == o.mAccessNetwork
242                 && this.mMeasurementType == o.mMeasurementType
243                 && this.mThreshold == o.mThreshold
244                 && this.mMatchType == o.mMatchType
245                 && this.mWaitTime == o.mWaitTime;
246     }
247 }
248