text: var xnm=xnm||{};xnm.aio=xnm.aio||{};xnm.aio.m10T=xnm.aio.m10T||{};
  xnm.aio.m10T._CRC8Table=[0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,140,210,48,110,237,179,81,
  15,78,16,242,172,47,113,147,205,17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53];
  xnm.aio.m10T.CRC8=function(d){for(var c=0,a=0;a<d.length;a++)c=xnm.aio.m10T._CRC8Table[c^d[a]];return c};xnm.aio.m10T.CRC32=function(d){for(var c=Array(256),a=0;256>a;a++){for(var b=a<<24&4294967295,e=0;8>e;e++)0!=(b&-2147483648)?(b<<=1,b^=79764919):b<<=1;c[a]=b&4294967295}a=4294967295;for(b=0;b<d.length;b++)a=(a^(d[b]&255)<<24)&4294967295,e=a>>24&255,a=a<<8&4294967295,a=(a^c[e])&4294967295;return XC.getHexVal(a>>>16,4)+XC.getHexVal(a&65535,4)};
  xnm.aio.m10T.IBus=function(d,c,a){this._type=d;this._host=c;this._port=a;this._socket=null;this._timeout=2E3;this._callback=null};
  xnm.aio.m10T.IBus.prototype._openSocket=function(d){var c=d;this._socket=require("net").connect({port:this._port,host:this._host});this._socket.setTimeout(this._timeout);this._socket.setEncoding("binary");var a=this;this._socket.on("connect",function(){var b=new Buffer(a._type);a._socket.write(b)});this._socket.on("data",function(b){b=new Buffer(b,"binary");if(null!=a._socket){var d=[];if(2<=b.length&&79==b[0]&&75==b[1])if(c)a._socket.write(new Buffer(c)),c=null;else{if(3<b.length)for(var f=3;f<b.length;f++)d.push(b[f]);
  a._callback&&a._callback({error:null,data:d})}else a._callback&&a._callback({error:"socket data error",data:null})}});this._socket.on("error",function(b){null!=a._socket&&(a._socket.destroy(),a._socket=null,a._callback&&a._callback({error:"socket error",data:null}))});this._socket.on("timeout",function(){null!=a._socket&&(a._socket.destroy(),a._socket=null,a._callback&&a._callback({error:"socket timeout",data:null}))});this._socket.on("close",function(){a._socket=null});this._socket._doConnect&&"function"==
  typeof this._socket._doConnect&&this._socket._doConnect()};xnm.aio.m10T.IBus.prototype.send=function(d,c,a){this._callback=a;d=c?[87,58].concat(c).concat(d):[87,58].concat(d);this._socket?this._socket.write(new Buffer(d)):this._openSocket(d)};xnm.aio.m10T.IBus.prototype.receive=function(d,c,a){if("SPI"==this._type){c=[];for(var b=0;b<d;b++)c.push(0);this.send(c,null,function(b){a&&a(b)})}else this._callback=a,bytes=[82,58].concat([c,d]),this._socket?this._socket.write(new Buffer(bytes)):this._openSocket(bytes)};
  xnm.aio.m10T.IBus.prototype.end=function(){this._socket&&(this._socket.write(new Buffer("E:")),this._socket.destroy(),this._socket=null)};xnm.aio.m10T.STMSPIBootloader=function(d){this._spi=d;this._retryCnt=0};xnm.aio.m10T.STMSPIBootloader.prototype.start=function(d){var c=this;this._spi.send([90,0,0,121],null,function(a){a.data&&4==a.data.length&&121==a.data[2]?d&&d(!0):40>c._retryCnt++?setTimeout(function(){c.start(d)},100):d&&d(!1)})};
  xnm.aio.m10T.STMSPIBootloader.prototype.sendGet=function(d){var c=this;this._spi.send([90,0,255,0,0,121],null,function(a){a.data&&6==a.data.length&&121==a.data[4]?c._spi.receive(14,null,function(a){d&&d(a.data)}):d&&d()})};
  xnm.aio.m10T.STMSPIBootloader.prototype.readFlash=function(d,c){var a=d>>0&255,b=d>>8&255,e=d>>16&255;d=d>>24&255;var f=this;this._spi.send([90,17,238,0,0,121,d,e,b,a,d^e^b^a,0,0,121,255,0,0,0,121],null,function(a){a.data&&19==a.data.length&&121==a.data[4]&&121==a.data[12]&&121==a.data[17]?f._spi.receive(257,null,function(a){c&&c(a.data)}):c&&c()})};
  xnm.aio.m10T.STMSPIBootloader.prototype.eraseFlash=function(d){var c=this;this._spi.send([90,68,187,0,0,121,255,255,0],null,function(a){a.data&&9==a.data.length&&121==a.data[4]?c._waitForACK(1E3,10,d):d&&d()})};
  xnm.aio.m10T.STMSPIBootloader.prototype.writeFlash=function(d,c,a){var b=d>>0&255,e=d>>8&255,f=d>>16&255;d=d>>24&255;var g=d^f^e^b,h=this;this._spi.send([90,49,206,0,0,121,d,f,e,b,g,0,0,121],null,function(b){if(b.data&&14==b.data.length&&121==b.data[4]&&121==b.data[12]){b=[c.length-1].concat(c);for(var d=g=0;d<b.length;d++)g^=b[d];b=b.concat([g]);h._spi.send(b,null,function(b){h._waitForACK(10,50,a)})}else a&&a()})};xnm.aio.m10T.STMSPIBootloader.prototype.end=function(){this._spi.end()};
  xnm.aio.m10T.STMSPIBootloader.prototype._waitForACK=function(d,c,a){if(1>c)a&&a(!1);else{var b=this;setTimeout(function(){b._spi.send([0,0,121],null,function(e){e.data&&121==e.data[1]?a&&(a(!0),a=null):a&&b._waitForACK(d,c-1,a)})},d)}};
  xnm.aio.m10T.STMUpdater=function(d,c,a){this._file=d;this._host=this._bootloader=null;this.dfuTool=xnm.aio.m10T.V5Updater.DFU_TOOL.DFU_UTIL;this.hostType="A20";this.auth=this.at=null;this._offset=0;c&&(this._offset=c);this._fixedSize=a;this._callback=null;this._retries=this._bytesWritten=this._bytesToWrite=0;this._blockSize=256};
  xnm.aio.m10T.STMUpdater.prototype._writeNextBlock=function(){var d=this;require("fs").readFileBinary(this._file,this._offset+this._bytesWritten,this._blockSize,function(c,a){if(!c&&a){c=[];a=new Buffer(XC.getBytes(a,!0));for(c=Array.prototype.slice.call(a,0);c.length<d._blockSize;)c.push(255);var b=!1;d._bootloader.writeFlash(134217728+d._bytesWritten,c,function(a){b||(b=!0,a?(d._retries=0,d._bytesWritten+=d._blockSize,d._bytesWritten>=d._bytesToWrite?(d._bootloader.end(),setTimeout(function(){d._resetViaNet(function(){d._callback&&
  (d._callback({error:null,state:100}),d._callback=null)})},50)):(d._callback&&d._callback({error:null,state:Math.floor(d._bytesWritten/d._bytesToWrite*100)}),d._writeNextBlock())):12>d._retries?(d._retries++,setTimeout(function(){d._writeNextBlock()},500)):(d._bootloader.end(),d._callback&&(d._callback({error:"failed to write ("+d._bytesWritten+")",state:0}),d._callback=null)))})}else d._callback&&(d._bootloader.end(),d._callback&&(d._callback({error:"failed to read file ("+d._bytesWritten+")",state:0}),
  d._callback=null))})};
  xnm.aio.m10T.STMUpdater.prototype._resetViaNet=function(d,c){var a="";this.at?a="&at="+this.at:this.auth&&(a="&auth="+this.auth);var b=this;$.ajax({type:"GET",url:"http://"+b._host+"/set?io3=0&t=50"+a,timeout:2E3,cache:!1,complete:function(e){var f=!1;e&&e.responseText&&(e=e.responseText);if(e){var g=null;try{g=$.parseJSON(e)}catch(h){}g&&g.XC_SUC&&(f=!0)}$.ajax({type:"GET",url:"http://"+b._host+"/set?io3=3"+a,timeout:2E3,cache:!1,complete:function(a){f||c?d&&d():b._resetViaNet(d,!0)}})}})};
  xnm.aio.m10T.STMUpdater.prototype._startSPIBootloader=function(d){var c="http://"+this._host+"/custom/startSPIBL";this.at?c+="?at="+this.at:this.auth&&(c+="?auth="+this.auth);$.ajax({type:"GET",url:c,timeout:4E3,cache:!1,complete:function(a){var b="request error";a&&a.responseText&&(a=a.responseText);if(a){var c=null;try{c=$.parseJSON(a)}catch(f){}c&&(b=c.XC_SUC?null:c.XC_ERR?c.XC_ERR.code?"code "+c.XC_ERR.code:"init error":"data error")}else b="response error";d&&d(b)}})};
  xnm.aio.m10T.STMUpdater.prototype.startNetUpdate=function(d,c){var a=new xnm.aio.m10T.IBus("SPI",d,8081);this._bootloader=new xnm.aio.m10T.STMSPIBootloader(a);this._host=d;this._callback=c;this._bytesWritten=this._bytesToWrite=0;var b=this;require("fs").stat(this._file,function(a,d){a?c&&(c({error:"invalid update file",state:0}),b._callback=c=null):(b._bytesToWrite=b._fixedSize?b._fixedSize:d.size,b._startSPIBootloader(function(a){a?(b._bootloader.end(),c&&(c({error:"failed to start bootloader ("+
  a+")",state:0}),b._callback=c=null)):b._bootloader.eraseFlash(function(a){a?b._writeNextBlock():(b._bootloader.end(),c&&(c({error:"failed to erase flash",state:0}),b._callback=c=null))})}))})};xnm.aio.m10T.STMUpdater.prototype.startDFUUpdate=function(d,c,a){this.dfuTool===xnm.aio.m10T.V5Updater.DFU_TOOL.STM32?this._startDFUUpdate_STM32(d,c,a):this._startDFUUpdate_DFUUtil(d,c)};
  xnm.aio.m10T.STMUpdater.prototype._startDFUUpdate_DFUUtil=function(d,c){this._callback=c;this._bytesWritten=this._bytesToWrite=0;if(!require("fs").existsSync(this._file)&&c)c({error:"invalid dfu file",state:0}),b._callback=c=null;else{d='"'+d+'" -t 1024 -d 0483:df11 -a 0 -s 0x08000000:leave -D '+this._file;c=require("child_process").exec;d=c(d);var a=0,b=this;d.stdout.on("data",function(c){c=""+c;if("."==c)b._bytesWritten+=1024,b._callback&&(a=Math.floor(b._bytesWritten/b._bytesToWrite*100),b._callback({error:null,
  state:a}));else if(c&&1<c.length&&"-"==c.charAt(0))a=Number(c.substr(1)),b._callback&&100!=a&&b._callback({error:null,state:a});else if(c&&1===c.indexOf("Download"))c=c.split("%")[0].split(" "),a=Number(c[c.length-1]),b._callback&&!isNaN(a)&&100!=a&&b._callback({error:null,state:a});else if(c){var d=c.indexOf(", size = ");-1<d&&(b._bytesToWrite=parseInt(c.substr(d+9)))}});d.on("error",function(a){b._callback&&(b._callback({error:"failed to start dfu-util",state:0}),b._callback=null)});d.on("close",
  function(c){0==c||74==c&&100==a?b._callback&&(b._callback({error:null,state:100}),b._callback=null):b._callback&&(b._callback({error:"failed to flash via dfu-util",state:0}),b._callback=null)})}};
  xnm.aio.m10T.STMUpdater.prototype._startDFUUpdate_STM32=function(d,c,a){this._callback=c;if(!require("fs").existsSync(this._file)&&c)c({error:"invalid dfu file",state:0}),h._callback=c=null;else{d='"'+d+'" -c port='+a+" -w "+this._file+" 0x08000000 -v -s";c=require("child_process").exec;var b=c(d),e=0,f="",g=!1,h=this;b.stdout.on("data",function(a){a=a.toString();XC.debugf(a);if(0<=a.toLowerCase().indexOf("error: failed to download the file"))h._callback&&(h._callback({error:"Failed to write file with STM32 cli.",
  state:0}),b.kill());else if(f+=a,!g)if(0<=a.indexOf("File download complete"))g=!0;else{var c=a.indexOf("%");1<=c&&(a=a.substr(c-3,3).trim(),e=parseInt(a,10),h._callback&&100!=e&&h._callback({error:null,state:e}))}});b.on("error",function(a){XC.debugf("[STMUpdater._startDFUUpdate_STM32] Error "+a+": Failed to start STM32 cli.","error");h._callback&&(h._callback({error:"Failed to start STM32 cli.",state:0}),h._callback=null)});b.on("close",function(a,b){XC.debugf("DFU update via STM32 cli done. Close code/signal: "+
  a+"/"+b);0==a||74==a&&100==e?h._callback&&(h._callback({error:null,state:100}),h._callback=null):h._callback&&(a="Failed to flash via STM32 cli.",0<=f.indexOf("verification failed")&&(a+=" Verification failed."),XC.debugf("[STMUpdater._startDFUUpdate_STM32] "+a,"error"),h._callback({error:a,state:0}),h._callback=null)})}};
  xnm.aio.m10T.STMUpdater.prototype.resetST=function(d){var c=require("child_process").exec,a="/sys/class/gpio/gpio240/value",b="/sys/class/gpio/gpio241/value";"PI2"==this.hostType&&(a="/sys/class/gpio/gpio21/value",b="/sys/class/gpio/gpio20/value");d?c("echo 1 > "+a):c("echo 0 > "+a);c("echo 1 > "+b);c("echo 0 > "+b);c("echo 1 > "+b);c("echo 0 > "+a)};
  xnm.aio.m10T.STMUpdater.prototype.startSPIUpdate=function(d,c){this._bootloader=new xnm.aio.m10T.STMSPIBootloader(d);this._callback=c;this._bytesWritten=this._bytesToWrite=0;var a=this;require("fs").stat(this._file,function(b,d){b?c&&(c({error:"invalid update file (spi)",state:0}),a._callback=c=null):(a.resetST(!0),a._bytesToWrite=a._fixedSize?a._fixedSize:d.size,a._bootloader.start(function(b){b?a._bootloader.eraseFlash(function(b){b?a._writeNextBlock():(a._bootloader.end(),c&&(c({error:"failed to erase flash (spi)",
  state:0}),a._callback=c=null))}):(a._bootloader.end(),c&&(c({error:"failed to start bootloader (spi)",state:0}),a._callback=c=null))}))})};xnm.aio.m10T.STMUSBSerial=function(){this._onTimeout=this._onData=this._onError=this._timeoutTimer=this._serialPort=this._port=null;this._useChromeAPI=!1;this._connId=this._connectionInfo=null;try{require("serialport")}catch(d){"undefined"!==typeof chrome&&"undefined"!==typeof chrome.serial&&(this._useChromeAPI=!0)}};
  xnm.aio.m10T.STMUSBSerial.prototype.on=function(d,c){"error"==d?this._onError=c:"data"==d?this._onData=c:"timeout"==d&&(this._onTimeout=c)};
  xnm.aio.m10T.STMUSBSerial.prototype.open=function(d,c,a){a||(a=230400);if(this._useChromeAPI)this._chromeOpen(d,c,a);else{this._port=d;this._baudrate=a;var b=require("serialport");if(b){this._serialPort=b.SerialPort?new b.SerialPort(d,{baudrate:a}):new b(d,{baudRate:a});var e=this;this._serialPort.on("open",function(){c&&c();e._serialPort.flush();e._serialPort.on("data",function(a){e._timeoutTimer&&(clearTimeout(e._timeoutTimer),e._timeoutTimer=null);e._onData&&e._onData(a)})});this._serialPort.on("error",
  function(a){e._serialPort=null;e._timeoutTimer&&(clearTimeout(e._timeoutTimer),e._timeoutTimer=null);e._onError&&e._onError(a)})}else e._onError&&e._onError()}};xnm.aio.m10T.STMUSBSerial.prototype.write=function(d,c,a){if(this._useChromeAPI)this._chromeWrite(d,c,a);else if(this._serialPort){var b=this;this._serialPort.write(d,function(b){a&&a(b)});c&&(this._timeoutTimer&&clearTimeout(this._timeoutTimer),this._timeoutTimer=setTimeout(function(){b._onTimeout&&b._onTimeout()},c))}};
  xnm.aio.m10T.STMUSBSerial.prototype.flush=function(){this._useChromeAPI?this._chromeFlush():this._serialPort&&this._serialPort.flush()};xnm.aio.m10T.STMUSBSerial.prototype.close=function(){this._useChromeAPI?this._chromeClose():(this._serialPort&&this._serialPort.close(),this._timeoutTimer&&(clearTimeout(this._timeoutTimer),this._timeoutTimer=null),this._onTimeout=this._onData=this._onError=null)};
  xnm.aio.m10T.STMUSBSerial.prototype.generateCommandMessage=function(d,c){var a=[],b=[];if(c){a=[31];a.push(c);b.push(c);for(c=0;c<d.length;c++)if("string"==typeof d)a.push(d.charCodeAt(c)),b.push(d.charCodeAt(c));else{var e=d[c];b.push(e);31!=e&&4!=e&&27!=e||a.push(27);a.push(e)}d=xnm.aio.m10T.CRC8(b);31!=d&&4!=d&&27!=d||a.push(27);a.push(d);a.push(4)}return a};
  xnm.aio.m10T.STMUSBSerial.prototype.getMessageData=function(d){for(var c=null,a=!1,b=!1,e=0;e<d.length;e++){var f=d[e];if(27==f||4==f||31==f)if(a)c&&c.push(f),a=!1;else if(27==f)a=!0;else if(4==f){b=!0;d.splice(0,e+1);break}else 31==f&&(c=[],a=!1);else c&&c.push(f),a=!1}return c&&b&&1<c.length?(xnm.aio.m10T.CRC8(c.slice(0,c.length-1)),c=c.slice(0,c.length-1)):null};
  xnm.aio.m10T.STMUSBSerial.prototype._chromeOpen=function(d,c,a){var b=this;null!==this._connId||this._port||"darwin"===process.platform&&0>String(d).indexOf("/tty.")||(this._port=d,this._baudrate=a,a={bitrate:this._baudrate,persistent:!0},chrome.serial.onReceiveError.addListener(function(a){b._onError&&b._onError();b._chromeClose()}),chrome.serial.connect(d,a,function(a){a?(b._connectionInfo=a,b._connId=a.connectionId,chrome.serial.onReceive.addListener(function(a){a&&a.connectionId===b._connId&&
  (b._timeoutTimer&&(clearTimeout(b._timeoutTimer),b._timeoutTimer=null),b._onData&&(a=Buffer.from(a.data),b._onData(a)))}),chrome.serial.flush(b._connId,function(a){c&&c()})):b._chromeClose()}))};
  xnm.aio.m10T.STMUSBSerial.prototype._chromeWrite=function(d,c,a){if(null!==this._connId){var b=this;d=new Uint8Array(d);chrome.serial.send(b._connId,d,function(a){console.log("[STMUSBSerial._chromeWrite] send:",a)});b._timeoutTimer&&clearTimeout(b._timeoutTimer);c&&(b._timeoutTimer=setTimeout(function(){b._onTimeout&&b._onTimeout()},c))}};xnm.aio.m10T.STMUSBSerial.prototype._chromeFlush=function(){null!==this._connId&&chrome.serial.flush(this._connId,function(d){})};
  xnm.aio.m10T.STMUSBSerial.prototype._chromeClose=function(){if(null!==this._connId){var d=this._connId;chrome.serial.disconnect(this._connId,function(c){console.log("[STMUSBSerial._chromeOpen] Connection to "+d+" has been closed. ("+c+")")})}this._connId=this._connectionInfo=null;this._timeoutTimer&&(clearTimeout(this._timeoutTimer),this._timeoutTimer=null);this._onTimeout=this._onData=this._onError=this._port=null};
  xnm.aio.m10T.ESPUpdater=function(d,c,a,b){this._file=d;this._offset=this._filesize=0;c&&(this._offset=c);this._fixedSize=a;this._md5=null;b&&(this._md5=b);this._useNewHeader=!0;this._callback=null;this._blockSize=496;this._retries=this._bytesSent=0};
  xnm.aio.m10T.ESPUpdater.prototype._getNextSerialFlashData=function(d,c){require("fs").readFileBinary(this._file,this._offset+this._bytesSent,this._blockSize,function(a,b){if(!a&&b){var e=b.length/2;a=[192,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(var f=239,g=0;g<b.length;g+=2){var h=parseInt(b[g]+b[g+1],16);f^=h;a.push(h)}a[3]=e+16&255;a[4]=(e+16&65280)>>8;a[5]=f;a[9]=e&255;a[10]=(e&65280)>>8;a[13]=d&255;a[14]=(d&65280)>>8;b=[192];for(g=1;g<a.length;g++){h=a[g];if(192==h||219==h)b.push(219),
  h=208+((h&240)>>4);b.push(h)}b.push(192);c&&c(b)}else c&&c()})};
  xnm.aio.m10T.ESPUpdater.prototype.startSerialUpdate32=function(d,c,a){this._callback=a;this._blockSize=1024;this._bytesSent=0;var b=this,e=b._file,f=new xnm.aio.m10T.STMUSBSerial;new Buffer([]);f.on("error",function(){a&&a({error:"serial port error",state:0})});f.on("data",function(a){});f.on("timeout",function(){a&&a({error:"serial timeout",state:0});f.close()});f.open(d,function(){f.write([31,48,0,4]);setTimeout(function(){f.close();var a="python "+c+" --chip esp32 --port "+d+' --baud 115200 --before no_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect 0xe000 "'+
  e+'/ota_data_initial.bin" 0x1000 "'+e+'/bootloader/bootloader.bin" 0x10000 "'+e+'/main.bin" 0x8000 "'+e+'/partitions.bin"',h=require("child_process").exec;a=h(a);a.stdout.on("data",function(a){a=a.trim();26<a.length&&0==a.indexOf("Writing at ")&&(a=parseInt(a.substr(26)),99<a&&(a=99),b._callback&&b._callback({error:null,state:a}))});a.on("error",function(a){});a.on("close",function(a){b._callback&&(0==a?b._callback({error:null,state:100}):b._callback({error:"esptool error",state:0}))})},500)})};
  xnm.aio.m10T.ESPUpdater.prototype.startSerialUpdate=function(d,c,a){this._callback=c;this._blockSize=1024;this._bytesSent=0;var b=this;require("fs").stat(this._file,function(e,f){if(e)c&&c({error:"invalid update file",state:0});else{b._filesize=b._fixedSize?b._fixedSize:f.size;var g=new xnm.aio.m10T.STMUSBSerial,h=!1,k=!1,l=0,m=[192,0,8,36,0,221,0,0,0,7,7,18,32];for(e=0;32>e;e++)m.push(85);m.push(192);var n=new Buffer([]);g.on("error",function(a){g.close();if(c){var b="serial port error";a&&a.message&&
  (b+=" ("+a.message+")");c({error:b,state:0})}});g.on("data",function(d){var f=!1;n=Buffer.concat([n,d]);var e=-1,l=null;for(d=0;d<n.length;d++)if(192==n[d])if(0>e)e=d;else if(l=n.slice(e,e+d+1),12==l.length&&0==l[9]&&0==l[10])if(!h&&8==l[2]||!k&&2==l[2]||3==l[2]){n=new Buffer([]);g.flush();f=!0;break}else e=d;else e=d;b._connTimeoutTimer&&(clearTimeout(b._connTimeoutTimer),b._connTimeoutTimer=null);h||k||(b._connTimeoutTimer=setTimeout(function(){h||k||(g.close(),c&&c({error:"communication error",
  state:0}))},2E3));if(f)if(h||k)if(h=!0,k?b._bytesSent+=b._blockSize:k=!0,b._bytesSent<b._filesize)b._callback&&b._callback({error:null,state:Math.floor(b._bytesSent/b._filesize*100)}),f=Math.round(b._bytesSent/b._blockSize),b._bytesSent+b._blockSize>b._filesize&&(b._blockSize=b._filesize-b._bytesSent),b._getNextSerialFlashData(f,function(a){a?(b._lastCRC=a[5],setTimeout(function(){g.write(a,500)},5)):b._callback&&b._callback({error:"failed to read update file",state:0})});else{var m=[192,0,4,4,0,
  b._lastCRC,0,0,0,1,0,0,0,192];setTimeout(function(){g.write(m);setTimeout(function(){b._callback&&b._callback({error:null,state:100});g.close()},250)},50)}else{h=!0;var p=[192,0,2,16,0,26,0,0,0,0,240,3,0,0,2,0,0,0,4,0,0,0,0,0,0,192];p[11]=7;p[10]=0;1==a&&(p[11]=63);f=239;for(d=10;d<p.length-1;d++)f^=p[d];p[5]=f;setTimeout(function(){g.write(p,2E4)},20)}});g.on("timeout",function(){h?(c&&c({error:"serial timeout",state:0}),g.close()):5>l?(g.write(m,500),l++):(g.close(),c&&c({error:"failed to connect",
  state:0}))});g.open(d,function(){g.write([31,48,0,4]);setTimeout(function(){g.write(m,500)},250)})}})};
  xnm.aio.m10T.ESPUpdater.prototype._sendNextNetPackage=function(d,c){var a=this;a._bytesSent<a._filesize&&(a._bytesSent+a._blockSize>a._filesize&&(a._blockSize=a._filesize-a._bytesSent),require("fs").readFileBinary(a._file,a._offset+a._bytesSent,a._blockSize,function(b,e){!b&&e?(b="443A",a._bytesSent+a._blockSize==a._filesize?b="453A":a._useNewHeader&&(b="4E3A"+XC.getHexVal(a._bytesSent/a._blockSize+1,4)),e=new Buffer(XC.getBytes(b+e,!0)),d.write(e),a._bytesSent+=a._blockSize):(d.destroy(),c&&c({error:"failed to read file",
  state:0}))}))};
  xnm.aio.m10T.ESPUpdater.prototype._createUpdateSocket=function(d,c,a){var b=this,e={port:c,host:d},f=require("net").connect(e);f.setTimeout(1500);f.setNoDelay&&f.setNoDelay(!1);f.setEncoding("binary");f.on("connect",function(){var a=new Buffer("UPD");b._useNewHeader&&0!=b._retries&&(a=new Buffer("UP2"));f.write(a)});f.on("data",function(e){e=new Buffer(e,"binary");if(2==e.length&&79==e[0]&&75==e[1])b._md5&&!b._md5Sent?(e=new Buffer("M:"+b._md5),f.write(e),b._md5Sent=!0):(b._retries=0,a&&a({error:null,
  state:Math.floor(b._bytesSent/b._filesize*100)}),b._bytesSent<b._filesize?(b._bytesSent+b._blockSize>b._filesize&&(b._blockSize=b._filesize-b._bytesSent),require("fs").readFileBinary(b._file,b._offset+b._bytesSent,b._blockSize,function(c,d){!c&&d?(c="443A",b._bytesSent+b._blockSize==b._filesize?c="453A":b._useNewHeader&&(c="4E3A"+XC.getHexVal(b._bytesSent/b._blockSize+1,4)),d=new Buffer(XC.getBytes(c+d,!0)),f.write(d),b._bytesSent+=b._blockSize):a&&a({error:"failed to read file",state:0})})):f.destroy());
  else if(5==e.length&&69==e[0]&&82==e[1]&&82==e[2]&&79==e[3]&&82==e[4]&&b._md5&&b._md5Sent&&0==b._bytesSent){b._md5=null;var g=a;a=null;setTimeout(function(){b.startNetUpdate(d,c,g)},0)}else 5==e.length&&69==e[0]&&82==e[1]&&82==e[2]&&79==e[3]&&82==e[4]&&b._useNewHeader&&b._bytesSent==b._blockSize?(b._useNewHeader=!1,g=a,a=null,setTimeout(function(){b.startNetUpdate(d,c,g)},0)):(f.destroy(),a&&a({error:"failed to flash block",state:0}))});f.on("error",function(b){a&&a({error:"socket error",state:0})});
  f.on("timeout",function(){b._useNewHeader&&2>b._retries?(b._retries++,b._bytesSent-=b._blockSize,f.destroy(),b._createUpdateSocket(d,c,a)):(a&&a({error:"socket timeout",state:0}),f.destroy())});f.on("close",function(){});f._doConnect&&"function"==typeof f._doConnect&&f._doConnect()};
  xnm.aio.m10T.ESPUpdater.prototype.startNetUpdate=function(d,c,a,b){this._callback=a;this._blockSize=496;this._retries=this._bytesSent=0;this._md5Sent=!1;var e=this;require("fs").stat(this._file,function(f,g){f?a&&a({error:"invalid update file",state:0}):(e._filesize=e._fixedSize?e._fixedSize:g.size,0<b?e._createUpdateSocket(d,c,function(f){f.error&&0<b?setTimeout(function(){e.startNetUpdate(d,c,a,b-1)},2E3):a(f)}):e._createUpdateSocket(d,c,a))})};
  xnm.aio.m10T.CC1310Updater=function(d,c,a,b){this._file=d;this._offset=this._filesize=0;c&&(this._offset=c);this._fixedSize=a;this._crc=null;b&&(this._crc=b);this._eraseFlash=!1;this._stmUSB=new xnm.aio.m10T.STMUSBSerial;this._flashingFinished=this._connected=!1;this._callback=null;this._blockSize=252;this._retries=this._bytesSent=0};
  xnm.aio.m10T.CC1310Updater.prototype._getNextPackage=function(d,c){var a=this;if(84==d[0]&&8==d.length&&(204==d[1]||51==d[1]))if(51==d[1]?a._retries++:a._retries=0,1==a._eraseFlash&&1!=d[2])d=a._stmUSB.generateCommandMessage([6,1],84),c(d);else if(0==a._eraseFlash&&0!=d[2])d=a._stmUSB.generateCommandMessage([6,0],84),c(d);else if(0==d[3])a._flashingFinished?(a._bytesSent=a._filesize,c([])):(d=a._stmUSB.generateCommandMessage([0],84),c(d));else if(5==d[3])a._flashingFinished=!0,d=a._stmUSB.generateCommandMessage([5],
  84),c(d);else if(1==d[3]||3==d[3]||4==d[3])a._bytesSent=(d[4]<<24)+(d[5]<<16)+(d[6]<<8)+d[7],a._bytesSent<a._filesize?(a._bytesSent+a._blockSize>a._filesize&&(a._blockSize=a._filesize-a._bytesSent),require("fs").readFileBinary(a._file,a._offset+a._bytesSent,a._blockSize,function(b,d){!b&&d?(b=[3,(a._bytesSent&4278190080)>>24,(a._bytesSent&16711680)>>16,(a._bytesSent&65280)>>8,a._bytesSent&255],b=b.concat(XC.getBytes(d,!0)),b=a._stmUSB.generateCommandMessage(b,84),c(b),a._bytesSent+=a._blockSize):
  c&&c(null)})):c([])};
  xnm.aio.m10T.CC1310Updater.prototype._createUpdateSocket=function(d,c,a){var b=this,e={port:c,host:d},f=require("net").connect(e);f.setTimeout(1E3);f.setNoDelay&&f.setNoDelay(!1);f.setEncoding("binary");var g=[];f.on("connect",function(){var a=new Buffer("SER");f.write(a)});f.on("data",function(c){c=new Buffer(c,"binary");for(var d=0;d<c.length;d++)g.push(c[d]);if(b._connected||2!=c.length||79!=c[0]||75!=c[1])if(b._connected)for(c=b._stmUSB.getMessageData(g);c;)b._getNextPackage(c,function(c){3<b._retries?
  (a&&a({error:"flashing failed ["+b._bytesSent+" bytes sent]",state:0}),f.destroy()):null!=c?(a&&a({error:null,state:Math.floor(b._bytesSent/b._filesize*100)}),0<c.length?f.write(new Buffer(c)):f.destroy()):a&&a({error:"failed to read file",state:0})}),c=b._stmUSB.getMessageData(g);else f.destroy(),a&&a({error:"failed to flash block",state:0});else b._connected=!0,g=[],c=b._stmUSB.generateCommandMessage([1],84),f.write(new Buffer(c))});f.on("error",function(b){a&&a({error:"socket error",state:0})});
  f.on("timeout",function(){2>b._retries?(b._retries++,b._connected=!1,b._bytesSent-=b._blockSize,0>b._bytesSent&&(b._bytesSent=0),f.destroy(),b._createUpdateSocket(d,c,a)):(a&&a({error:"socket timeout",state:0}),f.destroy())});f.on("close",function(){});f._doConnect&&"function"==typeof f._doConnect&&f._doConnect()};
  xnm.aio.m10T.CC1310Updater.prototype.startNetUpdate=function(d,c,a,b){this._callback=a;this._flashingFinished=this._connected=!1;this._retries=this._bytesSent=0;this._eraseFlash=!1;1==b&&(this._eraseFlash=!0);var e=this;require("fs").stat(this._file,function(b,g){b?a&&a({error:"invalid update file",state:0}):(e._filesize=e._fixedSize?e._fixedSize:g.size,e._createUpdateSocket(d,c,a))})};
  xnm.aio.m10T.CC1310Updater.prototype.startSerialUpdate=function(d,c,a){this._callback=c;this._flashingFinished=this._connected=!1;this._retries=this._bytesSent=0;this._eraseFlash=!1;1==a&&(this._eraseFlash=!0);var b=this;require("fs").stat(this._file,function(a,f){if(a)c&&c({error:"invalid update file",state:0});else{b._filesize=b._fixedSize?b._fixedSize:f.size;var e=new xnm.aio.m10T.STMUSBSerial,h=0,k=[];e.on("error",function(a){if(c){var b="serial port error";a&&a.message&&(b+=" ("+a.message+")");
  c({error:b,state:0})}});e.on("data",function(a){for(var d=0;d<a.length;d++)k.push(a[d]);for(a=b._stmUSB.getMessageData(k);a;)b._getNextPackage(a,function(a){if(3<b._retries)c&&c({error:"flashing failed ["+b._bytesSent+" bytes sent]",state:0}),e.close();else if(null!=a){var d=Math.floor(b._bytesSent/b._filesize*99);b._flashingFinished&&(d=100);c&&c({error:null,state:d});0<a.length?e.write(a,500):e.close()}else c&&c({error:"failed to read file",state:0})}),a=b._stmUSB.getMessageData(k)});e.on("timeout",
  function(){if(5>h){var a=b._stmUSB.generateCommandMessage([1],84);e.write(a,500);h++}else e.close(),c&&c({error:"failed to connect",state:0})});e.open(d,function(){setTimeout(function(){var a=b._stmUSB.generateCommandMessage([1],84);e.write(a,500)},250)})}})};xnm.aio.m10T.V5Updater=function(){this.auth=this.at=null;this.dfuTool=xnm.aio.m10T.V5Updater.DFU_TOOL.DFU_UTIL;this.useMD5=!1;this.useNewHeader=!0};xnm.aio.m10T.V5Updater.DFU_TOOL={DFU_UTIL:1,STM32:2};
  xnm.aio.m10T.V5Updater.prototype.parseHeader=function(d){var c=null,a=d.indexOf("HEADER@");0===a&&(c=d.substr(7),0<(a=c.indexOf("@HEADER"))&&(c=c.substr(0,a)));if(c&&c.length&&0<c.length&&(a=c.split("@"),2==a.length&&(c=a[0].split("_"),a=a[1].split("_"),5<=c.length&&5<=a.length))){var b=c[0],e=c[1],f=parseInt(c[c.length-2],10),g=c[c.length-1];c=c.slice(2,c.length-2).join("_");c=[b,e,c,f,g];e=a[0];f=a[1];var h=parseInt(a[a.length-2],10);b=a[a.length-1];a=a.slice(2,a.length-2).join("_");a=[e,f,a,h,
  b];e=g.split("-");2===e.length?(g=parseInt(e[0],10),c[4]=g,c.push(e[1])):(g=parseInt(g,10),c[4]=g);g=b.split("-");2===g.length?(b=parseInt(g[0],10),a[4]=b,a.push(g[1])):(b=parseInt(b,10),a[4]=b);return{esp:c,st:a,offset:d.length+1}}return null};
  xnm.aio.m10T.V5Updater.prototype.getFileInfos=function(d,c){var a=require("fs");a.stat(d,function(b,e){if(b)c(b,null);else{b=require("crypto");var f=a.createReadStream(d),g=b.createHash("md5"),h=new Buffer([]);g.setEncoding("hex");f.on("data",function(a){h=Buffer.concat([h,a])});f.on("end",function(){g.end();e.md5=g.read();e.crc32=xnm.aio.m10T.CRC32(h);c(null,e)});f.pipe(g)}})};
  xnm.aio.m10T.V5Updater.prototype.packFile=function(d,c,a,b,e,f,g){var h=require("fs"),k=this;k.getFileInfos(a,function(l,m){l?g&&g("invalid file"):k.getFileInfos(b,function(k,l){if(k)g&&g("invalid file");else{k="HEADER@"+("esp_"+e+"_"+d+"_0_"+m.size+"-"+m.md5)+"@"+("st_"+e+"_"+c+"_"+m.size+"_"+l.size+"-"+l.crc32)+"@HEADER\r\n";h.existsSync(f)&&h.unlinkSync(f);var n=h.createWriteStream(f);n.write(k);var p=h.createReadStream(a);p.on("end",function(){p=h.createReadStream(b);p.on("end",function(){n.end();
  g&&g()});p.pipe(n,{end:!1})});p.pipe(n,{end:!1})}})})};
  xnm.aio.m10T.V5Updater.prototype.packFileV5Plus=function(d,c,a,b,e,f,g){var h=require("fs");h.stat(a,function(k,l){k?g&&g("invalid file"):h.stat(b,function(k,n){if(k)g&&g("invalid file");else{k=0;var p="a20_"+e+"_"+d+"_"+k+"_"+l.size;k+=l.size;n="HEADER@"+p+"@"+("st_"+e+"_"+c+"_"+k+"_"+n.size)+"@HEADER\r\n";h.existsSync(f)&&h.unlinkSync(f);var m=h.createWriteStream(f);m.write(n);var q=h.createReadStream(a);q.on("end",function(){q=h.createReadStream(b);q.on("end",function(){m.end();g&&g()});q.pipe(m,
  {end:!1})});q.pipe(m,{end:!1})}})})};
  xnm.aio.m10T.V5Updater.prototype.updateFirmware=function(d,c,a,b){var e=require("fs"),f=this;e.existsSync(c)?e.stat(c,function(g,h){g||e.readFileLine(c,1,function(e,g){var h=!1;if(!e&&g){var k=f.parseHeader(g);if(k){e=null;f.useMD5&&(e=k.st[5]);var l=new xnm.aio.m10T.STMUpdater(c,k.offset+k.st[3],k.st[4],e);l.at=f.at;l.auth=f.auth;l.startNetUpdate(d,function(e){e.error&&a?a(e):(a&&a({error:null,state:Math.floor(e.state/2)}),100==e.state&&(e=null,f.useMD5&&(e=k.esp[5]),l=new xnm.aio.m10T.ESPUpdater(c,
  k.offset,k.esp[4],e),l.startNetUpdate(d,8081,function(b){b.error&&a?a(b):a&&a({error:null,state:Math.floor(50+b.state/2)})},b)))});h=!0}}!h&&a&&a({error:"[04] invalid firmware file",state:0})})}):a&&a({error:"[02] file does not exist",state:0})};
  xnm.aio.m10T.V5Updater.prototype.updateFirmwareInReverseOrder=function(d,c,a,b){var e=require("fs"),f=this;e.existsSync(c)?e.stat(c,function(g,h){g?a&&a({error:g.message,state:0}):e.readFileLine(c,1,function(e,g){var h=!1;if(!e&&g){var k=f.parseHeader(g);if(k){e=null;f.useMD5&&(e=k.esp[5]);var l=new xnm.aio.m10T.ESPUpdater(c,k.offset,k.esp[4],e);l.startNetUpdate(d,8081,function(b){if(b&&b.error)a&&a(b);else{var e=Math.floor(b.state/2);a&&a({error:null,state:e});if(100===b.state){var g=function(){var b=
  null;f.useMD5&&(b=k.st[5]);l=new xnm.aio.m10T.STMUpdater(c,k.offset+k.st[3],k.st[4],b);l.at=f.at;l.auth=f.auth;l.startNetUpdate(d,function(b){b&&b.error?a&&a(b):(b=Math.floor(50+b.state/2),a&&a({error:null,state:b}))})};setTimeout(function(){g()},9E4)}}},b);h=!0}}h||a&&a({error:"[04] invalid firmware file",state:0})})}):a&&a({error:"[02] file does not exist",state:0})};
  xnm.aio.m10T.V5Updater.prototype._updateFirmwareWithRetries_phase1=function(d,c,a,b,e,f){var g=this,h=null;this.useMD5&&(h=e.st[5]);h=new xnm.aio.m10T.STMUpdater(c,e.offset+e.st[3],e.st[4],h);h.at=g.at;h.auth=g.auth;h.startNetUpdate(d,function(h){h.error?(f&&f({error:h.error,state:h.state,phase:1,retriesLeft:b}),0===h.state&&0<b&&setTimeout(function(){g._updateFirmwareWithRetries_phase1(d,c,a,b-1,e,f)},2E3)):(f&&f({error:null,state:Math.floor(h.state/2)}),100==h.state&&g._updateFirmwareWithRetries_phase2(d,
  c,a,e,f))})};xnm.aio.m10T.V5Updater.prototype._updateFirmwareWithRetries_phase2=function(d,c,a,b,e){var f=this,g=null;this.useMD5&&(g=b.esp[5]);(new xnm.aio.m10T.ESPUpdater(c,b.offset,b.esp[4],g)).startNetUpdate(d,8081,function(g){g.error?(e&&e({error:g.error,state:g.state,phase:2,retriesLeft:a}),0<a&&setTimeout(function(){f._updateFirmwareWithRetries_phase2(d,c,a-1,b,e)},2E3)):e&&e({error:null,state:Math.floor(50+g.state/2)})})};
  xnm.aio.m10T.V5Updater.prototype.updateFirmwareWithRetries=function(d,c,a,b){var e=require("fs"),f=this;a=Number(a);0>a||isNaN(a)?b&&b({error:'[01] Invalid value for parameter "retries". Has to be a number >= 0.',state:0}):e.existsSync(c)?e.stat(c,function(g,h){g?b&&b({error:g.message,state:0}):e.readFileLine(c,1,function(e,g){!e&&g?(e=f.parseHeader(g))?f._updateFirmwareWithRetries_phase1(d,c,a,a,e,b):b&&b({error:"[03] Failed to parse firmware file header.",state:0}):b&&b({error:"[04] Invalid firmware file.",
  state:0})})}):b&&b({error:"[02] File does not exist.",state:0})};xnm.aio.m10T.V5Updater.prototype.isDFUConnected=function(d,c,a){this.dfuTool===xnm.aio.m10T.V5Updater.DFU_TOOL.STM32?this._isDFUConnected_STM32(d,c,a):this._isDFUConnected_DFUUtil(d,c,a)};
  xnm.aio.m10T.V5Updater.prototype._isDFUConnected_DFUUtil=function(d,c,a){var b=require("child_process").exec;d=b('"'+d+'" -l');var e=!1;d.stdout.on("data",function(a){a&&-1<a.toString().indexOf(c)&&(e=!0)});d.on("error",function(a){XC.debugf("error",a)});d.on("close",function(b){a&&a(e)})};
  xnm.aio.m10T.V5Updater.prototype._isDFUConnected_STM32=function(d,c,a){var b=require("child_process").exec;d=b('"'+d+'" -l usb');var e="";Array.isArray(c)||(c=[c]);d.stdout.on("data",function(a){a&&(e+=a.toString())});d.on("error",function(a){XC.debugf("STM32 tool error: "+a,"error")});d.on("close",function(b){b=!1;var d=null,f=null;XC.debugf(e);for(var k=0;k<c.length;k++){var l=c[k];if(-1<e.indexOf("Device ID              : "+l)){b=!0;d=l;k=e.split("\n");for(l=0;l<k.length;l++){var m=k[l];if(0<=
  m.indexOf("Device Index           : ")){f=m.split(":");f=f[f.length-1].trim();f=f.toLowerCase();break}}break}}a&&a(b,d,f)})};xnm.aio.m10T.V5Updater.prototype.waitForDFUConnect=function(d,c,a,b){var e=this;this.isDFUConnected(d,c,function(f,g,h){f?b&&b(!0,g,h):0<a?setTimeout(function(){e.waitForDFUConnect(d,c,a-.5,b)},500):b&&b(!1,g,h)})};
  xnm.aio.m10T.V5Updater.prototype.waitForSerialConnection=function(d,c,a){if(this._useChromeAPI)this._chromeWaitForSerialConnection(d,c,a);else{var b=this;require("serialport").list(function(e,f){void 0==f&&(f=[]);if(a&&f&&a.length<f.length)for(e=0;e<f.length;e++){for(var g=!0,h=0;h<a.length;h++)if(f[e].comName==a[h].comName){g=!1;break}if(g){var k=f[e].comName;setTimeout(function(){c&&c(k)},1E3)}}else 0<d?setTimeout(function(){b.waitForSerialConnection(d-.5,c,f)},500):c&&c(null)})}};
  xnm.aio.m10T.V5Updater.prototype._chromeWaitForSerialConnection=function(d,c,a){var b=this,e=function(a){setTimeout(function(){c&&c(a)},1E3)};chrome.serial.getDevices(function(f){Array.isArray(f)||(f=[]);if(a&&a.length<f.length)for(var g=0;g<f.length;g++){for(var h=!0,k=0;k<a.length;k++)if(f[g].path==a[k].path){h=!1;break}h&&e(f[g].path)}else 0<d?setTimeout(function(){b._chromeWaitForSerialConnection(d-.5,c,f)},500):c&&c(null)})};xnm.aio.m10T.Handler=function(){};
  xnm.aio.m10T.Handler.prototype.IBus=xnm.aio.m10T.IBus;xnm.aio.m10T.Handler.prototype.STMUpdater=xnm.aio.m10T.STMUpdater;xnm.aio.m10T.Handler.prototype.ESPUpdater=xnm.aio.m10T.ESPUpdater;xnm.aio.m10T.Handler.prototype.CC1310Updater=xnm.aio.m10T.CC1310Updater;xnm.aio.m10T.Handler.prototype.V5Updater=xnm.aio.m10T.V5Updater;xnm.aio.m10T.Handler.prototype.STMUSBSerial=xnm.aio.m10T.STMUSBSerial;"undefined"!=typeof exports&&"undefined"!=typeof module&&module.exports&&(exports=module.exports=new xnm.aio.m10T.Handler);
  