lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /***************************************************************/
|
| 2 | //
|
| 3 | //²Î¼û LPA½Ó¿ÚÎĵµV0.1 SGP.22, ·µ»ØAPDU
|
| 4 | //
|
| 5 | /***************************************************************/
|
| 6 |
|
| 7 | #include <stdio.h>
|
| 8 | #include <stdlib.h>
|
| 9 | #include <string.h>
|
| 10 | #include <ctype.h>
|
| 11 | #include <sys/time.h>
|
| 12 | #include <time.h>
|
| 13 | #include <termios.h>
|
| 14 |
|
| 15 | #include "lpa_inner.h"
|
| 16 |
|
| 17 | #define LPA_TRIGGER_DATA_LEN 512
|
| 18 | #define TRIGGER_HTTP_REQ_LEN 512
|
| 19 | #define TRIGGER_HTTP_RESP_LEN 512
|
| 20 |
|
| 21 | #define HASH_SIZE 16
|
| 22 | #define HASH_HEX_SIZE 33
|
| 23 |
|
| 24 | #define MAX_TIME_LEN 24
|
| 25 |
|
| 26 | #define MAX_APPID_LEN 65
|
| 27 | #define MAX_URL_LEN 129
|
| 28 |
|
| 29 | int g_chan_id = 0; //logical channel
|
| 30 | static int g_open_channel_flag = 0;
|
| 31 |
|
| 32 | extern char curent_iccid[ICCID_LEN+1];
|
| 33 |
|
| 34 | //use nv
|
| 35 | //#define APPID "O8XTcVkZ8L4RDv1V"
|
| 36 | //#define APPSECRET "WNbHQlRV9JKeqpVWSurQWZLXwDs5T9Dl"
|
| 37 | //#define TRIGGER_PORT 61622
|
| 38 | //#define TRIGGER_HOST "serviceplatformt.esim.chinaunicom.cn"
|
| 39 | //#define TRIGGER_EVENT_URL "/esim_uni_plus_server/api/event"
|
| 40 | //#define TRIGGER_UPDATA_URL "/esim_uni_plus_server/api/updata"
|
| 41 |
|
| 42 |
|
| 43 | //´¥·¢·þÎñÆ÷httpÇëÇó
|
| 44 | static char trigger_https_req[] =
|
| 45 | "POST %s HTTP/1.1\r\n"
|
| 46 | "Host: %s\r\n"
|
| 47 | "Content-Type: application/json\r\n"
|
| 48 | "Content-Length: %d\r\n"
|
| 49 | "Accept: */*\r\n"
|
| 50 | "\r\n%s\r\n";
|
| 51 |
|
| 52 | static char trigger_https_event[] =
|
| 53 | "{"
|
| 54 | "\"HEAD\": {"
|
| 55 | "\"APPID\": \"%s\","
|
| 56 | "\"TIME\": \"%s\","
|
| 57 | "\"TRANSID\": \"%s\","
|
| 58 | "\"TOKEN\": \"%s\""
|
| 59 | "},"
|
| 60 | "\"BODY\": {"
|
| 61 | "\"EID\": \"%s\","
|
| 62 | "\"IMEI\": \"%s\""
|
| 63 | "}"
|
| 64 | "}";
|
| 65 |
|
| 66 | static char trigger_https_updata[] =
|
| 67 | "{"
|
| 68 | "\"HEAD\": {"
|
| 69 | "\"APPID\": \"%s\","
|
| 70 | "\"TIME\": \"%s\","
|
| 71 | "\"TRANSID\": \"%s\","
|
| 72 | "\"TOKEN\": \"%s\""
|
| 73 | "},"
|
| 74 | "\"BODY\": {"
|
| 75 | "\"EID\": \"%s\","
|
| 76 | "\"IMEI\": \"%s\","
|
| 77 | "\"EXEC_RESULT\": %d,"
|
| 78 | "\"EXEC_REASON\": \"%s\","
|
| 79 | "\"DATA_INFO\": [%s]"
|
| 80 | "}"
|
| 81 | "}";
|
| 82 |
|
| 83 |
|
| 84 | char g_imei[MAX_IMEI_LEN] = {0};
|
| 85 |
|
| 86 | /***************************************************************/
|
| 87 | //
|
| 88 | //imei
|
| 89 | //
|
| 90 | /***************************************************************/
|
| 91 | static int lpa_get_imei(char *imei, int size)
|
| 92 | {
|
| 93 | int ret = -1;
|
| 94 | char t_imei[MAX_IMEI_LEN] = {0};
|
| 95 | void *p2[] = {t_imei};
|
| 96 |
|
| 97 | sc_cfg_get ("imei", imei, size);
|
| 98 | if (strlen(imei) < 15) {
|
| 99 | ret = get_modem_info("AT+CGSN\r\n", "%s", p2);
|
| 100 | if(ret == 0) {
|
| 101 | snprintf(imei, size, "%s", t_imei);
|
| 102 | }
|
| 103 | return ret;
|
| 104 | }
|
| 105 |
|
| 106 | return 0;
|
| 107 | }
|
| 108 |
|
| 109 | static int lpa_get_iccid(char *iccid, int size)
|
| 110 | {
|
| 111 | int ret = -1;
|
| 112 | char t_iccid[ICCID_LEN+1] = {0};
|
| 113 | void *p2[] = {t_iccid};
|
| 114 |
|
| 115 |
|
| 116 | ret = get_modem_info("AT+ZICCID?\r\n", "%s", p2);
|
| 117 | if(ret == 0) {
|
| 118 | snprintf(iccid, size, "%s", t_iccid);
|
| 119 | }
|
| 120 | return ret;
|
| 121 |
|
| 122 | }
|
| 123 |
|
| 124 | /***************************************************************/
|
| 125 | //
|
| 126 | //time
|
| 127 | //
|
| 128 | /***************************************************************/
|
| 129 | static int lpa_get_time(char *time, int size)
|
| 130 | {
|
| 131 | struct tm ptm = {0};
|
| 132 | struct timeval now_time = {0};
|
| 133 |
|
| 134 | gettimeofday(&now_time, NULL);
|
| 135 | localtime_r(&now_time.tv_sec, &ptm);
|
| 136 |
|
| 137 | snprintf(time, size, "%d-%02d-%02d %02d:%02d:%02d %03d",
|
| 138 | ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, now_time.tv_usec/1000);
|
| 139 |
|
| 140 | //printf("lpa_get_time: %s\n", time);
|
| 141 | return 0;
|
| 142 | }
|
| 143 |
|
| 144 | static int lpa_get_transid(char *transid, int size, char *imei)
|
| 145 | {
|
| 146 | struct tm ptm = {0};
|
| 147 | struct timeval now_time = {0};
|
| 148 |
|
| 149 | gettimeofday(&now_time, NULL);
|
| 150 | localtime_r(&now_time.tv_sec, &ptm);
|
| 151 |
|
| 152 | snprintf(transid, size, "%s%04d%02d%02d%02d%02d%02d%03d",
|
| 153 | imei, ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, now_time.tv_usec/1000);
|
| 154 |
|
| 155 | //printf("lpa_get_transid: %s\n", transid);
|
| 156 | return 0;
|
| 157 | }
|
| 158 |
|
| 159 | //0 fail 1 succ
|
| 160 | static int lpa_get_exeres(char *exe_reason, int len)
|
| 161 | {
|
| 162 | sc_cfg_get("lpa_last_res", exe_reason, len);
|
| 163 | if (0 == strcmp(exe_reason,"success"))
|
| 164 | return 1;
|
| 165 |
|
| 166 | return 0;
|
| 167 | }
|
| 168 |
|
| 169 | /***************************************************************/
|
| 170 | //
|
| 171 | //
|
| 172 | //
|
| 173 | /***************************************************************/
|
| 174 | static char *lpa_get_trigger_data(void)
|
| 175 | {
|
| 176 | int ret = -1;
|
| 177 | //char imei[MAX_IMEI_LEN] = {0};
|
| 178 | char time[MAX_TIME_LEN] = {0};
|
| 179 | char transid[MAX_TRANSID_LEN] = {0};
|
| 180 | char *eid = NULL;
|
| 181 | lpa_MD5_CTX md5ctx = {0};
|
| 182 | unsigned char hash[HASH_SIZE] = {0};
|
| 183 | char token[HASH_HEX_SIZE] = {0};
|
| 184 | char *js_data = NULL;
|
| 185 | char appid[MAX_APPID_LEN] = {0};
|
| 186 | char appsecret[MAX_APPID_LEN] = {0};
|
| 187 |
|
| 188 | sc_cfg_get("lpa_appid", appid, sizeof(appid));
|
| 189 | sc_cfg_get("lpa_appsecret", appsecret, sizeof(appsecret));
|
| 190 |
|
| 191 | lpa_get_time(time, sizeof(time));
|
| 192 | lpa_get_transid(transid, sizeof(transid), g_imei);
|
| 193 |
|
| 194 | lpa_MD5_Init(&md5ctx);
|
| 195 | lpa_MD5_Update(&md5ctx, appid, strlen(appid));
|
| 196 | lpa_MD5_Update(&md5ctx, transid, strlen(transid));
|
| 197 | lpa_MD5_Update(&md5ctx, time, strlen(time));
|
| 198 | lpa_MD5_Update(&md5ctx, appsecret, strlen(appsecret));
|
| 199 | lpa_MD5_Final(hash, &md5ctx);
|
| 200 | //token lower case
|
| 201 | bytes2string_lower(hash, token, sizeof(hash));
|
| 202 |
|
| 203 | eid = GetEID();
|
| 204 | if (eid == NULL) {
|
| 205 | return NULL;
|
| 206 | }
|
| 207 | js_data = malloc(LPA_TRIGGER_DATA_LEN);
|
| 208 | if (js_data != NULL) {
|
| 209 | memset(js_data, 0, LPA_TRIGGER_DATA_LEN);
|
| 210 | snprintf(js_data, LPA_TRIGGER_DATA_LEN, trigger_https_event,
|
| 211 | appid, time, transid, token, eid, g_imei);
|
| 212 | //printf("trigger json:-%s-\n", js_data);
|
| 213 | }
|
| 214 |
|
| 215 | free(eid);
|
| 216 | return js_data;
|
| 217 | }
|
| 218 |
|
| 219 | static char *lpa_get_updata_data(void)
|
| 220 | {
|
| 221 | int ret = -1;
|
| 222 | //char imei[MAX_IMEI_LEN] = {0};
|
| 223 | char time[MAX_TIME_LEN] = {0};
|
| 224 | char transid[MAX_TRANSID_LEN] = {0};
|
| 225 | char *eid = NULL;
|
| 226 | lpa_MD5_CTX md5ctx = {0};
|
| 227 | unsigned char hash[HASH_SIZE] = {0};
|
| 228 | char token[HASH_HEX_SIZE] = {0};
|
| 229 | char *js_data = NULL;
|
| 230 | char exe_reason[200] = {0};
|
| 231 | int exe_res = 0;
|
| 232 | char *pro_list = NULL;
|
| 233 | char appid[MAX_APPID_LEN] = {0};
|
| 234 | char appsecret[MAX_APPID_LEN] = {0};
|
| 235 |
|
| 236 | sc_cfg_get("lpa_appid", appid, sizeof(appid));
|
| 237 | sc_cfg_get("lpa_appsecret", appsecret, sizeof(appsecret));
|
| 238 |
|
| 239 | lpa_get_time(time, sizeof(time));
|
| 240 | lpa_get_transid(transid, sizeof(transid), g_imei);
|
| 241 |
|
| 242 | lpa_MD5_Init(&md5ctx);
|
| 243 | lpa_MD5_Update(&md5ctx, appid, strlen(appid));
|
| 244 | lpa_MD5_Update(&md5ctx, transid, strlen(transid));
|
| 245 | lpa_MD5_Update(&md5ctx, time, strlen(time));
|
| 246 | lpa_MD5_Update(&md5ctx, appsecret, strlen(appsecret));
|
| 247 | lpa_MD5_Final(hash, &md5ctx);
|
| 248 | //token lower case
|
| 249 | bytes2string_lower(hash, token, sizeof(hash));
|
| 250 |
|
| 251 | eid = GetEID();
|
| 252 | if (eid == NULL) {
|
| 253 | return NULL;
|
| 254 | }
|
| 255 |
|
| 256 | exe_res = lpa_get_exeres(exe_reason, sizeof(exe_reason));
|
| 257 |
|
| 258 | getProfileList(&pro_list);
|
| 259 |
|
| 260 | js_data = malloc(LPA_TRIGGER_DATA_LEN);
|
| 261 | if (js_data != NULL) {
|
| 262 | memset(js_data, 0, LPA_TRIGGER_DATA_LEN);
|
| 263 | if (pro_list != NULL) {
|
| 264 | snprintf(js_data, LPA_TRIGGER_DATA_LEN, trigger_https_updata,
|
| 265 | appid, time, transid, token, eid, g_imei, exe_res, exe_reason, pro_list);
|
| 266 | }
|
| 267 | else {
|
| 268 | snprintf(js_data, LPA_TRIGGER_DATA_LEN, trigger_https_updata,
|
| 269 | appid, time, transid, token, eid, g_imei, exe_res, exe_reason, "");
|
| 270 | }
|
| 271 | //printf("trigger json:-%s-\n", js_data);
|
| 272 | }
|
| 273 |
|
| 274 | if (pro_list != NULL)
|
| 275 | free(pro_list);
|
| 276 | free(eid);
|
| 277 | return js_data;
|
| 278 | }
|
| 279 |
|
| 280 | static int lpa_get_profile_state(char *iccid)
|
| 281 | {
|
| 282 | char *apdu = NULL;
|
| 283 | char *state = NULL;
|
| 284 | int status = 0;
|
| 285 |
|
| 286 | apdu = GetProfilesInfo(iccid);
|
| 287 | if (apdu == NULL)
|
| 288 | return -1;
|
| 289 |
|
| 290 |
|
| 291 | state = lpa_tag_apdu_from_atresp(apdu, 0x9F70);
|
| 292 | if (state == NULL) {
|
| 293 | free(apdu);
|
| 294 | return -1;
|
| 295 | }
|
| 296 | errno = 0;
|
| 297 | status = strtol(state, NULL, 16);
|
| 298 | if (errno == ERANGE)// kw ERRNO.NOT_CHECKED
|
| 299 | {
|
| 300 | printf("strtol errno %d: %s\n", errno, strerror(errno));
|
| 301 | }
|
| 302 | free(state);
|
| 303 | free(apdu);
|
| 304 | return status;
|
| 305 | }
|
| 306 |
|
| 307 | //µ±Ç°iccidÊÇ·ñΪ¼¤»î״̬µÄprofile
|
| 308 | static int lpa_get_cur_iccid_state(void)
|
| 309 | {
|
| 310 | char cur_iccid[ICCID_LEN+1] = {0};
|
| 311 |
|
| 312 | lpa_get_iccid(cur_iccid, sizeof(cur_iccid));
|
| 313 | lpa_trans_iccid(cur_iccid, strlen(cur_iccid));
|
| 314 | if (strlen(cur_iccid) == ICCID_LEN) {
|
| 315 | return lpa_get_profile_state(cur_iccid);
|
| 316 | }
|
| 317 |
|
| 318 | return -1;
|
| 319 | }
|
| 320 |
|
| 321 | /***************************************************************/
|
| 322 | //
|
| 323 | //
|
| 324 | //
|
| 325 | /***************************************************************/
|
| 326 | static char *lpa_parse_trigger_resp(char *srcStr, int *next_step)
|
| 327 | {
|
| 328 | cJSON *root = NULL;
|
| 329 | cJSON *object = NULL;
|
| 330 | cJSON *object2 = NULL;
|
| 331 | cJSON *array = NULL;
|
| 332 | char *accode_apdu = NULL;
|
| 333 |
|
| 334 | root = cJSON_Parse(srcStr);
|
| 335 | if (root == NULL) {
|
| 336 | printf("root == NULL\n");
|
| 337 | return NULL;
|
| 338 | }
|
| 339 |
|
| 340 | object = cJSON_GetObjectItem(root, "BODY");
|
| 341 | if (object == NULL) {
|
| 342 | printf("BODY not find\n");
|
| 343 | cJSON_Delete(root);
|
| 344 | return NULL;
|
| 345 | }
|
| 346 |
|
| 347 | object2 = cJSON_GetObjectItem(object, "NEXT_STEP");
|
| 348 | if (object2 == NULL) {
|
| 349 | printf("NEXT_STEP not find\n");
|
| 350 | cJSON_Delete(root);
|
| 351 | return NULL;
|
| 352 | }
|
| 353 | printf("NEXT_STEP:%d\n", object2->valueint);
|
| 354 | *next_step = object2->valueint;
|
| 355 | if (object2->valueint == 2 || object2->valueint == 3
|
| 356 | || object2->valueint == 4) {
|
| 357 | object2 = cJSON_GetObjectItem(object, "ICCID");
|
| 358 | if (object2 == NULL) {
|
| 359 | printf("ICCID not find\n");
|
| 360 | cJSON_Delete(root);
|
| 361 | return NULL;
|
| 362 | }
|
| 363 | printf("ICCID:%s\n", object2->valuestring);
|
| 364 |
|
| 365 | accode_apdu = malloc(APDU_RESP_LEN);
|
| 366 | if (accode_apdu != NULL) {
|
| 367 | memset(accode_apdu, 0, APDU_RESP_LEN);
|
| 368 | strncpy(accode_apdu, object2->valuestring, APDU_RESP_LEN-1);
|
| 369 | lpa_trans_iccid(accode_apdu, strlen(accode_apdu));
|
| 370 | printf("ICCID-Trans:-%s-\n", accode_apdu);
|
| 371 | }
|
| 372 | }
|
| 373 | else if (object2->valueint == 1) {
|
| 374 | object2 = cJSON_GetObjectItem(object, "COMMAND");
|
| 375 | if (object2 == NULL) {
|
| 376 | printf("COMMAND not find\n");
|
| 377 | cJSON_Delete(root);
|
| 378 | return NULL;
|
| 379 | }
|
| 380 | array = cJSON_GetArrayItem(object2, 0);
|
| 381 | if (array == NULL) {
|
| 382 | printf("COMMAND no item\n");
|
| 383 | cJSON_Delete(root);
|
| 384 | return NULL;
|
| 385 | }
|
| 386 | printf("COMMAND:%s\n", array->valuestring);
|
| 387 |
|
| 388 | accode_apdu = malloc(APDU_RESP_LEN);
|
| 389 | if (accode_apdu != NULL) {
|
| 390 | memset(accode_apdu, 0, APDU_RESP_LEN);
|
| 391 | strncpy(accode_apdu, array->valuestring, APDU_RESP_LEN-1);
|
| 392 | printf("accode or apdu:-%s-\n", accode_apdu);
|
| 393 | }
|
| 394 | }
|
| 395 | cJSON_Delete(root);
|
| 396 | return accode_apdu;
|
| 397 | }
|
| 398 |
|
| 399 | static char *lpa_parse_updata_resp(char *srcStr)
|
| 400 | {
|
| 401 | cJSON *root = NULL;
|
| 402 | cJSON *object = NULL;
|
| 403 | cJSON *object2 = NULL;
|
| 404 | cJSON *array = NULL;
|
| 405 | char *code = NULL;
|
| 406 |
|
| 407 | root = cJSON_Parse(srcStr);
|
| 408 | if (root == NULL) {
|
| 409 | printf("root == NULL\n");
|
| 410 | return NULL;
|
| 411 | }
|
| 412 |
|
| 413 | object = cJSON_GetObjectItem(root, "HEAD");
|
| 414 | if (object == NULL) {
|
| 415 | printf("HEAD not find\n");
|
| 416 | cJSON_Delete(root);
|
| 417 | return NULL;
|
| 418 | }
|
| 419 |
|
| 420 | object2 = cJSON_GetObjectItem(object, "CODE");
|
| 421 | if (object2 == NULL) {
|
| 422 | printf("CODE not find\n");
|
| 423 | cJSON_Delete(root);
|
| 424 | return NULL;
|
| 425 | }
|
| 426 | printf("CODE:%s\n", object2->valuestring);
|
| 427 |
|
| 428 | code = malloc(APDU_RESP_LEN);
|
| 429 | if (code != NULL) {
|
| 430 | memset(code, 0, APDU_RESP_LEN);
|
| 431 | strncpy(code, object2->valuestring, APDU_RESP_LEN-1);
|
| 432 | }
|
| 433 | cJSON_Delete(root);
|
| 434 | return code;
|
| 435 | }
|
| 436 |
|
| 437 | static void lpa_open_cfun(void)
|
| 438 | {
|
| 439 | int cfun_stat = -1;
|
| 440 | void *p2[] = {&cfun_stat};
|
| 441 |
|
| 442 | get_modem_info("AT+CFUN?\r\n", "%d", p2);
|
| 443 | if (cfun_stat != 1) {
|
| 444 | get_modem_info("AT+CFUN=0\r\n", NULL, NULL);
|
| 445 | get_modem_info("AT+CFUN=1\r\n", NULL, NULL);
|
| 446 | lpa_close_logical_channel(0);
|
| 447 | }
|
| 448 | }
|
| 449 |
|
| 450 | //wifiʱ·ÀÖ¹ËÑÍøµÄsim¿¨²Ù×÷´ò¶Ïesim·Ö¶Îд¿¨
|
| 451 | static void lpa_close_cfun(void)
|
| 452 | {
|
| 453 | char ps_wan[20] = {0};
|
| 454 | char def_wan[20] = {0};
|
| 455 | int cfun_stat = -1;
|
| 456 | void *p2[] = {&cfun_stat};
|
| 457 |
|
| 458 | sc_cfg_get("pswan", ps_wan, sizeof(ps_wan));
|
| 459 | sc_cfg_get("default_wan_name", def_wan, sizeof(def_wan));
|
| 460 | if (strncmp(def_wan, ps_wan, strlen(ps_wan)) != 0) {
|
| 461 | get_modem_info("AT+CFUN?\r\n", "%d", p2);
|
| 462 | if (cfun_stat == 1) {
|
| 463 | get_modem_info("AT+CFUN=4\r\n", NULL, NULL);
|
| 464 | }
|
| 465 | }
|
| 466 | }
|
| 467 |
|
| 468 | static int lpa_exe_updata(void)
|
| 469 | {
|
| 470 | int code = -1;
|
| 471 | int i = 0;
|
| 472 | char updata_retry[6] = {0};
|
| 473 | int count = 0;
|
| 474 | //dtest
|
| 475 | #if 1
|
| 476 | sc_cfg_get("lpa_updata_retry", updata_retry, sizeof(updata_retry));
|
| 477 | count = atoi(updata_retry);
|
| 478 | if (count <= 0 || count > 10) { //kw 3
|
| 479 | count = 1;
|
| 480 | }
|
| 481 |
|
| 482 | if (0 == lpa_check_status(60)) {
|
| 483 | printf("lpa_exe_updata not ready\n");
|
| 484 | return -1;
|
| 485 | }
|
| 486 |
|
| 487 | for (i = 0; i < count; i++) {
|
| 488 | code = lpa_trigger_updata();
|
| 489 | if (code == 0 || i == count-1)
|
| 490 | break;
|
| 491 | //retry
|
| 492 | sleep(30);
|
| 493 | }
|
| 494 |
|
| 495 | if (code != 0) {
|
| 496 | printf("lpa_exe_updata updata fail\n");
|
| 497 | return -1;
|
| 498 | }
|
| 499 | #endif
|
| 500 | return 0;
|
| 501 | }
|
| 502 | //check sim && net && sntp ready
|
| 503 | int lpa_check_status(int retry)
|
| 504 | {
|
| 505 | int i = 0;
|
| 506 | int res = -1;
|
| 507 |
|
| 508 | //+ZPBIC
|
| 509 | for(i = 0; i < retry; i++) {
|
| 510 | char sntp_res[20] = {0};
|
| 511 | sc_cfg_get("zpbic_pb", sntp_res, sizeof(sntp_res));
|
| 512 | if (strcmp(sntp_res, "ready") == 0) {
|
| 513 | res = 1;
|
| 514 | break;
|
| 515 | }
|
| 516 | printf("lpa zpbic_pb: %s\n", sntp_res);
|
| 517 | sleep(6);
|
| 518 | }
|
| 519 | if (res != 1) {
|
| 520 | printf("lpa sim not ready\n");
|
| 521 | return 0;
|
| 522 | }
|
| 523 |
|
| 524 | //net
|
| 525 | res = -1;
|
| 526 | for(i = 0; i < retry; i++) {
|
| 527 | res = default_route_check();
|
| 528 | if (res == 1)
|
| 529 | break;
|
| 530 | printf("lpa default_route_check: %d\n", res);
|
| 531 | sleep(6);
|
| 532 | }
|
| 533 | if (res != 1) {
|
| 534 | printf("lpa check net timeout\n");
|
| 535 | return 0;
|
| 536 | }
|
| 537 |
|
| 538 | //sntp, no +zmmi
|
| 539 | res = -1;
|
| 540 | for(i = 0; i < retry; i++) {
|
| 541 | char sntp_res[20] = {0};
|
| 542 | sc_cfg_get("sntp_process_result", sntp_res, sizeof(sntp_res));
|
| 543 | if (strcmp(sntp_res, "success") == 0) {
|
| 544 | res = 1;
|
| 545 | break;
|
| 546 | }
|
| 547 | printf("lpa sntp_process_result: %s\n", sntp_res);
|
| 548 | sleep(6);
|
| 549 | }
|
| 550 | if (res != 1) {
|
| 551 | printf("lpa check sntp timeout\n");
|
| 552 | return 0;
|
| 553 | }
|
| 554 |
|
| 555 | //open channel
|
| 556 | res = -1;
|
| 557 | for(i = 0; i < 4; i++) {
|
| 558 | res = lpa_open_logical_channel();
|
| 559 | if (res == 0)
|
| 560 | break;
|
| 561 | printf("lpa lpa_open_logical_channel: %d\n", res);
|
| 562 | sleep(2);
|
| 563 | }
|
| 564 | if (res != 0) {
|
| 565 | printf("lpa open channel fail\n");
|
| 566 | return 0;
|
| 567 | }
|
| 568 |
|
| 569 | ////select app id
|
| 570 | res = -1;
|
| 571 | for(i = 0; i < 4; i++) {
|
| 572 | res = lpa_select_aid();
|
| 573 | if (res == 0)
|
| 574 | break;
|
| 575 | printf("lpa lpa_select_aid: %d\n", res);
|
| 576 | sleep(2);
|
| 577 | }
|
| 578 | if (res != 0) {
|
| 579 | printf("lpa select fail\n");
|
| 580 | return 0;
|
| 581 | }
|
| 582 | return 1;
|
| 583 | }
|
| 584 |
|
| 585 | //²éѯһЩ¹Ì¶¨²ÎÊý
|
| 586 | int lpa_init(void)
|
| 587 | {
|
| 588 | //zpbic && sntp && net ok, if use wlan close modem, open channel select isdr
|
| 589 | if (0 == lpa_check_status(60)) {
|
| 590 | printf("lpa not ready\n");
|
| 591 | return -1;
|
| 592 | }
|
| 593 |
|
| 594 | if(0 != lpa_get_imei(g_imei, sizeof(g_imei))) {
|
| 595 | printf("lpa no imei\n");
|
| 596 | return -1;
|
| 597 | }
|
| 598 |
|
| 599 | return 0;
|
| 600 | }
|
| 601 | int lpa_uninit(void)
|
| 602 | {
|
| 603 | lpa_close_logical_channel(1);
|
| 604 |
|
| 605 | return 0;
|
| 606 | }
|
| 607 |
|
| 608 | static char *lpa_tigger_session(char *path, char *req_data)
|
| 609 | {
|
| 610 | int ret = -1;
|
| 611 | int cont_len = 0;
|
| 612 | https_context_t https_ct = {0};
|
| 613 | char trigger_port[6] = {0};
|
| 614 | char trigger_host[MAX_URL_LEN] = {0};
|
| 615 | char *https_req = NULL;
|
| 616 | char *https_resp = NULL;
|
| 617 | int https_req_len = 0;
|
| 618 | //kw 3
|
| 619 | //if (https_req_len > TRIG_HTTP_MAX_LEN)
|
| 620 | // return NULL;
|
| 621 |
|
| 622 | sc_cfg_get("lpa_trigger_port", trigger_port, sizeof(trigger_port));
|
| 623 | sc_cfg_get("lpa_trigger_host", trigger_host, sizeof(trigger_host));
|
| 624 |
|
| 625 | SSL_library_init();
|
| 626 | https_ct.port = atoi(trigger_port);
|
| 627 | https_ct.host = trigger_host;
|
| 628 | https_ct.path = path;
|
| 629 | ret = https_init(&https_ct);
|
| 630 | if (ret != 0) {
|
| 631 | printf("[trigger] https_init fail\n");
|
| 632 | return NULL;
|
| 633 | }
|
| 634 |
|
| 635 | https_req_len = strlen(trigger_https_req)+strlen(https_ct.path)+strlen(https_ct.host)+strlen(req_data)+32;
|
| 636 | https_req = malloc(https_req_len);
|
| 637 | if (https_req == NULL) {
|
| 638 | https_uninit(&https_ct);
|
| 639 | return NULL;
|
| 640 | }
|
| 641 | memset(https_req, 0, https_req_len);
|
| 642 | ret = snprintf(https_req, https_req_len, trigger_https_req,
|
| 643 | https_ct.path, https_ct.host, strlen(req_data), req_data);
|
| 644 |
|
| 645 | printf("[trigger] request##%s##\n", https_req);
|
| 646 | ret = https_write(&https_ct, https_req, ret);
|
| 647 | printf("[trigger] https_write ret = %d.\n", ret);
|
| 648 |
|
| 649 | ret = https_get_status_code(&https_ct, &cont_len);
|
| 650 | printf("[trigger] https_recv code = %d.\n", ret);
|
| 651 | if(ret == 200 && (cont_len > 0 && cont_len < TRIG_HTTP_MAX_LEN))
|
| 652 | {
|
| 653 | https_resp = malloc(cont_len + 1);
|
| 654 | if (https_resp == NULL) {
|
| 655 | https_uninit(&https_ct);
|
| 656 | free(https_req);
|
| 657 | printf("[trigger] https_resp_cont NULL.\n");
|
| 658 | return NULL;
|
| 659 | }
|
| 660 |
|
| 661 | memset(https_resp, 0, cont_len + 1);
|
| 662 | ret = https_read_content(&https_ct, https_resp, cont_len);
|
| 663 | if(cont_len == ret)
|
| 664 | {
|
| 665 | https_resp[ret] = '\0'; //×Ö·û´®½áÊø±êʶ
|
| 666 | printf("[trigger] https_write https_resp_content = \n %s.\n", https_resp);
|
| 667 | }
|
| 668 | }
|
| 669 |
|
| 670 | https_uninit(&https_ct);
|
| 671 | free(https_req);
|
| 672 | return https_resp;
|
| 673 | }
|
| 674 |
|
| 675 | /***************************************************************/
|
| 676 | //
|
| 677 | //´Ó´¥·¢·þÎñÆ÷²éѯָÁî
|
| 678 | //
|
| 679 | /***************************************************************/
|
| 680 | char *lpa_trigger_event(int *next_step)
|
| 681 | {
|
| 682 | char *trigger_data = NULL;
|
| 683 | char *https_resp_cont = NULL;
|
| 684 | char *command = NULL;
|
| 685 | char trigger_event[MAX_URL_LEN] = {0};
|
| 686 |
|
| 687 | trigger_data = lpa_get_trigger_data();
|
| 688 | if (trigger_data == NULL) {
|
| 689 | printf("[trigger] request data NULL.\n");
|
| 690 | return NULL;
|
| 691 | }
|
| 692 |
|
| 693 | sc_cfg_get("lpa_trigger_event_url", trigger_event, sizeof(trigger_event));
|
| 694 | https_resp_cont = lpa_tigger_session(trigger_event, trigger_data);
|
| 695 | if (https_resp_cont != NULL) {
|
| 696 | command = lpa_parse_trigger_resp(https_resp_cont, next_step);
|
| 697 | free(https_resp_cont);
|
| 698 | }
|
| 699 |
|
| 700 | free(trigger_data);
|
| 701 | return command;
|
| 702 | }
|
| 703 |
|
| 704 | /***************************************************************/
|
| 705 | //
|
| 706 | //Ïò´¥·¢·þÎñÆ÷Éϱ¨Êý¾Ý
|
| 707 | //
|
| 708 | /***************************************************************/
|
| 709 | int lpa_trigger_updata(void)
|
| 710 | {
|
| 711 | char *trigger_data = NULL;
|
| 712 | char *https_resp_cont = NULL;
|
| 713 | char *code = NULL;
|
| 714 | int res_code = -1;
|
| 715 | char trigger_updata[MAX_URL_LEN] = {0};
|
| 716 |
|
| 717 | trigger_data = lpa_get_updata_data();
|
| 718 | if (trigger_data == NULL) {
|
| 719 | printf("[trigger] request data NULL.\n");
|
| 720 | return NULL;
|
| 721 | }
|
| 722 |
|
| 723 | sc_cfg_get("lpa_trigger_updata_url", trigger_updata, sizeof(trigger_updata));
|
| 724 | https_resp_cont = lpa_tigger_session(trigger_updata, trigger_data);
|
| 725 | if (https_resp_cont != NULL) {
|
| 726 | code = lpa_parse_updata_resp(https_resp_cont);
|
| 727 | if (code != NULL) {
|
| 728 | errno = 0;
|
| 729 | res_code = strtol(code, NULL, 10);
|
| 730 | if (errno == ERANGE)// kw ERRNO.NOT_CHECKED
|
| 731 | {
|
| 732 | printf("strtol errno %d: %s\n", errno, strerror(errno));
|
| 733 | }
|
| 734 | free(code);
|
| 735 | }
|
| 736 | free(https_resp_cont);
|
| 737 | }
|
| 738 |
|
| 739 | free(trigger_data);
|
| 740 | return res_code;
|
| 741 | }
|
| 742 |
|
| 743 | //µôµçµÈ³¡¾°£¬Èç´æÔÚiccid¼¤»î²¢Éϱ¨£¬²»´æÔÚÖ±½Ó×ßevent£¬ÖØÐ´¥·¢ÏÂÔØµÈ²Ù×÷
|
| 744 | int lpa_exception_process(void)
|
| 745 | {
|
| 746 | char has_iccid[ICCID_LEN+1] = {0};
|
| 747 | int status = 0;
|
| 748 | int res = 0;
|
| 749 |
|
| 750 | sc_cfg_get("lpa_bpp_iccid", has_iccid, sizeof(has_iccid));
|
| 751 | if (strlen(has_iccid) == ICCID_LEN) {
|
| 752 | status = lpa_get_profile_state(has_iccid);
|
| 753 | if (status == 1 || status == 0) {
|
| 754 | res = lpa_enable_profile(has_iccid);
|
| 755 | if (res == 0) {
|
| 756 | sc_cfg_set("lpa_bpp_iccid", "");
|
| 757 | sc_cfg_save();
|
| 758 | return 0;
|
| 759 | }
|
| 760 | return -1;
|
| 761 | }
|
| 762 | }
|
| 763 |
|
| 764 | return 0;
|
| 765 | }
|
| 766 |
|
| 767 | int lpa_download_profile(char *activecode, char *confirmcode)
|
| 768 | {
|
| 769 | int res = -1;
|
| 770 |
|
| 771 | lpa_close_cfun();
|
| 772 |
|
| 773 | memset(curent_iccid, 0, sizeof(curent_iccid));
|
| 774 | res = downloadProfileByAc(activecode, confirmcode);
|
| 775 |
|
| 776 | //²»¹Ü³É¹¦Óë·ñ£¬¶¼ÒªÉϱ¨
|
| 777 | res = lpa_enable_profile(curent_iccid);
|
| 778 | if (res == 0) {
|
| 779 | sc_cfg_set("lpa_bpp_iccid", "");
|
| 780 | sc_cfg_save();
|
| 781 | }
|
| 782 |
|
| 783 | lpa_open_cfun();
|
| 784 |
|
| 785 | return res;
|
| 786 | }
|
| 787 |
|
| 788 | int lpa_enable_profile(char *iccid)
|
| 789 | {
|
| 790 | int res = 0;
|
| 791 | int code = 0;
|
| 792 |
|
| 793 | res = enableProfile(iccid);
|
| 794 | if (res == 0) {
|
| 795 | sc_cfg_set("zpbic_pb", "");//wait sim ready
|
| 796 | sc_cfg_set("lpa_last_res", "success");
|
| 797 | lpa_close_logical_channel(0);
|
| 798 | sleep(2);
|
| 799 | }
|
| 800 | else if (res == 2) {
|
| 801 | sc_cfg_set("lpa_last_res", "success");
|
| 802 | }
|
| 803 | else {
|
| 804 | sc_cfg_set("lpa_last_res", "fail");
|
| 805 | }
|
| 806 |
|
| 807 | code = lpa_exe_updata();
|
| 808 | if (code != 0) {
|
| 809 | printf("lpa_enable_profile updata fail\n");
|
| 810 | return -1;
|
| 811 | }
|
| 812 | return 0;
|
| 813 | }
|
| 814 |
|
| 815 | int lpa_disable_profile(char *iccid)
|
| 816 | {
|
| 817 | int res = 0;
|
| 818 | int code = 0;
|
| 819 |
|
| 820 | res = disableProfile(iccid);
|
| 821 | if (res == 0) {
|
| 822 | sc_cfg_set("zpbic_pb", "");//wait sim ready
|
| 823 | sc_cfg_set("lpa_last_res", "success");
|
| 824 | lpa_close_logical_channel(0);
|
| 825 | sleep(2);
|
| 826 | }
|
| 827 | else if (res == 2) {
|
| 828 | sc_cfg_set("lpa_last_res", "success");
|
| 829 | }
|
| 830 | else {
|
| 831 | sc_cfg_set("lpa_last_res", "fail");
|
| 832 | }
|
| 833 |
|
| 834 | code = lpa_exe_updata();
|
| 835 | if (code != 0) {
|
| 836 | printf("lpa_disable_profile updata fail\n");
|
| 837 | return -1;
|
| 838 | }
|
| 839 | return 0;
|
| 840 | }
|
| 841 |
|
| 842 | int lpa_delete_profile(char *iccid)
|
| 843 | {
|
| 844 | int res = 0;
|
| 845 | int code = 0;
|
| 846 |
|
| 847 | res = deleteProfile(iccid);
|
| 848 | if (res == 0) {
|
| 849 | sc_cfg_set("lpa_last_res", "success");
|
| 850 | sleep(2);
|
| 851 | }
|
| 852 | else if (res == 1) {
|
| 853 | sc_cfg_set("lpa_last_res", "success");
|
| 854 | }
|
| 855 | else {
|
| 856 | sc_cfg_set("lpa_last_res", "fail");
|
| 857 | }
|
| 858 |
|
| 859 | code = lpa_exe_updata();
|
| 860 | if (code != 0) {
|
| 861 | printf("lpa_disable_profile updata fail\n");
|
| 862 | return -1;
|
| 863 | }
|
| 864 | return 0;
|
| 865 | }
|
| 866 |
|
| 867 | int lpa_memory_reset(void)
|
| 868 | {
|
| 869 | int res = 0;
|
| 870 | int state = 0;
|
| 871 | int code = 0;
|
| 872 |
|
| 873 | state = lpa_get_cur_iccid_state();
|
| 874 | res = memoryReset();
|
| 875 | if (res == 0) {
|
| 876 | if(state == 1) {
|
| 877 | sc_cfg_set("zpbic_pb", "");//wait sim ready
|
| 878 | lpa_close_logical_channel(0);
|
| 879 | printf("lpa zpbic_pb reset\n");
|
| 880 | }
|
| 881 | sc_cfg_set("lpa_last_res", "success");
|
| 882 | sleep(2);//wait sim ready
|
| 883 | }
|
| 884 | else if (res == 1) {
|
| 885 | sc_cfg_set("lpa_last_res", "success");
|
| 886 | }
|
| 887 | else {
|
| 888 | sc_cfg_set("lpa_last_res", "fail");
|
| 889 | }
|
| 890 |
|
| 891 | code = lpa_exe_updata();
|
| 892 | if (code != 0) {
|
| 893 | printf("lpa_disable_profile updata fail\n");
|
| 894 | return -1;
|
| 895 | }
|
| 896 | return 0;
|
| 897 | }
|
| 898 |
|
| 899 | int lpa_open_logical_channel(void)
|
| 900 | {
|
| 901 | char *apdu = NULL;
|
| 902 | int chan = 0;
|
| 903 | if (g_open_channel_flag == 0) {
|
| 904 | apdu = OpenLogicalChannel();
|
| 905 | if (apdu == NULL)
|
| 906 | return -1;
|
| 907 | errno = 0;
|
| 908 | chan = strtol(apdu, NULL, 16);
|
| 909 | if (errno == ERANGE)// kw ERRNO.NOT_CHECKED
|
| 910 | {
|
| 911 | printf("strtol errno %d: %s\n", errno, strerror(errno));
|
| 912 | }
|
| 913 | free(apdu);
|
| 914 | if (chan < 0 || chan > 3)
|
| 915 | return -1;
|
| 916 | g_open_channel_flag = 1;
|
| 917 | g_chan_id = chan;
|
| 918 | }
|
| 919 | return 0;
|
| 920 | }
|
| 921 | //flag = 0, when refresh not need close
|
| 922 | int lpa_close_logical_channel(int flag)
|
| 923 | {
|
| 924 | char *apdu = NULL;
|
| 925 | int res = 0;
|
| 926 | if (flag == 1 && g_open_channel_flag == 1) {
|
| 927 | res = CloseLogicalChannel(g_chan_id);
|
| 928 | printf("lpa_close_logical_channel:%d, res:%d\n", g_chan_id, res);
|
| 929 | }
|
| 930 | g_chan_id = 0;
|
| 931 | g_open_channel_flag = 0;
|
| 932 |
|
| 933 | return res;
|
| 934 | }
|
| 935 |
|