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 #ifndef OHOS_STK_CONTROLLER_H
17 #define OHOS_STK_CONTROLLER_H
18 
19 #include "system_ability_definition.h"
20 #include "system_ability_status_change_stub.h"
21 #include "common_event_subscriber.h"
22 #include "telephony_state_registry_client.h"
23 #include "i_tel_ril_manager.h"
24 #include "inner_event.h"
25 #include "sim_constant.h"
26 #include "sim_state_manager.h"
27 #include "tel_event_handler.h"
28 #include "want.h"
29 
30 #define STK_CMD_TYPE_INDEX 10
31 #define STK_CMD_TYPE_LEN 2
32 #define STK_BIP_CMD_OPEN_CHANNEL "40"
33 #define STK_BIP_CMD_SEND_DATA "43"
34 #define STK_BIP_CMD_RECEVIE_DATA "42"
35 #define STK_BIP_CMD_GET_CHANNEL_STATUS "44"
36 #define STK_BIP_CMD_CLOSE_CHANNEL "41"
37 
38 namespace OHOS {
39 namespace Telephony {
40 using namespace OHOS::EventFwk;
41 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo;
42 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber;
43 class StkController : public TelEventHandler {
44 public:
45     explicit StkController(const std::weak_ptr<Telephony::ITelRilManager> &telRilManager,
46         const std::weak_ptr<Telephony::SimStateManager> &simStateManager, int32_t slotId);
47     ~StkController();
48     void Init();
49     std::string initStkBudleName();
50     int32_t SendTerminalResponseCmd(const std::string &strCmd);
51     int32_t SendEnvelopeCmd(const std::string &strCmd);
52     int32_t SendCallSetupRequestResult(bool accept);
53     void UnRegisterEvents();
54 
55 public:
56     enum {
57         RETRY_SEND_RIL_PROACTIVE_COMMAND = 0,
58     };
59 
60 private:
61     void RegisterEvents();
62     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
63     void OnIccStateChanged(const AppExecFwk::InnerEvent::Pointer &event);
64     void OnSendRilSessionEnd(const AppExecFwk::InnerEvent::Pointer &event);
65     void OnSendRilProactiveCommand(const AppExecFwk::InnerEvent::Pointer &event);
66     void OnSendRilAlphaNotify(const AppExecFwk::InnerEvent::Pointer &event);
67     void OnSendRilEventNotify(const AppExecFwk::InnerEvent::Pointer &event);
68     void OnIccRefresh(const AppExecFwk::InnerEvent::Pointer &event);
69     bool PublishStkEvent(AAFwk::Want &want);
70     void OnSendTerminalResponseResult(const AppExecFwk::InnerEvent::Pointer &event);
71     void OnSendEnvelopeCmdResult(const AppExecFwk::InnerEvent::Pointer &event);
72     void OnSendCallSetupRequestResult(const AppExecFwk::InnerEvent::Pointer &event);
73     bool CheckIsSystemApp(const std::string &bundleName);
74     bool CheckIsBipCmd(const std::string &cmdData);
75     sptr<OHOS::IRemoteObject> GetBundleMgr();
76     void RetrySendRilProactiveCommand();
77     void UnSubscribeListeners();
78     void InitListener();
79     void SubscribeBundleScanFinished();
80     void OnReceiveBms();
81 
82 private:
83     class BundleScanFinishedEventSubscriber : public CommonEventSubscriber {
84     public:
BundleScanFinishedEventSubscriber(const CommonEventSubscribeInfo & info,StkController & handler)85         explicit BundleScanFinishedEventSubscriber(
86             const CommonEventSubscribeInfo &info, StkController &handler)
87             : CommonEventSubscriber(info), handler_(handler) {}
88         ~BundleScanFinishedEventSubscriber() = default;
89         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override;
90         StkController &handler_;
91     };
92 
93     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
94     public:
SystemAbilityStatusChangeListener(StkController & handler)95         explicit SystemAbilityStatusChangeListener(StkController &handler) : handler_(handler) {};
96         ~SystemAbilityStatusChangeListener() = default;
97         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
98         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
99 
100     private:
101         StkController &handler_;
102     };
103 
104 private:
105     std::weak_ptr<Telephony::ITelRilManager> telRilManager_;
106     std::weak_ptr<Telephony::SimStateManager> simStateManager_;
107     int slotId_ = 0;
108     int32_t iccCardState_ = 0;
109     int32_t envelopeResponseResult_ = 0;
110     int32_t terminalResponseResult_ = 0;
111     int32_t callSetupResponseResult_ = 0;
112     bool responseFinished_ = false;
113     std::string stkBundleName_ = "";
114     std::mutex stkMutex_;
115     std::condition_variable stkCv_;
116     AAFwk::Want retryWant_;
117     int32_t remainTryCount_ = 0;
118     bool isProactiveCommandSucc = false;
119     std::shared_ptr<BundleScanFinishedEventSubscriber> bundleScanFinishedSubscriber_ = nullptr;
120     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
121 };
122 } // namespace Telephony
123 } // namespace OHOS
124 #endif // OHOS_STK_CONTROLLER_H
125