pal_socket.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * \copyright
  3. * Copyright© 2018, Infineon Technologies AG
  4. * All rights reserved.
  5. *
  6. * This software is provided with terms and conditions as specified in OPTIGA™ Trust X Evaluation Kit License Agreement.
  7. * \endcopyright
  8. *
  9. * \author Infineon AG
  10. *
  11. * \file
  12. *
  13. * \brief This file implements the prototype declarations of pal socket functionalities
  14. * \ingroup grPAL
  15. * @{
  16. */
  17. #ifndef _PAL_SOCKET_H_
  18. #define _PAL_SOCKET_H_
  19. /**********************************************************************************************************************
  20. * HEADER FILES
  21. *********************************************************************************************************************/
  22. #ifndef WIN32
  23. #include "Datatypes.h"
  24. #include "DAVE.h"
  25. #include "udp.h"
  26. #include "inet.h"
  27. #else
  28. #include <winsock2.h>
  29. #include "Datatypes.h"
  30. #endif
  31. #include "ErrorCodes.h"
  32. #include "UDPErrorCodes.h"
  33. /// @cond hidden
  34. /**********************************************************************************************************************
  35. * MACROS
  36. *********************************************************************************************************************/
  37. #ifndef WIN32
  38. #define IPAddressParse(pzIpAddress, psIPAddress) (inet_aton(pzIpAddress, psIPAddress))
  39. #else
  40. #define IPAddressParse(pzIpAddress, psIPAddress) (1)
  41. #endif
  42. /// @endcond
  43. /**********************************************************************************************************************
  44. * ENUMS
  45. *********************************************************************************************************************/
  46. /**********************************************************************************************************************
  47. * DATA STRUCTURES
  48. *********************************************************************************************************************/
  49. #ifndef WIN32
  50. /**
  51. * \brief Pointer type definition of pal socket receive event callback
  52. */
  53. typedef void (*pal_socket_event_listener)(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  54. ip_addr_t *addr, u16_t port);
  55. #endif
  56. /**
  57. * \brief This structure contains socket communication data
  58. */
  59. typedef enum eRecvMode_d
  60. {
  61. eBlock = 0x10,
  62. eNonBlock = 0x20
  63. }eRecvMode_d;
  64. /**
  65. * \brief This structure contains socket communication data
  66. */
  67. #ifndef WIN32
  68. typedef struct pal_socket
  69. {
  70. ///UDP structure Tx
  71. struct udp_pcb *pcbTx;
  72. ///UDP structure Rx
  73. struct udp_pcb *pcbRx;
  74. //Received IP address
  75. ip_addr_t sIPAddress;
  76. ///Function pointer to hold receive callback
  77. pal_socket_event_listener pfListen;
  78. ///Port for UDP communication
  79. uint16_t wPort;
  80. ///Transport Layer Timeout
  81. uint16_t wTimeout;
  82. ///Enumeration to indicate Blocking or Non blocking
  83. uint8_t bMode;
  84. } pal_socket_t;
  85. #else
  86. typedef struct pal_socket
  87. {
  88. ///Received IP address
  89. char* sIPAddress;
  90. ///Port for UDP communication
  91. uint16_t wPort;
  92. ///Pointer to the socket for Receiving
  93. SOCKET SocketHdl;
  94. ///IPv4 Socket address for Receiving
  95. SOCKADDR_IN sSocketAddrIn;
  96. ///Transport Layer Timeout
  97. uint16_t wTimeout;
  98. ///Enumeration to indicate Blocking or Non blocking
  99. uint8_t bMode;
  100. } pal_socket_t;
  101. #endif
  102. /**********************************************************************************************************************
  103. * API Prototypes
  104. *********************************************************************************************************************/
  105. /**
  106. * \brief Assign IP address
  107. */
  108. #ifndef WIN32
  109. int32_t pal_socket_assign_ip_address(const char* p_ip_address,void *p_input_ip_address);
  110. #else
  111. int32_t pal_socket_assign_ip_address(const char_t* p_ip_address,char** p_input_ip_address);
  112. #endif
  113. /**
  114. * \brief Initializes the socket communication structure
  115. */
  116. int32_t pal_socket_init(pal_socket_t* p_socket);
  117. /**
  118. * \brief Creates server port and bind
  119. */
  120. int32_t pal_socket_open(pal_socket_t* p_socket,
  121. uint16_t port);
  122. /**
  123. * \brief Creates a client port and connect
  124. */
  125. int32_t pal_socket_connect(pal_socket_t* p_socket,
  126. uint16_t port);
  127. /**
  128. * \brief Receive data from the client
  129. */
  130. int32_t pal_socket_listen(pal_socket_t* p_socket, uint8_t *p_data,
  131. uint32_t *p_length);
  132. /**
  133. * \brief Sends the data to the the client
  134. */
  135. int32_t pal_socket_send(const pal_socket_t* p_socket, uint8_t *p_data,
  136. uint32_t length);
  137. /**
  138. * \brief Closes the socket communication and release the udp port
  139. */
  140. void pal_socket_close(pal_socket_t* p_socket);
  141. #endif //_PAL_SOCKET_H_
  142. /**
  143. * @}
  144. */