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.

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