emcute.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
85 #include <stdint.h>
86 #include <stddef.h>
87 #include <stdbool.h>
88 
89 #include "net/sock/udp.h"
90 
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 
108 #ifndef CONFIG_EMCUTE_DEFAULT_PORT
109 #define CONFIG_EMCUTE_DEFAULT_PORT (1883U)
110 #endif
111 
120 #ifndef CONFIG_EMCUTE_BUFSIZE
121 #define CONFIG_EMCUTE_BUFSIZE (512U)
122 #endif
123 
129 #ifndef CONFIG_EMCUTE_TOPIC_MAXLEN
130 #define CONFIG_EMCUTE_TOPIC_MAXLEN (196U)
131 #endif
132 
140 #ifndef CONFIG_EMCUTE_KEEPALIVE
141 #define CONFIG_EMCUTE_KEEPALIVE (360) /* -> 6 min*/
142 #endif
143 
154 #ifndef CONFIG_EMCUTE_T_RETRY
155 #define CONFIG_EMCUTE_T_RETRY (15U) /* -> 15 sec */
156 #endif
157 
167 #ifndef CONFIG_EMCUTE_N_RETRY
168 #define CONFIG_EMCUTE_N_RETRY (3U)
169 #endif
182 enum {
183  EMCUTE_DUP = 0x80,
185  EMCUTE_QOS_2 = 0x40,
186  EMCUTE_QOS_1 = 0x20,
187  EMCUTE_QOS_0 = 0x00,
188  EMCUTE_RETAIN = 0x10,
189  EMCUTE_WILL = 0x08,
190  EMCUTE_CS = 0x04,
194  EMCUTE_TIT_NORMAL = 0x00
195 };
196 
200 enum {
201  EMCUTE_OK = 0,
202  EMCUTE_NOGW = -1,
206  EMCUTE_NOTSUP = -5
207 };
208 
212 typedef struct {
213  const char *name;
214  uint16_t id;
216 
224 typedef void(*emcute_cb_t)(const emcute_topic_t *topic, void *data, size_t len);
225 
229 typedef struct emcute_sub {
230  struct emcute_sub *next;
233  void *arg;
235 
257 int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic,
258  const void *will_msg, size_t will_msg_len, unsigned flags);
259 
267 int emcute_discon(void);
268 
281 
298 int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len,
299  unsigned flags);
300 
318 int emcute_sub(emcute_sub_t *sub, unsigned flags);
319 
330 
344 int emcute_willupd_topic(const char *topic, unsigned flags);
345 
359 int emcute_willupd_msg(const void *data, size_t len);
360 
370 void emcute_run(uint16_t port, const char *id);
371 
382 const char *emcute_type_str(uint8_t type);
383 
384 #ifdef __cplusplus
385 }
386 #endif
387 
int emcute_willupd_topic(const char *topic, unsigned flags)
Update the last will topic.
int emcute_discon(void)
Disconnect from the gateway we are currently connected to.
int emcute_willupd_msg(const void *data, size_t len)
Update the last will message.
int emcute_unsub(emcute_sub_t *sub)
Unsubscripbe the given topic.
int emcute_reg(emcute_topic_t *topic)
Get a topic ID for the given topic name from the gateway.
const char * emcute_type_str(uint8_t type)
Return the string representation of the given type value.
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:224
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_pub(emcute_topic_t *topic, const void *buf, size_t len, unsigned flags)
Publish data on the given topic.
int emcute_sub(emcute_sub_t *sub, unsigned flags)
Subscribe to the given topic.
struct emcute_sub emcute_sub_t
Data-structure for keeping track of topics we register to.
void emcute_run(uint16_t port, const char *id)
Run emCute, will 'occupy' the calling thread.
@ EMCUTE_CS
clean session flag
Definition: emcute.h:190
@ EMCUTE_RETAIN
retain flag
Definition: emcute.h:188
@ EMCUTE_QOS_2
QoS level 2.
Definition: emcute.h:185
@ EMCUTE_QOS_1
QoS level 1.
Definition: emcute.h:186
@ EMCUTE_DUP
duplicate flag
Definition: emcute.h:183
@ EMCUTE_QOS_MASK
QoS level mask.
Definition: emcute.h:184
@ EMCUTE_QOS_0
QoS level 0.
Definition: emcute.h:187
@ EMCUTE_TIT_NORMAL
topic ID: normal
Definition: emcute.h:194
@ EMCUTE_TIT_SHORT
topic ID: short
Definition: emcute.h:192
@ EMCUTE_TIT_MASK
topic ID type mask
Definition: emcute.h:191
@ EMCUTE_TIT_PREDEF
topic ID: pre-defined
Definition: emcute.h:193
@ EMCUTE_WILL
will flag, used during CONNECT
Definition: emcute.h:189
@ EMCUTE_NOGW
error: not connected to a gateway
Definition: emcute.h:202
@ EMCUTE_NOTSUP
error: feature not supported
Definition: emcute.h:206
@ EMCUTE_OVERFLOW
error: ran out of buffer space
Definition: emcute.h:204
@ EMCUTE_OK
everything went as expect
Definition: emcute.h:201
@ EMCUTE_TIMEOUT
error: timeout
Definition: emcute.h:205
@ EMCUTE_REJECT
error: operation was rejected by broker
Definition: emcute.h:203
UDP sock definitions.
Common IP-based transport layer end point.
Definition: sock.h:211
Data-structure for keeping track of topics we register to.
Definition: emcute.h:229
emcute_topic_t topic
topic we subscribe to
Definition: emcute.h:231
struct emcute_sub * next
next subscription (saved in a list)
Definition: emcute.h:230
emcute_cb_t cb
function called when receiving messages
Definition: emcute.h:232
void * arg
optional custom argument
Definition: emcute.h:233
MQTT-SN topic.
Definition: emcute.h:212
const char * name
topic string (currently ASCII only)
Definition: emcute.h:213
uint16_t id
topic id, as assigned by the gateway
Definition: emcute.h:214