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-03-31
Revision:
33:a4c015046956
Parent:
27:d5aaefa252f1
Child:
34:11fffbe98ef9

File content as of revision 33:a4c015046956:

//------------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 DeviceFamily
  {
    DS28E25_FAMILY = 0x47,
    DS28E22_FAMILY = 0x48,
    DS28E15_FAMILY = 0x17,
    UNKNOWN_FAMILY = 0
  };

  enum DevicePages
  {
    DS28E25_PAGES = 16,
    DS28E22_PAGES = 8,
    DS28E15_PAGES = 2,
    UNKNOWN_PAGES = 0
  };
  
  enum DeviceBlocks
  {
    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;
  
  // 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);
  
  DS28E15_22_25(OneWireMaster & OW_master);
  
  // 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();
  
  //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);
  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 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 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]);
  
private: 
  OneWireMaster & m_OW_master;
  
  CmdResult ReadStatus(bool personality, bool allpages, unsigned int pageNum, unsigned char *rdbuf) const;
};

#endif