luci2.wireless: add getPhyName() rpc call
[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
67                         var phy_attrs = [
68                                 'country', 'channel', 'frequency', 'frequency_offset',
69                                 'txpower', 'txpower_offset', 'hwmodes', 'hardware', 'phy'
70                         ];
71
72                         var net_attrs = [
73                                 'ssid', 'bssid', 'mode', 'quality', 'quality_max',
74                                 'signal', 'noise', 'bitrate', 'encryption'
75                         ];
76
77                         for (var i = 0; i < networks.length; i++)
78                         {
79                                 var phy = rv[networks[i].phy] || (
80                                         rv[networks[i].phy] = { networks: [ ] }
81                                 );
82
83                                 var net = {
84                                         device: networks[i].device
85                                 };
86
87                                 for (var j = 0; j < phy_attrs.length; j++)
88                                         phy[phy_attrs[j]] = networks[i][phy_attrs[j]];
89
90                                 for (var j = 0; j < net_attrs.length; j++)
91                                         net[net_attrs[j]] = networks[i][net_attrs[j]];
92
93                                 phy.networks.push(net);
94                         }
95
96                         return rv;
97                 });
98         },
99
100         getAssocLists: function()
101         {
102                 return this.listDeviceNames().then(function(names) {
103                         L.rpc.batch();
104
105                         for (var i = 0; i < names.length; i++)
106                                 L.wireless.getAssocList(names[i]);
107
108                         return L.rpc.flush();
109                 }).then(function(assoclists) {
110                         var rv = [ ];
111
112                         for (var i = 0; i < assoclists.length; i++)
113                                 for (var j = 0; j < assoclists[i].length; j++)
114                                         rv.push(assoclists[i][j]);
115
116                         return rv;
117                 });
118         },
119
120         formatEncryption: function(enc)
121         {
122                 var format_list = function(l, s)
123                 {
124                         var rv = [ ];
125                         for (var i = 0; i < l.length; i++)
126                                 rv.push(l[i].toUpperCase());
127                         return rv.join(s ? s : ', ');
128                 }
129
130                 if (!enc || !enc.enabled)
131                         return L.tr('None');
132
133                 if (enc.wep)
134                 {
135                         if (enc.wep.length == 2)
136                                 return L.tr('WEP Open/Shared') + ' (%s)'.format(format_list(enc.ciphers, ', '));
137                         else if (enc.wep[0] == 'shared')
138                                 return L.tr('WEP Shared Auth') + ' (%s)'.format(format_list(enc.ciphers, ', '));
139                         else
140                                 return L.tr('WEP Open System') + ' (%s)'.format(format_list(enc.ciphers, ', '));
141                 }
142                 else if (enc.wpa)
143                 {
144                         if (enc.wpa.length == 2)
145                                 return L.tr('mixed WPA/WPA2') + ' %s (%s)'.format(
146                                         format_list(enc.authentication, '/'),
147                                         format_list(enc.ciphers, ', ')
148                                 );
149                         else if (enc.wpa[0] == 2)
150                                 return 'WPA2 %s (%s)'.format(
151                                         format_list(enc.authentication, '/'),
152                                         format_list(enc.ciphers, ', ')
153                                 );
154                         else
155                                 return 'WPA %s (%s)'.format(
156                                         format_list(enc.authentication, '/'),
157                                         format_list(enc.ciphers, ', ')
158                                 );
159                 }
160
161                 return L.tr('Unknown');
162         }
163 });