Loading...
Searching...
No Matches
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
44#ifndef NET_ASYMCUTE_H
45#define NET_ASYMCUTE_H
46
47#include <stdint.h>
48#include <stddef.h>
49#include <stdbool.h>
50
51#include "assert.h"
52#include "event/timeout.h"
53#include "event/callback.h"
54#include "net/mqttsn.h"
55#include "net/sock/udp.h"
56#include "net/sock/util.h"
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
77#ifndef CONFIG_ASYMCUTE_DEFAULT_PORT
78#define CONFIG_ASYMCUTE_DEFAULT_PORT (1883U)
79#endif
80
84#ifndef CONFIG_ASYMCUTE_BUFSIZE
85#define CONFIG_ASYMCUTE_BUFSIZE (128U)
86#endif
87
93#ifndef CONFIG_ASYMCUTE_TOPIC_MAXLEN
94#define CONFIG_ASYMCUTE_TOPIC_MAXLEN (32U)
95#endif
96
104#ifndef CONFIG_ASYMCUTE_KEEPALIVE
105#define CONFIG_ASYMCUTE_KEEPALIVE (360)
106#endif
107
116#ifndef CONFIG_ASYMCUTE_KEEPALIVE_PING
117#define CONFIG_ASYMCUTE_KEEPALIVE_PING ((CONFIG_ASYMCUTE_KEEPALIVE / 4) * 3)
118#endif
119
130#ifndef CONFIG_ASYMCUTE_T_RETRY
131#define CONFIG_ASYMCUTE_T_RETRY (10U)
132#endif
133
143#ifndef CONFIG_ASYMCUTE_N_RETRY
144#define CONFIG_ASYMCUTE_N_RETRY (3U)
145#endif
148#ifndef ASYMCUTE_HANDLER_PRIO
152#define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
153#endif
154
155#ifndef ASYMCUTE_HANDLER_STACKSIZE
159#define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
160#endif
161
165enum {
174};
175
179enum {
189};
190
195
200
205
210
215
224typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
225
235typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
236 const void *data, size_t len, void *arg);
237
248typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
249
266
285
295
305
310 const char *topic;
311 void *msg;
312 size_t msg_len;
313};
314
323static inline bool asymcute_req_in_use(const asymcute_req_t *req)
324{
325 assert(req);
326 return (req->con != NULL);
327}
328
337static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
338{
339 assert(sub);
340 return (sub->topic != NULL);
341}
342
351static inline void asymcute_topic_reset(asymcute_topic_t *topic)
352{
353 assert(topic);
354 memset(topic, 0, sizeof(asymcute_topic_t));
355}
356
365static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
366{
367 assert(topic);
368 return (topic->con != NULL);
369}
370
379static inline bool asymcute_topic_is_short(const asymcute_topic_t *topic)
380{
381 assert(topic);
382 return ((topic->flags & MQTTSN_TIT_SHORT) != 0);
383}
384
393static inline bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
394{
395 assert(topic);
396 return ((topic->flags & MQTTSN_TIT_PREDEF) != 0);
397}
398
407static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
408{
409 assert(topic);
410 return (topic->name[0] != '\0');
411}
412
422static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
423 const asymcute_topic_t *b)
424{
425 assert(a);
426 assert(b);
427
428 return ((a->flags == b->flags) && (a->id == b->id));
429}
430
445int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
446 uint16_t topic_id);
447
455
465
484 sock_udp_ep_t *server, const char *cli_id, bool clean,
485 asymcute_will_t *will, asymcute_evt_cb_t callback);
486
498
512 asymcute_topic_t *topic);
513
532 const asymcute_topic_t *topic,
533 const void *data, size_t data_len, uint8_t flags);
534
555 asymcute_sub_t *sub, asymcute_topic_t *topic,
556 asymcute_sub_cb_t callback, void *arg, uint8_t flags);
557
571 asymcute_sub_t *sub);
572
573#ifdef __cplusplus
574}
575#endif
576
577#endif /* NET_ASYMCUTE_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
Provides a callback-with-argument event type.
Provides functionality to trigger events after timeout.
#define CONFIG_ASYMCUTE_BUFSIZE
Default buffer size used for receive and request buffers.
Definition asymcute.h:85
#define CONFIG_ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition asymcute.h:94
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition asymcute.h:248
int asymcute_unsubscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub)
Cancel an active subscription.
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, asymcute_evt_cb_t callback)
Connect to the given MQTT-SN gateway.
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:422
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition asymcute.h:351
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.
void asymcute_handler_run(void)
Start the global Asymcute handler thread for processing timeouts and keep alive events.
bool asymcute_is_connected(const asymcute_con_t *con)
Check if the given connection context is connected to a gateway.
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:224
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition asymcute.h:407
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:365
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.
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition asymcute.h:337
static bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
Check if a given topic is a pre-defined topic.
Definition asymcute.h:393
static bool asymcute_topic_is_short(const asymcute_topic_t *topic)
Check if a given topic is a short topic.
Definition asymcute.h:379
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition asymcute.h:323
int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name, uint16_t topic_id)
Initialize the given topic.
int asymcute_register(asymcute_con_t *con, asymcute_req_t *req, asymcute_topic_t *topic)
Register a given topic with the connected gateway.
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:235
int asymcute_disconnect(asymcute_con_t *con, asymcute_req_t *req)
Close the given connection.
@ ASYMCUTE_CONNECTED
connected to gateway
Definition asymcute.h:183
@ ASYMCUTE_SUBSCRIBED
client was subscribed to topic
Definition asymcute.h:187
@ ASYMCUTE_DISCONNECTED
connection got disconnected
Definition asymcute.h:184
@ ASYMCUTE_UNSUBSCRIBED
client was unsubscribed from topic
Definition asymcute.h:188
@ ASYMCUTE_REJECTED
request was rejected
Definition asymcute.h:182
@ ASYMCUTE_REGISTERED
topic was registered
Definition asymcute.h:185
@ ASYMCUTE_CANCELED
request was canceled
Definition asymcute.h:181
@ ASYMCUTE_PUBLISHED
data was published
Definition asymcute.h:186
@ ASYMCUTE_TIMEOUT
request timed out
Definition asymcute.h:180
@ ASYMCUTE_NOTSUP
error: feature not supported
Definition asymcute.h:169
@ ASYMCUTE_GWERR
error: bad gateway connection state
Definition asymcute.h:168
@ ASYMCUTE_SUBERR
error: subscription invalid
Definition asymcute.h:172
@ ASYMCUTE_OVERFLOW
error: insufficient buffer space
Definition asymcute.h:167
@ ASYMCUTE_SENDERR
error: unable to sent packet
Definition asymcute.h:173
@ ASYMCUTE_BUSY
error: context already in use
Definition asymcute.h:170
@ ASYMCUTE_REGERR
error: registration invalid
Definition asymcute.h:171
@ ASYMCUTE_OK
all is good
Definition asymcute.h:166
@ MQTTSN_TIT_SHORT
topic ID: short
Definition mqttsn.h:59
@ MQTTSN_TIT_PREDEF
topic ID: pre-defined
Definition mqttsn.h:60
Generic MQTT-SN definitions.
UDP sock definitions.
Common IP-based transport layer end point.
Definition sock.h:215
Asymcute connection context.
Definition asymcute.h:270
sock_udp_t sock
socket used by a connections
Definition asymcute.h:272
event_callback_t keepalive_evt
keep alive event
Definition asymcute.h:276
asymcute_evt_cb_t user_cb
event callback provided by user
Definition asymcute.h:275
uint8_t rxbuf[CONFIG_ASYMCUTE_BUFSIZE]
connection specific receive buf
Definition asymcute.h:282
asymcute_req_t * pending
list holding pending requests
Definition asymcute.h:273
char cli_id[MQTTSN_CLI_ID_MAXLEN+1]
buffer to store client ID
Definition asymcute.h:283
event_timeout_t keepalive_timer
keep alive timer
Definition asymcute.h:277
mutex_t lock
synchronization lock
Definition asymcute.h:271
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition asymcute.h:280
uint16_t last_id
last used message ID for this connection
Definition asymcute.h:278
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition asymcute.h:274
uint8_t state
connection state
Definition asymcute.h:281
Asymcute request context.
Definition asymcute.h:253
asymcute_con_t * con
connection the request is using
Definition asymcute.h:256
mutex_t lock
synchronization lock
Definition asymcute.h:254
asymcute_to_cb_t cb
internally used callback
Definition asymcute.h:257
event_timeout_t to_timer
timeout timer
Definition asymcute.h:260
uint8_t retry_cnt
retransmission counter
Definition asymcute.h:264
void * arg
internally used additional state
Definition asymcute.h:258
event_callback_t to_evt
timeout event
Definition asymcute.h:259
size_t data_len
length of the request packet in byte
Definition asymcute.h:262
uint16_t msg_id
used message id for this request
Definition asymcute.h:263
uint8_t data[CONFIG_ASYMCUTE_BUFSIZE]
buffer holding the request's data
Definition asymcute.h:261
struct asymcute_req * next
the requests list entry
Definition asymcute.h:255
Data-structure holding the state of subscriptions.
Definition asymcute.h:299
asymcute_sub_t * next
the subscriptions list entry
Definition asymcute.h:300
asymcute_sub_cb_t cb
called on incoming data
Definition asymcute.h:302
asymcute_topic_t * topic
topic we subscribe to
Definition asymcute.h:301
void * arg
user supplied callback argument
Definition asymcute.h:303
Data-structure for holding topics and their registration status.
Definition asymcute.h:289
uint8_t flags
normal, short, or pre-defined
Definition asymcute.h:292
asymcute_con_t * con
connection used for registration
Definition asymcute.h:290
char name[CONFIG_ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ASCII only)
Definition asymcute.h:291
uint16_t id
topic id
Definition asymcute.h:293
Data structure for defining a last will.
Definition asymcute.h:309
size_t msg_len
length of last will message content
Definition asymcute.h:312
void * msg
last will message content
Definition asymcute.h:311
const char * topic
last will topic
Definition asymcute.h:310
Callback Event structure definition.
Definition callback.h:48
Timeout Event structure.
Definition timeout.h:50
Mutex structure.
Definition mutex.h:146
UDP sock type.
Definition sock_types.h:128
sock utility function definitions