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
Diff: MaximInterfaceDevices/DS28E16.hpp
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
--- a/MaximInterfaceDevices/DS28E16.hpp Mon Jul 22 11:44:07 2019 -0500 +++ b/MaximInterfaceDevices/DS28E16.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_DS28E16 -#define MaximInterfaceDevices_DS28E16 +#ifndef MaximInterfaceDevices_DS28E16_hpp +#define MaximInterfaceDevices_DS28E16_hpp #include <stdint.h> #include <MaximInterfaceCore/Algorithm.hpp> @@ -91,9 +91,9 @@ typedef Core::FlagSet<PageProtectionType, 4> PageProtection; struct Status { - typedef Core::array<PageProtection, memoryPages> PageProtectionList; + typedef Core::array_span<PageProtection, memoryPages> PageProtectionList; - PageProtectionList pageProtection; + PageProtectionList::array pageProtection; uint_least8_t manId; uint_least8_t deviceVersion; }; @@ -108,48 +108,47 @@ /// @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. - /// @param[out] status Status that was read. - MaximInterfaceDevices_EXPORT Core::error_code readStatus(Status & status); + /// @returns Status that was read. + MaximInterfaceDevices_EXPORT Core::Result<Status> readStatus() 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 HMAC. /// @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] hmac Computed page HMAC. - MaximInterfaceDevices_EXPORT Core::error_code + /// @returns Computed page HMAC. + MaximInterfaceDevices_EXPORT Core::Result<DoublePage::array> computeAndReadPageAuthentication(int pageNum, bool anonymous, - DoublePage::const_span challenge, - DoublePage::span hmac); + DoublePage::const_span challenge) const; /// Decrement the decrement-only counter. - MaximInterfaceDevices_EXPORT Core::error_code decrementCounter(); + MaximInterfaceDevices_EXPORT Core::Result<void> decrementCounter(); /// Set password that will be subsequently used to disable the device. - MaximInterfaceDevices_EXPORT Core::error_code + MaximInterfaceDevices_EXPORT Core::Result<void> setDisableDevicePassword(DisableDevicePassword::const_span password); /// @brief Lock-out all disable functionality for the device. /// @note Only allowed prior to setting password. - MaximInterfaceDevices_EXPORT Core::error_code lockOutDisableDevice(); + MaximInterfaceDevices_EXPORT Core::Result<void> lockOutDisableDevice(); /// Permanently disable the device. - MaximInterfaceDevices_EXPORT Core::error_code + MaximInterfaceDevices_EXPORT Core::Result<void> disableDevice(DisableDevicePassword::const_span password); /// @brief @@ -162,7 +161,7 @@ /// @note /// This command should be executed prior to the /// Compute and Read Page Authentication command. - MaximInterfaceDevices_EXPORT Core::error_code + MaximInterfaceDevices_EXPORT Core::Result<void> computeSecret(int bindingDataPageNum, bool constantBindingData, bool anonymous, DoublePage::const_span partialSecret); @@ -170,11 +169,11 @@ 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: @@ -184,22 +183,26 @@ DisableDevice = 0x00 }; - Core::error_code disableDevice(DisableDeviceOperation operation, - DisableDevicePassword::const_span password); + Core::Result<void> disableDevice(DisableDeviceOperation operation, + DisableDevicePassword::const_span password); Core::RunCommand doRunCommand; }; +} // namespace MaximInterfaceDevices +namespace MaximInterfaceCore { + +template <> +struct is_error_code_enum<MaximInterfaceDevices::DS28E16::ErrorValue> + : true_type {}; + +} // namespace MaximInterfaceCore +namespace MaximInterfaceDevices { + inline Core::error_code make_error_code(DS28E16::ErrorValue e) { return Core::error_code(e, DS28E16::errorCategory()); } -/// @brief Read the device MAN ID using the Read Status command. -/// @param device Device to read. -/// @param[out] manId Read MAN ID valid when operation is successful. -MaximInterfaceDevices_EXPORT Core::error_code readManId(DS28E16 & device, - uint_least8_t & manId); - /// Format page authentication input data. class DS28E16::PageAuthenticationData { public: @@ -317,13 +320,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 + 2 * Page::size; - static const index pageNumIdx = challengeIdx + DoublePage::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 + 2 * Page::size; + static const size_t pageNumIdx = challengeIdx + DoublePage::size; + static const size_t manIdIdx = pageNumIdx + 1; Result::array result_; };