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.

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