@@ -75,16 +75,16 @@ enum mqtt_sn_topic_type {
7575 * MQTT-SN return codes.
7676 */
7777enum mqtt_sn_return_code {
78- MQTT_SN_CODE_ACCEPTED = 0x00 , /**< Accepted */
78+ MQTT_SN_CODE_ACCEPTED = 0x00 , /**< Accepted */
7979 MQTT_SN_CODE_REJECTED_CONGESTION = 0x01 , /**< Rejected: congestion */
80- MQTT_SN_CODE_REJECTED_TOPIC_ID = 0x02 , /**< Rejected: Invalid Topic ID */
81- MQTT_SN_CODE_REJECTED_NOTSUP = 0x03 , /**< Rejected: Not Supported */
80+ MQTT_SN_CODE_REJECTED_TOPIC_ID = 0x02 , /**< Rejected: Invalid Topic ID */
81+ MQTT_SN_CODE_REJECTED_NOTSUP = 0x03 , /**< Rejected: Not Supported */
8282};
8383
8484/** @brief Abstracts memory buffers. */
8585struct mqtt_sn_data {
8686 const uint8_t * data ; /**< Pointer to data. */
87- uint16_t size ; /**< Size of data, in bytes. */
87+ size_t size ; /**< Size of data, in bytes. */
8888};
8989
9090/**
@@ -105,19 +105,22 @@ struct mqtt_sn_data {
105105 *
106106 * struct mqtt_sn_data data = MQTT_SN_DATA_BYTES(0x13, 0x37);
107107 */
108- #define MQTT_SN_DATA_BYTES (...) \
109- ((struct mqtt_sn_data) { (uint8_t[]){ __VA_ARGS__ }, sizeof((uint8_t[]){ __VA_ARGS__ })})
108+ #define MQTT_SN_DATA_BYTES (...) \
109+ ((struct mqtt_sn_data){ (uint8_t[]){__VA_ARGS__}, sizeof((uint8_t[]){__VA_ARGS__})})
110110
111111/**
112112 * Event types that can be emitted by the library.
113113 */
114114enum mqtt_sn_evt_type {
115- MQTT_SN_EVT_CONNECTED , /**< Connected to a gateway */
115+ MQTT_SN_EVT_CONNECTED , /**< Connected to a gateway */
116116 MQTT_SN_EVT_DISCONNECTED , /**< Disconnected */
117- MQTT_SN_EVT_ASLEEP , /**< Entered ASLEEP state */
118- MQTT_SN_EVT_AWAKE , /**< Entered AWAKE state */
119- MQTT_SN_EVT_PUBLISH , /**< Received a PUBLISH message */
120- MQTT_SN_EVT_PINGRESP /**< Received a PINGRESP */
117+ MQTT_SN_EVT_ASLEEP , /**< Entered ASLEEP state */
118+ MQTT_SN_EVT_AWAKE , /**< Entered AWAKE state */
119+ MQTT_SN_EVT_PUBLISH , /**< Received a PUBLISH message */
120+ MQTT_SN_EVT_PINGRESP , /**< Received a PINGRESP */
121+ MQTT_SN_EVT_ADVERTISE , /**< Received a ADVERTISE */
122+ MQTT_SN_EVT_GWINFO , /**< Received a GWINFO */
123+ MQTT_SN_EVT_SEARCHGW /**< Received a SEARCHGW */
121124};
122125
123126/**
@@ -180,16 +183,27 @@ struct mqtt_sn_transport {
180183 void (* deinit )(struct mqtt_sn_transport * transport );
181184
182185 /**
183- * Will be called by the library when it wants to send a message.
186+ * @brief Will be called by the library when it wants to send a message.
187+ *
188+ * Implementations should follow sendto conventions with exceptions.
189+ * When dest_addr == NULL, message should be broadcast with addrlen being
190+ * the broadcast radius. This should also handle setting up/destroying
191+ * connections as required when the address changes.
192+ *
193+ * @return ENOERR on connection+transmission success, Negative values
194+ * signal errors.
184195 */
185- int (* msg_send )(struct mqtt_sn_client * client , void * buf , size_t sz );
196+ int (* sendto )(struct mqtt_sn_client * client , void * buf , size_t sz , const void * dest_addr ,
197+ size_t addrlen );
186198
187199 /**
188200 * @brief Will be called by the library when it wants to receive a message.
189201 *
190- * Implementations should follow recv conventions.
202+ * Implementations should follow recvfrom conventions with the exception
203+ * of a NULL src_addr being a broadcast message.
191204 */
192- ssize_t (* recv )(struct mqtt_sn_client * client , void * buffer , size_t length );
205+ ssize_t (* recvfrom )(struct mqtt_sn_client * client , void * rx_buf , size_t rx_len ,
206+ void * src_addr , size_t * addrlen );
193207
194208 /**
195209 * @brief Check if incoming data is available.
@@ -215,9 +229,9 @@ struct mqtt_sn_transport_udp {
215229 /** Socket FD */
216230 int sock ;
217231
218- /** Address of the gateway */
219- struct sockaddr gwaddr ;
220- socklen_t gwaddrlen ;
232+ /** Address of broadcasts */
233+ struct sockaddr bcaddr ;
234+ socklen_t bcaddrlen ;
221235};
222236
223237#define UDP_TRANSPORT (transport ) CONTAINER_OF(transport, struct mqtt_sn_transport_udp, tp)
@@ -265,6 +279,9 @@ struct mqtt_sn_client {
265279 /** Buffer for incoming data */
266280 struct net_buf_simple rx ;
267281
282+ /** Buffer for incoming data sender address */
283+ struct net_buf_simple rx_addr ;
284+
268285 /** Event callback */
269286 mqtt_sn_evt_cb_t evt_cb ;
270287
@@ -277,6 +294,9 @@ struct mqtt_sn_client {
277294 /** List of registered topics */
278295 sys_slist_t topic ;
279296
297+ /** List of found gateways */
298+ sys_slist_t gateway ;
299+
280300 /** Current state of the MQTT-SN client */
281301 int state ;
282302
@@ -286,6 +306,15 @@ struct mqtt_sn_client {
286306 /** Number of retries for failed ping attempts */
287307 uint8_t ping_retries ;
288308
309+ /** Timestamp of the next SEARCHGW transmission */
310+ int64_t ts_searchgw ;
311+
312+ /** Timestamp of the next GWINFO transmission */
313+ int64_t ts_gwinfo ;
314+
315+ /** Radius of the next GWINFO transmission */
316+ int64_t radius_gwinfo ;
317+
289318 /** Delayable work structure for processing MQTT-SN events */
290319 struct k_work_delayable process_work ;
291320};
@@ -317,6 +346,29 @@ int mqtt_sn_client_init(struct mqtt_sn_client *client, const struct mqtt_sn_data
317346 */
318347void mqtt_sn_client_deinit (struct mqtt_sn_client * client );
319348
349+ /**
350+ * @brief Manually add a Gateway, bypasing the normal search process.
351+ *
352+ * This function manually creates a gateway that is stored internal to the library.
353+ *
354+ * @param client The MQTT-SN client to connect.
355+ * @param gw_id Single byte Gateway Identifier
356+ * @param gw_addr Address data structure to be used by the transport layer.
357+ *
358+ * @return 0 or a negative error code (errno.h) indicating reason of failure.
359+ */
360+ int mqtt_sn_add_gw (struct mqtt_sn_client * client , uint8_t gw_id , struct mqtt_sn_data gw_addr );
361+
362+ /**
363+ * @brief Initiate the MQTT-SN GW Search process.
364+ *
365+ * @param client The MQTT-SN client to connect.
366+ * @param radius Broadcast radius for the search message.
367+ *
368+ * @return 0 or a negative error code (errno.h) indicating reason of failure.
369+ */
370+ int mqtt_sn_search (struct mqtt_sn_client * client , uint8_t radius );
371+
320372/**
321373 * @brief Connect the client.
322374 *
0 commit comments