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