cipconnectionmanager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #ifndef OPENER_CIPCONNECTIONMANAGER_H_
  7. #define OPENER_CIPCONNECTIONMANAGER_H_
  8. #include "opener_user_conf.h"
  9. #include "opener_api.h"
  10. #include "typedefs.h"
  11. #include "ciptypes.h"
  12. /**
  13. * @brief Sets the routing type of a connection, either
  14. * - Point-to-point connections (unicast)
  15. * - Multicast connection
  16. */
  17. typedef enum {
  18. kRoutingTypePointToPointConnection = 0x4000,
  19. kRoutingTypeMulticastConnection = 0x2000
  20. } RoutingType;
  21. /** @brief Connection Manager Error codes */
  22. typedef enum {
  23. kConnectionManagerStatusCodeSuccess = 0x00,
  24. kConnectionManagerStatusCodeErrorConnectionInUse = 0x0100,
  25. kConnectionManagerStatusCodeErrorTransportTriggerNotSupported = 0x0103,
  26. kConnectionManagerStatusCodeErrorOwnershipConflict = 0x0106,
  27. kConnectionManagerStatusCodeErrorConnectionNotFoundAtTargetApplication = 0x0107,
  28. kConnectionManagerStatusCodeErrorInvalidOToTConnectionType = 0x123,
  29. kConnectionManagerStatusCodeErrorInvalidTToOConnectionType = 0x124,
  30. kConnectionManagerStatusCodeErrorInvalidOToTConnectionSize = 0x127,
  31. kConnectionManagerStatusCodeErrorInvalidTToOConnectionSize = 0x128,
  32. kConnectionManagerStatusCodeErrorNoMoreConnectionsAvailable = 0x0113,
  33. kConnectionManagerStatusCodeErrorVendorIdOrProductcodeError = 0x0114,
  34. kConnectionManagerStatusCodeErrorDeviceTypeError = 0x0115,
  35. kConnectionManagerStatusCodeErrorRevisionMismatch = 0x0116,
  36. kConnectionManagerStatusCodeInvalidConfigurationApplicationPath = 0x0129,
  37. kConnectionManagerStatusCodeInvalidConsumingApllicationPath = 0x012A,
  38. kConnectionManagerStatusCodeInvalidProducingApplicationPath = 0x012B,
  39. kConnectionManagerStatusCodeInconsistentApplicationPathCombo = 0x012F,
  40. kConnectionManagerStatusCodeNonListenOnlyConnectionNotOpened = 0x0119,
  41. kConnectionManagerStatusCodeErrorParameterErrorInUnconnectedSendService = 0x0205,
  42. kConnectionManagerStatusCodeErrorInvalidSegmentTypeInPath = 0x0315,
  43. kConnectionManagerStatusCodeTargetObjectOutOfConnections = 0x011A
  44. } ConnectionManagerStatusCode;
  45. typedef enum {
  46. kConnectionTriggerTypeProductionTriggerMask = 0x70,
  47. kConnectionTriggerTypeCyclicConnection = 0x0,
  48. kConnectionTriggerTypeChangeOfStateTriggeredConnection = 0x10,
  49. kConnectionTriggerTypeApplicationTriggeredConnection = 0x20
  50. } ConnectionTriggerType;
  51. /** @brief macros for comparing sequence numbers according to CIP spec vol
  52. * 2 3-4.2 for int type variables
  53. * @define SEQ_LEQ32(a, b) Checks if sequence number a is less or equal than b
  54. * @define SEQ_GEQ32(a, b) Checks if sequence number a is greater or equal than
  55. * b
  56. * @define SEQ_GT32(a, b) Checks if sequence number a is greater than b
  57. */
  58. #define SEQ_LEQ32(a, b) ((int)((a) - (b)) <= 0)
  59. #define SEQ_GEQ32(a, b) ((int)((a) - (b)) >= 0)
  60. #define SEQ_GT32(a, b) ((int)((a) - (b)) > 0)
  61. /** @brief similar macros for comparing 16 bit sequence numbers
  62. * @define SEQ_LEQ16(a, b) Checks if sequence number a is less or equal than b
  63. * @define SEQ_GEQ16(a, b) Checks if sequence number a is greater or equal than
  64. * b
  65. */
  66. #define SEQ_LEQ16(a, b) ((short)((a) - (b)) <= 0)
  67. #define SEQ_GEQ16(a, b) ((short)((a) - (b)) >= 0)
  68. /** @brief States of a connection */
  69. typedef enum {
  70. kConnectionStateNonExistent = 0,
  71. kConnectionStateConfiguring = 1,
  72. kConnectionStateWaitingForConnectionId = 2 /**< only used in DeviceNet */,
  73. kConnectionStateEstablished = 3,
  74. kConnectionStateTimedOut = 4,
  75. kConnectionStateDeferredDelete = 5 /**< only used in DeviceNet */,
  76. kConnectionStateClosing
  77. } ConnectionState;
  78. /** @brief instance_type attributes */
  79. typedef enum {
  80. kConnectionTypeExplicit = 0,
  81. kConnectionTypeIoExclusiveOwner = 0x01,
  82. kConnectionTypeIoInputOnly = 0x11,
  83. kConnectionTypeIoListenOnly = 0x21
  84. } ConnectionType;
  85. /** @brief Possible values for the watch dog time out action of a connection */
  86. typedef enum {
  87. kWatchdogTimeoutActionTransitionToTimedOut = 0, /**< , invalid for explicit message connections */
  88. kWatchdogTimeoutActionAutoDelete = 1, /**< Default for explicit message connections,
  89. default for I/O connections on EIP */
  90. kWatchdogTimeoutActionAutoReset = 2, /**< Invalid for explicit message connections */
  91. kWatchdogTimeoutActionDeferredDelete = 3 /**< Only valid for DeviceNet, invalid for I/O connections */
  92. } WatchdogTimeoutAction;
  93. typedef struct {
  94. ConnectionState state;
  95. EipUint16 connection_id;
  96. /*TODO think if this is needed anymore
  97. TCMReceiveDataFunc m_ptfuncReceiveData; */
  98. } LinkConsumer;
  99. typedef struct {
  100. ConnectionState state;
  101. EipUint16 connection_id;
  102. } LinkProducer;
  103. typedef struct {
  104. LinkConsumer consumer;
  105. LinkProducer producer;
  106. } LinkObject;
  107. /** The data needed for handling connections. This data is strongly related to
  108. * the connection object defined in the CIP-specification. However the full
  109. * functionality of the connection object is not implemented. Therefore this
  110. * data can not be accessed with CIP means.
  111. */
  112. typedef struct connection_object {
  113. ConnectionState state;
  114. ConnectionType instance_type;
  115. /* conditional
  116. EipUint16 DeviceNetProductedConnectionID;
  117. EipUint16 DeviceNetConsumedConnectionID;
  118. */
  119. EipByte device_net_initial_comm_characteristcs;
  120. EipUint16 produced_connection_size;
  121. EipUint16 consumed_connection_size;
  122. EipUint16 expected_packet_rate;
  123. /*conditional*/
  124. EipUint32 produced_connection_id;
  125. EipUint32 consumed_connection_id;
  126. /**/
  127. WatchdogTimeoutAction watchdog_timeout_action;
  128. EipUint16 produced_connection_path_length;
  129. CipEpath produced_connection_path;
  130. EipUint16 consumed_connection_path_length;
  131. CipEpath consumed_connection_path;
  132. /* conditional
  133. UINT16 ProductionInhibitTime;
  134. */
  135. /* non CIP Attributes, only relevant for opened connections */
  136. EipByte priority_timetick;
  137. EipUint8 timeout_ticks;
  138. EipUint16 connection_serial_number;
  139. EipUint16 originator_vendor_id;
  140. EipUint32 originator_serial_number;
  141. EipUint16 connection_timeout_multiplier;
  142. EipUint32 o_to_t_requested_packet_interval;
  143. EipUint16 o_to_t_network_connection_parameter;
  144. EipUint32 t_to_o_requested_packet_interval;
  145. EipUint16 t_to_o_network_connection_parameter;
  146. EipByte transport_type_class_trigger;
  147. EipUint8 connection_path_size;
  148. CipElectronicKey electronic_key;
  149. CipConnectionPath connection_path; /* padded EPATH*/
  150. LinkObject link_object;
  151. CipInstance *consuming_instance;
  152. /*S_CIP_CM_Object *p_stConsumingCMObject; */
  153. CipInstance *producing_instance;
  154. /*S_CIP_CM_Object *p_stProducingCMObject; */
  155. EipUint32 eip_level_sequence_count_producing; /* the EIP level sequence Count
  156. for Class 0/1
  157. Producing Connections may have a
  158. different
  159. value than SequenceCountProducing */
  160. EipUint32 eip_level_sequence_count_consuming; /* the EIP level sequence Count
  161. for Class 0/1
  162. Producing Connections may have a
  163. different
  164. value than SequenceCountProducing */
  165. EipUint16 sequence_count_producing; /* sequence Count for Class 1 Producing
  166. Connections */
  167. EipUint16 sequence_count_consuming; /* sequence Count for Class 1 Producing
  168. Connections */
  169. EipInt32 transmission_trigger_timer;
  170. EipInt32 inactivity_watchdog_timer;
  171. /** @brief Minimal time between the production of two application triggered
  172. * or change of state triggered I/O connection messages
  173. */
  174. EipUint16 production_inhibit_time;
  175. /** @brief Timer for the production inhibition of application triggered or
  176. * change-of-state I/O connections.
  177. */
  178. EipInt32 production_inhibit_timer;
  179. struct sockaddr_in remote_address; /* socket address for produce */
  180. struct sockaddr_in originator_address; /* the address of the originator that
  181. established the connection. needed
  182. for scanning if the right packet is
  183. arriving */
  184. int socket[2]; /* socket handles, indexed by kConsuming or kProducing */
  185. /* pointers to connection handling functions */
  186. ConnectionCloseFunction connection_close_function;
  187. ConnectionTimeoutFunction connection_timeout_function;
  188. ConnectionSendDataFunction connection_send_data_function;
  189. ConnectionReceiveDataFunction connection_receive_data_function;
  190. /* pointers to be used in the active connection list */
  191. struct connection_object *next_connection_object;
  192. struct connection_object *first_connection_object;
  193. EipUint16 correct_originator_to_target_size;
  194. EipUint16 correct_target_to_originator_size;
  195. } ConnectionObject;
  196. /** @brief Connection Manager class code */
  197. static const int g_kCipConnectionManagerClassCode = 0x06;
  198. /* public functions */
  199. /** @brief Initialize the data of the connection manager object
  200. *
  201. * @param A unique connection id
  202. * @return kEipStatusOk if successful, otherwise kEipStatusError
  203. */
  204. EipStatus ConnectionManagerInit(EipUint16 unique_connection_id);
  205. /** @brief Get a connected object dependent on requested ConnectionID.
  206. *
  207. * @param connection_id requested @var connection_id of opened connection
  208. * @return pointer to connected Object
  209. * 0 .. connection not present in device
  210. */
  211. ConnectionObject* GetConnectedObject(EipUint32 connection_id);
  212. /** Get a connection object for a given output assembly.
  213. *
  214. * @param output_assembly_id requested output assembly of requested
  215. * connection
  216. * @return pointer to connected Object
  217. * 0 .. connection not present in device
  218. */
  219. ConnectionObject *GetConnectedOutputAssembly(EipUint32 output_assembly_id);
  220. /** @brief Copy the given connection data from source to destination
  221. *
  222. * @param destination Destination of the copy operation
  223. * @param osurce Source of the copy operation
  224. */
  225. void CopyConnectionData(ConnectionObject *RESTRICT destination, const ConnectionObject *RESTRICT const source);
  226. /** @brief Close the given connection
  227. *
  228. * This function will take the data form the connection and correctly closes the
  229. *connection (e.g., open sockets)
  230. * @param connection_object pointer to the connection object structure to be
  231. *closed
  232. */
  233. void CloseConnection(ConnectionObject *RESTRICT connection_object);
  234. /* TODO: Missing documentation */
  235. EipBool8 IsConnectedOutputAssembly(const EipUint32 instance_number);
  236. /** @brief Generate the ConnectionIDs and set the general configuration
  237. * parameter in the given connection object.
  238. *
  239. * @param connection_object pointer to the connection object that should be set
  240. * up.
  241. */
  242. void GeneralConnectionConfiguration(ConnectionObject *connection_object);
  243. /** @brief Insert the given connection object to the list of currently active
  244. * and managed connections.
  245. *
  246. * By adding a connection to the active connection list the connection manager
  247. * will perform the supervision and handle the timing (e.g., timeout,
  248. * production inhibit, etc).
  249. *
  250. * @param connection_object pointer to the connection object to be added.
  251. */
  252. void AddNewActiveConnection(ConnectionObject *connection_object);
  253. /** @brief Removes connection from the list of active connections
  254. *
  255. * @param connection_object Connection object to be removed from the active connection list
  256. */
  257. void RemoveFromActiveConnections(ConnectionObject *connection_object);
  258. #endif /* OPENER_CIPCONNECTIONMANAGER_H_ */