• 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 (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->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     ret = ParseSimPinInputTimesResult(pLine, pinInputTimes);
749     TELEPHONY_LOGD("code:%{public}s, times:%{public}d, puk:%{public}d,"
750         " pin:%{public}d, puk2:%{public}d, pin2:%{public}d",
751         pinInputTimes->code, pinInputTimes->times, pinInputTimes->pukTimes,
752         pinInputTimes->pinTimes, pinInputTimes->puk2Times, pinInputTimes->pin2Times);
753     if (ret != 0) {
754         if (pResponse && pResponse->result) {
755             pLine = pResponse->result;
756             SkipATPrefix(&pLine);
757             NextInt(&pLine, &ret);
758         } else {
759             ret = HRIL_ERR_GENERIC_FAILURE;
760         }
761         TELEPHONY_LOGE("AT+^CPIN send failed ret3:%{public}d", ret);
762         return;
763     }
764     return;
765 }
766 
ReqUnlockPin2(const ReqDataInfo * requestInfo,const char * pin2)767 void ReqUnlockPin2(const ReqDataInfo *requestInfo, const char *pin2)
768 {
769     char cmd[MAX_CMD_LENGTH] = {0};
770     char *pLine = NULL;
771     int32_t ret;
772     ResponseInfo *pResponse = NULL;
773 
774     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^CPIN2=\"%s\"", pin2);
775     if (result <= 0) {
776         TELEPHONY_LOGE("GenerateCommand is failed");
777     }
778     ret = SendCommandLock(cmd, "^CPIN2", 0, &pResponse);
779     HRilLockStatus lockStatus = {0};
780     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
781         TELEPHONY_LOGE("AT^CPIN2 send failed");
782         if (pResponse && pResponse->result) {
783             pLine = pResponse->result;
784             TELEPHONY_LOGD("AT^CPIN2 send failed pLine1:%{public}s", pLine);
785             SkipATPrefix(&pLine);
786             TELEPHONY_LOGD("AT^CPIN2 send failed pLine2:%{public}s", pLine);
787             NextInt(&pLine, &ret);
788             TELEPHONY_LOGD("AT^CPIN2 send failed ret:%{public}d", ret);
789             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
790                 HRilPinInputTimes pinInputTimes = {0};
791                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
792                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
793                 lockStatus.remain = pinInputTimes.pin2Times;
794             } else {
795                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
796                 lockStatus.remain = -1;
797                 TELEPHONY_LOGE("AT+^CPIN2 send failed");
798             }
799             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
800             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
801             FreeResponseInfo(pResponse);
802             return;
803         } else {
804             ret = HRIL_ERR_GENERIC_FAILURE;
805             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
806             lockStatus.remain = -1;
807             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
808             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
809             FreeResponseInfo(pResponse);
810             return;
811         }
812     }
813 
814     lockStatus.result = HRIL_UNLOCK_SUCCESS;
815     lockStatus.remain = -1;
816     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
817     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
818     FreeResponseInfo(pResponse);
819 }
820 
ReqUnlockPuk2(const ReqDataInfo * requestInfo,const char * puk2,const char * pin2)821 void ReqUnlockPuk2(const ReqDataInfo *requestInfo, const char *puk2, const char *pin2)
822 {
823     char cmd[MAX_CMD_LENGTH] = {0};
824     char *pLine = NULL;
825     int32_t ret;
826     ResponseInfo *pResponse = NULL;
827 
828     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^CPIN2=\"%s\",\"%s\"", puk2, pin2);
829     if (result <= 0) {
830         TELEPHONY_LOGE("GenerateCommand is failed");
831     }
832     ret = SendCommandLock(cmd, "^CPIN2", 0, &pResponse);
833     HRilLockStatus lockStatus = {0};
834     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
835         TELEPHONY_LOGE("AT^CPIN2 send failed");
836         if (pResponse && pResponse->result) {
837             pLine = pResponse->result;
838             TELEPHONY_LOGD("AT^CPIN2 send failed pLine1:%{public}s", pLine);
839             SkipATPrefix(&pLine);
840             TELEPHONY_LOGD("AT^CPIN2 send failed pLine2:%{public}s", pLine);
841             NextInt(&pLine, &ret);
842             TELEPHONY_LOGD("AT^CPIN2 send failed ret:%{public}d", ret);
843             if (ret == AT_RESPONSE_INCORRECT_PASSWORD) {
844                 HRilPinInputTimes pinInputTimes = {0};
845                 ReqGetSimPinInputTimesRemain(requestInfo, &pinInputTimes);
846                 lockStatus.result = HRIL_UNLOCK_PASSWORD_ERR;
847                 lockStatus.remain = pinInputTimes.puk2Times;
848             } else {
849                 lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
850                 lockStatus.remain = -1;
851                 TELEPHONY_LOGE("AT+^CPIN2 send failed");
852             }
853             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
854             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
855             FreeResponseInfo(pResponse);
856             return;
857         } else {
858             ret = HRIL_ERR_GENERIC_FAILURE;
859             lockStatus.result = HRIL_UNLOCK_OTHER_ERR;
860             lockStatus.remain = -1;
861             struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
862             OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
863             FreeResponseInfo(pResponse);
864             return;
865         }
866     }
867 
868     lockStatus.result = HRIL_UNLOCK_SUCCESS;
869     lockStatus.remain = -1;
870     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
871     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(lockStatus));
872     FreeResponseInfo(pResponse);
873 }
874 
ReqGetSimPin2InputTimes(const ReqDataInfo * requestInfo)875 void ReqGetSimPin2InputTimes(const ReqDataInfo *requestInfo)
876 {
877     char *pLine = NULL;
878     int32_t ret;
879     HRilPinInputTimes pin2InputTimes = {0};
880     ResponseInfo *pResponse = NULL;
881 
882     ret = SendCommandLock("AT^CPIN2?", "^CPIN2", 0, &pResponse);
883     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
884         TELEPHONY_LOGE("AT^CPIN2? send failed");
885     }
886     if (pResponse != NULL && pResponse->head != NULL) {
887         pLine = pResponse->head->data;
888     }
889     ret = ParseSimPinInputTimesResult(pLine, &pin2InputTimes);
890     if (ret != 0) {
891         if (pResponse && pResponse->result) {
892             pLine = pResponse->result;
893             SkipATPrefix(&pLine);
894             NextInt(&pLine, &ret);
895         } else {
896             ret = HRIL_ERR_GENERIC_FAILURE;
897         }
898         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
899         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
900         FreeResponseInfo(pResponse);
901         return;
902     }
903     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
904     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&pin2InputTimes, 0);
905     FreeResponseInfo(pResponse);
906 }
907 
ReqSetActiveSim(const ReqDataInfo * requestInfo,int32_t index,int32_t enable)908 void ReqSetActiveSim(const ReqDataInfo *requestInfo, int32_t index, int32_t enable)
909 {
910     char cmd[MAX_CMD_LENGTH] = {0};
911     char *pLine = NULL;
912     int32_t ret;
913     ResponseInfo *pResponse = NULL;
914 
915     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^HVSST=%d,%d", index, enable);
916     if (result <= 0) {
917         TELEPHONY_LOGE("GenerateCommand is failed");
918     }
919     ret = SendCommandLock(cmd, "^HVSST", 0, &pResponse);
920     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
921         TELEPHONY_LOGE("AT^HVSST send failed");
922         if (pResponse && pResponse->result) {
923             pLine = pResponse->result;
924             SkipATPrefix(&pLine);
925             NextInt(&pLine, &ret);
926         } else {
927             ret = HRIL_ERR_GENERIC_FAILURE;
928         }
929         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
930         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
931         FreeResponseInfo(pResponse);
932         return;
933     }
934     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
935     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
936     FreeResponseInfo(pResponse);
937 }
938 
ReqSimStkSendTerminalResponse(const ReqDataInfo * requestInfo,const char * strCmd)939 void ReqSimStkSendTerminalResponse(const ReqDataInfo *requestInfo, const char *strCmd)
940 {
941     char cmd[MAX_CMD_LENGTH] = {0};
942     char *pLine = NULL;
943     int32_t ret;
944     ResponseInfo *pResponse = NULL;
945 
946     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPUSATTERMINAL=\"%s\"", strCmd);
947     if (result < 0) {
948         TELEPHONY_LOGE("GenerateCommand failed, err = %{public}d\n", result);
949     }
950     ret = SendCommandLock(cmd, "+SPUSATTERMINAL", 0, &pResponse);
951     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
952         TELEPHONY_LOGE("AT+SPUSATTERMINAL send failed");
953         if (pResponse && pResponse->result) {
954             pLine = pResponse->result;
955             SkipATPrefix(&pLine);
956             NextInt(&pLine, &ret);
957         } else {
958             ret = HRIL_ERR_GENERIC_FAILURE;
959         }
960         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
961         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
962         FreeResponseInfo(pResponse);
963         return;
964     }
965     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
966     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
967     FreeResponseInfo(pResponse);
968 }
969 
ReqSimStkSendEnvelope(const ReqDataInfo * requestInfo,const char * strCmd)970 void ReqSimStkSendEnvelope(const ReqDataInfo *requestInfo, const char *strCmd)
971 {
972     char cmd[MAX_CMD_LENGTH] = {0};
973     char *pLine = NULL;
974     int32_t ret;
975     ResponseInfo *pResponse = NULL;
976 
977     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPUSATENVECMD=\"%s\"", strCmd);
978     if (result < 0) {
979         TELEPHONY_LOGE("GenerateCommand failed, err = %{public}d\n", result);
980     }
981     ret = SendCommandLock(cmd, "+SPUSATENVECMD", 0, &pResponse);
982     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
983         TELEPHONY_LOGE("AT+SPUSATENVECMD send failed");
984         if (pResponse && pResponse->result) {
985             pLine = pResponse->result;
986             SkipATPrefix(&pLine);
987             NextInt(&pLine, &ret);
988         } else {
989             ret = HRIL_ERR_GENERIC_FAILURE;
990         }
991         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
992         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
993         FreeResponseInfo(pResponse);
994         return;
995     }
996     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
997     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
998     FreeResponseInfo(pResponse);
999 }
1000 
ReqSimStkSendCallSetupRequestResult(const ReqDataInfo * requestInfo,int32_t accept)1001 void ReqSimStkSendCallSetupRequestResult(const ReqDataInfo *requestInfo, int32_t accept)
1002 {
1003     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1004     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1005 }
1006 
ReqSimStkIsReady(const ReqDataInfo * requestInfo)1007 void ReqSimStkIsReady(const ReqDataInfo *requestInfo)
1008 {
1009     char *pLine = NULL;
1010     int32_t ret;
1011     ResponseInfo *pResponse = NULL;
1012 
1013     ret = SendCommandLock("AT+SPUSATPROFILE?", "+SPUSATPROFILE", 0, &pResponse);
1014     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1015         TELEPHONY_LOGE("AT+SPUSATPROFILE send failed");
1016         if (pResponse && pResponse->result) {
1017             pLine = pResponse->result;
1018             SkipATPrefix(&pLine);
1019             NextInt(&pLine, &ret);
1020         } else {
1021             ret = HRIL_ERR_GENERIC_FAILURE;
1022         }
1023         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1024         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1025         FreeResponseInfo(pResponse);
1026         return;
1027     }
1028     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1029     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1030     FreeResponseInfo(pResponse);
1031 }
1032 
ReqGetRadioProtocol(const ReqDataInfo * requestInfo)1033 void ReqGetRadioProtocol(const ReqDataInfo *requestInfo)
1034 {
1035     TELEPHONY_LOGD("ReqGetRadioProtocol");
1036     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1037     HRilRadioProtocol radioProtocol = {};
1038     radioProtocol.sessionId = 0;
1039     radioProtocol.phase = HRIL_RADIO_PROTOCOL_PHASE_INITIAL;
1040     radioProtocol.technology = HRIL_RADIO_PROTOCOL_TECH_GSM | HRIL_RADIO_PROTOCOL_TECH_WCDMA |
1041                                HRIL_RADIO_PROTOCOL_TECH_HSPA | HRIL_RADIO_PROTOCOL_TECH_HSPAP |
1042                                HRIL_RADIO_PROTOCOL_TECH_LTE | HRIL_RADIO_PROTOCOL_TECH_LTE_CA;
1043     radioProtocol.modemId = 0;
1044     radioProtocol.status = HRIL_RADIO_PROTOCOL_STATUS_NONE;
1045     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&radioProtocol, sizeof(HRilRadioProtocol));
1046 }
1047 
ReqSetRadioProtocol(const ReqDataInfo * requestInfo,const HRilRadioProtocol * data)1048 void ReqSetRadioProtocol(const ReqDataInfo *requestInfo, const HRilRadioProtocol *data)
1049 {
1050     HRilRadioProtocol *radioProtocol = (HRilRadioProtocol *)data;
1051     struct ReportInfo reportInfo = { 0 };
1052     if (radioProtocol == NULL) {
1053         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
1054         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1055         return;
1056     }
1057 
1058     if (radioProtocol->phase != HRIL_RADIO_PROTOCOL_PHASE_UPDATE) {
1059         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1060         OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)radioProtocol, sizeof(HRilRadioProtocol));
1061         return;
1062     }
1063     radioProtocol->phase = HRIL_RADIO_PROTOCOL_PHASE_NOTIFY;
1064     radioProtocol->status = HRIL_RADIO_PROTOCOL_STATUS_SUCCESS;
1065     reportInfo.error = HRIL_ERR_SUCCESS;
1066     reportInfo.type = HRIL_NOTIFICATION;
1067     reportInfo.notifyId = HNOTI_SIM_RADIO_PROTOCOL_UPDATED;
1068     OnSimReport(GetSlotId(NULL), reportInfo, (const uint8_t *)radioProtocol, sizeof(HRilRadioProtocol));
1069 }
1070 
ReqSimOpenLogicalChannel(const ReqDataInfo * requestInfo,const char * appID,int32_t p2)1071 void ReqSimOpenLogicalChannel(const ReqDataInfo *requestInfo, const char *appID, int32_t p2)
1072 {
1073     char cmd[MAX_CMD_LENGTH] = {0};
1074     char *pLine = NULL;
1075     ResponseInfo *pResponse = NULL;
1076 
1077     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+SPCCHO=\"%s\",%d", appID, p2);
1078     if (result <= 0) {
1079         TELEPHONY_LOGE("GenerateCommand is failed");
1080     }
1081     int32_t ret = SendCommandLock(cmd, "+SPCCHO", 0, &pResponse);
1082     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1083         TELEPHONY_LOGE("AT+SPCCHO send failed");
1084         if (pResponse && pResponse->result) {
1085             pLine = pResponse->result;
1086             SkipATPrefix(&pLine);
1087             NextInt(&pLine, &ret);
1088         } else {
1089             ret = HRIL_ERR_GENERIC_FAILURE;
1090         }
1091         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1092         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1093         FreeResponseInfo(pResponse);
1094         return;
1095     }
1096     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1097     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1098     FreeResponseInfo(pResponse);
1099 }
1100 
ReqSimCloseLogicalChannel(const ReqDataInfo * requestInfo,int32_t channelId)1101 void ReqSimCloseLogicalChannel(const ReqDataInfo *requestInfo, int32_t channelId)
1102 {
1103     char cmd[MAX_CMD_LENGTH] = {0};
1104     char *pLine = NULL;
1105     int32_t ret;
1106     ResponseInfo *pResponse = NULL;
1107 
1108     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CCHC=%d", channelId);
1109     if (result <= 0) {
1110         TELEPHONY_LOGE("GenerateCommand is failed");
1111     }
1112     ret = SendCommandLock(cmd, "+CCHC", 0, &pResponse);
1113     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1114         TELEPHONY_LOGE("AT+CCHC send failed");
1115         if (pResponse && pResponse->result) {
1116             pLine = pResponse->result;
1117             SkipATPrefix(&pLine);
1118             NextInt(&pLine, &ret);
1119         } else {
1120             ret = HRIL_ERR_GENERIC_FAILURE;
1121         }
1122         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1123         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1124         FreeResponseInfo(pResponse);
1125         return;
1126     }
1127     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1128     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1129     FreeResponseInfo(pResponse);
1130 }
1131 
ReqSimTransmitApduLogicalChannel(const ReqDataInfo * requestInfo,HRilApduSimIO * data,size_t dataLen)1132 void ReqSimTransmitApduLogicalChannel(const ReqDataInfo *requestInfo, HRilApduSimIO *data, size_t dataLen)
1133 {
1134     char cmd[MAX_CMD_LENGTH] = {0};
1135     char *pLine = NULL;
1136     int32_t ret;
1137     ResponseInfo *pResponse = NULL;
1138     HRilSimIOResponse apduSimResponse = {0};
1139     HRilApduSimIO *pApduSimIO = (HRilApduSimIO *)data;
1140 
1141     if (pApduSimIO == NULL) {
1142         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
1143         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1144         FreeResponseInfo(pResponse);
1145         return;
1146     }
1147 
1148     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT+CGLA=%d,%d,%d,%d,%d,%d,\"%s\"", pApduSimIO->channelId,
1149         pApduSimIO->type, pApduSimIO->instruction, pApduSimIO->p1, pApduSimIO->p2, pApduSimIO->p3, pApduSimIO->data);
1150     if (result <= 0) {
1151         TELEPHONY_LOGE("GenerateCommand is failed");
1152     }
1153     ret = SendCommandLock(cmd, "+CGLA", 0, &pResponse);
1154     if (ret != 0 || pResponse == NULL || !pResponse->success) {
1155         TELEPHONY_LOGE("AT+CGLA send failed  dataLen:%{public}zu", dataLen);
1156         if (pResponse && pResponse->result) {
1157             pLine = pResponse->result;
1158             SkipATPrefix(&pLine);
1159             NextInt(&pLine, &ret);
1160         } else {
1161             ret = HRIL_ERR_GENERIC_FAILURE;
1162         }
1163         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1164         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1165         FreeResponseInfo(pResponse);
1166         return;
1167     }
1168     if (pResponse->head) {
1169         pLine = pResponse->head->data;
1170     }
1171     ret = ParseSimResponseResult(pLine, &apduSimResponse);
1172     if (ret != HRIL_ERR_SUCCESS) {
1173         HandlerSimIOResult(pResponse, NULL, requestInfo, pLine, &ret);
1174         return;
1175     }
1176     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1177     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&apduSimResponse, sizeof(HRilSimIOResponse));
1178     FreeResponseInfo(pResponse);
1179 }
1180 
ReqSimAuthentication(const ReqDataInfo * requestInfo,HRilSimAuthenticationRequestInfo * data,size_t dataLen)1181 void ReqSimAuthentication(const ReqDataInfo *requestInfo, HRilSimAuthenticationRequestInfo *data, size_t dataLen)
1182 {
1183     HRilSimAuthenticationRequestInfo *HRilSimAuthInfo = (HRilSimAuthenticationRequestInfo *)data;
1184     if (HRilSimAuthInfo == NULL) {
1185         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
1186         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1187         return;
1188     }
1189     TELEPHONY_LOGD("ReqSimAuthentication serial = %{public}d, aid = %{public}s, data = %{public}s",
1190         HRilSimAuthInfo->serial, HRilSimAuthInfo->aid, HRilSimAuthInfo->data);
1191     HRilSimIOResponse simAuthResponse = { 0 };
1192     simAuthResponse.sw1 = (int32_t)0x90;
1193     simAuthResponse.sw2 = (int32_t)0x00;
1194     simAuthResponse.response = ("FFFFFFFFFFFFFF");
1195     int32_t ret = 0;
1196     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1197     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&simAuthResponse, sizeof(HRilSimIOResponse));
1198 }
1199 
ReqUnlockSimLock(const ReqDataInfo * requestInfo,int32_t lockType,const char * password)1200 void ReqUnlockSimLock(const ReqDataInfo *requestInfo, int32_t lockType, const char *password)
1201 {
1202     char cmd[MAX_CMD_LENGTH] = {0};
1203     char *pLine = NULL;
1204     int32_t ret;
1205     ResponseInfo *pResponse = NULL;
1206 
1207     int32_t result = GenerateCommand(cmd, MAX_CMD_LENGTH, "AT^UNLOCKSIMLOCK=\"%d\",%s", lockType, password);
1208     if (result <= 0) {
1209         TELEPHONY_LOGE("GenerateCommand is failed");
1210     }
1211     ret = SendCommandLock(cmd, "^UNLOCKSIMLOCK", 0, &pResponse);
1212     if (ret != 0 || (pResponse != NULL && !pResponse->success)) {
1213         TELEPHONY_LOGE("AT^UNLOCKSIMLOCK send failed");
1214         if (pResponse && pResponse->result) {
1215             pLine = pResponse->result;
1216             SkipATPrefix(&pLine);
1217             NextInt(&pLine, &ret);
1218         } else {
1219             ret = HRIL_ERR_GENERIC_FAILURE;
1220         }
1221         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1222         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1223         FreeResponseInfo(pResponse);
1224         return;
1225     }
1226     if (pResponse && pResponse->head) {
1227         pLine = pResponse->head->data;
1228     }
1229     HRilLockStatus lockStatus = {0};
1230     ret = ParseUnlockSimLockResult(pLine, &lockStatus);
1231     if (ret != 0) {
1232         if (pResponse && pResponse->result) {
1233             pLine = pResponse->result;
1234             SkipATPrefix(&pLine);
1235             NextInt(&pLine, &ret);
1236         } else {
1237             ret = HRIL_ERR_GENERIC_FAILURE;
1238         }
1239         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, ret, HRIL_RESPONSE, 0);
1240         OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1241         FreeResponseInfo(pResponse);
1242         return;
1243     }
1244     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1245     OnSimReport(GetSlotId(requestInfo), reportInfo, (const uint8_t *)&lockStatus, sizeof(HRilLockStatus));
1246     FreeResponseInfo(pResponse);
1247 }
1248 
ReqSendSimMatchedOperatorInfo(const ReqDataInfo * requestInfo,HRilNcfgOperatorInfo * data,size_t dataLen)1249 void ReqSendSimMatchedOperatorInfo(const ReqDataInfo *requestInfo, HRilNcfgOperatorInfo *data, size_t dataLen)
1250 {
1251     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1252     OnSimReport(GetSlotId(requestInfo), reportInfo, NULL, 0);
1253 }
1254 
ConvertUsimFcpToSimRsp(uint8_t ** simResponse)1255 void ConvertUsimFcpToSimRsp(uint8_t **simResponse)
1256 {
1257     uint16_t fcpLen = strlen((char *)*simResponse) / HALF_LEN;
1258     uint8_t *fcpByte = malloc(fcpLen);
1259     UsimFileDescriptor fDescriptor = { 0 };
1260     UsimFileIdentifier fId = { 0 };
1261     uint8_t simRspByte[GET_RESPONSE_EF_SIZE_BYTES] = { 0 };
1262     if (fcpByte == NULL) {
1263         TELEPHONY_LOGE("fcpByte is NULL");
1264         free(fcpByte);
1265         return;
1266     }
1267     if (memset_s(fcpByte, fcpLen, 0, fcpLen) != EOK) {
1268         TELEPHONY_LOGE("ConvertUsimFcpToSimRsp memset_s failed");
1269         free(fcpByte);
1270         return;
1271     }
1272     ConvertHexStringToByteArray(*simResponse, strlen((char *)*simResponse), fcpByte, fcpLen);
1273     if (FcpFileDescriptorQuery(fcpByte, fcpLen, (UsimFileDescriptor *)&fDescriptor) == FALSE) {
1274         TELEPHONY_LOGE("FcpFileDescriptorQuery failed");
1275         free(fcpByte);
1276         return;
1277     }
1278     if (FcpFileIdentifierQuery(fcpByte, fcpLen, (UsimFileIdentifier *)&fId) == FALSE) {
1279         TELEPHONY_LOGE("FcpFileIdentifierQuery failed");
1280         free(fcpByte);
1281         return;
1282     }
1283     if (IsDedicatedFile(fDescriptor.fd)) {
1284         simRspByte[RESPONSE_DATA_FILE_TYPE] = TYPE_DF;
1285         *simResponse = ConvertByteArrayToHexString(simRspByte, fcpLen);
1286         free(fcpByte);
1287         return;
1288     }
1289     CreateSimRspByte(simRspByte, GET_RESPONSE_EF_SIZE_BYTES, &fId, &fDescriptor);
1290     *simResponse = ConvertByteArrayToHexString(simRspByte, GET_RESPONSE_EF_SIZE_BYTES);
1291     free(fcpByte);
1292 }
1293 
CreateSimRspByte(uint8_t simRspByte[],int responseLen,UsimFileIdentifier * fId,UsimFileDescriptor * fDescriptor)1294 void CreateSimRspByte(uint8_t simRspByte[], int responseLen, UsimFileIdentifier *fId, UsimFileDescriptor *fDescriptor)
1295 {
1296     if (responseLen < RESPONSE_DATA_RECORD_LENGTH + 1) {
1297         TELEPHONY_LOGE("simRspByte size error");
1298         return;
1299     }
1300     if (fId == NULL || fDescriptor == NULL) {
1301         TELEPHONY_LOGE("fId or  fDescriptor is null");
1302         return;
1303     }
1304     simRspByte[RESPONSE_DATA_FILE_TYPE] = TYPE_EF;
1305     simRspByte[RESPONSE_DATA_FILE_ID_1] = (fId->fileId & BYTE_NUM_1) >> ADDR_OFFSET_8BIT;
1306     simRspByte[RESPONSE_DATA_FILE_ID_2] = fId->fileId & BYTE_NUM_2;
1307     simRspByte[RESPONSE_DATA_LENGTH] = (GET_RESPONSE_EF_SIZE_BYTES - RESPONSE_DATA_LENGTH - 1);
1308     if (IsLinearFixedFile(fDescriptor->fd)) {
1309         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_LINEAR_FIXED;
1310         simRspByte[RESPONSE_DATA_RECORD_LENGTH] = fDescriptor->recLen;
1311         fDescriptor->dataSize = (fDescriptor->numRec & BYTE_NUM_0) * (fDescriptor->recLen);
1312         simRspByte[RESPONSE_DATA_FILE_SIZE_1] = (fDescriptor->dataSize & BYTE_NUM_1) >> ADDR_OFFSET_8BIT;
1313         simRspByte[RESPONSE_DATA_FILE_SIZE_2] = fDescriptor->dataSize & BYTE_NUM_2;
1314         simRspByte[RESPONSE_DATA_FILE_STATUS] = VALID_FILE_STATUS;
1315         return;
1316     }
1317     if (IsTransparentFile(fDescriptor->fd)) {
1318         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_TRANSPARENT;
1319         return;
1320     }
1321     if (IsCyclicFile(fDescriptor->fd)) {
1322         simRspByte[RESPONSE_DATA_STRUCTURE] = EF_TYPE_CYCLIC;
1323         simRspByte[RESPONSE_DATA_RECORD_LENGTH] = fDescriptor->recLen;
1324         return;
1325     }
1326 }
1327 
FcpTlvSearchTag(uint8_t * dataPtr,uint16_t len,UsimFcpTag tag,uint8_t ** outPtr)1328 uint8_t FcpTlvSearchTag(uint8_t *dataPtr, uint16_t len, UsimFcpTag tag, uint8_t **outPtr)
1329 {
1330     uint8_t tagLen = 0;
1331     uint16_t lenVar = len;
1332     for (*outPtr = dataPtr; lenVar > 0; *outPtr += tagLen) {
1333         tagLen = (*(*outPtr + 1) + HALF_LEN);
1334         if (**outPtr == (uint8_t)tag) {
1335             *outPtr += HALF_LEN;
1336             return *(*outPtr - 1);
1337         }
1338         lenVar -= tagLen;
1339     }
1340     *outPtr = NULL;
1341     return FALSE;
1342 }
1343 
FcpFileDescriptorQuery(uint8_t * fcpByte,uint16_t fcpLen,UsimFileDescriptor * filledStructPtr)1344 uint8_t FcpFileDescriptorQuery(uint8_t *fcpByte, uint16_t fcpLen, UsimFileDescriptor *filledStructPtr)
1345 {
1346     if (fcpByte == NULL || fcpLen < HALF_LEN + 1) {
1347         TELEPHONY_LOGE("fcpByte size error");
1348         return FALSE;
1349     }
1350     uint8_t valueLen = fcpByte[1];
1351     uint8_t *dataPtr = &fcpByte[HALF_LEN];
1352     if (fcpByte[0] != FCP_TEMP_T) {
1353         TELEPHONY_LOGE("fcpByte data error");
1354         return FALSE;
1355     }
1356     uint8_t resultLen = 0;
1357     uint8_t *outPtr = NULL;
1358     UsimFileDescriptor *queryPtr = filledStructPtr;
1359     resultLen = FcpTlvSearchTag(dataPtr, valueLen, FCP_FILE_DES_T, &outPtr);
1360     if (!((outPtr != NULL) && ((resultLen == HALF_LEN) || (resultLen == FIVE_LEN)))) {
1361         TELEPHONY_LOGE("resultLen value error");
1362         return FALSE;
1363     }
1364     queryPtr->fd = outPtr[0];
1365     queryPtr->dataCoding = outPtr[1];
1366     if (resultLen == FIVE_LEN) {
1367         queryPtr->recLen = (short)((outPtr[HALF_LEN] << ADDR_OFFSET_8BIT) | outPtr[THIRD_INDEX]);
1368         queryPtr->numRec = outPtr[HALF_BYTE_LEN];
1369         return TRUE;
1370     }
1371     queryPtr->recLen = 0;
1372     queryPtr->numRec = 0;
1373     return TRUE;
1374 }
1375 
FcpFileIdentifierQuery(uint8_t * fcpByte,uint16_t fcpLen,UsimFileIdentifier * filledStructPtr)1376 uint8_t FcpFileIdentifierQuery(uint8_t *fcpByte, uint16_t fcpLen, UsimFileIdentifier *filledStructPtr)
1377 {
1378     if (fcpByte == NULL || fcpLen < HALF_LEN + 1) {
1379         TELEPHONY_LOGE("fcpByte size error");
1380         return FALSE;
1381     }
1382     uint8_t valueLen = fcpByte[1];
1383     uint8_t *dataPtr = &fcpByte[HALF_LEN];
1384     if (fcpByte[0] != FCP_TEMP_T) {
1385         TELEPHONY_LOGE("fcpByte data error");
1386         return FALSE;
1387     }
1388     uint8_t resultLen = 0;
1389     uint8_t *outPtr = NULL;
1390     UsimFileIdentifier *queryPtr = (UsimFileIdentifier *)filledStructPtr;
1391     resultLen = FcpTlvSearchTag(dataPtr, valueLen, FCP_FILE_ID_T, &outPtr);
1392     if (outPtr == NULL) {
1393         queryPtr->fileId = 0;
1394         return FALSE;
1395     }
1396     if (resultLen != HALF_LEN) {
1397         TELEPHONY_LOGE("resultLen size error");
1398         return FALSE;
1399     }
1400     queryPtr->fileId = (short)((outPtr[0] << ADDR_OFFSET_8BIT) | outPtr[1]);
1401     return TRUE;
1402 }
1403 
IsCyclicFile(uint8_t fd)1404 uint8_t IsCyclicFile(uint8_t fd)
1405 {
1406     return (0x07 & (fd)) == 0x06;
1407 }
1408 
IsDedicatedFile(uint8_t fd)1409 uint8_t IsDedicatedFile(uint8_t fd)
1410 {
1411     return (0x38 & (fd)) == 0x38;
1412 }
1413 
IsLinearFixedFile(uint8_t fd)1414 uint8_t IsLinearFixedFile(uint8_t fd)
1415 {
1416     return (0x07 & (fd)) == 0x02;
1417 }
1418 
IsTransparentFile(uint8_t fd)1419 uint8_t IsTransparentFile(uint8_t fd)
1420 {
1421     return (0x07 & (fd)) == 0x01;
1422 }
1423 
ConvertHexStringToByteArray(uint8_t * originHexString,int responseLen,uint8_t * byteArray,int fcpLen)1424 void ConvertHexStringToByteArray(uint8_t *originHexString, int responseLen, uint8_t *byteArray, int fcpLen)
1425 {
1426     if (responseLen <= 0 || fcpLen <= 0) {
1427         TELEPHONY_LOGE("originHexString is error, size=%{public}d", responseLen);
1428         return;
1429     }
1430     for (int i = 0; i < fcpLen; i++) {
1431         int hexIndex = i * HALF_LEN;
1432         if (hexIndex + 1 >= responseLen) {
1433             break;
1434         }
1435         byteArray[i] = (ToByte(originHexString[hexIndex]) << HALF_BYTE_LEN) | ToByte(originHexString[hexIndex + 1]);
1436     }
1437 }
1438 
ConvertByteArrayToHexString(uint8_t * byteArray,int byteArrayLen)1439 uint8_t *ConvertByteArrayToHexString(uint8_t *byteArray, int byteArrayLen)
1440 {
1441     uint8_t *buf = malloc(byteArrayLen * HALF_LEN + 1);
1442     if (buf == NULL) {
1443         TELEPHONY_LOGE("buf is NULL");
1444         return NULL;
1445     }
1446     int bufIndex = 0;
1447     const char HEX_DIGITS[HEX_DIGITS_LEN] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
1448         'F' };
1449     for (int i = 0; i < byteArrayLen; i++) {
1450         uint8_t b = byteArray[i];
1451         buf[bufIndex++] = HEX_DIGITS[(b >> HALF_BYTE_LEN) & BYTE_NUM_4];
1452         buf[bufIndex++] = HEX_DIGITS[b & BYTE_NUM_4];
1453     }
1454     buf[bufIndex] = '\0';
1455     return buf;
1456 }
1457 
ToByte(char c)1458 uint8_t ToByte(char c)
1459 {
1460     if (c >= '0' && c <= '9') {
1461         return (c - '0');
1462     }
1463     if (c >= 'A' && c <= 'F') {
1464         return (c - 'A' + DECIMAL_MAX);
1465     }
1466     if (c >= 'a' && c <= 'f') {
1467         return (c - 'a' + DECIMAL_MAX);
1468     }
1469     TELEPHONY_LOGE("ToByte Error: %{public}c", c);
1470     return 0;
1471 }
1472