Connect wifi service with dbus-native

20 Views Asked by At

I am working on a project in the Node.js environment that via dbus-native interfaces with Connman. What I need to do is create some code that allows it to connect to a secure wifi network. I went to implement the Agent and the RequestInput function and from another file I go to call the Connect(), but once it is called it goes the loop and won't connect. I pass the passphrase statically from the code. Has anyone done something similar before?

Agent implementation:

   const dbus = require('dbus-native');
const systemBus = dbus.systemBus();
const passphrase = '123passwordExample';

const agent = {
        Release: function() {
                console.log('Agent released');
        },

        ReportError: function(path, error){
                console.log('Agent error reported: ', path, error);
        },
        RequestBrowser: function(path){
                console.log('Agent requests browser:', path);
        },
        RequestInput: function(path, fields, callback){
                console.log('Agent requests input:', path, fields);
                let response = {};
                console.log('fields[0][0]:', fields[0][0]);
                console.log('Ingresso if...');
                console.log(fields[0][0].toString() === 'Passphrase');
                if(fields[0][0].toString() === 'Passphrase'){
                        console.log(fields[0]);
                        response["Passphrase"] = passphrase;
                        console.log(response);
                        callback(response);
                }
                else{
                        console.log('If scartato');
                        callback({});
                }
                //return response;
        },
        Cancel: function(){
                console.log('Agent cancelled');
        }
};

systemBus.exportInterface(agent, '/net/connman/Agent', {
        name: 'net.connman.Agent',
        methods: agent,
        signals: {},
        properties: {}
});

const managerService = systemBus.getService('net.connman');
managerService.getInterface('/', 'net.connman.Manager', (err, manager) => {
        if(err){
                console.log('Error getting manager interfce:', err);
                return;
        }

        manager.RegisterAgent('/net/connman/Agent', function(err){
                if(err){
                        console.log('Error registering agent:', err);
                }
                else{
                        console.log('Agent registered');
                }
        });
}); 

Connect() call:

    
const dbus = require('dbus-native');
const systemBus = dbus.systemBus();

const wifiService = systemBus.getService('net.connman');
async function wifiConnect(){
        try{
                const wifiProps = await new Promise((resolve, reject) => {
                        wifiService.getInterface('/net/connman/service/wifi_ssid12345', 'net.connman.Service', (err, wifiProps) => {
                                if(err){
                                        console.log('Error getting wifi service interface:',err);
                                        reject(err);
                                        return;
                                }
                                resolve(wifiProps);
                        });
                });

                const props = await new Promise((resolve, reject) => {
                        wifiProps.GetProperties((err, props) => {
                                if(err){
                                        console.log('Error getting properties:', err);
                                        reject(err);
                                        return;
                                }
                                resolve(props);
                        });
                });
                const state = props[2][1][1][0].toString();
                console.log(state);
                if(state === 'idle'){
                        await new Promise((resolve, reject) => {
                                wifiProps.Connect(err => {
                                        if(err){
                                                console.log('Error connecting to wifi', err);                                               reject(err);
                                                return;
                                        }
                                        resolve();
                                });
                        });
                        return 'Connected';
                }
                else{
                        throw new Error('Already connect');
                }
        }
        catch(error){
                throw error;
        }
}
wifiConnect()
        .then(result => console.log(result))
        .catch(error => console.error(error));

This is the output error:

Error connecting to wifi [ 'Operation timeout' ]
0

There are 0 best solutions below