cs104_slave.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * cs104_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 _CS104_SLAVE_H_
  24. #define _CS104_SLAVE_H_
  25. #include "iec60870_slave.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * \file cs104_slave.h
  31. * \brief CS 104 slave side definitions
  32. */
  33. /**
  34. * @addtogroup SLAVE Slave related functions
  35. *
  36. * @{
  37. */
  38. /**
  39. * @defgroup CS104_SLAVE CS 104 slave (TCP/IP server) related functions
  40. *
  41. * @{
  42. */
  43. typedef struct sCS104_Slave* CS104_Slave;
  44. typedef enum {
  45. CS104_MODE_SINGLE_REDUNDANCY_GROUP,
  46. CS104_MODE_CONNECTION_IS_REDUNDANCY_GROUP,
  47. CS104_MODE_MULTIPLE_REDUNDANCY_GROUPS
  48. } CS104_ServerMode;
  49. typedef enum
  50. {
  51. IP_ADDRESS_TYPE_IPV4,
  52. IP_ADDRESS_TYPE_IPV6
  53. } eCS104_IPAddressType;
  54. typedef struct sCS104_RedundancyGroup* CS104_RedundancyGroup;
  55. /**
  56. * \brief Connection request handler is called when a client tries to connect to the server.
  57. *
  58. * \param parameter user provided parameter
  59. * \param ipAddress string containing IP address and TCP port number (e.g. "192.168.1.1:34521")
  60. *
  61. * \return true to accept the connection request, false to deny
  62. */
  63. typedef bool (*CS104_ConnectionRequestHandler) (void* parameter, const char* ipAddress);
  64. typedef enum {
  65. CS104_CON_EVENT_CONNECTION_OPENED = 0,
  66. CS104_CON_EVENT_CONNECTION_CLOSED = 1,
  67. CS104_CON_EVENT_ACTIVATED = 2,
  68. CS104_CON_EVENT_DEACTIVATED = 3
  69. } CS104_PeerConnectionEvent;
  70. /**
  71. * \brief Handler that is called when a peer connection is established or closed, or START_DT/STOP_DT is issued
  72. *
  73. * \param parameter user provided parameter
  74. * \param connection the connection object
  75. * \param event event type
  76. */
  77. typedef void (*CS104_ConnectionEventHandler) (void* parameter, IMasterConnection connection, CS104_PeerConnectionEvent event);
  78. /**
  79. * \brief Callback handler for sent and received messages
  80. *
  81. * This callback handler provides access to the raw message buffer of received or sent
  82. * messages. It can be used for debugging purposes. Usually it is not used nor required
  83. * for applications.
  84. *
  85. * \param parameter user provided parameter
  86. * \param connection the connection that sent or received the message
  87. * \param msg the message buffer
  88. * \param msgSize size of the message
  89. * \param sent indicates if the message was sent or received
  90. */
  91. typedef void (*CS104_SlaveRawMessageHandler) (void* parameter, IMasterConnection connection, uint8_t* msg, int msgSize, bool send);
  92. /**
  93. * \brief Create a new instance of a CS104 slave (server)
  94. *
  95. * \param maxLowPrioQueueSize the maximum size of the event queue
  96. * \param maxHighPrioQueueSize the maximum size of the high-priority queue
  97. *
  98. * \return the new slave instance
  99. */
  100. CS104_Slave
  101. CS104_Slave_create(int maxLowPrioQueueSize, int maxHighPrioQueueSize);
  102. /**
  103. * \brief Create a new instance of a CS104 slave (server) with TLS enabled
  104. *
  105. * \param maxLowPrioQueueSize the maximum size of the event queue
  106. * \param maxHighPrioQueueSize the maximum size of the high-priority queue
  107. * \param tlsConfig the TLS configuration object (containing configuration parameters, keys, and certificates)
  108. *
  109. * \return the new slave instance
  110. */
  111. //CS104_Slave
  112. //CS104_Slave_createSecure(int maxLowPrioQueueSize, int maxHighPrioQueueSize, TLSConfiguration tlsConfig);
  113. void
  114. CS104_Slave_addPlugin(CS104_Slave self, CS101_SlavePlugin plugin);
  115. /**
  116. * \brief Set the local IP address to bind the server
  117. * use "0.0.0.0" to bind to all interfaces
  118. *
  119. * \param self the slave instance
  120. * \param ipAddress the IP address string or hostname
  121. */
  122. void
  123. CS104_Slave_setLocalAddress(CS104_Slave self, const char* ipAddress);
  124. /**
  125. * \brief Set the local TCP port to bind the server
  126. *
  127. * \param self the slave instance
  128. * \param tcpPort the TCP port to use (default is 2404)
  129. */
  130. void
  131. CS104_Slave_setLocalPort(CS104_Slave self, int tcpPort);
  132. /**
  133. * \brief Get the number of connected clients
  134. *
  135. * \param self the slave instance
  136. */
  137. int
  138. CS104_Slave_getOpenConnections(CS104_Slave self);
  139. /**
  140. * \brief set the maximum number of open client connections allowed
  141. *
  142. * NOTE: the number cannot be larger than the static maximum defined in
  143. *
  144. * \param self the slave instance
  145. * \param maxOpenConnections the maximum number of open client connections allowed
  146. */
  147. void
  148. CS104_Slave_setMaxOpenConnections(CS104_Slave self, int maxOpenConnections);
  149. /**
  150. * \brief Set one of the server modes
  151. *
  152. * \param self the slave instance
  153. * \param serverMode the server mode (see \ref CS104_ServerMode) to use
  154. */
  155. void
  156. CS104_Slave_setServerMode(CS104_Slave self, CS104_ServerMode serverMode);
  157. /**
  158. * \brief Set the connection request handler
  159. *
  160. * The connection request handler is called whenever a client/master is trying to connect.
  161. * This handler can be used to implement access control mechanisms as it allows the user to decide
  162. * if the new connection is accepted or not.
  163. *
  164. * \param self the slave instance
  165. * \param handler the callback function to be used
  166. * \param parameter user provided context parameter that will be passed to the callback function (or NULL if not required).
  167. */
  168. void
  169. CS104_Slave_setConnectionRequestHandler(CS104_Slave self, CS104_ConnectionRequestHandler handler, void* parameter);
  170. /**
  171. * \brief Set the connection event handler
  172. *
  173. * The connection request handler is called whenever a connection event happens. A connection event
  174. * can be when a client connects or disconnects, or when a START_DT or STOP_DT message is received.
  175. *
  176. * \param self the slave instance
  177. * \param handler the callback function to be used
  178. * \param parameter user provided context parameter that will be passed to the callback function (or NULL if not required).
  179. */
  180. void
  181. CS104_Slave_setConnectionEventHandler(CS104_Slave self, CS104_ConnectionEventHandler handler, void* parameter);
  182. void
  183. CS104_Slave_setInterrogationHandler(CS104_Slave self, CS101_InterrogationHandler handler, void* parameter);
  184. void
  185. CS104_Slave_setCounterInterrogationHandler(CS104_Slave self, CS101_CounterInterrogationHandler handler, void* parameter);
  186. /**
  187. * \brief set handler for read request (C_RD_NA_1 - 102)
  188. */
  189. void
  190. CS104_Slave_setReadHandler(CS104_Slave self, CS101_ReadHandler handler, void* parameter);
  191. void
  192. CS104_Slave_setASDUHandler(CS104_Slave self, CS101_ASDUHandler handler, void* parameter);
  193. void
  194. CS104_Slave_setClockSyncHandler(CS104_Slave self, CS101_ClockSynchronizationHandler handler, void* parameter);
  195. /**
  196. * \brief Set the raw message callback (called when a message is sent or received)
  197. *
  198. * \param handler user provided callback handler function
  199. * \param parameter user provided parameter that is passed to the callback handler
  200. */
  201. void
  202. CS104_Slave_setRawMessageHandler(CS104_Slave self, CS104_SlaveRawMessageHandler handler, void* parameter);
  203. /**
  204. * \brief Get the APCI parameters instance. APCI parameters are CS 104 specific parameters.
  205. */
  206. CS104_APCIParameters
  207. CS104_Slave_getConnectionParameters(CS104_Slave self);
  208. /**
  209. * \brief Get the application layer parameters instance..
  210. */
  211. CS101_AppLayerParameters
  212. CS104_Slave_getAppLayerParameters(CS104_Slave self);
  213. /**
  214. * \brief Start the CS 104 slave. The slave (server) will listen on the configured TCP/IP port
  215. *
  216. * NOTE: This function will start a thread that handles the incoming client connections.
  217. * This function requires CONFIG_USE_THREADS = 1 and CONFIG_USE_SEMAPHORES == 1 in lib60870_config.h
  218. *
  219. * \param self CS104_Slave instance
  220. */
  221. void
  222. CS104_Slave_start(CS104_Slave self);
  223. /**
  224. * \brief Check if slave is running
  225. *
  226. * \param self CS104_Slave instance
  227. *
  228. * \return true when slave is running, false otherwise
  229. */
  230. bool
  231. CS104_Slave_isRunning(CS104_Slave self);
  232. /**
  233. * \brief Stop the server.
  234. *
  235. * Stop listening to incoming TCP/IP connections and close all open connections.
  236. * Event buffers will be deactivated.
  237. */
  238. void
  239. CS104_Slave_stop(CS104_Slave self);
  240. /**
  241. * \brief Start the slave (server) in non-threaded mode.
  242. *
  243. * Start listening to incoming TCP/IP connections.
  244. *
  245. * NOTE: Server should only be started after all configuration is done.
  246. */
  247. void
  248. CS104_Slave_startThreadless(CS104_Slave self);
  249. /**
  250. * \brief Stop the server in non-threaded mode
  251. *
  252. * Stop listening to incoming TCP/IP connections and close all open connections.
  253. * Event buffers will be deactivated.
  254. */
  255. void
  256. CS104_Slave_stopThreadless(CS104_Slave self);
  257. /**
  258. * \brief Protocol stack tick function for non-threaded mode.
  259. *
  260. * Handle incoming connection requests and messages, send buffered events, and
  261. * handle periodic tasks.
  262. *
  263. * NOTE: This function has to be called periodically by the application.
  264. */
  265. void
  266. CS104_Slave_tick(CS104_Slave self);
  267. /*
  268. * \brief Gets the number of ASDU in the low-priority queue
  269. *
  270. * NOTE: Mode CS104_MODE_CONNECTION_IS_REDUNDANCY_GROUP is not supported by this function.
  271. *
  272. * \param redGroup the redundancy group to use or NULL for single redundancy mode
  273. *
  274. * \return the number of ASDU in the low-priority queue
  275. */
  276. int
  277. CS104_Slave_getNumberOfQueueEntries(CS104_Slave self, CS104_RedundancyGroup redGroup);
  278. /**
  279. * \brief Add an ASDU to the low-priority queue of the slave (use for periodic and spontaneous messages)
  280. *
  281. * \param asdu the ASDU to add
  282. */
  283. void
  284. CS104_Slave_enqueueASDU(CS104_Slave self, CS101_ASDU asdu);
  285. /**
  286. * \brief Add a new redundancy group to the server.
  287. *
  288. * A redundancy group is a group of clients that share the same event queue. This function can
  289. * only be used with server mode CS104_MODE_MULTIPLE_REDUNDANCY_GROUPS.
  290. *
  291. * NOTE: Has to be called before the server is started!
  292. *
  293. * \param redundancyGroup the new redundancy group
  294. */
  295. void
  296. CS104_Slave_addRedundancyGroup(CS104_Slave self, CS104_RedundancyGroup redundancyGroup);
  297. /**
  298. * \brief Delete the slave instance. Release all resources.
  299. */
  300. void
  301. CS104_Slave_destroy(CS104_Slave self);
  302. /**
  303. * \brief Create a new redundancy group.
  304. *
  305. * A redundancy group is a group of clients that share the same event queue. Redundancy groups can
  306. * only be used with server mode CS104_MODE_MULTIPLE_REDUNDANCY_GROUPS.
  307. */
  308. CS104_RedundancyGroup
  309. CS104_RedundancyGroup_create(const char* name);
  310. /**
  311. * \brief Add an allowed client to the redundancy group
  312. *
  313. * \param ipAddress the IP address of the client as C string (can be IPv4 or IPv6 address).
  314. */
  315. void
  316. CS104_RedundancyGroup_addAllowedClient(CS104_RedundancyGroup self, const char* ipAddress);
  317. /**
  318. * \brief Add an allowed client to the redundancy group
  319. *
  320. * \param ipAddress the IP address as byte buffer (4 byte for IPv4, 16 byte for IPv6)
  321. * \param addressType type of the IP address (either IP_ADDRESS_TYPE_IPV4 or IP_ADDRESS_TYPE_IPV6)
  322. */
  323. void
  324. CS104_RedundancyGroup_addAllowedClientEx(CS104_RedundancyGroup self, const uint8_t* ipAddress, eCS104_IPAddressType addressType);
  325. /**
  326. * \brief Destroy the instance and release all resources.
  327. *
  328. * NOTE: This function will be called by \ref CS104_Slave_destroy. After using
  329. * the \ref CS104_Slave_addRedundancyGroup function the redundancy group object must
  330. * not be destroyed manually.
  331. */
  332. void
  333. CS104_RedundancyGroup_destroy(CS104_RedundancyGroup self);
  334. /**
  335. * @}
  336. */
  337. /**
  338. * @}
  339. */
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #endif /* SRC_INC_API_CS104_SLAVE_H_ */