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.

806 lines
18 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file TinyGsmClientSIM800.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef TinyGsmClientSIM800_h
  9. #define TinyGsmClientSIM800_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 5
  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 TinyGsm
  34. {
  35. public:
  36. class GsmClient : public Client
  37. {
  38. friend class TinyGsm;
  39. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  40. public:
  41. GsmClient() {}
  42. GsmClient(TinyGsm& modem, uint8_t mux = 1) {
  43. init(&modem, mux);
  44. }
  45. bool init(TinyGsm* modem, uint8_t mux = 1) {
  46. this->at = modem;
  47. this->mux = mux;
  48. sock_available = 0;
  49. prev_check = 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) {
  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("+CIPCLOSE="), mux);
  76. sock_connected = false;
  77. at->waitResponse();
  78. }
  79. virtual size_t write(const uint8_t *buf, size_t size) {
  80. TINY_GSM_YIELD();
  81. at->maintain();
  82. return at->modemSend(buf, size, mux);
  83. }
  84. virtual size_t write(uint8_t c) {
  85. return write(&c, 1);
  86. }
  87. virtual int available() {
  88. TINY_GSM_YIELD();
  89. if (!rx.size() && sock_connected) {
  90. // Workaround: sometimes SIM800 forgets to notify about data arrival.
  91. // TODO: Currently we ping the module periodically,
  92. // but maybe there's a better indicator that we need to poll
  93. if (millis() - prev_check > 500) {
  94. got_data = true;
  95. prev_check = millis();
  96. }
  97. at->maintain();
  98. }
  99. return rx.size() + sock_available;
  100. }
  101. virtual int read(uint8_t *buf, size_t size) {
  102. TINY_GSM_YIELD();
  103. at->maintain();
  104. size_t cnt = 0;
  105. while (cnt < size) {
  106. size_t chunk = TinyGsmMin(size-cnt, rx.size());
  107. if (chunk > 0) {
  108. rx.get(buf, chunk);
  109. buf += chunk;
  110. cnt += chunk;
  111. continue;
  112. }
  113. // TODO: Read directly into user buffer?
  114. at->maintain();
  115. if (sock_available > 0) {
  116. at->modemRead(rx.free(), mux);
  117. } else {
  118. break;
  119. }
  120. }
  121. return cnt;
  122. }
  123. virtual int read() {
  124. uint8_t c;
  125. if (read(&c, 1) == 1) {
  126. return c;
  127. }
  128. return -1;
  129. }
  130. virtual int peek() { return -1; } //TODO
  131. virtual void flush() { at->stream.flush(); }
  132. virtual uint8_t connected() {
  133. if (available()) {
  134. return true;
  135. }
  136. return sock_connected;
  137. }
  138. virtual operator bool() { return connected(); }
  139. /*
  140. * Extended API
  141. */
  142. String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  143. private:
  144. TinyGsm* at;
  145. uint8_t mux;
  146. uint16_t sock_available;
  147. uint32_t prev_check;
  148. bool sock_connected;
  149. bool got_data;
  150. RxFifo rx;
  151. };
  152. public:
  153. TinyGsm(Stream& stream)
  154. : stream(stream)
  155. {
  156. memset(sockets, 0, sizeof(sockets));
  157. }
  158. /*
  159. * Basic functions
  160. */
  161. bool begin() {
  162. return init();
  163. }
  164. bool init() {
  165. if (!autoBaud()) {
  166. return false;
  167. }
  168. sendAT(GF("&FZ")); // Factory + Reset
  169. waitResponse();
  170. sendAT(GF("E0")); // Echo Off
  171. if (waitResponse() != 1) {
  172. return false;
  173. }
  174. getSimStatus();
  175. return true;
  176. }
  177. bool autoBaud(unsigned long timeout = 10000L) {
  178. //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate
  179. for (unsigned long start = millis(); millis() - start < timeout; ) {
  180. sendAT(GF(""));
  181. if (waitResponse(200) == 1) {
  182. delay(100);
  183. return true;
  184. }
  185. delay(100);
  186. }
  187. return false;
  188. }
  189. void maintain() {
  190. for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
  191. GsmClient* sock = sockets[mux];
  192. if (sock && sock->got_data) {
  193. sock->got_data = false;
  194. sock->sock_available = modemGetAvailable(mux);
  195. }
  196. }
  197. while (stream.available()) {
  198. waitResponse(10, NULL, NULL);
  199. }
  200. }
  201. bool factoryDefault() {
  202. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  203. waitResponse();
  204. sendAT(GF("+IPR=0")); // Auto-baud
  205. waitResponse();
  206. sendAT(GF("+IFC=0,0")); // No Flow Control
  207. waitResponse();
  208. sendAT(GF("+ICF=3,3")); // 8 data 0 parity 1 stop
  209. waitResponse();
  210. sendAT(GF("+CSCLK=0")); // Disable Slow Clock
  211. waitResponse();
  212. sendAT(GF("&W")); // Write configuration
  213. return waitResponse() == 1;
  214. }
  215. /*
  216. * Power functions
  217. */
  218. bool restart() {
  219. if (!autoBaud()) {
  220. return false;
  221. }
  222. sendAT(GF("+CFUN=0"));
  223. if (waitResponse(10000L) != 1) {
  224. return false;
  225. }
  226. sendAT(GF("+CFUN=1,1"));
  227. if (waitResponse(10000L) != 1) {
  228. return false;
  229. }
  230. delay(3000);
  231. return init();
  232. }
  233. bool radioOff() {
  234. if (!autoBaud()) {
  235. return false;
  236. }
  237. sendAT(GF("+CFUN=0"));
  238. if (waitResponse(10000L) != 1) {
  239. return false;
  240. }
  241. delay(3000);
  242. return true;
  243. }
  244. /*
  245. * SIM card functions
  246. */
  247. bool simUnlock(const char *pin) {
  248. sendAT(GF("+CPIN=\""), pin, GF("\""));
  249. return waitResponse() == 1;
  250. }
  251. String getSimCCID() {
  252. sendAT(GF("+ICCID"));
  253. if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
  254. return "";
  255. }
  256. String res = stream.readStringUntil('\n');
  257. waitResponse();
  258. res.trim();
  259. return res;
  260. }
  261. String getIMEI() {
  262. sendAT(GF("+GSN"));
  263. if (waitResponse(GF(GSM_NL)) != 1) {
  264. return "";
  265. }
  266. String res = stream.readStringUntil('\n');
  267. waitResponse();
  268. res.trim();
  269. return res;
  270. }
  271. SimStatus getSimStatus(unsigned long timeout = 10000L) {
  272. for (unsigned long start = millis(); millis() - start < timeout; ) {
  273. sendAT(GF("+CPIN?"));
  274. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  275. delay(1000);
  276. continue;
  277. }
  278. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED"));
  279. waitResponse();
  280. switch (status) {
  281. case 2:
  282. case 3: return SIM_LOCKED;
  283. case 1: return SIM_READY;
  284. default: return SIM_ERROR;
  285. }
  286. }
  287. return SIM_ERROR;
  288. }
  289. RegStatus getRegistrationStatus() {
  290. sendAT(GF("+CREG?"));
  291. if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
  292. return REG_UNKNOWN;
  293. }
  294. streamSkipUntil(','); // Skip format (0)
  295. int status = stream.readStringUntil('\n').toInt();
  296. waitResponse();
  297. return (RegStatus)status;
  298. }
  299. String getOperator() {
  300. sendAT(GF("+COPS?"));
  301. if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
  302. return "";
  303. }
  304. streamSkipUntil('"'); // Skip mode and format
  305. String res = stream.readStringUntil('"');
  306. waitResponse();
  307. return res;
  308. }
  309. /*
  310. * Generic network functions
  311. */
  312. int getSignalQuality() {
  313. sendAT(GF("+CSQ"));
  314. if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
  315. return 99;
  316. }
  317. int res = stream.readStringUntil(',').toInt();
  318. waitResponse();
  319. return res;
  320. }
  321. bool waitForNetwork(unsigned long timeout = 60000L) {
  322. for (unsigned long start = millis(); millis() - start < timeout; ) {
  323. RegStatus s = getRegistrationStatus();
  324. if (s == REG_OK_HOME || s == REG_OK_ROAMING) {
  325. return true;
  326. }
  327. delay(1000);
  328. }
  329. return false;
  330. }
  331. /*
  332. * GPRS functions
  333. */
  334. bool gprsConnect(const char* apn, const char* user, const char* pwd) {
  335. gprsDisconnect();
  336. sendAT(GF("+SAPBR=3,1,\"Contype\",\"GPRS\""));
  337. waitResponse();
  338. sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"');
  339. waitResponse();
  340. if (user) {
  341. sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"');
  342. waitResponse();
  343. }
  344. if (pwd) {
  345. sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"');
  346. waitResponse();
  347. }
  348. sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"');
  349. waitResponse();
  350. sendAT(GF("+CGACT=1,1"));
  351. waitResponse(60000L);
  352. // Open a GPRS context
  353. sendAT(GF("+SAPBR=1,1"));
  354. waitResponse(85000L);
  355. // Query the GPRS context
  356. sendAT(GF("+SAPBR=2,1"));
  357. if (waitResponse(30000L) != 1)
  358. return false;
  359. sendAT(GF("+CGATT=1"));
  360. if (waitResponse(60000L) != 1)
  361. return false;
  362. // TODO: wait AT+CGATT?
  363. sendAT(GF("+CIPMUX=1"));
  364. if (waitResponse() != 1) {
  365. return false;
  366. }
  367. sendAT(GF("+CIPQSEND=1"));
  368. if (waitResponse() != 1) {
  369. return false;
  370. }
  371. sendAT(GF("+CIPRXGET=1"));
  372. if (waitResponse() != 1) {
  373. return false;
  374. }
  375. sendAT(GF("+CSTT=\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\""));
  376. if (waitResponse(60000L) != 1) {
  377. return false;
  378. }
  379. sendAT(GF("+CIICR"));
  380. if (waitResponse(60000L) != 1) {
  381. return false;
  382. }
  383. sendAT(GF("+CIFSR;E0"));
  384. if (waitResponse(10000L) != 1) {
  385. return false;
  386. }
  387. sendAT(GF("+CDNSCFG=\"8.8.8.8\",\"8.8.4.4\""));
  388. if (waitResponse() != 1) {
  389. return false;
  390. }
  391. return true;
  392. }
  393. bool gprsDisconnect() {
  394. sendAT(GF("+CIPSHUT"));
  395. return waitResponse(60000L) == 1;
  396. }
  397. String getLocalIP() {
  398. sendAT(GF("+CIFSR;E0"));
  399. String res;
  400. if (waitResponse(10000L, res) != 1) {
  401. return "";
  402. }
  403. res.trim();
  404. return res;
  405. }
  406. IPAddress localIP() {
  407. IPAddress res;
  408. res.fromString(getLocalIP());
  409. return res;
  410. }
  411. /*
  412. * Phone Call functions
  413. */
  414. bool setGsmBusy(bool busy = true) {
  415. sendAT(GF("+GSMBUSY="), busy ? 1 : 0);
  416. return waitResponse() == 1;
  417. }
  418. bool callAnswer() {
  419. sendAT(GF("A"));
  420. return waitResponse() == 1;
  421. }
  422. bool callNumber(const String& number) {
  423. sendAT(GF("D"), number);
  424. return waitResponse() == 1;
  425. }
  426. void callRedial() {
  427. sendAT(GF("DL"));
  428. return waitResponse() == 1;
  429. }
  430. bool callHangup(const String& number) {
  431. sendAT(GF("H"), number);
  432. return waitResponse() == 1;
  433. }
  434. /*
  435. * Messaging functions
  436. */
  437. void sendUSSD() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  438. bool sendSMS(const String& number, const String& text) {
  439. sendAT(GF("+CMGF=1"));
  440. waitResponse();
  441. sendAT(GF("+CMGS=\""), number, GF("\""));
  442. if (waitResponse(GF(">")) != 1) {
  443. return false;
  444. }
  445. stream.print(text);
  446. stream.write((char)0x1A);
  447. stream.flush();
  448. return waitResponse(60000L) == 1;
  449. }
  450. bool sendSMS_UTF16(const String& number, const void* text, size_t len) {
  451. sendAT(GF("+CMGF=1"));
  452. waitResponse();
  453. sendAT(GF("+CSCS=\"HEX\""));
  454. waitResponse();
  455. sendAT(GF("+CSMP=17,167,0,8"));
  456. waitResponse();
  457. sendAT(GF("+CMGS=\""), number, GF("\""));
  458. if (waitResponse(GF(">")) != 1) {
  459. return false;
  460. }
  461. uint16_t* t = (uint16_t*)text;
  462. for (size_t i=0; i<len; i++) {
  463. uint8_t c = t[i] >> 8;
  464. if (c < 0x10) { stream.print('0'); }
  465. stream.print(c, HEX);
  466. c = t[i] & 0xFF;
  467. if (c < 0x10) { stream.print('0'); }
  468. stream.print(c, HEX);
  469. }
  470. stream.write((char)0x1A);
  471. stream.flush();
  472. return waitResponse(60000L) == 1;
  473. }
  474. /*
  475. * Location functions
  476. */
  477. String getGsmLocation() {
  478. sendAT(GF("+CIPGSMLOC=1,1"));
  479. if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) {
  480. return "";
  481. }
  482. String res = stream.readStringUntil('\n');
  483. waitResponse();
  484. res.trim();
  485. return res;
  486. }
  487. /*
  488. * Battery functions
  489. */
  490. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  491. uint16_t getBattVoltage() {
  492. sendAT(GF("+CBC"));
  493. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  494. return 0;
  495. }
  496. streamSkipUntil(','); // Skip
  497. streamSkipUntil(','); // Skip
  498. uint16_t res = stream.readStringUntil(',').toInt();
  499. waitResponse();
  500. return res;
  501. }
  502. int getBattPercent() {
  503. if (!autoBaud()) {
  504. return false;
  505. }
  506. sendAT(GF("+CBC"));
  507. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  508. return false;
  509. }
  510. stream.readStringUntil(',');
  511. int res = stream.readStringUntil(',').toInt();
  512. waitResponse();
  513. return res;
  514. }
  515. private:
  516. bool modemConnect(const char* host, uint16_t port, uint8_t mux) {
  517. sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port);
  518. int rsp = waitResponse(75000L,
  519. GF("CONNECT OK" GSM_NL),
  520. GF("CONNECT FAIL" GSM_NL),
  521. GF("ALREADY CONNECT" GSM_NL));
  522. return (1 == rsp);
  523. }
  524. int modemSend(const void* buff, size_t len, uint8_t mux) {
  525. sendAT(GF("+CIPSEND="), mux, ',', len);
  526. if (waitResponse(GF(">")) != 1) {
  527. return -1;
  528. }
  529. stream.write((uint8_t*)buff, len);
  530. stream.flush();
  531. if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
  532. return -1;
  533. }
  534. streamSkipUntil(','); // Skip mux
  535. return stream.readStringUntil('\n').toInt();
  536. }
  537. size_t modemRead(size_t size, uint8_t mux) {
  538. #ifdef TINY_GSM_USE_HEX
  539. sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
  540. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  541. return 0;
  542. }
  543. #else
  544. sendAT(GF("+CIPRXGET=2,"), mux, ',', size);
  545. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  546. return 0;
  547. }
  548. #endif
  549. streamSkipUntil(','); // Skip mode 2/3
  550. streamSkipUntil(','); // Skip mux
  551. size_t len = stream.readStringUntil(',').toInt();
  552. sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
  553. for (size_t i=0; i<len; i++) {
  554. #ifdef TINY_GSM_USE_HEX
  555. while (stream.available() < 2) { TINY_GSM_YIELD(); }
  556. char buf[4] = { 0, };
  557. buf[0] = stream.read();
  558. buf[1] = stream.read();
  559. char c = strtol(buf, NULL, 16);
  560. #else
  561. while (!stream.available()) { TINY_GSM_YIELD(); }
  562. char c = stream.read();
  563. #endif
  564. sockets[mux]->rx.put(c);
  565. }
  566. waitResponse();
  567. return len;
  568. }
  569. size_t modemGetAvailable(uint8_t mux) {
  570. sendAT(GF("+CIPRXGET=4,"), mux);
  571. size_t result = 0;
  572. if (waitResponse(GF("+CIPRXGET:")) == 1) {
  573. streamSkipUntil(','); // Skip mode 4
  574. streamSkipUntil(','); // Skip mux
  575. result = stream.readStringUntil('\n').toInt();
  576. waitResponse();
  577. }
  578. if (!result) {
  579. sockets[mux]->sock_connected = modemGetConnected(mux);
  580. }
  581. return result;
  582. }
  583. bool modemGetConnected(uint8_t mux) {
  584. sendAT(GF("+CIPSTATUS="), mux);
  585. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  586. waitResponse();
  587. return 1 == res;
  588. }
  589. public:
  590. /* Utilities */
  591. template<typename T>
  592. void streamWrite(T last) {
  593. stream.print(last);
  594. }
  595. template<typename T, typename... Args>
  596. void streamWrite(T head, Args... tail) {
  597. stream.print(head);
  598. streamWrite(tail...);
  599. }
  600. bool streamSkipUntil(char c) { //TODO: timeout
  601. while (true) {
  602. while (!stream.available()) { TINY_GSM_YIELD(); }
  603. if (stream.read() == c)
  604. return true;
  605. }
  606. return false;
  607. }
  608. template<typename... Args>
  609. void sendAT(Args... cmd) {
  610. streamWrite("AT", cmd..., GSM_NL);
  611. stream.flush();
  612. TINY_GSM_YIELD();
  613. //DBG("### AT:", cmd...);
  614. }
  615. // TODO: Optimize this!
  616. uint8_t waitResponse(uint32_t timeout, String& data,
  617. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  618. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  619. {
  620. /*String r1s(r1); r1s.trim();
  621. String r2s(r2); r2s.trim();
  622. String r3s(r3); r3s.trim();
  623. String r4s(r4); r4s.trim();
  624. String r5s(r5); r5s.trim();
  625. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  626. data.reserve(64);
  627. int index = 0;
  628. unsigned long startMillis = millis();
  629. do {
  630. TINY_GSM_YIELD();
  631. while (stream.available() > 0) {
  632. int a = stream.read();
  633. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  634. data += (char)a;
  635. if (r1 && data.endsWith(r1)) {
  636. index = 1;
  637. goto finish;
  638. } else if (r2 && data.endsWith(r2)) {
  639. index = 2;
  640. goto finish;
  641. } else if (r3 && data.endsWith(r3)) {
  642. index = 3;
  643. goto finish;
  644. } else if (r4 && data.endsWith(r4)) {
  645. index = 4;
  646. goto finish;
  647. } else if (r5 && data.endsWith(r5)) {
  648. index = 5;
  649. goto finish;
  650. } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) {
  651. String mode = stream.readStringUntil(',');
  652. if (mode.toInt() == 1) {
  653. int mux = stream.readStringUntil('\n').toInt();
  654. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  655. sockets[mux]->got_data = true;
  656. }
  657. data = "";
  658. } else {
  659. data += mode;
  660. }
  661. } else if (data.endsWith(GF("CLOSED" GSM_NL))) {
  662. int nl = data.lastIndexOf(GSM_NL, data.length()-8);
  663. int coma = data.indexOf(',', nl+2);
  664. int mux = data.substring(nl+2, coma).toInt();
  665. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  666. sockets[mux]->sock_connected = false;
  667. }
  668. data = "";
  669. }
  670. }
  671. } while (millis() - startMillis < timeout);
  672. finish:
  673. if (!index) {
  674. data.trim();
  675. if (data.length()) {
  676. DBG("### Unhandled:", data);
  677. }
  678. data = "";
  679. }
  680. return index;
  681. }
  682. uint8_t waitResponse(uint32_t timeout,
  683. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  684. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  685. {
  686. String data;
  687. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  688. }
  689. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  690. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  691. {
  692. return waitResponse(1000, r1, r2, r3, r4, r5);
  693. }
  694. private:
  695. Stream& stream;
  696. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  697. };
  698. typedef TinyGsm::GsmClient TinyGsmClient;
  699. #endif