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.

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