emcute.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Freie Universit├Ąt Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
85 #ifndef NET_EMCUTE_H
86 #define NET_EMCUTE_H
87 
88 #include <stdint.h>
89 #include <stddef.h>
90 #include <stdbool.h>
91 
92 #include "net/sock/udp.h"
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 #ifndef EMCUTE_DEFAULT_PORT
99 
102 #define EMCUTE_DEFAULT_PORT (1883U)
103 #endif
104 
105 #ifndef EMCUTE_BUFSIZE
106 
114 #define EMCUTE_BUFSIZE (512U)
115 #endif
116 
117 #ifndef EMCUTE_ID_MAXLEN
118 
124 #define EMCUTE_ID_MAXLEN (196U)
125 #endif
126 
127 #ifndef EMCUTE_TOPIC_MAXLEN
128 
134 #define EMCUTE_TOPIC_MAXLEN (196U)
135 #endif
136 
137 #ifndef EMCUTE_KEEPALIVE
138 
146 #define EMCUTE_KEEPALIVE (360) /* -> 6 min*/
147 #endif
148 
149 #ifndef EMCUTE_T_RETRY
150 
155 #define EMCUTE_T_RETRY (15U) /* -> 15 sec */
156 #endif
157 
158 #ifndef EMCUTE_N_RETRY
159 
164 #define EMCUTE_N_RETRY (3U)
165 #endif
166 
177 enum {
178  EMCUTE_DUP = 0x80,
180  EMCUTE_QOS_2 = 0x40,
181  EMCUTE_QOS_1 = 0x20,
182  EMCUTE_QOS_0 = 0x00,
183  EMCUTE_RETAIN = 0x10,
184  EMCUTE_WILL = 0x08,
185  EMCUTE_CS = 0x04,
190 };
191 
195 enum {
196  EMCUTE_OK = 0,
197  EMCUTE_NOGW = -1,
202 };
203 
207 typedef struct {
208  const char *name;
209  uint16_t id;
211 
219 typedef void(*emcute_cb_t)(const emcute_topic_t *topic, void *data, size_t len);
220 
224 typedef struct emcute_sub {
225  struct emcute_sub *next;
228  void *arg;
229 } emcute_sub_t;
230 
252 int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic,
253  const void *will_msg, size_t will_msg_len, unsigned flags);
254 
262 int emcute_discon(void);
263 
275 int emcute_reg(emcute_topic_t *topic);
276 
293 int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len,
294  unsigned flags);
295 
313 int emcute_sub(emcute_sub_t *sub, unsigned flags);
314 
324 int emcute_unsub(emcute_sub_t *sub);
325 
339 int emcute_willupd_topic(const char *topic, unsigned flags);
340 
354 int emcute_willupd_msg(const void *data, size_t len);
355 
365 void emcute_run(uint16_t port, const char *id);
366 
377 const char *emcute_type_str(uint8_t type);
378 
379 #ifdef __cplusplus
380 }
381 #endif
382 
383 #endif /* NET_EMCUTE_H */
384 
topic ID type mask
Definition: emcute.h:186
const char * name
topic string (currently ACSII only)
Definition: emcute.h:208
struct emcute_sub * next
next subscription (saved in a list)
Definition: emcute.h:225
topic ID: normal
Definition: emcute.h:189
error: timeout
Definition: emcute.h:200
UDP sock definitions.
error: ran out of buffer space
Definition: emcute.h:199
emcute_topic_t topic
topic we subscribe to
Definition: emcute.h:226
int emcute_sub(emcute_sub_t *sub, unsigned flags)
Subscribe to the given topic.
duplicate flag
Definition: emcute.h:178
error: not connected to a gateway
Definition: emcute.h:197
int emcute_willupd_topic(const char *topic, unsigned flags)
Update the last will topic.
retain flag
Definition: emcute.h:183
emcute_cb_t cb
function called when receiving messages
Definition: emcute.h:227
error: feature not supported
Definition: emcute.h:201
QoS level 0.
Definition: emcute.h:182
Data-structure for keeping track of topics we register to.
Definition: emcute.h:224
int emcute_willupd_msg(const void *data, size_t len)
Update the last will message.
int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len, unsigned flags)
Publish data on the given topic.
MQTT-SN topic.
Definition: emcute.h:207
QoS level 2.
Definition: emcute.h:180
struct emcute_sub emcute_sub_t
Data-structure for keeping track of topics we register to.
topic ID: short
Definition: emcute.h:187
void emcute_run(uint16_t port, const char *id)
Run emCute, will &#39;occupy&#39; the calling thread.
uint16_t id
topic id, as assigned by the gateway
Definition: emcute.h:209
topic ID: pre-defined
Definition: emcute.h:188
Common IP-based transport layer end point.
Definition: sock.h:195
everything went as expect
Definition: emcute.h:196
QoS level 1.
Definition: emcute.h:181
int emcute_reg(emcute_topic_t *topic)
Get a topic ID for the given topic name from the gateway.
void(* emcute_cb_t)(const emcute_topic_t *topic, void *data, size_t len)
Signature for callbacks fired when publish messages are received.
Definition: emcute.h:219
error: operation was rejected by broker
Definition: emcute.h:198
int emcute_discon(void)
Disconnect from the gateway we are currently connected to.
clean session flag
Definition: emcute.h:185
const char * emcute_type_str(uint8_t type)
Return the string representation of the given type value.
will flag, used during CONNECT
Definition: emcute.h:184
void * arg
optional custom argument
Definition: emcute.h:228
QoS level mask.
Definition: emcute.h:179
int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic, const void *will_msg, size_t will_msg_len, unsigned flags)
Connect to a given MQTT-SN gateway (CONNECT)
int emcute_unsub(emcute_sub_t *sub)
Unsubscripbe the given topic.