From 55e17533b9cbb76c9f80546545d4429b480b75e5 Mon Sep 17 00:00:00 2001 From: Nick Banks Date: Mon, 5 Dec 2016 16:23:30 -0800 Subject: [PATCH] Fix otNetifAddress declaration to work between C & C++ (#1037) * Fix otNetifAddress declaration to work between C & C++ --- etc/visual-studio/UnitTests.vcxproj | 1 + examples/drivers/windows/otLwf/radio.c | 4 +- include/openthread-types.h | 1 + src/missing/stdbool/stdbool.h | 2 +- tests/unit/Makefile.am | 2 +- tests/unit/test_toolchain.cpp | 20 ++++++++++ tests/unit/test_toolchain_c.c | 52 ++++++++++++++++++++++++++ tests/unit/test_windows.cpp | 4 ++ 8 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 tests/unit/test_toolchain_c.c diff --git a/etc/visual-studio/UnitTests.vcxproj b/etc/visual-studio/UnitTests.vcxproj index 614990728..87225c2df 100644 --- a/etc/visual-studio/UnitTests.vcxproj +++ b/etc/visual-studio/UnitTests.vcxproj @@ -72,6 +72,7 @@ + diff --git a/examples/drivers/windows/otLwf/radio.c b/examples/drivers/windows/otLwf/radio.c index 8fb575d44..70217f550 100644 --- a/examples/drivers/windows/otLwf/radio.c +++ b/examples/drivers/windows/otLwf/radio.c @@ -245,7 +245,7 @@ void otPlatRadioSetShortAddress(_In_ otInstance *otCtx, uint16_t address) } } -void otPlatRadioSetPromiscuous(_In_ otInstance *otCtx, int aEnable) +void otPlatRadioSetPromiscuous(_In_ otInstance *otCtx, bool aEnable) { NT_ASSERT(otCtx); PMS_FILTER pFilter = otCtxToFilter(otCtx); @@ -444,7 +444,7 @@ otRadioCaps otPlatRadioGetCaps(_In_ otInstance *otCtx) return otCtxToFilter(otCtx)->otRadioCapabilities; } -int otPlatRadioGetPromiscuous(_In_ otInstance *otCtx) +bool otPlatRadioGetPromiscuous(_In_ otInstance *otCtx) { NT_ASSERT(otCtx); PMS_FILTER pFilter = otCtxToFilter(otCtx); diff --git a/include/openthread-types.h b/include/openthread-types.h index 762e425d1..70d021042 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -48,6 +48,7 @@ extern "C" { #endif #ifdef _WIN32 +#pragma warning(disable:4214) // nonstandard extension used: bit field types other than int #ifdef _KERNEL_MODE #include #else diff --git a/src/missing/stdbool/stdbool.h b/src/missing/stdbool/stdbool.h index 7e6050fa7..e28121c5f 100644 --- a/src/missing/stdbool/stdbool.h +++ b/src/missing/stdbool/stdbool.h @@ -31,7 +31,7 @@ #ifndef __cplusplus -typedef int bool; +typedef _Bool bool; #define false 0 #define true 1 diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 7355638e1..0d5cf7d6b 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -145,7 +145,7 @@ test_timer_LDADD = $(COMMON_LDADD) test_timer_SOURCES = test_platform.cpp test_timer.cpp test_toolchain_LDADD = $(COMMON_LDADD) -test_toolchain_SOURCES = test_platform.cpp test_toolchain.cpp +test_toolchain_SOURCES = test_platform.cpp test_toolchain.cpp test_toolchain_c.c if OPENTHREAD_ENABLE_DIAG test_diag_LDADD = $(top_builddir)/src/diag/libopenthread-diag.a \ diff --git a/tests/unit/test_toolchain.cpp b/tests/unit/test_toolchain.cpp index 9e0e976b2..82ec5e476 100644 --- a/tests/unit/test_toolchain.cpp +++ b/tests/unit/test_toolchain.cpp @@ -32,6 +32,12 @@ #include #include "test_util.h" +extern "C" { + uint32_t otNetifAddress_Size_c(); + uint32_t otNetifAddress_offset_mNext_c(); + otNetifAddress CreateNetif_c(); +} + void test_packed1() { OT_TOOL_PACKED_BEGIN @@ -93,12 +99,26 @@ void test_packed_enum() VerifyOrQuit(neighbor.mState == Thread::Neighbor::kStateValid, "Toolchain::OT_TOOL_PACKED failed 4\n"); } +void test_addr_sizes() +{ + VerifyOrQuit(offsetof(otNetifAddress, mNext) == otNetifAddress_offset_mNext_c(), + "mNext should offset the same in C & C++"); + VerifyOrQuit(sizeof(otNetifAddress) == otNetifAddress_Size_c(), "otNetifAddress should the same in C & C++"); +} + +void test_addr_bitfield() +{ + VerifyOrQuit(CreateNetif_c().mScopeOverrideValid == true, "Toolchain::test_addr_size_cpp\n"); +} + void TestToolchain(void) { test_packed1(); test_packed2(); test_packed_union(); test_packed_enum(); + test_addr_sizes(); + test_addr_bitfield(); } #ifdef ENABLE_TEST_MAIN diff --git a/tests/unit/test_toolchain_c.c b/tests/unit/test_toolchain_c.c new file mode 100644 index 000000000..43898ffc9 --- /dev/null +++ b/tests/unit/test_toolchain_c.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. + */ + +#include +#include +#include +#include +#include +#include "test_util.h" + +uint32_t otNetifAddress_Size_c() +{ + return sizeof(otNetifAddress); +} + +uint32_t otNetifAddress_offset_mNext_c() +{ + return offsetof(otNetifAddress, mNext); +} + +otNetifAddress CreateNetif_c() +{ + otNetifAddress addr; + memset(&addr, 0, sizeof(addr)); + addr.mScopeOverrideValid = true; + return addr; +} diff --git a/tests/unit/test_windows.cpp b/tests/unit/test_windows.cpp index 0c8a85571..7e42027a1 100644 --- a/tests/unit/test_windows.cpp +++ b/tests/unit/test_windows.cpp @@ -80,6 +80,8 @@ void test_packed1(); void test_packed2(); void test_packed_union(); void test_packed_enum(); +void test_addr_sizes(); +void test_addr_bitfield(); // test_fuzz.cpp void TestFuzz(uint32_t aSeconds); @@ -155,6 +157,8 @@ namespace Thread TEST_METHOD(test_packed2) { ::test_packed2(); } TEST_METHOD(test_packed_union) { ::test_packed_union(); } TEST_METHOD(test_packed_enum) { ::test_packed_enum(); } + TEST_METHOD(test_addr_sizes) { ::test_addr_sizes(); } + TEST_METHOD(test_addr_bitfield) { ::test_addr_bitfield(); } // test_settings.cpp TEST_METHOD(RunTestFuzz) { ::TestFuzz(30); }