• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "stk_controller.h"
17 
18 #include "ability_manager_client.h"
19 #include "bundle_mgr_proxy.h"
20 #include "common_event_data.h"
21 #include "common_event_manager.h"
22 #include "common_event_publish_info.h"
23 #include "common_event_support.h"
24 #include "extension_ability_info.h"
25 #ifdef CORE_SERVICE_SUPPORT_ESIM
26 #include "esim_controller.h"
27 #endif
28 #include "tel_ril_types.h"
29 #include "if_system_ability_manager.h"
30 #include "iservice_registry.h"
31 #include "parameters.h"
32 #include "radio_event.h"
33 #include "system_ability_definition.h"
34 #include "telephony_errors.h"
35 #include "telephony_log_wrapper.h"
36 #include "telephony_ext_wrapper.h"
37 
38 namespace OHOS {
39 namespace Telephony {
40 namespace {
41 const int32_t ICC_CARD_STATE_ABSENT = 0;
42 const int32_t ICC_CARD_STATE_PRESENT = 1;
43 const int32_t WAIT_TIME_SECOND = 2; // Set the timeout for sending the stk command
44 const int32_t PARAMETER_LENGTH = 128;
45 const int64_t DELAY_TIME = 3000;
46 const int32_t MAX_RETRY_COUNT = 10;
47 const int32_t REFRESH_RESULT_FILE_UPDATE = 0;
48 const std::string PARAM_SLOTID = "slotId";
49 const std::string PARAM_MSG_CMD = "msgCmd";
50 const std::string PARAM_CARD_STATUS = "cardStatus";
51 const std::string PARAM_ALPHA_STRING = "alphaString";
52 const std::string PARAM_REFRESH_RESULT = "refreshResult";
53 const std::string STK_BUNDLE = "const.telephony.stk_bundle_name";
54 const std::string ABILITY_NAME = "ServiceExtAbility";
55 const std::string DEFAULT_BUNDLE = "";
56 } // namespace
57 
StkController(const std::weak_ptr<Telephony::ITelRilManager> & telRilManager,const std::weak_ptr<Telephony::SimStateManager> & simStateManager,int32_t slotId)58 StkController::StkController(const std::weak_ptr<Telephony::ITelRilManager> &telRilManager,
59     const std::weak_ptr<Telephony::SimStateManager> &simStateManager, int32_t slotId)
60     : TelEventHandler("StkController"), telRilManager_(telRilManager), simStateManager_(simStateManager),
61       slotId_(slotId)
62 {}
63 
~StkController()64 StkController::~StkController()
65 {
66     UnSubscribeListeners();
67 }
68 
Init()69 void StkController::Init()
70 {
71     stkBundleName_ = initStkBudleName();
72     RegisterEvents();
73     if (TELEPHONY_EXT_WRAPPER.initBip_ != nullptr) {
74         TELEPHONY_EXT_WRAPPER.initBip_(slotId_);
75     }
76     InitListener();
77 }
78 
UnSubscribeListeners()79 void StkController::UnSubscribeListeners()
80 {
81     if (bundleScanFinishedSubscriber_ != nullptr &&
82         CommonEventManager::UnSubscribeCommonEvent(bundleScanFinishedSubscriber_)) {
83         bundleScanFinishedSubscriber_ = nullptr;
84         TELEPHONY_LOGI("Unsubscribe Bundle Scan Finished success");
85     }
86     if (statusChangeListener_ != nullptr) {
87         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88         if (samgrProxy != nullptr) {
89             samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, statusChangeListener_);
90             statusChangeListener_ = nullptr;
91             TELEPHONY_LOGI("Unsubscribe COMMON_EVENT_SERVICE_ID success");
92         }
93     }
94 }
95 
InitListener()96 void StkController::InitListener()
97 {
98     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
99     statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(*this);
100     if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
101         TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
102         return;
103     }
104     auto ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
105     TELEPHONY_LOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result is %{public}d", ret);
106 }
107 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)108 void StkController::SystemAbilityStatusChangeListener::OnAddSystemAbility(int32_t systemAbilityId,
109     const std::string &deviceId)
110 {
111     switch (systemAbilityId) {
112         case COMMON_EVENT_SERVICE_ID: {
113             TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID is running");
114             handler_.SubscribeBundleScanFinished();
115             break;
116         }
117         default:
118             TELEPHONY_LOGE("systemAbilityId is invalid");
119             break;
120     }
121 }
122 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)123 void StkController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(int32_t systemAbilityId,
124     const std::string &deviceId)
125 {
126     switch (systemAbilityId) {
127         case COMMON_EVENT_SERVICE_ID: {
128             handler_.UnSubscribeListeners();
129             TELEPHONY_LOGI("COMMON_EVENT_SERVICE_ID stopped");
130             break;
131         }
132         default:
133             TELEPHONY_LOGE("systemAbilityId is invalid");
134             break;
135     }
136 }
137 
SubscribeBundleScanFinished()138 void StkController::SubscribeBundleScanFinished()
139 {
140     if (bundleScanFinishedSubscriber_ != nullptr) {
141         TELEPHONY_LOGW("Bundle Scan Finished has Subscribed");
142         return;
143     }
144     MatchingSkills matchingSkills;
145     matchingSkills.AddEvent(BUNDLE_SCAN_FINISHED_EVENT);
146     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
147     subscriberInfo.SetThreadMode(CommonEventSubscribeInfo::COMMON);
148     bundleScanFinishedSubscriber_ = std::make_shared<BundleScanFinishedEventSubscriber>(subscriberInfo, *this);
149     if (CommonEventManager::SubscribeCommonEvent(bundleScanFinishedSubscriber_)) {
150         TELEPHONY_LOGI("Subscribe Bundle Scan Finished success");
151     } else {
152         bundleScanFinishedSubscriber_ = nullptr;
153         TELEPHONY_LOGE("Subscribe Bundle Scan Finished fail");
154     }
155 }
156 
OnReceiveEvent(const CommonEventData & data)157 void StkController::BundleScanFinishedEventSubscriber::OnReceiveEvent(const CommonEventData &data)
158 {
159     OHOS::EventFwk::Want want = data.GetWant();
160     std::string action = want.GetAction();
161     TELEPHONY_LOGI("action = %{public}s", action.c_str());
162     if (action == BUNDLE_SCAN_FINISHED_EVENT) {
163         handler_.OnReceiveBms();
164     }
165 }
166 
OnReceiveBms()167 void StkController::OnReceiveBms()
168 {
169     if (!retryWant_.GetStringParam(PARAM_MSG_CMD).empty() && !isProactiveCommandSucc) {
170         if (remainTryCount_ == 0) {
171             remainTryCount_ = MAX_RETRY_COUNT;
172             TELEPHONY_LOGI("OnReceiveBms retry send stkdata");
173             SendEvent(StkController::RETRY_SEND_RIL_PROACTIVE_COMMAND, 0, DELAY_TIME);
174         } else {
175             remainTryCount_ = MAX_RETRY_COUNT;
176         }
177     }
178 }
179 
initStkBudleName()180 std::string StkController::initStkBudleName()
181 {
182     char bundleName[PARAMETER_LENGTH] = { 0 };
183     GetParameter(STK_BUNDLE.c_str(), DEFAULT_BUNDLE.c_str(), bundleName, PARAMETER_LENGTH);
184     return bundleName;
185 }
186 
GetBundleMgr()187 sptr<OHOS::IRemoteObject> StkController::GetBundleMgr()
188 {
189     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
190         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
191     if (systemAbilityManager == nullptr) {
192         TELEPHONY_LOGE("Failed to get ability mgr.");
193         return nullptr;
194     }
195     return systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
196 }
197 
RegisterEvents()198 void StkController::RegisterEvents()
199 {
200     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
201     if (telRilManager == nullptr) {
202         TELEPHONY_LOGE("StkController[%{public}d]::RegisterEvents() telRilManager is nullptr", slotId_);
203         return;
204     }
205     std::shared_ptr<SimStateManager> simStateManager = simStateManager_.lock();
206     if (simStateManager == nullptr) {
207         TELEPHONY_LOGE("StkController[%{public}d]::RegisterEvents() simStateManager is nullptr", slotId_);
208         return;
209     }
210     simStateManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
211     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_SESSION_END, nullptr);
212     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_PROACTIVE_COMMAND, nullptr);
213     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_ALPHA_NOTIFY, nullptr);
214     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_EVENT_NOTIFY, nullptr);
215     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_CALL_SETUP, nullptr);
216     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ICC_REFRESH, nullptr);
217     telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED, nullptr);
218 }
219 
UnRegisterEvents()220 void StkController::UnRegisterEvents()
221 {
222     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
223     if (telRilManager == nullptr) {
224         TELEPHONY_LOGE("StkController[%{public}d]::UnRegisterEvents() telRilManager is nullptr", slotId_);
225         return;
226     }
227     std::shared_ptr<SimStateManager> simStateManager = simStateManager_.lock();
228     if (simStateManager == nullptr) {
229         TELEPHONY_LOGE("StkController[%{public}d]::UnRegisterEvents() simStateManager is nullptr", slotId_);
230         return;
231     }
232     simStateManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
233     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_SESSION_END);
234     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_PROACTIVE_COMMAND);
235     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_ALPHA_NOTIFY);
236     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_EVENT_NOTIFY);
237     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STK_CALL_SETUP);
238     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_ICC_REFRESH);
239     telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
240 }
241 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)242 void StkController::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
243 {
244     if (event == nullptr) {
245         TELEPHONY_LOGE("StkController[%{public}d]::ProcessEvent() event is nullptr", slotId_);
246         return;
247     }
248     uint32_t id = event->GetInnerEventId();
249     switch (id) {
250         case RadioEvent::RADIO_SIM_STATE_CHANGE:
251             OnIccStateChanged(event);
252             break;
253         case RadioEvent::RADIO_STK_SESSION_END:
254             OnSendRilSessionEnd(event);
255             break;
256         case RadioEvent::RADIO_STK_PROACTIVE_COMMAND:
257             OnSendRilProactiveCommand(event);
258             break;
259         case RadioEvent::RADIO_STK_ALPHA_NOTIFY:
260             OnSendRilAlphaNotify(event);
261             break;
262         case RadioEvent::RADIO_STK_EVENT_NOTIFY:
263             OnSendRilEventNotify(event);
264             break;
265         case RadioEvent::RADIO_STK_CALL_SETUP:
266             TELEPHONY_LOGI("StkController[%{public}d]::ProcessEvent(),"
267                 " event notify command supplied all the information needed for set up call processing", slotId_);
268             break;
269         case RadioEvent::RADIO_ICC_REFRESH:
270             OnIccRefresh(event);
271             break;
272         case RadioEvent::RADIO_STK_SEND_TERMINAL_RESPONSE:
273             OnSendTerminalResponseResult(event);
274             break;
275         case RadioEvent::RADIO_STK_SEND_ENVELOPE:
276             OnSendEnvelopeCmdResult(event);
277             break;
278         case RadioEvent::RADIO_STK_IS_READY:
279             TELEPHONY_LOGI("StkController[%{public}d]::ProcessEvent() SimStkIsReady done", slotId_);
280             break;
281         case RadioEvent::RADIO_STK_SEND_CALL_SETUP_REQUEST_RESULT:
282             OnSendCallSetupRequestResult(event);
283             break;
284         case StkController::RETRY_SEND_RIL_PROACTIVE_COMMAND:
285             RetrySendRilProactiveCommand();
286             break;
287         default:
288             ProcessEventExt(id, event);
289             break;
290     }
291 }
292 
ProcessEventExt(uint32_t id,const AppExecFwk::InnerEvent::Pointer & event)293 void StkController::ProcessEventExt(uint32_t id, const AppExecFwk::InnerEvent::Pointer &event)
294 {
295     switch (id) {
296         case RadioEvent::RADIO_STATE_CHANGED:
297             OnRadioStateChanged(event);
298             break;
299         default:
300             TELEPHONY_LOGE("StkController[%{public}d]::ProcessEvent() unknown event", slotId_);
301             break;
302     }
303 }
304 
OnIccStateChanged(const AppExecFwk::InnerEvent::Pointer & event)305 void StkController::OnIccStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
306 {
307     std::shared_ptr<SimStateManager> simStateManager = simStateManager_.lock();
308     if (simStateManager == nullptr) {
309         TELEPHONY_LOGE("StkController[%{public}d]::OnIccStateChanged() simStateManager is nullptr", slotId_);
310         return;
311     }
312     int32_t newState = simStateManager->HasSimCard() ? ICC_CARD_STATE_PRESENT : ICC_CARD_STATE_ABSENT;
313     int32_t oldState = iccCardState_;
314     iccCardState_ = newState;
315     TELEPHONY_LOGI("StkController[%{public}d]::OnIccStateChanged(), oldState: %{public}d newState: %{public}d",
316         slotId_, oldState, newState);
317     if (oldState == ICC_CARD_STATE_PRESENT && newState == ICC_CARD_STATE_ABSENT) {
318         AAFwk::Want want;
319         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_CARD_STATE_CHANGED);
320         want.SetParam(PARAM_SLOTID, slotId_);
321         want.SetParam(PARAM_CARD_STATUS, iccCardState_);
322         bool publishResult = PublishStkEvent(want);
323         TELEPHONY_LOGI("StkController[%{public}d]::OnIccStateChanged() publishResult = %{public}d",
324             slotId_, publishResult);
325     } else if (oldState == ICC_CARD_STATE_ABSENT && newState == ICC_CARD_STATE_PRESENT) {
326         TELEPHONY_LOGI("StkController[%{public}d]::OnIccStateChanged(), call SimStkIsReady()", slotId_);
327         auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_STK_IS_READY);
328         if (event == nullptr) {
329             TELEPHONY_LOGE("StkController[%{public}d]::OnIccStateChanged() event is nullptr", slotId_);
330             return;
331         }
332         event->SetOwner(shared_from_this());
333         std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
334         if (telRilManager == nullptr) {
335             TELEPHONY_LOGE("StkController[%{public}d]::OnIccStateChanged() telRilManager is nullptr", slotId_);
336             return;
337         }
338         telRilManager->SimStkIsReady(slotId_, event);
339     }
340 }
341 
OnSendRilSessionEnd(const AppExecFwk::InnerEvent::Pointer & event)342 void StkController::OnSendRilSessionEnd(const AppExecFwk::InnerEvent::Pointer &event)
343 {
344     AAFwk::Want want;
345     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_SESSION_END);
346     want.SetParam(PARAM_SLOTID, slotId_);
347     bool publishResult = PublishStkEvent(want);
348     TELEPHONY_LOGI("StkController[%{public}d]::OnSendRilSessionEnd() publishResult = %{public}d",
349         slotId_, publishResult);
350 }
351 
SafeSubstr(const std::string & str,size_t pos,size_t len)352 std::string SafeSubstr(const std::string& str, size_t pos, size_t len)
353 {
354     if (pos >= str.length()) {
355         TELEPHONY_LOGE("string pos abnormal. str: %{public}s  pos: %{public}zu, len: %{public}zu",
356             str.c_str(), pos, len);
357         return "";
358     }
359 
360     if (pos + len > str.length()) {
361         TELEPHONY_LOGI("string len abnormal: str: %{public}s  pos: %{public}zu, len: %{public}zu",
362             str.c_str(), pos, len);
363         len = str.length() - pos;
364     }
365 
366     return str.substr(pos, len);
367 }
368 
HandleStkBipCmd(const std::string & cmdData)369 void StkController::HandleStkBipCmd(const std::string &cmdData)
370 {
371     std::string commandLen = SafeSubstr(cmdData, STK_CMD_CMD_LEN_INDEX, STK_CMD_TYPE_LEN);
372     uint32_t typeOffset;
373     if (commandLen == "") {
374         return;
375     } else if (commandLen == STK_CMD_CMD_LEN_81) {
376         typeOffset = STK_CMD_TYPE_81_INDEX;
377     } else if (commandLen == STK_CMD_CMD_LEN_82) {
378         typeOffset = STK_CMD_TYPE_82_INDEX;
379     } else if (commandLen == STK_CMD_CMD_LEN_83) {
380         typeOffset = STK_CMD_TYPE_83_INDEX;
381     } else {
382         typeOffset = STK_CMD_TYPE_80_INDEX;
383     }
384 
385     std::string commandType = SafeSubstr(cmdData, typeOffset, STK_CMD_TYPE_LEN);
386     if (commandType == STK_BIP_CMD_OPEN_CHANNEL || commandType == STK_BIP_CMD_SEND_DATA ||
387         commandType == STK_BIP_CMD_RECEVIE_DATA || commandType == STK_BIP_CMD_GET_CHANNEL_STATUS ||
388         commandType == STK_BIP_CMD_CLOSE_CHANNEL || commandType == STK_BIP_CMD_SET_UP_EVENT_LIST) {
389         if (TELEPHONY_EXT_WRAPPER.sendEvent_ &&
390             TELEPHONY_EXT_WRAPPER.sendEvent_(std::make_shared<std::string>(cmdData), slotId_)) {
391                 TELEPHONY_LOGI("sendEvent_. slotId_ [%{public}d]", slotId_);
392         }
393     }
394 }
395 
OnSendRilProactiveCommand(const AppExecFwk::InnerEvent::Pointer & event)396 void StkController::OnSendRilProactiveCommand(const AppExecFwk::InnerEvent::Pointer &event)
397 {
398     auto stkData = event->GetSharedObject<std::string>();
399     if (stkData == nullptr) {
400         TELEPHONY_LOGE("StkController[%{public}d]::OnSendRilProactiveCommand() stkData is nullptr", slotId_);
401         return;
402     }
403 
404     std::string cmdData = (std::string)*stkData;
405     HandleStkBipCmd(cmdData);
406 
407 #ifdef CORE_SERVICE_SUPPORT_ESIM
408     if (EsimController::GetInstance().ChecIsVerifyBindCommand(cmdData)) {
409         EsimController::GetInstance().ProcessCommandMessage(slotId_, cmdData);
410         return;
411     }
412 #endif
413 
414     AAFwk::Want want;
415     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_COMMAND);
416     want.SetParam(PARAM_SLOTID, slotId_);
417     want.SetParam(PARAM_MSG_CMD, cmdData);
418     bool publishResult = PublishStkEvent(want);
419     TELEPHONY_LOGI("StkController[%{public}d]::OnSendRilProactiveCommand() stkData = %{public}s "
420         "publishResult = %{public}d", slotId_, cmdData.c_str(), publishResult);
421     if (!publishResult) {
422         retryWant_ = want;
423         remainTryCount_ = MAX_RETRY_COUNT;
424         SendEvent(StkController::RETRY_SEND_RIL_PROACTIVE_COMMAND, 0, DELAY_TIME);
425         return;
426     }
427     isProactiveCommandSucc = true;
428     remainTryCount_ = 0;
429 }
430 
RetrySendRilProactiveCommand()431 void StkController::RetrySendRilProactiveCommand()
432 {
433     remainTryCount_--;
434     TELEPHONY_LOGI("StkController[%{public}d], remainTryCount_ is %{public}d", slotId_, remainTryCount_);
435     if (remainTryCount_ > 0) {
436         if (!PublishStkEvent(retryWant_)) {
437             SendEvent(StkController::RETRY_SEND_RIL_PROACTIVE_COMMAND, 0, DELAY_TIME);
438             return;
439         }
440         TELEPHONY_LOGI("StkController[%{public}d] retry sucess", slotId_);
441         isProactiveCommandSucc = true;
442         remainTryCount_ = 0;
443         return;
444     }
445     TELEPHONY_LOGI("StkController[%{public}d] stop retry", slotId_);
446 }
447 
OnSendRilAlphaNotify(const AppExecFwk::InnerEvent::Pointer & event)448 void StkController::OnSendRilAlphaNotify(const AppExecFwk::InnerEvent::Pointer &event)
449 {
450     auto alphaData = event->GetSharedObject<std::string>();
451     if (alphaData == nullptr) {
452         TELEPHONY_LOGE("StkController[%{public}d]::OnSendRilAlphaNotify() alphaData is nullptr", slotId_);
453         return;
454     }
455     std::string cmdData = (std::string)*alphaData;
456     AAFwk::Want want;
457     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_ALPHA_IDENTIFIER);
458     want.SetParam(PARAM_SLOTID, slotId_);
459     want.SetParam(PARAM_ALPHA_STRING, cmdData);
460     bool publishResult = PublishStkEvent(want);
461     TELEPHONY_LOGI("StkController[%{public}d]::OnSendRilAlphaNotify() alphaData = %{public}s "
462         "publishResult = %{public}d", slotId_, cmdData.c_str(), publishResult);
463 }
464 
OnSendRilEventNotify(const AppExecFwk::InnerEvent::Pointer & event)465 void StkController::OnSendRilEventNotify(const AppExecFwk::InnerEvent::Pointer &event)
466 {
467     auto eventData = event->GetSharedObject<std::string>();
468     if (eventData == nullptr) {
469         TELEPHONY_LOGE("StkController[%{public}d]::OnSendRilEventNotify() eventData is nullptr", slotId_);
470         return;
471     }
472     std::string cmdData = (std::string)*eventData;
473     AAFwk::Want want;
474     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_COMMAND);
475     want.SetParam(PARAM_SLOTID, slotId_);
476     want.SetParam(PARAM_MSG_CMD, cmdData);
477     bool publishResult = PublishStkEvent(want);
478     TELEPHONY_LOGI("StkController[%{public}d]::OnSendRilEventNotify() eventData = %{public}s "
479         "publishResult = %{public}d", slotId_, cmdData.c_str(), publishResult);
480 }
481 
OnIccRefresh(const AppExecFwk::InnerEvent::Pointer & event)482 void StkController::OnIccRefresh(const AppExecFwk::InnerEvent::Pointer &event)
483 {
484     auto refreshResult = event->GetSharedObject<int32_t>();
485     int32_t result = REFRESH_RESULT_FILE_UPDATE;
486     if (refreshResult == nullptr) {
487         TELEPHONY_LOGE("StkController[%{public}d]::OnIccRefresh() refreshResult is nullptr", slotId_);
488     } else {
489         result = (int32_t)*refreshResult;
490     }
491     AAFwk::Want want;
492     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_STK_CARD_STATE_CHANGED);
493     want.SetParam(PARAM_SLOTID, slotId_);
494     want.SetParam(PARAM_CARD_STATUS, ICC_CARD_STATE_PRESENT);
495     want.SetParam(PARAM_REFRESH_RESULT, result);
496     bool publishResult = PublishStkEvent(want);
497     TELEPHONY_LOGI("StkController[%{public}d]::OnIccRefresh() refresh result = %{public}d publishResult = %{public}d",
498         slotId_, result, publishResult);
499 }
500 
PublishStkEvent(AAFwk::Want & want)501 bool StkController::PublishStkEvent(AAFwk::Want &want)
502 {
503     if (stkBundleName_.empty()) {
504         TELEPHONY_LOGE("stkBundleName_ is empty");
505         return false;
506     }
507     if (!CheckIsSystemApp(stkBundleName_)) {
508         TELEPHONY_LOGE("is not system app");
509         return false;
510     }
511     AppExecFwk::ElementName element("", stkBundleName_, ABILITY_NAME);
512     want.SetElement(element);
513     int32_t accountId = -1;
514     auto ret = AAFwk::AbilityManagerClient::GetInstance()->StartExtensionAbility(
515         want, nullptr, accountId, AppExecFwk::ExtensionAbilityType::SERVICE);
516     return ret == 0;
517 }
518 
CheckIsSystemApp(const std::string & bundleName)519 bool StkController::CheckIsSystemApp(const std::string &bundleName)
520 {
521     sptr<OHOS::IRemoteObject> remoteObject = GetBundleMgr();
522     if (remoteObject == nullptr) {
523         TELEPHONY_LOGE("error to get bundleMgr");
524         return false;
525     }
526     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
527     if (iBundleMgr == nullptr) {
528         TELEPHONY_LOGE("iBundleMgr is null");
529         return false;
530     }
531     OHOS::AppExecFwk::BundleInfo info;
532     info.applicationInfo.isSystemApp = false;
533     if (!iBundleMgr->GetBundleInfo(
534         bundleName, OHOS::AppExecFwk::GET_BUNDLE_DEFAULT, info, AppExecFwk::Constants::ALL_USERID)) {
535         TELEPHONY_LOGE("Failed to get bundleInfo from bundleMgr");
536     } else {
537         TELEPHONY_LOGI("isSystemApp =%{public}d", info.applicationInfo.isSystemApp);
538     }
539     return info.applicationInfo.isSystemApp;
540 }
541 
SendTerminalResponseCmd(const std::string & strCmd)542 int32_t StkController::SendTerminalResponseCmd(const std::string &strCmd)
543 {
544     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_STK_SEND_TERMINAL_RESPONSE);
545     if (event == nullptr) {
546         TELEPHONY_LOGE("StkController[%{public}d]::SendTerminalResponseCmd() event is nullptr", slotId_);
547         return TELEPHONY_ERR_LOCAL_PTR_NULL;
548     }
549     event->SetOwner(shared_from_this());
550     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
551     if (telRilManager == nullptr) {
552         TELEPHONY_LOGE("StkController[%{public}d]::SendTerminalResponseCmd() telRilManager is nullptr", slotId_);
553         return TELEPHONY_ERR_LOCAL_PTR_NULL;
554     }
555 
556     std::unique_lock<std::mutex> terminalResponselock(stkMutex_);
557     terminalResponseResult_ = 0;
558     responseFinished_ = false;
559     telRilManager->SendTerminalResponseCmd(slotId_, strCmd, event);
560     while (!responseFinished_) {
561         TELEPHONY_LOGI("StkController[%{public}d]::SendTerminalResponseCmd() wait for the response to finish", slotId_);
562         if (stkCv_.wait_for(terminalResponselock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
563             TELEPHONY_LOGE("StkController[%{public}d]::SendTerminalResponseCmd() wait timeout", slotId_);
564             break;
565         }
566     }
567     if (!responseFinished_) {
568         TELEPHONY_LOGE("ril cmd fail");
569         return TELEPHONY_ERR_RIL_CMD_FAIL;
570     }
571     return TELEPHONY_SUCCESS;
572 }
573 
SendEnvelopeCmd(const std::string & strCmd)574 int32_t StkController::SendEnvelopeCmd(const std::string &strCmd)
575 {
576     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_STK_SEND_ENVELOPE);
577     if (event == nullptr) {
578         TELEPHONY_LOGE("StkController[%{public}d]::SendEnvelopeCmd() event is nullptr", slotId_);
579         return TELEPHONY_ERR_LOCAL_PTR_NULL;
580     }
581     event->SetOwner(shared_from_this());
582     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
583     if (telRilManager == nullptr) {
584         TELEPHONY_LOGE("StkController[%{public}d]::SendEnvelopeCmd() telRilManager is nullptr", slotId_);
585         return TELEPHONY_ERR_LOCAL_PTR_NULL;
586     }
587 
588     std::unique_lock<std::mutex> envelopelock(stkMutex_);
589     envelopeResponseResult_ = 0;
590     responseFinished_ = false;
591     telRilManager->SendEnvelopeCmd(slotId_, strCmd, event);
592     while (!responseFinished_) {
593         TELEPHONY_LOGI("StkController[%{public}d]::SendEnvelopeCmd() wait for the response to finish", slotId_);
594         if (stkCv_.wait_for(envelopelock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
595             TELEPHONY_LOGE("StkController[%{public}d]::SendEnvelopeCmd() wait timeout", slotId_);
596             break;
597         }
598     }
599     if (!responseFinished_) {
600         TELEPHONY_LOGE("ril cmd fail");
601         return TELEPHONY_ERR_RIL_CMD_FAIL;
602     }
603     return TELEPHONY_SUCCESS;
604 }
605 
SendCallSetupRequestResult(bool accept)606 int32_t StkController::SendCallSetupRequestResult(bool accept)
607 {
608     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_STK_SEND_CALL_SETUP_REQUEST_RESULT);
609     if (event == nullptr) {
610         TELEPHONY_LOGE("StkController[%{public}d]::SendCallSetupRequestResult() event is nullptr", slotId_);
611         return TELEPHONY_ERR_FAIL;
612     }
613     event->SetOwner(shared_from_this());
614     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
615     if (telRilManager == nullptr) {
616         TELEPHONY_LOGE("StkController[%{public}d]::SendCallSetupRequestResult() telRilManager is nullptr", slotId_);
617         return TELEPHONY_ERR_FAIL;
618     }
619 
620     std::unique_lock<std::mutex> callSetupRequestlock(stkMutex_);
621     callSetupResponseResult_ = TELEPHONY_ERR_FAIL;
622     responseFinished_ = false;
623     telRilManager->SendCallSetupRequestResult(slotId_, accept, event);
624     while (!responseFinished_) {
625         TELEPHONY_LOGI(
626             "StkController[%{public}d]::SendCallSetupRequestResult() wait for the response to finish", slotId_);
627         if (stkCv_.wait_for(callSetupRequestlock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
628             TELEPHONY_LOGE("StkController[%{public}d]::SendCallSetupRequestResult() wait timeout", slotId_);
629             responseFinished_ = true;
630         }
631     }
632     return callSetupResponseResult_;
633 }
634 
OnSendTerminalResponseResult(const AppExecFwk::InnerEvent::Pointer & event)635 void StkController::OnSendTerminalResponseResult(const AppExecFwk::InnerEvent::Pointer &event)
636 {
637     if (event == nullptr) {
638         TELEPHONY_LOGE("StkController[%{public}d]::OnSendTerminalResponseResult() event is nullptr", slotId_);
639         return;
640     }
641     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
642     if (response == nullptr) {
643         TELEPHONY_LOGE("StkController[%{public}d]::OnSendTerminalResponseResult() response is nullptr", slotId_);
644         return;
645     }
646     terminalResponseResult_ = response->error == ErrType::NONE;
647     TELEPHONY_LOGI("StkController[%{public}d]::OnSendTerminalResponseResult(), result = %{public}d",
648         slotId_, terminalResponseResult_);
649     responseFinished_ = true;
650     stkCv_.notify_one();
651 }
652 
OnSendEnvelopeCmdResult(const AppExecFwk::InnerEvent::Pointer & event)653 void StkController::OnSendEnvelopeCmdResult(const AppExecFwk::InnerEvent::Pointer &event)
654 {
655     if (event == nullptr) {
656         TELEPHONY_LOGE("StkController[%{public}d]::OnSendEnvelopeCmdResult() event is nullptr", slotId_);
657         return;
658     }
659     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
660     if (response == nullptr) {
661         TELEPHONY_LOGE("StkController[%{public}d]::OnSendEnvelopeCmdResult() response is nullptr", slotId_);
662         return;
663     }
664     envelopeResponseResult_ = response->error == ErrType::NONE;
665     TELEPHONY_LOGI("StkController[%{public}d]::OnSendEnvelopeCmdResult(), result = %{public}d",
666         slotId_, envelopeResponseResult_);
667     responseFinished_ = true;
668     stkCv_.notify_one();
669 }
670 
OnSendCallSetupRequestResult(const AppExecFwk::InnerEvent::Pointer & event)671 void StkController::OnSendCallSetupRequestResult(const AppExecFwk::InnerEvent::Pointer &event)
672 {
673     if (event == nullptr) {
674         TELEPHONY_LOGE("StkController[%{public}d]::OnSendCallSetupRequestResult() event is nullptr", slotId_);
675         return;
676     }
677     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
678     if (response == nullptr) {
679         TELEPHONY_LOGE("StkController[%{public}d]::OnSendCallSetupRequestResult() response is nullptr", slotId_);
680         return;
681     }
682     callSetupResponseResult_ = response->error == ErrType::NONE ? TELEPHONY_ERR_SUCCESS : TELEPHONY_ERR_FAIL;
683     TELEPHONY_LOGI("StkController[%{public}d]::OnSendCallSetupRequestResult(), result = %{public}d",
684         slotId_, callSetupResponseResult_);
685     responseFinished_ = true;
686     stkCv_.notify_one();
687 }
688 
OnRadioStateChanged(const AppExecFwk::InnerEvent::Pointer & event)689 void StkController::OnRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
690 {
691     std::shared_ptr<Int32Parcel> object = event->GetSharedObject<Int32Parcel>();
692     if (object == nullptr) {
693         TELEPHONY_LOGE("StkController[%{public}d]::OnRadioStateChanged object is nullptr", slotId_);
694         return;
695     }
696 
697     int32_t radioState = object->data;
698     if (radioState == CORE_SERVICE_POWER_NOT_AVAILABLE) {
699         TELEPHONY_LOGI("StkController[%{public}d]::OnRadioStateChanged radioState: -1, iccCardState: %{public}d "
700             "set to absent", slotId_, iccCardState_);
701         iccCardState_ = ICC_CARD_STATE_ABSENT;
702     }
703 }
704 
705 } // namespace Telephony
706 } // namespace OHOS
707