You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

758 lines
18 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. * @file TinyGsmClientMC60.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. *
  8. * @MC60 support added by Tamas Dajka 2017.10.15 - with fixes by Sara Damiano
  9. *
  10. */
  11. #ifndef TinyGsmClientMC60_h
  12. #define TinyGsmClientMC60_h
  13. //#pragma message("TinyGSM: TinyGsmClientMC60")
  14. //#define TINY_GSM_DEBUG Serial
  15. #if !defined(TINY_GSM_RX_BUFFER)
  16. #define TINY_GSM_RX_BUFFER 64
  17. #endif
  18. #define TINY_GSM_MUX_COUNT 6
  19. #include <TinyGsmCommon.h>
  20. #define GSM_NL "\r\n"
  21. static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
  22. static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
  23. enum SimStatus {
  24. SIM_ERROR = 0,
  25. SIM_READY = 1,
  26. SIM_LOCKED = 2,
  27. SIM_ANTITHEFT_LOCKED = 3,
  28. };
  29. enum RegStatus {
  30. REG_UNREGISTERED = 0,
  31. REG_SEARCHING = 2,
  32. REG_DENIED = 3,
  33. REG_OK_HOME = 1,
  34. REG_OK_ROAMING = 5,
  35. REG_UNKNOWN = 4,
  36. };
  37. class TinyGsmMC60
  38. {
  39. public:
  40. class GsmClient : public Client
  41. {
  42. friend class TinyGsmMC60;
  43. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  44. public:
  45. GsmClient() {}
  46. GsmClient(TinyGsmMC60& modem, uint8_t mux = 1) {
  47. init(&modem, mux);
  48. }
  49. virtual ~GsmClient(){}
  50. bool init(TinyGsmMC60* modem, uint8_t mux = 1) {
  51. this->at = modem;
  52. this->mux = mux;
  53. sock_available = 0;
  54. sock_connected = false;
  55. got_data = false;
  56. at->sockets[mux] = this;
  57. return true;
  58. }
  59. public:
  60. virtual int connect(const char *host, uint16_t port, int timeout_s) {
  61. stop();
  62. TINY_GSM_YIELD();
  63. rx.clear();
  64. sock_connected = at->modemConnect(host, port, mux, false, timeout_s);
  65. return sock_connected;
  66. }
  67. TINY_GSM_CLIENT_CONNECT_OVERLOADS()
  68. virtual void stop(uint32_t maxWaitMs) {
  69. TINY_GSM_CLIENT_DUMP_MODEM_BUFFER()
  70. at->sendAT(GF("+QICLOSE="), mux);
  71. sock_connected = false;
  72. at->waitResponse((maxWaitMs - (millis() - startMillis)), GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
  73. }
  74. virtual void stop() { stop(75000L); }
  75. TINY_GSM_CLIENT_WRITE()
  76. TINY_GSM_CLIENT_AVAILABLE_NO_BUFFER_CHECK()
  77. TINY_GSM_CLIENT_READ_NO_BUFFER_CHECK()
  78. TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED()
  79. /*
  80. * Extended API
  81. */
  82. String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  83. private:
  84. TinyGsmMC60* at;
  85. uint8_t mux;
  86. uint16_t sock_available;
  87. bool sock_connected;
  88. bool got_data;
  89. RxFifo rx;
  90. };
  91. // class GsmClientSecure : public GsmClient
  92. // {
  93. // public:
  94. // GsmClientSecure() {}
  95. //
  96. // GsmClientSecure(TinyGsmMC60& modem, uint8_t mux = 1)
  97. // : GsmClient(modem, mux)
  98. // {}
  99. //
  100. // virtual ~GsmClientSecure(){}
  101. //
  102. // public:
  103. // virtual int connect(const char *host, uint16_t port, int timeout_s) {
  104. // stop();
  105. // TINY_GSM_YIELD();
  106. // rx.clear();
  107. // sock_connected = at->modemConnect(host, port, mux, true, timeout_s);
  108. // return sock_connected;
  109. // }
  110. // };
  111. public:
  112. TinyGsmMC60(Stream& stream)
  113. : stream(stream)
  114. {
  115. memset(sockets, 0, sizeof(sockets));
  116. }
  117. virtual ~TinyGsmMC60() {}
  118. /*
  119. * Basic functions
  120. */
  121. bool begin(const char* pin = NULL) {
  122. return init(pin);
  123. }
  124. bool init(const char* pin = NULL) {
  125. DBG(GF("### TinyGSM Version:"), TINYGSM_VERSION);
  126. if (!testAT()) {
  127. return false;
  128. }
  129. sendAT(GF("&FZ")); // Factory + Reset
  130. waitResponse();
  131. sendAT(GF("E0")); // Echo Off
  132. if (waitResponse() != 1) {
  133. return false;
  134. }
  135. DBG(GF("### Modem:"), getModemName());
  136. getSimStatus();
  137. return true;
  138. }
  139. String getModemName() {
  140. #if defined(TINY_GSM_MODEM_MC60)
  141. return "Quectel MC60";
  142. #elif defined(TINY_GSM_MODEM_MC60E)
  143. return "Quectel MC60E";
  144. #endif
  145. return "Quectel MC60";
  146. }
  147. TINY_GSM_MODEM_SET_BAUD_IPR()
  148. TINY_GSM_MODEM_TEST_AT()
  149. TINY_GSM_MODEM_MAINTAIN_LISTEN()
  150. bool factoryDefault() {
  151. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  152. waitResponse();
  153. sendAT(GF("+IPR=0")); // Auto-baud
  154. waitResponse();
  155. sendAT(GF("&W")); // Write configuration
  156. return waitResponse() == 1;
  157. }
  158. TINY_GSM_MODEM_GET_INFO_ATI()
  159. /*
  160. * under development
  161. */
  162. // bool hasSSL() {
  163. // sendAT(GF("+QIPSSL=?"));
  164. // if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) {
  165. // return false;
  166. // }
  167. // return waitResponse() == 1;
  168. // }
  169. bool hasSSL() {
  170. return false; // TODO: For now
  171. }
  172. bool hasWifi() {
  173. return false;
  174. }
  175. bool hasGPRS() {
  176. return true;
  177. }
  178. /*
  179. * Power functions
  180. */
  181. bool restart() {
  182. if (!testAT()) {
  183. return false;
  184. }
  185. sendAT(GF("+CFUN=0"));
  186. if (waitResponse(10000L) != 1) {
  187. return false;
  188. }
  189. sendAT(GF("+CFUN=1,1"));
  190. if (waitResponse(10000L) != 1) {
  191. return false;
  192. }
  193. delay(3000);
  194. return init();
  195. }
  196. bool poweroff() {
  197. sendAT(GF("+QPOWD=1"));
  198. return waitResponse(GF("NORMAL POWER DOWN")) == 1;
  199. }
  200. bool radioOff() {
  201. if (!testAT()) {
  202. return false;
  203. }
  204. sendAT(GF("+CFUN=0"));
  205. if (waitResponse(10000L) != 1) {
  206. return false;
  207. }
  208. delay(3000);
  209. return true;
  210. }
  211. bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED;
  212. /*
  213. * SIM card functions
  214. */
  215. TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
  216. TINY_GSM_MODEM_GET_SIMCCID_CCID()
  217. TINY_GSM_MODEM_GET_IMEI_GSN()
  218. SimStatus getSimStatus(unsigned long timeout_ms = 10000L) {
  219. for (unsigned long start = millis(); millis() - start < timeout_ms; ) {
  220. sendAT(GF("+CPIN?"));
  221. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  222. delay(1000);
  223. continue;
  224. }
  225. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED"), GF("PH_SIM PIN"), GF("PH_SIM PUK"));
  226. waitResponse();
  227. switch (status) {
  228. case 2:
  229. case 3: return SIM_LOCKED;
  230. case 5:
  231. case 6: return SIM_ANTITHEFT_LOCKED;
  232. case 1: return SIM_READY;
  233. default: return SIM_ERROR;
  234. }
  235. }
  236. return SIM_ERROR;
  237. }
  238. TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
  239. TINY_GSM_MODEM_GET_OPERATOR_COPS()
  240. /*
  241. * Generic network functions
  242. */
  243. TINY_GSM_MODEM_GET_CSQ()
  244. bool isNetworkConnected() {
  245. RegStatus s = getRegistrationStatus();
  246. return (s == REG_OK_HOME || s == REG_OK_ROAMING);
  247. }
  248. TINY_GSM_MODEM_WAIT_FOR_NETWORK()
  249. /*
  250. * GPRS functions
  251. */
  252. bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
  253. gprsDisconnect();
  254. // select foreground context 0 = VIRTUAL_UART_1
  255. sendAT(GF("+QIFGCNT=0"));
  256. if (waitResponse() != 1) {
  257. return false;
  258. }
  259. //Select GPRS (=1) as the Bearer
  260. sendAT(GF("+QICSGP=1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\""));
  261. if (waitResponse() != 1) {
  262. return false;
  263. }
  264. //Define PDP context - is this necessary?
  265. sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"');
  266. waitResponse();
  267. // Activate PDP context - is this necessary?
  268. sendAT(GF("+CGACT=1,1"));
  269. waitResponse(60000L);
  270. //Start TCPIP Task and Set APN, User Name and Password
  271. sendAT("+QIREGAPP=\"", apn, "\",\"", user, "\",\"", pwd, "\"" );
  272. if (waitResponse() != 1) {
  273. return false;
  274. }
  275. //Activate GPRS/CSD Context
  276. sendAT(GF("+QIACT"));
  277. if (waitResponse(60000L) != 1) {
  278. return false;
  279. }
  280. //Enable multiple TCP/IP connections
  281. sendAT(GF("+QIMUX=1"));
  282. if (waitResponse() != 1) {
  283. return false;
  284. }
  285. //Request an IP header for received data ("IPD(data length):")
  286. sendAT(GF("+QIHEAD=1"));
  287. if (waitResponse() != 1) {
  288. return false;
  289. }
  290. //Set Method to Handle Received TCP/IP Data - Retrieve Data by Command
  291. sendAT(GF("+QINDI=1"));
  292. if (waitResponse() != 1) {
  293. return false;
  294. }
  295. // Check that we have a local IP address
  296. if (localIP() != IPAddress(0,0,0,0)) {
  297. return true;
  298. }
  299. return false;
  300. }
  301. bool gprsDisconnect() {
  302. sendAT(GF("+QIDEACT"));
  303. return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
  304. }
  305. TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED()
  306. /*
  307. * IP Address functions
  308. */
  309. String getLocalIP() {
  310. sendAT(GF("+QILOCIP"));
  311. stream.readStringUntil('\n');
  312. String res = stream.readStringUntil('\n');
  313. res.trim();
  314. return res;
  315. }
  316. IPAddress localIP() {
  317. return TinyGsmIpFromString(getLocalIP());
  318. }
  319. /*
  320. * Messaging functions
  321. */
  322. String sendUSSD(const String& code) {
  323. sendAT(GF("+CMGF=1"));
  324. waitResponse();
  325. sendAT(GF("+CSCS=\"HEX\""));
  326. waitResponse();
  327. sendAT(GF("+CUSD=1,\""), code, GF("\""));
  328. if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
  329. return "";
  330. }
  331. stream.readStringUntil('"');
  332. String hex = stream.readStringUntil('"');
  333. stream.readStringUntil(',');
  334. int dcs = stream.readStringUntil('\n').toInt();
  335. if (waitResponse() != 1) {
  336. return "";
  337. }
  338. if (dcs == 15) {
  339. return TinyGsmDecodeHex8bit(hex);
  340. } else if (dcs == 72) {
  341. return TinyGsmDecodeHex16bit(hex);
  342. } else {
  343. return hex;
  344. }
  345. }
  346. bool sendSMS(const String& number, const String& text) {
  347. sendAT(GF("+CMGF=1"));
  348. waitResponse();
  349. //Set GSM 7 bit default alphabet (3GPP TS 23.038)
  350. sendAT(GF("+CSCS=\"GSM\""));
  351. waitResponse();
  352. sendAT(GF("+CMGS=\""), number, GF("\""));
  353. if (waitResponse(GF(">")) != 1) {
  354. return false;
  355. }
  356. stream.print(text);
  357. stream.write((char)0x1A);
  358. stream.flush();
  359. return waitResponse(60000L) == 1;
  360. }
  361. bool sendSMS_UTF16(const String& number, const void* text, size_t len) {
  362. sendAT(GF("+CMGF=1"));
  363. waitResponse();
  364. sendAT(GF("+CSCS=\"HEX\""));
  365. waitResponse();
  366. sendAT(GF("+CSMP=17,167,0,8"));
  367. waitResponse();
  368. sendAT(GF("+CMGS=\""), number, GF("\""));
  369. if (waitResponse(GF(">")) != 1) {
  370. return false;
  371. }
  372. uint16_t* t = (uint16_t*)text;
  373. for (size_t i=0; i<len; i++) {
  374. uint8_t c = t[i] >> 8;
  375. if (c < 0x10) { stream.print('0'); }
  376. stream.print(c, HEX);
  377. c = t[i] & 0xFF;
  378. if (c < 0x10) { stream.print('0'); }
  379. stream.print(c, HEX);
  380. }
  381. stream.write((char)0x1A);
  382. stream.flush();
  383. return waitResponse(60000L) == 1;
  384. }
  385. /** Delete all SMS */
  386. bool deleteAllSMS() {
  387. sendAT(GF("+QMGDA=6"));
  388. if (waitResponse(waitResponse(60000L, GF("OK"), GF("ERROR")) == 1) ) {
  389. return true;
  390. }
  391. return false;
  392. }
  393. /*
  394. * Location functions
  395. */
  396. String getGsmLocation() {
  397. sendAT(GF("+CIPGSMLOC=1,1"));
  398. if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) {
  399. return "";
  400. }
  401. String res = stream.readStringUntil('\n');
  402. waitResponse();
  403. res.trim();
  404. return res;
  405. }
  406. /*
  407. * Battery & temperature functions
  408. */
  409. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  410. uint16_t getBattVoltage() {
  411. sendAT(GF("+CBC"));
  412. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  413. return 0;
  414. }
  415. streamSkipUntil(','); // Skip battery charge status
  416. streamSkipUntil(','); // Skip battery charge level
  417. // return voltage in mV
  418. uint16_t res = stream.readStringUntil(',').toInt();
  419. // Wait for final OK
  420. waitResponse();
  421. return res;
  422. }
  423. int8_t getBattPercent() {
  424. sendAT(GF("+CBC"));
  425. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  426. return false;
  427. }
  428. streamSkipUntil(','); // Skip battery charge status
  429. // Read battery charge level
  430. int res = stream.readStringUntil(',').toInt();
  431. // Wait for final OK
  432. waitResponse();
  433. return res;
  434. }
  435. uint8_t getBattChargeState() {
  436. sendAT(GF("+CBC?"));
  437. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  438. return false;
  439. }
  440. // Read battery charge status
  441. int res = stream.readStringUntil(',').toInt();
  442. // Wait for final OK
  443. waitResponse();
  444. return res;
  445. }
  446. bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) {
  447. sendAT(GF("+CBC?"));
  448. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  449. return false;
  450. }
  451. chargeState = stream.readStringUntil(',').toInt();
  452. percent = stream.readStringUntil(',').toInt();
  453. milliVolts = stream.readStringUntil('\n').toInt();
  454. // Wait for final OK
  455. waitResponse();
  456. return true;
  457. }
  458. float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE;
  459. /*
  460. * Client related functions
  461. */
  462. protected:
  463. bool modemConnect(const char* host, uint16_t port, uint8_t mux,
  464. bool ssl = false, int timeout_s = 75)
  465. {
  466. uint32_t timeout_ms = ((uint32_t)timeout_s)*1000;
  467. sendAT(GF("+QIOPEN="), mux, GF("\"TCP"), GF("\",\""), host, GF("\","), port);
  468. int rsp = waitResponse(timeout_ms,
  469. GF("CONNECT OK" GSM_NL),
  470. GF("CONNECT FAIL" GSM_NL),
  471. GF("ALREADY CONNECT" GSM_NL));
  472. return (1 == rsp);
  473. }
  474. int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
  475. sendAT(GF("+QISEND="), mux, ',', len);
  476. if (waitResponse(GF(">")) != 1) {
  477. return 0;
  478. }
  479. stream.write((uint8_t*)buff, len);
  480. stream.flush();
  481. if (waitResponse(GF(GSM_NL "SEND OK")) != 1) {
  482. return 0;
  483. }
  484. bool allAcknowledged = false;
  485. // bool failed = false;
  486. while ( !allAcknowledged ) {
  487. sendAT( GF("+QISACK"));
  488. if (waitResponse(5000L, GF(GSM_NL "+QISACK:")) != 1) {
  489. return -1;
  490. } else {
  491. streamSkipUntil(','); /** Skip total */
  492. streamSkipUntil(','); /** Skip acknowledged data size */
  493. if ( stream.readStringUntil('\n').toInt() == 0 ) {
  494. allAcknowledged = true;
  495. }
  496. }
  497. }
  498. waitResponse(5000L);
  499. // streamSkipUntil(','); // Skip mux
  500. // return stream.readStringUntil('\n').toInt();
  501. return len; // TODO
  502. }
  503. size_t modemRead(size_t size, uint8_t mux) {
  504. // TODO: Does this work????
  505. // AT+QIRD=<id>,<sc>,<sid>,<len>
  506. // id = GPRS context number - 0, set in GPRS connect
  507. // sc = roll in connection - 1, client of connection
  508. // sid = index of connection - mux
  509. // len = maximum length of data to send
  510. sendAT(GF("+QIRD=0,1,"), mux, ',', size);
  511. // sendAT(GF("+QIRD="), mux, ',', size);
  512. if (waitResponse(GF("+QIRD:")) != 1) {
  513. return 0;
  514. }
  515. streamSkipUntil(':'); // skip IP address
  516. streamSkipUntil(','); // skip port
  517. streamSkipUntil(','); // skip connection type (TCP/UDP)
  518. size_t len = stream.readStringUntil('\n').toInt(); // read length
  519. for (size_t i=0; i<len; i++) {
  520. TINY_GSM_MODEM_STREAM_TO_MUX_FIFO_WITH_DOUBLE_TIMEOUT
  521. sockets[mux]->sock_available--;
  522. // ^^ One less character available after moving from modem's FIFO to our FIFO
  523. }
  524. waitResponse();
  525. DBG("### READ:", len, "from", mux);
  526. return len;
  527. }
  528. bool modemGetConnected(uint8_t mux) {
  529. sendAT(GF("+QISTATE=1,"), mux);
  530. //+QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
  531. if (waitResponse(GF("+QISTATE:")))
  532. return false;
  533. streamSkipUntil(','); // Skip mux
  534. streamSkipUntil(','); // Skip socket type
  535. streamSkipUntil(','); // Skip remote ip
  536. streamSkipUntil(','); // Skip remote port
  537. streamSkipUntil(','); // Skip local port
  538. int res = stream.readStringUntil(',').toInt(); // socket state
  539. waitResponse();
  540. // 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
  541. return 2 == res;
  542. }
  543. public:
  544. /*
  545. Utilities
  546. */
  547. TINY_GSM_MODEM_STREAM_UTILITIES()
  548. // TODO: Optimize this!
  549. uint8_t waitResponse(uint32_t timeout_ms, String& data,
  550. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  551. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL, GsmConstStr r6=NULL)
  552. {
  553. /*String r1s(r1); r1s.trim();
  554. String r2s(r2); r2s.trim();
  555. String r3s(r3); r3s.trim();
  556. String r4s(r4); r4s.trim();
  557. String r5s(r5); r5s.trim();
  558. String r6s(r6); r6s.trim();
  559. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s, ",", r6s);*/
  560. data.reserve(64);
  561. int index = 0;
  562. unsigned long startMillis = millis();
  563. do {
  564. TINY_GSM_YIELD();
  565. while (stream.available() > 0) {
  566. TINY_GSM_YIELD();
  567. int a = stream.read();
  568. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  569. data += (char)a;
  570. if (r1 && data.endsWith(r1)) {
  571. index = 1;
  572. goto finish;
  573. } else if (r2 && data.endsWith(r2)) {
  574. index = 2;
  575. goto finish;
  576. } else if (r3 && data.endsWith(r3)) {
  577. index = 3;
  578. goto finish;
  579. } else if (r4 && data.endsWith(r4)) {
  580. index = 4;
  581. goto finish;
  582. } else if (r5 && data.endsWith(r5)) {
  583. index = 5;
  584. goto finish;
  585. } else if (r6 && data.endsWith(r6)) {
  586. index = 6;
  587. goto finish;
  588. } else if (data.endsWith(GF(GSM_NL "+QIRD:"))) { // TODO: QIRD? or QIRDI?
  589. streamSkipUntil(','); // Skip the context
  590. streamSkipUntil(','); // Skip the role
  591. int mux = stream.readStringUntil('\n').toInt();
  592. DBG("### Got Data:", mux);
  593. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  594. sockets[mux]->got_data = true;
  595. }
  596. } else if (data.endsWith(GF("CLOSED" GSM_NL))) {
  597. int nl = data.lastIndexOf(GSM_NL, data.length()-8);
  598. int coma = data.indexOf(',', nl+2);
  599. int mux = data.substring(nl+2, coma).toInt();
  600. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  601. sockets[mux]->sock_connected = false;
  602. }
  603. data = "";
  604. DBG("### Closed: ", mux);
  605. }
  606. }
  607. } while (millis() - startMillis < timeout_ms);
  608. finish:
  609. if (!index) {
  610. data.trim();
  611. if (data.length()) {
  612. DBG("### Unhandled:", data);
  613. }
  614. data = "";
  615. }
  616. //data.replace(GSM_NL, "/");
  617. //DBG('<', index, '>', data);
  618. return index;
  619. }
  620. uint8_t waitResponse(uint32_t timeout_ms,
  621. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  622. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL, GsmConstStr r6=NULL)
  623. {
  624. String data;
  625. return waitResponse(timeout_ms, data, r1, r2, r3, r4, r5, r6);
  626. }
  627. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  628. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL, GsmConstStr r6=NULL)
  629. {
  630. return waitResponse(1000, r1, r2, r3, r4, r5, r6);
  631. }
  632. public:
  633. Stream& stream;
  634. protected:
  635. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  636. };
  637. #endif