luci-app-acme: convert to client side rendering
[project/luci.git] / protocols / luci-proto-modemmanager / htdocs / luci-static / resources / protocol / modemmanager.js
1 'use strict';
2 'require fs';
3 'require form';
4 'require network';
5
6 function getModemList() {
7 return fs.exec_direct('/usr/bin/mmcli', [ '-L' ]).then(function(res) {
8 var lines = (res || '').split(/\n/),
9 tasks = [];
10
11 for (var i = 0; i < lines.length; i++) {
12 var m = lines[i].match(/\/Modem\/(\d+)/);
13 if (m)
14 tasks.push(fs.exec_direct('/usr/bin/mmcli', [ '-m', m[1] ]));
15 }
16
17 return Promise.all(tasks).then(function(res) {
18 var modems = [];
19
20 for (var i = 0; i < res.length; i++) {
21 var man = res[i].match(/manufacturer: ([^\n]+)/),
22 mod = res[i].match(/model: ([^\n]+)/),
23 dev = res[i].match(/device: ([^\n]+)/);
24
25 if (dev) {
26 modems.push({
27 device: dev[1].trim(),
28 manufacturer: (man ? man[1].trim() : '') || '?',
29 model: (mod ? mod[1].trim() : '') || dev[1].trim()
30 });
31 }
32 }
33
34 return modems;
35 });
36 });
37 }
38
39 network.registerPatternVirtual(/^mobiledata-.+$/);
40 network.registerErrorCode('CALL_FAILED', _('Call failed'));
41 network.registerErrorCode('NO_CID', _('Unable to obtain client ID'));
42 network.registerErrorCode('PLMN_FAILED', _('Setting PLMN failed'));
43
44 return network.registerProtocol('modemmanager', {
45 getI18n: function() {
46 return _('ModemManager');
47 },
48
49 getIfname: function() {
50 return this._ubus('l3_device') || 'modemmanager-%s'.format(this.sid);
51 },
52
53 getOpkgPackage: function() {
54 return 'modemmanager';
55 },
56
57 isFloating: function() {
58 return true;
59 },
60
61 isVirtual: function() {
62 return true;
63 },
64
65 getDevices: function() {
66 return null;
67 },
68
69 containsDevice: function(ifname) {
70 return (network.getIfnameOf(ifname) == this.getIfname());
71 },
72
73 renderFormOptions: function(s) {
74 var dev = this.getL3Device() || this.getDevice(), o;
75
76 o = s.taboption('general', form.ListValue, 'device', _('Modem device'));
77 o.rmempty = false;
78 o.load = function(section_id) {
79 return getModemList().then(L.bind(function(devices) {
80 for (var i = 0; i < devices.length; i++)
81 this.value(devices[i].device,
82 '%s - %s'.format(devices[i].manufacturer, devices[i].model));
83 return form.Value.prototype.load.apply(this, [section_id]);
84 }, this));
85 };
86
87 s.taboption('general', form.Value, 'apn', _('APN'));
88 s.taboption('general', form.Value, 'pincode', _('PIN'));
89
90 o = s.taboption('general', form.ListValue, 'auth', _('Authentication Type'));
91 o.value('both', _('PAP/CHAP (both)'));
92 o.value('pap', 'PAP');
93 o.value('chap', 'CHAP');
94 o.value('none', _('None'));
95 o.default = 'none';
96
97 o = s.taboption('general', form.Value, 'username', _('PAP/CHAP username'));
98 o.depends('auth', 'pap');
99 o.depends('auth', 'chap');
100 o.depends('auth', 'both');
101
102 o = s.taboption('general', form.Value, 'password', _('PAP/CHAP password'));
103 o.depends('auth', 'pap');
104 o.depends('auth', 'chap');
105 o.depends('auth', 'both');
106 o.password = true;
107
108 o = s.taboption('general', form.ListValue, 'iptype', _('IP Type'));
109 o.value('ipv4v6', _('IPv4/IPv6 (both - defaults to IPv4)'))
110 o.value('ipv4', _('IPv4 only'));
111 o.value('ipv6', _('IPv6 only'));
112 o.default = 'ipv4v6';
113
114 o = s.taboption('advanced', form.Value, 'mtu', _('Override MTU'));
115 o.placeholder = dev ? (dev.getMTU() || '1500') : '1500';
116 o.datatype = 'max(9200)';
117
118 s.taboption('general', form.Value, 'metric', _('Gateway metric'));
119
120 }
121 });