Search code examples
node.jswpawpa-supplicant

getting list of wpa_supplicant interfaces in node.js


My eventual goal is to create a module that can tell me why setup of a wireless connection failed.

Currently I am trying to access the wlan0 interface using the node-dbus module. As it stands wpa_supplicant tells me it is not aware of wlan0

'wpa_supplicant knows nothing about this interface.'

Any help or suggestions are greatly appreciated.

code:

var dbus = require('dbus-native');
var util = require('util');

var bus = dbus.systemBus();
var wpas = bus.getService('fi.w1.wpa_supplicant1');

var wpai = wpas.getInterface('/fi/w1/wpa_supplicant1'
    , 'fi.w1.wpa_supplicant1', function (err, iface) {
        //console.log(err, iface);

        iface.on('PropertiesChanged', function(dict) {
            console.log('interface properties have changed!');
            console.log(dict);
        });

        iface.on('InterfaceAdded', function(path, dict) {
            console.log('interface has been added!');
            console.log(path, dict);
        });

        iface.on('InterfaceRemoved', function(path) {
            console.log('interface has been removed!');
            console.log(path);
        });

        iface.GetInterface('wlan0', function (err, iface2) {
            console.log(err, iface2);
        });

        console.log(util.inspect(iface, true, 3));

    });

Update 1:

I used the DBus properties api to investigate the Interfaces properties and discovered that the the property itself was null.

wpas.getInterface('/fi/w1/wpa_supplicant1', 'org.freedesktop.DBus.Properties', function(err, device) {
                device.GetAll('fi.w1.wpa_supplicant1', function(err, prop) {
                    var props = arrToMap(prop);
                    console.log(err,props);
                });
            });

function arrToMap(arr) {
    var output = {};
    for (var i = 0; i < arr.length; i++) {
        output[arr[i][0]] = arr[i][1][1][0];
    }
    return output;
}

My only conclusion is that the wpa_supplicant never registers any new interfaces with the dbus.

(I have ensured that I set up my wlan0 using wpa_supplicant using terminal commands)


Solution

  • I managed to fix my problem by rewriting the code above using promises. It is also important to note that the above GetInterface only returns the wpa_supplicant adapter.

    An additional call on this object with GetInterface is needed to get the actual wlan0 interface.

    Should anyone run into the error 'wpa_supplicant cannot grab this interface' try removing the wlan0 file at: /run/wpa_supplicant/wlan0 (if any)