diff --git a/lora-socket.h b/lora-socket.h index 29bd404..a1f7c98 100644 --- a/lora-socket.h +++ b/lora-socket.h @@ -33,19 +33,47 @@ class LoRaSocket { inline static void onReceived(void (*f)(String, String, String, String)){ _f = f; }; - + inline bool isNewMsg(){ + isLoopMode = true; + return (newType == "") ? false : true; + }; + inline const String getNewMsg(){ + String msg = newMsg; + clearNewMsg(); + return msg; + }; + inline void getNewMsg(String& msg, String& from, String& to, String& type){ + msg = newMsg; + from = newFrom; + to = newTo; + type = newType; + clearNewMsg(); + }; private: static StringVec tcp_sendingStack, tcp_receiveStack; static Vector tcp_sendingTryTimes; static void(*_f)(String, String, String, String); + static String newMsg, newFrom, newTo, newType; + static bool isLoopMode; + inline void clearNewMsg(){ + newMsg = ""; + newFrom = ""; + newTo = ""; + newType = ""; + }; inline static void _onReceived(const String& msg, const String& from, const String& to, const String& type){ - (*_f)(msg, from, to, type); + newMsg = msg; + newFrom = from; + newTo = to; + newType = type; + if(!isLoopMode) (*_f)(msg, from, to, type); }; /* LoRa Functions */ static void LoRa_tx_mode(); static void LoRa_rx_mode(); static void send(const String& s); + static void send(const char *s); static const String receiveMsg(); /* Package Functions */ inline static const String getIPHeader(const String& to = "0.0.0.0"){ @@ -135,6 +163,7 @@ class LoRaSocket { }; void LoRaSocket::getMsg(const String& msg){ + if(!isGoodPackage(msg)) return; if(getToIP(msg) != LORA_SOCKET_IP && getToIP(msg) != "0.0.0.0") return; @@ -152,9 +181,12 @@ void LoRaSocket::getMsg(const String& msg){ } void LoRaSocket::udp(const String& msg, const String& to){ - String fin = "udp|"+ getIPHeader(to) + encode(msg) + "|"; - fin += hash(fin); - send(fin); + char *c; + c = (char*)malloc((msg.length()+39)*sizeof(char)); + sprintf(c, "udp|%s%s|", getIPHeader(to).c_str(), encode(msg).c_str()); + sprintf(c, "%s%s", c, hash(c).c_str()); + send(c); + free(c); }; @@ -178,10 +210,12 @@ void LoRaSocket::rtcp(const String& msg){ StringVec LoRaSocket::tcp_sendingStack, LoRaSocket::tcp_receiveStack; Vector LoRaSocket::tcp_sendingTryTimes; void (*LoRaSocket::_f)(String, String, String, String); +String LoRaSocket::newMsg = "", LoRaSocket::newFrom = "", LoRaSocket::newTo = "", LoRaSocket::newType = ""; +bool LoRaSocket::isLoopMode = false; void LoRaSocket::ini() { - + LoRa_rx_mode(); } void LoRaSocket::core() { @@ -204,10 +238,20 @@ void LoRaSocket::LoRa_rx_mode(){ LoRa.receive(); } +void LoRaSocket::send(const char *s){ + LoRa_tx_mode(); + delay(200); + LoRa.beginPacket();Serial.println(s); + LoRa.print(s); + LoRa.endPacket(); + delay(200); + LoRa_rx_mode(); +} + void LoRaSocket::send(const String& s){ LoRa_tx_mode(); delay(200); - LoRa.beginPacket(); + LoRa.beginPacket();Serial.println(s); LoRa.print(s); LoRa.endPacket(); delay(200);