-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathazure_iot_transport_interface.h
165 lines (156 loc) · 6.31 KB
/
azure_iot_transport_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License. */
/**
* @file azure_iot_transport_interface.h
* @brief Transport interface definition to send and receive data over the
* network.
*/
#ifndef AZURE_IOT_TRANSPORT_INTERFACE_H
#define AZURE_IOT_TRANSPORT_INTERFACE_H
#include <stdint.h>
#include <stddef.h>
/**
* @brief The transport interface definition.
*
* The transport interface is a set of APIs that must be implemented using an
* external transport layer protocol. The transport interface is defined in
* @ref transport_interface.h. This interface allows protocols like MQTT
* to send and receive data over the transport layer. This
* interface does not handle connection and disconnection to the server of
* interest. The connection, disconnection, and other transport settings, like
* timeout and TLS setup, must be handled in the user application.
* <br>
*
* The functions that must be implemented are:<br>
* - [Transport Receive](@ref AzureIoTTransportRecv_t)
* - [Transport Send](@ref AzureIoTTransportSend_t)
*
* Each of the functions above take in an opaque context @ref struct NetworkContext.
* The functions above and the context are also grouped together in the
* @ref AzureIoTTransportInterface_t structure:<br><br>
* @snippet this define_transportinterface
* <br>
*
* The following steps give guidance on implementing the transport interface:
*
* -# Implementing @ref struct NetworkContext<br><br>
* @snippet this define_networkcontext
* <br>
* The implemented struct NetworkContext must contain all of the information
* that is needed to receive and send data with the @ref AzureIoTTransportRecv_t
* and the @ref AzureIoTTransportSend_t implementations.<br>
* In the case of TLS over TCP, struct NetworkContext is typically implemented
* with the TCP socket context and a TLS context.<br><br>
* <b>Example code:</b>
* @code{c}
* struct NetworkContext
* {
* struct MyTCPSocketContext tcpSocketContext;
* struct MyTLSContext tlsContext;
* };
* @endcode
* <br>
* -# Implementing @ref AzureIoTTransportRecv_t<br><br>
* @snippet this define_transportrecv
* <br>
* This function is expected to populate a buffer, with bytes received from the
* transport, and return the number of bytes placed in the buffer.
* In the case of TLS over TCP, @ref AzureIoTTransportRecv_t is typically implemented by
* calling the TLS layer function to receive data. In case of plaintext TCP
* without TLS, it is typically implemented by calling the TCP layer receive
* function. @ref AzureIoTTransportRecv_t may be invoked multiple times by the protocol
* library, if fewer bytes than were requested to receive are returned.
* <br><br>
* <b>Example code:</b>
* @code{c}
* int32_t myNetworkRecvImplementation( struct NetworkContext * pNetworkContext,
* void * pBuffer,
* size_t bytesToRecv )
* {
* int32_t bytesReceived = 0;
* bytesReceived = TLSRecv( pNetworkContext->tlsContext,
* pBuffer,
* bytesToRecv,
* MY_SOCKET_TIMEOUT );
* if( bytesReceived < 0 )
* {
* // Handle socket error.
* }
* // Handle other cases.
*
* return bytesReceived;
* }
* @endcode
* <br>
* -# Implementing @ref AzureIoTTransportSend_t<br><br>
* @snippet this define_transportsend
* <br>
* This function is expected to send the bytes, in the given buffer over the
* transport, and return the number of bytes sent.
* In the case of TLS over TCP, @ref AzureIoTTransportSend_t is typically implemented by
* calling the TLS layer function to send data. In case of plaintext TCP
* without TLS, it is typically implemented by calling the TCP layer send
* function. @ref AzureIoTTransportSend_t may be invoked multiple times by the protocol
* library, if fewer bytes than were requested to send are returned.
* <br><br>
* <b>Example code:</b>
* @code{c}
* int32_t myNetworkSendImplementation( struct NetworkContext * pNetworkContext,
* const void * pBuffer,
* size_t bytesToSend )
* {
* int32_t bytesSent = 0;
* bytesSent = TLSSend( pNetworkContext->tlsContext,
* pBuffer,
* bytesToSend,
* MY_SOCKET_TIMEOUT );
* if( bytesSent < 0 )
* {
* // Handle socket error.
* }
* // Handle other cases.
*
* return bytesSent;
* }
* @endcode
*/
/**
* @brief The NetworkContext is an incomplete type. An implementation of this
* interface must define struct NetworkContext for the system requirements.
* This context is passed into the network interface functions.
*/
struct NetworkContext;
/**
* @brief User defined function for receiving data on the network.
*
* @param[in] pxNetworkContext Implementation-defined network context.
* @param[in] pvBuffer Buffer to receive the data into.
* @param[in] xBytesToRecv Number of bytes requested from the network.
*
* @return The number of bytes received or a negative error code.
*/
typedef int32_t ( * AzureIoTTransportRecv_t )( struct NetworkContext * pxNetworkContext,
void * pvBuffer,
size_t xBytesToRecv );
/**
* @brief User defined function for sending data on the network.
*
* @param[in] pxNetworkContext Implementation-defined network context.
* @param[in] pvBuffer Buffer containing the bytes to send over the network stack.
* @param[in] xBytesToSend Number of bytes to send over the network.
*
* @return The number of bytes sent or a negative error code.
*/
typedef int32_t ( * AzureIoTTransportSend_t )( struct NetworkContext * pxNetworkContext,
const void * pvBuffer,
size_t xBytesToSend );
/**
* @brief The transport layer interface.
*/
typedef struct AzureIoTTransportInterface
{
AzureIoTTransportRecv_t xRecv; /**< Transport receive interface. */
AzureIoTTransportSend_t xSend; /**< Transport send interface. */
struct NetworkContext * pxNetworkContext; /**< Implementation-defined network context. */
} AzureIoTTransportInterface_t;
#endif /* AZURE_IOT_TRANSPORT_INTERFACE_H */