diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index f54cf0811e9fd9ef2f73aaeff323b2c7c5d8e771..831bafa8004a7905f2eb0d33838c29ab1e808a38 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -150,11 +150,13 @@ int didacticPSNet::extractData(int startCounter, int maxLength, char* buffer, ch buffer[counter-startCounter] = _readBufferMessage[counter]; counter++; if((counter-startCounter) > maxLength){ - return -1; + //return -1; + Serial.print("\nextractData - Return on MAX length\n"); + break; //if > maxLenght -> leave while and return } } buffer[counter-startCounter] = '\0'; - return counter-startCounter; //length of Topic + return counter-startCounter; //length } int didacticPSNet::checkData(){ @@ -203,7 +205,9 @@ didacticPSNetClient::didacticPSNetClient(){} didacticPSNetClient::~didacticPSNetClient(){} -bool didacticPSNetClient::publish(char* topic, char* payload){ +int didacticPSNetClient::publish(char* topic, char* payload){ + int error = DN_ERROR_NOERROR; + int topicLength = strlen(topic); int payloadLength = strlen(payload); _sendBufferMessage[0] = MSG_PRELIMITER; @@ -212,11 +216,28 @@ bool didacticPSNetClient::publish(char* topic, char* payload){ _sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0'; + //TODO: check + if(topicLength > MAX_LEN_TOPICS){ + topicLength = MAX_LEN_TOPICS; + error += DN_ERROR_TOPIC_LEN; + } + if(payloadLength > MAX_LEN_PAYLOAD){ + payloadLength = MAX_LEN_PAYLOAD; + error += DN_ERROR_PAYLOAD_LEN; + } + + for(int i = 0; i < topicLength; i++){ + _sendBufferMessage[2+i] = topic[i]; + } + for(int i = 0; i < payloadLength; i++){ + _sendBufferMessage[2+topicLength+1+i] = payload[i]; + } +/* if(topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _sendBufferMessage[2+i] = topic[i]; } - }else { + }else { //cut topic if longer than max _dataToSend = false; return false; } @@ -224,26 +245,47 @@ bool didacticPSNetClient::publish(char* topic, char* payload){ for(int i = 0; i < payloadLength; i++){ _sendBufferMessage[2+topicLength+1+i] = payload[i]; } - }else { + }else { //cut message if longer than max _dataToSend = false; return false; } + */ + _dataToSend = true; - return true; + return error; } -bool didacticPSNetClient::publish(char* topic, int topicLength, char* payload , int payloadLength){ - _sendBufferMessage[0] = MSG_PRELIMITER; +int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , int payloadLength){ + int error = DN_ERROR_NOERROR; + + _sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[1] = MSG_PUBLISH; _sendBufferMessage[2+topicLength] = MSG_SEPARATOR; _sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0'; + //TODO: check + if(topicLength > MAX_LEN_TOPICS){ + topicLength = MAX_LEN_TOPICS; + error += DN_ERROR_TOPIC_LEN; + } + if(payloadLength > MAX_LEN_PAYLOAD){ + payloadLength = MAX_LEN_PAYLOAD; + error += DN_ERROR_PAYLOAD_LEN; + } + + for(int i = 0; i < topicLength; i++){ + _sendBufferMessage[2+i] = topic[i]; + } + for(int i = 0; i < payloadLength; i++){ + _sendBufferMessage[2+topicLength+1+i] = payload[i]; + } +/* if(topicLength <= MAX_LEN_TOPICS){ for(int i = 0; i < topicLength; i++){ _sendBufferMessage[2+i] = topic[i]; } - }else { + }else { //cut topic if longer than max _dataToSend = false; return false; } @@ -251,12 +293,13 @@ bool didacticPSNetClient::publish(char* topic, int topicLength, char* payload , for(int i = 0; i < payloadLength; i++){ _sendBufferMessage[2+topicLength+1+i] = payload[i]; } - }else { + }else { //cut message if longer than max _dataToSend = false; return false; } + */ _dataToSend = true; - return true; + return error; } bool didacticPSNetClient::subscribe(char* topic){ @@ -389,7 +432,7 @@ bool didacticPSNetClient::handleData(){ int didacticPSNetClient::getTopicNr(char* topic){ for (int i = 0; i < MAX_NR_TOPICS_CLIENT; i++) { - if (strcmp(_topic[i], topic) == 0) { + if (strcmp(_topic[i], topic) == 0 || _topic[i][0] == MSG_TOPIC_MULTI) { //TODO: check ... or equal MSG_TOPIC_MULTI return i; } } diff --git a/src/didacticNet.h b/src/didacticNet.h index 1132420a4d5117eee576cb730074128d71540a55..0206e8da9459db04c9807817a7b2d287863997ee 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -15,20 +15,17 @@ #define MSG_DELIMITER '>' #define MSG_SEPARATOR '|' - //@ publish → on publish check topic, then send topic-update //? subscribe → subscribe starts update, topic filter @client //# update → update to specific topic Broker to client #define MSG_PUBLISH '@' #define MSG_SUBSCRIBE '?' #define MSG_UPDATE '#' +#define MSG_TOPIC_MULTI '*' //<@topic|payload> #define LEN_OVERHEAD 4 -#define MSG_TOPIC_MULTI '*' - - #define CSMA_CHECK_DELAY_US 400 #define CSMA_MIN_DELAY_MS 10 #define CSMA_MID_DELAY_MS 20 @@ -39,7 +36,14 @@ #define MAX_LEN_TOPICS 10 #define MAX_LEN_PAYLOAD 20 +#define DN_ERROR_NOERROR 0 +#define DN_ERROR_TOPIC_LEN -1 +#define DN_ERROR_PAYLOAD_LEN -2 + //little helpers +#define ASCII_CR 13 +#define ASCII_NL 10 + int edgeDetected(bool); bool valueChanged(int, int); bool timeElapsed(long); @@ -52,10 +56,10 @@ class didacticPSNet PSNET_CALLBACK_SIGNATURE; - char _bufferTopic[MAX_LEN_TOPICS+1] = {0}; - char _bufferPayload[MAX_LEN_PAYLOAD+1] = {0}; - char _readBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD +4+1]; - char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD +4+1]; + char _bufferTopic[MAX_LEN_TOPICS +1] = {0}; + char _bufferPayload[MAX_LEN_PAYLOAD +1] = {0}; + char _readBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1]; + char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1]; bool _dataToSend = false; // int Data to send for queue? unsigned long _waitingTime = 0L; @@ -104,8 +108,8 @@ class didacticPSNetClient : public didacticPSNet didacticPSNetClient(); ~didacticPSNetClient(); - bool publish(char*, char*); - bool publish(char*, int, char*, int); + int publish(char*, char*); + int publish(char*, int, char*, int); bool subscribe(char*); bool subscribe(char*, int); bool unsubscribe(char*);