Skip to content

Commit

Permalink
Able to generate a working flashair ini file
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Xavier Kowalski committed Mar 18, 2017
1 parent 5b28fde commit ad20b83
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 140 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,20 @@ flashair.command.getFileList("/", function (err, files) {
});
```

Resources
---------

* [FlashAir Developper Home](https://flashair-developers.com/)
* [FlashAir Developpers Home](https://flashair-developers.com/en/support/forum/)
* [FlashAir StackOverflow channel](http://stackoverflow.com/questions/tagged/flashair)

License
-------

The MIT License (MIT)

Copyright (c) 2013 Takenori Nakagawa
Copyright (c) 2016 Francois-Xavier Kowalski
Copyright (c) 2016-2017 Francois-Xavier Kowalski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 31 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

var FlashAir = require('../lib')
var argv = require('minimist')(process.argv.slice(2))
var flashair = new FlashAir('flashair', 'IPT')
try {
if (argv._[0] === 'config') {
var homeNet = {
ssid: argv['home-ssid'],
key: argv['home-key']
}
var flashairNet = {
ssid: argv['card-ssid'],
key: argv['card-key']
}
flashair.ini.runInPassThroughMode(homeNet, flashairNet).save()
} else if (argv._[0] === 'ls') {
flashair.command.getFileList(argv._[1] || '/', function (err, data) {
if (err) {
console.console.error(err)
} else {
console.log('data:', data)
}
})
} else {
throw new Error('unknown cli command "' + argv._[0] + '"')
}
} catch (e) {
console.error('***', e.toString())
process.exit(1)
}
8 changes: 4 additions & 4 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

// default
var APPNAME = 'flashair'
var APPMODE = 'STA' // or AP
var APPMODE = 'STA' // or AP or IPT

var Libs = require('./libs')
var hostname = require('os').hostname()

module.exports = function (appname, appmode) {
module.exports = function (appname, appmode, configPath) {
// API End-point
var endpoint = appname || APPNAME
APPMODE = appmode || APPMODE
Expand All @@ -24,7 +24,7 @@ module.exports = function (appname, appmode) {
endpoint += '.local'
}

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

module.exports.sd = require('./libs/sd')
console.log(12)
4 changes: 3 additions & 1 deletion libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

var Command = require('./command')
var Config = require('./config')
var Ini = require('./ini')
var Thumbnail = require('./thumbnail')

exports = module.exports = Libs

function Libs (endpoint) {
function Libs (endpoint, configPath) {
this.command = new Command(endpoint)
this.config = new Config(endpoint)
this.ini = new Ini(configPath)
this.thumbnail = new Thumbnail(endpoint)
};
243 changes: 111 additions & 132 deletions libs/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@
*/

var fs = require('fs')
var path = require('path')
var ini = require('ini')
var debug = require('debug')('flashair:config')
var _ = require('underscore')
var debug = require('debug')('flashair:ini')

var _hwConfigFile = '/Volumes/NO NAME/SD_WLAN/CONFIG'
var _hwConfig // hardware => private singleton
var volumePath = process.platform === 'darwin' ? '/Volumes/NO NAME' : undefined

function load () {
_hwConfig = ini.parse(fs.readFileSync(_hwConfigFile, 'utf-8'))
_hwConfig.WLANSD = _hwConfig.WLANSD || {}
debug('loaded hwConfig:', _hwConfig)
}

function save () {
debug('saving hwConfig:', _hwConfig)
var iniConfig = ini.encode(_hwConfig, {
whitespace: false,
newline: true,
platform: 'win32'
})
debug('as:', iniConfig)
fs.writeFileSync(_hwConfigFile, iniConfig, 'utf-8')
}

load()
var defaultConfigPath = 'SD_WLAN/CONFIG'

// https://flashair-developers.com/en/documents/api/config/#APPMODE

Expand All @@ -37,125 +19,122 @@ var appModes = {
'IPT': 6
}

module.exports = {

save: function _save () {
save()
return this
},

getHw: function _getHw () {
return _.clone(_hwConfig)
},

setHw: function _setHw (hwConfig) {
_hwConfig.Vendor = _.extend(_hwConfig.Vendor, hwConfig.Vendor)
_hwConfig.WLANSD = _.extend(_hwConfig.WLANSD, hwConfig.WLANSD)
return this
},

runAsAccessPoint: function _runAsAccessPoint (flashairNet) {
debug('runAsAccessPoint: flashairNet:', flashairNet)
_checkNetwork(flashairNet)
var hwConfig = {
Vendor: {
APPMODE: appModes.AP,
APPSSID: flashairNet.ssid,
APPNETWORKKEY: flashairNet.key
}
module.exports = IniConfig

function IniConfig (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 iniConfig = ini.encode(this._hwConfig, {
whitespace: false,
newline: true,
platform: 'win32'
})
debug('saving hwConfig %O as "%s"', iniConfig, configPath)
fs.writeFileSync(configPath, iniConfig, 'utf-8')
}
}

IniConfig.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:', hwConfig)
this.setHw(hwConfig)
return this
},

runInPassThroughMode: function _runInPassThroughMode (homeNet, flashairNet) {
debug('runInPassThroughMode: homeNet:', homeNet, 'flashairNet:', 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('runAsAccessPoint: Setting hw: %O', hwConfig)
this._hwConfig.Vendor = hwConfig.Vendor
return this
}

IniConfig.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:', hwConfig)
this.setHw(hwConfig)
return this
},

get: function _get () {
var config = {
appMode: this.getAppMode(),
wlanProtocol: this.getWlanProtocol()
}
debug('runInPassThroughMode: Setting hw: %O', hwConfig)
this._hwConfig.Vendor = hwConfig.Vendor
this._hwConfig.WLANSD = hwConfig.WLANSD
return this
}

IniConfig.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))
}
}

IniConfig.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 || ''
}
}
debug('config:', config)
return config
},

setAppMode: function _setAppMode (accessMode) {
if (accessMode.accessPoint && accessMode.station) {
_hwConfig.Vendor.APPMODE = appModes.IPT
} else if (accessMode.accessPoint && !accessMode.station) {
_hwConfig.Vendor.APPMODE = appModes.AP
} else if (!accessMode.accessPoint && accessMode.station) {
_hwConfig.Vendor.APPMODE = appModes.STA
} else {
throw new Error('Invalid accessMode' + JSON.stringify(accessMode))
} else if (this._hwConfig.Vendor.APPMODE === appModes.AP) {
return {
accessPoint: true,
station: false
}
},

getAppMode: function _getAppMode () {
if (_hwConfig.Vendor.APPMODE === appModes.IPT) {
return {
accessPoint: true,
station: {
ssid: _hwConfig.Vendor.BRGSSID || '',
key: _hwConfig.Vendor.BRGNETWORKKEY || ''
}
}
} else if (_hwConfig.Vendor.APPMODE === appModes.AP) {
return {
accessPoint: true,
station: false
}
} else if (_hwConfig.Vendor.APPMODE === appModes.STA) {
return {
accessPoint: false,
station: true
}
} else if (this._hwConfig.Vendor.APPMODE === appModes.STA) {
return {
accessPoint: false,
station: true
}
},
}
}

// https://flashair-developers.com/en/documents/api/config/#WLANAPMODE
// https://flashair-developers.com/en/documents/api/config/#WLANAPMODE

setWlanProtocol: function _setWlanStd (wlanProtocol) {
var WLANAPMODES = {
'802.11b': '0x01',
'802.11g': '0x02',
'802.11bg': '0x03',
'802.11ng': '0x82'
}
_hwConfig.Vendor.WLANAPMODE = WLANAPMODES[wlanProtocol] || _hwConfig.Vendor.WLANAPMODE
},

getWlanProtocol: function _getWlanProtocol () {
var WLANAPMODES = {
'0x01': '802.11b',
'0x02': '802.11g',
'0x03': '802.11bg',
'0x82': '802.11ng'
}
return WLANAPMODES[_hwConfig.Vendor.WLANAPMODE || '0x03']
IniConfig.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
}

IniConfig.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) {
Expand Down
27 changes: 27 additions & 0 deletions libs/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,32 @@ module.exports = {
data = JSON.stringify(data)
}
require('fs').writeFileSync('data.log', data)
},

// https://github.com/jashkenas/underscore/issues/162
// http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object/728694#728694
clone: function (obj) {
if (obj === null || typeof obj !== 'object') {
return obj
}
var copy = obj.constructor()
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = obj[attr]
}
}
return copy
},

// http://stackoverflow.com/questions/14843815/recursive-deep-extend-assign-in-underscore-js
extend: function (target, source) {
for (var prop in source) {
if (prop in target) {
this.extend(target[prop], source[prop])
} else {
target[prop] = source[prop]
}
}
return target
}
}
Loading

0 comments on commit ad20b83

Please sign in to comment.