Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RunCommand.hpp Source File

RunCommand.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceCore_RunCommand_hpp
00034 #define MaximInterfaceCore_RunCommand_hpp
00035 
00036 #include "Config.hpp"
00037 #include "Function.hpp"
00038 #include "SelectRom.hpp"
00039 #include "span.hpp"
00040 #include "system_error.hpp"
00041 
00042 namespace MaximInterfaceCore {
00043 
00044 class I2CMaster;
00045 class OneWireMaster;
00046 class Sleep;
00047 
00048 /// @brief
00049 /// Runs a command sequence by writing the command request, waiting the
00050 /// specified amount of time, and reading back the command response.
00051 /// @details
00052 /// The parameters for this function are the command request, the delay time in
00053 /// milliseconds, and a mutable buffer for the command response. The actual
00054 /// buffer used for the command response is returned.
00055 typedef Function<Result<span<uint_least8_t> >(span<const uint_least8_t>, int,
00056                                               span<uint_least8_t>)>
00057     RunCommand;
00058 
00059 class RunCommandWithOneWireMaster {
00060 public:
00061   typedef RunCommand::result_type result_type;
00062 
00063   enum ErrorValue { CrcError = 1, InvalidResponseError };
00064   MaximInterfaceCore_EXPORT static const error_category & errorCategory();
00065 
00066   RunCommandWithOneWireMaster(Sleep & sleep, OneWireMaster & master,
00067                               const SelectRom & selectRom)
00068       : selectRom(selectRom), master(&master), sleep(&sleep) {}
00069 
00070   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00071 
00072   void setMaster(OneWireMaster & master) { this->master = &master; }
00073 
00074   void setSelectRom(const SelectRom & selectRom) {
00075     this->selectRom = selectRom;
00076   }
00077 
00078   MaximInterfaceCore_EXPORT Result<span<uint_least8_t> >
00079   operator()(span<const uint_least8_t> request, int delayTime,
00080              span<uint_least8_t> response) const;
00081 
00082 private:
00083   SelectRom selectRom;
00084   OneWireMaster * master;
00085   const Sleep * sleep;
00086 };
00087 
00088 template <>
00089 struct is_error_code_enum<RunCommandWithOneWireMaster::ErrorValue>
00090     : true_type {};
00091 
00092 inline error_code make_error_code(RunCommandWithOneWireMaster::ErrorValue e) {
00093   return error_code(e, RunCommandWithOneWireMaster::errorCategory());
00094 }
00095 
00096 class RunCommandWithI2CMaster {
00097 public:
00098   typedef RunCommand::result_type result_type;
00099 
00100   enum ErrorValue { InvalidResponseError = 1 };
00101   MaximInterfaceCore_EXPORT static const error_category & errorCategory();
00102 
00103   RunCommandWithI2CMaster(Sleep & sleep, I2CMaster & master,
00104                           uint_least8_t address)
00105       : sleep(&sleep), master(&master), address_(address & 0xFE) {}
00106 
00107   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00108 
00109   void setMaster(I2CMaster & master) { this->master = &master; }
00110 
00111   uint_least8_t address() const { return address_; }
00112 
00113   void setAddress(uint_least8_t address) { address_ = address & 0xFE; }
00114 
00115   MaximInterfaceCore_EXPORT Result<span<uint_least8_t> >
00116   operator()(span<const uint_least8_t> request, int delayTime,
00117              span<uint_least8_t> response) const;
00118 
00119 private:
00120   const Sleep * sleep;
00121   I2CMaster * master;
00122   uint_least8_t address_;
00123 };
00124 
00125 template <>
00126 struct is_error_code_enum<RunCommandWithI2CMaster::ErrorValue> : true_type {};
00127 
00128 inline error_code make_error_code(RunCommandWithI2CMaster::ErrorValue e) {
00129   return error_code(e, RunCommandWithI2CMaster::errorCategory());
00130 }
00131 
00132 } // namespace MaximInterfaceCore
00133 
00134 #endif