cipconnectionmanager.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include "cipconnectionobject.h"
  13. /**
  14. * @brief Connection Type constants of the Forward Open service request
  15. * Indicates either a
  16. * - Null Request
  17. * - Point-to-point connection request (unicast)
  18. * - Multicast connection request
  19. */
  20. typedef enum {
  21. kConnectionManagerGeneralStatusSuccess = 0x00, /**< General Status - Everything is ok */
  22. kConnectionManagerGeneralStatusExtendedStatus = 0x01, /**< Indicates that extended status is set */
  23. kConnectionManagerGeneralStatusResourceUnavailableForUnconnectedSend = 0x02,
  24. kConnectionManagerGeneralStatusPathSegmentErrorInUnconnectedSend = 0x04,
  25. kConnectionManagerGeneralStatusErrorInDataSegment = 0x09,
  26. kConnectionManagerGeneralStatusObjectStateError = 0x0C,
  27. kConnectionManagerGeneralStatusDeviceStateError = 0x10,
  28. kConnectionManagerGeneralStatusNotEnoughData = 0x13,
  29. kConnectionManagerGeneralStatusTooMuchData = 0x15,
  30. } ConnectionManagerGeneralStatus;
  31. /** @brief Connection Manager Error codes */
  32. typedef enum {
  33. kConnectionManagerExtendedStatusCodeSuccess = 0x00, /**< Obsolete code, should be General Status - Everything is ok */
  34. kConnectionManagerExtendedStatusCodeErrorConnectionInUseOrDuplicateForwardOpen
  35. = 0x0100, /**< General Status has to be 0x01, Connection is already in use, or a duplicate Forward Open was received */
  36. kConnectionManagerExtendedStatusCodeErrorTransportClassAndTriggerCombinationNotSupported
  37. = 0x0103, /**< General Status has to be 0x01, A Transport class and trigger combination has been specified, which is not supported by the target application */
  38. kConnectionManagerExtendedStatusCodeErrorOwnershipConflict = 0x0106, /**< General Status has to be 0x01, Another connection has already reserved some needed resources */
  39. kConnectionManagerExtendedStatusCodeErrorConnectionTargetConnectionNotFound =
  40. 0x0107, /**< General Status has to be 0x01, Forward Close error message, if connection to be closed is not found at the target */
  41. kConnectionManagerExtendedStatusCodeErrorTargetForConnectionNotConfigured =
  42. 0x0110, /**< General Status has to be 0x01, Target application not configured and connection request does not contain data segment for configuration */
  43. kConnectionManagerExtendedStatusCodeRpiNotSupported = 0x0111,
  44. kConnectionManagerExtendedStatusCodeErrorRpiValuesNotAcceptable = 0x0112, /**< General Status has to be 0x01, Requested RPI parameters outside of range, needs 6 16-bit extended status words, see Vol.1 Table 3-5.33 */
  45. kConnectionManagerExtendedStatusCodeErrorNoMoreConnectionsAvailable = 0x0113, /**< General Status has to be 0x01, No free connection slots available */
  46. kConnectionManagerExtendedStatusCodeErrorVendorIdOrProductcodeError = 0x0114, /**< General Status has to be 0x01, The Product Code or Vendor ID in the electronic key logical segment does not match the Product Code or Vendor ID of the device, or if the compatibility bit is set and one or both are zero, or cannot be emulated. */
  47. kConnectionManagerExtendedStatusCodeErrorDeviceTypeError = 0x0115, /**< General Status has to be 0x01, Device Type specified in the electronic key logical segment does not match the Device Type, or if the compatibility bit is set and Device Type is zero, or cannot be emulated. */
  48. kConnectionManagerExtendedStatusCodeErrorRevisionMismatch = 0x0116, /**< General Status has to be 0x01, Major and minor revision specified in the electronic key logical segment is not a valid revision of the device, or if the compatibility bit is set and the requested Major Revision and/or Minor Revision is 0 or the device cannot emulate the specified revision. */
  49. kConnectionManagerExtendedStatusCodeNonListenOnlyConnectionNotOpened = 0x0119, /**< General Status has to be 0x01, listen-only connection cannot be established, if no non-listen only connections are established */
  50. kConnectionManagerExtendedStatusCodeTargetObjectOutOfConnections = 0x011A, /**< Maximum number of connections supported by the instance of the target object exceeded */
  51. kConnectionManagerExtendedStatusCodeProductionInhibitTimerGreaterThanRpi =
  52. 0x011B, /**< The Production Inhibit Time is greater than the Target to Originator RPI */
  53. kConnectionManagerExtendedStatusCodeTransportClassNotSupported = 0x011C, /**< The transport class requested in the Transport Type/Trigger parameter is not supported. */
  54. kConnectionManagerExtendedStatusCodeProductionTriggerNotSuppoerted = 0x011D, /**< The production trigger requested in the Transport Type/Trigger parameter is not supported. */
  55. kConnectionManagerExtendedStatusCodeDirectionNotSupported = 0x011E, /**< The direction requested in the Transport Type/Trigger parameter is not supported */
  56. kConnectionManagerExtendedStatusCodeInvalidOToTNetworkConnectionFixVar =
  57. 0x011F, /**< Shall be returned as the result of specifying an O->T fixed / variable flag that is not supported. */
  58. kConnectionManagerExtendedStatusCodeInvalidTToONetworkConnectionFixVar =
  59. 0x0120, /**< Shall be returned as the result of specifying an T->O fixed / variable flag that is not supported. */
  60. kConnectionManagerExtendedStatusCodeInvalidOToTNetworkConnectionPriority =
  61. 0x0121, /**< Shall be returned as the result of specifying an O->T priority code that is not supported. */
  62. kConnectionManagerExtendedStatusCodeInvalidTToONetworkConnectionPriority =
  63. 0x0122, /**< Shall be returned as the result of specifying an T->O priority code that is not supported. */
  64. kConnectionManagerExtendedStatusCodeErrorInvalidOToTConnectionType = 0x0123, /**< Shall be returned as the result of specifying an O->T connection type that is not supported */
  65. kConnectionManagerExtendedStatusCodeErrorInvalidTToOConnectionType = 0x0124, /**< Shall be returned as the result of specifying a T->O connection type that is not supported */
  66. kConnectionManagerExtendedStatusCodeInvalidOToTNetworkConnectionRedundantOwner
  67. = 0x0125, /**< Shall be returned as the result of specifying an O->T Redundant Owner flag that is not supported. */
  68. kConnectionManagerExtendedStatusCodeInvalidConfigurationSize = 0x0126, /**< The data segment provided in the Connection_Path parameter did not contain an acceptable number of 16-bit words for the the configuration application path requested. Two additional status words shall follow, the error code plus the max size in words */
  69. kConnectionManagerExtendedStatusCodeErrorInvalidOToTConnectionSize = 0x0127, /**< The size of the consuming object declared in the Forward_Open request and available on the target does not match the size declared in the O->T Network Connection Parameter. Two additional status words shall follow, the error code plus the max size in words */
  70. kConnectionManagerExtendedStatusCodeErrorInvalidTToOConnectionSize = 0x0128, /**< The size of the consuming object declared in the Forward_Open request and available on the target does not match the size declared in the T->O Network Connection Parameter. Two additional status words shall follow, the error code plus the max size in words */
  71. kConnectionManagerExtendedStatusCodeInvalidConfigurationApplicationPath =
  72. 0x0129, /**< Configuration application path specified does not correspond to a valid configuration application path within the target application. This error could also be returned if a configuration application path was required, but not provided by a connection request. */
  73. kConnectionManagerExtendedStatusCodeInvalidConsumingApplicationPath = 0x012A, /**< Consumed application path specified does not correspond to a valid consumed application path within the target application. This error could also be returned if a consumed application path was required, but not provided by a connection request. */
  74. kConnectionManagerExtendedStatusCodeInvalidProducingApplicationPath = 0x012B, /**< Produced application path specified does not correspond to a valid produced application path within the target application. This error could also be returned if a produced application path was required, but not provided by a connection request. */
  75. kConnectionManagerExtendedStatusCodeConfigurationSymbolDoesNotExist = 0x012C,
  76. kConnectionManagerExtendedStatusCodeConsumingSymbolDoesNotExist = 0x012D,
  77. kConnectionManagerExtendedStatusCodeProducingSymbolDoesNotExist = 0x012E,
  78. kConnectionManagerExtendedStatusCodeInconsistentApplicationPathCombo = 0x012F, /**< */
  79. kConnectionManagerExtendedStatusCodeInconsistentConsumeDataFormat = 0x0130,
  80. kConnectionManagerExtendedStatusCodeInconsistentProduceDataFormat = 0x0131,
  81. kConnectionManagerExtendedStatusCodeNullForwardOpenNotSupported = 0x0132,
  82. kConnectionManagerExtendedStatusCodeConnectionTimeoutMultiplierNotAcceptable
  83. =
  84. 0x0133,
  85. kConnectionManagerExtendedStatusCodeMismatchedTToONetworkConnectionFixVar =
  86. 0x0135,
  87. kConnectionManagerExtendedStatusCodeMismatchedTToONetworkConnectionPriority =
  88. 0x0136,
  89. kConnectionManagerExtendedStatusCodeMismatchedTransportClass = 0x0137,
  90. kConnectionManagerExtendedStatusCodeMismatchedTToOProductionTrigger = 0x0138,
  91. kConnectionManagerExtendedStatusCodeMismatchedTToOProductionInhibitTimeSegment
  92. = 0x0139,
  93. kConnectionManagerExtendedStatusCodeConnectionTimedOut = 0x0203,
  94. kConnectionManagerExtendedStatusCodeUnconnectedRequestTimedOut = 0x0204,
  95. kConnectionManagerExtendedStatusCodeErrorParameterErrorInUnconnectedSendService
  96. = 0x0205, /**< */
  97. kConnectionManagerExtendedStatusCodeMessageToLargeForUnconnectedSendService =
  98. 0x0206,
  99. kConnectionManagerExtendedStatusCodeUnconnectedAcknowledgeWithoutReply =
  100. 0x0207,
  101. kConnectionManagerExtendedStatusCodeNoBufferMemoryAvailable = 0x0301,
  102. kConnectionManagerExtendedStatusCodeNetworkBandwithNotAvailableForData =
  103. 0x0302,
  104. kConnectionManagerExtendedStatusCodeNoConsumedConnectionIdFilterAvailable =
  105. 0x0303,
  106. kConnectionManagerExtendedStatusCodeNotConfiguredToSendScheduledPriorityData
  107. = 0x0304,
  108. kConnectionManagerExtendedStatusCodeScheduleSignatureMismatch = 0x0305,
  109. kConnectionManagerExtendedStatusCodeScheduleSignatureValidationNotPossible =
  110. 0x0306,
  111. kConnectionManagerExtendedStatusCodePortNotAvailable = 0x0311,
  112. kConnectionManagerExtendedStatusCodeLinkAddressNotValid = 0x0312,
  113. kConnectionManagerExtendedStatusCodeErrorInvalidSegmentTypeInPath = 0x0315, /**< */
  114. kConnectionManagerExtendedStatusCodeForwardCloseServiceConnectionPathMismatch
  115. = 0x0316,
  116. kConnectionManagerExtendedStatusCodeSchedulingNotSpecified = 0x0317,
  117. kConnectionManagerExtendedStatusCodeLinkAddressToSelfInvalid = 0x0318,
  118. kConnectionManagerExtendedStatusCodeSecondaryResourcesUnavailable = 0x0319,
  119. kConnectionManagerExtendedStatusCodeRackConnectionAlreadyEstablished = 0x031A,
  120. kConnectionManagerExtendedStatusCodeModuleConnectionAlreadyEstablished =
  121. 0x031B,
  122. kConnectionManagerExtendedStatusCodeMiscellaneous = 0x031C,
  123. kConnectionManagerExtendedStatusCodeRedundantConnectionMismatch = 0x031D,
  124. kConnectionManagerExtendedStatusCodeNoMoreUserConfigurableLinkConsumerResourcesAvailableInTheProducingModule
  125. = 0x031E,
  126. kConnectionManagerExtendedStatusCodeNoUserConfigurableLinkConsumerResourcesConfiguredInTheProducingModule
  127. = 0x031F,
  128. kConnectionManagerExtendedStatusCodeNetworkLinkOffline = 0x0800,
  129. kConnectionManagerExtendedStatusCodeNoTargetApplicationDataAvailable = 0x0810,
  130. kConnectionManagerExtendedStatusCodeNoOriginatorApplicationDataAvailable =
  131. 0x0811,
  132. kConnectionManagerExtendedStatusCodeNodeAddressHasChangedSinceTheNetworkWasScheduled
  133. = 0x0812,
  134. kConnectionManagerExtendedStatusCodeNotConfiguredForOffSubnetMulticast =
  135. 0x0813,
  136. kConnectionManagerExtendedStatusCodeInvalidProduceConsumeDataFormat = 0x0814,
  137. kConnectionManagerExtendedStatusWrongCloser = 0xFFFF /* No a real extended error code, but needed for forward close */
  138. } ConnectionManagerExtendedStatusCode;
  139. /** @brief macros for comparing sequence numbers according to CIP spec vol
  140. * 2 3-4.2 for int type variables
  141. * @def SEQ_LEQ32(a, b) Checks if sequence number a is less or equal than b
  142. * @def SEQ_GEQ32(a, b) Checks if sequence number a is greater or equal than
  143. * b
  144. * @def SEQ_GT32(a, b) Checks if sequence number a is greater than b
  145. */
  146. #define SEQ_LEQ32(a, b) ( (int)( (a) - (b) ) <= 0 )
  147. #define SEQ_GEQ32(a, b) ( (int)( (a) - (b) ) >= 0 )
  148. #define SEQ_GT32(a, b) ( (int)( (a) - (b) ) > 0 )
  149. /** @brief similar macros for comparing 16 bit sequence numbers
  150. * @def SEQ_LEQ16(a, b) Checks if sequence number a is less or equal than b
  151. * @def SEQ_GEQ16(a, b) Checks if sequence number a is greater or equal than
  152. * b
  153. */
  154. #define SEQ_LEQ16(a, b) ( (short)( (a) - (b) ) <= 0 )
  155. #define SEQ_GEQ16(a, b) ( (short)( (a) - (b) ) >= 0 )
  156. /** @brief Connection Manager class code */
  157. static const CipUint kCipConnectionManagerClassCode = 0x06U;
  158. /* public functions */
  159. /** @brief Initialize the data of the connection manager object
  160. *
  161. * @param unique_connection_id A unique connection id
  162. * @return kEipStatusOk if successful, otherwise kEipStatusError
  163. */
  164. EipStatus ConnectionManagerInit(EipUint16 unique_connection_id);
  165. /** @brief Get a connected object dependent on requested ConnectionID.
  166. *
  167. * @param connection_id Connection ID of the Connection Object to get
  168. * @return pointer to connected Object
  169. * NULL .. connection not present in device
  170. */
  171. CipConnectionObject *GetConnectedObject(const EipUint32 connection_id);
  172. /** Get a connection object for a given output assembly.
  173. *
  174. * @param output_assembly_id requested output assembly of requested
  175. * connection
  176. * @return pointer to connected Object
  177. * 0 .. connection not present in device
  178. */
  179. CipConnectionObject *GetConnectedOutputAssembly(
  180. const EipUint32 output_assembly_id);
  181. /** @brief Close the given connection
  182. *
  183. * This function will take the data form the connection and correctly closes the
  184. * connection (e.g., open sockets)
  185. * @param connection_object pointer to the connection object structure to be
  186. * closed
  187. */
  188. void CloseConnection(CipConnectionObject *RESTRICT connection_object);
  189. /* TODO: Missing documentation */
  190. EipBool8 IsConnectedOutputAssembly(const CipInstanceNum instance_number);
  191. /** @brief Insert the given connection object to the list of currently active
  192. * and managed connections.
  193. *
  194. * By adding a connection to the active connection list the connection manager
  195. * will perform the supervision and handle the timing (e.g., timeout,
  196. * production inhibit, etc).
  197. *
  198. * @param connection_object pointer to the connection object to be added.
  199. */
  200. void AddNewActiveConnection(CipConnectionObject *const connection_object);
  201. /** @brief Removes connection from the list of active connections
  202. *
  203. * @param connection_object Connection object to be removed from the active connection list
  204. */
  205. void RemoveFromActiveConnections(CipConnectionObject *const connection_object);
  206. CipUdint GetConnectionId(void);
  207. typedef void (*CloseSessionFunction)(const CipConnectionObject *const
  208. connection_object);
  209. void CheckForTimedOutConnectionsAndCloseTCPConnections(
  210. const CipConnectionObject *const connection_object,
  211. CloseSessionFunction CloseSessions);
  212. #endif /* OPENER_CIPCONNECTIONMANAGER_H_ */