1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "parameter.h"
17 #include "ril_test_util.h"
18
19 namespace OHOS {
20 namespace Telephony {
21 namespace {
22 const std::string KEY_VOICECALL_CAP = "const.telephony.voice.capable";
23 const char *TEL_SIM_SLOT_COUNT = "const.telephony.slotCount";
24 const char *DEFAULT_SLOT_COUNT = "1";
25 const int32_t VOICECALL_CAP_VAL_LEN = 6;
26 }
27
28 RilTestUtil RilTestUtil::rilTestUtil_;
29
WaitFor(int32_t timeoutSecond)30 void RilTestUtil::WaitFor(int32_t timeoutSecond)
31 {
32 rilTestUtil_.WaitForInner(WAIT_TIME_SECOND);
33 }
34
GetBoolResult(HdiId hdiId_)35 bool RilTestUtil::GetBoolResult(HdiId hdiId_)
36 {
37 return rilTestUtil_.GetBoolResultInner(hdiId_);
38 }
39
HasVoiceCapability()40 bool RilTestUtil::HasVoiceCapability()
41 {
42 return rilTestUtil_.HasVoiceCapabilityInner();
43 }
44
GetSerialId()45 int32_t RilTestUtil::GetSerialId()
46 {
47 return rilTestUtil_.GetSerialIdInner();
48 }
49
IsValidSlotId(int32_t slotId)50 bool RilTestUtil::IsValidSlotId(int32_t slotId)
51 {
52 return rilTestUtil_.IsValidSlotIdInner(slotId);
53 }
54
IsReady(int32_t slotId)55 bool RilTestUtil::IsReady(int32_t slotId)
56 {
57 return rilTestUtil_.IsReadyInner(slotId);
58 }
59
GetMaxSimCount()60 int32_t RilTestUtil::GetMaxSimCount()
61 {
62 char simSlotCount[SYSPARA_SIZE] = { 0 };
63 GetParameter(TEL_SIM_SLOT_COUNT, DEFAULT_SLOT_COUNT, simSlotCount, SYSPARA_SIZE);
64 int32_t slotCount = std::atoi(simSlotCount);
65 return slotCount;
66 }
67
Init()68 void RilTestUtil::Init()
69 {
70 if (isInit_) {
71 TELEPHONY_LOGE("RilTestUtil has init");
72 return;
73 }
74 rilInterface_ = IRil::Get();
75 hasVoiceCapable_ = HasVoiceCapability();
76 slotCount_ = GetMaxSimCount();
77 if (rilInterface_ == nullptr) {
78 TELEPHONY_LOGE("RilTestUtil has init");
79 return;
80 }
81 callback_ = new RilCallbackTest();
82 rilInterface_->SetCallback(callback_);
83 rilInterface_->GetSimStatus(SLOTID_1, RilTestUtil::GetSerialId());
84 WaitFor(WAIT_TIME_SECOND);
85 rilInterface_->GetSimStatus(SLOTID_2, RilTestUtil::GetSerialId());
86 WaitFor(WAIT_TIME_SECOND);
87 isInit_ = true;
88 }
89
WaitForInner(int32_t timeoutSecond)90 void RilTestUtil::WaitForInner(int32_t timeoutSecond)
91 {
92 if (callback_ == nullptr) {
93 TELEPHONY_LOGE("callback_ is null");
94 return;
95 }
96 callback_->WaitFor(WAIT_TIME_SECOND);
97 }
98
GetBoolResultInner(HdiId hdiId_)99 bool RilTestUtil::GetBoolResultInner(HdiId hdiId_)
100 {
101 if (callback_ == nullptr) {
102 TELEPHONY_LOGE("callback_ is null");
103 return false;
104 }
105 return callback_->GetBoolResult(hdiId_);
106 }
107
GetSerialIdInner()108 int32_t RilTestUtil::GetSerialIdInner()
109 {
110 if (callback_ == nullptr) {
111 TELEPHONY_LOGE("callback_ is null");
112 return 0;
113 }
114 return callback_->GetSerialId();
115 }
116
HasVoiceCapabilityInner()117 bool RilTestUtil::HasVoiceCapabilityInner()
118 {
119 char retValue[VOICECALL_CAP_VAL_LEN + 1] = { "true" };
120 int retLen = GetParameter(KEY_VOICECALL_CAP.c_str(), "true", retValue, VOICECALL_CAP_VAL_LEN);
121 TELEPHONY_LOGI("HasVoiceCapability retValue %{public}s, retLen %{public}d", retValue, retLen);
122 if (strcmp(retValue, "false") == 0) {
123 return false;
124 }
125 return true;
126 }
127
IsValidSlotIdInner(int32_t slotId)128 bool RilTestUtil::IsValidSlotIdInner(int32_t slotId)
129 {
130 if ((slotId < 0) || (slotId >= slotCount_)) {
131 TELEPHONY_LOGE("slotId is invalid, slotId = %{public}d", slotId);
132 return false;
133 }
134 TELEPHONY_LOGD("slotId is valid, slotId = %{public}d", slotId);
135 return true;
136 }
137
IsReadyInner(int32_t slotId)138 bool RilTestUtil::IsReadyInner(int32_t slotId)
139 {
140 if (rilInterface_ == nullptr || !hasVoiceCapable_ || !IsValidSlotId(slotId)) {
141 return false;
142 }
143 if (callback_ == nullptr) {
144 TELEPHONY_LOGE("callback_ is null");
145 return false;
146 }
147 return callback_->IsReady(slotId);
148 }
149
GetRilInterface()150 sptr<IRil> RilTestUtil::GetRilInterface()
151 {
152 return rilTestUtil_.rilInterface_;
153 }
154
GetCallback()155 sptr<RilCallbackTest> RilTestUtil::GetCallback()
156 {
157 return rilTestUtil_.callback_;
158 }
159
GetInstance()160 RilTestUtil &RilTestUtil::GetInstance()
161 {
162 return rilTestUtil_;
163 }
164
165 } // namespace Telephony
166 } // namespace OHOS