Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
mqtt_sn.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 René Beckmann
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
20#ifndef ZEPHYR_INCLUDE_NET_MQTT_SN_H_
21#define ZEPHYR_INCLUDE_NET_MQTT_SN_H_
22
23#include <stddef.h>
24
25#include <zephyr/net/buf.h>
26#include <zephyr/types.h>
27
28#include <sys/types.h>
29
30#ifdef CONFIG_MQTT_SN_TRANSPORT_UDP
31#include <zephyr/net/net_ip.h>
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
48
71
81
87
97#define MQTT_SN_DATA_STRING_LITERAL(literal) ((struct mqtt_sn_data){literal, sizeof(literal) - 1})
98
106#define MQTT_SN_DATA_BYTES(...) \
107 ((struct mqtt_sn_data) { (uint8_t[]){ __VA_ARGS__ }, sizeof((uint8_t[]){ __VA_ARGS__ })})
108
120
135
145
146struct mqtt_sn_client;
147
156typedef void (*mqtt_sn_evt_cb_t)(struct mqtt_sn_client *client, const struct mqtt_sn_evt *evt);
157
171 int (*init)(struct mqtt_sn_transport *transport);
172
178 void (*deinit)(struct mqtt_sn_transport *transport);
179
183 int (*msg_send)(struct mqtt_sn_client *client, void *buf, size_t sz);
184
190 ssize_t (*recv)(struct mqtt_sn_client *client, void *buffer, size_t length);
191
202 int (*poll)(struct mqtt_sn_client *client);
203};
204
205#ifdef CONFIG_MQTT_SN_TRANSPORT_UDP
209struct mqtt_sn_transport_udp {
211 struct mqtt_sn_transport tp;
212
214 int sock;
215
217 struct sockaddr gwaddr;
218 socklen_t gwaddrlen;
219};
220
221#define UDP_TRANSPORT(transport) CONTAINER_OF(transport, struct mqtt_sn_transport_udp, tp)
222
230int mqtt_sn_transport_udp_init(struct mqtt_sn_transport_udp *udp, struct sockaddr *gwaddr,
231 socklen_t addrlen);
232#endif
233
290
305int mqtt_sn_client_init(struct mqtt_sn_client *client, const struct mqtt_sn_data *client_id,
306 struct mqtt_sn_transport *transport, mqtt_sn_evt_cb_t evt_cb, void *tx,
307 size_t txsz, void *rx, size_t rxsz);
308
317
327int mqtt_sn_connect(struct mqtt_sn_client *client, bool will, bool clean_session);
328
337
346int mqtt_sn_sleep(struct mqtt_sn_client *client, uint16_t duration);
347
357int mqtt_sn_subscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
358 struct mqtt_sn_data *topic_name);
359
369int mqtt_sn_unsubscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
370 struct mqtt_sn_data *topic_name);
371
385int mqtt_sn_publish(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
386 struct mqtt_sn_data *topic_name, bool retain, struct mqtt_sn_data *data);
387
398int mqtt_sn_input(struct mqtt_sn_client *client);
399
411 struct mqtt_sn_data *topic_name);
412
413#ifdef __cplusplus
414}
415#endif
416
417#endif /* ZEPHYR_INCLUDE_NET_MQTT_SN_H_ */
418
size_t socklen_t
Length of a socket address.
Definition net_ip.h:168
int mqtt_sn_unsubscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos, struct mqtt_sn_data *topic_name)
Unsubscribe from a topic.
int mqtt_sn_publish(struct mqtt_sn_client *client, enum mqtt_sn_qos qos, struct mqtt_sn_data *topic_name, bool retain, struct mqtt_sn_data *data)
Publish a value.
void mqtt_sn_client_deinit(struct mqtt_sn_client *client)
Deinitialize the client.
mqtt_sn_qos
Quality of Service.
Definition mqtt_sn.h:42
int mqtt_sn_connect(struct mqtt_sn_client *client, bool will, bool clean_session)
Connect the client.
int mqtt_sn_subscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos, struct mqtt_sn_data *topic_name)
Subscribe to a given topic.
mqtt_sn_evt_type
Event types that can be emitted by the library.
Definition mqtt_sn.h:112
mqtt_sn_return_code
MQTT-SN return codes.
Definition mqtt_sn.h:75
int mqtt_sn_disconnect(struct mqtt_sn_client *client)
Disconnect the client.
mqtt_sn_topic_type
MQTT-SN topic types.
Definition mqtt_sn.h:52
int mqtt_sn_get_topic_name(struct mqtt_sn_client *client, uint16_t id, struct mqtt_sn_data *topic_name)
Get topic name by topic ID.
int mqtt_sn_client_init(struct mqtt_sn_client *client, const struct mqtt_sn_data *client_id, struct mqtt_sn_transport *transport, mqtt_sn_evt_cb_t evt_cb, void *tx, size_t txsz, void *rx, size_t rxsz)
Initialize a client.
void(* mqtt_sn_evt_cb_t)(struct mqtt_sn_client *client, const struct mqtt_sn_evt *evt)
Asynchronous event notification callback registered by the application.
Definition mqtt_sn.h:156
int mqtt_sn_sleep(struct mqtt_sn_client *client, uint16_t duration)
Set the client into sleep state.
int mqtt_sn_input(struct mqtt_sn_client *client)
Check the transport for new incoming data.
@ MQTT_SN_QOS_M1
QOS -1.
Definition mqtt_sn.h:46
@ MQTT_SN_QOS_2
QOS 2.
Definition mqtt_sn.h:45
@ MQTT_SN_QOS_1
QOS 1.
Definition mqtt_sn.h:44
@ MQTT_SN_QOS_0
QOS 0.
Definition mqtt_sn.h:43
@ MQTT_SN_EVT_PINGRESP
Received a PINGRESP.
Definition mqtt_sn.h:118
@ MQTT_SN_EVT_CONNECTED
Connected to a gateway.
Definition mqtt_sn.h:113
@ MQTT_SN_EVT_PUBLISH
Received a PUBLISH message.
Definition mqtt_sn.h:117
@ MQTT_SN_EVT_ASLEEP
Entered ASLEEP state.
Definition mqtt_sn.h:115
@ MQTT_SN_EVT_DISCONNECTED
Disconnected.
Definition mqtt_sn.h:114
@ MQTT_SN_EVT_AWAKE
Entered AWAKE state.
Definition mqtt_sn.h:116
@ MQTT_SN_CODE_ACCEPTED
Accepted.
Definition mqtt_sn.h:76
@ MQTT_SN_CODE_REJECTED_CONGESTION
Rejected: congestion.
Definition mqtt_sn.h:77
@ MQTT_SN_CODE_REJECTED_NOTSUP
Rejected: Not Supported.
Definition mqtt_sn.h:79
@ MQTT_SN_CODE_REJECTED_TOPIC_ID
Rejected: Invalid Topic ID.
Definition mqtt_sn.h:78
@ MQTT_SN_TOPIC_TYPE_NORMAL
Normal topic.
Definition mqtt_sn.h:57
@ MQTT_SN_TOPIC_TYPE_PREDEF
Pre-defined topic.
Definition mqtt_sn.h:64
@ MQTT_SN_TOPIC_TYPE_SHORT
Short topic.
Definition mqtt_sn.h:69
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
__SIZE_TYPE__ ssize_t
Definition types.h:28
Buffer management.
IPv6 and IPv4 definitions.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT64_TYPE__ int64_t
Definition stdint.h:75
A structure used to submit work after a delay.
Definition kernel.h:3908
Structure describing an MQTT-SN client.
Definition mqtt_sn.h:237
int state
Current state of the MQTT-SN client.
Definition mqtt_sn.h:279
uint16_t next_msg_id
Message ID for the next message to be sent.
Definition mqtt_sn.h:270
uint8_t ping_retries
Number of retries for failed ping attempts.
Definition mqtt_sn.h:285
sys_slist_t publish
List of pending publish messages.
Definition mqtt_sn.h:273
struct net_buf_simple tx
Buffer for outgoing data.
Definition mqtt_sn.h:261
enum mqtt_sn_qos will_qos
Quality of Service for the Will message.
Definition mqtt_sn.h:252
struct k_work_delayable process_work
Delayable work structure for processing MQTT-SN events.
Definition mqtt_sn.h:288
struct mqtt_sn_data client_id
1-23 character unique client ID
Definition mqtt_sn.h:239
struct mqtt_sn_data will_msg
Will message.
Definition mqtt_sn.h:249
sys_slist_t topic
List of registered topics.
Definition mqtt_sn.h:276
mqtt_sn_evt_cb_t evt_cb
Event callback.
Definition mqtt_sn.h:267
bool will_retain
Flag indicating if the will message should be retained by the broker.
Definition mqtt_sn.h:255
struct mqtt_sn_transport * transport
Underlying transport to be used by the client.
Definition mqtt_sn.h:258
struct net_buf_simple rx
Buffer for incoming data.
Definition mqtt_sn.h:264
int64_t last_ping
Timestamp of the last ping request.
Definition mqtt_sn.h:282
struct mqtt_sn_data will_topic
Topic for Will message.
Definition mqtt_sn.h:244
Abstracts memory buffers.
Definition mqtt_sn.h:83
uint16_t size
Size of data, in bytes.
Definition mqtt_sn.h:85
const uint8_t * data
Pointer to data.
Definition mqtt_sn.h:84
MQTT-SN event structure to be handled by the event callback.
Definition mqtt_sn.h:139
union mqtt_sn_evt_param param
Event parameters.
Definition mqtt_sn.h:143
enum mqtt_sn_evt_type type
Event type.
Definition mqtt_sn.h:141
Structure to describe an MQTT-SN transport.
Definition mqtt_sn.h:165
ssize_t(* recv)(struct mqtt_sn_client *client, void *buffer, size_t length)
Will be called by the library when it wants to receive a message.
Definition mqtt_sn.h:190
int(* init)(struct mqtt_sn_transport *transport)
Will be called once on client init to initialize the transport.
Definition mqtt_sn.h:171
int(* poll)(struct mqtt_sn_client *client)
Check if incoming data is available.
Definition mqtt_sn.h:202
int(* msg_send)(struct mqtt_sn_client *client, void *buf, size_t sz)
Will be called by the library when it wants to send a message.
Definition mqtt_sn.h:183
void(* deinit)(struct mqtt_sn_transport *transport)
Will be called on client deinit.
Definition mqtt_sn.h:178
Simple network buffer representation.
Definition buf.h:87
Generic sockaddr struct.
Definition net_ip.h:385
static fdata_t data[2]
Definition test_fifo_contexts.c:15
static ZTEST_BMEM char buffer[8]
Definition test_mbox_api.c:433
static ZTEST_DMEM int tp
Definition main.c:41
Event metadata.
Definition mqtt_sn.h:124
struct mqtt_sn_data data
The payload data associated with the event.
Definition mqtt_sn.h:128
enum mqtt_sn_topic_type topic_type
The type of topic for the event.
Definition mqtt_sn.h:130
uint16_t topic_id
The identifier for the topic of the event.
Definition mqtt_sn.h:132
struct mqtt_sn_evt_param::@329 publish
Structure holding publish event details.