Pubsubclient: Added MQTT retain boolean to message callback function

Created on 22 Jul 2019  路  3Comments  路  Source: knolleary/pubsubclient

I need to have the MQTT retain flag from the control header exposed in the callback function. I have modified my local version to do this & it only required 4 lines in PubSubClient to be changed. I've tested it & it works fine.

Is this change of interest to anyone else? If so, I can make post the code changes here so that the library can be updated. Is this something that could/should be added to the core library?

I am not quite sure how to change the callback function calling parameters (uses a 4th new parameter) so that it is backwards compatible (with calls using just the 3 original parameters). I personally don't need the backward compatibility - although I realise the core code base would do. I am confident it can be done - just a bit beyond my C++ skills at the moment.

Comments? Suggestions?

Most helpful comment

Hello. Anyway you can do the pull request and if somebody will need this - he will use it

All 3 comments

Hello. Anyway you can do the pull request and if somebody will need this - he will use it

@SGF-Lon Would you mind sharing your solution?

@iuliux.........no problem.

There are 2 changes in PubSubClient.h:

...around line 83...

+// changed callback to expose control header - SGF - 22/7/2019
-#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
+#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int, bool)>

...around line 85...

+// changed callback to expose control header - SGF - 22/7/2019
-#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
+#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int, bool)

And there are 2 changes in PubSubClient.cpp:

...around line 345...

+//                         callback changed to expose control header - SGF - 22/7/2019
-                           callback(topic,payload,len-llen-3-tl-2);
+                           callback(topic,payload,len-llen-3-tl-2, buffer[0]&0x01);

...around line 357...

+//                          callback changed to expose control header - SGF - 22/7/2019
-                            callback(topic,payload,len-llen-3-tl);
+                            callback(topic,payload,len-llen-3-tl,buffer[0]&0x01);

I just created a local version of them in the 'lib' folder (I use ATOM/PlatformIO) & edited the changes into my local version. Not future-proof against updates to the github version - but good enough for me for now.

SGF-Lon

Was this page helpful?
0 / 5 - 0 ratings