cpf.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #ifndef OPENER_CPF_H_
  7. #define OPENER_CPF_H_
  8. #include "typedefs.h"
  9. #include "ciptypes.h"
  10. #include "encap.h"
  11. /** @ingroup ENCAP
  12. * @brief CPF is Common Packet Format
  13. * CPF packet := \<number of items\> {\<items\>}
  14. * item := \<TypeID\> \<Length\> \<data\>
  15. * \<number of items\> := two bytes
  16. * \<TypeID\> := two bytes
  17. * \<Length\> := two bytes
  18. * \<data\> := \<the number of bytes specified by Length\>
  19. */
  20. /** @brief Definition of Item ID numbers used for address and data items in CPF structures */
  21. typedef enum {
  22. kCipItemIdNullAddress = 0x0000, /**< Type: Address; Indicates that encapsulation routing is not needed. */
  23. kCipItemIdListIdentityResponse = 0x000C,
  24. kCipItemIdConnectionAddress = 0x00A1, /**< Type: Address; Connection-based, used for connected messages, see Vol.2, p.42 */
  25. kCipItemIdConnectedDataItem = 0x00B1, /**< Type: Data; Connected data item, see Vol.2, p.43 */
  26. kCipItemIdUnconnectedDataItem = 0x00B2, /**< Type: Data; Unconnected message */
  27. kCipItemIdListServiceResponse = 0x0100,
  28. kCipItemIdSocketAddressInfoOriginatorToTarget = 0x8000, /**< Type: Data; Sockaddr info item originator to target */
  29. kCipItemIdSocketAddressInfoTargetToOriginator = 0x8001, /**< Type: Data; Sockaddr info item target to originator */
  30. kCipItemIdSequencedAddressItem = 0x8002 /**< Sequenced Address item */
  31. } CipItemId;
  32. typedef struct {
  33. EipUint32 connection_identifier;
  34. EipUint32 sequence_number;
  35. } AddressData;
  36. typedef struct {
  37. CipUint type_id;
  38. CipUint length;
  39. AddressData data;
  40. } AddressItem;
  41. typedef struct {
  42. EipUint16 type_id;
  43. EipUint16 length;
  44. EipUint8 *data;
  45. } DataItem;
  46. /** @brief CPF Sockaddr Item
  47. *
  48. */
  49. typedef struct {
  50. CipUint type_id; /**< Either 0x8000 for O->T or 0x8001 for T->O */
  51. CipUint length; /**< Length shall be 16 bytes */
  52. CipInt sin_family; /**< Shall be AF_INET = 2 in big endian order */
  53. CipUint sin_port; /**< For point-to-point connection this shall be set to the used UDP port (recommended port = 0x08AE). For multicast this shall be set to 0x08AE and treated by the receiver as don't care. Big endian order */
  54. CipUdint sin_addr; /**< For multicast connections shall be set to the multicast address. For point-to-point shall be treated as don't care, recommended value 0. Big endian order. */
  55. CipUsint nasin_zero[8]; /**< Length of 8, Recommended value zero */
  56. } SocketAddressInfoItem;
  57. /* this one case of a CPF packet is supported:*/
  58. /** @brief A variant of a CPF packet, including item count, one address item, one data item, and two Sockaddr Info items */
  59. typedef struct {
  60. EipUint16 item_count; /**< Up to four for this structure allowed */
  61. AddressItem address_item;
  62. DataItem data_item;
  63. SocketAddressInfoItem address_info_item[2];
  64. } CipCommonPacketFormatData;
  65. /** @ingroup ENCAP
  66. * Parse the CPF data from a received unconnected explicit message and
  67. * hand the data on to the message router
  68. *
  69. * @param received_data pointer to the encapsulation structure with the received message
  70. * @param originator_address Address struct of the originator
  71. * @param outgoing_message The outgoing ENIP message struct
  72. * @return kEipStatusOkSend: a response needs to be sent, others: EIP stack status
  73. */
  74. EipStatus NotifyCommonPacketFormat(
  75. const EncapsulationData *const received_data,
  76. const struct sockaddr *const originator_address,
  77. ENIPMessage *const outgoing_message);
  78. /** @ingroup ENCAP
  79. * Parse the CPF data from a received connected explicit message, check
  80. * the connection status, update any timers, and hand the data on to
  81. * the message router
  82. *
  83. * @param received_data pointer to the encapsulation structure with the received message
  84. * @param originator_address Address struct of the originator
  85. * @param outgoing_message The outgoing ENIP message struct
  86. * @return kEipStatusOkSend: a response needs to be sent, others: EIP stack status
  87. */
  88. EipStatus NotifyConnectedCommonPacketFormat(
  89. const EncapsulationData *const received_data,
  90. const struct sockaddr *const originator_address,
  91. ENIPMessage *const outgoing_message);
  92. /** @ingroup ENCAP
  93. * Create CPF structure out of the received data.
  94. * @param data pointer to data which need to be structured.
  95. * @param data_length length of data in pa_Data.
  96. * @param common_packet_format_data pointer to structure of CPF data item.
  97. * @return status
  98. * EIP_OK .. success
  99. * EIP_ERROR .. error
  100. */
  101. EipStatus CreateCommonPacketFormatStructure(
  102. const EipUint8 *data,
  103. size_t data_length,
  104. CipCommonPacketFormatData *common_packet_format_data);
  105. /** @ingroup ENCAP
  106. * Copy data from CPFDataItem into linear memory in message for transmission over in encapsulation.
  107. * @param common_packet_format_data_item pointer to CPF structure which has to be aligned into linear memory.
  108. * @param message Modified ENIPMessage struct
  109. * @return length of modification in bytes
  110. * kEipStatusError .. error
  111. */
  112. void AssembleIOMessage(
  113. const CipCommonPacketFormatData *const common_packet_format_data_item,
  114. ENIPMessage *const message);
  115. /** @ingroup ENCAP
  116. * @brief Copy data from message_router_response struct and common_packet_format_data_item into
  117. * ENIPMessage struct outgoing_message via encapsulation.
  118. *
  119. * @param message_router_response pointer to message router response which has to be aligned into linear memory.
  120. * @param common_packet_format_data_item pointer to CPF structure which has to be aligned into linear memory.
  121. * @param outgoing_message Modified ENIPMessage struct
  122. * @return length of modification in bytes
  123. * kEipStatusError .. error
  124. */
  125. EipStatus AssembleLinearMessage(
  126. const CipMessageRouterResponse *const message_router_response,
  127. const CipCommonPacketFormatData *const common_packet_format_data_item,
  128. ENIPMessage *const outgoing_message);
  129. /** @ingroup ENCAP
  130. * @brief Data storage for the any CPF data
  131. * Currently we are single threaded and need only one CPF at the time.
  132. * For future extensions towards multithreading maybe more CPF data items may be necessary
  133. */
  134. extern CipCommonPacketFormatData g_common_packet_format_data_item;
  135. #endif /* OPENER_CPF_H_ */