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.

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