DDS::Topic_var TestBase::create_topic() { const char* name = DEFAULT_TOPIC; TestMessageTypeSupport_var ts = new TestMessageTypeSupportImpl(); if (ts->register_type(this->participant_.in(), "") != DDS::RETCODE_OK) { ACE_ERROR((LM_ERROR, ACE_TEXT("ERROR: %N:%l: create_topic() -") ACE_TEXT(" register_type failed!\n"))); ACE_OS::exit(-1); } CORBA::String_var s = ts->get_type_name(); const char* type_name = s.in(); DDS::TopicQos qos; if (this->participant_->get_default_topic_qos(qos) != DDS::RETCODE_OK) { ACE_ERROR((LM_ERROR, ACE_TEXT("ERROR: %N:%l: create_topic() -") ACE_TEXT(" get_default_topic_qos failed!\n"))); ACE_OS::exit(-1); } DDS::TopicListener_ptr listener = DDS::TopicListener::_nil(); DDS::StatusMask status = OpenDDS::DCPS::DEFAULT_STATUS_MASK; if (init_topic(name, type_name, qos, listener, status) != DDS::RETCODE_OK) { ACE_ERROR((LM_ERROR, ACE_TEXT("ERROR: %N:%l: create_topic() -") ACE_TEXT(" init_topic failed!\n"))); ACE_OS::exit(-1); } DDS::Topic_var topic = this->participant_->create_topic(name, type_name, qos, listener, status); if (CORBA::is_nil(topic.in())) { ACE_ERROR((LM_ERROR, ACE_TEXT("ERROR: %N:%l: create_topic() -") ACE_TEXT(" create_topic failed!\n"))); ACE_OS::exit(-1); } return topic; }
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; } }