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

Committer:
IanBenzMaxim
Date:
Fri May 29 16:19:22 2020 -0500
Revision:
12:7eb41621ba22
Parent:
10:947d3f44e0a0
Updated to version 2.2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 8:5ea891c7d1a1 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 8:5ea891c7d1a1 3 *
IanBenzMaxim 8:5ea891c7d1a1 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 8:5ea891c7d1a1 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 8:5ea891c7d1a1 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 8:5ea891c7d1a1 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 8:5ea891c7d1a1 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 8:5ea891c7d1a1 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 8:5ea891c7d1a1 10 *
IanBenzMaxim 8:5ea891c7d1a1 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 8:5ea891c7d1a1 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 8:5ea891c7d1a1 13 *
IanBenzMaxim 8:5ea891c7d1a1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 8:5ea891c7d1a1 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 8:5ea891c7d1a1 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 8:5ea891c7d1a1 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 8:5ea891c7d1a1 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 8:5ea891c7d1a1 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 8:5ea891c7d1a1 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 8:5ea891c7d1a1 21 *
IanBenzMaxim 8:5ea891c7d1a1 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 8:5ea891c7d1a1 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 8:5ea891c7d1a1 24 * Products, Inc. Branding Policy.
IanBenzMaxim 8:5ea891c7d1a1 25 *
IanBenzMaxim 8:5ea891c7d1a1 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 8:5ea891c7d1a1 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 8:5ea891c7d1a1 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 8:5ea891c7d1a1 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 8:5ea891c7d1a1 30 * ownership rights.
IanBenzMaxim 8:5ea891c7d1a1 31 *******************************************************************************/
IanBenzMaxim 8:5ea891c7d1a1 32
IanBenzMaxim 8:5ea891c7d1a1 33 #include <cctype>
IanBenzMaxim 8:5ea891c7d1a1 34 #include <cstdio>
IanBenzMaxim 8:5ea891c7d1a1 35 #include <cstdlib>
IanBenzMaxim 8:5ea891c7d1a1 36 #include "HexString.hpp"
IanBenzMaxim 8:5ea891c7d1a1 37
IanBenzMaxim 8:5ea891c7d1a1 38 namespace MaximInterfaceCore {
IanBenzMaxim 8:5ea891c7d1a1 39
IanBenzMaxim 8:5ea891c7d1a1 40 static const int charsPerByte = 2;
IanBenzMaxim 8:5ea891c7d1a1 41
IanBenzMaxim 10:947d3f44e0a0 42 std::string toHexString(span<const uint_least8_t> byteArray) {
IanBenzMaxim 10:947d3f44e0a0 43 std::string hexString;
IanBenzMaxim 8:5ea891c7d1a1 44 hexString.reserve(byteArray.size() * charsPerByte);
IanBenzMaxim 8:5ea891c7d1a1 45 for (span<const uint_least8_t>::index_type i = 0; i < byteArray.size(); ++i) {
IanBenzMaxim 8:5ea891c7d1a1 46 char hexBuf[charsPerByte + 1];
IanBenzMaxim 8:5ea891c7d1a1 47 std::sprintf(hexBuf, "%2.2X", byteArray[i] & 0xFF);
IanBenzMaxim 8:5ea891c7d1a1 48 hexString.append(hexBuf, charsPerByte);
IanBenzMaxim 8:5ea891c7d1a1 49 }
IanBenzMaxim 8:5ea891c7d1a1 50 return hexString;
IanBenzMaxim 8:5ea891c7d1a1 51 }
IanBenzMaxim 8:5ea891c7d1a1 52
IanBenzMaxim 10:947d3f44e0a0 53 Optional<std::vector<uint_least8_t> >
IanBenzMaxim 10:947d3f44e0a0 54 fromHexString(span<const char> hexString) {
IanBenzMaxim 10:947d3f44e0a0 55 Optional<std::vector<uint_least8_t> > byteArray =
IanBenzMaxim 10:947d3f44e0a0 56 std::vector<uint_least8_t>();
IanBenzMaxim 8:5ea891c7d1a1 57 byteArray->reserve(hexString.size() / charsPerByte);
IanBenzMaxim 8:5ea891c7d1a1 58 char hexBuf[charsPerByte + 1];
IanBenzMaxim 8:5ea891c7d1a1 59 char * const hexBufEnd = hexBuf + charsPerByte;
IanBenzMaxim 8:5ea891c7d1a1 60 *hexBufEnd = '\0';
IanBenzMaxim 8:5ea891c7d1a1 61 char * hexBufIt = hexBuf;
IanBenzMaxim 10:947d3f44e0a0 62 for (span<const char>::iterator hexStringIt = hexString.begin(),
IanBenzMaxim 10:947d3f44e0a0 63 hexStringEnd = hexString.end();
IanBenzMaxim 8:5ea891c7d1a1 64 hexStringIt != hexStringEnd; ++hexStringIt) {
IanBenzMaxim 10:947d3f44e0a0 65 if (!(std::isspace(*hexStringIt) || *hexStringIt == '\0')) {
IanBenzMaxim 8:5ea891c7d1a1 66 *hexBufIt = *hexStringIt;
IanBenzMaxim 8:5ea891c7d1a1 67 if (++hexBufIt == hexBufEnd) {
IanBenzMaxim 8:5ea891c7d1a1 68 char * hexBufParseEnd;
IanBenzMaxim 8:5ea891c7d1a1 69 byteArray->push_back(static_cast<uint_least8_t>(
IanBenzMaxim 8:5ea891c7d1a1 70 std::strtoul(hexBuf, &hexBufParseEnd, 16)));
IanBenzMaxim 8:5ea891c7d1a1 71 if (hexBufParseEnd != hexBufEnd) {
IanBenzMaxim 8:5ea891c7d1a1 72 break;
IanBenzMaxim 8:5ea891c7d1a1 73 }
IanBenzMaxim 8:5ea891c7d1a1 74 hexBufIt = hexBuf;
IanBenzMaxim 8:5ea891c7d1a1 75 }
IanBenzMaxim 8:5ea891c7d1a1 76 }
IanBenzMaxim 8:5ea891c7d1a1 77 }
IanBenzMaxim 8:5ea891c7d1a1 78 if (hexBufIt != hexBuf) {
IanBenzMaxim 8:5ea891c7d1a1 79 byteArray.reset();
IanBenzMaxim 8:5ea891c7d1a1 80 }
IanBenzMaxim 8:5ea891c7d1a1 81 return byteArray;
IanBenzMaxim 8:5ea891c7d1a1 82 }
IanBenzMaxim 8:5ea891c7d1a1 83
IanBenzMaxim 8:5ea891c7d1a1 84 } // namespace MaximInterfaceCore