Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp@51:a65f031e997b, 2016-04-11 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Apr 11 15:34:22 2016 -0500
- Revision:
- 51:a65f031e997b
- Parent:
- 50:e967f9befbd0
- Child:
- 60:e8244cbc7946
Marked new read status functions as const. Switched to correct grouping type for members.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 25:bdb1c5a53b58 | 1 | //------------Copyright (C) 2013 Maxim Integrated Products -------------- |
IanBenzMaxim | 25:bdb1c5a53b58 | 2 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 25:bdb1c5a53b58 | 4 | // copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 25:bdb1c5a53b58 | 5 | // to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 25:bdb1c5a53b58 | 6 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 25:bdb1c5a53b58 | 7 | // and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 25:bdb1c5a53b58 | 8 | // Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 25:bdb1c5a53b58 | 9 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 10 | // The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 25:bdb1c5a53b58 | 11 | // in all copies or substantial portions of the Software. |
IanBenzMaxim | 25:bdb1c5a53b58 | 12 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 25:bdb1c5a53b58 | 14 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 25:bdb1c5a53b58 | 15 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 25:bdb1c5a53b58 | 16 | // IN NO EVENT SHALL MAXIM INTEGRATED PRODCUTS BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 25:bdb1c5a53b58 | 17 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 25:bdb1c5a53b58 | 18 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 25:bdb1c5a53b58 | 19 | // OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 25:bdb1c5a53b58 | 20 | // |
IanBenzMaxim | 25:bdb1c5a53b58 | 21 | // Except as contained in this notice, the name of Maxim Integrated Products |
IanBenzMaxim | 25:bdb1c5a53b58 | 22 | // shall not be used except as stated in the Maxim Integrated Products |
IanBenzMaxim | 25:bdb1c5a53b58 | 23 | // Branding Policy. |
IanBenzMaxim | 25:bdb1c5a53b58 | 24 | // --------------------------------------------------------------------------- |
IanBenzMaxim | 25:bdb1c5a53b58 | 25 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 26 | #ifndef DS28E15_22_25_H |
IanBenzMaxim | 25:bdb1c5a53b58 | 27 | #define DS28E15_22_25_H |
IanBenzMaxim | 25:bdb1c5a53b58 | 28 | |
IanBenzMaxim | 33:a4c015046956 | 29 | #include "array.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 30 | #include "OneWire_Masters/ISha256MacCoprocessor.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 31 | #include "OneWireSlave.hpp" |
IanBenzMaxim | 25:bdb1c5a53b58 | 32 | |
IanBenzMaxim | 27:d5aaefa252f1 | 33 | class OneWireMaster; |
IanBenzMaxim | 27:d5aaefa252f1 | 34 | |
IanBenzMaxim | 49:36954b62f503 | 35 | /// Interface to the DS28E15/22/25 series of authenticators |
IanBenzMaxim | 49:36954b62f503 | 36 | /// including low power variants. |
IanBenzMaxim | 25:bdb1c5a53b58 | 37 | class DS28E15_22_25 : public OneWireSlave |
IanBenzMaxim | 25:bdb1c5a53b58 | 38 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 39 | public: |
IanBenzMaxim | 49:36954b62f503 | 40 | /// Family code for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 41 | enum FamilyCode |
IanBenzMaxim | 25:bdb1c5a53b58 | 42 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 43 | DS28E25_FAMILY = 0x47, |
IanBenzMaxim | 25:bdb1c5a53b58 | 44 | DS28E22_FAMILY = 0x48, |
IanBenzMaxim | 25:bdb1c5a53b58 | 45 | DS28E15_FAMILY = 0x17, |
IanBenzMaxim | 25:bdb1c5a53b58 | 46 | UNKNOWN_FAMILY = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 47 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 48 | |
IanBenzMaxim | 49:36954b62f503 | 49 | /// Number for memory pages for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 50 | enum MemoryPages |
IanBenzMaxim | 25:bdb1c5a53b58 | 51 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 52 | DS28E25_PAGES = 16, |
IanBenzMaxim | 25:bdb1c5a53b58 | 53 | DS28E22_PAGES = 8, |
IanBenzMaxim | 25:bdb1c5a53b58 | 54 | DS28E15_PAGES = 2, |
IanBenzMaxim | 25:bdb1c5a53b58 | 55 | UNKNOWN_PAGES = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 56 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 57 | |
IanBenzMaxim | 49:36954b62f503 | 58 | /// Number of protection blocks for each device. |
IanBenzMaxim | 34:11fffbe98ef9 | 59 | enum ProtectionBlocks |
IanBenzMaxim | 25:bdb1c5a53b58 | 60 | { |
IanBenzMaxim | 25:bdb1c5a53b58 | 61 | DS28E25_BLOCKS = 8, |
IanBenzMaxim | 25:bdb1c5a53b58 | 62 | DS28E22_BLOCKS = 4, |
IanBenzMaxim | 25:bdb1c5a53b58 | 63 | DS28E15_BLOCKS = 4, |
IanBenzMaxim | 25:bdb1c5a53b58 | 64 | UNKNOWN_BLOCKS = 0 |
IanBenzMaxim | 25:bdb1c5a53b58 | 65 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 66 | |
IanBenzMaxim | 49:36954b62f503 | 67 | /// Holds the contents of a device memory page. |
IanBenzMaxim | 33:a4c015046956 | 68 | typedef array<std::uint8_t, 32> Page; |
IanBenzMaxim | 49:36954b62f503 | 69 | |
IanBenzMaxim | 49:36954b62f503 | 70 | /// Holds the contents of the device scratchpad. |
IanBenzMaxim | 33:a4c015046956 | 71 | typedef array<std::uint8_t, 32> Scratchpad; |
IanBenzMaxim | 49:36954b62f503 | 72 | |
IanBenzMaxim | 49:36954b62f503 | 73 | /// Container for a SHA-256 MAC. |
IanBenzMaxim | 33:a4c015046956 | 74 | typedef array<std::uint8_t, 32> Mac; |
IanBenzMaxim | 49:36954b62f503 | 75 | |
IanBenzMaxim | 49:36954b62f503 | 76 | /// Holds the contents of a device memory segment. |
IanBenzMaxim | 33:a4c015046956 | 77 | typedef array<std::uint8_t, 4> Segment; |
IanBenzMaxim | 49:36954b62f503 | 78 | |
IanBenzMaxim | 49:36954b62f503 | 79 | /// Container for a manufacturer ID. |
IanBenzMaxim | 33:a4c015046956 | 80 | typedef array<std::uint8_t, 2> ManId; |
IanBenzMaxim | 25:bdb1c5a53b58 | 81 | |
IanBenzMaxim | 50:e967f9befbd0 | 82 | /// Container for the device personality. |
IanBenzMaxim | 50:e967f9befbd0 | 83 | union Personality |
IanBenzMaxim | 50:e967f9befbd0 | 84 | { |
IanBenzMaxim | 50:e967f9befbd0 | 85 | array<std::uint8_t, 4>::Buffer bytes; |
IanBenzMaxim | 50:e967f9befbd0 | 86 | struct |
IanBenzMaxim | 50:e967f9befbd0 | 87 | { |
IanBenzMaxim | 50:e967f9befbd0 | 88 | std::uint8_t PB1; |
IanBenzMaxim | 50:e967f9befbd0 | 89 | std::uint8_t PB2; |
IanBenzMaxim | 50:e967f9befbd0 | 90 | ManId::Buffer manIdBytes; |
IanBenzMaxim | 50:e967f9befbd0 | 91 | } fields; |
IanBenzMaxim | 50:e967f9befbd0 | 92 | |
IanBenzMaxim | 50:e967f9befbd0 | 93 | bool secretLocked() const { return (fields.PB2 & 0x01); } |
IanBenzMaxim | 50:e967f9befbd0 | 94 | }; |
IanBenzMaxim | 50:e967f9befbd0 | 95 | |
IanBenzMaxim | 49:36954b62f503 | 96 | /// Represents the status of a memory protection block. |
IanBenzMaxim | 34:11fffbe98ef9 | 97 | class BlockProtection |
IanBenzMaxim | 34:11fffbe98ef9 | 98 | { |
IanBenzMaxim | 34:11fffbe98ef9 | 99 | private: |
IanBenzMaxim | 34:11fffbe98ef9 | 100 | static const std::uint8_t readProtectionMask = 0x80, writeProtectionMask = 0x40, eepromEmulationMask = 0x20, authProtectionMask = 0x10, blockNumMask = 0x03; |
IanBenzMaxim | 34:11fffbe98ef9 | 101 | std::uint8_t m_status; |
IanBenzMaxim | 34:11fffbe98ef9 | 102 | |
IanBenzMaxim | 34:11fffbe98ef9 | 103 | public: |
IanBenzMaxim | 34:11fffbe98ef9 | 104 | BlockProtection() : m_status(0x00) { } |
IanBenzMaxim | 34:11fffbe98ef9 | 105 | BlockProtection(bool readProtection, bool writeProtection, bool eepromEmulation, bool authProtection, std::uint8_t blockNum); |
IanBenzMaxim | 34:11fffbe98ef9 | 106 | |
IanBenzMaxim | 49:36954b62f503 | 107 | /// Get the byte representation used by the device. |
IanBenzMaxim | 49:36954b62f503 | 108 | std::uint8_t statusByte() const { return m_status; } |
IanBenzMaxim | 49:36954b62f503 | 109 | /// Set the byte representation used by the device. |
IanBenzMaxim | 49:36954b62f503 | 110 | void setStatusByte(std::uint8_t status) { m_status = status; } |
IanBenzMaxim | 34:11fffbe98ef9 | 111 | |
IanBenzMaxim | 49:36954b62f503 | 112 | /// Get the Block Number which is indexed from zero. |
IanBenzMaxim | 34:11fffbe98ef9 | 113 | std::uint8_t blockNum() const { return (m_status & blockNumMask); } |
IanBenzMaxim | 49:36954b62f503 | 114 | /// Set the Block Number which is indexed from zero. |
IanBenzMaxim | 34:11fffbe98ef9 | 115 | void setBlockNum(std::uint8_t blockNum); |
IanBenzMaxim | 34:11fffbe98ef9 | 116 | |
IanBenzMaxim | 49:36954b62f503 | 117 | /// Get the Read Protection status. |
IanBenzMaxim | 49:36954b62f503 | 118 | /// @returns True if Read Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 119 | bool readProtection() const { return ((m_status & readProtectionMask) == readProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 120 | /// Set the Read Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 121 | void setReadProtection(bool readProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 122 | |
IanBenzMaxim | 49:36954b62f503 | 123 | /// Get the Write Protection status. |
IanBenzMaxim | 49:36954b62f503 | 124 | /// @returns True if Write Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 125 | bool writeProtection() const { return ((m_status & writeProtectionMask) == writeProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 126 | /// Set the Write Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 127 | void setWriteProtection(bool writeProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 128 | |
IanBenzMaxim | 49:36954b62f503 | 129 | /// Get the EEPROM Emulation Mode status. |
IanBenzMaxim | 49:36954b62f503 | 130 | /// @returns True if EEPROM Emulation Mode is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 131 | bool eepromEmulation() const { return ((m_status & eepromEmulationMask) == eepromEmulationMask); } |
IanBenzMaxim | 49:36954b62f503 | 132 | /// Set the EEPROM Emulation Mode status. |
IanBenzMaxim | 34:11fffbe98ef9 | 133 | void setEepromEmulation(bool eepromEmulation); |
IanBenzMaxim | 34:11fffbe98ef9 | 134 | |
IanBenzMaxim | 49:36954b62f503 | 135 | /// Get the Authentication Protection status. |
IanBenzMaxim | 49:36954b62f503 | 136 | /// @returns True if Authentication Protection is enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 137 | bool authProtection() const { return ((m_status & authProtectionMask) == authProtectionMask); } |
IanBenzMaxim | 49:36954b62f503 | 138 | /// Set the Authentication Protection status. |
IanBenzMaxim | 34:11fffbe98ef9 | 139 | void setAuthProtection(bool authProtection); |
IanBenzMaxim | 34:11fffbe98ef9 | 140 | |
IanBenzMaxim | 49:36954b62f503 | 141 | /// Check if no protection options are enabled. |
IanBenzMaxim | 49:36954b62f503 | 142 | /// @returns True if no protection options are enabled. |
IanBenzMaxim | 34:11fffbe98ef9 | 143 | bool noProtection() const; |
IanBenzMaxim | 34:11fffbe98ef9 | 144 | |
IanBenzMaxim | 34:11fffbe98ef9 | 145 | bool operator==(const BlockProtection & rhs) const { return (this->m_status == rhs.m_status); } |
IanBenzMaxim | 34:11fffbe98ef9 | 146 | bool operator!=(const BlockProtection & rhs) const { return !operator==(rhs); } |
IanBenzMaxim | 34:11fffbe98ef9 | 147 | }; |
IanBenzMaxim | 34:11fffbe98ef9 | 148 | |
IanBenzMaxim | 49:36954b62f503 | 149 | /// Compute the MAC for an Authenticated Write to a memory segment. |
IanBenzMaxim | 49:36954b62f503 | 150 | /// @param MacCoproc Coprocessor with Slave Secret to use for the computation. |
IanBenzMaxim | 49:36954b62f503 | 151 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 152 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 153 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 154 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 49:36954b62f503 | 155 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 156 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 157 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 158 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 159 | static ISha256MacCoprocessor::CmdResult computeSegmentWriteMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 160 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 161 | unsigned int segmentNum, |
IanBenzMaxim | 49:36954b62f503 | 162 | const Segment & newData, |
IanBenzMaxim | 49:36954b62f503 | 163 | const Segment & oldData, |
IanBenzMaxim | 49:36954b62f503 | 164 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 165 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 166 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 167 | |
IanBenzMaxim | 49:36954b62f503 | 168 | /// Compute the MAC for an Authenticated Write to a memory protection block. |
IanBenzMaxim | 49:36954b62f503 | 169 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 170 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 49:36954b62f503 | 171 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 49:36954b62f503 | 172 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 173 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 174 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 175 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 176 | static ISha256MacCoprocessor::CmdResult computeProtectionWriteMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 177 | const BlockProtection & newProtection, |
IanBenzMaxim | 49:36954b62f503 | 178 | const BlockProtection & oldProtection, |
IanBenzMaxim | 49:36954b62f503 | 179 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 180 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 181 | Mac & mac); |
IanBenzMaxim | 34:11fffbe98ef9 | 182 | |
IanBenzMaxim | 49:36954b62f503 | 183 | /// Compute the next secret from the existing secret. |
IanBenzMaxim | 49:36954b62f503 | 184 | /// @param MacCoproc Coprocessor with Master Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 185 | /// Slave Secret will be updated with the computation result. |
IanBenzMaxim | 49:36954b62f503 | 186 | /// @param[in] bindingPage Binding data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 187 | /// @param bindingPageNum Number of the page where the binding data is from. |
IanBenzMaxim | 49:36954b62f503 | 188 | /// @param[in] partialSecret Partial secret data from the device scratchpad. |
IanBenzMaxim | 49:36954b62f503 | 189 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 190 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 191 | /// @returns The result code indicated by the coprocessor. |
IanBenzMaxim | 49:36954b62f503 | 192 | static ISha256MacCoprocessor::CmdResult computeNextSecret(ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 193 | const Page & bindingPage, |
IanBenzMaxim | 49:36954b62f503 | 194 | unsigned int bindingPageNum, |
IanBenzMaxim | 49:36954b62f503 | 195 | const Scratchpad & partialSecret, |
IanBenzMaxim | 49:36954b62f503 | 196 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 197 | const ManId & manId); |
IanBenzMaxim | 49:36954b62f503 | 198 | |
IanBenzMaxim | 49:36954b62f503 | 199 | /// Compute a Page MAC for authentication. |
IanBenzMaxim | 49:36954b62f503 | 200 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 201 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 202 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 49:36954b62f503 | 203 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 49:36954b62f503 | 204 | /// @param[in] romId 1-Wire ROM ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 205 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 206 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 207 | static ISha256MacCoprocessor::CmdResult computeAuthMac(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 208 | const Page & pageData, |
IanBenzMaxim | 49:36954b62f503 | 209 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 210 | const Scratchpad & challenge, |
IanBenzMaxim | 49:36954b62f503 | 211 | const RomId & romId, |
IanBenzMaxim | 49:36954b62f503 | 212 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 213 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 214 | |
IanBenzMaxim | 49:36954b62f503 | 215 | /// Compute a Page MAC for authentication using anonymous mode. |
IanBenzMaxim | 49:36954b62f503 | 216 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 217 | /// @param[in] pageData Data from a device memory page. |
IanBenzMaxim | 49:36954b62f503 | 218 | /// @param pageNum Number of the page to use data from. |
IanBenzMaxim | 49:36954b62f503 | 219 | /// @param[in] challenge Random challenge to prevent replay attacks. |
IanBenzMaxim | 49:36954b62f503 | 220 | /// @param[in] manId Manufacturer ID of the device. |
IanBenzMaxim | 49:36954b62f503 | 221 | /// @param[out] mac The computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 222 | static ISha256MacCoprocessor::CmdResult computeAuthMacAnon(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 49:36954b62f503 | 223 | const Page & pageData, |
IanBenzMaxim | 49:36954b62f503 | 224 | unsigned int pageNum, |
IanBenzMaxim | 49:36954b62f503 | 225 | const Scratchpad & challenge, |
IanBenzMaxim | 49:36954b62f503 | 226 | const ManId & manId, |
IanBenzMaxim | 49:36954b62f503 | 227 | Mac & mac); |
IanBenzMaxim | 49:36954b62f503 | 228 | |
IanBenzMaxim | 49:36954b62f503 | 229 | /// Manufacturer ID |
IanBenzMaxim | 33:a4c015046956 | 230 | ManId manId; |
IanBenzMaxim | 25:bdb1c5a53b58 | 231 | |
IanBenzMaxim | 49:36954b62f503 | 232 | /// Enable low voltage timing |
IanBenzMaxim | 34:11fffbe98ef9 | 233 | bool lowVoltage; |
IanBenzMaxim | 25:bdb1c5a53b58 | 234 | |
IanBenzMaxim | 49:36954b62f503 | 235 | /// @param OW_master 1-Wire Master to use for communication with DS28E15/22/25. |
IanBenzMaxim | 49:36954b62f503 | 236 | /// @param lowVoltage Enable low voltage timing. |
IanBenzMaxim | 34:11fffbe98ef9 | 237 | DS28E15_22_25(OneWireMaster & OW_master, bool lowVoltage = false); |
IanBenzMaxim | 25:bdb1c5a53b58 | 238 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 239 | // Const member functions should not affect the state of the memory, block protection, or secret on the DS28Exx. |
IanBenzMaxim | 25:bdb1c5a53b58 | 240 | // Scratchpad on the DS28Exx is considered mutable. |
IanBenzMaxim | 25:bdb1c5a53b58 | 241 | |
IanBenzMaxim | 49:36954b62f503 | 242 | /// Get the number of memory pages from the ROM ID of this device. |
IanBenzMaxim | 34:11fffbe98ef9 | 243 | MemoryPages memoryPages(); |
IanBenzMaxim | 49:36954b62f503 | 244 | |
IanBenzMaxim | 49:36954b62f503 | 245 | /// Get the number of protection blocks from the ROM ID of this device. |
IanBenzMaxim | 34:11fffbe98ef9 | 246 | ProtectionBlocks protectionBlocks(); |
IanBenzMaxim | 25:bdb1c5a53b58 | 247 | |
IanBenzMaxim | 49:36954b62f503 | 248 | /// Perform Load and Lock Secret command on the device. |
IanBenzMaxim | 49:36954b62f503 | 249 | /// @note The secret should already be stored in the scratchpad on the device. |
IanBenzMaxim | 49:36954b62f503 | 250 | /// @param lock Prevent further changes to the secret on the device after loading. |
IanBenzMaxim | 34:11fffbe98ef9 | 251 | CmdResult loadSecret(bool lock); |
IanBenzMaxim | 49:36954b62f503 | 252 | |
IanBenzMaxim | 49:36954b62f503 | 253 | /// Perform Write Scratchpad operation on the device. |
IanBenzMaxim | 49:36954b62f503 | 254 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 255 | /// @param[in] data Data to write to the scratchpad. |
IanBenzMaxim | 34:11fffbe98ef9 | 256 | CmdResult writeScratchpad(const Scratchpad & data) const; |
IanBenzMaxim | 49:36954b62f503 | 257 | |
IanBenzMaxim | 49:36954b62f503 | 258 | /// Perform a Read Scratchpad operation on the device. |
IanBenzMaxim | 49:36954b62f503 | 259 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 260 | /// @param[out] data Buffer to read data from the scratchpad into. |
IanBenzMaxim | 49:36954b62f503 | 261 | CmdResult readScratchpad(Scratchpad & data) const; |
IanBenzMaxim | 49:36954b62f503 | 262 | |
IanBenzMaxim | 49:36954b62f503 | 263 | /// Read memory segment using the Read Memory command on the device. |
IanBenzMaxim | 49:36954b62f503 | 264 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 265 | /// @param pageNum Page number for read operation. |
IanBenzMaxim | 49:36954b62f503 | 266 | /// @param segmentNum Segment number within page for read operation. |
IanBenzMaxim | 49:36954b62f503 | 267 | /// @param[out] data Buffer to read data from the segment into. |
IanBenzMaxim | 50:e967f9befbd0 | 268 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 50:e967f9befbd0 | 269 | /// False to begin a new command. |
IanBenzMaxim | 50:e967f9befbd0 | 270 | CmdResult readSegment(unsigned int pageNum, unsigned int segmentNum, Segment & data, bool continuing = false) const; |
IanBenzMaxim | 49:36954b62f503 | 271 | |
IanBenzMaxim | 49:36954b62f503 | 272 | /// Write memory segment using the Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 273 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 274 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 275 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 276 | /// @param[in] data Data to write to the memory segment. |
IanBenzMaxim | 49:36954b62f503 | 277 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 278 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 279 | CmdResult writeSegment(unsigned int pageNum, unsigned int segmentNum, const Segment & data, bool continuing = false); |
IanBenzMaxim | 49:36954b62f503 | 280 | |
IanBenzMaxim | 49:36954b62f503 | 281 | /// Read memory page using the Read Memory command on the device. |
IanBenzMaxim | 49:36954b62f503 | 282 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 283 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 284 | /// @param[out] rdbuf Buffer to read data from the page into. |
IanBenzMaxim | 49:36954b62f503 | 285 | /// @param continuing True if continuing a previous Read Memory command. |
IanBenzMaxim | 49:36954b62f503 | 286 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 287 | CmdResult readPage(unsigned int pageNum, Page & rdbuf, bool continuing = false) const; |
IanBenzMaxim | 49:36954b62f503 | 288 | |
IanBenzMaxim | 49:36954b62f503 | 289 | /// Perform a Compute and Lock Secret command on the device. |
IanBenzMaxim | 49:36954b62f503 | 290 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 291 | /// @param pageNum Page number to use as the binding data. |
IanBenzMaxim | 49:36954b62f503 | 292 | /// @param lock Prevent further changes to the secret on the device after computing. |
IanBenzMaxim | 34:11fffbe98ef9 | 293 | CmdResult computeSecret(unsigned int pageNum, bool lock); |
IanBenzMaxim | 49:36954b62f503 | 294 | |
IanBenzMaxim | 49:36954b62f503 | 295 | /// Perform a Compute Page MAC command on the device. |
IanBenzMaxim | 49:36954b62f503 | 296 | /// Read back the MAC and verify the CRC16. |
IanBenzMaxim | 49:36954b62f503 | 297 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 298 | /// @param pageNum Page number to use for the computation. |
IanBenzMaxim | 49:36954b62f503 | 299 | /// @param anon True to compute in anonymous mode where ROM ID is not used. |
IanBenzMaxim | 49:36954b62f503 | 300 | /// @param[out] mac The device computed MAC. |
IanBenzMaxim | 49:36954b62f503 | 301 | CmdResult computeReadPageMac(unsigned int pageNum, bool anon, Mac & mac) const; |
IanBenzMaxim | 49:36954b62f503 | 302 | |
IanBenzMaxim | 49:36954b62f503 | 303 | /// Read the status of a memory protection block using the Read Status command. |
IanBenzMaxim | 49:36954b62f503 | 304 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 305 | /// @param blockNum Block number to to read status of. |
IanBenzMaxim | 49:36954b62f503 | 306 | /// @param[out] protection Receives protection status read from device. |
IanBenzMaxim | 34:11fffbe98ef9 | 307 | CmdResult readBlockProtection(unsigned int blockNum, BlockProtection & protection); |
IanBenzMaxim | 49:36954b62f503 | 308 | |
IanBenzMaxim | 50:e967f9befbd0 | 309 | /// @{ |
IanBenzMaxim | 50:e967f9befbd0 | 310 | /// Read the status of all memory protection blocks using the Read Status command. |
IanBenzMaxim | 50:e967f9befbd0 | 311 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 50:e967f9befbd0 | 312 | /// @param[out] protection Receives protection statuses read from device. |
IanBenzMaxim | 51:a65f031e997b | 313 | CmdResult readAllBlockProtection(BlockProtection (&protection)[DS28E15_BLOCKS]) const; |
IanBenzMaxim | 51:a65f031e997b | 314 | CmdResult readAllBlockProtection(BlockProtection (&protection)[DS28E25_BLOCKS]) const; |
IanBenzMaxim | 50:e967f9befbd0 | 315 | /// @} |
IanBenzMaxim | 50:e967f9befbd0 | 316 | |
IanBenzMaxim | 50:e967f9befbd0 | 317 | /// Read the personality bytes using the Read Status command. |
IanBenzMaxim | 50:e967f9befbd0 | 318 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 50:e967f9befbd0 | 319 | /// @param[out] personality Receives personality read from device. |
IanBenzMaxim | 51:a65f031e997b | 320 | CmdResult readPersonality(Personality & personality) const; |
IanBenzMaxim | 50:e967f9befbd0 | 321 | |
IanBenzMaxim | 49:36954b62f503 | 322 | /// Update the status of a memory protection block using the Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 323 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 324 | /// @param[in] Desired protection status for the block. |
IanBenzMaxim | 49:36954b62f503 | 325 | /// It is not possible to disable existing protections. |
IanBenzMaxim | 49:36954b62f503 | 326 | /// @param continuing True to continue a previous Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 327 | /// False to begin a new command. |
IanBenzMaxim | 50:e967f9befbd0 | 328 | CmdResult writeBlockProtection(const BlockProtection & protection); |
IanBenzMaxim | 49:36954b62f503 | 329 | |
IanBenzMaxim | 49:36954b62f503 | 330 | /// Update the status of a memory protection block using the Authenticated Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 331 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 332 | /// @param MacCoproc Coprocessor with Slave Secret to use for the operation. |
IanBenzMaxim | 49:36954b62f503 | 333 | /// @param[in] newProtection New protection status to write. |
IanBenzMaxim | 49:36954b62f503 | 334 | /// @param[in] oldProtection Existing protection status in device. |
IanBenzMaxim | 49:36954b62f503 | 335 | /// @param continuing True to continue a previous Authenticated Write Page Protection command. |
IanBenzMaxim | 49:36954b62f503 | 336 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 337 | CmdResult writeAuthBlockProtection(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 34:11fffbe98ef9 | 338 | const BlockProtection & newProtection, |
IanBenzMaxim | 50:e967f9befbd0 | 339 | const BlockProtection & oldProtection); |
IanBenzMaxim | 49:36954b62f503 | 340 | |
IanBenzMaxim | 49:36954b62f503 | 341 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 342 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 343 | /// @param MacCoproc Coprocessor to use for Write MAC computation. |
IanBenzMaxim | 49:36954b62f503 | 344 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 345 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 346 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 347 | /// @param[in] oldData Existing data contained in the segment. |
IanBenzMaxim | 49:36954b62f503 | 348 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 349 | /// False to begin a new command. |
IanBenzMaxim | 25:bdb1c5a53b58 | 350 | CmdResult writeAuthSegment(const ISha256MacCoprocessor & MacCoproc, |
IanBenzMaxim | 34:11fffbe98ef9 | 351 | unsigned int pageNum, |
IanBenzMaxim | 34:11fffbe98ef9 | 352 | unsigned int segmentNum, |
IanBenzMaxim | 34:11fffbe98ef9 | 353 | const Segment & newData, |
IanBenzMaxim | 34:11fffbe98ef9 | 354 | const Segment & oldData, |
IanBenzMaxim | 34:11fffbe98ef9 | 355 | bool continuing = false); |
IanBenzMaxim | 49:36954b62f503 | 356 | |
IanBenzMaxim | 49:36954b62f503 | 357 | /// Write memory segment with authentication using the Authenticated Write Memory command. |
IanBenzMaxim | 49:36954b62f503 | 358 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 359 | /// @param pageNum Page number for write operation. |
IanBenzMaxim | 49:36954b62f503 | 360 | /// @param segmentNum Segment number within page for write operation. |
IanBenzMaxim | 49:36954b62f503 | 361 | /// @param[in] newData New data to write to the segment. |
IanBenzMaxim | 49:36954b62f503 | 362 | /// @param[in] mac Write MAC computed for this operation. |
IanBenzMaxim | 49:36954b62f503 | 363 | /// @param continuing True to continue writing with the next sequential segment. |
IanBenzMaxim | 49:36954b62f503 | 364 | /// False to begin a new command. |
IanBenzMaxim | 34:11fffbe98ef9 | 365 | CmdResult writeAuthSegmentMac(unsigned int pageNum, |
IanBenzMaxim | 25:bdb1c5a53b58 | 366 | unsigned int segmentNum, |
IanBenzMaxim | 33:a4c015046956 | 367 | const Segment & newData, |
IanBenzMaxim | 33:a4c015046956 | 368 | const Mac & mac, |
IanBenzMaxim | 34:11fffbe98ef9 | 369 | bool continuing = false); |
IanBenzMaxim | 25:bdb1c5a53b58 | 370 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 371 | private: |
IanBenzMaxim | 34:11fffbe98ef9 | 372 | static const unsigned int shaComputationDelayMs = 3; |
IanBenzMaxim | 34:11fffbe98ef9 | 373 | static const unsigned int eepromWriteDelayMs = 10; |
IanBenzMaxim | 34:11fffbe98ef9 | 374 | unsigned int secretEepromWriteDelayMs() const { return (lowVoltage ? 200 : 100); } |
IanBenzMaxim | 34:11fffbe98ef9 | 375 | |
IanBenzMaxim | 25:bdb1c5a53b58 | 376 | OneWireMaster & m_OW_master; |
IanBenzMaxim | 49:36954b62f503 | 377 | |
IanBenzMaxim | 49:36954b62f503 | 378 | /// Read status bytes which are either personality or block protection. |
IanBenzMaxim | 49:36954b62f503 | 379 | /// @note 1-Wire ROM selection should have already occurred. |
IanBenzMaxim | 49:36954b62f503 | 380 | /// @param personality True to read personality or false to read block protection. |
IanBenzMaxim | 49:36954b62f503 | 381 | /// @param allpages True to read all pages or false to read one page. |
IanBenzMaxim | 49:36954b62f503 | 382 | /// @param pageNum Page number if reading block protection. |
IanBenzMaxim | 49:36954b62f503 | 383 | /// @param rdbuf Buffer to receive data read from device. |
IanBenzMaxim | 50:e967f9befbd0 | 384 | CmdResult readStatus(bool personality, bool allpages, unsigned int blockNum, std::uint8_t * rdbuf) const; |
IanBenzMaxim | 50:e967f9befbd0 | 385 | |
IanBenzMaxim | 51:a65f031e997b | 386 | /// Extends readStatus() to use the BlockProtection wrapper. |
IanBenzMaxim | 51:a65f031e997b | 387 | /// @see readAllBlockProtection(BlockProtection (&)[DS28E15_BLOCKS]) |
IanBenzMaxim | 51:a65f031e997b | 388 | template <ProtectionBlocks blocks> CmdResult readAllBlockProtection(BlockProtection (&protection)[blocks]) const; |
IanBenzMaxim | 25:bdb1c5a53b58 | 389 | }; |
IanBenzMaxim | 25:bdb1c5a53b58 | 390 | |
IanBenzMaxim | 49:36954b62f503 | 391 | #endif |