Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

OneWire_Memory/Authenticators/DS28E15_22_25/DS28E15_22_25.hpp

Committer:
IanBenzMaxim
Date:
2016-04-01
Revision:
34:11fffbe98ef9
Parent:
33:a4c015046956
Child:
49:36954b62f503

File content as of revision 34:11fffbe98ef9:

//------------Copyright (C) 2013 Maxim Integrated Products --------------
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL MAXIM INTEGRATED PRODCUTS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Maxim Integrated Products
// shall not be used except as stated in the Maxim Integrated Products
// Branding Policy.
// ---------------------------------------------------------------------------

#ifndef DS28E15_22_25_H
#define DS28E15_22_25_H

#include "array.hpp"
#include "OneWire_Masters/ISha256MacCoprocessor.hpp"
#include "OneWireSlave.hpp"

class OneWireMaster;

class DS28E15_22_25 : public OneWireSlave
{
public:
  enum FamilyCode
  {
    DS28E25_FAMILY = 0x47,
    DS28E22_FAMILY = 0x48,
    DS28E15_FAMILY = 0x17,
    UNKNOWN_FAMILY = 0
  };

  enum MemoryPages
  {
    DS28E25_PAGES = 16,
    DS28E22_PAGES = 8,
    DS28E15_PAGES = 2,
    UNKNOWN_PAGES = 0
  };
  
  enum ProtectionBlocks
  {
    DS28E25_BLOCKS = 8,
    DS28E22_BLOCKS = 4,
    DS28E15_BLOCKS = 4,
    UNKNOWN_BLOCKS = 0
  };
  
  typedef array<std::uint8_t, 32> Page;
  typedef array<std::uint8_t, 32> Scratchpad;
  typedef array<std::uint8_t, 32> Mac;
  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;
  
  // Low voltage
  bool lowVoltage;
  
  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.

  MemoryPages memoryPages();
  ProtectionBlocks protectionBlocks();
  
  //DS28Exx Specific Functions (DS28E15, DS28E22 & DS28E25)
  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 = 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 = false);
  CmdResult writeAuthSegmentMac(unsigned int pageNum,
                                unsigned int segmentNum,
                                const Segment & newData,
                                const Mac & mac, 
                                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, std::uint8_t * rdbuf) const;
};

#endif