hal_ethernet.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * ethernet_hal.h
  3. *
  4. * Copyright 2013-2021 Michael Zillgith
  5. *
  6. * This file is part of Platform Abstraction Layer (libpal)
  7. * for libiec61850, libmms, and lib60870.
  8. */
  9. #ifndef ETHERNET_HAL_H_
  10. #define ETHERNET_HAL_H_
  11. #include "hal_base.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /*! \addtogroup hal
  16. *
  17. * @{
  18. */
  19. /**
  20. * @defgroup HAL_ETHERNET Direct access to the Ethernet layer (optional - required by GOOSE and Sampled Values)
  21. *
  22. * @{
  23. */
  24. /**
  25. * \brief Opaque handle that represents an Ethernet "socket".
  26. */
  27. typedef struct sEthernetSocket* EthernetSocket;
  28. /** Opaque reference for a set of Ethernet socket handles */
  29. typedef struct sEthernetHandleSet* EthernetHandleSet;
  30. typedef enum {
  31. ETHERNET_SOCKET_MODE_PROMISC, /**<< receive all Ethernet messages */
  32. ETHERNET_SOCKET_MODE_ALL_MULTICAST, /**<< receive all multicast messages */
  33. ETHERNET_SOCKET_MODE_MULTICAST, /**<< receive only specific multicast messages */
  34. ETHERNET_SOCKET_MODE_HOST_ONLY /**<< receive only messages for the host */
  35. } EthernetSocketMode;
  36. /**
  37. * \brief Create a new connection handle set (EthernetHandleSet)
  38. *
  39. * \return new EthernetHandleSet instance
  40. */
  41. PAL_API EthernetHandleSet
  42. EthernetHandleSet_new(void);
  43. /**
  44. * \brief add a socket to an existing handle set
  45. *
  46. * \param self the HandleSet instance
  47. * \param sock the socket to add
  48. */
  49. PAL_API void
  50. EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock);
  51. /**
  52. * \brief remove a socket from an existing handle set
  53. *
  54. * \param self the HandleSet instance
  55. * \param sock the socket to add
  56. */
  57. PAL_API void
  58. EthernetHandleSet_removeSocket(EthernetHandleSet self, const EthernetSocket sock);
  59. /**
  60. * \brief wait for a socket to become ready
  61. *
  62. * This function is corresponding to the BSD socket select function.
  63. * The function will return after \p timeoutMs ms if no data is pending.
  64. *
  65. * \param self the HandleSet instance
  66. * \param timeoutMs in milliseconds (ms)
  67. * \return It returns the number of sockets on which data is pending
  68. * or 0 if no data is pending on any of the monitored connections.
  69. * The function shall return -1 if a socket error occures.
  70. */
  71. PAL_API int
  72. EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs);
  73. /**
  74. * \brief destroy the EthernetHandleSet instance
  75. *
  76. * \param self the HandleSet instance to destroy
  77. */
  78. PAL_API void
  79. EthernetHandleSet_destroy(EthernetHandleSet self);
  80. /**
  81. * \brief Return the MAC address of an Ethernet interface.
  82. *
  83. * The result are the six bytes that make up the Ethernet MAC address.
  84. *
  85. * \param interfaceId the ID of the Ethernet interface
  86. * \param addr pointer to a buffer to store the MAC address
  87. */
  88. PAL_API void
  89. Ethernet_getInterfaceMACAddress(const char* interfaceId, uint8_t* addr);
  90. /**
  91. * \brief Create an Ethernet socket using the specified interface and
  92. * destination MAC address.
  93. *
  94. * \param interfaceId the ID of the Ethernet interface
  95. * \param destAddress byte array that contains the Ethernet destination MAC address for sending
  96. */
  97. PAL_API EthernetSocket
  98. Ethernet_createSocket(const char* interfaceId, uint8_t* destAddress);
  99. /**
  100. * \brief destroy the ethernet socket
  101. *
  102. * \param ethSocket the ethernet socket handle
  103. */
  104. PAL_API void
  105. Ethernet_destroySocket(EthernetSocket ethSocket);
  106. PAL_API void
  107. Ethernet_sendPacket(EthernetSocket ethSocket, uint8_t* buffer, int packetSize);
  108. /*
  109. * \brief set the receive mode of the Ethernet socket
  110. *
  111. * NOTE: When not implemented the the implementation has to be able to receive
  112. * all messages required by GOOSE and/or SV (usually multicast addresses).
  113. *
  114. * \param ethSocket the ethernet socket handle
  115. * \param mode the mode of the socket
  116. */
  117. PAL_API void
  118. Ethernet_setMode(EthernetSocket ethSocket, EthernetSocketMode mode);
  119. /**
  120. * \brief Add a multicast address to be received by the Ethernet socket
  121. *
  122. * Used when mode is ETHERNET_SOCKET_MODE_MULTICAST
  123. *
  124. * \param ethSocket the ethernet socket handle
  125. * \param multicastAddress the multicast Ethernet address (this has to be a byte buffer of at least 6 byte)
  126. */
  127. PAL_API void
  128. Ethernet_addMulticastAddress(EthernetSocket ethSocket, uint8_t* multicastAddress);
  129. /*
  130. * \brief set a protocol filter for the specified etherType
  131. *
  132. * NOTE: Implementation is not required but can improve the performance when the ethertype
  133. * filtering can be done on OS/network stack layer.
  134. *
  135. * \param ethSocket the ethernet socket handle
  136. * \param etherType the ether type of messages to accept
  137. */
  138. PAL_API void
  139. Ethernet_setProtocolFilter(EthernetSocket ethSocket, uint16_t etherType);
  140. /**
  141. * \brief receive an ethernet packet (non-blocking)
  142. *
  143. * \param ethSocket the ethernet socket handle
  144. * \param buffer the buffer to copy the message to
  145. * \param bufferSize the maximum size of the buffer
  146. *
  147. * \return size of message received in bytes
  148. */
  149. PAL_API int
  150. Ethernet_receivePacket(EthernetSocket ethSocket, uint8_t* buffer, int bufferSize);
  151. /**
  152. * \brief Indicates if runtime provides support for direct Ethernet access
  153. *
  154. * \return true if Ethernet support is available, false otherwise
  155. */
  156. PAL_API bool
  157. Ethernet_isSupported(void);
  158. /*! @} */
  159. /*! @} */
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* ETHERNET_HAL_H_ */