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/DS28E38.hpp
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
--- a/MaximInterfaceDevices/DS28E38.hpp Mon Jul 22 11:44:07 2019 -0500 +++ b/MaximInterfaceDevices/DS28E38.hpp Mon Sep 16 11:13:37 2019 -0500 @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2017 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_DS28E38 -#define MaximInterfaceDevices_DS28E38 +#ifndef MaximInterfaceDevices_DS28E38_hpp +#define MaximInterfaceDevices_DS28E38_hpp #include <stdint.h> #include <MaximInterfaceCore/Algorithm.hpp> @@ -94,12 +94,12 @@ EntropyNotHealthy = 0xDD }; - typedef Core::array<PageProtection, memoryPages> PageProtectionList; - typedef Core::array<uint_least8_t, 2> RomVersion; + typedef Core::array_span<PageProtection, memoryPages> PageProtectionList; + typedef Core::array_span<uint_least8_t, 2> RomVersion; - PageProtectionList pageProtection; + PageProtectionList::array pageProtection; Core::ManId::array manId; - RomVersion romVersion; + RomVersion::array romVersion; EntropyHealthTestStatus entropyHealthTestStatus; }; @@ -113,81 +113,84 @@ /// @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 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; /// Decrement the decrement-only counter. - MaximInterfaceDevices_EXPORT Core::error_code decrementCounter(); + MaximInterfaceDevices_EXPORT Core::Result<void> decrementCounter(); /// Permanently disable the device. - MaximInterfaceDevices_EXPORT Core::error_code disableDevice(); + MaximInterfaceDevices_EXPORT Core::Result<void> disableDevice(); /// @brief Generate a new ECDSA public key from an existing private key. /// @param privateKeyPuf True if PUF is used as the private key. /// @param writeProtectEnable True to lock the key against further writes. - MaximInterfaceDevices_EXPORT Core::error_code + MaximInterfaceDevices_EXPORT Core::Result<void> generateEcc256KeyPair(bool privateKeyPuf, bool writeProtectEnable); /// @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; MaximInterfaceDevices_EXPORT static const Core::error_category & 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::DS28E38::ErrorValue> + : true_type {}; + +} // namespace MaximInterfaceCore +namespace MaximInterfaceDevices { + inline Core::error_code make_error_code(DS28E38::ErrorValue e) { return Core::error_code(e, DS28E38::errorCategory()); } -/// @brief Read the device MAN ID using the Read Status command. -/// @param ds28e38 Device to read. -/// @param[out] manId Read MAN ID valid when operation is successful. -MaximInterfaceDevices_EXPORT Core::error_code -readManId(DS28E38 & ds28e38, Core::ManId::span manId); - /// Format page authentication input data. class DS28E38::PageAuthenticationData { public: @@ -307,13 +310,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_; };