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 LoggingOneWireMaster.cpp Source File

LoggingOneWireMaster.cpp

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 #include "HexString.hpp"
00034 #include "LoggingOneWireMaster.hpp"
00035 
00036 using std::string;
00037 
00038 namespace MaximInterfaceCore {
00039 
00040 static const char strongLevelString[] = "<SP_ON>";
00041 
00042 static string formatDataString(span<const uint_least8_t> data, bool read) {
00043   string dataBuilder;
00044   for (span<const uint_least8_t>::index_type i = 0; i < data.size(); ++i) {
00045     if (read) {
00046       dataBuilder.append(1, '[');
00047     }
00048     dataBuilder.append(toHexString(data.subspan(i, 1)));
00049     if (read) {
00050       dataBuilder.append(1, ']');
00051     }
00052     dataBuilder.append(1, ' ');
00053   }
00054   return dataBuilder;
00055 }
00056 
00057 void LoggingOneWireMaster::tryWriteMessage(const std::string & message) {
00058   if (writeMessage) {
00059     writeMessage(message);
00060   }
00061 }
00062 
00063 Result<void> LoggingOneWireMaster::reset() {
00064   Result<void> result = OneWireMasterDecorator::reset();
00065   tryWriteMessage(result ? "RP" : "RN");
00066   return result;
00067 }
00068 
00069 Result<void> LoggingOneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
00070                                                      Level afterLevel) {
00071   tryWriteMessage(formatDataString(make_span(&sendByte, 1), false));
00072   if (afterLevel == StrongLevel) {
00073     tryWriteMessage(strongLevelString);
00074   }
00075   return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
00076 }
00077 
00078 Result<uint_least8_t> LoggingOneWireMaster::readByteSetLevel(Level afterLevel) {
00079   const Result<uint_least8_t> recvByte =
00080       OneWireMasterDecorator::readByteSetLevel(afterLevel);
00081   if (recvByte) {
00082     tryWriteMessage(formatDataString(make_span(&recvByte.value(), 1), true));
00083     if (afterLevel == StrongLevel) {
00084       tryWriteMessage(strongLevelString);
00085     }
00086   }
00087   return recvByte;
00088 }
00089 
00090 Result<void>
00091 LoggingOneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
00092   tryWriteMessage(formatDataString(sendBuf, false));
00093   return OneWireMasterDecorator::writeBlock(sendBuf);
00094 }
00095 
00096 Result<void> LoggingOneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
00097   Result<void> result = OneWireMasterDecorator::readBlock(recvBuf);
00098   if (result) {
00099     tryWriteMessage(formatDataString(recvBuf, true));
00100   }
00101   return result;
00102 }
00103 
00104 Result<void> LoggingOneWireMaster::setSpeed(Speed newSpeed) {
00105   Result<void> result = OneWireMasterDecorator::setSpeed(newSpeed);
00106   if (result) {
00107     string newSpeedString;
00108     switch (newSpeed) {
00109     case StandardSpeed:
00110       newSpeedString = "<STD>";
00111       break;
00112 
00113     case OverdriveSpeed:
00114       newSpeedString = "<OVR>";
00115       break;
00116     }
00117     if (!newSpeedString.empty()) {
00118       tryWriteMessage(newSpeedString);
00119     }
00120   }
00121   return result;
00122 }
00123 
00124 Result<void> LoggingOneWireMaster::setLevel(Level newLevel) {
00125   Result<void> result = OneWireMasterDecorator::setLevel(newLevel);
00126   if (result) {
00127     string newLevelString;
00128     switch (newLevel) {
00129     case NormalLevel:
00130       newLevelString = "<SP_OFF>";
00131       break;
00132 
00133     case StrongLevel:
00134       newLevelString = strongLevelString;
00135       break;
00136     }
00137     if (!newLevelString.empty()) {
00138       tryWriteMessage(newLevelString);
00139     }
00140   }
00141   return result;
00142 }
00143 
00144 } // namespace MaximInterfaceCore