Loading...
Searching...
No Matches
asymcute.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
42
43#include <stdint.h>
44#include <stddef.h>
45#include <stdbool.h>
46
47#include "assert.h"
48#include "event/timeout.h"
49#include "event/callback.h"
50#include "net/mqttsn.h"
51#include "net/sock/udp.h"
52#include "net/sock/util.h"
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
73#ifndef CONFIG_ASYMCUTE_DEFAULT_PORT
74#define CONFIG_ASYMCUTE_DEFAULT_PORT (1883U)
75#endif
76
80#ifndef CONFIG_ASYMCUTE_BUFSIZE
81#define CONFIG_ASYMCUTE_BUFSIZE (128U)
82#endif
83
89#ifndef CONFIG_ASYMCUTE_TOPIC_MAXLEN
90#define CONFIG_ASYMCUTE_TOPIC_MAXLEN (32U)
91#endif
92
100#ifndef CONFIG_ASYMCUTE_KEEPALIVE
101#define CONFIG_ASYMCUTE_KEEPALIVE (360)
102#endif
103
112#ifndef CONFIG_ASYMCUTE_KEEPALIVE_PING
113#define CONFIG_ASYMCUTE_KEEPALIVE_PING ((CONFIG_ASYMCUTE_KEEPALIVE / 4) * 3)
114#endif
115
126#ifndef CONFIG_ASYMCUTE_T_RETRY
127#define CONFIG_ASYMCUTE_T_RETRY (10U)
128#endif
129
139#ifndef CONFIG_ASYMCUTE_N_RETRY
140#define CONFIG_ASYMCUTE_N_RETRY (3U)
141#endif
143
144#ifndef ASYMCUTE_HANDLER_PRIO
148#define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
149#endif
150
151#ifndef ASYMCUTE_HANDLER_STACKSIZE
155#define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
156#endif
157
161enum {
170};
171
175enum {
185};
186
191
196
201
206
211
220typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
221
231typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
232 const void *data, size_t len, void *arg);
233
244typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
245
262
281
291
301
306 const char *topic;
307 void *msg;
308 size_t msg_len;
309};
310
319static inline bool asymcute_req_in_use(const asymcute_req_t *req)
320{
321 assert(req);
322 return (req->con != NULL);
323}
324
333static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
334{
335 assert(sub);
336 return (sub->topic != NULL);
337}
338
347static inline void asymcute_topic_reset(asymcute_topic_t *topic)
348{
349 assert(topic);
350 memset(topic, 0, sizeof(asymcute_topic_t));
351}
352
361static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
362{
363 assert(topic);
364 return (topic->con != NULL);
365}
366
375static inline bool asymcute_topic_is_short(const asymcute_topic_t *topic)
376{
377 assert(topic);
378 return ((topic->flags & MQTTSN_TIT_SHORT) != 0);
379}
380
389static inline bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
390{
391 assert(topic);
392 return ((topic->flags & MQTTSN_TIT_PREDEF) != 0);
393}
394
403static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
404{
405 assert(topic);
406 return (topic->name[0] != '\0');
407}
408
418static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
419 const asymcute_topic_t *b)
420{
421 assert(a);
422 assert(b);
423
424 return ((a->flags == b->flags) && (a->id == b->id));
425}
426
441int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
442 uint16_t topic_id);
443
451
461
480 sock_udp_ep_t *server, const char *cli_id, bool clean,
481 asymcute_will_t *will, asymcute_evt_cb_t callback);
482
494
508 asymcute_topic_t *topic);
509
528 const asymcute_topic_t *topic,
529 const void *data, size_t data_len, uint8_t flags);
530
551 asymcute_sub_t *sub, asymcute_topic_t *topic,
552 asymcute_sub_cb_t callback, void *arg, uint8_t flags);
553
567 asymcute_sub_t *sub);
568
569#ifdef __cplusplus
570}
571#endif
572
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Provides a callback-with-argument event type.
#define CONFIG_ASYMCUTE_BUFSIZE
Default buffer size used for receive and request buffers.
Definition asymcute.h:81
#define CONFIG_ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition asymcute.h:90
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition asymcute.h:244
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:418
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition asymcute.h:347
struct asymcute_con asymcute_con_t
Forward type declaration for connections contexts.
Definition asymcute.h:190
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.
struct asymcute_sub asymcute_sub_t
Forward type declaration for subscription contexts.
Definition asymcute.h:200
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:220
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition asymcute.h:403
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:361
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.
struct asymcute_topic asymcute_topic_t
Forward type declaration for topic definitions.
Definition asymcute.h:205
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition asymcute.h:333
static bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
Check if a given topic is a pre-defined topic.
Definition asymcute.h:389
static bool asymcute_topic_is_short(const asymcute_topic_t *topic)
Check if a given topic is a short topic.
Definition asymcute.h:375
struct asymcute_will asymcute_will_t
Forward type declaration for last will definitions.
Definition asymcute.h:210
struct asymcute_req asymcute_req_t
Forward type declaration for request contexts.
Definition asymcute.h:195
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition asymcute.h:319
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:231
int asymcute_disconnect(asymcute_con_t *con, asymcute_req_t *req)
Close the given connection.
@ ASYMCUTE_CONNECTED
connected to gateway
Definition asymcute.h:179
@ ASYMCUTE_SUBSCRIBED
client was subscribed to topic
Definition asymcute.h:183
@ ASYMCUTE_DISCONNECTED
connection got disconnected
Definition asymcute.h:180
@ ASYMCUTE_UNSUBSCRIBED
client was unsubscribed from topic
Definition asymcute.h:184
@ ASYMCUTE_REJECTED
request was rejected
Definition asymcute.h:178
@ ASYMCUTE_REGISTERED
topic was registered
Definition asymcute.h:181
@ ASYMCUTE_CANCELED
request was canceled
Definition asymcute.h:177
@ ASYMCUTE_PUBLISHED
data was published
Definition asymcute.h:182
@ ASYMCUTE_TIMEOUT
request timed out
Definition asymcute.h:176
@ ASYMCUTE_NOTSUP
error: feature not supported
Definition asymcute.h:165
@ ASYMCUTE_GWERR
error: bad gateway connection state
Definition asymcute.h:164
@ ASYMCUTE_SUBERR
error: subscription invalid
Definition asymcute.h:168
@ ASYMCUTE_OVERFLOW
error: insufficient buffer space
Definition asymcute.h:163
@ ASYMCUTE_SENDERR
error: unable to sent packet
Definition asymcute.h:169
@ ASYMCUTE_BUSY
error: context already in use
Definition asymcute.h:166
@ ASYMCUTE_REGERR
error: registration invalid
Definition asymcute.h:167
@ ASYMCUTE_OK
all is good
Definition asymcute.h:162
@ MQTTSN_TIT_SHORT
topic ID: short
Definition mqttsn.h:55
@ MQTTSN_TIT_PREDEF
topic ID: pre-defined
Definition mqttsn.h:56
struct sock_udp sock_udp_t
forward declare for async
Definition types.h:136
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:292
Generic MQTT-SN definitions.
UDP sock definitions.
Asymcute connection context.
Definition asymcute.h:266
sock_udp_t sock
socket used by a connections
Definition asymcute.h:268
event_callback_t keepalive_evt
keep alive event
Definition asymcute.h:272
asymcute_evt_cb_t user_cb
event callback provided by user
Definition asymcute.h:271
uint8_t rxbuf[CONFIG_ASYMCUTE_BUFSIZE]
connection specific receive buf
Definition asymcute.h:278
asymcute_req_t * pending
list holding pending requests
Definition asymcute.h:269
char cli_id[MQTTSN_CLI_ID_MAXLEN+1]
buffer to store client ID
Definition asymcute.h:279
event_timeout_t keepalive_timer
keep alive timer
Definition asymcute.h:273
mutex_t lock
synchronization lock
Definition asymcute.h:267
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition asymcute.h:276
uint16_t last_id
last used message ID for this connection
Definition asymcute.h:274
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition asymcute.h:270
uint8_t state
connection state
Definition asymcute.h:277
Asymcute request context.
Definition asymcute.h:249
asymcute_con_t * con
connection the request is using
Definition asymcute.h:252
mutex_t lock
synchronization lock
Definition asymcute.h:250
asymcute_to_cb_t cb
internally used callback
Definition asymcute.h:253
event_timeout_t to_timer
timeout timer
Definition asymcute.h:256
uint8_t retry_cnt
retransmission counter
Definition asymcute.h:260
void * arg
internally used additional state
Definition asymcute.h:254
event_callback_t to_evt
timeout event
Definition asymcute.h:255
size_t data_len
length of the request packet in byte
Definition asymcute.h:258
uint16_t msg_id
used message id for this request
Definition asymcute.h:259
uint8_t data[CONFIG_ASYMCUTE_BUFSIZE]
buffer holding the request's data
Definition asymcute.h:257
struct asymcute_req * next
the requests list entry
Definition asymcute.h:251
Data-structure holding the state of subscriptions.
Definition asymcute.h:295
asymcute_sub_t * next
the subscriptions list entry
Definition asymcute.h:296
asymcute_sub_cb_t cb
called on incoming data
Definition asymcute.h:298
asymcute_topic_t * topic
topic we subscribe to
Definition asymcute.h:297
void * arg
user supplied callback argument
Definition asymcute.h:299
Data-structure for holding topics and their registration status.
Definition asymcute.h:285
uint8_t flags
normal, short, or pre-defined
Definition asymcute.h:288
asymcute_con_t * con
connection used for registration
Definition asymcute.h:286
char name[CONFIG_ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ASCII only)
Definition asymcute.h:287
uint16_t id
topic id
Definition asymcute.h:289
Data structure for defining a last will.
Definition asymcute.h:305
size_t msg_len
length of last will message content
Definition asymcute.h:308
void * msg
last will message content
Definition asymcute.h:307
const char * topic
last will topic
Definition asymcute.h:306
Callback Event structure definition.
Definition callback.h:45
Timeout Event structure.
Definition timeout.h:46
Mutex structure.
Definition mutex.h:36
Provides functionality to trigger events after timeout.
sock utility function definitions