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.

197 lines
4.3 KiB

8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file TinyGsmCommon.h
  3. * @author Volodymyr Shymanskyy
  4. * @license LGPL-3.0
  5. * @copyright Copyright (c) 2016 Volodymyr Shymanskyy
  6. * @date Nov 2016
  7. */
  8. #ifndef TinyGsmCommon_h
  9. #define TinyGsmCommon_h
  10. #if defined(SPARK) || defined(PARTICLE)
  11. #include "Particle.h"
  12. #elif defined(ARDUINO)
  13. #if ARDUINO >= 100
  14. #include "Arduino.h"
  15. #else
  16. #include "WProgram.h"
  17. #endif
  18. #endif
  19. #if defined(ARDUINO_DASH)
  20. #include <ArduinoCompat/Client.h>
  21. #else
  22. #include <Client.h>
  23. #endif
  24. #include <TinyGsmFifo.h>
  25. #ifndef TINY_GSM_YIELD
  26. #define TINY_GSM_YIELD() { delay(0); }
  27. #endif
  28. #define TINY_GSM_ATTR_NOT_AVAILABLE __attribute__((error("Not available on this modem type")))
  29. #define TINY_GSM_ATTR_NOT_IMPLEMENTED __attribute__((error("Not implemented")))
  30. #if defined(__AVR__)
  31. #define TINY_GSM_PROGMEM PROGMEM
  32. typedef const __FlashStringHelper* GsmConstStr;
  33. #define GFP(x) (reinterpret_cast<GsmConstStr>(x))
  34. #define GF(x) F(x)
  35. #else
  36. #define TINY_GSM_PROGMEM
  37. typedef const char* GsmConstStr;
  38. #define GFP(x) x
  39. #define GF(x) x
  40. #endif
  41. #ifdef TINY_GSM_DEBUG
  42. namespace {
  43. template<typename T>
  44. static void DBG_PLAIN(T last) {
  45. TINY_GSM_DEBUG.println(last);
  46. }
  47. template<typename T, typename... Args>
  48. static void DBG_PLAIN(T head, Args... tail) {
  49. TINY_GSM_DEBUG.print(head);
  50. TINY_GSM_DEBUG.print(' ');
  51. DBG_PLAIN(tail...);
  52. }
  53. template<typename... Args>
  54. static void DBG(Args... args) {
  55. TINY_GSM_DEBUG.print(GF("["));
  56. TINY_GSM_DEBUG.print(millis());
  57. TINY_GSM_DEBUG.print(GF("] "));
  58. DBG_PLAIN(args...);
  59. }
  60. }
  61. #else
  62. #define DBG(...)
  63. #endif
  64. template<class T>
  65. const T& TinyGsmMin(const T& a, const T& b)
  66. {
  67. return (b < a) ? b : a;
  68. }
  69. template<class T>
  70. const T& TinyGsmMax(const T& a, const T& b)
  71. {
  72. return (b < a) ? a : b;
  73. }
  74. template<class T>
  75. uint32_t TinyGsmAutoBaud(T& SerialAT, uint32_t minimum = 9600, uint32_t maximum = 115200)
  76. {
  77. static uint32_t rates[] = { 115200, 57600, 38400, 19200, 9600, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
  78. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  79. uint32_t rate = rates[i];
  80. if (rate < minimum || rate > maximum) continue;
  81. DBG("Trying baud rate", rate, "...");
  82. SerialAT.begin(rate);
  83. delay(10);
  84. for (int i=0; i<3; i++) {
  85. SerialAT.print("AT\r\n");
  86. String input = SerialAT.readString();
  87. if (input.indexOf("OK") >= 0) {
  88. DBG("Modem responded at rate", rate);
  89. return rate;
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. static inline
  96. IPAddress TinyGsmIpFromString(const String& strIP) {
  97. int Parts[4] = {0, };
  98. int Part = 0;
  99. for (uint8_t i=0; i<strIP.length(); i++) {
  100. char c = strIP[i];
  101. if (c == '.') {
  102. Part++;
  103. if (Part > 3) {
  104. return IPAddress(0,0,0,0);
  105. }
  106. continue;
  107. } else if (c >= '0' && c <= '9') {
  108. Parts[Part] *= 10;
  109. Parts[Part] += c - '0';
  110. } else {
  111. if (Part == 3) break;
  112. }
  113. }
  114. return IPAddress(Parts[0], Parts[1], Parts[2], Parts[3]);
  115. }
  116. static inline
  117. String TinyGsmDecodeHex7bit(String &instr) {
  118. String result;
  119. byte reminder = 0;
  120. int bitstate = 7;
  121. for (unsigned i=0; i<instr.length(); i+=2) {
  122. char buf[4] = { 0, };
  123. buf[0] = instr[i];
  124. buf[1] = instr[i+1];
  125. byte b = strtol(buf, NULL, 16);
  126. byte bb = b << (7 - bitstate);
  127. char c = (bb + reminder) & 0x7F;
  128. result += c;
  129. reminder = b >> bitstate;
  130. bitstate--;
  131. if (bitstate == 0) {
  132. char c = reminder;
  133. result += c;
  134. reminder = 0;
  135. bitstate = 7;
  136. }
  137. }
  138. return result;
  139. }
  140. static inline
  141. String TinyGsmDecodeHex8bit(String &instr) {
  142. String result;
  143. for (unsigned i=0; i<instr.length(); i+=2) {
  144. char buf[4] = { 0, };
  145. buf[0] = instr[i];
  146. buf[1] = instr[i+1];
  147. char b = strtol(buf, NULL, 16);
  148. result += b;
  149. }
  150. return result;
  151. }
  152. static inline
  153. String TinyGsmDecodeHex16bit(String &instr) {
  154. String result;
  155. for (unsigned i=0; i<instr.length(); i+=4) {
  156. char buf[4] = { 0, };
  157. buf[0] = instr[i];
  158. buf[1] = instr[i+1];
  159. char b = strtol(buf, NULL, 16);
  160. if (b) { // If high byte is non-zero, we can't handle it ;(
  161. #if defined(TINY_GSM_UNICODE_TO_HEX)
  162. result += "\\x";
  163. result += instr.substring(i, i+4);
  164. #else
  165. result += "?";
  166. #endif
  167. } else {
  168. buf[0] = instr[i+2];
  169. buf[1] = instr[i+3];
  170. b = strtol(buf, NULL, 16);
  171. result += b;
  172. }
  173. }
  174. return result;
  175. }
  176. #endif