use common TinyGsmUTFSMS calss
This commit is contained in:
		| @@ -40,7 +40,7 @@ enum RegStatus { | ||||
| }; | ||||
|  | ||||
|  | ||||
| class TinyGsmBG96 | ||||
| class TinyGsmBG96: public TinyGsmUTFSMS<TinyGsmBG96> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -409,33 +409,6 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|  | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   /* | ||||
|    * Location functions | ||||
|   | ||||
| @@ -40,7 +40,7 @@ enum RegStatus { | ||||
| }; | ||||
|  | ||||
|  | ||||
| class TinyGsmM95 | ||||
| class TinyGsmM95: public TinyGsmUTFSMS<TinyGsmM95> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -474,33 +474,6 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|  | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   /** Delete all SMS */ | ||||
|   bool deleteAllSMS() { | ||||
|     sendAT(GF("+QMGDA=6")); | ||||
|   | ||||
| @@ -44,7 +44,7 @@ enum RegStatus { | ||||
| }; | ||||
|  | ||||
|  | ||||
| class TinyGsmMC60 | ||||
| class TinyGsmMC60: public TinyGsmUTFSMS<TinyGsmMC60> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -458,33 +458,6 @@ TINY_GSM_MODEM_GET_GPRS_IP_CONNECTED() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|  | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   /** Delete all SMS */ | ||||
|   bool deleteAllSMS() { | ||||
|     sendAT(GF("+QMGDA=6")); | ||||
|   | ||||
| @@ -45,7 +45,7 @@ enum TinyGSMDateTimeFormat { | ||||
|   DATE_DATE = 2 | ||||
| }; | ||||
|  | ||||
| class TinyGsmSim5360 | ||||
| class TinyGsmSim5360: public TinyGsmUTFSMS<TinyGsmSim5360> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -520,36 +520,6 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     // Select message format (1=text) | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     // Select TE character set | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     // Set text mode parameters | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|     // Send the message | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   /* | ||||
|    * Location functions | ||||
|   | ||||
| @@ -45,7 +45,7 @@ enum TinyGSMDateTimeFormat { | ||||
|   DATE_DATE = 2 | ||||
| }; | ||||
|  | ||||
| class TinyGsmSim7000 | ||||
| class TinyGsmSim7000: public TinyGsmUTFSMS<TinyGsmSim7000> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -562,33 +562,6 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|  | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   /* | ||||
|    * Location functions | ||||
|   | ||||
| @@ -45,7 +45,7 @@ enum TinyGSMDateTimeFormat { | ||||
|   DATE_DATE = 2 | ||||
| }; | ||||
|  | ||||
| class TinyGsmSim7600 | ||||
| class TinyGsmSim7600: public TinyGsmUTFSMS<TinyGsmSim7600> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -496,36 +496,6 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     // Select message format (1=text) | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     // Select TE character set | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     // Set text mode parameters | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|     // Send the message | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   /* | ||||
|    * Location functions | ||||
|   | ||||
| @@ -46,7 +46,7 @@ enum TinyGSMDateTimeFormat { | ||||
|   DATE_DATE = 2 | ||||
| }; | ||||
|  | ||||
| class TinyGsmSim800 | ||||
| class TinyGsmSim800 : public TinyGsmUTFSMS<TinyGsmSim800> | ||||
| { | ||||
|  | ||||
| public: | ||||
| @@ -593,34 +593,6 @@ TINY_GSM_MODEM_WAIT_FOR_NETWORK() | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const String& number, const void* text, size_t len) { | ||||
|     sendAT(GF("+CMGF=1")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     waitResponse(); | ||||
|     sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     waitResponse(); | ||||
|  | ||||
|     sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     if (waitResponse(GF(">")) != 1) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { stream.print('0'); } | ||||
|       stream.print(c, HEX); | ||||
|     } | ||||
|     stream.write((char)0x1A); | ||||
|     stream.flush(); | ||||
|     return waitResponse(60000L) == 1; | ||||
|   } | ||||
|  | ||||
|  | ||||
|   /* | ||||
|    * Location functions | ||||
|    */ | ||||
|   | ||||
| @@ -645,4 +645,77 @@ String TinyGsmDecodeHex16bit(String &instr) { | ||||
|   } | ||||
|  | ||||
|  | ||||
| //Common methods for UTF8/UTF16 SMS.  | ||||
| //Supported by: BG96, M95, MC60, SIM5360, SIM7000, SIM7600, SIM800 | ||||
| template<class T> | ||||
| class TinyGsmUTFSMS { | ||||
| public: | ||||
|  | ||||
|   class UTF8Print : public Print { | ||||
|   public: | ||||
|     UTF8Print(Print& p) : p(p) {} | ||||
|     virtual size_t write(const uint8_t c) override { | ||||
|       if(prv < 0xC0) { | ||||
|         if(c < 0xC0) printHex(c); | ||||
|         prv = c; | ||||
|       } else { | ||||
|         uint16_t v = uint16_t(prv)<<8 | c; | ||||
|         v -= (v>>8 == 0xD0)? 0xCC80 : 0xCD40; | ||||
|         printHex(v); | ||||
|         prv = 0; | ||||
|       } | ||||
|       return 1; | ||||
|     } | ||||
|   private: | ||||
|     Print& p; | ||||
|     uint8_t prv = 0; | ||||
|     void printHex(const uint16_t v) { | ||||
|       uint8_t c = v >> 8; | ||||
|       if (c < 0x10) p.print('0'); | ||||
|       p.print(c, HEX); | ||||
|       c = v & 0xFF; | ||||
|       if (c < 0x10) p.print('0'); | ||||
|       p.print(c, HEX); | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   bool sendSMS_UTF8_begin(const char* const number) { | ||||
|     static_cast<T*>(this)->sendAT(GF("+CMGF=1")); | ||||
|     static_cast<T*>(this)->waitResponse(); | ||||
|     static_cast<T*>(this)->sendAT(GF("+CSCS=\"HEX\"")); | ||||
|     static_cast<T*>(this)->waitResponse(); | ||||
|     static_cast<T*>(this)->sendAT(GF("+CSMP=17,167,0,8")); | ||||
|     static_cast<T*>(this)->waitResponse(); | ||||
|  | ||||
|     static_cast<T*>(this)->sendAT(GF("+CMGS=\""), number, GF("\"")); | ||||
|     return static_cast<T*>(this)->waitResponse(GF(">")) == 1; | ||||
|   } | ||||
|   bool sendSMS_UTF8_end() { | ||||
|     static_cast<T*>(this)->stream.write((char)0x1A); | ||||
|     static_cast<T*>(this)->stream.flush(); | ||||
|     return static_cast<T*>(this)->waitResponse(60000L) == 1; | ||||
|   } | ||||
|   UTF8Print sendSMS_UTF8_stream() { | ||||
|     return UTF8Print(static_cast<T*>(this)->stream); | ||||
|   } | ||||
|  | ||||
|   bool sendSMS_UTF16(const char* const number, const void* text, size_t len) { | ||||
|     if (!sendSMS_UTF8_begin(number)) { | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     uint16_t* t = (uint16_t*)text; | ||||
|     for (size_t i=0; i<len; i++) { | ||||
|       uint8_t c = t[i] >> 8; | ||||
|       if (c < 0x10) { static_cast<T*>(this)->stream.print('0'); } | ||||
|       static_cast<T*>(this)->stream.print(c, HEX); | ||||
|       c = t[i] & 0xFF; | ||||
|       if (c < 0x10) { static_cast<T*>(this)->stream.print('0'); } | ||||
|       static_cast<T*>(this)->stream.print(c, HEX); | ||||
|     } | ||||
|      | ||||
|     return sendSMS_UTF8_end(); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| #endif | ||||
|   | ||||
		Reference in New Issue
	
	Block a user