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
9#pragma once
10
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
58extern "C" {
59#endif
60
76#ifndef CONFIG_ASYMCUTE_DEFAULT_PORT
77#define CONFIG_ASYMCUTE_DEFAULT_PORT (1883U)
78#endif
79
83#ifndef CONFIG_ASYMCUTE_BUFSIZE
84#define CONFIG_ASYMCUTE_BUFSIZE (128U)
85#endif
86
92#ifndef CONFIG_ASYMCUTE_TOPIC_MAXLEN
93#define CONFIG_ASYMCUTE_TOPIC_MAXLEN (32U)
94#endif
95
103#ifndef CONFIG_ASYMCUTE_KEEPALIVE
104#define CONFIG_ASYMCUTE_KEEPALIVE (360)
105#endif
106
115#ifndef CONFIG_ASYMCUTE_KEEPALIVE_PING
116#define CONFIG_ASYMCUTE_KEEPALIVE_PING ((CONFIG_ASYMCUTE_KEEPALIVE / 4) * 3)
117#endif
118
129#ifndef CONFIG_ASYMCUTE_T_RETRY
130#define CONFIG_ASYMCUTE_T_RETRY (10U)
131#endif
132
142#ifndef CONFIG_ASYMCUTE_N_RETRY
143#define CONFIG_ASYMCUTE_N_RETRY (3U)
144#endif
146
147#ifndef ASYMCUTE_HANDLER_PRIO
151#define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
152#endif
153
154#ifndef ASYMCUTE_HANDLER_STACKSIZE
158#define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
159#endif
160
164enum {
173};
174
178enum {
188};
189
194
199
204
209
214
223typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
224
234typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
235 const void *data, size_t len, void *arg);
236
247typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
248
265
284
294
304
309 const char *topic;
310 void *msg;
311 size_t msg_len;
312};
313
322static inline bool asymcute_req_in_use(const asymcute_req_t *req)
323{
324 assert(req);
325 return (req->con != NULL);
326}
327
336static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
337{
338 assert(sub);
339 return (sub->topic != NULL);
340}
341
350static inline void asymcute_topic_reset(asymcute_topic_t *topic)
351{
352 assert(topic);
353 memset(topic, 0, sizeof(asymcute_topic_t));
354}
355
364static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
365{
366 assert(topic);
367 return (topic->con != NULL);
368}
369
378static inline bool asymcute_topic_is_short(const asymcute_topic_t *topic)
379{
380 assert(topic);
381 return ((topic->flags & MQTTSN_TIT_SHORT) != 0);
382}
383
392static inline bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
393{
394 assert(topic);
395 return ((topic->flags & MQTTSN_TIT_PREDEF) != 0);
396}
397
406static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
407{
408 assert(topic);
409 return (topic->name[0] != '\0');
410}
411
421static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
422 const asymcute_topic_t *b)
423{
424 assert(a);
425 assert(b);
426
427 return ((a->flags == b->flags) && (a->id == b->id));
428}
429
444int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
445 uint16_t topic_id);
446
454
464
483 sock_udp_ep_t *server, const char *cli_id, bool clean,
484 asymcute_will_t *will, asymcute_evt_cb_t callback);
485
497
511 asymcute_topic_t *topic);
512
531 const asymcute_topic_t *topic,
532 const void *data, size_t data_len, uint8_t flags);
533
554 asymcute_sub_t *sub, asymcute_topic_t *topic,
555 asymcute_sub_cb_t callback, void *arg, uint8_t flags);
556
570 asymcute_sub_t *sub);
571
572#ifdef __cplusplus
573}
574#endif
575
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:135
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:84
#define CONFIG_ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition asymcute.h:93
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition asymcute.h:247
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:421
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition asymcute.h:350
struct asymcute_con asymcute_con_t
Forward type declaration for connections contexts.
Definition asymcute.h:193
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:203
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:223
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition asymcute.h:406
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:364
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:208
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition asymcute.h:336
static bool asymcute_topic_is_predef(const asymcute_topic_t *topic)
Check if a given topic is a pre-defined topic.
Definition asymcute.h:392
static bool asymcute_topic_is_short(const asymcute_topic_t *topic)
Check if a given topic is a short topic.
Definition asymcute.h:378
struct asymcute_will asymcute_will_t
Forward type declaration for last will definitions.
Definition asymcute.h:213
struct asymcute_req asymcute_req_t
Forward type declaration for request contexts.
Definition asymcute.h:198
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition asymcute.h:322
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:234
int asymcute_disconnect(asymcute_con_t *con, asymcute_req_t *req)
Close the given connection.
@ ASYMCUTE_CONNECTED
connected to gateway
Definition asymcute.h:182
@ ASYMCUTE_SUBSCRIBED
client was subscribed to topic
Definition asymcute.h:186
@ ASYMCUTE_DISCONNECTED
connection got disconnected
Definition asymcute.h:183
@ ASYMCUTE_UNSUBSCRIBED
client was unsubscribed from topic
Definition asymcute.h:187
@ ASYMCUTE_REJECTED
request was rejected
Definition asymcute.h:181
@ ASYMCUTE_REGISTERED
topic was registered
Definition asymcute.h:184
@ ASYMCUTE_CANCELED
request was canceled
Definition asymcute.h:180
@ ASYMCUTE_PUBLISHED
data was published
Definition asymcute.h:185
@ ASYMCUTE_TIMEOUT
request timed out
Definition asymcute.h:179
@ ASYMCUTE_NOTSUP
error: feature not supported
Definition asymcute.h:168
@ ASYMCUTE_GWERR
error: bad gateway connection state
Definition asymcute.h:167
@ ASYMCUTE_SUBERR
error: subscription invalid
Definition asymcute.h:171
@ ASYMCUTE_OVERFLOW
error: insufficient buffer space
Definition asymcute.h:166
@ ASYMCUTE_SENDERR
error: unable to sent packet
Definition asymcute.h:172
@ ASYMCUTE_BUSY
error: context already in use
Definition asymcute.h:169
@ ASYMCUTE_REGERR
error: registration invalid
Definition asymcute.h:170
@ ASYMCUTE_OK
all is good
Definition asymcute.h:165
@ MQTTSN_TIT_SHORT
topic ID: short
Definition mqttsn.h:58
@ MQTTSN_TIT_PREDEF
topic ID: pre-defined
Definition mqttsn.h:59
struct sock_udp sock_udp_t
forward declare for async
Definition types.h:139
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:293
Generic MQTT-SN definitions.
UDP sock definitions.
Asymcute connection context.
Definition asymcute.h:269
sock_udp_t sock
socket used by a connections
Definition asymcute.h:271
event_callback_t keepalive_evt
keep alive event
Definition asymcute.h:275
asymcute_evt_cb_t user_cb
event callback provided by user
Definition asymcute.h:274
uint8_t rxbuf[CONFIG_ASYMCUTE_BUFSIZE]
connection specific receive buf
Definition asymcute.h:281
asymcute_req_t * pending
list holding pending requests
Definition asymcute.h:272
char cli_id[MQTTSN_CLI_ID_MAXLEN+1]
buffer to store client ID
Definition asymcute.h:282
event_timeout_t keepalive_timer
keep alive timer
Definition asymcute.h:276
mutex_t lock
synchronization lock
Definition asymcute.h:270
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition asymcute.h:279
uint16_t last_id
last used message ID for this connection
Definition asymcute.h:277
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition asymcute.h:273
uint8_t state
connection state
Definition asymcute.h:280
Asymcute request context.
Definition asymcute.h:252
asymcute_con_t * con
connection the request is using
Definition asymcute.h:255
mutex_t lock
synchronization lock
Definition asymcute.h:253
asymcute_to_cb_t cb
internally used callback
Definition asymcute.h:256
event_timeout_t to_timer
timeout timer
Definition asymcute.h:259
uint8_t retry_cnt
retransmission counter
Definition asymcute.h:263
void * arg
internally used additional state
Definition asymcute.h:257
event_callback_t to_evt
timeout event
Definition asymcute.h:258
size_t data_len
length of the request packet in byte
Definition asymcute.h:261
uint16_t msg_id
used message id for this request
Definition asymcute.h:262
uint8_t data[CONFIG_ASYMCUTE_BUFSIZE]
buffer holding the request's data
Definition asymcute.h:260
struct asymcute_req * next
the requests list entry
Definition asymcute.h:254
Data-structure holding the state of subscriptions.
Definition asymcute.h:298
asymcute_sub_t * next
the subscriptions list entry
Definition asymcute.h:299
asymcute_sub_cb_t cb
called on incoming data
Definition asymcute.h:301
asymcute_topic_t * topic
topic we subscribe to
Definition asymcute.h:300
void * arg
user supplied callback argument
Definition asymcute.h:302
Data-structure for holding topics and their registration status.
Definition asymcute.h:288
uint8_t flags
normal, short, or pre-defined
Definition asymcute.h:291
asymcute_con_t * con
connection used for registration
Definition asymcute.h:289
char name[CONFIG_ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ASCII only)
Definition asymcute.h:290
uint16_t id
topic id
Definition asymcute.h:292
Data structure for defining a last will.
Definition asymcute.h:308
size_t msg_len
length of last will message content
Definition asymcute.h:311
void * msg
last will message content
Definition asymcute.h:310
const char * topic
last will topic
Definition asymcute.h:309
Callback Event structure definition.
Definition callback.h:48
Timeout Event structure.
Definition timeout.h:49
Mutex structure.
Definition mutex.h:39
sock utility function definitions