case2_qos0sub.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 PRE_DEF_TOPIC_ID 1
  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. recv_pubtopic->data.id = pubtopic.data.id;
  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 = 1;
  71. unsigned char retained = 0;
  72. short packetid = 1;
  73. //char *topicname = "/predefined/topic/name/hello";
  74. unsigned short predef_topicid1 = PRE_DEF_TOPIC_ID;
  75. unsigned short predef_topicid2 = PRE_DEF_TOPIC_ID+1;
  76. char *host = "127.0.0.1";
  77. int port = 1884;
  78. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  79. unsigned short topicid;
  80. char * recv_payload = NULL;
  81. char final_result = RESULT_PASS;
  82. mysock = transport_open();
  83. if(mysock < 0)
  84. return mysock;
  85. if (argc > 1)
  86. host = argv[1];
  87. if (argc > 2)
  88. port = atoi(argv[2]);
  89. TLOG("Sending to hostname %s port %d\n", host, port);
  90. options.clientID.cstring = "subpredef0 MQTT-SN";
  91. len = MQTTSNSerialize_connect(buf, buflen, &options);
  92. rc = transport_sendPacketBuffer(host, port, buf, len);
  93. /* wait for connack */
  94. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  95. {
  96. int connack_rc = -1;
  97. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  98. {
  99. TLOG("Unable to connect, return code %d\n", connack_rc);
  100. goto exit;
  101. }
  102. else
  103. TLOG("connected rc %d\n", connack_rc);
  104. }
  105. else
  106. goto exit;
  107. /* subscribe */
  108. TLOG("Subscribing to predef_topicid1\n");
  109. topic.type = MQTTSN_TOPIC_TYPE_PREDEFINED;
  110. topic.data.id = predef_topicid1;
  111. len = MQTTSNSerialize_subscribe(buf, buflen, 0, 0, packetid, &topic);
  112. rc = transport_sendPacketBuffer(host, port, buf, len);
  113. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_SUBACK) /* wait for suback */
  114. {
  115. unsigned short submsgid;
  116. int granted_qos;
  117. unsigned char returncode;
  118. rc = MQTTSNDeserialize_suback(&granted_qos, &predef_topicid1, &submsgid, &returncode, buf, buflen);
  119. if (granted_qos != 0 || returncode != 0)
  120. {
  121. TLOG("granted qos != 2, %d return code %d\n", granted_qos, returncode);
  122. goto exit;
  123. }
  124. else
  125. TLOG("suback topic id %d\n", predef_topicid1);
  126. }
  127. else
  128. goto exit;
  129. packetid = 2;
  130. TLOG("Subscribing to predef_topicid2\n");
  131. topic.type = MQTTSN_TOPIC_TYPE_PREDEFINED;
  132. topic.data.id = predef_topicid2;
  133. len = MQTTSNSerialize_subscribe(buf, buflen, 0, 0, packetid, &topic);
  134. rc = transport_sendPacketBuffer(host, port, buf, len);
  135. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_SUBACK) /* wait for suback */
  136. {
  137. unsigned short submsgid;
  138. int granted_qos;
  139. unsigned char returncode;
  140. rc = MQTTSNDeserialize_suback(&granted_qos, &predef_topicid2, &submsgid, &returncode, buf, buflen);
  141. if (granted_qos != 0 || returncode != 0)
  142. {
  143. TLOG("granted qos != 2, %d return code %d\n", granted_qos, returncode);
  144. goto exit;
  145. }
  146. else
  147. TLOG("suback topic id %d\n", predef_topicid2);
  148. }
  149. else
  150. goto exit;
  151. /*receive publish*/
  152. TLOG("Receive publish\n");
  153. for(i=0;i<2;i++)
  154. {
  155. MQTTSN_topicid pubtopic = {0};
  156. char ascii = 'a'+(i%26);
  157. expect_payload[0] = expect_payload[1] = expect_payload[2] = ascii;
  158. expect_payload[3] = 0;
  159. recv_payload = read_publish(host, port, buf, buflen, &pubtopic);
  160. if( recv_payload )
  161. {
  162. TLOG("case1_qos0sub %d receive %s\n", i, recv_payload);
  163. if( strcmp(recv_payload, expect_payload) != 0 )
  164. {
  165. final_result = RESULT_FAIL;
  166. break;
  167. }
  168. }
  169. if((pubtopic.type != MQTTSN_TOPIC_TYPE_PREDEFINED) || (pubtopic.data.id != predef_topicid1+i))
  170. {
  171. final_result = RESULT_FAIL;
  172. break;
  173. }
  174. sleep(1);
  175. }
  176. len = MQTTSNSerialize_disconnect(buf, buflen, 0);
  177. rc = transport_sendPacketBuffer(host, port, buf, len);
  178. exit:
  179. transport_close();
  180. mark_result(argv[0], final_result);
  181. return 0;
  182. }