Exemplo n.º 1
0
/**
  * @brief  向代理(服务器)发送一个消息
  * @param  pTopic 消息主题
  * @param  pMessage 消息内容
  * @retval 小于0表示发送失败
  */
int mqtt_publish(char *pTopic,char *pMessage)
{
  int32_t len,rc;
  MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
  unsigned char buf[200];
  MQTTString topicString = MQTTString_initializer;
  int msglen = strlen(pMessage);
  int buflen = sizeof(buf);

  data.clientID.cstring = "me";
  data.keepAliveInterval = 5;
  data.cleansession = 1;
  len = MQTTSerialize_connect(buf, buflen, &data); /* 1 */

  topicString.cstring = pTopic;
  len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, (unsigned char*)pMessage, msglen); /* 2 */

  len += MQTTSerialize_disconnect(buf + len, buflen - len); /* 3 */
  transport_open();	
  rc = transport_sendPacketBuffer(buf,len);
  transport_close();
	if (rc == len)
		printf("Successfully published\n\r");
	else
		printf("Publish failed\n\r");
  return 0;
}
Exemplo n.º 2
0
int send_pub(char *host, int port, char * buf, int buflen, int qos, int retained, short packetid, char *payload, int payloadlen)
{
    MQTTSN_topicid topic;
    int rc = 0;
    int len = 0;
    int dup = 0;
    
    /* publish with short name */
    topic.type = MQTTSN_TOPIC_TYPE_SHORT;
    memcpy(topic.data.short_name, SHORT_TOPIC, 2);
    len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
            topic, payload, payloadlen);
    rc = transport_sendPacketBuffer(host, port, buf, len);

    TLOG("rc %d from send packet %s for publish topictype %d topicid %d length %d\n", rc, payload, topic.type, topic.data.id, len);
    
    return rc;
}
Exemplo n.º 3
0
int main(int argc, char** argv)
{
	int rc = 0;
	int mysock;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	MQTTSN_topicid topic;
	unsigned char* payload = (unsigned char*)"mypayload";
	int payloadlen = strlen((char*)payload);
	int len = 0;
	int dup = 0;
	int qos = 3;
	int retained = 0;
	short packetid = 0;
	char *topicname = "a long topic name";
	char *host = "127.0.0.1";
	int port = 1883;

	mysock = transport_open();
	if(mysock < 0)
		return mysock;

	if (argc > 1)
		host = argv[1];

	if (argc > 2)
		port = atoi(argv[2]);

	printf("Sending to hostname %s port %d\n", host, port);

	topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
	topic.data.long_.name = topicname;
	topic.data.long_.len = strlen(topicname);

	len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
			topic, payload, payloadlen);

	rc = transport_sendPacketBuffer(host, port, buf, len);

	transport_close();

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char** argv)
{
    int rc = 0;
    int mysock;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    
    unsigned char payload[16];
    int payloadlen = 0;
    int len = 0;
    int qos = 0;
    int retained = 0;
    short packetid = 0;
    char ascii = 0;
//  char *topicname = "a long topic name";
    char *host = "127.0.0.1";
    int port = 1884;
    int i = 0;
    MQTTSNString msstr = {0};
    MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

    mysock = transport_open();
    if(mysock < 0)
        return mysock;

    if (argc > 1)
        host = argv[1];

    if (argc > 2)
        port = atoi(argv[2]);

    TLOG("Sending to hostname %s port %d\n", host, port);

    options.clientID.cstring = "testclientid_case1";
    len = MQTTSNSerialize_connect(buf, buflen, &options);
    rc = transport_sendPacketBuffer(host, port, buf, len);

    /* wait for connack */
    if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
    {
        int connack_rc = -1;

        if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
        {
            TLOG("Unable to connect, return code %d\n", connack_rc);
            goto exit;
        }
        else 
            TLOG("connected rc %d\n", connack_rc);
    }
    else
        goto exit;

    
    
    len = MQTTSNSerialize_disconnect(buf, buflen, 5);
    rc = transport_sendPacketBuffer(host, port, buf, len);
    /* wait for DISCONNECT */
    if ((rc = MQTTSNPacket_read(buf, buflen, transport_getdata)) == MQTTSN_DISCONNECT)
    {
        int duration = 0;
        if (MQTTSNDeserialize_disconnect(&duration, buf, buflen) != 1)
        {
            TLOG("Unable to diconnect, return code %d\n", duration);
            goto exit;
        }
        else 
            TLOG("duration %d\n", duration);
    }
    else
    {
    	TLOG("rc %d\n", rc);
        goto exit;
    }


    sleep(2);


    msstr.cstring = "testclientid_case1";
    len = MQTTSNSerialize_pingreq(buf, buflen, msstr);
    rc = transport_sendPacketBuffer(host, port, buf, len);
    /* wait for PINGRSP */
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PINGRESP)
	{
		if (MQTTSNDeserialize_pingresp(buf, buflen) != 1)
		{
			TLOG("Unable to ping\n");
			goto exit;
		}
		else
			TLOG("get pingresp\n");
	}
	else
		goto exit;


	mark_result(argv[0], RESULT_PASS);
	transport_close();
	return 0;

