case2_qos0pub.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 PRE_DEF_TOPIC_ID 1
  30. int send_pub(char *host, int port, char * buf, int buflen, int qos, int retained, short packetid, unsigned short predef_topicid, 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_PREDEFINED;
  38. topic.data.id = predef_topicid;
  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 length %d\n", rc, payload, 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 = 0;
  55. int retained = 0;
  56. short packetid = 0;
  57. char ascii = 0;
  58. // char *topicname = "a long topic name";
  59. unsigned short predef_topicid = PRE_DEF_TOPIC_ID;
  60. char *host = "127.0.0.1";
  61. int port = 1884;
  62. int i = 0;
  63. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  64. mysock = transport_open();
  65. if(mysock < 0)
  66. return mysock;
  67. if (argc > 1)
  68. host = argv[1];
  69. if (argc > 2)
  70. port = atoi(argv[2]);
  71. TLOG("Sending to hostname %s port %d\n", host, port);
  72. options.clientID.cstring = "pubpredef0 MQTT-SN";
  73. len = MQTTSNSerialize_connect(buf, buflen, &options);
  74. rc = transport_sendPacketBuffer(host, port, buf, len);
  75. /* wait for connack */
  76. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  77. {
  78. int connack_rc = -1;
  79. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  80. {
  81. TLOG("Unable to connect, return code %d\n", connack_rc);
  82. goto exit;
  83. }
  84. else
  85. TLOG("connected rc %d\n", connack_rc);
  86. }
  87. else
  88. goto exit;
  89. for(i=0;i<2;i++)
  90. {
  91. unsigned short topicid = predef_topicid+i;
  92. ascii = 'a'+(i%26);
  93. payload[0] = payload[1] = payload[2] = ascii;
  94. payload[3] = 0;
  95. payloadlen = 4;
  96. send_pub(host, port, buf, buflen, qos, retained, packetid, topicid, payload, payloadlen);
  97. TLOG("%d send publish %s", i, payload);
  98. sleep(1);
  99. }
  100. exit:
  101. transport_close();
  102. return 0;
  103. }