diff --git a/configure.ac b/configure.ac index ca5c51e43..77c76c8dc 100644 --- a/configure.ac +++ b/configure.ac @@ -425,7 +425,9 @@ NL_RESTORE_WERROR AC_CONFIG_FILES([ Makefile include/Makefile +include/cli/Makefile include/crypto/Makefile +include/ncp/Makefile include/platform/Makefile src/Makefile src/cli/Makefile diff --git a/examples/posix/app/cli/Makefile.am b/examples/posix/app/cli/Makefile.am index c5ba8680f..38b406b2f 100644 --- a/examples/posix/app/cli/Makefile.am +++ b/examples/posix/app/cli/Makefile.am @@ -43,10 +43,11 @@ soc_LDADD = \ $(top_builddir)/src/cli/libopenthread-cli.a \ $(top_builddir)/examples/posix/platform/libopenthread-posix.a \ $(top_builddir)/third_party/mbedtls/libmbedcrypto.a \ + -lstdc++ \ $(NULL) soc_SOURCES = \ - main.cpp \ + main.c \ $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/examples/posix/app/cli/main.cpp b/examples/posix/app/cli/main.c similarity index 95% rename from examples/posix/app/cli/main.cpp rename to examples/posix/app/cli/main.c index b85edf950..5b6b60264 100644 --- a/examples/posix/app/cli/main.cpp +++ b/examples/posix/app/cli/main.c @@ -29,11 +29,9 @@ #include #include -#include +#include #include -Thread::Cli::Serial sCliServer; - void otSignalTaskletPending(void) { } @@ -49,7 +47,7 @@ int main(int argc, char *argv[]) posixPlatformInit(); otInit(); - sCliServer.Start(); + otCliSerialInit(); while (1) { diff --git a/examples/posix/app/ncp/Makefile.am b/examples/posix/app/ncp/Makefile.am index 5c31b5644..aed2a0aab 100644 --- a/examples/posix/app/ncp/Makefile.am +++ b/examples/posix/app/ncp/Makefile.am @@ -44,8 +44,11 @@ ncp_LDADD = \ $(top_builddir)/src/core/libopenthread.a \ $(top_builddir)/examples/posix/platform/libopenthread-posix.a \ $(top_builddir)/third_party/mbedtls/libmbedcrypto.a \ + -lstdc++ \ $(NULL) -ncp_SOURCES = main.cpp +ncp_SOURCES = \ + main.c \ + $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/examples/posix/app/ncp/main.cpp b/examples/posix/app/ncp/main.c similarity index 96% rename from examples/posix/app/ncp/main.cpp rename to examples/posix/app/ncp/main.c index c402cc067..54b37a2e6 100644 --- a/examples/posix/app/ncp/main.cpp +++ b/examples/posix/app/ncp/main.c @@ -27,11 +27,10 @@ #include -#include +#include +#include #include -Thread::Ncp sNcp; - void otSignalTaskletPending(void) { } @@ -46,8 +45,7 @@ int main(int argc, char *argv[]) NODE_ID = atoi(argv[1]); posixPlatformInit(); otInit(); - - sNcp.Start(); + otNcpInit(); while (1) { diff --git a/examples/posix/platform/platform.c b/examples/posix/platform/platform.c index 9c884c8a9..fe9fe08ac 100644 --- a/examples/posix/platform/platform.c +++ b/examples/posix/platform/platform.c @@ -40,6 +40,7 @@ #include #include +#include #include uint32_t NODE_ID = 1; @@ -50,6 +51,7 @@ void posixPlatformInit(void) posixPlatformAlarmInit(); posixPlatformRadioInit(); posixPlatformRandomInit(); + otPlatSerialEnable(); } void posixPlatformProcessDrivers(void) diff --git a/examples/posix/platform/serial.c b/examples/posix/platform/serial.c index 8150b6864..5ecd30dee 100644 --- a/examples/posix/platform/serial.c +++ b/examples/posix/platform/serial.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include diff --git a/include/Makefile.am b/include/Makefile.am index feaa395ad..29406db7f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -31,21 +31,27 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am # Always package (e.g. for 'make dist') these subdirectories. DIST_SUBDIRS = \ + cli \ crypto \ + ncp \ platform \ $(NULL) # Always build (e.g. for 'make all') these subdirectories. SUBDIRS = \ + cli \ crypto \ + ncp \ platform \ $(NULL) # Always pretty (e.g. for 'make pretty') these subdirectories. PRETTY_SUBDIRS = \ + cli \ crypto \ + ncp \ platform \ $(NULL) diff --git a/include/cli/Makefile.am b/include/cli/Makefile.am new file mode 100644 index 000000000..fc0e39cec --- /dev/null +++ b/include/cli/Makefile.am @@ -0,0 +1,37 @@ +# +# Copyright (c) 2016, Nest Labs, Inc. +# 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 $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +include_HEADERS = \ + cli-serial.h \ + $(NULL) + +install-headers: install-includeHEADERS + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/include/cli/cli-serial.h b/include/cli/cli-serial.h new file mode 100644 index 000000000..f321d344d --- /dev/null +++ b/include/cli/cli-serial.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016, Nest Labs, Inc. + * 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. + */ + +/** + * @file + * @brief + * This file defines the top-level functions for the OpenThread CLI server. + */ + +#ifndef CLI_SERIAL_H_ +#define CLI_SERIAL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Initialize the CLI serial server. + * + */ +void otCliSerialInit(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/include/ncp/Makefile.am b/include/ncp/Makefile.am new file mode 100644 index 000000000..be14cab36 --- /dev/null +++ b/include/ncp/Makefile.am @@ -0,0 +1,37 @@ +# +# Copyright (c) 2016, Nest Labs, Inc. +# 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 $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +include_HEADERS = \ + ncp.h \ + $(NULL) + +install-headers: install-includeHEADERS + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/include/ncp/ncp.h b/include/ncp/ncp.h new file mode 100644 index 000000000..437fe4e44 --- /dev/null +++ b/include/ncp/ncp.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016, Nest Labs, Inc. + * 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. + */ + +/** + * @file + * @brief + * This file defines the top-level functions for the OpenThread CLI server. + */ + +#ifndef NCP_H_ +#define NCP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Initialize the CLI serial server. + * + */ +void otNcpInit(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/src/cli/cli_serial.cpp b/src/cli/cli_serial.cpp index 6d723c2ea..963fc5bfb 100644 --- a/src/cli/cli_serial.cpp +++ b/src/cli/cli_serial.cpp @@ -31,12 +31,15 @@ * This file implements the CLI server on the serial service. */ +#include + #include #include #include #include #include +#include #include #include #include @@ -50,19 +53,19 @@ static const char sEraseString[] = {'\b', ' ', '\b'}; static const char CRNL[] = {'\r', '\n'}; static Serial *sServer; -Serial::Serial(void) +static otDEFINE_ALIGNED_VAR(sCliSerialRaw, sizeof(Serial), uint64_t); + +extern "C" void otCliSerialInit(void) { - sServer = this; + sServer = new(&sCliSerialRaw) Serial; } -ThreadError Serial::Start(void) +Serial::Serial(void) { mRxLength = 0; mTxHead = 0; mTxLength = 0; mSendLength = 0; - otPlatSerialEnable(); - return kThreadError_None; } extern "C" void otPlatSerialReceived(const uint8_t *aBuf, uint16_t aBufLength) diff --git a/src/cli/cli_serial.hpp b/src/cli/cli_serial.hpp index fc6aeecd5..108461360 100644 --- a/src/cli/cli_serial.hpp +++ b/src/cli/cli_serial.hpp @@ -50,14 +50,6 @@ class Serial: public Server public: Serial(void); - /** - * This method starts the CLI server. - * - * @retval kThreadError_None Successfully started the server. - * - */ - ThreadError Start(void); - /** * This method delivers raw characters to the client. * diff --git a/src/cli/cli_server.hpp b/src/cli/cli_server.hpp index b6e61322d..8d103a5a6 100644 --- a/src/cli/cli_server.hpp +++ b/src/cli/cli_server.hpp @@ -46,14 +46,6 @@ namespace Cli { class Server { public: - /** - * This method starts the CLI server. - * - * @retval kThreadError_None Successfully started the server. - * - */ - virtual ThreadError Start() = 0; - /** * This method delivers raw characters to the client. * diff --git a/src/core/common/code_utils.hpp b/src/core/common/code_utils.hpp index 8325ce7af..cd702007d 100644 --- a/src/core/common/code_utils.hpp +++ b/src/core/common/code_utils.hpp @@ -36,6 +36,10 @@ #include +// Allocate the structure using "raw" storage. +#define otDEFINE_ALIGNED_VAR(name, size, align_type) \ + align_type name[(((size) + (sizeof (align_type) - 1)) / sizeof (align_type))] + #define SuccessOrExit(ERR) \ do { \ if ((ERR) != 0) { \ diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 4001bf235..68ff71eea 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -54,10 +54,6 @@ ThreadNetif *sThreadNetif; extern "C" { #endif -// Allocate the structure using "raw" storage. -#define otDEFINE_ALIGNED_VAR(name, size, align_type) \ - align_type name[(((size) + (sizeof (align_type) - 1)) / sizeof (align_type))] - static otDEFINE_ALIGNED_VAR(sThreadNetifRaw, sizeof(ThreadNetif), uint64_t); static void HandleActiveScanResult(void *aContext, Mac::Frame *aFrame); diff --git a/src/ncp/ncp.cpp b/src/ncp/ncp.cpp index 8bebb72b0..552678bff 100644 --- a/src/ncp/ncp.cpp +++ b/src/ncp/ncp.cpp @@ -30,31 +30,27 @@ * This file implements an HDLC interface to the Thread stack. */ +#include + #include +#include #include #include namespace Thread { +static otDEFINE_ALIGNED_VAR(sNcpRaw, sizeof(Ncp), uint64_t); static Ncp *sNcp; +extern "C" void otNcpInit(void) +{ + sNcp = new(&sNcpRaw) Ncp; +} + Ncp::Ncp(): NcpBase(), mFrameDecoder(mReceiveFrame, sizeof(mReceiveFrame), &HandleFrame, this) { - sNcp = this; -} - -ThreadError Ncp::Start() -{ - otPlatSerialEnable(); - return super_t::Start(); -} - -ThreadError Ncp::Stop() -{ - otPlatSerialDisable(); - return super_t::Stop(); } uint16_t diff --git a/src/ncp/ncp.hpp b/src/ncp/ncp.hpp index 8053a8001..a3d4e9a8c 100644 --- a/src/ncp/ncp.hpp +++ b/src/ncp/ncp.hpp @@ -46,10 +46,6 @@ class Ncp : public NcpBase public: Ncp(); - ThreadError Start(); - ThreadError Stop(); - - virtual ThreadError OutboundFrameBegin(void); virtual uint16_t OutboundFrameGetRemaining(void); virtual ThreadError OutboundFrameFeedData(const uint8_t *frame, uint16_t frameLength); diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 325ca51cd..78442cd11 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -228,19 +228,10 @@ NcpBase::NcpBase(): mSupportedChannelMask = (0xFFFF << 11); // Default to 2.4GHz 802.15.4 channels. mChannelMask = mSupportedChannelMask; mScanPeriod = 200; // ms -} -ThreadError NcpBase::Start() -{ assert(sThreadNetif != NULL); sThreadNetif->RegisterHandler(mNetifHandler); Ip6::Ip6::SetNcpReceivedHandler(&HandleDatagramFromStack, this); - return kThreadError_None; -} - -ThreadError NcpBase::Stop() -{ - return kThreadError_None; } // ---------------------------------------------------------------------------- diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index 278e90794..1b0175903 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -47,9 +47,6 @@ public: NcpBase(); - ThreadError Start(); - ThreadError Stop(); - protected: virtual ThreadError OutboundFrameBegin(void) = 0;