mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
nanostack-libservice/source/libBits/common_functions.c@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | |
mbedAustin | 11:cada08fc8a70 | 17 | /* |
mbedAustin | 11:cada08fc8a70 | 18 | * Most functions can be inlined, and definitions are in common_functions.h. |
mbedAustin | 11:cada08fc8a70 | 19 | * Define COMMON_FUNCTIONS_FN before including it to generate external definitions. |
mbedAustin | 11:cada08fc8a70 | 20 | */ |
mbedAustin | 11:cada08fc8a70 | 21 | #define COMMON_FUNCTIONS_FN extern |
mbedAustin | 11:cada08fc8a70 | 22 | |
mbedAustin | 11:cada08fc8a70 | 23 | #include "common_functions.h" |
mbedAustin | 11:cada08fc8a70 | 24 | |
mbedAustin | 11:cada08fc8a70 | 25 | #include <string.h> |
mbedAustin | 11:cada08fc8a70 | 26 | |
mbedAustin | 11:cada08fc8a70 | 27 | /* Returns mask for <split_value> (0-8) most-significant bits of a byte */ |
mbedAustin | 11:cada08fc8a70 | 28 | static inline uint8_t context_split_mask(uint_fast8_t split_value) |
mbedAustin | 11:cada08fc8a70 | 29 | { |
mbedAustin | 11:cada08fc8a70 | 30 | return (uint8_t) - (0x100u >> split_value); |
mbedAustin | 11:cada08fc8a70 | 31 | } |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits) |
mbedAustin | 11:cada08fc8a70 | 34 | { |
mbedAustin | 11:cada08fc8a70 | 35 | uint_fast8_t bytes = bits / 8; |
mbedAustin | 11:cada08fc8a70 | 36 | bits %= 8; |
mbedAustin | 11:cada08fc8a70 | 37 | |
mbedAustin | 11:cada08fc8a70 | 38 | if (memcmp(a, b, bytes)) { |
mbedAustin | 11:cada08fc8a70 | 39 | return false; |
mbedAustin | 11:cada08fc8a70 | 40 | } |
mbedAustin | 11:cada08fc8a70 | 41 | |
mbedAustin | 11:cada08fc8a70 | 42 | if (bits) { |
mbedAustin | 11:cada08fc8a70 | 43 | uint_fast8_t split_bit = context_split_mask(bits); |
mbedAustin | 11:cada08fc8a70 | 44 | if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) { |
mbedAustin | 11:cada08fc8a70 | 45 | return false; |
mbedAustin | 11:cada08fc8a70 | 46 | } |
mbedAustin | 11:cada08fc8a70 | 47 | } |
mbedAustin | 11:cada08fc8a70 | 48 | |
mbedAustin | 11:cada08fc8a70 | 49 | return true; |
mbedAustin | 11:cada08fc8a70 | 50 | } |
mbedAustin | 11:cada08fc8a70 | 51 | |
mbedAustin | 11:cada08fc8a70 | 52 | uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) |
mbedAustin | 11:cada08fc8a70 | 53 | { |
mbedAustin | 11:cada08fc8a70 | 54 | uint_fast8_t bytes = bits / 8; |
mbedAustin | 11:cada08fc8a70 | 55 | bits %= 8; |
mbedAustin | 11:cada08fc8a70 | 56 | |
mbedAustin | 11:cada08fc8a70 | 57 | if (bytes) { |
mbedAustin | 11:cada08fc8a70 | 58 | dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; |
mbedAustin | 11:cada08fc8a70 | 59 | src += bytes; |
mbedAustin | 11:cada08fc8a70 | 60 | } |
mbedAustin | 11:cada08fc8a70 | 61 | |
mbedAustin | 11:cada08fc8a70 | 62 | if (bits) { |
mbedAustin | 11:cada08fc8a70 | 63 | uint_fast8_t split_bit = context_split_mask(bits); |
mbedAustin | 11:cada08fc8a70 | 64 | *dst = (*src & split_bit) | (*dst & ~ split_bit); |
mbedAustin | 11:cada08fc8a70 | 65 | } |
mbedAustin | 11:cada08fc8a70 | 66 | |
mbedAustin | 11:cada08fc8a70 | 67 | return dst; |
mbedAustin | 11:cada08fc8a70 | 68 | } |
mbedAustin | 11:cada08fc8a70 | 69 | |
mbedAustin | 11:cada08fc8a70 | 70 | uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) |
mbedAustin | 11:cada08fc8a70 | 71 | { |
mbedAustin | 11:cada08fc8a70 | 72 | uint_fast8_t bytes = bits / 8; |
mbedAustin | 11:cada08fc8a70 | 73 | bits %= 8; |
mbedAustin | 11:cada08fc8a70 | 74 | |
mbedAustin | 11:cada08fc8a70 | 75 | if (bytes) { |
mbedAustin | 11:cada08fc8a70 | 76 | dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; |
mbedAustin | 11:cada08fc8a70 | 77 | src += bytes; |
mbedAustin | 11:cada08fc8a70 | 78 | } |
mbedAustin | 11:cada08fc8a70 | 79 | |
mbedAustin | 11:cada08fc8a70 | 80 | if (bits) { |
mbedAustin | 11:cada08fc8a70 | 81 | uint_fast8_t split_bit = context_split_mask(bits); |
mbedAustin | 11:cada08fc8a70 | 82 | *dst = (*src & split_bit); |
mbedAustin | 11:cada08fc8a70 | 83 | } |
mbedAustin | 11:cada08fc8a70 | 84 | |
mbedAustin | 11:cada08fc8a70 | 85 | return dst; |
mbedAustin | 11:cada08fc8a70 | 86 | } |