mqtt/mqtt_template.c
changeset 4103 63c002e87c57
parent 4099 97e93962be0c
equal deleted inserted replaced
4102:32e0ffdf2f44 4103:63c002e87c57
   258         pthread_mutex_unlock(&MQTT_thread_wakeup_mutex);
   258         pthread_mutex_unlock(&MQTT_thread_wakeup_mutex);
   259     }}
   259     }}
   260 }}
   260 }}
   261 
   261 
   262 
   262 
   263 typedef int(*callback_fptr_t)(char* topic, char* data, uint32_t datalen);
   263 typedef int(*cb_onmsg_fptr_t)(char* topic, char* data, uint32_t datalen);
   264 static callback_fptr_t __mqtt_python_callback_fptr_{name} = NULL;
   264 static cb_onmsg_fptr_t __mqtt_python_cb_onmsg_fptr_{name} = NULL;
       
   265 
       
   266 typedef int(*cb_resub_fptr_t)(void);
       
   267 static cb_resub_fptr_t __mqtt_python_cb_resub_fptr_{name} = NULL;
   265 
   268 
   266 static int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
   269 static int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
   267 {{
   270 {{
   268     int low = 0;
   271     int low = 0;
   269     int size = sizeof(topics) / sizeof(topics[0]);
   272     int size = sizeof(topics) / sizeof(topics[0]);
   307         // If key is smaller, ignore right half
   310         // If key is smaller, ignore right half
   308         else
   311         else
   309             high = mid - 1;
   312             high = mid - 1;
   310     }}
   313     }}
   311     // If we reach here, then the element was not present
   314     // If we reach here, then the element was not present
   312     if(__mqtt_python_callback_fptr_{name} && 
   315     if(__mqtt_python_cb_onmsg_fptr_{name} && 
   313        (*__mqtt_python_callback_fptr_{name})(topicName,
   316        (*__mqtt_python_cb_onmsg_fptr_{name})(topicName,
   314                                      (char*)message->payload,
   317                                      (char*)message->payload,
   315                                      message->payloadlen) == 0){{
   318                                      message->payloadlen) == 0){{
   316         // Topic was handled in python
   319         // Topic was handled in python
   317         goto exit;
   320         goto exit;
   318     }} else {{
   321     }} else {{
   403         _PUBLISH(#Topic, QoS, sizeof(C_type), &MQTT_##c_loc_name##_buf, Retained)
   406         _PUBLISH(#Topic, QoS, sizeof(C_type), &MQTT_##c_loc_name##_buf, Retained)
   404 
   407 
   405 #define PUBLISH_JSON(Topic, QoS, C_type, c_loc_name, Retained)                                    \
   408 #define PUBLISH_JSON(Topic, QoS, C_type, c_loc_name, Retained)                                    \
   406         int res = json_gen_##c_loc_name(&MQTT_##c_loc_name##_buf);                                \
   409         int res = json_gen_##c_loc_name(&MQTT_##c_loc_name##_buf);                                \
   407         if(res == 0) {{                                                                           \
   410         if(res == 0) {{                                                                           \
   408             _PUBLISH(Topic, QoS, json_out_len, json_out_buf, Retained)                            \
   411             _PUBLISH(#Topic, QoS, json_out_len, json_out_buf, Retained)                            \
   409         }}
   412         }}
   410 
   413 
   411 #define INIT_PUBLICATION(encoding, Topic, QoS, C_type, c_loc_name, Retained)                      \
   414 #define INIT_PUBLICATION(encoding, Topic, QoS, C_type, c_loc_name, Retained)                      \
   412     {{                                                                                            \
   415     {{                                                                                            \
   413         int rc;                                                                                   \
   416         int rc;                                                                                   \
   455     }}else{{
   458     }}else{{
   456         MQTT_is_disconnected = 0;
   459         MQTT_is_disconnected = 0;
   457     }}
   460     }}
   458 
   461 
   459 {init_pubsub}
   462 {init_pubsub}
       
   463 
       
   464     if(__mqtt_python_cb_resub_fptr_{name}){{ 
       
   465         (*__mqtt_python_cb_resub_fptr_{name})();
       
   466     }}
   460 
   467 
   461     return MQTTCLIENT_SUCCESS;
   468     return MQTTCLIENT_SUCCESS;
   462 }}
   469 }}
   463 
   470 
   464 static void *__MQTT_thread_proc(void *_unused) {{
   471 static void *__MQTT_thread_proc(void *_unused) {{
   628         return rc;
   635         return rc;
   629     }}
   636     }}
   630     return 0;
   637     return 0;
   631 }}
   638 }}
   632 
   639 
   633 int __mqtt_python_callback_setter_{name}(callback_fptr_t cb)
   640 int __mqtt_python_callback_setter_{name}(cb_onmsg_fptr_t cb_onmsg, cb_resub_fptr_t cb_resub)
   634 {{
   641 {{
   635     __mqtt_python_callback_fptr_{name} = cb;
   642     __mqtt_python_cb_onmsg_fptr_{name} = cb_onmsg;
       
   643     __mqtt_python_cb_resub_fptr_{name} = cb_resub;
   636     return 0;
   644     return 0;
   637 }}
   645 }}