• 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 "at_sim.h"
17 
18 #include "hril_notification.h"
19 #include "securec.h"
20 
21 #include "vendor_adapter.h"
22 #include "vendor_report.h"
23 
24 static const int32_t ERR = -1;
25 
GetSimType(void)26 static int32_t GetSimType(void)
27 {
28     char *pLine = NULL;
29     int32_t ret;
30     int32_t simType = 0;
31     ResponseInfo *pResponse = NULL;
32 
33     ret = SendCommandLock("AT^CARDMODE", "^CARDMODE:", 0, &pResponse);
34     if (ret != 0 || pResponse == NULL || !pResponse->success) {
35         TELEPHONY_LOGE("AT^CARDMODE send failed");
36         return HRIL_SIM_TYPE_UNKNOWN;
37     }
38     if (pResponse->head) {
39         pLine = pResponse->head->data;
40     }
41     ret = SkipATPrefix(&pLine);
42     if (ret < 0) {
43         return HRIL_SIM_TYPE_UNKNOWN;
44     }
45     ret = NextInt(&pLine, &simType);
46     if (ret < 0) {
47         return HRIL_SIM_TYPE_UNKNOWN;
48     }
49     FreeResponseInfo(pResponse);
50     return simType;
51 }
52 
GetSimState(char * pLine,char * pResult,ResponseInfo * pResponse)53 static int32_t GetSimState(char *pLine, char *pResult, ResponseInfo *pResponse)
54 {
55     int32_t status = HRIL_SIM_NOT_INSERTED;
56     int32_t ret;
57     ret = SkipATPrefix(&pLine);
58     if (ret != 0) {
59         return HRIL_SIM_NOT_READY;
60     }
61     ret = NextStr(&pLine, &pResult);
62     if (ret != 0) {
63         return HRIL_SIM_NOT_READY;
64     }
65     if (strcmp(pResult, "READY") == 0) {
66         status = HRIL_SIM_READY;
67     } else if (strcmp(pResult, "SIM PIN") == 0) {
68         status = HRIL_SIM_PIN;
69     } else if (strcmp(pResult, "SIM PUK") == 0) {
70         status = HRIL_SIM_PUK;
71     } else if (strcmp(pResult, "PH-NET PIN") == 0) {
72         status = HRIL_PH_NET_PIN;
73     } else if (strcmp(pResult, "PH-NET PUK") == 0) {
74         status = HRIL_PH_NET_PIN;
75     } else if (!ReportStrWith(pResponse->result, "+CME ERROR:")) {
76         status = HRIL_SIM_NOT_READY;
77     }
78     return status;
79 }
80 
ParseSimResponseResult(char * pLine,HRilSimIOResponse * pSimResponse)81 static int32_t ParseSimResponseResult(char *pLine, HRilSimIOResponse *pSimResponse)
82 {
83     if (pSimResponse == NULL) {
84         return ERR;
85     }
86     int32_t err = SkipATPrefix(&pLine);
87     if (err != 0) {
88         return err;
89     }
90     err = NextInt(&pLine, &pSimResponse->sw1);
91     if (err != 0) {
92         return err;
93     }
94     err = NextInt(&pLine, &pSimResponse->sw2);
95     if (err != 0) {
96         return err;
97     }
98 
99     if ((pLine != NULL && *pLine != '\0')) {
100         err = NextStr(&pLine, &pSimResponse->response);
101         if (err != 0) {
102             return err;
103         }
104     }
105     return 0;
106 }
107 
ParseSimPinInputTimesResult(char * pLine,HRilPinInputTimes * pinInputTimes)108 static int32_t ParseSimPinInputTimesResult(char *pLine, HRilPinInputTimes *pinInputTimes)
109 {
110     int32_t err = HRIL_ERR_GENERIC_FAILURE;
111     if (pinInputTimes == NULL) {
112         TELEPHONY_LOGE("pinInputTimes is null!!!");
113         return err;
114     }
115     err = SkipATPrefix(&pLine);
116     if (err != 0) {
117         return err;
118     }
119     err = NextStr(&pLine, &pinInputTimes->code);
120     if (err != 0) {
121         return err;
122     }
123     err = NextInt(&pLine, &pinInputTimes->times);
124     size_t atProlen = 0;
125     if (err != 0 && strlen(pLine) == atProlen) {
126         return err;
127     }
128     err = NextInt(&pLine, &pinInputTimes->pukTimes);
129     if (err != 0) {
130         return err;
131     }
132     err = NextInt(&pLine, &pinInputTimes->pinTimes);
133     if (err != 0) {
134         return err;
135     }
136     err = NextInt(&pLine, &pinInputTimes->puk2Times);
137     if (err != 0) {
138         return err;
139     }
140     err = NextInt(&pLine, &pinInputTimes->pin2Times);
141     if (err != 0) {
142         return err;
143     }
144     return 0;
145 }
146 
ParseUnlockSimLockResult(char * pLine,HRilLockStatus * lockStatus)147 static int32_t ParseUnlockSimLockResult(char *pLine, HRilLockStatus *lockStatus)
148 {
149     int32_t err = HRIL_ERR_GENERIC_FAILURE;
150     if (lockStatus == NULL) {
151         TELEPHONY_LOGE("lockStatus is null!!!");
152         return err;
153     }
154     err = SkipATPrefix(&pLine);
155     if (err != 0) {
156         return err;
157     }
158     err = NextInt(&pLine, &lockStatus->result);
159     if (err != 0) {
160         return err;
161     }
162     err = NextInt(&pLine, &lockStatus->remain);
163     TELEPHONY_LOGD("ParseUnlockSimLockResult, lockStatus->result: %{public}d", lockStatus->result);
164     TELEPHONY_LOGD("ParseUnlockSimLockResult, lockStatus->remain: %{public}d", lockStatus->remain);
165     if (err != 0) {
166         return err;
167     }
168     return 0;
169 }
170 
ReqGetSimStatus(const ReqDataInfo * requestInfo)171 void ReqGetSimStatus(const ReqDataInfo *requestInfo)
172 {
173     char *pLine = NULL;
174     char *pResult = NULL;
175     int32_t ret;
176     ResponseInfo *pResponse = NULL;
177     HRilCardState cardState = {0};
178 
179     HRilRadioState radioState = GetRadioState();
180     if (radioState == HRIL_RADIO_POWER_STATE_UNAVAILABLE || radioState == HRIL_RADIO_POWER_STATE_OFF) {
181         cardState.simState = HRIL_SIM_NOT_READY;
182     }
183     cardState.simType = GetSimType();
184     ret = SendCommandLock("AT+CPIN?", "+CPIN:", 0, &pResponse);
185     if (ret != 0 || pResponse == NULL || !pResponse->success) {
186         TELEPHONY_LOGE("AT+CPIN? send failed");
187         if (pResponse && pResponse->result) {
188             pLine = pResponse->result;
189             SkipATPrefix(&pLine);
190             NextInt(&pLine, &ret);
191         } else {
192             ret = HRIL_ERR_GENERIC_FAILURE;
193         }
194         if (ret == HRIL_ERR_NO_SIMCARD_INSERTED) {
195             cardState.simState = HRIL_SIM_NOT_INSERTED;
196             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
197             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&cardState, sizeof(HRilCardState));
198         } else {
199             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
200             OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
201         }
202         FreeResponseInfo(pResponse);
203         return;
204     }
205     if (pResponse->head) {
206         pLine = pResponse->head->data;
207     }
208     cardState.simState = GetSimState(pLine, pResult, pResponse);
209     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
210     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&cardState, sizeof(HRilCardState));
211     FreeResponseInfo(pResponse);
212 }
213 
ReqGetSimIOFDNWrite(HRilSimIO * pSim,ResponseInfo ** ppResponse,size_t dataLen)214 static int32_t ReqGetSimIOFDNWrite(HRilSimIO *pSim, ResponseInfo **ppResponse, size_t dataLen)
215 {
216     char cmd[MAX_CMD_LENGTH] = {0};
217     int32_t tmp = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CLCK=\"%s\",%d,\"%s\"", "FD", 1, pSim->pin2);
218     if (tmp < 0) {
219         TELEPHONY_LOGE("GenerateCommand failed");
220         return VENDOR_FAIL;
221     }
222     int32_t ret = SendCommandLock(cmd, "+CLCK", 0, ppResponse);
223     if (ret != 0 || (ppResponse != NULL && !(*ppResponse)->success)) {
224         TELEPHONY_LOGE("AT+CLCK failed");
225         return HRIL_ERR_CMD_SEND_FAILURE;
226     }
227     tmp = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CRSM=%d,%d,%d,%d,%d,\"%s\",\"%s\"", pSim->command, pSim->fileid,
228         pSim->p1, pSim->p2, pSim->p3, ((pSim->data == NULL) ? "" : (pSim->data)), pSim->pathid);
229     if (tmp < 0) {
230         TELEPHONY_LOGE("GenerateCommand failed");
231         return VENDOR_FAIL;
232     }
233     ret = SendCommandLock(cmd, "+CRSM", 0, ppResponse);
234     if (ret != 0 || (ppResponse != NULL && !(*ppResponse)->success)) {
235         return HRIL_ERR_CMD_SEND_FAILURE;
236     }
237     tmp = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CLCK=\"%s\",%d,\"%s\"", "FD", 0, pSim->pin2);
238     if (tmp < 0) {
239         return VENDOR_FAIL;
240     }
241     ret = SendCommandLock(cmd, "+CLCK", 0, ppResponse);
242     if (ret != 0 || !(*ppResponse)->success) {
243         TELEPHONY_LOGE("AT+CLCK failed");
244         return HRIL_ERR_CMD_SEND_FAILURE;
245     }
246     return HRIL_ERR_SUCCESS;
247 }
248 
ReqGetSimIOFDN(HRilSimIO * pSim,ResponseInfo ** ppResponse,size_t dataLen)249 static int32_t ReqGetSimIOFDN(HRilSimIO *pSim, ResponseInfo **ppResponse, size_t dataLen)
250 {
251     const char *queryCmd = "AT+CLCK=\"FD\",2";
252     int32_t ret = SendCommandLock(queryCmd, "+CLCK", 0, ppResponse);
253     if (ret != 0 || ppResponse == NULL || *ppResponse == NULL || !(*ppResponse)->success) {
254         TELEPHONY_LOGE("AT+CLCK failed");
255         return HRIL_ERR_CMD_SEND_FAILURE;
256     }
257     char *pLine = (*ppResponse)->last == NULL ? (*ppResponse)->result : (*ppResponse)->last->data;
258     SkipATPrefix(&pLine);
259     int32_t clckRes = VENDOR_FAIL;
260     NextInt(&pLine, &clckRes);
261     clckRes = (clckRes > 0) ? 1 : 0;
262     TELEPHONY_LOGD("FDN had got FDN clck res:%{public}d", clckRes);
263     int32_t writeRet = ReqGetSimIOFDNWrite(pSim, ppResponse, dataLen);
264     char cmd[MAX_CMD_LENGTH] = {0};
265     int32_t tmp = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CLCK=\"%s\",%d,\"%s\"", "FD", clckRes, pSim->pin2);
266     if (tmp < 0) {
267         TELEPHONY_LOGE("GenerateCommand failed");
268         return VENDOR_FAIL;
269     }
270     ret = SendCommandLock(cmd, "+CLCK", 0, ppResponse);
271     if (ret != 0 || (ppResponse != NULL && !(*ppResponse)->success)) {
272         TELEPHONY_LOGE("AT+CLCK failed");
273         return HRIL_ERR_CMD_SEND_FAILURE;
274     }
275     return writeRet;
276 }
277 
HandlerSimIOResult(ResponseInfo * pResponse,HRilSimIOResponse * simResponse,const ReqDataInfo * requestInfo,char * pLine,int32_t * ret)278 static void HandlerSimIOResult(ResponseInfo *pResponse, HRilSimIOResponse *simResponse,
279     const ReqDataInfo *requestInfo, char *pLine, int32_t *ret)
280 {
281     if (pResponse == NULL || ret == NULL) {
282         TELEPHONY_LOGE("pResponse is NULL");
283         return;
284     }
285 
286     if (*ret != HRIL_ERR_SUCCESS) {
287         if (pResponse && pResponse->result) {
288             pLine = pResponse->result;
289             SkipATPrefix(&pLine);
290             NextInt(&pLine, ret);
291         } else {
292             *ret = HRIL_ERR_GENERIC_FAILURE;
293         }
294     }
295     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, *ret, HRIL_RESPONSE, 0);
296     if (simResponse == NULL) {
297         TELEPHONY_LOGE("simResponse is NULL");
298         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
299     } else {
300         OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)simResponse, sizeof(HRilSimIOResponse));
301     }
302     FreeResponseInfo(pResponse);
303 }
304 
ReqGetSimIO(const ReqDataInfo * requestInfo,const HRilSimIO * data,size_t dataLen)305 void ReqGetSimIO(const ReqDataInfo *requestInfo, const HRilSimIO *data, size_t dataLen)
306 {
307     char *pLine = NULL;
308     int32_t ret;
309     const int32_t FILEID = 0x6F3B;
310     ResponseInfo *pResponse = NULL;
311     HRilSimIOResponse simResponse = {0};
312     HRilSimIO *pSim = (HRilSimIO *)data;
313     if (pSim == NULL) {
314         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
315         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
316         FreeResponseInfo(pResponse);
317         return;
318     }
319     if (pSim->pin2 != NULL && strcmp(pSim->pin2, "") != 0 && pSim->fileid == FILEID) {
320         ret = ReqGetSimIOFDN(pSim, &pResponse, dataLen);
321         TELEPHONY_LOGE("ReqGetSimIOFDN call over");
322         if (ret != HRIL_ERR_SUCCESS || !pResponse->success) {
323             TELEPHONY_LOGE("FDN is failed");
324             HandlerSimIOResult(pResponse, NULL, requestInfo, pLine, &ret);
325             return;
326         }
327         TELEPHONY_LOGE("FDN is success");
328         HandlerSimIOResult(pResponse, &simResponse, requestInfo, pLine, &ret);
329         return;
330     }
331     char cmd[MAX_CMD_LENGTH] = {0};
332     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CRSM=%d,%d,%d,%d,%d,\"%s\",\"%s\"", pSim->command,
333         pSim->fileid, pSim->p1, pSim->p2, pSim->p3, ((pSim->data == NULL) ? "" : (pSim->data)), pSim->pathid);
334     if (result <= 0) {
335         TELEPHONY_LOGE("GenerateCommand is failed");
336     }
337     ret = SendCommandLock(cmd, "+CRSM", 0, &pResponse);
338     if (ret != HRIL_ERR_SUCCESS || pResponse == NULL || !pResponse->success) {
339         TELEPHONY_LOGE("send failed dataLen:%{public}zu", dataLen);
340         HandlerSimIOResult(pResponse, NULL, requestInfo, pLine, &ret);
341         return;
342     }
343     if (pResponse->head) {
344         pLine = pResponse->head->data;
345     }
346     ret = ParseSimResponseResult(pLine, &simResponse);
347     if (ret != HRIL_ERR_SUCCESS) {
348         HandlerSimIOResult(pResponse, NULL, requestInfo, pLine, &ret);
349         return;
350     }
351     if (GetSimType() == HRIL_SIM_TYPE_USIM && pSim->command == CMD_GET_RESPONSE) {
352         ConvertUsimFcpToSimRsp((unsigned char **)&(simResponse.response));
353     }
354     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
355     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&simResponse, sizeof(HRilSimIOResponse));
356     FreeResponseInfo(pResponse);
357 }
358 
HandlerSimImsiResult(ResponseInfo * pResponse,struct ReportInfo reportInfo,const ReqDataInfo * requestInfo,char * pLine,int32_t * ret)359 static void HandlerSimImsiResult(
360     ResponseInfo *pResponse, struct ReportInfo reportInfo, const ReqDataInfo *requestInfo, char *pLine, int32_t *ret)
361 {
362     if (pResponse == NULL || ret == NULL) {
363         TELEPHONY_LOGE("pResponse is NULL");
364         return;
365     }
366     if (pResponse && pResponse->result) {
367         pLine = pResponse->result;
368         SkipATPrefix(&pLine);
369         NextInt(&pLine, ret);
370     } else {
371         *ret = HRIL_ERR_GENERIC_FAILURE;
372     }
373     reportInfo = CreateReportInfo(requestInfo, *ret, HRIL_RESPONSE, 0);
374     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
375     FreeResponseInfo(pResponse);
376 }
377 
ReqGetSimImsi(const ReqDataInfo * requestInfo)378 void ReqGetSimImsi(const ReqDataInfo *requestInfo)
379 {
380     char *pLine = NULL;
381     char *result = NULL;
382     int32_t ret;
383     ResponseInfo *pResponse = NULL;
384     struct ReportInfo reportInfo = {0};
385 
386     ret = SendCommandLock("AT+CIMI", NULL, 0, &pResponse);
387     if (ret != 0 || pResponse == NULL || !pResponse->success) {
388         TELEPHONY_LOGE("AT+CIMI send failed");
389         HandlerSimImsiResult(pResponse, reportInfo, requestInfo, pLine, &ret);
390         return;
391     }
392     if (pResponse->head) {
393         result = pResponse->head->data;
394     } else {
395         HandlerSimImsiResult(pResponse, reportInfo, requestInfo, pLine, &ret);
396         return;
397     }
398     reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
399     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)result, 0);
400     FreeResponseInfo(pResponse);
401 }
402 
HandlerSimLockStatusResult(ResponseInfo * pResponse,struct ReportInfo reportInfo,const ReqDataInfo * requestInfo,char * pLine,int32_t * ret)403 static void HandlerSimLockStatusResult(
404     ResponseInfo *pResponse, struct ReportInfo reportInfo, const ReqDataInfo *requestInfo, char *pLine, int32_t *ret)
405 {
406     if (pResponse == NULL || ret == NULL) {
407         TELEPHONY_LOGE("pResponse is NULL");
408         return;
409     }
410     if (pResponse && pResponse->result) {
411         pLine = pResponse->result;
412         SkipATPrefix(&pLine);
413         NextInt(&pLine, ret);
414     } else {
415         *ret = HRIL_ERR_GENERIC_FAILURE;
416     }
417     reportInfo = CreateReportInfo(requestInfo, *ret, HRIL_RESPONSE, 0);
418     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
419     FreeResponseInfo(pResponse);
420 }
421 
ReqGetSimLockStatus(const ReqDataInfo * requestInfo,const HRilSimClock * data,size_t dataLen)422 void ReqGetSimLockStatus(const ReqDataInfo *requestInfo, const HRilSimClock *data, size_t dataLen)
423 {
424     char cmd[MAX_CMD_LENGTH] = {0};
425     int32_t ret;
426     char *pLine = NULL;
427     int32_t status = 0;
428     HRilSimClock *pSimClck = NULL;
429     ResponseInfo *pResponse = NULL;
430     struct ReportInfo reportInfo = {0};
431 
432     pSimClck = (HRilSimClock *)data;
433     if (pSimClck == NULL) {
434         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
435         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
436         FreeResponseInfo(pResponse);
437         return;
438     }
439     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CLCK=\"%s\",%d", pSimClck->fac, pSimClck->mode);
440     if (result <= 0) {
441         TELEPHONY_LOGE("GenerateCommand is failed");
442     }
443     ret = SendCommandLock(cmd, "+CLCK", 0, &pResponse);
444     if (ret != 0 || pResponse == NULL || !pResponse->success) {
445         TELEPHONY_LOGE("AT+CLCK send failed dataLen:%{public}zu", dataLen);
446         HandlerSimLockStatusResult(pResponse, reportInfo, requestInfo, pLine, &ret);
447         return;
448     }
449     if (pResponse->head) {
450         pLine = pResponse->head->data;
451     }
452     ret = SkipATPrefix(&pLine);
453     if (ret != 0) {
454         HandlerSimLockStatusResult(pResponse, reportInfo, requestInfo, pLine, &ret);
455         return;
456     }
457 
458     ret = NextInt(&pLine, &status);
459     if (ret != 0) {
460         HandlerSimLockStatusResult(pResponse, reportInfo, requestInfo, pLine, &ret);
461         return;
462     }
463     reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
464     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&status, sizeof(int32_t));
465     FreeResponseInfo(pResponse);
466 }
467 
ReqSetSimLock(const ReqDataInfo * requestInfo,const HRilSimClock * data,size_t dataLen)468 void ReqSetSimLock(const ReqDataInfo *requestInfo, const HRilSimClock *data, size_t dataLen)
469 {
470     char cmd[MAX_CMD_LENGTH] = {0};
471     char *pLine = NULL;
472     int32_t ret;
473     HRilSimClock *pSimClck = NULL;
474     ResponseInfo *pResponse = NULL;
475 
476     pSimClck = (HRilSimClock *)data;
477     if (pSimClck == NULL) {
478         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
479         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
480         FreeResponseInfo(pResponse);
481         return;
482     }
483     int32_t result = GenerateCommand(
484         cmd, MAX_CMD_LENGTH, "AT+CLCK=\"%s\",%d,\"%s\"", pSimClck->fac, pSimClck->mode, pSimClck->passwd);
485     if (result <= 0) {
486         TELEPHONY_LOGE("GenerateCommand is failed");
487     }
488     ret = SendCommandLock(cmd, "+CLCK", 0, &pResponse);
489     HRilLockStatus lockStatus = {0};
490     lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
491     lockStatus.remain = -1;
492     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
493         TELEPHONY_LOGE("AT+CLCK send failed dataLen:%{public}zu", dataLen);
494         if (pResponse && pResponse->result) {
495             pLine = pResponse->result;
496             SkipATPrefix(&pLine);
497             NextInt(&pLine, &ret);
498             HRilPinInputTimes pinInputTimes = { 0 };
499             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
500                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
501                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
502                 lockStatus.remain = pinInputTimes.pinTimes;
503             }
504             if (strcmp(pSimClck->fac, "FD") == 0) {
505                 lockStatus.remain = pinInputTimes.pin2Times;
506             }
507             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
508             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
509             FreeResponseInfo(pResponse);
510         } else {
511             ret = HRIL_ERR_GENERIC_FAILURE;
512 
513             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
514             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
515             FreeResponseInfo(pResponse);
516         }
517         return;
518     }
519     lockStatus.result = HRIL_UNLOCK_SUCCESS;
520     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
521     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
522     FreeResponseInfo(pResponse);
523 }
524 
ReqChangeSimPassword(const ReqDataInfo * requestInfo,const HRilSimPassword * data,size_t dataLen)525 void ReqChangeSimPassword(const ReqDataInfo *requestInfo, const HRilSimPassword *data, size_t dataLen)
526 {
527     char cmd[MAX_CMD_LENGTH] = {0};
528     char *pLine = NULL;
529     HRilSimPassword *pSimPassword = NULL;
530     int32_t ret;
531     ResponseInfo *pResponse = NULL;
532 
533     pSimPassword = (HRilSimPassword *)data;
534     if (pSimPassword == NULL) {
535         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
536         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
537         FreeResponseInfo(pResponse);
538         return;
539     }
540     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CPWD=\"%s\",\"%s\",\"%s\"", pSimPassword->fac,
541         pSimPassword->oldPassword, pSimPassword->newPassword);
542     if (result <= 0) {
543         TELEPHONY_LOGE("GenerateCommand is failed");
544     }
545     ret = SendCommandLock(cmd, "+CPWD", 0, &pResponse);
546     HRilLockStatus lockStatus = {0};
547     lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
548     lockStatus.remain = -1;
549     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
550         TELEPHONY_LOGE("AT+CPWD send failed dataLen:%{public}zu", dataLen);
551         if (pResponse && pResponse->result) {
552             pLine = pResponse->result;
553             SkipATPrefix(&pLine);
554             NextInt(&pLine, &ret);
555             HRilPinInputTimes pinInputTimes = {0};
556             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
557                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
558                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
559                 lockStatus.remain = pinInputTimes.pinTimes;
560             }
561             if (strcmp(pSimPassword->fac, "P2") == 0) {
562                 lockStatus.remain = pinInputTimes.pin2Times;
563             }
564             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
565             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
566             FreeResponseInfo(pResponse);
567         } else {
568             ret = HRIL_ERR_GENERIC_FAILURE;
569             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
570             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
571             FreeResponseInfo(pResponse);
572         }
573         return;
574     }
575     lockStatus.result = HRIL_UNLOCK_SUCCESS;
576     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
577     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
578     FreeResponseInfo(pResponse);
579 }
580 
ReqUnlockPin(const ReqDataInfo * requestInfo,const char * pin)581 void ReqUnlockPin(const ReqDataInfo *requestInfo, const char *pin)
582 {
583     char cmd[MAX_CMD_LENGTH] = {0};
584     char *pLine = NULL;
585     int32_t ret;
586     ResponseInfo *pResponse = NULL;
587 
588     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CPIN=\"%s\"", pin);
589     if (result <= 0) {
590         TELEPHONY_LOGE("GenerateCommand is failed");
591     }
592     ret = SendCommandLock(cmd, "+CPIN", 0, &pResponse);
593     HRilLockStatus lockStatus = {0};
594     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
595         TELEPHONY_LOGE("AT+CPIN send failed");
596         if (pResponse && pResponse->result) {
597             pLine = pResponse->result;
598             TELEPHONY_LOGD("AT+CPIN send failed pLine1:%{public}s", pLine);
599             SkipATPrefix(&pLine);
600             TELEPHONY_LOGD("AT+CPIN send failed pLine2:%{public}s", pLine);
601             NextInt(&pLine, &ret);
602             TELEPHONY_LOGD("AT+CPIN send failed ret:%{public}d", ret);
603             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
604                 HRilPinInputTimes pinInputTimes = {0};
605                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
606                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
607                 lockStatus.remain = pinInputTimes.pinTimes;
608             } else {
609                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
610                 lockStatus.remain = -1;
611                 TELEPHONY_LOGE("AT+CPWD send failed");
612             }
613             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
614             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
615             FreeResponseInfo(pResponse);
616             return;
617         } else {
618             ret = HRIL_ERR_GENERIC_FAILURE;
619             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
620             lockStatus.remain = -1;
621             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
622             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
623             FreeResponseInfo(pResponse);
624             return;
625         }
626     }
627 
628     lockStatus.result = HRIL_UNLOCK_SUCCESS;
629     lockStatus.remain = -1;
630     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
631     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
632     FreeResponseInfo(pResponse);
633 }
634 
ReqUnlockPuk(const ReqDataInfo * requestInfo,const char * puk,const char * pin)635 void ReqUnlockPuk(const ReqDataInfo *requestInfo, const char *puk, const char *pin)
636 {
637     char cmd[MAX_CMD_LENGTH] = {0};
638     char *pLine = NULL;
639     int32_t ret;
640     ResponseInfo *pResponse = NULL;
641 
642     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CPIN=\"%s\",\"%s\"", puk, pin);
643     if (result <= 0) {
644         TELEPHONY_LOGE("GenerateCommand is failed");
645     }
646     ret = SendCommandLock(cmd, "+CPIN", 0, &pResponse);
647     HRilLockStatus lockStatus = {0};
648     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
649         TELEPHONY_LOGE("AT+CPIN send failed");
650         if (pResponse && pResponse->result) {
651             pLine = pResponse->result;
652             TELEPHONY_LOGD("AT+CPIN send failed pLine1:%{public}s", pLine);
653             SkipATPrefix(&pLine);
654             TELEPHONY_LOGD("AT+CPIN send failed pLine2:%{public}s", pLine);
655             NextInt(&pLine, &ret);
656             TELEPHONY_LOGD("AT+CPIN send failed ret:%{public}d", ret);
657             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
658                 HRilPinInputTimes pinInputTimes = {0};
659                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
660                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
661                 lockStatus.remain = pinInputTimes.pukTimes;
662             } else {
663                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
664                 lockStatus.remain = -1;
665                 TELEPHONY_LOGE("AT+CPIN send failed");
666             }
667             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
668             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
669             FreeResponseInfo(pResponse);
670             return;
671         } else {
672             ret = HRIL_ERR_GENERIC_FAILURE;
673             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
674             lockStatus.remain = -1;
675             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
676             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
677             FreeResponseInfo(pResponse);
678             return;
679         }
680     }
681 
682     lockStatus.result = HRIL_UNLOCK_SUCCESS;
683     lockStatus.remain = -1;
684     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
685     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
686     FreeResponseInfo(pResponse);
687 }
688 
ReqGetSimPinInputTimes(const ReqDataInfo * requestInfo)689 void ReqGetSimPinInputTimes(const ReqDataInfo *requestInfo)
690 {
691     char *pLine = NULL;
692     int32_t ret;
693     HRilPinInputTimes pinInputTimes = {0};
694     ResponseInfo *pResponse = NULL;
695 
696     ret = SendCommandLock("AT^CPIN?", "^CPIN", 0, &pResponse);
697     if (ret != 0 || pResponse == NULL || !pResponse->success) {
698         TELEPHONY_LOGE("AT^CPIN? send failed");
699         if (pResponse && pResponse->result) {
700             pLine = pResponse->result;
701             SkipATPrefix(&pLine);
702             NextInt(&pLine, &ret);
703         } else {
704             ret = HRIL_ERR_GENERIC_FAILURE;
705         }
706         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
707         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
708         FreeResponseInfo(pResponse);
709         return;
710     }
711     if (pResponse->head) {
712         pLine = pResponse->head->data;
713     }
714     ret = ParseSimPinInputTimesResult(pLine, &pinInputTimes);
715     if (ret != 0) {
716         if (pResponse && pResponse->result) {
717             pLine = pResponse->result;
718             SkipATPrefix(&pLine);
719             NextInt(&pLine, &ret);
720         } else {
721             ret = HRIL_ERR_GENERIC_FAILURE;
722         }
723         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
724         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
725         FreeResponseInfo(pResponse);
726         return;
727     }
728     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
729     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&pinInputTimes, sizeof(HRilPinInputTimes));
730     FreeResponseInfo(pResponse);
731 }
732 
ReqGetSimPinInputTimesRemain(const ReqDataInfo * requestInfo,HRilPinInputTimes * pinInputTimes)733 void ReqGetSimPinInputTimesRemain(const ReqDataInfo *requestInfo, HRilPinInputTimes *pinInputTimes)
734 {
735     char *pLine = NULL;
736     int32_t ret;
737     ResponseInfo *pResponse = NULL;
738 
739     ret = SendCommandLock("AT^CPIN?", "^CPIN", 0, &pResponse);
740     TELEPHONY_LOGI("AT+^CPIN send failed ret:%{public}d", ret);
741     if (ret != 0 || pResponse == NULL || !pResponse->success) {
742         TELEPHONY_LOGE("AT^CPIN? send failed");
743         if (pResponse && pResponse->result) {
744             pLine = pResponse->result;
745             SkipATPrefix(&pLine);
746             NextInt(&pLine, &ret);
747         } else {
748             ret = HRIL_ERR_GENERIC_FAILURE;
749         }
750         FreeResponseInfo(pResponse);
751         return;
752     }
753     if (pResponse && pResponse->head) {
754         pLine = pResponse->head->data;
755     }
756     TELEPHONY_LOGD("ReqGetSimPinInputTimesRemain pLine:%{public}s, result:%{public}s, success:%{public}d", pLine,
757         pResponse->result, pResponse->success);
758     ret = ParseSimPinInputTimesResult(pLine, pinInputTimes);
759     TELEPHONY_LOGD("code:%{public}s, times:%{public}d, puk:%{public}d,"
760         " pin:%{public}d, puk2:%{public}d, pin2:%{public}d",
761         pinInputTimes->code, pinInputTimes->times, pinInputTimes->pukTimes,
762         pinInputTimes->pinTimes, pinInputTimes->puk2Times, pinInputTimes->pin2Times);
763     if (ret != 0) {
764         if (pResponse && pResponse->result) {
765             pLine = pResponse->result;
766             SkipATPrefix(&pLine);
767             NextInt(&pLine, &ret);
768         } else {
769             ret = HRIL_ERR_GENERIC_FAILURE;
770         }
771         TELEPHONY_LOGE("AT+^CPIN send failed ret3:%{public}d", ret);
772         return;
773     }
774     return;
775 }
776 
ReqUnlockPin2(const ReqDataInfo * requestInfo,const char * pin2)777 void ReqUnlockPin2(const ReqDataInfo *requestInfo, const char *pin2)
778 {
779     char cmd[MAX_CMD_LENGTH] = {0};
780     char *pLine = NULL;
781     int32_t ret;
782     ResponseInfo *pResponse = NULL;
783 
784     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^CPIN2=\"%s\"", pin2);
785     if (result <= 0) {
786         TELEPHONY_LOGE("GenerateCommand is failed");
787     }
788     ret = SendCommandLock(cmd, "^CPIN2", 0, &pResponse);
789     HRilLockStatus lockStatus = {0};
790     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
791         TELEPHONY_LOGE("AT^CPIN2 send failed");
792         if (pResponse && pResponse->result) {
793             pLine = pResponse->result;
794             TELEPHONY_LOGD("AT^CPIN2 send failed pLine1:%{public}s", pLine);
795             SkipATPrefix(&pLine);
796             TELEPHONY_LOGD("AT^CPIN2 send failed pLine2:%{public}s", pLine);
797             NextInt(&pLine, &ret);
798             TELEPHONY_LOGD("AT^CPIN2 send failed ret:%{public}d", ret);
799             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
800                 HRilPinInputTimes pinInputTimes = {0};
801                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
802                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
803                 lockStatus.remain = pinInputTimes.pin2Times;
804             } else {
805                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
806                 lockStatus.remain = -1;
807                 TELEPHONY_LOGE("AT+^CPIN2 send failed");
808             }
809             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
810             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
811             FreeResponseInfo(pResponse);
812             return;
813         } else {
814             ret = HRIL_ERR_GENERIC_FAILURE;
815             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
816             lockStatus.remain = -1;
817             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
818             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
819             FreeResponseInfo(pResponse);
820             return;
821         }
822     }
823 
824     lockStatus.result = HRIL_UNLOCK_SUCCESS;
825     lockStatus.remain = -1;
826     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
827     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
828     FreeResponseInfo(pResponse);
829 }
830 
ReqUnlockPuk2(const ReqDataInfo * requestInfo,const char * puk2,const char * pin2)831 void ReqUnlockPuk2(const ReqDataInfo *requestInfo, const char *puk2, const char *pin2)
832 {
833     char cmd[MAX_CMD_LENGTH] = {0};
834     char *pLine = NULL;
835     int32_t ret;
836     ResponseInfo *pResponse = NULL;
837 
838     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^CPIN2=\"%s\",\"%s\"", puk2, pin2);
839     if (result <= 0) {
840         TELEPHONY_LOGE("GenerateCommand is failed");
841     }
842     ret = SendCommandLock(cmd, "^CPIN2", 0, &pResponse);
843     HRilLockStatus lockStatus = {0};
844     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
845         TELEPHONY_LOGE("AT^CPIN2 send failed");
846         if (pResponse && pResponse->result) {
847             pLine = pResponse->result;
848             TELEPHONY_LOGD("AT^CPIN2 send failed pLine1:%{public}s", pLine);
849             SkipATPrefix(&pLine);
850             TELEPHONY_LOGD("AT^CPIN2 send failed pLine2:%{public}s", pLine);
851             NextInt(&pLine, &ret);
852             TELEPHONY_LOGD("AT^CPIN2 send failed ret:%{public}d", ret);
853             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
854                 HRilPinInputTimes pinInputTimes = {0};
855                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
856                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
857                 lockStatus.remain = pinInputTimes.puk2Times;
858             } else {
859                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
860                 lockStatus.remain = -1;
861                 TELEPHONY_LOGE("AT+^CPIN2 send failed");
862             }
863             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
864             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
865             FreeResponseInfo(pResponse);
866             return;
867         } else {
868             ret = HRIL_ERR_GENERIC_FAILURE;
869             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
870             lockStatus.remain = -1;
871             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
872             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
873             FreeResponseInfo(pResponse);
874             return;
875         }
876     }
877 
878     lockStatus.result = HRIL_UNLOCK_SUCCESS;
879     lockStatus.remain = -1;
880     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
881     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
882     FreeResponseInfo(pResponse);
883 }
884 
ReqGetSimPin2InputTimes(const ReqDataInfo * requestInfo)885 void ReqGetSimPin2InputTimes(const ReqDataInfo *requestInfo)
886 {
887     char *pLine = NULL;
888     int32_t ret;
889     HRilPinInputTimes pin2InputTimes = {0};
890     ResponseInfo *pResponse = NULL;
891 
892     ret = SendCommandLock("AT^CPIN2?", "^CPIN2", 0, &pResponse);
893     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
894         TELEPHONY_LOGE("AT^CPIN2? send failed");
895     }
896     if (pResponse != NULL && pResponse->head != NULL) {
897         pLine = pResponse->head->data;
898     }
899     ret = ParseSimPinInputTimesResult(pLine, &pin2InputTimes);
900     if (ret != 0) {
901         if (pResponse && pResponse->result) {
902             pLine = pResponse->result;
903             SkipATPrefix(&pLine);
904             NextInt(&pLine, &ret);
905         } else {
906             ret = HRIL_ERR_GENERIC_FAILURE;
907         }
908         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
909         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
910         FreeResponseInfo(pResponse);
911         return;
912     }
913     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
914     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&pin2InputTimes, 0);
915     FreeResponseInfo(pResponse);
916 }
917 
ReqSetActiveSim(const ReqDataInfo * requestInfo,int32_t index,int32_t enable)918 void ReqSetActiveSim(const ReqDataInfo *requestInfo, int32_t index, int32_t enable)
919 {
920     char cmd[MAX_CMD_LENGTH] = {0};
921     char *pLine = NULL;
922     int32_t ret;
923     ResponseInfo *pResponse = NULL;
924 
925     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^HVSST=%d,%d", index, enable);
926     if (result <= 0) {
927         TELEPHONY_LOGE("GenerateCommand is failed");
928     }
929     ret = SendCommandLock(cmd, "^HVSST", 0, &pResponse);
930     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
931         TELEPHONY_LOGE("AT^HVSST send failed");
932         if (pResponse && pResponse->result) {
933             pLine = pResponse->result;
934             SkipATPrefix(&pLine);
935             NextInt(&pLine, &ret);
936         } else {
937             ret = HRIL_ERR_GENERIC_FAILURE;
938         }
939         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
940         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
941         FreeResponseInfo(pResponse);
942         return;
943     }
944     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
945     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
946     FreeResponseInfo(pResponse);
947 }
948 
ReqSimStkSendTerminalResponse(const ReqDataInfo * requestInfo,const char * strCmd)949 void ReqSimStkSendTerminalResponse(const ReqDataInfo *requestInfo, const char *strCmd)
950 {
951     char cmd[MAX_CMD_LENGTH] = {0};
952     char *pLine = NULL;
953     int32_t ret;
954     ResponseInfo *pResponse = NULL;
955 
956     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPUSATTERMINAL=\"%s\"", strCmd);
957     if (result < 0) {
958         TELEPHONY_LOGE("GenerateCommand failed, err = %{public}d\n", result);
959     }
960     ret = SendCommandLock(cmd, "+SPUSATTERMINAL", 0, &pResponse);
961     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
962         TELEPHONY_LOGE("AT+SPUSATTERMINAL send failed");
963         if (pResponse && pResponse->result) {
964             pLine = pResponse->result;
965             SkipATPrefix(&pLine);
966             NextInt(&pLine, &ret);
967         } else {
968             ret = HRIL_ERR_GENERIC_FAILURE;
969         }
970         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
971         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
972         FreeResponseInfo(pResponse);
973         return;
974     }
975     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
976     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
977     FreeResponseInfo(pResponse);
978 }
979 
ReqSimStkSendEnvelope(const ReqDataInfo * requestInfo,const char * strCmd)980 void ReqSimStkSendEnvelope(const ReqDataInfo *requestInfo, const char *strCmd)
981 {
982     char cmd[MAX_CMD_LENGTH] = {0};
983     char *pLine = NULL;
984     int32_t ret;
985     ResponseInfo *pResponse = NULL;
986 
987     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPUSATENVECMD=\"%s\"", strCmd);
988     if (result < 0) {
989         TELEPHONY_LOGE("GenerateCommand failed, err = %{public}d\n", result);
990     }
991     ret = SendCommandLock(cmd, "+SPUSATENVECMD", 0, &pResponse);
992     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
993         TELEPHONY_LOGE("AT+SPUSATENVECMD send failed");
994         if (pResponse && pResponse->result) {
995             pLine = pResponse->result;
996             SkipATPrefix(&pLine);
997             NextInt(&pLine, &ret);
998         } else {
999             ret = HRIL_ERR_GENERIC_FAILURE;
1000         }
1001         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1002         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1003         FreeResponseInfo(pResponse);
1004         return;
1005     }
1006     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1007     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1008     FreeResponseInfo(pResponse);
1009 }
1010 
ReqSimStkSendCallSetupRequestResult(const ReqDataInfo * requestInfo,int32_t accept)1011 void ReqSimStkSendCallSetupRequestResult(const ReqDataInfo *requestInfo, int32_t accept)
1012 {
1013     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1014     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1015 }
1016 
ReqSimStkIsReady(const ReqDataInfo * requestInfo)1017 void ReqSimStkIsReady(const ReqDataInfo *requestInfo)
1018 {
1019     char *pLine = NULL;
1020     int32_t ret;
1021     ResponseInfo *pResponse = NULL;
1022 
1023     ret = SendCommandLock("AT+SPUSATPROFILE?", "+SPUSATPROFILE", 0, &pResponse);
1024     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1025         TELEPHONY_LOGE("AT+SPUSATPROFILE send failed");
1026         if (pResponse && pResponse->result) {
1027             pLine = pResponse->result;
1028             SkipATPrefix(&pLine);
1029             NextInt(&pLine, &ret);
1030         } else {
1031             ret = HRIL_ERR_GENERIC_FAILURE;
1032         }
1033         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1034         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1035         FreeResponseInfo(pResponse);
1036         return;
1037     }
1038     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1039     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1040     FreeResponseInfo(pResponse);
1041 }
1042 
ReqGetRadioProtocol(const ReqDataInfo * requestInfo)1043 void ReqGetRadioProtocol(const ReqDataInfo *requestInfo)
1044 {
1045     TELEPHONY_LOGD("ReqGetRadioProtocol");
1046     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1047     HRilRadioProtocol radioProtocol = {};
1048     radioProtocol.sessionId = 0;
1049     radioProtocol.phase = HRIL_RADIO_PROTOCOL_PHASE_INITIAL;
1050     radioProtocol.technology = HRIL_RADIO_PROTOCOL_TECH_GSM | HRIL_RADIO_PROTOCOL_TECH_WCDMA |
1051                                HRIL_RADIO_PROTOCOL_TECH_HSPA | HRIL_RADIO_PROTOCOL_TECH_HSPAP |
1052                                HRIL_RADIO_PROTOCOL_TECH_LTE | HRIL_RADIO_PROTOCOL_TECH_LTE_CA;
1053     radioProtocol.modemId = 0;
1054     radioProtocol.status = HRIL_RADIO_PROTOCOL_STATUS_NONE;
1055     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&radioProtocol, sizeof(HRilRadioProtocol));
1056 }
1057 
ReqSetRadioProtocol(const ReqDataInfo * requestInfo,const HRilRadioProtocol * data)1058 void ReqSetRadioProtocol(const ReqDataInfo *requestInfo, const HRilRadioProtocol *data)
1059 {
1060     HRilRadioProtocol *radioProtocol = (HRilRadioProtocol *)data;
1061     struct ReportInfo reportInfo = { 0 };
1062     if (radioProtocol == NULL) {
1063         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
1064         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1065         return;
1066     }
1067 
1068     if (radioProtocol->phase != HRIL_RADIO_PROTOCOL_PHASE_UPDATE) {
1069         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1070         OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)radioProtocol, sizeof(HRilRadioProtocol));
1071         return;
1072     }
1073     radioProtocol->phase = HRIL_RADIO_PROTOCOL_PHASE_NOTIFY;
1074     radioProtocol->status = HRIL_RADIO_PROTOCOL_STATUS_SUCCESS;
1075     reportInfo.error = HRIL_ERR_SUCCESS;
1076     reportInfo.type = HRIL_NOTIFICATION;
1077     reportInfo.notifyId = HNOTI_SIM_RADIO_PROTOCOL_UPDATED;
1078     OnSimReport(GetSlotId(NULL), reportInfo, (const uint8_t *)radioProtocol, sizeof(HRilRadioProtocol));
1079 }
1080 
ReqSimOpenLogicalChannel(const ReqDataInfo * requestInfo,const char * appID,int32_t p2)1081 void ReqSimOpenLogicalChannel(const ReqDataInfo *requestInfo, const char *appID, int32_t p2)
1082 {
1083     char cmd[MAX_CMD_LENGTH] = {0};
1084     char *pLine = NULL;
1085     ResponseInfo *pResponse = NULL;
1086 
1087     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPCCHO=\"%s\",%d", appID, p2);
1088     if (result <= 0) {
1089         TELEPHONY_LOGE("GenerateCommand is failed");
1090     }
1091     int32_t ret = SendCommandLock(cmd, "+SPCCHO", 0, &pResponse);
1092     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1093         TELEPHONY_LOGE("AT+SPCCHO send failed");
1094         if (pResponse && pResponse->result) {
1095             pLine = pResponse->result;
1096             SkipATPrefix(&pLine);
1097             NextInt(&pLine, &ret);
1098         } else {
1099             ret = HRIL_ERR_GENERIC_FAILURE;
1100         }
1101         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1102         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1103         FreeResponseInfo(pResponse);
1104         return;
1105     }
1106     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1107     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1108     FreeResponseInfo(pResponse);
1109 }
1110 
ReqSimCloseLogicalChannel(const ReqDataInfo * requestInfo,int32_t channelId)1111 void ReqSimCloseLogicalChannel(const ReqDataInfo *requestInfo, int32_t channelId)
1112 {
1113     char cmd[MAX_CMD_LENGTH] = {0};
1114     char *pLine = NULL;
1115     int32_t ret;
1116     ResponseInfo *pResponse = NULL;
1117 
1118     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CCHC=%d", channelId);
1119     if (result <= 0) {
1120         TELEPHONY_LOGE("GenerateCommand is failed");
1121     }
1122     ret = SendCommandLock(cmd, "+CCHC", 0, &pResponse);
1123     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1124         TELEPHONY_LOGE("AT+CCHC send failed");
1125         if (pResponse && pResponse->result) {
1126             pLine = pResponse->result;
1127             SkipATPrefix(&pLine);
1128             NextInt(&pLine, &ret);
1129         } else {
1130             ret = HRIL_ERR_GENERIC_FAILURE;
1131         }
1132         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1133         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1134         FreeResponseInfo(pResponse);
1135         return;
1136     }
1137     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1138     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1139     FreeResponseInfo(pResponse);
1140 }
1141 
ReqSimTransmitApduLogicalChannel(const ReqDataInfo * requestInfo,HRilApduSimIO * data,size_t dataLen)1142 void ReqSimTransmitApduLogicalChannel(const ReqDataInfo *requestInfo, HRilApduSimIO *data, size_t dataLen)
1143 {
1144     char cmd[MAX_CMD_LENGTH] = {0};
1145     char *pLine = NULL;
1146     int32_t ret;
1147     ResponseInfo *pResponse = NULL;
1148     HRilSimIOResponse apduSimResponse = {0};
1149     HRilApduSimIO *pApduSimIO = (HRilApduSimIO *)data;
1150 
1151     if (pApduSimIO == NULL) {
1152         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
1153         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1154         FreeResponseInfo(pResponse);
1155         return;
1156     }
1157 
1158     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CGLA=%d,%d,%d,%d,%d,%d,\"%s\"", pApduSimIO->channelId,
1159         pApduSimIO->type, pApduSimIO->instruction, pApduSimIO->p1, pApduSimIO->p2, pApduSimIO->p3, pApduSimIO->data);
1160     if (result <= 0) {
1161         TELEPHONY_LOGE("GenerateCommand is failed");
1162     }
1163     ret = SendCommandLock(cmd, "+CGLA", 0, &pResponse);
1164     if (ret != 0 || pResponse == NULL || !pResponse->success) {
1165         TELEPHONY_LOGE("AT+CGLA send failed  dataLen:%{public}zu", dataLen);
1166         if (pResponse && pResponse->result) {
1167             pLine = pResponse->result;
1168             SkipATPrefix(&pLine);
1169             NextInt(&pLine, &ret);
1170         } else {
1171             ret = HRIL_ERR_GENERIC_FAILURE;
1172         }
1173         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1174         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1175         FreeResponseInfo(pResponse);
1176         return;
1177     }
1178     if (pResponse->head) {
1179         pLine = pResponse->head->data;
1180     }
1181     ret = ParseSimResponseResult(pLine, &apduSimResponse);
1182     if (ret != HRIL_ERR_SUCCESS) {
1183         HandlerSimIOResult(pResponse, NULL, requestInfo, pLine, &ret);
1184         return;
1185     }
1186     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1187     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&apduSimResponse, sizeof(HRilSimIOResponse));
1188     FreeResponseInfo(pResponse);
1189 }
1190 
ReqUnlockSimLock(const ReqDataInfo * requestInfo,int32_t lockType,const char * password)1191 void ReqUnlockSimLock(const ReqDataInfo *requestInfo, int32_t lockType, const char *password)
1192 {
1193     char cmd[MAX_CMD_LENGTH] = {0};
1194     char *pLine = NULL;
1195     int32_t ret;
1196     ResponseInfo *pResponse = NULL;
1197 
1198     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^UNLOCKSIMLOCK=\"%d\",%s", lockType, password);
1199     if (result <= 0) {
1200         TELEPHONY_LOGE("GenerateCommand is failed");
1201     }
1202     ret = SendCommandLock(cmd, "^UNLOCKSIMLOCK", 0, &pResponse);
1203     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1204         TELEPHONY_LOGE("AT^UNLOCKSIMLOCK send failed");
1205         if (pResponse && pResponse->result) {
1206             pLine = pResponse->result;
1207             SkipATPrefix(&pLine);
1208             NextInt(&pLine, &ret);
1209         } else {
1210             ret = HRIL_ERR_GENERIC_FAILURE;
1211         }
1212         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1213         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1214         FreeResponseInfo(pResponse);
1215         return;
1216     }
1217     if (pResponse && pResponse->head) {
1218         pLine = pResponse->head->data;
1219     }
1220     HRilLockStatus lockStatus = {0};
1221     ret = ParseUnlockSimLockResult(pLine, &lockStatus);
1222     if (ret != 0) {
1223         if (pResponse && pResponse->result) {
1224             pLine = pResponse->result;
1225             SkipATPrefix(&pLine);
1226             NextInt(&pLine, &ret);
1227         } else {
1228             ret = HRIL_ERR_GENERIC_FAILURE;
1229         }
1230         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1231         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1232         FreeResponseInfo(pResponse);
1233         return;
1234     }
1235     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1236     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(HRilLockStatus));
1237     FreeResponseInfo(pResponse);
1238 }
1239 
ConvertUsimFcpToSimRsp(uint8_t ** simResponse)1240 void ConvertUsimFcpToSimRsp(uint8_t **simResponse)
1241 {
1242     uint16_t fcpLen = strlen((char *)*simResponse) / HALF_LEN;
1243     uint8_t *fcpByte = malloc(fcpLen);
1244     UsimFileDescriptor fDescriptor = { 0 };
1245     UsimFileIdentifier fId = { 0 };
1246     uint8_t simRspByte[GET_RESPONSE_EF_SIZE_BYTES] = { 0 };
1247     if (fcpByte == NULL) {
1248         TELEPHONY_LOGE("fcpByte is NULL");
1249         free(fcpByte);
1250         return;
1251     }
1252     if (memset_s(fcpByte, fcpLen, 0, fcpLen) != EOK) {
1253         TELEPHONY_LOGE("ConvertUsimFcpToSimRsp memset_s failed");
1254         free(fcpByte);
1255         return;
1256     }
1257     ConvertHexStringToByteArray(*simResponse, strlen((char *)*simResponse), fcpByte, fcpLen);
1258     if (FcpFileDescriptorQuery(fcpByte, fcpLen, (UsimFileDescriptor *)&fDescriptor) == FALSE) {
1259         TELEPHONY_LOGE("FcpFileDescriptorQuery failed");
1260         free(fcpByte);
1261         return;
1262     }
1263     if (FcpFileIdentifierQuery(fcpByte, fcpLen, (UsimFileIdentifier *)&fId) == FALSE) {
1264         TELEPHONY_LOGE("FcpFileIdentifierQuery failed");
1265         free(fcpByte);
1266         return;
1267     }
1268     if (IsDedicatedFile(fDescriptor.fd)) {
1269         simRspByte[RESPONSE_DATA_FILE_TYPE] = TYPE_DF;
1270         *simResponse = ConvertByteArrayToHexString(simRspByte, fcpLen);
1271         free(fcpByte);
1272         return;
1273     }
1274     CreateSimRspByte(simRspByte, GET_RESPONSE_EF_SIZE_BYTES, &fId, &fDescriptor);
1275     *simResponse = ConvertByteArrayToHexString(simRspByte, GET_RESPONSE_EF_SIZE_BYTES);
1276     free(fcpByte);
1277 }
1278 
CreateSimRspByte(uint8_t simRspByte[],int responseLen,UsimFileIdentifier * fId,UsimFileDescriptor * fDescriptor)1279 void CreateSimRspByte(uint8_t simRspByte[], int responseLen, UsimFileIdentifier *fId, UsimFileDescriptor *fDescriptor)
1280 {
1281     if (responseLen < RESPONSE_DATA_RECORD_LENGTH + 1) {
1282         TELEPHONY_LOGE("simRspByte size error");
1283         return;
1284     }
1285     if (fId == NULL || fDescriptor == NULL) {
1286         TELEPHONY_LOGE("fId or  fDescriptor is null");
1287         return;
1288     }
1289     simRspByte[RESPONSE_DATA_FILE_TYPE] = TYPE_EF;
1290     simRspByte[RESPONSE_DATA_FILE_ID_1] = (fId->fileId & BYTE_NUM_1) >> ADDR_OFFSET_8BIT;
1291     simRspByte[RESPONSE_DATA_FILE_ID_2] = fId->fileId & BYTE_NUM_2;
1292     simRspByte[RESPONSE_DATA_LENGTH] = (GET_RESPONSE_EF_SIZE_BYTES - RESPONSE_DATA_LENGTH - 1);
1293     if (IsLinearFixedFile(fDescriptor->fd)) {
1294         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_LINEAR_FIXED;
1295         simRspByte[RESPONSE_DATA_RECORD_LENGTH] = fDescriptor->recLen;
1296         fDescriptor->dataSize = (fDescriptor->numRec & BYTE_NUM_0) * (fDescriptor->recLen);
1297         simRspByte[RESPONSE_DATA_FILE_SIZE_1] = (fDescriptor->dataSize & BYTE_NUM_1) >> ADDR_OFFSET_8BIT;
1298         simRspByte[RESPONSE_DATA_FILE_SIZE_2] = fDescriptor->dataSize & BYTE_NUM_2;
1299         simRspByte[RESPONSE_DATA_FILE_STATUS] = VALID_FILE_STATUS;
1300         return;
1301     }
1302     if (IsTransparentFile(fDescriptor->fd)) {
1303         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_TRANSPARENT;
1304         return;
1305     }
1306     if (IsCyclicFile(fDescriptor->fd)) {
1307         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_CYCLIC;
1308         simRspByte[RESPONSE_DATA_RECORD_LENGTH] = fDescriptor->recLen;
1309         return;
1310     }
1311 }
1312 
FcpTlvSearchTag(uint8_t * dataPtr,uint16_t len,UsimFcpTag tag,uint8_t ** outPtr)1313 uint8_t FcpTlvSearchTag(uint8_t *dataPtr, uint16_t len, UsimFcpTag tag, uint8_t **outPtr)
1314 {
1315     uint8_t tagLen = 0;
1316     uint16_t lenVar = len;
1317     for (*outPtr = dataPtr; lenVar > 0; *outPtr += tagLen) {
1318         tagLen = (*(*outPtr + 1) + HALF_LEN);
1319         if (**outPtr == (uint8_t)tag) {
1320             *outPtr += HALF_LEN;
1321             return *(*outPtr - 1);
1322         }
1323         lenVar -= tagLen;
1324     }
1325     *outPtr = NULL;
1326     return FALSE;
1327 }
1328 
FcpFileDescriptorQuery(uint8_t * fcpByte,uint16_t fcpLen,UsimFileDescriptor * filledStructPtr)1329 uint8_t FcpFileDescriptorQuery(uint8_t *fcpByte, uint16_t fcpLen, UsimFileDescriptor *filledStructPtr)
1330 {
1331     if (fcpByte == NULL || fcpLen < HALF_LEN + 1) {
1332         TELEPHONY_LOGE("fcpByte size error");
1333         return FALSE;
1334     }
1335     uint8_t valueLen = fcpByte[1];
1336     uint8_t *dataPtr = &fcpByte[HALF_LEN];
1337     if (fcpByte[0] != FCP_TEMP_T) {
1338         TELEPHONY_LOGE("fcpByte data error");
1339         return FALSE;
1340     }
1341     uint8_t resultLen = 0;
1342     uint8_t *outPtr = NULL;
1343     UsimFileDescriptor *queryPtr = filledStructPtr;
1344     resultLen = FcpTlvSearchTag(dataPtr, valueLen, FCP_FILE_DES_T, &outPtr);
1345     if (!((outPtr != NULL) && ((resultLen == HALF_LEN) || (resultLen == FIVE_LEN)))) {
1346         TELEPHONY_LOGE("resultLen value error");
1347         return FALSE;
1348     }
1349     queryPtr->fd = outPtr[0];
1350     queryPtr->dataCoding = outPtr[1];
1351     if (resultLen == FIVE_LEN) {
1352         queryPtr->recLen = (short)((outPtr[HALF_LEN] << ADDR_OFFSET_8BIT) | outPtr[THIRD_INDEX]);
1353         queryPtr->numRec = outPtr[HALF_BYTE_LEN];
1354         return TRUE;
1355     }
1356     queryPtr->recLen = 0;
1357     queryPtr->numRec = 0;
1358     return TRUE;
1359 }
1360 
FcpFileIdentifierQuery(uint8_t * fcpByte,uint16_t fcpLen,UsimFileIdentifier * filledStructPtr)1361 uint8_t FcpFileIdentifierQuery(uint8_t *fcpByte, uint16_t fcpLen, UsimFileIdentifier *filledStructPtr)
1362 {
1363     if (fcpByte == NULL || fcpLen < HALF_LEN + 1) {
1364         TELEPHONY_LOGE("fcpByte size error");
1365         return FALSE;
1366     }
1367     uint8_t valueLen = fcpByte[1];
1368     uint8_t *dataPtr = &fcpByte[HALF_LEN];
1369     if (fcpByte[0] != FCP_TEMP_T) {
1370         TELEPHONY_LOGE("fcpByte data error");
1371         return FALSE;
1372     }
1373     uint8_t resultLen = 0;
1374     uint8_t *outPtr = NULL;
1375     UsimFileIdentifier *queryPtr = (UsimFileIdentifier *)filledStructPtr;
1376     resultLen = FcpTlvSearchTag(dataPtr, valueLen, FCP_FILE_ID_T, &outPtr);
1377     if (outPtr == NULL) {
1378         queryPtr->fileId = 0;
1379         return FALSE;
1380     }
1381     if (resultLen != HALF_LEN) {
1382         TELEPHONY_LOGE("resultLen size error");
1383         return FALSE;
1384     }
1385     queryPtr->fileId = (short)((outPtr[0] << ADDR_OFFSET_8BIT) | outPtr[1]);
1386     return TRUE;
1387 }
1388 
IsCyclicFile(uint8_t fd)1389 uint8_t IsCyclicFile(uint8_t fd)
1390 {
1391     return (0x07 & (fd)) == 0x06;
1392 }
1393 
IsDedicatedFile(uint8_t fd)1394 uint8_t IsDedicatedFile(uint8_t fd)
1395 {
1396     return (0x38 & (fd)) == 0x38;
1397 }
1398 
IsLinearFixedFile(uint8_t fd)1399 uint8_t IsLinearFixedFile(uint8_t fd)
1400 {
1401     return (0x07 & (fd)) == 0x02;
1402 }
1403 
IsTransparentFile(uint8_t fd)1404 uint8_t IsTransparentFile(uint8_t fd)
1405 {
1406     return (0x07 & (fd)) == 0x01;
1407 }
1408 
ConvertHexStringToByteArray(uint8_t * originHexString,int responseLen,uint8_t * byteArray,int fcpLen)1409 void ConvertHexStringToByteArray(uint8_t *originHexString, int responseLen, uint8_t *byteArray, int fcpLen)
1410 {
1411     if (responseLen <= 0 || fcpLen <= 0) {
1412         TELEPHONY_LOGE("originHexString is error, size=%{public}d", responseLen);
1413         return;
1414     }
1415     for (int i = 0; i < fcpLen; i++) {
1416         int hexIndex = i * HALF_LEN;
1417         if (hexIndex + 1 >= responseLen) {
1418             break;
1419         }
1420         byteArray[i] = (ToByte(originHexString[hexIndex]) << HALF_BYTE_LEN) | ToByte(originHexString[hexIndex + 1]);
1421     }
1422 }
1423 
ConvertByteArrayToHexString(uint8_t * byteArray,int byteArrayLen)1424 uint8_t *ConvertByteArrayToHexString(uint8_t *byteArray, int byteArrayLen)
1425 {
1426     uint8_t *buf = malloc(byteArrayLen * HALF_LEN + 1);
1427     if (buf == NULL) {
1428         TELEPHONY_LOGE("buf is NULL");
1429         return NULL;
1430     }
1431     int bufIndex = 0;
1432     const char HEX_DIGITS[HEX_DIGITS_LEN] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
1433         'F' };
1434     for (int i = 0; i < byteArrayLen; i++) {
1435         uint8_t b = byteArray[i];
1436         buf[bufIndex++] = HEX_DIGITS[(b >> HALF_BYTE_LEN) & BYTE_NUM_4];
1437         buf[bufIndex++] = HEX_DIGITS[b & BYTE_NUM_4];
1438     }
1439     buf[bufIndex] = '\0';
1440     return buf;
1441 }
1442 
ToByte(char c)1443 uint8_t ToByte(char c)
1444 {
1445     if (c >= '0' && c <= '9') {
1446         return (c - '0');
1447     }
1448     if (c >= 'A' && c <= 'F') {
1449         return (c - 'A' + DECIMAL_MAX);
1450     }
1451     if (c >= 'a' && c <= 'f') {
1452         return (c - 'a' + DECIMAL_MAX);
1453     }
1454     TELEPHONY_LOGE("ToByte Error: %{public}c", c);
1455     return 0;
1456 }
1457