• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SIM_DIALLING_NUMBERS_HANDLER_H
17 #define OHOS_SIM_DIALLING_NUMBERS_HANDLER_H
18 
19 #include <thread>
20 #include <unordered_map>
21 
22 #include "dialling_numbers_info.h"
23 #include "icc_file_controller.h"
24 #include "sim_data_type.h"
25 #include "sim_number_decode.h"
26 #include "sim_utils.h"
27 #include "tel_event_handler.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 enum {
32     MSG_SIM_OBTAIN_ADN_DONE = 1,
33     MSG_SIM_OBTAIN_ALL_ADN_DONE = 2,
34     MSG_SIM_OBTAIN_LINEAR_FILE_SIZE_DONE = 3,
35     MSG_SIM_RENEW_ADN_DONE = 4,
36     MSG_SIM_EXT_RECORD_LOAD_DONE = 5
37 };
38 
39 enum {
40     // 3gpp ts51.011 10.5.1
41     BCD_NUMBER_BYTES = 0,
42     TON_NPI_NUMBER = 1,
43     DIALING_NUMBERS_BEGIN = 2,
44     DIALING_NUMBERS_END = 11,
45     MORE_FILE_ID = 12,
46     EXTRA_FILE_ID = 13
47 };
48 
49 enum {
50     MORE_FILE_TYPE_DATA = 0x02,
51     MORE_FILE_FLAG = 0x03,
52     MAX_MORE_PARTY_LENGTH = 0xa
53 };
54 
55 struct DiallingNumberLoadRequest {
56 public:
DiallingNumberLoadRequestDiallingNumberLoadRequest57     DiallingNumberLoadRequest(int serialId, int fileId, int exId, int indexNum, const std::string &pin2Str,
58         const AppExecFwk::InnerEvent::Pointer &pointer) : elementaryFileId(fileId), extFileId(exId),
59         pin2(pin2Str), index(indexNum), loadId(serialId)
60     {
61         callPointer = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(pointer));
62     }
SetResultDiallingNumberLoadRequest63     void SetResult(std::shared_ptr<void> result)
64     {
65         this->result = result;
66     }
GetResultDiallingNumberLoadRequest67     std::shared_ptr<void> GetResult()
68     {
69         return this->result;
70     }
71 
SetExceptionDiallingNumberLoadRequest72     void SetException(std::shared_ptr<void> exception)
73     {
74         this->exception = exception;
75     }
76 
GetExceptionDiallingNumberLoadRequest77     std::shared_ptr<void> GetException()
78     {
79         return this->exception;
80     }
81 
SetElementaryFileIdDiallingNumberLoadRequest82     void SetElementaryFileId(int id)
83     {
84         this->elementaryFileId = id;
85     }
86 
GetElementaryFileIdDiallingNumberLoadRequest87     int GetElementaryFileId()
88     {
89         return this->elementaryFileId;
90     }
91 
SetExEFDiallingNumberLoadRequest92     void SetExEF(int ef)
93     {
94         this->extFileId = ef;
95     }
96 
GetExEFDiallingNumberLoadRequest97     int GetExEF()
98     {
99         return this->extFileId;
100     }
101 
SetIndexDiallingNumberLoadRequest102     void SetIndex(int index)
103     {
104         this->index = index;
105     }
106 
GetIndexDiallingNumberLoadRequest107     int GetIndex()
108     {
109         return this->index;
110     }
111 
SetPin2DiallingNumberLoadRequest112     void SetPin2(std::string pin2Code)
113     {
114         this->pin2 = pin2Code;
115     }
116 
GetPin2DiallingNumberLoadRequest117     std::string GetPin2()
118     {
119         return this->pin2;
120     }
121 
SetIsDeleteDiallingNumberLoadRequest122     void SetIsDelete(bool del)
123     {
124         this->isDelete = del;
125     }
126 
GetIsDeleteDiallingNumberLoadRequest127     bool GetIsDelete()
128     {
129         return this->isDelete;
130     }
131 
SetLoadIdDiallingNumberLoadRequest132     void SetLoadId(int id)
133     {
134         this->loadId = id;
135     }
136 
GetLoadIdDiallingNumberLoadRequest137     int GetLoadId()
138     {
139         return this->loadId;
140     }
GetCallerDiallingNumberLoadRequest141     AppExecFwk::InnerEvent::Pointer &GetCaller()
142     {
143         return callPointer;
144     }
HasCountDiallingNumberLoadRequest145     bool HasCount()
146     {
147         return moreFileToGet != 0;
148     }
InitCountDiallingNumberLoadRequest149     void InitCount()
150     {
151         moreFileToGet = INIT_COUNT;
152     }
CountUpDiallingNumberLoadRequest153     void CountUp()
154     {
155         ++moreFileToGet;
156     }
CountDownDiallingNumberLoadRequest157     void CountDown()
158     {
159         --moreFileToGet;
160     }
ClearCountDiallingNumberLoadRequest161     void ClearCount()
162     {
163         moreFileToGet = 0;
164     }
HasExtendedRecordDiallingNumberLoadRequest165     bool HasExtendedRecord() const
166     {
167         return extensionRecord_ != 0 && extensionRecord_ != 0xff;
168     }
169     unsigned char extensionRecord_ = 0;
170 private:
171     int elementaryFileId = 0;
172     int extFileId = 0;
173     int moreFileToGet = 0;
174     std::string pin2 = "";
175     int index = 0;
176     int loadId = 0;
177     bool isDelete = false;
178     std::shared_ptr<void> result = nullptr;
179     std::shared_ptr<void> exception = nullptr;
180     AppExecFwk::InnerEvent::Pointer &callPointer = nullptr_;
181     AppExecFwk::InnerEvent::Pointer nullptr_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
182     const int INIT_COUNT = 1;
183 };
184 struct DiallingNumberUpdateInfor {
185     std::shared_ptr<DiallingNumbersInfo> diallingNumber = nullptr;
186     int index = 0;
187     int fileId = 0;
188     int extFile = 0;
189     std::string pin2 = "";
190     bool isDel = false;
191 };
192 class IccDiallingNumbersHandler : public TelEventHandler {
193 public:
194     explicit IccDiallingNumbersHandler(std::shared_ptr<IccFileController> fh);
195     ~IccDiallingNumbersHandler();
196     void GetDiallingNumbers(int ef, int extensionEF, int recordNumber, AppExecFwk::InnerEvent::Pointer &response);
197     void GetAllDiallingNumbers(int ef, int extensionEF, AppExecFwk::InnerEvent::Pointer &response);
198     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
199     void UpdateDiallingNumbers(const DiallingNumberUpdateInfor &infor, AppExecFwk::InnerEvent::Pointer &response);
GetNextSerialId()200     static int GetNextSerialId()
201     {
202         return nextSerialId_++;
203     }
204     static std::atomic_int nextSerialId_;
205     static std::unordered_map<int, std::shared_ptr<DiallingNumberLoadRequest>> requestMap_;
206     static std::shared_ptr<DiallingNumberLoadRequest> FindLoadRequest(int serial);
207     static void ClearLoadRequest(int serial);
208     static std::shared_ptr<DiallingNumberLoadRequest> CreateLoadRequest(int fileId,
209         int exId, int indexNum, const std::string &pin2Str, const AppExecFwk::InnerEvent::Pointer &result);
210     void UpdateFileController(const std::shared_ptr<IccFileController> &fileController);
211 
212 protected:
213     std::shared_ptr<IccFileController> fileController_;
214     std::string GetFilePath(int elementaryFileId);
215     // 3GPP TS 51.011 V4.1.0 section 10.7 files of gsm
216     const std::string MASTER_FILE_SIM = "3F00";
217     const std::string DEDICATED_FILE_TELECOM = "7F10";
218 
219 private:
220     using ProcessFunc = std::function<void(const AppExecFwk::InnerEvent::Pointer &event, int &id)>;
221     std::map<int, ProcessFunc> memberFuncMap_;
222     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, int loadId);
223     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, std::shared_ptr<void> pobj, int loadId);
224     void ProcessDiallingNumberAllLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id);
225     void ProcessDiallingNumber(const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest,
226         const std::shared_ptr<MultiRecordResult> &object);
227     void ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id);
228     void ProcessExtensionRecordNumbers(const AppExecFwk::InnerEvent::Pointer &event, int &id);
229     void ProcessLinearSizeDone(const AppExecFwk::InnerEvent::Pointer &event, int &id);
230     void ProcessUpdateRecordDone(const AppExecFwk::InnerEvent::Pointer &event, int &id);
231     bool IsAdnHasExtRecord(int eventId, int loadId);
232     bool SendBackResult(int loadId);
233     void FetchDiallingNumberContent(
234         const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, const std::string &recordData,
235         const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest);
236     void FetchExtensionContent(
237         const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, const std::string &recordData);
238     std::shared_ptr<unsigned char> CreateSavingSequence(
239         const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int dataLength);
240     std::shared_ptr<unsigned char> CreateNameSequence(const std::u16string &name, int &seqLength);
241     std::shared_ptr<RadioResponseInfo> MakeExceptionResult(int code);
242     void FillNumberFiledForDiallingNumber(
243         std::shared_ptr<unsigned char> diallingNumber, const std::string &number, int dataLength);
244     bool FormatNameAndNumber(std::shared_ptr<DiallingNumbersInfo> &diallingNumber, bool isDel);
245     void SendUpdateCommand(const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int length,
246         const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, int loadId);
247     void InitFuncMap();
248 };
249 } // namespace Telephony
250 } // namespace OHOS
251 
252 #endif // OHOS_SIM_DIALLING_NUMBERS_HANDLER_H
253