/** * \brief Callback to get the MQTT status update. * * \param[in] conn_id instance id of connection which is being used. * \param[in] type type of MQTT notification. Possible types are: * - [MQTT_CALLBACK_SOCK_CONNECTED](@ref MQTT_CALLBACK_SOCK_CONNECTED) * - [MQTT_CALLBACK_CONNECTED](@ref MQTT_CALLBACK_CONNECTED) * - [MQTT_CALLBACK_PUBLISHED](@ref MQTT_CALLBACK_PUBLISHED) * - [MQTT_CALLBACK_SUBSCRIBED](@ref MQTT_CALLBACK_SUBSCRIBED) * - [MQTT_CALLBACK_UNSUBSCRIBED](@ref MQTT_CALLBACK_UNSUBSCRIBED) * - [MQTT_CALLBACK_DISCONNECTED](@ref MQTT_CALLBACK_DISCONNECTED) * - [MQTT_CALLBACK_RECV_PUBLISH](@ref MQTT_CALLBACK_RECV_PUBLISH) * \param[in] data A structure contains notification informations. @ref mqtt_data */ static void mqtt_callback(struct mqtt_module *module_inst, int type, union mqtt_data *data) { switch (type) { case MQTT_CALLBACK_SOCK_CONNECTED: { /* * If connecting to broker server is complete successfully, Start sending CONNECT message of MQTT. * Or else retry to connect to broker server. */ if (data->sock_connected.result >= 0) { mqtt_connect_broker(module_inst, 1, NULL, NULL, mqtt_user, NULL, NULL, 0, 0, 0); } else { printf("Connect fail to server(%s)! retry it automatically.\r\n", main_mqtt_broker); mqtt_connect(module_inst, main_mqtt_broker); /* Retry that. */ } } break; case MQTT_CALLBACK_CONNECTED: if (data->connected.result == MQTT_CONN_RESULT_ACCEPT) { /* Subscribe chat topic. */ mqtt_subscribe(module_inst, MAIN_CHAT_TOPIC "#", 0); /* Enable USART receiving callback. */ usart_enable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); printf("Preparation of the chat has been completed.\r\n"); } else { /* Cannot connect for some reason. */ printf("MQTT broker decline your access! error code %d\r\n", data->connected.result); } break; case MQTT_CALLBACK_RECV_PUBLISH: /* You received publish message which you had subscribed. */ if (data->recv_publish.topic != NULL && data->recv_publish.msg != NULL) { if (!strncmp(data->recv_publish.topic, MAIN_CHAT_TOPIC, strlen(MAIN_CHAT_TOPIC))) { /* Print user name and message */ for (int i = strlen(MAIN_CHAT_TOPIC); i < data->recv_publish.topic_size; i++) { printf("%c", data->recv_publish.topic[i]); } printf(" >> "); for (int i = 0; i < data->recv_publish.msg_size; i++) { printf("%c", data->recv_publish.msg[i]); } printf("\r\n"); } } break; case MQTT_CALLBACK_DISCONNECTED: /* Stop timer and USART callback. */ printf("MQTT disconnected\r\n"); usart_disable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); break; } }
static void mqtt_callback(struct mqtt_module *module_inst, int type, union mqtt_data *data) { printf("MQTT CALLBACK ENTER"DEBUG_EOL); switch (type) { case MQTT_CALLBACK_SOCK_CONNECTED: { printf("MQTT SOCK CONNECTED"DEBUG_EOL); /* * If connecting to broker server is complete successfully, Start sending CONNECT message of MQTT. * Or else retry to connect to broker server. */ if (data->sock_connected.result >= 0) { printf("mqtt connect broker"DEBUG_EOL); printf("socket %i", data->sock_connected.result); int response = mqtt_connect_broker(module_inst, 1, NULL, NULL, gau0TheThingsIOHttpSubsURL, NULL, NULL, 0, 0, 0); // int response = mqtt_connect_broker(module_inst, 1, "1", "test", NULL, NULL, NULL, 0, 0, 0); printf("response %s %i"DEBUG_EOL,gau0TheThingsIOHttpSubsURL, response); } else { printf("Connect fail to server(%s)! retry it automatically.\r\n", main_mqtt_broker); mqtt_connect(module_inst, main_mqtt_broker); /* Retry that. */ } } break; case MQTT_CALLBACK_CONNECTED: if (data->connected.result == MQTT_CONN_RESULT_ACCEPT) { /* Subscribe chat topic. */ /* the subscribe topic in the subscribe endpoint */ printf("subscribe topic %s"DEBUG_EOL, gau0TheThingsIOHttpSubsURL); mqtt_subscribe(module_inst, gau0TheThingsIOHttpSubsURL, 0); /* Enable USART receiving callback. */ // TODO we have mqtt connection //usart_enable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); printf("Preparation of the chat has been completed.\r\n"); } else { /* Cannot connect for some reason. */ printf("MQTT broker decline your access! error code %d\r\n", data->connected.result); } break; case MQTT_CALLBACK_RECV_PUBLISH: printf("--- start receiving MQTT subscribe data ---"DEBUG_EOL); /* You received publish message which you had subscribed. */ if (data->recv_publish.topic != NULL && data->recv_publish.msg != NULL) { char message[data->recv_publish.msg_size]; for (int i = 0; i < data->recv_publish.msg_size; i++) { // printf("%c", data->recv_publish.msg[i]); message[i] = data->recv_publish.msg[i]; } //message[data->recv_publish.msg_size +] = '\0'; if (subscribe_cb) { subscribe_cb(&message); } printf(DEBUG_EOL); printf("--- end receiving MQTT subscribe data ---"DEBUG_EOL); /* printf("%c", data->recv_publish.topic[i]); printf("\r\n"); if (!strncmp(data->recv_publish.topic, MAIN_CHAT_TOPIC, strlen(MAIN_CHAT_TOPIC))) { ///* Print user name and message for (int i = strlen(MAIN_CHAT_TOPIC); i < data->recv_publish.topic_size; i++) { printf("%c", data->recv_publish.topic[i]); } printf(" >> "); for (int i = 0; i < data->recv_publish.msg_size; i++) { printf("%c", data->recv_publish.msg[i]); } } */ } break; case MQTT_CALLBACK_DISCONNECTED: /* Stop timer and USART callback. */ //DBG_LOG("\r\nMQTT disconnected :"); //DBG_LOG_DEV("length of the data is %d",len); if (gboolAlwaysMQTTConnected) { mqtt_connect(module_inst, main_mqtt_broker); } // TODO we don't have mqtt connection //usart_disable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); break; } }