/
mqtt_client.h
204 lines (186 loc) · 5.31 KB
/
mqtt_client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @file inet/mqtt_client.h
* @version 7.0.0
*
* @section License
* Copyright (C) 2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERBITSTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#ifndef __INET_MQTT_CLIENT_H__
#define __INET_MQTT_CLIENT_H__
#include "simba.h"
/** Client states. */
enum mqtt_client_state_t {
mqtt_client_state_disconnected_t,
mqtt_client_state_connected_t,
mqtt_client_state_connecting_t
};
/** Quality of Service. */
enum mqtt_qos_t {
mqtt_qos_0_t = 0,
mqtt_qos_1_t = 1,
mqtt_qos_2_t = 2
};
struct mqtt_client_t;
/**
* Prototype of the on-publish callback function.
*
* @param[in] client_p The client.
* @param[in] topic_p The received topic.
* @param[in] chin_p The channel to read the value from.
* @param[in] size Number of bytes of the value to read from chin_p.
*
* @return Number of bytes read from the input channel.
*/
typedef size_t (*mqtt_on_publish_t)(struct mqtt_client_t *client_p,
const char *topic_p,
chan_t *chin_p,
size_t size);
/**
* Prototype of the on-error callback function.
*
* @param[in] client_p The client.
* @param[in] error The number of error that occured.
*
* @return zero(0) or nagative error code.
*/
typedef int (*mqtt_on_error_t)(struct mqtt_client_t *client_p,
int error);
/**
* MQTT client.
*/
struct mqtt_client_t {
const char *name_p;
struct log_object_t *log_object_p;
int state;
struct {
int type;
void *data_p;
} message;
struct {
chan_t *out_p;
chan_t *in_p;
} transport;
struct {
struct queue_t out;
struct queue_t in;
} control;
mqtt_on_publish_t on_publish;
mqtt_on_error_t on_error;
};
/**
* MQTT application message.
*/
struct mqtt_application_message_t {
struct {
const char *buf_p;
size_t size;
} topic;
struct {
const void *buf_p;
size_t size;
} payload;
enum mqtt_qos_t qos;
};
/**
* Initialize given MQTT client.
*
* @param[in] self_p MQTT client.
* @param[in] name_p Name of the thread.
* @param[in] log_object_p Log object.
* @param[in] chout_p Output channel for client to server packets.
* @param[in] chin_p Input channel for server to client packets.
* @param[in] on_publish On-publish callback function. Called when the
* server publishes a message.
* @param[in] on_error On-error callback function. Called when an error
* occurs.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_init(struct mqtt_client_t *self_p,
const char *name_p,
struct log_object_t *log_object_p,
chan_t *chout_p,
chan_t *chin_p,
mqtt_on_publish_t on_publish,
mqtt_on_error_t on_error);
/**
* MQTT client thread.
*
* @param[in] arg_p MQTT client.
*
* @return Never returns.
*/
void *mqtt_client_main(void *arg_p);
/**
* Establish a connection to the server.
*
* @param[in] self_p MQTT client.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_connect(struct mqtt_client_t *self_p);
/**
* Disconnect from the server.
*
* @param[in] self_p MQTT client.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_disconnect(struct mqtt_client_t *self_p);
/**
* Send a ping request to the server (broker) and wait for the ping
* response.
*
* @param[in] self_p MQTT client.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_ping(struct mqtt_client_t *self_p);
/**
* Publish given topic.
*
* @param[in] self_p MQTT client.
* @param[in] topic_p Topic.
* @param[in] payload_p Payload to publish. May be NULL.
* @param[in] payload_size Number of bytes in the payload.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_publish(struct mqtt_client_t *self_p,
struct mqtt_application_message_t *message_p);
/**
* Subscribe to given message.
*
* @param[in] self_p MQTT client.
* @param[in] message_p The message to subscribe to. The payload part
* of the message is not used. The topic may use
* wildcards, given that the server supports it.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_subscribe(struct mqtt_client_t *self_p,
struct mqtt_application_message_t *message_p);
/**
* Unsubscribe from given message.
*
* @param[in] self_p MQTT client.
* @param[in] message_p The message to unsubscribe from. Only the
* topic in the message is used.
*
* @return zero(0) or negative error code.
*/
int mqtt_client_unsubscribe(struct mqtt_client_t *self_p,
struct mqtt_application_message_t *message_p);
#endif