Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 /*
MACRUM 0:119624335925 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
MACRUM 0:119624335925 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:119624335925 5 * not use this file except in compliance with the License.
MACRUM 0:119624335925 6 * You may obtain a copy of the License at
MACRUM 0:119624335925 7 *
MACRUM 0:119624335925 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 9 *
MACRUM 0:119624335925 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:119624335925 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 13 * See the License for the specific language governing permissions and
MACRUM 0:119624335925 14 * limitations under the License.
MACRUM 0:119624335925 15 */
MACRUM 0:119624335925 16 #include <stdio.h>
MACRUM 0:119624335925 17 #include <string.h>
MACRUM 0:119624335925 18 #include "common_functions.h"
MACRUM 0:119624335925 19 #include "ip6string.h"
MACRUM 0:119624335925 20
MACRUM 0:119624335925 21 /**
MACRUM 0:119624335925 22 * Print binary IPv6 address to a string.
MACRUM 0:119624335925 23 * String must contain enough room for full address, 40 bytes exact.
MACRUM 0:119624335925 24 * IPv4 tunneling addresses are not covered.
MACRUM 0:119624335925 25 * \param addr IPv6 address.
MACRUM 0:119624335925 26 * \p buffer to write string to.
MACRUM 0:119624335925 27 */
MACRUM 0:119624335925 28 uint_fast8_t ip6tos(const void *ip6addr, char *p)
MACRUM 0:119624335925 29 {
MACRUM 0:119624335925 30 char *p_orig = p;
MACRUM 0:119624335925 31 uint_fast8_t zero_start = 255, zero_len = 1;
MACRUM 0:119624335925 32 const uint8_t *addr = ip6addr;
MACRUM 0:119624335925 33 uint_fast16_t part;
MACRUM 0:119624335925 34
MACRUM 0:119624335925 35 /* Follow RFC 5952 - pre-scan for longest run of zeros */
MACRUM 0:119624335925 36 for (uint_fast8_t n = 0; n < 8; n++) {
MACRUM 0:119624335925 37 part = *addr++;
MACRUM 0:119624335925 38 part = (part << 8) | *addr++;
MACRUM 0:119624335925 39 if (part != 0) {
MACRUM 0:119624335925 40 continue;
MACRUM 0:119624335925 41 }
MACRUM 0:119624335925 42
MACRUM 0:119624335925 43 /* We're at the start of a run of zeros - scan to non-zero (or end) */
MACRUM 0:119624335925 44 uint_fast8_t n0 = n;
MACRUM 0:119624335925 45 for (n = n0 + 1; n < 8; n++) {
MACRUM 0:119624335925 46 part = *addr++;
MACRUM 0:119624335925 47 part = (part << 8) | *addr++;
MACRUM 0:119624335925 48 if (part != 0) {
MACRUM 0:119624335925 49 break;
MACRUM 0:119624335925 50 }
MACRUM 0:119624335925 51 }
MACRUM 0:119624335925 52
MACRUM 0:119624335925 53 /* Now n0->initial zero of run, n->after final zero in run. Is this the
MACRUM 0:119624335925 54 * longest run yet? If equal, we stick with the previous one - RFC 5952
MACRUM 0:119624335925 55 * S4.2.3. Note that zero_len being initialised to 1 stops us
MACRUM 0:119624335925 56 * shortening a 1-part run (S4.2.2.)
MACRUM 0:119624335925 57 */
MACRUM 0:119624335925 58 if (n - n0 > zero_len) {
MACRUM 0:119624335925 59 zero_start = n0;
MACRUM 0:119624335925 60 zero_len = n - n0;
MACRUM 0:119624335925 61 }
MACRUM 0:119624335925 62
MACRUM 0:119624335925 63 /* Continue scan for initial zeros from part n+1 - we've already
MACRUM 0:119624335925 64 * consumed part n, and know it's non-zero. */
MACRUM 0:119624335925 65 }
MACRUM 0:119624335925 66
MACRUM 0:119624335925 67 /* Now go back and print, jumping over any zero run */
MACRUM 0:119624335925 68 addr = ip6addr;
MACRUM 0:119624335925 69 for (uint_fast8_t n = 0; n < 8;) {
MACRUM 0:119624335925 70 if (n == zero_start) {
MACRUM 0:119624335925 71 if (n == 0) {
MACRUM 0:119624335925 72 *p++ = ':';
MACRUM 0:119624335925 73 }
MACRUM 0:119624335925 74 *p++ = ':';
MACRUM 0:119624335925 75 addr += 2 * zero_len;
MACRUM 0:119624335925 76 n += zero_len;
MACRUM 0:119624335925 77 continue;
MACRUM 0:119624335925 78 }
MACRUM 0:119624335925 79
MACRUM 0:119624335925 80 part = *addr++;
MACRUM 0:119624335925 81 part = (part << 8) | *addr++;
MACRUM 0:119624335925 82 n++;
MACRUM 0:119624335925 83
MACRUM 0:119624335925 84 p += sprintf(p, "%"PRIxFAST16, part);
MACRUM 0:119624335925 85
MACRUM 0:119624335925 86 /* One iteration writes "part:" rather than ":part", and has the
MACRUM 0:119624335925 87 * explicit check for n == 8 below, to allow easy extension for
MACRUM 0:119624335925 88 * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just
MACRUM 0:119624335925 89 * run the same loop for 6 parts, and output would then finish with the
MACRUM 0:119624335925 90 * required : or ::, ready for "a.b.c.d" to be tacked on.
MACRUM 0:119624335925 91 */
MACRUM 0:119624335925 92 if (n != 8) {
MACRUM 0:119624335925 93 *p++ = ':';
MACRUM 0:119624335925 94 }
MACRUM 0:119624335925 95 }
MACRUM 0:119624335925 96 *p = '\0';
MACRUM 0:119624335925 97
MACRUM 0:119624335925 98 // Return length of generated string, excluding the terminating null character
MACRUM 0:119624335925 99 return p - p_orig;
MACRUM 0:119624335925 100 }
MACRUM 0:119624335925 101
MACRUM 0:119624335925 102 uint_fast8_t ip6_prefix_tos(const void *prefix, uint_fast8_t prefix_len, char *p)
MACRUM 0:119624335925 103 {
MACRUM 0:119624335925 104 char *wptr = p;
MACRUM 0:119624335925 105 uint8_t addr[16] = {0};
MACRUM 0:119624335925 106
MACRUM 0:119624335925 107 if (prefix_len > 128) {
MACRUM 0:119624335925 108 return 0;
MACRUM 0:119624335925 109 }
MACRUM 0:119624335925 110
MACRUM 0:119624335925 111 // Generate prefix part of the string
MACRUM 0:119624335925 112 bitcopy(addr, prefix, prefix_len);
MACRUM 0:119624335925 113 wptr += ip6tos(addr, wptr);
MACRUM 0:119624335925 114 // Add the prefix length part of the string
MACRUM 0:119624335925 115 wptr += sprintf(wptr, "/%"PRIuFAST8, prefix_len);
MACRUM 0:119624335925 116
MACRUM 0:119624335925 117 // Return total length of generated string
MACRUM 0:119624335925 118 return wptr - p;
MACRUM 0:119624335925 119 }