diff --git a/README.md b/README.md index 27f63ba..28b8201 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ Resources --------- * [FlashAir Developper Home](https://flashair-developers.com/) -* [FlashAir Developpers Home](https://flashair-developers.com/en/support/forum/) +* [FlashAir Developpers Forum](https://flashair-developers.com/en/support/forum/) * [FlashAir StackOverflow channel](http://stackoverflow.com/questions/tagged/flashair) License diff --git a/bin/cli.js b/bin/cli.js index 1ba10f3..8136daf 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -13,7 +13,8 @@ try { ssid: argv['card-ssid'], key: argv['card-key'] } - flashair.ini.runInPassThroughMode(homeNet, flashairNet).save() + flashair.config.runInPassThroughMode(homeNet, flashairNet) + flashair.config.save() } else if (argv._[0] === 'ls') { flashair.command.getFileList(argv._[1] || '/', function (err, data) { if (err) { diff --git a/lib.js b/lib.js index e7a94bd..d05da04 100644 --- a/lib.js +++ b/lib.js @@ -14,7 +14,7 @@ var APPMODE = 'STA' // or AP or IPT var Libs = require('./libs') var hostname = require('os').hostname() -module.exports = function (appname, appmode, configPath) { +module.exports = function (appname, appmode) { // API End-point var endpoint = appname || APPNAME APPMODE = appmode || APPMODE @@ -24,7 +24,5 @@ module.exports = function (appname, appmode, configPath) { endpoint += '.local' } - return new Libs(endpoint, configPath) + return new Libs(endpoint) } - -console.log(12) diff --git a/libs/index.js b/libs/index.js index f54c436..7f0c517 100644 --- a/libs/index.js +++ b/libs/index.js @@ -6,17 +6,30 @@ * Module name : Library * Description : */ +var debug = require('debug')('flashair:libs') var Command = require('./command') var Config = require('./config') -var Ini = require('./ini') +var LocalConfig = require('./local-config') var Thumbnail = require('./thumbnail') exports = module.exports = Libs function Libs (endpoint, configPath) { - this.command = new Command(endpoint) - this.config = new Config(endpoint) - this.ini = new Ini(configPath) - this.thumbnail = new Thumbnail(endpoint) -}; + try { + // Local configuration file? config use-case + this.config = new LocalConfig() + debug('using local-config') + } catch (e) { + if (!e.code || (e.code !== 'ENOENT' && e.code !== 'ENOTDIR')) { + // Unknown error: real one + throw e + } else { + debug('using remote-ops') + // Missing config file or config directory: run-time use-case + this.command = new Command(endpoint) + this.config = new Config(endpoint) + this.thumbnail = new Thumbnail(endpoint) + } + } +} diff --git a/libs/local-config.js b/libs/local-config.js new file mode 100644 index 0000000..3028a32 --- /dev/null +++ b/libs/local-config.js @@ -0,0 +1,147 @@ +/** + * Parse & Write the content of /SD_WLAN/CONFIG + */ + +var fs = require('fs') +var path = require('path') +var ini = require('ini') +var debug = require('debug')('flashair:local-config') + +var volumePath = process.platform === 'darwin' ? '/Volumes/NO NAME' : undefined + +var defaultConfigPath = 'SD_WLAN/CONFIG' + +// https://flashair-developers.com/en/documents/api/config/#APPMODE + +var appModes = { + 'AP': 4, + 'STA': 5, + 'IPT': 6 +} + +module.exports = LocalConfig + +function LocalConfig (configPath) { + configPath = configPath || path.join(volumePath, defaultConfigPath) + + this.load = function () { + debug('loading hwConfig from "%s"', configPath) + this._hwConfig = ini.parse(fs.readFileSync(configPath, 'utf-8')) + debug('loaded hwConfig: %O', this._hwConfig) + } + + this.load() + + this.save = function () { + var LocalConfig = ini.encode(this._hwConfig, { + whitespace: false, + newline: true, + platform: 'win32' + }) + debug('saving hwConfig %O as "%s"', LocalConfig, configPath) + fs.writeFileSync(configPath, LocalConfig, 'utf-8') + } +} + +LocalConfig.prototype.runAsAccessPoint = function _runAsAccessPoint (flashairNet) { + debug('runAsAccessPoint: flashairNet: %s', flashairNet) + _checkNetwork(flashairNet) + var hwConfig = { + Vendor: { + APPMODE: appModes.AP, + APPSSID: flashairNet.ssid, + APPNETWORKKEY: flashairNet.key + } + } + debug('runAsAccessPoint: Setting hw: %O', hwConfig) + this._hwConfig.Vendor = hwConfig.Vendor + return this +} + +LocalConfig.prototype.runInPassThroughMode = function _runInPassThroughMode (homeNet, flashairNet) { + debug('runInPassThroughMode: homeNet: %O, flashairNet: %O', homeNet, flashairNet) + _checkNetwork(flashairNet) + _checkNetwork(homeNet) + var hwConfig = { + Vendor: { + APPMODE: appModes.IPT, + APPSSID: flashairNet.ssid, + APPNETWORKKEY: flashairNet.key, + BRGSSID: homeNet.ssid, + BRGNETWORKKEY: homeNet.key + }, + WLANSD: { + ID: flashairNet.ssid, + DHCP_Enabled: 'YES' + } + } + debug('runInPassThroughMode: Setting hw: %O', hwConfig) + this._hwConfig.Vendor = hwConfig.Vendor + this._hwConfig.WLANSD = hwConfig.WLANSD + return this +} + +LocalConfig.prototypesetAppMode = function _setAppMode (accessMode) { + if (accessMode.accessPoint && accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.IPT + } else if (accessMode.accessPoint && !accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.AP + } else if (!accessMode.accessPoint && accessMode.station) { + this._hwConfig.Vendor.APPMODE = appModes.STA + } else { + throw new Error('Invalid accessMode' + JSON.stringify(accessMode)) + } +} + +LocalConfig.prototype.getAppMode = function _getAppMode () { + if (this._hwConfig.Vendor.APPMODE === appModes.IPT) { + return { + accessPoint: true, + station: { + ssid: this._hwConfig.Vendor.BRGSSID || '', + key: this._hwConfig.Vendor.BRGNETWORKKEY || '' + } + } + } else if (this._hwConfig.Vendor.APPMODE === appModes.AP) { + return { + accessPoint: true, + station: false + } + } else if (this._hwConfig.Vendor.APPMODE === appModes.STA) { + return { + accessPoint: false, + station: true + } + } +} + +// https://flashair-developers.com/en/documents/api/config/#WLANAPMODE + +LocalConfig.prototype.setWlanProtocol = function _setWlanStd (wlanProtocol) { + var WLANAPMODES = { + '802.11b': '0x01', + '802.11g': '0x02', + '802.11bg': '0x03', + '802.11ng': '0x82' + } + this._hwConfig.Vendor.WLANAPMODE = WLANAPMODES[wlanProtocol] || this._hwConfig.Vendor.WLANAPMODE +} + +LocalConfig.prototype.getWlanProtocol = function _getWlanProtocol () { + var WLANAPMODES = { + '0x01': '802.11b', + '0x02': '802.11g', + '0x03': '802.11bg', + '0x82': '802.11ng' + } + return WLANAPMODES[this._hwConfig.Vendor.WLANAPMODE || '0x03'] +} + +function _checkNetwork (net) { + if (typeof net.ssid !== 'string' || net.ssid.length < 1) { + throw new Error("Invalid ssid='" + net.ssid + "' must be a string of 1+ characters") + } + if (typeof net.key !== 'string' || net.key.length < 8) { + throw new Error("Invalid key='" + net.key + "' must be a string of 8+ characters") + } +}