blob: 5869ee928f79272e1b43c841ee6b0d39d928f68f [file] [log] [blame]
b.liud440f9f2025-04-18 10:44:31 +08001/*-----------------------------------------------------------------------------------------------*/
2/**
3 @file ql_sim.h
4 @brief SIM service API
5*/
6/*-----------------------------------------------------------------------------------------------*/
7
8/*-------------------------------------------------------------------------------------------------
9 Copyright (c) 2024 mobiletek Wireless Solution, Co., Ltd. All Rights Reserved.
10 mobiletek Wireless Solution Proprietary and Confidential.
11-------------------------------------------------------------------------------------------------*/
12
13/*-------------------------------------------------------------------------------------------------
14 EDIT HISTORY
15 This section contains comments describing changes made to the file.
16 Notice that changes are listed in reverse chronological order.
17 $Header: $
18 when who what, where, why
19 -------- --------- -----------------------------------------------------------------
20 20241022 yq.wang Created .
21-------------------------------------------------------------------------------------------------*/
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <sys/epoll.h>
27#include <unistd.h>
28#include <errno.h>
29#include <pthread.h>
30#include <string.h>
31#include <fcntl.h>
32
33#include "ql_sim.h"
34#include "ql_type.h"
35#include "mbtk_type.h"
36#include "mbtk_ril_api.h"
37#include "mbtk_list.h"
38#include "mbtk_log.h"
39
40/*----------------------------------------------DEFINE-------------------------------------------*/
41#define MBTK_ERR_OK 0
42
43#define MBTK_PIN_VALUE_LENGTH 16
44#define MBTK_BUFF_TEMP_SIZE_16 16
45#define MBTK_BUFF_TEMP_SIZE_32 32
46#define MBTK_BUFF_TEMP_SIZE_64 64
47#define MBTK_BUFF_TEMP_SIZE_128 128
48
49#define MBTK_READ_EVENT_SIZE 1
50#define MBTK_WRITE_EVENT_SIZE 1
51
52#define QL_SIM_LOGE LOGE
53#define QL_SIM_LOGD LOGD
54
55#define MBTK_PIN_ENABLE 1
56/*----------------------------------------------DEFINE-------------------------------------------*/
57
58/*----------------------------------------------ENUM-------------------------------------------*/
59typedef enum {
60 QL_SIM_EVENT_THREAD_QUIT = 0,
61 QL_SIM_EVENT_STATE_CHANGE,
62 QL_SIM_EVENT_MAX = 255,
63}ql_sim_event_enum;
64
65/*----------------------------------------------ENUM-------------------------------------------*/
66
67/*----------------------------------------------STRUCT-------------------------------------------*/
68typedef struct {
69 mbtk_ril_handle* handle;
70 int control[2];
71 pthread_t cb_thread_id;
72 ql_sim_card_status_cb_f status_cb;
73 ql_sim_service_error_cb_f server_cb;
74}ql_sim_info_handle_t;
75
76/*----------------------------------------------STRUCT-------------------------------------------*/
77
78/*----------------------------------------------GLOBAL STATIC VARIABLE---------------------------*/
79static ql_sim_info_handle_t* sim_handle = NULL;
80
81/*----------------------------------------------GLOBAL STATIC VARIABLE---------------------------*/
82
83/*----------------------------------------------SIM FUNCTION-------------------------------------*/
l.yange4f94f42025-04-29 18:08:39 -070084
85static bool check_slot_valid(QL_SIM_SLOT_E slot)
86{
87 if (slot != QL_SIM_SLOT_1 && slot != QL_SIM_SLOT_2)
88 {
89 QL_SIM_LOGE("bad slot: %d, slot should be 1 or 2", slot);
90 return false;
91 }
92
93 return true;
94}
95
96static void ql_slot_convert_to_mbtk(QL_SIM_SLOT_E ql_slot,mbtk_sim_type_enum *mbtk_slot)
97{
98 if(ql_slot == QL_SIM_SLOT_1)
99 {
100 *mbtk_slot = MBTK_SIM_1;
101 }
102
103 if(ql_slot == QL_SIM_SLOT_2)
104 {
105 *mbtk_slot = MBTK_SIM_2;
106 }
107 return;
108
109}
110
b.liud440f9f2025-04-18 10:44:31 +0800111static void ql_sim_state_change_cb(const void* data, int data_len)
112{
113 if(data_len != sizeof(mbtk_ril_sim_state_info_t))
114 {
115 QL_SIM_LOGE("[%s] data_len[%d] than sim[%d] fail. ", __func__, data_len, sizeof(mbtk_ril_sim_state_info_t));
116 }
117 else
118 {
119 mbtk_ril_sim_state_info_t *ptr = (mbtk_ril_sim_state_info_t*)data;
120 QL_SIM_LOGD("[%s] state[%d] type[%d].", __func__, ptr->sim_state , ptr->sim_type);
121 ql_sim_event_enum cmd = QL_SIM_EVENT_STATE_CHANGE;
122 if(sim_handle->control[0] >= 0 && ptr->sim_state != MBTK_SIM_STATE_UNKNOWN)
123 {
124 int ret = write(sim_handle->control[0], &cmd, MBTK_WRITE_EVENT_SIZE);
125 if(ret != MBTK_WRITE_EVENT_SIZE)
126 {
127 QL_SIM_LOGE("[%s] write fail.[%d]", __func__, ret);
128 }
129 }
130 }
131}
132
133static void ql_sim_server_change_cb(const void* data, int data_len)
134{
135 if(data) {
136 const uint8 *ptr = (const uint8*)data;
137 mbtk_ril_ser_state_enum state = (mbtk_ril_ser_state_enum)ptr[0];
138 LOGD("ril server state : %d.\n", state);
139 if(sim_handle->server_cb && MBTK_RIL_SER_STATE_EXIT == state) {
140 sim_handle->server_cb(QL_ERR_ABORTED);
141 }
142 }
143}
144
145static int ql_epoll_register(int epoll_fd, int fd)
146{
147 struct epoll_event ev;
148 int ret, flags;
149
150 /* important: make the fd non-blocking */
151 flags = fcntl(fd, F_GETFL);
152 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
153
154 ev.events = EPOLLIN;
155 ev.data.fd = fd;
156
157 do
158 {
159 ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
160 if(ret < 0)
161 {
162 QL_SIM_LOGE("[%s] epoll_ctl fail.[%s]", __func__, strerror(errno));
163 }
164 } while (ret < 0 && errno == EINTR);
165
166 return QL_ERR_OK;
167}
168
169static void* ql_cb_thread(void* arg)
170{
171 ql_sim_info_handle_t* info = (ql_sim_info_handle_t*)arg;
172
173 if(info->control[0] < 0 && info->control[0] < 0)
174 {
175 if(socketpair(AF_LOCAL, SOCK_STREAM, 0, info->control ) < 0)
176 {
177 QL_SIM_LOGE("[%s] control creat fail.", __func__);
178 return NULL;
179 }
180 }
181
182 int ne = 0;
183 int nevents = 0;
184 int ret = 0;
185 struct epoll_event events[3];
186 int control_fd = info->control[1];
187 ql_sim_event_enum cmd = QL_SIM_EVENT_THREAD_QUIT;
188
189 //A maximum of three events can be processed simultaneously
190 int epoll_fd = epoll_create(3);
191 ql_epoll_register(epoll_fd, control_fd);
192
193 QL_SIM_LOGE("[%s] thread start run.", __func__);
194 while(1)
195 {
196 nevents = epoll_wait(epoll_fd, events, 3, -1);
197 if(nevents < 0)
198 {
199 if(errno != EINTR)
200 {
201 QL_SIM_LOGE("[%s] epoll_wait fail.[%s]", __func__, strerror(errno));
202 }
203 continue;
204 }
205
206 for (ne = 0; ne < nevents; ne++)
207 {
208 if((events[ne].events & (EPOLLERR|EPOLLHUP)) != 0)
209 {
210 QL_SIM_LOGE("[%s] EPOLLERR or EPOLLHUP event error.", __func__);
211 break;
212 }
213
214 if ((events[ne].events & EPOLLIN) != 0)
215 {
216 if (events[ne].data.fd == control_fd)
217 {
218 ret = read(control_fd, &cmd, MBTK_READ_EVENT_SIZE);
219 if(ret != MBTK_READ_EVENT_SIZE)
220 {
221 QL_SIM_LOGE("[%s] read fail.[%d]", __func__, ret);
222 }
223 else
224 {
225 switch(cmd)
226 {
227 case QL_SIM_EVENT_THREAD_QUIT:
228 {
229 close(epoll_fd);
230 epoll_fd = -1;
231 if(info->control[0] >= 0)
232 {
233 close(info->control[0]);
234 info->control[0] = -1;
235 }
236 if(info->control[1] >= 0)
237 {
238 close(info->control[1]);
239 info->control[1] = -1;
240 }
241 QL_SIM_LOGD("[%s] thread quit.", __func__);
242 return NULL;
243 }
244 case QL_SIM_EVENT_STATE_CHANGE:
245 {
246 if(info->status_cb)
247 {
248 ql_sim_card_info_t sim_info = {0};
249 ql_sim_get_card_info(QL_SIM_SLOT_1, &sim_info);
250 info->status_cb(QL_SIM_SLOT_1, &sim_info);
l.yange4f94f42025-04-29 18:08:39 -0700251 ql_sim_get_card_info(QL_SIM_SLOT_2, &sim_info);
252 info->status_cb(QL_SIM_SLOT_2, &sim_info);
b.liud440f9f2025-04-18 10:44:31 +0800253 }
l.yange4f94f42025-04-29 18:08:39 -0700254
b.liud440f9f2025-04-18 10:44:31 +0800255 break;
256 }
257 default:
258 {
259 QL_SIM_LOGE("[%s] unknown event.[%d]", __func__, cmd);
260 break;
261 }
262 }
263 }
264 }
265 }
266 }
267 }
268}
269
270static int ql_cb_thread_creat(ql_sim_info_handle_t *info)
271{
272 if(info->cb_thread_id == 0)
273 {
274 pthread_attr_t thread_attr;
275 pthread_attr_init(&thread_attr);
276 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
277 {
278 QL_SIM_LOGE("[%s] pthread_attr_setdetachstate fail.", __func__);
279 return QL_ERR_FAILED;
280 }
281 if(pthread_create(&(info->cb_thread_id), &thread_attr, ql_cb_thread, info))
282 {
283 QL_SIM_LOGE("[%s] pthread_create fail.[%ld]", __func__, info->cb_thread_id);
284 return QL_ERR_FAILED;
285 }
286 QL_SIM_LOGD("[%s] pthread_create success.[%ld]", __func__, info->cb_thread_id);
287 }
288
289 return QL_ERR_OK;
290}
291
292static int ql_cb_thread_free(ql_sim_info_handle_t *info)
293{
294 int ret = 0;
295 if(info->cb_thread_id != 0)
296 {
297 ql_sim_event_enum cmd = QL_SIM_EVENT_THREAD_QUIT;
298 if(info->control[0] >= 0)
299 {
300 ret = write(info->control[0], &cmd, MBTK_WRITE_EVENT_SIZE);
301 if(ret != MBTK_WRITE_EVENT_SIZE)
302 {
303 QL_SIM_LOGE("[%s] write fail.[%d]", __func__, ret);
304 return QL_ERR_FAILED;
305 }
306 }
307 info->cb_thread_id = 0;
308 QL_SIM_LOGE("[%s] pthread quit success.", __func__);
309 }
310
311 return QL_ERR_OK;
312}
313
314/*----------------------------------------------SIM FUNCTION-------------------------------------*/
315
316/*----------------------------------------------SIM API------------------------------------------*/
317
318/*-----------------------------------------------------------------------------------------------*/
319/**
320 @brief Initializes SIM service.
321 @note You must call this function before other functions can be used in this module.
322 @return Whether the SIM service was intialized successfully.
323 @retval QL_ERR_OK successful
324 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
325 @retval Other error code defined by ql_type.h
326 */
327/*-----------------------------------------------------------------------------------------------*/
328int ql_sim_init(void)
329{
330 if(NULL == sim_handle)
331 {
332 sim_handle = (ql_sim_info_handle_t*)malloc(sizeof(ql_sim_info_handle_t));
333 if(NULL == sim_handle)
334 {
335 QL_SIM_LOGE("[%s] sim handle malloc fail.", __func__);
336 return QL_ERR_FAILED;
337 }
338
339 sim_handle->handle = mbtk_ril_open(MBTK_AT_PORT_DEF);
340 if(NULL == sim_handle->handle)
341 {
342 QL_SIM_LOGE("[%s] mbtk handle init fail.", __func__);
343 goto error_1;
344 }
345
l.yange4f94f42025-04-29 18:08:39 -0700346 int ret = mbtk_ds_sim_state_change_cb_reg(MBTK_SIM_1,ql_sim_state_change_cb);
b.liud440f9f2025-04-18 10:44:31 +0800347 if(ret != MBTK_ERR_OK)
348 {
349 QL_SIM_LOGE("[%s] set sim state cb fail.[%d]", __func__, ret);
350 goto error_2;
351 }
352
l.yange4f94f42025-04-29 18:08:39 -0700353 ret = mbtk_ds_sim_state_change_cb_reg(MBTK_SIM_2,ql_sim_state_change_cb);
354 if(ret != MBTK_ERR_OK)
355 {
356 QL_SIM_LOGE("[%s] set sim state cb fail.[%d]", __func__, ret);
357 goto error_2;
358 }
359
b.liud440f9f2025-04-18 10:44:31 +0800360 ret = mbtk_ril_ser_state_change_cb_reg(ql_sim_server_change_cb);
361 if(ret != MBTK_ERR_OK)
362 {
363 QL_SIM_LOGE("[%s] set sim server cb fail.[%d]", __func__, ret);
364 goto error_2;
365 }
366
367 sim_handle->status_cb = NULL;
368 sim_handle->server_cb = NULL;
369 sim_handle->control[0] = -1;
370 sim_handle->control[1] = -1;
371 sim_handle->cb_thread_id = 0;
372 }
373
374 return QL_ERR_OK;
375
376error_2:
377 if(sim_handle->handle)
378 {
379 mbtk_ril_close(MBTK_AT_PORT_DEF);
380 sim_handle->handle = NULL;
381 }
382error_1:
383 if(sim_handle)
384 {
385 free(sim_handle);
386 sim_handle = NULL;
387 }
388 return QL_ERR_FAILED;
389}
390
391/*-----------------------------------------------------------------------------------------------*/
392/**
393 @brief Deinitializes SIM service.
394 @return Whether the SIM service was deintialized successfully.
395 @retval QL_ERR_OK successful
396 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
397 @retval Other error code defined by ql_type.h
398 */
399/*-----------------------------------------------------------------------------------------------*/
400int ql_sim_deinit(void)
401{
402 if(NULL == sim_handle)
403 {
404 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
405 return QL_ERR_NOT_INIT;
406 }
407
408 int ret = 0;
409 sim_handle->server_cb = NULL;
410 sim_handle->status_cb = NULL;
411
412 if(NULL != sim_handle->handle)
413 {
414 ret = mbtk_ril_close(MBTK_AT_PORT_DEF);
415 if(ret != MBTK_ERR_OK)
416 {
417 QL_SIM_LOGE("[%s] mbtk handle deinit fail.[%d]", __func__, ret);
418 return QL_ERR_FAILED;
419 }
420 sim_handle->handle = NULL;
421 }
422
423 ret = ql_cb_thread_free(sim_handle);
424 if(ret != QL_ERR_OK)
425 {
426 QL_SIM_LOGE("[%s] cb thread free deinit fail.", __func__);
427 return QL_ERR_FAILED;
428 }
429
430 free(sim_handle);
431 sim_handle = NULL;
432 return QL_ERR_OK;
433}
434
435/*-----------------------------------------------------------------------------------------------*/
436/**
437 @brief Gets the IMSI (for 3GPP) or IMSI_M (for 3GPP2) from the SIM in ASCII form.
438 @param [in] slot Slot to be used.
439 @note This function is only supported by Identify card in slot 1
440 @param [in] app_type Application type.
441 @note This function is only supported by 3GPP applications
442 @param [out] imsi Buffer to fill IMSI data.
443 @param [in] imsi_len Buffer length.
444 @return Whether the IMSI was successfully obtained.
445 @retval QL_ERR_OK successful
446 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
447 @retval Other error code defined by ql_type.h
448 */
449/*-----------------------------------------------------------------------------------------------*/
450int ql_sim_get_imsi(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type, char *imsi, int imsi_len)
451{
l.yange4f94f42025-04-29 18:08:39 -0700452 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800453 UNUSED(app_type);
454
l.yange4f94f42025-04-29 18:08:39 -0700455 mbtk_sim_type_enum sim_id = -1;
b.liud440f9f2025-04-18 10:44:31 +0800456 if(NULL == sim_handle)
457 {
458 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
459 return QL_ERR_NOT_INIT;
460 }
461
462 if(NULL == imsi)
463 {
464 QL_SIM_LOGE("[%s] imsi is NULL.", __func__);
465 return QL_ERR_INVALID_ARG;
466 }
l.yange4f94f42025-04-29 18:08:39 -0700467
b.liud440f9f2025-04-18 10:44:31 +0800468 if(imsi_len < QL_SIM_IMSI_LENGTH)
469 {
470 QL_SIM_LOGE("[%s] imsi buf is too short.", __func__);
471 return QL_ERR_BUF_OVERFLOW;
472 }
l.yange4f94f42025-04-29 18:08:39 -0700473
474 if (!check_slot_valid(slot))
475 {
476 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
477 return QL_ERR_INVALID_ARG;
478 }
479 ql_slot_convert_to_mbtk(slot,&sim_id);
480
b.liud440f9f2025-04-18 10:44:31 +0800481 char temp_buff[MBTK_BUFF_TEMP_SIZE_32] = {0};
482 memset(imsi, 0x0, imsi_len);
483 memset(temp_buff, 0x0, MBTK_BUFF_TEMP_SIZE_32);
l.yange4f94f42025-04-29 18:08:39 -0700484 int ret = mbtk_ds_imsi_get(sim_handle->handle,sim_id,(void *)temp_buff);
b.liud440f9f2025-04-18 10:44:31 +0800485 if(ret != MBTK_ERR_OK)
486 {
487 QL_SIM_LOGE("[%s] imsi get fail.[%d]", __func__, ret);
488 return QL_ERR_FAILED;
489 }
490
491 memcpy(imsi, temp_buff, QL_SIM_IMSI_LENGTH);
492 return QL_ERR_OK;
493}
494
495/*-----------------------------------------------------------------------------------------------*/
496/**
497 @brief Retrieves the Integrated Circuit Card ID (ICCID) stored on the card.
498 @param [in] slot Slot to be used(the parameter currently does not support).
499 @note This function is only supported by Identify card in slot 1
500 @param [out] iccid Buffer to fill ICCID data.
501 @param [in] iccid_len Buffer length.
502 @return Whether the ICCID was successfully obtained.
503 @retval QL_ERR_OK successful
504 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
505 @retval Other error code defined by ql_type.h
506 */
507/*-----------------------------------------------------------------------------------------------*/
508int ql_sim_get_iccid(QL_SIM_SLOT_E slot, char *iccid, int iccid_len)
509{
l.yange4f94f42025-04-29 18:08:39 -0700510 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800511
512 if(NULL == sim_handle)
513 {
514 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
515 return QL_ERR_NOT_INIT;
516 }
517
518 if(NULL == iccid)
519 {
520 QL_SIM_LOGE("[%s] iccid is NULL.", __func__);
521 return QL_ERR_INVALID_ARG;
522 }
523
524 if(iccid_len < QL_SIM_ICCID_LENGTH)
525 {
526 QL_SIM_LOGE("[%s] imsi buf is too short.", __func__);
527 return QL_ERR_BUF_OVERFLOW;
528 }
l.yange4f94f42025-04-29 18:08:39 -0700529 mbtk_sim_type_enum sim_id = -1;
530 if(!check_slot_valid(slot))
531 {
532 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
533 return QL_ERR_INVALID_ARG;
534 }
535 ql_slot_convert_to_mbtk(slot,&sim_id);
b.liud440f9f2025-04-18 10:44:31 +0800536 char temp_buff[MBTK_BUFF_TEMP_SIZE_32] = {0};
537 memset(iccid, 0x0, iccid_len);
538 memset(temp_buff, 0x0, MBTK_BUFF_TEMP_SIZE_32);
l.yange4f94f42025-04-29 18:08:39 -0700539 int ret = mbtk_ds_iccid_get(sim_handle->handle,sim_id, (void *)temp_buff);
b.liud440f9f2025-04-18 10:44:31 +0800540 if(ret != MBTK_ERR_OK)
541 {
542 QL_SIM_LOGE("[%s] iccid get fail.[%d]", __func__, ret);
543 return QL_ERR_FAILED;
544 }
545
546 memcpy(iccid, temp_buff, QL_SIM_ICCID_LENGTH);
547 return QL_ERR_OK;
548}
549
550/*-----------------------------------------------------------------------------------------------*/
551/**
552 @brief Retrieves the device phone number stored on the card.
553 @param [in] slot Slot to be used.
554 @note This function is only supported by Identify card in slot 1
555 @param [in] app_type Application type.
556 @note This function is only supported by 3GPP applications
557 @param [out] phone_num Buffer to fill phone number.
558 @param [in] phone_num_len Buffer length.
559 @return Whether the phone number was successfully retrieved.
560 @retval QL_ERR_OK successful
561 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
562 @retval Other error code defined by ql_type.h
563 */
564/*-----------------------------------------------------------------------------------------------*/
565int ql_sim_get_phone_num(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
566 char *phone_num, int phone_num_len)
567{
l.yange4f94f42025-04-29 18:08:39 -0700568 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800569 UNUSED(app_type);
570
571 if(NULL == sim_handle)
572 {
573 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
574 return QL_ERR_NOT_INIT;
575 }
576
577 if(NULL == phone_num)
578 {
579 QL_SIM_LOGE("[%s] phone_num is NULL.", __func__);
580 return QL_ERR_INVALID_ARG;
581 }
582
583 if(phone_num_len < QL_SIM_PHONE_NUMBER_MAX)
584 {
585 QL_SIM_LOGE("[%s] phone_num buf is too short.", __func__);
586 return QL_ERR_BUF_OVERFLOW;
587 }
588
l.yange4f94f42025-04-29 18:08:39 -0700589 mbtk_sim_type_enum sim_id = -1;
590 if(!check_slot_valid(slot))
591 {
592 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
593 return QL_ERR_INVALID_ARG;
594 }
595 ql_slot_convert_to_mbtk(slot,&sim_id);
596
b.liud440f9f2025-04-18 10:44:31 +0800597 char temp_buff[MBTK_BUFF_TEMP_SIZE_128] = {0};
598 memset(phone_num, 0x0, phone_num_len);
599 memset(temp_buff, 0x0, MBTK_BUFF_TEMP_SIZE_128);
l.yange4f94f42025-04-29 18:08:39 -0700600 int ret = mbtk_ds_phone_number_get(sim_handle->handle,sim_id, (void *)temp_buff);
b.liud440f9f2025-04-18 10:44:31 +0800601 if(ret != MBTK_ERR_OK)
602 {
603 QL_SIM_LOGE("[%s] phone_num get fail.[%d]", __func__, ret);
604 return QL_ERR_FAILED;
605 }
606
607 memcpy(phone_num, temp_buff, QL_SIM_PHONE_NUMBER_MAX);
608 return QL_ERR_OK;
609}
610
611/*-----------------------------------------------------------------------------------------------*/
612/**
613 @brief Retrieves the preferred operators stored on the card.
614 @param [in] slot Slot to be used.
615 @note This function is only supported by Identify card in slot 1
616 @param [out] list Buffer to hold operators.
617 @note This function is only supported by 3GPP applications
618 @return Whether the preferred operators were successfully retrieved.
619 @retval QL_ERR_OK successful
620 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry
621 @retval Other error code defined by ql_type.h
622 */
623/*-----------------------------------------------------------------------------------------------*/
624int ql_sim_get_operators(QL_SIM_SLOT_E slot, ql_sim_operator_list_t *list)
625{
l.yange4f94f42025-04-29 18:08:39 -0700626 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800627
628 if(NULL == sim_handle)
629 {
630 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
631 return QL_ERR_NOT_INIT;
632 }
633
634 if(NULL == list)
635 {
636 QL_SIM_LOGE("[%s] list is NULL.", __func__);
637 return QL_ERR_INVALID_ARG;
638 }
l.yange4f94f42025-04-29 18:08:39 -0700639 mbtk_sim_type_enum sim_id = -1;
640 if(!check_slot_valid(slot))
641 {
642 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
643 return QL_ERR_INVALID_ARG;
644 }
645 ql_slot_convert_to_mbtk(slot,&sim_id);
b.liud440f9f2025-04-18 10:44:31 +0800646#if 0
647 list_node_t* operators_list = NULL;
648 mbtk_net_info_t* operator = NULL;
649 char temp_buff[MBTK_BUFF_TEMP_SIZE_16] = {0};
650 int operators_size = 0;
651 int ret = mbtk_available_net_get(sim_handle->handle, &operators_list);
652 if(ret != MBTK_ERR_OK)
653 {
654 QL_SIM_LOGE("[%s] available operators get fail.[%d]", __func__, ret);
655 if(NULL != operators_list)
656 {
657 list_free(operators_list);
658 operators_list = NULL;
659 }
660 return QL_ERR_FAILED;
661 }
662
663 if(NULL == operators_list)
664 {
665 QL_SIM_LOGE("[%s] operators list is NULL.", __func__);
666 return QL_ERR_FAILED;
667 }
668 else
669 {
670 list_first(operators_list);
671 memset(list, 0x0, sizeof(ql_sim_operator_list_t));
672 while ((operator = (mbtk_net_info_t*) list_next(operators_list)))
673 {
674 QL_SIM_LOGD("[%s]operators: %d, %d, %d, %d.", __func__, operator->net_sel_mode, operator->net_type, operator->net_state, operator->plmn);
675 memset(temp_buff, 0x0, MBTK_BUFF_TEMP_SIZE_16);
676 ret = sprintf(temp_buff, "%d", operator->plmn);
677 list->operators[operators_size].mnc_len = ret - QL_SIM_MCC_LENGHT;
678 if(ret > QL_SIM_MCC_LENGHT && list->operators[operators_size].mnc_len <= QL_SIM_MNC_MAX)
679 {
680 memcpy(list->operators[operators_size].mcc, temp_buff, QL_SIM_MCC_LENGHT);
681 memcpy(list->operators[operators_size].mnc, temp_buff + QL_SIM_MCC_LENGHT, list->operators[operators_size].mnc_len);
682 }
683 else
684 {
685 QL_SIM_LOGE("[%s] MCC MNC length error.", __func__);
686 list_free(operators_list);
687 operators_list = NULL;
688 return QL_ERR_FAILED;
689 }
690 operators_size++;
691 }
692 list->len = operators_size;
693 }
694 list_free(operators_list);
695 operators_list = NULL;
696#else
697 mbtk_net_info_array_t net_list;
l.yange4f94f42025-04-29 18:08:39 -0700698 mbtk_ril_err_enum err = mbtk_ds_available_net_get(sim_handle->handle,sim_id,&net_list);
b.liud440f9f2025-04-18 10:44:31 +0800699 if(err != MBTK_RIL_ERR_SUCCESS) {
700 LOGE("Error : %d", err);
701 } else {
702 LOGD("Available net number:%d\n", net_list.num);
703 int i = 0;
704 char temp_buff[MBTK_BUFF_TEMP_SIZE_16] = {0};
705 int ret;
706 memset(list, 0x0, sizeof(ql_sim_operator_list_t));
707 while(i < net_list.num) {
708 memset(temp_buff, 0x0, MBTK_BUFF_TEMP_SIZE_16);
709 LOGD("NET : %d,%d,%d,%d\n", net_list.net_info[i].net_sel_mode,
710 net_list.net_info[i].net_type, net_list.net_info[i].net_state,
711 net_list.net_info[i].plmn);
712
713 ret = sprintf(temp_buff, "%d", net_list.net_info[i].plmn);
714 list->operators[i].mnc_len = ret - QL_SIM_MCC_LENGHT;
715 if(ret > QL_SIM_MCC_LENGHT && list->operators[i].mnc_len <= QL_SIM_MNC_MAX)
716 {
717 memcpy(list->operators[i].mcc, temp_buff, QL_SIM_MCC_LENGHT);
718 memcpy(list->operators[i].mnc, temp_buff + QL_SIM_MCC_LENGHT, list->operators[i].mnc_len);
719 }
720 else
721 {
722 QL_SIM_LOGE("[%s] MCC MNC length error.", __func__);
723 return QL_ERR_FAILED;
724 }
725 i++;
726 }
727 list->len = net_list.num;
728 }
729
730#endif
731 return QL_ERR_OK;
732}
733
734/*-----------------------------------------------------------------------------------------------*/
735/**
736 @brief Enables the PIN on an application.
737 @param [in] slot Slot to be used.
738 @note This function is only supported by Identify card in slot 1
739 @param [in] app_type Application type.
740 @note This function is only supported by 3GPP applications
741 @param [in] pin PIN to be used.
742 @note This function is only supported by Level 1 user verification
743 @param [in] pin_value PIN value. NULL terminated.
744 @return Whether the PIN was successfully enabled.
745 @retval QL_ERR_OK successful.
746 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
747 @retval Other error code defined by ql_type.h.
748 */
749/*-----------------------------------------------------------------------------------------------*/
750int ql_sim_enable_pin(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
751 QL_SIM_PIN_E pin, const char *pin_value)
752{
l.yange4f94f42025-04-29 18:08:39 -0700753 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800754 UNUSED(app_type);
755 UNUSED(pin);
756
757 if(NULL == sim_handle)
758 {
759 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
760 return QL_ERR_NOT_INIT;
761 }
762
763 if(NULL == pin_value)
764 {
765 QL_SIM_LOGE("[%s] pin_value is NULL.", __func__);
766 return QL_ERR_INVALID_ARG;
767 }
768
769 int pin_len = strlen(pin_value);
770 if(pin_len > QL_SIM_PIN_MAX)
771 {
772 QL_SIM_LOGE("[%s] pin length is too long.", __func__);
773 return QL_ERR_ARG_TOO_LONG;
774 }
775 else if(pin_len <= 0)
776 {
777 QL_SIM_LOGE("[%s] pin length is too short.", __func__);
778 return QL_ERR_INVALID_ARG;
779 }
780
l.yange4f94f42025-04-29 18:08:39 -0700781 mbtk_sim_type_enum sim_id = -1;
782 if(!check_slot_valid(slot))
783 {
784 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
785 return QL_ERR_INVALID_ARG;
786 }
787 ql_slot_convert_to_mbtk(slot,&sim_id);
b.liud440f9f2025-04-18 10:44:31 +0800788 mbtk_sim_lock_info_t info = {0};
789 info.type = MBTK_SIM_LOCK_TYPE_ENABLE;
790 memcpy(info.pin1, pin_value, pin_len);
791
l.yange4f94f42025-04-29 18:08:39 -0700792 int err = mbtk_ds_sim_lock_set(sim_handle->handle,sim_id, &info);
b.liud440f9f2025-04-18 10:44:31 +0800793 if(err)
794 {
795 QL_SIM_LOGE("[%s] enable pin fail.[%d]", __func__, err);
796 return QL_ERR_FAILED;
797 }
798
799 return QL_ERR_OK;
800}
801
802/*-----------------------------------------------------------------------------------------------*/
803/**
804 @brief Disables the PIN on an application.
805 @param [in] slot Slot to be used.
806 @note This function is only supported by Identify card in slot 1
807 @param [in] app_type Application type.
808 @note This function is only supported by 3GPP applications
809 @param [in] pin PIN to be used.
810 @note This function is only supported by Level 1 user verification
811 @param [in] pin_value PIN value. NULL terminated.
812 @return Whether the PIN was successfully disabled.
813 @retval QL_ERR_OK successful.
814 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
815 @retval Other error code defined by ql_type.h.
816 */
817/*-----------------------------------------------------------------------------------------------*/
818int ql_sim_disable_pin(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
819 QL_SIM_PIN_E pin, const char *pin_value)
820{
l.yange4f94f42025-04-29 18:08:39 -0700821 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800822 UNUSED(app_type);
823 UNUSED(pin);
824
825 if(NULL == sim_handle)
826 {
827 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
828 return QL_ERR_NOT_INIT;
829 }
830
831 if(NULL == pin_value)
832 {
833 QL_SIM_LOGE("[%s] pin_value is NULL.", __func__);
834 return QL_ERR_INVALID_ARG;
835 }
836
837 int pin_len = strlen(pin_value);
838 if(pin_len > QL_SIM_PIN_MAX)
839 {
840 QL_SIM_LOGE("[%s] pin length is too long.", __func__);
841 return QL_ERR_ARG_TOO_LONG;
842 }
843 else if(pin_len <= 0)
844 {
845 QL_SIM_LOGE("[%s] pin length is too short.", __func__);
846 return QL_ERR_INVALID_ARG;
847 }
l.yange4f94f42025-04-29 18:08:39 -0700848 mbtk_sim_type_enum sim_id = -1;
849 if(!check_slot_valid(slot))
850 {
851 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
852 return QL_ERR_INVALID_ARG;
853 }
854 ql_slot_convert_to_mbtk(slot,&sim_id);
855
b.liud440f9f2025-04-18 10:44:31 +0800856 mbtk_sim_lock_info_t info = {0};
857 info.type = MBTK_SIM_LOCK_TYPE_DISABLE;
858 memcpy(info.pin1, pin_value, pin_len);
859
l.yange4f94f42025-04-29 18:08:39 -0700860 int err = mbtk_ds_sim_lock_set(sim_handle->handle,sim_id, &info);
b.liud440f9f2025-04-18 10:44:31 +0800861 if(err)
862 {
863 QL_SIM_LOGE("[%s] disenable pin fail.[%d]", __func__, err);
864 return QL_ERR_FAILED;
865 }
866
867 return QL_ERR_OK;
868}
869
870/*-----------------------------------------------------------------------------------------------*/
871/**
872 @brief Verifies the PIN value of an application.
873 @param [in] slot Slot to be used.
874 @note This function is only supported by Identify card in slot 1
875 @param [in] app_type Application type.
876 @note This function is only supported by 3GPP applications
877 @param [in] pin PIN to be used.
878 @note This function is only supported by Level 1 user verification
879 @param [in] pin_value PIN value. NULL terminated.
880 @note PIN must be enabled before calling this function.
881 @return Whether the PIN was successfully verified.
882 @retval QL_ERR_OK successful.
883 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
884 @retval Other error code defined by ql_type.h.
885 */
886/*-----------------------------------------------------------------------------------------------*/
887int ql_sim_verify_pin(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
888 QL_SIM_PIN_E pin, const char *pin_value)
889{
l.yange4f94f42025-04-29 18:08:39 -0700890 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800891 UNUSED(app_type);
892 UNUSED(pin);
893
894 if(NULL == sim_handle)
895 {
896 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
897 return QL_ERR_NOT_INIT;
898 }
899
900 if(NULL == pin_value)
901 {
902 QL_SIM_LOGE("[%s] pin_value is NULL.", __func__);
903 return QL_ERR_INVALID_ARG;
904 }
905
906 int pin_len = strlen(pin_value);
907 if(pin_len > QL_SIM_PIN_MAX)
908 {
909 QL_SIM_LOGE("[%s] pin length is too long.", __func__);
910 return QL_ERR_ARG_TOO_LONG;
911 }
912 else if(pin_len <= 0)
913 {
914 QL_SIM_LOGE("[%s] pin length is too short.", __func__);
915 return QL_ERR_INVALID_ARG;
916 }
l.yange4f94f42025-04-29 18:08:39 -0700917 mbtk_sim_type_enum sim_id = -1;
918 if(!check_slot_valid(slot))
919 {
920 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
921 return QL_ERR_INVALID_ARG;
922 }
923 ql_slot_convert_to_mbtk(slot,&sim_id);
924
b.liud440f9f2025-04-18 10:44:31 +0800925 mbtk_sim_lock_info_t info = {0};
926 info.type = MBTK_SIM_LOCK_TYPE_VERIFY_PIN;
927 memcpy(info.pin1, pin_value, pin_len);
928
l.yange4f94f42025-04-29 18:08:39 -0700929 int err = mbtk_ds_sim_lock_set(sim_handle->handle,sim_id, &info);
b.liud440f9f2025-04-18 10:44:31 +0800930 if(err)
931 {
932 QL_SIM_LOGE("[%s] verify pin fail.[%d]", __func__, err);
933 return QL_ERR_FAILED;
934 }
935
936 return QL_ERR_OK;
937}
938
939/*-----------------------------------------------------------------------------------------------*/
940/**
941 @brief Changes the PIN value of an application.
942 @param [in] slot Slot to be used.
943 @note This function is only supported by Identify card in slot 1
944 @param [in] app_type Application type.
945 @note This function is only supported by 3GPP applications
946 @param [in] pin PIN to be used.
947 @note This function is only supported by Level 1 user verification
948 @param [in] old_pin_value Old PIN value. NULL terminated.
949 @param [in] new_pin_value New PIN value. NULL terminated.
950 @return Whether the PIN was successfully changed.
951 @retval QL_ERR_OK successful.
952 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
953 @retval Other error code defined by ql_type.h.
954 */
955/*-----------------------------------------------------------------------------------------------*/
956int ql_sim_change_pin(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
957 QL_SIM_PIN_E pin, const char *old_pin_value, const char *new_pin_value)
958{
l.yange4f94f42025-04-29 18:08:39 -0700959 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +0800960 UNUSED(app_type);
961 UNUSED(pin);
962
963 if(NULL == sim_handle)
964 {
965 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
966 return QL_ERR_NOT_INIT;
967 }
968
969 if(NULL == old_pin_value || NULL == new_pin_value)
970 {
971 QL_SIM_LOGE("[%s] pin_value is NULL.", __func__);
972 return QL_ERR_INVALID_ARG;
973 }
974
975 int old_pin_len = strlen(old_pin_value);
976 int new_pin_len = strlen(new_pin_value);
977 if(old_pin_len > QL_SIM_PIN_MAX || new_pin_len > QL_SIM_PIN_MAX)
978 {
979 QL_SIM_LOGE("[%s] pin length is too long.", __func__);
980 return QL_ERR_ARG_TOO_LONG;
981 }
982 else if(old_pin_len <= 0 || new_pin_len <= 0)
983 {
984 QL_SIM_LOGE("[%s] pin length is too short.", __func__);
985 return QL_ERR_INVALID_ARG;
986 }
l.yange4f94f42025-04-29 18:08:39 -0700987 mbtk_sim_type_enum sim_id = -1;
988 if(!check_slot_valid(slot))
989 {
990 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
991 return QL_ERR_INVALID_ARG;
992 }
993 ql_slot_convert_to_mbtk(slot,&sim_id);
994
b.liud440f9f2025-04-18 10:44:31 +0800995 mbtk_sim_lock_info_t info = {0};
996 info.type = MBTK_SIM_LOCK_TYPE_CHANGE;
997 memcpy(info.pin1, old_pin_value, old_pin_len);
998 memcpy(info.pin2, new_pin_value, new_pin_len);
999
l.yange4f94f42025-04-29 18:08:39 -07001000 int err = mbtk_ds_sim_lock_set(sim_handle->handle,sim_id, &info);
b.liud440f9f2025-04-18 10:44:31 +08001001 if(err)
1002 {
1003 QL_SIM_LOGE("[%s] change pin fail.[%d]", __func__, err);
1004 return QL_ERR_FAILED;
1005 }
1006
1007 return QL_ERR_OK;
1008}
1009
1010/*-----------------------------------------------------------------------------------------------*/
1011/**
1012 @brief Unblocks a blocked PIN using the PUK code.
1013 @param [in] slot Slot to be used.
1014 @note This function is only supported by Identify card in slot 1
1015 @param [in] app_type Application type.
1016 @note This function is only supported by 3GPP applications
1017 @param [in] pin PIN to be used.
1018 @note This function is only supported by Level 1 user verification
1019 @param [in] puk_value PUK value. NULL terminated.
1020 @param [in] pin_value New PIN value. NULL terminated.
1021 @note The user must pass PUK1 to unblock PIN1 or PUK2 to unblock PIN2.
1022 @return Whether the PIN was successfully unblocked.
1023 @retval QL_ERR_OK successful.
1024 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1025 @retval Other error code defined by ql_type.h.
1026 */
1027/*-----------------------------------------------------------------------------------------------*/
1028int ql_sim_unblock_pin(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
1029 QL_SIM_PIN_E pin, const char *puk_value, const char *pin_value)
1030{
l.yange4f94f42025-04-29 18:08:39 -07001031 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +08001032 UNUSED(app_type);
1033 UNUSED(pin);
1034
1035 if(NULL == sim_handle)
1036 {
1037 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1038 return QL_ERR_NOT_INIT;
1039 }
1040
1041 if(NULL == puk_value || NULL == pin_value)
1042 {
1043 QL_SIM_LOGE("[%s] value is NULL.", __func__);
1044 return QL_ERR_INVALID_ARG;
1045 }
1046
1047 int puk_len = strlen(puk_value);
1048 int pin_len = strlen(pin_value);
1049 if(puk_len > QL_SIM_PUK_LENGTH || pin_len > QL_SIM_PIN_MAX)
1050 {
1051 QL_SIM_LOGE("[%s] length is too long.", __func__);
1052 return QL_ERR_ARG_TOO_LONG;
1053 }
1054 else if(puk_len <= 0 || pin_len <= 0)
1055 {
1056 QL_SIM_LOGE("[%s] length is too short.", __func__);
1057 return QL_ERR_INVALID_ARG;
1058 }
l.yange4f94f42025-04-29 18:08:39 -07001059 mbtk_sim_type_enum sim_id = -1;
1060 if(!check_slot_valid(slot))
1061 {
1062 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
1063 return QL_ERR_INVALID_ARG;
1064 }
1065 ql_slot_convert_to_mbtk(slot,&sim_id);
1066
b.liud440f9f2025-04-18 10:44:31 +08001067 mbtk_sim_lock_info_t info = {0};
1068 info.type = MBTK_SIM_LOCK_TYPE_VERIFY_PUK;
1069 memcpy(info.pin1, pin_value, pin_len);
1070 memcpy(info.puk, puk_value, puk_len);
1071
l.yange4f94f42025-04-29 18:08:39 -07001072 int err = mbtk_ds_sim_lock_set(sim_handle->handle,sim_id, &info);
b.liud440f9f2025-04-18 10:44:31 +08001073 if(err)
1074 {
1075 QL_SIM_LOGE("[%s] unblock pin fail.[%d]", __func__, err);
1076 return QL_ERR_FAILED;
1077 }
1078
1079 return QL_ERR_OK;
1080}
1081
1082/*-----------------------------------------------------------------------------------------------*/
1083/**
1084 @brief Retrieves the card info stored on a card.
1085 @param [in] slot Slot to be used.
1086 @note This function is only supported by Identify card in slot 1
1087 @param [out] p_info Pointer of ql_sim_card_info_t.
1088 @return Whether the card info was successfully retrieved.
1089 @retval QL_ERR_OK successful.
1090 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1091 @retval Other error code defined by ql_type.h.
1092 */
1093/*-----------------------------------------------------------------------------------------------*/
1094int ql_sim_get_card_info(QL_SIM_SLOT_E slot, ql_sim_card_info_t *p_info)
1095{
l.yange4f94f42025-04-29 18:08:39 -07001096 //UNUSED(slot);
b.liud440f9f2025-04-18 10:44:31 +08001097
1098 if(NULL == sim_handle)
1099 {
1100 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1101 return QL_ERR_NOT_INIT;
1102 }
1103
1104 if(NULL == p_info)
1105 {
1106 QL_SIM_LOGE("[%s] p_info is NULL.", __func__);
1107 return QL_ERR_INVALID_ARG;
1108 }
1109
l.yange4f94f42025-04-29 18:08:39 -07001110 mbtk_sim_type_enum sim_id = -1;
1111 if(!check_slot_valid(slot))
1112 {
1113 QL_SIM_LOGE("[%s] check_slot_valid failed.", __func__);
1114 return QL_ERR_INVALID_ARG;
1115 }
1116 ql_slot_convert_to_mbtk(slot,&sim_id);
1117
b.liud440f9f2025-04-18 10:44:31 +08001118 mbtk_sim_state_enum sim;
l.yange4f94f42025-04-29 18:08:39 -07001119 int ret = mbtk_ds_sim_state_get(sim_handle->handle,sim_id, &sim);
b.liud440f9f2025-04-18 10:44:31 +08001120 if(ret != MBTK_ERR_OK)
1121 {
1122 QL_SIM_LOGE("[%s] sim state get fail.[%d]", __func__, ret);
1123 return QL_ERR_FAILED;
1124 }
l.yange4f94f42025-04-29 18:08:39 -07001125
b.liud440f9f2025-04-18 10:44:31 +08001126 mbtk_sim_card_type_enum sim_card_type;
l.yange4f94f42025-04-29 18:08:39 -07001127 ret = mbtk_ds_sim_type_get(sim_handle->handle,sim_id, &sim_card_type);
b.liud440f9f2025-04-18 10:44:31 +08001128 if(ret)
1129 {
1130 QL_SIM_LOGE("[%s] sim type get fail.[%d]", __func__, ret);
1131 return QL_ERR_FAILED;
1132 }
1133
1134 mbtk_pin_puk_last_times_t sim_last_times = {0};
l.yange4f94f42025-04-29 18:08:39 -07001135 ret = mbtk_ds_sim_lock_retry_times_get(sim_handle->handle,sim_id, &sim_last_times);
b.liud440f9f2025-04-18 10:44:31 +08001136 if(ret)
1137 {
1138 QL_SIM_LOGE("[%s] sim pin puk times get fail.[%d]", __func__, ret);
1139 return QL_ERR_FAILED;
1140 }
1141
1142 int pin_state;
l.yange4f94f42025-04-29 18:08:39 -07001143 ret = mbtk_ds_sim_lock_get(sim_handle->handle,sim_id, &pin_state);
b.liud440f9f2025-04-18 10:44:31 +08001144 if(ret)
1145 {
1146 QL_SIM_LOGE("[%s] sim pin state get fail.[%d]", __func__, ret);
1147 return QL_ERR_FAILED;
1148 }
1149
1150 memset(p_info, 0x0, sizeof(ql_sim_card_info_t));
1151 switch(sim)
1152 {
1153 case MBTK_SIM_STATE_ABSENT: //CARD ABSENT
1154 {
1155 p_info->state = QL_SIM_CARD_STATE_ABSENT;
1156 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_UNKNOWN;
1157 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_UNKNOWN;
1158 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1159 break;
1160 }
1161 case MBTK_SIM_STATE_READY: //CARD READY
1162 {
1163 p_info->state = QL_SIM_CARD_STATE_PRESENT;
1164 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_READY;
1165 if(pin_state == MBTK_PIN_ENABLE)
1166 {
1167 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_ENABLED_VERIFIED;
1168 }
1169 else
1170 {
1171 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_DISABLED;
1172 }
1173 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1174 break;
1175 }
1176 case MBTK_SIM_STATE_SIM_PIN: //SIM PIN
1177 {
1178 p_info->state = QL_SIM_CARD_STATE_PRESENT;
1179 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_PIN1_REQ;
1180 if(pin_state == MBTK_PIN_ENABLE)
1181 {
1182 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_ENABLED_NOT_VERIFIED;
1183 }
1184 else
1185 {
1186 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_DISABLED;
1187 }
1188 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1189 break;
1190 }
1191 case MBTK_SIM_STATE_SIM_PUK: //SIM PUK
1192 {
1193 p_info->state = QL_SIM_CARD_STATE_PRESENT;
1194 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_PUK1_REQ;
1195 if(pin_state == MBTK_PIN_ENABLE)
1196 {
1197 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_BLOCKED;
1198 }
1199 else
1200 {
1201 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_DISABLED;
1202 }
1203 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1204 break;
1205 }
1206 case MBTK_SIM_STATE_PH_NET_PIN: //NETWORK PERSON PIN
1207 {
1208 p_info->state = QL_SIM_CARD_STATE_PRESENT;
1209 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_UNKNOWN;
1210 if(pin_state == MBTK_PIN_ENABLE)
1211 {
1212 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_ENABLED_NOT_VERIFIED;
1213 }
1214 else
1215 {
1216 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_DISABLED;
1217 }
1218 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1219 break;
1220 }
1221 case MBTK_SIM_STATE_NOT_READY: //UNKNOWN STATE
1222 default:
1223 {
1224 p_info->state = QL_SIM_CARD_STATE_UNKNOWN;
1225 p_info->app_3gpp.app_state = QL_SIM_APP_STATE_UNKNOWN;
1226 p_info->app_3gpp.pin1_state = QL_SIM_PIN_STATE_UNKNOWN;
1227 p_info->app_3gpp.pin2_state = QL_SIM_PIN_STATE_UNKNOWN;
1228 break;
1229 }
1230 }
1231
1232 switch(sim_card_type)
1233 {
1234 case MBTK_SIM:
1235 case MBTK_TEST_SIM:
1236 {
1237 p_info->type = QL_SIM_CARD_TYPE_ICC;
1238 break;
1239 }
1240 case MBTK_USIM:
1241 case MBTK_TEST_USIM:
1242 {
1243 p_info->type = QL_SIM_CARD_TYPE_UICC;
1244 break;
1245 }
1246 default:
1247 {
1248 p_info->type = QL_SIM_CARD_TYPE_UNKNOWN;
1249 break;
1250 }
1251 }
1252
1253 p_info->app_3gpp.pin1_num_retries = sim_last_times.p1_retry;
1254 p_info->app_3gpp.puk1_num_retries = sim_last_times.puk1_retry;
1255 p_info->app_3gpp.pin2_num_retries = sim_last_times.p2_retry;
1256 p_info->app_3gpp.puk2_num_retries = sim_last_times.puk2_retry;
1257
1258 return QL_ERR_OK;
1259}
1260
1261/*-----------------------------------------------------------------------------------------------*/
1262/**
1263 @brief Reads data from a specific file on a specified application on the card.
1264 @param [in] slot Slot to be used.
1265 @param [in] app_type Application type.
1266 @param [inout] p_file Pointer of ql_sim_file_t.
1267 @return Whether the file was successfully read.
1268 @retval QL_ERR_OK successful.
1269 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1270 @retval Other error code defined by ql_type.h.
1271 */
1272/*-----------------------------------------------------------------------------------------------*/
1273int ql_sim_read_file(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type, ql_sim_file_t *p_file)
1274{
1275 UNUSED(slot);
1276 UNUSED(app_type);
1277 UNUSED(p_file);
1278
1279 if(NULL == sim_handle)
1280 {
1281 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1282 return QL_ERR_NOT_INIT;
1283 }
1284
1285 return QL_ERR_OK;
1286}
1287
1288/*-----------------------------------------------------------------------------------------------*/
1289/**
1290 @brief Writes data to a specific file on a specified application on the card.
1291 @param [in] slot Slot to be used.
1292 @param [in] app_type Application type.
1293 @param [in] p_file Pointer of ql_sim_file_t
1294 @note The type of file is determined by the record number field,
1295 which indicates a transparent file when zero and a record-based file otherwise.
1296 @return Whether the file was successfully written.
1297 @retval QL_ERR_OK successful.
1298 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1299 @retval Other error code defined by ql_type.h.
1300 */
1301/*-----------------------------------------------------------------------------------------------*/
1302int ql_sim_write_file(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type, ql_sim_file_t *p_file)
1303{
1304 UNUSED(slot);
1305 UNUSED(app_type);
1306 UNUSED(p_file);
1307
1308 if(NULL == sim_handle)
1309 {
1310 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1311 return QL_ERR_NOT_INIT;
1312 }
1313
1314 return QL_ERR_OK;
1315}
1316
1317/*-----------------------------------------------------------------------------------------------*/
1318/**
1319 @brief Retrieves the info of a specific file on a specified application on the card.
1320 @param [in] slot Slot to be used.
1321 @param [in] app_type Application type.
1322 @param [inout] p_info Pointer of ql_sim_file_info_t.
1323 @return Whether the file info was successfully retrieved.
1324 @retval QL_ERR_OK successful.
1325 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1326 @retval Other error code defined by ql_type.h.
1327 */
1328/*-----------------------------------------------------------------------------------------------*/
1329int ql_sim_get_file_info(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
1330 ql_sim_file_info_t *p_info)
1331{
1332 UNUSED(slot);
1333 UNUSED(app_type);
1334 UNUSED(p_info);
1335
1336 if(NULL == sim_handle)
1337 {
1338 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1339 return QL_ERR_NOT_INIT;
1340 }
1341
1342 return QL_ERR_OK;
1343}
1344
1345/*-----------------------------------------------------------------------------------------------*/
1346/**
1347 @brief Reads phone book on a specified application on the card.
1348 @param [in] slot Slot to be used.
1349 @param [in] app_type Spplication type.
1350 @param [in] pb_path Phone book path. NULL terminated.
1351 @param [in] record_idx Record index to read. Starts from 1.
1352 @param [out] p_record Pointer of ql_sim_phone_book_record_t.
1353 @return Whether the phone book record was successfully retrieved.
1354 @retval QL_ERR_OK successful.
1355 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1356 @retval Other error code defined by ql_type.h.
1357 */
1358/*-----------------------------------------------------------------------------------------------*/
1359int ql_sim_read_phone_book(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
1360 const char *pb_path, uint8_t record_idx,
1361 ql_sim_phone_book_record_t *p_record)
1362{
1363 UNUSED(slot);
1364 UNUSED(app_type);
1365 UNUSED(pb_path);
1366 UNUSED(record_idx);
1367 UNUSED(p_record);
1368
1369 if(NULL == sim_handle)
1370 {
1371 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1372 return QL_ERR_NOT_INIT;
1373 }
1374
1375 return QL_ERR_OK;
1376}
1377
1378/*-----------------------------------------------------------------------------------------------*/
1379/**
1380 @brief Writes phone book on a specified application on the card.
1381 @param [in] slot Slot to be used.
1382 @param [in] app_type Application type.
1383 @param [in] pb_path Phone book path. NULL terminated.
1384 @param [in] record_idx Record index to write. Starts from 1.
1385 @param [in] p_record Pointer of ql_sim_phone_book_record_t.
1386 @note If p_record->name[0] = 0 and p_record->number[0] = 0, record will be deleted.
1387 @return Whether the phone book record was successfully saved.
1388 @retval QL_ERR_OK successful.
1389 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1390 @retval Other error code defined by ql_type.h.
1391 */
1392/*-----------------------------------------------------------------------------------------------*/
1393int ql_sim_write_phone_book(QL_SIM_SLOT_E slot, QL_SIM_APP_TYPE_E app_type,
1394 const char *pb_path, uint8_t record_idx,
1395 ql_sim_phone_book_record_t *p_record)
1396{
1397 UNUSED(slot);
1398 UNUSED(app_type);
1399 UNUSED(pb_path);
1400 UNUSED(record_idx);
1401 UNUSED(p_record);
1402
1403 if(NULL == sim_handle)
1404 {
1405 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1406 return QL_ERR_NOT_INIT;
1407 }
1408
1409 return QL_ERR_OK;
1410}
1411
1412/*-----------------------------------------------------------------------------------------------*/
1413/**
1414 @brief Opens a logical channel on a UICC card.
1415 @param [in] slot Slot to be used.
1416 @param [out] channel_id Channel opened.
1417 @return Whether the logical channel was successfully opened.
1418 @retval QL_ERR_OK successful.
1419 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1420 @retval Other error code defined by ql_type.h.
1421 */
1422/*-----------------------------------------------------------------------------------------------*/
1423int ql_sim_open_logical_channel(QL_SIM_SLOT_E slot, uint8_t *channel_id)
1424{
1425 UNUSED(slot);
1426 UNUSED(channel_id);
1427
1428 if(NULL == sim_handle)
1429 {
1430 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1431 return QL_ERR_NOT_INIT;
1432 }
1433
1434 return QL_ERR_OK;
1435}
1436
1437/*-----------------------------------------------------------------------------------------------*/
1438/**
1439 @brief Closes a logical channel on a UICC card.
1440 @param [in] slot Slot to be used.
1441 @param [in] channel_id Channel to be closed.
1442 @return Whether the logical channel was successfully closed.
1443 @retval QL_ERR_OK successful.
1444 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1445 @retval Other error code defined by ql_type.h.
1446 */
1447/*-----------------------------------------------------------------------------------------------*/
1448int ql_sim_close_logical_channel(QL_SIM_SLOT_E slot, uint8_t channel_id)
1449{
1450 UNUSED(slot);
1451 UNUSED(channel_id);
1452
1453 if(NULL == sim_handle)
1454 {
1455 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1456 return QL_ERR_NOT_INIT;
1457 }
1458
1459 return QL_ERR_OK;
1460}
1461
1462/*-----------------------------------------------------------------------------------------------*/
1463/**
1464 @brief Sends an APDU to the card.
1465 @param [in] slot Slot to be used.
1466 @param [in] channel_id Channel to be used.
1467 @param [inout] p_apdu Pointer of ql_sim_apdu_t.
1468 @note You must call ql_sim_open_logical_channel before sending an APDU.
1469 @return Whether the APDU was successfully sent.
1470 @retval QL_ERR_OK successful.
1471 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1472 @retval Other error code defined by ql_type.h.
1473 */
1474/*-----------------------------------------------------------------------------------------------*/
1475int ql_sim_send_apdu(QL_SIM_SLOT_E slot, uint8_t channel_id, ql_sim_apdu_t *p_apdu)
1476{
1477 UNUSED(slot);
1478 UNUSED(channel_id);
1479 UNUSED(p_apdu);
1480
1481 if(NULL == sim_handle)
1482 {
1483 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1484 return QL_ERR_NOT_INIT;
1485 }
1486
1487 return QL_ERR_OK;
1488}
1489
1490/*-----------------------------------------------------------------------------------------------*/
1491/**
1492 @brief Sets SIM card status callback handler
1493 @param[in] cb call back handler.
1494 @return Whether the card status callback handler was successfully set.
1495 @retval QL_ERR_OK successful.
1496 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1497 @retval Other error code defined by ql_type.h.
1498 */
1499/*-----------------------------------------------------------------------------------------------*/
1500int ql_sim_set_card_status_cb(ql_sim_card_status_cb_f cb)
1501{
1502 if(NULL == sim_handle)
1503 {
1504 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1505 return QL_ERR_NOT_INIT;
1506 }
1507
1508 int ret = QL_ERR_OK;
1509 if(NULL != cb)
1510 {
1511 ret = ql_cb_thread_creat(sim_handle);
1512 if(ret != QL_ERR_OK)
1513 {
1514 QL_SIM_LOGE("[%s] cb thread creat fail.", __func__);
1515 }
1516 }
1517 else
1518 {
1519 ret = ql_cb_thread_free(sim_handle);
1520 if(ret != QL_ERR_OK)
1521 {
1522 QL_SIM_LOGE("[%s] cb thread free deinit fail.", __func__);
1523 return QL_ERR_FAILED;
1524 }
1525 }
1526 sim_handle->status_cb = cb;
1527
1528 return QL_ERR_OK;
1529}
1530
1531/*-----------------------------------------------------------------------------------------------*/
1532/**
1533 @brief Switches slot.
1534 @param [in] log_slot Logical slot to be switched.
1535 @param [in] phy_slot Physical slot to be switched.
1536 @return Whether the slot was successfully switched.
1537 @retval QL_ERR_OK successful.
1538 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1539 @retval Other error code defined by ql_type.h.
1540 */
1541/*-----------------------------------------------------------------------------------------------*/
1542int ql_sim_switch_slot(QL_SIM_SLOT_E log_slot, QL_SIM_PHY_SLOT_E phy_slot)
1543{
1544 UNUSED(log_slot);
1545 UNUSED(phy_slot);
1546
1547 if(NULL == sim_handle)
1548 {
1549 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1550 return QL_ERR_NOT_INIT;
1551 }
1552
1553 return QL_ERR_OK;
1554}
1555
1556/*-----------------------------------------------------------------------------------------------*/
1557/**
1558 @brief Gets active slots.
1559 @param [Out] p_active_slots Active slots.
1560 @return Whether the active slots were successfully obtained.
1561 @retval QL_ERR_OK successful.
1562 @retval QL_ERR_SERVICE_NOT_READY service is not ready, need to retry.
1563 @retval Other error code defined by ql_type.h.
1564 */
1565/*-----------------------------------------------------------------------------------------------*/
1566int ql_sim_get_active_slots(ql_sim_active_slots_t *p_active_slots)
1567{
1568 UNUSED(p_active_slots);
1569
1570 if(NULL == sim_handle)
1571 {
1572 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1573 return QL_ERR_NOT_INIT;
1574 }
1575
1576 return QL_ERR_OK;
1577}
1578
1579/*-----------------------------------------------------------------------------------------------*/
1580/**
1581 @brief Registration server error callback. Currently, only if the server exits abnormally,
1582 the callback function will be executed, and the error code is QL_ERR_ABORTED;
1583 @param[in] cb Callback function
1584 @return
1585 QL_ERR_OK - successful
1586 Other - error code defined by ql_type.h
1587 */
1588/*-----------------------------------------------------------------------------------------------*/
1589int ql_sim_set_service_error_cb(ql_sim_service_error_cb_f cb)
1590{
1591 if(NULL == sim_handle)
1592 {
1593 QL_SIM_LOGE("[%s] sim handle not init.", __func__);
1594 return QL_ERR_NOT_INIT;
1595 }
1596
1597 sim_handle->server_cb = cb;
1598
1599 return QL_ERR_OK;
1600}
1601
1602//int ql_sim_switch_slot(QL_SIM_SLOT_E log_slot, QL_SIM_PHY_SLOT_E phy_slot);
1603//int ql_sim_get_active_slots(ql_sim_active_slots_t *p_active_slots);
1604/*----------------------------------------------SIM API------------------------------------------*/
1605