EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
wifi-scanner.js
Go to the documentation of this file.
1/*
2Copyright 2018-2021 Intel Corporation
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17var MemoryStream = require('MemoryStream');
18var WindowsChildScript = 'var parent = require("ScriptContainer");var Wireless = require("wifi-scanner-windows");Wireless.on("Scan", function (ap) { parent.send(ap); });Wireless.Scan();';
19
20
21function AccessPoint(_ssid, _bssid, _lq)
22{
23 this.ssid = _ssid;
24 this.bssid = _bssid;
25 this.lq = _lq;
26}
27AccessPoint.prototype.toString = function ()
28{
29 return ("[" + this.bssid + "]: " + this.ssid + " (" + this.lq + ")");
30 //return (this.ssid + " [" + this.bssid + "]: " + this.lq);
31}
32
33function WiFiScanner()
34{
35 var emitterUtils = require('events').inherits(this);
36 emitterUtils.createEvent('accessPoint');
37
38 this.hasWireless = function ()
39 {
40 var retVal = false;
41 var interfaces = require('os').networkInterfaces();
42 for (var name in interfaces)
43 {
44 if (interfaces[name][0].type == 'wireless') { retVal = true; break; }
45 }
46 return (retVal);
47 };
48
49 this.Scan = function ()
50 {
51 if (process.platform == 'win32')
52 {
53 this.main = require('ScriptContainer').Create(15, ContainerPermissions.DEFAULT);
54 this.main.parent = this;
55 this.main.on('data', function (j) { this.parent.emit('accessPoint', new AccessPoint(j.ssid, j.bssid, j.lq)); });
56
57 this.main.addModule('wifi-scanner-windows', getJSModule('wifi-scanner-windows'));
58 this.main.ExecuteString(WindowsChildScript);
59 }
60 else if (process.platform == 'linux')
61 {
62 // Need to get the wireless interface name
63 var interfaces = require('os').networkInterfaces();
64 var wlan = null;
65 for (var i in interfaces)
66 {
67 if (interfaces[i][0].type == 'wireless')
68 {
69 wlan = i;
70 break;
71 }
72 }
73 if (wlan != null)
74 {
75 this.child = require('child_process').execFile('/sbin/iwlist', ['iwlist', wlan, 'scan']);
76 this.child.parent = this;
77 this.child.ms = new MemoryStream();
78 this.child.ms.parent = this.child;
79 this.child.stdout.on('data', function (buffer) { this.parent.ms.write(buffer); });
80 this.child.on('exit', function () { this.ms.end(); });
81 this.child.ms.on('end', function ()
82 {
83 var str = this.buffer.toString();
84 tokens = str.split(' - Address: ');
85 for (var block in tokens)
86 {
87 if (block == 0) continue;
88 var ln = tokens[block].split('\n');
89 var _bssid = ln[0];
90 var _lq;
91 var _ssid;
92
93 for (var lnblock in ln)
94 {
95 lnblock = ln[lnblock].trim();
96 lnblock = lnblock.trim();
97 if (lnblock.startsWith('ESSID:'))
98 {
99 _ssid = lnblock.slice(7, lnblock.length - 1);
100 if (_ssid == '<hidden>') { _ssid = ''; }
101 }
102 if (lnblock.startsWith('Signal level='))
103 {
104 _lq = lnblock.slice(13,lnblock.length-4);
105 }
106 else if (lnblock.startsWith('Quality='))
107 {
108 _lq = lnblock.slice(8, 10);
109 var scale = lnblock.slice(11, 13);
110 }
111 }
112 this.parent.parent.emit('accessPoint', new AccessPoint(_ssid, _bssid, _lq));
113 }
114 });
115 }
116 }
117 }
118}
119
120module.exports = WiFiScanner;
121
122
123
124
125
126
127