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.

575 lines
13 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
7 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
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
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
  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. #if !defined(TINY_GSM_RX_BUFFER)
  13. #define TINY_GSM_RX_BUFFER 256
  14. #endif
  15. #define TINY_GSM_MUX_COUNT 2
  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 = 3,
  28. REG_DENIED = 2,
  29. REG_OK_HOME = 1,
  30. REG_OK_ROAMING = 5,
  31. REG_UNKNOWN = 4,
  32. };
  33. class TinyGsmM590
  34. {
  35. public:
  36. class GsmClient : public Client
  37. {
  38. friend class TinyGsmM590;
  39. typedef TinyGsmFifo<uint8_t, TINY_GSM_RX_BUFFER> RxFifo;
  40. public:
  41. GsmClient() {}
  42. GsmClient(TinyGsmM590& modem, uint8_t mux = 1) {
  43. init(&modem, mux);
  44. }
  45. bool init(TinyGsmM590* modem, uint8_t mux = 1) {
  46. this->at = modem;
  47. this->mux = mux;
  48. sock_connected = false;
  49. at->sockets[mux] = this;
  50. return true;
  51. }
  52. public:
  53. virtual int connect(const char *host, uint16_t port, int timeout_s) {
  54. stop();
  55. TINY_GSM_YIELD();
  56. rx.clear();
  57. sock_connected = at->modemConnect(host, port, mux, timeout_s);
  58. return sock_connected;
  59. }
  60. TINY_GSM_CLIENT_CONNECT_OVERLOADS()
  61. virtual void stop() {
  62. TINY_GSM_YIELD();
  63. at->sendAT(GF("+TCPCLOSE="), mux);
  64. sock_connected = false;
  65. at->waitResponse();
  66. rx.clear();
  67. }
  68. TINY_GSM_CLIENT_WRITE()
  69. TINY_GSM_CLIENT_AVAILABLE_NO_MODEM_FIFO()
  70. TINY_GSM_CLIENT_READ_NO_MODEM_FIFO()
  71. TINY_GSM_CLIENT_PEEK_FLUSH_CONNECTED()
  72. /*
  73. * Extended API
  74. */
  75. String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  76. private:
  77. TinyGsmM590* at;
  78. uint8_t mux;
  79. bool sock_connected;
  80. RxFifo rx;
  81. };
  82. public:
  83. TinyGsmM590(Stream& stream)
  84. : stream(stream)
  85. {
  86. memset(sockets, 0, sizeof(sockets));
  87. }
  88. /*
  89. * Basic functions
  90. */
  91. bool begin(const char* pin = NULL) {
  92. return init(pin);
  93. }
  94. bool init(const char* pin = NULL) {
  95. DBG(GF("### TinyGSM Version:"), TINYGSM_VERSION);
  96. if (!testAT()) {
  97. return false;
  98. }
  99. sendAT(GF("&FZE0")); // Factory + Reset + Echo Off
  100. if (waitResponse() != 1) {
  101. return false;
  102. }
  103. #ifdef TINY_GSM_DEBUG
  104. sendAT(GF("+CMEE=2"));
  105. waitResponse();
  106. #endif
  107. DBG(GF("### Modem:"), getModemName());
  108. getSimStatus();
  109. return true;
  110. }
  111. String getModemName() {
  112. return "Neoway M590";
  113. }
  114. TINY_GSM_MODEM_SET_BAUD_IPR()
  115. TINY_GSM_MODEM_TEST_AT()
  116. TINY_GSM_MODEM_MAINTAIN_LISTEN()
  117. bool factoryDefault() {
  118. sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
  119. waitResponse();
  120. sendAT(GF("+ICF=3,1")); // 8 data 0 parity 1 stop
  121. waitResponse();
  122. sendAT(GF("+ENPWRSAVE=0")); // Disable PWR save
  123. waitResponse();
  124. sendAT(GF("+XISP=0")); // Use internal stack
  125. waitResponse();
  126. sendAT(GF("&W")); // Write configuration
  127. return waitResponse() == 1;
  128. }
  129. TINY_GSM_MODEM_GET_INFO_ATI()
  130. bool hasSSL() {
  131. return false;
  132. }
  133. bool hasWifi() {
  134. return false;
  135. }
  136. bool hasGPRS() {
  137. return true;
  138. }
  139. /*
  140. * Power functions
  141. */
  142. bool restart() {
  143. if (!testAT()) {
  144. return false;
  145. }
  146. sendAT(GF("+CFUN=15"));
  147. if (waitResponse(10000L) != 1) {
  148. return false;
  149. }
  150. //MODEM:STARTUP
  151. waitResponse(60000L, GF(GSM_NL "+PBREADY" GSM_NL));
  152. return init();
  153. }
  154. bool poweroff() {
  155. sendAT(GF("+CPWROFF"));
  156. return waitResponse(3000L) == 1;
  157. }
  158. bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED;
  159. bool sleepEnable(bool enable = true) {
  160. sendAT(GF("+ENPWRSAVE="), enable);
  161. return waitResponse() == 1;
  162. }
  163. /*
  164. * SIM card functions
  165. */
  166. TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
  167. TINY_GSM_MODEM_GET_SIMCCID_CCID()
  168. TINY_GSM_MODEM_GET_IMEI_GSN()
  169. SimStatus getSimStatus(unsigned long timeout_ms = 10000L) {
  170. for (unsigned long start = millis(); millis() - start < timeout_ms; ) {
  171. sendAT(GF("+CPIN?"));
  172. if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
  173. delay(1000);
  174. continue;
  175. }
  176. int status = waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"));
  177. waitResponse();
  178. switch (status) {
  179. case 2:
  180. case 3: return SIM_LOCKED;
  181. case 1: return SIM_READY;
  182. default: return SIM_ERROR;
  183. }
  184. }
  185. return SIM_ERROR;
  186. }
  187. TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
  188. TINY_GSM_MODEM_GET_OPERATOR_COPS()
  189. /*
  190. * Generic network functions
  191. */
  192. TINY_GSM_MODEM_GET_CSQ()
  193. bool isNetworkConnected() {
  194. RegStatus s = getRegistrationStatus();
  195. return (s == REG_OK_HOME || s == REG_OK_ROAMING);
  196. }
  197. TINY_GSM_MODEM_WAIT_FOR_NETWORK()
  198. /*
  199. * GPRS functions
  200. */
  201. bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) {
  202. gprsDisconnect();
  203. sendAT(GF("+XISP=0"));
  204. waitResponse();
  205. sendAT(GF("+CGDCONT=1,\"IP\",\""), apn, '"');
  206. waitResponse();
  207. if (!user) user = "";
  208. if (!pwd) pwd = "";
  209. sendAT(GF("+XGAUTH=1,1,\""), user, GF("\",\""), pwd, GF("\""));
  210. waitResponse();
  211. sendAT(GF("+XIIC=1"));
  212. waitResponse();
  213. const unsigned long timeout_ms = 60000L;
  214. for (unsigned long start = millis(); millis() - start < timeout_ms; ) {
  215. if (isGprsConnected()) {
  216. //goto set_dns; // TODO
  217. return true;
  218. }
  219. delay(500);
  220. }
  221. return false;
  222. // set_dns: // TODO
  223. // sendAT(GF("+DNSSERVER=1,8.8.8.8"));
  224. // waitResponse();
  225. //
  226. // sendAT(GF("+DNSSERVER=2,8.8.4.4"));
  227. // waitResponse();
  228. return true;
  229. }
  230. bool gprsDisconnect() {
  231. // TODO: There is no command in AT command set
  232. // XIIC=0 does not work
  233. return true;
  234. }
  235. bool isGprsConnected() {
  236. sendAT(GF("+XIIC?"));
  237. if (waitResponse(GF(GSM_NL "+XIIC:")) != 1) {
  238. return false;
  239. }
  240. int res = stream.readStringUntil(',').toInt();
  241. waitResponse();
  242. return res == 1;
  243. }
  244. /*
  245. * IP Address functions
  246. */
  247. String getLocalIP() {
  248. sendAT(GF("+XIIC?"));
  249. if (waitResponse(GF(GSM_NL "+XIIC:")) != 1) {
  250. return "";
  251. }
  252. stream.readStringUntil(',');
  253. String res = stream.readStringUntil('\n');
  254. waitResponse();
  255. res.trim();
  256. return res;
  257. }
  258. IPAddress localIP() {
  259. return TinyGsmIpFromString(getLocalIP());
  260. }
  261. /*
  262. * Phone Call functions
  263. */
  264. bool setGsmBusy(bool busy = true) TINY_GSM_ATTR_NOT_AVAILABLE;
  265. bool callAnswer() TINY_GSM_ATTR_NOT_AVAILABLE;
  266. bool callNumber(const String& number) TINY_GSM_ATTR_NOT_AVAILABLE;
  267. bool callHangup() TINY_GSM_ATTR_NOT_AVAILABLE;
  268. /*
  269. * Messaging functions
  270. */
  271. String sendUSSD(const String& code) {
  272. sendAT(GF("+CMGF=1"));
  273. waitResponse();
  274. sendAT(GF("+CSCS=\"HEX\""));
  275. waitResponse();
  276. sendAT(GF("D"), code);
  277. if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
  278. return "";
  279. }
  280. stream.readStringUntil('"');
  281. String hex = stream.readStringUntil('"');
  282. stream.readStringUntil(',');
  283. int dcs = stream.readStringUntil('\n').toInt();
  284. if (waitResponse() != 1) {
  285. return "";
  286. }
  287. if (dcs == 15) {
  288. return TinyGsmDecodeHex8bit(hex);
  289. } else if (dcs == 72) {
  290. return TinyGsmDecodeHex16bit(hex);
  291. } else {
  292. return hex;
  293. }
  294. }
  295. bool sendSMS(const String& number, const String& text) {
  296. sendAT(GF("+CSCS=\"GSM\""));
  297. waitResponse();
  298. sendAT(GF("+CMGF=1"));
  299. waitResponse();
  300. sendAT(GF("+CMGS=\""), number, GF("\""));
  301. if (waitResponse(GF(">")) != 1) {
  302. return false;
  303. }
  304. stream.print(text);
  305. stream.write((char)0x1A);
  306. stream.flush();
  307. return waitResponse(60000L) == 1;
  308. }
  309. bool sendSMS_UTF16(const String& number, const void* text, size_t len)
  310. TINY_GSM_ATTR_NOT_AVAILABLE;
  311. /*
  312. * Location functions
  313. */
  314. String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE;
  315. /*
  316. * Battery & temperature functions
  317. */
  318. uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE;
  319. int8_t getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE;
  320. uint8_t getBattChargeState() TINY_GSM_ATTR_NOT_AVAILABLE;
  321. bool getBattStats(uint8_t &chargeState, int8_t &percent, uint16_t &milliVolts) TINY_GSM_ATTR_NOT_AVAILABLE;
  322. float getTemperature() TINY_GSM_ATTR_NOT_AVAILABLE;
  323. /*
  324. * Client related functions
  325. */
  326. protected:
  327. bool modemConnect(const char* host, uint16_t port, uint8_t mux, int timeout_s = 75) {
  328. uint32_t timeout_ms = ((uint32_t)timeout_s)*1000;
  329. for (int i=0; i<3; i++) { // TODO: no need for loop?
  330. String ip = dnsIpQuery(host);
  331. sendAT(GF("+TCPSETUP="), mux, GF(","), ip, GF(","), port);
  332. int rsp = waitResponse(timeout_ms,
  333. GF(",OK" GSM_NL),
  334. GF(",FAIL" GSM_NL),
  335. GF("+TCPSETUP:Error" GSM_NL));
  336. if (1 == rsp) {
  337. return true;
  338. } else if (3 == rsp) {
  339. sendAT(GF("+TCPCLOSE="), mux);
  340. waitResponse();
  341. }
  342. delay(1000);
  343. }
  344. return false;
  345. }
  346. int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
  347. sendAT(GF("+TCPSEND="), mux, ',', len);
  348. if (waitResponse(GF(">")) != 1) {
  349. return 0;
  350. }
  351. stream.write((uint8_t*)buff, len);
  352. stream.write((char)0x0D);
  353. stream.flush();
  354. if (waitResponse(30000L, GF(GSM_NL "+TCPSEND:")) != 1) {
  355. return 0;
  356. }
  357. stream.readStringUntil('\n');
  358. return len;
  359. }
  360. bool modemGetConnected(uint8_t mux) {
  361. sendAT(GF("+CIPSTATUS="), mux);
  362. int res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), GF(",\"CLOSING\""), GF(",\"INITIAL\""));
  363. waitResponse();
  364. return 1 == res;
  365. }
  366. String dnsIpQuery(const char* host) {
  367. sendAT(GF("+DNS=\""), host, GF("\""));
  368. if (waitResponse(10000L, GF(GSM_NL "+DNS:")) != 1) {
  369. return "";
  370. }
  371. String res = stream.readStringUntil('\n');
  372. waitResponse(GF("+DNS:OK" GSM_NL));
  373. res.trim();
  374. return res;
  375. }
  376. public:
  377. /*
  378. Utilities
  379. */
  380. TINY_GSM_MODEM_STREAM_UTILITIES()
  381. // TODO: Optimize this!
  382. uint8_t waitResponse(uint32_t timeout_ms, String& data,
  383. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  384. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  385. {
  386. /*String r1s(r1); r1s.trim();
  387. String r2s(r2); r2s.trim();
  388. String r3s(r3); r3s.trim();
  389. String r4s(r4); r4s.trim();
  390. String r5s(r5); r5s.trim();
  391. DBG("### ..:", r1s, ",", r2s, ",", r3s, ",", r4s, ",", r5s);*/
  392. data.reserve(64);
  393. int index = 0;
  394. unsigned long startMillis = millis();
  395. do {
  396. TINY_GSM_YIELD();
  397. while (stream.available() > 0) {
  398. int a = stream.read();
  399. if (a <= 0) continue; // Skip 0x00 bytes, just in case
  400. data += (char)a;
  401. if (r1 && data.endsWith(r1)) {
  402. index = 1;
  403. goto finish;
  404. } else if (r2 && data.endsWith(r2)) {
  405. index = 2;
  406. goto finish;
  407. } else if (r3 && data.endsWith(r3)) {
  408. index = 3;
  409. goto finish;
  410. } else if (r4 && data.endsWith(r4)) {
  411. index = 4;
  412. goto finish;
  413. } else if (r5 && data.endsWith(r5)) {
  414. index = 5;
  415. goto finish;
  416. } else if (data.endsWith(GF("+TCPRECV:"))) {
  417. int mux = stream.readStringUntil(',').toInt();
  418. int len = stream.readStringUntil(',').toInt();
  419. int len_orig = len;
  420. if (len > sockets[mux]->rx.free()) {
  421. DBG("### Buffer overflow: ", len, "->", sockets[mux]->rx.free());
  422. } else {
  423. DBG("### Got: ", len, "->", sockets[mux]->rx.free());
  424. }
  425. while (len--) {
  426. TINY_GSM_MODEM_STREAM_TO_MUX_FIFO_WITH_DOUBLE_TIMEOUT
  427. }
  428. if (len_orig > sockets[mux]->available()) { // TODO
  429. DBG("### Fewer characters received than expected: ", sockets[mux]->available(), " vs ", len_orig);
  430. }
  431. data = "";
  432. } else if (data.endsWith(GF("+TCPCLOSE:"))) {
  433. int mux = stream.readStringUntil(',').toInt();
  434. stream.readStringUntil('\n');
  435. if (mux >= 0 && mux < TINY_GSM_MUX_COUNT) {
  436. sockets[mux]->sock_connected = false;
  437. }
  438. data = "";
  439. DBG("### Closed: ", mux);
  440. }
  441. }
  442. } while (millis() - startMillis < timeout_ms);
  443. finish:
  444. if (!index) {
  445. data.trim();
  446. if (data.length()) {
  447. DBG("### Unhandled:", data);
  448. }
  449. data = "";
  450. }
  451. //DBG('<', index, '>');
  452. return index;
  453. }
  454. uint8_t waitResponse(uint32_t timeout_ms,
  455. GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  456. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  457. {
  458. String data;
  459. return waitResponse(timeout_ms, data, r1, r2, r3, r4, r5);
  460. }
  461. uint8_t waitResponse(GsmConstStr r1=GFP(GSM_OK), GsmConstStr r2=GFP(GSM_ERROR),
  462. GsmConstStr r3=NULL, GsmConstStr r4=NULL, GsmConstStr r5=NULL)
  463. {
  464. return waitResponse(1000, r1, r2, r3, r4, r5);
  465. }
  466. public:
  467. Stream& stream;
  468. protected:
  469. GsmClient* sockets[TINY_GSM_MUX_COUNT];
  470. };
  471. #endif