Beispiel #1
0
int main(int argc, char* argv[])
{
	int packet_length;
	uint16_t msg_id, msg_id_rcv;
	mqtt_broker_handle_t broker;

   mqtt_init(&broker, "sancho");
   mqtt_init_auth(&broker, "quijote", "rocinante");
   init_socket(&broker, "192.168.10.40", 1883, keepalive);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	// <<<<< CONNACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK)
	{
		fprintf(stderr, "CONNACK expected!\n");
		return -2;
	}

	if(packet_buffer[3] != 0x00)
	{
		fprintf(stderr, "CONNACK failed!\n");
		return -2;
	}


	// >>>>> PUBLISH QoS 0
	printf("Publish: QoS 0\n");
	mqtt_publish(&broker, "public/myexample/example", "Test libemqtt message.", 0);
	// >>>>> PUBLISH QoS 1
	printf("Publish: QoS 1\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 1", 0, 1, &msg_id);
	// <<<<< PUBACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
	{
		fprintf(stderr, "PUBACK expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBLISH QoS 2
	printf("Publish: QoS 2\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 2", 1, 2, &msg_id); // Retain
	// <<<<< PUBREC
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBREC)
	{
		fprintf(stderr, "PUBREC expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBREL
	mqtt_pubrel(&broker, msg_id);
	// <<<<< PUBCOMP
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBCOMP)
	{
		fprintf(stderr, "PUBCOMP expected!\n");
		return -2;
	}

	MQTTParseMessageId(packet_buffer, msg_id_rcv);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> DISCONNECT
	mqtt_disconnect(&broker);
	close_socket(&broker);
	return 0;
}
Beispiel #2
0
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;
	}
		
}
Beispiel #3
0
int main(int argc, char* argv[])
{
	int packet_length;
	uint16_t msg_id, msg_id_rcv;
	mqtt_broker_handle_t broker;

	mqtt_init(&broker, "avengalvon");
	mqtt_init_auth(&broker, "cid", "campeador");
	init_socket(&broker, "127.0.0.1", 1883);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	// <<<<< CONNACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_CONNACK)
	{
		fprintf(stderr, "CONNACK expected!\n");
		return -2;
	}

	if(packet_buffer[3] != 0x00)
	{
		fprintf(stderr, "CONNACK failed!\n");
		return -2;
	}

	// >>>>> PUBLISH QoS 0
	printf("Publish: QoS 0\n");
	mqtt_publish(&broker, "hello/emqtt", "Example: QoS 0", 0);

	// >>>>> PUBLISH QoS 1
	printf("Publish: QoS 1\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 1", 0, 1, &msg_id);
	// <<<<< PUBACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
	{
		fprintf(stderr, "PUBACK expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBLISH QoS 2
	printf("Publish: QoS 2\n");
	mqtt_publish_with_qos(&broker, "hello/emqtt", "Example: QoS 2", 1, 2, &msg_id); // Retain
	// <<<<< PUBREC
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBREC)
	{
		fprintf(stderr, "PUBREC expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> PUBREL
	mqtt_pubrel(&broker, msg_id);
	// <<<<< PUBCOMP
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBCOMP)
	{
		fprintf(stderr, "PUBCOMP expected!\n");
		return -2;
	}

	msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
	if(msg_id != msg_id_rcv)
	{
		fprintf(stderr, "%d message id was expected, but %d message id was found!\n", msg_id, msg_id_rcv);
		return -3;
	}

	// >>>>> DISCONNECT
	mqtt_disconnect(&broker);
	close_socket(&broker);
	return 0;
}