Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
139:f0e0a7976846
Parent:
113:13e2865603df
diff -r 5bd0a7a82bb4 -r f0e0a7976846 Slaves/Authenticators/DS28E15_22_25/DS28E15_22_25.h
--- a/Slaves/Authenticators/DS28E15_22_25/DS28E15_22_25.h	Fri Dec 02 19:21:55 2016 +0000
+++ b/Slaves/Authenticators/DS28E15_22_25/DS28E15_22_25.h	Tue Dec 13 13:31:30 2016 -0800
@@ -47,61 +47,7 @@
         typedef array<uint8_t, 4> Segment;
 
         /// Holds the contents of a device memory page.
-        class Page
-        {
-        public:
-            /// Length of the buffer in bytes.
-            static const size_t length = 32;
-
-        private:
-            array<uint8_t, length> m_data;
-
-        public:
-            /// Built-in array representation.
-            typedef array<uint8_t, length>::Buffer Buffer;
-
-            Page() { }
-            Page(const Page & page) : m_data(page.m_data) { }
-            Page(const Buffer & dataBytes) : m_data(dataBytes) { }
-
-            const Page & operator=(const Page & rhs)
-            {
-                this->m_data = rhs.m_data;
-                return rhs;
-            }
-
-            bool operator==(const Page & rhs) const
-            {
-                return (this->m_data == rhs.m_data);
-            }
-
-            bool operator!=(const Page & rhs) const
-            {
-                return !operator==(rhs);
-            }
-
-            /// Conversion to array reference.
-            operator Buffer &()
-            {
-                return m_data;
-            }
-
-            /// Conversion to const array reference.
-            operator const Buffer &() const
-            {
-                return m_data;
-            }
-
-            /// Creates a segment representation from a subsection of the page data.
-            /// @param segmentNum Segment number within page to copy from.
-            /// @returns The copied segment data.
-            Segment toSegment(unsigned int segmentNum) const;
-
-            /// Copies segment data to the page.
-            /// @param segmentNum Segment number within the page to copy to.
-            /// @param[in] segment Segment to copy from.
-            void fromSegment(unsigned int segmentNum, const Segment & segment);
-        };
+        typedef array<uint8_t, 32> Page;
 
         /// Holds the contents of the device scratchpad.
         typedef array<uint8_t, 32> Scratchpad;
@@ -113,17 +59,25 @@
         typedef array<uint8_t, 2> ManId;
 
         /// Container for the device personality.
-        union Personality
+        class Personality
         {
-            array<uint8_t, 4>::Buffer bytes;
-            struct
-            {
-                uint8_t PB1;
-                uint8_t PB2;
-                ManId::Buffer manIdBytes;
+        public:
+            typedef array<uint8_t, 4> Buffer;
+            
+        private:
+            Buffer m_data;
 
-                bool secretLocked() const { return (PB2 & 0x01); }
-            } fields;
+        public:
+            Personality() { }
+            explicit Personality(const Buffer & dataBytes) : m_data(dataBytes) { }
+           
+            uint8_t PB1() const { return m_data[0]; }
+            uint8_t PB2() const { return m_data[1]; }
+            ManId manId() const { ManId manId = { m_data[2], m_data[3] }; return manId; }
+            bool secretLocked() const { return (PB2() & 0x01); }
+            
+            bool operator==(const Personality & rhs) const { return (this->m_data == rhs.m_data); }
+            bool operator!=(const Personality & rhs) const { return !operator==(rhs); }
         };
 
         /// Represents the status of a memory protection block.
@@ -134,8 +88,8 @@
             uint8_t m_status;
 
         public:
-            BlockProtection() : m_status(0x00) { }
-            BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, uint8_t blockNum);
+            explicit BlockProtection(uint8_t status = 0x00) : m_status(status) { }
+            BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, unsigned int blockNum);
 
             /// Get the byte representation used by the device.
             uint8_t statusByte() const { return m_status; }
@@ -143,9 +97,9 @@
             void setStatusByte(uint8_t status) { m_status = status; }
 
             /// Get the Block Number which is indexed from zero.
-            uint8_t blockNum() const { return (m_status & blockNumMask); }
+            unsigned int blockNum() const { return (m_status & blockNumMask); }
             /// Set the Block Number which is indexed from zero.
-            void setBlockNum(uint8_t blockNum);
+            void setBlockNum(unsigned int blockNum);
 
             /// Get the Read Protection status.
             /// @returns True if Read Protection is enabled.
@@ -260,7 +214,17 @@
                                                               Mac & mac);
 
         /// Number of segments per page.
-        static const unsigned int segmentsPerPage = (Page::length / Segment::length);
+        static const unsigned int segmentsPerPage = (Page::csize / Segment::csize);
+        
+        /// Creates a segment representation from a subsection of the page data.
+        /// @param segmentNum Segment number within page to copy from.
+        /// @returns The copied segment data.
+        static Segment segmentFromPage(unsigned int segmentNum, const Page & page);
+
+        /// Copies segment data to the page.
+        /// @param segmentNum Segment number within the page to copy to.
+        /// @param[in] segment Segment to copy from.
+        static void segmentToPage(unsigned int segmentNum, const Segment & segment, Page & page);
 
         /// @{
         /// Manufacturer ID
@@ -282,16 +246,6 @@
         /// @param lock Prevent further changes to the secret on the device after loading.
         CmdResult loadSecret(bool lock);
 
