2Copyright 2018-2021 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
19 var wlanInterfaces = this.Marshal.CreatePointer();
20 this.Native.WlanEnumInterfaces(this.Handle, 0, wlanInterfaces);
22 var count = wlanInterfaces.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
24 var info = wlanInterfaces.Deref().Deref(8, 532);
25 var iname = info.Deref(16, 512).AnsiString;
28 switch (info.Deref(528, 4).toBuffer().readUInt32LE(0))
40 istate = "DISCONNECTING";
43 istate = "DISCONNECTED";
46 istate = "ASSOCIATING";
49 istate = "DISCOVERING";
52 istate = "AUTHENTICATING";
59 var iguid = info.Deref(0, 16);
60 if (this.Native.WlanScan(this.Handle, iguid, 0, 0, 0).Val == 0)
70function AccessPoint(_ssid, _bssid, _rssi, _lq)
77AccessPoint.prototype.toString = function()
79 return (this.ssid + " [" + this.bssid + "]: " + this.lq);
82function OnNotify(NotificationData)
84 var NotificationSource = NotificationData.Deref(0, 4).toBuffer().readUInt32LE(0);
85 var NotificationCode = NotificationData.Deref(4, 4).toBuffer().readUInt32LE(0);
86 var dataGuid = NotificationData.Deref(8, 16);
88 if ((NotificationSource & 0X00000008) && (NotificationCode == 7))
90 var bss = this.Parent.Marshal.CreatePointer();
91 var result = this.Parent.Native.GetBSSList(this.Parent.Handle, dataGuid, 0, 3, 0, 0, bss).Val;
94 var totalSize = bss.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
95 var numItems = bss.Deref().Deref(4, 4).toBuffer().readUInt32LE(0);
96 for (i = 0; i < numItems; ++i)
98 var item = bss.Deref().Deref(8 + (360 * i), 360);
99 var ssid = item.Deref(4, 32).String.trim();
100 var bssid = item.Deref(40, 6).HexString2;
101 var rssi = item.Deref(56, 4).toBuffer().readUInt32LE(0);
102 var lq = item.Deref(60, 4).toBuffer().readUInt32LE(0);
104 this.Parent.emit('Scan', new AccessPoint(ssid, bssid, rssi, lq));
113 var emitterUtils = require('events').inherits(this);
115 this.Marshal = require('_GenericMarshal');
116 this.Native = this.Marshal.CreateNativeProxy("wlanapi.dll");
117 this.Native.CreateMethod("WlanOpenHandle");
118 this.Native.CreateMethod("WlanGetNetworkBssList", "GetBSSList");
119 this.Native.CreateMethod("WlanRegisterNotification");
120 this.Native.CreateMethod("WlanEnumInterfaces");
121 this.Native.CreateMethod("WlanScan");
122 this.Native.CreateMethod("WlanQueryInterface");
124 var negotiated = this.Marshal.CreatePointer();
125 var h = this.Marshal.CreatePointer();
127 this.Native.WlanOpenHandle(2, 0, negotiated, h);
128 this.Handle = h.Deref();
130 this._NOTIFY_PROXY_OBJECT = this.Marshal.CreateCallbackProxy(OnNotify, 2);
131 this._NOTIFY_PROXY_OBJECT.Parent = this;
132 var PrevSource = this.Marshal.CreatePointer();
133 var result = this.Native.WlanRegisterNotification(this.Handle, 0X0000FFFF, 0, this._NOTIFY_PROXY_OBJECT.Callback, this._NOTIFY_PROXY_OBJECT.State, 0, PrevSource);
135 emitterUtils.createEvent('Scan');
136 emitterUtils.addMethod('Scan', _Scan);
138 this.GetConnectedNetwork = function ()
140 var interfaces = this.Marshal.CreatePointer();
142 console.log('Success = ' + this.Native.WlanEnumInterfaces(this.Handle, 0, interfaces).Val);
143 var count = interfaces.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
144 var info = interfaces.Deref().Deref(8, 532);
145 var iname = info.Deref(16, 512).AnsiString;
146 var istate = info.Deref(528, 4).toBuffer().readUInt32LE(0);
147 if(info.Deref(528, 4).toBuffer().readUInt32LE(0) == 1) // CONNECTED
149 var dataSize = this.Marshal.CreatePointer();
150 var pData = this.Marshal.CreatePointer();
151 var valueType = this.Marshal.CreatePointer();
152 var iguid = info.Deref(0, 16);
153 var retVal = this.Native.WlanQueryInterface(this.Handle, iguid, 7, 0, dataSize, pData, valueType).Val;
156 var associatedSSID = pData.Deref().Deref(524, 32).String;
157 var bssid = pData.Deref().Deref(560, 6).HexString;
158 var lq = pData.Deref().Deref(576, 4).toBuffer().readUInt32LE(0);
160 return (new AccessPoint(associatedSSID, bssid, 0, lq));
163 throw ("GetConnectedNetworks: FAILED (not associated to a network)");
170module.exports = new Wireless();