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_cdma.h>
17 
18 namespace general_test {
19 
validateIdentity(const struct chreWwanCellIdentityCdma identity)20 bool CellInfoCdma::validateIdentity(
21     const struct chreWwanCellIdentityCdma identity) {
22   bool valid = false;
23   constexpr int32_t max = INT32_MAX;
24 
25   if (!isBoundedInt32(identity.networkId, 0, 65535, max)) {
26     sendFatalFailureInt32(
27         "Invalid CDMA Network Id: %d", identity.networkId);
28   } else if (!isBoundedInt32(identity.systemId, 0, 32767, max)) {
29     sendFatalFailureInt32(
30         "Invalid CDMA System Id: %d", identity.systemId);
31   } else if (!isBoundedInt32(identity.basestationId, 0, 65535, max)) {
32     sendFatalFailureInt32("Invalid CDMA Base Station Id: %d",
33                           identity.basestationId);
34   } else if (!isBoundedInt32(identity.longitude, -2592000, 2592000, max)) {
35     sendFatalFailureInt32("Invalid CDMA Longitude: %d", identity.longitude);
36   } else if (!isBoundedInt32(identity.latitude, -1296000, 1296000, max)) {
37     sendFatalFailureInt32("Invalid CDMA Latitude: %d", identity.latitude);
38   } else {
39     valid = true;
40   }
41 
42   return valid;
43 }
44 
validateSignalStrengthCdma(const struct chreWwanSignalStrengthCdma strength)45 bool CellInfoCdma::validateSignalStrengthCdma(
46     const struct chreWwanSignalStrengthCdma strength) {
47   bool valid = false;
48 
49   // TODO: Find exact limits on dbm and ecio
50   if ((strength.dbm < 0) || (strength.dbm > 160)) {
51     sendFatalFailureInt32("Invalid CDMA/CDMA dbm: %d", strength.dbm);
52   } else if ((strength.ecio < 0) || (strength.ecio > 1600)) {
53     sendFatalFailureInt32("Invalid CDMA/CDMA ecio: %d", strength.ecio);
54   } else {
55     valid = true;
56   }
57 
58   return valid;
59 }
60 
validateSignalStrengthEvdo(const struct chreWwanSignalStrengthEvdo strength)61 bool CellInfoCdma::validateSignalStrengthEvdo(
62     const struct chreWwanSignalStrengthEvdo strength) {
63   bool valid = false;
64 
65   // TODO: Find exact limits on dbm and ecio
66   if ((strength.dbm < 0) || (strength.dbm > 160)) {
67     sendFatalFailureInt32("Invalid CDMA/EVDO dbm: %d", strength.dbm);
68   } else if ((strength.ecio < 0) || (strength.ecio > 1600)) {
69     sendFatalFailureInt32("Invalid CDMA/EVDO ecio: %d", strength.ecio);
70   } else if ((strength.signalNoiseRatio < 0)
71              || (strength.signalNoiseRatio > 8)) {
72     sendFatalFailureInt32("Invalid evdo signal noise ratio: %d",
73                           strength.signalNoiseRatio);
74   } else {
75     valid = true;
76   }
77 
78   return valid;
79 }
80 
validate(const struct chreWwanCellInfoCdma & cell)81 bool CellInfoCdma::validate(const struct chreWwanCellInfoCdma& cell) {
82   return (validateIdentity(cell.cellIdentityCdma)
83           && validateSignalStrengthCdma(cell.signalStrengthCdma)
84           && validateSignalStrengthEvdo(cell.signalStrengthEvdo));
85 }
86 
87 } // namespace general_test
88