Skip to content

Commit

Permalink
local-config is mutually exclusive with remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Xavier Kowalski committed Mar 19, 2017
1 parent ad20b83 commit 7ded709
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,5 @@ module.exports = function (appname, appmode, configPath) {
endpoint += '.local'
}

return new Libs(endpoint, configPath)
return new Libs(endpoint)
}

console.log(12)
25 changes: 19 additions & 6 deletions libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
147 changes: 147 additions & 0 deletions libs/local-config.js
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 7ded709

Please sign in to comment.