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_TOPIC_MAXLEN
118 
124 #define EMCUTE_TOPIC_MAXLEN (196U)
125 #endif
126 
127 #ifndef EMCUTE_KEEPALIVE
128 
136 #define EMCUTE_KEEPALIVE (360) /* -> 6 min*/
137 #endif
138 
139 #ifndef EMCUTE_T_RETRY
140 
145 #define EMCUTE_T_RETRY (15U) /* -> 15 sec */
146 #endif
147 
148 #ifndef EMCUTE_N_RETRY
149 
154 #define EMCUTE_N_RETRY (3U)
155 #endif
156 
167 enum {
168  EMCUTE_DUP = 0x80,
170  EMCUTE_QOS_2 = 0x40,
171  EMCUTE_QOS_1 = 0x20,
172  EMCUTE_QOS_0 = 0x00,
173  EMCUTE_RETAIN = 0x10,
174  EMCUTE_WILL = 0x08,
175  EMCUTE_CS = 0x04,
180 };
181 
185 enum {
186  EMCUTE_OK = 0,
187  EMCUTE_NOGW = -1,
192 };
193 
197 typedef struct {
198  const char *name;
199  uint16_t id;
201 
209 typedef void(*emcute_cb_t)(const emcute_topic_t *topic, void *data, size_t len);
210 
214 typedef struct emcute_sub {
215  struct emcute_sub *next;
218  void *arg;
219 } emcute_sub_t;
220 
242 int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic,
243  const void *will_msg, size_t will_msg_len, unsigned flags);
244 
252 int emcute_discon(void);
253 
266 
283 int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len,
284  unsigned flags);
285 
303 int emcute_sub(emcute_sub_t *sub, unsigned flags);
304 
314 int emcute_unsub(emcute_sub_t *sub);
315 
329 int emcute_willupd_topic(const char *topic, unsigned flags);
330 
344 int emcute_willupd_msg(const void *data, size_t len);
345 
355 void emcute_run(uint16_t port, const char *id);
356 
367 const char *emcute_type_str(uint8_t type);
368 
369 #ifdef __cplusplus
370 }
371 #endif
372 
373 #endif /* NET_EMCUTE_H */
374 
EMCUTE_RETAIN
@ EMCUTE_RETAIN
retain flag
Definition: emcute.h:173
EMCUTE_TIMEOUT
@ EMCUTE_TIMEOUT
error: timeout
Definition: emcute.h:190
EMCUTE_OVERFLOW
@ EMCUTE_OVERFLOW
error: ran out of buffer space
Definition: emcute.h:189
emcute_sub
Data-structure for keeping track of topics we register to.
Definition: emcute.h:214
emcute_willupd_topic
int emcute_willupd_topic(const char *topic, unsigned flags)
Update the last will topic.
udp.h
UDP sock definitions.
emcute_topic_t::name
const char * name
topic string (currently ACSII only)
Definition: emcute.h:198
EMCUTE_DUP
@ EMCUTE_DUP
duplicate flag
Definition: emcute.h:168
emcute_sub::cb
emcute_cb_t cb
function called when receiving messages
Definition: emcute.h:217
emcute_sub
int emcute_sub(emcute_sub_t *sub, unsigned flags)
Subscribe to the given topic.
emcute_sub_t
struct emcute_sub emcute_sub_t
Data-structure for keeping track of topics we register to.
EMCUTE_REJECT
@ EMCUTE_REJECT
error: operation was rejected by broker
Definition: emcute.h:188
emcute_willupd_msg
int emcute_willupd_msg(const void *data, size_t len)
Update the last will message.
emcute_run
void emcute_run(uint16_t port, const char *id)
Run emCute, will 'occupy' the calling thread.
emcute_pub
int emcute_pub(emcute_topic_t *topic, const void *buf, size_t len, unsigned flags)
Publish data on the given topic.
_sock_tl_ep
Common IP-based transport layer end point.
Definition: sock.h:213
EMCUTE_QOS_2
@ EMCUTE_QOS_2
QoS level 2.
Definition: emcute.h:170
EMCUTE_TIT_MASK
@ EMCUTE_TIT_MASK
topic ID type mask
Definition: emcute.h:176
EMCUTE_WILL
@ EMCUTE_WILL
will flag, used during CONNECT
Definition: emcute.h:174
emcute_topic_t::id
uint16_t id
topic id, as assigned by the gateway
Definition: emcute.h:199
EMCUTE_NOGW
@ EMCUTE_NOGW
error: not connected to a gateway
Definition: emcute.h:187
emcute_sub::topic
emcute_topic_t topic
topic we subscribe to
Definition: emcute.h:216
EMCUTE_QOS_0
@ EMCUTE_QOS_0
QoS level 0.
Definition: emcute.h:172
EMCUTE_NOTSUP
@ EMCUTE_NOTSUP
error: feature not supported
Definition: emcute.h:191
emcute_reg
int emcute_reg(emcute_topic_t *topic)
Get a topic ID for the given topic name from the gateway.
emcute_cb_t
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:209
EMCUTE_TIT_SHORT
@ EMCUTE_TIT_SHORT
topic ID: short
Definition: emcute.h:177
EMCUTE_QOS_MASK
@ EMCUTE_QOS_MASK
QoS level mask.
Definition: emcute.h:169
emcute_discon
int emcute_discon(void)
Disconnect from the gateway we are currently connected to.
EMCUTE_TIT_NORMAL
@ EMCUTE_TIT_NORMAL
topic ID: normal
Definition: emcute.h:179
EMCUTE_OK
@ EMCUTE_OK
everything went as expect
Definition: emcute.h:186
emcute_con
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)
EMCUTE_TIT_PREDEF
@ EMCUTE_TIT_PREDEF
topic ID: pre-defined
Definition: emcute.h:178
EMCUTE_QOS_1
@ EMCUTE_QOS_1
QoS level 1.
Definition: emcute.h:171
emcute_sub::arg
void * arg
optional custom argument
Definition: emcute.h:218
emcute_type_str
const char * emcute_type_str(uint8_t type)
Return the string representation of the given type value.
emcute_sub::next
struct emcute_sub * next
next subscription (saved in a list)
Definition: emcute.h:215
emcute_unsub
int emcute_unsub(emcute_sub_t *sub)
Unsubscripbe the given topic.
EMCUTE_CS
@ EMCUTE_CS
clean session flag
Definition: emcute.h:175
emcute_topic_t
MQTT-SN topic.
Definition: emcute.h:197