diff options
Diffstat (limited to 'CAN-binder')
20 files changed, 1591 insertions, 0 deletions
diff --git a/CAN-binder/libs/bitfield-c/.gitignore b/CAN-binder/libs/bitfield-c/.gitignore new file mode 100644 index 0000000..834a305 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/.gitignore @@ -0,0 +1,5 @@ +*.o +.DS_Store +*~ +*.bin +build diff --git a/CAN-binder/libs/bitfield-c/.travis.yml b/CAN-binder/libs/bitfield-c/.travis.yml new file mode 100644 index 0000000..7654aba --- /dev/null +++ b/CAN-binder/libs/bitfield-c/.travis.yml @@ -0,0 +1,7 @@ +language: c +compiler: + - gcc +script: make test +before_install: + - sudo apt-get update -qq + - sudo apt-get install check diff --git a/CAN-binder/libs/bitfield-c/CHANGELOG.mkd b/CAN-binder/libs/bitfield-c/CHANGELOG.mkd new file mode 100644 index 0000000..eea48c0 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/CHANGELOG.mkd @@ -0,0 +1,5 @@ +# Bitfield Utilities in C + +## v0.1 + +* Initial release diff --git a/CAN-binder/libs/bitfield-c/LICENSE b/CAN-binder/libs/bitfield-c/LICENSE new file mode 100644 index 0000000..330d61f --- /dev/null +++ b/CAN-binder/libs/bitfield-c/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2013 Ford Motor Company +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the <organization> nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/CAN-binder/libs/bitfield-c/Makefile b/CAN-binder/libs/bitfield-c/Makefile new file mode 100644 index 0000000..e93efb4 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/Makefile @@ -0,0 +1,53 @@ +CC = gcc +INCLUDES = -Isrc +CFLAGS = $(INCLUDES) -c -Wall -Werror -g -ggdb -coverage +LDFLAGS = -coverage -lm +LDLIBS = -lcheck + +TEST_DIR = tests +TEST_OBJDIR = build + +# Guard against \r\n line endings only in Cygwin +OSTYPE := $(shell uname) +ifneq ($(OSTYPE),Darwin) + OSTYPE := $(shell uname -o) + ifeq ($(OSTYPE),Cygwin) + TEST_SET_OPTS = igncr + endif +endif + +SRC = $(wildcard src/**/*.c) +OBJS = $(SRC:.c=.o) +OBJS = $(patsubst %,$(TEST_OBJDIR)/%,$(SRC:.c=.o)) +TEST_SRC = $(wildcard $(TEST_DIR)/*_tests.c) +TESTS=$(patsubst %.c,$(TEST_OBJDIR)/%.bin,$(TEST_SRC)) + +all: $(OBJS) + +test: $(TESTS) + @set -o $(TEST_SET_OPTS) >/dev/null 2>&1 + @export SHELLOPTS + @sh runtests.sh $(TEST_OBJDIR)/$(TEST_DIR) + +COVERAGE_INFO_FILENAME = coverage.info +COVERAGE_INFO_PATH = $(TEST_OBJDIR)/$(COVERAGE_INFO_FILENAME) +coverage: + @lcov --base-directory . --directory $(TEST_OBJDIR) --zerocounters -q + @make clean + @make test + @lcov --base-directory . --directory $(TEST_OBJDIR) -c -o $(TEST_OBJDIR)/coverage.info + @lcov --remove $(COVERAGE_INFO_PATH) "/usr/*" -o $(COVERAGE_INFO_PATH) + @genhtml -o $(TEST_OBJDIR)/coverage -t "isotp-c test coverage" --num-spaces 4 $(COVERAGE_INFO_PATH) + @$(BROWSER) $(TEST_OBJDIR)/coverage/index.html + @echo "$(GREEN)Coverage information generated in $(TEST_OBJDIR)/coverage/index.html.$(COLOR_RESET)" + +$(TEST_OBJDIR)/%.o: %.c + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(CC_SYMBOLS) $(INCLUDES) -o $@ $< + +$(TEST_OBJDIR)/%.bin: $(TEST_OBJDIR)/%.o $(OBJS) $(TEST_SUPPORT_OBJS) + @mkdir -p $(dir $@) + $(CC) $(LDFLAGS) $(CC_SYMBOLS) $(INCLUDES) -o $@ $^ $(LDLIBS) + +clean: + rm -rf $(TEST_OBJDIR) diff --git a/CAN-binder/libs/bitfield-c/README.mkd b/CAN-binder/libs/bitfield-c/README.mkd new file mode 100644 index 0000000..439b3dd --- /dev/null +++ b/CAN-binder/libs/bitfield-c/README.mkd @@ -0,0 +1,107 @@ +Bitfield Utilities in C +=========================== + +This is a C library with functions to help encode and decode Controller Area +Network (CAN) message payloads or other bitfields. + +The header files contain complete function documentation, but to get you +started, here are examples using the API: + +## Bitfield Manipulation + +The bitfields are stored in `uint8_t[]`. + + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result = get_byte(data, sizeof(data), 0); + // result = 0x12; + result = get_nibble(data, sizeof(data), 0); + // result = 0x1; + bool success = copy_bits_right_aligned(data, 4, 4, 12, result, 4) + // success == true + // result[0] == 0x2 + // result[1] == 0x34 + +## 8 Byte Helpers + +If you are dealing with 8 byte CAN messages as `uint64_t`, there are some +additional functions prefixed with `eightbyte_` that may be faster or more +useful. + +### 8 Byte Decoding + + uint64_t data = 0x8000000000000000; + uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); + // result == 0x1 + + data = 0x0402574d555a0401; + result = eightbyte_get_bitfield(data, 16, 32, false); + // result = 0x574d555a; + + data = 0x00000000F34DFCFF; + result = eightbyte_get_byte(data, 0, false); + //result = 0x0 + + result = eightbyte_get_byte(data, 4, false); + //result = 0xF3 + + result = eightbyte_get_nibble(data, 10, false); + //result = 0x4; + +### 8 Byte Encoding + + uint64_t data = 0; + fail_unless(8byte_set_bitfield(1, 0, 1, &data)); + uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); + ck_assert_int_eq(result, 0x1); + +### CAN Signal Encoding + +The library supports encoding floating point CAN signals as well as booleans +into a uint64_t payload. + + uint64_t payload = eightbyte_encode_float(1, 1, 3, 1, 0) + // payload == 0x1000000000000000 + + payload = eightbyte_encode_bool(true, 1, 3); + // payload == 0x1000000000000000 + +### CAN Signal Decoding + +The library supports parsing floating point CAN signals as well as booleans. + + uint64_t payload = 0xeb00000000000000; + float float_result = eightbyte_parse_float(payload, + 2, // starting bit + 4, // width of the signal's field + 1001.0, // transformation factor for the signal value + -30000.0); // transformation offset for the signal value + // float_result == -19990.0 + + bool bool_result = eightbyte_parse_bool(payload, + 0, // starting bit + 1, // width of the signal's field + 1.0, // transformation factor for the signal value + 0); // transformation offset for the signal value + // bool_result == true + +## Testing + +The library includes a test suite that uses the `check` C unit test library. It +requires the unit testing library `check`. + + $ make test + +You can also see the test coverage if you have `lcov` installed and the +`BROWSER` environment variable set to your choice of web browsers: + + $ BROWSER=google-chrome-stable make coverage + +## Authors + +Chris Peplin cpeplin@ford.com + +## License + +Copyright (c) 2013 Ford Motor Company + +Licensed under the BSD license. diff --git a/CAN-binder/libs/bitfield-c/runtests.sh b/CAN-binder/libs/bitfield-c/runtests.sh new file mode 100644 index 0000000..4781636 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/runtests.sh @@ -0,0 +1,17 @@ +echo "Running unit tests:" + +for i in $1/*.bin +do + if test -f $i + then + if ./$i + then + echo $i PASS + else + echo "ERROR in test $i:" + exit 1 + fi + fi +done + +echo "${txtbld}$(tput setaf 2)All unit tests passed.$(tput sgr0)" diff --git a/CAN-binder/libs/bitfield-c/src/bitfield/8byte.c b/CAN-binder/libs/bitfield-c/src/bitfield/8byte.c new file mode 100644 index 0000000..9325ed1 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/bitfield/8byte.c @@ -0,0 +1,61 @@ +#include <bitfield/bitfield.h> +#include <bitfield/8byte.h> +#include <stddef.h> +#include <limits.h> +#include <string.h> + +#define EIGHTBYTE_BIT (8 * sizeof(uint64_t)) + +uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, + const bool data_is_big_endian) { + return (uint8_t) eightbyte_get_bitfield(source, NIBBLE_SIZE * nibble_index, + NIBBLE_SIZE, data_is_big_endian); +} + +uint8_t eightbyte_get_byte(uint64_t source, const uint8_t byte_index, + const bool data_is_big_endian) { + if(data_is_big_endian) { + source = __builtin_bswap64(source); + } + return (source >> (EIGHTBYTE_BIT - ((byte_index + 1) * CHAR_BIT))) & 0xFF; +} + +// TODO is this funciton necessary anymore? is it any faster for uint64_t than +// get_bitfield(data[], ...)? is the performance better on a 32 bit platform +// like the PIC32? +uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, + const uint16_t bit_count, const bool data_is_big_endian) { + int startByte = offset / CHAR_BIT; + int endByte = (offset + bit_count - 1) / CHAR_BIT; + + if(!data_is_big_endian) { + source = __builtin_bswap64(source); + } + + uint8_t* bytes = (uint8_t*)&source; + uint64_t ret = bytes[startByte]; + if(startByte != endByte) { + // The lowest byte address contains the most significant bit. + uint8_t i; + for(i = startByte + 1; i <= endByte; i++) { + ret = ret << 8; + ret = ret | bytes[i]; + } + } + + ret >>= 8 - find_end_bit(offset + bit_count); + return ret & bitmask(bit_count); +} + +bool eightbyte_set_bitfield(uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint64_t* destination) { + if(value > bitmask(bit_count)) { + return false; + } + + int shiftDistance = EIGHTBYTE_BIT - offset - bit_count; + value <<= shiftDistance; + *destination &= ~(bitmask(bit_count) << shiftDistance); + *destination |= value; + return true; +} diff --git a/CAN-binder/libs/bitfield-c/src/bitfield/8byte.h b/CAN-binder/libs/bitfield-c/src/bitfield/8byte.h new file mode 100644 index 0000000..0451269 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/bitfield/8byte.h @@ -0,0 +1,88 @@ +#ifndef __8BYTE_H__ +#define __8BYTE_H__ + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public: Reads a subset of bits into a uint64_t. + * + * source - the bytes in question. + * offset - the starting index of the bit field (beginning from 0). + * bit_count - the width of the bit field to extract. + * data_is_big_endian - if the data passed in is little endian, set this to false and it + * will be flipped before grabbing the bit field. + * + * Bit fields are positioned according to big-endian bit layout. + * + * For example, the bit layout of the value "42" (i.e. 00101010 set at position + * 14 with length 6 is: + * + * 000000000000001010100000000000000000000000000000000000000000000 + * + * and the same value and position but with length 8 is: + * + * 000000000000000010101000000000000000000000000000000000000000000 + * + * If the architecture where is code is running is little-endian, the input data + * will be swapped before grabbing the bit field. + * + * Examples + * + * uint64_t value = get_bitfield(data, 2, 4); + * + * Returns the value of the requested bit field, right aligned in a uint64_t. + */ +uint64_t eightbyte_get_bitfield(uint64_t source, const uint16_t offset, + const uint16_t bit_count, const bool data_is_big_endian); + +/* Public: Return a single nibble from the payload, with range checking. + * + * source - the source payload. + * nibble_index - the index of the nibble to retreive. The leftmost nibble is + * index 0. + * data_is_big_endian - if the data passed in is little endian, set this to false and it + * will be flipped before grabbing the bit field. + * + * Returns the retreived nibble, right aligned in a uint8_t. + */ +uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index, + const bool data_is_big_endian); + +/* Public: Return a single byte from the payload, with range checking. + * + * source - the source byte array. + * byte_index - the index of the byte to retreive. The leftmost byte is index 0. + * data_is_big_endian - if the data passed in is little endian, set this to false and it + * will be flipped before grabbing the bit field. + * + * Returns the retreived byte. + */ +uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index, + const bool data_is_big_endian); + +/* Public: Set the bit field in the given data array to the new value. + * + * destination - a byte array with size at least offset + bit_count. + * value - the value to set in the bit field. + * offset - the starting index of the bit field (beginning from 0). + * bit_count - the number of bits to set in the data. + * + * Returns true if the bit_count is enough to fully represent the value, and + * false if it will not fit. + */ +bool eightbyte_set_bitfield(uint64_t value, + const uint16_t offset, const uint16_t bit_count, uint64_t* destination); + +/* Private: Determine the index of the last bit used. + */ +uint8_t find_end_bit(const uint16_t num_bits); + +#ifdef __cplusplus +} +#endif + +#endif // __8BYTE_H__ diff --git a/CAN-binder/libs/bitfield-c/src/bitfield/bitarray.c b/CAN-binder/libs/bitfield-c/src/bitfield/bitarray.c new file mode 100644 index 0000000..dcb9a08 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/bitfield/bitarray.c @@ -0,0 +1,145 @@ +#include <bitfield/bitfield.h> +#include <stddef.h> +#include <limits.h> +#include <string.h> + +#define PREPARE_FIRST_COPY() \ + do { \ + if (bit_count >= (CHAR_BIT - destination_offset_modulo)) { \ + *destination &= reverse_mask[destination_offset_modulo]; \ + bit_count -= CHAR_BIT - destination_offset_modulo; \ + } else { \ + *destination &= reverse_mask[destination_offset_modulo] \ + | reverse_mask_xor[destination_offset_modulo + bit_count + 1];\ + c &= reverse_mask[destination_offset_modulo + bit_count ];\ + bit_count = 0; \ + } } while (0) + +static const uint8_t reverse_mask[] = + { 0x55, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; +static const uint8_t reverse_mask_xor[] = + { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 }; + +bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, + const uint16_t source_offset, uint16_t bit_count, + uint8_t* destination_origin, const uint16_t destination_length, + const uint16_t destination_offset) { + if(bit_count < 1) { + return false; + } + + if(source_offset + bit_count > source_length * CHAR_BIT || + destination_offset + bit_count > destination_length * CHAR_BIT ) { + return false; + } + + const uint8_t* source = source_origin + (source_offset / CHAR_BIT); + uint8_t* destination = destination_origin + (destination_offset / CHAR_BIT); + int source_offset_modulo = source_offset % CHAR_BIT; + int destination_offset_modulo = destination_offset % CHAR_BIT; + + if(source_offset_modulo == destination_offset_modulo) { + if(source_offset_modulo > 0) { + uint8_t c = reverse_mask_xor[destination_offset_modulo] & *source++; + PREPARE_FIRST_COPY(); + *destination++ |= c; + } + + int byte_len = bit_count / CHAR_BIT; + int bit_count_modulo = bit_count % CHAR_BIT; + + if(byte_len > 0) { + memcpy(destination, source, byte_len); + source += byte_len; + destination += byte_len; + } + + if(bit_count_modulo > 0) { + *destination &= reverse_mask_xor[bit_count_modulo]; + *destination |= reverse_mask[bit_count_modulo] & *source; + } + } else { + int bit_diff_left_shift; + int bit_diff_right_shift; + uint8_t c; + /* + * Begin: Line things up on destination. + */ + if(source_offset_modulo > destination_offset_modulo) { + bit_diff_left_shift = source_offset_modulo - destination_offset_modulo; + bit_diff_right_shift = CHAR_BIT - bit_diff_left_shift; + + c = *source++ << bit_diff_left_shift; + c |= *source >> bit_diff_right_shift; + c &= reverse_mask_xor[destination_offset_modulo]; + } else { + bit_diff_right_shift = destination_offset_modulo - source_offset_modulo; + bit_diff_left_shift = CHAR_BIT - bit_diff_right_shift; + + c = *source >> bit_diff_right_shift & + reverse_mask_xor[destination_offset_modulo]; + } + PREPARE_FIRST_COPY(); + *destination++ |= c; + + /* + * Middle: copy with only shifting the source. + */ + int byte_len = bit_count / CHAR_BIT; + while(--byte_len >= 0) { + c = *source++ << bit_diff_left_shift; + c |= *source >> bit_diff_right_shift; + *destination++ = c; + } + + /* + * End: copy the remaing bits; + */ + int bit_count_modulo = bit_count % CHAR_BIT; + if(bit_count_modulo > 0) { + c = *source++ << bit_diff_left_shift; + c |= *source >> bit_diff_right_shift; + c &= reverse_mask[bit_count_modulo]; + + *destination &= reverse_mask_xor[bit_count_modulo]; + *destination |= c; + } + } + return true; +} + +uint16_t bits_to_bytes(uint32_t bits) { + uint8_t byte_count = bits / CHAR_BIT; + if(bits % CHAR_BIT != 0) { + ++byte_count; + } + return byte_count; +} + +/** + * Find the ending bit of a bitfield within the final byte. + * + * Returns: a bit position from 0 to 7. + */ +uint8_t find_end_bit(const uint16_t numBits) { + int endBit = numBits % CHAR_BIT; + return endBit == 0 ? CHAR_BIT : endBit; +} + +bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length, + const uint16_t offset, const uint16_t bit_count, + uint8_t* destination, const uint16_t destination_length) { + return copy_bits(source, source_length, offset, bit_count, destination, + destination_length, + // provide a proper destination offset so the result is right + // aligned + (destination_length - bits_to_bytes(bit_count)) * CHAR_BIT + + CHAR_BIT - find_end_bit(bit_count)); +} + +bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_length, + const uint16_t offset, const uint16_t byte_count, + uint8_t* destination, const uint16_t destination_length) { + return copy_bits_right_aligned(source, source_length, offset * CHAR_BIT, + byte_count * CHAR_BIT, destination, destination_length); +} diff --git a/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.c b/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.c new file mode 100644 index 0000000..795f020 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.c @@ -0,0 +1,74 @@ +#include <bitfield/bitfield.h> +#include <limits.h> +#include <string.h> +#include <stddef.h> +#include <sys/param.h> + +uint64_t bitmask(const uint8_t bit_count) { + return (((uint64_t)0x1) << bit_count) - 1; +} + +uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, + const uint8_t nibble_index) { + uint8_t byte_index = nibble_index / 2; + uint8_t result = get_byte(source, source_length, byte_index); + if(nibble_index % 2 == 0) { + result >>= NIBBLE_SIZE; + } + result &= bitmask(NIBBLE_SIZE); + return result; +} + +uint8_t get_byte(const uint8_t source[], const uint8_t source_length, + const uint8_t byte_index) { + if(byte_index < source_length) { + return source[byte_index]; + } + return 0; +} + +uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, + const uint16_t offset, const uint16_t bit_count) { + if(bit_count > 64 || bit_count < 1) { + // TODO error reporting? + return 0; + } + + ArrayOrBytes combined; + memset(combined.bytes, 0, sizeof(combined.bytes)); + if(copy_bits_right_aligned(source, source_length, offset, bit_count, + combined.bytes, sizeof(combined.bytes))) { + if(BYTE_ORDER == LITTLE_ENDIAN) { + combined.whole = __builtin_bswap64(combined.whole); + } + } else { + // debug("couldn't copy enough bits from source") + } + return combined.whole; +} + +bool set_nibble(const uint16_t nibble_index, const uint8_t value, + uint8_t* destination, const uint16_t destination_length) { + return copy_bits(&value, CHAR_BIT, NIBBLE_SIZE, NIBBLE_SIZE, destination, + destination_length, nibble_index * NIBBLE_SIZE); +} + +bool set_bitfield(const uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint8_t destination[], + uint16_t destination_length) { + if(value > bitmask(bit_count)) { + return false; + } + + ArrayOrBytes combined = { + whole: value + }; + + if(BYTE_ORDER == LITTLE_ENDIAN) { + combined.whole = __builtin_bswap64(combined.whole); + } + + return copy_bits(combined.bytes, sizeof(combined.bytes), + sizeof(combined.bytes) * CHAR_BIT - bit_count, bit_count, + destination, destination_length, offset); +} diff --git a/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.h b/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.h new file mode 100644 index 0000000..df92639 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/bitfield/bitfield.h @@ -0,0 +1,220 @@ +#ifndef __BITFIELD_H__ +#define __BITFIELD_H__ + +#include <stdint.h> +#include <stdbool.h> + +#define NIBBLE_SIZE (CHAR_BIT / 2) + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public: Reads a subset of bits into a uint64_t, right aligned so they may be + * interpreted as a number. + * + * source - the bytes in question. + * source_size - the number of bytes in the source. + * offset - the starting index of the bit field (beginning from 0). + * bit_count - the width of the bit field to extract. This must be less than or + * equal to 64. + * + * Bit fields are positioned according to big-endian bit layout and the data is + * swapped automatically as necessary depending on the compiled architecture. + * + * For example, the bit layout of the value "42" (i.e. 00101010 set at position + * 14 with length 6 is: + * + * 000000000000001010100000000000000000000000000000000000000000000 + * + * and the same value and position but with length 8 is: + * + * 000000000000000010101000000000000000000000000000000000000000000 + * + * Examples + * + * uint64_t value = get_bitfield(data, data_size, 2, 4); + * + * Returns the value of the requested bit field, right aligned in a uint64_t. + */ +uint64_t get_bitfield(const uint8_t source[], const uint8_t source_length, + const uint16_t offset, const uint16_t bit_count); + +/* Public: Return a single nibble from the byte array, with range checking. + * + * source - the source byte array. + * source_length - the total length of the source array. + * nibble_index - the index of the nibble to retreive. The leftmost nibble is + * index 0. + * + * Returns the retreived nibble, right aligned in a uint8_t. + */ +uint8_t get_nibble(const uint8_t source[], const uint8_t source_length, + const uint8_t nibble_index); + +/* Public: Return a single byte from the byte array, with range checking. + * + * source - the source byte array. + * source_length - the total length of the source array. + * byte_index - the index of the byte to retreive. The leftmost byte is index 0. + * + * Returns the retreived byte. + */ +uint8_t get_byte(const uint8_t source[], const uint8_t source_length, + const uint8_t byte_index); + +/* Public: Copy a range of bits from one bit array to another. + * + * The range does not need to be byte aligned, and the source and destination do + * not have to be the same size (as long as the desitnation has enough room to + * fit the range). + * + * A bit array with regards to this function always has the leftmost bit in byte + * 0, i.e. bit index is the leftmost bit of byte 0. Endianness does not matter. + * + * For example: + * + * uint8_t source[4] = {0x11, 0x22, 0x33, 0x44}; + * uint8_t destination[4] = {0}; + * copy_bits(source, sizeof(source), 8, 8, destination, + * sizeof(destination), 0); + * // destination[0] == 0x22 + * // destination[1] == 0x0 + * // destination[2] == 0x0 + * // destination[3] == 0x0 + * + * Thanks to + * http://stackoverflow.com/questions/3534535/whats-a-time-efficient-algorithm-to-copy-unaligned-bit-arrays + * for the implementation of the algorithm. + * + * source_origin - the source array. + * source_length - the total length of the source array in bytes, + * for range checking. + * source_offset - an offset in bits to start the copy from the source array. + * Specify 0 to start from source_origin. + * bit_count - the number of bits to copy. + * destination_origin - the destination array. + * desitnation_length - the total length of the destination array in bytes, + * for range checking. + * destination_offset - an offset in bits to start placing the copied range into + * the destination array. Specify 0 to start from the beginning of the + * destination. If you are copying a range not aligned on a byte, you + * probably want to set this to a positive offset to right the resulting + * bits in the destination. + * + * Returns true if the copy was successful and false if the range exceeded the + * size of the source or destination, or if the range size negative or 0. + */ +bool copy_bits(const uint8_t* source_origin, const uint16_t source_length, + const uint16_t source_offset, uint16_t bit_count, + uint8_t* destination_origin, const uint16_t destination_length, + const uint16_t destination_offset); + +/* Public: Copy a range of bits from one array to another, right aligning the + * result. + * + * This is mostly useful if you want to cast the result to an integer type + * instead of a byte array. + * + * For example: + * + * uint8_t source[4] = {0x11, 0x22, 0x33, 0x44}; + * uint8_t destination[4] = {0}; + * copy_bits_right_aligned(source, sizeof(source), 8, 8, destination, + * sizeof(destination)); + * // destination[0] == 0x0 + * // destination[1] == 0x0 + * // destination[2] == 0x0 + * // destination[3] == 0x22 + * + * int value = (int)destination; + * // value == 0x22 == 32 + * + * The arguments are the same as copy_bits, but without the destination_offset + * option - that's set automatically to right align the result. + * + * Returns true if the copy was successful and false if the range exceeded the + * size of the source or destination, or if the range size negative or 0. + */ +bool copy_bits_right_aligned(const uint8_t source[], const uint16_t source_length, + const uint16_t offset, const uint16_t bit_count, + uint8_t* destination, const uint16_t destination_length); + +/* Public: Copy a range of bytes from one byte array to another. + * + * The source and destination do not have to be the same size (as long as the + * desitnation has enough room to fit the range). + * + * source_origin - the source array. + * source_length - the total length of the source array in bytes, + * for range checking. + * source_offset - a byte offset to start the copy from the source array. + * Specify 0 to start from source_origin. + * byte_count - the number of bytes to copy. + * destination_origin - the destination array. + * desitnation_length - the total length of the destination array in bytes, + * for range checking. + * destination_offset - an offset in bytes to start placing the copied range into + * the destination array. Specify 0 to start from the beginning of the + * destination. + * + * Returns true if the copy was successful and false if the range exceeded the + * size of the source or destination, or if the range size negative or 0. + */ +bool copy_bytes_right_aligned(const uint8_t source[], const uint16_t source_length, + const uint16_t offset, const uint16_t byte_count, + uint8_t* destination, const uint16_t destination_length); + +/* Public: Set the a nibble in the given data array to the new value. + * + * nibble_index - the index of the nibble to retreive. The leftmost nibble is + * index 0. + * value - the value to set in the bit field. + * destination - the destination array. + * destination_length - the total length of the destination array in bytes, + * for range checking. + * + * Returns true if the bit_count is enough to fully represent the value, and + * false if it will not fit. + */ +bool set_nibble(const uint16_t nibble_index, const uint8_t value, + uint8_t* destination, const uint16_t destination_length); + +/* Public: Set the bit field in the given data array to the new value. + * + * value - the value to set in the bit field. + * offset - the starting index of the bit field (beginning from 0). + * bit_count - the number of bits to set in the data. + * destination - the destination array. + * destination_length - the total length of the destination array in bytes, + * for range checking. + * + * Returns true if the bit_count is enough to fully represent the value, and + * false if it will not fit. + */ +bool set_bitfield(const uint64_t value, const uint16_t offset, + const uint16_t bit_count, uint8_t destination[], + uint16_t destination_length); + +/* Public: Return a right aligned bitmask for a uint64_t. + * + * bit_count - the number of bits to mask, right aligned. + */ +uint64_t bitmask(const uint8_t bit_count); + +/* Private: + */ +uint16_t bits_to_bytes(uint32_t bits); + +/* Private: A union to assist swapping between uint64_t and a uint8_t array. + */ +typedef union { + uint64_t whole; + uint8_t bytes[sizeof(uint64_t)]; +} ArrayOrBytes; + +#ifdef __cplusplus +} +#endif + +#endif // __BITFIELD_H__ diff --git a/CAN-binder/libs/bitfield-c/src/canutil/read.c b/CAN-binder/libs/bitfield-c/src/canutil/read.c new file mode 100644 index 0000000..d0cbb71 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/canutil/read.c @@ -0,0 +1,34 @@ +#include <canutil/read.h> +#include <bitfield/bitfield.h> +#include <bitfield/8byte.h> + +static float decode_float(uint64_t raw, float factor, float offset) { + return raw * factor + offset; +} + +float eightbyte_parse_float(uint64_t data, uint8_t bit_offset, uint8_t bit_size, + float factor, float offset) { + return decode_float(eightbyte_get_bitfield(data, bit_offset, bit_size, + true), factor, offset); +} + +bool eightbyte_parse_bool(uint64_t data, uint8_t bit_offset, uint8_t bit_size, + float factor, float offset) { + float value = eightbyte_parse_float(data, bit_offset, bit_size, factor, offset); + return value == 0.0 ? false : true; +} + +float bitfield_parse_float(const uint8_t source[], const uint16_t source_length, + const uint8_t bit_offset, const uint8_t bit_size, const float factor, + const float offset) { + return decode_float(get_bitfield(source, source_length, bit_offset, bit_size), + factor, offset); +} + +bool bitfield_parse_bool(const uint8_t source[], const uint16_t source_length, + const uint8_t bit_offset, const uint8_t bit_size, const float factor, + const float offset) { + float value = bitfield_parse_float(source, source_length, bit_offset, + bit_size, factor, offset); + return value == 0.0 ? false : true; +} diff --git a/CAN-binder/libs/bitfield-c/src/canutil/read.h b/CAN-binder/libs/bitfield-c/src/canutil/read.h new file mode 100644 index 0000000..86fea78 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/canutil/read.h @@ -0,0 +1,81 @@ +#ifndef __READ_H__ +#define __READ_H__ + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public: Parse a CAN signal from a message and apply required transformation. + * + * source - the payload containing the signal. + * bit_offset - the starting bit for the signal. + * bit_size - the width of the signal. + * factor - the transformation factor for the signal value, applied after + * pulling out the bit field. Use 1.0 for no factor. + * offset - the transformation offset for the signal value, applied after + * pulling out the bit field. Use 0 for no offset. + * + * Returns the decoded and transformed value of the signal. + */ +float eightbyte_parse_float(const uint64_t source, const uint8_t bit_offset, + const uint8_t bit_size, const float factor, const float offset); + +/* Public: Parse a CAN signal from a message storage as a byte array and apply + * required transformation. + * + * source - the payload containing the signal. + * source_size - the size of the payload in bytes. + * bit_offset - the starting bit for the signal. + * bit_size - the width of the signal. + * factor - the transformation factor for the signal value, applied after + * pulling out the bit field. Use 1.0 for no factor. + * offset - the transformation offset for the signal value, applied after + * pulling out the bit field. Use 0 for no offset. + * + * Returns the decoded and transformed value of the signal. + */ +float bitfield_parse_float(const uint8_t source[], const uint16_t source_size, + const uint8_t bit_offset, const uint8_t bit_size, const float factor, + const float offset); + +/* Public: Parse a CAN signal from a message and interpret it as a boolean. + * + * source - the payload containing the signal. + * bit_offset - the starting bit for the signal. + * bit_size - the width of the signal. + * factor - the transformation factor for the signal value, applied after + * pulling out the bit field. Use 1.0 for no factor. + * offset - the transformation offset for the signal value, applied after + * pulling out the bit field. Use 0 for no offset. + * + * Returns false if the value was 0, otherwise true. + */ +bool eightbyte_parse_bool(uint64_t source, uint8_t bit_offset, uint8_t bit_size, + float factor, float offset); + +/* Public: Parse a CAN signal from a message storage as a byte array and + * interpret it as a boolean. + * + * source - the payload containing the signal. + * source_size - the size of the payload in bytes. + * bit_offset - the starting bit for the signal. + * bit_size - the width of the signal. + * factor - the transformation factor for the signal value, applied after + * pulling out the bit field. Use 1.0 for no factor. + * offset - the transformation offset for the signal value, applied after + * pulling out the bit field. Use 0 for no offset. + * + * Returns false if the value was 0, otherwise true. + */ +bool bitfield_parse_bool(const uint8_t source[], const uint16_t source_size, + const uint8_t bit_offset, const uint8_t bit_size, const float factor, + const float offset); + +#ifdef __cplusplus +} +#endif + +#endif // __READ_H__ diff --git a/CAN-binder/libs/bitfield-c/src/canutil/write.c b/CAN-binder/libs/bitfield-c/src/canutil/write.c new file mode 100644 index 0000000..7f3a3e0 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/canutil/write.c @@ -0,0 +1,48 @@ +#include <canutil/write.h> +#include <bitfield/bitfield.h> +#include <bitfield/8byte.h> + +uint64_t float_to_fixed_point(const float value, const float factor, + const float offset) { + float raw = (value - offset) / factor; + if(raw > 0) { + // round up to avoid losing precision when we cast to an int + // TODO do we need a way to encode an int back to a signal without any + // rounding? + raw += 0.5; + } + return (uint64_t)raw; +} + +uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size, + float factor, float offset) { + uint64_t result = 0; + if(!eightbyte_set_bitfield(float_to_fixed_point(value, factor, offset), + bit_offset, bit_size, &result)) { + // debug("%f will not fit in a %d bit field", value, bit_size); + } + return result; +} + +uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset, + const uint8_t bit_size) { + return eightbyte_encode_float(value, bit_offset, bit_size, 1.0, 0); +} + +bool bitfield_encode_float(const float value, const uint8_t bit_offset, + const uint8_t bit_size, const float factor, const float offset, + uint8_t destination[], const uint8_t destination_length) { + if(!set_bitfield(float_to_fixed_point(value, factor, offset), bit_offset, + bit_size, destination, destination_length)) { + // debug("%f will not fit in a %d bit field", value, bit_size); + return false; + } + return true; +} + +bool bitfield_encode_bool(const bool value, const uint8_t bit_offset, + const uint8_t bit_size, uint8_t destination[], + const uint16_t destination_length) { + return bitfield_encode_float(value, bit_offset, bit_size, 1.0, 0, + destination, destination_length); +} diff --git a/CAN-binder/libs/bitfield-c/src/canutil/write.h b/CAN-binder/libs/bitfield-c/src/canutil/write.h new file mode 100644 index 0000000..c2bef20 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/src/canutil/write.h @@ -0,0 +1,57 @@ +#ifndef __WRITE_H__ +#define __WRITE_H__ + +#include <stdint.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* Public: Encode a floating point number into a fixed point, fixed bit width + * field in a bit array. + * + * value - the floating point value to encode. + * bit_offset - the starting point for the encoded bits in the returned value. + * bit_size - The max width of the field in the resulting bit array. If bit_size + * isn't big enough to store the fixed point version of the value, the + * bitfeld will *not* be set. TODO some error reporting would be nice. + * factor - a factor used to transform from floating to fixed point before + * encoding. Use 1.0 for no factor. + * offset - an offset used to transform from floating to fixed point before + * encoding. Use 0 for no offset. + * + * Returns a big-endian uint64_t with the value encoded as a bitfield. + */ +uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, + uint8_t bit_size, float factor, float offset); + +uint64_t float_to_fixed_point(const float value, const float factor, + const float offset); + +bool bitfield_encode_float(const float value, const uint8_t bit_offset, + const uint8_t bit_size, const float factor, const float offset, + uint8_t destination[], const uint8_t destination_length); + +/* Public: Encode a boolean into fixed bit width field in a bit array. + * + * value - the boolean value to encode - true will be 1, false will be 0. + * bit_offset - the starting point for the encoded bits in the returned value. + * bit_size - The max width of the field in the resulting bit array. If bit_size + * isn't big enough to store the fixed point version of the value, the + * bitfeld will *not* be set. TODO some error reporting would be nice. + * + * Returns a big-endian uint64_t with the value encoded as a bitfield. + */ +uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset, + const uint8_t bit_size); + +bool bitfield_encode_bool(const bool value, const uint8_t bit_offset, const + uint8_t bit_size, uint8_t destination[], + const uint16_t destination_length); + +#ifdef __cplusplus +} +#endif + +#endif // __WRITE_H__ diff --git a/CAN-binder/libs/bitfield-c/tests/8byte_tests.c b/CAN-binder/libs/bitfield-c/tests/8byte_tests.c new file mode 100644 index 0000000..64554ac --- /dev/null +++ b/CAN-binder/libs/bitfield-c/tests/8byte_tests.c @@ -0,0 +1,261 @@ +#include <check.h> +#include <stdint.h> +#include <bitfield/bitfield.h> +#include <bitfield/8byte.h> + +START_TEST (test_large_bitmask) +{ + uint64_t result = bitmask(32); + fail_if(result != 0xffffffff); +} +END_TEST + +START_TEST (test_one_bit_not_swapped) +{ + uint64_t data = 0x80; + uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); + fail_if(result == 1); +} +END_TEST + +START_TEST (test_one_bit) +{ + uint64_t data = 0x8000000000000000; + uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); + fail_unless(result == 0x1, + "First bit in 0x%llx was 0x%llx instead of 0x1", data, result); +} +END_TEST + +START_TEST (test_32_bit_parse) +{ + uint64_t data = 0x0402574d555a0401; + uint64_t result = eightbyte_get_bitfield(data, 16, 32, false); + uint64_t expectedValue = 0x574d555a; + fail_unless(result == expectedValue, + "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data, + result, expectedValue); +} +END_TEST + +START_TEST (test_16_bit_parse) +{ + uint64_t data = 0xF34DFCFF00000000; + uint64_t result = eightbyte_get_bitfield(data, 16, 16, false); + uint64_t expectedValue = 0xFCFF; + fail_unless(result == expectedValue, + "Field retrieved in 0x%llx was 0x%llx instead of 0x%llx", data, + result, expectedValue); +} +END_TEST + +START_TEST (test_one_byte) +{ + uint64_t data = 0xFA00000000000000; + uint64_t result = eightbyte_get_bitfield(data, 0, 4, false); + fail_unless(result == 0xF, + "First nibble in 0x%llx was 0x%llx instead of 0xF", data, result); + result = eightbyte_get_bitfield(data, 4, 4, false); + fail_unless(result == 0xA, + "Second nibble in 0x%llx was 0x%llx instead of 0xA", data, result); + result = eightbyte_get_bitfield(data, 0, 8, false); + fail_unless(result == 0xFA, + "All bits in 0x%llx were 0x%llx instead of 0x%llx", data, result, data); +} +END_TEST + +START_TEST (test_multi_byte) +{ + uint64_t data = 0x12FA000000000000; + uint64_t result = eightbyte_get_bitfield(data, 0, 4, false); + fail_unless(result == 0x1, + "First 4 bits in 0x%llx was 0x%llx instead of 0xF", (data >> 60) & 0xF, + result); + result = eightbyte_get_bitfield(data, 4, 4, false); + fail_unless(result == 0x2, + "Second 4 bits in 0x%llx was 0x%llx instead of 0xA", (data >> 56) & 0xF, + result); + result = eightbyte_get_bitfield(data, 8, 4, false); + fail_unless(result == 0xF, + "First 4 bits in 0x%llx was 0x%llx instead of 0x1", (data >> 52) & 0xF, + result); + result = eightbyte_get_bitfield(data, 12, 4, false); + fail_unless(result == 0xA, + "Second 4 bits in 0x%llx was 0x%llx instead of 0x2", (data >> 48) % 0xF, + result); +} +END_TEST + +START_TEST (test_get_multi_byte) +{ + uint64_t data = 0x12FA000000000000; + uint64_t result = eightbyte_get_bitfield(data, 0, 9, false); + ck_assert_int_eq(result, 0x25); +} +END_TEST + +START_TEST (test_get_off_byte_boundary) +{ + uint64_t data = 0x000012FA00000000; + uint64_t result = eightbyte_get_bitfield(data, 12, 8, false); + ck_assert_int_eq(result, 0x01); +} END_TEST + +START_TEST (test_set_wont_fit) +{ + uint64_t data = 0; + fail_if(eightbyte_set_bitfield(100, 0, 1, &data)); +} +END_TEST + +START_TEST (test_set_field) +{ + uint64_t data = 0; + fail_unless(eightbyte_set_bitfield(1, 0, 1, &data)); + uint64_t result = eightbyte_get_bitfield(data, 0, 1, false); + ck_assert_int_eq(result, 0x1); + data = 0; + fail_unless(eightbyte_set_bitfield(1, 1, 1, &data)); + result = eightbyte_get_bitfield(data, 1, 1, false); + ck_assert_int_eq(result, 0x1); + + data = 0; + fail_unless(eightbyte_set_bitfield(0xf, 3, 4, &data)); + result = eightbyte_get_bitfield(data, 3, 4, false); + ck_assert_int_eq(result, 0xf); +} +END_TEST + +START_TEST (test_set_doesnt_clobber_existing_data) +{ + uint64_t data = 0xFFFC4DF300000000; + fail_unless(eightbyte_set_bitfield(0x4fc8, 16, 16, &data)); + uint64_t result = eightbyte_get_bitfield(data, 16, 16, false); + fail_unless(result == 0x4fc8, + "Field retrieved in 0x%llx was 0x%llx instead of 0x%x", data, result, + 0xc84f); + + data = 0x8000000000000000; + fail_unless(eightbyte_set_bitfield(1, 21, 1, &data)); + fail_unless(data == 0x8000040000000000LLU, + "Expected combined value 0x8000040000000000 but got 0x%llx%llx", + data >> 32, data); +} +END_TEST + +START_TEST (test_set_off_byte_boundary) +{ + uint64_t data = 0xFFFC4DF300000000; + fail_unless(eightbyte_set_bitfield(0x12, 12, 8, &data)); + uint64_t result = eightbyte_get_bitfield(data, 12, 12, false); + ck_assert_int_eq(result,0x12d); +} +END_TEST + +START_TEST (test_set_odd_number_of_bits) +{ + uint64_t data = 0xFFFC4DF300000000LLU; + fail_unless(eightbyte_set_bitfield(0x12, 11, 5, &data)); + uint64_t result = eightbyte_get_bitfield(data, 11, 5, false); + fail_unless(result == 0x12, + "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result, + 0x12); + + data = 0xFFFC4DF300000000LLU; + fail_unless(eightbyte_set_bitfield(0x2, 11, 5, &data)); + result = eightbyte_get_bitfield(data, 11, 5, false); + fail_unless(result == 0x2, + "Field set in 0x%llx%llx%llx%llx was 0x%llx instead of 0x%llx", data, result, + 0x2); +} +END_TEST + +START_TEST(test_eightbyte_get_byte) +{ + uint64_t data = 0x00000000F34DFCFF; + uint8_t result = eightbyte_get_byte(data, 0, false); + uint8_t expected = 0x0; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_byte(data, 4, false); + expected = 0xF3; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_byte(data, 5, false); + expected = 0x4D; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_byte(data, 6, false); + expected = 0xFC; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_byte(data, 7, false); + expected = 0xFF; + ck_assert_int_eq(result, expected); +} +END_TEST + +START_TEST(test_eightbyte_get_nibble) +{ + uint64_t data = 0x00000000F34DFCFF; + uint8_t result = eightbyte_get_nibble(data, 0, false); + uint8_t expected = 0x0; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_nibble(data, 2, false); + expected = 0x0; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_nibble(data, 8, false); + expected = 0xF; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_nibble(data, 9, false); + expected = 0x3; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_nibble(data, 10, false); + expected = 0x4; + ck_assert_int_eq(result, expected); + + result = eightbyte_get_nibble(data, 13, false); + expected = 0xC; + ck_assert_int_eq(result, expected); +} +END_TEST + +Suite* bitfieldSuite(void) { + Suite* s = suite_create("bitfield"); + TCase *tc_core = tcase_create("core"); + tcase_add_test(tc_core, test_large_bitmask); + tcase_add_test(tc_core, test_one_bit); + tcase_add_test(tc_core, test_one_bit_not_swapped); + tcase_add_test(tc_core, test_one_byte); + tcase_add_test(tc_core, test_16_bit_parse); + tcase_add_test(tc_core, test_32_bit_parse); + tcase_add_test(tc_core, test_multi_byte); + tcase_add_test(tc_core, test_get_multi_byte); + tcase_add_test(tc_core, test_get_off_byte_boundary); + tcase_add_test(tc_core, test_set_wont_fit); + tcase_add_test(tc_core, test_set_field); + tcase_add_test(tc_core, test_set_doesnt_clobber_existing_data); + tcase_add_test(tc_core, test_set_off_byte_boundary); + tcase_add_test(tc_core, test_set_odd_number_of_bits); + tcase_add_test(tc_core, test_eightbyte_get_nibble); + tcase_add_test(tc_core, test_eightbyte_get_byte); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) { + int numberFailed; + Suite* s = bitfieldSuite(); + SRunner *sr = srunner_create(s); + // Don't fork so we can actually use gdb + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_NORMAL); + numberFailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (numberFailed == 0) ? 0 : 1; +} diff --git a/CAN-binder/libs/bitfield-c/tests/bitfield_tests.c b/CAN-binder/libs/bitfield-c/tests/bitfield_tests.c new file mode 100644 index 0000000..b8c83b5 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/tests/bitfield_tests.c @@ -0,0 +1,132 @@ +#include <check.h> +#include <stdint.h> +#include <bitfield/bitfield.h> + +START_TEST (test_get_byte) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result = get_byte(data, sizeof(data), 0); + ck_assert_int_eq(result, 0x12); + result = get_byte(data, sizeof(data), 3); + ck_assert_int_eq(result, 0x78); +} +END_TEST + +START_TEST (test_set_nibble) +{ + uint8_t data[4] = {0}; + fail_unless(set_nibble(0, 0x1, data, sizeof(data))); + fail_unless(set_nibble(1, 0x2, data, sizeof(data))); + fail_unless(set_nibble(2, 0x3, data, sizeof(data))); + fail_unless(set_nibble(3, 0x4, data, sizeof(data))); + fail_unless(set_nibble(4, 0x5, data, sizeof(data))); + ck_assert_int_eq(data[0], 0x12); + ck_assert_int_eq(data[1], 0x34); + ck_assert_int_eq(data[2], 0x50); +} +END_TEST + +START_TEST (test_set_bitfield) +{ + uint8_t data[4] = {0}; + fail_unless(set_bitfield(0x12, 0, 8, data, sizeof(data))); + fail_unless(set_bitfield(bitmask(3), 10, 3, data, sizeof(data))); + ck_assert_int_eq(data[0], 0x12); + ck_assert_int_eq(data[1], 0x38); +} +END_TEST + +START_TEST (test_set_bitfield_doesnt_fit) +{ + uint8_t data[4] = {0}; + fail_if(set_bitfield(0xffff, 0, 8, data, sizeof(data))); + ck_assert_int_eq(data[0], 0); + ck_assert_int_eq(data[1], 0); + ck_assert_int_eq(data[2], 0); + ck_assert_int_eq(data[3], 0); +} +END_TEST + +START_TEST (test_get_nibble) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result = get_nibble(data, sizeof(data), 0); + ck_assert_int_eq(result, 0x1); + result = get_nibble(data, sizeof(data), 1); + ck_assert_int_eq(result, 0x2); + result = get_nibble(data, sizeof(data), 2); + ck_assert_int_eq(result, 0x3); +} +END_TEST + +START_TEST (test_get_bits_out_of_range) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result[4]; + fail_if(copy_bits_right_aligned(data, sizeof(data), 25, 16, result, + sizeof(result))); +} +END_TEST + +START_TEST (test_get_bits) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result[4] = {0}; + fail_unless(copy_bits_right_aligned(data, sizeof(data), 0, 16, result, + sizeof(result))); + ck_assert_int_eq(result[2], 0x12); + ck_assert_int_eq(result[3], 0x34); +} +END_TEST + +START_TEST (test_copy_bytes) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result[4] = {0}; + fail_unless(copy_bytes_right_aligned(data, sizeof(data), 1, 3, result, + sizeof(result))); + ck_assert_int_eq(result[1], 0x34); + ck_assert_int_eq(result[2], 0x56); + ck_assert_int_eq(result[3], 0x78); +} +END_TEST + +START_TEST (test_get_uneven_bits) +{ + uint8_t data[4] = {0x12, 0x34, 0x56, 0x78}; + uint8_t result[4] = {0}; + fail_unless(copy_bits_right_aligned(data, sizeof(data), 4, 12, result, + sizeof(result))); + ck_assert_int_eq(result[2], 0x2); + ck_assert_int_eq(result[3], 0x34); +} +END_TEST + +Suite* bitfieldSuite(void) { + Suite* s = suite_create("bitfield"); + TCase *tc_core = tcase_create("core"); + tcase_add_test(tc_core, test_get_byte); + tcase_add_test(tc_core, test_get_nibble); + tcase_add_test(tc_core, test_set_nibble); + tcase_add_test(tc_core, test_set_bitfield); + tcase_add_test(tc_core, test_set_bitfield_doesnt_fit); + tcase_add_test(tc_core, test_get_bits); + tcase_add_test(tc_core, test_copy_bytes); + tcase_add_test(tc_core, test_get_bits_out_of_range); + tcase_add_test(tc_core, test_get_uneven_bits); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) { + int numberFailed; + Suite* s = bitfieldSuite(); + SRunner *sr = srunner_create(s); + // Don't fork so we can actually use gdb + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_NORMAL); + numberFailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (numberFailed == 0) ? 0 : 1; +} diff --git a/CAN-binder/libs/bitfield-c/tests/read_tests.c b/CAN-binder/libs/bitfield-c/tests/read_tests.c new file mode 100644 index 0000000..5008cc5 --- /dev/null +++ b/CAN-binder/libs/bitfield-c/tests/read_tests.c @@ -0,0 +1,67 @@ +#include <check.h> +#include <stdint.h> +#include <canutil/read.h> + +const uint64_t BIG_ENDIAN_TEST_DATA = __builtin_bswap64(0xEB00000000000000); +const uint8_t ARRAY_TEST_DATA[] = {0xEB, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; + +START_TEST (test_eightbyte_parse_float) +{ + float result = eightbyte_parse_float(BIG_ENDIAN_TEST_DATA, 2, 4, 1001.0, + -30000.0); + float correctResult = 0xA * 1001.0 - 30000.0; + fail_unless(result == correctResult, + "parse is incorrect: %f but should be %f", result, correctResult); +} +END_TEST + +START_TEST (test_eightbyte_parse_bool) +{ + bool result = eightbyte_parse_bool(BIG_ENDIAN_TEST_DATA, 0, 1, 1.0, 0); + bool correctResult = true; + fail_unless(result == correctResult, + "parse is incorrect: %d but should be %d", result, correctResult); +} +END_TEST + +START_TEST (test_bitfield_parse_float) +{ + float result = bitfield_parse_float(ARRAY_TEST_DATA, + sizeof(ARRAY_TEST_DATA), 2, 4, 1001.0, -30000.0); + float correctResult = 0xA * 1001.0 - 30000.0; + fail_unless(result == correctResult, + "parse is incorrect: %f but should be %f", result, correctResult); +} +END_TEST + +START_TEST (test_bitfield_parse_bool) +{ + fail_unless(bitfield_parse_bool(ARRAY_TEST_DATA, sizeof(ARRAY_TEST_DATA), + 0, 1, 1.0, 0)); +} +END_TEST + +Suite* canreadSuite(void) { + Suite* s = suite_create("read"); + TCase *tc_core = tcase_create("core"); + tcase_add_checked_fixture(tc_core, NULL, NULL); + tcase_add_test(tc_core, test_eightbyte_parse_float); + tcase_add_test(tc_core, test_eightbyte_parse_bool); + tcase_add_test(tc_core, test_bitfield_parse_float); + tcase_add_test(tc_core, test_bitfield_parse_bool); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) { + int numberFailed; + Suite* s = canreadSuite(); + SRunner *sr = srunner_create(s); + // Don't fork so we can actually use gdb + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_NORMAL); + numberFailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (numberFailed == 0) ? 0 : 1; +} diff --git a/CAN-binder/libs/bitfield-c/tests/write_tests.c b/CAN-binder/libs/bitfield-c/tests/write_tests.c new file mode 100644 index 0000000..4d5d8fc --- /dev/null +++ b/CAN-binder/libs/bitfield-c/tests/write_tests.c @@ -0,0 +1,105 @@ +#include <canutil/write.h> +#include <check.h> +#include <stdint.h> + +START_TEST (test_eightbyte_encode_float_precision) +{ + uint64_t value = eightbyte_encode_float(50, 2, 19, 0.001, 0); + ck_assert_int_eq(value, 0x061a800000000000LLU); +} +END_TEST + +START_TEST (test_eightbyte_encode_float) +{ + uint64_t value = eightbyte_encode_float(0, 1, 3, 1, 0); + ck_assert_int_eq(value, 0); + + value = eightbyte_encode_float(1, 1, 3, 1, 0); + ck_assert_int_eq(value, 0x1000000000000000LLU); +} +END_TEST + +START_TEST (test_eightbyte_encode_bool) +{ + uint64_t value = eightbyte_encode_bool(true, 1, 3); + ck_assert_int_eq(value, 0x1000000000000000LLU); + value = eightbyte_encode_bool(false, 1, 3); + ck_assert_int_eq(value, 0x0000000000000000LLU); +} +END_TEST + +START_TEST (test_bitfield_encode_float) +{ + uint8_t data[8] = {0}; + bitfield_encode_float(0, 1, 3, 1, 0, data, sizeof(data)); + ck_assert_int_eq(data[0], 0); + ck_assert_int_eq(data[1], 0); + ck_assert_int_eq(data[2], 0); + ck_assert_int_eq(data[3], 0); + ck_assert_int_eq(data[4], 0); + ck_assert_int_eq(data[5], 0); + ck_assert_int_eq(data[6], 0); + ck_assert_int_eq(data[7], 0); + + bitfield_encode_float(1, 1, 3, 1, 0, data, sizeof(data)); + ck_assert_int_eq(data[0], 0x10); + ck_assert_int_eq(data[1], 0); + ck_assert_int_eq(data[2], 0); + ck_assert_int_eq(data[3], 0); + ck_assert_int_eq(data[4], 0); + ck_assert_int_eq(data[5], 0); + ck_assert_int_eq(data[6], 0); + ck_assert_int_eq(data[7], 0); +} +END_TEST + +START_TEST (test_bitfield_encode_bool) +{ + uint8_t data[8] = {0}; + bitfield_encode_bool(true, 1, 3, data, sizeof(data)); + ck_assert_int_eq(data[0], 0x10); + ck_assert_int_eq(data[1], 0); + ck_assert_int_eq(data[2], 0); + ck_assert_int_eq(data[3], 0); + ck_assert_int_eq(data[4], 0); + ck_assert_int_eq(data[5], 0); + ck_assert_int_eq(data[6], 0); + ck_assert_int_eq(data[7], 0); + + bitfield_encode_bool(false, 1, 3, data, sizeof(data)); + ck_assert_int_eq(data[0], 0); + ck_assert_int_eq(data[1], 0); + ck_assert_int_eq(data[2], 0); + ck_assert_int_eq(data[3], 0); + ck_assert_int_eq(data[4], 0); + ck_assert_int_eq(data[5], 0); + ck_assert_int_eq(data[6], 0); + ck_assert_int_eq(data[7], 0); +} +END_TEST + +Suite* canwriteSuite(void) { + Suite* s = suite_create("write"); + TCase *tc_core = tcase_create("core"); + tcase_add_checked_fixture(tc_core, NULL, NULL); + tcase_add_test(tc_core, test_eightbyte_encode_float); + tcase_add_test(tc_core, test_eightbyte_encode_bool); + tcase_add_test(tc_core, test_eightbyte_encode_float_precision); + tcase_add_test(tc_core, test_bitfield_encode_float); + tcase_add_test(tc_core, test_bitfield_encode_bool); + suite_add_tcase(s, tc_core); + + return s; +} + +int main(void) { + int numberFailed; + Suite* s = canwriteSuite(); + SRunner *sr = srunner_create(s); + // Don't fork so we can actually use gdb + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_NORMAL); + numberFailed = srunner_ntests_failed(sr); + srunner_free(sr); + return (numberFailed == 0) ? 0 : 1; +} |