case6_sleep.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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("case6: ", fmt, ## __VA_ARGS__)
  29. int main(int argc, char** argv)
  30. {
  31. int rc = 0;
  32. int mysock;
  33. unsigned char buf[200];
  34. int buflen = sizeof(buf);
  35. unsigned char payload[16];
  36. int payloadlen = 0;
  37. int len = 0;
  38. int qos = 0;
  39. int retained = 0;
  40. short packetid = 0;
  41. char ascii = 0;
  42. // char *topicname = "a long topic name";
  43. char *host = "127.0.0.1";
  44. int port = 1884;
  45. int i = 0;
  46. MQTTSNString msstr = {0};
  47. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  48. mysock = transport_open();
  49. if(mysock < 0)
  50. return mysock;
  51. if (argc > 1)
  52. host = argv[1];
  53. if (argc > 2)
  54. port = atoi(argv[2]);
  55. TLOG("Sending to hostname %s port %d\n", host, port);
  56. options.clientID.cstring = "testclientid_case1";
  57. len = MQTTSNSerialize_connect(buf, buflen, &options);
  58. rc = transport_sendPacketBuffer(host, port, buf, len);
  59. /* wait for connack */
  60. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  61. {
  62. int connack_rc = -1;
  63. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  64. {
  65. TLOG("Unable to connect, return code %d\n", connack_rc);
  66. goto exit;
  67. }
  68. else
  69. TLOG("connected rc %d\n", connack_rc);
  70. }
  71. else
  72. goto exit;
  73. len = MQTTSNSerialize_disconnect(buf, buflen, 5);
  74. rc = transport_sendPacketBuffer(host, port, buf, len);
  75. /* wait for DISCONNECT */
  76. if ((rc = MQTTSNPacket_read(buf, buflen, transport_getdata)) == MQTTSN_DISCONNECT)
  77. {
  78. int duration = 0;
  79. if (MQTTSNDeserialize_disconnect(&duration, buf, buflen) != 1)
  80. {
  81. TLOG("Unable to diconnect, return code %d\n", duration);
  82. goto exit;
  83. }
  84. else
  85. TLOG("duration %d\n", duration);
  86. }
  87. else
  88. {
  89. TLOG("rc %d\n", rc);
  90. goto exit;
  91. }
  92. sleep(2);
  93. msstr.cstring = "testclientid_case1";
  94. len = MQTTSNSerialize_pingreq(buf, buflen, msstr);
  95. rc = transport_sendPacketBuffer(host, port, buf, len);
  96. /* wait for PINGRSP */
  97. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PINGRESP)
  98. {
  99. if (MQTTSNDeserialize_pingresp(buf, buflen) != 1)
  100. {
  101. TLOG("Unable to ping\n");
  102. goto exit;
  103. }
  104. else
  105. TLOG("get pingresp\n");
  106. }
  107. else
  108. goto exit;
  109. mark_result(argv[0], RESULT_PASS);
  110. transport_close();
  111. return 0;
  112. exit:
  113. mark_result(argv[0], RESULT_FAIL);
  114. transport_close();
  115. return 0;
  116. }