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

added length error publish

parent ff6ff78b
Branches
No related tags found
2 merge requests!3Dev to master,!2Dev to Master
...@@ -150,11 +150,13 @@ int didacticPSNet::extractData(int startCounter, int maxLength, char* buffer, ch ...@@ -150,11 +150,13 @@ int didacticPSNet::extractData(int startCounter, int maxLength, char* buffer, ch
buffer[counter-startCounter] = _readBufferMessage[counter]; buffer[counter-startCounter] = _readBufferMessage[counter];
counter++; counter++;
if((counter-startCounter) > maxLength){ 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'; buffer[counter-startCounter] = '\0';
return counter-startCounter; //length of Topic return counter-startCounter; //length
} }
int didacticPSNet::checkData(){ int didacticPSNet::checkData(){
...@@ -203,7 +205,9 @@ didacticPSNetClient::didacticPSNetClient(){} ...@@ -203,7 +205,9 @@ didacticPSNetClient::didacticPSNetClient(){}
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 topicLength = strlen(topic);
int payloadLength = strlen(payload); int payloadLength = strlen(payload);
_sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[0] = MSG_PRELIMITER;
...@@ -212,11 +216,28 @@ bool didacticPSNetClient::publish(char* topic, char* payload){ ...@@ -212,11 +216,28 @@ bool didacticPSNetClient::publish(char* topic, char* payload){
_sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER;
_sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0'; _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){ 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];
} }
}else { }else { //cut topic if longer than max
_dataToSend = false; _dataToSend = false;
return false; return false;
} }
...@@ -224,26 +245,47 @@ bool didacticPSNetClient::publish(char* topic, char* payload){ ...@@ -224,26 +245,47 @@ bool didacticPSNetClient::publish(char* topic, char* payload){
for(int i = 0; i < payloadLength; i++){ for(int i = 0; i < payloadLength; i++){
_sendBufferMessage[2+topicLength+1+i] = payload[i]; _sendBufferMessage[2+topicLength+1+i] = payload[i];
} }
}else { }else { //cut message if longer than max
_dataToSend = false; _dataToSend = false;
return false; return false;
} }
*/
_dataToSend = true; _dataToSend = true;
return true; return error;
} }
bool didacticPSNetClient::publish(char* topic, int topicLength, char* payload , int payloadLength){ int didacticPSNetClient::publish(char* topic, int topicLength, char* payload , int payloadLength){
int error = DN_ERROR_NOERROR;
_sendBufferMessage[0] = MSG_PRELIMITER; _sendBufferMessage[0] = MSG_PRELIMITER;
_sendBufferMessage[1] = MSG_PUBLISH; _sendBufferMessage[1] = MSG_PUBLISH;
_sendBufferMessage[2+topicLength] = MSG_SEPARATOR; _sendBufferMessage[2+topicLength] = MSG_SEPARATOR;
_sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER; _sendBufferMessage[2+topicLength+1+payloadLength] = MSG_DELIMITER;
_sendBufferMessage[2+topicLength+1+payloadLength+1] = '\0'; _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){ 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];
} }
}else { }else { //cut topic if longer than max
_dataToSend = false; _dataToSend = false;
return false; return false;
} }
...@@ -251,12 +293,13 @@ bool didacticPSNetClient::publish(char* topic, int topicLength, char* payload , ...@@ -251,12 +293,13 @@ bool didacticPSNetClient::publish(char* topic, int topicLength, char* payload ,
for(int i = 0; i < payloadLength; i++){ for(int i = 0; i < payloadLength; i++){
_sendBufferMessage[2+topicLength+1+i] = payload[i]; _sendBufferMessage[2+topicLength+1+i] = payload[i];
} }
}else { }else { //cut message if longer than max
_dataToSend = false; _dataToSend = false;
return false; return false;
} }
*/
_dataToSend = true; _dataToSend = true;
return true; return error;
} }
bool didacticPSNetClient::subscribe(char* topic){ bool didacticPSNetClient::subscribe(char* topic){
...@@ -389,7 +432,7 @@ bool didacticPSNetClient::handleData(){ ...@@ -389,7 +432,7 @@ bool didacticPSNetClient::handleData(){
int didacticPSNetClient::getTopicNr(char* topic){ int didacticPSNetClient::getTopicNr(char* topic){
for (int i = 0; i < MAX_NR_TOPICS_CLIENT; i++) { 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; return i;
} }
} }
......
...@@ -15,20 +15,17 @@ ...@@ -15,20 +15,17 @@
#define MSG_DELIMITER '>' #define MSG_DELIMITER '>'
#define MSG_SEPARATOR '|' #define MSG_SEPARATOR '|'
//@ publish → on publish check topic, then send topic-update //@ publish → on publish check topic, then send topic-update
//? subscribe → subscribe starts update, topic filter @client //? subscribe → subscribe starts update, topic filter @client
//# update → update to specific topic Broker to client //# update → update to specific topic Broker to client
#define MSG_PUBLISH '@' #define MSG_PUBLISH '@'
#define MSG_SUBSCRIBE '?' #define MSG_SUBSCRIBE '?'
#define MSG_UPDATE '#' #define MSG_UPDATE '#'
#define MSG_TOPIC_MULTI '*'
//<@topic|payload> //<@topic|payload>
#define LEN_OVERHEAD 4 #define LEN_OVERHEAD 4
#define MSG_TOPIC_MULTI '*'
#define CSMA_CHECK_DELAY_US 400 #define CSMA_CHECK_DELAY_US 400
#define CSMA_MIN_DELAY_MS 10 #define CSMA_MIN_DELAY_MS 10
#define CSMA_MID_DELAY_MS 20 #define CSMA_MID_DELAY_MS 20
...@@ -39,7 +36,14 @@ ...@@ -39,7 +36,14 @@
#define MAX_LEN_TOPICS 10 #define MAX_LEN_TOPICS 10
#define MAX_LEN_PAYLOAD 20 #define MAX_LEN_PAYLOAD 20
#define DN_ERROR_NOERROR 0
#define DN_ERROR_TOPIC_LEN -1
#define DN_ERROR_PAYLOAD_LEN -2
//little helpers //little helpers
#define ASCII_CR 13
#define ASCII_NL 10
int edgeDetected(bool); int edgeDetected(bool);
bool valueChanged(int, int); bool valueChanged(int, int);
bool timeElapsed(long); bool timeElapsed(long);
...@@ -54,8 +58,8 @@ class didacticPSNet ...@@ -54,8 +58,8 @@ class didacticPSNet
char _bufferTopic[MAX_LEN_TOPICS +1] = {0}; char _bufferTopic[MAX_LEN_TOPICS +1] = {0};
char _bufferPayload[MAX_LEN_PAYLOAD +1] = {0}; char _bufferPayload[MAX_LEN_PAYLOAD +1] = {0};
char _readBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD +4+1]; char _readBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1];
char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD +4+1]; char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1];
bool _dataToSend = false; // int Data to send for queue? bool _dataToSend = false; // int Data to send for queue?
unsigned long _waitingTime = 0L; unsigned long _waitingTime = 0L;
...@@ -104,8 +108,8 @@ class didacticPSNetClient : public didacticPSNet ...@@ -104,8 +108,8 @@ class didacticPSNetClient : public didacticPSNet
didacticPSNetClient(); didacticPSNetClient();
~didacticPSNetClient(); ~didacticPSNetClient();
bool publish(char*, char*); int publish(char*, char*);
bool publish(char*, int, char*, int); int publish(char*, int, char*, int);
bool subscribe(char*); bool subscribe(char*);
bool subscribe(char*, int); bool subscribe(char*, int);
bool unsubscribe(char*); bool unsubscribe(char*);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment