host: First cut at portability issues with endianess macros.

This changeset includes changes suggested by maggo1404 in the
following pull request.

https://github.com/Nuand/bladeRF/pull/52

These suggestions were reworked a bit to avoid having #ifdef
blocks throughout the codebase and to alleviate the need to copy items
between utilitie and library code.
This commit is contained in:
Jon Szymaniak 2013-08-23 00:25:14 -04:00
commit 2237314a4f
9 changed files with 123 additions and 10 deletions

View file

@ -25,8 +25,14 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/modules)
# Firmware <--> Host common files
set(BLADERF_FW_COMMON_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/../firmware_common)
# Source common amongst host software
set(BLADERF_HOST_COMMON_DIR ${CMAKE_CURRENT_LIST_DIR}/common)
# Source and headers common amongst host software
set(BLADERF_HOST_COMMON_INCLUDE_DIRS
${CMAKE_CURRENT_LIST_DIR}/common
${CMAKE_CURRENT_BINARY_DIR}/common
)
set(BLADERF_HOST_COMMON_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/common)
################################################################################
# Compiler configuration
@ -72,14 +78,29 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
endif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
###############################
# System configuration
###############################
# Linux (TODO determine if this will actually be sufficient for BSD)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "BSD")
set(BLADERF_OS_LINUX 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(BLADERF_OS_OSX 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(BLADERF_OS_WINDOWS 1)
else()
message(FATAL_ERROR "Unsupported host operating system: ${CMAKE_SYSTEM_NAME}")
endif()
################################################################################
# Process subdirectories
################################################################################
add_subdirectory(common)
add_subdirectory(libraries)
add_subdirectory(utilities)
add_subdirectory(misc)
add_subdirectory(utilities)
################################################################################
# Create uninstall target

View file

@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 2.8)
project(bladerf_host_common C)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/host_config.h.in
${CMAKE_CURRENT_BINARY_DIR}/host_config.h
@ONLY
)

View file

@ -0,0 +1,82 @@
#ifndef HOST_CONFIG_H__
#define HOST_CONFIG_H__
#cmakedefine01 BLADERF_OS_LINUX
#cmakedefine01 BLADERF_OS_OSX
#cmakedefine01 BLADERF_OS_WINDOWS
#if !(BLADERF_OS_LINUX || BLADERF_OS_OSX || BLADERF_OS_WINDOWS)
# error "Build not configured for any supported operating systems"
#endif
#if 1 < (BLADERF_OS_LINUX + BLADERF_OS_OSX + BLADERF_OS_WINDOWS)
#error "Build configured for multiple operating systems"
#endif
/*******************************************************************************
* Endianness conversions
*
* HOST_TO_LE16 16-bit host endianness to little endian conversion
* LE16_TO_HOST 16-bit little endian to host endianness conversion
* HOST_TO_BE16 16-bit host endianness to big endian conversion
* BE16_TO_HOST 16-bit big endian to host endianness conversion
******************************************************************************/
/*-----------------------
* Linux
*---------------------*/
#if BLADERF_OS_LINUX
#include <endian.h>
#define HOST_TO_LE16(val) htole16(val)
#define LE16_TO_HOST(val) le16toh(val)
#define HOST_TO_BE16(val) htobe16(val)
#define BE16_TO_HOST(val) be16toh(val)
/*-----------------------
* Apple OSX
*---------------------*/
#elif BLADERF_OS_OSX
#include <libkern/OSByteOrder.h>
#define HOST_TO_LE16(val) OSSwapHostToLittleInt16(val)
#define LE16_TO_HOST(val) OSSwapLittleToHostInt16(val)
#define HOST_TO_BE16(val) OSSwapHostToBigInt16(val)
#define BE16_TO_HOST(val) OSSwapBigToHostInt16(val)
/*-----------------------
* Windows
*---------------------*/
#elif BLADERF_OS_WINDOWS
#include <intrin.h>
/* We'll assume little endian for Windows platforms:
* http://blogs.msdn.com/b/larryosterman/archive/2005/06/07/426334.aspx
*/
#define HOST_TO_LE16(val) (val)
#define LE16_TO_HOST(val) (val)
#define HOST_TO_BE16(val) _byteswap_ushort(val)
#define BE16_TO_HOST(val) _byteswap_ushort(val)
/*-----------------------
* Unsupported or bug
*---------------------*/
#else
#error "Encountered an OS that we do not have endian wrappers for?"
#endif
/*******************************************************************************
* Miscellaneous fixups
******************************************************************************/
/* Windows does not appear to provide strtok_r, but appears to provide a
* strtok_s which presents the same interface */
#if BLADERF_OS_WINDOWS
#define strtok_r strtok_s
#endif
#endif

View file

@ -91,7 +91,7 @@ endif()
################################################################################
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/src
${BLADERF_HOST_COMMON_DIR}
${BLADERF_HOST_COMMON_INCLUDE_DIRS}
${BLADERF_FW_COMMON_INCLUDE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/src
${LIBUSB_INCLUDE_DIRS}
@ -127,7 +127,7 @@ set(LIBBLADERF_SOURCE
src/lms.c
src/si5338.c
src/version.h
${BLADERF_HOST_COMMON_DIR}/conversions.c
${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c
)
if(ENABLE_BACKEND_LIBUSB)

View file

@ -7,7 +7,6 @@
#include <stdbool.h>
#include <sys/stat.h>
#include <unistd.h>
#include <endian.h>
#include <bladeRF.h>
#include <libusb-1.0/libusb.h>
@ -742,8 +741,8 @@ static int lusb_get_fw_version(struct bladerf *dev,
status = BLADERF_ERR_IO;
*maj = *min = 0;
} else {
*maj = (unsigned int) le16toh(fw_ver.major);
*min = (unsigned int) le16toh(fw_ver.minor);
*maj = (unsigned int) LE16_TO_HOST(fw_ver.major);
*min = (unsigned int) LE16_TO_HOST(fw_ver.minor);
}
return status;

View file

@ -6,6 +6,7 @@
#include <limits.h>
#include <libbladeRF.h>
#include "host_config.h"
/* Reserved values for bladerf_devinfo fields to indicate "undefined" */
#define DEVINFO_SERIAL_ANY "ANY"

View file

@ -41,7 +41,7 @@ set(BLADERF_CLI_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/src/cmd
${CMAKE_CURRENT_SOURCE_DIR}/src/interactive
${CMAKE_CURRENT_BINARY_DIR}/src
${BLADERF_HOST_COMMON_DIR}
${BLADERF_HOST_COMMON_INCLUDE_DIRS}
${libbladeRF_SOURCE_DIR}/include
)
@ -67,6 +67,7 @@ set(BLADERF_CLI_SOURCE
src/cmd/probe.c
src/cmd/rxtx.c
src/cmd/version.c
${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c
)
if(ENABLE_INTERACTIVE_TECLA)

View file

@ -13,7 +13,6 @@
#include <stdint.h>
#include <pthread.h>
#include <limits.h>
#include <endian.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
@ -21,6 +20,7 @@
#include "cmd.h"
#include "conversions.h"
#include "minmax.h"
#include "host_config.h"
#define RXTX_CMDSTR_START "start"
#define RXTX_CMDSTR_STOP "stop"

View file

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <libbladeRF.h>
/**
* Differentiates error code types
*/