| 1 | /* |
| 2 | Copyright 2018-2021 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | function _Scan() |
| 18 | { |
| 19 | var wlanInterfaces = this.Marshal.CreatePointer(); |
| 20 | this.Native.WlanEnumInterfaces(this.Handle, 0, wlanInterfaces); |
| 21 | |
| 22 | var count = wlanInterfaces.Deref().Deref(0, 4).toBuffer().readUInt32LE(0); |
| 23 | |
| 24 | var info = wlanInterfaces.Deref().Deref(8, 532); |
| 25 | var iname = info.Deref(16, 512).AnsiString; |
| 26 | |
| 27 | var istate; |
| 28 | switch (info.Deref(528, 4).toBuffer().readUInt32LE(0)) |
| 29 | { |
| 30 | case 0: |
| 31 | istate = "NOT READY"; |
| 32 | break; |
| 33 | case 1: |
| 34 | istate = "CONNECTED"; |
| 35 | break; |
| 36 | case 2: |
| 37 | istate = "AD-HOC"; |
| 38 | break; |
| 39 | case 3: |
| 40 | istate = "DISCONNECTING"; |
| 41 | break; |
| 42 | case 4: |
| 43 | istate = "DISCONNECTED"; |
| 44 | break; |
| 45 | case 5: |
| 46 | istate = "ASSOCIATING"; |
| 47 | break; |
| 48 | case 6: |
| 49 | istate = "DISCOVERING"; |
| 50 | break; |
| 51 | case 7: |
| 52 | istate = "AUTHENTICATING"; |
| 53 | break; |
| 54 | default: |
| 55 | istate = "UNKNOWN"; |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | var iguid = info.Deref(0, 16); |
| 60 | if (this.Native.WlanScan(this.Handle, iguid, 0, 0, 0).Val == 0) |
| 61 | { |
| 62 | return (true); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | return (false); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function AccessPoint(_ssid, _bssid, _rssi, _lq) |
| 71 | { |
| 72 | this.ssid = _ssid; |
| 73 | this.bssid = _bssid; |
| 74 | this.rssi = _rssi; |
| 75 | this.lq = _lq; |
| 76 | } |
| 77 | AccessPoint.prototype.toString = function() |
| 78 | { |
| 79 | return (this.ssid + " [" + this.bssid + "]: " + this.lq); |
| 80 | } |
| 81 | |
| 82 | function OnNotify(NotificationData) |
| 83 | { |
| 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); |
| 87 | |
| 88 | if ((NotificationSource & 0X00000008) && (NotificationCode == 7)) |
| 89 | { |
| 90 | var bss = this.Parent.Marshal.CreatePointer(); |
| 91 | var result = this.Parent.Native.GetBSSList(this.Parent.Handle, dataGuid, 0, 3, 0, 0, bss).Val; |
| 92 | if (result == 0) |
| 93 | { |
| 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) |
| 97 | { |
| 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); |
| 103 | |
| 104 | this.Parent.emit('Scan', new AccessPoint(ssid, bssid, rssi, lq)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | function Wireless() |
| 112 | { |
| 113 | var emitterUtils = require('events').inherits(this); |
| 114 | |
| 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"); |
| 123 | |
| 124 | var negotiated = this.Marshal.CreatePointer(); |
| 125 | var h = this.Marshal.CreatePointer(); |
| 126 | |
| 127 | this.Native.WlanOpenHandle(2, 0, negotiated, h); |
| 128 | this.Handle = h.Deref(); |
| 129 | |
| 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); |
| 134 | |
| 135 | emitterUtils.createEvent('Scan'); |
| 136 | emitterUtils.addMethod('Scan', _Scan); |
| 137 | |
| 138 | this.GetConnectedNetwork = function () |
| 139 | { |
| 140 | var interfaces = this.Marshal.CreatePointer(); |
| 141 | |
| 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 |
| 148 | { |
| 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; |
| 154 | if (retVal == 0) |
| 155 | { |
| 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); |
| 159 | |
| 160 | return (new AccessPoint(associatedSSID, bssid, 0, lq)); |
| 161 | } |
| 162 | } |
| 163 | throw ("GetConnectedNetworks: FAILED (not associated to a network)"); |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | return (this); |
| 168 | } |
| 169 | |
| 170 | module.exports = new Wireless(); |