cs101_slave.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * cs101_slave.h
  3. *
  4. * Copyright 2017-2022 Michael Zillgith
  5. *
  6. * This file is part of lib60870-C
  7. *
  8. * lib60870-C is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * lib60870-C is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with lib60870-C. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * See COPYING file for the complete license text.
  22. */
  23. #ifndef SRC_IEC60870_CS101_CS101_SLAVE_H_
  24. #define SRC_IEC60870_CS101_CS101_SLAVE_H_
  25. /**
  26. * \file cs101_slave.h
  27. * \brief Functions for CS101_Slave ADT.
  28. * Can be used to implement a balanced or unbalanced CS 101 slave.
  29. */
  30. #include "iec_include.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * @defgroup SLAVE Slave related functions
  36. *
  37. * @{
  38. */
  39. /**
  40. * @defgroup CS101_SLAVE CS 101 slave (serial link layer) related functions
  41. *
  42. * @{
  43. */
  44. /**
  45. * \brief CS101_Slave type
  46. */
  47. typedef struct sCS101_Slave* CS101_Slave;
  48. /**
  49. * \brief Create a new balanced or unbalanced CS101 slave
  50. *
  51. * NOTE: The CS101_Slave instance has two separate data queues for class 1 and class 2 data.
  52. * This constructor uses the default max queue size for both queues.
  53. *
  54. * \param serialPort the serial port to be used
  55. * \param llParameters the link layer parameters to be used
  56. * \param alParameters the CS101 application layer parameters
  57. * \param linkLayerMode the link layer mode (either BALANCED or UNBALANCED)
  58. *
  59. * \return the new slave instance
  60. */
  61. CS101_Slave
  62. CS101_Slave_create(SerialPort serialPort, const LinkLayerParameters llParameters, const CS101_AppLayerParameters alParameters, IEC60870_LinkLayerMode linkLayerMode);
  63. /**
  64. * \brief Create a new balanced or unbalanced CS101 slave
  65. *
  66. * NOTE: The CS101_Slave instance has two separate data queues for class 1 and class 2 data.
  67. *
  68. * \param serialPort the serial port to be used
  69. * \param llParameters the link layer parameters to be used
  70. * \param alParameters the CS101 application layer parameters
  71. * \param linkLayerMode the link layer mode (either BALANCED or UNBALANCED)
  72. * \param class1QueueSize size of the class1 data queue
  73. * \param class2QueueSize size of the class2 data queue
  74. *
  75. * \return the new slave instance
  76. */
  77. CS101_Slave
  78. CS101_Slave_createEx(SerialPort serialPort, const LinkLayerParameters llParameters, const CS101_AppLayerParameters alParameters, IEC60870_LinkLayerMode linkLayerMode,
  79. int class1QueueSize, int class2QueueSize);
  80. /**
  81. * \brief Destroy the slave instance and cleanup all resources
  82. *
  83. * \param self CS101_Slave instance
  84. */
  85. void
  86. CS101_Slave_destroy(CS101_Slave self);
  87. /**
  88. * \brief Set the value of the DIR bit when sending messages (only balanced mode)
  89. *
  90. * NOTE: Default value is false (controlled station). In the case of two equivalent stations
  91. * the value is defined by agreement.
  92. *
  93. * \param dir the value of the DIR bit when sending messages
  94. */
  95. void
  96. CS101_Slave_setDIR(CS101_Slave self, bool dir);
  97. /**
  98. * \brief Register a plugin instance with this slave instance
  99. *
  100. * \param the plugin instance.
  101. */
  102. void
  103. CS101_Slave_addPlugin(CS101_Slave self, CS101_SlavePlugin plugin);
  104. /**
  105. * \brief Set the idle timeout
  106. *
  107. * Time with no activity after which the connection is considered
  108. * in idle (LL_STATE_IDLE) state.
  109. *
  110. * \param timeoutInMs the timeout value in milliseconds
  111. */
  112. void
  113. CS101_Slave_setIdleTimeout(CS101_Slave self, int timeoutInMs);
  114. /**
  115. * \brief Set a callback handler for link layer state changes
  116. */
  117. void
  118. CS101_Slave_setLinkLayerStateChanged(CS101_Slave self, IEC60870_LinkLayerStateChangedHandler handler, void* parameter);
  119. /**
  120. * \brief Set the local link layer address
  121. *
  122. * \param self CS101_Slave instance
  123. * \param address the link layer address (can be either 1 or 2 byte wide).
  124. */
  125. void
  126. CS101_Slave_setLinkLayerAddress(CS101_Slave self, int address);
  127. /**
  128. * \brief Set the link layer address of the remote station
  129. *
  130. * \param self CS101_Slave instance
  131. * \param address the link layer address (can be either 1 or 2 byte wide).
  132. */
  133. void
  134. CS101_Slave_setLinkLayerAddressOtherStation(CS101_Slave self, int address);
  135. /**
  136. * \brief Check if the class 1 ASDU is full
  137. *
  138. * \param self CS101_Slave instance
  139. *
  140. * \return true when the queue is full, false otherwise
  141. */
  142. bool
  143. CS101_Slave_isClass1QueueFull(CS101_Slave self);
  144. /**
  145. * \brief Enqueue an ASDU into the class 1 data queue
  146. *
  147. * \param self CS101_Slave instance
  148. * \param asdu the ASDU instance to enqueue
  149. */
  150. void
  151. CS101_Slave_enqueueUserDataClass1(CS101_Slave self, CS101_ASDU asdu);
  152. /**
  153. * \brief Check if the class 2 ASDU is full
  154. *
  155. * \param self CS101_Slave instance
  156. *
  157. * \return true when the queue is full, false otherwise
  158. */
  159. bool
  160. CS101_Slave_isClass2QueueFull(CS101_Slave self);
  161. /**
  162. * \brief Enqueue an ASDU into the class 2 data queue
  163. *
  164. * \param self CS101_Slave instance
  165. * \param asdu the ASDU instance to enqueue
  166. */
  167. void
  168. CS101_Slave_enqueueUserDataClass2(CS101_Slave self, CS101_ASDU asdu);
  169. /**
  170. * \brief Remove all ASDUs from the class 1/2 data queues
  171. *
  172. * \param self CS101_Slave instance
  173. */
  174. void
  175. CS101_Slave_flushQueues(CS101_Slave self);
  176. /**
  177. * \brief Receive a new message and run the link layer state machines
  178. *
  179. * NOTE: Has to be called frequently, when the start/stop functions are
  180. * not used. Otherwise it will be called by the background thread.
  181. *
  182. * \param self CS101_Slave instance
  183. */
  184. void
  185. CS101_Slave_run(CS101_Slave self);
  186. /**
  187. * \brief Start a background thread that handles the link layer connections
  188. *
  189. * NOTE: This requires threads. If you don't want to use a separate thread
  190. * for the slave instance you have to call the \ref CS101_Slave_run function
  191. * periodically.
  192. *
  193. * \param self CS101_Slave instance
  194. */
  195. void
  196. CS101_Slave_start(CS101_Slave self);
  197. /**
  198. * \brief Stops the background thread that handles the link layer connections
  199. *
  200. * \param self CS101_Slave instance
  201. */
  202. void
  203. CS101_Slave_stop(CS101_Slave self);
  204. /**
  205. * \brief Returns the application layer parameters object of this slave instance
  206. *
  207. * \param self CS101_Slave instance
  208. *
  209. * \return the CS101_AppLayerParameters instance used by this slave
  210. */
  211. CS101_AppLayerParameters
  212. CS101_Slave_getAppLayerParameters(CS101_Slave self);
  213. /**
  214. * \brief Returns the link layer parameters object of this slave instance
  215. *
  216. * \param self CS101_Slave instance
  217. *
  218. * \return the LinkLayerParameters instance used by this slave
  219. */
  220. LinkLayerParameters
  221. CS101_Slave_getLinkLayerParameters(CS101_Slave self);
  222. /**
  223. * \brief Set the handler for the reset CU (communication unit) message
  224. *
  225. * \param handler the callback handler function
  226. * \param parameter user provided parameter to be passed to the callback handler
  227. */
  228. void
  229. CS101_Slave_setResetCUHandler(CS101_Slave self, CS101_ResetCUHandler handler, void* parameter);
  230. /**
  231. * \brief Set the handler for the general interrogation message
  232. *
  233. * \param handler the callback handler function
  234. * \param parameter user provided parameter to be passed to the callback handler
  235. */
  236. void
  237. CS101_Slave_setInterrogationHandler(CS101_Slave self, CS101_InterrogationHandler handler, void* parameter);
  238. /**
  239. * \brief Set the handler for the counter interrogation message
  240. *
  241. * \param handler the callback handler function
  242. * \param parameter user provided parameter to be passed to the callback handler
  243. */
  244. void
  245. CS101_Slave_setCounterInterrogationHandler(CS101_Slave self, CS101_CounterInterrogationHandler handler, void* parameter);
  246. /**
  247. * \brief Set the handler for the read message
  248. *
  249. * \param handler the callback handler function
  250. * \param parameter user provided parameter to be passed to the callback handler
  251. */
  252. void
  253. CS101_Slave_setReadHandler(CS101_Slave self, CS101_ReadHandler handler, void* parameter);
  254. /**
  255. * \brief Set the handler for the clock synchronization message
  256. *
  257. * \param handler the callback handler function
  258. * \param parameter user provided parameter to be passed to the callback handler
  259. */
  260. void
  261. CS101_Slave_setClockSyncHandler(CS101_Slave self, CS101_ClockSynchronizationHandler handler, void* parameter);
  262. /**
  263. * \brief Set the handler for the reset process message
  264. *
  265. * \param handler the callback handler function
  266. * \param parameter user provided parameter to be passed to the callback handler
  267. */
  268. void
  269. CS101_Slave_setResetProcessHandler(CS101_Slave self, CS101_ResetProcessHandler handler, void* parameter);
  270. /**
  271. * \brief Set the handler for the delay acquisition message
  272. *
  273. * \param handler the callback handler function
  274. * \param parameter user provided parameter to be passed to the callback handler
  275. */
  276. void
  277. CS101_Slave_setDelayAcquisitionHandler(CS101_Slave self, CS101_DelayAcquisitionHandler handler, void* parameter);
  278. /**
  279. * \brief Set the handler for a received ASDU
  280. *
  281. * NOTE: This a generic handler that will only be called when the ASDU has not been handled by
  282. * one of the other callback handlers.
  283. *
  284. * \param handler the callback handler function
  285. * \param parameter user provided parameter to be passed to the callback handler
  286. */
  287. void
  288. CS101_Slave_setASDUHandler(CS101_Slave self, CS101_ASDUHandler handler, void* parameter);
  289. /**
  290. * \brief Set the raw message callback (called when a message is sent or received)
  291. *
  292. * \param handler user provided callback handler function
  293. * \param parameter user provided parameter that is passed to the callback handler
  294. */
  295. void
  296. CS101_Slave_setRawMessageHandler(CS101_Slave self, IEC60870_RawMessageHandler handler, void* parameter);
  297. /**
  298. * @}
  299. */
  300. /**
  301. * @}
  302. */
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif /* SRC_IEC60870_CS101_CS101_SLAVE_H_ */