diff --git a/examples/sPSN_Chat/sPSN_Chat.ino b/examples/sPSN_Chat/sPSN_Chat.ino index 60fa1d3e9b1d31b655766a9429169d26661fd97b..151cfda4b2b349d60659d7044d3f7d90d537071d 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 9334b7be09db19a4ae2d1fa1eaa6257eb76b7181..1342edfdace97ea73d70a8bd63fbe83a099d6c43 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 831bafa8004a7905f2eb0d33838c29ab1e808a38..ddc2d7b588065a933e035bfab0c390abfd6bf1c6 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 0206e8da9459db04c9807817a7b2d287863997ee..ae681e5d848dffb445a4d8ed21a925097ee74b39 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 } };