Fork for fixes

Committer:
ivo_n
Date:
Sat Sep 26 08:31:41 2020 +0000
Revision:
22:a0b1d0e6d237
Parent:
16:269f652b4d0b
Everything seems to work

Who changed what in which revision?

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