• 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 #include "icc_dialling_numbers_handler.h"
17 
18 using namespace std;
19 using namespace OHOS::AppExecFwk;
20 
21 namespace OHOS {
22 namespace Telephony {
23 std::atomic_int IccDiallingNumbersHandler::nextSerialId_(1);
24 std::unordered_map<int, std::shared_ptr<DiallingNumberLoadRequest>> IccDiallingNumbersHandler::requestMap_;
25 static std::mutex requestLock_;
26 
IccDiallingNumbersHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::shared_ptr<IccFileController> fh)27 IccDiallingNumbersHandler::IccDiallingNumbersHandler(
28     const std::shared_ptr<AppExecFwk::EventRunner> &runner, std::shared_ptr<IccFileController> fh)
29     : AppExecFwk::EventHandler(runner), fileController_(fh)
30 {
31     InitFuncMap();
32 }
33 
CreateLoadRequest(int fileId,int exId,int indexNum,const std::string & pin2Str,const AppExecFwk::InnerEvent::Pointer & result)34 std::shared_ptr<DiallingNumberLoadRequest> IccDiallingNumbersHandler::CreateLoadRequest(
35     int fileId, int exId, int indexNum, const std::string &pin2Str, const AppExecFwk::InnerEvent::Pointer &result)
36 {
37     std::lock_guard<std::mutex> lock(requestLock_);
38     std::shared_ptr<DiallingNumberLoadRequest> loadRequest =
39         std::make_shared<DiallingNumberLoadRequest>(GetNextSerialId(), fileId, exId, indexNum, pin2Str, result);
40     if (loadRequest == nullptr) {
41         TELEPHONY_LOGE("IccDiallingNumbersHandler loadRequest is nullptr");
42         return nullptr;
43     }
44     IccDiallingNumbersHandler::requestMap_.insert(std::make_pair(loadRequest->GetLoadId(), loadRequest));
45     return loadRequest;
46 }
47 
GetFilePath(int elementaryFileId)48 std::string IccDiallingNumbersHandler::GetFilePath(int elementaryFileId)
49 {
50     // GSM SIM file path from TS 51.011
51     std::string mf = MASTER_FILE_SIM;
52     mf.append(DEDICATED_FILE_TELECOM);
53     return elementaryFileId == ELEMENTARY_FILE_ADN ? mf : "";
54 }
55 
GetDiallingNumbers(int ef,int exid,int index,AppExecFwk::InnerEvent::Pointer & response)56 void IccDiallingNumbersHandler::GetDiallingNumbers(
57     int ef, int exid, int index, AppExecFwk::InnerEvent::Pointer &response)
58 {
59     if (fileController_ == nullptr) {
60         TELEPHONY_LOGE("fileController_ is null pointer");
61         return;
62     }
63     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = CreateLoadRequest(ef, exid, index, "", response);
64     AppExecFwk::InnerEvent::Pointer ptDiallingNumberRead =
65         BuildCallerInfo(MSG_SIM_OBTAIN_ADN_DONE, loadRequest->GetLoadId());
66     fileController_->ObtainLinearFixedFile(ef, GetFilePath(ef), index, ptDiallingNumberRead);
67 }
68 
GetAllDiallingNumbers(int ef,int exid,AppExecFwk::InnerEvent::Pointer & response)69 void IccDiallingNumbersHandler::GetAllDiallingNumbers(int ef, int exid, AppExecFwk::InnerEvent::Pointer &response)
70 {
71     if (fileController_ == nullptr) {
72         TELEPHONY_LOGE("fileController_ is null pointer");
73         return;
74     }
75     TELEPHONY_LOGI("IccDiallingNumbersHandler::GetAllDiallingNumbers start");
76     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = CreateLoadRequest(ef, exid, 0, "", response);
77     AppExecFwk::InnerEvent::Pointer ptDiallingNumberReadAll =
78         BuildCallerInfo(MSG_SIM_OBTAIN_ALL_ADN_DONE, loadRequest->GetLoadId());
79     fileController_->ObtainAllLinearFixedFile(ef, GetFilePath(ef), ptDiallingNumberReadAll);
80 }
81 
UpdateDiallingNumbers(const DiallingNumberUpdateInfor & infor,AppExecFwk::InnerEvent::Pointer & response)82 void IccDiallingNumbersHandler::UpdateDiallingNumbers(
83     const DiallingNumberUpdateInfor &infor, AppExecFwk::InnerEvent::Pointer &response)
84 {
85     if (fileController_ == nullptr) {
86         TELEPHONY_LOGE("fileController_ is null pointer");
87         return;
88     }
89     std::shared_ptr<DiallingNumberLoadRequest> loadRequest =
90         CreateLoadRequest(infor.fileId, infor.extFile, infor.index, infor.pin2, response);
91     loadRequest->SetIsDelete(infor.isDel);
92     TELEPHONY_LOGI("UpdateDiallingNumbers contents ready");
93     std::shared_ptr<void> diallingNumberObj = static_cast<std::shared_ptr<void>>(infor.diallingNumber);
94     AppExecFwk::InnerEvent::Pointer linearFileSize =
95         BuildCallerInfo(MSG_SIM_OBTAIN_LINEAR_FILE_SIZE_DONE, diallingNumberObj, loadRequest->GetLoadId());
96     fileController_->ObtainLinearFileSize(infor.fileId, GetFilePath(infor.fileId), linearFileSize);
97 }
98 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)99 void IccDiallingNumbersHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
100 {
101     if (event == nullptr) {
102         TELEPHONY_LOGE("event is nullptr!");
103         return;
104     }
105     auto id = event->GetInnerEventId();
106     int loadId = 0;
107     TELEPHONY_LOGI("IccDiallingNumbersHandler::ProcessEvent id %{public}d", id);
108     auto itFunc = memberFuncMap_.find(id);
109     if (itFunc != memberFuncMap_.end()) {
110         auto memberFunc = itFunc->second;
111         if (memberFunc != nullptr) {
112             (this->*memberFunc)(event, loadId);
113             SendBackResult(loadId);
114         }
115     } else {
116         TELEPHONY_LOGI("IccDiallingNumbersHandler::ProcessEvent unknown id %{public}d", id);
117     }
118 }
119 
ProcessLinearSizeDone(const AppExecFwk::InnerEvent::Pointer & event,int & id)120 void IccDiallingNumbersHandler::ProcessLinearSizeDone(const AppExecFwk::InnerEvent::Pointer &event, int &id)
121 {
122     if (event == nullptr) {
123         TELEPHONY_LOGE("event is nullptr!");
124         return;
125     }
126     std::unique_ptr<ControllerToFileMsg> fdError = event->GetUniqueObject<ControllerToFileMsg>();
127     int loadId = 0;
128     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = nullptr;
129     if (fdError != nullptr) {
130         loadId = fdError->arg1;
131         id = loadId;
132         loadRequest = FindLoadRequest(loadId);
133         if (loadRequest != nullptr) {
134             loadRequest->SetException(fdError->exception);
135         }
136         TELEPHONY_LOGE("ProcessLinearSizeDone error occured");
137         return;
138     }
139     std::shared_ptr<EfLinearResult> object = event->GetSharedObject<EfLinearResult>();
140     if (object == nullptr) {
141         TELEPHONY_LOGE("object is nullptr!");
142         return;
143     }
144     std::shared_ptr<DiallingNumbersInfo> diallingNumberLoad = nullptr;
145     loadId = object->arg1;
146     id = loadId;
147     loadRequest = FindLoadRequest(loadId);
148     if (loadRequest == nullptr) {
149         TELEPHONY_LOGE("loadRequest is nullptr!");
150         return;
151     }
152     if (object->exception == nullptr) {
153         std::shared_ptr<void> baseLoad = object->iccLoader;
154         int *dataSize = object->valueData;
155         if (baseLoad != nullptr) {
156             diallingNumberLoad = std::static_pointer_cast<DiallingNumbersInfo>(baseLoad);
157         }
158         if (!FormatNameAndNumber(diallingNumberLoad, loadRequest->GetIsDelete())) {
159             loadRequest->SetException(static_cast<std::shared_ptr<void>>(MakeExceptionResult(PARAMETER_INCORRECT)));
160             return;
161         }
162         SendUpdateCommand(diallingNumberLoad, dataSize[0], loadRequest, loadId);
163     }
164 }
165 
ProcessUpdateRecordDone(const AppExecFwk::InnerEvent::Pointer & event,int & id)166 void IccDiallingNumbersHandler::ProcessUpdateRecordDone(const AppExecFwk::InnerEvent::Pointer &event, int &id)
167 {
168     if (event == nullptr) {
169         TELEPHONY_LOGE("event is nullptr!");
170         return;
171     }
172     std::unique_ptr<ControllerToFileMsg> object = event->GetUniqueObject<ControllerToFileMsg>();
173     if (object == nullptr) {
174         TELEPHONY_LOGE("object is nullptr!");
175         return;
176     }
177     int loadId = object->arg1;
178     id = loadId;
179     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = FindLoadRequest(loadId);
180     if (loadRequest != nullptr) {
181         loadRequest->ClearCount();
182         loadRequest->SetException(object->exception);
183         if (loadRequest->GetException() == nullptr) {
184             loadRequest->SetResult(object->iccLoader);
185         }
186     }
187 }
188 
ProcessDiallingNumberAllLoadDone(const AppExecFwk::InnerEvent::Pointer & event,int & id)189 void IccDiallingNumbersHandler::ProcessDiallingNumberAllLoadDone(
190     const AppExecFwk::InnerEvent::Pointer &event, int &id)
191 {
192     if (event == nullptr) {
193         TELEPHONY_LOGE("event is nullptr!");
194         return;
195     }
196     std::unique_ptr<ControllerToFileMsg> fdError = event->GetUniqueObject<ControllerToFileMsg>();
197     int loadId = 0;
198     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = nullptr;
199     if (fdError != nullptr) {
200         loadId = fdError->arg1;
201         id = loadId;
202         loadRequest = FindLoadRequest(loadId);
203         if (loadRequest != nullptr) {
204             loadRequest->SetException(fdError->exception);
205         }
206         TELEPHONY_LOGE("ProcessDiallingNumberAllLoadDone error occured");
207         return;
208     }
209 
210     std::shared_ptr<MultiRecordResult> object = event->GetSharedObject<MultiRecordResult>();
211     if (object == nullptr) {
212         TELEPHONY_LOGE("object is nullptr!");
213         return;
214     }
215     loadId = object->arg1;
216     id = loadId;
217     loadRequest = FindLoadRequest(loadId);
218     if (object->exception != nullptr) {
219         TELEPHONY_LOGE("ProcessDiallingNumberAllLoadDone load failed");
220         if (loadRequest != nullptr) {
221             loadRequest->SetException(object->exception);
222         }
223         return;
224     }
225 
226     ProcessDiallingNumber(loadRequest, object);
227 }
228 
ProcessDiallingNumber(const std::shared_ptr<DiallingNumberLoadRequest> & loadRequest,const std::shared_ptr<MultiRecordResult> & object)229 void IccDiallingNumbersHandler::ProcessDiallingNumber(
230     const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, const std::shared_ptr<MultiRecordResult> &object)
231 {
232     if (loadRequest == nullptr || object == nullptr) {
233         TELEPHONY_LOGE("IccDiallingNumbersHandler::ProcessDiallingNumber loadRequest or object is nullptr");
234         return;
235     }
236     loadRequest->ClearCount();
237     std::shared_ptr<std::vector<std::shared_ptr<DiallingNumbersInfo>>> diallingNumberList =
238         std::make_shared<std::vector<std::shared_ptr<DiallingNumbersInfo>>>();
239     loadRequest->SetResult(static_cast<std::shared_ptr<void>>(diallingNumberList));
240 
241     std::vector<std::string> &dataList = object->fileResults;
242     TELEPHONY_LOGI("ProcessDiallingNumberAllLoadDone start: %{public}zu", dataList.size());
243     int i = 0;
244     for (std::vector<std::string>::iterator it = dataList.begin(); it != dataList.end(); ++it, i++) {
245         std::string item = *it;
246         std::shared_ptr<DiallingNumbersInfo> diallingNumber =
247             std::make_shared<DiallingNumbersInfo>(loadRequest->GetElementaryFileId(), 1 + i);
248         FetchDiallingNumberContent(diallingNumber, item);
249         diallingNumberList->push_back(diallingNumber);
250     }
251 }
252 
ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer & event,int & id)253 void IccDiallingNumbersHandler::ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id)
254 {
255     if (event == nullptr) {
256         TELEPHONY_LOGE("event is nullptr!");
257         return;
258     }
259     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
260     int loadId = fd->arg1;
261     id = loadId;
262     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = FindLoadRequest(loadId);
263     if (fd->exception != nullptr) {
264         if (loadRequest != nullptr) {
265             loadRequest->SetException(fd->exception);
266         }
267         TELEPHONY_LOGE("ProcessDiallingNumberLoadDone load failed with exception");
268         return;
269     }
270 
271     std::string iccData = fd->resultData;
272     TELEPHONY_LOGI("ProcessDiallingNumberLoadDone handle start");
273     if (loadRequest == nullptr) {
274         TELEPHONY_LOGE("ProcessDiallingNumberLoadDone loadRequest is nullptr");
275         return;
276     }
277     std::shared_ptr<DiallingNumbersInfo> diallingNumber =
278     std::make_shared<DiallingNumbersInfo>(loadRequest->GetElementaryFileId(), loadRequest->GetIndex());
279     FetchDiallingNumberContent(diallingNumber, iccData);
280     loadRequest->SetResult(static_cast<std::shared_ptr<void>>(diallingNumber));
281 }
282 
SendUpdateCommand(const std::shared_ptr<DiallingNumbersInfo> & diallingNumber,int length,const std::shared_ptr<DiallingNumberLoadRequest> & loadRequest,int loadId)283 void IccDiallingNumbersHandler::SendUpdateCommand(const std::shared_ptr<DiallingNumbersInfo> &diallingNumber,
284     int length, const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, int loadId)
285 {
286     int dataLen = length;
287     if (dataLen == INVALID_LENGTH) {
288         dataLen = RECORD_LENGTH;
289     }
290     TELEPHONY_LOGI("DiallingNumbersHandler::SendUpdateCommand start!! %{public}d %{public}d", dataLen, length);
291     std::shared_ptr<unsigned char> dataDiallingNumber = nullptr;
292     if (dataLen > 0) {
293         dataDiallingNumber = CreateSavingSequence(diallingNumber, dataLen);
294     }
295     if (dataDiallingNumber != nullptr) {
296         std::string data = SIMUtils::BytesConvertToHexString(dataDiallingNumber.get(), dataLen);
297         AppExecFwk::InnerEvent::Pointer event =
298             BuildCallerInfo(MSG_SIM_RENEW_ADN_DONE, diallingNumber, loadId);
299         fileController_->UpdateLinearFixedFile(loadRequest->GetElementaryFileId(),
300             GetFilePath(loadRequest->GetElementaryFileId()), loadRequest->GetIndex(), data, dataLen,
301             loadRequest->GetPin2(), event);
302         loadRequest->InitCount();
303     } else {
304         TELEPHONY_LOGE("invalid adn data");
305         loadRequest->SetException(static_cast<std::shared_ptr<void>>(MakeExceptionResult(
306             PARAMETER_INCORRECT)));
307     }
308 }
309 
SendBackResult(int loadId)310 bool IccDiallingNumbersHandler::SendBackResult(int loadId)
311 {
312     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = FindLoadRequest(loadId);
313     if (loadRequest == nullptr) {
314         return false;
315     }
316     if (loadRequest->GetCaller() == nullptr || loadRequest->HasCount()) {
317         return false;
318     }
319     auto owner = loadRequest->GetCaller()->GetOwner();
320     uint32_t id = loadRequest->GetCaller()->GetInnerEventId();
321     std::unique_ptr<DiallingNumbersHandleHolder> fd =
322         loadRequest->GetCaller()->GetUniqueObject<DiallingNumbersHandleHolder>();
323     if (fd == nullptr) {
324         TELEPHONY_LOGE("fd is nullptr!");
325         return false;
326     }
327     std::unique_ptr<DiallingNumbersHandlerResult> data = make_unique<DiallingNumbersHandlerResult>(fd.get());
328     data->result = loadRequest->GetResult();
329     data->exception = loadRequest->GetException();
330     if (owner == nullptr) {
331         TELEPHONY_LOGE("IccDiallingNumbersHandler::SendBackResult owner null pointer");
332         return false;
333     }
334     owner->SendEvent(id, data);
335     ClearLoadRequest(loadId);
336     TELEPHONY_LOGI("IccDiallingNumbersHandler::SendBackResult send end");
337     return true;
338 }
339 
BuildCallerInfo(int eventId,int loadId)340 AppExecFwk::InnerEvent::Pointer IccDiallingNumbersHandler::BuildCallerInfo(int eventId, int loadId)
341 {
342     std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
343     object->arg1 = loadId;
344     int eventParam = 0;
345     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
346     if (event == nullptr) {
347         TELEPHONY_LOGE("event is nullptr!");
348         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
349     }
350     event->SetOwner(shared_from_this());
351     return event;
352 }
353 
BuildCallerInfo(int eventId,std::shared_ptr<void> pobj,int loadId)354 AppExecFwk::InnerEvent::Pointer IccDiallingNumbersHandler::BuildCallerInfo(
355     int eventId, std::shared_ptr<void> pobj, int loadId)
356 {
357     std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
358     object->arg1 = loadId;
359     object->iccLoader = pobj;
360     int eventParam = 0;
361     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
362     if (event == nullptr) {
363         TELEPHONY_LOGE("event is nullptr!");
364         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
365     }
366     event->SetOwner(shared_from_this());
367     return event;
368 }
369 
FindLoadRequest(int serial)370 std::shared_ptr<DiallingNumberLoadRequest> IccDiallingNumbersHandler::FindLoadRequest(int serial)
371 {
372     std::lock_guard<std::mutex> lock(requestLock_);
373     std::shared_ptr<DiallingNumberLoadRequest> loadRequest = nullptr;
374     auto iter = IccDiallingNumbersHandler::requestMap_.find(serial);
375     if (iter == IccDiallingNumbersHandler::requestMap_.end()) {
376         TELEPHONY_LOGI("FindLoadRequest not found serial:%{public}d", serial);
377     } else {
378         loadRequest = iter->second;
379     }
380     TELEPHONY_LOGI("FindLoadRequest : %{public}d", serial);
381     return loadRequest;
382 }
383 
ClearLoadRequest(int serial)384 void IccDiallingNumbersHandler::ClearLoadRequest(int serial)
385 {
386     std::lock_guard<std::mutex> lock(requestLock_);
387     IccDiallingNumbersHandler::requestMap_.erase(serial);
388 }
389 
FetchDiallingNumberContent(const std::shared_ptr<DiallingNumbersInfo> & diallingNumber,const std::string & recordData)390 void IccDiallingNumbersHandler::FetchDiallingNumberContent(
391     const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, const std::string &recordData)
392 {
393     TELEPHONY_LOGI("FetchDiallingNumberContent start");
394     int recordLen = 0;
395     std::shared_ptr<unsigned char> data = SIMUtils::HexStringConvertToBytes(recordData, recordLen);
396     if (diallingNumber == nullptr || data == nullptr) {
397         TELEPHONY_LOGE("FetchDiallingNumberContent null data");
398         return;
399     }
400     /* parse name */
401     int offset = 0;
402     int length = recordLen - PRE_BYTES_NUM;
403     unsigned char *record = data.get();
404     std::string tempStrTag = SIMUtils::DiallingNumberStringFieldConvertToString(data, offset, length, NAME_CHAR_POS);
405     diallingNumber->name_ = Str8ToStr16(tempStrTag);
406     /* parse length */
407     offset += length;
408     length = static_cast<int>(record[offset]);
409     if (length > MAX_NUMBER_SIZE_BYTES) {
410         diallingNumber->number_ = u"";
411         TELEPHONY_LOGE("FetchDiallingNumberContent number error");
412         return;
413     }
414     /* parse number */
415     ++offset;
416     std::string tempStrNumber =
417     SimNumberDecode::BCDConvertToString(data, offset, length, SimNumberDecode::BCD_TYPE_ADN);
418     diallingNumber->number_ = Str8ToStr16(tempStrNumber);
419     TELEPHONY_LOGI("FetchDiallingNumberContent result end");
420 }
421 
CreateSavingSequence(const std::shared_ptr<DiallingNumbersInfo> & diallingNumber,int dataLength)422 std::shared_ptr<unsigned char> IccDiallingNumbersHandler::CreateSavingSequence(
423     const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int dataLength)
424 {
425     std::shared_ptr<unsigned char> byteTagPac = nullptr;
426     std::shared_ptr<unsigned char> diallingNumberStringPac = nullptr;
427     unsigned char *byteTag = nullptr;
428     unsigned char *diallingNumberString = nullptr;
429     int offset = dataLength - PRE_BYTES_NUM;
430     int byteTagLen = 0;
431     if (dataLength < 0) {
432         return nullptr;
433     }
434     unsigned char *cache = (unsigned char *)calloc(dataLength, sizeof(unsigned char));
435     if (cache == nullptr) {
436         return nullptr;
437     }
438     diallingNumberStringPac = std::shared_ptr<unsigned char>(cache, [](unsigned char *ptr) {
439         if (ptr != nullptr) {
440             free(ptr);
441             ptr = nullptr;
442         }
443     });
444     diallingNumberString = diallingNumberStringPac.get();
445     for (int i = 0; i < dataLength; i++) {
446         diallingNumberString[i] = (unsigned char)BYTE_VALUE;
447     }
448     TELEPHONY_LOGI("CreateSavingSequence contents start");
449 
450     uint maxNumberSize = (DIALING_NUMBERS_END - DIALING_NUMBERS_BEGIN + 1) * LENGTH_RATE;
451     if (diallingNumber->number_.empty()) {
452         TELEPHONY_LOGE("CreateSavingSequence number should not be empty");
453         return diallingNumberStringPac;
454     } else if (diallingNumber->number_.size() > maxNumberSize) {
455         TELEPHONY_LOGE("CreateSavingSequence number length exceed the maximum");
456         return nullptr;
457     }
458     byteTagPac = CreateNameSequence(diallingNumber->name_, byteTagLen);
459     byteTag = byteTagPac.get();
460 
461     if (byteTagLen <= offset) {
462         std::string tempNum = Str16ToStr8(diallingNumber->number_);
463         FillNumberFiledForDiallingNumber(diallingNumberStringPac, tempNum, dataLength);
464         if (byteTagLen > 0) {
465             SIMUtils::ArrayCopy(byteTag, 0, diallingNumberString, 0, byteTagLen);
466         }
467         TELEPHONY_LOGI("CreateSavingSequence result: %{public}d", byteTagLen);
468         return diallingNumberStringPac;
469     } else {
470         TELEPHONY_LOGE("CreateSavingSequence max data length is %{public}d", offset);
471         return nullptr;
472     }
473 }
474 
FillNumberFiledForDiallingNumber(std::shared_ptr<unsigned char> diallingNumber,const std::string & number,int dataLength)475 void IccDiallingNumbersHandler::FillNumberFiledForDiallingNumber(
476     std::shared_ptr<unsigned char> diallingNumber, const std::string &number, int dataLength)
477 {
478     if (diallingNumber == nullptr) {
479         return;
480     }
481     unsigned char *diallingNumberString = diallingNumber.get();
482     int offSet = dataLength - PRE_BYTES_NUM;
483     std::vector<uint8_t> bcdCodes;
484     if (!SimNumberDecode::NumberConvertToBCD(number, bcdCodes, false, SimNumberDecode::BCD_TYPE_ADN)) {
485         TELEPHONY_LOGE("FillNumberFiledForDiallingNumber fail at to SimNumberDecode::NumberConvertToBCD");
486         return;
487     }
488     TELEPHONY_LOGI(" get result from SimNumberDecode::NumberConvertToBCD !! size:%{public}zu", bcdCodes.size());
489     for (size_t i = 0; i < bcdCodes.size(); ++i) {
490         diallingNumberString[offSet + TON_NPI_NUMBER + i] = bcdCodes.at(i);
491     }
492     int index = offSet + BCD_NUMBER_BYTES;
493     unsigned char value = (unsigned char)(bcdCodes.size());
494     diallingNumberString[index] = value;
495     index = offSet + MORE_FILE_ID;
496     value = (unsigned char)(BYTE_VALUE);
497     diallingNumberString[index] = value;
498     index = offSet + EXTRA_FILE_ID;
499     diallingNumberString[index] = value;
500 }
501 
CreateNameSequence(const std::u16string & name,int & seqLength)502 std::shared_ptr<unsigned char> IccDiallingNumbersHandler::CreateNameSequence(
503     const std::u16string &name, int &seqLength)
504 {
505     std::string tempTag = Str16ToStr8(name);
506     std::shared_ptr<unsigned char> seqResult = nullptr;
507     if (SimCharDecode::IsChineseString(tempTag)) {
508         std::string sq = SimCharDecode::CharCodeToSequence(name, true);
509         seqResult = SIMUtils::HexStringConvertToBytes(sq, seqLength);
510         TELEPHONY_LOGI("chinese alphabet encode result %{public}s %{public}d", sq.c_str(), seqLength);
511     } else {
512         std::string sq = SimCharDecode::CharCodeToSequence(tempTag, false);
513         seqResult = SIMUtils::HexStringConvertToBytes(sq, seqLength);
514         TELEPHONY_LOGI("english alphabet encode result %{public}s %{public}d", sq.c_str(), seqLength);
515     }
516     return seqResult;
517 }
518 
FormatNameAndNumber(std::shared_ptr<DiallingNumbersInfo> & diallingNumber,bool isDel)519 bool IccDiallingNumbersHandler::FormatNameAndNumber(
520     std::shared_ptr<DiallingNumbersInfo> &diallingNumber, bool isDel)
521 {
522     if (diallingNumber == nullptr) {
523         TELEPHONY_LOGE("diallingNumber is nullptr!");
524         return false;
525     }
526     if (isDel) {
527         diallingNumber->number_ = u"";
528         diallingNumber->name_ = u"";
529         return true;
530     }
531     std::string nameTemp = Str16ToStr8(diallingNumber->name_);
532     std::string numberTemp = Str16ToStr8(diallingNumber->number_);
533     std::string &&name = SIMUtils::Trim(nameTemp);
534     std::string &&number = SIMUtils::Trim(numberTemp);
535 
536     if (name.empty() || number.empty()) {
537         TELEPHONY_LOGE("name or number shuld not be empty");
538         return false;
539     }
540     std::u16string &&nameWide = Str8ToStr16(name);
541     std::u16string &&numberWide = Str8ToStr16(number);
542 
543     uint nameMaxNum = SimCharDecode::IsChineseString(name) ? MAX_CHINESE_NAME : MAX_ENGLISH_NAME;
544     if (nameWide.size() > nameMaxNum) {
545         diallingNumber->name_ = nameWide.substr(0, nameMaxNum);
546     }
547 
548     uint numberMaxNum = MAX_NUMBER_CHAR;
549     if (numberWide.size() > numberMaxNum) {
550         std::string tempNum = number.substr(0, numberMaxNum);
551         if (SimNumberDecode::IsValidNumberString(tempNum)) {
552             diallingNumber->number_ = Str8ToStr16(tempNum);
553         } else {
554             TELEPHONY_LOGE("invalid number full string");
555             return false;
556         }
557     } else {
558         if (!SimNumberDecode::IsValidNumberString(number)) {
559             TELEPHONY_LOGE("invalid number string");
560             return false;
561         }
562     }
563     return true;
564 }
565 
MakeExceptionResult(int code)566 std::shared_ptr<HRilRadioResponseInfo> IccDiallingNumbersHandler::MakeExceptionResult(int code)
567 {
568     std::shared_ptr<HRilRadioResponseInfo> responseInfo = std::make_shared<HRilRadioResponseInfo>();
569     responseInfo->error = static_cast<Telephony::HRilErrType>(code);
570     return responseInfo;
571 }
572 
InitFuncMap()573 void IccDiallingNumbersHandler::InitFuncMap()
574 {
575     memberFuncMap_[MSG_SIM_OBTAIN_ADN_DONE] = &IccDiallingNumbersHandler::ProcessDiallingNumberLoadDone;
576     memberFuncMap_[MSG_SIM_OBTAIN_ALL_ADN_DONE] = &IccDiallingNumbersHandler::ProcessDiallingNumberAllLoadDone;
577     memberFuncMap_[MSG_SIM_OBTAIN_LINEAR_FILE_SIZE_DONE] = &IccDiallingNumbersHandler::ProcessLinearSizeDone;
578     memberFuncMap_[MSG_SIM_RENEW_ADN_DONE] = &IccDiallingNumbersHandler::ProcessUpdateRecordDone;
579 }
580 
UpdateFileController(const std::shared_ptr<IccFileController> & fileController)581 void IccDiallingNumbersHandler::UpdateFileController(const std::shared_ptr<IccFileController> &fileController)
582 {
583     fileController_ = fileController;
584 }
~IccDiallingNumbersHandler()585 IccDiallingNumbersHandler::~IccDiallingNumbersHandler() {}
586 } // namespace Telephony
587 } // namespace OHOS
588