/*************************************************
 *
 *		Function : check mqtt witch qos 1
 *		packet_bufferBUF: MQTT receive data
 *		packet_length :	the packet length 
 *   add by Ale lin  2014-04-03
 *		
 ***************************************************/
int check_mqttpushqos1( uint8_t *packet_bufferBUF,int packet_length, uint16_t msg_id )
{
    uint16_t msg_id_rcv;
    uint8_t *packet_buffer=NULL;
    packet_buffer = ( uint8_t* )malloc(packet_length);
    memset( packet_buffer,0,packet_length);
    memcpy( packet_buffer,packet_bufferBUF,packet_length);
    if(packet_length < 0)
    {
        GAgent_Printf(GAGENT_INFO,"Error on read packet!");	
        free(packet_buffer);
        return -1;
    }
    if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_PUBACK)
    {
        GAgent_Printf(GAGENT_INFO,"PUBACK expected!");
        free(packet_buffer);
        return -1;						
    }
    msg_id_rcv = mqtt_parse_msg_id(packet_buffer);
    if(msg_id != msg_id_rcv)
    {
        GAgent_Printf(GAGENT_INFO," message id was expected, but message id was found!");
        free(packet_buffer);
        return -1;
    }			
    free(packet_buffer);						
    GAgent_Printf(GAGENT_INFO,"check_mqttpushqos1 OK");
    
    return 1;														
}
Esempio n. 2
0
/****************************************************************
        Function        :   Cloud_ResSubTopic
        Description     :   check sub topic respond.
        buf             :   data form mqtt.
        msgsubId        :   sub topic messages id
        return          :   0 sub topic ok.
                            other fail.
        Add by Alex.lin     --2015-03-09
****************************************************************/
uint32 Cloud_ResSubTopic( const uint8* buf,int8 msgsubId )
{
    uint16 recmsgId=0;
    recmsgId = mqtt_parse_msg_id( buf );
    if( recmsgId!=msgsubId )
        return 1;
    else 
        return 0;
}
Esempio n. 3
0
/****************************************************************
        Function        :   Cloud_ResSubTopic
        Description     :   check sub topic respond.
        buf             :   data form mqtt.
        msgsubId        :   sub topic messages id
        return          :   0 sub topic ok.
                            other fail.
        Add by Alex.lin     --2015-03-09
****************************************************************/
uint32 Cloud_ResSubTopic( const uint8* buf,int8 msgsubId )
{
    uint16 recmsgId=0;
     if(NULL == buf)
        return RET_FAILED;
    recmsgId = mqtt_parse_msg_id( buf );
    if( recmsgId!=msgsubId )
        return RET_FAILED;
    else 
        return RET_SUCCESS;
}
Esempio n. 4
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;
	}
		
}
Esempio n. 5
0
/**
 * Main routine
 *
 */
int main()
{
	int packet_length;
	uint16_t msg_id, msg_id_rcv;

	mqtt_init(&broker, "client-id");
	//mqtt_init_auth(&broker, "quijote", "rocinante");
	init_socket(&broker, "107.22.188.194", 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;
	}

	// Signals after connect MQTT
	signal(SIGALRM, alive);
	alarm(keepalive);
	signal(SIGINT, term);

	// >>>>> SUBSCRIBE
	mqtt_subscribe(&broker, "public/test/topic", &msg_id);
	// <<<<< SUBACK
	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_SUBACK)
	{
		fprintf(stderr, "SUBACK 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;
	}

	while(1)
	{
		// <<<<<
		packet_length = read_packet(0);
		if(packet_length == -1)
		{
			fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
			return -1;
		}
		else if(packet_length > 0)
		{
			printf("Packet Header: 0x%x...\n", packet_buffer[0]);
			if(MQTTParseMessageType(packet_buffer) == MQTT_MSG_PUBLISH)
			{
				uint8_t topic[255], msg[1000];
				uint16_t len;
				len = mqtt_parse_pub_topic(packet_buffer, topic);
				topic[len] = '\0'; // for printf
				len = mqtt_parse_publish_msg(packet_buffer, msg);
				msg[len] = '\0'; // for printf
				printf("%s %s\n", topic, msg);
			}
		}

	}
	return 0;
}
Esempio n. 6
0
/**
 * Main routine
 *
 */
