/
rtstate.c
1687 lines (1453 loc) · 37 KB
/
rtstate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <bcmnvram.h>
#include <bcmdevs.h>
#include <shutils.h>
#include <shared.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef RTCONFIG_BROOP
#include <linux/netlink.h>
#include <rtstate.h>
#endif
#include <limits.h> //PATH_MAX
static char *wantype_str[] = {
[WANS_DUALWAN_IF_LAN] = "lan",
[WANS_DUALWAN_IF_2G] = "2g",
[WANS_DUALWAN_IF_5G] = "5g",
[WANS_DUALWAN_IF_USB] = "usb",
[WANS_DUALWAN_IF_DSL] = "dsl",
[WANS_DUALWAN_IF_WAN] = "wan",
[WANS_DUALWAN_IF_WAN2] = "wan2",
[WANS_DUALWAN_IF_USB2] = "usb2",
[WANS_DUALWAN_IF_SFPP] = "sfp+",
};
/* keyword for rc_support */
/* ipv6 mssid update parental */
void add_rc_support(char *feature)
{
char *rcsupport, *features;
if (!(feature && *feature))
return;
rcsupport = nvram_safe_get("rc_support");
if (*rcsupport) {
if (asprintf(&features, "%s %s", rcsupport, feature) < 0 || !features) {
_dprintf("add_rc_support fail\n");
return;
}
nvram_set("rc_support", features);
free(features);
} else
nvram_set("rc_support", feature);
}
void del_rc_support(char *feature)
{
char *rcsupport, *features;
char word[256], *next;
if (!(feature && *feature))
return;
rcsupport = nvram_safe_get("rc_support");
if (*rcsupport) {
features = strdup(rcsupport);
if (!features) {
_dprintf("del_rc_support fail\n");
return;
}
foreach(word, feature, next)
remove_from_list(word, features, strlen(features) + 1);
if (strcmp(rcsupport, features) != 0)
nvram_set("rc_support", features);
free(features);
}
}
#if defined(RTCONFIG_DUALWAN)
/**
* is_nat_enabled() for dual/multiple WAN.
* In Single WAN mode or Dual WAN Fail-Over/Fail-Back mode, check primary WAN.
* In Dual WAN load-balance mode, check all WAN.
*/
int is_nat_enabled(void)
{
int i, nr_nat = 0, sw_mode = nvram_get_int("sw_mode");
char prefix[sizeof("wanX_XXXXXX")];
if (sw_mode != SW_MODE_ROUTER && sw_mode != SW_MODE_HOTSPOT)
return 0;
if (get_nr_wan_unit() >= 2 && nvram_match("wans_mode", "lb")) {
/* Dual WAN LB, check all WAN unit. */
for (i = WAN_UNIT_FIRST; i < WAN_UNIT_MAX; ++i) {
snprintf(prefix, sizeof(prefix), "wan%d_", i);
if (nvram_pf_get_int(prefix, "nat_x") == 1)
nr_nat++;
}
} else {
/* Single WAN/Dual WAN FO/FB, check primary WAN unit only. */
snprintf(prefix, sizeof(prefix), "wan%d_", wan_primary_ifunit());
nr_nat = nvram_pf_get_int(prefix, "nat_x");
}
return (nr_nat > 0)? 1 : 0;
}
#endif
int get_wan_state(int unit){
char tmp[100], prefix[16];
snprintf(prefix, 16, "wan%d_", unit);
return nvram_get_int(strlcat_r(prefix, "state_t", tmp, sizeof(tmp)));
}
int get_wan_sbstate(int unit){
char tmp[100], prefix[16];
snprintf(prefix, 16, "wan%d_", unit);
return nvram_get_int(strlcat_r(prefix, "sbstate_t", tmp, sizeof(tmp)));
}
int get_wan_auxstate(int unit){
char tmp[100], prefix[16];
snprintf(prefix, 16, "wan%d_", unit);
return nvram_get_int(strlcat_r(prefix, "auxstate_t", tmp, sizeof(tmp)));
}
char *link_wan_nvname(int unit, char *buf, int size){
if(buf == NULL)
return NULL;
if(unit == WAN_UNIT_FIRST)
snprintf(buf, size, "link_wan");
else
snprintf(buf, size, "link_wan%d", unit);
#ifdef RTCONFIG_MULTISERVICE_WAN
if (unit > WAN_UNIT_MULTISRV_BASE)
{
int base_unit = get_ms_base_unit(unit);
if (base_unit == WAN_UNIT_FIRST)
snprintf(buf, size, "link_wan");
else
snprintf(buf, size, "link_wan%d", base_unit);
}
#endif
return buf;
}
int is_internet_connect(int unit){
int link_internet, wan_auxstate;
if(!is_wan_connect(unit))
return 0;
if(nvram_match("wans_mode", "lb")){
link_internet = nvram_get_int("link_internet");
if(link_internet == 2)
return 1;
}
else{
wan_auxstate = get_wan_auxstate(unit);
if(wan_auxstate == WAN_AUXSTATE_NONE)
return 1;
}
return 0;
}
int is_wan_connect(int unit){
int wan_state, wan_sbstate, wan_auxstate;
#ifdef RTCONFIG_BCMARM
char prefix[] = "wanXXXXXXXXXX_";
int wan_proto = 0;
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_proto = get_wan_proto(prefix);
if(wan_proto == WAN_STATIC)
return is_phy_connect2(unit);
#endif
if(!is_phy_connect(unit))
return 0;
wan_state = get_wan_state(unit);
wan_sbstate = get_wan_sbstate(unit);
wan_auxstate = get_wan_auxstate(unit);
if(wan_state == WAN_STATE_CONNECTED && wan_sbstate == WAN_STOPPED_REASON_NONE &&
(wan_auxstate == WAN_AUXSTATE_NONE || wan_auxstate == WAN_AUXSTATE_NO_INTERNET_ACTIVITY)
)
return 1;
else
return 0;
}
// auxstate will be reset by update_wan_state(), but wanduck cannot set it soon sometimes.
// only link_wan will be safe.
int is_phy_connect(int unit){
char prefix[sizeof("link_wanXXXXXX")], *ptr;
int link_wan;
#ifdef RTCONFIG_BCMARM
int wan_proto = 0;
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_proto = get_wan_proto(prefix);
if(wan_proto == WAN_STATIC)
return is_phy_connect2(unit);
#endif
link_wan_nvname(unit, prefix, sizeof(prefix));
ptr = nvram_safe_get(prefix);
if(strlen(ptr) > 0){
link_wan = atoi(ptr);
if(link_wan)
return 1;
else
{
#ifdef RTCONFIG_DSL
int wan_type = get_dualwan_by_unit(unit);
if (wan_type == WANS_DUALWAN_IF_WAN || wan_type == WANS_DUALWAN_IF_LAN)
return get_wanports_status(unit);
#endif
return 0;
}
}
else
#ifdef RTCONFIG_USB_MODEM
if(dualwan_unit__usbif(unit))
return 1;
else
#endif
return get_wanports_status(unit);
}
int is_phy_connect2(int unit){
#ifdef RTCONFIG_USB_MODEM
if(dualwan_unit__usbif(unit))
return 1;
else
#endif
return get_wanports_status(unit);
}
int is_ip_conflict(int unit){
int wan_state, wan_sbstate;
wan_state = get_wan_state(unit);
wan_sbstate = get_wan_sbstate(unit);
if(wan_state == WAN_STATE_STOPPED && wan_sbstate == WAN_STOPPED_REASON_INVALID_IPADDR)
return 1;
else
return 0;
}
// get wan_unit from device ifname or hw device ifname
#if 0
int get_wan_unit(char *ifname)
{
char word[256], tmp[100], *next;
char prefix[32]="wanXXXXXX_";
int unit, found = 0;
unit = WAN_UNIT_FIRST;
foreach (word, nvram_safe_get("wan_ifnames"), next) {
if(strncmp(ifname, "ppp", 3)==0) {
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
if(strcmp(nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp))), ifname)==0) {
found = 1;
}
}
else if(strcmp(ifname, word)==0) {
found = 1;
}
if(found) break;
unit ++;
}
if(!found) unit = WAN_UNIT_FIRST;
return unit;
}
#else
int get_wan_unit(char *ifname)
{
char tmp[100], prefix[sizeof("wanXXXXXXXXXX_")];
int unit, wan_proto;
if (ifname == NULL || *ifname == '\0')
return -1;
#ifdef BLUECAVE
/* TODO: unclear, why this workaround is required? */
int bluecave = (get_model() == MODEL_BLUECAVE &&
strcmp(ifname, "eth1") == 0 && nvram_get_int("switch_stb_x") > 0);
#endif
for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; unit++) {
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_proto = get_wan_proto(prefix);
switch (wan_proto) {
case WAN_PPPOE:
case WAN_PPTP:
case WAN_L2TP:
#ifdef RTCONFIG_SOFTWIRE46
case WAN_LW4O6:
case WAN_MAPE:
case WAN_V6PLUS:
case WAN_OCNVC:
case WAN_DSLITE:
#endif
if (nvram_match(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)), ifname))
return unit;
#ifdef RTCONFIG_USB_MODEM
if (dualwan_unit__usbif(unit))
break;
#endif
/* fall through */
default:
if (nvram_match(strlcat_r(prefix, "ifname", tmp, sizeof(tmp)), ifname))
return unit;
#ifdef BLUECAVE
if (bluecave)
return unit;
#endif
break;
}
}
return -1;
}
#endif
// Get physical wan ifname of working connection
char *get_wanx_ifname(int unit)
{
char *wan_ifname;
char tmp[100], prefix[sizeof("wanXXXXXXXXXX_")];
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_ifname = nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp)));
return wan_ifname;
}
// Get wan ifname of working connection
char *get_wan_ifname(int unit)
{
char tmp[100], prefix[sizeof("wanXXXXXXXXXX_")];
char *wan_ifname;
int wan_proto;
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_proto = get_wan_proto(prefix);
#ifdef RTCONFIG_USB_MODEM
if (dualwan_unit__usbif(unit)) {
wan_ifname = (wan_proto == WAN_DHCP) ?
nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp))) :
nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)));
} else
#endif
#if defined(RTAX82_XD6) || defined(RTAX82_XD6S) || defined(XD6_V2) || defined(ET12)
if (!strncmp(nvram_safe_get("territory_code"), "CH", 2) &&
nvram_match(ipv6_nvname("ipv6_only"), "1"))
return nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp)));
else
#endif
switch (wan_proto) {
case WAN_PPPOE:
case WAN_PPTP:
case WAN_L2TP:
#ifdef RTCONFIG_SOFTWIRE46
case WAN_LW4O6:
case WAN_MAPE:
#endif
wan_ifname = nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)));
break;
#ifdef RTCONFIG_SOFTWIRE46
case WAN_V6PLUS:
case WAN_OCNVC:
case WAN_DSLITE:
if (nvram_pf_get_int(prefix, "s46_hgw_case") >= S46_CASE_MAP_HGW_OFF) {
wan_ifname = nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)));
break;
}
#endif
default:
wan_ifname = nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp)));
break;
}
return wan_ifname;
}
// Get wan ipv6 ifname of working connection
#ifdef RTCONFIG_IPV6
char *get_wan6_ifname(int unit)
{
char tmp[100], prefix[sizeof("wanXXXXXXXXXX_")];
char *wan_ifname;
int wan_proto;
switch (get_ipv6_service_by_unit(unit)) {
case IPV6_NATIVE_DHCP:
case IPV6_MANUAL:
#ifdef RTCONFIG_6RELAYD
case IPV6_PASSTHROUGH:
#endif
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
wan_proto = get_wan_proto(prefix);
#ifdef RTCONFIG_USB_MODEM
if (dualwan_unit__usbif(unit)) {
wan_ifname = (wan_proto == WAN_DHCP) ?
nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp))) :
nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)));
} else
#endif
switch (wan_proto) {
case WAN_PPPOE:
case WAN_PPTP:
case WAN_L2TP:
if (nvram_match(ipv6_nvname_by_unit("ipv6_ifdev", unit), "ppp")) {
wan_ifname = nvram_safe_get(strlcat_r(prefix, "pppoe_ifname", tmp, sizeof(tmp)));
break;
}
/* fall through */
default:
wan_ifname = nvram_safe_get(strlcat_r(prefix, "ifname", tmp, sizeof(tmp)));
break;
}
break;
case IPV6_6TO4:
case IPV6_6IN4:
case IPV6_6RD:
/* no ipv6 multiwan tunnel support so far */
wan_ifname = "v6tun0";
break;
default:
return "";
}
return wan_ifname;
}
int get_wan6_unit(char* ifname)
{
if (!ifname)
return 0;
if (!strncmp(ifname, "v6tun", 5)) {
return (strlen(ifname) > 5) ? atoi(ifname+5) : 0;
}
else {
return get_wan_unit(ifname);
}
}
#endif
int get_ports_status(unsigned int port_mask)
{
#if defined(RTCONFIG_QCA) && defined(RTCONFIG_DETWAN)
return rtkswitch_Port_phyStatus(port_mask);
#else
return -1;
#endif
}
// OR all lan port status
int get_lanports_status(void)
{
return lanport_status();
}
// OR all wan port status
int get_wanports_status(int wan_unit)
{
// 1. PHY type, 2. factory owner, 3. model.
#ifdef RTCONFIG_DSL
#ifdef RTCONFIG_DUALWAN
if(get_dualwan_by_unit(wan_unit) == WANS_DUALWAN_IF_DSL)
#endif
{
if (nvram_match("dsltmp_adslsyncsts","up")) return 1;
return 0;
}
#ifdef RTCONFIG_DUALWAN
if( get_dualwan_by_unit(wan_unit) == WANS_DUALWAN_IF_LAN
|| get_dualwan_by_unit(wan_unit) == WANS_DUALWAN_IF_WAN
)
{
#ifdef RTCONFIG_RALINK
return rtkswitch_wanPort_phyStatus(wan_unit); //Paul modify 2012/12/4
#else
return wanport_status(wan_unit) ? 1 : 0;
#endif
}
#endif
// TO CHENI:
// HOW TO HANDLE USB?
#else // RJ-45
#if defined(RTCONFIG_RALINK) || defined(RTCONFIG_QCA)
return rtkswitch_wanPort_phyStatus(wan_unit);
#else
return wanport_status(wan_unit);
#endif
#endif
}
int get_usb_modem_state(){
if(!strcmp(nvram_safe_get("modem_running"), "1"))
return 1;
else
return 0;
}
int set_usb_modem_state(const int flag){
if(flag != 1 && flag != 0)
return 0;
if(flag){
nvram_set("modem_running", "1");
return 1;
}
else{
nvram_set("modem_running", "0");
return 0;
}
}
int
set_wan_primary_ifunit(const int unit)
{
char tmp[100], prefix[] = "wanXXXXXXXXXX_";
int i;
if (unit < WAN_UNIT_FIRST || unit >= WAN_UNIT_MAX)
return -1;
nvram_set_int("wan_primary", unit);
for (i = WAN_UNIT_FIRST; i < WAN_UNIT_MAX; i++) {
snprintf(prefix, sizeof(prefix), "wan%d_", i);
nvram_set_int(strlcat_r(prefix, "primary", tmp, sizeof(tmp)), (i == unit) ? 1 : 0);
}
return 0;
}
int
wan_primary_ifunit(void)
{
char tmp[100], prefix[] = "wanXXXXXXXXXX_";
int unit;
/* TODO: Why not just nvram_get_int("wan_primary")? */
for (unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; unit ++) {
snprintf(prefix, sizeof(prefix), "wan%d_", unit);
if (nvram_match(strlcat_r(prefix, "primary", tmp, sizeof(tmp)), "1"))
return unit;
}
return 0;
}
#ifdef RTCONFIG_REALTEK
/* The fuction is avoiding watchdog segfault on RP-AC68U.
* This is a workaround solution.
* */
int
rtk_wan_primary_ifunit(void)
{
return wan_primary_ifunit();
}
#endif
int
wan_primary_ifunit_ipv6(void)
{
#ifdef RTCONFIG_DUALWAN
#if defined(RTCONFIG_MULTIWAN_CFG)
int unit = wan_primary_ifunit();
if (!strstr(nvram_safe_get("wans_dualwan"), "none")
#ifdef RTCONFIG_IPV6
&& get_ipv6_service_by_unit(unit) == IPV6_DISABLED
#endif
)
return (1 - unit);
return unit;
#else
return 0;
#endif
#else
return wan_primary_ifunit();
#endif
}
#ifdef RTCONFIG_MEDIA_SERVER
void
set_invoke_later(int flag)
{
nvram_set_int("invoke_later", nvram_get_int("invoke_later")|flag);
}
int
get_invoke_later()
{
return(nvram_get_int("invoke_later"));
}
#endif /* RTCONFIG_MEDIA_SERVER */
#ifdef RTCONFIG_USB
#if defined(RTCONFIG_ALPINE) || defined(RTCONFIG_LANTIQ)
char *get_usb_xhci_port(int port){
if(port == 2)
return USB_XHCI_PORT_2;
else
return USB_XHCI_PORT_1;
}
char *get_usb_ehci_port(int port){
if(port == 3)
return USB_EHCI_PORT_3;
else if(port == 2)
return USB_EHCI_PORT_2;
else
return USB_EHCI_PORT_1;
}
char *get_usb_ohci_port(int port){
if(port == 3)
return USB_OHCI_PORT_3;
else if(port == 2)
return USB_OHCI_PORT_2;
else
return USB_OHCI_PORT_1;
}
int get_usb_port_number(const char *usb_port){
int i;
for(i = 1; i <= 2; ++i){
if(!strcmp(usb_port, get_usb_xhci_port(i))){
return i;
}
}
for(i = 1; i <= 3; ++i){
if(!strcmp(usb_port, get_usb_ehci_port(i))){
return i;
}
}
for(i = 1; i <= 3; ++i){
if(!strcmp(usb_port, get_usb_ohci_port(i))){
return i;
}
}
return 0;
}
int get_usb_port_host(const char *usb_port){
int i;
for(i = 1; i <= 2; ++i){
if(!strcmp(usb_port, get_usb_xhci_port(i))){
return USB_HOST_XHCI;
}
}
for(i = 1; i <= 3; ++i){
if(!strcmp(usb_port, get_usb_ehci_port(i))){
return USB_HOST_EHCI;
}
}
for(i = 1; i <= 3; ++i){
if(!strcmp(usb_port, get_usb_ohci_port(i))){
return USB_HOST_OHCI;
}
}
return USB_HOST_NONE;
}
#else
char xhci_string[32];
char ehci_string[32];
char ohci_string[32];
char *get_usb_xhci_port(int port)
{
char word[100], *next;
int i=0;
strlcpy(xhci_string, "xxxxxxxx", sizeof(xhci_string));
foreach(word, nvram_safe_get("xhci_ports"), next){
if(i == port){
strlcpy(xhci_string, word, sizeof(xhci_string));
break;
}
i++;
}
return xhci_string;
}
char *get_usb_ehci_port(int port)
{
char word[100], *next;
int i=0;
strlcpy(ehci_string, "xxxxxxxx", sizeof(ehci_string));
foreach(word, nvram_safe_get("ehci_ports"), next) {
if(i==port) {
strlcpy(ehci_string, word, sizeof(ehci_string));
break;
}
i++;
}
return ehci_string;
}
char *get_usb_ohci_port(int port)
{
char word[100], *next;
int i=0;
strlcpy(ohci_string, "xxxxxxxx", sizeof(ohci_string));
foreach(word, nvram_safe_get("ohci_ports"), next) {
if(i==port) {
strlcpy(ohci_string, word, sizeof(ohci_string));
break;
}
i++;
}
return ohci_string;
}
int get_usb_port_number(const char *usb_port)
{
char word[100], *next;
int i;
i = 0;
foreach(word, nvram_safe_get("xhci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return i;
}
}
i = 0;
foreach(word, nvram_safe_get("ehci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return i;
}
}
i = 0;
foreach(word, nvram_safe_get("ohci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return i;
}
}
return 0;
}
int get_usb_port_host(const char *usb_port)
{
char word[100], *next;
int i;
i = 0;
foreach(word, nvram_safe_get("xhci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return USB_HOST_XHCI;
}
}
i = 0;
foreach(word, nvram_safe_get("ehci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return USB_HOST_EHCI;
}
}
i = 0;
foreach(word, nvram_safe_get("ohci_ports"), next){
++i;
if(!strcmp(usb_port, word)){
return USB_HOST_OHCI;
}
}
return USB_HOST_NONE;
}
#endif
#endif // RTCONFIG_USB
void set_wanscap_support(char *feature)
{
nvram_set("wans_cap", feature);
}
#if defined(RTCONFIG_DUALWAN)
void add_wanscap_support(char *feature)
{
char features[128];
int len;
strlcpy(features, nvram_safe_get("wans_cap"), sizeof(features));
if((len = strlen(features))==0)
nvram_set("wans_cap", feature);
else {
len += sprintf(features + len, " %s", feature);
nvram_set("wans_cap", features);
}
}
int get_wans_cap(void)
{
char wans_cap[64] = {0};
char word[8] = {0};
char *next = NULL;
int caps = 0;
strlcpy(wans_cap, nvram_safe_get("wans_cap"), sizeof(wans_cap));
foreach(word, wans_cap, next) {
if (!strcmp(word,"lan")) caps |= WANSCAP_LAN;
if (!strcmp(word,"2g")) caps |= WANSCAP_2G;
if (!strcmp(word,"5g")) caps |= WANSCAP_5G;
if (!strcmp(word,"usb")) caps |= WANSCAP_USB;
if (!strcmp(word,"dsl")) caps |= WANSCAP_DSL;
if (!strcmp(word,"wan")) caps |= WANSCAP_WAN;
if (!strcmp(word,"wan2")) caps |= WANSCAP_WAN2;
if (!strcmp(word,"sfp+")) caps |= WANSCAP_SFPP;
if (!strcmp(word,"6g")) caps |= WANSCAP_6G;
}
return caps;
}
int get_wans_dualwan_str(char *wancaps, int size){
snprintf(wancaps, size, "%s", nvram_get("wans_dualwan") ? : (nvram_default_get("wans_dualwan") ? :
#ifdef RTCONFIG_DSL
"dsl"
#elif defined(RTCONFIG_INTERNAL_GOBI) && defined(RTCONFIG_NO_WANPORT)
"usb"
#else
"wan"
#endif
" "DEF_SECOND_WANIF
)
);
return 0;
}
int get_wans_dualwan(void)
{
int caps=0;
char word[80], *next;
char *wancaps = nvram_get("wans_dualwan");
if(wancaps == NULL)
{
#ifdef RTCONFIG_DSL
caps = WANSCAP_DSL;
#elif defined(RTCONFIG_INTERNAL_GOBI) && defined(RTCONFIG_NO_WANPORT)
caps = WANSCAP_USB;
#else
caps = WANSCAP_WAN;
#endif
wancaps = DEF_SECOND_WANIF;
}
foreach(word, wancaps, next) {
if (!strcmp(word,"lan")) caps |= WANSCAP_LAN;
if (!strcmp(word,"2g")) caps |= WANSCAP_2G;
if (!strcmp(word,"5g")) caps |= WANSCAP_5G;
if (!strcmp(word,"usb")) caps |= WANSCAP_USB;
if (!strcmp(word,"dsl")) caps |= WANSCAP_DSL;
if (!strcmp(word,"wan")) caps |= WANSCAP_WAN;
if (!strcmp(word,"wan2")) caps |= WANSCAP_WAN2;
if (!strcmp(word,"sfp+")) caps |= WANSCAP_SFPP;
if (!strcmp(word,"6g")) caps |= WANSCAP_6G;
}
return caps;
}
int get_dualwan_by_unit(int unit)
{
int i;
char word[80], *next;
char *wans_dualwan = nvram_get("wans_dualwan");
if(wans_dualwan == NULL) //default value
{
wans_dualwan = nvram_default_get("wans_dualwan");
}
#ifdef RTCONFIG_MULTICAST_IPTV
if(unit == WAN_UNIT_IPTV)
return WAN_UNIT_IPTV;
if(unit == WAN_UNIT_VOIP)
return WAN_UNIT_VOIP;
#endif
i = 0;
foreach(word, wans_dualwan, next) {
if(i==unit
#ifdef RTCONFIG_MULTISERVICE_WAN
|| i == get_ms_base_unit(unit)
#endif
) {
if (!strcmp(word,"lan")) return WANS_DUALWAN_IF_LAN;
if (!strcmp(word,"2g")) return WANS_DUALWAN_IF_2G;
if (!strcmp(word,"5g")) return WANS_DUALWAN_IF_5G;
if (!strcmp(word,"usb")) return WANS_DUALWAN_IF_USB;
if (!strcmp(word,"dsl")) return WANS_DUALWAN_IF_DSL;
if (!strcmp(word,"wan")) return WANS_DUALWAN_IF_WAN;
if (!strcmp(word,"wan2")) return WANS_DUALWAN_IF_WAN2;
#ifdef RTCONFIG_USB_MULTIMODEM
if (!strcmp(word,"usb2")) return WANS_DUALWAN_IF_USB2;
#endif
if (!strcmp(word,"sfp+")) return WANS_DUALWAN_IF_SFPP;
return WANS_DUALWAN_IF_NONE;
}
i++;
}
return WANS_DUALWAN_IF_NONE;
}
int get_wanunit_by_type(int wan_type){
int unit;
for(unit = WAN_UNIT_FIRST; unit < WAN_UNIT_MAX; ++unit){
if(get_dualwan_by_unit(unit) == wan_type){
return unit;
}
}
return WAN_UNIT_NONE;
}
/* Return wan type string of @unit wan_unit.
* @return: pointer to a string.
* NULL: invalid parameter or unknown wan type.
*/
char *get_wantype_str_by_unit(int unit)
{
int type = get_dualwan_by_unit(unit);
if (unit < 0 || unit > ARRAY_SIZE(wantype_str))
return NULL;
return wantype_str[type];
}
// imply: unit 0: primary, unit 1: secondary
int get_dualwan_primary(void)
{
return get_dualwan_by_unit(0);
}
int get_dualwan_secondary(void)
{
return get_dualwan_by_unit(1);
}
/**
* Return total number of WAN unit.
* @return:
*/
int get_nr_wan_unit(void)
{