open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
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
32
33
34
35/** UNIX-like platform support */
36#if defined(__unix__) || defined(__APPLE__) || defined(__NuttX__)
37 #include <limits.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <time.h>
41 #include <arpa/inet.h>
42 #include <pthread.h>
43
44 #define MQTT_PAL_HTONS(s) htons(s)
45 #define MQTT_PAL_NTOHS(s) ntohs(s)
46
47 #define MQTT_PAL_TIME() time(NULL)
48
49 typedef time_t mqtt_pal_time_t;
50 typedef pthread_mutex_t mqtt_pal_mutex_t;
51
52 #define MQTT_PAL_MUTEX_INIT(mtx_ptr) pthread_mutex_init(mtx_ptr, NULL)
53 #define MQTT_PAL_MUTEX_LOCK(mtx_ptr) pthread_mutex_lock(mtx_ptr)
54 #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) pthread_mutex_unlock(mtx_ptr)
55
56 #if !defined(MQTT_USE_CUSTOM_SOCKET_HANDLE)
57 #if defined(MQTT_USE_MBEDTLS)
58 struct mbedtls_ssl_context;
59 typedef struct mbedtls_ssl_context *mqtt_pal_socket_handle;
60 #elif defined(MQTT_USE_WOLFSSL)
61 #include <wolfssl/ssl.h>
62 typedef WOLFSSL* mqtt_pal_socket_handle;
63 #elif defined(MQTT_USE_BIO)
64 #include <openssl/bio.h>
65 typedef BIO* mqtt_pal_socket_handle;
66 #elif defined(MQTT_USE_BEARSSL)
67 #include <bearssl.h>
68
69 typedef struct _bearssl_context {
70 br_ssl_client_context sc;
71 br_x509_minimal_context xc;
72 br_sslio_context ioc;
73 size_t ta_count;
74 br_x509_trust_anchor *anchOut;
75 int fd;
76 int (*low_read)(void *read_context, unsigned char *buf, size_t len);
77 int (*low_write)(void *write_context, const unsigned char *buf, size_t len);
78 } bearssl_context;
79
80 typedef bearssl_context* mqtt_pal_socket_handle;
81 #else
82 typedef int mqtt_pal_socket_handle;
83 #endif
84 #endif
85#elif defined(_MSC_VER) || defined(WIN32)
86 #include <limits.h>
87 #include <winsock2.h>
88 #include <windows.h>
89 #include <time.h>
90 #include <stdint.h>
91
92 typedef SSIZE_T ssize_t;
93 #define MQTT_PAL_HTONS(s) htons(s)
94 #define MQTT_PAL_NTOHS(s) ntohs(s)
95
96 #define MQTT_PAL_TIME() time(NULL)
97
98 typedef time_t mqtt_pal_time_t;
99 typedef CRITICAL_SECTION mqtt_pal_mutex_t;
100
101 #define MQTT_PAL_MUTEX_INIT(mtx_ptr) InitializeCriticalSection(mtx_ptr)
102 #define MQTT_PAL_MUTEX_LOCK(mtx_ptr) EnterCriticalSection(mtx_ptr)
103 #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) LeaveCriticalSection(mtx_ptr)
104
105
106 #if !defined(MQTT_USE_CUSTOM_SOCKET_HANDLE)
107 #if defined(MQTT_USE_BIO)
108 #include <openssl/bio.h>
109 typedef BIO* mqtt_pal_socket_handle;
110 #else
111 typedef SOCKET mqtt_pal_socket_handle;
112 #endif
113 #endif
114
115#endif
116
117
118ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags);
119
120
121ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags);
122
123#if defined(__cplusplus)
124}
125#endif
126
127
128#endif
ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void *buf, size_t bufsz, int flags)
ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void *buf, size_t len, int flags)
UNIX-like platform support.