static int mqtt_sub(void)
{
	int packet_length,socket_id;
	uint16_t msg_id, msg_id_rcv;
        mqtt_broker_handle_t broker;
        
        packet_buffer.len=BUFSIZE;
        broker.socket_info = (void *)&socket_id;
        

	mqtt_init(&broker, "MQTT_SUB");
	//mqtt_init_auth(&broker, "quijote", "rocinante");
	socket_id = init_socket(&broker);

	// >>>>> CONNECT
	mqtt_connect(&broker);
	// <<<<< CONNACK
        
       // unsigned long pubTaskHandle= getTaskHandle(SUB_TASKID);
       // vTaskPrioritySet( (xTaskHandle)&pubTaskHandle, PUB_TASK_PRIORITY);                   //to degrade sub_task priority to let it as the same of pub_task

      
        
	packet_length = read_packet(1,socket_id,(Tranmit_t *)&packet_buffer);
	if(packet_length < 0)
	{
		UART_PRINT("Error(%d) on read packet!\n\r");
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer.buffer) != MQTT_MSG_CONNACK)
	{
		UART_PRINT("CONNACK expected!\n\r");
		return -2;
	}

	if(packet_buffer.buffer[3] != 0x00)
	{
		UART_PRINT("CONNACK failed!\n\r");
		return -2;
	}
        
        
        
        UART_PRINT("Connected to broker!\n\r");
        
        
        
                if(OSI_OK != osi_TaskCreate( taskPub,
    				(const signed char*)"taskPub",
    				2048, NULL, PUB_TASK_PRIORITY, (OsiTaskHandle)&pubTaskHandle ))
      UART_PRINT("taskPub failed\n\r");

	// Signals after connect MQTT
	//signal(SIGALRM, alive);
	//alarm(keepalive);
	//signal(SIGINT, term);

	// >>>>> SUBSCRIBE
	mqtt_subscribe(&broker, "helloword", &msg_id);
	// <<<<< SUBACK
	packet_length = read_packet(1,socket_id,(Tranmit_t *)&packet_buffer);
	if(packet_length < 0)
	{
		UART_PRINT("Error(%d) on read packet!\n\r");
		return -1;
	}

	if(MQTTParseMessageType(packet_buffer.buffer) != MQTT_MSG_SUBACK)
	{
		UART_PRINT("SUBACK expected!\n\r");
		return -2;
	}

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

	while(1)
	{
		// <<<<<
		packet_length = read_packet(0,socket_id,(Tranmit_t *)&packet_buffer);
		if(packet_length == -1)
		{
			UART_PRINT("Error(%d) on read packet!\n\r");
			return -1;
		}
		else if(packet_length > 0)
		{
			UART_PRINT("Packet Header: 0x%x...\n\r");
			if(MQTTParseMessageType(packet_buffer.buffer) == MQTT_MSG_PUBLISH)
			{
				uint8_t topic[TOPIC_LEN_MAX], msg[MSG_LEN_MAX];
				uint16_t len;
				len = mqtt_parse_pub_topic(packet_buffer.buffer, topic);
				topic[len] = '\0'; // for printf
				len = mqtt_parse_publish_msg(packet_buffer.buffer, msg);
				msg[len] = '\0'; // for printf
				//UART_PRINT("%s %s\n\r", topic, msg);
                                UART_PRINT(topic);
                                UART_PRINT("\n\r");
                                UART_PRINT(msg);
                                UART_PRINT("\n\r");
			}
		}

	}
	return 0;
}
void *listener_thread(void *threadId)
{
    int packet_length;
	uint16_t msg_id, msg_id_rcv;
	int error = 0;

	printf("Starting listener thread...\n");

    // >>>>> SUBSCRIBE
	mqtt_subscribe(&broker, "yefsec0rk7uhatp/HighTemp", &msg_id);
	// <<<<< SUBACK
	packet_length = read_packet(1);
	if(packet_length < 0)
	{
		fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
		error = 1;
	}

	//printf("%s", packet_buffer);

	if(MQTTParseMessageType(packet_buffer) != MQTT_MSG_SUBACK)
	{
		fprintf(stderr, "SUBACK expected!\n");
		error = 1;
	}

	//printf("%s", packet_buffer);

	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);
		error = 1;
	}

	//printf("%s", packet_buffer);

	if (error)
    {
        printf("error setting up listener socket\n");
    }

	while(1)
	{
	    printf("Waiting for message...\n");

		packet_length = read_packet(0);
		if(packet_length == -1)
		{
			fprintf(stderr, "Error(%d) on read packet!\n", packet_length);
			break;
		}
		else if(packet_length > 0)
		{
			printf("Packet Header: 0x%x...\n", packet_buffer[0]);
			if(MQTTParseMessageType(packet_buffer) == MQTT_MSG_PUBLISH)
			{
				uint8_t topic[255], msg[1000];
				uint16_t len;

				led("g1", 1);

				len = mqtt_parse_pub_topic(packet_buffer, topic);
				topic[len] = '\0'; // for printf
				len = mqtt_parse_publish_msg(packet_buffer, msg);
				msg[len] = '\0'; // for printf
				printf("%s %s\n", topic, msg);

				sleep(1);

				led("g1", 0);
			}
			else if (MQTTParseMessageType(packet_buffer) == MQTT_MSG_PINGRESP)
            {
                printf("Ping response received\n");
            }
		}

	}

	pthread_exit(NULL);
}
Esempio n. 8
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;
}