mqtt_pal.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef __MQTT_PAL_H__
  2. #define __MQTT_PAL_H__
  3. /**
  4. * Copyright (c) 2019 Kalycito Infotech Private Limited
  5. * @file
  6. * @brief Includes/supports the types/calls required by the MQTT-C client.
  7. *
  8. * @note This is the \em only file included in mqtt.h, and mqtt.c. It is therefore
  9. * responsible for including/supporting all the required types and calls.
  10. *
  11. * @defgroup pal Platform abstraction layer
  12. * @brief Documentation of the types and calls required to port MQTT-C to a new platform.
  13. *
  14. * mqtt_pal.h is the \em only header file included in mqtt.c. Therefore, to port MQTT-C to a
  15. * new platform the following types, functions, constants, and macros must be defined in
  16. * mqtt_pal.h:
  17. * - Types:
  18. * - \c size_t, \c ssize_t
  19. * - \c uint8_t, \c uint16_t, \c uint32_t
  20. * - \c va_list
  21. * - \c mqtt_pal_time_t : return type of \c MQTT_PAL_TIME()
  22. * - \c mqtt_pal_mutex_t : type of the argument that is passed to \c MQTT_PAL_MUTEX_LOCK and
  23. * \c MQTT_PAL_MUTEX_RELEASE
  24. * - Functions:
  25. * - \c memcpy, \c strlen
  26. * - \c va_start, \c va_arg, \c va_end
  27. * - Constants:
  28. * - \c INT_MIN
  29. *
  30. * Additionally, three macro's are required:
  31. * - \c MQTT_PAL_HTONS(s) : host-to-network endian conversion for uint16_t.
  32. * - \c MQTT_PAL_NTOHS(s) : network-to-host endian conversion for uint16_t.
  33. * - \c MQTT_PAL_TIME() : returns [type: \c mqtt_pal_time_t] current time in seconds.
  34. * - \c MQTT_PAL_MUTEX_LOCK(mtx_pointer) : macro that locks the mutex pointed to by \c mtx_pointer.
  35. * - \c MQTT_PAL_MUTEX_RELEASE(mtx_pointer) : macro that unlocks the mutex pointed to by
  36. * \c mtx_pointer.
  37. *
  38. * Lastly, \ref mqtt_pal_sendall and \ref mqtt_pal_recvall, must be implemented in mqtt_pal.c
  39. * for sending and receiving data using the platforms socket calls.
  40. */
  41. #include <open62541/types.h>
  42. /* UNIX-like platform support */
  43. #ifdef __unix__
  44. #include <limits.h>
  45. #include <string.h>
  46. #include <stdarg.h>
  47. #include <time.h>
  48. #include <arpa/inet.h>
  49. #include <pthread.h>
  50. /*#ifdef MQTT_USE_BIO
  51. #include <openssl/bio.h>
  52. typedef BIO* mqtt_pal_socket_handle;
  53. #else
  54. typedef int mqtt_pal_socket_handle;
  55. #endif*/
  56. #endif
  57. #ifdef __MINGW32__
  58. #include <pthread.h>
  59. #endif
  60. #define MQTT_PAL_HTONS(s) htons(s)
  61. #define MQTT_PAL_NTOHS(s) ntohs(s)
  62. #define MQTT_PAL_TIME() time(NULL)
  63. typedef time_t mqtt_pal_time_t;
  64. typedef pthread_mutex_t mqtt_pal_mutex_t;
  65. #define MQTT_PAL_MUTEX_INIT(mtx_ptr) pthread_mutex_init(mtx_ptr, NULL)
  66. #define MQTT_PAL_MUTEX_LOCK(mtx_ptr) pthread_mutex_lock(mtx_ptr)
  67. #define MQTT_PAL_MUTEX_UNLOCK(mtx_ptr) pthread_mutex_unlock(mtx_ptr)
  68. struct my_custom_socket_handle {
  69. void* client;
  70. void* connection;
  71. uint16_t timeout;
  72. };
  73. typedef struct my_custom_socket_handle* mqtt_pal_socket_handle;
  74. /**
  75. * @brief Sends all the bytes in a buffer.
  76. * @ingroup pal
  77. *
  78. * @param[in] fd The file-descriptor (or handle) of the socket.
  79. * @param[in] buf A pointer to the first byte in the buffer to send.
  80. * @param[in] len The number of bytes to send (starting at \p buf).
  81. * @param[in] flags Flags which are passed to the underlying socket.
  82. *
  83. * @returns The number of bytes sent if successful, an \ref MQTTErrors otherwise.
  84. */
  85. ssize_t mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags);
  86. /**
  87. * @brief Non-blocking receive all the byte available.
  88. * @ingroup pal
  89. *
  90. * @param[in] fd The file-descriptor (or handle) of the socket.
  91. * @param[in] buf A pointer to the receive buffer.
  92. * @param[in] bufsz The max number of bytes that can be put into \p buf.
  93. * @param[in] flags Flags which are passed to the underlying socket.
  94. *
  95. * @returns The number of bytes received if successful, an \ref MQTTErrors otherwise.
  96. */
  97. ssize_t mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags);
  98. #endif /* __MQTT_PAL_H__ */