Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Revision:
6:a8c83a2e6fa4
Parent:
0:f77ad7f72d04
Child:
7:471901a04573
--- a/Devices/DS2465.hpp	Fri Jan 19 10:25:02 2018 -0600
+++ b/Devices/DS2465.hpp	Wed Jan 23 13:11:04 2019 -0600
@@ -36,6 +36,7 @@
 #include <MaximInterface/Links/I2CMaster.hpp>
 #include <MaximInterface/Links/OneWireMaster.hpp>
 #include <MaximInterface/Links/Sleep.hpp>
+#include <MaximInterface/Utilities/array_span.hpp>
 #include <MaximInterface/Utilities/Export.h>
 #include <MaximInterface/Utilities/Sha256.hpp>
 
@@ -64,13 +65,13 @@
   enum PageRegion { FullPage = 0x03, FirstHalf = 0x01, SecondHalf = 0x02 };
 
   /// Holds the contents of a device memory segment.
-  typedef array<uint_least8_t, 4> Segment;
+  typedef array_span<uint_least8_t, 4> Segment;
 
   /// Holds the contents of a device memory page.
-  typedef array<uint_least8_t, 32> Page;
+  typedef array_span<uint_least8_t, 32> Page;
 
   static const int memoryPages = 2;
-  static const int segmentsPerPage = Page::csize / Segment::csize;
+  static const int segmentsPerPage = Page::size / Segment::size;
 
   /// Represents a DS2465 configuration.
   class Config {
@@ -82,48 +83,56 @@
     /// @{
     /// 1-Wire Speed
     bool get1WS() const { return (readByte_ & option1WS) == option1WS; }
-    void set1WS(bool new1WS) {
+    
+    Config & set1WS(bool new1WS) {
       if (new1WS) {
         readByte_ |= option1WS;
       } else {
         readByte_ &= ~option1WS;
       }
+      return *this;
     }
     /// @}
 
     /// @{
     /// Strong Pullup
     bool getSPU() const { return (readByte_ & optionSPU) == optionSPU; }
-    void setSPU(bool newSPU) {
+    
+    Config & setSPU(bool newSPU) {
       if (newSPU) {
         readByte_ |= optionSPU;
       } else {
         readByte_ &= ~optionSPU;
       }
+      return *this;
     }
     /// @}
 
     /// @{
     /// 1-Wire Power Down
     bool getPDN() const { return (readByte_ & optionPDN) == optionPDN; }
-    void setPDN(bool newPDN) {
+    
+    Config & setPDN(bool newPDN) {
       if (newPDN) {
         readByte_ |= optionPDN;
       } else {
         readByte_ &= ~optionPDN;
       }
+      return *this;
     }
     /// @}
 
     /// @{
     /// Active Pullup
     bool getAPU() const { return (readByte_ & optionAPU) == optionAPU; }
-    void setAPU(bool newAPU) {
+    
+    Config & setAPU(bool newAPU) {
       if (newAPU) {
         readByte_ |= optionAPU;
       } else {
         readByte_ &= ~optionAPU;
       }
+      return *this;
     }
     /// @}
 
@@ -143,16 +152,16 @@
   // affect the state of the 1-Wire bus. Read pointer, scratchpad, MAC output
   // register, and command register on the DS2465 are considered mutable.
 
-  DS2465(const Sleep & sleep, I2CMaster & i2cMaster,
-         uint_least8_t i2cAddress = 0x30)
-      : sleep(&sleep), i2cMaster(&i2cMaster), i2cAddress_(i2cAddress & 0xFE) {}
+  DS2465(Sleep & sleep, I2CMaster & master, uint_least8_t address = 0x30)
+      : sleep(&sleep), master(&master), address_(address & 0xFE) {}
 
-  void setSleep(const Sleep & sleep) { this->sleep = &sleep; }
-  void setI2CMaster(I2CMaster & i2cMaster) { this->i2cMaster = &i2cMaster; }
-  uint_least8_t i2cAddress() const { return i2cAddress_; }
-  void setI2CAddress(uint_least8_t i2cAddress) {
-    this->i2cAddress_ = (i2cAddress & 0xFE);
-  }
+  void setSleep(Sleep & sleep) { this->sleep = &sleep; }
+  
+  void setMaster(I2CMaster & master) { this->master = &master; }
+  
+  uint_least8_t address() const { return address_; }
+  
+  void setAddress(uint_least8_t address) { address_ = address & 0xFE; }
 
   /// Initialize hardware for use.
   MaximInterface_EXPORT error_code initialize(Config config = Config());
@@ -171,64 +180,74 @@
   // 1-Wire Master Commands
 
   MaximInterface_EXPORT virtual error_code reset();
+  
   MaximInterface_EXPORT virtual error_code touchBitSetLevel(bool & sendRecvBit,
                                                             Level afterLevel);
+  
   MaximInterface_EXPORT virtual error_code
   readByteSetLevel(uint_least8_t & recvByte, Level afterLevel);
+  
   MaximInterface_EXPORT virtual error_code
   writeByteSetLevel(uint_least8_t sendByte, Level afterLevel);
-  MaximInterface_EXPORT virtual error_code readBlock(uint_least8_t * recvBuf,
-                                                     size_t recvLen);
+  
+  MaximInterface_EXPORT virtual error_code
+  readBlock(span<uint_least8_t> recvBuf);
+  
   MaximInterface_EXPORT virtual error_code
-  writeBlock(const uint_least8_t * sendBuf, size_t sendLen);
+  writeBlock(span<const uint_least8_t> sendBuf);
+  
   MaximInterface_EXPORT virtual error_code setSpeed(Speed newSpeed);
+  
   /// @note The DS2465 only supports enabling strong pullup following a 1-Wire
   /// read or write operation.
   MaximInterface_EXPORT virtual error_code setLevel(Level newLevel);
+  
   MaximInterface_EXPORT virtual error_code triplet(TripletData & data);
 
   // DS2465 Coprocessor Commands
-  
+
   /// Read data from an EEPROM memory page.
   /// @param pageNum Page number to read from.
-  MaximInterface_EXPORT error_code readPage(int pageNum, Page & data) const;
+  MaximInterface_EXPORT error_code readPage(int pageNum, Page::span data) const;
 
   /// Write data to an EEPROM memory page.
   /// @param pageNum Page number to copy to.
-  MaximInterface_EXPORT error_code writePage(int pageNum, const Page & data);
+  MaximInterface_EXPORT error_code writePage(int pageNum,
+                                             Page::const_span data);
 
   /// Write data to an EEPROM memory segment.
   /// @param pageNum Page number to copy to.
   /// @param segmentNum Segment number to copy to.
   MaximInterface_EXPORT error_code writeSegment(int pageNum, int segmentNum,
-                                                const Segment & data);
+                                                Segment::const_span data);
 
   /// Write data to the secret EEPROM memory page.
   MaximInterface_EXPORT error_code
-  writeMasterSecret(const Sha256::Hash & masterSecret);
+  writeMasterSecret(Sha256::Hash::const_span masterSecret);
 
   /// Compute Next Master Secret.
   /// @param data Combined data fields for computation.
   MaximInterface_EXPORT error_code
-  computeNextMasterSecret(const Sha256::SlaveSecretData & data);
+  computeNextMasterSecret(Sha256::AuthenticationData::const_span data);
 
   /// Compute Next Master Secret with page swapping.
   /// @param data Combined data fields for computation.
   /// @param pageNum Page number to swap in.
   /// @param region Region of the page to swap in.
-  MaximInterface_EXPORT error_code computeNextMasterSecretWithSwap(
-      const Sha256::SlaveSecretData & data, int pageNum, PageRegion region);
+  MaximInterface_EXPORT error_code
+  computeNextMasterSecretWithSwap(Sha256::AuthenticationData::const_span data,
+                                  int pageNum, PageRegion region);
 
   /// Compute Write MAC.
   /// @param data Combined data fields for computation.
   /// @param[out] mac Computed Write MAC.
-  MaximInterface_EXPORT error_code
-  computeWriteMac(const Sha256::WriteMacData & data, Sha256::Hash & mac) const;
+  MaximInterface_EXPORT error_code computeWriteMac(
+      Sha256::WriteMacData::const_span data, Sha256::Hash::span mac) const;
 
   /// Compute Write MAC.
   /// @param data Combined data fields for computation.
   MaximInterface_EXPORT error_code
-  computeAndTransmitWriteMac(const Sha256::WriteMacData & data) const;
+  computeAndTransmitWriteMac(Sha256::WriteMacData::const_span data) const;
 
   /// Compute Write MAC with page swapping.
   /// @param data Combined data fields for computation.
@@ -236,61 +255,64 @@
   /// @param segmentNum Segment number to swap in.
   /// @param[out] mac Computed Write MAC.
   MaximInterface_EXPORT error_code
-  computeWriteMacWithSwap(const Sha256::WriteMacData & data, int pageNum,
-                          int segmentNum, Sha256::Hash & mac) const;
+  computeWriteMacWithSwap(Sha256::WriteMacData::const_span data, int pageNum,
+                          int segmentNum, Sha256::Hash::span mac) const;
 
   /// Compute Write MAC with page swapping.
   /// @param data Combined data fields for computation.
   /// @param pageNum Page number to swap in.
   /// @param segmentNum Segment number to swap in.
   MaximInterface_EXPORT error_code computeAndTransmitWriteMacWithSwap(
-      const Sha256::WriteMacData & data, int pageNum, int segmentNum) const;
+      Sha256::WriteMacData::const_span data, int pageNum, int segmentNum) const;
 
   /// Compute Slave Secret (S-Secret).
   /// @param data Combined data fields for computation.
   MaximInterface_EXPORT error_code
-  computeSlaveSecret(const Sha256::SlaveSecretData & data);
+  computeSlaveSecret(Sha256::AuthenticationData::const_span data);
 
   /// Compute Slave Secret (S-Secret) with page swapping.
   /// @param data Combined data fields for computation.
   /// @param pageNum Page number to swap in.
   /// @param region Region of the page to swap in.
-  MaximInterface_EXPORT error_code computeSlaveSecretWithSwap(
-      const Sha256::SlaveSecretData & data, int pageNum, PageRegion region);
+  MaximInterface_EXPORT error_code
+  computeSlaveSecretWithSwap(Sha256::AuthenticationData::const_span data,
+                             int pageNum, PageRegion region);
 
   /// Compute Authentication MAC.
   /// @param data Combined data fields for computation.
   /// @param[out] mac Computed Auth MAC.
   MaximInterface_EXPORT error_code
-  computeAuthMac(const Sha256::AuthMacData & data, Sha256::Hash & mac) const;
+  computeAuthMac(Sha256::AuthenticationData::const_span data,
+                 Sha256::Hash::span mac) const;
 
   /// Compute Authentication MAC.
   /// @param data Combined data fields for computation.
   MaximInterface_EXPORT error_code
-  computeAndTransmitAuthMac(const Sha256::AuthMacData & data) const;
+  computeAndTransmitAuthMac(Sha256::AuthenticationData::const_span data) const;
 
   /// Compute Authentication MAC with page swapping.
   /// @param data Combined data fields for computation.
   /// @param pageNum Page number to swap in.
   /// @param region Region of the page to swap in.
   /// @param[out] mac Computed Auth MAC.
-  MaximInterface_EXPORT error_code
-  computeAuthMacWithSwap(const Sha256::AuthMacData & data, int pageNum,
-                         PageRegion region, Sha256::Hash & mac) const;
+  MaximInterface_EXPORT error_code computeAuthMacWithSwap(
+      Sha256::AuthenticationData::const_span data, int pageNum,
+      PageRegion region, Sha256::Hash::span mac) const;
 
   /// Compute Authentication MAC with page swapping.
   /// @param data Combined data fields for computation.
   /// @param pageNum Page number to swap in.
   /// @param region Region of the page to swap in.
-  MaximInterface_EXPORT error_code computeAndTransmitAuthMacWithSwap(
-      const Sha256::AuthMacData & data, int pageNum, PageRegion region) const;
+  MaximInterface_EXPORT error_code
+  computeAndTransmitAuthMacWithSwap(Sha256::AuthenticationData::const_span data,
+                                    int pageNum, PageRegion region) const;
 
   MaximInterface_EXPORT static const error_category & errorCategory();
 
 private:
   const Sleep * sleep;
-  I2CMaster * i2cMaster;
-  uint_least8_t i2cAddress_;
+  I2CMaster * master;
+  uint_least8_t address_;
   Config curConfig;
 
   /// Performs a soft reset on the DS2465.
@@ -307,32 +329,29 @@
   error_code configureLevel(Level level);
 
   /// Const since only for internal use.
-  error_code writeMemory(uint_least8_t addr, const uint_least8_t * buf,
-                         size_t bufLen) const;
+  error_code writeMemory(uint_least8_t addr,
+                         span<const uint_least8_t> buf) const;
 
   /// Read memory from the DS2465.
   /// @param addr Address to begin reading from.
   /// @param[out] buf Buffer to hold read data.
-  /// @param bufLen Length of buffer, buf, and number of bytes to read.
-  error_code readMemory(uint_least8_t addr, uint_least8_t * buf,
-                        size_t bufLen) const;
+  error_code readMemory(uint_least8_t addr, span<uint_least8_t> buf) const;
 
   /// Read memory from the DS2465 at the current pointer.
   /// @param[out] buf Buffer to hold read data.
-  /// @param bufLen Length of buffer, buf, and number of bytes to read.
-  error_code readMemory(uint_least8_t * buf, size_t bufLen) const;
+  error_code readMemory(span<uint_least8_t> buf) const;
 
   /// Write the last computed MAC to the 1-Wire bus.
   error_code writeMacBlock() const;
 
-  error_code computeWriteMac(const Sha256::WriteMacData & data) const;
+  error_code computeWriteMac(Sha256::WriteMacData::const_span data) const;
 
-  error_code computeWriteMacWithSwap(const Sha256::WriteMacData & data,
+  error_code computeWriteMacWithSwap(Sha256::WriteMacData::const_span data,
                                      int pageNum, int segmentNum) const;
 
-  error_code computeAuthMac(const Sha256::AuthMacData & data) const;
+  error_code computeAuthMac(Sha256::AuthenticationData::const_span data) const;
 
-  error_code computeAuthMacWithSwap(const Sha256::AuthMacData & data,
+  error_code computeAuthMacWithSwap(Sha256::AuthenticationData::const_span data,
                                     int pageNum, PageRegion region) const;
 
   // Legacy implementations