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_MULTI_SIM_MONITOR_H
17 #define OHOS_MULTI_SIM_MONITOR_H
18 
19 #include <list>
20 
21 #include "common_event_subscriber.h"
22 #include "iservice_registry.h"
23 #include "multi_sim_controller.h"
24 #include "sim_constant.h"
25 #include "sim_file_manager.h"
26 #include "system_ability_definition.h"
27 #include "system_ability_status_change_stub.h"
28 #include "tel_event_handler.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_state_registry_client.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 using namespace OHOS::EventFwk;
35 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo;
36 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber;
37 class MultiSimMonitor : public TelEventHandler {
38 public:
39     explicit MultiSimMonitor(const std::shared_ptr<MultiSimController> &controller,
40         std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,
41         std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager);
42     ~MultiSimMonitor();
43 
44     void Init();
45     void AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager,
46         std::shared_ptr<Telephony::SimFileManager> simFileManager);
47     void RegisterCoreNotify(int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what);
48     int32_t RegisterSimAccountCallback(const int32_t tokenId, const sptr<SimAccountCallback> &callback);
49     int32_t UnregisterSimAccountCallback(const int32_t tokenId);
50     void NotifySimAccountChanged();
51     void RegisterSimNotify();
52     void RegisterSimNotify(int32_t slotId);
53     void UnRegisterSimNotify();
54     bool IsVSimSlotId(int32_t slotId);
55 
56 public:
57     enum {
58         REGISTER_SIM_NOTIFY_EVENT = 0,
59         RESET_OPKEY_CONFIG = 1,
60         REGISTER_SIM_NOTIFY_RETRY_EVENT = 2,
61         INIT_DATA_RETRY_EVENT = 3
62     };
63 
64 private:
65     struct SimAccountCallbackRecord {
66         int32_t tokenId = 0;
67         sptr<SimAccountCallback> simAccountCallback = nullptr;
68     };
69 
70 private:
71     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
72     void RefreshData(int32_t slotId);
73     void InitData(int32_t slotId);
74     bool IsValidSlotId(int32_t slotId);
75     std::list<SimAccountCallbackRecord> GetSimAccountCallbackRecords();
76     void InitListener();
77     void SubscribeDataShareReady();
78     void UnSubscribeListeners();
79     void CheckOpcNeedUpdata(const bool isDataShareError);
80     int32_t CheckUpdateOpcVersion();
81     void ClearAllOpcCache();
82     void UpdateAllOpkeyConfigs();
83     void CheckDataShareError();
84     void CheckSimNotifyRegister();
85     void setRemainCount(int remainCount);
86 
87 private:
88     class DataShareEventSubscriber : public CommonEventSubscriber {
89     public:
DataShareEventSubscriber(const CommonEventSubscribeInfo & info,MultiSimMonitor & handler)90         explicit DataShareEventSubscriber(
91             const CommonEventSubscribeInfo &info, MultiSimMonitor &handler)
92             : CommonEventSubscriber(info), handler_(handler) {}
93         ~DataShareEventSubscriber() = default;
94         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override;
95         MultiSimMonitor &handler_;
96     };
97 
98     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
99     public:
SystemAbilityStatusChangeListener(MultiSimMonitor & handler)100         explicit SystemAbilityStatusChangeListener(MultiSimMonitor &handler) : handler_(handler) {};
101         ~SystemAbilityStatusChangeListener() = default;
102         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
103         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
104 
105     private:
106         MultiSimMonitor &handler_;
107     };
108 
109 private:
110     std::shared_ptr<MultiSimController> controller_ = nullptr;
111     std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager_;
112     std::vector<std::weak_ptr<Telephony::SimFileManager>> simFileManager_;
113     std::vector<int> isSimAccountLoaded_;
114     std::vector<int> initDataRemainCount_;
115     std::unique_ptr<ObserverHandler> observerHandler_ = nullptr;
116     std::list<SimAccountCallbackRecord> listSimAccountCallbackRecord_;
117     std::shared_ptr<DataShareEventSubscriber> dataShareSubscriber_ = nullptr;
118     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
119     std::mutex mutexInner_;
120     std::mutex mutexForData_;
121     std::atomic<int32_t> remainCount_ = 15;
122     int32_t maxSlotCount_ = 0;
123     bool isForgetAllDataDone_ = false;
124 };
125 } // namespace Telephony
126 } // namespace OHOS
127 #endif // OHOS_MULTI_SIM_MONITOR_H
128