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