WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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