LiamBindle / MQTT-C

A portable MQTT C client for embedded systems and PCs alike.
https://liambindle.ca/MQTT-C
MIT License
775 stars 275 forks source link

We cannot pass the context of the program to mqtt publish_callback #180

Open Lecrapouille opened 1 year ago

Lecrapouille commented 1 year ago

Why not passing the context of the program in mqtt_init ? Because in void publish_callback(void** /*unused*/, struct mqtt_response_publish *published) you have to store data in a global variable since there is no way to access it.

Idea:


struct mqtt_client client;
struct context { int foo; int bar; } my_context;

client->context = &my_context;
...

static void publish_callback(void** /*unused*/, struct mqtt_response_publish *published)
{
   struct context* my_context = (struct  context*) published->context;
   my_context->foo = process(published->application_message, published->application_message_size);
}
arielmol commented 1 year ago

just found the same

Lecrapouille commented 1 year ago

@arielmol I have made a quick implementation in C++ if you wish https://github.com/Lecrapouille/MQTT