Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / protocols / luci-proto-ppp / htdocs / luci-static / resources / protocol / ppp.js
1 'use strict';
2 'require rpc';
3 'require uci';
4 'require form';
5 'require network';
6
7 var callFileList = rpc.declare({
8         object: 'file',
9         method: 'list',
10         params: [ 'path' ],
11         expect: { entries: [] },
12         filter: function(list, params) {
13                 var rv = [];
14                 for (var i = 0; i < list.length; i++)
15                         if (list[i].name.match(/^tty[A-Z]/) || list[i].name.match(/^cdc-wdm/) || list[i].name.match(/^[0-9]+$/))
16                                 rv.push(params.path + list[i].name);
17                 return rv.sort();
18         }
19 });
20
21 network.registerPatternVirtual(/^ppp-.+$/);
22
23 function write_keepalive(section_id, value) {
24         var f_opt = this.map.lookupOption('_keepalive_failure', section_id),
25             i_opt = this.map.lookupOption('_keepalive_interval', section_id),
26             f = (f_opt != null) ? +f_opt[0].formvalue(section_id) : null,
27             i = (i_opt != null) ? +i_opt[0].formvalue(section_id) : null;
28
29         if (f == null || f == '' || isNaN(f))
30                 f = 0;
31
32         if (i == null || i == '' || isNaN(i) || i < 1)
33                 i = 1;
34
35         if (f > 0)
36                 uci.set('network', section_id, 'keepalive', '%d %d'.format(f, i));
37         else
38                 uci.unset('network', section_id, 'keepalive');
39 }
40
41 return network.registerProtocol('ppp', {
42         getI18n: function() {
43                 return _('PPP');
44         },
45
46         getIfname: function() {
47                 return this._ubus('l3_device') || 'ppp-%s'.format(this.sid);
48         },
49
50         getOpkgPackage: function() {
51                 return 'ppp';
52         },
53
54         isFloating: function() {
55                 return true;
56         },
57
58         isVirtual: function() {
59                 return true;
60         },
61
62         getDevices: function() {
63                 return null;
64         },
65
66         containsDevice: function(ifname) {
67                 return (network.getIfnameOf(ifname) == this.getIfname());
68         },
69
70         renderFormOptions: function(s) {
71                 var dev = this.getL3Device() || this.getDevice(), o;
72
73                 o = s.taboption('general', form.Value, 'device', _('Modem device'));
74                 o.rmempty = false;
75                 o.load = function(section_id) {
76                         return callFileList('/dev/').then(L.bind(function(devices) {
77                                 for (var i = 0; i < devices.length; i++)
78                                         this.value(devices[i]);
79                                 return callFileList('/dev/tts/');
80                         }, this)).then(L.bind(function(devices) {
81                                 for (var i = 0; i < devices.length; i++)
82                                         this.value(devices[i]);
83                                 return form.Value.prototype.load.apply(this, [section_id]);
84                         }, this));
85                 };
86
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                 if (L.hasSystemFeature('ipv6')) {
93                         o = s.taboption('advanced', form.ListValue, 'ipv6', _('Obtain IPv6-Address'), _('Enable IPv6 negotiation on the PPP link'));
94                         o.value('auto', _('Automatic'));
95                         o.value('0', _('Disabled'));
96                         o.value('1', _('Manual'));
97                         o.default = 'auto';
98                 }
99
100                 o = s.taboption('advanced', form.Flag, 'defaultroute', _('Use default gateway'), _('If unchecked, no default route is configured'));
101                 o.default = o.enabled;
102
103                 o = s.taboption('advanced', form.Flag, 'peerdns', _('Use DNS servers advertised by peer'), _('If unchecked, the advertised DNS server addresses are ignored'));
104                 o.default = o.enabled;
105
106                 o = s.taboption('advanced', form.DynamicList, 'dns', _('Use custom DNS servers'));
107                 o.depends('peerdns', '0');
108                 o.datatype = 'ipaddr';
109                 o.cast     = 'string';
110
111                 o = s.taboption('advanced', form.Value, 'metric', _('Use gateway metric'));
112                 o.placeholder = '0';
113                 o.datatype    = 'uinteger';
114
115                 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'));
116                 o.placeholder = '0';
117                 o.datatype    = 'uinteger';
118                 o.write       = write_keepalive;
119                 o.remove      = write_keepalive;
120                 o.cfgvalue = function(section_id) {
121                         var v = uci.get('network', section_id, 'keepalive');
122                         if (typeof(v) == 'string' && v != '') {
123                                 var m = v.match(/^(\d+)[ ,]\d+$/);
124                                 return m ? m[1] : v;
125                         }
126                 };
127
128                 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'));
129                 o.placeholder = '5';
130                 o.datatype    = 'min(1)';
131                 o.write       = write_keepalive;
132                 o.remove      = write_keepalive;
133                 o.cfgvalue = function(section_id) {
134                         var v = uci.get('network', section_id, 'keepalive');
135                         if (typeof(v) == 'string' && v != '') {
136                                 var m = v.match(/^\d+[ ,](\d+)$/);
137                                 return m ? m[1] : v;
138                         }
139                 };
140
141                 o = s.taboption('advanced', form.Value, 'demand', _('Inactivity timeout'), _('Close inactive connection after the given amount of seconds, use 0 to persist connection'));
142                 o.placeholder = '0';
143                 o.datatype    = 'uinteger';
144
145                 o = s.taboption('advanced', form.Value, 'mtu', _('Override MTU'));
146                 o.placeholder = dev ? (dev.getMTU() || '1500') : '1500';
147                 o.datatype    = 'max(9200)';
148         }
149 });