Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

Revision:
8:5ea891c7d1a1
Parent:
7:9cd16581b578
--- a/MaximInterfaceDevices/DS28C39.hpp	Mon Jul 22 11:44:07 2019 -0500
+++ b/MaximInterfaceDevices/DS28C39.hpp	Mon Sep 16 11:13:37 2019 -0500
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
+* Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
@@ -30,8 +30,8 @@
 * ownership rights.
 *******************************************************************************/
 
-#ifndef MaximInterfaceDevices_DS28C39
-#define MaximInterfaceDevices_DS28C39
+#ifndef MaximInterfaceDevices_DS28C39_hpp
+#define MaximInterfaceDevices_DS28C39_hpp
 
 #include <stdint.h>
 #include <MaximInterfaceCore/Algorithm.hpp>
@@ -96,13 +96,15 @@
       EntropyNotHealthy = 0xDD
     };
 
-    typedef Core::array<PageProtection, memoryPages - 2> PageProtectionList;
-    typedef Core::array<uint_least8_t, 2> RomVersion;
+    typedef Core::array_span<PageProtection, memoryPages - 2>
+        PageProtectionList;
 
-    PageProtectionList pageProtection;
+    typedef Core::array_span<uint_least8_t, 2> RomVersion;
+
+    PageProtectionList::array pageProtection;
     Core::RomId::array romId;
     Core::ManId::array manId;
-    RomVersion romVersion;
+    RomVersion::array romVersion;
     EntropyHealthTestStatus entropyHealthTestStatus;
   };
 
@@ -116,51 +118,50 @@
   /// @brief Write memory with no protection.
   /// @param pageNum Number of page to write.
   /// @param page Data to write.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   writeMemory(int pageNum, Page::const_span page);
 
   /// @brief Read memory with no protection.
   /// @param pageNum Number of page to read.
-  /// @param[out] page Data that was read.
-  MaximInterfaceDevices_EXPORT Core::error_code readMemory(int pageNum,
-                                                           Page::span page);
+  /// @returns Data that was read.
+  MaximInterfaceDevices_EXPORT Core::Result<Page::array>
+  readMemory(int pageNum) const;
 
   /// @brief
   /// Reads the current status of the device and optionally performs an
   /// entropy health test.
   /// @param entropyHealthTest True to perform an entropy health test.
-  /// @param[out] status Status that was read.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  readStatus(bool entropyHealthTest, Status & status);
+  /// @returns Status that was read.
+  MaximInterfaceDevices_EXPORT Core::Result<Status>
+  readStatus(bool entropyHealthTest) const;
 
   /// @brief Set the protection settings of a page.
   /// @param pageNum Number of page to write.
   /// @param protection Protection to write.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   setPageProtection(int pageNum, const PageProtection & protection);
 
   /// @brief Compute and read page authentication with ECDSA.
   /// @param pageNum Number of page to authenticate.
   /// @param anonymous True to disable use of ROM ID in computation.
   /// @param challenge Random challenge used to prevent replay attacks.
-  /// @param[out] signature Computed page signature.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  /// @returns Computed page signature.
+  MaximInterfaceDevices_EXPORT Core::Result<Core::Ecc256::Signature::array>
   computeAndReadPageAuthentication(int pageNum, bool anonymous,
-                                   Page::const_span challenge,
-                                   Core::Ecc256::Signature::span signature);
+                                   Page::const_span challenge) const;
 
   /// Permanently disable the device.
-  MaximInterfaceDevices_EXPORT Core::error_code disableDevice();
+  MaximInterfaceDevices_EXPORT Core::Result<void> disableDevice();
 
   /// @brief Generate the device's ECDSA public key from the PUF private key.
-  /// @param[out] devicePublicKey Device Public Key
-  MaximInterfaceDevices_EXPORT Core::error_code
-  readDevicePublicKey(Core::Ecc256::PublicKey::span devicePublicKey);
+  /// @returns Device Public Key
+  MaximInterfaceDevices_EXPORT Core::Result<Core::Ecc256::PublicKey::array>
+  readDevicePublicKey() const;
 
   /// @brief Read a block of random data from the RNG.
   /// @param[out] data Random data from RNG with length from 1 to 64.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  readRng(Core::span<uint_least8_t> data);
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  readRng(Core::span<uint_least8_t> data) const;
 
   /// @brief
   /// Authenticate a public key for authenticated writes using the
@@ -169,7 +170,7 @@
   /// Certificate to use for authentication of the Write Public Key.
   /// @param customization
   /// Certificate customization with length from 1 to 32.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   authenticatePublicKey(Core::Ecc256::Signature::const_span certificate,
                         Core::span<const uint_least8_t> customization);
 
@@ -177,7 +178,7 @@
   /// @param pageNum Number of page to write.
   /// @param page Data to write.
   /// @param signature Signature to use for authentication of page data.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   authenticatedWriteMemory(int pageNum, Page::const_span page,
                            Core::Ecc256::Signature::const_span signature);
 
@@ -185,29 +186,31 @@
   errorCategory();
 
 protected:
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<Core::span<uint_least8_t> >
   runCommand(Core::span<const uint_least8_t> request, int delayTime,
-             Core::span<uint_least8_t> & response);
+             Core::span<uint_least8_t> response) const;
 
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   runCommand(Core::span<const uint_least8_t> request, int delayTime);
 
 private:
   Core::RunCommand doRunCommand;
 };
 
+} // namespace MaximInterfaceDevices
+namespace MaximInterfaceCore {
+
+template <>
+struct is_error_code_enum<MaximInterfaceDevices::DS28C39::ErrorValue>
+    : true_type {};
+
+} // namespace MaximInterfaceCore
+namespace MaximInterfaceDevices {
+
 inline Core::error_code make_error_code(DS28C39::ErrorValue e) {
   return Core::error_code(e, DS28C39::errorCategory());
 }
 
-/// @brief Read the device ROM ID and MAN ID using the Read Status command.
-/// @param ds28c39 Device to read.
-/// @param[out] romId Read ROM ID valid when operation is successful.
-/// @param[out] manId Read MAN ID valid when operation is successful.
-MaximInterfaceDevices_EXPORT Core::error_code
-readRomIdAndManId(DS28C39 & ds28c39, Core::RomId::span romId,
-                  Core::ManId::span manId);
-
 /// Format page authentication input data.
 class DS28C39::PageAuthenticationData {
 public:
@@ -327,13 +330,11 @@
   /// @}
 
 private:
-  typedef Result::span::index_type index;
-
-  static const index romIdIdx = 0;
-  static const index pageIdx = romIdIdx + Core::RomId::size;
-  static const index challengeIdx = pageIdx + Page::size;
-  static const index pageNumIdx = challengeIdx + Page::size;
-  static const index manIdIdx = pageNumIdx + 1;
+  static const size_t romIdIdx = 0;
+  static const size_t pageIdx = romIdIdx + Core::RomId::size;
+  static const size_t challengeIdx = pageIdx + Page::size;
+  static const size_t pageNumIdx = challengeIdx + Page::size;
+  static const size_t manIdIdx = pageNumIdx + 1;
 
   Result::array result_;
 };