exit:
	mark_result(argv[0], RESULT_FAIL);
    transport_close();

    return 0;
}
Exemplo n.º 5
0
/**
  * @brief  向服务器订阅一个消息,该函数会因为TCP接收数据函数而阻塞
  * @param  pTopic 消息主题,传入
  * @param  pMessage 消息内容,传出
  * @retval 小于0表示订阅消息失败
  */
int mqtt_subscrib(char *pTopic,char *pMessage)
{
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	int rc = 0;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	int msgid = 1;
	MQTTString topicString = MQTTString_initializer;
	int req_qos = 0;

	int len = 0;
	rc = transport_open();
	if(rc < 0){
    printf("transport_open error\n\r");
		return rc;
  }

	data.clientID.cstring = "";
	data.keepAliveInterval = 5;//服务器保持连接时间,超过该时间后,服务器会主动断开连接,单位为秒
	data.cleansession = 1;
	data.username.cstring = "";
	data.password.cstring = "";

	len = MQTTSerialize_connect(buf, buflen, &data);
	rc = transport_sendPacketBuffer(buf, len);
  if(rc != len){
    printf("connect transport_sendPacketBuffer error\n\r");
    goto exit;
  }
	/* wait for connack */
	if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
	{
		unsigned char sessionPresent, connack_rc;

		if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
		{
			printf("Unable to connect, return code %d\n\r", connack_rc);
			goto exit;
		}
	}else{
    printf("MQTTPacket_read error\n\r");
		goto exit;
  }

	/* subscribe */
	topicString.cstring = pTopic;
	len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);

	rc = transport_sendPacketBuffer(buf, len);
  if(rc != len){
    printf("connect transport_sendPacketBuffer error\n\r");
    goto exit;
  }
	if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) 	/* wait for suback */
	{
		unsigned short submsgid;
		int subcount;
		int granted_qos;

		rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
		if (granted_qos != 0)
		{
			printf("granted qos != 0, %d\n\r", granted_qos);
			goto exit;
		}
	}
	else
		goto exit;

	/* loop getting msgs on subscribed topic */
	topicString.cstring = pTopic;
  memset(buf,0,buflen);
  //transport_getdata接收数据会阻塞,除非服务器断开连接后才返回
  if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH){
    unsigned char dup;
    int qos;
    unsigned char retained;
    unsigned short msgid;
    int payloadlen_in;
    unsigned char* payload_in;
    MQTTString receivedTopic;

    rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
        &payload_in, &payloadlen_in, buf, buflen);
    printf("message arrived %d: %s\n\r", payloadlen_in, payload_in);
    strcpy(pMessage,(const char *)payload_in);
  }
  printf("disconnecting\n\r");
  len = MQTTSerialize_disconnect(buf, buflen);
  rc = transport_sendPacketBuffer(buf, len);
