Skip to content

Commit

Permalink
Merge branch 'fix_publish_without_template' into 'master'
Browse files Browse the repository at this point in the history
Fix publish without template

See merge request smartme.io/arancino/arancino-library!6
  • Loading branch information
sergiotomasello committed Jan 17, 2020
2 parents d18a80d + 6cfbbb5 commit 7894435
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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..
Expand Down
51 changes: 6 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <Arancino.h>

void setup() {
##### *ArancinoPacket publish(char&ast; channel, char&ast; message)*
##### *ArancinoPacket publish(char&ast; channel, int message)*
##### *ArancinoPacket publish(char&ast; 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&ast; channel, char&ast; message)*
##### *ArancinoPacket publishPacket(int channel, char&ast; message)*
Posts a message to the given channel.


Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion examples/Arancino/12-PUBLISH/PUBLISH/PUBLISH.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void setup() {
}

void loop() {
ArancinoPacket apckt = Arancino.publish<ArancinoPacket>(0,"Hello from Arancino");
ArancinoPacket apckt = Arancino.publish("0","Hello from Arancino");
if (!apckt.isError)
{
Serial.println("PUBLISH OK");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arancino
version=1.0.0
version=1.0.1
author=smartme.IO
maintainer=smartme.IO <[email protected]>
sentence=Enables communication between microcontroller and Arancino Module running mainly in Arancino boards.
Expand Down
57 changes: 21 additions & 36 deletions src/Arancino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void ArancinoClass::begin(int timeout) {
{
ArancinoPacket temp = {false, _getResponseCode(message), VOID, {.string = NULL}};
packet = temp;
std::free(message);
}
else
{
Expand Down Expand Up @@ -304,6 +305,7 @@ ArancinoPacket ArancinoClass::_getPacket( char* key ) {
{
ArancinoPacket temp = {false, _getResponseCode(message), STRING, {.string = _parse(message)}};
packet = temp;
std::free(message);
}
else
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -1088,22 +1072,23 @@ ArancinoPacket ArancinoClass::__publish(char* channel, char* msg) {
return packet;
}

template<> ArancinoPacket ArancinoClass::publish<ArancinoPacket> (int channel, char* msg){
ArancinoPacket ArancinoClass::publish(char* channel, char* msg){
return _publishPacket(channel, msg);
}

template<> ArancinoPacket ArancinoClass::publish<ArancinoPacket> (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 *********/

Expand Down Expand Up @@ -1146,6 +1131,7 @@ ArancinoPacket ArancinoClass::flush() {
ArancinoPacket temp = {false, _getResponseCode(message), VOID, {.string = NULL}};
packet = temp;
std::free(messageParsed);
std::free(message);
}
else
{
Expand Down Expand Up @@ -1437,7 +1423,6 @@ char* ArancinoClass::_parse(char* message) {
}
#endif

std::free(message);
std::free(status);
return value;

Expand Down
11 changes: 4 additions & 7 deletions src/Arancino.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,10 @@ class ArancinoClass {
template<class T = char**> 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<class T = int> T publish(int channel, char* msg);
template<class T = int> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/ArancinoConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7894435

Please sign in to comment.