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 
61 #ifndef ASYMCUTE_BUFSIZE
62 
65 #define ASYMCUTE_BUFSIZE (128U)
66 #endif
67 
68 #ifndef ASYMCUTE_HANDLER_PRIO
69 
72 #define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
73 #endif
74 
75 #ifndef ASYMCUTE_HANDLER_STACKSIZE
76 
79 #define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
80 #endif
81 
82 #ifndef ASYMCUTE_LISTENER_PRIO
83 
88 #define ASYMCUTE_LISTENER_PRIO (THREAD_PRIORITY_MAIN - 3)
89 #endif
90 
91 #ifndef ASYMCUTE_LISTENER_STACKSIZE
92 
95 #define ASYMCUTE_LISTENER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
96 #endif
97 
98 #ifndef ASYMCUTE_TOPIC_MAXLEN
99 
104 #define ASYMCUTE_TOPIC_MAXLEN (32U)
105 #endif
106 
107 #ifndef ASYMCUTE_KEEPALIVE
108 
113 #define ASYMCUTE_KEEPALIVE (360) /* -> 6 min*/
114 #endif
115 
116 #ifndef ASYMCUTE_KEEPALIVE_PING
117 
125 #define ASYMCUTE_KEEPALIVE_PING ((ASYMCUTE_KEEPALIVE / 4) * 3)
126 #endif
127 
128 #ifndef ASYMCUTE_T_RETRY
129 
134 #define ASYMCUTE_T_RETRY (10U) /* -> 10 sec */
135 #endif
136 
137 #ifndef ASYMCUTE_N_RETRY
138 
143 #define ASYMCUTE_N_RETRY (3U)
144 #endif
145 
149 enum {
157 };
158 
162 enum {
172 };
173 
178 
183 
188 
193 
198 
207 typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
208 
218 typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
219  const void *data, size_t len, void *arg);
220 
231 typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
232 
236 struct asymcute_req {
238  struct asymcute_req *next;
241  void *arg;
244  uint8_t data[ASYMCUTE_BUFSIZE];
245  size_t data_len;
246  uint16_t msg_id;
247  uint8_t retry_cnt;
248 };
249 
253 struct asymcute_con {
262  uint16_t last_id;
265  uint8_t state;
266  uint8_t rxbuf[ASYMCUTE_BUFSIZE];
267  char cli_id[MQTTSN_CLI_ID_MAXLEN + 1];
268 };
269 
275  char name[ASYMCUTE_TOPIC_MAXLEN + 1];
276  uint8_t flags;
277  uint16_t id;
278 };
279 
283 struct asymcute_sub {
287  void *arg;
288 };
289 
294  const char *topic;
295  void *msg;
296  size_t msg_len;
297 };
298 
307 static inline bool asymcute_req_in_use(const asymcute_req_t *req)
308 {
309  assert(req);
310  return (req->con != NULL);
311 }
312 
321 static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
322 {
323  assert(sub);
324  return (sub->topic != NULL);
325 }
326 
335 static inline void asymcute_topic_reset(asymcute_topic_t *topic)
336 {
337  assert(topic);
338  memset(topic, 0, sizeof(asymcute_topic_t));
339 }
340 
349 static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
350 {
351  assert(topic);
352  return (topic->con != NULL);
353 }
354 
363 static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
364 {
365  assert(topic);
366  return (topic->name[0] != '\0');
367 }
368 
378 static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
379  const asymcute_topic_t *b)
380 {
381  assert(a);
382  assert(b);
383 
384  return ((a->flags == b->flags) && (a->id == b->id));
385 }
386 
401 int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
402  uint16_t topic_id);
403 
420 int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize,
421  char priority, asymcute_evt_cb_t callback);
422 
429 void asymcute_handler_run(void);
430 
439 bool asymcute_is_connected(const asymcute_con_t *con);
440 
458  sock_udp_ep_t *server, const char *cli_id, bool clean,
459  asymcute_will_t *will);
460 
472 
486  asymcute_topic_t *topic);
487 
506  const asymcute_topic_t *topic,
507  const void *data, size_t data_len, uint8_t flags);
508 
529  asymcute_sub_t *sub, asymcute_topic_t *topic,
530  asymcute_sub_cb_t callback, void *arg, uint8_t flags);
531 
545  asymcute_sub_t *sub);
546 
547 #ifdef __cplusplus
548 }
549 #endif
550 
551 #endif /* NET_ASYMCUTE_H */
552 
error: registration invalid
Definition: asymcute.h:155
void * arg
user supplied callback argument
Definition: asymcute.h:287
asymcute_req_t * pending
list holding pending requests
Definition: asymcute.h:257
connection got disconnected
Definition: asymcute.h:167
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition: asymcute.h:264
request was rejected
Definition: asymcute.h:165
void * arg
internally used additional state
Definition: asymcute.h:241
asymcute_sub_t * next
the subscriptions list entry
Definition: asymcute.h:284
uint8_t flags
normal, short, or pre-defined
Definition: asymcute.h:276
Provides functionality to trigger events after timeout.
asymcute_sub_cb_t cb
called on incoming data
Definition: asymcute.h:286
struct asymcute_req * next
the requests list entry
Definition: asymcute.h:238
topic was registered
Definition: asymcute.h:168
Callback Event structure definition.
Definition: callback.h:48
int asymcute_disconnect(asymcute_con_t *con, asymcute_req_t *req)
Close the given connection.
Data structure for defining a last will.
Definition: asymcute.h:293
UDP sock definitions.
uint16_t msg_id
used message id for this request
Definition: asymcute.h:246
error: context already in use
Definition: asymcute.h:154
int asymcute_unsubscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub)
Cancel an active subscription.
asymcute_evt_cb_t user_cb
event callback provided by user
Definition: asymcute.h:259
request was canceled
Definition: asymcute.h:164
Provides a callback-with-argument event type.
uint8_t retry_cnt
retransmission counter
Definition: asymcute.h:247
sock_udp_t sock
socket used by a connections
Definition: asymcute.h:255
sock_udp_ep_t server_ep
the gateway&#39;s UDP endpoint
Definition: asymcute.h:256
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition: asymcute.h:307
const char * topic
last will topic
Definition: asymcute.h:294
int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name, uint16_t topic_id)
Initialize the given topic.
client was subscribed to topic
Definition: asymcute.h:170
POSIX.1-2008 compliant version of the assert macro.
asymcute_topic_t * topic
topic we subscribe to
Definition: asymcute.h:285
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition: asymcute.h:335
uint16_t last_id
last used message ID for this connection
Definition: asymcute.h:262
error: bad gateway connection state
Definition: asymcute.h:152
char name[ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ACSII only)
Definition: asymcute.h:275
event_callback_t to_evt
timeout event
Definition: asymcute.h:242
connected to gateway
Definition: asymcute.h:166
asymcute_con_t * con
connection used for registration
Definition: asymcute.h:274
Asymcute request context.
Definition: asymcute.h:236
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:349
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
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:378
Common IP-based transport layer end point.
Definition: sock.h:196
asymcute_con_t * con
connection the request is using
Definition: asymcute.h:239
mutex_t lock
synchronization lock
Definition: asymcute.h:254
event_timeout_t keepalive_timer
keep alive timer
Definition: asymcute.h:261
sock utility function definitions
error: insufficient buffer space
Definition: asymcute.h:151
event_timeout_t to_timer
timeout timer
Definition: asymcute.h:243
void * msg
last will message content
Definition: asymcute.h:295
Data-structure holding the state of subscriptions.
Definition: asymcute.h:283
bool asymcute_is_connected(const asymcute_con_t *con)
Check if the given connection context is connected to a gateway.
uint16_t id
topic id
Definition: asymcute.h:277
data was published
Definition: asymcute.h:169
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:218
#define ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition: asymcute.h:104
UDP sock type.
Definition: sock_types.h:71
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.
void asymcute_handler_run(void)
Start the global Asymcute handler thread for processing timeouts and keep alive events.
uint8_t state
connection state
Definition: asymcute.h:265
event_callback_t keepalive_evt
keep alive event
Definition: asymcute.h:260
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:207
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition: asymcute.h:363
int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize, char priority, asymcute_evt_cb_t callback)
Start a listener thread.
#define ASYMCUTE_BUFSIZE
Default buffer size used for receive and request buffers.
Definition: asymcute.h:65
all is good
Definition: asymcute.h:150
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition: asymcute.h:231
Mutex structure.
Definition: mutex.h:36
size_t data_len
length of the request packet in byte
Definition: asymcute.h:245
error: feature not supported
Definition: asymcute.h:153
Asymcute connection context.
Definition: asymcute.h:253
Timeout Event structure.
Definition: timeout.h:50
request timed out
Definition: asymcute.h:163
Data-structure for holding topics and their registration status.
Definition: asymcute.h:273
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition: asymcute.h:321
error: subscription invalid
Definition: asymcute.h:156
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.
Generic MQTT-SN definitions.
size_t msg_len
length of last will message content
Definition: asymcute.h:296
asymcute_to_cb_t cb
internally used callback
Definition: asymcute.h:240
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition: asymcute.h:258
client was unsubscribed from topic
Definition: asymcute.h:171
mutex_t lock
synchronization lock
Definition: asymcute.h:237
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.
int asymcute_register(asymcute_con_t *con, asymcute_req_t *req, asymcute_topic_t *topic)
Register a given topic with the connected gateway.