Hi all,
I am making a device using the sam3x8e and the wiznet 5500 ethernet chip. I will communicate with the device using mqtt and the pubsubclient library.
I am wondering what will be the best implementation of a topic parser for receiving mqtt msg. All the incoming topics have the same structure, similiar to: foo/bar/id. The id is the only changing parameter and it is always a number. What I am wondering about is: Whats the best way of implementing a parser for these topics. The topics are arrays of characters / C-strings. I have two ideas for now:
- Use memcmp / strcmp to compare the full topic. Something like:
#define TOPIC1 "foo/bar/1"
#define TOPIC2 "foo/bar/2"
#define TOPIC3 "foo/bar/3"
if(strcmp(topic, TOPIC1) == 0){
// Parse payload for topic 1
}else if(strcmp(topic, TOPIC2) == 0){
// Parse payload for topic 2
}else if(strcmp(topic, TOPIC2) == 0){
// Parse payload for topic 3
}
// and so on..
- Since the topic is always foo/bar/id and id is numeric, it could also be possible to substring the part before the last / and check if the first part is correct. Then convert the id field to an integer and then use a switch-case statement to implement the parser.
I think solution 2 is easier to read in code compared to solution 1. Which solution would be the best when it comes to memory usage and performance? Does it actually matter which one I go for? Any other ideas are welcome as well.