1 /*
2  * Copyright (C) 2017 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 #include <general_test/cell_info_lte.h>
17 
18 namespace general_test {
19 
validateIdentity(const struct chreWwanCellIdentityLte & identity)20 bool CellInfoLte::validateIdentity(
21     const struct chreWwanCellIdentityLte &identity) {
22   bool valid = false;
23 
24   if (!isBoundedInt32(identity.mcc, 0, 999, INT32_MAX)) {
25     sendFatalFailureInt32("Invalid LTE Mobile Country Code: %d", identity.mcc);
26   } else if (!isBoundedInt32(identity.mnc, 0, 999, INT32_MAX)) {
27     sendFatalFailureInt32("Invalid LTE Mobile Network Code: %d", identity.mnc);
28   } else if (!isBoundedInt32(identity.ci, 0, 268435455, INT32_MAX)) {
29     sendFatalFailureInt32("Invalid LTE Cell Identity: %d", identity.ci);
30   } else if (!isBoundedInt32(identity.pci, 0, 503, INT32_MAX)) {
31     sendFatalFailureInt32("Invalid LTE Physical Cell Id: %d", identity.pci);
32   } else if (!isBoundedInt32(identity.tac, 0, 65535, INT32_MAX)) {
33     sendFatalFailureInt32("Invalid LTE Tracking Area Code: %d", identity.tac);
34   } else if (!isBoundedInt32(identity.earfcn, 0, 262144, INT32_MAX)) {
35     sendFatalFailureInt32("Invalid LTE Absolute RF Channel Number: %d",
36                           identity.earfcn);
37   } else {
38     valid = true;
39   }
40 
41   return valid;
42 }
43 
validateSignalStrength(const struct chreWwanSignalStrengthLte & strength)44 bool CellInfoLte::validateSignalStrength(
45     const struct chreWwanSignalStrengthLte &strength) {
46   bool valid = false;
47   constexpr int32_t max = INT32_MAX;
48 
49   if (!isBoundedInt32(strength.signalStrength, 0, 31, 99)) {
50     sendFatalFailureInt32("Invalid LTE Signal Strength: %d",
51                           strength.signalStrength);
52   } else if (!isBoundedInt32(strength.rsrp, 44, 140, max)) {
53     sendFatalFailureInt32("Invalid LTE Reference Signal Receive Power: %d",
54                           strength.rsrp);
55   } else if (!isBoundedInt32(strength.rsrq, 3, 20, max)) {
56     sendFatalFailureInt32("Invalid LTE Reference Signal Receive Quality: %d",
57                           strength.rsrq);
58   } else if (!isBoundedInt32(strength.rssnr, -200, 300, max)) {
59     sendFatalFailureInt32(
60         "Invalid LTE Reference Signal Signal-to-noise Ratio: %d",
61         strength.rssnr);
62   } else if (!isBoundedInt32(strength.cqi, 0, 15, max)) {
63     sendFatalFailureInt32("Invalid LTE Channel Quality Indicator: %d",
64                           strength.cqi);
65   } else if (!isBoundedInt32(strength.timingAdvance, 0, max, max)) {
66     sendFatalFailureInt32("Invalid LTE Timing Advance (ms): %d",
67                           strength.timingAdvance);
68   } else {
69     valid = true;
70   }
71 
72   return valid;
73 }
74 
validate(const struct chreWwanCellInfoLte & cell)75 bool CellInfoLte::validate(const struct chreWwanCellInfoLte &cell) {
76   return (validateIdentity(cell.cellIdentityLte) &&
77           validateSignalStrength(cell.signalStrengthLte));
78 }
79 
80 }  // namespace general_test
81