mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	Moved modem functions to pre-processor macros
This commit is contained in:
		@@ -185,31 +185,11 @@ public:
 | 
			
		||||
      return "Quectel MC60";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void setBaud(unsigned long baud) {
 | 
			
		||||
    sendAT(GF("+IPR="), baud);
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_SET_BAUD_IPR()
 | 
			
		||||
 | 
			
		||||
  bool testAT(unsigned long timeout = 10000L) {
 | 
			
		||||
    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
			
		||||
      sendAT(GF(""));
 | 
			
		||||
      if (waitResponse(200) == 1) return true;
 | 
			
		||||
      delay(100);
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_TEST_AT()
 | 
			
		||||
 | 
			
		||||
  void maintain() {
 | 
			
		||||
    for (int mux = 0; mux < TINY_GSM_MUX_COUNT; mux++) {
 | 
			
		||||
      GsmClient* sock = sockets[mux];
 | 
			
		||||
      if (sock && sock->got_data) {
 | 
			
		||||
        sock->got_data = false;
 | 
			
		||||
        sock->sock_available = modemGetAvailable(mux);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    while (stream.available()) {
 | 
			
		||||
      waitResponse(10, NULL, NULL);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_MAINTAIN_CHECK_SOCKS()
 | 
			
		||||
 | 
			
		||||
  bool factoryDefault() {
 | 
			
		||||
    sendAT(GF("&FZE0&W"));  // Factory + Reset + Echo Off + Write
 | 
			
		||||
@@ -220,17 +200,7 @@ public:
 | 
			
		||||
    return waitResponse() == 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  String getModemInfo() {
 | 
			
		||||
    sendAT(GF("I"));
 | 
			
		||||
    String res;
 | 
			
		||||
    if (waitResponse(1000L, res) != 1) {
 | 
			
		||||
      return "";
 | 
			
		||||
    }
 | 
			
		||||
    res.replace(GSM_NL "OK" GSM_NL, "");
 | 
			
		||||
    res.replace(GSM_NL, " ");
 | 
			
		||||
    res.trim();
 | 
			
		||||
    return res;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_GET_INFO_ATI()
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
  * under development
 | 
			
		||||
@@ -298,32 +268,11 @@ public:
 | 
			
		||||
   * SIM card functions
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  bool simUnlock(const char *pin) {
 | 
			
		||||
    sendAT(GF("+CPIN=\""), pin, GF("\""));
 | 
			
		||||
    return waitResponse() == 1;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_SIM_UNLOCK_CPIN()
 | 
			
		||||
 | 
			
		||||
  String getSimCCID() {
 | 
			
		||||
    sendAT(GF("+ICCID"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
 | 
			
		||||
      return "";
 | 
			
		||||
    }
 | 
			
		||||
    String res = stream.readStringUntil('\n');
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    res.trim();
 | 
			
		||||
    return res;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_GET_SIMCCID_CCID()
 | 
			
		||||
 | 
			
		||||
  String getIMEI() {
 | 
			
		||||
    sendAT(GF("+GSN"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL)) != 1) {
 | 
			
		||||
      return "";
 | 
			
		||||
    }
 | 
			
		||||
    String res = stream.readStringUntil('\n');
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    res.trim();
 | 
			
		||||
    return res;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_GET_IMEI_GSN()
 | 
			
		||||
 | 
			
		||||
  SimStatus getSimStatus(unsigned long timeout = 10000L) {
 | 
			
		||||
    for (unsigned long start = millis(); millis() - start < timeout; ) {
 | 
			
		||||
@@ -346,56 +295,22 @@ public:
 | 
			
		||||
    return SIM_ERROR;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  RegStatus getRegistrationStatus() {
 | 
			
		||||
    sendAT(GF("+CREG?"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
 | 
			
		||||
      return REG_UNKNOWN;
 | 
			
		||||
    }
 | 
			
		||||
    streamSkipUntil(','); // Skip format (0)
 | 
			
		||||
    int status = stream.readStringUntil('\n').toInt();
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    return (RegStatus)status;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_GET_REGISTRATION_XREG(CREG)
 | 
			
		||||
 | 
			
		||||
  String getOperator() {
 | 
			
		||||
    sendAT(GF("+COPS?"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
 | 
			
		||||
      return "";
 | 
			
		||||
    }
 | 
			
		||||
    streamSkipUntil('"'); // Skip mode and format
 | 
			
		||||
    String res = stream.readStringUntil('"');
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    return res;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_GET_OPERATOR_COPS()
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * Generic network functions
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  int16_t getSignalQuality() {
 | 
			
		||||
    sendAT(GF("+CSQ"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL "+CSQ:")) != 1) {
 | 
			
		||||
      return 99;
 | 
			
		||||
    }
 | 
			
		||||
    int res = stream.readStringUntil(',').toInt();
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    return res;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSP_MODEM_GET_CSQ()
 | 
			
		||||
 | 
			
		||||
  bool isNetworkConnected() {
 | 
			
		||||
    RegStatus s = getRegistrationStatus();
 | 
			
		||||
    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;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSM_MODEM_WAIT_FOR_NETWORK()
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * GPRS functions
 | 
			
		||||
@@ -466,18 +381,7 @@ public:
 | 
			
		||||
    return waitResponse(60000L, GF("DEACT OK"), GF("ERROR")) == 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool isGprsConnected() {
 | 
			
		||||
    sendAT(GF("+CGATT?"));
 | 
			
		||||
    if (waitResponse(GF(GSM_NL "+CGATT:")) != 1) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    int res = stream.readStringUntil('\n').toInt();
 | 
			
		||||
    waitResponse();
 | 
			
		||||
    if (res != 1)
 | 
			
		||||
      return false;
 | 
			
		||||
 | 
			
		||||
    return localIP() != IPAddress(0,0,0,0);
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSP_MODEM_GET_GPRS_IP_CONNECTED()
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * IP Address functions
 | 
			
		||||
@@ -730,37 +634,7 @@ public:
 | 
			
		||||
   Utilities
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  template<typename T>
 | 
			
		||||
  void streamWrite(T last) {
 | 
			
		||||
    stream.print(last);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  template<typename T, typename... Args>
 | 
			
		||||
  void streamWrite(T head, Args... tail) {
 | 
			
		||||
    stream.print(head);
 | 
			
		||||
    streamWrite(tail...);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  template<typename... Args>
 | 
			
		||||
  void sendAT(Args... cmd) {
 | 
			
		||||
    streamWrite("AT", cmd..., GSM_NL);
 | 
			
		||||
    stream.flush();
 | 
			
		||||
    TINY_GSM_YIELD();
 | 
			
		||||
    //DBG("### AT:", cmd...);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool streamSkipUntil(const char c, const unsigned long timeout = 1000L) {
 | 
			
		||||
    unsigned long startMillis = millis();
 | 
			
		||||
    while (millis() - startMillis < timeout) {
 | 
			
		||||
      while (millis() - startMillis < timeout && !stream.available()) {
 | 
			
		||||
        TINY_GSM_YIELD();
 | 
			
		||||
      }
 | 
			
		||||
      if (stream.read() == c) {
 | 
			
		||||
        return true;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
TINY_GSP_MODEM_STREAM_UTILITIES()
 | 
			
		||||
 | 
			
		||||
  // TODO: Optimize this!
 | 
			
		||||
  uint8_t waitResponse(uint32_t timeout, String& data,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user