luci2: split into submodules
[project/luci2/ui.git] / luci2 / htdocs / luci2 / wireless.js
1 Class.extend({
2 listDeviceNames: L.rpc.declare({
3 object: 'iwinfo',
4 method: 'devices',
5 expect: { 'devices': [ ] },
6 filter: function(data) {
7 data.sort();
8 return data;
9 }
10 }),
11
12 getDeviceStatus: L.rpc.declare({
13 object: 'iwinfo',
14 method: 'info',
15 params: [ 'device' ],
16 expect: { '': { } },
17 filter: function(data, params) {
18 if (!$.isEmptyObject(data))
19 {
20 data['device'] = params['device'];
21 return data;
22 }
23 return undefined;
24 }
25 }),
26
27 getAssocList: L.rpc.declare({
28 object: 'iwinfo',
29 method: 'assoclist',
30 params: [ 'device' ],
31 expect: { results: [ ] },
32 filter: function(data, params) {
33 for (var i = 0; i < data.length; i++)
34 data[i]['device'] = params['device'];
35
36 data.sort(function(a, b) {
37 if (a.bssid < b.bssid)
38 return -1;
39 else if (a.bssid > b.bssid)
40 return 1;
41 else
42 return 0;
43 });
44
45 return data;
46 }
47 }),
48
49 getWirelessStatus: function() {
50 return this.listDeviceNames().then(function(names) {
51 L.rpc.batch();
52
53 for (var i = 0; i < names.length; i++)
54 L.wireless.getDeviceStatus(names[i]);
55
56 return L.rpc.flush();
57 }).then(function(networks) {
58 var rv = { };
59
60 var phy_attrs = [
61 'country', 'channel', 'frequency', 'frequency_offset',
62 'txpower', 'txpower_offset', 'hwmodes', 'hardware', 'phy'
63 ];
64
65 var net_attrs = [
66 'ssid', 'bssid', 'mode', 'quality', 'quality_max',
67 'signal', 'noise', 'bitrate', 'encryption'
68 ];
69
70 for (var i = 0; i < networks.length; i++)
71 {
72 var phy = rv[networks[i].phy] || (
73 rv[networks[i].phy] = { networks: [ ] }
74 );
75
76 var net = {
77 device: networks[i].device
78 };
79
80 for (var j = 0; j < phy_attrs.length; j++)
81 phy[phy_attrs[j]] = networks[i][phy_attrs[j]];
82
83 for (var j = 0; j < net_attrs.length; j++)
84 net[net_attrs[j]] = networks[i][net_attrs[j]];
85
86 phy.networks.push(net);
87 }
88
89 return rv;
90 });
91 },
92
93 getAssocLists: function()
94 {
95 return this.listDeviceNames().then(function(names) {
96 L.rpc.batch();
97
98 for (var i = 0; i < names.length; i++)
99 L.wireless.getAssocList(names[i]);
100
101 return L.rpc.flush();
102 }).then(function(assoclists) {
103 var rv = [ ];
104
105 for (var i = 0; i < assoclists.length; i++)
106 for (var j = 0; j < assoclists[i].length; j++)
107 rv.push(assoclists[i][j]);
108
109 return rv;
110 });
111 },
112
113 formatEncryption: function(enc)
114 {
115 var format_list = function(l, s)
116 {
117 var rv = [ ];
118 for (var i = 0; i < l.length; i++)
119 rv.push(l[i].toUpperCase());
120 return rv.join(s ? s : ', ');
121 }
122
123 if (!enc || !enc.enabled)
124 return L.tr('None');
125
126 if (enc.wep)
127 {
128 if (enc.wep.length == 2)
129 return L.tr('WEP Open/Shared') + ' (%s)'.format(format_list(enc.ciphers, ', '));
130 else if (enc.wep[0] == 'shared')
131 return L.tr('WEP Shared Auth') + ' (%s)'.format(format_list(enc.ciphers, ', '));
132 else
133 return L.tr('WEP Open System') + ' (%s)'.format(format_list(enc.ciphers, ', '));
134 }
135 else if (enc.wpa)
136 {
137 if (enc.wpa.length == 2)
138 return L.tr('mixed WPA/WPA2') + ' %s (%s)'.format(
139 format_list(enc.authentication, '/'),
140 format_list(enc.ciphers, ', ')
141 );
142 else if (enc.wpa[0] == 2)
143 return 'WPA2 %s (%s)'.format(
144 format_list(enc.authentication, '/'),
145 format_list(enc.ciphers, ', ')
146 );
147 else
148 return 'WPA %s (%s)'.format(
149 format_list(enc.authentication, '/'),
150 format_list(enc.ciphers, ', ')
151 );
152 }
153
154 return L.tr('Unknown');
155 }
156 });