blob: f2195e01aaa3fd2e63d3841a8948793e50945792 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include "lynq/lynq_net_api.h"
2
3static mbtk_info_handle_t* info_handle = NULL;
4
5typedef struct
6{
7 uint8 *operator_l;
8 uint8 *operator_s;
9 uint32 mcc_mnc;
10} operator_mcc_mnc_t;
11
12static operator_mcc_mnc_t operator_mcc_mnc[] =
13{
14 {"China Mobile","CMCC",46000},
15 {"China Unicom","CU",46001},
16 {"China Mobile","CMCC",46002},
17 {"China Telecom","CT",46003},
18 {"China Mobile","CMCC",46004},
19 {"China Telecom","CT",46005},
20 {"China Unicom","CU",46006},
21 {"China Mobile","CMCC",46007},
22 {"China Mobile","CMCC",46008},
23 {"China Unicom","CU",46009},
24 {"China Telecom","CT",46011}
25};
26
27int lynq_network_init(int uToken)
28{
29 UNUSED(uToken);
30 if(info_handle == NULL)
31 {
32 info_handle = mbtk_info_handle_get();
33 if(info_handle)
34 {
35 return 0;
36 }
37 }
38
39 return -1;
40}
41
42int lynq_network_deinit(void)
43{
44 if(info_handle)
45 {
46 return mbtk_info_handle_free(&info_handle);
47 }
48 else
49 {
50 return -1;
51 }
52}
53
54int lynq_get_version(char buf[])
55{
56 if(info_handle == NULL || buf == NULL)
57 {
58 return -1;
59 }
60
61 int err = mbtk_version_get(info_handle, buf);
62 if(err) {
63 return -1;
64 } else {
65 return 0;
66 }
67}
68
69int lynq_get_imei(char buf[])
70{
71 if(info_handle == NULL || buf == NULL)
72 {
73 return -1;
74 }
75
76 int err = mbtk_imei_get(info_handle, buf);
77 if(err) {
78 return -1;
79 } else {
80 return 0;
81 }
82}
83
84int lynq_get_sn(char buf[])
85{
86 if(info_handle == NULL || buf == NULL)
87 {
88 return -1;
89 }
90
91 int err = mbtk_sn_get(info_handle, buf);
92 if(err) {
93 return -1;
94 } else {
95 return 0;
96 }
97}
98
99int lynq_get_zone_tmp(ZONE_NUM num, int *temp)
100{
101 if(info_handle == NULL || temp == NULL)
102 {
103 return -1;
104 }
105
106 return mbtk_temp_get(info_handle, num, temp);
107}
108
109int lynq_shutdown(char options[])
110{
111 if(options == NULL)
112 {
113 return -1;
114 }
115
116 if(!strcmp(options, "reboot")) {
117 return mbtk_system_reboot(0);
118 } else if(!strcmp(options, "poweroff")) {
119 return mbtk_system_reboot(1);
120 } else if(!strcmp(options, "halt")) {
121 return mbtk_system_reboot(2);
122 } else {
123 return -1;
124 }
125 return 0;
126}
127
128int lynq_time_set(mbtk_time_type_enum time_type, char* time_str)
129{
130 if(info_handle == NULL)
131 {
132 return -1;
133 }
134
135 return mbtk_time_set(info_handle, time_type, time_str);
136}
137
138int lynq_get_sim_status(int *card_status)
139{
140 if(info_handle == NULL || card_status == NULL)
141 {
142 return -1;
143 }
144
145 mbtk_sim_state_enum sim;
146 int err = mbtk_sim_state_get(info_handle, &sim);
147 if(err) {
148 return -1;
149 } else {
150 *card_status = sim;
151 return 0;
152 }
153}
154
155int lynq_get_imsi(char buf[])
156{
157 if(info_handle == NULL || buf == NULL)
158 {
159 return -1;
160 }
161
162 int err = mbtk_imsi_get(info_handle, buf);
163 if(err) {
164 return -1;
165 } else {
166 return 0;
167 }
168}
169
170int lynq_get_iccid(char buf[])
171{
172 if(info_handle == NULL || buf == NULL)
173 {
174 return -1;
175 }
176
177 int err = mbtk_iccid_get(info_handle, buf);
178 if(err) {
179 return -1;
180 } else {
181 return 0;
182 }
183}
184
185int lynq_query_phone_number(char buf[])
186{
187 if(info_handle == NULL || buf == NULL)
188 {
189 return -1;
190 }
191
192 int err = mbtk_phone_number_get(info_handle, buf);
193 if(err) {
194 return -1;
195 } else {
196 return 0;
197 }
198}
199
200int lynq_sim_power (int mode)
201{
202 if(mode != 0 && mode != 1)
203 {
204 return -1;
205 }
206
207 return mbtk_sim_power_set(mode);
208}
209
210int lynq_query_operater(char *OperatorFN,char *OperatorSH,char *MccMnc)
211{
212 if(info_handle == NULL || OperatorFN == NULL || OperatorSH == NULL || MccMnc == NULL)
213 {
214 return -1;
215 }
216
217 mbtk_net_info_t net;
218 if(!mbtk_net_sel_mode_get(info_handle, &net) && net.plmn > 0)
219 {
220 // printf("Net : %d, %d, %d\n", net.net_sel_mode, net.net_type, net.plmn);
221 int i = 0;
222 while(i < ARRAY_SIZE(operator_mcc_mnc))
223 {
224 if(operator_mcc_mnc[i].mcc_mnc == net.plmn)
225 break;
226 i++;
227 }
228
229 if(i == ARRAY_SIZE(operator_mcc_mnc)) // No found mcc&mnc
230 {
231 strcpy(OperatorFN, "UNKNOWN");
232 strcpy(OperatorSH, "UNKNOWN");
233 sprintf(MccMnc, "%d", net.plmn);
234 }
235 else
236 {
237 strcpy(OperatorFN, operator_mcc_mnc[i].operator_l);
238 strcpy(OperatorSH, operator_mcc_mnc[i].operator_s);
239 sprintf(MccMnc, "%d", operator_mcc_mnc[i].mcc_mnc);
240 }
241 return 0;
242 }
243
244 return -1;
245}
246
247int lynq_query_network_selection_mode (int *netselMode)
248{
249 if(info_handle == NULL || netselMode == NULL)
250 {
251 return -1;
252 }
253
254 mbtk_net_info_t net;
255 if(!mbtk_net_sel_mode_get(info_handle, &net))
256 {
257 // printf("Net : %d, %d, %d\n", net.net_sel_mode, net.net_type, net.plmn);
258 *netselMode = net.net_sel_mode;
259 return 0;
260 }
261
262 return -1;
263}
264
265int lynq_set_network_selection_mode(const char *mode, const char* mccmnc)
266{
267 if(info_handle == NULL || str_empty(mode))
268 {
269 return -1;
270 }
271
272 mbtk_net_info_t net;
273 net.net_type = 0xFF;
274 if(!strcmp(mode, "Auto"))
275 {
276 net.net_sel_mode = 0;
277 net.plmn = 0;
278 }
279 else if(!strcmp(mode, "Manual") && !str_empty(mccmnc))
280 {
281 net.net_sel_mode = 1;
282 net.plmn = (uint32)atoi(mccmnc);
283 }
284 else
285 {
286 return -1;
287 }
288 if(!mbtk_net_sel_mode_set(info_handle, &net))
289 {
290 return 0;
291 }
292
293 return -1;
294}
295
296int lynq_query_available_network(list_node_t** net_list)
297{
298 if(info_handle == NULL)
299 {
300 return -1;
301 }
302
303 return mbtk_available_net_get(info_handle, net_list);
304}
305
306int lynq_query_registration_state(const char *type, int* regState,int *imsRegState,char * LAC,char *CID,int *netType,int * radioTechFam,int *netRejected)
307{
308 if(info_handle == NULL || str_empty(type) || regState == NULL || imsRegState == NULL
309 || LAC == NULL || CID == NULL || netType == NULL || radioTechFam == NULL || netRejected == NULL)
310 {
311 return -1;
312 }
313 mbtk_net_reg_info_t reg;
314 int err = mbtk_net_reg_get(info_handle, &reg);
315 if(err) {
316 *netRejected = err;
317 return -1;
318 } else {
319 //printf("REG : %d, %d, %d, %04x, %08o\n", reg.state, reg.type, reg.ims_reg, reg.lac, reg.ci);
320 // Voice/Data/IMS
321 if(strcmp("Voice", type) == 0) {
322 *regState = reg.call_state;
323 } else if(strcmp("Data", type) == 0) {
324 *regState = reg.data_state;
325 } else if(strcmp("IMS", type) == 0) {
326 *imsRegState = reg.ims_state;
327 } else {
328 return -1;
329 }
330
331 if(reg.call_state != MBTK_NET_REG_STATE_NON || reg.data_state != MBTK_NET_REG_STATE_NON || reg.ims_state != MBTK_NET_REG_STATE_NON) {
332 sprintf(LAC, "%04x", reg.lac);
333 sprintf(CID, "%08o", reg.ci);
334 *netType = reg.type;
335 *radioTechFam = RADIO_TECH_3GPP;
336 }
337 return 0;
338 }
339}
340
341int lynq_query_prefferred_networktype (int *preNetType)
342{
343 if(info_handle == NULL || preNetType == NULL)
344 {
345 return -1;
346 }
347
348 mbtk_band_info_t band;
349 int err = mbtk_current_band_get(info_handle, &band);
350 if(err) {
351 return -1;
352 } else {
353 //printf("Band : %d, %d, %d, %d, %d\n", band.net_pref, band.gsm_band, band.umts_band, band.tdlte_band, band.fddlte_band);
354 *preNetType = band.net_pref;
355 return 0;
356 }
357}
358
359int lynq_set_prefferred_networktype (const int preNetType)
360{
361
362 if(info_handle == NULL)
363 {
364 return -1;
365 }
366 if(preNetType < 0 || preNetType > 15)
367 {
368 return -2;
369 }
370 mbtk_band_info_t band;
371 memset(&band, 0, sizeof(mbtk_band_info_t));
372 band.net_pref = preNetType;
373 int err = mbtk_current_band_set(info_handle, &band);
374 if(err) {
375 return -1;
376 } else {
377 return 0;
378 }
379}
380
381int lynq_set_band_mode(int gsm_band, int umts_band, int tdlte_band, int fddlte_band)
382{
383 if(info_handle == NULL)
384 {
385 return -1;
386 }
387
388 mbtk_band_info_t band;
389 band.net_pref = 0xFF; // No change network pref.
390 band.gsm_band = (uint16)gsm_band;
391 band.umts_band = (uint16)umts_band;
392 band.tdlte_band = (uint32)tdlte_band;
393 band.fddlte_band = (uint32)fddlte_band;
394 int err = mbtk_current_band_set(info_handle, &band);
395 if(err) {
396 return -1;
397 } else {
398 return 0;
399 }
400}
401
402int lynq_query_available_bandmode (int *gsm_band, int *umts_band, int *tdlte_band, int *fddlte_band)
403{
404 if(info_handle == NULL || gsm_band == NULL || umts_band == NULL || tdlte_band == NULL || fddlte_band == NULL)
405 {
406 return -1;
407 }
408
409 mbtk_band_info_t band;
410 int err = mbtk_support_band_get(info_handle, &band);
411 if(err) {
412 return -1;
413 } else {
414 //printf("Band : %d, %d, %d, %d, %d\n", band.net_pref, band.gsm_band, band.umts_band, band.tdlte_band, band.fddlte_band);
415 *gsm_band = band.gsm_band;
416 *umts_band = band.umts_band;
417 *tdlte_band = band.tdlte_band;
418 *fddlte_band = band.fddlte_band;
419 return 0;
420 }
421}
422
423int lynq_radio_on (const int data)
424{
425 if(info_handle == NULL || (data != 0 && data != 1))
426 {
427 return -1;
428 }
429
430 int err = mbtk_radio_state_set(info_handle, data);
431 if(err) {
432 return -1;
433 } else {
434 return 0;
435 }
436}
437
438int lynq_query_radio_tech (int* radioTech)
439{
440 if(info_handle == NULL || radioTech == NULL)
441 {
442 return -1;
443 }
444
445 int err = mbtk_radio_state_get(info_handle, radioTech);
446 if(err) {
447 return -1;
448 } else {
449 return 0;
450 }
451}
452
453int lynq_solicited_signal_strength (signalStrength_t *solSigStren)
454{
455 if(info_handle == NULL || solSigStren == NULL)
456 {
457 return -1;
458 }
459
460 mbtk_signal_info_t signal;
461 int err = mbtk_net_signal_get(info_handle, &signal);
462 if(err) {
463 return -1;
464 } else {
465 memset(solSigStren, 0, sizeof(signalStrength_t));
466 switch(mbtk_net_type_get(signal.type))
467 {
468 case MBTK_NET_TYPE_GSM:
469 solSigStren->gsm_sig_valid = 1;
470 break;
471 case MBTK_NET_TYPE_UMTS:
472 solSigStren->umts_sig_valid = 1;
473 break;
474 case MBTK_NET_TYPE_LTE:
475 solSigStren->lte_sig_valid = 1;
476 break;
477 default:
478 break;
479 }
480 solSigStren->rssi = signal.rssi;
481 solSigStren->ber = signal.ber;
482 solSigStren->rxlev = signal.rxlev;
483 solSigStren->rscp = signal.rscp;
484 solSigStren->ecno = signal.ecno;
485 solSigStren->rsrq = signal.rsrq;
486 solSigStren->rsrp = signal.rsrp;
487 return 0;
488 }
489}
490
491int lynq_set_ims (const int ims_mode)
492{
493 if(info_handle == NULL || (ims_mode != 0 && ims_mode != 1))
494 {
495 return -1;
496 }
497
498 int err = mbtk_volte_state_set(info_handle, ims_mode);
499 if(err) {
500 return -1;
501 } else {
502 return 0;
503 }
504}
505
506int lynq_init_cell(void)
507{
508 if(info_handle == NULL)
509 {
510 info_handle = mbtk_info_handle_get();
511 if(info_handle)
512 {
513 printf("creat info_handle is success\n");
514 }
515 else{
516 printf("creat info_handle is fail\n");
517 return -1;
518 }
519 }
520
521 return 0;
522}
523
524int lynq_deinit_cell(void)
525{
526 if(info_handle)
527 {
528 return mbtk_info_handle_free(&info_handle);
529 }
530 else
531 {
532 return -1;
533 }
534}
535
536/*
537* Get current cell infomation.
538*/
539int lynq_query_cell_info(int *type, list_node_t **cell_list)
540{
541 if(info_handle == NULL || type == NULL)
542 {
543 return -1;
544 }
545
546 return mbtk_cell_get(info_handle, type, cell_list);
547}
548
549/*
550* set current cell infomation.
551*/
552void lynq_set_cell_info(char *mem)
553{
554 if(info_handle == NULL || mem == NULL)
555 {
556 return -1;
557 }
558
559 char resp[1024] = {0};
560 mbtk_cell_set(info_handle, mem, resp);
561
562 return ;
563}
564
565/*
566* Set specific APN informations.
567*
568* cid : 2-7
569*/
570int lynq_apn_set(int cid, mbtk_ip_type_enum ip_type, const void* apn_name,
571 const void *user_name, const void *user_pass, const void *auth)
572{
573 if(info_handle == NULL || str_empty(apn_name))
574 {
575 return -1;
576 }
577
578 return mbtk_apn_set(info_handle, cid, ip_type, apn_name, user_name, user_pass, auth);
579}
580
581/*
582* Get current all APN informations.
583*/
584int lynq_apn_get(int *apn_num, mbtk_apn_info_t apns[])
585{
586 if(info_handle == NULL || apn_num == NULL || apns == NULL)
587 {
588 return -1;
589 }
590
591 return mbtk_apn_get(info_handle, apn_num, apns);
592}
593
594/*
595* Start data call.
596*/
597int lynq_data_call_start(int cid, int timeout)
598{
599 if(info_handle == NULL)
600 {
601 return -1;
602 }
603
604 return mbtk_data_call_start(info_handle, cid, 0, FALSE, timeout);
605}
606
607/*
608* Stop data call.
609*/
610int lynq_data_call_stop(int cid, int timeout)
611{
612 if(info_handle == NULL)
613 {
614 return -1;
615 }
616
617 return mbtk_data_call_stop(info_handle, cid, timeout);
618}
619
620/*
621* Query data call state.
622*/
623int lynq_data_call_query(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6)
624{
625 if(info_handle == NULL || ipv4 == NULL || ipv6 == NULL)
626 {
627 return -1;
628 }
629
630 return mbtk_data_call_state_get(info_handle, cid, ipv4, ipv6);
631}
632
633/*
634 * Get the native ip and free port.
635 */
636int lynq_get_ip_and_port(char *ipBuf_out,int *port,int iptype)
637{
638 char psz_port_cmd[128];
639 int i=0;
640
641 *port = rand() % (60000 - 50000 + 1) + 50000;
642 sprintf(psz_port_cmd, "netstat -an | grep :%d > /dev/null", *port);
643
644 char ipBuf[32] = "";
645 FILE *fstream=NULL;
646
647 char buff[1024];
648 char iptype_str[8];
649 memset(buff,0,sizeof(buff));
650 /*eth0????eth1?docker0?em1?lo?*/
651 if(iptype == 1)
652 {
653
654 if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet6 addr: 2\" | awk '{print $3}'","r")))
655 {
656 snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0");
657 }
658 if(NULL!=fgets(buff, sizeof(buff), fstream))
659 {
660 snprintf(ipBuf, 39, "%s",buff);
661 }
662 else
663 {
664 snprintf(ipBuf, 39, "%s","0:0:0:0:0:0:0:0");
665 pclose(fstream);
666 }
667 }
668 else if(iptype == 0)
669 {
670 if(NULL==(fstream=popen("ifconfig ccinet0 | grep \"inet addr:\" | awk \'{print $2}\' | cut -c 6-","r")))
671 {
672 snprintf(ipBuf, 18, "%s","0.0.0.0");
673 }
674 if(NULL!=fgets(buff, sizeof(buff), fstream))
675 {
676 snprintf(ipBuf, 18, "%s",buff);
677 }
678 else
679 {
680 snprintf(ipBuf, 18, "%s","0.0.0.0");
681 pclose(fstream);
682 }
683 }
684 else
685 {
686 return -1;
687 }
688 pclose(fstream);
689
690 printf("ip:%s\n", ipBuf);
691 memcpy(ipBuf_out, ipBuf, 32);
692 return 0;
693}
694
695int lynq_get_modem_fun(MBTK_DEV_MODEM_FUNCTION *fun)
696{
697 if(info_handle == NULL)
698 {
699 return -1;
700 }
701
702 int err = mbtk_get_modem_fun(info_handle, fun);
703 if(err) {
704 return -1;
705 } else {
706 return 0;
707 }
708}