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.

174 lines
4.6 KiB

5 years ago
5 years ago
5 years ago
  1. /**
  2. * @file TinyGsmGPRS.tpp
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef SRC_TINYGSMGPRS_H_
  9. #define SRC_TINYGSMGPRS_H_
  10. #include "TinyGsmCommon.h"
  11. #define TINY_GSM_MODEM_HAS_GPRS
  12. enum SimStatus {
  13. SIM_ERROR = 0,
  14. SIM_READY = 1,
  15. SIM_LOCKED = 2,
  16. SIM_ANTITHEFT_LOCKED = 3,
  17. };
  18. template <class modemType>
  19. class TinyGsmGPRS {
  20. public:
  21. /*
  22. * SIM card functions
  23. */
  24. // Unlocks the SIM
  25. bool simUnlock(const char* pin) {
  26. return thisModem().simUnlockImpl(pin);
  27. }
  28. // Gets the CCID of a sim card via AT+CCID
  29. String getSimCCID() {
  30. return thisModem().getSimCCIDImpl();
  31. }
  32. // Asks for TA Serial Number Identification (IMEI)
  33. String getIMEI() {
  34. return thisModem().getIMEIImpl();
  35. }
  36. // Asks for International Mobile Subscriber Identity IMSI
  37. String getIMSI() {
  38. return thisModem().getIMSIImpl();
  39. }
  40. SimStatus getSimStatus(uint32_t timeout_ms = 10000L) {
  41. return thisModem().getSimStatusImpl(timeout_ms);
  42. }
  43. /*
  44. * GPRS functions
  45. */
  46. bool gprsConnect(const char* apn, const char* user = NULL,
  47. const char* pwd = NULL) {
  48. return thisModem().gprsConnectImpl(apn, user, pwd);
  49. }
  50. bool gprsDisconnect() {
  51. return thisModem().gprsDisconnectImpl();
  52. }
  53. // Checks if current attached to GPRS/EPS service
  54. bool isGprsConnected() {
  55. return thisModem().isGprsConnectedImpl();
  56. }
  57. // Gets the current network operator
  58. String getOperator() {
  59. return thisModem().getOperatorImpl();
  60. }
  61. /*
  62. * CRTP Helper
  63. */
  64. protected:
  65. inline const modemType& thisModem() const {
  66. return static_cast<const modemType&>(*this);
  67. }
  68. inline modemType& thisModem() {
  69. return static_cast<modemType&>(*this);
  70. }
  71. /*
  72. * SIM card functions
  73. */
  74. protected:
  75. // Unlocks a sim via the 3GPP TS command AT+CPIN
  76. bool simUnlockImpl(const char* pin) {
  77. if (pin && strlen(pin) > 0) {
  78. thisModem().sendAT(GF("+CPIN=\""), pin, GF("\""));
  79. return thisModem().waitResponse() == 1;
  80. }
  81. return true;
  82. }
  83. // Gets the CCID of a sim card via AT+CCID
  84. String getSimCCIDImpl() {
  85. thisModem().sendAT(GF("+CCID"));
  86. if (thisModem().waitResponse(GF("+CCID:")) != 1) { return ""; }
  87. String res = thisModem().stream.readStringUntil('\n');
  88. thisModem().waitResponse();
  89. res.trim();
  90. return res;
  91. }
  92. // Asks for TA Serial Number Identification (IMEI) via the V.25TER standard
  93. // AT+GSN command
  94. String getIMEIImpl() {
  95. thisModem().sendAT(GF("+GSN"));
  96. thisModem().streamSkipUntil('\n'); // skip first newline
  97. String res = thisModem().stream.readStringUntil('\n');
  98. thisModem().waitResponse();
  99. res.trim();
  100. return res;
  101. }
  102. // Asks for International Mobile Subscriber Identity IMSI via the AT+CIMI
  103. // command
  104. String getIMSIImpl() {
  105. thisModem().sendAT(GF("+CIMI"));
  106. thisModem().streamSkipUntil('\n'); // skip first newline
  107. String res = thisModem().stream.readStringUntil('\n');
  108. thisModem().waitResponse();
  109. res.trim();
  110. return res;
  111. }
  112. SimStatus getSimStatusImpl(uint32_t timeout_ms = 10000L) {
  113. for (uint32_t start = millis(); millis() - start < timeout_ms;) {
  114. thisModem().sendAT(GF("+CPIN?"));
  115. if (thisModem().waitResponse(GF("+CPIN:")) != 1) {
  116. delay(1000);
  117. continue;
  118. }
  119. int8_t status =
  120. thisModem().waitResponse(GF("READY"), GF("SIM PIN"), GF("SIM PUK"),
  121. GF("NOT INSERTED"), GF("NOT READY"));
  122. thisModem().waitResponse();
  123. switch (status) {
  124. case 2:
  125. case 3: return SIM_LOCKED;
  126. case 1: return SIM_READY;
  127. default: return SIM_ERROR;
  128. }
  129. }
  130. return SIM_ERROR;
  131. }
  132. /*
  133. * GPRS functions
  134. */
  135. protected:
  136. bool thisHasGPRS() {
  137. return true;
  138. }
  139. // Checks if current attached to GPRS/EPS service
  140. bool isGprsConnectedImpl() {
  141. thisModem().sendAT(GF("+CGATT?"));
  142. if (thisModem().waitResponse(GF("+CGATT:")) != 1) { return false; }
  143. int8_t res = thisModem().streamGetIntBefore('\n');
  144. thisModem().waitResponse();
  145. if (res != 1) { return false; }
  146. return thisModem().localIP() != IPAddress(0, 0, 0, 0);
  147. }
  148. // Gets the current network operator via the 3GPP TS command AT+COPS
  149. String getOperatorImpl() {
  150. thisModem().sendAT(GF("+COPS?"));
  151. if (thisModem().waitResponse(GF("+COPS:")) != 1) { return ""; }
  152. thisModem().streamSkipUntil('"'); /* Skip mode and format */
  153. String res = thisModem().stream.readStringUntil('"');
  154. thisModem().waitResponse();
  155. return res;
  156. }
  157. };
  158. #endif // SRC_TINYGSMGPRS_H_