Skip to content

Commit

Permalink
WIP hotspot - closes krellian#59
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Oct 29, 2021
1 parent f9150d4 commit 4386c56
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 15 deletions.
8 changes: 5 additions & 3 deletions services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ var Services = {
// Start database and credential manager
database.start()
.then(() => credentials.start())
.catch((error) => {console.log(error)});
.catch((error) => {console.error(error)});

// Start network manager
network.start();
// Start network manager and create Wi-Fi access point
network.start()
.then(() => {network.createWifiAccessPoint('kiosk');})
.catch((error) => {console.error(error)});

// Start HTTP server
if (port == 'systemd' && process.env.LISTEN_FDS) {
Expand Down
91 changes: 79 additions & 12 deletions services/models/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,89 @@ const Network = {
/**
* Start network manager by opening connection to system bus and finding
* primary Wi-Fi adapter.
*
* @returns {Promise} Resolves on successfully finding Wi-Fi adapter.
*/
start: function() {
console.log('Starting network manager...');

try {
this.systemBus = DBus.getBus('system');
} catch (error) {
console.error('Failed to access system bus ' + error);
}
return new Promise((resolve, reject) => {
console.log('Starting network manager...');

this.getWiFiDevices().then((wifiDevices) => {
if(wifiDevices.length > 0) {
this.wifiAdapter = wifiDevices[0];
try {
this.systemBus = DBus.getBus('system');
} catch (error) {
console.error('Failed to access system bus ' + error);
reject();
}
}).catch((error) => {
console.error('Unable to find a Wi-Fi adapter: ' + error);

this.getWiFiDevices().then((wifiDevices) => {
if(wifiDevices.length > 0) {
this.wifiAdapter = wifiDevices[0];
resolve();
} else {
reject();
}
}).catch((error) => {
console.error('Unable to find a Wi-Fi adapter: ' + error);
reject();
});
});
},

/**
* Creates a Wi-Fi Access Point for clients to connect to in order to carry
* out first time setup.
*/
createWifiAccessPoint: function(ssid) {
return new Promise((resolve, reject) => {
this.systemBus.getInterface('org.freedesktop.NetworkManager',
'/org/freedesktop/NetworkManager',
'org.freedesktop.NetworkManager',
(error, interface) => {

if (error) {
reject(error);
}

// Convert SSID to an array of bytes
let ssidBytes = Buffer.from(ssid);

// Access point connection information
let connectionInfo = {
'802-11-wireless': {
'ssid': ssidBytes,
'mode': 'ap',
//'band': 'bg',
//'hidden': false
},
//'802-11-wireless-security': {
// 'key-mgmt': 'wpa-psk',
// 'psk': 'password',
//},
'connection': {
'id': ssid,
'autoconnect': false,
'type': '802-11-wireless',
//'inteface-name': 'wlan0',
},
'ipv4': {
'method': 'manual',
'address-data': {
'address': '192.168.2.1',
'prefix': 24,
}
}
};

interface.AddAndActivateConnection(connectionInfo, this.wifiAdapter, '/',
function(error, value) {
if (error) {
console.error(error);
reject(error);
}
resolve(value);
});

});
});
},

Expand Down

0 comments on commit 4386c56

Please sign in to comment.