Skip to content

Commit

Permalink
refactor: ClusterDuck-Protocol#423 replace arduino string with std::s…
Browse files Browse the repository at this point in the history
…tring (ClusterDuck-Protocol#452)

- the current examples contained traces of arduino string class

- replaced with std::string to align with standards

Signed-off-by: Miguel Cárdenas <[email protected]>
  • Loading branch information
miguelcsx authored Oct 8, 2024
1 parent 14ed77c commit 2b2895d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 44 deletions.
15 changes: 9 additions & 6 deletions examples/Basic-Ducks/DuckLink/DuckLink.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <arduino-timer.h>
#include <string>
#include <vector>
#include <CDP.h>


Expand Down Expand Up @@ -39,7 +40,8 @@ void setup() {
devId.insert(devId.end(), deviceId.begin(), deviceId.end());
rc = duck.setupWithDefaults(devId);
if (rc != DUCK_ERR_NONE) {
Serial.print("[LINK] Failed to setup ducklink: rc = ");Serial .println(rc);
Serial.print("[LINK] Failed to setup ducklink: rc = ");
Serial.println(rc);
return;
}

Expand All @@ -64,7 +66,7 @@ void loop() {
duck.run();
}

std::vector<byte> stringToByteVector(const String& str) {
std::vector<byte> stringToByteVector(const std::string& str) {
std::vector<byte> byteVec;
byteVec.reserve(str.length());

Expand All @@ -79,21 +81,22 @@ bool sendSensorData() {
bool result = false;
const byte* buffer;

String message = String("Counter:") + String(counter);
std::string message = std::string("Counter:") + std::to_string(counter);
Serial.print("[LINK] sensor data: ");
Serial.println(message);
Serial.println(message.c_str());

int err = duck.sendData(topics::status, stringToByteVector(message));
if (err == DUCK_ERR_NONE) {
result = true;
counter++;
} else {
Serial.println("[LINK] Failed to send data. error = " + String(err));
std::string errMessage = "[LINK] Failed to send data. error = " + std::to_string(err);
Serial.println(errMessage.c_str());
return false;
}
return result;
}

bool runSensor(void *) {
return sendSensorData();
}
}
10 changes: 6 additions & 4 deletions examples/Basic-Ducks/MamaDuck/MamaDuck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <string>
#include <vector>
#include <arduino-timer.h>
#include <CDP.h>

Expand Down Expand Up @@ -47,7 +48,7 @@ void setup() {

}

std::vector<byte> stringToByteVector(const String& str) {
std::vector<byte> stringToByteVector(const std::string& str) {
std::vector<byte> byteVec;
byteVec.reserve(str.length());

Expand All @@ -72,9 +73,9 @@ void loop() {
bool runSensor(void *) {
bool result;

String message = String("Counter:") + String(counter)+ " " +String("Free Memory:") + String(freeMemory());
std::string message = std::string("Counter:") + std::to_string(counter) + " " + std::string("Free Memory:") + std::to_string(freeMemory());
Serial.print("[MAMA] sensor data: ");
Serial.println(message);
Serial.println(message.c_str());

result = sendData(stringToByteVector(message));
if (result) {
Expand All @@ -94,7 +95,8 @@ bool sendData(std::vector<byte> message) {
sentOk = true;
}
if (!sentOk) {
Serial.println("[MAMA] Failed to send data. error = " + String(err));
std::string errMessage = "[MAMA] Failed to send data. error = " + std::to_string(err);
Serial.println(errMessage.c_str());
}
return sentOk;
}
22 changes: 11 additions & 11 deletions examples/Basic-Ducks/PapaDuck/PapaDuck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


std::string toTopicString(byte topic);
String convertToHex(byte* data, int size);
std::string convertToHex(byte* data, int size);
int toJSON(CdpPacket packet);


Expand Down Expand Up @@ -89,9 +89,9 @@ std::string toTopicString(byte topic)
return topicString;
}

String convertToHex(byte* data, int size)
std::string convertToHex(byte* data, int size)
{
String buf = "";
std::string buf = "";
buf.reserve(size * 2); // 2 digit hex
const char* cs = "0123456789ABCDEF";
for (int i = 0; i < size; i++) {
Expand All @@ -118,15 +118,15 @@ int toJSON(CdpPacket packet)
std::string dduid = ss.str();
std::string muid(packet.muid.begin(), packet.muid.end());

Serial.println("[PAPA] topic: " + String(toTopicString(packet.topic).c_str()));
Serial.println("[PAPA] topic: " + std::string(toTopicString(packet.topic).c_str()));

Serial.println("[PAPA] sduid: " + String(sduid.c_str()));
Serial.println("[PAPA] dduid: " + String(dduid.c_str()));
Serial.println("[PAPA] sduid: " + std::string(sduid.c_str()));
Serial.println("[PAPA] dduid: " + std::string(dduid.c_str()));

Serial.println("[PAPA] muid: " + String(muid.c_str()));
Serial.println("[PAPA] data: " + String(payload.c_str()));
Serial.println("[PAPA] hops: " + String(packet.hopCount));
Serial.println("[PAPA] duck: " + String(packet.duckType));
Serial.println("[PAPA] muid: " + std::string(muid.c_str()));
Serial.println("[PAPA] data: " + std::string(payload.c_str()));
Serial.println("[PAPA] hops: " + std::string(packet.hopCount));
Serial.println("[PAPA] duck: " + std::string(packet.duckType));

doc["DeviceId"] = sduid;
doc["topic"].set(toTopicString(packet.topic));
Expand All @@ -135,7 +135,7 @@ int toJSON(CdpPacket packet)
doc["hops"].set(packet.hopCount);
doc["duckType"].set(packet.duckType);

String jsonstat;
std::string jsonstat;
serializeJson(doc, jsonstat);
serializeJsonPretty(doc, Serial);
Serial.print("\n[PAPA] --------------------------------------------------------------------------------------\n\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool runSensor(void *) {
bool result;
const byte* buffer;

String message = String("Counter:") + String(counter);
std::string message = std::string("Counter:") + std::string(counter);
int length = message.length();
Serial.print("[MAMA] sensor data: ");
Serial.println(message);
Expand All @@ -115,7 +115,7 @@ bool sendData(const byte* buffer, int length) {
sentOk = true;
}
if (!sentOk) {
Serial.println("[MAMA] Failed to send data. error = " + String(err));
Serial.println("[MAMA] Failed to send data. error = " + std::string(err));
}
return sentOk;
}
Expand Down
24 changes: 12 additions & 12 deletions examples/Custom-Papa-Examples/AWS-PapaDuck/AWS-PapaDuck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ std::string toTopicString(byte topic) {
return topicString;
}

String convertToHex(byte* data, int size) {
String buf = "";
std::string convertToHex(byte* data, int size) {
std::string buf = "";
buf.reserve(size * 2); // 2 digit hex
const char* cs = "0123456789ABCDEF";
for (int i = 0; i < size; i++) {
Expand All @@ -140,14 +140,14 @@ int quackJson(CdpPacket packet) {
std::string path(packet.path.begin(), packet.path.end());

Serial.println("[PAPA] Packet Received:");
Serial.println("[PAPA] sduid: " + String(sduid.c_str()));
Serial.println("[PAPA] dduid: " + String(dduid.c_str()));
Serial.println("[PAPA] sduid: " + std::string(sduid.c_str()));
Serial.println("[PAPA] dduid: " + std::string(dduid.c_str()));

Serial.println("[PAPA] muid: " + String(muid.c_str()));
Serial.println("[PAPA] path: " + String(path.c_str()));
Serial.println("[PAPA] data: " + String(payload.c_str()));
Serial.println("[PAPA] hops: " + String(packet.hopCount));
Serial.println("[PAPA] duck: " + String(packet.duckType));
Serial.println("[PAPA] muid: " + std::string(muid.c_str()));
Serial.println("[PAPA] path: " + std::string(path.c_str()));
Serial.println("[PAPA] data: " + std::string(payload.c_str()));
Serial.println("[PAPA] hops: " + std::string(packet.hopCount));
Serial.println("[PAPA] duck: " + std::string(packet.duckType));

doc["DeviceID"] = sduid;
doc["MessageID"] = muid;
Expand All @@ -167,7 +167,7 @@ int quackJson(CdpPacket packet) {

std::string topic = "owl/device/" + std::string(THINGNAME) + "/evt/" + cdpTopic;

String jsonstat;
std::string jsonstat;
serializeJson(doc, jsonstat);

//Filter out private chat so it won't get sent to DMS
Expand Down Expand Up @@ -276,7 +276,7 @@ void loop() {
void gotMsg(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("gotMsg: invoked for topic: "); Serial.println(topic);

if (String(topic).indexOf(CMD_STATE_WIFI) > 0) {
if (std::string(topic).indexOf(CMD_STATE_WIFI) > 0) {
Serial.println("Start WiFi Command");
byte sCmd = 1;
std::vector<byte> sValue = {payload[0]};
Expand All @@ -294,7 +294,7 @@ void gotMsg(char* topic, byte* payload, unsigned int payloadLength) {
} else {
duck.sendCommand(sCmd, sValue);
}
} else if (String(topic).indexOf(CMD_STATE_HEALTH) > 0) {
} else if (std::string(topic).indexOf(CMD_STATE_HEALTH) > 0) {
byte sCmd = 0;
std::vector<byte> sValue = {payload[0]};
if(payloadLength >= 8) {
Expand Down
14 changes: 7 additions & 7 deletions examples/Custom-Papa-Examples/MQTT-PapaDuck/MQTT-PapaDuck.ino
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ const std::string WIFI_SSID="ENTER SSID"; // Replace with WiFi SSID
const std::string WIFI_PASS="ENTER PASSWORD"; // Replace with WiFi Password

std::string toTopicString(byte topic);
String arduinoStringFromHex(byte* data, int size);
std::string arduinoStringFromHex(byte* data, int size);
bool setup_mqtt(void);
void handleIncomingMqttMessages(void);
// std::string toHexString(std::vector<byte>& data);
std::vector<uint8_t> fromHexString(const std::string& hexString);

// std::string toHexString(std::vector<byte>& data) {
// String str = arduinoStringFromHex(data.data(), data.size());
// std::string str = arduinoStringFromHex(data.data(), data.size());
// return str.c_str();
// }

Expand Down Expand Up @@ -147,9 +147,9 @@ std::string toTopicString(byte topic)
return topicString;
}

String arduinoStringFromHex(byte* data, int size)
std::string arduinoStringFromHex(byte* data, int size)
{
String buf = "";
std::string buf = "";
buf.reserve(size * 2); // 2 digit hex
const char* cs = "0123456789ABCDEF";
for (int i = 0; i < size; i++) {
Expand All @@ -160,7 +160,7 @@ String arduinoStringFromHex(byte* data, int size)
return buf;
}

bool publishToMqttTopic(std::string source, std::string topic, String message) {
bool publishToMqttTopic(std::string source, std::string topic, std::string message) {
bool ok = false;
if (hub.isWifiConnected()) {
ok = setup_mqtt();
Expand Down Expand Up @@ -211,7 +211,7 @@ void handleIncomingMqttMessages(void) {
Serial.println(err.c_str());
continue;
}
String jsonstat;
std::string jsonstat;
serializeJson(doc, jsonstat);
serializeJsonPretty(doc, Serial);

Expand Down Expand Up @@ -254,7 +254,7 @@ void processMessageFromDucks(std::vector<byte> packetBuffer) {

doc["payload"]["Message"] = payload.c_str();

String jsonstat;
std::string jsonstat;
serializeJson(doc, jsonstat);
Serial.printf("%s\n",jsonstat.c_str());

Expand Down
2 changes: 1 addition & 1 deletion src/DuckUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ std::string toUpperCase(std::string str) {

// Note: This function is provided as a convenience for Arduino users who are using String in their code
// This function should not be used in CDP library code!
std::vector<byte> stringToByteVector(const String& str) {
std::vector<byte> stringToByteVector(const std::string& str) {
std::vector<byte> byteVec;
byteVec.reserve(str.length());

Expand Down
2 changes: 1 addition & 1 deletion test/test_DuckUtils/test_DuckUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void test_DuckUtils_toUpperCase(void) {
}

void test_DuckUtils_stringToByteVector(void) {
String str = "hello";
std::string str = "hello";
std::vector<byte> bytes = duckutils::stringToByteVector(str);
TEST_ASSERT_EQUAL(5, bytes.size());
TEST_ASSERT_EQUAL('h', bytes[0]);
Expand Down

0 comments on commit 2b2895d

Please sign in to comment.