diff --git a/config/config-sample.json b/config/config-sample.json index 5e5b1b9..cbffa14 100644 --- a/config/config-sample.json +++ b/config/config-sample.json @@ -9,7 +9,7 @@ "NBI_PORT" : 7557, "FS_INTERFACE" : "0.0.0.0", "FS_PORT" : 7567, - "FILES_IP" : "192.168.0.1", + "FS_IP" : "192.168.0.1", "LOG_INFORMS" : true, "DEBUG" : false } diff --git a/lib/common.coffee b/lib/common.coffee index ec82c83..0983161 100644 --- a/lib/common.coffee +++ b/lib/common.coffee @@ -20,6 +20,9 @@ # THE SOFTWARE. ### +buffer = require 'buffer' +querystring = require 'querystring' + UNDEFINED_TYPE = '[object Undefined]' NULL_TYPE = '[object Null]' BOOLEAN_TYPE = '[object Boolean]' @@ -50,11 +53,11 @@ exports.arrayToHash = (arr) -> return hash -exports.getDeviceId = (deviceIdStruct) -> +exports.generateDeviceId = (deviceIdStruct) -> # Percent escaping function. Escapes everything except alphanumerics and underscore esc = (str) -> str.replace(/[^A-Za-z0-9_]/g, (chr) -> - buf = new require('buffer').Buffer(chr) + buf = new buffer.Buffer(chr, 'utf8') rep = '' rep += "%#{b.toString(16).toUpperCase()}" for b in buf return rep @@ -67,6 +70,17 @@ exports.getDeviceId = (deviceIdStruct) -> return "#{esc(deviceIdStruct['OUI'])}-#{esc(deviceIdStruct['SerialNumber'])}" +exports.parseDeviceId = (deviceId) -> + parts = deviceId.split('-') + ret = {oui : querystring.unescape(parts[0])} + if parts.length == 3 + ret.productClass = querystring.unescape(parts[1]) + ret.serialNumber = querystring.unescape(parts[2]) + else + ret.serialNumber = querystring.unescape(parts[1]) + return ret + + exports.extend = (obj, mixin) -> obj[name] = method for name, method of mixin obj diff --git a/lib/config.coffee b/lib/config.coffee index 2b5155e..b01d3fc 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -39,7 +39,7 @@ defaults = { FS_PORT : 7567, FS_INTERFACE : '0.0.0.0', FS_SSL : false, - FILES_IP : '192.168.0.1', # Used when sending download requests to devices + FS_IP : '192.168.0.1', # Used when sending download requests to devices CACHE_DURATION : 60, # in seconds PRESETS_CACHE_DURATION : 86400, diff --git a/lib/cwmp.coffee b/lib/cwmp.coffee index aa74bbb..28705b9 100644 --- a/lib/cwmp.coffee +++ b/lib/cwmp.coffee @@ -437,7 +437,7 @@ listener = (httpRequest, httpResponse) -> httpResponse.end('Session is expired') return session = { - deviceId : common.getDeviceId(cwmpRequest.methodRequest.deviceId), + deviceId : common.generateDeviceId(cwmpRequest.methodRequest.deviceId), cwmpVersion : cwmpRequest.cwmpVersion, sessionTimeout : cwmpRequest.sessionTimeout ? config.SESSION_TIMEOUT } @@ -499,29 +499,21 @@ listener = (httpRequest, httpResponse) -> presetSoftwareVersion = devicePreset.softwareVersion?.preset currentSoftwareVersion = devicePreset.softwareVersion?.current._value if presetSoftwareVersion? and presetSoftwareVersion != currentSoftwareVersion - projection = { - '_deviceId._Manufacturer' : 1, - '_deviceId._ProductClass' : 1, - } - db.devicesCollection.findOne({'_id' : currentRequest.session.deviceId}, projection, (err, device) -> + deviceIdStruct = common.parseDeviceId(currentRequest.session.deviceId) + db.filesCollection.findOne({'metadata.fileType' : '1 Firmware Upgrade Image', 'metadata.oui' : deviceIdStruct.oui, 'metadata.productClass' : deviceIdStruct.productClass, 'metadata.version' : presetSoftwareVersion}, {_id : 1}, (err, file) -> throw err if err - manufacturer = device._deviceId._Manufacturer - productClass = device._deviceId._ProductClass - db.filesCollection.findOne({'metadata.fileType' : '1 Firmware Upgrade Image', 'metadata.manufacturer' : manufacturer, 'metadata.productClass' : productClass, 'metadata.version' : presetSoftwareVersion}, {_id : 1}, (err, file) -> - throw err if err - if not file? - util.error("#{currentRequest.session.deviceId}: Firmware image not found (#{presetSoftwareVersion})") - return requestDownloadResponse() + if not file? + util.error("#{currentRequest.session.deviceId}: Firmware image not found (#{presetSoftwareVersion})") + return requestDownloadResponse() - task = { - device : currentRequest.session.deviceId, - name : 'download', - file : file['_id'] - } - apiFunctions.insertTasks(task, allAliases, (err, tasks) -> - throw err if err - return requestDownloadResponse() - ) + task = { + device : currentRequest.session.deviceId, + name : 'download', + file : file['_id'] + } + apiFunctions.insertTasks(task, allAliases, (err, tasks) -> + throw err if err + return requestDownloadResponse() ) ) else diff --git a/lib/nbi.coffee b/lib/nbi.coffee index 9cf1376..d7cb943 100644 --- a/lib/nbi.coffee +++ b/lib/nbi.coffee @@ -275,7 +275,7 @@ listener = (request, response) -> if request.method == 'PUT' metadata = { fileType : request.headers.filetype, - manufacturer : request.headers.manufacturer, + oui : request.headers.oui, productClass : request.headers.productclass, version : request.headers.version, } diff --git a/lib/tasks.coffee b/lib/tasks.coffee index 723e494..27f356b 100644 --- a/lib/tasks.coffee +++ b/lib/tasks.coffee @@ -332,7 +332,7 @@ this.download = (task, methodResponse, callback) -> type : 'Download', fileType : file.metadata.fileType, fileSize : file.length, - url : "http://#{config.FILES_IP}:#{config.FILES_PORT}/#{encodeURIComponent(file.filename)}", + url : "http://#{config.FS_IP}:#{config.FS_PORT}/#{encodeURIComponent(file.filename)}", successUrl : task.successUrl, failureUrl : task.failureUrl }