blob: 4aecb67e65c1399c05eea9f4da04fb283d3c6744 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*=============================================================================
2# FileName: lynq_sim.cpp
3# Desc: about SIM API
4# Author: mobiletek
5# Version: V1.0
6# LastChange: 2020-07-29
7# History:
8=============================================================================*/
9#include <dlfcn.h>
10#include <string.h>
11#include <stdint.h>
12#include <unistd.h>
13#include <fcntl.h>
14#include "lynq_common.h"
15#include "lynq_sim.h"
16#include <stateManager/stateManager.h>
17
18#undef LOG_TAG
19#define LOG_TAG "LYNQ_SIM"
20simInfoLink *simInfoLinkHead=NULL;
21
22
23/*Initialize SIM infomation queue for lynq_sim API.*/
24int init_sim()
25{
26 simInfoLinkHead=createSimInfoLink();
27 if(simInfoLinkHead==NULL)
28 {
29 RLOGD("init sim info link head fail!");
30 return -1;
31 }
32 RLOGD("init sim info link head success!");
33 millli_sleep_with_restart(300);//The premise of obtaining imsi is that aid_ptr has been obtained
34 return 0;
35
36}
37simStatus *parceToSimInfo(Parcel &p,int &len)
38{
39 int32_t res;
40 int num=0;
41 simStatus *msg=(simStatus *)malloc(sizeof(simStatus));
42 p.setDataPosition(0);
43 if (p.dataAvail() > 0)
44 {
45 p.readInt32(&res);
46 msg->card_status = (RIL_CardState)res;
47 p.readInt32(&num);
48 len = num;
49 LYDBGLOG("[%s] num is%d\n ",__FUNCTION__,num);
50 for (int i = 0; i < num; i++) //for future.
51 {
52 p.readInt32(&res);
53 msg->card_type = (RIL_AppType)res;
54 p.readInt32(&res);
55 msg->pin_state = (RIL_PinState)res;
56 }
57 return msg;
58 }
59 return NULL;
60}
61int checkParcelSimInfo(simStatus *msg,int32_t token)
62{
63 int time_sim =300;//3 s.
64 enum{RUN,EXIT}state;
65 state=RUN;
66 int length=0;
67 lynqQueue *node=NULL;
68 simStatus *temp=NULL;
69 while(time_sim--)
70 {
71 millli_sleep_with_restart(10);
72 node=LynqQueueHead;
73 if(node==NULL)
74 {
75 LYDBGLOG("[%s][%d][%s] Queue head is NULL, maybe malloc fail!",__FUNCTION__,__LINE__,__FILE__);
76 continue;
77 }
78 node = searchTokeninQueue(token, node);
79 if(node ==NULL)
80 {
81 msg->card_status=-1;
82 msg->card_type=-1;
83 msg->pin_state=-1;
84 msg->base.e=-1;
85 return -1;
86 }
87 if(node->t_Errno!=RIL_E_SUCCESS)
88 {
89 RLOGD("get sim status fail, the error code is %d",node->t_Errno);
90 msg->base.e=node->t_Errno;
91 msg->card_status=-1;
92 msg->card_type=-1;
93 msg->pin_state=-1;
94 return -1;
95 }
96 else
97 {
98 temp = parceToSimInfo(node->parcel,length);
99 if(temp ==NULL )
100 {
101 continue;
102 }
103 else
104 {
105 msg->card_status=temp->card_status;
106 msg->card_type=temp->card_type;
107 msg->pin_state=temp->pin_state;
108 msg->base.e=node->t_Errno;
109 free(temp);
110 temp=NULL;
111 return 0;
112 }
113 }
114 }
115 printf("time_sim is %d\n",time_sim);
116 msg->base.e=-1;
117 msg->card_status=-1;
118 msg->card_type=-1;
119 msg->pin_state=-1;
120 return -1;
121}
122/*return 0:success ,1:fail*/
123int checkSimInfo(simStatus *msg,int32_t token)
124{
125 int time_sim =300;//3 s.
126 enum{RUN,EXIT}state;
127 state=RUN;
128 printf("check sim Info start\n");
129 simInfoLink *temp;
130 while(time_sim--)
131 {
132 millli_sleep_with_restart(10);
133 temp=simInfoLinkHead;
134 if(temp==NULL)
135 {
136 RLOGD("sim info link head is NULL, maybe malloc fail!");
137 continue;
138 }
139 do
140 {
141 if((temp->simInfoLen!=0)&&(temp->token==token))
142 {
143 if(temp->Error_tok!=RIL_E_SUCCESS)
144 {
145 RLOGD("get sim status fail, the error code is %d",temp->Error_tok);
146 msg->base.e=temp->Error_tok;
147 msg->card_status=-1;
148 msg->card_type=-1;
149 msg->pin_state=-1;
150 return 1;
151 }
152 else
153 {
154 msg->card_status=temp->card_status;
155 msg->card_type=temp->card_type;
156 msg->pin_state=temp->pin_state;
157 msg->base.e=temp->Error_tok;
158 return 0;
159 }
160 }
161 temp=temp->next;
162 }while(temp!=NULL);
163 }
164 printf("time_sim is %d\n",time_sim);
165 msg->card_status=-1;
166 msg->card_type=-1;
167 msg->pin_state=-1;
168 msg->base.e=-1;
169 return 0;
170}
171/*return 0:success ,1:fail*/
172int checkParcelImsiInfo(simImsi *msg,int32_t token)
173{
174 int time_sim =300;//3 s.
175 enum{RUN,EXIT}state;
176 int num;
177 state=RUN;
178 printf("check IMSI Info start\n");
179 lynqQueue *node = NULL;
180 lynqQueue *temp =NULL;
181 while(time_sim--)
182 {
183 millli_sleep_with_restart(10);
184 temp=LynqQueueHead;
185 if(temp==NULL)
186 {
187 RLOGD("sim info link head is NULL, maybe malloc fail!");
188 continue;
189 }
190 node = searchTokeninQueue(token, temp);
191 if(node ==NULL)
192 {
193 memset(msg->imsi,0,sizeof(msg->imsi));
194 msg->base.e=-1;
195 return -1;
196 }
197 if(node->t_Errno!=RIL_E_SUCCESS)
198 {
199 RLOGD("get sim status fail, the error code is %d",node->t_Errno);
200 msg->base.e=node->t_Errno;
201 memset(msg->imsi,0,sizeof(msg->imsi));
202 return -1;
203 }
204 else
205 {
206 node->parcel.setDataPosition(0);
207 if (node->parcel.dataAvail() > 0)
208 {
209
210 node->parcel.readInt32(&num);
211 for(int i =0;i<num;i++)
212 {
213 char * str=lynqStrdupReadString(node->parcel);
214 memcpy(msg->imsi,str,strlen(str)+1);
215 msg->base.e=node->t_Errno;
216 }
217 return 0;
218 }
219 else
220 {
221 continue;
222 }
223 }
224 }
225 printf("time_sim is %d\n",time_sim);
226 msg->base.e=-1;
227 memset(msg->imsi,0,sizeof(msg->imsi));
228 return 0;
229}
230int checkImsiInfo(simImsi *msg,int32_t token)
231{
232 int time_sim =300;//3 s.
233 enum{RUN,EXIT}state;
234 state=RUN;
235 printf("check IMEI Info start\n");
236 simInfoLink *temp;
237 while(time_sim--)
238 {
239 millli_sleep_with_restart(10);
240 temp=simInfoLinkHead;
241 if(temp==NULL)
242 {
243 RLOGD("sim info link head is NULL, maybe malloc fail!");
244 continue;
245 }
246 do
247 {
248 if((temp->simInfoLen!=0)&&(temp->token==token))
249 {
250 if(temp->Error_tok!=RIL_E_SUCCESS)
251 {
252 RLOGD("get sim status fail, the error code is %d",temp->Error_tok);
253 msg->base.e=temp->Error_tok;
254 //msg->imsi = NULL;
255 memset(msg->imsi,0,sizeof(msg->imsi));
256 msg->base.token=token;
257 return 1;
258 }
259 else
260 {
261 msg->base.e=temp->Error_tok;
262 memcpy(msg->imsi,temp->imsi,strlen(temp->imsi)+1);
263 //msg->imsi = temp->imsi;
264 msg->base.token=token;
265 return 0;
266 }
267 }
268 temp=temp->next;
269 }while(temp!=NULL);
270 }
271 memset(msg->imsi,0,sizeof(msg->imsi));
272 //msg->imsi = NULL;
273 msg->base.token=token;
274 msg->base.e=-1;
275 return 0;
276}
277
278/*If you need to use any API under lynq_sim, you mustfirst call the init_sim() function to initialize these functions.*/
279int lynq_get_sim_status(simStatus *msg)
280{
281 int32_t token=0;
282 int length=0;
283 lynqQueue *node = NULL;
284 int timeout = 500; //timeout is 5s.
285 RIL_Errno err = -1;
286 simStatus *temp =NULL;
287 const char requestStr[MAX_LEN] = {"RIL_REQUEST_GET_SIM_STATUS"};
288 if(token = lynqNoneParame(requestStr))
289 {
290 msg->base.token=token;
291 msg->base.request = RIL_REQUEST_GET_SIM_STATUS;
292 node = commonUpdateEstatus(token,timeout,err);
293 msg->base.e = err;
294 if(err != RIL_E_SUCCESS)
295 {
296 LYDBGLOG("[%s] error code is %d!",__FUNCTION__,err);
297 msg->card_status=-1;
298 msg->card_type=-1;
299 msg->pin_state=-1;
300 lynqDeQueue(token);
301 return token;
302 }
303 if(node)
304 {
305 temp = parceToSimInfo(node->parcel,length);
306 if(length ==0)//it means sim card absent
307 {
308 msg->card_status = 0;
309 msg->card_type = 0;
310 msg->pin_state = 0;
311 }
312 else
313 {
314 msg->card_status=temp->card_status;
315 msg->card_type=temp->card_type;
316 msg->pin_state=temp->pin_state;
317 }
318 free(temp);
319 temp = NULL;
320 }
321 lynqDeQueue(token);
322 }
323 return token;
324}
325/*AT> AT+CIMI AT< IMSI*/
326int lynq_get_imsi(simImsi * msg)
327{
328 int32_t token = 0;
329 lynqQueue *node = NULL;
330 int timeout = 500; //timeout is 5s.
331 RIL_Errno err = -1;
332 int num = 0;
333 //memset(msg,0,sizeof(simImsi));
334 const char requestStr[MAX_LEN] = {"RIL_REQUEST_GET_IMSI"};
335 if(token = lynqNoneParame(requestStr))
336 {
337 msg->base.request = RIL_REQUEST_GET_IMSI;
338 msg->base.token = token;
339 node = commonUpdateEstatus(token,timeout,err);
340 msg->base.e = err;
341 if(err != RIL_E_SUCCESS)
342 {
343 LYDBGLOG("[%s] error code is %d!",__FUNCTION__,err);
344 memset(msg->imsi,0,sizeof(msg->imsi));
345 lynqDeQueue(token);
346 return token;
347 }
348 if(node)
349 {
350 node->parcel.setDataPosition(0);
351 if (node->parcel.dataAvail() > 0)
352 {
353 node->parcel.readInt32(&num);
354 for(int i =0;i<num;i++)//for DSDS.
355 {
356 char * str=lynqStrdupReadString(node->parcel);
357 memcpy(msg->imsi,str,strlen(str)+1);
358 }
359 }
360 }
361 lynqDeQueue(token);
362 }
363 return token;
364}
365
366
367