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_USIM_DIALLING_NUMBERS_SERVICE_H
17 #define OHOS_USIM_DIALLING_NUMBERS_SERVICE_H
18 
19 #include "icc_dialling_numbers_handler.h"
20 #include "icc_file_controller.h"
21 #include "sim_constant.h"
22 #include "sim_utils.h"
23 #include "tag_service.h"
24 #include "tel_event_handler.h"
25 #include "telephony_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 enum UsimMessage {
30     MSG_USIM_PBR_LOAD_DONE = 1,
31     MSG_USIM_USIM_ADN_LOAD_DONE = 2
32 };
33 
34 struct UsimDiallingNumberFile {
35     std::map<int, std::shared_ptr<TagData>> fileIds_ {};
36     int fileNumber_ = 0;
37 };
38 
39 enum UsimConstant {
40     LEN_MIN = 2,
41     LEN_MAX = 3,
42     INVALID_SFI = -1,
43     BIT_OF_BYTE = 8
44 };
45 
46 class UsimDiallingNumbersService : public TelEventHandler {
47 public:
48     UsimDiallingNumbersService();
49     ~UsimDiallingNumbersService();
50     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
51     void ObtainUsimElementaryFiles(const AppExecFwk::InnerEvent::Pointer &pointer);
52     void EnableReloadFiles();
53     void SetFileControllerAndDiallingNumberHandler(
54         std::shared_ptr<IccFileController> &ctrl, std::shared_ptr<IccDiallingNumbersHandler> handler);
55 
56 protected:
57     std::shared_ptr<IccFileController> fileController_ = nullptr;
58     bool pbrFileLoaded_ = true;
59     std::vector<std::shared_ptr<UsimDiallingNumberFile>> pbrFiles_;
60     std::vector<std::shared_ptr<DiallingNumbersInfo>> diallingNumbersFiles_;
61     std::map<int, int> efIdOfSfi_;
62     uint pbrIndex_ = 0;
63     void ReloadAllFiles();
64     void LoadPbrFiles();
65     std::shared_ptr<IccDiallingNumbersHandler> diallingNumbersHandler_ = nullptr;
66     AppExecFwk::InnerEvent::Pointer callerPointer_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
67 
68 private:
69     using ProcessFunc = std::function<void(const AppExecFwk::InnerEvent::Pointer &event)>;
70     std::map<int, ProcessFunc> memberFuncMap_;
71     void UpdatePhoneDiallingNumberFile();
72     std::string GetEmailContents(int index);
73     bool LoadDiallingNumberFiles(int index);
74     void GeneratePbrFile(std::vector<std::string> &records);
75     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId);
76     AppExecFwk::InnerEvent::Pointer CreateHandlerPointer(
77         int eventid, int efId, int index, std::shared_ptr<void> pobj);
78     static std::mutex mtx_;
79     void ProcessPbrLoadDone(const AppExecFwk::InnerEvent::Pointer &event);
80     void ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer &event);
81     void FillDiallingNumbersRecords(const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &list);
82     bool CheckEmailFiles(const std::shared_ptr<TagData> &email, int index);
83 
84     std::shared_ptr<UsimDiallingNumberFile> BuildNumberFileByRecord(const std::string &record);
85     void StorePbrDetailInfo(std::shared_ptr<UsimDiallingNumberFile> file,
86         std::shared_ptr<TagService> tlv, int parentTag);
87     void SendBackResult(const std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> &diallingnumbers);
88     void SendLocalBack();
89     void InitFuncMap();
90     void NextStep(int msgId);
91     const int NEXT = 123;
92     enum UsimFileType {
93         TYPE1_FLAG = 168, // 3gpp 31102-9j0 4.4.2.1
94         TYPE2_FLAG = 169,
95         TYPE3_FLAG = 170,
96         TAG_SIM_USIM_ADN = 192,
97         TAG_SIM_USIM_IAP,
98         TAG_SIM_USIM_EXT1,
99         TAG_SIM_USIM_SNE,
100         TAG_SIM_USIM_ANR, // 3gpp 31102-9j0 4.4.2.9
101         TAG_SIM_USIM_PBC,
102         TAG_SIM_USIM_GRP,
103         TAG_SIM_USIM_AAS,
104         TAG_SIM_USIM_GSD,
105         TAG_SIM_USIM_UID,
106         TAG_SIM_USIM_EMAIL,
107         TAG_SIM_USIM_CCP1
108     };
109 };
110 } // namespace Telephony
111 } // namespace OHOS
112 #endif // OHOS_USIM_DIALLING_NUMBERS_SERVICE_H