blob: b08f228da2eac9a4d358a2b10046e57d0a7bff75 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include "ql/DSI_ConnectManager.h"
2#include "mbtk_info_api.h"
3
4extern mbtk_info_handle_t* ql_info_handle;
5extern int ql_info_handle_num;
6static bool inited = FALSE;
7
8typedef struct
9{
10 bool active;
11 uint8 cid;
12 int ip_type;
13 char user[128];
14 char pass[128];
15 QL_DSI_AUTH_PREF_T auth;
16} apn_info;
17
18// 2 - 7
19static apn_info ql_apn_info[9]={0};
20static int mbtk_interval_sec = 0;
21static nw_status_cb ql_net_status_cb = NULL;
22static ex_conn_status_cb ql_net_ex_status_cb = NULL;
wangyouqiang65884152023-10-25 19:54:15 +080023static int now_cid = 0;
liubin281ac462023-07-19 14:22:54 +080024
25void ql_wan_net_state_change_cb(const void* data, int data_len)
26{
27 uint8 *net_data = NULL;
28 net_data = (uint8 *)data;
29
wangyouqiang65884152023-10-25 19:54:15 +080030 if(*net_data > 100 && *net_data < 200)
liubin281ac462023-07-19 14:22:54 +080031 {
wangyouqiang65884152023-10-25 19:54:15 +080032 int cid;
33 cid = *net_data;
34 cid = cid - 100;
35 ql_apn_info[cid].active = FALSE;//close
36 if(ql_net_ex_status_cb != NULL)
liubin281ac462023-07-19 14:22:54 +080037 {
wangyouqiang65884152023-10-25 19:54:15 +080038 ql_net_ex_status_cb(cid, ql_apn_info[cid].ip_type, CONNECT_DISCON, 0);
liubin281ac462023-07-19 14:22:54 +080039 }
wangyouqiang65884152023-10-25 19:54:15 +080040 }
41 else if(*net_data > 200)
42 {
43 int cid;
44 cid = *net_data;
45 cid = cid - 200;
46 ql_apn_info[cid].active = TRUE;//open
47 }
48 else
49 {
50 if(ql_net_status_cb != NULL)
liubin281ac462023-07-19 14:22:54 +080051 {
52 ql_net_status_cb(*net_data);
53
54 if(*net_data == 25 || *net_data == 26 || *net_data == 27)
55 {
56 ql_net_status_cb(CONNECT_CONSUCCESS);
57 }
58 }
wangyouqiang65884152023-10-25 19:54:15 +080059
60 if(ql_net_ex_status_cb != NULL)
61 {
62 if(*net_data == 1)
63 {
64 ql_net_ex_status_cb(now_cid, ql_apn_info[now_cid].ip_type, CONNECT_CONSUCCESS, 0);
65 }
66 }
liubin281ac462023-07-19 14:22:54 +080067 }
wangyouqiang65884152023-10-25 19:54:15 +080068
liubin281ac462023-07-19 14:22:54 +080069}
70
71int ql_wan_init(void)
72{
73 if(!inited && ql_info_handle == NULL)
74 {
75 ql_info_handle = mbtk_info_handle_get();
76 if(ql_info_handle)
77 {
78 ql_info_handle_num++;
79 inited = TRUE;
80 mbtk_pdp_state_change_cb_reg(ql_info_handle, ql_wan_net_state_change_cb);
wangyouqiang65884152023-10-25 19:54:15 +080081
wangyouqiangf2c72912023-11-02 19:33:44 +080082 //return 0;
liubin281ac462023-07-19 14:22:54 +080083 } else {
84 LOGE("mbtk_info_handle_get() fail.");
85 return -1;
86 }
87 } else {
88 if(!inited) {
89 ql_info_handle_num++;
90 inited = TRUE;
91 mbtk_pdp_state_change_cb_reg(ql_info_handle, ql_wan_net_state_change_cb);
92 }
wangyouqiangf2c72912023-11-02 19:33:44 +080093 //return 0;
liubin281ac462023-07-19 14:22:54 +080094 }
wangyouqiangf2c72912023-11-02 19:33:44 +080095
96 mbtk_apn_info_t apns[10] = {0};
97 int apn_num = MBTK_APN_CID_MAX;
98 int ret = mbtk_apn_get(ql_info_handle, &apn_num, apns);
99 if(ret != 0)
100 {
101 LOGD("mbtk_apn_get ret = %d",ret);
102 }
103 else
104 {
105 int i;
106 int profile_idx;
107 for(i=0;i<MBTK_APN_CID_MAX;i++)
108 {
109 if(apns[i].cid > 0 && apns[i].cid <= MBTK_APN_CID_MAX)
110 {
111 profile_idx = apns[i].cid;
112 if(apns[i].ip_type == MBTK_IP_TYPE_IPV4V6) // IPV4V6
113 ql_apn_info[profile_idx].ip_type = 0;
114 else if(apns[i].ip_type == MBTK_IP_TYPE_IP) // IPV4
115 ql_apn_info[profile_idx].ip_type = 1;
116 else if(apns[i].ip_type == MBTK_IP_TYPE_IPV6) // IPV6
117 ql_apn_info[profile_idx].ip_type = 2;
118 else
119 ql_apn_info[profile_idx].ip_type = 0;
120 }
121 }
122 }
123 return 0;
liubin281ac462023-07-19 14:22:54 +0800124}
125
126int ql_wan_release(void)
127{
128 if(ql_info_handle)
129 {
130 LOGD("ql_info_handle_num = %d", ql_info_handle_num);
131 if(ql_info_handle_num == 1) { // 最后一个引用,可释放。
132 int ret = mbtk_info_handle_free(&ql_info_handle);
133 if(ret) {
134 LOGE("mbtk_info_handle_free() fail.");
135 return -1;
136 }
137 else
138 {
139 ql_info_handle_num = 0;
140 ql_info_handle = NULL;
141 return 0;
142 }
143 } else {
144 ql_info_handle_num--;
145 return 0;
146 }
147 }
148 else
149 {
150 LOGE("DATA handle not inited.");
151 return -1;
152 }
153}
154/*
155*Set data call over time.
156*/
157int ql_wan_set_autoconnect(int auto_status, int interval_sec)
158{
159 if(auto_status == 0) //不开启失败重连
160 mbtk_interval_sec = 0;
161 else //开启失败重连等待时间
162 {
163 if(interval_sec <= 0)
164 mbtk_interval_sec = interval_sec;
165 else
166 mbtk_interval_sec = 5; // 默认为5s
167 }
168 return 0;
169}
170
171int ql_wan_start_ex(int profile_idx, int op, ex_conn_status_cb nw_cb)
172{
173 int ret = -1;
174 int count = 0;
175 if(ql_info_handle == NULL)
176 {
177 return -1;
178 }
179
180 ql_net_ex_status_cb = nw_cb;
wangyouqiang65884152023-10-25 19:54:15 +0800181 ql_net_status_cb = NULL;
182 now_cid = profile_idx;
liubin281ac462023-07-19 14:22:54 +0800183 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
184 {
185 LOGD("ql_wan_start_ex() cid out of range.");
wangyouqiangf2c72912023-11-02 19:33:44 +0800186#if 0
liubin281ac462023-07-19 14:22:54 +0800187 if(ql_net_ex_status_cb)
wangyouqiangf2c72912023-11-02 19:33:44 +0800188 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type ,PDP_CID_EXIST_FAIL, 0);
liubin281ac462023-07-19 14:22:54 +0800189#endif
190 return -1;
191 }
192
liubin281ac462023-07-19 14:22:54 +0800193 //ql_netw_status_cb(ql_apn_info[profile_idx].cid);
194 if(op == 0)
195 {
196 ret = ql_wan_stop(profile_idx);
197 goto exit;
198 }
199
wangyouqiangf2c72912023-11-02 19:33:44 +0800200#if 0
liubin281ac462023-07-19 14:22:54 +0800201 if(ql_apn_info[profile_idx].active)
202 {
203 if(ql_net_ex_status_cb)
204 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type, PDP_CID_EXIST_FAIL, 0);
205 return -1;
206 }
wangyouqiangf2c72912023-11-02 19:33:44 +0800207#endif
liubin281ac462023-07-19 14:22:54 +0800208
209 if(ql_net_ex_status_cb) {
210 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type, CONNECT_DIAL_IMMEDIATELY, 0);
211 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type, CONNECT_CONNING, 0);
212 }
213
214data_call_continue:
wangyouqiangf2c72912023-11-02 19:33:44 +0800215 ret = mbtk_data_call_start(ql_info_handle, profile_idx, 0, op == 3 ? TRUE : FALSE, 0);
liubin281ac462023-07-19 14:22:54 +0800216 if(ret != 0)
217 {
liubin281ac462023-07-19 14:22:54 +0800218 if(mbtk_interval_sec <= 0)
219 {
220 if(ql_net_ex_status_cb)
221 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type, CONNECT_DISCON, 0);
222 return ret;
223 }
224 else
225 {
226 if(count >= 5)
227 goto exit;
228 sleep(mbtk_interval_sec);
229 count++;
230 if(ql_net_ex_status_cb)
231 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type, CONNECT_REDIAL, 0);
232 goto data_call_continue;
233 }
liubin281ac462023-07-19 14:22:54 +0800234 LOGD("mbtk_data_call_start() fail.");
235 }
236 else
237 {
238 //ql_apn_info[8].cid=profile_idx;
239 //ql_netw_status_cb(CONNECT_CONSUCCESS);
240 LOGD("mbtk_data_call_start() success.");
241 }
242exit:
243 return ret;
244}
245
246
247/*
248* Start data call.
249*/
250int ql_wan_start(int profile_idx, int op, nw_status_cb nw_cb)
251{
252 int ret = -1;
253 int count = 0;
254 if(ql_info_handle == NULL)
255 {
256 return -1;
257 }
258
259 ql_net_status_cb = nw_cb;
wangyouqiang65884152023-10-25 19:54:15 +0800260 ql_net_ex_status_cb = NULL;
liubin281ac462023-07-19 14:22:54 +0800261 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
262 {
263 ql_net_status_cb(PDP_ERROR_UNSPECIFIED);
264 return -1;
265 }
266
267 //ql_netw_status_cb(ql_apn_info[profile_idx].cid);
268 if(op == 0)
269 {
270 ret = ql_wan_stop(profile_idx);
271 goto exit;
272 }
273
274 if(ql_apn_info[profile_idx].active)
275 {
276 ql_net_status_cb(PDP_CID_EXIST_FAIL);
277 return -1;
278 }
279
280 ql_net_status_cb(CONNECT_DIAL_IMMEDIATELY);
281 ql_net_status_cb(CONNECT_CONNING);
282data_call_continue:
wangyouqiangf2c72912023-11-02 19:33:44 +0800283 ret = mbtk_data_call_start(ql_info_handle, profile_idx, 0, op == 3 ? TRUE : FALSE, 0);
liubin281ac462023-07-19 14:22:54 +0800284 if(ret != 0)
285 {
286 if(mbtk_interval_sec <= 0)
287 {
288 ql_net_status_cb(CONNECT_DISCON);
289 return ret;
290 }
291 else
292 {
293 if(count >= 5)
294 goto exit;
295 sleep(mbtk_interval_sec);
296 count++;
297 ql_net_status_cb(CONNECT_REDIAL);
298 goto data_call_continue;
299 }
300 }
301 else
302 {
303 //ql_apn_info[8].cid=profile_idx;
304 //ql_netw_status_cb(CONNECT_CONSUCCESS);
305 LOGD("mbtk_data_call_start() success.");
306 }
307exit:
308 return ret;
309}
310
311/*
312* Stop data call.
313*/
314int ql_wan_stop(int profile_idx)
315{
316 int err;
317 if(ql_info_handle == NULL)
318 {
319 return -1;
320 }
321 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
322 {
323 LOGE("CID error.");
324 return -1;
325 }
326
327#if 0 //mbtk wyq for data_call add "if 0"
328 if(!ql_apn_info[profile_idx].active)
329 {
330 if(ql_net_ex_status_cb)
331 ql_net_ex_status_cb(profile_idx, ql_apn_info[profile_idx].ip_type ,PDP_CID_EXIST_FAIL, 0);
332 if(ql_net_status_cb)
333 ql_net_status_cb(PDP_CID_EXIST_FAIL);
334 return -1;
335 }
336#endif
337 err = mbtk_data_call_stop(ql_info_handle, profile_idx, 15);
338
339 return err;
340}
341
342/*
343* Query data call state.
344*/
345int ql_get_data_call_info(int profile_idx, ql_data_call_info *info)
346{
347 if(ql_info_handle == NULL || info == NULL)
348 {
349 return -1;
350 }
351 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
352 {
353 LOGE("CID error.");
354 return -1;
355 }
356 mbtk_ipv4_info_t ipv4;
357 mbtk_ipv6_info_t ipv6;
358 memset(info, 0, sizeof(ql_data_call_info));
359 int ret = mbtk_data_call_state_get(ql_info_handle, profile_idx, &ipv4, &ipv6);
360 if(ret != 0)
361 return -1;
362 else
363 {
364 info->profile_idx = profile_idx;
365 if(ipv4.valid)
366 {
367 info->ip_type = 1; // IPV4
368 info->v4.state = 1;
369 sprintf(info->v4.addr.name, "ccinet%d", profile_idx - 1);
370 if(inet_ntop(AF_INET, &(ipv4.IPAddr), info->v4.addr.ip, 32) == NULL) {
371 LOGD("IP error.");
372 } else {
373 LOGD("IP : %s", info->v4.addr.ip);
374 }
375 if(inet_ntop(AF_INET, &(ipv4.PrimaryDNS), info->v4.addr.pri_dns, 32) == NULL) {
376 LOGD("PrimaryDNS error.");
377 } else {
378 LOGD("PrimaryDNS : %s", info->v4.addr.pri_dns);
379 }
380 if(inet_ntop(AF_INET, &(ipv4.SecondaryDNS), info->v4.addr.sec_dns, 32) == NULL) {
381 LOGD("SecondaryDNS error.");
382 } else {
383 LOGD("SecondaryDNS : %s", info->v4.addr.sec_dns);
384 }
385 }
386 if(ipv6.valid)
387 {
388 info->ip_type = 2; // IPV6
389 info->v6.state = 1;
390 sprintf(info->v6.addr.name, "ccinet%d", profile_idx - 1);
391 if(ipv6_2_str(&(ipv6.IPV6Addr), info->v6.addr.ip))
392 {
393 LOGD("IP error.");
394 } else {
395 LOGD("IP : %s", info->v6.addr.ip);
396 }
397 if(ipv6_2_str(&(ipv6.PrimaryDNS), info->v6.addr.pri_dns))
398 {
399 LOGD("PrimaryDNS error.");
400 } else {
401 LOGD("PrimaryDNS : %s", info->v6.addr.pri_dns);
402 }
403 if(ipv6_2_str(&(ipv6.SecondaryDNS), info->v6.addr.sec_dns))
404 {
405 LOGD("SecondaryDNS error.");
406 } else {
407 LOGD("SecondaryDNS : %s", info->v6.addr.sec_dns);
408 }
409 }
410 if(ipv4.valid && ipv6.valid)
411 info->ip_type = 0; // IPV4V6
412 if(!ipv4.valid && !ipv6.valid)
413 {
414 info->v4.state = 0;
415 info->v6.state = 0;
416 }
417 return 0;
418 }
419
420}
421
422/*
423* Set specific APN informations.
424*
425* cid : 2-7
426*/
427int ql_wan_setapn(int profile_idx, int ip_type, const char *apn, const char *userName,
428 const char *password, QL_DSI_AUTH_PREF_T auth)
429{
430 if(ql_info_handle == NULL)
431 {
432 return -1;
433 }
434 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
435 {
436 LOGE("CID error.");
437 return -1;
438 }
439 /*
440 QL_DSI_AUTH_PREF_NULL = 0, //无认证
441 QL_DSI_AUTH_PREF_ONLY_PAP, //PAP 认证
442 QL_DSI_AUTH_PREF_ONLY_CHAP, //CHAP 认证
443 QL_DSI_AUTH_PREF_BOTH_PAP_CHAP //PAP 和 CHAP 认证
444 */
445 char mbtk_auth[64]={0};
446 if(auth == QL_DSI_AUTH_PREF_NULL)
447 memcpy(mbtk_auth,"NONE",strlen("NONE")+1);
448 else if(auth == QL_DSI_AUTH_PREF_ONLY_PAP)
449 memcpy(mbtk_auth,"PAP",strlen("PAP")+1);
450 else if(auth == QL_DSI_AUTH_PREF_ONLY_CHAP)
451 memcpy(mbtk_auth,"CHAP",strlen("CHAP")+1);
452 else
453 {
454 LOGD("auth input error!");
455 return -1;
456 }
457
458 memset(&(ql_apn_info[profile_idx]), 0, sizeof(apn_info));
459 ql_apn_info[profile_idx].cid = profile_idx;
460 ql_apn_info[profile_idx].ip_type = ip_type;
461 if(!str_empty(userName) && !str_empty(password)) {
462 memcpy(ql_apn_info[profile_idx].user, userName, strlen(userName));
463 memcpy(ql_apn_info[profile_idx].pass, password, strlen(password));
464 ql_apn_info[profile_idx].auth = auth;
465 LOGD("ql_wan_setapn: %d, %d, %s, %s, %s, %s",profile_idx, ip_type, apn, userName, password, mbtk_auth);
466 } else {
467 LOGD("ql_wan_setapn: %d, %d, %s, NULL, NULL, %s",profile_idx, ip_type, apn, mbtk_auth);
468 }
469
470 /*
471 if(ip_type < 0 || ip_type > 3)
472 ql_netw_status_cb(PDP_UNKNOWN_PDP_ADDRESS_TYPE);
473 */
474 if(ip_type == 0) // IPV4V6
475 ip_type = MBTK_IP_TYPE_IPV4V6;
476 else if(ip_type == 1) // IPV4
477 ip_type = MBTK_IP_TYPE_IP;
478 else if(ip_type == 2) // IPV6
479 ip_type = MBTK_IP_TYPE_IPV6;
480
481 return mbtk_apn_set(ql_info_handle, profile_idx, ip_type, apn, userName, password, mbtk_auth);
482}
483
484/*
485* Get current all APN informations.
486*/
487int __ql_wan_getapn(int profile_idx, int *ip_type, char *apn, int apnLen, char *userName, int userLen, char *password, int pwdLen,int* auth)
488{
489 if(ql_info_handle == NULL || apn == NULL)
490 {
491 return -1;
492 }
493 if(profile_idx < MBTK_APN_CID_MIN || profile_idx > MBTK_APN_CID_MAX)
494 {
495 LOGE("CID error.");
496 return -1;
497 }
498 mbtk_apn_info_t apns[10];
499 int apn_num = MBTK_APN_CID_MAX;
500 int ret = mbtk_apn_get(ql_info_handle, &apn_num, apns);
501 if(ret != 0)
502 {
503 LOGD("mbtk_apn_get ret = %d",ret);
504 return -1;
505 }
506 else
507 {
508 int i;
509 for(i=0;i<MBTK_APN_CID_MAX;i++)
510 {
511 if(apns[i].cid == profile_idx)
512 break;
513 }
514 if(i == MBTK_APN_CID_MAX)
515 return -1;
516 LOGD("cid = %d",i);
517 if(apns[i].ip_type == MBTK_IP_TYPE_IPV4V6) // IPV4V6
518 *ip_type = 0;
519 else if(apns[i].ip_type == MBTK_IP_TYPE_IP) // IPV4
520 *ip_type = 1;
521 else if(apns[i].ip_type == MBTK_IP_TYPE_IPV6) // IPV6
522 *ip_type = 2;
523 else
524 *ip_type = 0;
525
526 if(strlen(apns[i].apn)+1 > apnLen)
527 return -1;
528 else
529 memcpy(apn, apns[i].apn,strlen(apns[i].apn)+1);
530
531 if(strlen(apns[i].user)+1 > userLen)
532 return -1;
533 else
534 {
535 if(strlen(apns[i].user) > 0)
536 memcpy(userName, apns[i].user, strlen(apns[i].user)+1);
537 else
538 memcpy(userName, ql_apn_info[profile_idx].user, strlen(ql_apn_info[profile_idx].user)+1);
539 }
540
541 if(strlen(apns[i].pass)+1 > pwdLen)
542 return -1;
543 else
544 {
545 if(strlen(apns[i].pass) > 0)
546 memcpy(password, apns[i].pass, strlen(apns[i].pass)+1);
547 else
548 memcpy(password, ql_apn_info[profile_idx].pass, strlen(ql_apn_info[profile_idx].pass)+1);
549 }
550
551 if(strlen(apns[i].auth) > 0) {
552 if(strcmp(apns[i].auth, "NONE") == 0)
553 *auth = QL_DSI_AUTH_PREF_NULL;
554 else if(strcmp(apns[i].auth, "PAP") == 0)
555 *auth = QL_DSI_AUTH_PREF_ONLY_PAP;
556 else if(strcmp(apns[i].auth, "CHAP") == 0)
557 *auth = QL_DSI_AUTH_PREF_ONLY_CHAP;
558 else
559 {
560 LOGD("auth error!");
561 return -1;
562 }
563 } else {
564 *auth = ql_apn_info[profile_idx].auth;
565 }
566
567 return 0;
568 }
569}
570
571