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
Child:
11:3f3bf6bf5e6c
diff -r 9cd16581b578 -r 5ea891c7d1a1 MaximInterfaceDevices/DS28E16.cpp
--- a/MaximInterfaceDevices/DS28E16.cpp	Mon Jul 22 11:44:07 2019 -0500
+++ b/MaximInterfaceDevices/DS28E16.cpp	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"),
@@ -50,9 +50,9 @@
 const int DS28E16::masterSecretPage;
 const int DS28E16::memoryPages;
 
-error_code DS28E16::writeMemory(int pageNum, Page::const_span page) {
+Result<void> DS28E16::writeMemory(int pageNum, Page::const_span page) {
   if (pageNum < 0 || pageNum >= memoryPages) {
-    return make_error_code(InvalidParameterError);
+    return InvalidParameterError;
   }
 
   uint_least8_t request[2 + Page::size];
@@ -62,49 +62,51 @@
   return runCommand(request, writeMemoryTimeMs);
 }
 
-error_code DS28E16::readMemory(int pageNum, Page::span page) {
+Result<DS28E16::Page::array> DS28E16::readMemory(int pageNum) const {
   if (pageNum < 0 || pageNum >= memoryPages) {
-    return make_error_code(InvalidParameterError);
+    return InvalidParameterError;
   }
 
   uint_least8_t buffer[1 + Page::size * 2];
   buffer[0] = 0x44;
   buffer[1] = pageNum;
-  span<uint_least8_t> response(buffer);
-  const error_code result =
-      runCommand(make_span(buffer, 2), readMemoryTimeMs, response);
-  if (!result) {
-    response = response.first(Page::size);
-    copy(response.begin(), response.end(), page.begin());
+  Result<span<uint_least8_t> > response =
+      runCommand(make_span(buffer, 2), readMemoryTimeMs, buffer);
+  if (!response) {
+    return response.error();
   }
-  return result;
+  Page::array page;
+  response.value() = response.value().first(Page::size);
+  copy(response.value().begin(), response.value().end(), page.begin());
+  return page;
 }
 
-error_code DS28E16::readStatus(Status & status) {
-  uint_least8_t buffer[1 + Status::PageProtectionList::csize + 2];
+Result<DS28E16::Status> DS28E16::readStatus() const {
+  uint_least8_t buffer[1 + Status::PageProtectionList::size + 2];
   buffer[0] = 0xAA;
-  span<uint_least8_t> response(buffer);
-  const error_code result =
-      runCommand(make_span(buffer, 1), readMemoryTimeMs, response);
-  if (!result) {
-    span<uint_least8_t>::const_iterator responseIt = response.begin();
-    for (Status::PageProtectionList::iterator it =
-             status.pageProtection.begin();
-         it != status.pageProtection.end(); ++it) {
-      *it = *responseIt;
-      ++responseIt;
-    }
-    status.manId = *responseIt;
+  const Result<span<uint_least8_t> > response =
+      runCommand(make_span(buffer, 1), readMemoryTimeMs, buffer);
+  if (!response) {
+    return response.error();
+  }
+  Status status;
+  span<uint_least8_t>::const_iterator responseIt = response.value().begin();
+  for (Status::PageProtectionList::array::iterator it =
+           status.pageProtection.begin();
+       it != status.pageProtection.end(); ++it) {
+    *it = *responseIt;
     ++responseIt;
-    status.deviceVersion = *responseIt;
   }
-  return result;
+  status.manId = *responseIt;
+  ++responseIt;
+  status.deviceVersion = *responseIt;
+  return status;
 }
 
-error_code DS28E16::setPageProtection(int pageNum,
-                                      const PageProtection & protection) {
+Result<void> DS28E16::setPageProtection(int pageNum,
+                                        const PageProtection & protection) {
   if (pageNum < 0 || pageNum >= memoryPages) {
-    return make_error_code(InvalidParameterError);
+    return InvalidParameterError;
   }
 
   const uint_least8_t request[] = {
@@ -113,12 +115,10 @@
   return runCommand(request, shortWriteMemoryTimeMs);
 }
 
-error_code
-DS28E16::computeAndReadPageAuthentication(int pageNum, bool anonymous,
-                                          DoublePage::const_span challenge,
-                                          DoublePage::span hmac) {
+Result<DS28E16::DoublePage::array> DS28E16::computeAndReadPageAuthentication(
+    int pageNum, bool anonymous, DoublePage::const_span challenge) const {
   if (pageNum < 0 || pageNum >= memoryPages) {
-    return make_error_code(InvalidParameterError);
+    return InvalidParameterError;
   }
 
   const size_t requestSize = 3 + DoublePage::size;
@@ -131,20 +131,22 @@
   }
   buffer[2] = 0x02;
   copy(challenge.begin(), challenge.end(), buffer + 3);
-  span<uint_least8_t> response(buffer, responseSize);
-  const error_code result =
-      runCommand(make_span(buffer, requestSize), computationTimeMs, response);
-  if (!result) {
-    copy(response.begin(), response.end(), hmac.begin());
+  const Result<span<uint_least8_t> > response =
+      runCommand(make_span(buffer, requestSize), computationTimeMs,
+                 make_span(buffer, responseSize));
+  if (!response) {
+    return response.error();
   }
-  return result;
+  DoublePage::array hmac;
+  copy(response.value().begin(), response.value().end(), hmac.begin());
+  return hmac;
 }
 
-error_code DS28E16::computeSecret(int bindingDataPageNum,
-                                  bool constantBindingData, bool anonymous,
-                                  DoublePage::const_span partialSecret) {
+Result<void> DS28E16::computeSecret(int bindingDataPageNum,
+                                    bool constantBindingData, bool anonymous,
+                                    DoublePage::const_span partialSecret) {
   if (bindingDataPageNum < 0 || bindingDataPageNum >= memoryPages) {
-    return make_error_code(InvalidParameterError);
+    return InvalidParameterError;
   }
 
   uint_least8_t request[3 + DoublePage::size];
@@ -161,27 +163,29 @@
   return runCommand(request, computationTimeMs);
 }
 
-error_code DS28E16::decrementCounter() {
+Result<void> DS28E16::decrementCounter() {
   const uint_least8_t request = 0xC9;
   return runCommand(make_span(&request, 1), writeMemoryTimeMs);
 }
 
-error_code DS28E16::lockOutDisableDevice() {
-  const DisableDevicePassword::array password = {};
+Result<void> DS28E16::lockOutDisableDevice() {
+  const DisableDevicePassword::array password = {0};
   return disableDevice(LockOutDisableDevice, password);
 }
 
-error_code
+Result<void>
 DS28E16::setDisableDevicePassword(DisableDevicePassword::const_span password) {
   return disableDevice(SetDisableDevicePassword, password);
 }
 
-error_code DS28E16::disableDevice(DisableDevicePassword::const_span password) {
+Result<void>
+DS28E16::disableDevice(DisableDevicePassword::const_span password) {
   return disableDevice(DisableDevice, password);
 }
 
-error_code DS28E16::disableDevice(DisableDeviceOperation operation,
-                                  DisableDevicePassword::const_span password) {
+Result<void>
+DS28E16::disableDevice(DisableDeviceOperation operation,
+                       DisableDevicePassword::const_span password) {
   const uint_least8_t request[] = {
       0x33,        static_cast<uint_least8_t>(operation),
       password[0], password[1],
@@ -191,43 +195,40 @@
   return runCommand(request, shortWriteMemoryTimeMs);
 }
 
-error_code DS28E16::runCommand(span<const uint_least8_t> request, int delayTime,
-                               span<uint_least8_t> & response) {
-  const span<const uint_least8_t>::index_type responseInputSize =
-      response.size();
-  error_code result = doRunCommand(request, delayTime, response);
-  if (result) {
-    return result;
+Result<span<uint_least8_t> >
+DS28E16::runCommand(span<const uint_least8_t> request, int delayTime,
+                    span<uint_least8_t> response) const {
+  const Result<span<uint_least8_t> > responseOutput =
+      doRunCommand(request, delayTime, response);
+  if (!responseOutput) {
+    return responseOutput;
   }
-  if (response.empty()) {
-    return make_error_code(InvalidResponseError);
+  if (responseOutput.value().empty()) {
+    return InvalidResponseError;
   }
   // Parse command result byte.
-  switch (response[0]) {
+  switch (responseOutput.value().front()) {
   case 0xAA:
     // Success response.
-    if (response.size() != responseInputSize) {
-      result = make_error_code(InvalidResponseError);
-    }
     break;
 
   case 0x00:
-    result = make_error_code(AuthenticationError);
-    break;
+    return AuthenticationError;
 
   default:
-    result.assign(response[0], errorCategory());
-    break;
+    return error_code(responseOutput.value().front(), errorCategory());
   }
-  response = response.subspan(1);
-  return result;
+  if (responseOutput.value().size() != response.size()) {
+    return InvalidResponseError;
+  }
+  return responseOutput.value().subspan(1);
 }
 
-error_code DS28E16::runCommand(span<const uint_least8_t> request,
-                               int delayTime) {
+Result<void> DS28E16::runCommand(span<const uint_least8_t> request,
+                                 int delayTime) {
   uint_least8_t buffer;
-  span<uint_least8_t> response(&buffer, 1);
-  return runCommand(request, delayTime, response);
+  MaximInterfaceCore_TRY(runCommand(request, delayTime, make_span(&buffer, 1)));
+  return none;
 }
 
 const error_category & DS28E16::errorCategory() {
@@ -264,15 +265,6 @@
   return instance;
 }
 
-error_code readManId(DS28E16 & device, uint_least8_t & manId) {
-  DS28E16::Status status;
-  const error_code result = device.readStatus(status);
-  if (!result) {
-    manId = status.manId;
-  }
-  return result;
-}
-
 DS28E16::PageAuthenticationData &
 DS28E16::PageAuthenticationData::setAnonymousRomId() {
   fill(romId().begin(), romId().end(), 0xFF);