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.

696 lines
16 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
8 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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
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
7 years ago
7 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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file TinyGsmClientM590.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef TinyGsmClientM590_h
  9. #define TinyGsmClientM590_h
  10. //#pragma message("TinyGSM: TinyGsmClientM590")
  11. //#define TINY_GSM_DEBUG Serial
  12. #define TINY_GSM_MUX_COUNT 2
  13. #include <TinyGsmCommon.h>
  14. #define GSM_NL "\r\n"
  15. static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
  16. static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
  17. enum SimStatus {
  18. SIM_ERROR = 0,
  19. SIM_READY = 1,
  20. SIM_LOCKED = 2,
  21. };
  22. enum RegStatus {
  23. REG_UNREGISTERED = 0,
  24. REG_SEARCHING = 3,
  25. REG_DENIED = 2,
  26. REG_OK_HOME = 1,
  27. REG_OK_ROAMING = 5,
  28. REG_UNKNOWN = 4,
  29. };
  30. class TinyGsmM590 : public TinyGsmModem
  31. {
  32. public:
  33. class GsmClient : public Client
  34. {
  35. friend class TinyGsmM590;
  36. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  37. public:
  38. GsmClient() {}
  39. GsmClient(TinyGsmM590& modem, uint8_t mux = 1) {
  40. init(&modem, mux);
  41. }
  42. bool init(TinyGsmM590* modem, uint8_t mux = 1) {
  43. this->at = modem;
  44. this->mux = mux;
  45. sock_connected = false;
  46. at->sockets[mux] = this;
  47. return true;
  48. }
  49. public:
  50. virtual int connect(const char *host, uint16_t port) {
  51. stop();
  52. TINY_GSM_YIELD();
  53. rx.clear();
  54. sock_connected = at->modemConnect(host, port, mux);
  55. return sock_connected;
  56. }
  57. virtual int connect(IPAddress ip, uint16_t port) {
  58. String host; host.reserve(16);
  59. host += ip[0];
  60. host += ".";
  61. host += ip[1];
  62. host += ".";
  63. host += ip[2];
  64. host += ".";
  65. host += ip[3];
  66. return connect(host.c_str(), port);
  67. }
  68. virtual void stop() {
  69. TINY_GSM_YIELD();
  70. at->sendAT(GF("+TCPCLOSE="), mux);
  71. sock_connected = false;
  72. at->waitResponse();
  73. rx.clear();
  74. }
  75. virtual size_t write(const uint8_t *buf, size_t size) {
  76. TINY_GSM_YIELD();
  77. //at->maintain();
  78. return at->modemSend(buf, size, mux);
  79. }
  80. virtual size_t write(uint8_t c) {
  81. return write(&c, 1);
  82. }
  83. virtual size_t write(const char *str) {
  84. if (str == NULL) return 0;
  85. return write((const uint8_t *)str, strlen(str));
  86. }
  87. virtual int available() {
  88. TINY_GSM_YIELD();
  89. if (!rx.size() && sock_connected) {
  90. at->maintain();
  91. }
  92. return rx.size();
  93. }
  94. virtual int read(uint8_t *buf, size_t size) {
  95. TINY_GSM_YIELD();
  96. size_t cnt = 0;
  97. uint32_t _startMillis = millis();
  98. while (cnt < size && millis() - _startMillis < _timeout) {
  99. size_t chunk = TinyGsmMin(size-cnt, rx.size());
  100. if (chunk > 0) {
  101. rx.get(buf, chunk);
  102. buf += chunk;
  103. cnt += chunk;
  104. continue;
  105. }
  106. // TODO: Read directly into user buffer?
  107. if (!rx.size()) {
  108. at->maintain();
  109. }
  110. }
  111. return cnt;
  112. }
  113. virtual int read() {
  114. uint8_t c;
  115. if (read(&c, 1) == 1) {
  116. return c;
  117. }
  118. return -1;
  119. }
  120. virtual int peek() { return -1; } //TODO
  121. virtual void flush() { at->stream.flush(); }
  122. virtual uint8_t connected() {
  123. if (available()) {
  124. return true;
  125. }
  126. return sock_connected;
  127. }
  128. virtual operator bool() { return connected(); }
  129. /*
  130. * Extended API
  131. */
  132. String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  133. private:
  134. TinyGsmM590* at;
  135. uint8_t mux;
  136. bool sock_connected;
  137. RxFifo rx;
  138. };
  139. public:
  140. TinyGsmM590(Stream& stream)
  141. : TinyGsmModem(stream), stream(stream)
  142. {
  143. memset(sockets, 0, sizeof(sockets));
  144. }
  145. /*
  146. * Basic functions
  147. */
  148. bool init(const char* pin = NULL) {
  149. if (!testAT()) {
  150. return false;
  151. }
  152. sendAT(GF("&FZE0")); // Factory + Reset + Echo Off
  153. if (waitResponse() != 1) {
  154. return false;
  155. }
  156. #ifdef TINY_GSM_DEBUG
  157. sendAT(GF("+CMEE=2"));
  158. waitResponse();
  159. #endif
  160. DBG(GF("### Modem:"), getModemName());
  161. getSimStatus();
  162. return true;
  163. }
  164. String getModemName() {
  165. return "Neoway M590";
  166. }
  167. void setBaud(unsigned long baud) {
  168. sendAT(GF("+IPR="), baud);
  169. }
  170. bool testAT(unsigned long timeout = 10000L) {
  171. for (unsigned long start = millis(); millis() - start < timeout; ) {
  172. sendAT(GF(""));
  173. if (waitResponse(200) == 1) return true;
  174. delay(100);
  175. }
  176. return false;
  177. }
  178. void maintain() {
  179. //while (stream.available()) {
  180. waitResponse(10, NULL, NULL);
  181. //}
  182. }
  183. bool factoryDefault() {
  184. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  185. waitResponse();
  186. sendAT(GF("+ICF=3,1")); // 8 data 0 parity 1 stop
  187. waitResponse();
  188. sendAT(GF("+ENPWRSAVE=0")); // Disable PWR save
  189. waitResponse();
  190. sendAT(GF("+XISP=0")); // Use internal stack
  191. waitResponse();
  192. sendAT(GF("&W")); // Write configuration
  193. return waitResponse() == 1;
  194. }
  195. String getModemInfo() {
  196. sendAT(GF("I"));
  197. String res;
  198. if (waitResponse(1000L, res) != 1) {
  199. return "";
  200. }
  201. res.replace(GSM_NL "OK" GSM_NL, "");
  202. res.replace(GSM_NL, " ");
  203. res.trim();
  204. return res;
  205. }
  206. bool hasSSL() {
  207. return false;
  208. }
  209. bool hasWifi() {
  210. return false;
  211. }
  212. bool hasGPRS() {
  213. return true;
  214. }
  215. /*
  216. * Power functions
  217. */
  218. bool restart() {
  219. if (!testAT()) {
  220. return false;
  221. }
  222. sendAT(GF("+CFUN=15"));
  223. if (waitResponse(10000L) != 1) {
  224. return false;
  225. }
  226. //MODEM:STARTUP
  227. waitResponse(60000L, GF(GSM_NL "+PBREADY" GSM_NL));
  228. return init();
  229. }
  230. bool poweroff() {
  231. sendAT(GF("+CPWROFF"));
  232. return waitResponse(3000L) == 1;
  233. }
  234. bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  235. bool sleepEnable(bool enable = true) {
  236. sendAT(GF("+ENPWRSAVE="), enable);
  237. return waitResponse() == 1;
  238. }
  239. /*
  240. * SIM card functions
  241. */
  242. bool simUnlock(const char *pin) {
  243. sendAT(GF("+CPIN=\""), pin, GF("\""));
  244. return waitResponse() == 1;
  245. }
  246. String getSimCCID() {
  247. sendAT(GF("+CCID"));
  248. if (waitResponse(GF(GSM_NL "+CCID:")) != 1) {
  249. return "";
  250. }
  251. String res = stream.readStringUntil('\n');
  252. waitResponse();
  253. res.trim();
  254. return res;
  255. }
  256. String getIMEI() {
  257. sendAT(GF("+GSN"));
  258. if (waitResponse(GF(GSM_NL)) != 1) {
  259. return "";
  260. }
  261. String res = stream.readStringUntil('\n');
  262. waitResponse();
  263. res.trim();
  264. return res;
  265. }
  266. SimStatus getSimStatus(unsigned long timeout = 10000L) {
  267. for (unsigned long start = millis(); millis() - start < timeout; ) {
  268. sendAT(GF("+CPIN?"));
  269. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  270. delay(1000);
  271. continue;
  272. }
  273. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"));
  274. waitResponse();
  275. switch (status) {
  276. case 2:
  277. case 3: return SIM_LOCKED;
  278. case 1: return SIM_READY;
  279. default: return SIM_ERROR;
  280. }
  281. }
  282. return SIM_ERROR;
  283. }
  284. RegStatus getRegistrationStatus() {
  285. sendAT(GF("+CREG?"));
  286. if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
  287. return REG_UNKNOWN;
  288. }
  289. streamSkipUntil(','); // Skip format (0)
  290. int status = stream.readStringUntil('\n').toInt();
  291. waitResponse();
  292. return (RegStatus)status;
  293. }
  294. String getOperator() {
  295. sendAT(GF("+COPS?"));
  296. if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
  297. return "";
  298. }
  299. streamSkipUntil('"'); // Skip mode and format
  300. String res = stream.readStringUntil('"');
  301. waitResponse();
  302. return res;
  303. }
  304. /*
  305. * Generic network functions
  306. */
  307. int16_t getSignalQuality() {
  308. sendAT(GF("+CSQ"));
  309. if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
  310. return 99;
  311. }
  312. int res = stream.readStringUntil(',').toInt();
  313. waitResponse();
  314. return res;
  315. }
  316. bool isNetworkConnected() {
  317. RegStatus s = getRegistrationStatus();
  318. return (s == REG_OK_HOME || s == REG_OK_ROAMING);
  319. }
  320. /*
  321. * GPRS functions
  322. */
  323. bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
  324. gprsDisconnect();
  325. sendAT(GF("+XISP=0"));
  326. waitResponse();
  327. sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"');
  328. waitResponse();
  329. if (!user) user = "";
  330. if (!pwd) pwd = "";
  331. sendAT(GF("+XGAUTH=1,1,\""), user, GF("\",\""), pwd, GF("\""));
  332. waitResponse();
  333. sendAT(GF("+XIIC=1"));
  334. waitResponse();
  335. const unsigned long timeout = 60000L;
  336. for (unsigned long start = millis(); millis() - start < timeout; ) {
  337. if (isGprsConnected()) {
  338. //goto set_dns; // TODO
  339. return true;
  340. }
  341. delay(500);
  342. }
  343. return false;
  344. // set_dns: // TODO
  345. // sendAT(GF("+DNSSERVER=1,8.8.8.8"));
  346. // waitResponse();
  347. //
  348. // sendAT(GF("+DNSSERVER=2,8.8.4.4"));
  349. // waitResponse();
  350. return true;
  351. }
  352. bool gprsDisconnect() {
  353. // TODO: There is no command in AT command set
  354. // XIIC=0 does not work
  355. return true;
  356. }
  357. bool isGprsConnected() {
  358. sendAT(GF("+XIIC?"));
  359. if (waitResponse(GF(GSM_NL "+XIIC:")) != 1) {
  360. return false;
  361. }
  362. int res = stream.readStringUntil(',').toInt();
  363. waitResponse();
  364. return res == 1;
  365. }
  366. /*
  367. * IP Address functions
  368. */
  369. String getLocalIP() {
  370. sendAT(GF("+XIIC?"));
  371. if (waitResponse(GF(GSM_NL "+XIIC:")) != 1) {
  372. return "";
  373. }
  374. stream.readStringUntil(',');
  375. String res = stream.readStringUntil('\n');
  376. waitResponse();
  377. res.trim();
  378. return res;
  379. }
  380. /*
  381. * Phone Call functions
  382. */
  383. bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE;
  384. bool callAnswer() TINY_GSM_ATTR_NOT_AVAILABLE;
  385. bool callNumber(const String& number) TINY_GSM_ATTR_NOT_AVAILABLE;
  386. bool callHangup() TINY_GSM_ATTR_NOT_AVAILABLE;
  387. /*
  388. * Messaging functions
  389. */
  390. String sendUSSD(const String& code) {
  391. sendAT(GF("+CMGF=1"));
  392. waitResponse();
  393. sendAT(GF("+CSCS=\"HEX\""));
  394. waitResponse();
  395. sendAT(GF("D"), code);
  396. if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
  397. return "";
  398. }
  399. stream.readStringUntil('"');
  400. String hex = stream.readStringUntil('"');
  401. stream.readStringUntil(',');
  402. int dcs = stream.readStringUntil('\n').toInt();
  403. if (waitResponse() != 1) {
  404. return "";
  405. }
  406. if (dcs == 15) {
  407. return TinyGsmDecodeHex8bit(hex);
  408. } else if (dcs == 72) {
  409. return TinyGsmDecodeHex16bit(hex);
  410. } else {
  411. return hex;
  412. }
  413. }
  414. bool sendSMS(const String& number, const String& text) {
  415. sendAT(GF("+CSCS=\"GSM\""));
  416. waitResponse();
  417. sendAT(GF("+CMGF=1"));
  418. waitResponse();
  419. sendAT(GF("+CMGS=\""), number, GF("\""));
  420. if (waitResponse(GF(">")) != 1) {
  421. return false;
  422. }
  423. stream.print(text);
  424. stream.write((char)0x1A);
  425. stream.flush();
  426. return waitResponse(60000L) == 1;
  427. }
  428. bool sendSMS_UTF16(const String& number, const void* text, size_t len)
  429. TINY_GSM_ATTR_NOT_AVAILABLE;
  430. /*
  431. * Location functions
  432. */
  433. String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE;
  434. /*
  435. * Battery functions
  436. */
  437. uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE;
  438. int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE;
  439. /*
  440. * Client related functions
  441. */
  442. protected:
  443. bool modemConnect(const char* host, uint16_t port, uint8_t mux) {
  444. for (int i=0; i<3; i++) { // TODO: no need for loop?
  445. String ip = dnsIpQuery(host);
  446. sendAT(GF("+TCPSETUP="), mux, GF(","), ip, GF(","), port);
  447. int rsp = waitResponse(75000L,
  448. GF(",OK" GSM_NL),
  449. GF(",FAIL" GSM_NL),
  450. GF("+TCPSETUP:Error" GSM_NL));
  451. if (1 == rsp) {
  452. return true;
  453. } else if (3 == rsp) {
  454. sendAT(GF("+TCPCLOSE="), mux);
  455. waitResponse();
  456. }
  457. delay(1000);
  458. }
  459. return false;
  460. }
  461. int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
  462. sendAT(GF("+TCPSEND="), mux, ',', len);
  463. if (waitResponse(GF(">")) != 1) {
  464. return 0;
  465. }
  466. stream.write((uint8_t*)buff, len);
  467. stream.write((char)0x0D);
  468. stream.flush();
  469. if (waitResponse(30000L, GF(GSM_NL "+TCPSEND:")) != 1) {
  470. return 0;
  471. }
  472. stream.readStringUntil('\n');
  473. return len;
  474. }
  475. bool modemGetConnected(uint8_t mux) {
  476. sendAT(GF("+CIPSTATUS="), mux);
  477. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  478. waitResponse();
  479. return 1 == res;
  480. }
  481. String dnsIpQuery(const char* host) {
  482. sendAT(GF("+DNS=\""), host, GF("\""));
  483. if (waitResponse(10000L, GF(GSM_NL "+DNS:")) != 1) {
  484. return "";
  485. }
  486. String res = stream.readStringUntil('\n');
  487. waitResponse(GF("+DNS:OK" GSM_NL));
  488. res.trim();
  489. return res;
  490. }
  491. public:
  492. /*
  493. Utilities
  494. */
  495. template<typename... Args>
  496. void sendAT(Args... cmd) {
  497. streamWrite("AT", cmd..., GSM_NL);
  498. stream.flush();
  499. TINY_GSM_YIELD();
  500. //DBG("### AT:", cmd...);
  501. }
  502. // TODO: Optimize this!
  503. uint8_t waitResponse(uint32_t timeout, String& data,
  504. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  505. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  506. {
  507. /*String r1s(r1); r1s.trim();
  508. String r2s(r2); r2s.trim();
  509. String r3s(r3); r3s.trim();
  510. String r4s(r4); r4s.trim();
  511. String r5s(r5); r5s.trim();
  512. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  513. data.reserve(64);
  514. int index = 0;
  515. unsigned long startMillis = millis();
  516. do {
  517. TINY_GSM_YIELD();
  518. while (stream.available() > 0) {
  519. int a = stream.read();
  520. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  521. data += (char)a;
  522. if (r1 && data.endsWith(r1)) {
  523. index = 1;
  524. goto finish;
  525. } else if (r2 && data.endsWith(r2)) {
  526. index = 2;
  527. goto finish;
  528. } else if (r3 && data.endsWith(r3)) {
  529. index = 3;
  530. goto finish;
  531. } else if (r4 && data.endsWith(r4)) {
  532. index = 4;
  533. goto finish;
  534. } else if (r5 && data.endsWith(r5)) {
  535. index = 5;
  536. goto finish;
  537. } else if (data.endsWith(GF("+TCPRECV:"))) {
  538. int mux = stream.readStringUntil(',').toInt();
  539. int len = stream.readStringUntil(',').toInt();
  540. int len_orig = len;
  541. if (len > sockets[mux]->rx.free()) {
  542. DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
  543. } else {
  544. DBG("### Got: ", len, "->", sockets[mux]->rx.free());
  545. }
  546. while (len--) {
  547. while (!stream.available()) { TINY_GSM_YIELD(); }
  548. sockets[mux]->rx.put(stream.read());
  549. }
  550. if (len_orig > sockets[mux]->available()) { // TODO
  551. DBG("### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig);
  552. }
  553. data = "";
  554. } else if (data.endsWith(GF("+TCPCLOSE:"))) {
  555. int mux = stream.readStringUntil(',').toInt();
  556. stream.readStringUntil('\n');
  557. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  558. sockets[mux]->sock_connected = false;
  559. }
  560. data = "";
  561. DBG("### Closed: ", mux);
  562. }
  563. }
  564. } while (millis() - startMillis < timeout);
  565. finish:
  566. if (!index) {
  567. data.trim();
  568. if (data.length()) {
  569. DBG("### Unhandled:", data);
  570. }
  571. data = "";
  572. }
  573. //DBG('<', index, '>');
  574. return index;
  575. }
  576. uint8_t waitResponse(uint32_t timeout,
  577. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  578. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  579. {
  580. String data;
  581. return waitResponse(timeout, data, r1, r2, r3, r4, r5);
  582. }
  583. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  584. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  585. {
  586. return waitResponse(1000, r1, r2, r3, r4, r5);
  587. }
  588. public:
  589. Stream& stream;
  590. protected:
  591. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  592. };
  593. #endif