|
1 | | -const gwType = 'Ethernet'; |
2 | | -const gwAddress = '10.0.1.99'; |
3 | | -const gwPort = 9999; |
| 1 | +//const gwType = 'Ethernet'; |
| 2 | +//const gwAddress = '10.0.1.99'; |
| 3 | +//const gwPort = 9999; |
4 | 4 |
|
5 | | -//const gwType = 'Serial'; |
| 5 | +const gwType = 'Serial'; |
6 | 6 | //const gwPort = 'COM4'; |
7 | | -//const gwPort = '/dev/ttyUSB0'; |
8 | | -//const gwBaud = 115200; |
| 7 | +const gwPort = '/dev/ttyAMA0'; |
| 8 | +const gwBaud = 115200; |
9 | 9 |
|
10 | 10 | const dbAddress = '127.0.0.1'; |
11 | 11 | const dbPort = 27017; |
12 | 12 | const dbName = 'MySensorsDb'; |
13 | 13 |
|
14 | | -const fwHexFiles = [ ]; |
15 | | -const fwDefaultType = 0xFFFF; // index of hex file from array above (0xFFFF == none defined) |
| 14 | +const fwSketches = [ '../libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino' ]; |
| 15 | +const fwDefaultType = 0; // index of hex file from array above |
16 | 16 |
|
17 | 17 | const FIRMWARE_BLOCK_SIZE = 16; |
18 | 18 | const BROADCAST_ADDRESS = 255; |
@@ -117,6 +117,8 @@ const P_ULONG32 = 5; |
117 | 117 | const P_CUSTOM = 6; |
118 | 118 |
|
119 | 119 | var fs = require('fs'); |
| 120 | +var path = require('path'); |
| 121 | +var requestify = require('requestify'); |
120 | 122 | var appendedString=""; |
121 | 123 |
|
122 | 124 | function crcUpdate(old, value) { |
@@ -146,76 +148,99 @@ function pushDWord(arr, val) { |
146 | 148 | arr.push((val >> 24) & 0x000000FF); |
147 | 149 | } |
148 | 150 |
|
149 | | -function loadFirmware(fwtype, fwversion, filename, db) { |
150 | | - console.log("loading firmware: " + filename); |
151 | | - fwdata = []; |
152 | | - var start = 0; |
153 | | - var end = 0; |
154 | | - var pos = 0; |
155 | | - var hex = fs.readFileSync(filename).toString().split("\n"); |
156 | | - for(l in hex) { |
157 | | - line = hex[l].trim(); |
158 | | - if (line.length > 0) { |
159 | | - while (line.substring(0, 1) != ":") |
160 | | - line = line.substring(1); |
161 | | - var reclen = parseInt(line.substring(1, 3), 16); |
162 | | - var offset = parseInt(line.substring(3, 7), 16); |
163 | | - var rectype = parseInt(line.substring(7, 9), 16); |
164 | | - var data = line.substring(9, 9 + 2 * reclen); |
165 | | - var chksum = parseInt(line.substring(9 + (2 * reclen), 9 + (2 * reclen) + 2), 16); |
166 | | - if (rectype == 0) { |
167 | | - if ((start == 0) && (end == 0)) { |
168 | | - if (offset % 128 > 0) |
169 | | - throw new Error("error loading hex file - offset can't be devided by 128"); |
170 | | - start = offset; |
171 | | - end = offset; |
| 151 | +function loadFirmware(fwtype, fwversion, sketch, db) { |
| 152 | + var filename = path.basename(sketch); |
| 153 | + console.log("compiling firmware: " + filename); |
| 154 | + var req = { |
| 155 | + files: [{ |
| 156 | + filename: filename, |
| 157 | + content: fs.readFileSync(sketch).toString() |
| 158 | + }], |
| 159 | + format: "hex", |
| 160 | + version: "105", |
| 161 | + build: { |
| 162 | + mcu: "atmega328p", |
| 163 | + f_cpu: "16000000L", |
| 164 | + core: "arduino", |
| 165 | + variant: "standard" |
| 166 | + } |
| 167 | + }; |
| 168 | + requestify.post('https://codebender.cc/utilities/compile/', req).then(function(res) { |
| 169 | + var body = JSON.parse(res.getBody()); |
| 170 | + if (body.success) { |
| 171 | + console.log("loading firmware: " + filename); |
| 172 | + fwdata = []; |
| 173 | + var start = 0; |
| 174 | + var end = 0; |
| 175 | + var pos = 0; |
| 176 | + var hex = body.output.split("\n"); |
| 177 | + for(l in hex) { |
| 178 | + line = hex[l].trim(); |
| 179 | + if (line.length > 0) { |
| 180 | + while (line.substring(0, 1) != ":") |
| 181 | + line = line.substring(1); |
| 182 | + var reclen = parseInt(line.substring(1, 3), 16); |
| 183 | + var offset = parseInt(line.substring(3, 7), 16); |
| 184 | + var rectype = parseInt(line.substring(7, 9), 16); |
| 185 | + var data = line.substring(9, 9 + 2 * reclen); |
| 186 | + var chksum = parseInt(line.substring(9 + (2 * reclen), 9 + (2 * reclen) + 2), 16); |
| 187 | + if (rectype == 0) { |
| 188 | + if ((start == 0) && (end == 0)) { |
| 189 | + if (offset % 128 > 0) |
| 190 | + throw new Error("error loading hex file - offset can't be devided by 128"); |
| 191 | + start = offset; |
| 192 | + end = offset; |
| 193 | + } |
| 194 | + if (offset < end) |
| 195 | + throw new Error("error loading hex file - offset lower than end"); |
| 196 | + while (offset > end) { |
| 197 | + fwdata.push(255); |
| 198 | + pos++; |
| 199 | + end++; |
| 200 | + } |
| 201 | + for (var i = 0; i < reclen; i++) { |
| 202 | + fwdata.push(parseInt(data.substring(i * 2, (i * 2) + 2), 16)); |
| 203 | + pos++; |
| 204 | + } |
| 205 | + end += reclen; |
| 206 | + } |
172 | 207 | } |
173 | | - if (offset < end) |
174 | | - throw new Error("error loading hex file - offset lower than end"); |
175 | | - while (offset > end) { |
176 | | - fwdata.push(255); |
177 | | - pos++; |
178 | | - end++; |
179 | | - } |
180 | | - for (var i = 0; i < reclen; i++) { |
181 | | - fwdata.push(parseInt(data.substring(i * 2, (i * 2) + 2), 16)); |
182 | | - pos++; |
183 | | - } |
184 | | - end += reclen; |
| 208 | + } |
| 209 | + var pad = end % 128; // ATMega328 has 64 words per page / 128 bytes per page |
| 210 | + for (var i = 0; i < 128 - pad; i++) { |
| 211 | + fwdata.push(255); |
| 212 | + pos++; |
| 213 | + end++; |
185 | 214 | } |
186 | | - } |
187 | | - } |
188 | | - var pad = end % 128; // ATMega328 has 64 words per page / 128 bytes per page |
189 | | - for (var i = 0; i < 128 - pad; i++) { |
190 | | - fwdata.push(255); |
191 | | - pos++; |
192 | | - end++; |
193 | | - } |
194 | | - var blocks = (end - start) / FIRMWARE_BLOCK_SIZE; |
195 | | - var crc = 0xFFFF; |
196 | | - for (var i = 0; i < blocks * FIRMWARE_BLOCK_SIZE; ++i) { |
197 | | - var v = crc; |
198 | | - crc = crcUpdate(crc, fwdata[i]); |
199 | | - } |
200 | | - db.collection('firmware', function(err, c) { |
201 | | - c.update({ |
202 | | - 'type': fwtype, |
203 | | - 'version': fwversion |
204 | | - }, { |
205 | | - $set: { |
206 | | - 'filename': filename, |
207 | | - 'blocks': blocks, |
208 | | - 'crc': crc, |
209 | | - 'data': fwdata |
| 215 | + var blocks = (end - start) / FIRMWARE_BLOCK_SIZE; |
| 216 | + var crc = 0xFFFF; |
| 217 | + for (var i = 0; i < blocks * FIRMWARE_BLOCK_SIZE; ++i) { |
| 218 | + var v = crc; |
| 219 | + crc = crcUpdate(crc, fwdata[i]); |
210 | 220 | } |
211 | | - }, { |
212 | | - upsert: true |
213 | | - }, function(err, result) { |
214 | | - if (err) |
215 | | - console.log('Error writing firmware to database'); |
216 | | - }); |
217 | | - }); |
218 | | - console.log("loading firmware done. blocks: " + blocks + " / crc: " + crc); |
| 221 | + db.collection('firmware', function(err, c) { |
| 222 | + c.update({ |
| 223 | + 'type': fwtype, |
| 224 | + 'version': fwversion |
| 225 | + }, { |
| 226 | + $set: { |
| 227 | + 'filename': filename, |
| 228 | + 'blocks': blocks, |
| 229 | + 'crc': crc, |
| 230 | + 'data': fwdata |
| 231 | + } |
| 232 | + }, { |
| 233 | + upsert: true |
| 234 | + }, function(err, result) { |
| 235 | + if (err) |
| 236 | + console.log('Error writing firmware to database'); |
| 237 | + }); |
| 238 | + }); |
| 239 | + console.log("loading firmware done. blocks: " + blocks + " / crc: " + crc); |
| 240 | + } else { |
| 241 | + console.log("error: %j", res.body); |
| 242 | + } |
| 243 | + }); |
219 | 244 | } |
220 | 245 |
|
221 | 246 | /* |
@@ -645,8 +670,8 @@ dbc.connect('mongodb://' + dbAddress + ':' + dbPort + '/' + dbName, function(err |
645 | 670 | db.createCollection('firmware', function(err, collection) { }); |
646 | 671 |
|
647 | 672 | // ToDo : check for new hex files / only load if new / get type and version from filename |
648 | | - for (var i = 0; i < fwHexFiles.length; i++) |
649 | | - loadFirmware(i, 1, fwHexFiles[i], db); |
| 673 | + for (var i = 0; i < fwSketches.length; i++) |
| 674 | + loadFirmware(i, 1, fwSketches[i], db); |
650 | 675 |
|
651 | 676 | var gw; |
652 | 677 | if (gwType == 'Ethernet') { |
|
0 commit comments