endianconv.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #ifndef OPENER_ENDIANCONV_H_
  7. #define OPENER_ENDIANCONV_H_
  8. #include "typedefs.h"
  9. #include "ciptypes.h"
  10. /** @file endianconv.h
  11. * @brief Responsible for Endianess conversion
  12. */
  13. typedef enum {
  14. kOpenerEndianessUnknown = -1,
  15. kOpENerEndianessLittle = 0,
  16. kOpENerEndianessBig = 1
  17. } OpenerEndianess;
  18. /** @ingroup ENCAP
  19. * @brief Reads EIP_UINT8 from *buffer and converts little endian to host.
  20. * @param buffer pointer where data should be reed.
  21. * @return EIP_UINT8 data value
  22. */
  23. CipSint GetSintFromMessage(const EipUint8 **const buffer);
  24. CipByte GetByteFromMessage(const CipOctet **const buffer_address);
  25. CipUsint GetUsintFromMessage(const CipOctet **const buffer_address);
  26. CipBool GetBoolFromMessage(const EipBool8 **const buffer_address);
  27. /** @ingroup ENCAP
  28. *
  29. * @brief Get an 16Bit integer from the network buffer, and moves pointer beyond the 16 bit value
  30. * @param buffer Pointer to the network buffer array. This pointer will be incremented by 2!
  31. * @return Extracted 16 bit integer value
  32. */
  33. CipInt GetIntFromMessage(const EipUint8 **const buffer);
  34. CipUint GetUintFromMessage(const CipOctet **const buffer_address);
  35. CipWord GetWordFromMessage(const CipOctet **const buffer_address);
  36. /** @ingroup ENCAP
  37. *
  38. * @brief Get an 32Bit integer from the network buffer.
  39. * @param buffer pointer to the network buffer array. This pointer will be incremented by 4!
  40. * @return Extracted 32 bit integer value
  41. */
  42. CipDint GetDintFromMessage(const EipUint8 **const buffer);
  43. CipUdint GetUdintFromMessage(const CipOctet **const buffer_address);
  44. CipUdint GetDwordFromMessage(const CipOctet **const buffer_address);
  45. /** @ingroup ENCAP
  46. *
  47. * @brief converts UINT8 data from host to little endian an writes it to buffer.
  48. * @param data value to be written
  49. * @param buffer pointer where data should be written.
  50. */
  51. void AddSintToMessage(const EipUint8 data,
  52. ENIPMessage *const outgoing_message);
  53. /** @ingroup ENCAP
  54. *
  55. * @brief Write an 16Bit integer to the network buffer.
  56. * @param data value to write
  57. * @param buffer pointer to the network buffer array. This pointer will be incremented by 2!
  58. *
  59. * @return Length in bytes of the encoded message
  60. */
  61. void AddIntToMessage(const EipUint16 data,
  62. ENIPMessage *const outgoing_message);
  63. /** @ingroup ENCAP
  64. *
  65. * @brief Write an 32Bit integer to the network buffer.
  66. * @param data value to write
  67. * @param buffer pointer to the network buffer array. This pointer will be incremented by 4!
  68. *
  69. * @return Length in bytes of the encoded message
  70. */
  71. void AddDintToMessage(const EipUint32 data,
  72. ENIPMessage *const outgoing_message);
  73. EipUint64 GetLintFromMessage(const EipUint8 **const buffer);
  74. /** @ingroup ENCAP
  75. *
  76. * @brief Write an 64Bit integer to the network buffer.
  77. * @param data value to write
  78. * @param buffer pointer to the network buffer array. This pointer will be incremented by 8!
  79. *
  80. */
  81. void AddLintToMessage(const EipUint64 pa_unData,
  82. ENIPMessage *const outgoing_message);
  83. /** @brief Encapsulate the sockaddr information as necessary for the Common Packet Format data items
  84. *
  85. * Converts and adds the provided port and IP address into an common packet format message
  86. *
  87. * @param port Port of the socket, has to be provided in big-endian
  88. * @param address IP address of the socket, has to be provided in big-endian
  89. * @param communication_buffer The message buffer for sending the message
  90. */
  91. void EncapsulateIpAddress(EipUint16 port,
  92. EipUint32 address,
  93. ENIPMessage *const outgoing_message);
  94. /** Identify if we are running on a big or little endian system and set
  95. * variable.
  96. */
  97. void DetermineEndianess(void);
  98. /** @brief Return the endianess identified on system startup
  99. * @return
  100. * - -1 endianess has not been identified up to now
  101. * - 0 little endian system
  102. * - 1 big endian system
  103. */
  104. int GetEndianess(void);
  105. void MoveMessageNOctets(const int amount_of_bytes_moved,
  106. ENIPMessage *const outgoing_message);
  107. void FillNextNMessageOctetsWith(CipOctet value,
  108. unsigned int amount_of_bytes_written,
  109. ENIPMessage *const outgoing_message);
  110. void FillNextNMessageOctetsWithValueAndMoveToNextPosition(CipOctet value,
  111. unsigned int amount_of_filled_bytes,
  112. ENIPMessage *const outgoing_message);
  113. #endif /* OPENER_ENDIANCONV_H_ */