c724f85b4c555ab3abf721d5784838f4f5f2058e
[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 getPhyName: L.rpc.declare({
13 object: 'iwinfo',
14 method: 'phyname',
15 params: [ 'section' ],
16 expect: { 'phyname': '' }
17 }),
18
19 getDeviceStatus: L.rpc.declare({
20 object: 'iwinfo',
21 method: 'info',
22 params: [ 'device' ],
23 expect: { '': { } },
24 filter: function(data, params) {
25 if (!$.isEmptyObject(data))
26 {
27 data['device'] = params['device'];
28 return data;
29 }
30 return undefined;
31 }
32 }),
33
34 getAssocList: L.rpc.declare({
35 object: 'iwinfo',
36 method: 'assoclist',
37 params: [ 'device' ],
38 expect: { results: [ ] },
39 filter: function(data, params) {
40 for (var i = 0; i < data.length; i++)
41 data[i]['device'] = params['device'];
42
43 data.sort(function(a, b) {
44 if (a.bssid < b.bssid)
45 return -1;
46 else if (a.bssid > b.bssid)
47 return 1;
48 else
49 return 0;
50 });
51
52 return data;
53 }
54 }),
55
56 getWirelessStatus: function() {
57 return this.listDeviceNames().then(function(names) {
58 L.rpc.batch();
59
60 for (var i = 0; i < names.length; i++)
61 L.wireless.getDeviceStatus(names[i]);
62
63 return L.rpc.flush();
64 }).then(function(networks) {
65 var rv = { };
66 var net_by_devname = { };
67
68 var phy_attrs = [
69 'country', 'channel', 'frequency', 'frequency_offset',
70 'txpower', 'txpower_offset', 'hwmodes', 'hardware', 'phy'
71 ];
72
73 var net_attrs = [
74 'ssid', 'bssid', 'mode', 'quality', 'quality_max',
75 'signal', 'noise', 'bitrate', 'encryption'
76 ];
77
78 for (var i = 0; i < networks.length; i++)
79 {
80 var phy = rv[networks[i].phy] || (
81 rv[networks[i].phy] = { networks: [ ] }
82 );
83
84 var net = net_by_devname[networks[i].device] = {
85 device: networks[i].device
86 };
87
88 for (var j = 0; j < phy_attrs.length; j++)
89 phy[phy_attrs[j]] = networks[i][phy_attrs[j]];
90
91 for (var j = 0; j < net_attrs.length; j++)
92 net[net_attrs[j]] = networks[i][net_attrs[j]];
93
94 /* copy parent interface properties to wds interfaces */
95 if (net.device.match(/^(.+)\.sta\d+$/) &&
96 net_by_devname[RegExp.$1])
97 {
98 var pnet = net_by_devname[RegExp.$1];
99 for (var j = 0; j < net_attrs.length; j++)
100 if (typeof(networks[i][net_attrs[j]]) === 'undefined' ||
101 net_attrs[j] == 'encryption')
102 net[net_attrs[j]] = pnet[net_attrs[j]];
103 }
104
105 phy.networks.push(net);
106 }
107
108 return rv;
109 });
110 },
111
112 getAssocLists: function()
113 {
114 return this.listDeviceNames().then(function(names) {
115 L.rpc.batch();
116
117 for (var i = 0; i < names.length; i++)
118 L.wireless.getAssocList(names[i]);
119
120 return L.rpc.flush();
121 }).then(function(assoclists) {
122 var rv = [ ];
123
124 for (var i = 0; i < assoclists.length; i++)
125 for (var j = 0; j < assoclists[i].length; j++)
126 rv.push(assoclists[i][j]);
127
128 return rv;
129 });
130 },
131
132 formatEncryption: function(enc)
133 {
134 var format_list = function(l, s)
135 {
136 var rv = [ ];
137 for (var i = 0; i < l.length; i++)
138 rv.push(l[i].toUpperCase());
139 return rv.join(s ? s : ', ');
140 }
141
142 if (!enc || !enc.enabled)
143 return L.tr('None');
144
145 if (enc.wep)
146 {
147 if (enc.wep.length == 2)
148 return L.tr('WEP Open/Shared') + ' (%s)'.format(format_list(enc.ciphers, ', '));
149 else if (enc.wep[0] == 'shared')
150 return L.tr('WEP Shared Auth') + ' (%s)'.format(format_list(enc.ciphers, ', '));
151 else
152 return L.tr('WEP Open System') + ' (%s)'.format(format_list(enc.ciphers, ', '));
153 }
154 else if (enc.wpa)
155 {
156 if (enc.wpa.length == 2)
157 return L.tr('mixed WPA/WPA2') + ' %s (%s)'.format(
158 format_list(enc.authentication, '/'),
159 format_list(enc.ciphers, ', ')
160 );
161 else if (enc.wpa[0] == 2)
162 return 'WPA2 %s (%s)'.format(
163 format_list(enc.authentication, '/'),
164 format_list(enc.ciphers, ', ')
165 );
166 else
167 return 'WPA %s (%s)'.format(
168 format_list(enc.authentication, '/'),
169 format_list(enc.ciphers, ', ')
170 );
171 }
172
173 return L.tr('Unknown');
174 }
175 });