exit:
	transport_close();
  return rc;
}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	int rc = 0;
	int mysock = 0;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	int msgid = 1;
	MQTTString topicString = MQTTString_initializer;
	int req_qos = 0;
	char* payload = "mypayload";
	int payloadlen = strlen(payload);
	int len = 0;
	char *host = "m2m.eclipse.org";
	int port = 1883;
	MQTTTransport mytransport;

	stop_init();
	if (argc > 1)
		host = argv[1];

	if (argc > 2)
		port = atoi(argv[2]);

	mysock = transport_open(host, port);
	if(mysock < 0)
		return mysock;

	printf("Sending to hostname %s port %d\n", host, port);

	mytransport.sck = &mysock;
	mytransport.getfn = transport_getdatanb;
	mytransport.state = 0;
	data.clientID.cstring = "me";
	data.keepAliveInterval = 20;
	data.cleansession = 1;
	data.username.cstring = "testuser";
	data.password.cstring = "testpassword";

	len = MQTTSerialize_connect(buf, buflen, &data);
	rc = transport_sendPacketBuffer(mysock, buf, len);

	/* wait for connack */
	if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
	{
		unsigned char sessionPresent, connack_rc;

		if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
		{
			printf("Unable to connect, return code %d\n", connack_rc);
			goto exit;
		}
	}
	else
		goto exit;

	/* subscribe */
	topicString.cstring = "substopic";
	len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);

	rc = transport_sendPacketBuffer(mysock, buf, len);
	do {
		int frc;
		if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */
		{
			unsigned short submsgid;
			int subcount;
			int granted_qos;

			rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
			if (granted_qos != 0)
			{
				printf("granted qos != 0, %d\n", granted_qos);
				goto exit;
			}
			break;
		}
		else if (frc == -1)
			goto exit;
	} while (1); /* handle timeouts here */
	/* loop getting msgs on subscribed topic */
	topicString.cstring = "pubtopic";
	while (!toStop)
	{
		/* handle timeouts */
		if (MQTTPacket_readnb(buf, buflen, &mytransport) == PUBLISH)
		{
			unsigned char dup;
			int qos;
			unsigned char retained;
			unsigned short msgid;
			int payloadlen_in;
			unsigned char* payload_in;
			int rc;
			MQTTString receivedTopic;

			rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
					&payload_in, &payloadlen_in, buf, buflen);
			printf("message arrived %.*s\n", payloadlen_in, payload_in);
			printf("publishing reading\n");
			len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
			rc = transport_sendPacketBuffer(mysock, buf, len);
		}
	}

	printf("disconnecting\n");
	len = MQTTSerialize_disconnect(buf, buflen);
	rc = transport_sendPacketBuffer(mysock, buf, len);

