Maxim Integrated / MaximInterface

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Committer:
IanBenzMaxim
Date:
Mon Jul 22 11:44:07 2019 -0500
Revision:
7:9cd16581b578
Updated to version 1.9.

Who changed what in which revision?

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