Skip to content
Snippets Groups Projects
Commit 0b3ad2a5 authored by Anian Bühler's avatar Anian Bühler
Browse files

added error return tu subscribe

parent e5db9571
No related branches found
No related tags found
2 merge requests!3Dev to master,!2Dev to Master
...@@ -75,14 +75,14 @@ void loop() { ...@@ -75,14 +75,14 @@ void loop() {
int nrOfAscii = readSerialData(Serial, readData, 13); //Einlesen der Nutzereingabe am SerialMonitor (Rueckgabewert = Anzahl der gelesenen Zeichen) 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 (nrOfAscii > 0) { //Wenn Daten fertig eingelesen wurden
if (readData[0] == '@') { //Wenn '@' vorne steht, dann neuer Chatpartner anlegen (neues Topic abonnieren) if (readData[0] == '@') { //Wenn '@' vorne steht, dann neuer Chatpartner anlegen (neues Topic abonnieren)
strcpy(readData, readData + 1); //verschiebe um ein Zeichen (entferne '@') 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) 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("Nachrichten von "); //Ausgabe welches Topic abonniert wurde
Serial.print(readData); 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) else if (readData[0] == '#') { //Wenn '#' vorne steht, dann neuer eigener Name (neues Topic unter dem gesendet wird)
...@@ -98,7 +98,6 @@ void loop() { ...@@ -98,7 +98,6 @@ void loop() {
Serial.print("Ich:\t"); //Ausgabe was unter deinem Topic veröffentlicht wird Serial.print("Ich:\t"); //Ausgabe was unter deinem Topic veröffentlicht wird
Serial.println(readData); Serial.println(readData);
} }
} }
......
...@@ -16,6 +16,7 @@ didacticPSNetClient KEYWORD1 ...@@ -16,6 +16,7 @@ didacticPSNetClient KEYWORD1
edgeDetected KEYWORD2 edgeDetected KEYWORD2
valueChanged KEYWORD2 valueChanged KEYWORD2
timeElapsed KEYWORD2 timeElapsed KEYWORD2
readSerialData KEYWORD2
setCallback KEYWORD2 setCallback KEYWORD2
setStream KEYWORD2 setStream KEYWORD2
...@@ -42,3 +43,7 @@ MAX_NR_TOPICS_CLIENT LITERAL1 ...@@ -42,3 +43,7 @@ MAX_NR_TOPICS_CLIENT LITERAL1
MAX_NR_TOPICS_BROKER LITERAL1 MAX_NR_TOPICS_BROKER LITERAL1
MAX_LEN_TOPICS LITERAL1 MAX_LEN_TOPICS LITERAL1
MAX_LEN_PAYLOAD LITERAL1 MAX_LEN_PAYLOAD LITERAL1
DN_ERROR_NOERROR LITERAL1
DN_ERROR_TOPIC_LEN LITERAL1
DN_ERROR_PAYLOAD_LEN LITERAL1
...@@ -302,8 +302,16 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i ...@@ -302,8 +302,16 @@ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , i
return error; return error;
} }
bool didacticPSNetClient::subscribe(char* topic){ int didacticPSNetClient::subscribe(char* topic){
int error = DN_ERROR_NOERROR;
int topicLength = strlen(topic); int topicLength = strlen(topic);
if( topicLength > MAX_LEN_TOPICS){
topicLength = MAX_LEN_TOPICS;
error = DN_ERROR_TOPIC_LEN;
}
_sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[0] = MSG_PRELIMITER;
_sendBufferMessage[1] = MSG_SUBSCRIBE; _sendBufferMessage[1] = MSG_SUBSCRIBE;
_sendBufferMessage[2+topicLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength] = MSG_DELIMITER;
...@@ -315,7 +323,8 @@ bool didacticPSNetClient::subscribe(char* topic){ ...@@ -315,7 +323,8 @@ bool didacticPSNetClient::subscribe(char* topic){
if(topicNumber < 0){ if(topicNumber < 0){
topicNumber = 0; topicNumber = 0;
} }
if( topicLength <= MAX_LEN_TOPICS){
// if( topicLength <= MAX_LEN_TOPICS){
for(int i = 0; i < topicLength; i++){ for(int i = 0; i < topicLength; i++){
_topic[topicNumber][i] = topic[i]; _topic[topicNumber][i] = topic[i];
_sendBufferMessage[2+i]= topic[i]; _sendBufferMessage[2+i]= topic[i];
...@@ -323,28 +332,36 @@ bool didacticPSNetClient::subscribe(char* topic){ ...@@ -323,28 +332,36 @@ bool didacticPSNetClient::subscribe(char* topic){
_topic[topicNumber][topicLength] = '\0'; _topic[topicNumber][topicLength] = '\0';
_sendBufferMessage[2+topicLength+1]= '\0'; _sendBufferMessage[2+topicLength+1]= '\0';
_dataToSend = true; _dataToSend = true;
} /* }
else { else {
_dataToSend = false; _dataToSend = false;
return false; return false;
} }*/
} }
else{ else{
if( topicLength <= MAX_LEN_TOPICS){ //if( topicLength <= MAX_LEN_TOPICS){
for(int i = 0; i < topicLength; i++){ for(int i = 0; i < topicLength; i++){
_sendBufferMessage[2+i]= topic[i]; _sendBufferMessage[2+i]= topic[i];
} }
_sendBufferMessage[2+topicLength+1]= '\0'; _sendBufferMessage[2+topicLength+1]= '\0';
_dataToSend = true; _dataToSend = true;
} //}
} }
while(_dataToSend){ while(_dataToSend){
handleNetwork(); 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[0] = MSG_PRELIMITER;
_sendBufferMessage[1] = MSG_SUBSCRIBE; _sendBufferMessage[1] = MSG_SUBSCRIBE;
_sendBufferMessage[2+topicLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength] = MSG_DELIMITER;
...@@ -356,7 +373,8 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){ ...@@ -356,7 +373,8 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){
if(topicNumber < 0){ if(topicNumber < 0){
topicNumber = 0; topicNumber = 0;
} }
if( topicLength <= MAX_LEN_TOPICS){
// if( topicLength <= MAX_LEN_TOPICS){
for(int i = 0; i < topicLength; i++){ for(int i = 0; i < topicLength; i++){
_topic[topicNumber][i] = topic[i]; _topic[topicNumber][i] = topic[i];
_sendBufferMessage[2+i]= topic[i]; _sendBufferMessage[2+i]= topic[i];
...@@ -364,25 +382,26 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){ ...@@ -364,25 +382,26 @@ bool didacticPSNetClient::subscribe(char* topic, int topicLength){
_topic[topicNumber][topicLength] = '\0'; _topic[topicNumber][topicLength] = '\0';
_sendBufferMessage[2+topicLength+1]= '\0'; _sendBufferMessage[2+topicLength+1]= '\0';
_dataToSend = true; _dataToSend = true;
} /* }
else { else {
_dataToSend = false; _dataToSend = false;
return false; return false;
} }*/
} }
else{ else{
if( topicLength <= MAX_LEN_TOPICS){ //if( topicLength <= MAX_LEN_TOPICS){
for(int i = 0; i < topicLength; i++){ for(int i = 0; i < topicLength; i++){
_sendBufferMessage[2+i]= topic[i]; _sendBufferMessage[2+i]= topic[i];
} }
_sendBufferMessage[2+topicLength+1]= '\0'; _sendBufferMessage[2+topicLength+1]= '\0';
_dataToSend = true; _dataToSend = true;
} //}
} }
while(_dataToSend){ while(_dataToSend){
handleNetwork(); handleNetwork();
} }
return true; return error;
} }
......
...@@ -94,7 +94,6 @@ class didacticPSNet ...@@ -94,7 +94,6 @@ class didacticPSNet
class didacticPSNetClient : public didacticPSNet class didacticPSNetClient : public didacticPSNet
{ {
private: private:
char _topic[MAX_NR_TOPICS_CLIENT][MAX_LEN_TOPICS+1] = { { 0 } }; char _topic[MAX_NR_TOPICS_CLIENT][MAX_LEN_TOPICS+1] = { { 0 } };
char _payload[MAX_NR_TOPICS_CLIENT][MAX_LEN_PAYLOAD+1] = { { 0 } }; char _payload[MAX_NR_TOPICS_CLIENT][MAX_LEN_PAYLOAD+1] = { { 0 } };
...@@ -110,18 +109,16 @@ class didacticPSNetClient : public didacticPSNet ...@@ -110,18 +109,16 @@ class didacticPSNetClient : public didacticPSNet
int publish(char*, char*); int publish(char*, char*);
int publish(char*, int, char*, int); int publish(char*, int, char*, int);
bool subscribe(char*); int subscribe(char*);
bool subscribe(char*, int); int subscribe(char*, int);
bool unsubscribe(char*); bool unsubscribe(char*);
bool unsubscribe(char*, int); bool unsubscribe(char*, int);
}; };
class didacticPSNetBroker: public didacticPSNet class didacticPSNetBroker: public didacticPSNet
{ {
private: private:
char _topic[MAX_NR_TOPICS_BROKER][MAX_LEN_TOPICS+1] = { { 0 } }; char _topic[MAX_NR_TOPICS_BROKER][MAX_LEN_TOPICS+1] = { { 0 } };
char _data[MAX_NR_TOPICS_BROKER][MAX_LEN_PAYLOAD+1] = { { 0 } }; char _data[MAX_NR_TOPICS_BROKER][MAX_LEN_PAYLOAD+1] = { { 0 } };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment