bool MqttClient::unsubscribe(String topic) { uint16_t msgId = 0; debugf("unsubscribing from '%s'", topic.c_str()); int res = mqtt_unsubscribe(&broker, topic.c_str(), &msgId); return res > 0; }
void itc_mqtt_subscribe_unsubscribe_p(void) { int res; g_mqtt_client_handle = mqtt_init_client(&g_mqtt_client_config); TC_ASSERT_NEQ("mqtt_init_client", g_mqtt_client_handle, NULL); res = mqtt_connect(g_mqtt_client_handle, CONFIG_ITC_MQTT_BROKER_ADDR, CONFIG_ITC_MQTT_BROKER_PORT, 0); TC_ASSERT_EQ_CLEANUP("mqtt_connect", res, 0, mqtt_deinit_client(g_mqtt_client_handle)); ITC_MQTT_WAIT_SIGNAL; res = mqtt_subscribe(g_mqtt_client_handle, ITC_MQTT_TOPIC, 0); TC_ASSERT_EQ_CLEANUP("mqtt_subscribe", res, 0, mqtt_disconnect(g_mqtt_client_handle); mqtt_deinit_client(g_mqtt_client_handle)); res = mqtt_unsubscribe(g_mqtt_client_handle, ITC_MQTT_TOPIC); TC_ASSERT_EQ_CLEANUP("mqtt_unsubscribe", res, 0, mqtt_disconnect(g_mqtt_client_handle); mqtt_deinit_client(g_mqtt_client_handle)); res = mqtt_disconnect(g_mqtt_client_handle); TC_ASSERT_EQ("mqtt_disconnect", res, 0); ITC_MQTT_WAIT_SIGNAL; res = mqtt_deinit_client(g_mqtt_client_handle); TC_ASSERT_EQ("mqtt_deinit_client", res, 0); TC_SUCCESS_RESULT(); }
bool stream_1hz(uint32_t streamid, stream_t *stream, void *userdata) { if (0 == stream->timeoutS--) { LOG_DEBUG("streamid=%u timeout", streamid); if (sublist_remove(stream->topicstr)) { LOG_INFO("unsubscribing from %s", stream->topicstr); mqtt_unsubscribe(&g_srvctx.mqttctx, stream->topicstr, NULL); } return true; } return false; }
/* * Subscribe the topic: IOT_MQTT_Subscribe(pclient, TOPIC_DATA, IOTX_MQTT_QOS1, _demo_message_arrive, NULL); * Publish the topic: IOT_MQTT_Publish(pclient, TOPIC_DATA, &topic_msg); */ static void mqtt_work(void *parms) { int rc = -1; if (is_subscribed == 0) { /* Subscribe the specific topic */ rc = mqtt_subscribe(TOPIC_GET, mqtt_sub_callback, NULL); if (rc < 0) { // IOT_MQTT_Destroy(&pclient); LOG("IOT_MQTT_Subscribe() failed, rc = %d", rc); } is_subscribed = 1; aos_schedule_call(ota_init, NULL); } #ifndef MQTT_PRESS_TEST else { /* Generate topic message */ int msg_len = snprintf(msg_pub, sizeof(msg_pub), "{\"attr_name\":\"temperature\", \"attr_value\":\"%d\"}", cnt); if (msg_len < 0) { LOG("Error occur! Exit program"); } rc = mqtt_publish(TOPIC_UPDATE, IOTX_MQTT_QOS1, msg_pub, msg_len); if (rc < 0) { LOG("error occur when publish"); } LOG("packet-id=%u, publish topic msg=%s", (uint32_t)rc, msg_pub); } cnt++; if (cnt < 200) { aos_post_delayed_action(3000, mqtt_work, NULL); } else { aos_cancel_delayed_action(3000, mqtt_work, NULL); mqtt_unsubscribe(TOPIC_GET); aos_msleep(200); mqtt_deinit_instance(); is_subscribed = 0; cnt = 0; } #endif }
static int check_option_on_client_running(void) { int result = CHECK_OPTION_RESULT_CHECKED_ERROR; if (!g_mqtt_client_handle) { if (g_stop || g_sub_topic || g_unsub_topic) { printf("Error: MQTT client is not running.\n"); goto done; } result = CHECK_OPTION_RESULT_NOTHING_TODO; goto done; } if (g_mqtt_client_handle && !(g_stop || g_sub_topic || g_unsub_topic)) { printf("Error: MQTT client is running. You have to stop the mqtt subscriber with --stop\n"); printf(" in order to start new mqtt subscriber.\n"); goto done; } if (g_stop) { int disconnect_try_count = 20; MQTT_SUB_DEBUG_PRINT(g_mqtt_client_handle, "disconnect from a MQTT broker before stopping MQTT client.\n"); while ((mqtt_disconnect(g_mqtt_client_handle) != 0) && disconnect_try_count) { disconnect_try_count--; usleep(500 * 1000); } if (disconnect_try_count == 0) { fprintf(stderr, "Error: mqtt_disconnect() failed.\n"); goto done; } MQTT_SUB_DEBUG_PRINT(g_mqtt_client_handle, "deinitialize MQTT client context.\n"); if (mqtt_deinit_client(g_mqtt_client_handle) != 0) { fprintf(stderr, "Error: mqtt_deinit_client() failed.\n"); goto done; } g_mqtt_client_handle = NULL; clean_client_config(); } else { if (g_sub_topic) { MQTT_SUB_DEBUG_PRINT(g_mqtt_client_handle, "subscribe the specified topic.\n"); if (mqtt_subscribe(g_mqtt_client_handle, g_sub_topic, g_qos) != 0) { fprintf(stderr, "Error: mqtt_subscribe() failed.\n"); goto done; } } if (g_unsub_topic) { MQTT_SUB_DEBUG_PRINT(g_mqtt_client_handle, "unsubscribe the specified topic.\n"); if (mqtt_unsubscribe(g_mqtt_client_handle, g_unsub_topic) != 0) { fprintf(stderr, "Error: mqtt_unsubscribe() failed.\n"); goto done; } } } /* result is success */ result = CHECK_OPTION_RESULT_CHECKED_OK; done: return result; }
void app_parse_mqttmsg(uint8_t *packet_buffer) { uint8_t msg_type = 0; uint16_t msg_id_rcv = 0; char topic_name[56]={0}; char msg[128]={0}; msg_type = MQTTParseMessageType(packet_buffer); //printf("-----> parse:0x%02X\n", msg_type); switch(msg_type) { case MQTT_MSG_CONNACK: if(packet_buffer[3] == 0) { printf("Mqtt login server success\n"); /* subscribe */ init_topic(&g_sub_topic, sub_topic_name, sizeof(sub_topic_name)); mqtt_subscribe(&g_stMQTTBroker, g_sub_topic->name, &(g_sub_topic->msg_id)); /* publish msg with Qos 0 */ //step1:>>>publish init_topic(&g_pub_topic1, pub_topic_name1, sizeof(pub_topic_name1)); mqtt_publish(&g_stMQTTBroker, g_pub_topic1->name, pub_msg1, 0); printf("APP publish msg[%s] with Qos 0\n", pub_msg1); deinit_topic(&g_pub_topic1); /* publish msg with Qos 1 */ //step1:>>>publish //step2:<<<puback init_topic(&g_pub_topic2, pub_topic_name2, sizeof(pub_topic_name2)); mqtt_publish_with_qos(&g_stMQTTBroker, g_pub_topic2->name, pub_msg2, 0, 1, &(g_pub_topic2->msg_id)); } else printf("Mqtt login server fail!\n"); break; case MQTT_MSG_SUBACK: msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_sub_topic && msg_id_rcv == g_sub_topic->msg_id) printf("Subcribe topic[%s] success\n", g_sub_topic->name); break; case MQTT_MSG_PUBLISH: mqtt_parse_pub_topic(packet_buffer, topic_name); mqtt_parse_publish_msg(packet_buffer, msg); printf("****** Topic[%s] recv msg: *****\n%s\n",topic_name, msg); /* unsubscribe */ if(!strcmp(g_sub_topic->name, topic_name)) mqtt_unsubscribe(&g_stMQTTBroker, g_sub_topic->name, &(g_sub_topic->msg_id)); break; case MQTT_MSG_UNSUBACK: msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_sub_topic && msg_id_rcv == g_sub_topic->msg_id) { printf("Unsubcribe topic[%s] success\n", g_sub_topic->name); deinit_topic(&g_sub_topic); } break; case MQTT_MSG_PUBACK://Qos1 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic2 && msg_id_rcv == g_pub_topic2->msg_id) { printf("APP publish msg[%s] with Qos 1\n", pub_msg2); deinit_topic(&g_pub_topic2); /* publish msg with Qos 2 */ //step1:>>>publish //step2:<<<pubrec //step3:>>>pubrel //step4:<<<pubcomp init_topic(&g_pub_topic3, pub_topic_name3, sizeof(pub_topic_name3)); mqtt_publish_with_qos(&g_stMQTTBroker, g_pub_topic3->name, pub_msg3, 1, 2, &(g_pub_topic3->msg_id)); } break; case MQTT_MSG_PUBREC://Qos2 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic3 && msg_id_rcv == g_pub_topic3->msg_id) mqtt_pubrel(&g_stMQTTBroker, g_pub_topic3->msg_id); break; case MQTT_MSG_PUBCOMP://Qos2 msg_id_rcv = mqtt_parse_msg_id(packet_buffer); if(g_pub_topic3 && msg_id_rcv == g_pub_topic3->msg_id) { printf("APP publish msg[%s] with Qos 2\n", pub_msg3); deinit_topic(&g_pub_topic3); } break; default: printf("Unknow mqtt msg type\n"); break; } }