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.

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