asymcute.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 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 
43 #ifndef NET_ASYMCUTE_H
44 #define NET_ASYMCUTE_H
45 
46 #include <stdint.h>
47 #include <stddef.h>
48 #include <stdbool.h>
49 
50 #include "assert.h"
51 #include "event/timeout.h"
52 #include "event/callback.h"
53 #include "net/mqttsn.h"
54 #include "net/sock/udp.h"
55 #include "net/sock/util.h"
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
72 #ifndef CONFIG_ASYMCUTE_BUFSIZE_EXP
73 #define CONFIG_ASYMCUTE_BUFSIZE_EXP (7U)
74 #endif
75 
81 #ifndef CONFIG_ASYMCUTE_TOPIC_MAXLEN
82 #define CONFIG_ASYMCUTE_TOPIC_MAXLEN (32U)
83 #endif
84 
92 #ifndef CONFIG_ASYMCUTE_KEEPALIVE
93 #define CONFIG_ASYMCUTE_KEEPALIVE (360)
94 #endif
95 
104 #ifndef CONFIG_ASYMCUTE_KEEPALIVE_PING
105 #define CONFIG_ASYMCUTE_KEEPALIVE_PING ((CONFIG_ASYMCUTE_KEEPALIVE / 4) * 3)
106 #endif
107 
118 #ifndef CONFIG_ASYMCUTE_T_RETRY
119 #define CONFIG_ASYMCUTE_T_RETRY (10U)
120 #endif
121 
131 #ifndef CONFIG_ASYMCUTE_N_RETRY
132 #define CONFIG_ASYMCUTE_N_RETRY (3U)
133 #endif
134 
136 #ifndef ASYMCUTE_BUFSIZE
137 
140 #define ASYMCUTE_BUFSIZE (1 << CONFIG_ASYMCUTE_BUFSIZE_EXP)
141 #endif
142 
143 #ifndef ASYMCUTE_HANDLER_PRIO
144 
147 #define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
148 #endif
149 
150 #ifndef ASYMCUTE_HANDLER_STACKSIZE
151 
154 #define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
155 #endif
156 
157 #ifndef ASYMCUTE_LISTENER_PRIO
158 
163 #define ASYMCUTE_LISTENER_PRIO (THREAD_PRIORITY_MAIN - 3)
164 #endif
165 
166 #ifndef ASYMCUTE_LISTENER_STACKSIZE
167 
170 #define ASYMCUTE_LISTENER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
171 #endif
172 
176 enum {
184 };
185 
189 enum {
199 };
200 
205 
210 
215 
220 
225 
234 typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
235 
245 typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
246  const void *data, size_t len, void *arg);
247 
258 typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
259 
263 struct asymcute_req {
265  struct asymcute_req *next;
268  void *arg;
272  size_t data_len;
273  uint16_t msg_id;
274  uint8_t retry_cnt;
275 };
276 
280 struct asymcute_con {
289  uint16_t last_id;
292  uint8_t state;
294  char cli_id[MQTTSN_CLI_ID_MAXLEN + 1];
295 };
296 
303  uint8_t flags;
304  uint16_t id;
305 };
306 
310 struct asymcute_sub {
314  void *arg;
315 };
316 
321  const char *topic;
322  void *msg;
323  size_t msg_len;
324 };
325 
334 static inline bool asymcute_req_in_use(const asymcute_req_t *req)
335 {
336  assert(req);
337  return (req->con != NULL);
338 }
339 
348 static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
349 {
350  assert(sub);
351  return (sub->topic != NULL);
352 }
353 
362 static inline void asymcute_topic_reset(asymcute_topic_t *topic)
363 {
364  assert(topic);
365  memset(topic, 0, sizeof(asymcute_topic_t));
366 }
367 
376 static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
377 {
378  assert(topic);
379  return (topic->con != NULL);
380 }
381 
390 static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
391 {
392  assert(topic);
393  return (topic->name[0] != '\0');
394 }
395 
405 static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
406  const asymcute_topic_t *b)
407 {
408  assert(a);
409  assert(b);
410 
411  return ((a->flags == b->flags) && (a->id == b->id));
412 }
413 
428 int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
429  uint16_t topic_id);
430 
447 int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize,
448  char priority, asymcute_evt_cb_t callback);
449 
456 void asymcute_handler_run(void);
457 
466 bool asymcute_is_connected(const asymcute_con_t *con);
467 
485  sock_udp_ep_t *server, const char *cli_id, bool clean,
486  asymcute_will_t *will);
487 
499 
513  asymcute_topic_t *topic);
514 
533  const asymcute_topic_t *topic,
534  const void *data, size_t data_len, uint8_t flags);
535 
556  asymcute_sub_t *sub, asymcute_topic_t *topic,
557  asymcute_sub_cb_t callback, void *arg, uint8_t flags);
558 
572  asymcute_sub_t *sub);
573 
574 #ifdef __cplusplus
575 }
576 #endif
577 
578 #endif /* NET_ASYMCUTE_H */
579 
asymcute_will
Data structure for defining a last will.
Definition: asymcute.h:320
mqttsn.h
Generic MQTT-SN definitions.
ASYMCUTE_CANCELED
@ ASYMCUTE_CANCELED
request was canceled
Definition: asymcute.h:191
asymcute_to_cb_t
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition: asymcute.h:258
ASYMCUTE_BUSY
@ ASYMCUTE_BUSY
error: context already in use
Definition: asymcute.h:181
asymcute_topic::flags
uint8_t flags
normal, short, or pre-defined
Definition: asymcute.h:303
asymcute_subscribe
int asymcute_subscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub, asymcute_topic_t *topic, asymcute_sub_cb_t callback, void *arg, uint8_t flags)
Subscribe to a given topic.
asymcute_will::msg_len
size_t msg_len
length of last will message content
Definition: asymcute.h:323
asymcute_disconnect
int asymcute_disconnect(asymcute_con_t *con, asymcute_req_t *req)
Close the given connection.
event_timeout_t
Timeout Event structure.
Definition: timeout.h:50
asymcute_con::subscriptions
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition: asymcute.h:285
asymcute_con::keepalive_retry_cnt
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition: asymcute.h:291
udp.h
UDP sock definitions.
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
asymcute_register
int asymcute_register(asymcute_con_t *con, asymcute_req_t *req, asymcute_topic_t *topic)
Register a given topic with the connected gateway.
ASYMCUTE_PUBLISHED
@ ASYMCUTE_PUBLISHED
data was published
Definition: asymcute.h:196
asymcute_con::server_ep
sock_udp_ep_t server_ep
the gateway's UDP endpoint
Definition: asymcute.h:283
asymcute_unsubscribe
int asymcute_unsubscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub)
Cancel an active subscription.
asymcute_sub::cb
asymcute_sub_cb_t cb
called on incoming data
Definition: asymcute.h:313
assert.h
POSIX.1-2008 compliant version of the assert macro.
timeout.h
Provides functionality to trigger events after timeout.
asymcute_req::to_timer
event_timeout_t to_timer
timeout timer
Definition: asymcute.h:270
ASYMCUTE_DISCONNECTED
@ ASYMCUTE_DISCONNECTED
connection got disconnected
Definition: asymcute.h:194
callback.h
Provides a callback-with-argument event type.
asymcute_req_in_use
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition: asymcute.h:334
asymcute_con::state
uint8_t state
connection state
Definition: asymcute.h:292
asymcute_topic_is_reg
static bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
Check if a given topic is currently registered with a gateway.
Definition: asymcute.h:376
ASYMCUTE_UNSUBSCRIBED
@ ASYMCUTE_UNSUBSCRIBED
client was unsubscribed from topic
Definition: asymcute.h:198
ASYMCUTE_SUBSCRIBED
@ ASYMCUTE_SUBSCRIBED
client was subscribed to topic
Definition: asymcute.h:197
asymcute_topic::name
char name[CONFIG_ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ACSII only)
Definition: asymcute.h:302
ASYMCUTE_CONNECTED
@ ASYMCUTE_CONNECTED
connected to gateway
Definition: asymcute.h:193
event_callback_t
Callback Event structure definition.
Definition: callback.h:48
asymcute_topic_init
int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name, uint16_t topic_id)
Initialize the given topic.
ASYMCUTE_OK
@ ASYMCUTE_OK
all is good
Definition: asymcute.h:177
asymcute_req::lock
mutex_t lock
synchronization lock
Definition: asymcute.h:264
asymcute_topic::id
uint16_t id
topic id
Definition: asymcute.h:304
asymcute_con::cli_id
char cli_id[MQTTSN_CLI_ID_MAXLEN+1]
buffer to store client ID
Definition: asymcute.h:294
asymcute_will::msg
void * msg
last will message content
Definition: asymcute.h:322
asymcute_sub::next
asymcute_sub_t * next
the subscriptions list entry
Definition: asymcute.h:311
asymcute_req::cb
asymcute_to_cb_t cb
internally used callback
Definition: asymcute.h:267
asymcute_req::next
struct asymcute_req * next
the requests list entry
Definition: asymcute.h:265
asymcute_req::data_len
size_t data_len
length of the request packet in byte
Definition: asymcute.h:272
asymcute_topic::con
asymcute_con_t * con
connection used for registration
Definition: asymcute.h:301
asymcute_req::data
uint8_t data[ASYMCUTE_BUFSIZE]
buffer holding the request's data
Definition: asymcute.h:271
asymcute_sub::topic
asymcute_topic_t * topic
topic we subscribe to
Definition: asymcute.h:312
asymcute_req
Asymcute request context.
Definition: asymcute.h:263
asymcute_topic_equal
static bool asymcute_topic_equal(const asymcute_topic_t *a, const asymcute_topic_t *b)
Compare two given topics and check if they are equal.
Definition: asymcute.h:405
asymcute_con::keepalive_timer
event_timeout_t keepalive_timer
keep alive timer
Definition: asymcute.h:288
sock_udp
UDP sock type.
Definition: sock_types.h:128
ASYMCUTE_REGERR
@ ASYMCUTE_REGERR
error: registration invalid
Definition: asymcute.h:182
asymcute_topic_is_init
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition: asymcute.h:390
_sock_tl_ep
Common IP-based transport layer end point.
Definition: sock.h:213
asymcute_con
Asymcute connection context.
Definition: asymcute.h:280
asymcute_req::retry_cnt
uint8_t retry_cnt
retransmission counter
Definition: asymcute.h:274
asymcute_req::arg
void * arg
internally used additional state
Definition: asymcute.h:268
asymcute_sub_active
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition: asymcute.h:348
asymcute_con::lock
mutex_t lock
synchronization lock
Definition: asymcute.h:281
asymcute_topic_reset
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition: asymcute.h:362
ASYMCUTE_OVERFLOW
@ ASYMCUTE_OVERFLOW
error: insufficient buffer space
Definition: asymcute.h:178
asymcute_con::last_id
uint16_t last_id
last used message ID for this connection
Definition: asymcute.h:289
asymcute_req::msg_id
uint16_t msg_id
used message id for this request
Definition: asymcute.h:273
asymcute_sub_cb_t
void(* asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type, const void *data, size_t len, void *arg)
Callback triggered on events for active subscriptions.
Definition: asymcute.h:245
asymcute_handler_run
void asymcute_handler_run(void)
Start the global Asymcute handler thread for processing timeouts and keep alive events.
ASYMCUTE_REGISTERED
@ ASYMCUTE_REGISTERED
topic was registered
Definition: asymcute.h:195
asymcute_con::user_cb
asymcute_evt_cb_t user_cb
event callback provided by user
Definition: asymcute.h:286
ASYMCUTE_GWERR
@ ASYMCUTE_GWERR
error: bad gateway connection state
Definition: asymcute.h:179
ASYMCUTE_NOTSUP
@ ASYMCUTE_NOTSUP
error: feature not supported
Definition: asymcute.h:180
asymcute_publish
int asymcute_publish(asymcute_con_t *con, asymcute_req_t *req, const asymcute_topic_t *topic, const void *data, size_t data_len, uint8_t flags)
Publish the given data to the given topic.
asymcute_con::keepalive_evt
event_callback_t keepalive_evt
keep alive event
Definition: asymcute.h:287
asymcute_con::rxbuf
uint8_t rxbuf[ASYMCUTE_BUFSIZE]
connection specific receive buf
Definition: asymcute.h:293
ASYMCUTE_REJECTED
@ ASYMCUTE_REJECTED
request was rejected
Definition: asymcute.h:192
asymcute_topic
Data-structure for holding topics and their registration status.
Definition: asymcute.h:300
asymcute_evt_cb_t
void(* asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type)
Event callback used for communicating connection and request related events to the user.
Definition: asymcute.h:234
asymcute_sub::arg
void * arg
user supplied callback argument
Definition: asymcute.h:314
asymcute_req::to_evt
event_callback_t to_evt
timeout event
Definition: asymcute.h:269
asymcute_is_connected
bool asymcute_is_connected(const asymcute_con_t *con)
Check if the given connection context is connected to a gateway.
asymcute_sub
Data-structure holding the state of subscriptions.
Definition: asymcute.h:310
asymcute_will::topic
const char * topic
last will topic
Definition: asymcute.h:321
asymcute_connect
int asymcute_connect(asymcute_con_t *con, asymcute_req_t *req, sock_udp_ep_t *server, const char *cli_id, bool clean, asymcute_will_t *will)
Connect to the given MQTT-SN gateway.
ASYMCUTE_BUFSIZE
#define ASYMCUTE_BUFSIZE
Default buffer size used for receive and request buffers.
Definition: asymcute.h:140
asymcute_con::pending
asymcute_req_t * pending
list holding pending requests
Definition: asymcute.h:284
util.h
sock utility function definitions
CONFIG_ASYMCUTE_TOPIC_MAXLEN
#define CONFIG_ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition: asymcute.h:82
asymcute_con::sock
sock_udp_t sock
socket used by a connections
Definition: asymcute.h:282
ASYMCUTE_TIMEOUT
@ ASYMCUTE_TIMEOUT
request timed out
Definition: asymcute.h:190
asymcute_req::con
asymcute_con_t * con
connection the request is using
Definition: asymcute.h:266
mutex_t
Mutex structure.
Definition: mutex.h:120
ASYMCUTE_SUBERR
@ ASYMCUTE_SUBERR
error: subscription invalid
Definition: asymcute.h:183
asymcute_listener_run
int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize, char priority, asymcute_evt_cb_t callback)
Start a listener thread.