• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_CONTROLLER_H
17 #define OHOS_MULTI_SIM_CONTROLLER_H
18 
19 #include <list>
20 
21 #include <ffrt.h>
22 #include "if_system_ability_manager.h"
23 #include "radio_protocol_controller.h"
24 #include "sim_constant.h"
25 #include "sim_file_manager.h"
26 #include "sim_rdb_helper.h"
27 #include "sim_state_manager.h"
28 #include "telephony_errors.h"
29 #include "tel_event_handler.h"
30 #include "want.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 class MultiSimController : public TelEventHandler {
35 public:
36     MultiSimController(std::shared_ptr<Telephony::ITelRilManager> telRilManager,
37         std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager,
38         std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager);
39     virtual ~MultiSimController();
40 
41     void AddExtraManagers(std::shared_ptr<Telephony::SimStateManager> simStateManager,
42         std::shared_ptr<Telephony::SimFileManager> simFileManager);
43 
44     void Init();
45     bool InitData(int32_t slotId);
46     bool InitEsimData();
47     int32_t GetDefaultVoiceSlotId();
48     int32_t SetDefaultVoiceSlotId(int32_t slotId);
49     int32_t GetDefaultSmsSlotId();
50     int32_t SetDefaultSmsSlotId(int32_t slotId);
51     int32_t GetSimAccountInfo(int32_t slotId, bool denied, IccAccountInfo &info);
52     int32_t GetDefaultCellularDataSlotId();
53     int32_t SetDefaultCellularDataSlotId(int32_t slotId);
54     int32_t GetPrimarySlotId();
55     int32_t SetPrimarySlotId(int32_t slotId, bool isUserSet);
56     int32_t GetShowNumber(int32_t slotId, std::u16string &showNumber);
57     int32_t SetShowNumber(int32_t slotId, std::u16string Number, bool force = false);
58     int32_t SetShowNumberToDB(int32_t slotId, std::u16string Number);
59     int32_t GetShowName(int32_t slotId, std::u16string &showName);
60     int32_t SetShowName(int32_t slotId, std::u16string name, bool force = false);
61     int32_t GetSimTelephoneNumber(int32_t slotId, std::u16string &telephoneNumber);
62     bool IsSimActive(int32_t slotId);
63     int32_t SetActiveSim(int32_t slotId, int32_t enable, bool force = false);
64     int32_t SetActiveSimSatellite(int32_t slotId, int32_t enable, bool force = false);
65     bool SetActiveSimToRil(int32_t slotId, int32_t type, int32_t enable);
66     bool ForgetAllData();
67     bool ForgetAllData(int32_t slotId);
68     void ResetSetPrimarySlotRemain(int32_t slotId);
69     int32_t GetSlotId(int32_t simId);
70     int32_t GetSimId(int32_t slotId);
71     int32_t SaveImsSwitch(int32_t slotId, int32_t imsSwitchValue);
72     int32_t QueryImsSwitch(int32_t slotId, int32_t &imsSwitchValue);
73     bool GetListFromDataBase();
74     bool GetAllListFromDataBase();
75     int32_t GetActiveSimAccountInfoList(bool denied, std::vector<IccAccountInfo> &iccAccountInfoList);
76     int32_t GetAllSimAccountInfoList(bool denied, std::vector<IccAccountInfo> &iccAccountInfoList);
77     int32_t GetRadioProtocolTech(int32_t slotId);
78     void GetRadioProtocol(int32_t slotId);
79     bool InitShowName(int slotId);
80     void ReCheckPrimary();
81     bool IsDataShareError();
82     void ResetDataShareError();
83     int32_t UpdateOpKeyInfo();
84     bool IsSetActiveSimInProgress(int32_t slotId);
IsSetPrimarySlotIdInProgress()85     inline bool IsSetPrimarySlotIdInProgress()
86     {
87         return isSetPrimarySlotIdInProgress_;
88     }
89     int32_t SavePrimarySlotId(int32_t slotId);
90     int32_t GetDefaultMainSlotByIccId();
91     bool IsEsim(int32_t slotId);
92     int32_t InsertEsimData(const std::string &iccId, int32_t esimLabel, const std::string &operatorName);
93     int32_t GetSimLabel(int32_t slotId, SimLabel &simLabel);
94     int32_t SetSimLabelIndex(const std::string &iccId, int32_t labelIndex);
95 
96 public:
97     int32_t unInitModemSlotId_ = INVALID_VALUE;
98     std::unordered_map<int32_t, std::string> loadedSimCardInfo_;
99     ffrt::shared_mutex loadedSimCardInfoMutex_;
100     static constexpr const char *PHONE_NUMBER_PREF = "sim_number_";
101     enum {
102         SET_PRIMARY_SLOT_RETRY_EVENT = 0,
103         RIL_SET_PRIMARY_SLOT_TIMEOUT_EVENT
104     };
105     std::vector<bool> isSimSlotsMapping_ = {false, false};
106 
107 private:
108     bool IsValidData(int32_t slotId);
109     int32_t GetFirstActivedSlotId();
110     bool InitShowNumber(int slotId);
111     bool InitActive(int slotId);
112     bool InitIccId(int slotId);
113     int32_t UpdateDataByIccId(int slotId, const std::string &newIccId);
114     int32_t InsertData(int slotId, const std::string &newIccId);
115     void SortCache();
116     void SortAllCache();
117     void SavePrimarySlotIdInfo(int32_t slotId);
118     void SaveDefaultCellularDataSlotIdInfo(int32_t slotId);
119     bool AnnouncePrimarySimIdChanged(int32_t simId);
120     bool AnnounceDefaultVoiceSimIdChanged(int32_t simId);
121     bool AnnounceDefaultSmsSimIdChanged(int32_t simId);
122     bool AnnounceDefaultCellularDataSimIdChanged(int32_t simId);
123     bool PublishSimFileEvent(const AAFwk::Want &want, int eventCode, const std::string &eventData);
124     bool UpdateIccAccountInfoList(std::vector<IccAccountInfo> &accountInfoList,
125         std::vector<SimRdbInfo> &localCacheInfo, bool isGetActiveAccountInfo);
126     std::string EncryptIccId(const std::string iccid);
127     void CheckIfNeedSwitchMainSlotId(bool isInit = true);
128     bool IsValidSlotId(int32_t slotId);
129     bool InitPrimary();
130     bool IsAllCardsReady();
131     bool IsAllCardsLoaded();
132     void SendMainCardBroadCast(int32_t slotId);
133     void SendDefaultCellularDataBroadCast(int32_t slotId);
134     void InitMainCardSlotId();
135     void PublishSetPrimaryEvent(bool setDone);
136     int32_t GetTargetDefaultSimId(int32_t slotId, int &simId);
137     size_t GetLocalCacheSize();
138     int32_t GetTargetSimId(int32_t slotId, int &simId);
139     int32_t GetTargetIccId(int32_t slotId, std::string &iccId);
140     bool IsAllModemInitDone();
141     int32_t IsSatelliteSupported();
142     int32_t SetActiveCommonSim(int32_t slotId, int32_t enable, bool force, int32_t curSimId);
143     int32_t UpdataCacheSetActiveState(int32_t slotId, int32_t enable, int32_t curSimId);
144     int32_t UpdateDBSetActiveResult(int32_t slotId, int32_t enable, int32_t curSimId);
145     void UpdateSubState(int32_t slotId, int32_t enable);
146     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
147     void SetPrimarySlotIdDone();
148     int32_t SetPrimarySlotIdWithoutModemReboot(int32_t slotId);
149     void OnRilSetPrimarySlotDone(const AppExecFwk::InnerEvent::Pointer &event);
150     void OnRilSetPrimarySlotTimeout(const AppExecFwk::InnerEvent::Pointer &event);
151     bool SetPrimarySlotToRil(int32_t slotId);
152     void SendSetPrimarySlotEvent(int32_t slotId);
153     void ProcessRilSetPrimarySlotResponse(bool result);
154 
155 private:
156     const int32_t IMS_SWITCH_STATUS_UNKNOWN = -1;
157     const int ACTIVE_SIM_IN_PROGRESS = 1;
158     const int ACTIVE_SIM_NOT_IN_PROGRESS = 0;
159     ffrt::condition_variable activeSimConn_;
160     ffrt::mutex activeSimMutex_;
161     int32_t maxCount_ = 0;
162     int32_t primarySimId_ = 0;
163     int32_t defaultSmsSimId_ = 0;
164     int32_t defaultCellularSimId_ = 0;
165     int32_t defaultVoiceSimId_ = 0;
166     int32_t lastPrimarySlotId_ = 0;
167     int32_t lastCellularDataSlotId_ = 0;
168     static constexpr int32_t WAIT_REMOTE_TIME_SEC = 4;
169     std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager_;
170     std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager_;
171     std::unique_ptr<SimRdbHelper> simDbHelper_ = nullptr;
172     std::vector<IccAccountInfo> activeIccAccountInfoList_;
173     std::vector<SimRdbInfo> localCacheInfo_;
174     std::vector<IccAccountInfo> allIccAccountInfoList_;
175     std::vector<SimRdbInfo> allLocalCacheInfo_;
176     ffrt::mutex mutex_;
177     std::shared_ptr<RadioProtocolController> radioProtocolController_ = nullptr;
178     std::vector<int> isSetActiveSimInProgress_;
179     std::vector<int> setPrimarySlotRemainCount_;
180     std::atomic<bool> isSetPrimarySlotIdInProgress_{false};
181     ffrt::mutex setPrimarySlotToRilMutex_;
182     ffrt::mutex writeDbMutex_;
183     ffrt::condition_variable setPrimarySlotToRilCv_;
184     std::weak_ptr<Telephony::ITelRilManager> telRilManager_;
185     bool isSettingPrimarySlotToRil_ = false;
186     bool setPrimarySlotResponseResult_ = false;
187     bool isRilSetPrimarySlotSupport_ = false;
188 };
189 } // namespace Telephony
190 } // namespace OHOS
191 #endif // OHOS_MULTI_SIM_CONTROLLER_H
192