1 /* 2 * Copyright (c) 2018 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** @file mqtt_transport.h 8 * 9 * @brief Internal functions to handle transport in MQTT module. 10 */ 11 12 #ifndef MQTT_TRANSPORT_H_ 13 #define MQTT_TRANSPORT_H_ 14 15 #include <zephyr/net/mqtt.h> 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 /**@brief Transport for handling transport connect procedure. */ 22 typedef int (*transport_connect_handler_t)(struct mqtt_client *client); 23 24 /**@brief Transport write handler. */ 25 typedef int (*transport_write_handler_t)(struct mqtt_client *client, 26 const uint8_t *data, uint32_t datalen); 27 28 /**@brief Transport write message handler, similar to POSIX sendmsg function. */ 29 typedef int (*transport_write_msg_handler_t)(struct mqtt_client *client, 30 const struct msghdr *message); 31 32 /**@brief Transport read handler. */ 33 typedef int (*transport_read_handler_t)(struct mqtt_client *client, uint8_t *data, 34 uint32_t buflen, bool shall_block); 35 36 /**@brief Transport disconnect handler. */ 37 typedef int (*transport_disconnect_handler_t)(struct mqtt_client *client); 38 39 /**@brief Transport procedure handlers. */ 40 struct transport_procedure { 41 /** Transport connect handler. Handles TCP connection callback based on 42 * type of transport. 43 */ 44 transport_connect_handler_t connect; 45 46 /** Transport write handler. Handles transport write based on type of 47 * transport. 48 */ 49 transport_write_handler_t write; 50 51 /** Transport write message handler. Handles transport write based 52 * on type of transport. 53 */ 54 transport_write_msg_handler_t write_msg; 55 56 /** Transport read handler. Handles transport read based on type of 57 * transport. 58 */ 59 transport_read_handler_t read; 60 61 /** Transport disconnect handler. Handles transport disconnection based 62 * on type of transport. 63 */ 64 transport_disconnect_handler_t disconnect; 65 }; 66 67 /**@brief Handles TCP Connection Complete for configured transport. 68 * 69 * @param[in] client Identifies the client on which the procedure is requested. 70 * 71 * @retval 0 or an error code indicating reason for failure. 72 */ 73 int mqtt_transport_connect(struct mqtt_client *client); 74 75 /**@brief Handles write requests on configured transport. 76 * 77 * @param[in] client Identifies the client on which the procedure is requested. 78 * @param[in] data Data to be written on the transport. 79 * @param[in] datalen Length of data to be written on the transport. 80 * 81 * @retval 0 or an error code indicating reason for failure. 82 */ 83 int mqtt_transport_write(struct mqtt_client *client, const uint8_t *data, 84 uint32_t datalen); 85 86 /**@brief Handles write message requests on configured transport. 87 * 88 * @param[in] client Identifies the client on which the procedure is requested. 89 * @param[in] message Pointer to the `struct msghdr` structure, containing data 90 * to be written on the transport. 91 * 92 * @retval 0 or an error code indicating reason for failure. 93 */ 94 int mqtt_transport_write_msg(struct mqtt_client *client, 95 const struct msghdr *message); 96 97 /**@brief Handles read requests on configured transport. 98 * 99 * @param[in] client Identifies the client on which the procedure is requested. 100 * @param[in] data Pointer where read data is to be fetched. 101 * @param[in] buflen Size of memory provided for the operation. 102 * @param[in] shall_block Information whether the call should block or not. 103 * 104 * @retval Number of bytes read or an error code indicating reason for failure. 105 * 0 if connection was closed. 106 */ 107 int mqtt_transport_read(struct mqtt_client *client, uint8_t *data, uint32_t buflen, 108 bool shall_block); 109 110 /**@brief Handles transport disconnection requests on configured transport. 111 * 112 * @param[in] client Identifies the client on which the procedure is requested. 113 * 114 * @retval 0 or an error code indicating reason for failure. 115 */ 116 int mqtt_transport_disconnect(struct mqtt_client *client); 117 118 /* Transport handler functions for TCP socket transport. */ 119 int mqtt_client_tcp_connect(struct mqtt_client *client); 120 int mqtt_client_tcp_write(struct mqtt_client *client, const uint8_t *data, 121 uint32_t datalen); 122 int mqtt_client_tcp_write_msg(struct mqtt_client *client, 123 const struct msghdr *message); 124 int mqtt_client_tcp_read(struct mqtt_client *client, uint8_t *data, 125 uint32_t buflen, bool shall_block); 126 int mqtt_client_tcp_disconnect(struct mqtt_client *client); 127 128 #if defined(CONFIG_MQTT_LIB_TLS) 129 /* Transport handler functions for TLS socket transport. */ 130 int mqtt_client_tls_connect(struct mqtt_client *client); 131 int mqtt_client_tls_write(struct mqtt_client *client, const uint8_t *data, 132 uint32_t datalen); 133 int mqtt_client_tls_write_msg(struct mqtt_client *client, 134 const struct msghdr *message); 135 int mqtt_client_tls_read(struct mqtt_client *client, uint8_t *data, 136 uint32_t buflen, bool shall_block); 137 int mqtt_client_tls_disconnect(struct mqtt_client *client); 138 #endif /* CONFIG_MQTT_LIB_TLS */ 139 140 #if defined(CONFIG_MQTT_LIB_WEBSOCKET) 141 int mqtt_client_websocket_connect(struct mqtt_client *client); 142 int mqtt_client_websocket_write(struct mqtt_client *client, const uint8_t *data, 143 uint32_t datalen); 144 int mqtt_client_websocket_write_msg(struct mqtt_client *client, 145 const struct msghdr *message); 146 int mqtt_client_websocket_read(struct mqtt_client *client, uint8_t *data, 147 uint32_t buflen, bool shall_block); 148 int mqtt_client_websocket_disconnect(struct mqtt_client *client); 149 #endif 150 151 #if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT) 152 int mqtt_client_custom_transport_connect(struct mqtt_client *client); 153 int mqtt_client_custom_transport_write(struct mqtt_client *client, const uint8_t *data, 154 uint32_t datalen); 155 int mqtt_client_custom_transport_write_msg(struct mqtt_client *client, 156 const struct msghdr *message); 157 int mqtt_client_custom_transport_read(struct mqtt_client *client, uint8_t *data, 158 uint32_t buflen, bool shall_block); 159 int mqtt_client_custom_transport_disconnect(struct mqtt_client *client); 160 #endif 161 162 #ifdef __cplusplus 163 } 164 #endif 165 166 #endif /* MQTT_TRANSPORT_H_ */ 167