9 #ifndef TinyGsmCommon_h
10 #define TinyGsmCommon_h
12 #if defined(SPARK) || defined(PARTICLE)
14 #elif defined(ARDUINO)
23 #include "TinyGsmFifo.h"
25 #ifndef TINY_GSM_YIELD
26 #define TINY_GSM_YIELD() { delay(0); }
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")))
33 #define TINY_GSM_PROGMEM PROGMEM
34 typedef const __FlashStringHelper* GsmConstStr;
35 #define GFP(x) (reinterpret_cast<GsmConstStr>(x))
38 #define TINY_GSM_PROGMEM
39 typedef const char* GsmConstStr;
48 static void DBG(T
last)
50 TINY_GSM_DEBUG.println(
last);
53 template<
typename T,
typename... Args>
54 static void DBG(T head, Args... tail)
56 TINY_GSM_DEBUG.print(head);
57 TINY_GSM_DEBUG.print(
' ');
66 const T& TinyGsmMin(
const T& a,
const T& b)
68 return (b < a) ? b : a;
72 const T& TinyGsmMax(
const T& a,
const T& b)
74 return (b < a) ? a : b;
78 uint32_t TinyGsmAutoBaud(T& SerialAT, uint32_t minimum = 9600, uint32_t maximum = 115200)
80 static uint32_t rates[] = { 115200, 57600, 38400, 19200, 9600, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
82 for (
unsigned i = 0; i <
sizeof(rates)/
sizeof(rates[0]); i++) {
83 uint32_t rate = rates[i];
84 if (rate < minimum || rate > maximum) {
88 DBG(
"Trying baud rate", rate,
"...");
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);
104 IPAddress TinyGsmIpFromString(
const String& strIP)
106 int Parts[4] = {0, };
108 for (uint8_t i=0; i<strIP.length(); i++) {
116 }
else if (c >=
'0' && c <=
'9') {
118 Parts[Part] += c -
'0';
125 return IPAddress(Parts[0], Parts[1], Parts[2], Parts[3]);
129 String TinyGsmDecodeHex7bit(String &instr)
134 for (
unsigned i=0; i<instr.length(); i+=2) {
135 char buf[4] = { 0, };
138 byte b = strtol(buf, NULL, 16);
140 byte bb = b << (7 - bitstate);
141 char c = (bb + reminder) & 0x7F;
143 reminder = b >> bitstate;
156 String TinyGsmDecodeHex8bit(String &instr)
159 for (
unsigned i=0; i<instr.length(); i+=2) {
160 char buf[4] = { 0, };
163 char b = strtol(buf, NULL, 16);
170 String TinyGsmDecodeHex16bit(String &instr)
173 for (
unsigned i=0; i<instr.length(); i+=4) {
174 char buf[4] = { 0, };
177 char b = strtol(buf, NULL, 16);
179 #if defined(TINY_GSM_UNICODE_TO_HEX)
181 result += instr.substring(i, i+4);
188 b = strtol(buf, NULL, 16);