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_ID_MAXLEN
99 
104 #define ASYMCUTE_ID_MAXLEN (32U)
105 #endif
106 
107 #ifndef ASYMCUTE_TOPIC_MAXLEN
108 
113 #define ASYMCUTE_TOPIC_MAXLEN (32U)
114 #endif
115 
116 #ifndef ASYMCUTE_KEEPALIVE
117 
122 #define ASYMCUTE_KEEPALIVE (360) /* -> 6 min*/
123 #endif
124 
125 #ifndef ASYMCUTE_KEEPALIVE_PING
126 
134 #define ASYMCUTE_KEEPALIVE_PING ((ASYMCUTE_KEEPALIVE / 4) * 3)
135 #endif
136 
137 #ifndef ASYMCUTE_T_RETRY
138 
143 #define ASYMCUTE_T_RETRY (10U) /* -> 10 sec */
144 #endif
145 
146 #ifndef ASYMCUTE_N_RETRY
147 
152 #define ASYMCUTE_N_RETRY (3U)
153 #endif
154 
158 enum {
166 };
167 
171 enum {
181 };
182 
187 
192 
197 
202 
207 
216 typedef void(*asymcute_evt_cb_t)(asymcute_req_t *req, unsigned evt_type);
217 
227 typedef void(*asymcute_sub_cb_t)(const asymcute_sub_t *sub, unsigned evt_type,
228  const void *data, size_t len, void *arg);
229 
240 typedef unsigned(*asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req);
241 
245 struct asymcute_req {
247  struct asymcute_req *next;
250  void *arg;
253  uint8_t data[ASYMCUTE_BUFSIZE];
254  size_t data_len;
255  uint16_t msg_id;
256  uint8_t retry_cnt;
257 };
258 
262 struct asymcute_con {
271  uint16_t last_id;
274  uint8_t state;
275  uint8_t rxbuf[ASYMCUTE_BUFSIZE];
276  char cli_id[ASYMCUTE_ID_MAXLEN + 1];
277 };
278 
284  char name[ASYMCUTE_TOPIC_MAXLEN + 1];
285  uint8_t flags;
286  uint16_t id;
287 };
288 
292 struct asymcute_sub {
296  void *arg;
297 };
298 
303  const char *topic;
304  void *msg;
305  size_t msg_len;
306 };
307 
316 static inline bool asymcute_req_in_use(const asymcute_req_t *req)
317 {
318  assert(req);
319  return (req->con != NULL);
320 }
321 
330 static inline bool asymcute_sub_active(const asymcute_sub_t *sub)
331 {
332  assert(sub);
333  return (sub->topic != NULL);
334 }
335 
344 static inline void asymcute_topic_reset(asymcute_topic_t *topic)
345 {
346  assert(topic);
347  memset(topic, 0, sizeof(asymcute_topic_t));
348 }
349 
358 static inline bool asymcute_topic_is_reg(const asymcute_topic_t *topic)
359 {
360  assert(topic);
361  return (topic->con != NULL);
362 }
363 
372 static inline bool asymcute_topic_is_init(const asymcute_topic_t *topic)
373 {
374  assert(topic);
375  return (topic->name[0] != '\0');
376 }
377 
387 static inline bool asymcute_topic_equal(const asymcute_topic_t *a,
388  const asymcute_topic_t *b)
389 {
390  assert(a);
391  assert(b);
392 
393  return ((a->flags == b->flags) && (a->id == b->id));
394 }
395 
410 int asymcute_topic_init(asymcute_topic_t *topic, const char *topic_name,
411  uint16_t topic_id);
412 
429 int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize,
430  char priority, asymcute_evt_cb_t callback);
431 
438 void asymcute_handler_run(void);
439 
448 bool asymcute_is_connected(const asymcute_con_t *con);
449 
467  sock_udp_ep_t *server, const char *cli_id, bool clean,
468  asymcute_will_t *will);
469 
481 
495  asymcute_topic_t *topic);
496 
515  const asymcute_topic_t *topic,
516  const void *data, size_t data_len, uint8_t flags);
517 
538  asymcute_sub_t *sub, asymcute_topic_t *topic,
539  asymcute_sub_cb_t callback, void *arg, uint8_t flags);
540 
554  asymcute_sub_t *sub);
555 
556 #ifdef __cplusplus
557 }
558 #endif
559 
560 #endif /* NET_ASYMCUTE_H */
561 
void * arg
user supplied callback argument
Definition: asymcute.h:296
asymcute_req_t * pending
list holding pending requests
Definition: asymcute.h:266
uint8_t keepalive_retry_cnt
keep alive transmission counter
Definition: asymcute.h:273
void * arg
internally used additional state
Definition: asymcute.h:250
error: subscription invalid
Definition: asymcute.h:165
request was canceled
Definition: asymcute.h:173
asymcute_sub_t * next
the subscriptions list entry
Definition: asymcute.h:293
data was published
Definition: asymcute.h:178
uint8_t flags
normal, short, or pre-defined
Definition: asymcute.h:285
Provides functionality to trigger events after timeout.
all is good
Definition: asymcute.h:159
asymcute_sub_cb_t cb
called on incoming data
Definition: asymcute.h:295
struct asymcute_req * next
the requests list entry
Definition: asymcute.h:247
error: registration invalid
Definition: asymcute.h:164
Callback Event structure definition.
Definition: callback.h:48
connection got disconnected
Definition: asymcute.h:176
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:302
UDP sock definitions.
uint16_t msg_id
used message id for this request
Definition: asymcute.h:255
request timed out
Definition: asymcute.h:172
int asymcute_unsubscribe(asymcute_con_t *con, asymcute_req_t *req, asymcute_sub_t *sub)
Cancel an active subscription.
request was rejected
Definition: asymcute.h:174
topic was registered
Definition: asymcute.h:177
asymcute_evt_cb_t user_cb
event callback provided by user
Definition: asymcute.h:268
Provides a callback-with-argument event type.
uint8_t retry_cnt
retransmission counter
Definition: asymcute.h:256
sock_udp_t sock
socket used by a connections
Definition: asymcute.h:264
sock_udp_ep_t server_ep
the gateway&#39;s UDP endpoint
Definition: asymcute.h:265
static bool asymcute_req_in_use(const asymcute_req_t *req)
Check if a given request context is currently used.
Definition: asymcute.h:316
const char * topic
last will topic
Definition: asymcute.h:303
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:294
static void asymcute_topic_reset(asymcute_topic_t *topic)
Reset the given topic.
Definition: asymcute.h:344
uint16_t last_id
last used message ID for this connection
Definition: asymcute.h:271
char name[ASYMCUTE_TOPIC_MAXLEN+1]
topic string (ACSII only)
Definition: asymcute.h:284
event_callback_t to_evt
timeout event
Definition: asymcute.h:251
asymcute_con_t * con
connection used for registration
Definition: asymcute.h:283
Asymcute request context.
Definition: asymcute.h:245
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:358
error: context already in use
Definition: asymcute.h:163
#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:387
Common IP-based transport layer end point.
Definition: sock.h:195
asymcute_con_t * con
connection the request is using
Definition: asymcute.h:248
mutex_t lock
synchronization lock
Definition: asymcute.h:263
event_timeout_t keepalive_timer
keep alive timer
Definition: asymcute.h:270
sock utility function definitions
error: feature not supported
Definition: asymcute.h:162
event_timeout_t to_timer
timeout timer
Definition: asymcute.h:252
void * msg
last will message content
Definition: asymcute.h:304
Data-structure holding the state of subscriptions.
Definition: asymcute.h:292
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:286
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:227
#define ASYMCUTE_TOPIC_MAXLEN
Maximum topic length.
Definition: asymcute.h:113
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:274
event_callback_t keepalive_evt
keep alive event
Definition: asymcute.h:269
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:216
static bool asymcute_topic_is_init(const asymcute_topic_t *topic)
Check if a given topic is initialized.
Definition: asymcute.h:372
error: insufficient buffer space
Definition: asymcute.h:160
#define ASYMCUTE_ID_MAXLEN
Maximum client ID length.
Definition: asymcute.h:104
int asymcute_listener_run(asymcute_con_t *con, char *stack, size_t stacksize, char priority, asymcute_evt_cb_t callback)
Start a listener thread.
error: bad gateway connection state
Definition: asymcute.h:161
#define ASYMCUTE_BUFSIZE
Default buffer size used for receive and request buffers.
Definition: asymcute.h:65
connected to gateway
Definition: asymcute.h:175
unsigned(* asymcute_to_cb_t)(asymcute_con_t *con, asymcute_req_t *req)
Context specific timeout callback, only used internally.
Definition: asymcute.h:240
Mutex structure.
Definition: mutex.h:36
size_t data_len
length of the request packet in byte
Definition: asymcute.h:254
Asymcute connection context.
Definition: asymcute.h:262
Timeout Event structure.
Definition: timeout.h:50
client was subscribed to topic
Definition: asymcute.h:179
client was unsubscribed from topic
Definition: asymcute.h:180
Data-structure for holding topics and their registration status.
Definition: asymcute.h:282
static bool asymcute_sub_active(const asymcute_sub_t *sub)
Check if a given subscription is currently active.
Definition: asymcute.h:330
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:305
asymcute_to_cb_t cb
internally used callback
Definition: asymcute.h:249
asymcute_sub_t * subscriptions
list holding active subscriptions
Definition: asymcute.h:267
mutex_t lock
synchronization lock
Definition: asymcute.h:246
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.