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