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/wwan_cell_info_test.h>
17 
18 #include <general_test/cell_info_base.h>
19 #include <general_test/cell_info_cdma.h>
20 #include <general_test/cell_info_gsm.h>
21 #include <general_test/cell_info_lte.h>
22 #include <general_test/cell_info_nr.h>
23 #include <general_test/cell_info_tdscdma.h>
24 #include <general_test/cell_info_wcdma.h>
25 
26 #include <shared/send_message.h>
27 
28 /*
29  * General philosophy behind this test:
30  *   Make a call to chreWwanGetCellInfoAsync and then ensure the following:
31  *     1) Data is received within CHRE_ASYNC_RESULT_TIMEOUT_NS + small buffer.
32  *     2) Various fields in the returned data are correct.
33  */
34 
35 namespace general_test {
36 
WwanCellInfoTest()37 WwanCellInfoTest::WwanCellInfoTest() : Test(CHRE_API_VERSION_1_1) {}
38 
setUp(uint32_t messageSize,const void *)39 void WwanCellInfoTest::setUp(uint32_t messageSize, const void * /* message */) {
40   if ((chreWwanGetCapabilities() & CHRE_WWAN_GET_CELL_INFO) == 0) {
41     sendMessageToHost(nanoapp_testing::MessageType::kSkipped);
42   } else if (!chreWwanGetCellInfoAsync(&mTimerHandle)) {
43     nanoapp_testing::sendFatalFailureToHost(
44         "chreWwanGetCellInfo failed unexpectedly");
45   } else {
46     mTimerHandle = chreTimerSet(CHRE_ASYNC_RESULT_TIMEOUT_NS, &mTimerHandle,
47                                 true /* oneShot */);
48 
49     if (mTimerHandle == CHRE_TIMER_INVALID) {
50       nanoapp_testing::sendFatalFailureToHost(
51           "Unable to set timer for automatic failure");
52     }
53   }
54 }
55 
~WwanCellInfoTest()56 WwanCellInfoTest::~WwanCellInfoTest() {
57   // Ensure the timer is cancelled
58   cancelTimer();
59 }
60 
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)61 void WwanCellInfoTest::handleEvent(uint32_t senderInstanceId,
62                                    uint16_t eventType, const void *eventData) {
63   // The only expected message is from the async call
64   if (senderInstanceId != CHRE_INSTANCE_ID) {
65     nanoapp_testing::sendFatalFailureToHost(
66         "handleEvent received event from unexpected sender:",
67         &senderInstanceId);
68   } else if (eventType == CHRE_EVENT_WWAN_CELL_INFO_RESULT) {
69     cancelTimer();
70     validateCellInfoResult(eventData);
71   } else if (eventType == CHRE_EVENT_TIMER) {
72     nanoapp_testing::sendFatalFailureToHost(
73         "chreWwanGetCellInfo did not return data in time");
74   } else {
75     uint32_t type = eventType;
76     nanoapp_testing::sendFatalFailureToHost(
77         "handleEvent received an unexpected eventType:", &type);
78   }
79 }
80 
cancelTimer()81 void WwanCellInfoTest::cancelTimer() {
82   if (mTimerHandle != CHRE_TIMER_INVALID) {
83     chreTimerCancel(mTimerHandle);
84     mTimerHandle = CHRE_TIMER_INVALID;
85   }
86 }
87 
validateCellInfo(uint8_t count,const struct chreWwanCellInfo * cells) const88 void WwanCellInfoTest::validateCellInfo(
89     uint8_t count, const struct chreWwanCellInfo *cells) const {
90   bool valid = true;
91 
92   for (int i = 0; (i < count) && valid; ++i) {
93     if (cells[i].reserved != 0) {
94       valid = false;
95       CellInfoBase::sendFatalFailureUint8("Invalid reserved CellInfo field: %d",
96                                           cells[i].reserved);
97     }
98 
99     if ((cells[i].timeStampType != CHRE_WWAN_CELL_TIMESTAMP_TYPE_UNKNOWN) &&
100         (cells[i].timeStampType != CHRE_WWAN_CELL_TIMESTAMP_TYPE_ANTENNA) &&
101         (cells[i].timeStampType != CHRE_WWAN_CELL_TIMESTAMP_TYPE_MODEM) &&
102         (cells[i].timeStampType != CHRE_WWAN_CELL_TIMESTAMP_TYPE_OEM_RIL) &&
103         (cells[i].timeStampType != CHRE_WWAN_CELL_TIMESTAMP_TYPE_JAVA_RIL)) {
104       valid = false;
105       CellInfoBase::sendFatalFailureUint8("Invalid timeStampType: %d",
106                                           cells[i].timeStampType);
107     }
108 
109     if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_GSM) {
110       valid &= CellInfoGsm::validate(cells[i].CellInfo.gsm);
111     } else if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_CDMA) {
112       valid &= CellInfoCdma::validate(cells[i].CellInfo.cdma);
113     } else if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_LTE) {
114       valid &= CellInfoLte::validate(cells[i].CellInfo.lte);
115     } else if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_WCDMA) {
116       valid &= CellInfoWcdma::validate(cells[i].CellInfo.wcdma);
117     } else if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_TD_SCDMA) {
118       valid &= CellInfoTdscdma::validate(cells[i].CellInfo.tdscdma);
119     } else if (cells[i].cellInfoType == CHRE_WWAN_CELL_INFO_TYPE_NR) {
120       valid &=
121           CellInfoNr::validate(cells[i].CellInfo.nr, cells[i].registered != 0);
122     } else {
123       valid = false;
124       CellInfoBase::sendFatalFailureUint8("Invalid cellInfoType: %d",
125                                           cells[i].cellInfoType);
126     }
127   }
128 
129   if (valid) {
130     nanoapp_testing::sendSuccessToHost();
131   }
132 }
133 
validateCellInfoResult(const void * eventData) const134 void WwanCellInfoTest::validateCellInfoResult(const void *eventData) const {
135   const struct chreWwanCellInfoResult *result =
136       static_cast<const chreWwanCellInfoResult *>(eventData);
137 
138   if (eventData == nullptr) {
139     nanoapp_testing::sendFatalFailureToHost("Received eventData is null");
140   } else if (result->version != CHRE_WWAN_CELL_INFO_RESULT_VERSION) {
141     nanoapp_testing::sendFatalFailureToHost(
142         "Received version is unexpected value");
143   } else if (result->reserved != 0) {
144     nanoapp_testing::sendFatalFailureToHost("Received reserved field non-zero");
145   } else {
146     const uint32_t *receivedCookie =
147         static_cast<const uint32_t *>(result->cookie);
148 
149     if (receivedCookie != &mTimerHandle) {
150       nanoapp_testing::sendFatalFailureToHost("Received cookie does not match");
151     } else if (result->cellInfoCount != 0) {
152       validateCellInfo(result->cellInfoCount, result->cells);
153     } else {
154       nanoapp_testing::sendSuccessToHost();
155     }
156   }
157 }
158 
159 }  // namespace general_test
160