rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /*============================================================================= |
| 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" |
| 20 | simInfoLink *simInfoLinkHead=NULL; |
| 21 | |
| 22 | |
| 23 | /*Initialize SIM infomation queue for lynq_sim API.*/ |
| 24 | int 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 | } |
| 37 | simStatus *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 | } |
| 61 | int 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*/ |
| 123 | int 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*/ |
| 172 | int 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 | } |
| 230 | int 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 | int lynq_set_default_sim_all(int sim_id) //sim_id 0 ro 1 |
| 279 | { |
| 280 | const char requestStr[MAX_LEN] = {"SET_DEFAULT_SIM_ALL"}; |
| 281 | return lynqIntParame(requestStr,sim_id); |
| 282 | } |
| 283 | /*If you need to use any API under lynq_sim, you mustfirst call the init_sim() function to initialize these functions.*/ |
| 284 | int lynq_get_sim_status(simStatus *msg) |
| 285 | { |
| 286 | int32_t token=0; |
| 287 | int length=0; |
| 288 | lynqQueue *node = NULL; |
| 289 | int timeout = 500; //timeout is 5s. |
| 290 | RIL_Errno err = -1; |
| 291 | simStatus *temp =NULL; |
| 292 | const char requestStr[MAX_LEN] = {"RIL_REQUEST_GET_SIM_STATUS"}; |
| 293 | if(token = lynqNoneParame(requestStr)) |
| 294 | { |
| 295 | msg->base.token=token; |
| 296 | msg->base.request = RIL_REQUEST_GET_SIM_STATUS; |
| 297 | node = commonUpdateEstatus(token,timeout,err); |
| 298 | msg->base.e = err; |
| 299 | if(err != RIL_E_SUCCESS) |
| 300 | { |
| 301 | LYDBGLOG("[%s] error code is %d!",__FUNCTION__,err); |
| 302 | msg->card_status=-1; |
| 303 | msg->card_type=-1; |
| 304 | msg->pin_state=-1; |
| 305 | lynqDeQueue(token); |
| 306 | return token; |
| 307 | } |
| 308 | if(node) |
| 309 | { |
| 310 | temp = parceToSimInfo(node->parcel,length); |
| 311 | if(length ==0)//it means sim card absent |
| 312 | { |
| 313 | msg->card_status = 0; |
| 314 | msg->card_type = 0; |
| 315 | msg->pin_state = 0; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | msg->card_status=temp->card_status; |
| 320 | msg->card_type=temp->card_type; |
| 321 | msg->pin_state=temp->pin_state; |
| 322 | } |
| 323 | free(temp); |
| 324 | temp = NULL; |
| 325 | } |
| 326 | lynqDeQueue(token); |
| 327 | } |
| 328 | return token; |
| 329 | } |
| 330 | /*AT> AT+CIMI AT< IMSI*/ |
| 331 | int lynq_get_sim_status_ext(int sim_id ) |
| 332 | { |
| 333 | RLOGD("lynq_get_sim_status_ext,sim_id:%d",sim_id); |
| 334 | return getSimState(sim_id); |
| 335 | } |
| 336 | int lynq_get_imsi(simImsi * msg) |
| 337 | { |
| 338 | int32_t token = 0; |
| 339 | lynqQueue *node = NULL; |
| 340 | int timeout = 500; //timeout is 5s. |
| 341 | RIL_Errno err = -1; |
| 342 | int num = 0; |
| 343 | //memset(msg,0,sizeof(simImsi)); |
| 344 | const char requestStr[MAX_LEN] = {"RIL_REQUEST_GET_IMSI"}; |
| 345 | if(token = lynqNoneParame(requestStr)) |
| 346 | { |
| 347 | msg->base.request = RIL_REQUEST_GET_IMSI; |
| 348 | msg->base.token = token; |
| 349 | node = commonUpdateEstatus(token,timeout,err); |
| 350 | msg->base.e = err; |
| 351 | if(err != RIL_E_SUCCESS) |
| 352 | { |
| 353 | LYDBGLOG("[%s] error code is %d!",__FUNCTION__,err); |
| 354 | memset(msg->imsi,0,sizeof(msg->imsi)); |
| 355 | lynqDeQueue(token); |
| 356 | return token; |
| 357 | } |
| 358 | if(node) |
| 359 | { |
| 360 | node->parcel.setDataPosition(0); |
| 361 | if (node->parcel.dataAvail() > 0) |
| 362 | { |
| 363 | node->parcel.readInt32(&num); |
| 364 | for(int i =0;i<num;i++)//for DSDS. |
| 365 | { |
| 366 | char * str=lynqStrdupReadString(node->parcel); |
| 367 | memcpy(msg->imsi,str,strlen(str)+1); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | lynqDeQueue(token); |
| 372 | } |
| 373 | return token; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |