Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp
- Revision:
- 34:11fffbe98ef9
- Parent:
- 33:a4c015046956
- Child:
- 49:36954b62f503
diff -r a4c015046956 -r 11fffbe98ef9 OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp --- a/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp Thu Mar 31 11:56:01 2016 -0500 +++ b/OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp Fri Apr 01 09:29:55 2016 -0500 @@ -35,7 +35,7 @@ class DS28E15_22_25 : public OneWireSlave { public: - enum DeviceFamily + enum FamilyCode { DS28E25_FAMILY = 0x47, DS28E22_FAMILY = 0x48, @@ -43,7 +43,7 @@ UNKNOWN_FAMILY = 0 }; - enum DevicePages + enum MemoryPages { DS28E25_PAGES = 16, DS28E22_PAGES = 8, @@ -51,7 +51,7 @@ UNKNOWN_PAGES = 0 }; - enum DeviceBlocks + enum ProtectionBlocks { DS28E25_BLOCKS = 8, DS28E22_BLOCKS = 4, @@ -65,86 +65,116 @@ typedef array<std::uint8_t, 4> Segment; typedef array<std::uint8_t, 2> ManId; + class BlockProtection + { + private: + static const std::uint8_t readProtectionMask = 0x80, writeProtectionMask = 0x40, eepromEmulationMask = 0x20, authProtectionMask = 0x10, blockNumMask = 0x03; + std::uint8_t m_status; + + public: + BlockProtection() : m_status(0x00) { } + BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, std::uint8_t blockNum); + + std::uint8_t status() const { return m_status; } + void setStatus(std::uint8_t status) { m_status = status; } + + std::uint8_t blockNum() const { return (m_status & blockNumMask); } + void setBlockNum(std::uint8_t blockNum); + + bool readProtection() const { return ((m_status & readProtectionMask) == readProtectionMask); } + void setReadProtection(bool readProtection); + + bool writeProtection() const { return ((m_status & writeProtectionMask) == writeProtectionMask); } + void setWriteProtection(bool writeProtection); + + bool eepromEmulation() const { return ((m_status & eepromEmulationMask) == eepromEmulationMask); } + void setEepromEmulation(bool eepromEmulation); + + bool authProtection() const { return ((m_status & authProtectionMask) == authProtectionMask); } + void setAuthProtection(bool authProtection); + + bool noProtection() const; + + bool operator==(const BlockProtection & rhs) const { return (this->m_status == rhs.m_status); } + bool operator!=(const BlockProtection & rhs) const { return !operator==(rhs); } + }; + + static ISha256MacCoprocessor::CmdResult calculateSegmentWriteMac(const ISha256MacCoprocessor & MacCoproc, + unsigned int pageNum, + unsigned int segmentNum, + const Segment & newData, + const Segment & oldData, + const RomId & romId, + const ManId & manId, + Mac & mac); + static ISha256MacCoprocessor::CmdResult calculateProtectionWriteMac(const ISha256MacCoprocessor & MacCoproc, + const BlockProtection & newProtection, + const BlockProtection & oldProtection, + const RomId & romId, + const ManId & manId, + Mac & mac); + static ISha256MacCoprocessor::CmdResult calculateNextSecret(ISha256MacCoprocessor & MacCoproc, + const Page & binding, + const Scratchpad & partial, + const RomId & romId, + const ManId & manId, + unsigned int pageNum); + static ISha256MacCoprocessor::CmdResult calculateAuthMac(const ISha256MacCoprocessor & MacCoproc, + unsigned int pageNum, + const Scratchpad & challenge, + const Page & pageData, + const RomId & romId, + const ManId & manId, + bool anon, + Mac & mac); + // Manufacturer ID ManId manId; - static ISha256MacCoprocessor::CmdResult CalculateSegmentWriteMAC256(const ISha256MacCoprocessor & MacCoproc, - unsigned int pageNum, - unsigned int segmentNum, - const Segment & newData, - const Segment & oldData, - const RomId & romId, - const ManId & manId, - Mac & mac); - static ISha256MacCoprocessor::CmdResult CalculateProtectionWriteMAC256(const ISha256MacCoprocessor & MacCoproc, - std::uint8_t newProtection, - std::uint8_t oldProtection, - const RomId & romId, - const ManId & manId, - Mac & mac); - static ISha256MacCoprocessor::CmdResult CalculateNextSecret(ISha256MacCoprocessor & MacCoproc, - const Page & binding, - const Scratchpad & partial, - const RomId & romId, - const ManId & manId, - unsigned int pageNum); - static ISha256MacCoprocessor::CmdResult CalculateAuthMAC256(const ISha256MacCoprocessor & MacCoproc, - unsigned int pageNum, - const Scratchpad & challenge, - const Page & pageData, - const RomId & romId, - const ManId & manId, - bool anon, - Mac & mac); - static ISha256MacCoprocessor::CmdResult AuthVerify(const ISha256MacCoprocessor & MacCoproc, - unsigned int pageNum, - const Scratchpad & challenge, - const Page & pageData, - const RomId & romId, - const ManId & manId, - bool anon, - const Mac & mac); + // Low voltage + bool lowVoltage; - DS28E15_22_25(OneWireMaster & OW_master); + DS28E15_22_25(OneWireMaster & OW_master, bool lowVoltage = false); // Const member functions should not affect the state of the memory, block protection, or secret on the DS28Exx. // Scratchpad on the DS28Exx is considered mutable. - DevicePages devicePages(); - DeviceBlocks deviceBlocks(); + MemoryPages memoryPages(); + ProtectionBlocks protectionBlocks(); //DS28Exx Specific Functions (DS28E15, DS28E22 & DS28E25) - CmdResult LoadSecret(bool lock); - CmdResult WriteScratchpad(const Scratchpad & data) const; + CmdResult loadSecret(bool lock); + CmdResult writeScratchpad(const Scratchpad & data) const; CmdResult readSegment(unsigned int pageNum, unsigned int segmentNum, Segment & data) const; - CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing); - CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing) const; - CmdResult ComputeSecret(unsigned int pageNum, bool lock); - CmdResult ComputeReadPageMAC(unsigned int pageNum, bool anon, Mac & mac) const; - CmdResult WriteBlockProtection(std::uint8_t protection, bool continuing); - CmdResult WriteAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, - std::uint8_t newProtection, - std::uint8_t oldProtection, - bool continuing); + CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing = false); + CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing = false) const; + CmdResult computeSecret(unsigned int pageNum, bool lock); + CmdResult computeReadPageMAC(unsigned int pageNum, bool anon, Mac & mac) const; + CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection); + CmdResult writeBlockProtection(const BlockProtection & protection, bool continuing = false); + CmdResult writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, + const BlockProtection & newProtection, + const BlockProtection & oldProtection, + bool continuing = false); CmdResult writeAuthSegment(const ISha256MacCoprocessor & MacCoproc, - unsigned int pageNum, - unsigned int segmentNum, - const Segment & newData, - const Segment & oldData, - bool continuing); - CmdResult writeAuthSegmentMAC(unsigned int pageNum, + unsigned int pageNum, + unsigned int segmentNum, + const Segment & newData, + const Segment & oldData, + bool continuing = false); + CmdResult writeAuthSegmentMac(unsigned int pageNum, unsigned int segmentNum, const Segment & newData, const Mac & mac, - bool continuing); - - CmdResult ReadBlockProtectionStatus(unsigned int blockNum, std::uint8_t & status); - // bool ReadAllBlockProtectionStatuses(std::uint8_t (&statuses)[numBlocks]); + bool continuing = false); private: + static const unsigned int shaComputationDelayMs = 3; + static const unsigned int eepromWriteDelayMs = 10; + unsigned int secretEepromWriteDelayMs() const { return (lowVoltage ? 200 : 100); } + OneWireMaster & m_OW_master; - - CmdResult ReadStatus(bool personality, bool allpages, unsigned int pageNum, unsigned char *rdbuf) const; + CmdResult readStatus(bool personality, bool allpages, unsigned int pageNum, std::uint8_t * rdbuf) const; }; #endif \ No newline at end of file