hal_ethernet.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  31. * \brief Create a new connection handle set (EthernetHandleSet)
  32. *
  33. * \return new EthernetHandleSet instance
  34. */
  35. PAL_API EthernetHandleSet
  36. EthernetHandleSet_new(void);
  37. /**
  38. * \brief add a socket to an existing handle set
  39. *
  40. * \param self the HandleSet instance
  41. * \param sock the socket to add
  42. */
  43. PAL_API void
  44. EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock);
  45. /**
  46. * \brief remove a socket from an existing handle set
  47. *
  48. * \param self the HandleSet instance
  49. * \param sock the socket to add
  50. */
  51. PAL_API void
  52. EthernetHandleSet_removeSocket(EthernetHandleSet self, const EthernetSocket sock);
  53. /**
  54. * \brief wait for a socket to become ready
  55. *
  56. * This function is corresponding to the BSD socket select function.
  57. * The function will return after \p timeoutMs ms if no data is pending.
  58. *
  59. * \param self the HandleSet instance
  60. * \param timeoutMs in milliseconds (ms)
  61. * \return It returns the number of sockets on which data is pending
  62. * or 0 if no data is pending on any of the monitored connections.
  63. * The function shall return -1 if a socket error occures.
  64. */
  65. PAL_API int
  66. EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs);
  67. /**
  68. * \brief destroy the EthernetHandleSet instance
  69. *
  70. * \param self the HandleSet instance to destroy
  71. */
  72. PAL_API void
  73. EthernetHandleSet_destroy(EthernetHandleSet self);
  74. /**
  75. * \brief Return the MAC address of an Ethernet interface.
  76. *
  77. * The result are the six bytes that make up the Ethernet MAC address.
  78. *
  79. * \param interfaceId the ID of the Ethernet interface
  80. * \param addr pointer to a buffer to store the MAC address
  81. */
  82. PAL_API void
  83. Ethernet_getInterfaceMACAddress(const char* interfaceId, uint8_t* addr);
  84. /**
  85. * \brief Create an Ethernet socket using the specified interface and
  86. * destination MAC address.
  87. *
  88. * \param interfaceId the ID of the Ethernet interface
  89. * \param destAddress byte array that contains the Ethernet destination MAC address for sending
  90. */
  91. PAL_API EthernetSocket
  92. Ethernet_createSocket(const char* interfaceId, uint8_t* destAddress);
  93. /**
  94. * \brief destroy the ethernet socket
  95. *
  96. * \param ethSocket the ethernet socket handle
  97. */
  98. PAL_API void
  99. Ethernet_destroySocket(EthernetSocket ethSocket);
  100. PAL_API void
  101. Ethernet_sendPacket(EthernetSocket ethSocket, uint8_t* buffer, int packetSize);
  102. /*
  103. * \brief set a protocol filter for the specified etherType
  104. *
  105. * NOTE: Implementation is not required but can improve the performance when the ethertype
  106. * filtering can be done on OS/network stack layer.
  107. *
  108. * \param ethSocket the ethernet socket handle
  109. * \param etherType the ether type of messages to accept
  110. */
  111. PAL_API void
  112. Ethernet_setProtocolFilter(EthernetSocket ethSocket, uint16_t etherType);
  113. /**
  114. * \brief receive an ethernet packet (non-blocking)
  115. *
  116. * \param ethSocket the ethernet socket handle
  117. * \param buffer the buffer to copy the message to
  118. * \param bufferSize the maximum size of the buffer
  119. *
  120. * \return size of message received in bytes
  121. */
  122. PAL_API int
  123. Ethernet_receivePacket(EthernetSocket ethSocket, uint8_t* buffer, int bufferSize);
  124. /**
  125. * \brief Indicates if runtime provides support for direct Ethernet access
  126. *
  127. * \return true if Ethernet support is available, false otherwise
  128. */
  129. PAL_API bool
  130. Ethernet_isSupported(void);
  131. /*! @} */
  132. /*! @} */
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* ETHERNET_HAL_H_ */