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.

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