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.

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