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