protocols: add client side protocol handler implementations
[project/luci.git] / protocols / luci-proto-3g / htdocs / luci-static / resources / protocol / 3g.js
1 'use strict';
2 'require rpc';
3 'require uci';
4 'require form';
5 'require network';
6
7 var callTTYDevices = rpc.declare({
8 object: 'luci',
9 method: 'getTTYDevices',
10 params: [ 'with_cdc', 'with_tts' ],
11 expect: { result: [] }
12 });
13
14 network.registerPatternVirtual(/^3g-.+$/);
15
16 function write_keepalive(section_id, value) {
17 var f_opt = this.map.lookupOption('_keepalive_failure', section_id),
18 i_opt = this.map.lookupOption('_keepalive_interval', section_id),
19 f = (f_opt != null) ? +f_opt[0].formvalue(section_id) : null,
20 i = (i_opt != null) ? +i_opt[0].formvalue(section_id) : null;
21
22 if (f == null || f == '' || isNaN(f))
23 f = 0;
24
25 if (i == null || i == '' || isNaN(i) || i < 1)
26 i = 1;
27
28 if (f > 0)
29 uci.set('network', section_id, 'keepalive', '%d %d'.format(f, i));
30 else
31 uci.unset('network', section_id, 'keepalive');
32 }
33
34 return network.registerProtocol('3g', {
35 getI18n: function() {
36 return _('UMTS/GPRS/EV-DO');
37 },
38
39 getIfname: function() {
40 return this._ubus('l3_device') || '3g-%s'.format(this.sid);
41 },
42
43 getOpkgPackage: function() {
44 return 'comgt';
45 },
46
47 isFloating: function() {
48 return true;
49 },
50
51 isVirtual: function() {
52 return true;
53 },
54
55 getDevices: function() {
56 return null;
57 },
58
59 containsDevice: function(ifname) {
60 return (network.getIfnameOf(ifname) == this.getIfname());
61 },
62
63 renderFormOptions: function(s) {
64 var o;
65
66 o = s.taboption('general', form.Value, 'device', _('Modem device'));
67 o.rmempty = false;
68 o.load = function(section_id) {
69 return callTTYDevices(false, true).then(L.bind(function(devices) {
70 if (Array.isArray(devices))
71 for (var i = 0; i < devices.length; i++)
72 this.value(devices[i]);
73
74 return form.Value.prototype.load.apply(this, [section_id]);
75 }, this));
76 };
77
78 o = s.taboption('general', form.Value, 'service', _('Service Type'));
79 o.value('', _('-- Please choose --'));
80 o.value('umts', 'UMTS/GPRS');
81 o.value('umts_only', _('UMTS only'));
82 o.value('gprs_only', _('GPRS only'));
83 o.value('evdo', 'CDMA/EV-DO');
84
85 s.taboption('general', form.Value, 'apn', _('APN'));
86 s.taboption('general', form.Value, 'pincode', _('PIN'));
87 s.taboption('general', form.Value, 'username', _('PAP/CHAP username'));
88
89 o = s.taboption('general', form.Value, 'password', _('PAP/CHAP password'));
90 o.password = true;
91
92 o = s.taboption('general', form.Value, 'dialnumber', _('Dial number'));
93 o.placeholder = '*99***1#';
94
95 if (L.hasSystemFeature('ipv6')) {
96 o = s.taboption('advanced', form.ListValue, 'ipv6', _('Obtain IPv6-Address'));
97 o.value('auto', _('Automatic'));
98 o.value('0', _('Disabled'));
99 o.value('1', _('Manual'));
100 o.default = 'auto';
101 }
102
103 o = s.taboption('advanced', form.Value, 'delay', _('Modem init timeout'), _('Maximum amount of seconds to wait for the modem to become ready'));
104 o.placeholder = '10';
105 o.datatype = 'min(1)';
106
107 o = s.taboption('advanced', form.Flag, 'defaultroute', _('Use default gateway'), _('If unchecked, no default route is configured'));
108 o.default = o.enabled;
109
110 o = s.taboption('advanced', form.Value, 'metric', _('Use gateway metric'));
111 o.placeholder = '0';
112 o.datatype = 'uinteger';
113 o.depends('defaultroute', '1');
114
115 o = s.taboption('advanced', form.Flag, 'peerdns', _('Use DNS servers advertised by peer'), _('If unchecked, the advertised DNS server addresses are ignored'));
116 o.default = o.enabled;
117
118 o = s.taboption('advanced', form.DynamicList, 'dns', _('Use custom DNS servers'));
119 o.depends('peerdns', '0');
120 o.datatype = 'ipaddr';
121
122 o = s.taboption('advanced', form.Value, '_keepalive_failure', _('LCP echo failure threshold'), _('Presume peer to be dead after given amount of LCP echo failures, use 0 to ignore failures'));
123 o.placeholder = '0';
124 o.datatype = 'uinteger';
125 o.write = write_keepalive;
126 o.remove = write_keepalive;
127 o.cfgvalue = function(section_id) {
128 var v = uci.get('network', section_id, 'keepalive');
129 if (typeof(v) == 'string' && v != '') {
130 var m = v.match(/^(\d+)[ ,]\d+$/);
131 return m ? m[1] : v;
132 }
133 };
134
135 o = s.taboption('advanced', form.Value, '_keepalive_interval', _('LCP echo interval'), _('Send LCP echo requests at the given interval in seconds, only effective in conjunction with failure threshold'));
136 o.placeholder = '5';
137 o.datatype = 'min(1)';
138 o.write = write_keepalive;
139 o.remove = write_keepalive;
140 o.cfgvalue = function(section_id) {
141 var v = uci.get('network', section_id, 'keepalive');
142 if (typeof(v) == 'string' && v != '') {
143 var m = v.match(/^\d+[ ,](\d+)$/);
144 return m ? m[1] : v;
145 }
146 };
147
148 o = s.taboption('advanced', form.Value, 'demand', _('Inactivity timeout'), _('Close inactive connection after the given amount of seconds, use 0 to persist connection'));
149 o.placeholder = '0';
150 o.datatype = 'uinteger';
151 }
152 });