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