Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 /*
maygup01 0:11cc2b7889af 2 * Copyright (c) 2014-2018 ARM Limited. All rights reserved.
maygup01 0:11cc2b7889af 3 * SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 4 * Licensed under the Apache License, Version 2.0 (the License); you may
maygup01 0:11cc2b7889af 5 * not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 6 * You may obtain a copy of the License at
maygup01 0:11cc2b7889af 7 *
maygup01 0:11cc2b7889af 8 * http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 9 *
maygup01 0:11cc2b7889af 10 * Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
maygup01 0:11cc2b7889af 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 13 * See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 14 * limitations under the License.
maygup01 0:11cc2b7889af 15 */
maygup01 0:11cc2b7889af 16 #include <string.h>
maygup01 0:11cc2b7889af 17 #include <stdlib.h>
maygup01 0:11cc2b7889af 18 #include <stdint.h>
maygup01 0:11cc2b7889af 19 #include "common_functions.h"
maygup01 0:11cc2b7889af 20 #include "ip4string.h"
maygup01 0:11cc2b7889af 21
maygup01 0:11cc2b7889af 22 /**
maygup01 0:11cc2b7889af 23 * Convert numeric IPv4 address string to a binary.
maygup01 0:11cc2b7889af 24 * \param ip4addr IPv4 address in string format.
maygup01 0:11cc2b7889af 25 * \param len Length of IPv4 string, maximum of 16..
maygup01 0:11cc2b7889af 26 * \param dest buffer for address. MUST be 4 bytes.
maygup01 0:11cc2b7889af 27 * \return boolean set to true if conversion succeded, false if it didn't
maygup01 0:11cc2b7889af 28 */
maygup01 0:11cc2b7889af 29 bool stoip4(const char *ip4addr, size_t len, void *dest)
maygup01 0:11cc2b7889af 30 {
maygup01 0:11cc2b7889af 31 uint8_t *addr = dest;
maygup01 0:11cc2b7889af 32
maygup01 0:11cc2b7889af 33 if (len > 16) { // Too long, not possible
maygup01 0:11cc2b7889af 34 return false;
maygup01 0:11cc2b7889af 35 }
maygup01 0:11cc2b7889af 36
maygup01 0:11cc2b7889af 37 uint_fast8_t stringLength = 0, byteCount = 0;
maygup01 0:11cc2b7889af 38
maygup01 0:11cc2b7889af 39 //Iterate over each component of the IP. The exit condition is in the middle of the loop
maygup01 0:11cc2b7889af 40 while (true) {
maygup01 0:11cc2b7889af 41
maygup01 0:11cc2b7889af 42 //No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z)
maygup01 0:11cc2b7889af 43 if (stringLength == len || ip4addr[stringLength] < '0' || ip4addr[stringLength] > '9') {
maygup01 0:11cc2b7889af 44 return false;
maygup01 0:11cc2b7889af 45 }
maygup01 0:11cc2b7889af 46
maygup01 0:11cc2b7889af 47 //For each component, we convert it to the raw value
maygup01 0:11cc2b7889af 48 uint_fast16_t byte = 0;
maygup01 0:11cc2b7889af 49 while (stringLength < len && ip4addr[stringLength] >= '0' && ip4addr[stringLength] <= '9') {
maygup01 0:11cc2b7889af 50 byte *= 10;
maygup01 0:11cc2b7889af 51 byte += ip4addr[stringLength++] - '0';
maygup01 0:11cc2b7889af 52
maygup01 0:11cc2b7889af 53 //We go over the maximum value for an IPv4 component
maygup01 0:11cc2b7889af 54 if (byte > 0xff) {
maygup01 0:11cc2b7889af 55 return false;
maygup01 0:11cc2b7889af 56 }
maygup01 0:11cc2b7889af 57 }
maygup01 0:11cc2b7889af 58
maygup01 0:11cc2b7889af 59 //Append the component
maygup01 0:11cc2b7889af 60 addr[byteCount++] = (uint8_t) byte;
maygup01 0:11cc2b7889af 61
maygup01 0:11cc2b7889af 62 //If we're at the end, we leave the loop. It's the only way to reach the `true` output
maygup01 0:11cc2b7889af 63 if (byteCount == 4) {
maygup01 0:11cc2b7889af 64 break;
maygup01 0:11cc2b7889af 65 }
maygup01 0:11cc2b7889af 66
maygup01 0:11cc2b7889af 67 //If the next character is invalid, we return false
maygup01 0:11cc2b7889af 68 if (stringLength == len || ip4addr[stringLength++] != '.') {
maygup01 0:11cc2b7889af 69 return false;
maygup01 0:11cc2b7889af 70 }
maygup01 0:11cc2b7889af 71 }
maygup01 0:11cc2b7889af 72
maygup01 0:11cc2b7889af 73 return stringLength == len || ip4addr[stringLength] == '\0';
maygup01 0:11cc2b7889af 74 }