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.

835 lines
19 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /**
  2. * @file TinyGsmClientM95.h
  3. * @author Volodymyr Shymanskyy, Pacman Pereira, and Replicade Ltd.
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy, (c)2017 Replicade Ltd. <http://www.replicade.com>
  6. * @date Nov 2016
  7. */
  8. #ifndef TinyGsmClientM95_h
  9. #define TinyGsmClientM95_h
  10. //#define TINY_GSM_DEBUG Serial
  11. //#define TINY_GSM_USE_HEX
  12. #if !defined(TINY_GSM_RX_BUFFER)
  13. #define TINY_GSM_RX_BUFFER 64
  14. #endif
  15. #define TINY_GSM_MUX_COUNT 6
  16. #include <TinyGsmCommon.h>
  17. #define GSM_NL "\r\n"
  18. static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
  19. static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
  20. enum SimStatus {
  21. SIM_ERROR = 0,
  22. SIM_READY = 1,
  23. SIM_LOCKED = 2,
  24. };
  25. enum RegStatus {
  26. REG_UNREGISTERED = 0,
  27. REG_SEARCHING = 2,
  28. REG_DENIED = 3,
  29. REG_OK_HOME = 1,
  30. REG_OK_ROAMING = 5,
  31. REG_UNKNOWN = 4,
  32. };
  33. class TinyGsmM95 : public TinyGsmModem
  34. {
  35. public:
  36. class GsmClient : public Client
  37. {
  38. friend class TinyGsmM95;
  39. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  40. public:
  41. GsmClient() {}
  42. GsmClient(TinyGsmM95& modem, uint8_t mux = 1) {
  43. init(&modem, mux);
  44. }
  45. bool init(TinyGsmM95* modem, uint8_t mux = 1) {
  46. this->at = modem;
  47. this->mux = mux;
  48. sock_available = 0;
  49. sock_connected = false;
  50. got_data = false;
  51. at->sockets[mux] = this;
  52. return true;
  53. }
  54. public:
  55. virtual int connect(const char *host, uint16_t port) {
  56. stop();
  57. TINY_GSM_YIELD();
  58. rx.clear();
  59. sock_connected = at->modemConnect(host, port, mux);
  60. return sock_connected;
  61. }
  62. virtual int connect(IPAddress ip, uint16_t port) {
  63. String host; host.reserve(16);
  64. host += ip[0];
  65. host += ".";
  66. host += ip[1];
  67. host += ".";
  68. host += ip[2];
  69. host += ".";
  70. host += ip[3];
  71. return connect(host.c_str(), port);
  72. }
  73. virtual void stop() {
  74. TINY_GSM_YIELD();
  75. at->sendAT(GF("+QICLOSE="), mux);
  76. sock_connected = false;
  77. at->waitResponse(60000L, GF("CLOSED"), GF("CLOSE OK"), GF("ERROR"));
  78. rx.clear();
  79. }
  80. virtual size_t write(const uint8_t *buf, size_t size) {
  81. TINY_GSM_YIELD();
  82. at->maintain();
  83. return at->modemSend(buf, size, mux);
  84. }
  85. virtual size_t write(uint8_t c) {
  86. return write(&c, 1);
  87. }
  88. virtual size_t write(const char *str) {
  89. if (str == NULL) return 0;
  90. return write((const uint8_t *)str, strlen(str));
  91. }
  92. virtual int available() {
  93. TINY_GSM_YIELD();
  94. if (!rx.size() && sock_connected) {
  95. at->maintain();
  96. }
  97. return rx.size() + sock_available;
  98. }
  99. virtual int read(uint8_t *buf, size_t size) {
  100. TINY_GSM_YIELD();
  101. at->maintain();
  102. size_t cnt = 0;
  103. while (cnt < size && sock_connected) {
  104. size_t chunk = TinyGsmMin(size-cnt, rx.size());
  105. if (chunk > 0) {
  106. rx.get(buf, chunk);
  107. buf += chunk;
  108. cnt += chunk;
  109. continue;
  110. }
  111. // TODO: Read directly into user buffer?
  112. at->maintain();
  113. if (sock_available > 0) {
  114. at->modemRead(rx.free(), mux);
  115. } else {
  116. break;
  117. }
  118. }
  119. return cnt;
  120. }
  121. virtual int read() {
  122. uint8_t c;
  123. if (read(&c, 1) == 1) {
  124. return c;
  125. }
  126. return -1;
  127. }
  128. virtual int peek() { return -1; } //TODO
  129. virtual void flush() { at->stream.flush(); }
  130. virtual uint8_t connected() {
  131. if (available()) {
  132. return true;
  133. }
  134. return sock_connected;
  135. }
  136. virtual operator bool() { return connected(); }
  137. /*
  138. * Extended API
  139. */
  140. String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  141. private:
  142. TinyGsmM95* at;
  143. uint8_t mux;
  144. uint16_t sock_available;
  145. bool sock_connected;
  146. bool got_data;
  147. RxFifo rx;
  148. };
  149. // class GsmClientSecure : public GsmClient
  150. // {
  151. // public:
  152. // GsmClientSecure() {}
  153. //
  154. // GsmClientSecure(TinyGsmm95& modem, uint8_t mux = 1)
  155. // : GsmClient(modem, mux)
  156. // {}
  157. //
  158. // public:
  159. // virtual int connect(const char *host, uint16_t port) {
  160. // stop();
  161. // TINY_GSM_YIELD();
  162. // rx.clear();
  163. // sock_connected = at->modemConnect(host, port, mux, true);
  164. // return sock_connected;
  165. // }
  166. // };
  167. public:
  168. TinyGsmM95(Stream& stream)
  169. : TinyGsmModem(stream), stream(stream)
  170. {
  171. memset(sockets, 0, sizeof(sockets));
  172. }
  173. /*
  174. * Basic functions
  175. */
  176. bool init(const char* pin = NULL) {
  177. if (!testAT()) {
  178. return false;
  179. }
  180. sendAT(GF("&FZE0")); // Factory + Reset + Echo Off
  181. if (waitResponse() != 1) {
  182. return false;
  183. }
  184. #ifdef TINY_GSM_DEBUG
  185. sendAT(GF("+CMEE=2"));
  186. waitResponse();
  187. #endif
  188. getSimStatus();
  189. return true;
  190. }
  191. String getModemName() {
  192. return "Quectel M95";
  193. }
  194. void setBaud(unsigned long baud) {
  195. sendAT(GF("+IPR="), baud);
  196. }
  197. bool testAT(unsigned long timeout = 10000L) {
  198. for (unsigned long start = millis(); millis() - start < timeout; ) {
  199. sendAT(GF(""));
  200. if (waitResponse(200) == 1) {
  201. delay(100);
  202. return true;
  203. }
  204. delay(100);
  205. }
  206. return false;
  207. }
  208. void maintain() {
  209. for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
  210. GsmClient* sock = sockets[mux];
  211. if (sock && sock->got_data) {
  212. sock->got_data = false;
  213. sock->sock_available = modemGetAvailable(mux);
  214. }
  215. }
  216. while (stream.available()) {
  217. waitResponse(10, NULL, NULL);
  218. }
  219. }
  220. bool factoryDefault() {
  221. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  222. waitResponse();
  223. sendAT(GF("+IPR=0")); // Auto-baud
  224. waitResponse();
  225. sendAT(GF("&W")); // Write configuration
  226. return waitResponse() == 1;
  227. }
  228. String getModemInfo() {
  229. sendAT(GF("I"));
  230. String res;
  231. if (waitResponse(1000L, res) != 1) {
  232. return "";
  233. }
  234. res.replace(GSM_NL "OK" GSM_NL, "");
  235. res.replace(GSM_NL, " ");
  236. res.trim();
  237. return res;
  238. }
  239. bool hasSSL() {
  240. return false; // TODO: For now
  241. }
  242. bool hasWifi() {
  243. return false;
  244. }
  245. bool hasGPRS() {
  246. return true;
  247. }
  248. /*
  249. * Power functions
  250. */
  251. bool restart() {
  252. if (!testAT()) {
  253. return false;
  254. }
  255. sendAT(GF("+CFUN=0"));
  256. if (waitResponse(10000L, GF("NORMAL POWER DOWN"), GF("OK"), GF("FAIL")) == 3) {
  257. return false;
  258. }
  259. sendAT(GF("+CFUN=1"));
  260. if (waitResponse(10000L, GF("Call Ready"), GF("OK"), GF("FAIL")) == 3) {
  261. return false;
  262. }
  263. return init();
  264. }
  265. bool poweroff() {
  266. sendAT(GF("+QPOWD"));
  267. return waitResponse(GF("POWERED DOWN")) == 1; // TODO
  268. }
  269. bool radioOff() {
  270. sendAT(GF("+CFUN=0"));
  271. if (waitResponse(10000L) != 1) {
  272. return false;
  273. }
  274. delay(3000);
  275. return true;
  276. }
  277. /*
  278. * SIM card functions
  279. */
  280. bool simUnlock(const char *pin) {
  281. sendAT(GF("+CPIN=\""), pin, GF("\""));
  282. return waitResponse() == 1;
  283. }
  284. String getSimCCID() {
  285. sendAT(GF("+ICCID"));
  286. if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
  287. return "";
  288. }
  289. String res = stream.readStringUntil('\n');
  290. waitResponse();
  291. res.trim();
  292. return res;
  293. }
  294. String getIMEI() {
  295. sendAT(GF("+GSN"));
  296. if (waitResponse(GF(GSM_NL)) != 1) {
  297. return "";
  298. }
  299. String res = stream.readStringUntil('\n');
  300. waitResponse();
  301. res.trim();
  302. return res;
  303. }
  304. SimStatus getSimStatus(unsigned long timeout = 10000L) {
  305. for (unsigned long start = millis(); millis() - start < timeout; ) {
  306. sendAT(GF("+CPIN?"));
  307. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  308. delay(1000);
  309. continue;
  310. }
  311. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"));
  312. waitResponse();
  313. switch (status) {
  314. case 2:
  315. case 3: return SIM_LOCKED;
  316. case 1: return SIM_READY;
  317. default: return SIM_ERROR;
  318. }
  319. }
  320. return SIM_ERROR;
  321. }
  322. RegStatus getRegistrationStatus() {
  323. sendAT(GF("+CREG?"));
  324. if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
  325. return REG_UNKNOWN;
  326. }
  327. streamSkipUntil(','); // Skip format (0)
  328. int status = stream.readStringUntil('\n').toInt();
  329. waitResponse();
  330. return (RegStatus)status;
  331. }
  332. String getOperator() {
  333. sendAT(GF("+COPS?"));
  334. if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
  335. return "";
  336. }
  337. streamSkipUntil('"'); // Skip mode and format
  338. String res = stream.readStringUntil('"');
  339. waitResponse();
  340. return res;
  341. }
  342. /*
  343. * Generic network functions
  344. */
  345. int getSignalQuality() {
  346. sendAT(GF("+CSQ"));
  347. if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
  348. return 99;
  349. }
  350. int res = stream.readStringUntil(',').toInt();
  351. waitResponse();
  352. return res;
  353. }
  354. bool isNetworkConnected() {
  355. RegStatus s = getRegistrationStatus();
  356. return (s == REG_OK_HOME || s == REG_OK_ROAMING);
  357. }
  358. void setHostFormat( bool useDottedQuad ) {
  359. if ( useDottedQuad ) {
  360. sendAT(GF("+QIDNSIP=0"));
  361. } else {
  362. sendAT(GF("+QIDNSIP=1"));
  363. }
  364. waitResponse();
  365. }
  366. /*
  367. * GPRS functions
  368. */
  369. bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
  370. gprsDisconnect();
  371. // select foreground context 0 = VIRTUAL_UART_1
  372. sendAT(GF("+QIFGCNT=0"));
  373. if (waitResponse() != 1) {
  374. return false;
  375. }
  376. //Select GPRS (=1) as the Bearer
  377. sendAT(GF("+QICSGP=1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\""));
  378. if (waitResponse() != 1) {
  379. return false;
  380. }
  381. //Start TCPIP Task and Set APN, User Name and Password
  382. sendAT("+QIREGAPP=\"", apn, "\",\"", user, "\",\"", pwd, "\"" );
  383. if (waitResponse() != 1) {
  384. return false;
  385. }
  386. //Activate GPRS/CSD Context
  387. sendAT(GF("+QIACT"));
  388. if (waitResponse(10000) != 1) {
  389. return false;
  390. }
  391. //Enable multiple TCP/IP connections
  392. sendAT(GF("+QIMUX=1"));
  393. if (waitResponse() != 1) {
  394. return false;
  395. }
  396. //Request an IP header for received data ("IPD(data length):")
  397. sendAT(GF("+QIHEAD=1"));
  398. if (waitResponse() != 1) {
  399. return false;
  400. }
  401. //Set Method to Handle Received TCP/IP Data - Retrieve Data by Command
  402. sendAT(GF("+QINDI=1"));
  403. if (waitResponse() != 1) {
  404. return false;
  405. }
  406. return true;
  407. }
  408. bool gprsDisconnect() {
  409. sendAT(GF("+QIDEACT"));
  410. return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
  411. }
  412. bool isGprsConnected() {
  413. sendAT(GF("+CGATT?"));
  414. if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
  415. return false;
  416. }
  417. int res = stream.readStringUntil('\n').toInt();
  418. waitResponse();
  419. if (res != 1)
  420. return false;
  421. return localIP() != 0;
  422. }
  423. /*
  424. * IP Address functions
  425. */
  426. String getLocalIP() {
  427. sendAT(GF("+QILOCIP"));
  428. stream.readStringUntil('\n');
  429. String res = stream.readStringUntil('\n');
  430. res.trim();
  431. return res;
  432. }
  433. /*
  434. * Messaging functions
  435. */
  436. String sendUSSD(const String& code) {
  437. sendAT(GF("+CMGF=1"));
  438. waitResponse();
  439. sendAT(GF("+CSCS=\"HEX\""));
  440. waitResponse();
  441. sendAT(GF("+CUSD=1,\""), code, GF("\""));
  442. if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
  443. return "";
  444. }
  445. stream.readStringUntil('"');
  446. String hex = stream.readStringUntil('"');
  447. stream.readStringUntil(',');
  448. int dcs = stream.readStringUntil('\n').toInt();
  449. if (waitResponse() != 1) {
  450. return "";
  451. }
  452. if (dcs == 15) {
  453. return TinyGsmDecodeHex8bit(hex);
  454. } else if (dcs == 72) {
  455. return TinyGsmDecodeHex16bit(hex);
  456. } else {
  457. return hex;
  458. }
  459. }
  460. bool sendSMS(const String& number, const String& text) {
  461. sendAT(GF("+CMGF=1"));
  462. waitResponse();
  463. //Set GSM 7 bit default alphabet (3GPP TS 23.038)
  464. sendAT(GF("+CSCS=\"GSM\""));
  465. waitResponse();
  466. sendAT(GF("+CMGS=\""), number, GF("\""));
  467. if (waitResponse(GF(">")) != 1) {
  468. return false;
  469. }
  470. stream.print(text);
  471. stream.write((char)0x1A);
  472. stream.flush();
  473. return waitResponse(60000L) == 1;
  474. }
  475. bool sendSMS_UTF16(const String& number, const void* text, size_t len) {
  476. sendAT(GF("+CMGF=1"));
  477. waitResponse();
  478. sendAT(GF("+CSCS=\"HEX\""));
  479. waitResponse();
  480. sendAT(GF("+CSMP=17,167,0,8"));
  481. waitResponse();
  482. sendAT(GF("+CMGS=\""), number, GF("\""));
  483. if (waitResponse(GF(">")) != 1) {
  484. return false;
  485. }
  486. uint16_t* t = (uint16_t*)text;
  487. for (size_t i=0; i<len; i++) {
  488. uint8_t c = t[i] >> 8;
  489. if (c < 0x10) { stream.print('0'); }
  490. stream.print(c, HEX);
  491. c = t[i] & 0xFF;
  492. if (c < 0x10) { stream.print('0'); }
  493. stream.print(c, HEX);
  494. }
  495. stream.write((char)0x1A);
  496. stream.flush();
  497. return waitResponse(60000L) == 1;
  498. }
  499. /** Delete all SMS */
  500. bool deleteAllSMS() {
  501. sendAT(GF("+QMGDA=6"));
  502. if (waitResponse(waitResponse(60000L, GF("OK"), GF("ERROR")) == 1) ) {
  503. return true;
  504. }
  505. return false;
  506. }
  507. /*
  508. * Location functions
  509. */
  510. String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE;
  511. /*
  512. * Battery functions
  513. */
  514. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  515. uint16_t getBattVoltage() {
  516. sendAT(GF("+CBC"));
  517. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  518. return 0;
  519. }
  520. streamSkipUntil(','); // Skip
  521. streamSkipUntil(','); // Skip
  522. uint16_t res = stream.readStringUntil(',').toInt();
  523. waitResponse();
  524. return res;
  525. }
  526. int getBattPercent() {
  527. sendAT(GF("+CBC"));
  528. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  529. return false;
  530. }
  531. stream.readStringUntil(',');
  532. int res = stream.readStringUntil(',').toInt();
  533. waitResponse();
  534. return res;
  535. }
  536. /*
  537. * Client related functions
  538. */
  539. protected:
  540. bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) {
  541. sendAT(GF("+QIOPEN="), GF("\"TCP"), GF("\",\""), host, GF("\","), port);
  542. int rsp = waitResponse(75000L,
  543. GF("CONNECT OK" GSM_NL),
  544. GF("CONNECT FAIL" GSM_NL),
  545. GF("ALREADY CONNECT" GSM_NL));
  546. if ( rsp != 1 ) {
  547. return false;
  548. }
  549. return (1 == rsp);
  550. }
  551. int modemSend(const void* buff, size_t len, uint8_t mux) {
  552. sendAT(GF("+QISEND="), mux, ',', len);
  553. if (waitResponse(GF(">")) != 1) {
  554. return 0;
  555. }
  556. stream.write((uint8_t*)buff, len);
  557. stream.flush();
  558. if (waitResponse(GF(GSM_NL "SEND OK")) != 1) {
  559. return 0;
  560. }
  561. bool allAcknowledged = false;
  562. // bool failed = false;
  563. while ( !allAcknowledged ) {
  564. sendAT( GF("+QISACK"));
  565. if (waitResponse(5000L, GF(GSM_NL "+QISACK:")) != 1) {
  566. return -1;
  567. } else {
  568. streamSkipUntil(','); /** Skip total */
  569. streamSkipUntil(','); /** Skip acknowledged data size */
  570. if ( stream.readStringUntil('\n').toInt() == 0 ) {
  571. allAcknowledged = true;
  572. }
  573. }
  574. }
  575. waitResponse(5000L);
  576. // streamSkipUntil(','); // Skip mux
  577. // return stream.readStringUntil('\n').toInt();
  578. return 1;
  579. }
  580. size_t modemRead(size_t size, uint8_t mux) {
  581. sendAT(GF("+QIRD="), mux, ',', size);
  582. if (waitResponse(GF("+QIRD:")) != 1) {
  583. return 0;
  584. }
  585. size_t len = stream.readStringUntil('\n').toInt();
  586. for (size_t i=0; i<len; i++) {
  587. while (!stream.available()) { TINY_GSM_YIELD(); }
  588. char c = stream.read();
  589. sockets[mux]->rx.put(c);
  590. }
  591. waitResponse();
  592. DBG("### READ:", mux, ",", len);
  593. return len;
  594. }
  595. size_t modemGetAvailable(uint8_t mux) {
  596. sendAT(GF("+QIRD="), mux, GF(",0"));
  597. size_t result = 0;
  598. if (waitResponse(GF("+QIRD:")) == 1) {
  599. streamSkipUntil(','); // Skip total received
  600. streamSkipUntil(','); // Skip have read
  601. result = stream.readStringUntil('\n').toInt();
  602. DBG("### STILL:", mux, "has", result);
  603. waitResponse();
  604. }
  605. if (!result) {
  606. sockets[mux]->sock_connected = modemGetConnected(mux);
  607. }
  608. return result;
  609. }
  610. bool modemGetConnected(uint8_t mux) {
  611. sendAT(GF("+QISTATE=1,"), mux);
  612. //+QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
  613. if (waitResponse(GF("+QISTATE:")))
  614. return false;
  615. streamSkipUntil(','); // Skip mux
  616. streamSkipUntil(','); // Skip socket type
  617. streamSkipUntil(','); // Skip remote ip
  618. streamSkipUntil(','); // Skip remote port
  619. streamSkipUntil(','); // Skip local port
  620. int res = stream.readStringUntil(',').toInt(); // socket state
  621. waitResponse();
  622. // 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
  623. return 2 == res;
  624. }
  625. public:
  626. /*
  627. Utilities
  628. */
  629. template<typename... Args>
  630. void sendAT(Args... cmd) {
  631. streamWrite("AT", cmd..., GSM_NL);
  632. stream.flush();
  633. TINY_GSM_YIELD();
  634. //DBG("### AT:", cmd...);
  635. }
  636. // TODO: Optimize this!
  637. uint8_t waitResponse(uint32_t timeout, String& data,
  638. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  639. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  640. {
  641. /*String r1s(r1); r1s.trim();
  642. String r2s(r2); r2s.trim();
  643. String r3s(r3); r3s.trim();
  644. String r4s(r4); r4s.trim();
  645. String r5s(r5); r5s.trim();
  646. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  647. data.reserve(64);
  648. int index = 0;
  649. unsigned long startMillis = millis();
  650. do {
  651. TINY_GSM_YIELD();
  652. while (stream.available() > 0) {
  653. int a = stream.read();
  654. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  655. data += (char)a;
  656. if (r1 && data.endsWith(r1)) {
  657. index = 1;
  658. goto finish;
  659. } else if (r2 && data.endsWith(r2)) {
  660. index = 2;
  661. goto finish;
  662. } else if (r3 && data.endsWith(r3)) {
  663. index = 3;
  664. goto finish;
  665. } else if (r4 && data.endsWith(r4)) {
  666. index = 4;
  667. goto finish;
  668. } else if (r5 && data.endsWith(r5)) {
  669. index = 5;
  670. goto finish;
  671. } else if (data.endsWith(GF(GSM_NL "+QIRD:"))) {
  672. streamSkipUntil(','); // Skip the context
  673. streamSkipUntil(','); // Skip the role
  674. int mux = stream.readStringUntil('\n').toInt();
  675. DBG("### Got Data:", mux);
  676. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  677. sockets[mux]->got_data = true;
  678. }
  679. } else if (data.endsWith(GF("CLOSED" GSM_NL))) {
  680. int nl = data.lastIndexOf(GSM_NL, data.length()-8);
  681. int coma = data.indexOf(',', nl+2);
  682. int mux = data.substring(nl+2, coma).toInt();
  683. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  684. sockets[mux]->sock_connected = false;
  685. }
  686. data = "";
  687. DBG("### Closed: ", mux);
  688. }
  689. }
  690. } while (millis() - startMillis < timeout);
  691. finish:
  692. if (!index) {
  693. data.trim();
  694. if (data.length()) {
  695. DBG("### Unhandled:", data);
  696. }
  697. data = "";
  698. }
  699. //DBG('<', index, '>');
  700. return index;
  701. }
  702. uint8_t waitResponse(uint32_t timeout,
  703. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  704. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  705. {
  706. String data;
  707. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  708. }
  709. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  710. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  711. {
  712. return waitResponse(1000, r1, r2, r3, r4, r5);
  713. }
  714. public:
  715. Stream& stream;
  716. protected:
  717. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  718. };
  719. #endif