From 47d9d0cc77ac635bf57b8c42c84758980d1c3492 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Mon, 4 Jul 2022 11:52:00 +0200
Subject: [PATCH] removed length from callback & updated examples

---
 examples/sPSN_Chat/sPSN_Chat.ino                   | 2 +-
 examples/sPSN_Client1/sPSN_Client1.ino             | 4 +---
 examples/sPSN_Client2/sPSN_Client2.ino             | 6 +-----
 examples/sPSN_ClientMinimal/sPSN_ClientMinimal.ino | 2 +-
 examples/sPSN_PingTest/sPSN_PingTest.ino           | 2 +-
 examples/sPSN_PongTester/sPSN_PongTester.ino       | 2 +-
 src/DidacticNet.cpp                                | 7 ++++++-
 src/DidacticNet.h                                  | 8 +++++++-
 8 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/examples/sPSN_Chat/sPSN_Chat.ino b/examples/sPSN_Chat/sPSN_Chat.ino
index ee8d7e5..3c21bd1 100644
--- a/examples/sPSN_Chat/sPSN_Chat.ino
+++ b/examples/sPSN_Chat/sPSN_Chat.ino
@@ -36,7 +36,7 @@ char topicSub[MAX_LEN_TOPICS] = {""};           //Array für neues Empfangs-Topi
 char topicPub[MAX_LEN_TOPICS] = {""};           //Array für eigenes Sende-Topic
 
 //Callback-Funktion wird bei neuen Daten automatisch aufgerufen
