forked from Parrot-Developers/node-flower-power
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate.js
More file actions
156 lines (133 loc) · 4.2 KB
/
Update.js
File metadata and controls
156 lines (133 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var bufferEqual = require('buffer-equal');
var fs = require('fs');
var async = require('async');
const LEN_FRAME = 16;
var DOWNLOAD_SERVICE = 'f000ffc004514000b000000000000000';
var OAD_IMAGE_NOTIFIY = 'f000ffc104514000b000000000000000';
var OAD_IMAGE_BLOCK = 'f000ffc204514000b000000000000000';
function Update(fp, binaryFile) {
this.fp = fp;
this.file = binaryFile;
this.fd;
this.size = 0;
return this;
};
Update.prototype.extractHeaderFile = function(callback) {
var buff = new Buffer(8);
fs.read(this.fd, buff, 0, buff.length, 4, function(err, bytesRead, buffHeader) {
if (err) return callback(err, null);
else {
this.size = buffHeader.readUInt16LE(2);
callback(err, buffHeader);
}
}.bind(this));
};
Update.prototype.setImageNotify = function(buffHeader, callback) {
this.fp.writeDataCharacteristic(DOWNLOAD_SERVICE, OAD_IMAGE_NOTIFIY, buffHeader, function(err) {
callback(err);
});
};
Update.prototype.getImageNotify = function(callback) {
this.fp.readDataCharacteristic(DOWNLOAD_SERVICE, OAD_IMAGE_NOTIFIY, function(err, data) {
if (err || !data) callback(err || new Error('No data'));
else callback(null, data);
});
};
Update.prototype.initUpdate = function(callback) {
async.parallel({
header: function(callback) {
this.extractHeaderFile(callback);
}.bind(this),
notify: function(callback) {
this.getImageNotify(callback);
}.bind(this),
index: function(callback) {
this.getIndex(callback);
}.bind(this)
}, function(err, res) {
if (!bufferEqual(res.header, res.notify)) {
this.setImageNotify(res.header, callback);
}
else callback("No update firmware required");
}.bind(this));
};
Update.prototype.getIndex = function(callback) {
this.fp.readDataCharacteristic(DOWNLOAD_SERVICE, OAD_IMAGE_BLOCK, function(err, data) {
if (err || !data) callback(err || new Error('No data'));
else callback(null, data.readUInt16LE(0));
});
};
Update.prototype.readAFrame = function(index, callback) {
var buff = new Buffer(LEN_FRAME);
fs.read(this.fd, buff, 0, buff.length, index * LEN_FRAME, function(err, bytesRead, buffer) {
callback(err, buffer);
}.bind(this));
};
Update.prototype.writeAFrame = function(index, buffer, callback) {
var frame = new Buffer(2 + LEN_FRAME); // index + buffer;
frame.writeUInt16LE(index);
buffer.copy(frame, 2);
console.log('(' + (index + 1) + '/' + this.size + ' ' + Math.floor(((index + 1) / this.size) * 100) + '%)', frame);
this.fp.writeDataCharacteristic(DOWNLOAD_SERVICE, OAD_IMAGE_BLOCK, frame, function(err) {
callback(err);
});
};
Update.prototype.readAndWriteGroupFrame = function(index, callback) {
var nbFrame = 0;
async.whilst(
function() {return nbFrame < 128}.bind(this),
function(callback) {
this.readAFrame(index + nbFrame, function(err, frame) {
if (err) callback(err);
else {
this.writeAFrame(index + nbFrame, frame, function(err) {
nbFrame++;
callback(err);
});
}
}.bind(this));
}.bind(this),
function(err, n) {
callback(err);
}
);
};
Update.prototype.updateGroupFrame = function(callback) {
this.getIndex(function(err, index) {
this.readAndWriteGroupFrame(index, function(err) {
callback(err);
}.bind(this));
}.bind(this));
};
Update.prototype.writeFrimware = function(callback) {
async.whilst(
function() {return true},
function(callback) {
this.updateGroupFrame(function(err) {
callback(err);
});
}.bind(this),
function(err, n) {callback(err)}
);
};
Update.prototype.startUpdate = function(callback) {
this.fp._peripheral.on('disconnect', function(err) {
return callback(err);
});
fs.open(this.file, 'r', function(err, fd) {
if (err) return callback(err);
this.fd = fd;
async.series([
function(callback) {
this.initUpdate(callback);
}.bind(this),
function(callback) {
this.writeFrimware(callback)
}.bind(this)
], function(err) {
if (this.fd >= 3) fs.closeSync(this.fd);
callback(err);
}.bind(this));
}.bind(this));
};
module.exports = Update;