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