exit:
	transport_close(mysock);

	return 0;
}
Exemplo n.º 7
0
int main(int argc, char** argv)
{
	int rc = 0;
	int mysock;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	MQTTSN_topicid topic;
	unsigned char* payload = (unsigned char*)"mypayload";
	int payloadlen = strlen((char*)payload);
	int len = 0;
	unsigned char dup = 0;
	int qos = 1;
	unsigned char retained = 0;
	short packetid = 1;
	char *topicname = "a long topic name";
	char *host = "127.0.0.1";
	int port = 1883;
	MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
	unsigned short topicid;

	mysock = transport_open();
	if(mysock < 0)
		return mysock;

	if (argc > 1)
		host = argv[1];

	if (argc > 2)
		port = atoi(argv[2]);

	printf("Sending to hostname %s port %d\n", host, port);

	options.clientID.cstring = "pub0sub1 MQTT-SN";
	len = MQTTSNSerialize_connect(buf, buflen, &options);
	rc = transport_sendPacketBuffer(host, port, buf, len);

	/* wait for connack */
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
	{
		int connack_rc = -1;

		if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
		{
			printf("Unable to connect, return code %d\n", connack_rc);
			goto exit;
		}
		else 
			printf("connected rc %d\n", connack_rc);
	}
	else
		goto exit;


	/* subscribe */
	printf("Subscribing\n");
	topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
	topic.data.long_.name = topicname;
	topic.data.long_.len = strlen(topic.data.long_.name);
	len = MQTTSNSerialize_subscribe(buf, buflen, 0, 2, packetid, &topic);
	rc = transport_sendPacketBuffer(host, port, buf, len);

	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_SUBACK) 	/* wait for suback */
	{
		unsigned short submsgid;
		int granted_qos;
		unsigned char returncode;

		rc = MQTTSNDeserialize_suback(&granted_qos, &topicid, &submsgid, &returncode, buf, buflen);
		if (granted_qos != 2 || returncode != 0)
		{
			printf("granted qos != 2, %d return code %d\n", granted_qos, returncode);
			goto exit;
		}
		else
			printf("suback topic id %d\n", topicid);
	}
	else
		goto exit;

	printf("Publishing\n");
	/* publish with short name */
	topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
	topic.data.id = topicid;
	++packetid;
	len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
			topic, payload, payloadlen);
	rc = transport_sendPacketBuffer(host, port, buf, len);

	/* wait for puback */
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBACK)
	{
		unsigned short packet_id, topic_id;
		unsigned char returncode;

		if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
			printf("Unable to publish, return code %d\n", returncode);
		else 
			printf("puback received, msgid %d topic id %d\n", packet_id, topic_id);
	}
	else
		goto exit;

	printf("Receive publish\n");
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBLISH)
	{
		unsigned short packet_id;
		int qos, payloadlen;
		unsigned char* payload;
		unsigned char dup, retained;
		MQTTSN_topicid pubtopic;

		if (MQTTSNDeserialize_publish(&dup, &qos, &retained, &packet_id, &pubtopic,
				&payload, &payloadlen, buf, buflen) != 1)
			printf("Error deserializing publish\n");
		else 
			printf("publish received, id %d qos %d\n", packet_id, qos);

		if (qos == 1)
		{
			len = MQTTSNSerialize_puback(buf, buflen, pubtopic.data.id, packet_id, MQTTSN_RC_ACCEPTED);
			rc = transport_sendPacketBuffer(host, port, buf, len);
			if (rc == 0)
				printf("puback sent\n");
		}
	}
	else
		goto exit;

	len = MQTTSNSerialize_disconnect(buf, buflen, 0);
	rc = transport_sendPacketBuffer(host, port, buf, len);

exit:
	transport_close();

	return 0;
}
Exemplo n.º 8
0
int main(int argc, char** argv)
{
    int rc = 0;
    int mysock;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    
    unsigned char payload[16];
    int payloadlen = 0;
    int len = 0;
    int qos = 3;
    int retained = 0;
    short packetid = 0;
    char ascii = 0;
//  char *topicname = "a long topic name";
    char *host = "127.0.0.1";
    int port = 1884;
    int i = 0;
    MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

    mysock = transport_open();
    if(mysock < 0)
        return mysock;

    if (argc > 1)
        host = argv[1];

    if (argc > 2)
        port = atoi(argv[2]);

    TLOG("Sending to hostname %s port %d\n", host, port);

    options.clientID.cstring = "pubpredef0 MQTT-SN";
    len = MQTTSNSerialize_connect(buf, buflen, &options);
    rc = transport_sendPacketBuffer(host, port, buf, len);

    /* wait for connack */
    if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
    {
        int connack_rc = -1;

        if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
        {
            TLOG("Unable to connect, return code %d\n", connack_rc);
            goto exit;
        }
        else 
            TLOG("connected rc %d\n", connack_rc);
    }
    else
        goto exit;

    for(i=0;i<2;i++)
    {
        ascii = 'a'+(i%26);
        payload[0] = payload[1] = payload[2] = ascii;
        payload[3] = 0;
        payloadlen = 4;
        send_pub(host, port, buf, buflen, qos, retained, packetid, payload, payloadlen);
        TLOG("%d send publish %s", i, payload);
        sleep(1);
    }

exit:
    transport_close();

    return 0;
}
Exemplo n.º 9
0
int main(int argc, char** argv)
{
	int rc = 0;
	int mysock;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	MQTTSN_topicid topic;
	unsigned char* payload = (unsigned char*)"mypayload";
	int payloadlen = strlen((char*)payload);
	int len = 0;
	int dup = 0;
	int qos = 1;
	int retained = 0;
	short packetid = 1;
	char *host = "127.0.0.1";
	int port = 1883;
	MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

	mysock = transport_open();
	if(mysock < 0)
		return mysock;

	if (argc > 1)
		host = argv[1];

	if (argc > 2)
		port = atoi(argv[2]);

	printf("Sending to hostname %s port %d\n", host, port);

	options.clientID.cstring = "myclientid";
	len = MQTTSNSerialize_connect(buf, buflen, &options);
	rc = transport_sendPacketBuffer(host, port, buf, len);

	/* wait for connack */
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
	{
		int connack_rc = -1;

		if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
		{
			printf("Unable to connect, return code %d\n", connack_rc);
			goto exit;
		}
		else 
			printf("connected rc %d\n", connack_rc);
	}
	else
		goto exit;

	/* publish with short name */
	topic.type = MQTTSN_TOPIC_TYPE_SHORT;
	memcpy(topic.data.short_name, "tt", 2);
	len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid,
			topic, payload, payloadlen);
	rc = transport_sendPacketBuffer(host, port, buf, len);

	/* wait for puback */
	if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBACK)
	{
		unsigned short packet_id, topic_id;
		unsigned char returncode;

		if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
			printf("Unable to publish, return code %d\n", returncode);
		else 
			printf("puback received, id %d\n", packet_id);
	}
	else
		goto exit;

	len = MQTTSNSerialize_disconnect(buf, buflen, 0);
	rc = transport_sendPacketBuffer(host, port, buf, len);

