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.

934 lines
21 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
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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
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
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
7 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 TinyGsmSim800
  34. {
  35. public:
  36. class GsmClient : public Client
  37. {
  38. friend class TinyGsmSim800;
  39. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  40. public:
  41. GsmClient() {}
  42. GsmClient(TinyGsmSim800& modem, uint8_t mux = 1) {
  43. init(&modem, mux);
  44. }
  45. bool init(TinyGsmSim800* 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. TinyGsmSim800* 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. class GsmClientSecure : public GsmClient
  153. {
  154. public:
  155. GsmClientSecure() {}
  156. GsmClientSecure(TinyGsmSim800& modem, uint8_t mux = 1)
  157. : GsmClient(modem, mux)
  158. {}
  159. public:
  160. virtual int connect(const char *host, uint16_t port) {
  161. TINY_GSM_YIELD();
  162. rx.clear();
  163. sock_connected = at->modemConnect(host, port, mux, true);
  164. return sock_connected;
  165. }
  166. };
  167. public:
  168. TinyGsmSim800(Stream& stream)
  169. : stream(stream)
  170. {
  171. memset(sockets, 0, sizeof(sockets));
  172. }
  173. /*
  174. * Basic functions
  175. */
  176. bool begin() {
  177. return init();
  178. }
  179. bool init() {
  180. if (!testAT()) {
  181. return false;
  182. }
  183. sendAT(GF("&FZ")); // Factory + Reset
  184. waitResponse();
  185. sendAT(GF("E0")); // Echo Off
  186. if (waitResponse() != 1) {
  187. return false;
  188. }
  189. getSimStatus();
  190. return true;
  191. }
  192. void setBaud(unsigned long baud) {
  193. sendAT(GF("+IPR="), baud);
  194. }
  195. bool testAT(unsigned long timeout = 10000L) {
  196. //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate
  197. for (unsigned long start = millis(); millis() - start < timeout; ) {
  198. sendAT(GF(""));
  199. if (waitResponse(200) == 1) {
  200. delay(100);
  201. return true;
  202. }
  203. delay(100);
  204. }
  205. return false;
  206. }
  207. void maintain() {
  208. for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
  209. GsmClient* sock = sockets[mux];
  210. if (sock && sock->got_data) {
  211. sock->got_data = false;
  212. sock->sock_available = modemGetAvailable(mux);
  213. }
  214. }
  215. while (stream.available()) {
  216. waitResponse(10, NULL, NULL);
  217. }
  218. }
  219. bool factoryDefault() {
  220. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  221. waitResponse();
  222. sendAT(GF("+IPR=0")); // Auto-baud
  223. waitResponse();
  224. sendAT(GF("+IFC=0,0")); // No Flow Control
  225. waitResponse();
  226. sendAT(GF("+ICF=3,3")); // 8 data 0 parity 1 stop
  227. waitResponse();
  228. sendAT(GF("+CSCLK=0")); // Disable Slow Clock
  229. waitResponse();
  230. sendAT(GF("&W")); // Write configuration
  231. return waitResponse() == 1;
  232. }
  233. String getModemInfo() {
  234. sendAT(GF("I"));
  235. String res;
  236. if (waitResponse(1000L, res) != 1) {
  237. return "";
  238. }
  239. res.replace(GSM_NL "OK" GSM_NL, "");
  240. res.replace(GSM_NL, " ");
  241. res.trim();
  242. return res;
  243. }
  244. bool hasSSL() {
  245. sendAT(GF("+CIPSSL=?"));
  246. if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) {
  247. return false;
  248. }
  249. return waitResponse() == 1;
  250. }
  251. /*
  252. * Power functions
  253. */
  254. bool restart() {
  255. if (!testAT()) {
  256. return false;
  257. }
  258. sendAT(GF("+CFUN=0"));
  259. if (waitResponse(10000L) != 1) {
  260. return false;
  261. }
  262. sendAT(GF("+CFUN=1,1"));
  263. if (waitResponse(10000L) != 1) {
  264. return false;
  265. }
  266. delay(3000);
  267. return init();
  268. }
  269. bool poweroff() {
  270. sendAT(GF("+CPOWD=1"));
  271. return waitResponse(GF("NORMAL POWER DOWN")) == 1;
  272. }
  273. bool radioOff() {
  274. sendAT(GF("+CFUN=0"));
  275. if (waitResponse(10000L) != 1) {
  276. return false;
  277. }
  278. delay(3000);
  279. return true;
  280. }
  281. /*
  282. During sleep, the SIM800 module has its serial communication disabled. In order to reestablish communication
  283. pull the DRT-pin of the SIM800 module LOW for at least 50ms. Then use this function to disable sleep mode.
  284. The DTR-pin can then be released again.
  285. */
  286. bool sleepEnable(bool enable = true) {
  287. sendAT(GF("+CSCLK="), enable);
  288. return waitResponse() == 1;
  289. }
  290. /*
  291. * SIM card functions
  292. */
  293. bool simUnlock(const char *pin) {
  294. sendAT(GF("+CPIN=\""), pin, GF("\""));
  295. return waitResponse() == 1;
  296. }
  297. String getSimCCID() {
  298. sendAT(GF("+ICCID"));
  299. if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
  300. return "";
  301. }
  302. String res = stream.readStringUntil('\n');
  303. waitResponse();
  304. res.trim();
  305. return res;
  306. }
  307. String getIMEI() {
  308. sendAT(GF("+GSN"));
  309. if (waitResponse(GF(GSM_NL)) != 1) {
  310. return "";
  311. }
  312. String res = stream.readStringUntil('\n');
  313. waitResponse();
  314. res.trim();
  315. return res;
  316. }
  317. SimStatus getSimStatus(unsigned long timeout = 10000L) {
  318. for (unsigned long start = millis(); millis() - start < timeout; ) {
  319. sendAT(GF("+CPIN?"));
  320. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  321. delay(1000);
  322. continue;
  323. }
  324. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"), GF("NOT INSERTED"));
  325. waitResponse();
  326. switch (status) {
  327. case 2:
  328. case 3: return SIM_LOCKED;
  329. case 1: return SIM_READY;
  330. default: return SIM_ERROR;
  331. }
  332. }
  333. return SIM_ERROR;
  334. }
  335. RegStatus getRegistrationStatus() {
  336. sendAT(GF("+CREG?"));
  337. if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
  338. return REG_UNKNOWN;
  339. }
  340. streamSkipUntil(','); // Skip format (0)
  341. int status = stream.readStringUntil('\n').toInt();
  342. waitResponse();
  343. return (RegStatus)status;
  344. }
  345. String getOperator() {
  346. sendAT(GF("+COPS?"));
  347. if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
  348. return "";
  349. }
  350. streamSkipUntil('"'); // Skip mode and format
  351. String res = stream.readStringUntil('"');
  352. waitResponse();
  353. return res;
  354. }
  355. /*
  356. * Generic network functions
  357. */
  358. int getSignalQuality() {
  359. sendAT(GF("+CSQ"));
  360. if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
  361. return 99;
  362. }
  363. int res = stream.readStringUntil(',').toInt();
  364. waitResponse();
  365. return res;
  366. }
  367. bool isNetworkConnected() {
  368. RegStatus s = getRegistrationStatus();
  369. return (s == REG_OK_HOME || s == REG_OK_ROAMING);
  370. }
  371. bool waitForNetwork(unsigned long timeout = 60000L) {
  372. for (unsigned long start = millis(); millis() - start < timeout; ) {
  373. if (isNetworkConnected()) {
  374. return true;
  375. }
  376. delay(500);
  377. }
  378. return false;
  379. }
  380. /*
  381. * GPRS functions
  382. */
  383. bool gprsConnect(const char* apn, const char* user, const char* pwd) {
  384. gprsDisconnect();
  385. sendAT(GF("+SAPBR=3,1,\"Contype\",\"GPRS\""));
  386. waitResponse();
  387. sendAT(GF("+SAPBR=3,1,\"APN\",\""), apn, '"');
  388. waitResponse();
  389. if (user && strlen(user) > 0) {
  390. sendAT(GF("+SAPBR=3,1,\"USER\",\""), user, '"');
  391. waitResponse();
  392. }
  393. if (pwd && strlen(pwd) > 0) {
  394. sendAT(GF("+SAPBR=3,1,\"PWD\",\""), pwd, '"');
  395. waitResponse();
  396. }
  397. sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"');
  398. waitResponse();
  399. sendAT(GF("+CGACT=1,1"));
  400. waitResponse(60000L);
  401. // Open a GPRS context
  402. sendAT(GF("+SAPBR=1,1"));
  403. waitResponse(85000L);
  404. // Query the GPRS context
  405. sendAT(GF("+SAPBR=2,1"));
  406. if (waitResponse(30000L) != 1)
  407. return false;
  408. sendAT(GF("+CGATT=1"));
  409. if (waitResponse(60000L) != 1)
  410. return false;
  411. // TODO: wait AT+CGATT?
  412. sendAT(GF("+CIPMUX=1"));
  413. if (waitResponse() != 1) {
  414. return false;
  415. }
  416. sendAT(GF("+CIPQSEND=1"));
  417. if (waitResponse() != 1) {
  418. return false;
  419. }
  420. sendAT(GF("+CIPRXGET=1"));
  421. if (waitResponse() != 1) {
  422. return false;
  423. }
  424. sendAT(GF("+CSTT=\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\""));
  425. if (waitResponse(60000L) != 1) {
  426. return false;
  427. }
  428. sendAT(GF("+CIICR"));
  429. if (waitResponse(60000L) != 1) {
  430. return false;
  431. }
  432. sendAT(GF("+CIFSR;E0"));
  433. if (waitResponse(10000L) != 1) {
  434. return false;
  435. }
  436. sendAT(GF("+CDNSCFG=\"8.8.8.8\",\"8.8.4.4\""));
  437. if (waitResponse() != 1) {
  438. return false;
  439. }
  440. return true;
  441. }
  442. bool gprsDisconnect() {
  443. sendAT(GF("+CIPSHUT"));
  444. if (waitResponse(60000L) != 1)
  445. return false;
  446. sendAT(GF("+CGATT=0"));
  447. if (waitResponse(60000L) != 1)
  448. return false;
  449. return true;
  450. }
  451. bool isGprsConnected() {
  452. sendAT(GF("+CGATT?"));
  453. if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
  454. return false;
  455. }
  456. int res = stream.readStringUntil('\n').toInt();
  457. waitResponse();
  458. if (res != 1)
  459. return false;
  460. sendAT(GF("+CIFSR;E0")); // Another option is to use AT+CGPADDR=1
  461. if (waitResponse() != 1)
  462. return false;
  463. return true;
  464. }
  465. String getLocalIP() {
  466. sendAT(GF("+CIFSR;E0"));
  467. String res;
  468. if (waitResponse(10000L, res) != 1) {
  469. return "";
  470. }
  471. res.replace(GSM_NL "OK" GSM_NL, "");
  472. res.replace(GSM_NL, "");
  473. res.trim();
  474. return res;
  475. }
  476. IPAddress localIP() {
  477. return TinyGsmIpFromString(getLocalIP());
  478. }
  479. /*
  480. * Phone Call functions
  481. */
  482. bool setGsmBusy(bool busy = true) {
  483. sendAT(GF("+GSMBUSY="), busy ? 1 : 0);
  484. return waitResponse() == 1;
  485. }
  486. bool callAnswer() {
  487. sendAT(GF("A"));
  488. return waitResponse() == 1;
  489. }
  490. // Returns true on pick-up, false on error/busy
  491. bool callNumber(const String& number) {
  492. if (number == GF("last")) {
  493. sendAT(GF("DL"));
  494. } else {
  495. sendAT(GF("D"), number, ";");
  496. }
  497. int status = waitResponse(60000L,
  498. GFP(GSM_OK),
  499. GF("BUSY" GSM_NL),
  500. GF("NO ANSWER" GSM_NL),
  501. GF("NO CARRIER" GSM_NL));
  502. switch (status) {
  503. case 1: return true;
  504. case 2:
  505. case 3: return false;
  506. default: return false;
  507. }
  508. }
  509. bool callHangup() {
  510. sendAT(GF("H"));
  511. return waitResponse() == 1;
  512. }
  513. // 0-9,*,#,A,B,C,D
  514. bool dtmfSend(char cmd, int duration_ms = 100) {
  515. duration_ms = constrain(duration_ms, 100, 1000);
  516. sendAT(GF("+VTD="), duration_ms / 100); // VTD accepts in 1/10 of a second
  517. waitResponse();
  518. sendAT(GF("+VTS="), cmd);
  519. return waitResponse(10000L) == 1;
  520. }
  521. /*
  522. * Messaging functions
  523. */
  524. String sendUSSD(const String& code) {
  525. sendAT(GF("+CMGF=1"));
  526. waitResponse();
  527. sendAT(GF("+CSCS=\"HEX\""));
  528. waitResponse();
  529. sendAT(GF("+CUSD=1,\""), code, GF("\""));
  530. if (waitResponse() != 1) {
  531. return "";
  532. }
  533. if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
  534. return "";
  535. }
  536. stream.readStringUntil('"');
  537. String hex = stream.readStringUntil('"');
  538. stream.readStringUntil(',');
  539. int dcs = stream.readStringUntil('\n').toInt();
  540. if (dcs == 15) {
  541. return TinyGsmDecodeHex8bit(hex);
  542. } else if (dcs == 72) {
  543. return TinyGsmDecodeHex16bit(hex);
  544. } else {
  545. return hex;
  546. }
  547. }
  548. bool sendSMS(const String& number, const String& text) {
  549. sendAT(GF("+CMGF=1"));
  550. waitResponse();
  551. sendAT(GF("+CMGS=\""), number, GF("\""));
  552. if (waitResponse(GF(">")) != 1) {
  553. return false;
  554. }
  555. stream.print(text);
  556. stream.write((char)0x1A);
  557. stream.flush();
  558. return waitResponse(60000L) == 1;
  559. }
  560. bool sendSMS_UTF16(const String& number, const void* text, size_t len) {
  561. sendAT(GF("+CMGF=1"));
  562. waitResponse();
  563. sendAT(GF("+CSCS=\"HEX\""));
  564. waitResponse();
  565. sendAT(GF("+CSMP=17,167,0,8"));
  566. waitResponse();
  567. sendAT(GF("+CMGS=\""), number, GF("\""));
  568. if (waitResponse(GF(">")) != 1) {
  569. return false;
  570. }
  571. uint16_t* t = (uint16_t*)text;
  572. for (size_t i=0; i<len; i++) {
  573. uint8_t c = t[i] >> 8;
  574. if (c < 0x10) { stream.print('0'); }
  575. stream.print(c, HEX);
  576. c = t[i] & 0xFF;
  577. if (c < 0x10) { stream.print('0'); }
  578. stream.print(c, HEX);
  579. }
  580. stream.write((char)0x1A);
  581. stream.flush();
  582. return waitResponse(60000L) == 1;
  583. }
  584. /*
  585. * Location functions
  586. */
  587. String getGsmLocation() {
  588. sendAT(GF("+CIPGSMLOC=1,1"));
  589. if (waitResponse(10000L, GF(GSM_NL "+CIPGSMLOC:")) != 1) {
  590. return "";
  591. }
  592. String res = stream.readStringUntil('\n');
  593. waitResponse();
  594. res.trim();
  595. return res;
  596. }
  597. /*
  598. * Battery functions
  599. */
  600. // Use: float vBatt = modem.getBattVoltage() / 1000.0;
  601. uint16_t getBattVoltage() {
  602. sendAT(GF("+CBC"));
  603. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  604. return 0;
  605. }
  606. streamSkipUntil(','); // Skip
  607. streamSkipUntil(','); // Skip
  608. uint16_t res = stream.readStringUntil(',').toInt();
  609. waitResponse();
  610. return res;
  611. }
  612. int getBattPercent() {
  613. sendAT(GF("+CBC"));
  614. if (waitResponse(GF(GSM_NL "+CBC:")) != 1) {
  615. return false;
  616. }
  617. stream.readStringUntil(',');
  618. int res = stream.readStringUntil(',').toInt();
  619. waitResponse();
  620. return res;
  621. }
  622. protected:
  623. bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) {
  624. sendAT(GF("+CIPSSL="), ssl);
  625. int rsp = waitResponse();
  626. if (ssl && rsp != 1) {
  627. return false;
  628. }
  629. sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, GF("\","), port);
  630. rsp = waitResponse(75000L,
  631. GF("CONNECT OK" GSM_NL),
  632. GF("CONNECT FAIL" GSM_NL),
  633. GF("ALREADY CONNECT" GSM_NL));
  634. return (1 == rsp);
  635. }
  636. int modemSend(const void* buff, size_t len, uint8_t mux) {
  637. sendAT(GF("+CIPSEND="), mux, ',', len);
  638. if (waitResponse(GF(">")) != 1) {
  639. return -1;
  640. }
  641. stream.write((uint8_t*)buff, len);
  642. stream.flush();
  643. if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
  644. return -1;
  645. }
  646. streamSkipUntil(','); // Skip mux
  647. return stream.readStringUntil('\n').toInt();
  648. }
  649. size_t modemRead(size_t size, uint8_t mux) {
  650. #ifdef TINY_GSM_USE_HEX
  651. sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
  652. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  653. return 0;
  654. }
  655. #else
  656. sendAT(GF("+CIPRXGET=2,"), mux, ',', size);
  657. if (waitResponse(GF("+CIPRXGET:")) != 1) {
  658. return 0;
  659. }
  660. #endif
  661. streamSkipUntil(','); // Skip mode 2/3
  662. streamSkipUntil(','); // Skip mux
  663. size_t len = stream.readStringUntil(',').toInt();
  664. sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();
  665. for (size_t i=0; i<len; i++) {
  666. #ifdef TINY_GSM_USE_HEX
  667. while (stream.available() < 2) { TINY_GSM_YIELD(); }
  668. char buf[4] = { 0, };
  669. buf[0] = stream.read();
  670. buf[1] = stream.read();
  671. char c = strtol(buf, NULL, 16);
  672. #else
  673. while (!stream.available()) { TINY_GSM_YIELD(); }
  674. char c = stream.read();
  675. #endif
  676. sockets[mux]->rx.put(c);
  677. }
  678. waitResponse();
  679. return len;
  680. }
  681. size_t modemGetAvailable(uint8_t mux) {
  682. sendAT(GF("+CIPRXGET=4,"), mux);
  683. size_t result = 0;
  684. if (waitResponse(GF("+CIPRXGET:")) == 1) {
  685. streamSkipUntil(','); // Skip mode 4
  686. streamSkipUntil(','); // Skip mux
  687. result = stream.readStringUntil('\n').toInt();
  688. waitResponse();
  689. }
  690. if (!result) {
  691. sockets[mux]->sock_connected = modemGetConnected(mux);
  692. }
  693. return result;
  694. }
  695. bool modemGetConnected(uint8_t mux) {
  696. sendAT(GF("+CIPSTATUS="), mux);
  697. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  698. waitResponse();
  699. return 1 == res;
  700. }
  701. public:
  702. /* Utilities */
  703. template<typename T>
  704. void streamWrite(T last) {
  705. stream.print(last);
  706. }
  707. template<typename T, typename... Args>
  708. void streamWrite(T head, Args... tail) {
  709. stream.print(head);
  710. streamWrite(tail...);
  711. }
  712. bool streamSkipUntil(char c) { //TODO: timeout
  713. while (true) {
  714. while (!stream.available()) { TINY_GSM_YIELD(); }
  715. if (stream.read() == c)
  716. return true;
  717. }
  718. return false;
  719. }
  720. template<typename... Args>
  721. void sendAT(Args... cmd) {
  722. streamWrite("AT", cmd..., GSM_NL);
  723. stream.flush();
  724. TINY_GSM_YIELD();
  725. //DBG("### AT:", cmd...);
  726. }
  727. // TODO: Optimize this!
  728. uint8_t waitResponse(uint32_t timeout, String& data,
  729. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  730. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  731. {
  732. /*String r1s(r1); r1s.trim();
  733. String r2s(r2); r2s.trim();
  734. String r3s(r3); r3s.trim();
  735. String r4s(r4); r4s.trim();
  736. String r5s(r5); r5s.trim();
  737. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  738. data.reserve(64);
  739. int index = 0;
  740. unsigned long startMillis = millis();
  741. do {
  742. TINY_GSM_YIELD();
  743. while (stream.available() > 0) {
  744. int a = stream.read();
  745. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  746. data += (char)a;
  747. if (r1 && data.endsWith(r1)) {
  748. index = 1;
  749. goto finish;
  750. } else if (r2 && data.endsWith(r2)) {
  751. index = 2;
  752. goto finish;
  753. } else if (r3 && data.endsWith(r3)) {
  754. index = 3;
  755. goto finish;
  756. } else if (r4 && data.endsWith(r4)) {
  757. index = 4;
  758. goto finish;
  759. } else if (r5 && data.endsWith(r5)) {
  760. index = 5;
  761. goto finish;
  762. } else if (data.endsWith(GF(GSM_NL "+CIPRXGET:"))) {
  763. String mode = stream.readStringUntil(',');
  764. if (mode.toInt() == 1) {
  765. int mux = stream.readStringUntil('\n').toInt();
  766. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  767. sockets[mux]->got_data = true;
  768. }
  769. data = "";
  770. } else {
  771. data += mode;
  772. }
  773. } else if (data.endsWith(GF("CLOSED" GSM_NL))) {
  774. int nl = data.lastIndexOf(GSM_NL, data.length()-8);
  775. int coma = data.indexOf(',', nl+2);
  776. int mux = data.substring(nl+2, coma).toInt();
  777. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) {
  778. sockets[mux]->sock_connected = false;
  779. }
  780. data = "";
  781. DBG("### Closed: ", mux);
  782. }
  783. }
  784. } while (millis() - startMillis < timeout);
  785. finish:
  786. if (!index) {
  787. data.trim();
  788. if (data.length()) {
  789. DBG("### Unhandled:", data);
  790. }
  791. data = "";
  792. }
  793. return index;
  794. }
  795. uint8_t waitResponse(uint32_t timeout,
  796. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  797. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  798. {
  799. String data;
  800. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  801. }
  802. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  803. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  804. {
  805. return waitResponse(1000, r1, r2, r3, r4, r5);
  806. }
  807. protected:
  808. Stream& stream;
  809. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  810. };
  811. #endif