mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
24 lines
12 KiB
JavaScript
24 lines
12 KiB
JavaScript
function k(a){console.log(a)}function b(a){B&&console.log(a)}function v(a,e,f,h){p?(b("Opening is not yet possible, busy state on socket "+a),5>h?setTimeout(function(){k("opening later...");v(a,e,f,h+1)},500):(b("Force the closure of socket "+a),g[a]="tobeclosed")):c.isBusy()?(b("Opening is not yet possible, AT busy state on socket "+a),5>h?setTimeout(function(){k("opening later...");v(a,e,f,h+1)},500):(b("Force the closure of socket "+a),g[a]="tobeclosed")):(b("AT+QIOPEN=1,"+a+',"TCP",'+
|
|
JSON.stringify(e)+","+f+",0,1"),c.cmd("AT+QIOPEN=1,"+a+',"TCP",'+JSON.stringify(e)+","+f+",0,1\r\n",15E3,function d(c){if("OK"==c)return d;c=="+QIOPEN: "+a+",0"?(b(c),b("AT+QIOPEN completed with socket: "+a),g[a]=!0):c=="+QIOPEN: "+a+",565"?(b("AT+QIOPEN failure DNS parse failed..."),5>h?setTimeout(function(){k("repeat opening the socket ...");v(a,e,f,h+1)},1E3):(b("Force the closure of socket "+a),g[a]="tobeclosed")):c=="+QIOPEN: "+a+",566"?(b("AT+QIOPEN failure could not connect socket ..."),5>
|
|
h?setTimeout(function(){k("repeat opening the socket ...");v(a,e,f,h+1)},3E3):(b("Force the closure of socket "+a),g[a]="tobeclosed")):(c=="+QIOPEN: "+a+",563"?b("AT+QIOPEN socket identity has been used..., socket is:"+a):b("AT+QIOPEN failed on socket:"+a),g[a]="tobeclosed");return""}))}function q(a,e){g[a]?0<m[a].length&&5>e?(k("socket "+a+" is emptying..."),setTimeout(function(){k("closing later...");q(a,e+1)},500)):p?(b("at register currenly programmed"),5>e?setTimeout(function(){k("closing later...");
|
|
q(a,e+1)},500):(b("several busy situation on socket "+a),g[a]=void 0)):c.isBusy()?(b("AT busy"),5>e?setTimeout(function(){k("closing again...");q(a,e+1)},500):(b("several AT busy situation on socket "+a),g[a]=void 0)):(b("sending AT+QICLOSE for socket "+a),c.cmd("AT+QICLOSE="+a+"\r\n",2E3,function G(c){if(void 0===c)return 5>e?setTimeout(function(){k("closing again...");q(a,e+1)},500):(b("cannot properly close socket "+a),g[a]=void 0),"";if("OK"==c)return b("socket "+a+" is properly closed"),g[a]=
|
|
void 0,"";b("socket "+a+" is not yet closed");return G})):b("socket already closed")}function C(a){var e=a.indexOf("\r\n");if(0>e)return a;var b=a.substring(15,e).split(",");b[1]|=0;var h=a.length-(e+2);if(h>=b[1])return 1==g[b[0]]&&(m[b[0]]+=a.substr(e+2,b[1])),a.substr(e+b[1]+3);m[b[0]]+=a.substr(e+2,h);c.getData(b[1]-h,function(a){m[b[0]]+=a});return""}function H(a){b("closehandler:"+a);var e=a.indexOf("\r\n");if(0>e&&(b("not enough data "+a),e=a.indexOf("\r"),0>e))return b("definitively not enough data "+
|
|
a),a;a=a.substring(0,e).split(",");a[1]|=0;q(a[1],0);return""}function I(a){b("pdpdeacthandler:"+a);c.cmd("AT+QIDEACT=1\r\n",1E3,function(a){b(a);for(a=0;12>a;a++)g[a]=void 0,m[a]=""});return""}function J(a){b("QindHandler in: "+a);var e=a.indexOf("\r\n"),c=a.substr(e,a.length);k(a.substr(e,a.length));return c}function K(a){b("QusimHandler in: "+a);var e=a.indexOf("\r\n"),c=a.substr(e,a.length);k(a.substr(e,a.length));return c}function L(a){b("CfunHandler in: "+a);var e=a.indexOf("\r\n"),c=a.substr(e,
|
|
a.length);k(a.substr(e,a.length));return c}function M(a){b("RdyHandler in: "+a);var c=a.indexOf("\r\n"),f=a.substr(c,a.length);k(a.substr(c,a.length));return f}function N(a){b("PowerDownHandler in: "+a);k("Modem is entering in power down");digitalWrite(w,y);return""}var c,p=!1,g=[],m=" ".split(" "),r,z,w,y,D=!1,t="",A=!1,B=!1,F,u=0,n=0,O={create:function(a,e){var f=0;if(void 0===a)return b("WARNING: this has not been fully ported for UGxx"),f=12,g[f]="Wait",m[f]="",c.cmd("AT+CIPSERVER=1,"+
|
|
e+"\r\n",1E4,function(a){if("OK"==a)g[f]=!0;else throw g[f]=void 0,Error("CIPSERVER failed");}),12;for(;void 0!==g[f];)f++;12<=f&&(b("WORKAROUND closing the socket: "+f),f--,q(f,0));g[f]="Wait";m[f]="";443==e?(b("delaying the TLS socket opening"),setTimeout(function(){v(f,a,e,0)},3E3)):v(f,a,e,0);b("(create) sckt "+f+" state = "+g[f]);return f},close:function(a){b("(local) Closing of the socket: "+a);q(a,0)},accept:function(a){b("Accept Socket "+a);for(a=0;12>a;a++)if(m[a]&&void 0===g[a])return g[a]=
|
|
!0,a;return-1},recv:function(a,b){if(c.isBusy()||"Wait"==g[a])return"";if(m[a]){if(m[a].length>b){var e=m[a].substr(0,b);m[a]=m[a].substr(b)}else e=m[a],m[a]="";return e}return g[a]&&"tobeclosed"!=g[a]?"":-1},send:function(a,e){if(p||c.isBusy()||"Wait"==g[a])return 0;if("tobeclosed"==g[a]||!g[a])return-1;if(1460<e.length)return b("data too big"),-1;p=!0;n=setTimeout(function(){b("Abort waiting modem response (sending data) on socket "+a);p=!1;n=0;c.unregisterLine("SEND OK");c.unregisterLine("SEND FAIL");
|
|
c.unregisterLine("ERROR");g[a]="tobeclosed"},6E3);u=setTimeout(function(){b("Abort waiting prompt (for sending data) on socket "+a);p=!1;u=0;n&&(clearTimeout(n),n=0,c.unregisterLine("SEND OK"),c.unregisterLine("SEND FAIL"),c.unregisterLine("ERROR"));c.unregister(">");g[a]="tobeclosed"},5E3);c.register(">",function(){b("Prompt coming, sending data ...");c.unregister(">");clearTimeout(u);u=0;b("writing data amount of "+e.length);c.write(e);return""});c.registerLine("SEND OK",function(){b("UGxx - SEND OK");
|
|
c.unregisterLine("SEND OK");c.unregisterLine("SEND FAIL");c.unregisterLine("ERROR");clearTimeout(n);n=0;p=!1;return""});c.registerLine("SEND FAIL",function(a){b("UGxx - SEND FAIL");c.unregisterLine("SEND OK");c.unregisterLine("SEND FAIL");c.unregisterLine("ERROR");clearTimeout(n);n=0;p=!1;g[a]="tobeclosed";return""});c.registerLine("ERROR",function(a){b("UGxx - ERROR communication");u&&(c.unregister(">"),clearTimeout(u),u=0);c.unregisterLine("SEND OK");c.unregisterLine("SEND FAIL");c.unregisterLine("ERROR");
|
|
clearTimeout(n);n=0;p=!1;g[a]="tobeclosed";return""});b("AT+QISEND="+a+","+e.length);c.write("AT+QISEND="+a+","+e.length+"\r\n");b("(send) sckt "+a+" state = "+g[a]);b("send "+e.length);return e.length}},x={receiveHandler:C,debug:function(a,b){B=void 0===a?!1:a;(void 0===b?0:b)&&c.debug();return{socks:g,sockData:m}},init:function(a){var e=0,f=0,h=0,g=!1,l=function(d){switch(h){case 0:b("debug AT_SYNCHRO :"+d);if("AT"===d)return l;"OK"===d?(k("Synchronisation with module passed"),h=1,c.cmd("ATV1\r\n",
|
|
1E3,l)):(h=0,e++,b("AT sync failed: "+d),10>=e?setTimeout(function(){c.cmd("AT\r\n",1E3,l)},500):(b("No OK return after 10 times"),b("Check the module is power on"),a("Error in AT sync: "+d)));break;case 1:b("debug AT_RSP_FORMAT :"+d);if("OK"===d)h=2,c.cmd("ATE0\r\n",1E3,l);else{if("ATV1"===d)return l;d&&a("Error in ATV1: "+d)}void 0===d&&(b("Cannot set TA response format"),a("ATV1 time-out !!!"));break;case 2:b("debug AT_ECHO_OFF :"+d);if("IIIIATE0"===d||d==="IIII"+String.fromCharCode(255)+"ATE0"||
|
|
"ATE0"===d)return l;"OK"===d?(b("ATE0 passed: "+d),D?(h=11,c.cmd("AT+IFC=2,2\r\n",2E3,l)):(h=3,c.cmd("AT+CPIN?\r\n",5E3,l))):"atE0"===d?(b("UGxx returns "+d),h=3,c.cmd("AT+CPIN?\r\n",5E3,l)):d&&a("Error in ATE0: "+d);void 0===d&&(b("ATE0 time-out !!!"),h=3,c.cmd("AT+CPIN?\r\n",5E3,l));break;case 11:b("debug AT_HW_FLOW_CONTROL :"+d);"OK"===d?b("HW flow control establismnent succeeded"):a("HW flow control establismnent failed");h=3;c.cmd("AT+CPIN?\r\n",5E3,l);break;case 3:b("debug AT_CPIN :"+d);if("+CPIN: READY"===
|
|
d)return l;"OK"===d?(h=4,c.cmd("AT+QCCID\r\n",2E3,l)):d&&a("Error in CPIN: "+d);void 0===d&&(b("SIM cannot be read, SIM is either protected, blocked or not accessible"),a("AT+CPIN time-out !!!"));break;case 4:b("debug AT_SHOW_SIM_ID :"+d);if(d&&"+QCCID:"==d.substr(0,7))return b("SIM ID :"+d),F=d.substring(8,d.length-1),l;"OK"===d?(h=5,c.cmd("AT+CFUN=1\r\n",15E3,l)):d&&a("Error in QCCID: "+d);void 0===d&&a("AT+QCCID time-out !!!");break;case 5:b("debug AT_RADIO_ON :"+d);"OK"===d?(h=6,c.cmd("AT+CSQ\r\n",
|
|
2E3,l)):d&&a("Error in CFUN: "+d);void 0===d&&a("AT+CFUN time-out !!!");break;case 6:b("debug AT_QUERY_SIGNAL_QUALITY :"+d);if(d&&"+CSQ:"==d.substr(0,5))return d&&"+CSQ: 99,99"==d.substr(0,11)?(b("Signal not known or not detectable yet"),g=!1):(g=!0,d=d.substring(6,d.length).split(","),d[0]|=0,k("quality_level_dbm = "+(-113+2*d[0])+"dBm")),l;"OK"===d?g?(h=7,b("PS attachment is starting. It may take until a minute, please wait ... "),setTimeout(function(){c.cmd("AT+CGATT=1\r\n",75E3,l)},5E3)):setTimeout(function(){c.cmd("AT+CSQ\r\n",
|
|
2E3,l)},1E3):d&&a("Error in QCCID: "+d);void 0===d&&a("AT+CSQ time-out !!!");break;case 7:b("debug AT_PS_ATTACMENT :"+d);"OK"===d?(k("PS attachment succeeded"),h=10,c.cmd("AT+COPS?\r\n",2E4,l)):d&&(f+=1,b("Error in CGATT: "+d+" - retry: "+f),1==f?(b("Trying an automatic registration first. It may take until 3 minutes, please wait ..."),h=8,setTimeout(function(){c.cmd("AT+COPS=0\r\n",18E4,l)},2E3)):4>f?(b("Retrying CGATT with retry "+f),h=7,b("PS attacment is attempting again. It may take until a minute, please wait ... "),
|
|
setTimeout(function(){c.cmd("AT+CGATT=1\r\n",75E3,l)},2E3*f)):a("Unrecoverable Error, PS attachment failed: "+d));break;case 8:b("COPS returns: "+d);h=9;setTimeout(function(){c.cmd("AT+COPS?\r\n",1E4,l)},5E3);break;case 9:b(d+" - Now retrying PS attachment");h=7;setTimeout(function(){c.cmd("AT+CGATT=1\r\n",3E4,l)},3E4);break;case 10:b("debug AT_CURRENT_OPERATOR :"+d);if("OK"===d)a(null);else{if(d&&"+COPS:"==d.substr(0,6))return k("currently selected operator :"+d),l;d&&a("Error in COPS? "+d)}void 0===
|
|
d&&a("AT+COPS? time-out !!!")}};setTimeout(function(){c.cmd("AT\r\n",1E3,l)},500)},reset:function(a){function c(a){digitalWrite(r,z);setTimeout(function(){digitalWrite(r,!z);setTimeout(function(){x.init(a)},6E3)},200)}function f(a){digitalWrite(w,y);setTimeout(function(){digitalWrite(w,!y);setTimeout(function(){r?c(a):x.init(a)},5E3)},200)}for(var h=0;12>h;h++)g[h]=void 0,m[h]="";if(void 0===r&&void 0===w)return x.init(a);b("Here we go trhough reset sequence");r&&digitalWrite(r,!z);w?f(a):c(a);k("Cellular module initialization started, please wait ...")},
|
|
getVersion:function(a){c.cmd("AT+GMR\r\n",1E3,function(b){a(null,b)})},connect:function(a,e,f,h){var g=0,l=function(a){switch(g){case 0:b("connect callback after PDP context configuration");"OK"===a?(g=1,b("PDP context successfully configured"),c.cmd("AT+QIACT=1\r\n",3E4,l)):a&&h("Error in "+g+": "+a);void 0===a&&b("PDP context activation failed, timeout...");break;case 1:b("connect callback after PDP context activation"),"OK"===a?(k("PDP context successfully activated"),h(null)):a?h("Error in "+
|
|
g+": "+a):h(null)}};b("in the connect function: AT+QICSGP stage");c.cmd('AT+QICSGP=1,1,"'+a+'", "'+e+'", "'+f+'"\r\n',3E4,l)},initflowctrl:function(a){D=void 0===a?!1:a},getIP:function(a){var e="",f=function(c){b("AT+CGPADDR callback: "+c);if(void 0===c)b("timeout : any IP address allocated ..."),a(null,e);else if("OK"===c)a(null,e),k("IP address allocated, modem is ready to use");else return e+=c,b(c),f};b("getIP is AT+CGPADDR");c.cmd("AT+CGPADDR=1\r\n",3E4,f)},geoLocGet:function(a){k("Getting geolocalization data");
|
|
a(t)},geoLocStart:function(a){k("Starting GeoLocalization");1E4>a&&k("unpredictive behaviour with period not greater than 10 s !");A=!0;var e=function(f){if(void 0===f)b("GeoLocalization timeout"),t="";else if("OK"===f)k("GeoLocalization acquisition done : "+t);else{if(f&&"+QCELLLOC:"==f.substr(0,10)){var g=f.length,m=f.indexOf(" ");t=f.substring(1+m,g);b(t);return e}t="";k("Geolocalization error : "+f)}A&&setTimeout(function(){c.cmd("AT+QCELLLOC=1\r\n",2E3,e)},a)};c.cmd("AT+QCELLLOC=1\r\n",2E3,e)},
|
|
geoLocStop:function(){k("Stopping GeoLocalization");A=!1;t=""},turnOff:function(){k("Turning Off the modem");c.cmd("AT+QPOWD=1\r\n",2E3,function(a){"OK"===a?k("Please wait, disconnecting and saving data. It may last until 60 s"):k("Turn off error : "+a)})},getCCID:function(){return F}};resetOptions={rst:void 0,pwrkey:void 0,rst_active_level:1,pwrkey_active_level:1};exports.connect=function(a,b,f){b=b||{};r=b.rst||void 0;w=b.pwrkey||void 0;z=b.rst_active_level||0;y=b.pwrkey_active_level||0;x.at=c=
|
|
require("AT").connect(a);require("NetworkJS").create(O);c.register('+QIURC: "recv"',C);c.register('+QIURC: "closed"',H);c.register('+QIURC: "pdpdeact"',I);c.register("+QIND:",J);c.register("+QUSIM:",K);c.register("+CFUN:",L);c.register("RDY",M);c.register("POWERED DOWN",N);x.reset(f);return x} |