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