From 0b3ad2a5210d05eda8a9596906aaac0663ab6441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anian=20B=C3=BChler?= <anian.buehler@reutlingen-university.de> Date: Tue, 13 Jul 2021 13:46:17 +0200 Subject: [PATCH] added error return tu subscribe --- examples/sPSN_Chat/sPSN_Chat.ino | 5 ++-- keywords.txt | 5 ++++ src/didacticNet.cpp | 47 ++++++++++++++++++++++---------- src/didacticNet.h | 7 ++--- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/examples/sPSN_Chat/sPSN_Chat.ino b/examples/sPSN_Chat/sPSN_Chat.ino index 60fa1d3..151cfda 100644 --- a/examples/sPSN_Chat/sPSN_Chat.ino +++ b/examples/sPSN_Chat/sPSN_Chat.ino @@ -75,14 +75,14 @@ void loop() { int nrOfAscii = readSerialData(Serial, readData, 13); //Einlesen der Nutzereingabe am SerialMonitor (Rueckgabewert = Anzahl der gelesenen Zeichen) if (nrOfAscii > 0) { //Wenn Daten fertig eingelesen wurden - + if (readData[0] == '@') { //Wenn '@' vorne steht, dann neuer Chatpartner anlegen (neues Topic abonnieren) strcpy(readData, readData + 1); //verschiebe um ein Zeichen (entferne '@') psnClient.subscribe(readData); //Lege fest zu welchem Topic Daten empfangen werden sollen (den Namen des Chatpartners) Serial.print("Nachrichten von "); //Ausgabe welches Topic abonniert wurde Serial.print(readData); - Serial.println(" abonniert"); + Serial.println(" abonniert."); } else if (readData[0] == '#') { //Wenn '#' vorne steht, dann neuer eigener Name (neues Topic unter dem gesendet wird) @@ -98,7 +98,6 @@ void loop() { Serial.print("Ich:\t"); //Ausgabe was unter deinem Topic veröffentlicht wird Serial.println(readData); - } } diff --git a/keywords.txt b/keywords.txt index 9334b7b..1342edf 100644 --- a/keywords.txt +++ b/keywords.txt @@ -16,6 +16,7 @@ didacticPSNetClient KEYWORD1 edgeDetected KEYWORD2 valueChanged KEYWORD2 timeElapsed KEYWORD2 +readSerialData KEYWORD2 setCallback KEYWORD2 setStream KEYWORD2 @@ -42,3 +43,7 @@ MAX_NR_TOPICS_CLIENT LITERAL1 MAX_NR_TOPICS_BROKER LITERAL1 MAX_LEN_TOPICS LITERAL1 MAX_LEN_PAYLOAD LITERAL1 + +DN_ERROR_NOERROR LITERAL1 +DN_ERROR_TOPIC_LEN LITERAL1 +DN_ERROR_PAYLOAD_LEN LITERAL1 diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index 831bafa..ddc2d7b 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -302,8 +302,16 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i return error; } -bool didacticPSNetClient::subscribe(char* topic){ +int didacticPSNetClient::subscribe(char* topic){ + int error = DN_ERROR_NOERROR; + int topicLength = strlen(topic); + + if( topicLength > MAX_LEN_TOPICS){ + topicLength = MAX_LEN_TOPICS; + error = DN_ERROR_TOPIC_LEN; + } + _sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[1] = MSG_SUBSCRIBE; _sendBufferMessage[2+topicLength] = MSG_DELIMITER; @@ -315,7 +323,8 @@ bool didacticPSNetClient::subscribe(char* topic){ if(topicNumber < 0){ topicNumber = 0; } - if( topicLength <= MAX_LEN_TOPICS){ + + // if( topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _topic[topicNumber][i] = topic[i]; _sendBufferMessage[2+i]= topic[i]; @@ -323,28 +332,36 @@ bool didacticPSNetClient::subscribe(char* topic){ _topic[topicNumber][topicLength] = '\0'; _sendBufferMessage[2+topicLength+1]= '\0'; _dataToSend = true; - } + /* } else { _dataToSend = false; return false; - } + }*/ + } else{ - if( topicLength <= MAX_LEN_TOPICS){ + //if( topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _sendBufferMessage[2+i]= topic[i]; } _sendBufferMessage[2+topicLength+1]= '\0'; _dataToSend = true; - } + //} } while(_dataToSend){ handleNetwork(); } - return true; + return error; } -bool didacticPSNetClient::subscribe(char* topic, int topicLength){ +int didacticPSNetClient::subscribe(char* topic, int topicLength){ + int error = DN_ERROR_NOERROR; + + if( topicLength > MAX_LEN_TOPICS){ + topicLength = MAX_LEN_TOPICS; + error = DN_ERROR_TOPIC_LEN; + } + _sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[1] = MSG_SUBSCRIBE; _sendBufferMessage[2+topicLength] = MSG_DELIMITER; @@ -356,7 +373,8 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){ if(topicNumber < 0){ topicNumber = 0; } - if( topicLength <= MAX_LEN_TOPICS){ + + // if( topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _topic[topicNumber][i] = topic[i]; _sendBufferMessage[2+i]= topic[i]; @@ -364,25 +382,26 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){ _topic[topicNumber][topicLength] = '\0'; _sendBufferMessage[2+topicLength+1]= '\0'; _dataToSend = true; - } + /* } else { _dataToSend = false; return false; - } + }*/ + } else{ - if( topicLength <= MAX_LEN_TOPICS){ + //if( topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _sendBufferMessage[2+i]= topic[i]; } _sendBufferMessage[2+topicLength+1]= '\0'; _dataToSend = true; - } + //} } while(_dataToSend){ handleNetwork(); } - return true; + return error; } diff --git a/src/didacticNet.h b/src/didacticNet.h index 0206e8d..ae681e5 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -94,7 +94,6 @@ class didacticPSNet class didacticPSNetClient : public didacticPSNet { private: - char _topic[MAX_NR_TOPICS_CLIENT][MAX_LEN_TOPICS+1] = { { 0 } }; char _payload[MAX_NR_TOPICS_CLIENT][MAX_LEN_PAYLOAD+1] = { { 0 } }; @@ -110,18 +109,16 @@ class didacticPSNetClient : public didacticPSNet int publish(char*, char*); int publish(char*, int, char*, int); - bool subscribe(char*); - bool subscribe(char*, int); + int subscribe(char*); + int subscribe(char*, int); bool unsubscribe(char*); bool unsubscribe(char*, int); - }; class didacticPSNetBroker: public didacticPSNet { private: - char _topic[MAX_NR_TOPICS_BROKER][MAX_LEN_TOPICS+1] = { { 0 } }; char _data[MAX_NR_TOPICS_BROKER][MAX_LEN_PAYLOAD+1] = { { 0 } }; -- GitLab