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 
72 #ifndef CONFIG_ASYMCUTE_BUFSIZE_EXP
73 #define CONFIG_ASYMCUTE_BUFSIZE_EXP (7U)
74 #endif
75 
81 #ifndef CONFIG_ASYMCUTE_TOPIC_MAXLEN
82 #define CONFIG_ASYMCUTE_TOPIC_MAXLEN (32U)
83 #endif
84 
92 #ifndef CONFIG_ASYMCUTE_KEEPALIVE
93 #define CONFIG_ASYMCUTE_KEEPALIVE (360)
94 #endif
95 
104 #ifndef CONFIG_ASYMCUTE_KEEPALIVE_PING
105 #define CONFIG_ASYMCUTE_KEEPALIVE_PING ((CONFIG_ASYMCUTE_KEEPALIVE / 4) * 3)
106 #endif
107 
118 #ifndef CONFIG_ASYMCUTE_T_RETRY
119 #define CONFIG_ASYMCUTE_T_RETRY (10U)
120 #endif
121 
131 #ifndef CONFIG_ASYMCUTE_N_RETRY
132 #define CONFIG_ASYMCUTE_N_RETRY (3U)
133 #endif
134 
136 #ifndef ASYMCUTE_BUFSIZE
137 
140 #define ASYMCUTE_BUFSIZE (1 << CONFIG_ASYMCUTE_BUFSIZE_EXP)
141 #endif
142 
143 #ifndef ASYMCUTE_HANDLER_PRIO
144 
147 #define ASYMCUTE_HANDLER_PRIO (THREAD_PRIORITY_MAIN - 2)
148 #endif
149 
150 #ifndef ASYMCUTE_HANDLER_STACKSIZE
151 
154 #define ASYMCUTE_HANDLER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
155 #endif
156 
157 #ifndef ASYMCUTE_LISTENER_PRIO
158 
163 #define ASYMCUTE_LISTENER_PRIO (THREAD_PRIORITY_MAIN - 3)
164 #endif
165 
166 #ifndef ASYMCUTE_LISTENER_STACKSIZE
167 
170 #define ASYMCUTE_LISTENER_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
171 #endif
172 
176 enum {
184 };
185 
189 enum {
199 };
200 
205 
210 
215 
220 
225 
234 typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
235 
245 typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
246  const void *data, size_t len, void *arg);
247 
258 typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
259 
263 struct asymcute_req {
265  struct asymcute_req *next;
268  void *arg;
271  uint8_t data[ASYMCUTE_BUFSIZE];
272  size_t data_len;
273  uint16_t msg_id;
274  uint8_t retry_cnt;
275 };
276 
280 struct asymcute_con {
289  uint16_t last_id;
292  uint8_t state;
293  uint8_t rxbuf[ASYMCUTE_BUFSIZE];
294  char cli_id[MQTTSN_CLI_ID_MAXLEN + 1];
295 };
296 
303  uint8_t flags;
304  uint16_t id;
305 };
306 
310 struct asymcute_sub {
314  void *arg;
315 };
316 
321  const char *topic;
322  void *msg;
323  size_t msg_len;
324 };
325 
334 static inline bool asymcute_req_in_use(const asymcute_req_t *req)
335 {
336  assert(req);
337  return (req->con != NULL);
338 }
339 
348 static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
349 {
350  assert(sub);
351  return (sub->topic != NULL);
352 }
353 
362 static inline void asymcute_topic_reset(asymcute_topic_t *topic)
363 {
364  assert(topic);
365  memset(topic, 0, sizeof(asymcute_topic_t));
366 }
367 
376 static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
377 {
378  assert(topic);
379  return (topic->con != NULL);
380 }
381 
390 static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
391 {
392  assert(topic);
393  return (topic->name[0] != '\0');
394 }
395 
405 static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
406  const asymcute_topic_t *b)
407 {
408  assert(a);
409  assert(b);
410 
411  return ((a->flags == b->flags) && (a->id == b->id));
412 }
413 
428 int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
429  uint16_t topic_id);
430 
447 int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize,
448  char priority, asymcute_evt_cb_t callback);
449 
456 void asymcute_handler_run(void);
457 
466 bool asymcute_is_connected(const asymcute_con_t *con);
467 
485  sock_udp_ep_t *server, const char *cli_id, bool clean,
486  asymcute_will_t *will);
487 
499 
513  asymcute_topic_t *topic);
514 
533  const asymcute_topic_t *topic,
534  const void *data, size_t data_len, uint8_t flags);
535 
556  asymcute_sub_t *sub, asymcute_topic_t *topic,
557  asymcute_sub_cb_t callback, void *arg, uint8_t flags);
558 
572  asymcute_sub_t *sub);
573 
574 #ifdef __cplusplus
575 }
576 #endif
577 
578 #endif /* NET_ASYMCUTE_H */
579 
void * arg
user supplied callback argument
Definition: asymcute.h:314
asymcute_req_t * pending
list holding pending requests
Definition: asymcute.h:284
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition: asymcute.h:291
void * arg
internally used additional state
Definition: asymcute.h:268
client was unsubscribed from topic
Definition: asymcute.h:198
asymcute_sub_t * next
the subscriptions list entry
Definition: asymcute.h:311
client was subscribed to topic
Definition: asymcute.h:197
uint8_t flags
normal, short, or pre-defined
Definition: asymcute.h:303
Provides functionality to trigger events after timeout.
asymcute_sub_cb_t cb
called on incoming data
Definition: asymcute.h:313
struct asymcute_req * next
the requests list entry
Definition: asymcute.h:265
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:320
UDP sock definitions.
uint16_t msg_id
used message id for this request
Definition: asymcute.h:273
all is good
Definition: asymcute.h:177
int asymcute_unsubscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub)
Cancel an active subscription.
connected to gateway
Definition: asymcute.h:193
asymcute_evt_cb_t user_cb
event callback provided by user
Definition: asymcute.h:286
topic was registered
Definition: asymcute.h:195
error: context already in use
Definition: asymcute.h:181
error: subscription invalid
Definition: asymcute.h:183
#define CONFIG_ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition: asymcute.h:82
Provides a callback-with-argument event type.
uint8_t retry_cnt
retransmission counter
Definition: asymcute.h:274
sock_udp_t sock
socket used by a connections
Definition: asymcute.h:282
sock_udp_ep_t server_ep
the gateway&#39;s UDP endpoint
Definition: asymcute.h:283
request timed out
Definition: asymcute.h:190
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition: asymcute.h:334
const char * topic
last will topic
Definition: asymcute.h:321
int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name, uint16_t topic_id)
Initialize the given topic.
POSIX.1-2008 compliant version of the assert macro.
asymcute_topic_t * topic
topic we subscribe to
Definition: asymcute.h:312
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition: asymcute.h:362
uint16_t last_id
last used message ID for this connection
Definition: asymcute.h:289
error: feature not supported
Definition: asymcute.h:180
error: bad gateway connection state
Definition: asymcute.h:179
event_callback_t to_evt
timeout event
Definition: asymcute.h:269
asymcute_con_t * con
connection used for registration
Definition: asymcute.h:301
Asymcute request context.
Definition: asymcute.h:263
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:376
#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:405
request was canceled
Definition: asymcute.h:191
Common IP-based transport layer end point.
Definition: sock.h:213
asymcute_con_t * con
connection the request is using
Definition: asymcute.h:266
mutex_t lock
synchronization lock
Definition: asymcute.h:281
event_timeout_t keepalive_timer
keep alive timer
Definition: asymcute.h:288
sock utility function definitions
data was published
Definition: asymcute.h:196
event_timeout_t to_timer
timeout timer
Definition: asymcute.h:270
void * msg
last will message content
Definition: asymcute.h:322
Data-structure holding the state of subscriptions.
Definition: asymcute.h:310
error: registration invalid
Definition: asymcute.h:182
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:304
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:245
error: insufficient buffer space
Definition: asymcute.h:178
UDP sock type.
Definition: sock_types.h:128
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:292
event_callback_t keepalive_evt
keep alive event
Definition: asymcute.h:287
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:234
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition: asymcute.h:390
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:140
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition: asymcute.h:258
Mutex structure.
Definition: mutex.h:37
size_t data_len
length of the request packet in byte
Definition: asymcute.h:272
Asymcute connection context.
Definition: asymcute.h:280
Timeout Event structure.
Definition: timeout.h:50
request was rejected
Definition: asymcute.h:192
Data-structure for holding topics and their registration status.
Definition: asymcute.h:300
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition: asymcute.h:348
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.
connection got disconnected
Definition: asymcute.h:194
size_t msg_len
length of last will message content
Definition: asymcute.h:323
asymcute_to_cb_t cb
internally used callback
Definition: asymcute.h:267
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition: asymcute.h:285
mutex_t lock
synchronization lock
Definition: asymcute.h:264
char name[CONFIG_ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ACSII only)
Definition: asymcute.h:302
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.