From 63ed6fafb730a0500bd5c6de84706e5b2d956bef Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Mon, 10 Sep 2018 15:25:13 -0400 Subject: [PATCH] Some class cleaning --- src/TinyGsmClientA6.h | 38 ++++++++++----------- src/TinyGsmClientBG96.h | 33 ++++++++----------- src/TinyGsmClientESP8266.h | 41 ++++++++--------------- src/TinyGsmClientM590.h | 33 ++++++++----------- src/TinyGsmClientM95.h | 23 +++++++------ src/TinyGsmClientMC60.h | 58 +++++++++++++++++---------------- src/TinyGsmClientSIM800.h | 42 ++++++++++++------------ src/TinyGsmClientSIM808.h | 6 ++-- src/TinyGsmClientUBLOX.h | 31 +++++++----------- src/TinyGsmClientXBee.h | 26 ++++++++++----- src/TinyGsmCommon.h | 58 +++++++++++++++++++++++---------- tools/test_build/test_build.ino | 7 +++- 12 files changed, 201 insertions(+), 195 deletions(-) diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index 9dbd80c..e0b8122 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -190,11 +190,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -212,6 +209,15 @@ public: return true; } + String getModemName() { + #if defined(TINY_GSM_MODEM_A6) + return "AI-Thinker A6"; + #elif defined(TINY_GSM_MODEM_A7) + return "AI-Thinker A7"; + #endif + return "AI-Thinker A6"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -255,6 +261,14 @@ public: return false; } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -371,22 +385,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index 6a1de31..2550cd0 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -214,11 +214,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -230,6 +227,10 @@ public: return true; } + String getModemName() { + return "Quectel BG96"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -284,6 +285,14 @@ public: return false; // TODO: For now } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -405,22 +414,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index 4381e85..0e12c57 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -206,11 +206,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -229,6 +226,10 @@ public: return true; } + String getModemName() { + return "ESP8266"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -270,6 +271,14 @@ public: return true; } + bool hasWifi() { + return true; + } + + bool hasGPRS() { + return false; + } + /* * Power functions */ @@ -381,30 +390,6 @@ public: return res2; } - /* - * GPRS functions - */ - bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) TINY_GSM_ATTR_NOT_AVAILABLE; - bool gprsDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - - /* - * Messaging functions - */ - - /* - * Location functions - */ - - String getGsmLocation() TINY_GSM_ATTR_NOT_AVAILABLE; - - /* - * Battery functions - */ - - uint16_t getBattVoltage() TINY_GSM_ATTR_NOT_AVAILABLE; - - int getBattPercent() TINY_GSM_ATTR_NOT_AVAILABLE; - protected: bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false) { diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index d3ef97f..c6cffd0 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -187,11 +187,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -208,6 +205,10 @@ public: return true; } + String getModemName() { + return "Neoway M590"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -259,6 +260,14 @@ public: return false; } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -379,22 +388,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index e45b94f..37ff4dc 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -194,11 +194,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -215,6 +212,10 @@ public: return true; } + String getModemName() { + return "Quectel M95"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -273,6 +274,14 @@ public: return false; // TODO: For now } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -416,12 +425,6 @@ public: waitResponse(); } - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientMC60.h b/src/TinyGsmClientMC60.h index 7c1c0f4..4324f31 100644 --- a/src/TinyGsmClientMC60.h +++ b/src/TinyGsmClientMC60.h @@ -221,11 +221,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -239,6 +236,17 @@ public: return true; } + String getModemName() { + #if defined(TINY_GSM_MODEM_MC60) + return "Quectel MC60"; + #elif defined(TINY_GSM_MODEM_MC60E) + return "Quectel MC60E"; + #endif + return "Quectel MC60"; + } + + void setBaud(unsigned long baud) { return false; }; + bool testAT(unsigned long timeout = 10000L) { //streamWrite(GF("AAAAA" GSM_NL)); // TODO: extra A's to help detect the baud rate for (unsigned long start = millis(); millis() - start < timeout; ) { @@ -294,15 +302,25 @@ public: /* * under development - * - bool hasSSL() { - sendAT(GF("+QIPSSL=?")); - if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) { - return false; - } - return waitResponse() == 1; + */ + // bool hasSSL() { + // sendAT(GF("+QIPSSL=?")); + // if (waitResponse(GF(GSM_NL "+CIPSSL:")) != 1) { + // return false; + // } + // return waitResponse() == 1; + // } + + bool hasSSL() { return false; } + + bool hasWifi() { + return false; } -*/ + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -433,22 +451,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 2a77e1b..5071504 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -236,11 +236,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { if (!testAT()) { return false; } @@ -254,6 +251,19 @@ public: return true; } + String getModemName() { + #if defined(TINY_GSM_MODEM_SIM800) + return "SIMCom SIM800"; + #elif defined(TINY_GSM_MODEM_SIM808) + return "SIMCom SIM808"; + #elif defined(TINY_GSM_MODEM_SIM868) + return "SIMCom SIM868"; + #elif defined(TINY_GSM_MODEM_SIM900) + return "SIMCom SIM900"; + #endif + return "SIMCom SIM800"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -323,6 +333,14 @@ public: #endif } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -466,22 +484,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientSIM808.h b/src/TinyGsmClientSIM808.h index 94d7428..7f3417e 100644 --- a/src/TinyGsmClientSIM808.h +++ b/src/TinyGsmClientSIM808.h @@ -27,7 +27,7 @@ public: // enable GPS bool enableGPS() { - uint16_t state; + // uint16_t state; sendAT(GF("+CGNSPWR=1")); if (waitResponse() != 1) { @@ -38,7 +38,7 @@ public: } bool disableGPS() { - uint16_t state; + // uint16_t state; sendAT(GF("+CGNSPWR=0")); if (waitResponse() != 1) { @@ -65,7 +65,7 @@ public: // works only with ans SIM808 V2 bool getGPS(float *lat, float *lon, float *speed=0, int *alt=0, int *vsat=0, int *usat=0) { //String buffer = ""; - char chr_buffer[12]; + // char chr_buffer[12]; bool fix = false; sendAT(GF("+CGNSINF")); diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index f98ff2b..6024409 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -213,9 +213,6 @@ public: /* * Basic functions */ - bool begin(const char* pin = NULL) { - return init(pin); - } bool init(const char* pin = NULL) { if (!testAT()) { @@ -232,6 +229,10 @@ public: return (getSimStatus() == SIM_READY); } + String getModemName() { + return "ESP8266"; + } + void setBaud(unsigned long baud) { sendAT(GF("+IPR="), baud); } @@ -284,6 +285,14 @@ public: return true; } + bool hasWifi() { + return false; + } + + bool hasGPRS() { + return true; + } + /* * Power functions */ @@ -404,22 +413,6 @@ public: return (s == REG_OK_HOME || s == REG_OK_ROAMING); } - bool waitForNetwork(unsigned long timeout = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout; ) { - if (isNetworkConnected()) { - return true; - } - delay(250); - } - return false; - } - - /* - * WiFi functions - */ - bool networkConnect(const char* ssid, const char* pwd) TINY_GSM_ATTR_NOT_AVAILABLE; - bool networkDisconnect() TINY_GSM_ATTR_NOT_AVAILABLE; - /* * GPRS functions */ diff --git a/src/TinyGsmClientXBee.h b/src/TinyGsmClientXBee.h index 89cc696..d99ba8d 100644 --- a/src/TinyGsmClientXBee.h +++ b/src/TinyGsmClientXBee.h @@ -217,11 +217,8 @@ public: /* * Basic functions */ - bool begin() { - return init(); - } - bool init() { + bool init(const char* pin = NULL) { guardTime = 1100; // Start with a default guard time of 1 second if (!commandMode(10)) return false; // Try up to 10 times for the init @@ -246,6 +243,10 @@ public: return ret_val; } + String getModemName() { + return getBeeName(); + } + void setBaud(unsigned long baud) { if (!commandMode()) return; switch(baud) @@ -313,6 +314,16 @@ public: else return true; } + bool hasWifi() { + if (beeType == XBEE_S6B_WIFI) return true; + else return false; + } + + bool hasGPRS() { + if (beeType == XBEE_S6B_WIFI) return false; + else return true; + } + XBeeType getBeeType() { return beeType; } @@ -360,8 +371,8 @@ public: return false; } - void setupPinSleep(bool maintainAssociation = false) { - if (!commandMode()) return; // Return immediately + bool sleepEnable(bool maintainAssociation = false) { + if (!commandMode()) return false; // Return immediately sendAT(GF("SM"),1); // Pin sleep waitResponse(); if (beeType == XBEE_S6B_WIFI && !maintainAssociation) { @@ -375,14 +386,13 @@ public: } writeChanges(); exitCommand(); + return true; } bool poweroff() TINY_GSM_ATTR_NOT_IMPLEMENTED; bool radioOff() TINY_GSM_ATTR_NOT_IMPLEMENTED; - bool sleepEnable(bool enable = true) TINY_GSM_ATTR_NOT_IMPLEMENTED; - /* * SIM card functions */ diff --git a/src/TinyGsmCommon.h b/src/TinyGsmCommon.h index 9df812f..e65fd9a 100644 --- a/src/TinyGsmCommon.h +++ b/src/TinyGsmCommon.h @@ -204,7 +204,7 @@ public: class GsmClient : public Client { - friend class TinyGsmXBee; + friend class TinyGsmMasterModem; }; @@ -222,50 +222,78 @@ public: * Basic functions */ - virtual bool begin() = 0; - virtual bool init() = 0; + // Prepare the modem for further functionality + virtual bool init(const char* pin = NULL) = 0; + // Begin is redundant with init + virtual bool begin(const char* pin = NULL) { + return init(pin); + } + // Returns a string with the chip name + virtual String getModemName() = 0; + // Sets the serial communication baud rate virtual void setBaud(unsigned long baud) = 0; + // Checks that the modem is responding to standard AT commands virtual bool testAT(unsigned long timeout = 10000L) = 0; + // Holds open communication with the modem waiting for data to come in virtual void maintain() = 0; + // Resets all modem chip settings to factor defaults virtual bool factoryDefault() = 0; + // Returns the response to a get info request. The format varies by modem. virtual String getModemInfo() = 0; + // Answers whether secure communication is available on this modem virtual bool hasSSL() = 0; + virtual bool hasWifi() = 0; + virtual bool hasGPRS() = 0; /* * Power functions */ virtual bool restart() = 0; + virtual bool poweroff() { return false; } + virtual bool radioOff() { return false; } + virtual bool sleepEnable(bool enable = true) { return false; } /* * SIM card functions */ - virtual bool simUnlock(const char *pin) = 0; - virtual String getSimCCID() = 0; - virtual String getIMEI() = 0; - virtual String getOperator() = 0; + virtual bool simUnlock(const char *pin) { return false; } + virtual String getSimCCID() { return ""; } + virtual String getIMEI() { return ""; } + virtual String getOperator() { return ""; } /* * Generic network functions */ virtual int getSignalQuality() = 0; + // NOTE: this returns whether the modem is registered on the cellular or WiFi + // network NOT whether GPRS or other internet connections are available virtual bool isNetworkConnected() = 0; + virtual bool waitForNetwork(unsigned long timeout = 60000L) { + for (unsigned long start = millis(); millis() - start < timeout; ) { + if (isNetworkConnected()) { + return true; + } + delay(250); + } + return false; + } /* * WiFi functions */ - virtual bool networkConnect(const char* ssid, const char* pwd) = 0; - virtual bool networkDisconnect() = 0; + virtual bool networkConnect(const char* ssid, const char* pwd) { return false; } + virtual bool networkDisconnect() { return false; } /* * GPRS functions */ - virtual bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) = 0; - virtual bool gprsDisconnect() = 0; - virtual bool isGprsConnected() = 0; + + virtual bool gprsConnect(const char* apn, const char* user = NULL, const char* pwd = NULL) { return false; } + virtual bool gprsDisconnect() { return false; } /* * IP Address functions @@ -276,12 +304,6 @@ public: return TinyGsmIpFromString(getLocalIP()); } - /* - * Messaging functions - */ - - virtual bool sendSMS(const String& number, const String& text) = 0; - public: Stream& stream; }; diff --git a/tools/test_build/test_build.ino b/tools/test_build/test_build.ino index 245e38a..34d2ae9 100644 --- a/tools/test_build/test_build.ino +++ b/tools/test_build/test_build.ino @@ -3,6 +3,12 @@ * DO NOT USE THIS - this is just a compilation test! * **************************************************************/ +// #define TINY_GSM_MODEM_SIM800 // Select for a SIM800, SIM900, or variant thereof +// #define TINY_GSM_MODEM_A6 // Select for a AI-Thinker A6 or A7 chip +// #define TINY_GSM_MODEM_M590 // Select for a Neoway M590 +// #define TINY_GSM_MODEM_UBLOX // Select for most u-blox cellular modems +// #define TINY_GSM_MODEM_ESP8266 // Select for an ESP8266 using the DEFAULT AT COMMAND FIRMWARE +// #define TINY_GSM_MODEM_XBEE // Select for Digi brand WiFi or Cellular XBee's #include @@ -77,4 +83,3 @@ void loop() { modem.networkDisconnect(); #endif } -