MQTT-C
mqtt_pal.h
Go to the documentation of this file.
1#if !defined(__MQTT_PAL_H__)
2#define __MQTT_PAL_H__
3
4/*
5MIT License
6
7Copyright(c) 2018 Liam Bindle
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files(the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions :
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26*/
27
28#if defined(__cplusplus)
29extern "C" {
30#endif
31
68
69#if defined(__DOXYGEN)
74 #define MQTT_PAL_HTONS(s)
75
80 #define MQTT_PAL_NTOHS(s)
81
86 #define MQTT_PAL_TIME()
87
92 #define MQTT_PAL_MUTEX_INIT(mtx_ptr)
93
98 #define MQTT_PAL_MUTEX_LOCK(mtx_ptr)
99
104 #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr)
105
110 typedef void mqtt_pal_time_t;
111
117 typedef void mqtt_pal_mutex_t;
118
119#elif defined(__unix__) || defined(__APPLE__) || defined(__NuttX__)
120 /* UNIX-like platform support */
121 #include <limits.h>
122 #include <string.h>
123 #include <stdarg.h>
124 #include <time.h>
125 #include <arpa/inet.h>
126 #include <pthread.h>
127
128 #define MQTT_PAL_HTONS(s) htons(s)
129 #define MQTT_PAL_NTOHS(s) ntohs(s)
130
131 #define MQTT_PAL_TIME() time(NULL)
132
133 typedef time_t mqtt_pal_time_t;
134 typedef pthread_mutex_t mqtt_pal_mutex_t;
135
136 #define MQTT_PAL_MUTEX_INIT(mtx_ptr) pthread_mutex_init(mtx_ptr, NULL)
137 #define MQTT_PAL_MUTEX_LOCK(mtx_ptr) pthread_mutex_lock(mtx_ptr)
138 #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) pthread_mutex_unlock(mtx_ptr)
139
140 #if !defined(MQTT_USE_CUSTOM_SOCKET_HANDLE)
141 #if defined(MQTT_USE_MBEDTLS)
142 struct mbedtls_ssl_context;
143 typedef struct mbedtls_ssl_context *mqtt_pal_socket_handle;
144 #elif defined(MQTT_USE_WOLFSSL)
145 #include <wolfssl/ssl.h>
146 typedef WOLFSSL* mqtt_pal_socket_handle;
147 #elif defined(MQTT_USE_BIO)
148 #include <openssl/bio.h>
149 typedef BIO* mqtt_pal_socket_handle;
150 #elif defined(MQTT_USE_BEARSSL)
151 #include <bearssl.h>
152
153 typedef struct _bearssl_context {
154 br_ssl_client_context sc;
155 br_x509_minimal_context xc;
156 br_sslio_context ioc;
157 size_t ta_count;
158 br_x509_trust_anchor *anchOut;
159 int fd;
160 int (*low_read)(void *read_context, unsigned char *buf, size_t len);
161 int (*low_write)(void *write_context, const unsigned char *buf, size_t len);
162 } bearssl_context;
163
164 typedef bearssl_context* mqtt_pal_socket_handle;
165 #else
166 typedef int mqtt_pal_socket_handle;
167 #endif
168 #endif
169#elif defined(_MSC_VER) || defined(WIN32)
170 #include <limits.h>
171 #include <winsock2.h>
172 #include <windows.h>
173 #include <time.h>
174 #include <stdint.h>
175
176 typedef SSIZE_T ssize_t;
177 #define MQTT_PAL_HTONS(s) htons(s)
178 #define MQTT_PAL_NTOHS(s) ntohs(s)
179
180 #define MQTT_PAL_TIME() time(NULL)
181
182 typedef time_t mqtt_pal_time_t;
183 typedef CRITICAL_SECTION mqtt_pal_mutex_t;
184
185 #define MQTT_PAL_MUTEX_INIT(mtx_ptr) InitializeCriticalSection(mtx_ptr)
186 #define MQTT_PAL_MUTEX_LOCK(mtx_ptr) EnterCriticalSection(mtx_ptr)
187 #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) LeaveCriticalSection(mtx_ptr)
188
189
190 #if !defined(MQTT_USE_CUSTOM_SOCKET_HANDLE)
191 #if defined(MQTT_USE_BIO)
192 #include <openssl/bio.h>
193 typedef BIO* mqtt_pal_socket_handle;
194 #else
195 typedef SOCKET mqtt_pal_socket_handle;
196 #endif
197 #endif
198
199#endif
200
219ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags);
220
239ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags);
240
241#if defined(__cplusplus)
242}
243#endif
244
245
246#endif
ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void *buf, size_t bufsz, int flags)
Non-blocking receive all the byte available.
void mqtt_pal_mutex_t
type of the argument that is passed to MQTT_PAL_MUTEX_INIT(), MQTT_PAL_MUTEX_LOCK() and MQTT_PAL_MUTE...
Definition mqtt_pal.h:117
ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void *buf, size_t len, int flags)
Sends all the bytes in a buffer.
void mqtt_pal_time_t
return type of MQTT_PAL_TIME()
Definition mqtt_pal.h:110