cs101_master.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * cs101_master.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. /**
  24. * \file cs101_master.h
  25. * \brief Functions for CS101_Master ADT.
  26. * Can be used to implement a balanced or unbalanced CS 101 master.
  27. */
  28. #ifndef SRC_INC_API_CS101_MASTER_H_
  29. #define SRC_INC_API_CS101_MASTER_H_
  30. #include "iec60870_master.h"
  31. #include "hal_serial.h"
  32. #include "link_layer_paramete.h"
  33. #include <stdbool.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * @defgroup MASTER Master related functions
  39. *
  40. * @{
  41. */
  42. /**
  43. * @defgroup CS101_MASTER CS 101 master related functions
  44. *
  45. * @{
  46. */
  47. /**
  48. * \brief CS101_Master type */
  49. typedef struct sCS101_Master* CS101_Master;
  50. /**
  51. * \brief Create a new master instance
  52. *
  53. * \param port the serial port to use
  54. * \param llParameters the link layer parameters to use
  55. * \param alParameters the application layer parameters to use
  56. * \param mode the link layer mode (either IEC60870_LINK_LAYER_BALANCED or IEC60870_LINK_LAYER_UNBALANCED)
  57. *
  58. * \return the new CS101_Master instance
  59. */
  60. CS101_Master
  61. CS101_Master_create(SerialPort port, const LinkLayerParameters llParameters, const CS101_AppLayerParameters alParameters, IEC60870_LinkLayerMode mode);
  62. /**
  63. * \brief Create a new master instance and specify message queue size (for balanced mode)
  64. *
  65. * \param port the serial port to use
  66. * \param llParameters the link layer parameters to use
  67. * \param alParameters the application layer parameters to use
  68. * \param mode the link layer mode (either IEC60870_LINK_LAYER_BALANCED or IEC60870_LINK_LAYER_UNBALANCED)
  69. * \param queueSize set the message queue size (only for balanced mode)
  70. *
  71. * \return the new CS101_Master instance
  72. */
  73. CS101_Master
  74. CS101_Master_createEx(SerialPort serialPort, const LinkLayerParameters llParameters, const CS101_AppLayerParameters alParameters, IEC60870_LinkLayerMode linkLayerMode,
  75. int queueSize);
  76. /**
  77. * \brief Receive a new message and run the protocol state machine(s).
  78. *
  79. * NOTE: This function has to be called frequently in order to send and
  80. * receive messages to and from slaves.
  81. */
  82. void
  83. CS101_Master_run(CS101_Master self);
  84. /**
  85. * \brief Start a background thread that handles the link layer connections
  86. *
  87. * NOTE: This requires threads. If you don't want to use a separate thread
  88. * for the master instance you have to call the \ref CS101_Master_run function
  89. * periodically.
  90. *
  91. * \param self CS101_Master instance
  92. */
  93. void
  94. CS101_Master_start(CS101_Master self);
  95. /**
  96. * \brief Stops the background thread that handles the link layer connections
  97. *
  98. * \param self CS101_Master instance
  99. */
  100. void
  101. CS101_Master_stop(CS101_Master self);
  102. /**
  103. * \brief Add a new slave connection
  104. *
  105. * This function creates and starts a new link layer state machine
  106. * to be used for communication with the slave. It has to be called
  107. * before any application data can be send/received to/from the slave.
  108. *
  109. * \param address link layer address of the slave
  110. */
  111. void
  112. CS101_Master_addSlave(CS101_Master self, int address);
  113. /**
  114. * \brief Poll a slave (only unbalanced mode)
  115. *
  116. * NOTE: This command will instruct the unbalanced link layer to send a
  117. * request for class 2 data. It is required to frequently call this
  118. * message for each slave in order to receive application layer data from
  119. * the slave
  120. *
  121. * \param address the link layer address of the slave
  122. */
  123. void
  124. CS101_Master_pollSingleSlave(CS101_Master self, int address);
  125. /**
  126. * \brief Destroy the master instance and release all resources
  127. */
  128. void
  129. CS101_Master_destroy(CS101_Master self);
  130. /**
  131. * \brief Set the value of the DIR bit when sending messages (only balanced mode)
  132. *
  133. * NOTE: Default value is true (controlling station). In the case of two equivalent stations
  134. * the value is defined by agreement.
  135. *
  136. * \param dir the value of the DIR bit when sending messages
  137. */
  138. void
  139. CS101_Master_setDIR(CS101_Master self, bool dir);
  140. /**
  141. * \brief Set the own link layer address (only balanced mode)
  142. *
  143. * \param address the link layer address to use
  144. */
  145. void
  146. CS101_Master_setOwnAddress(CS101_Master self, int address);
  147. /**
  148. * \brief Set the slave address for the following send functions
  149. *
  150. * NOTE: This is always required in unbalanced mode. Some balanced slaves
  151. * also check the link layer address. In this case the slave address
  152. * has also to be set in balanced mode.
  153. *
  154. * \param address the link layer address of the slave to address
  155. */
  156. void
  157. CS101_Master_useSlaveAddress(CS101_Master self, int address);
  158. /**
  159. * \brief Returns the application layer parameters object of this master instance
  160. *
  161. * \return the CS101_AppLayerParameters instance used by this master
  162. */
  163. CS101_AppLayerParameters
  164. CS101_Master_getAppLayerParameters(CS101_Master self);
  165. /**
  166. * \brief Returns the link layer parameters object of this master instance
  167. *
  168. * \return the LinkLayerParameters instance used by this master
  169. */
  170. LinkLayerParameters
  171. CS101_Master_getLinkLayerParameters(CS101_Master self);
  172. /**
  173. * \brief Is the channel ready to transmit an ASDU (only unbalanced mode)
  174. *
  175. * The function will return true when the channel (slave) transmit buffer
  176. * is empty.
  177. *
  178. * \param address slave address of the recipient
  179. *
  180. * \return true, if channel ready to send a new ASDU, false otherwise
  181. */
  182. bool
  183. CS101_Master_isChannelReady(CS101_Master self, int address);
  184. /**
  185. * \brief Manually send link layer test function.
  186. *
  187. * Together with the IEC60870_LinkLayerStateChangedHandler this function can
  188. * be used to ensure that the link is working correctly
  189. */
  190. void
  191. CS101_Master_sendLinkLayerTestFunction(CS101_Master self);
  192. /**
  193. * \brief send an interrogation command
  194. *
  195. * \param cot cause of transmission
  196. * \param ca Common address of the slave/server
  197. * \param qoi qualifier of interrogation (20 for station interrogation)
  198. */
  199. void
  200. CS101_Master_sendInterrogationCommand(CS101_Master self, CS101_CauseOfTransmission cot, int ca, QualifierOfInterrogation qoi);
  201. /**
  202. * \brief send a counter interrogation command
  203. *
  204. * \param cot cause of transmission
  205. * \param ca Common address of the slave/server
  206. * \param qcc
  207. */
  208. void
  209. CS101_Master_sendCounterInterrogationCommand(CS101_Master self, CS101_CauseOfTransmission cot, int ca, uint8_t qcc);
  210. /**
  211. * \brief Sends a read command (C_RD_NA_1 typeID: 102)
  212. *
  213. * This will send a read command C_RC_NA_1 (102) to the slave/outstation. The COT is always REQUEST (5).
  214. * It is used to implement the cyclical polling of data application function.
  215. *
  216. * \param ca Common address of the slave/server
  217. * \param ioa Information object address of the data point to read
  218. */
  219. void
  220. CS101_Master_sendReadCommand(CS101_Master self, int ca, int ioa);
  221. /**
  222. * \brief Sends a clock synchronization command (C_CS_NA_1 typeID: 103)
  223. *
  224. * \param ca Common address of the slave/server
  225. * \param time new system time for the slave/server
  226. */
  227. void
  228. CS101_Master_sendClockSyncCommand(CS101_Master self, int ca, CP56Time2a time);
  229. /**
  230. * \brief Send a test command (C_TS_NA_1 typeID: 104)
  231. *
  232. * Note: This command is not supported by IEC 60870-5-104
  233. *
  234. * \param ca Common address of the slave/server
  235. */
  236. void
  237. CS101_Master_sendTestCommand(CS101_Master self, int ca);
  238. /**
  239. * \brief Send a process command to the controlled (or other) station
  240. *
  241. * \param cot the cause of transmission (should be ACTIVATION to select/execute or ACT_TERM to cancel the command)
  242. * \param ca the common address of the information object
  243. * \param command the command information object (e.g. SingleCommand or DoubleCommand)
  244. *
  245. */
  246. void
  247. CS101_Master_sendProcessCommand(CS101_Master self, CS101_CauseOfTransmission cot, int ca, InformationObject command);
  248. /**
  249. * \brief Send a user specified ASDU
  250. *
  251. * This function can be used for any kind of ASDU types. It can
  252. * also be used for monitoring messages in reverse direction.
  253. *
  254. * NOTE: The ASDU is put into a message queue and will be sent whenever
  255. * the link layer state machine is able to transmit the ASDU. The ASDUs will
  256. * be sent in the order they are put into the queue.
  257. *
  258. * \param asdu the ASDU to send
  259. */
  260. void
  261. CS101_Master_sendASDU(CS101_Master self, CS101_ASDU asdu);
  262. /**
  263. * \brief Register a callback handler for received ASDUs
  264. *
  265. * \param handler user provided callback handler function
  266. * \param parameter user provided parameter that is passed to the callback handler
  267. */
  268. void
  269. CS101_Master_setASDUReceivedHandler(CS101_Master self, CS101_ASDUReceivedHandler handler, void* parameter);
  270. /**
  271. * \brief Set a callback handler for link layer state changes
  272. */
  273. void
  274. CS101_Master_setLinkLayerStateChanged(CS101_Master self, IEC60870_LinkLayerStateChangedHandler handler, void* parameter);
  275. /**
  276. * \brief Set the raw message callback (called when a message is sent or received)
  277. *
  278. * \param handler user provided callback handler function
  279. * \param parameter user provided parameter that is passed to the callback handler
  280. */
  281. void
  282. CS101_Master_setRawMessageHandler(CS101_Master self, IEC60870_RawMessageHandler handler, void* parameter);
  283. /**
  284. * \brief Set the idle timeout (only for balanced mode)
  285. *
  286. * Time with no activity after which the connection is considered
  287. * in idle (LL_STATE_IDLE) state.
  288. *
  289. * \param timeoutInMs the timeout value in milliseconds
  290. */
  291. void
  292. CS101_Master_setIdleTimeout(CS101_Master self, int timeoutInMs);
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* SRC_INC_API_CS101_MASTER_H_ */