-        /// Perform Write Scratchpad operation on the device.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param[in] data Data to write to the scratchpad.
-        CmdResult writeScratchpad(const Scratchpad & data) const;
-
-        /// Perform a Read Scratchpad operation on the device.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param[out] data Buffer to read data from the scratchpad into.
-        CmdResult readScratchpad(Scratchpad & data) const;
-
         /// Read memory segment using the Read Memory command on the device.
         /// @note 1-Wire ROM selection should have already occurred.
         /// @param pageNum Page number for read operation.
@@ -332,17 +286,6 @@
         /// @param[out] mac The device computed MAC.
         CmdResult computeReadPageMac(unsigned int pageNum, bool anon, Mac & mac) const;
 
-        /// Read the status of a memory protection block using the Read Status command.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param blockNum Block number to to read status of.
-        /// @param[out] protection Receives protection status read from device.
-        CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection);
-
-        /// Read the personality bytes using the Read Status command.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param[out] personality Receives personality read from device.
-        CmdResult readPersonality(Personality & personality) const;
-
         /// Update the status of a memory protection block using the Write Page Protection command.
         /// @note 1-Wire ROM selection should have already occurred.
         /// @param[in] Desired protection status for the block.
@@ -361,68 +304,60 @@
         CmdResult writeAuthBlockProtection(const ISha256MacCoproc & MacCoproc,
                                            const BlockProtection & newProtection,
                                            const BlockProtection & oldProtection);
-
-        /// Write memory segment with authentication using the Authenticated Write Memory command.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param MacCoproc Coprocessor to use for Write MAC computation.
-        /// @param pageNum Page number for write operation.
-        /// @param segmentNum Segment number within page for write operation.
-        /// @param[in] newData New data to write to the segment.
-        /// @param[in] oldData Existing data contained in the segment.
-        /// @param continuing True to continue writing with the next sequential segment.
-        ///                   False to begin a new command.
-        CmdResult writeAuthSegment(const ISha256MacCoproc & MacCoproc,
-                                   unsigned int pageNum,
-                                   unsigned int segmentNum,
-                                   const Segment & newData,
-                                   const Segment & oldData,
-                                   bool continuing = false);
-
-        /// Write memory segment with authentication using the Authenticated Write Memory command.
-        /// @note 1-Wire ROM selection should have already occurred.
-        /// @param pageNum Page number for write operation.
-        /// @param segmentNum Segment number within page for write operation.
-        /// @param[in] newData New data to write to the segment.
-        /// @param[in] mac Write MAC computed for this operation.
-        /// @param continuing True to continue writing with the next sequential segment.
-        ///                   False to begin a new command.
-        CmdResult writeAuthSegmentMac(unsigned int pageNum,
-                                      unsigned int segmentNum,
-                                      const Segment & newData,
-                                      const Mac & mac,
-                                      bool continuing = false);
                                       
-    protected:
-        /// Family code for each device.
-        enum FamilyCode
-        {
-            DS28E25_Family = 0x47,
-            DS28E22_Family = 0x48,
-            DS28E15_Family = 0x17
-        };
-    
+    protected:    
         /// @param owMaster 1-Wire Master to use for communication with DS28E15/22/25.
         /// @param lowVoltage Enable low voltage timing.
-        DS28E15_22_25(RandomAccessRomIterator & selector, bool lowVoltage, FamilyCode variant);
+        DS28E15_22_25(RandomAccessRomIterator & selector, bool lowVoltage);
         
         ~DS28E15_22_25() { }
+        
+        template <class T>
+        CmdResult doWriteScratchpad(const Scratchpad & data) const;
+
+        template <class T>
+        CmdResult doReadScratchpad(Scratchpad & data) const;
+        
+        template <class T>
+        CmdResult doReadBlockProtection(unsigned int blockNum, BlockProtection & protection) const;
+
+        template <class T>
+        CmdResult doReadPersonality(Personality & personality) const;
+        
+        template <class T>
+        CmdResult doWriteAuthSegment(const ISha256MacCoproc & MacCoproc,
+                                     unsigned int pageNum,
+                                     unsigned int segmentNum,
+                                     const Segment & newData,
+                                     const Segment & oldData,
+                                     bool continuing);
+
+        template <class T>
+        CmdResult doWriteAuthSegmentMac(unsigned int pageNum,
+                                        unsigned int segmentNum,
+                                        const Segment & newData,
+                                        const Mac & mac,
+                                        bool continuing);
+        
+        template <class T, size_t N>
+        CmdResult doReadAllBlockProtection(array<BlockProtection, N> & protection) const;
 
     private:
-        ManId m_manId;
-        bool m_lowVoltage;
-        const FamilyCode m_variant;
-    
-        static const unsigned int shaComputationDelayMs = 3;
-        static const unsigned int eepromWriteDelayMs = 10;
-        unsigned int secretEepromWriteDelayMs() const { return (m_lowVoltage ? 200 : 100); }
-
         /// Read status bytes which are either personality or block protection.
         /// @note 1-Wire ROM selection should have already occurred.
         /// @param personality True to read personality or false to read block protection.
         /// @param allpages True to read all pages or false to read one page.
         /// @param pageNum Page number if reading block protection.
         /// @param rdbuf Buffer to receive data read from device.
+        template <class T>
         CmdResult readStatus(bool personality, bool allpages, unsigned int blockNum, uint8_t * rdbuf) const;
+    
+        ManId m_manId;
+        bool m_lowVoltage;
+    
+        static const unsigned int shaComputationDelayMs = 3;
+        static const unsigned int eepromWriteDelayMs = 10;
+        unsigned int secretEepromWriteDelayMs() const { return (m_lowVoltage ? 200 : 100); }
     };
 }