case5_qos3pub.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Short topic name used to avoid registration process
  19. *******************************************************************************/
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include "MQTTSNPacket.h"
  26. #include "transport.h"
  27. #include "int_test_result.h"
  28. #define TLOG(fmt, ...) tlog("qos0pub", fmt, ## __VA_ARGS__)
  29. #define SHORT_TOPIC "ST"
  30. int send_pub(char *host, int port, char * buf, int buflen, int qos, int retained, short packetid, char *payload, int payloadlen)
  31. {
  32. MQTTSN_topicid topic;
  33. int rc = 0;
  34. int len = 0;
  35. int dup = 0;
  36. /* publish with short name */
  37. topic.type = MQTTSN_TOPIC_TYPE_SHORT;
  38. memcpy(topic.data.short_name, SHORT_TOPIC, 2);
  39. len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
  40. topic, payload, payloadlen);
  41. rc = transport_sendPacketBuffer(host, port, buf, len);
  42. TLOG("rc %d from send packet %s for publish topictype %d topicid %d length %d\n", rc, payload, topic.type, topic.data.id, len);
  43. return rc;
  44. }
  45. int main(int argc, char** argv)
  46. {
  47. int rc = 0;
  48. int mysock;
  49. unsigned char buf[200];
  50. int buflen = sizeof(buf);
  51. unsigned char payload[16];
  52. int payloadlen = 0;
  53. int len = 0;
  54. int qos = 3;
  55. int retained = 0;
  56. short packetid = 0;
  57. char ascii = 0;
  58. // char *topicname = "a long topic name";
  59. char *host = "127.0.0.1";
  60. int port = 1884;
  61. int i = 0;
  62. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  63. mysock = transport_open();
  64. if(mysock < 0)
  65. return mysock;
  66. if (argc > 1)
  67. host = argv[1];
  68. if (argc > 2)
  69. port = atoi(argv[2]);
  70. TLOG("Sending to hostname %s port %d\n", host, port);
  71. options.clientID.cstring = "pubpredef0 MQTT-SN";
  72. len = MQTTSNSerialize_connect(buf, buflen, &options);
  73. rc = transport_sendPacketBuffer(host, port, buf, len);
  74. /* wait for connack */
  75. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  76. {
  77. int connack_rc = -1;
  78. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  79. {
  80. TLOG("Unable to connect, return code %d\n", connack_rc);
  81. goto exit;
  82. }
  83. else
  84. TLOG("connected rc %d\n", connack_rc);
  85. }
  86. else
  87. goto exit;
  88. for(i=0;i<2;i++)
  89. {
  90. ascii = 'a'+(i%26);
  91. payload[0] = payload[1] = payload[2] = ascii;
  92. payload[3] = 0;
  93. payloadlen = 4;
  94. send_pub(host, port, buf, buflen, qos, retained, packetid, payload, payloadlen);
  95. TLOG("%d send publish %s", i, payload);
  96. sleep(1);
  97. }
  98. exit:
  99. transport_close();
  100. return 0;
  101. }