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_manager.h"
17 
18 #include "telephony_errors.h"
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
StkManager(std::shared_ptr<ITelRilManager> telRilManager,std::shared_ptr<SimStateManager> simStateManager)23 StkManager::StkManager(std::shared_ptr<ITelRilManager> telRilManager, std::shared_ptr<SimStateManager> simStateManager)
24     : telRilManager_(telRilManager), simStateManager_(simStateManager)
25 {
26     TELEPHONY_LOGI("StkManager::StkManager()");
27 }
28 
~StkManager()29 StkManager::~StkManager()
30 {
31     if (stkController_ != nullptr) {
32         stkController_->UnRegisterEvents();
33     }
34 }
35 
Init(int slotId)36 void StkManager::Init(int slotId)
37 {
38     if (telRilManager_ == nullptr || simStateManager_ == nullptr) {
39         TELEPHONY_LOGE("StkManager[%{public}d]::Init() telRilManager or simStateManager_ is nullptr", slotId);
40         return;
41     }
42     stkController_ = std::make_shared<StkController>(telRilManager_, simStateManager_, slotId);
43     if (stkController_ == nullptr) {
44         TELEPHONY_LOGE("StkManager[%{public}d]::Init() failed to create StkController", slotId);
45         return;
46     }
47     stkController_->Init();
48     TELEPHONY_LOGI("StkManager[%{public}d]::Init() success", slotId);
49 }
50 
SendEnvelopeCmd(int32_t slotId,const std::string & cmd) const51 int32_t StkManager::SendEnvelopeCmd(int32_t slotId, const std::string &cmd) const
52 {
53     if (stkController_ == nullptr) {
54         TELEPHONY_LOGE("StkManager[%{public}d]::SendEnvelopeCmd() stkController_ is nullptr", slotId);
55         return TELEPHONY_ERR_LOCAL_PTR_NULL;
56     }
57     int32_t result = stkController_->SendEnvelopeCmd(cmd);
58     TELEPHONY_LOGI("StkManager[%{public}d]::SendEnvelopeCmd() result:%{public}s", slotId, (result ? "true" : "false"));
59     return result;
60 }
61 
SendTerminalResponseCmd(int32_t slotId,const std::string & cmd) const62 int32_t StkManager::SendTerminalResponseCmd(int32_t slotId, const std::string &cmd) const
63 {
64     if (stkController_ == nullptr) {
65         TELEPHONY_LOGE("StkManager[%{public}d]::SendTerminalResponseCmd() stkController_ is nullptr", slotId);
66         return TELEPHONY_ERR_LOCAL_PTR_NULL;
67     }
68     int32_t result = stkController_->SendTerminalResponseCmd(cmd);
69     TELEPHONY_LOGI("StkManager[%{public}d]::SendTerminalResponseCmd() result:%{public}s",
70         slotId, (result ? "true" : "false"));
71     return result;
72 }
73 
SendCallSetupRequestResult(int32_t slotId,bool accept) const74 int32_t StkManager::SendCallSetupRequestResult(int32_t slotId, bool accept) const
75 {
76     if (stkController_ == nullptr) {
77         TELEPHONY_LOGE("StkManager[%{public}d]::SendCallSetupRequestResult() stkController_ is nullptr", slotId);
78         return TELEPHONY_ERR_LOCAL_PTR_NULL;
79     }
80     int32_t result = stkController_->SendCallSetupRequestResult(accept);
81     TELEPHONY_LOGI("StkManager[%{public}d]::SendCallSetupRequestResult() result:%{public}d", slotId, result);
82     return result;
83 }
84 } // namespace Telephony
85 } // namespace OHOS
86