1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file mqtt_transport.c
8  *
9  * @brief Internal functions to handle transport in MQTT module.
10  */
11 
12 #include "mqtt_transport.h"
13 
14 /**@brief Function pointer array for TCP/TLS transport handlers. */
15 const struct transport_procedure transport_fn[MQTT_TRANSPORT_NUM] = {
16 	{
17 		mqtt_client_tcp_connect,
18 		mqtt_client_tcp_write,
19 		mqtt_client_tcp_write_msg,
20 		mqtt_client_tcp_read,
21 		mqtt_client_tcp_disconnect,
22 	},
23 #if defined(CONFIG_MQTT_LIB_TLS)
24 	{
25 		mqtt_client_tls_connect,
26 		mqtt_client_tls_write,
27 		mqtt_client_tls_write_msg,
28 		mqtt_client_tls_read,
29 		mqtt_client_tls_disconnect,
30 	},
31 #endif /* CONFIG_MQTT_LIB_TLS */
32 #if defined(CONFIG_MQTT_LIB_WEBSOCKET)
33 	{
34 		mqtt_client_websocket_connect,
35 		mqtt_client_websocket_write,
36 		mqtt_client_websocket_write_msg,
37 		mqtt_client_websocket_read,
38 		mqtt_client_websocket_disconnect,
39 	},
40 #if defined(CONFIG_MQTT_LIB_TLS)
41 	{
42 		mqtt_client_websocket_connect,
43 		mqtt_client_websocket_write,
44 		mqtt_client_websocket_write_msg,
45 		mqtt_client_websocket_read,
46 		mqtt_client_websocket_disconnect,
47 	},
48 #endif /* CONFIG_MQTT_LIB_TLS */
49 #endif /* CONFIG_MQTT_LIB_WEBSOCKET */
50 #if defined(CONFIG_MQTT_LIB_CUSTOM_TRANSPORT)
51 	{
52 		mqtt_client_custom_transport_connect,
53 		mqtt_client_custom_transport_write,
54 		mqtt_client_custom_transport_write_msg,
55 		mqtt_client_custom_transport_read,
56 		mqtt_client_custom_transport_disconnect,
57 	},
58 #endif /* CONFIG_MQTT_LIB_CUSTOM_TRANSPORT */
59 };
60 
mqtt_transport_connect(struct mqtt_client * client)61 int mqtt_transport_connect(struct mqtt_client *client)
62 {
63 	return transport_fn[client->transport.type].connect(client);
64 }
65 
mqtt_transport_write(struct mqtt_client * client,const uint8_t * data,uint32_t datalen)66 int mqtt_transport_write(struct mqtt_client *client, const uint8_t *data,
67 			 uint32_t datalen)
68 {
69 	return transport_fn[client->transport.type].write(client, data,
70 							  datalen);
71 }
72 
mqtt_transport_write_msg(struct mqtt_client * client,const struct msghdr * message)73 int mqtt_transport_write_msg(struct mqtt_client *client,
74 			     const struct msghdr *message)
75 {
76 	return transport_fn[client->transport.type].write_msg(client, message);
77 }
78 
mqtt_transport_read(struct mqtt_client * client,uint8_t * data,uint32_t buflen,bool shall_block)79 int mqtt_transport_read(struct mqtt_client *client, uint8_t *data, uint32_t buflen,
80 			bool shall_block)
81 {
82 	return transport_fn[client->transport.type].read(client, data, buflen,
83 							 shall_block);
84 }
85 
mqtt_transport_disconnect(struct mqtt_client * client)86 int mqtt_transport_disconnect(struct mqtt_client *client)
87 {
88 	return transport_fn[client->transport.type].disconnect(client);
89 }
90