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