encap.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #ifndef OPENER_ENCAP_H_
  7. #define OPENER_ENCAP_H_
  8. #include "typedefs.h"
  9. #include "cipconnectionobject.h"
  10. #include "generic_networkhandler.h"
  11. /** @file encap.h
  12. * @brief This file contains the public interface of the encapsulation layer
  13. */
  14. /** @defgroup ENCAP OpENer Ethernet encapsulation layer
  15. * The Ethernet encapsulation layer handles provides the abstraction between the Ethernet and the CIP layer.
  16. */
  17. /*** defines ***/
  18. #define ENCAPSULATION_HEADER_LENGTH 24
  19. /** @brief definition of status codes in encapsulation protocol
  20. * All other codes are either legacy codes, or reserved for future use
  21. * */
  22. typedef enum {
  23. kEncapsulationProtocolSuccess = 0x0000,
  24. kEncapsulationProtocolInvalidCommand = 0x0001,
  25. kEncapsulationProtocolInsufficientMemory = 0x0002,
  26. kEncapsulationProtocolIncorrectData = 0x0003,
  27. kEncapsulationProtocolInvalidSessionHandle = 0x0064,
  28. kEncapsulationProtocolInvalidLength = 0x0065,
  29. kEncapsulationProtocolUnsupportedProtocol = 0x0069
  30. } EncapsulationProtocolErrorCode;
  31. /*** structs ***/
  32. typedef struct encapsulation_data {
  33. CipUint command_code;
  34. CipUint data_length;
  35. CipSessionHandle session_handle;
  36. CipUdint status;
  37. CipOctet sender_context[8]; /**< length of 8, according to the specification */
  38. CipUdint options;
  39. const EipUint8 *communication_buffer_start; /**< Pointer to the communication buffer used for this message */
  40. const EipUint8 *current_communication_buffer_position; /**< The current position in the communication buffer during the decoding process */
  41. } EncapsulationData;
  42. typedef struct encapsulation_service_information {
  43. EipUint16 type_code;
  44. EipUint16 length;
  45. EipUint16 encapsulation_protocol_version;
  46. EipUint16 capability_flags;
  47. EipInt8 name_of_service[16];
  48. } EncapsulationServiceInformation;
  49. /*** global variables (public) ***/
  50. /*** public functions ***/
  51. /** @ingroup ENCAP
  52. * @brief Initialize the encapsulation layer.
  53. */
  54. void EncapsulationInit(void);
  55. /** @ingroup ENCAP
  56. * @brief Shutdown the encapsulation layer.
  57. *
  58. * This means that all open sessions including their sockets are closed.
  59. */
  60. void EncapsulationShutDown(void);
  61. /** @ingroup ENCAP
  62. * @brief Handle delayed encapsulation message responses
  63. *
  64. * Certain encapsulation message requests require a delayed sending of the response
  65. * message. This functions checks if messages need to be sent and performs the
  66. * sending.
  67. */
  68. void ManageEncapsulationMessages(const MilliSeconds elapsed_time);
  69. CipSessionHandle GetSessionFromSocket(const int socket_handle);
  70. void RemoveSession(const int socket);
  71. void CloseSessionBySessionHandle(const CipConnectionObject *const connection_object);
  72. void CloseEncapsulationSessionBySockAddr(const CipConnectionObject *const connection_object);
  73. void CloseClass3ConnectionBasedOnSession(CipSessionHandle encapsulation_session_handle);
  74. /* No reason to use this functions outside the encapsulation layer, they are here for testing */
  75. typedef struct enip_message ENIPMessage;
  76. void EncapsulateListIdentityResponseMessage(const EncapsulationData *const receive_data, ENIPMessage *const outgoing_message);
  77. int_fast32_t CreateEncapsulationStructure(const EipUint8 *receive_buffer,
  78. size_t receive_buffer_length,
  79. EncapsulationData *const encapsulation_data);
  80. void SkipEncapsulationHeader(ENIPMessage *const outgoing_message);
  81. void GenerateEncapsulationHeader(const EncapsulationData *const receive_data, const size_t command_specific_data_length, const CipSessionHandle session_handle,
  82. const EncapsulationProtocolErrorCode encapsulation_protocol_status, ENIPMessage *const outgoing_message);
  83. void HandleReceivedListServicesCommand(const EncapsulationData *const receive_data, ENIPMessage *const outgoing_message);
  84. void HandleReceivedListInterfacesCommand(const EncapsulationData *const receive_data, ENIPMessage *const outgoing_message);
  85. void HandleReceivedRegisterSessionCommand(int socket, const EncapsulationData *const receive_data, ENIPMessage *const outgoing_message);
  86. EipStatus HandleReceivedSendRequestResponseDataCommand(const EncapsulationData *const receive_data, const struct sockaddr *const originator_address,
  87. ENIPMessage *const outgoing_message);
  88. #endif /* OPENER_ENCAP_H_ */