cs104_connection.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 2016-2022 Michael Zillgith
  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 _CS104_CONNECTION_H_
  22. #define _CS104_CONNECTION_H_
  23. #include "iec_include.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * \file cs104_connection.h
  29. * \brief CS 104 master side definitions
  30. */
  31. /**
  32. * @addtogroup MASTER Master related functions
  33. *
  34. * @{
  35. */
  36. /**
  37. * @defgroup CS104_MASTER CS 104 master related functions
  38. *
  39. * @{
  40. */
  41. typedef struct sCS104_Connection* CS104_Connection;
  42. /**
  43. * \brief Create a new connection object
  44. *
  45. * \param hostname host name of IP address of the server to connect
  46. * \param tcpPort tcp port of the server to connect. If set to -1 use default port (2404)
  47. *
  48. * \return the new connection object
  49. */
  50. CS104_Connection
  51. CS104_Connection_create(const char* hostname, int tcpPort);
  52. /**
  53. * \brief Create a new secure connection object (uses TLS)
  54. *
  55. * \param hostname host name of IP address of the server to connect
  56. * \param tcpPort tcp port of the server to connect. If set to -1 use default port (19998)
  57. * \param tlcConfig the TLS configuration (certificates, keys, and parameters)
  58. *
  59. * \return the new connection object
  60. */
  61. CS104_Connection
  62. CS104_Connection_createSecure(const char* hostname, int tcpPort, TLSConfiguration tlsConfig);
  63. /**
  64. * \brief Set the local IP address and port to be used by the client
  65. *
  66. * NOTE: This function is optional. When not used the OS decides what IP address and TCP port to use.
  67. *
  68. * \param self CS104_Connection instance
  69. * \param localIpAddress the local IP address or hostname as C string
  70. * \param localPort the local TCP port to use. When < 1 the OS will chose the TCP port to use.
  71. */
  72. void
  73. CS104_Connection_setLocalAddress(CS104_Connection self, const char* localIpAddress, int localPort);
  74. /**
  75. * \brief Set the CS104 specific APCI parameters.
  76. *
  77. * If not set the default parameters are used. This function must be called before the
  78. * CS104_Connection_connect function is called! If the function is called after the connect
  79. * the behavior is undefined.
  80. *
  81. * \param self CS104_Connection instance
  82. * \param parameters the APCI layer parameters
  83. */
  84. void
  85. CS104_Connection_setAPCIParameters(CS104_Connection self, const CS104_APCIParameters parameters);
  86. /**
  87. * \brief Get the currently used CS104 specific APCI parameters
  88. */
  89. CS104_APCIParameters
  90. CS104_Connection_getAPCIParameters(CS104_Connection self);
  91. /**
  92. * \brief Set the CS101 application layer parameters
  93. *
  94. * If not set the default parameters are used. This function must be called before the
  95. * CS104_Connection_connect function is called! If the function is called after the connect
  96. * the behavior is undefined.
  97. *
  98. * \param self CS104_Connection instance
  99. * \param parameters the application layer parameters
  100. */
  101. void
  102. CS104_Connection_setAppLayerParameters(CS104_Connection self, const CS101_AppLayerParameters parameters);
  103. /**
  104. * \brief Return the currently used application layer parameter
  105. *
  106. * NOTE: The application layer parameters are required to create CS101_ASDU objects.
  107. *
  108. * \param self CS104_Connection instance
  109. *
  110. * \return the currently used CS101_AppLayerParameters object
  111. */
  112. CS101_AppLayerParameters
  113. CS104_Connection_getAppLayerParameters(CS104_Connection self);
  114. /**
  115. * \brief Sets the timeout for connecting to the server (in ms)
  116. *
  117. * \deprecated Function has no effect! Set T0 parameter instead.
  118. *
  119. * \param self
  120. * \param millies timeout value in ms
  121. */
  122. void
  123. CS104_Connection_setConnectTimeout(CS104_Connection self, int millies);
  124. /**
  125. * \brief non-blocking connect.
  126. *
  127. * Invokes a connection establishment to the server and returns immediately.
  128. *
  129. * \param self CS104_Connection instance
  130. */
  131. void
  132. CS104_Connection_connectAsync(CS104_Connection self);
  133. /**
  134. * \brief blocking connect
  135. *
  136. * Establishes a connection to a server. This function is blocking and will return
  137. * after the connection is established or the connect timeout elapsed.
  138. *
  139. * \param self CS104_Connection instance
  140. * \return true when connected, false otherwise
  141. */
  142. bool
  143. CS104_Connection_connect(CS104_Connection self);
  144. /**
  145. * \brief start data transmission on this connection
  146. *
  147. * After issuing this command the client (master) will receive spontaneous
  148. * (unsolicited) messages from the server (slave).
  149. */
  150. void
  151. CS104_Connection_sendStartDT(CS104_Connection self);
  152. /**
  153. * \brief stop data transmission on this connection
  154. */
  155. void
  156. CS104_Connection_sendStopDT(CS104_Connection self);
  157. /**
  158. * \brief Check if the transmit (send) buffer is full. If true the next send command will fail.
  159. *
  160. * The transmit buffer is full when the slave/server didn't confirm the last k sent messages.
  161. * In this case the next message can only be sent after the next confirmation (by I or S messages)
  162. * that frees part of the sent messages buffer.
  163. */
  164. bool
  165. CS104_Connection_isTransmitBufferFull(CS104_Connection self);
  166. /**
  167. * \brief send an interrogation command
  168. *
  169. * \param cot cause of transmission
  170. * \param ca Common address of the slave/server
  171. * \param qoi qualifier of interrogation (20 for station interrogation)
  172. *
  173. * \return true if message was sent, false otherwise
  174. */
  175. bool
  176. CS104_Connection_sendInterrogationCommand(CS104_Connection self, CS101_CauseOfTransmission cot, int ca, QualifierOfInterrogation qoi);
  177. /**
  178. * \brief send a counter interrogation command
  179. *
  180. * \param cot cause of transmission
  181. * \param ca Common address of the slave/server
  182. * \param qcc
  183. *
  184. * \return true if message was sent, false otherwise
  185. */
  186. bool
  187. CS104_Connection_sendCounterInterrogationCommand(CS104_Connection self, CS101_CauseOfTransmission cot, int ca, uint8_t qcc);
  188. /**
  189. * \brief Sends a read command (C_RD_NA_1 typeID: 102)
  190. *
  191. * This will send a read command C_RC_NA_1 (102) to the slave/outstation. The COT is always REQUEST (5).
  192. * It is used to implement the cyclical polling of data application function.
  193. *
  194. * \param ca Common address of the slave/server
  195. * \param ioa Information object address of the data point to read
  196. *
  197. * \return true if message was sent, false otherwise
  198. */
  199. bool
  200. CS104_Connection_sendReadCommand(CS104_Connection self, int ca, int ioa);
  201. /**
  202. * \brief Sends a clock synchronization command (C_CS_NA_1 typeID: 103)
  203. *
  204. * \param ca Common address of the slave/server
  205. * \param newTime new system time for the slave/server
  206. *
  207. * \return true if message was sent, false otherwise
  208. */
  209. bool
  210. CS104_Connection_sendClockSyncCommand(CS104_Connection self, int ca, CP56Time2a newTime);
  211. /**
  212. * \brief Send a test command (C_TS_NA_1 typeID: 104)
  213. *
  214. * Note: This command is not supported by IEC 60870-5-104
  215. *
  216. * \param ca Common address of the slave/server
  217. *
  218. * \return true if message was sent, false otherwise
  219. */
  220. bool
  221. CS104_Connection_sendTestCommand(CS104_Connection self, int ca);
  222. /**
  223. * \brief Send a test command with timestamp (C_TS_TA_1 typeID: 107)
  224. *
  225. * \param ca Common address of the slave/server
  226. * \param tsc test sequence counter
  227. * \param timestamp CP56Time2a timestamp
  228. *
  229. * \return true if message was sent, false otherwise
  230. */
  231. bool
  232. CS104_Connection_sendTestCommandWithTimestamp(CS104_Connection self, int ca, uint16_t tsc, CP56Time2a timestamp);
  233. /**
  234. * \brief Send a process command to the controlled (or other) station
  235. *
  236. * \deprecated Use \ref CS104_Connection_sendProcessCommandEx instead
  237. *
  238. * \param typeId the type ID of the command message to send or 0 to use the type ID of the information object
  239. * \param cot the cause of transmission (should be ACTIVATION to select/execute or ACT_TERM to cancel the command)
  240. * \param ca the common address of the information object
  241. * \param command the command information object (e.g. SingleCommand or DoubleCommand)
  242. *
  243. * \return true if message was sent, false otherwise
  244. */
  245. bool
  246. CS104_Connection_sendProcessCommand(CS104_Connection self, TypeID typeId, CS101_CauseOfTransmission cot,
  247. int ca, InformationObject command);
  248. /**
  249. * \brief Send a process command to the controlled (or other) station
  250. *
  251. * \param cot the cause of transmission (should be ACTIVATION to select/execute or ACT_TERM to cancel the command)
  252. * \param ca the common address of the information object
  253. * \param command the command information object (e.g. SingleCommand or DoubleCommand)
  254. *
  255. * \return true if message was sent, false otherwise
  256. */
  257. bool
  258. CS104_Connection_sendProcessCommandEx(CS104_Connection self, CS101_CauseOfTransmission cot, int ca, InformationObject sc);
  259. /**
  260. * \brief Send a user specified ASDU
  261. *
  262. * \param asdu the ASDU to send
  263. *
  264. * \return true if message was sent, false otherwise
  265. */
  266. bool
  267. CS104_Connection_sendASDU(CS104_Connection self, CS101_ASDU asdu);
  268. /**
  269. * \brief Register a callback handler for received ASDUs
  270. *
  271. * \param handler user provided callback handler function
  272. * \param parameter user provided parameter that is passed to the callback handler
  273. */
  274. void
  275. CS104_Connection_setASDUReceivedHandler(CS104_Connection self, CS101_ASDUReceivedHandler handler, void* parameter);
  276. typedef enum {
  277. CS104_CONNECTION_OPENED = 0,
  278. CS104_CONNECTION_CLOSED = 1,
  279. CS104_CONNECTION_STARTDT_CON_RECEIVED = 2,
  280. CS104_CONNECTION_STOPDT_CON_RECEIVED = 3,
  281. CS104_CONNECTION_FAILED = 4
  282. } CS104_ConnectionEvent;
  283. /**
  284. * \brief Handler that is called when the connection is established or closed
  285. *
  286. * \note Calling \ref CS104_Connection_destroy or \ref CS104_Connection_close inside
  287. * of the callback causes a memory leak!
  288. *
  289. * \param parameter user provided parameter
  290. * \param connection the connection object
  291. * \param event event type
  292. */
  293. typedef void (*CS104_ConnectionHandler) (void* parameter, CS104_Connection connection, CS104_ConnectionEvent event);
  294. /**
  295. * \brief Set the connection event handler
  296. *
  297. * \param handler user provided callback handler function
  298. * \param parameter user provided parameter that is passed to the callback handler
  299. */
  300. void
  301. CS104_Connection_setConnectionHandler(CS104_Connection self, CS104_ConnectionHandler handler, void* parameter);
  302. /**
  303. * \brief Set the raw message callback (called when a message is sent or received)
  304. *
  305. * \param handler user provided callback handler function
  306. * \param parameter user provided parameter that is passed to the callback handler
  307. */
  308. void
  309. CS104_Connection_setRawMessageHandler(CS104_Connection self, IEC60870_RawMessageHandler handler, void* parameter);
  310. /**
  311. * \brief Close the connection
  312. */
  313. void
  314. CS104_Connection_close(CS104_Connection self);
  315. /**
  316. * \brief Close the connection and free all related resources
  317. */
  318. void
  319. CS104_Connection_destroy(CS104_Connection self);
  320. /*! @} */
  321. /*! @} */
  322. #ifdef __cplusplus
  323. }
  324. #endif
  325. #endif /* SRC_INC_CS104_CONNECTION_H_ */