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 "stdint.h"
nexpaq 1:55a6170b404f 17 #include "ip_fsc.h"
nexpaq 1:55a6170b404f 18
nexpaq 1:55a6170b404f 19 /** \brief Compute IP checksum for arbitary data
nexpaq 1:55a6170b404f 20 *
nexpaq 1:55a6170b404f 21 * Compute an IP checksum, given a arbitrary gather list.
nexpaq 1:55a6170b404f 22 *
nexpaq 1:55a6170b404f 23 * See ipv6_fcf for discussion of use.
nexpaq 1:55a6170b404f 24 *
nexpaq 1:55a6170b404f 25 * This will work for any arbitrary gather list - it can handle odd
nexpaq 1:55a6170b404f 26 * alignments. The one limitation is that the 32-bit accumulator limits
nexpaq 1:55a6170b404f 27 * it to basically 64K of total data.
nexpaq 1:55a6170b404f 28 */
nexpaq 1:55a6170b404f 29 uint16_t ip_fcf_v(uint_fast8_t count, const ns_iovec_t vec[static count])
nexpaq 1:55a6170b404f 30 {
nexpaq 1:55a6170b404f 31 uint_fast32_t acc32 = 0;
nexpaq 1:55a6170b404f 32 bool odd = false;
nexpaq 1:55a6170b404f 33 while (count) {
nexpaq 1:55a6170b404f 34 const uint8_t *data_ptr = vec->iov_base;
nexpaq 1:55a6170b404f 35 uint_fast16_t data_length = vec->iov_len;
nexpaq 1:55a6170b404f 36 if (odd && data_length > 0) {
nexpaq 1:55a6170b404f 37 acc32 += *data_ptr++;
nexpaq 1:55a6170b404f 38 data_length--;
nexpaq 1:55a6170b404f 39 odd = false;
nexpaq 1:55a6170b404f 40 }
nexpaq 1:55a6170b404f 41 while (data_length >= 2) {
nexpaq 1:55a6170b404f 42 acc32 += (uint_fast16_t) data_ptr[0] << 8 | data_ptr[1];
nexpaq 1:55a6170b404f 43 data_ptr += 2;
nexpaq 1:55a6170b404f 44 data_length -= 2;
nexpaq 1:55a6170b404f 45 }
nexpaq 1:55a6170b404f 46 if (data_length) {
nexpaq 1:55a6170b404f 47 acc32 += (uint_fast16_t) data_ptr[0] << 8;
nexpaq 1:55a6170b404f 48 odd = true;
nexpaq 1:55a6170b404f 49 }
nexpaq 1:55a6170b404f 50 vec++;
nexpaq 1:55a6170b404f 51 count--;
nexpaq 1:55a6170b404f 52 }
nexpaq 1:55a6170b404f 53
nexpaq 1:55a6170b404f 54 // Fold down up to 0xffff carries in the 32-bit accumulator
nexpaq 1:55a6170b404f 55 acc32 = (acc32 >> 16) + (acc32 & 0xffff);
nexpaq 1:55a6170b404f 56
nexpaq 1:55a6170b404f 57 // Could be one more carry from the previous addition (result <= 0x1fffe)
nexpaq 1:55a6170b404f 58 uint16_t sum16 = (uint16_t)((acc32 >> 16) + (acc32 & 0xffff));
nexpaq 1:55a6170b404f 59 return ~sum16;
nexpaq 1:55a6170b404f 60 }
nexpaq 1:55a6170b404f 61
nexpaq 1:55a6170b404f 62 /** \brief Compute IPv6 checksum
nexpaq 1:55a6170b404f 63 *
nexpaq 1:55a6170b404f 64 * Compute an IPv6 checksum, given fields of an IPv6 pseudoheader and payload.
nexpaq 1:55a6170b404f 65 *
nexpaq 1:55a6170b404f 66 * This returns the 1's-complement of the checksum, as required when
nexpaq 1:55a6170b404f 67 * generating the checksum for transmission. The result can be 0x0000;
nexpaq 1:55a6170b404f 68 * for UDP (only) this must be transformed to 0xFFFF to distinguish from
nexpaq 1:55a6170b404f 69 * a packet with no checksum.
nexpaq 1:55a6170b404f 70 *
nexpaq 1:55a6170b404f 71 * To check a packet, this function will return 0 when run on a
nexpaq 1:55a6170b404f 72 * packet with a valid checksum. Checksums should be checked like this rather
nexpaq 1:55a6170b404f 73 * than setting the checksum field to zero and comparing generated checksum with
nexpaq 1:55a6170b404f 74 * the original value - this would fail in the case the received packet had
nexpaq 1:55a6170b404f 75 * checksum 0xFFFF.
nexpaq 1:55a6170b404f 76 */
nexpaq 1:55a6170b404f 77 uint16_t ipv6_fcf(const uint8_t src_address[static 16], const uint8_t dest_address[static 16],
nexpaq 1:55a6170b404f 78 uint16_t data_length, const uint8_t data_ptr[static data_length], uint8_t next_protocol)
nexpaq 1:55a6170b404f 79 {
nexpaq 1:55a6170b404f 80 // Use gather vector to lay out IPv6 pseudo-header (RFC 2460) and data
nexpaq 1:55a6170b404f 81 uint8_t hdr_data[] = { data_length >> 8, data_length, 0, next_protocol };
nexpaq 1:55a6170b404f 82 ns_iovec_t vec[4] = {
nexpaq 1:55a6170b404f 83 { (void *) src_address, 16 },
nexpaq 1:55a6170b404f 84 { (void *) dest_address, 16 },
nexpaq 1:55a6170b404f 85 { hdr_data, 4 },
nexpaq 1:55a6170b404f 86 { (void *) data_ptr, data_length }
nexpaq 1:55a6170b404f 87 };
nexpaq 1:55a6170b404f 88
nexpaq 1:55a6170b404f 89 return ip_fcf_v(4, vec);
nexpaq 1:55a6170b404f 90 }