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.

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