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.

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