MeterLogger
mqtt_msg.h
Go to the documentation of this file.
1 /*
2  * File: mqtt_msg.h
3  * Author: Minh Tuan
4  *
5  * Created on July 12, 2014, 1:05 PM
6  */
7 
8 #ifndef MQTT_MSG_H
9 #define MQTT_MSG_H
10 #include "user_config.h"
11 #include "c_types.h"
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /*
17 * Copyright (c) 2014, Stephen Robinson
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. Neither the name of the copyright holder nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGE.
44 *
45 */
46 /* 7 6 5 4 3 2 1 0*/
47 /*| --- Message Type---- | DUP Flag | QoS Level | Retain |
48 /* Remaining Length */
49 
50 
52 {
67 };
68 
70 {
77 };
78 
79 typedef struct mqtt_message
80 {
81  uint8_t* data;
82  uint16_t length;
83 
85 
86 typedef struct mqtt_connection
87 {
89 
90  uint16_t message_id;
91  uint8_t* buffer;
92  uint16_t buffer_length;
93 
95 
96 typedef struct mqtt_connect_info
97 {
98  char* client_id;
99  char* username;
100  char* password;
101  char* will_topic;
103  uint32_t keepalive;
104  int will_qos;
107 
109 
110 
111 static inline int ICACHE_FLASH_ATTR mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
112 static inline int ICACHE_FLASH_ATTR mqtt_get_connect_return_code(uint8_t* buffer) { return buffer[3]; }
113 static inline int ICACHE_FLASH_ATTR mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
114 static inline int ICACHE_FLASH_ATTR mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
115 static inline int ICACHE_FLASH_ATTR mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
116 
117 void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
118 int ICACHE_FLASH_ATTR mqtt_get_total_length(uint8_t* buffer, uint16_t length);
119 const char* ICACHE_FLASH_ATTR mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length);
120 const char* ICACHE_FLASH_ATTR mqtt_get_publish_data(uint8_t* buffer, uint16_t* length);
121 uint16_t ICACHE_FLASH_ATTR mqtt_get_id(uint8_t* buffer, uint16_t length);
122 
124 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id);
125 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id);
126 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id);
127 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id);
128 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id);
129 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id);
130 mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id);
134 
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif /* MQTT_MSG_H */
141 
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_subscribe(mqtt_connection_t *connection, const char *topic, int qos, uint16_t *message_id)
Definition: mqtt_msg.c:435
char * will_message
Definition: mqtt_msg.h:102
char * will_topic
Definition: mqtt_msg.h:101
int ICACHE_FLASH_ATTR mqtt_get_total_length(uint8_t *buffer, uint16_t length)
Definition: mqtt_msg.c:135
static int ICACHE_FLASH_ATTR mqtt_get_retain(uint8_t *buffer)
Definition: mqtt_msg.h:115
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
uint16_t message_id
Definition: mqtt_msg.h:90
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubrec(mqtt_connection_t *connection, uint16_t message_id)
Definition: mqtt_msg.c:411
char * username
Definition: mqtt_msg.h:99
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_connect(mqtt_connection_t *connection, mqtt_connect_info_t *info)
Definition: mqtt_msg.c:291
struct mqtt_message mqtt_message_t
const char *ICACHE_FLASH_ATTR mqtt_get_publish_data(uint8_t *buffer, uint16_t *length)
Definition: mqtt_msg.c:183
mqtt_message_t message
Definition: mqtt_msg.h:88
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubcomp(mqtt_connection_t *connection, uint16_t message_id)
Definition: mqtt_msg.c:427
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id)
Definition: mqtt_msg.c:403
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_disconnect(mqtt_connection_t *connection)
Definition: mqtt_msg.c:483
char * client_id
Definition: mqtt_msg.h:98
static int ICACHE_FLASH_ATTR mqtt_get_dup(uint8_t *buffer)
Definition: mqtt_msg.h:113
uint16_t ICACHE_FLASH_ATTR mqtt_get_id(uint8_t *buffer, uint16_t length)
Definition: mqtt_msg.c:229
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_unsubscribe(mqtt_connection_t *connection, const char *topic, uint16_t *message_id)
Definition: mqtt_msg.c:455
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_publish(mqtt_connection_t *connection, const char *topic, const char *data, int data_length, int qos, int retain, uint16_t *message_id)
Definition: mqtt_msg.c:377
uint8_t * data
Definition: mqtt_msg.h:81
void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t *connection, uint8_t *buffer, uint16_t buffer_length)
Definition: mqtt_msg.c:128
mqtt_connect_return_code
Definition: mqtt_msg.h:69
struct mqtt_connection mqtt_connection_t
static int ICACHE_FLASH_ATTR mqtt_get_connect_return_code(uint8_t *buffer)
Definition: mqtt_msg.h:112
uint16_t length
Definition: mqtt_msg.h:82
uint32_t keepalive
Definition: mqtt_msg.h:103
struct mqtt_connect_info mqtt_connect_info_t
static int ICACHE_FLASH_ATTR mqtt_get_type(uint8_t *buffer)
Definition: mqtt_msg.h:111
uint8_t * buffer
Definition: mqtt_msg.h:91
const char *ICACHE_FLASH_ATTR mqtt_get_publish_topic(uint8_t *buffer, uint16_t *length)
Definition: mqtt_msg.c:154
uint16_t buffer_length
Definition: mqtt_msg.h:92
mqtt_message_type
Definition: mqtt_msg.h:51
static int ICACHE_FLASH_ATTR mqtt_get_qos(uint8_t *buffer)
Definition: mqtt_msg.h:114
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pingreq(mqtt_connection_t *connection)
Definition: mqtt_msg.c:471
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pingresp(mqtt_connection_t *connection)
Definition: mqtt_msg.c:477
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubrel(mqtt_connection_t *connection, uint16_t message_id)
Definition: mqtt_msg.c:419