Added ChipDataStream class
ChipDataStream makes it possible to read a buffer of binary data
using the stream operators, ">>".
Instantiate ChipDataStream class with a pointer to a data buffer
and its size in bytes as below:
ChipDataStream cds(streamData,552);
Use the ">>" stream operator then to read the basic integral types
(bool, char, uint8_t, int8_t, uint16_t, int16_t, uint32_t,
int32_t, uint64_t, and int64_t).
Change-Id: I8b99e1112c6c05a240dc5a9d9f7326d7c6842fd5
Signed-off-by: Paul Greenwood <Paul.Greenwood@ibm.com>
diff --git a/src/chip_data/hei_chip_data_stream.hpp b/src/chip_data/hei_chip_data_stream.hpp
new file mode 100755
index 0000000..6d4d303
--- /dev/null
+++ b/src/chip_data/hei_chip_data_stream.hpp
@@ -0,0 +1,179 @@
+#pragma once
+
+/**
+@file hei_chip_data_stream.hpp
+@brief Description: The ChipDataStream class
+
+ChipDataStream makes it possible to read a buffer of binary data using the stream operators, ">>".
+
+Instantiate ChipDataStream class with a pointer to a data buffer
+and its size in bytes as below:
+
+ ChipDataStream cds(streamData,552);
+
+Use the ">>" stream operator then to read the basic integral types
+(bool, char, uint8_t, int8_t, uint16_t, int16_t, uint32_t,
+int32_t, uint64_t, and int64_t).
+
+Endian issues are taken care of by template specialization.
+**/
+
+#include <endian.h>
+#include <hei_includes.hpp>
+#include <string.h>
+
+namespace libhei
+{
+
+/**
+* ChipDataStream
+* ChipDataStream offers convenient stream operator access to binary data.
+**/
+class ChipDataStream
+{
+
+ private:
+
+ /** iv_buffer points to the first address of the Chip Data File
+ buffer. **/
+ const void * const iv_buffer;
+ /** iv_bufferSize is the size of the data buffer. It is
+ initialized when the ChipDataStream class is instantiated. **/
+ const size_t iv_bufferSize;
+ /** iv_asyncOffset keeps an offset into the buffer. This
+ offset is incremented appropriately as data is read. **/
+ size_t iv_asyncOffset;
+
+ public:
+
+ /* Constructors */
+
+ /**
+ When instantiating the ChipDataStream object a pointer to a
+ data buffer containing all the data and its size is passed
+ into the class.
+ **/
+ ChipDataStream(void * i_buffer, size_t i_bufferSize) :
+ iv_asyncOffset(0), iv_buffer(i_buffer), iv_bufferSize(i_bufferSize) {}
+
+ /** Eliminate copy and assignment operator constructors **/
+ ChipDataStream ( const ChipDataStream & ) = delete;
+ ChipDataStream & operator=( const ChipDataStream & ) = delete;
+
+ /** Destructor **/
+
+ ~ChipDataStream() = default;
+
+ /** Functions **/
+
+ /**
+ *@brief Default case for "operator>>" template.
+ *@param D: A type
+ *@param o_right: A pointer to where the data is stored
+ *@return *this: A pointer to "this" object
+ **/
+ template <class D>
+ ChipDataStream & operator>>( D & o_right )
+ {
+ read( &o_right, sizeof(D) );
+ return *this;
+ }
+
+ private:
+
+ /**
+ *@brief Read does the copy from the buffer
+ *@param o_buf a pointer to the location to copy to
+ *@param i_size the size (in bytes) to copy
+ *@return None\n\n
+ **/
+ void read( void * o_buf, size_t i_size )
+ {
+ /* Ensure memory is not accessed outside i_buffer */
+ HEI_ASSERT((iv_asyncOffset + i_size) <= iv_bufferSize);
+ /* Copy appropriate bytes from i_buffer to o_buff */
+ memcpy(o_buf,(char *)iv_buffer+iv_asyncOffset,i_size);
+ /* Increment asynchronous offset to next piece of data */
+ iv_asyncOffset = iv_asyncOffset + i_size;
+ }
+};
+
+/*
+* Note: The specializations below ensure the big-endian Chip Data File
+* format is converted into the host format.
+*/
+
+/**
+* @brief This template extracts a uint16_t data type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( uint16_t & o_right )
+ {
+ read( &o_right, sizeof(o_right));
+ be16toh( o_right );
+ return *this;
+ }
+
+/**@brief This template extracts an int16_t type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( int16_t & o_right )
+ {
+ read( &o_right, sizeof(o_right) );
+ be16toh( o_right );
+ return *this;
+ }
+
+/**@brief This template extracts a uint32_t type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( uint32_t & o_right )
+ {
+ read( &o_right, sizeof(o_right) );
+ be32toh( o_right );
+ return *this;
+ }
+
+/**@brief This template extracts an int32_t type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( int32_t & o_right )
+ {
+ read( &o_right, sizeof(o_right) );
+ be32toh( o_right );
+ return *this;
+ }
+
+/**@brief This template extracts a uint64_t type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( uint64_t & o_right )
+ {
+ read( &o_right, sizeof(o_right) );
+ be64toh( o_right );
+ return *this;
+ }
+
+/**@brief This template extracts an int64_t type from the data buffer
+* @param o_right: A pointer to where the data is stored
+* @return *this: A pointer to "this" object
+**/
+template <> inline
+ ChipDataStream & ChipDataStream::operator>>( int64_t & o_right )
+ {
+ read( &o_right, sizeof(o_right) );
+ be64toh( o_right );
+ return *this;
+ }
+
+} /** namespace libhei **/
diff --git a/src/chip_data/hei_chip_type.hpp b/src/chip_data/hei_chip_type.hpp
new file mode 100644
index 0000000..352282e
--- /dev/null
+++ b/src/chip_data/hei_chip_type.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+/**
+* @file hei_chip_type.hpp
+* @brief Module Description
+*
+* This module contains common types and constants.
+*
+* End Module Description
+*/
+
+
+#include <stdint.h>
+
+namespace libhei
+{
+
+typedef uint32_t ChipType_t;
+
+static constexpr ChipType_t DEFAULT_CHIP_TYPE = 0;
+
+}//namespace libhei
diff --git a/test/hei_user_defines.hpp b/test/hei_user_defines.hpp
index 54c7344..8b23473 100644
--- a/test/hei_user_defines.hpp
+++ b/test/hei_user_defines.hpp
@@ -1,3 +1,11 @@
+#pragma once
+
+/**
+* @file hei_user_defines.hpp
+* @brief The purpose of this file is to create common defines that
+* will be used throughout this library.
+**/
+
#include <stdio.h>
#include <assert.h>
@@ -15,4 +23,3 @@
#define HEI_ASSERT( expression ) \
assert( expression );
-