iec60870_slave.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 2016-2018 MZ Automation GmbH
  3. *
  4. * This file is part of lib60870-C
  5. *
  6. * lib60870-C is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * lib60870-C is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with lib60870-C. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * See COPYING file for the complete license text.
  20. */
  21. #ifndef SRC_IEC60750_SLAVE_H_
  22. #define SRC_IEC60750_SLAVE_H_
  23. #include "iec_include.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * \file iec60870_slave.h
  29. * \brief Common slave side definitions for IEC 60870-5-101/104
  30. * These types are used by CS101/CS104 slaves
  31. */
  32. /**
  33. * @addtogroup SLAVE Slave related functions
  34. *
  35. * @{
  36. */
  37. /**
  38. * @defgroup COMMON_SLAVE Common slave related functions and interfaces
  39. *
  40. * These definitions are used by both the CS 101 and CS 104 slave implementations.
  41. *
  42. * @{
  43. */
  44. /**
  45. * @defgroup IMASTER_CONNECTION IMasterConnection interface
  46. *
  47. * @{
  48. */
  49. /**
  50. * \brief Interface to send messages to the master (used by slave)
  51. */
  52. typedef struct sIMasterConnection* IMasterConnection;
  53. struct sIMasterConnection {
  54. bool (*isReady) (IMasterConnection self);
  55. bool (*sendASDU) (IMasterConnection self, CS101_ASDU asdu);
  56. bool (*sendACT_CON) (IMasterConnection self, CS101_ASDU asdu, bool negative);
  57. bool (*sendACT_TERM) (IMasterConnection self, CS101_ASDU asdu);
  58. void (*close) (IMasterConnection self);
  59. int (*getPeerAddress) (IMasterConnection self, char* addrBuf, int addrBufSize);
  60. CS101_AppLayerParameters (*getApplicationLayerParameters) (IMasterConnection self);
  61. void* object;
  62. };
  63. /*
  64. * \brief Check if the connection is ready to send an ASDU.
  65. *
  66. * \deprecated Use one of the send functions (e.g. \ref IMasterConnection_sendASDU) and evaluate
  67. * the return value.
  68. *
  69. * NOTE: The functions returns true when the connection is activated, the ASDU can be immediately sent,
  70. * or the queue has enough space to store another ASDU.
  71. *
  72. * \param self the connection object (this is usually received as a parameter of a callback function)
  73. *
  74. * \returns true if the connection is ready to send an ASDU, false otherwise
  75. */
  76. bool
  77. IMasterConnection_isReady(IMasterConnection self);
  78. /**
  79. * \brief Send an ASDU to the client/master
  80. *
  81. * NOTE: ASDU instance has to be released by the caller!
  82. *
  83. * \param self the connection object (this is usually received as a parameter of a callback function)
  84. * \param asdu the ASDU to send to the client/master
  85. *
  86. * \return true when the ASDU has been sent or queued for transmission, false otherwise
  87. */
  88. bool
  89. IMasterConnection_sendASDU(IMasterConnection self, CS101_ASDU asdu);
  90. /**
  91. * \brief Send an ACT_CON ASDU to the client/master
  92. *
  93. * ACT_CON is used for a command confirmation (positive or negative)
  94. *
  95. * \param asdu the ASDU to send to the client/master
  96. * \param negative value of the negative flag
  97. *
  98. * \return true when the ASDU has been sent or queued for transmission, false otherwise
  99. */
  100. bool
  101. IMasterConnection_sendACT_CON(IMasterConnection self, CS101_ASDU asdu, bool negative);
  102. /**
  103. * \brief Send an ACT_TERM ASDU to the client/master
  104. *
  105. * ACT_TERM is used to indicate that the command execution is complete.
  106. *
  107. * \param asdu the ASDU to send to the client/master
  108. *
  109. * \return true when the ASDU has been sent or queued for transmission, false otherwise
  110. */
  111. bool
  112. IMasterConnection_sendACT_TERM(IMasterConnection self, CS101_ASDU asdu);
  113. /**
  114. * \brief Get the peer address of the master (only for CS 104)
  115. *
  116. * \param addrBuf buffer where to store the IP address as string
  117. * \param addrBufSize the size of the buffer where to store the IP address
  118. *
  119. * \return the number of bytes written to the buffer, 0 if function not supported
  120. */
  121. int
  122. IMasterConnection_getPeerAddress(IMasterConnection self, char* addrBuf, int addrBufSize);
  123. /**
  124. * \brief Close the master connection (only for CS 104)
  125. *
  126. * Allows the slave to actively close a master connection (e.g. when some exception occurs)
  127. */
  128. void
  129. IMasterConnection_close(IMasterConnection self);
  130. /**
  131. * \brief Get the application layer parameters used by this connection
  132. */
  133. CS101_AppLayerParameters
  134. IMasterConnection_getApplicationLayerParameters(IMasterConnection self);
  135. /**
  136. * @}
  137. */
  138. /**
  139. * @defgroup SLAVE_PLUGIN Slave plugin interface
  140. *
  141. * Plugin interface to add functionality to the slave (e.g. file server)
  142. */
  143. typedef enum
  144. {
  145. CS101_PLUGIN_RESULT_NOT_HANDLED = 0,
  146. CS101_PLUGIN_RESULT_HANDLED = 1,
  147. CS101_PLUGIN_RESULT_INVALID_ASDU = 2
  148. } CS101_SlavePlugin_Result;
  149. /**
  150. * \brief Plugin interface for CS101 or CS104 slaves
  151. */
  152. typedef struct sCS101_SlavePlugin* CS101_SlavePlugin;
  153. struct sCS101_SlavePlugin
  154. {
  155. CS101_SlavePlugin_Result (*handleAsdu) (void* parameter, IMasterConnection connection, CS101_ASDU asdu);
  156. void (*runTask) (void* parameter, IMasterConnection connection);
  157. void* parameter;
  158. };
  159. /**
  160. * @}
  161. */
  162. /**
  163. * @defgroup CALLBACK_HANDLERS Slave callback handlers
  164. *
  165. * Callback handlers to handle events in the slave
  166. */
  167. /**
  168. * \brief Handler will be called when a link layer reset CU (communication unit) message is received
  169. *
  170. * NOTE: Can be used to empty the ASDU queues
  171. *
  172. * \param parameter a user provided parameter
  173. */
  174. typedef void (*CS101_ResetCUHandler) (void* parameter);
  175. /**
  176. * \brief Handler for interrogation command (C_IC_NA_1 - 100).
  177. */
  178. typedef bool (*CS101_InterrogationHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, uint8_t qoi);
  179. /**
  180. * \brief Handler for counter interrogation command (C_CI_NA_1 - 101).
  181. */
  182. typedef bool (*CS101_CounterInterrogationHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, QualifierOfCIC qcc);
  183. /**
  184. * \brief Handler for read command (C_RD_NA_1 - 102)
  185. */
  186. typedef bool (*CS101_ReadHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, int ioa);
  187. /**
  188. * \brief Handler for clock synchronization command (C_CS_NA_1 - 103)
  189. *
  190. * This handler will be called whenever a time synchronization command is received.
  191. * NOTE: The \ref CS104_Slave instance will automatically send an ACT-CON message for the received time sync command.
  192. *
  193. * \param[in] parameter user provided parameter
  194. * \param[in] connection represents the (TCP) connection that received the time sync command
  195. * \param[in] asdu the received ASDU
  196. * \param[in,out] the time received with the time sync message. The user can update this time for the ACT-CON message
  197. *
  198. * \return true when time synchronization has been successful, false otherwise
  199. */
  200. typedef bool (*CS101_ClockSynchronizationHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, CP56Time2a newTime);
  201. /**
  202. * \brief Handler for reset process command (C_RP_NA_1 - 105)
  203. */
  204. typedef bool (*CS101_ResetProcessHandler ) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, uint8_t qrp);
  205. /**
  206. * \brief Handler for delay acquisition command (C_CD_NA:1 - 106)
  207. */
  208. typedef bool (*CS101_DelayAcquisitionHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu, CP16Time2a delayTime);
  209. /**
  210. * \brief Handler for ASDUs that are not handled by other handlers (default handler)
  211. */
  212. typedef bool (*CS101_ASDUHandler) (void* parameter, IMasterConnection connection, CS101_ASDU asdu);
  213. /**
  214. * @}
  215. */
  216. /**
  217. * @}
  218. */
  219. /**
  220. * @}
  221. */
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* SRC_IEC60750_SLAVE_H_ */