exit:
	transport_close();

	return 0;
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
	/******MQTT*******/
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	int rc = 0;
	char buf[200];
	int buflen = sizeof(buf);
	int mysock = 0;
	MQTTString topicString = MQTTString_initializer;
	char* payload = "mypayload";
	int payloadlen = strlen(payload);
	int len = 0;
	char *host = "192.168.8.241";
	int port = 1883;

	/******UART*******/
	int fd = FALSE;
	int ret;
	char rcv_buf[512];
 	int i;
	if(argc < 2){
		printf("Usage: %s /dev/ttySn \n",argv[0]);
		return FALSE;
	}

	if (argc > 2)
		host = argv[2];

	if (argc > 3)
		port = atoi(argv[3]);
	
	fd = UART_Open(fd,argv[1]);
	if(FALSE == fd){
		printf("open error\n");
		exit(1);
	}
	ret  = UART_Init(fd,9600,0,8,1,'N');
	if (FALSE == fd){
		printf("Set Port Error\n");
		exit(1);
	}

	ret  = UART_Send(fd,"*IDN?\n",6);
	if(FALSE == ret){
		printf("write error!\n");
		exit(1);
	}
	printf("command: %s\n","*IDN?");
	memset(rcv_buf,0,sizeof(rcv_buf));

	/**************MQTT tansport*************/

	mysock = transport_open(host,port);
	if(mysock < 0)
		return mysock;

	printf("Sending to hostname %s port %d\n", host, port);

	data.clientID.cstring = "me";
	data.keepAliveInterval = 20;
	data.cleansession = 1;
	data.username.cstring = "testuser";
	data.password.cstring = "testpassword";
	data.MQTTVersion = 4;


	for(i=0;;i++)
	{
	   	ret = UART_Recv(fd, rcv_buf,512);	
		if(ret > 0)
		{
			rcv_buf[ret]='\0';
			printf("%s",rcv_buf);

			len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);
			topicString.cstring = "mytopic";
		
			len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)rcv_buf, ret+1);
			len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);
			rc = transport_sendPacketBuffer(mysock, buf, len);
			if (rc == len)
				printf("Successfully published\n");
			else
				printf("Publish failed\n");
		}
		else
		{
			printf("cannot receive data1\n");
			break;
		}
	}

exit:
	UART_Close(fd);
	transport_close(mysock);

	return 0;
}