Zephyr API Documentation 4.0.99
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
22#ifndef ZEPHYR_INCLUDE_NET_MQTT_SN_H_
23#define ZEPHYR_INCLUDE_NET_MQTT_SN_H_
24
25#include <stddef.h>
26
27#include <zephyr/net_buf.h>
28#include <zephyr/types.h>
29
30#include <sys/types.h>
31
32#ifdef CONFIG_MQTT_SN_TRANSPORT_UDP
33#include <zephyr/net/net_ip.h>
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
50
73
83
86 const uint8_t *data;
87 size_t size;
88};
89
99#define MQTT_SN_DATA_STRING_LITERAL(literal) ((struct mqtt_sn_data){literal, sizeof(literal) - 1})
100
108#define MQTT_SN_DATA_BYTES(...) \
109 ((struct mqtt_sn_data){(uint8_t[]){__VA_ARGS__}, sizeof((uint8_t[]){__VA_ARGS__})})
110
125
140
150
151struct mqtt_sn_client;
152
161typedef void (*mqtt_sn_evt_cb_t)(struct mqtt_sn_client *client, const struct mqtt_sn_evt *evt);
162
176 int (*init)(struct mqtt_sn_transport *transport);
177
183 void (*deinit)(struct mqtt_sn_transport *transport);
184
196 int (*sendto)(struct mqtt_sn_client *client, void *buf, size_t sz, const void *dest_addr,
197 size_t addrlen);
198
205 ssize_t (*recvfrom)(struct mqtt_sn_client *client, void *rx_buf, size_t rx_len,
206 void *src_addr, size_t *addrlen);
207
218 int (*poll)(struct mqtt_sn_client *client);
219};
220
221#ifdef CONFIG_MQTT_SN_TRANSPORT_UDP
225struct mqtt_sn_transport_udp {
227 struct mqtt_sn_transport tp;
228
230 int sock;
231
233 struct sockaddr bcaddr;
234 socklen_t bcaddrlen;
235};
236
237#define UDP_TRANSPORT(transport) CONTAINER_OF(transport, struct mqtt_sn_transport_udp, tp)
238
246int mqtt_sn_transport_udp_init(struct mqtt_sn_transport_udp *udp, struct sockaddr *gwaddr,
247 socklen_t addrlen);
248#endif
249
321
336int mqtt_sn_client_init(struct mqtt_sn_client *client, const struct mqtt_sn_data *client_id,
337 struct mqtt_sn_transport *transport, mqtt_sn_evt_cb_t evt_cb, void *tx,
338 size_t txsz, void *rx, size_t rxsz);
339
348
360int mqtt_sn_add_gw(struct mqtt_sn_client *client, uint8_t gw_id, struct mqtt_sn_data gw_addr);
361
370int mqtt_sn_search(struct mqtt_sn_client *client, uint8_t radius);
371
381int mqtt_sn_connect(struct mqtt_sn_client *client, bool will, bool clean_session);
382
391
400int mqtt_sn_sleep(struct mqtt_sn_client *client, uint16_t duration);
401
411int mqtt_sn_subscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
412 struct mqtt_sn_data *topic_name);
413
423int mqtt_sn_unsubscribe(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
424 struct mqtt_sn_data *topic_name);
425
439int mqtt_sn_publish(struct mqtt_sn_client *client, enum mqtt_sn_qos qos,
440 struct mqtt_sn_data *topic_name, bool retain, struct mqtt_sn_data *data);
441
452int mqtt_sn_input(struct mqtt_sn_client *client);
453
465 struct mqtt_sn_data *topic_name);
466
467#ifdef __cplusplus
468}
469#endif
470
471#endif /* ZEPHYR_INCLUDE_NET_MQTT_SN_H_ */
472
size_t socklen_t
Length of a socket address.
Definition net_ip.h:171
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:44
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:114
mqtt_sn_return_code
MQTT-SN return codes.
Definition mqtt_sn.h:77
int mqtt_sn_disconnect(struct mqtt_sn_client *client)
Disconnect the client.
mqtt_sn_topic_type
MQTT-SN topic types.
Definition mqtt_sn.h:54
int mqtt_sn_add_gw(struct mqtt_sn_client *client, uint8_t gw_id, struct mqtt_sn_data gw_addr)
Manually add a Gateway, bypasing the normal search process.
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:161
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.
int mqtt_sn_search(struct mqtt_sn_client *client, uint8_t radius)
Initiate the MQTT-SN GW Search process.
@ MQTT_SN_QOS_M1
QOS -1.
Definition mqtt_sn.h:48
@ MQTT_SN_QOS_2
QOS 2.
Definition mqtt_sn.h:47
@ MQTT_SN_QOS_1
QOS 1.
Definition mqtt_sn.h:46
@ MQTT_SN_QOS_0
QOS 0.
Definition mqtt_sn.h:45
@ MQTT_SN_EVT_PINGRESP
Received a PINGRESP.
Definition mqtt_sn.h:120
@ MQTT_SN_EVT_ADVERTISE
Received a ADVERTISE.
Definition mqtt_sn.h:121
@ MQTT_SN_EVT_CONNECTED
Connected to a gateway.
Definition mqtt_sn.h:115
@ MQTT_SN_EVT_PUBLISH
Received a PUBLISH message.
Definition mqtt_sn.h:119
@ MQTT_SN_EVT_ASLEEP
Entered ASLEEP state.
Definition mqtt_sn.h:117
@ MQTT_SN_EVT_DISCONNECTED
Disconnected.
Definition mqtt_sn.h:116
@ MQTT_SN_EVT_AWAKE
Entered AWAKE state.
Definition mqtt_sn.h:118
@ MQTT_SN_EVT_GWINFO
Received a GWINFO.
Definition mqtt_sn.h:122
@ MQTT_SN_EVT_SEARCHGW
Received a SEARCHGW.
Definition mqtt_sn.h:123
@ MQTT_SN_CODE_ACCEPTED
Accepted.
Definition mqtt_sn.h:78
@ MQTT_SN_CODE_REJECTED_CONGESTION
Rejected: congestion.
Definition mqtt_sn.h:79
@ MQTT_SN_CODE_REJECTED_NOTSUP
Rejected: Not Supported.
Definition mqtt_sn.h:81
@ MQTT_SN_CODE_REJECTED_TOPIC_ID
Rejected: Invalid Topic ID.
Definition mqtt_sn.h:80
@ MQTT_SN_TOPIC_TYPE_NORMAL
Normal topic.
Definition mqtt_sn.h:59
@ MQTT_SN_TOPIC_TYPE_PREDEF
Pre-defined topic.
Definition mqtt_sn.h:66
@ MQTT_SN_TOPIC_TYPE_SHORT
Short topic.
Definition mqtt_sn.h:71
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:3985
Structure describing an MQTT-SN client.
Definition mqtt_sn.h:253
int state
Current state of the MQTT-SN client.
Definition mqtt_sn.h:301
uint16_t next_msg_id
Message ID for the next message to be sent.
Definition mqtt_sn.h:289
int64_t ts_gwinfo
Timestamp of the next GWINFO transmission.
Definition mqtt_sn.h:313
uint8_t ping_retries
Number of retries for failed ping attempts.
Definition mqtt_sn.h:307
struct net_buf_simple rx_addr
Buffer for incoming data sender address.
Definition mqtt_sn.h:283
sys_slist_t publish
List of pending publish messages.
Definition mqtt_sn.h:292
struct net_buf_simple tx
Buffer for outgoing data.
Definition mqtt_sn.h:277
enum mqtt_sn_qos will_qos
Quality of Service for the Will message.
Definition mqtt_sn.h:268
struct k_work_delayable process_work
Delayable work structure for processing MQTT-SN events.
Definition mqtt_sn.h:319
sys_slist_t gateway
List of found gateways.
Definition mqtt_sn.h:298
struct mqtt_sn_data client_id
1-23 character unique client ID
Definition mqtt_sn.h:255
struct mqtt_sn_data will_msg
Will message.
Definition mqtt_sn.h:265
int64_t ts_searchgw
Timestamp of the next SEARCHGW transmission.
Definition mqtt_sn.h:310
int64_t radius_gwinfo
Radius of the next GWINFO transmission.
Definition mqtt_sn.h:316
sys_slist_t topic
List of registered topics.
Definition mqtt_sn.h:295
mqtt_sn_evt_cb_t evt_cb
Event callback.
Definition mqtt_sn.h:286
bool will_retain
Flag indicating if the will message should be retained by the broker.
Definition mqtt_sn.h:271
struct mqtt_sn_transport * transport
Underlying transport to be used by the client.
Definition mqtt_sn.h:274
struct net_buf_simple rx
Buffer for incoming data.
Definition mqtt_sn.h:280
int64_t last_ping
Timestamp of the last ping request.
Definition mqtt_sn.h:304
struct mqtt_sn_data will_topic
Topic for Will message.
Definition mqtt_sn.h:260
Abstracts memory buffers.
Definition mqtt_sn.h:85
size_t size
Size of data, in bytes.
Definition mqtt_sn.h:87
const uint8_t * data
Pointer to data.
Definition mqtt_sn.h:86
MQTT-SN event structure to be handled by the event callback.
Definition mqtt_sn.h:144
union mqtt_sn_evt_param param
Event parameters.
Definition mqtt_sn.h:148
enum mqtt_sn_evt_type type
Event type.
Definition mqtt_sn.h:146
Structure to describe an MQTT-SN transport.
Definition mqtt_sn.h:170
int(* sendto)(struct mqtt_sn_client *client, void *buf, size_t sz, const void *dest_addr, size_t addrlen)
Will be called by the library when it wants to send a message.
Definition mqtt_sn.h:196
int(* init)(struct mqtt_sn_transport *transport)
Will be called once on client init to initialize the transport.
Definition mqtt_sn.h:176
int(* poll)(struct mqtt_sn_client *client)
Check if incoming data is available.
Definition mqtt_sn.h:218
void(* deinit)(struct mqtt_sn_transport *transport)
Will be called on client deinit.
Definition mqtt_sn.h:183
ssize_t(* recvfrom)(struct mqtt_sn_client *client, void *rx_buf, size_t rx_len, void *src_addr, size_t *addrlen)
Will be called by the library when it wants to receive a message.
Definition mqtt_sn.h:205
Simple network buffer representation.
Definition net_buf.h:89
Generic sockaddr struct.
Definition net_ip.h:388
Event metadata.
Definition mqtt_sn.h:129
struct mqtt_sn_data data
The payload data associated with the event.
Definition mqtt_sn.h:133
enum mqtt_sn_topic_type topic_type
The type of topic for the event.
Definition mqtt_sn.h:135
struct mqtt_sn_evt_param::@383 publish
Structure holding publish event details.
uint16_t topic_id
The identifier for the topic of the event.
Definition mqtt_sn.h:137