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 
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
55 extern "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
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 
161 enum {
170 };
171 
175 enum {
185 };
186 
190 typedef struct asymcute_con asymcute_con_t;
191 
195 typedef struct asymcute_req asymcute_req_t;
196 
200 typedef struct asymcute_sub asymcute_sub_t;
201 
205 typedef struct asymcute_topic asymcute_topic_t;
206 
210 typedef struct asymcute_will asymcute_will_t;
211 
220 typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
221 
231 typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
232  const void *data, size_t len, void *arg);
233 
244 typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
245 
249 struct asymcute_req {
251  struct asymcute_req *next;
254  void *arg;
258  size_t data_len;
259  uint16_t msg_id;
260  uint8_t retry_cnt;
261 };
262 
266 struct asymcute_con {
274  uint16_t last_id;
277  uint8_t state;
279  char cli_id[MQTTSN_CLI_ID_MAXLEN + 1];
280 };
281 
288  uint8_t flags;
289  uint16_t id;
290 };
291 
295 struct asymcute_sub {
299  void *arg;
300 };
301 
306  const char *topic;
307  void *msg;
308  size_t msg_len;
309 };
310 
319 static inline bool asymcute_req_in_use(const asymcute_req_t *req)
320 {
321  assert(req);
322  return (req->con != NULL);
323 }
324 
333 static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
334 {
335  assert(sub);
336  return (sub->topic != NULL);
337 }
338 
347 static inline void asymcute_topic_reset(asymcute_topic_t *topic)
348 {
349  assert(topic);
350  memset(topic, 0, sizeof(asymcute_topic_t));
351 }
352 
361 static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
362 {
363  assert(topic);
364  return (topic->con != NULL);
365 }
366 
375 static 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 
389 static 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 
403 static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
404 {
405  assert(topic);
406  return (topic->name[0] != '\0');
407 }
408 
418 static 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 
441 int 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
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: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.
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
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
Generic MQTT-SN definitions.
UDP sock definitions.
Common IP-based transport layer end point.
Definition: sock.h:211
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
UDP sock type.
Definition: sock_types.h:128
Provides functionality to trigger events after timeout.
sock utility function definitions