From 70f30656a44d27b908e3cbc6ff96ecc1849e90de Mon Sep 17 00:00:00 2001 From: Zhiduo Zhang Date: Fri, 24 Nov 2017 01:52:40 +1100 Subject: [PATCH] WIP Add some test runs. Further work on dual interface --- app/api.js | 13 ++-- app/public/app.js | 3 - app/wifi_manager.js | 76 ++++++++++++++----- .../etc/{hostapd => }/hostapd.conf.template | 0 config.json | 4 +- run.js | 37 +++++---- test_runs/check_connection.js | 73 ++++++++++++++++++ test_runs/http_server.js | 52 +++++++++++++ test.js => test_runs/start_ap.js | 30 +++----- 9 files changed, 222 insertions(+), 66 deletions(-) rename assets/etc/{hostapd => }/hostapd.conf.template (100%) create mode 100644 test_runs/check_connection.js create mode 100644 test_runs/http_server.js rename test.js => test_runs/start_ap.js (67%) diff --git a/app/api.js b/app/api.js index 60738c1..6f66f49 100644 --- a/app/api.js +++ b/app/api.js @@ -61,14 +61,13 @@ module.exports = function(wifi_manager, callback) { if (error) { console.log("Enable Wifi ERROR: " + error); console.log("Attempt to re-enable AP mode"); - wifi_manager.enable_ap_mode(config.access_point.ssid, function(error) { - console.log("... AP mode reset"); - }); - response.redirect("/"); - } + log_error_send_success_with("Failed to connect to network", error, response); + } else { // Success! - exit - console.log("Wifi Enabled! - Exiting"); - process.exit(0); + console.log("Wifi Enabled! - Exiting"); + wifi_manager.disable_ap_mode(); + log_error_send_success_with("Success!", error, response); + } }); }); diff --git a/app/public/app.js b/app/public/app.js index 8870fb3..31944d9 100644 --- a/app/public/app.js +++ b/app/public/app.js @@ -31,9 +31,6 @@ app.controller("AppController", ["PiManager", "$scope", "$location", "$timeout", return parseInt(cell.signal_strength); } - $scope.foo = function() { console.log("foo"); } - $scope.bar = function() { console.log("bar"); } - // Scope function definitions $scope.rescan = function() { $scope.scan_results = []; diff --git a/app/wifi_manager.js b/app/wifi_manager.js index f5f8317..df3cd94 100644 --- a/app/wifi_manager.js +++ b/app/wifi_manager.js @@ -190,7 +190,7 @@ module.exports = function() { function update_interfaces(next_step) { write_template_to_file( - "./assets/etc/network/interfaces.d/ap.template", + config.root_dir+"/assets/etc/network/interfaces.d/ap.template", "/etc/network/interfaces.d/ap", context, next_step); }, @@ -199,7 +199,7 @@ module.exports = function() { function update_dhcpd(next_step) { // We must enable this to turn on the access point write_template_to_file( - "./assets/etc/dhcp/dhcpd.conf.template", + config.root_dir+"/assets/etc/dhcp/dhcpd.conf.template", "/etc/dhcp/dhcpd.conf", context, next_step); }, @@ -207,7 +207,7 @@ module.exports = function() { // Enable the interface in the dhcp server function update_dhcp_interface(next_step) { write_template_to_file( - "./assets/etc/default/isc-dhcp-server.template", + config.root_dir+"/assets/etc/default/isc-dhcp-server.template", "/etc/default/isc-dhcp-server", context, next_step); }, @@ -215,20 +215,20 @@ module.exports = function() { // Enable hostapd.conf file function update_hostapd_conf(next_step) { write_template_to_file( - "./assets/etc/hostapd.conf.template", + config.root_dir+"/assets/etc/hostapd.conf.template", "/etc/hostapd.conf", context, next_step); }, function update_hostapd_default(next_step) { write_template_to_file( - "./assets/etc/default/hostapd.template", + config.root_dir+"/assets/etc/default/hostapd.template", "/etc/default/hostapd", context, next_step); }, function start_ap(next_step) { - exec("sudo bash start_ap.sh", function(error, stdout, stderr) { + exec("sudo bash " + config.root_dir+"/start_ap.sh", function(error, stdout, stderr) { console.log(stdout); if (!error) console.log("... ap started!"); else console.log("... ap start failed! " + stderr); @@ -242,6 +242,26 @@ module.exports = function() { // Disables AP mode _enable_wifi_mode = function(connection_info, callback) { _is_wifi_enabled(function(error, result_ip) { + check_connection = function(time){ + if(!time) time=console.time(); + + //Timeout set to 10sec + is_wifi_enabled(function(error,result_ip){ + if(result_ip){ + console.log("\nWifi connection is enabled with IP: " + result_ip); + return callback(null); + } else { + if(error) console.log("Error checking wifi connection: " + error); + + if(console.time - time > 60000){ + return callback("Failed to connect"); + } else { + setTimeout(check_connection(time), 5000); + } + } + }); + } + if (error) return callback(error); if (result_ip) { @@ -253,41 +273,59 @@ module.exports = function() { // Update /etc/wpa_supplicant/wpa_supplicant.conf with correct info... function update_interfaces(next_step) { write_template_to_file( - "./assets/etc/wpa_supplicant/wpa_supplicant.conf.template", + config.root_dir+"/assets/etc/wpa_supplicant/wpa_supplicant.conf.template", "/etc/wpa_supplicant/wpa_supplicant.conf", connection_info, next_step); }, function disable_interface(next_step) { - exec("iw dev " + config.ap_interface + " del", function(error,stdout, stderr){ + exec("sudo ifdown " + config.ap_interface, function(error,stdout, stderr){ if(!error) console.log("... " + config.ap_interface + " interface shutdown"); else console.log("Failed to shutdown " + config.ap_interface + " interface" + error + stderr); - //next_step(); + next_step(); }); - } + }, - //Don't bother to reboot network interfaces - //function reboot_network_interfaces(next_step) { - // _reboot_wireless_network(config.wifi_interface, next_step); - //}, - ], callback); + function reboot_network_interfaces(next_step) { + _reboot_wireless_network(config.wifi_interface, next_step); + }, + ], check_connection); }); }; + _disable_ap_mode = function() { + _is_wifi_enabled(function(error, result_ip) { + if (error) return callback(error); + + if (result_ip) { + console.log("\nWifi connection is enabled with IP: " + result_ip); + } + + exec("sudo ifdown " + config.ap_interface, function(error,stdout, stderr){ + if(!error) + console.log("... " + config.ap_interface + " interface shutdown"); + else + console.log("Failed to shutdown " + config.ap_interface + " interface" + error + stderr); + }); + }); + }; + return { - get_wifi_info: _get_interface_info, + get_wifi_info: _get_interface_info, reboot_wireless_network: _reboot_wireless_network, - is_wifi_enabled: _is_wifi_enabled, + is_wifi_enabled: _is_wifi_enabled, is_wifi_enabled_sync: _is_wifi_enabled_sync, - is_ap_enabled: _is_ap_enabled, - is_ap_enabled_sync: _is_ap_enabled_sync, + is_ap_enabled: _is_ap_enabled, + is_ap_enabled_sync: _is_ap_enabled_sync, enable_ap_mode: _enable_ap_mode, enable_wifi_mode: _enable_wifi_mode, + + disable_ap_mode: _disable_ap_mode }; } diff --git a/assets/etc/hostapd/hostapd.conf.template b/assets/etc/hostapd.conf.template similarity index 100% rename from assets/etc/hostapd/hostapd.conf.template rename to assets/etc/hostapd.conf.template diff --git a/config.json b/config.json index c6fab0f..9e02a18 100644 --- a/config.json +++ b/config.json @@ -21,5 +21,7 @@ "server": { "port": 88 - } + }, + + "root_dir": "/home/pi/raspberry-wifi-conf" } diff --git a/run.js b/run.js index 7c56c70..ab1733e 100644 --- a/run.js +++ b/run.js @@ -3,6 +3,7 @@ var async = require("async"), dependency_manager = require("./app/dependency_manager")(), config = require("./config.json"); wifi_control = require("wifi-control"); + fs = require("fs"); /*****************************************************************************\ 1. Check for dependencies @@ -32,22 +33,28 @@ async.series([ }); }, - // 2. Check if wifi is enabled / connected + // 2. Check if wifi is enabled / connected function test_is_wifi_enabled(next_step) { - wifi_manager.is_wifi_enabled(function(error, result_ip) { - if (result_ip) { - console.log("\nWifi is enabled, and IP " + result_ip + " assigned"); - var reconfigure = config.access_point.force_reconfigure || false; - if (reconfigure) { - console.log("\nForce reconfigure enabled - try to enable access point"); - } else { - process.exit(0); - } - } else { - console.log("\nWifi is not enabled, Enabling AP for self-configure"); - } - next_step(error); - }); + if(fs.existsSync("/etc/wpa_supplicant/wpa_supplicant.conf")){ + setTimeout(function(){ + wifi_manager.is_wifi_enabled(function(error, result_ip) { + if (result_ip) { + console.log("\nWifi is enabled, and IP " + result_ip + " assigned"); + var reconfigure = config.access_point.force_reconfigure || false; + if (reconfigure) { + console.log("\nForce reconfigure enabled - try to enable access point"); + } else { + //process.exit(0); + } + } else { + console.log("\nWifi is not enabled, Enabling AP for self-configure"); + } + next_step(); + }); + }, 10000); + } else { + next_step(error); + } }, // 3. Turn RPI into an access point diff --git a/test_runs/check_connection.js b/test_runs/check_connection.js new file mode 100644 index 0000000..66ad280 --- /dev/null +++ b/test_runs/check_connection.js @@ -0,0 +1,73 @@ +var async = require("async"), + wifi_manager = require("../app/wifi_manager")(), + dependency_manager = require("../app/dependency_manager")(), + config = require("../config.json"); + fs = require("fs"); + +/*****************************************************************************\ + 1. Check for dependencies + 2. Check to see if we are connected to a wifi AP + 3. If connected to a wifi, do nothing -> exit + 4. Convert RPI to act as a AP (with a configurable SSID) + 5. Host a lightweight HTTP server which allows for the user to connect and + configure the RPIs wifi connection. The interfaces exposed are RESTy so + other applications can similarly implement their own UIs around the + data returned. + 6. Once the RPI is successfully configured, reset it to act as a wifi + device (not AP anymore), and setup its wifi network based on what the + user picked. + 7. At this stage, the RPI is named, and has a valid wifi connection which + its bound to, reboot the pi and re-run this script on startup. +\*****************************************************************************/ +async.series([ + + // 1. Check if we have the required dependencies installed + function test_deps(next_step) { + dependency_manager.check_deps({ + "binaries": ["dhcpd", "hostapd", "iw"], + "files": ["/etc/init.d/isc-dhcp-server"] + }, function(error) { + if (error) console.log(" * Dependency error, did you run `sudo npm run-script provision`?"); + next_step(error); + }); + }, + + // 2. Check if wifi is enabled / connected + function test_is_wifi_enabled(next_step) { + if(fs.existsSync("/etc/wpa_supplicant/wpa_supplicant.conf")){ + setTimeout(function(){ + wifi_manager.is_wifi_enabled(function(error, result_ip) { + if (result_ip) { + console.log("\nWifi is enabled, and IP " + result_ip + " assigned"); + var reconfigure = config.access_point.force_reconfigure || false; + if (reconfigure) { + console.log("\nForce reconfigure enabled - try to enable access point"); + } else { + //process.exit(0); + } + } else { + console.log("\nWifi is not enabled, Enabling AP for self-configure"); + } + next_step(); + }); + }, 10000); + } else { + next_step(error); + } + }, + + function test_is_ap_enabled(next_step){ + wifi_manager.is_ap_enabled(function(error, result) { + if(error || result==null){ + console.log("AP is not enabled: " + error); + } else { + console.log("AP is enabled: " + result); + } + next_step(error); + }); + } +], function(error) { + if (error) { + console.log("ERROR: " + error); + } +}); diff --git a/test_runs/http_server.js b/test_runs/http_server.js new file mode 100644 index 0000000..d9329ec --- /dev/null +++ b/test_runs/http_server.js @@ -0,0 +1,52 @@ +var async = require("async"), + wifi_manager = require("../app/wifi_manager")(), + dependency_manager = require("../app/dependency_manager")(), + config = require("../config.json"); + wifi_control = require("wifi-control"); + fs = require("fs"); + +/*****************************************************************************\ + 1. Check for dependencies + 5. Host a lightweight HTTP server which allows for the user to connect and + configure the RPIs wifi connection. The interfaces exposed are RESTy so + other applications can similarly implement their own UIs around the + data returned. +\*****************************************************************************/ +async.series([ + + // 1. Check if we have the required dependencies installed + function test_deps(next_step) { + dependency_manager.check_deps({ + "binaries": ["dhcpd", "hostapd", "iw"], + "files": ["/etc/init.d/isc-dhcp-server"] + }, function(error) { + if (error) console.log(" * Dependency error, did you run `sudo npm run-script provision`?"); + next_step(error); + }); + }, + + function test_is_ap_enabled(next_step){ + wifi_manager.is_ap_enabled(function(error, result) { + if(error || result==null){ + console.log("AP is not enabled: " + error); + process.exit(0); + } else { + console.log("AP is enabled: " + result); + } + next_step(error); + }); + }, + + // 4. Host HTTP server while functioning as AP, the "api.js" + // file contains all the needed logic to get a basic express + // server up. It uses a small angular application which allows + // us to choose the wifi of our choosing. + function start_http_server(next_step) { + console.log("\nHTTP server running..."); + require("../app/api.js")(wifi_manager, next_step); + } +], function(error) { + if (error) { + console.log("ERROR: " + error); + } +}); diff --git a/test.js b/test_runs/start_ap.js similarity index 67% rename from test.js rename to test_runs/start_ap.js index 81fcadd..d272959 100644 --- a/test.js +++ b/test_runs/start_ap.js @@ -1,7 +1,7 @@ var async = require("async"), - wifi_manager = require("./app/wifi_manager")(), - dependency_manager = require("./app/dependency_manager")(), - config = require("./config.json"); + wifi_manager = require("../app/wifi_manager")(), + dependency_manager = require("../app/dependency_manager")(), + config = require("../config.json"); /*****************************************************************************\ 1. Check for dependencies @@ -31,29 +31,17 @@ async.series([ }); }, - // 2. Check if wifi is enabled / connected - function test_is_wifi_enabled(next_step) { - wifi_manager.is_wifi_enabled(function(error, result_ip) { - if (result_ip) { - console.log("\nWifi is enabled, and IP " + result_ip + " assigned"); + // 3. Turn RPI into an access point + function enable_rpi_ap(next_step) { + wifi_manager.enable_ap_mode(config.access_point.ssid, function(error) { + if(error) { + console.log("... AP Enable ERROR: " + error); } else { - console.log("\nWifi is not enabled"); + console.log("... AP Enable Success!"); } next_step(error); }); }, - - function test_is_ap_enabled(next_step){ - wifi_manager.is_ap_enabled(function(error, result) { - if(error || result==null){ - console.log("AP is not enabled: " + error); - } else { - console.log("AP is enabled: " + result); - } - next_step(error); - }); - } - ], function(error) { if (error) { console.log("ERROR: " + error);