diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b40bf0..6ada6f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +#### v 1.0.1 - 2020.01.10 +* fix: `publish` API function now always returns an ArancinoPacket. +* fix: memory issues: free is called after variable usage. + #### v 1.0.0 - 2019.12.20 * Integration with FreeRTOS * Introduced ArancinoPacket to manage returns of Cortex Protocol, error cases and etc.. diff --git a/README.md b/README.md index 9cd2ee4..61b6ec7 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,10 @@ To get started with Arancino Library you can download the latest version from th Open the Arduino IDE, unzip the _Arancino Library_ and import the unzipped folder (*Sketch* → *Include Library* → *Add .zip Library...*). The library will appear under *Sketch* → *Include Library* menu at the end, under the *Contributed* section. Examples will be under *File* → *Examples* → *Examples of Custom Libraries*. #### from Arduino Library Manager -Open the Arduino IDE and go to *Sketch* → *Include Library* → *Manage Libraries*, the Arduino Library Manager window will be shown. Using the text box, type _Arancino_; finally selecte the Arancino Library item within the result list and click install. Be sure to select the latest version available. +Open the Arduino IDE and go to *Sketch* → *Include Library* → *Manage Libraries*, the Arduino Library Manager window will be shown. Using the text box, type _Arancino_; finally selecte the Arancino Library item within the result list and click install. Be sure to select the latest version available.x *Important Note:* -> Arancino Library use [FreeRTOS_SAMD21 Arduino library](https://github.com/BriscoeTech/Arduino-FreeRTOS-SAMD21) for samd21 when runs on Atmel samd21 uC (all Arancino Boards have Atmel samd21, please download Arduino platform package index for Arancino Boards [here](https://git.smartme.io/smartme.io/arancino/arduino/smartmeio-package-index/raw/master/package_smartmeio_index.json) ). FreeRTOS Library needs some extra flags when compile sketches, so when use Aracino boards whithin Arancino Library please pay attention and select _Yes_ from _Menu -> Tools -> Using Arancino Library?:_ in Arduino IDE. Consequently select _No_ when you are not using Arancino Library. - -*Note:* -> Arancino Library has one dependency: [FreeRTOS_SAMD21 Arduino library](https://github.com/BriscoeTech/Arduino-FreeRTOS-SAMD21) by BriscoeTech. Please download it from Arduino Library Manager. +> Arancino Library has one dependency when FreeRTOS capabilities are required: [FreeRTOS_SAMD21 Arduino library](https://github.com/BriscoeTech/Arduino-FreeRTOS-SAMD21) by BriscoeTech. Please download it from Arduino Library Manager. In that case you also have to select _Yes_ from _Menu -> Tools -> Using FreeRTOS?_. ## Data structures @@ -1135,46 +1132,10 @@ void loop() { ``` ___ ### publish -##### *int publish(char* channel, char* message)* -##### *int publish(int channel, char* message)* -Posts a message to the given channel. - - -##### Parameters -* **`channel`**: the name of the *channel* where the message will be sent. -* **`message`**: *message* to send. - -##### Return value -int reply: the number of clients that received the message. - -##### Example -```c++ -#include - -void setup() { +##### *ArancinoPacket publish(char* channel, char* message)* +##### *ArancinoPacket publish(char* channel, int message)* +##### *ArancinoPacket publish(char* channel, float message)* - Arancino.begin(); - Serial.begin(115200); - -} - -void loop() { - - int resp = Arancino.publish(0,"Hello from Arancino"); - Serial.print("Message sent to ") - Serial.print(resp); - Serial.println(" clients"); - //Message sent to 0 client - - delay(5000); //wait 5 seconds - -} -``` - -___ -### publishPacket -##### *ArancinoPacket publishPacket(char* channel, char* message)* -##### *ArancinoPacket publishPacket(int channel, char* message)* Posts a message to the given channel. @@ -1201,7 +1162,7 @@ void setup() { } void loop() { - ArancinoPacket temp = Arancino.publishPacket(0,"Hello from Arancino"); + ArancinoPacket temp = Arancino.publish(0,"Hello from Arancino"); if (!temp.isError) { Serial.println("PUBLISH OK"); diff --git a/examples/Arancino/12-PUBLISH/PUBLISH/PUBLISH.ino b/examples/Arancino/12-PUBLISH/PUBLISH/PUBLISH.ino index 9207803..8f94fda 100644 --- a/examples/Arancino/12-PUBLISH/PUBLISH/PUBLISH.ino +++ b/examples/Arancino/12-PUBLISH/PUBLISH/PUBLISH.ino @@ -47,7 +47,7 @@ void setup() { void loop() { - int resp = Arancino.publish(0,"hello from Arancino"); + int resp = Arancino.publish("0","hello from Arancino"); Serial.print("message send to ") Serial.print(resp); Serial.println(" client"); diff --git a/examples/Arancino/12-PUBLISH/PUBLISH_Packet/PUBLISH_Packet.ino b/examples/Arancino/12-PUBLISH/PUBLISH_Packet/PUBLISH_Packet.ino index 4a96c23..da67889 100644 --- a/examples/Arancino/12-PUBLISH/PUBLISH_Packet/PUBLISH_Packet.ino +++ b/examples/Arancino/12-PUBLISH/PUBLISH_Packet/PUBLISH_Packet.ino @@ -45,7 +45,7 @@ void setup() { } void loop() { - ArancinoPacket apckt = Arancino.publish(0,"Hello from Arancino"); + ArancinoPacket apckt = Arancino.publish("0","Hello from Arancino"); if (!apckt.isError) { Serial.println("PUBLISH OK"); diff --git a/library.properties b/library.properties index ed335b3..8444024 100755 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arancino -version=1.0.0 +version=1.0.1 author=smartme.IO maintainer=smartme.IO sentence=Enables communication between microcontroller and Arancino Module running mainly in Arancino boards. diff --git a/src/Arancino.cpp b/src/Arancino.cpp index db8d868..23be212 100644 --- a/src/Arancino.cpp +++ b/src/Arancino.cpp @@ -98,6 +98,7 @@ void ArancinoClass::begin(int timeout) { { ArancinoPacket temp = {false, _getResponseCode(message), VOID, {.string = NULL}}; packet = temp; + std::free(message); } else { @@ -304,6 +305,7 @@ ArancinoPacket ArancinoClass::_getPacket( char* key ) { { ArancinoPacket temp = {false, _getResponseCode(message), STRING, {.string = _parse(message)}}; packet = temp; + std::free(message); } else { @@ -387,6 +389,7 @@ ArancinoPacket ArancinoClass::_delPacket(char* key) { ArancinoPacket temp = {false, _getResponseCode(message), INT, {.integer = atoi(messageParsed)}}; packet = temp; std::free(messageParsed); + std::free(message); } else { @@ -555,6 +558,7 @@ ArancinoPacket ArancinoClass::_hgetPacket( char* key, char* field ) { { ArancinoPacket temp = {false, _getResponseCode(message), STRING, {.string = _parse(message)}}; //TODO getStatus to _getResponseCode packet = temp; + std::free(message); } else { @@ -637,6 +641,7 @@ ArancinoPacket ArancinoClass::_hgetallPacket(char* key) { { ArancinoPacket temp = {false, _getResponseCode(message), STRING_ARRAY, {.stringArray = _parseArray(_parse(message))}}; packet = temp; + std::free(message); } else { @@ -718,6 +723,7 @@ ArancinoPacket ArancinoClass::_hkeysPacket(char* key) { { ArancinoPacket temp = {false, _getResponseCode(message), STRING_ARRAY, {.stringArray = _parseArray(_parse(message))}}; packet = temp; + std::free(message); } else { @@ -799,6 +805,7 @@ ArancinoPacket ArancinoClass::_hvalsPacket(char* key) { { ArancinoPacket temp = {false, _getResponseCode(message), STRING_ARRAY, {.stringArray = _parseArray(_parse(message))}}; packet = temp; + std::free(message); } else { @@ -888,6 +895,7 @@ ArancinoPacket ArancinoClass::_hdelPacket( char* key, char* field) { ArancinoPacket temp = {false, _getResponseCode(message), INT, {.integer = atoi(messageParsed)}}; packet = temp; std::free(messageParsed); + std::free(message); } else { @@ -960,6 +968,7 @@ ArancinoPacket ArancinoClass::_keysPacket(char* pattern){ { ArancinoPacket temp = {false, _getResponseCode(message), STRING_ARRAY, {.stringArray = _parseArray(_parse(message))}}; packet = temp; + std::free(message); } else { @@ -993,36 +1002,10 @@ template<> char** ArancinoClass::keys(char* pattern){ /******** API BASIC :: PUBLISH *********/ -ArancinoPacket ArancinoClass::_publishPacket(int channel, char* msg) { - char str[20] = ""; - itoa(channel, str, 10); - return __publish(str, msg); -} - ArancinoPacket ArancinoClass::_publishPacket(char* channel, char* msg) { return __publish(channel, msg); } -int ArancinoClass::_publish(int channel, char* msg) { - ArancinoPacket packet = _publishPacket(channel, msg); - int retValue = 0; - if (!packet.isError) - { - retValue = packet.response.integer; - } - return retValue; -} - -int ArancinoClass::_publish(char* channel, char* msg) { - ArancinoPacket packet = _publishPacket(channel, msg); - int retValue = 0; - if (!packet.isError) - { - retValue = packet.response.integer; - } - return retValue; -} - ArancinoPacket ArancinoClass::__publish(char* channel, char* msg) { if(_isReservedKey(channel)){ //TODO maybe it's better to print a log @@ -1079,6 +1062,7 @@ ArancinoPacket ArancinoClass::__publish(char* channel, char* msg) { ArancinoPacket temp = {false, _getResponseCode(message), INT, {.integer = atoi(messageParsed)}}; packet = temp; std::free(messageParsed); + std::free(message); } else { @@ -1088,22 +1072,23 @@ ArancinoPacket ArancinoClass::__publish(char* channel, char* msg) { return packet; } -template<> ArancinoPacket ArancinoClass::publish (int channel, char* msg){ +ArancinoPacket ArancinoClass::publish(char* channel, char* msg){ return _publishPacket(channel, msg); } -template<> ArancinoPacket ArancinoClass::publish (char* channel, char* msg){ - return _publishPacket(channel, msg); -} +ArancinoPacket ArancinoClass::publish(char* channel, double msg){ + char str[20] = ""; + _doubleToString(msg, 4, str); -template<> int ArancinoClass::publish(int channel, char* msg){ - return _publish(channel, msg); + return _publishPacket(channel, str); } -template<> int ArancinoClass::publish(char* channel, char* msg){ - return _publish(channel, msg); -} +ArancinoPacket ArancinoClass::publish(char* channel, int msg){ + char str[20] = ""; + itoa(msg, str, 10); + return _publishPacket(channel, str); +} /******** API BASIC :: FLUSH *********/ @@ -1146,6 +1131,7 @@ ArancinoPacket ArancinoClass::flush() { ArancinoPacket temp = {false, _getResponseCode(message), VOID, {.string = NULL}}; packet = temp; std::free(messageParsed); + std::free(message); } else { @@ -1437,7 +1423,6 @@ char* ArancinoClass::_parse(char* message) { } #endif - std::free(message); std::free(status); return value; diff --git a/src/Arancino.h b/src/Arancino.h index 418f996..882c428 100644 --- a/src/Arancino.h +++ b/src/Arancino.h @@ -166,13 +166,10 @@ class ArancinoClass { template T keys(char* pattern=""); //PUBLISH - // ArancinoPacket publishPacket(int channel, char* msg); - // ArancinoPacket publishPacket(char* channel, char* msg); - // int publish(int channel, char* msg); - // int publish(char* channel, char* msg); - //PUBLISH W TEMPALTE - template T publish(int channel, char* msg); - template T publish(char* channel, char* msg); + ArancinoPacket publish(char* channel, char* msg); + ArancinoPacket publish(char* channel, double msg); + ArancinoPacket publish(char* channel, int msg); + //FLUSH ArancinoPacket flush(void); diff --git a/src/ArancinoConfig.h b/src/ArancinoConfig.h index 778dbf0..7ae6f9b 100644 --- a/src/ArancinoConfig.h +++ b/src/ArancinoConfig.h @@ -73,7 +73,7 @@ under the License #define LIBVERS_KEY "___LIBVERS___" #define MODVERS_KEY "___MODVERS___" #define POWER_KEY "___POWER___" -#define LIB_VERSION "1.0.0" //library version +#define LIB_VERSION "1.0.1" //library version //RESPONSE CODE #define RESERVED_KEY_ERROR -3