Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers common_functions.c Source File

common_functions.c

00001 /*
00002  * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /*
00018  * Most functions can be inlined, and definitions are in common_functions.h.
00019  * Define COMMON_FUNCTIONS_FN before including it to generate external definitions.
00020  */
00021 
00022 #include "mbed_version.h"
00023 
00024 #if MBED_MAJOR_VERSION == 2
00025 
00026 #define COMMON_FUNCTIONS_FN extern
00027 
00028 #include "common_functions.h"
00029 
00030 #include <string.h>
00031 
00032 /* Returns mask for <split_value> (0-8) most-significant bits of a byte */
00033 static inline uint8_t context_split_mask(uint_fast8_t split_value)
00034 {
00035     return (uint8_t) - (0x100u >> split_value);
00036 }
00037 
00038 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits)
00039 {
00040     uint_fast8_t bytes = bits / 8;
00041     bits %= 8;
00042 
00043     if (bytes && memcmp(a, b, bytes)) {
00044         return false;
00045     }
00046 
00047     if (bits) {
00048         uint_fast8_t split_bit = context_split_mask(bits);
00049         if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) {
00050             return false;
00051         }
00052     }
00053 
00054     return true;
00055 }
00056 
00057 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
00058 {
00059     uint_fast8_t bytes = bits / 8;
00060     bits %= 8;
00061 
00062     if (bytes) {
00063         dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
00064         src += bytes;
00065     }
00066 
00067     if (bits) {
00068         uint_fast8_t split_bit = context_split_mask(bits);
00069         *dst = (*src & split_bit) | (*dst & ~ split_bit);
00070     }
00071 
00072     return dst;
00073 }
00074 
00075 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
00076 {
00077     uint_fast8_t bytes = bits / 8;
00078     bits %= 8;
00079 
00080     if (bytes) {
00081         dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
00082         src += bytes;
00083     }
00084 
00085     if (bits) {
00086         uint_fast8_t split_bit = context_split_mask(bits);
00087         *dst = (*src & split_bit);
00088     }
00089 
00090     return dst;
00091 }
00092 
00093 #endif