case5_qos3sub.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*******************************************************************************
  2. * Copyright (c) 2014 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Sergio R. Caprile - clarifications and/or documentation extension
  16. *
  17. * Description:
  18. * Normal topic name is automatically registered at subscription, then
  19. * a message is published and the node receives it itself
  20. *******************************************************************************/
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include "MQTTSNPacket.h"
  26. #include "transport.h"
  27. #include "int_test_result.h"
  28. #define TLOG(fmt, ...) tlog("qos0sub", fmt, ## __VA_ARGS__)
  29. #define SHORT_TOPIC "ST"
  30. char * read_publish(char *host, int port, char * buf, int buflen, MQTTSN_topicid *recv_pubtopic)
  31. {
  32. int rc = 0;
  33. int len = 0;
  34. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBLISH)
  35. {
  36. unsigned short packet_id;
  37. int qos, payloadlen;
  38. unsigned char* payload = NULL;
  39. unsigned char dup, retained;
  40. MQTTSN_topicid pubtopic;
  41. if (MQTTSNDeserialize_publish(&dup, &qos, &retained, &packet_id, &pubtopic,
  42. &payload, &payloadlen, buf, buflen) != 1)
  43. TLOG("Error deserializing publish\n");
  44. else
  45. TLOG("publish received, packet_id %d qos %d topictype %d topicid %d\n", packet_id, qos, pubtopic.type, pubtopic.data.id);
  46. recv_pubtopic->type = pubtopic.type;
  47. memcpy(recv_pubtopic->data.short_name, pubtopic.data.short_name, 2);
  48. if (qos == 1)
  49. {
  50. len = MQTTSNSerialize_puback(buf, buflen, pubtopic.data.id, packet_id, MQTTSN_RC_ACCEPTED);
  51. rc = transport_sendPacketBuffer(host, port, buf, len);
  52. if (rc == 0)
  53. TLOG("puback sent\n");
  54. }
  55. return payload;
  56. }
  57. return NULL;
  58. }
  59. int main(int argc, char** argv)
  60. {
  61. int rc = 0;
  62. int mysock;
  63. unsigned char buf[200];
  64. int buflen = sizeof(buf);
  65. MQTTSN_topicid topic;
  66. char expect_payload[16];
  67. int len = 0;
  68. int i = 0;
  69. unsigned char dup = 0;
  70. int qos = 0;
  71. unsigned char retained = 0;
  72. short packetid = 1;
  73. //char *topicname = "predef_topic1";
  74. char *host = "127.0.0.1";
  75. int port = 1884;
  76. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  77. unsigned short topicid;
  78. char * recv_payload = NULL;
  79. char final_result = RESULT_PASS;
  80. mysock = transport_open();
  81. if(mysock < 0)
  82. return mysock;
  83. if (argc > 1)
  84. host = argv[1];
  85. if (argc > 2)
  86. port = atoi(argv[2]);
  87. TLOG("Sending to hostname %s port %d\n", host, port);
  88. options.clientID.cstring = "subpredef0 MQTT-SN";
  89. len = MQTTSNSerialize_connect(buf, buflen, &options);
  90. rc = transport_sendPacketBuffer(host, port, buf, len);
  91. /* wait for connack */
  92. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  93. {
  94. int connack_rc = -1;
  95. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  96. {
  97. TLOG("Unable to connect, return code %d\n", connack_rc);
  98. goto exit;
  99. }
  100. else
  101. TLOG("connected rc %d\n", connack_rc);
  102. }
  103. else
  104. goto exit;
  105. /* subscribe */
  106. TLOG("Subscribing to predef_topicid1\n");
  107. topic.type = MQTTSN_TOPIC_TYPE_SHORT;
  108. memcpy(topic.data.short_name, SHORT_TOPIC, 2);
  109. len = MQTTSNSerialize_subscribe(buf, buflen, 0, qos, packetid, &topic);
  110. rc = transport_sendPacketBuffer(host, port, buf, len);
  111. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_SUBACK) /* wait for suback */
  112. {
  113. unsigned short submsgid;
  114. int granted_qos;
  115. unsigned char returncode;
  116. rc = MQTTSNDeserialize_suback(&granted_qos, &topicid, &submsgid, &returncode, buf, buflen);
  117. if (granted_qos != 0 || returncode != 0)
  118. {
  119. TLOG("granted qos != 2, %d return code %d\n", granted_qos, returncode);
  120. goto exit;
  121. }
  122. else
  123. TLOG("suback topic id %d\n", topicid);
  124. }
  125. else
  126. goto exit;
  127. /*receive publish*/
  128. TLOG("Receive publish\n");
  129. for(i=0;i<2;i++)
  130. {
  131. MQTTSN_topicid pubtopic = {0};
  132. char ascii = 'a'+(i%26);
  133. expect_payload[0] = expect_payload[1] = expect_payload[2] = ascii;
  134. expect_payload[3] = 0;
  135. recv_payload = read_publish(host, port, buf, buflen, &pubtopic);
  136. if( recv_payload )
  137. {
  138. TLOG("case1_qos0sub %d receive %s\n", i, recv_payload);
  139. if( strcmp(recv_payload, expect_payload) != 0 )
  140. {
  141. final_result = RESULT_FAIL;
  142. break;
  143. }
  144. }
  145. if((pubtopic.type != MQTTSN_TOPIC_TYPE_SHORT) || (memcmp(pubtopic.data.short_name, SHORT_TOPIC, 2)))
  146. {
  147. final_result = RESULT_FAIL;
  148. break;
  149. }
  150. sleep(1);
  151. }
  152. len = MQTTSNSerialize_disconnect(buf, buflen, 0);
  153. rc = transport_sendPacketBuffer(host, port, buf, len);
  154. exit:
  155. transport_close();
  156. mark_result(argv[0], final_result);
  157. return 0;
  158. }