leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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