-void newData(char* mTopic, int mToLength, char* mData, int mDaLength) {
+void newData(char* mTopic, char* mData) {
   Serial.print(mTopic);
   Serial.print(":\t");
   Serial.println(mData);
diff --git a/examples/sPSN_Client1/sPSN_Client1.ino b/examples/sPSN_Client1/sPSN_Client1.ino
index 14afad6..0f4c43a 100644
--- a/examples/sPSN_Client1/sPSN_Client1.ino
+++ b/examples/sPSN_Client1/sPSN_Client1.ino
@@ -34,15 +34,13 @@
 char topicPublish[MAX_LEN_TOPICS]   = "potiVal";  //Topic unter dem (eigene) Daten veröffentlicht werden
 char topicSubscribe[MAX_LEN_TOPICS] = "btnState"; //Topic (von anderem TN) das abboniert werden soll
 
-char payload[MAX_LEN_PAYLOAD] = {0};
-
 SoftwareSerial sSerial(10, 11); //Erzeuge SoftwareSerial-Instanz mit Rx = Pin10 -> Empfänger | Tx = Pin11 -> Sender
 
 DidacticPSNetClient psnClient;  //Erzeuge PubSub-Client-Instanz
 
 
 //Callback-Funktion - wird beim Empfang neuer Daten aufgerufen
-void newData(char* mTopic, int mToLength, char* mData, int mDaLength) {
+void newData(char* mTopic, char* mData) {
   Serial.print("CB: ");
   Serial.print(mTopic);
   Serial.print(" | ");
diff --git a/examples/sPSN_Client2/sPSN_Client2.ino b/examples/sPSN_Client2/sPSN_Client2.ino
index 5ee164c..4b04bb5 100644
--- a/examples/sPSN_Client2/sPSN_Client2.ino
+++ b/examples/sPSN_Client2/sPSN_Client2.ino
@@ -30,10 +30,6 @@
 char topicPublish[MAX_LEN_TOPICS]   = "btnState";   //Topic unter dem (eigene) Daten veröffentlicht werden
 char topicSubscribe[MAX_LEN_TOPICS] = "potiVal";    //Topic (von anderem TN) das abboniert werden soll
 
-char payload[MAX_LEN_PAYLOAD] = {0};
-boolean newData  = false;
-boolean ledState = false;
-
 SoftwareSerial sSerial(10, 11); //Erzeuge SoftwareSerial-Instanz mit Rx = Pin10 -> Empfänger | Tx = Pin11 -> Sender
 
 DidacticPSNetClient psnClient;  //Erzeuge PubSub-Client-Instanz
@@ -41,7 +37,7 @@ UnblockingTimer uTimer;
 
 
 //Callback-Funktion - wird beim Empfang neuer Daten aufgerufen
-void newData(char* mTopic, int mToLength, char* mData, int mDaLength) {
+void newData(char* mTopic, char* mData) {
   Serial.print("CB: ");
   Serial.print(mTopic);
   Serial.print(" | ");
diff --git a/examples/sPSN_ClientMinimal/sPSN_ClientMinimal.ino b/examples/sPSN_ClientMinimal/sPSN_ClientMinimal.ino
index dabf7d2..3222fde 100644
--- a/examples/sPSN_ClientMinimal/sPSN_ClientMinimal.ino
+++ b/examples/sPSN_ClientMinimal/sPSN_ClientMinimal.ino
@@ -29,7 +29,7 @@ SoftwareSerial sSerial(10, 11); //Erzeuge SoftwareSerial-Instanz mit Rx = Pin10
 DidacticPSNetClient psnClient;  //Erzeuge PubSub-Client-Instanz
 
 //Callback-Funktion - wird beim Empfang neuer Daten aufgerufen
-void newData(char* mTopic, int mToLength, char* mPayload, int mPayloadLength) {
+void newData(char* mTopic, char* mPayload) {
   Serial.print("Nachricht von: ");
   Serial.print(mTopic);
   Serial.print(" - ");
diff --git a/examples/sPSN_PingTest/sPSN_PingTest.ino b/examples/sPSN_PingTest/sPSN_PingTest.ino
index f666e9c..14eebdd 100644
--- a/examples/sPSN_PingTest/sPSN_PingTest.ino
+++ b/examples/sPSN_PingTest/sPSN_PingTest.ino
@@ -50,7 +50,7 @@ char payloadSend[] = "Ping";
 
 bool ledState = true;
 
-void newData(char* mTopic, int mToLength, char* mData, int mDaLength) {
+void newData(char* mTopic, char* mData) {
   Serial.println(mData);
   digitalWrite(LED_BUILTIN, HIGH);
 }
diff --git a/examples/sPSN_PongTester/sPSN_PongTester.ino b/examples/sPSN_PongTester/sPSN_PongTester.ino
index 3f1b2c2..d96ba58 100644
--- a/examples/sPSN_PongTester/sPSN_PongTester.ino
+++ b/examples/sPSN_PongTester/sPSN_PongTester.ino
@@ -44,7 +44,7 @@ char payloadSend[] = "Pong";
 
 bool ledState = true;
 
-void newData(char* mTopic, int mToLength, char* mData, int mDaLength) {
+void newData(char* mTopic, char* mData) {
   Serial.println(mTopic);
   //reply pong
   strcpy(topicPub, mTopic);
diff --git a/src/DidacticNet.cpp b/src/DidacticNet.cpp
index e3a5791..380c12a 100644
--- a/src/DidacticNet.cpp
+++ b/src/DidacticNet.cpp
@@ -302,7 +302,12 @@ bool DidacticPSNetClient::handleData(){
     if( currentTopicNr >= 0){
       savePayload( _bufferPayload, currentTopicNr);
       //callback(_topic[currentTopicNr], topicLength, _payload[currentTopicNr], payloadLength);
-      callback(_bufferTopic, topicLength, _bufferPayload, payloadLength);
+      #ifdef CALLBACK_W_LENGTH	 
+        callback(_bufferTopic, topicLength, _bufferPayload, payloadLength);
+      #else
+        callback(_bufferTopic, _bufferPayload);
+      #endif
+      
     }
   }
   return true;
diff --git a/src/DidacticNet.h b/src/DidacticNet.h
index 31556a3..cad4bd1 100644
--- a/src/DidacticNet.h
+++ b/src/DidacticNet.h
@@ -9,7 +9,11 @@
 
 #include "Arduino.h"
 //callback(topic, topicLength, data, dataLength)
-#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, int, char*, int)
+#ifdef CALLBACK_W_LENGTH	 
+	#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, int, char*, int)
+#else
+	#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, char*)
+#endif
 
 #define MSG_PRELIMITER '<'
 #define MSG_DELIMITER  '>'
@@ -117,6 +121,7 @@ class DidacticPSNet
 		int _currentPayloadLength  = 0;
 
 		DidacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE);
+
 		void setStream(Stream& _port);
 
 		int checkData();
@@ -136,6 +141,7 @@ class DidacticPSNet
 
 		void begin(Stream& _port);
 		void begin(Stream& _port, PSNET_CALLBACK_SIGNATURE);
+
 		bool handleNetwork();
 		bool isDataToSend();
 
-- 
GitLab