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.

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