Firmware upgrade now functional

This commit is contained in:
Zaid Abdulla committed 2013-02-22 22:37:51 +03:00
1 parent e15f42a531
commit 39a5ff8a9a
5 files changed
+80 -64

No files matched your search

+3 -4
View File
@@ -94,10 +94,8 @@ updateDevice = (currentRequest, actions, callback) ->
runTask = (currentRequest, task, methodResponse) -> runTask = (currentRequest, task, methodResponse) ->
cwmpResponse = {} tasks.task(task, methodResponse, (err, status, cwmpResponse, deviceUpdates) ->
deviceUpdates = {} # TODO handle error
status = tasks.task(task, methodResponse, cwmpResponse, deviceUpdates)
updateDevice(currentRequest, deviceUpdates) updateDevice(currentRequest, deviceUpdates)
switch status switch status
@@ -126,6 +124,7 @@ runTask = (currentRequest, task, methodResponse) ->
) )
else else
throw Error('Unknown task status') throw Error('Unknown task status')
)
nextTask = (currentRequest) -> nextTask = (currentRequest) ->
+1
View File
@@ -7,6 +7,7 @@ exports.API_PORT = 7557
exports.API_INTERFACE = '0.0.0.0' exports.API_INTERFACE = '0.0.0.0'
exports.FILES_PORT = 7567 exports.FILES_PORT = 7567
exports.FILES_INTERFACE = '0.0.0.0' exports.FILES_INTERFACE = '0.0.0.0'
exports.FILES_IP = '172.240.21.2' # Used when sending download requests to devices
exports.CACHE_DURATION = 60 # in seconds exports.CACHE_DURATION = 60 # in seconds
exports.PRESETS_CACHE_DURATION = 86400 exports.PRESETS_CACHE_DURATION = 86400
exports.PRESETS_TIME_PADDING = 1 exports.PRESETS_TIME_PADDING = 1
+4
View File
@@ -33,6 +33,10 @@ db.open( (err, db) ->
db.collection('presets', (err, collection) -> db.collection('presets', (err, collection) ->
exports.presetsCollection = presetsCollection = collection exports.presetsCollection = presetsCollection = collection
) )
db.collection('fs.files', (err, collection) ->
exports.filesCollection = filesCollection = collection
)
) )
+71 -59
View File
@@ -1,4 +1,7 @@
config = require './config'
common = require './common' common = require './common'
db = require './db'
mongodb = require 'mongodb'
BATCH_SIZE = 16 BATCH_SIZE = 16
exports.STATUS_QUEUED = STATUS_QUEUED = 0 exports.STATUS_QUEUED = STATUS_QUEUED = 0
@@ -8,60 +11,64 @@ exports.STATUS_FAULT = STATUS_FAULT = 3
exports.STATUS_FINISHED = STATUS_FINISHED = 4 exports.STATUS_FINISHED = STATUS_FINISHED = 4
this.init = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.init = (task, methodResponse, callback) ->
if not task.subtask? if not task.subtask?
task.subtask = {'name' : 'getParameterNames', 'parameterPath' : '', 'nextLevel' : false} task.subtask = {name : 'getParameterNames', parameterPath : '', nextLevel : false}
if task.subtask.name == 'getParameterNames' if task.subtask.name == 'getParameterNames'
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
status = this.getParameterNames(task.subtask, methodResponse, cwmpResponse, deviceUpdates) this.getParameterNames(task.subtask, methodResponse, (err, status, cwmpResponse, deviceUpdates) =>
if status is STATUS_FINISHED if status is STATUS_FINISHED
parameterNames = [] parameterNames = []
for p in deviceUpdates.parameterNames for p in deviceUpdates.parameterNames
if not common.endsWith(p[0], '.') if not common.endsWith(p[0], '.')
parameterNames.push(p[0]) parameterNames.push(p[0])
task.subtask = {'name' : 'getParameterValues', 'parameterNames' : parameterNames} task.subtask = {name : 'getParameterValues', parameterNames : parameterNames}
else if status != STATUS_STARTED this.getParameterValues(task.subtask, {}, (err, status, cwmpResponse, deviceUpdates) ->
callback(err, status, cwmpResponse, deviceUpdates)
)
else if status is STATUS_STARTED
callback(err, STATUS_STARTED, cwmpResponse, deviceUpdates)
else
throw Error('Unexpected subtask status') throw Error('Unexpected subtask status')
)
if task.subtask.name == 'getParameterValues' else if task.subtask.name == 'getParameterValues'
if methodResponse.faultcode? if methodResponse.faultcode?
# Ignore GetParameterValues errors. A workaround for the crappy Seewon devices. # Ignore GetParameterValues errors. A workaround for the crappy Seewon devices.
status = this.getParameterValues(task.subtask, {parameterList : {}}, cwmpResponse, deviceUpdates) methodResponse = {parameterList : {}}
this.getParameterValues(task.subtask, methodResponse, (err, status, cwmpResponse, deviceUpdates) ->
callback(err, status, cwmpResponse, deviceUpdates)
)
else else
status = this.getParameterValues(task.subtask, methodResponse, cwmpResponse, deviceUpdates) throw Error('Unexpected subtask name')
if status is STATUS_FINISHED
return STATUS_FINISHED
else if status isnt STATUS_STARTED
throw Error('Unexpected subtask status')
return STATUS_STARTED
this.getParameterNames = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.getParameterNames = (task, methodResponse, callback) ->
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
if methodResponse.type is 'GetParameterNamesResponse' if methodResponse.type is 'GetParameterNamesResponse'
deviceUpdates.parameterNames = methodResponse.parameterList callback(null, STATUS_FINISHED, null, {parameterNames : methodResponse.parameterList})
return STATUS_FINISHED else
methodRequest = {
cwmpResponse.methodRequest = {
type : 'GetParameterNames', type : 'GetParameterNames',
parameterPath : task.parameterPath, parameterPath : task.parameterPath,
nextLevel : if task.nextLevel? then task.nextLevel else false nextLevel : if task.nextLevel? then task.nextLevel else false
} }
return STATUS_STARTED callback(null, STATUS_STARTED, {methodRequest : methodRequest})
this.getParameterValues = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.getParameterValues = (task, methodResponse, callback) ->
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
if not task.currentIndex? if not task.currentIndex?
task.currentIndex = 0 task.currentIndex = 0
@@ -72,23 +79,23 @@ this.getParameterValues = (task, methodResponse, cwmpResponse, deviceUpdates) ->
names = task.parameterNames.slice(task.currentIndex, task.nextIndex) names = task.parameterNames.slice(task.currentIndex, task.nextIndex)
if methodResponse.type is 'GetParameterValuesResponse' if methodResponse.type is 'GetParameterValuesResponse'
deviceUpdates.parameterValues = methodResponse.parameterList deviceUpdates = {parameterValues : methodResponse.parameterList}
if names.length == 0 if names.length == 0
return STATUS_FINISHED callback(null, STATUS_FINISHED, null, deviceUpdates)
else
cwmpResponse.methodRequest = { methodRequest = {
type : 'GetParameterValues', type : 'GetParameterValues',
parameterNames : names parameterNames : names
} }
callback(null, STATUS_STARTED, {methodRequest : methodRequest}, deviceUpdates)
return STATUS_STARTED
this.setParameterValues = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.setParameterValues = (task, methodResponse, callback) ->
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
if not task.currentIndex? if not task.currentIndex?
task.currentIndex = 0 task.currentIndex = 0
@@ -100,47 +107,52 @@ this.setParameterValues = (task, methodResponse, cwmpResponse, deviceUpdates) ->
values = task.parameterValues.slice(task.currentIndex, task.nextIndex) values = task.parameterValues.slice(task.currentIndex, task.nextIndex)
if prevValues? if prevValues?
deviceUpdates.parameterValues = prevValues deviceUpdates = {parameterValues : prevValues}
if values.length == 0 if values.length == 0
return STATUS_FINISHED callback(null, STATUS_FINISHED, null, deviceUpdates)
else
cwmpResponse.methodRequest = { callback(null, STATUS_STARTED, {methodRequest : {type : 'SetParameterValues', parameterList : values}}, deviceUpdates)
type : 'SetParameterValues',
parameterList : values
}
return STATUS_STARTED
this.reboot = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.reboot = (task, methodResponse, callback) ->
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
if methodResponse.type isnt 'RebootResponse' if methodResponse.type isnt 'RebootResponse'
cwmpResponse.methodRequest = { callback(null, STATUS_STARTED, {methodRequest : {type : 'Reboot'}})
type : 'Reboot' else
} callback(null, STATUS_FINISHED)
return STATUS_STARTED
return STATUS_FINISHED
this.download = (task, methodResponse, cwmpResponse, deviceUpdates) -> this.download = (task, methodResponse, callback) ->
if methodResponse.faultcode? if methodResponse.faultcode?
task.fault = methodResponse task.fault = methodResponse
return STATUS_FAULT callback(null, STATUS_FAULT)
return
if methodResponse.type isnt 'DownloadResponse' if methodResponse.type isnt 'DownloadResponse'
cwmpResponse.methodRequest = { db.filesCollection.findOne({_id : mongodb.ObjectID(task.file)}, (err, file) ->
if not file?
callback('File not found')
return
else if err?
callback(err)
return
methodRequest = {
type : 'Download', type : 'Download',
fileType : task.fileType, fileType : '1 Firmware Upgrade Image',
url : task.url fileSize : file.length,
url : "http://#{config.FILES_IP}:#{config.FILES_PORT}/#{file.filename}"
} }
return STATUS_STARTED callback(null, STATUS_STARTED, {methodRequest : methodRequest})
)
return STATUS_FINISHED else
callback(null, STATUS_FINISHED)
exports.task = (task, methodResponse, cwmpResponse, deviceUpdates) -> exports.task = (task, methodResponse, callback) ->
return this[task.name](task, methodResponse, cwmpResponse, deviceUpdates) this[task.name](task, methodResponse, callback)
+1 -1
View File
@@ -222,7 +222,7 @@ exports.response = (id, cwmpResponse, cookies = null) ->
if cookies? and Object.keys(cookies).length > 0 if cookies? and Object.keys(cookies).length > 0
headers['Set-Cookie'] = cookiesToStr(cookies) headers['Set-Cookie'] = cookiesToStr(cookies)
if cwmpResponse is null or Object.keys(cwmpResponse).length == 0 if not cwmpResponse? or Object.keys(cwmpResponse).length == 0
#console.log '>>> EMPTY RESPONSE' #console.log '>>> EMPTY RESPONSE'
# send empty response # send empty response
headers['Content-Length'] = 0 headers['Content-Length'] = 0