mqtt_msg.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Author: Minh Tuan
  3. * File: mqtt_msg.h
  4. *
  5. * Created on July 12, 2014, 1:05 PM
  6. */
  7. #ifndef MQTT_MSG_H
  8. #define MQTT_MSG_H
  9. #include "pando_types.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /*
  14. * Copyright (c) 2014, Stephen Robinson
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. * 3. Neither the name of the copyright holder nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. */
  43. /* 7 6 5 4 3 2 1 0*/
  44. /*| --- Message Type---- | DUP Flag | QoS Level | Retain |*/
  45. /* Remaining Length */
  46. enum mqtt_message_type
  47. {
  48. MQTT_MSG_TYPE_CONNECT = 1,
  49. MQTT_MSG_TYPE_CONNACK = 2,
  50. MQTT_MSG_TYPE_PUBLISH = 3,
  51. MQTT_MSG_TYPE_PUBACK = 4,
  52. MQTT_MSG_TYPE_PUBREC = 5,
  53. MQTT_MSG_TYPE_PUBREL = 6,
  54. MQTT_MSG_TYPE_PUBCOMP = 7,
  55. MQTT_MSG_TYPE_SUBSCRIBE = 8,
  56. MQTT_MSG_TYPE_SUBACK = 9,
  57. MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
  58. MQTT_MSG_TYPE_UNSUBACK = 11,
  59. MQTT_MSG_TYPE_PINGREQ = 12,
  60. MQTT_MSG_TYPE_PINGRESP = 13,
  61. MQTT_MSG_TYPE_DISCONNECT = 14
  62. };
  63. typedef struct mqtt_message
  64. {
  65. uint8_t* data;
  66. uint16_t length;
  67. } mqtt_message_t;
  68. typedef struct mqtt_connection
  69. {
  70. mqtt_message_t message;
  71. uint16_t message_id;
  72. uint8_t* buffer;
  73. uint16_t buffer_length;
  74. } mqtt_connection_t;
  75. typedef struct mqtt_connect_info
  76. {
  77. char* client_id;
  78. char* username;
  79. char* password;
  80. char* will_topic;
  81. char* will_message;
  82. int keepalive;
  83. int will_qos;
  84. int will_retain;
  85. int clean_session;
  86. } mqtt_connect_info_t;
  87. static inline int mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
  88. static inline int mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
  89. static inline int mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
  90. static inline int mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
  91. void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
  92. int mqtt_get_total_length(uint8_t* buffer, uint16_t length);
  93. const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length);
  94. const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length);
  95. uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length);
  96. mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info);
  97. mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id);
  98. mqtt_message_t* mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id);
  99. mqtt_message_t* mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id);
  100. mqtt_message_t* mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id);
  101. mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id);
  102. mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id);
  103. mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id);
  104. mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection);
  105. mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection);
  106. mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection);
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif /* MQTT_MSG_H */