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.

802 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() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  407. /*
  408. * Phone Call functions
  409. */
  410. bool setGsmBusy(bool busy = true) {
  411. sendAT(GF("+GSMBUSY="), busy ? 1 : 0);
  412. return waitResponse() == 1;
  413. }
  414. bool callAnswer() {
  415. sendAT(GF("A"));
  416. return waitResponse() == 1;
  417. }
  418. bool callNumber(const String& number) {
  419. sendAT(GF("D"), number);
  420. return waitResponse() == 1;
  421. }
  422. void callRedial() {
  423. sendAT(GF("DL"));
  424. return waitResponse() == 1;
  425. }
  426. bool callHangup(const String& number) {
  427. sendAT(GF("H"), number);
  428. return waitResponse() == 1;
  429. }
  430. /*
  431. * Messaging functions
  432. */
  433. void sendUSSD() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  434. bool sendSMS(const String& number, const String& text) {
  435. sendAT(GF("+CMGF=1"));
  436. waitResponse();
  437. sendAT(GF("+CMGS=\""), number, GF("\""));
  438. if (waitResponse(GF(">")) != 1) {
  439. return false;
  440. }
  441. stream.print(text);
  442. stream.write((char)0x1A);
  443. stream.flush();
  444. return waitResponse(60000L) == 1;
  445. }
  446. bool sendSMS_UTF16(const String& number, const void* text, size_t len) {
  447. sendAT(GF("+CMGF=1"));
  448. waitResponse();
  449. sendAT(GF("+CSCS=\"HEX\""));
  450. waitResponse();
  451. sendAT(GF("+CSMP=17,167,0,8"));
  452. waitResponse();
  453. sendAT(GF("+CMGS=\""), number, GF("\""));
  454. if (waitResponse(GF(">")) != 1) {
  455. return false;
  456. }
  457. uint16_t* t = (uint16_t*)text;
  458. for (size_t i=0; i<len; i++) {
  459. uint8_t c = t[i] >> 8;
  460. if (c < 0x10) { stream.print('0'); }
  461. stream.print(c, HEX);
  462. c = t[i] & 0xFF;
  463. if (c < 0x10) { stream.print('0'); }
  464. stream.print(c, HEX);
  465. }
  466. stream.write((char)0x1A);
  467. stream.flush();
  468. return waitResponse(60000L) == 1;
  469. }
  470. /*
  471. * Location functions
  472. */
  473. String getGsmLocation() {
  474. sendAT(GF("+CIPGSMLOC=1,1"));
  475. if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) {
  476. return "";
  477. }
  478. String res = stream.readStringUntil('\n');
  479. waitResponse();
  480. res.trim();
  481. return res;
  482. }
  483. /*
  484. * Battery functions
  485. */
  486. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  487. uint16_t getBattVoltage() {
  488. sendAT(GF("+CBC"));
  489. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  490. return 0;
  491. }
  492. streamSkipUntil(','); // Skip
  493. streamSkipUntil(','); // Skip
  494. uint16_t res = stream.readStringUntil(',').toInt();
  495. waitResponse();
  496. return res;
  497. }
  498. int getBattPercent() {
  499. if (!autoBaud()) {
  500. return false;
  501. }
  502. sendAT(GF("+CBC"));
  503. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  504. return false;
  505. }
  506. stream.readStringUntil(',');
  507. int res = stream.readStringUntil(',').toInt();
  508. waitResponse();
  509. return res;
  510. }
  511. private:
  512. bool modemConnect(const char* host, uint16_t port, uint8_t mux) {
  513. sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port);
  514. int rsp = waitResponse(75000L,
  515. GF("CONNECT OK" GSM_NL),
  516. GF("CONNECT FAIL" GSM_NL),
  517. GF("ALREADY CONNECT" GSM_NL));
  518. return (1 == rsp);
  519. }
  520. int modemSend(const void* buff, size_t len, uint8_t mux) {
  521. sendAT(GF("+CIPSEND="), mux, ',', len);
  522. if (waitResponse(GF(">")) != 1) {
  523. return -1;
  524. }
  525. stream.write((uint8_t*)buff, len);
  526. stream.flush();
  527. if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
  528. return -1;
  529. }
  530. streamSkipUntil(','); // Skip mux
  531. return stream.readStringUntil('\n').toInt();
  532. }
  533. size_t modemRead(size_t size, uint8_t mux) {
  534. #ifdef TINY_GSM_USE_HEX
  535. sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
  536. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  537. return 0;
  538. }
  539. #else
  540. sendAT(GF("+CIPRXGET=2,"), mux, ',', size);
  541. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  542. return 0;
  543. }
  544. #endif
  545. streamSkipUntil(','); // Skip mode 2/3
  546. streamSkipUntil(','); // Skip mux
  547. size_t len = stream.readStringUntil(',').toInt();
  548. sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
  549. for (size_t i=0; i<len; i++) {
  550. #ifdef TINY_GSM_USE_HEX
  551. while (stream.available() < 2) { TINY_GSM_YIELD(); }
  552. char buf[4] = { 0, };
  553. buf[0] = stream.read();
  554. buf[1] = stream.read();
  555. char c = strtol(buf, NULL, 16);
  556. #else
  557. while (!stream.available()) { TINY_GSM_YIELD(); }
  558. char c = stream.read();
  559. #endif
  560. sockets[mux]->rx.put(c);
  561. }
  562. waitResponse();
  563. return len;
  564. }
  565. size_t modemGetAvailable(uint8_t mux) {
  566. sendAT(GF("+CIPRXGET=4,"), mux);
  567. size_t result = 0;
  568. if (waitResponse(GF("+CIPRXGET:")) == 1) {
  569. streamSkipUntil(','); // Skip mode 4
  570. streamSkipUntil(','); // Skip mux
  571. result = stream.readStringUntil('\n').toInt();
  572. waitResponse();
  573. }
  574. if (!result) {
  575. sockets[mux]->sock_connected = modemGetConnected(mux);
  576. }
  577. return result;
  578. }
  579. bool modemGetConnected(uint8_t mux) {
  580. sendAT(GF("+CIPSTATUS="), mux);
  581. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  582. waitResponse();
  583. return 1 == res;
  584. }
  585. public:
  586. /* Utilities */
  587. template<typename T>
  588. void streamWrite(T last) {
  589. stream.print(last);
  590. }
  591. template<typename T, typename... Args>
  592. void streamWrite(T head, Args... tail) {
  593. stream.print(head);
  594. streamWrite(tail...);
  595. }
  596. bool streamSkipUntil(char c) { //TODO: timeout
  597. while (true) {
  598. while (!stream.available()) { TINY_GSM_YIELD(); }
  599. if (stream.read() == c)
  600. return true;
  601. }
  602. return false;
  603. }
  604. template<typename... Args>
  605. void sendAT(Args... cmd) {
  606. streamWrite("AT", cmd..., GSM_NL);
  607. stream.flush();
  608. TINY_GSM_YIELD();
  609. //DBG("### AT:", cmd...);
  610. }
  611. // TODO: Optimize this!
  612. uint8_t waitResponse(uint32_t timeout, String& data,
  613. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  614. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  615. {
  616. /*String r1s(r1); r1s.trim();
  617. String r2s(r2); r2s.trim();
  618. String r3s(r3); r3s.trim();
  619. String r4s(r4); r4s.trim();
  620. String r5s(r5); r5s.trim();
  621. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  622. data.reserve(64);
  623. int index = 0;
  624. unsigned long startMillis = millis();
  625. do {
  626. TINY_GSM_YIELD();
  627. while (stream.available() > 0) {
  628. int a = stream.read();
  629. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  630. data += (char)a;
  631. if (r1 && data.endsWith(r1)) {
  632. index = 1;
  633. goto finish;
  634. } else if (r2 && data.endsWith(r2)) {
  635. index = 2;
  636. goto finish;
  637. } else if (r3 && data.endsWith(r3)) {
  638. index = 3;
  639. goto finish;
  640. } else if (r4 && data.endsWith(r4)) {
  641. index = 4;
  642. goto finish;
  643. } else if (r5 && data.endsWith(r5)) {
  644. index = 5;
  645. goto finish;
  646. } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) {
  647. String mode = stream.readStringUntil(',');
  648. if (mode.toInt() == 1) {
  649. int mux = stream.readStringUntil('\n').toInt();
  650. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  651. sockets[mux]->got_data = true;
  652. }
  653. data = "";
  654. } else {
  655. data += mode;
  656. }
  657. } else if (data.endsWith(GF("CLOSED" GSM_NL))) {
  658. int nl = data.lastIndexOf(GSM_NL, data.length()-8);
  659. int coma = data.indexOf(',', nl+2);
  660. int mux = data.substring(nl+2, coma).toInt();
  661. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  662. sockets[mux]->sock_connected = false;
  663. }
  664. data = "";
  665. }
  666. }
  667. } while (millis() - startMillis < timeout);
  668. finish:
  669. if (!index) {
  670. data.trim();
  671. if (data.length()) {
  672. DBG("### Unhandled:", data);
  673. }
  674. data = "";
  675. }
  676. return index;
  677. }
  678. uint8_t waitResponse(uint32_t timeout,
  679. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  680. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  681. {
  682. String data;
  683. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  684. }
  685. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  686. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  687. {
  688. return waitResponse(1000, r1, r2, r3, r4, r5);
  689. }
  690. private:
  691. Stream& stream;
  692. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  693. };
  694. typedef TinyGsm::GsmClient TinyGsmClient;
  695. #endif