Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2014-2018 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16 #include <stdio.h>
vithyat 0:977e87915078 17 #include <string.h>
vithyat 0:977e87915078 18 #include "common_functions.h"
vithyat 0:977e87915078 19 #include "ip4string.h"
vithyat 0:977e87915078 20
vithyat 0:977e87915078 21 static void ipv4_itoa(char *string, uint8_t byte);
vithyat 0:977e87915078 22
vithyat 0:977e87915078 23 /**
vithyat 0:977e87915078 24 * Print binary IPv4 address to a string.
vithyat 0:977e87915078 25 * String must contain enough room for full address, 16 bytes exact.
vithyat 0:977e87915078 26 * \param addr IPv4 address.
vithyat 0:977e87915078 27 * \p buffer to write string to.
vithyat 0:977e87915078 28 */
vithyat 0:977e87915078 29 uint_fast8_t ip4tos(const void *ip4addr, char *p)
vithyat 0:977e87915078 30 {
vithyat 0:977e87915078 31 uint_fast8_t outputPos = 0;
vithyat 0:977e87915078 32 const uint8_t *byteArray = ip4addr;
vithyat 0:977e87915078 33
vithyat 0:977e87915078 34 for (uint_fast8_t component = 0; component < 4; ++component) {
vithyat 0:977e87915078 35 //Convert the byte to string
vithyat 0:977e87915078 36 ipv4_itoa(&p[outputPos], byteArray[component]);
vithyat 0:977e87915078 37
vithyat 0:977e87915078 38 //Move outputPos to the end of the string
vithyat 0:977e87915078 39 while (p[outputPos] != '\0') {
vithyat 0:977e87915078 40 outputPos += 1;
vithyat 0:977e87915078 41 }
vithyat 0:977e87915078 42
vithyat 0:977e87915078 43 //Append a dot if this is not the last digit
vithyat 0:977e87915078 44 if (component < 3) {
vithyat 0:977e87915078 45 p[outputPos++] = '.';
vithyat 0:977e87915078 46 }
vithyat 0:977e87915078 47 }
vithyat 0:977e87915078 48
vithyat 0:977e87915078 49 // Return length of generated string, excluding the terminating null character
vithyat 0:977e87915078 50 return outputPos;
vithyat 0:977e87915078 51 }
vithyat 0:977e87915078 52
vithyat 0:977e87915078 53 static void ipv4_itoa(char *string, uint8_t byte)
vithyat 0:977e87915078 54 {
vithyat 0:977e87915078 55 char *baseString = string;
vithyat 0:977e87915078 56
vithyat 0:977e87915078 57 //Write the digits to the buffer from the least significant to the most
vithyat 0:977e87915078 58 // This is the incorrect order but we will swap later
vithyat 0:977e87915078 59 do {
vithyat 0:977e87915078 60 *string++ = '0' + byte % 10;
vithyat 0:977e87915078 61 byte /= 10;
vithyat 0:977e87915078 62 } while (byte);
vithyat 0:977e87915078 63
vithyat 0:977e87915078 64 //We put the final \0, then go back one step on the last digit for the swap
vithyat 0:977e87915078 65 *string-- = '\0';
vithyat 0:977e87915078 66
vithyat 0:977e87915078 67 //We now swap the digits
vithyat 0:977e87915078 68 while (baseString < string) {
vithyat 0:977e87915078 69 uint8_t tmp = *string;
vithyat 0:977e87915078 70 *string-- = *baseString;
vithyat 0:977e87915078 71 *baseString++ = tmp;
vithyat 0:977e87915078 72 }
vithyat 0:977e87915078 73 }