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 
9 #pragma once
10 
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 
111 #ifndef CONFIG_EMCUTE_DEFAULT_PORT
112 #define CONFIG_EMCUTE_DEFAULT_PORT (1883U)
113 #endif
114 
123 #ifndef CONFIG_EMCUTE_BUFSIZE
124 #define CONFIG_EMCUTE_BUFSIZE (512U)
125 #endif
126 
132 #ifndef CONFIG_EMCUTE_TOPIC_MAXLEN
133 #define CONFIG_EMCUTE_TOPIC_MAXLEN (196U)
134 #endif
135 
143 #ifndef CONFIG_EMCUTE_KEEPALIVE
144 #define CONFIG_EMCUTE_KEEPALIVE (360) /* -> 6 min*/
145 #endif
146 
157 #ifndef CONFIG_EMCUTE_T_RETRY
158 #define CONFIG_EMCUTE_T_RETRY (15U) /* -> 15 sec */
159 #endif
160 
170 #ifndef CONFIG_EMCUTE_N_RETRY
171 #define CONFIG_EMCUTE_N_RETRY (3U)
172 #endif
185 enum {
186  EMCUTE_DUP = 0x80,
188  EMCUTE_QOS_2 = 0x40,
189  EMCUTE_QOS_1 = 0x20,
190  EMCUTE_QOS_0 = 0x00,
191  EMCUTE_RETAIN = 0x10,
192  EMCUTE_WILL = 0x08,
193  EMCUTE_CS = 0x04,
197  EMCUTE_TIT_NORMAL = 0x00
198 };
199 
203 enum {
204  EMCUTE_OK = 0,
205  EMCUTE_NOGW = -1,
209  EMCUTE_NOTSUP = -5
210 };
211 
215 typedef struct {
216  const char *name;
217  uint16_t id;
219 
227 typedef void(*emcute_cb_t)(const emcute_topic_t *topic, void *data, size_t len);
228 
232 typedef struct emcute_sub {
233  struct emcute_sub *next;
236  void *arg;
238 
260 int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic,
261  const void *will_msg, size_t will_msg_len, unsigned flags);
262 
270 int emcute_discon(void);
271 
284 
301 int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len,
302  unsigned flags);
303 
321 int emcute_sub(emcute_sub_t *sub, unsigned flags);
322 
333 
347 int emcute_willupd_topic(const char *topic, unsigned flags);
348 
362 int emcute_willupd_msg(const void *data, size_t len);
363 
373 void emcute_run(uint16_t port, const char *id);
374 
385 const char *emcute_type_str(uint8_t type);
386 
387 #ifdef __cplusplus
388 }
389 #endif
390 
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:227
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_NOGW
error: not connected to a gateway
Definition: emcute.h:205
@ EMCUTE_NOTSUP
error: feature not supported
Definition: emcute.h:209
@ EMCUTE_OVERFLOW
error: ran out of buffer space
Definition: emcute.h:207
@ EMCUTE_OK
everything went as expect
Definition: emcute.h:204
@ EMCUTE_TIMEOUT
error: timeout
Definition: emcute.h:208
@ EMCUTE_REJECT
error: operation was rejected by broker
Definition: emcute.h:206
@ EMCUTE_CS
clean session flag
Definition: emcute.h:193
@ EMCUTE_RETAIN
retain flag
Definition: emcute.h:191
@ EMCUTE_QOS_2
QoS level 2.
Definition: emcute.h:188
@ EMCUTE_QOS_1
QoS level 1.
Definition: emcute.h:189
@ EMCUTE_DUP
duplicate flag
Definition: emcute.h:186
@ EMCUTE_QOS_MASK
QoS level mask.
Definition: emcute.h:187
@ EMCUTE_QOS_0
QoS level 0.
Definition: emcute.h:190
@ EMCUTE_TIT_NORMAL
topic ID: normal
Definition: emcute.h:197
@ EMCUTE_TIT_SHORT
topic ID: short
Definition: emcute.h:195
@ EMCUTE_TIT_MASK
topic ID type mask
Definition: emcute.h:194
@ EMCUTE_TIT_PREDEF
topic ID: pre-defined
Definition: emcute.h:196
@ EMCUTE_WILL
will flag, used during CONNECT
Definition: emcute.h:192
UDP sock definitions.
Common IP-based transport layer end point.
Definition: sock.h:214
Data-structure for keeping track of topics we register to.
Definition: emcute.h:232
emcute_topic_t topic
topic we subscribe to
Definition: emcute.h:234
struct emcute_sub * next
next subscription (saved in a list)
Definition: emcute.h:233
emcute_cb_t cb
function called when receiving messages
Definition: emcute.h:235
void * arg
optional custom argument
Definition: emcute.h:236
MQTT-SN topic.
Definition: emcute.h:215
const char * name
topic string (currently ASCII only)
Definition: emcute.h:216
uint16_t id
topic id, as assigned by the gateway
Definition: emcute.h:217