diff --git a/Android.mk b/Android.mk index e74ae5492..9d9d73e58 100644 --- a/Android.mk +++ b/Android.mk @@ -236,17 +236,17 @@ LOCAL_SRC_FILES := \ src/ncp/spinel.c \ src/ncp/spinel_decoder.cpp \ src/ncp/spinel_encoder.cpp \ - src/posix/platform/alarm.c \ - src/posix/platform/entropy.c \ + src/posix/platform/alarm.cpp \ + src/posix/platform/entropy.cpp \ src/posix/platform/hdlc_interface.cpp \ - src/posix/platform/logging.c \ - src/posix/platform/misc.c \ + src/posix/platform/logging.cpp \ + src/posix/platform/misc.cpp \ src/posix/platform/netif.cpp \ src/posix/platform/radio_spinel.cpp \ src/posix/platform/settings.cpp \ src/posix/platform/spi_interface.cpp \ - src/posix/platform/system.c \ - src/posix/platform/uart.c \ + src/posix/platform/system.cpp \ + src/posix/platform/uart.cpp \ src/posix/platform/udp.cpp \ third_party/mbedtls/repo/library/md.c \ third_party/mbedtls/repo/library/md_wrap.c \ diff --git a/src/posix/CMakeLists.txt b/src/posix/CMakeLists.txt index 446d342da..19a0b8fae 100644 --- a/src/posix/CMakeLists.txt +++ b/src/posix/CMakeLists.txt @@ -58,7 +58,7 @@ if(OT_DAEMON) ) add_executable(ot-ctl - client.c + client.cpp ) target_compile_options(ot-ctl PRIVATE diff --git a/src/posix/Makefile.am b/src/posix/Makefile.am index 896893e8f..1b30e98aa 100644 --- a/src/posix/Makefile.am +++ b/src/posix/Makefile.am @@ -118,7 +118,7 @@ endif endif # OPENTHREAD_ENABLE_POSIX_APP_DAEMON ot_ctl_SOURCES = \ - client.c \ + client.cpp \ $(NULL) ot_ctl_CPPFLAGS = \ @@ -160,7 +160,7 @@ ot_cli_CPPFLAGS = \ ot_cli_SOURCES = \ main.c \ - console_cli.c \ + console_cli.cpp \ $(NULL) ot_cli_LDADD = \ diff --git a/src/posix/client.c b/src/posix/client.cpp similarity index 86% rename from src/posix/client.c rename to src/posix/client.cpp index dfd88fc54..717a293a7 100644 --- a/src/posix/client.c +++ b/src/posix/client.cpp @@ -40,13 +40,6 @@ #define OPENTHREAD_USE_READLINE (HAVE_LIBEDIT || HAVE_LIBREADLINE) -#if HAVE_LIBEDIT -#include -#elif HAVE_LIBREADLINE -#include -#include -#endif - #include #include #include @@ -54,7 +47,13 @@ #include #include -#include "code_utils.h" +#if HAVE_LIBEDIT +#include +#elif HAVE_LIBREADLINE +#include +#include +#endif + #include "common/code_utils.hpp" #include "platform-posix.h" @@ -155,22 +154,22 @@ static void SendBlockingCommand(int aArgc, char *aArgv[]) for (int i = 0; i < aArgc; i++) { - otEXPECT_ACTION(write(sSessionFd, aArgv[i], strlen(aArgv[i])) >= 0, perror("Failed to send command")); - otEXPECT_ACTION(write(sSessionFd, " ", 1) >= 0, perror("Failed to send command")); + VerifyOrExit(write(sSessionFd, aArgv[i], strlen(aArgv[i])) >= 0, perror("Failed to send command")); + VerifyOrExit(write(sSessionFd, " ", 1) >= 0, perror("Failed to send command")); } - otEXPECT_ACTION(write(sSessionFd, "\n", 1) >= 0, perror("Failed to send command")); + VerifyOrExit(write(sSessionFd, "\n", 1) >= 0, perror("Failed to send command")); while (true) { ssize_t rval = read(sSessionFd, buffer, sizeof(buffer)); - otEXPECT(rval >= 0); - write(STDOUT_FILENO, buffer, rval); + VerifyOrExit(rval >= 0); + write(STDOUT_FILENO, buffer, static_cast(rval)); for (ssize_t i = 0; i < rval; i++) { if (FindDone(&doneState, buffer[i]) || FindError(&errorState, buffer[i])) { - otEXIT_NOW(); + ExitNow(); } } } @@ -186,7 +185,7 @@ int main(int argc, char *argv[]) int ret; sSessionFd = socket(AF_UNIX, SOCK_STREAM, 0); - otEXPECT_ACTION(sSessionFd != -1, perror("socket"); ret = OT_EXIT_FAILURE); + VerifyOrExit(sSessionFd != -1, perror("socket"); ret = OT_EXIT_FAILURE); { struct sockaddr_un sockname; @@ -200,7 +199,7 @@ int main(int argc, char *argv[]) if (ret == -1) { fprintf(stderr, "OpenThread daemon is not running.\n"); - otEXIT_NOW(ret = OT_EXIT_FAILURE); + ExitNow(ret = OT_EXIT_FAILURE); } #if OPENTHREAD_USE_READLINE @@ -215,7 +214,7 @@ int main(int argc, char *argv[]) if (argc > 1) { SendBlockingCommand(argc - 1, &argv[1]); - otEXIT_NOW(ret = 0); + ExitNow(ret = 0); } while (1) @@ -232,11 +231,11 @@ int main(int argc, char *argv[]) ret = select(maxFd + 1, &readFdSet, NULL, NULL, NULL); - otEXPECT_ACTION(ret != -1, perror("select"); ret = OT_EXIT_FAILURE); + VerifyOrExit(ret != -1, perror("select"); ret = OT_EXIT_FAILURE); if (ret == 0) { - otEXIT_NOW(ret = OT_EXIT_SUCCESS); + ExitNow(ret = OT_EXIT_SUCCESS); } if (FD_ISSET(STDIN_FILENO, &readFdSet)) @@ -244,26 +243,26 @@ int main(int argc, char *argv[]) #if OPENTHREAD_USE_READLINE rl_callback_read_char(); #else - otEXPECT_ACTION(fgets(buffer, sizeof(buffer), stdin) != NULL, ret = OT_EXIT_FAILURE); + VerifyOrExit(fgets(buffer, sizeof(buffer), stdin) != NULL, ret = OT_EXIT_FAILURE); rval = write(sSessionFd, buffer, strlen(buffer)); - otEXPECT_ACTION(rval != -1, perror("write"); ret = OT_EXIT_FAILURE); + VerifyOrExit(rval != -1, perror("write"); ret = OT_EXIT_FAILURE); #endif } if (FD_ISSET(sSessionFd, &readFdSet)) { rval = read(sSessionFd, buffer, sizeof(buffer)); - otEXPECT_ACTION(rval != -1, perror("read"); ret = OT_EXIT_FAILURE); + VerifyOrExit(rval != -1, perror("read"); ret = OT_EXIT_FAILURE); if (rval == 0) { // daemon closed sSessionFd - otEXIT_NOW(ret = OT_EXIT_FAILURE); + ExitNow(ret = OT_EXIT_FAILURE); } else { - IgnoreReturnValue(write(STDOUT_FILENO, buffer, rval)); + IgnoreReturnValue(write(STDOUT_FILENO, buffer, static_cast(rval))); } } } diff --git a/src/posix/console_cli.c b/src/posix/console_cli.cpp similarity index 100% rename from src/posix/console_cli.c rename to src/posix/console_cli.cpp diff --git a/src/posix/console_cli.h b/src/posix/console_cli.h index 10732ff1a..a7c22140e 100644 --- a/src/posix/console_cli.h +++ b/src/posix/console_cli.h @@ -35,6 +35,10 @@ #include "openthread-system.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * This function initializes CLI console. * @@ -65,4 +69,8 @@ void otxConsoleUpdate(otSysMainloopContext *aMainloop); */ void otxConsoleProcess(const otSysMainloopContext *aMainloop); +#ifdef __cplusplus +} +#endif + #endif // OPENTHREAD_CONSOLE_CLI_H_ diff --git a/src/posix/platform/CMakeLists.txt b/src/posix/platform/CMakeLists.txt index d1676b48b..228155d9a 100644 --- a/src/posix/platform/CMakeLists.txt +++ b/src/posix/platform/CMakeLists.txt @@ -43,19 +43,19 @@ list(APPEND OT_PLATFORM_DEFINES set(OT_PLATFORM_DEFINES ${OT_PLATFORM_DEFINES} PARENT_SCOPE) add_library(openthread-posix - alarm.c - entropy.c + alarm.cpp + entropy.cpp hdlc_interface.cpp - logging.c - misc.c + logging.cpp + misc.cpp netif.cpp radio_spinel.cpp settings.cpp - sim.c spi_interface.cpp - system.c - uart.c + system.cpp + uart.cpp udp.cpp + virtual_time.cpp ) target_link_libraries(openthread-posix PUBLIC diff --git a/src/posix/platform/Makefile.am b/src/posix/platform/Makefile.am index 1772a3a44..451e6538e 100644 --- a/src/posix/platform/Makefile.am +++ b/src/posix/platform/Makefile.am @@ -42,25 +42,25 @@ libopenthread_posix_a_CPPFLAGS = \ $(NULL) libopenthread_posix_a_SOURCES = \ - alarm.c \ - entropy.c \ + alarm.cpp \ + entropy.cpp \ hdlc_interface.cpp \ - logging.c \ - misc.c \ + logging.cpp \ + misc.cpp \ netif.cpp \ radio_spinel.cpp \ settings.cpp \ - sim.c \ spi_interface.cpp \ - system.c \ - uart.c \ + system.cpp \ + uart.cpp \ udp.cpp \ + virtual_time.cpp \ $(NULL) noinst_HEADERS = \ + hdlc_interface.hpp \ openthread-posix-config.h \ platform-posix.h \ - hdlc_interface.hpp \ radio_spinel.hpp \ spinel_interface.hpp \ $(NULL) diff --git a/src/posix/platform/alarm.c b/src/posix/platform/alarm.cpp similarity index 98% rename from src/posix/platform/alarm.c rename to src/posix/platform/alarm.cpp index 00dd1ffc5..a5811eb5c 100644 --- a/src/posix/platform/alarm.c +++ b/src/posix/platform/alarm.cpp @@ -30,7 +30,6 @@ #include "platform-posix.h" #include -#include #include #include #include @@ -39,7 +38,7 @@ #include #include -#include "code_utils.h" +#include "common/code_utils.hpp" static bool sIsMsRunning = false; static uint32_t sMsAlarm = 0; @@ -128,7 +127,7 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout) if (sIsMsRunning) { remaining = (int32_t)(sMsAlarm - (uint32_t)(now / US_PER_MS)); - otEXPECT(remaining > 0); + VerifyOrExit(remaining > 0); remaining *= US_PER_MS; remaining -= (now % US_PER_MS); } diff --git a/src/posix/platform/code_utils.h b/src/posix/platform/code_utils.h deleted file mode 100644 index 46b0d0012..000000000 --- a/src/posix/platform/code_utils.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2017, 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. - */ - -/** - * @file - * This file includes macros for validating runtime conditions. - */ - -#ifndef CODE_UTILS_H -#define CODE_UTILS_H - -/** - * This checks for the specified condition, which is expected to - * commonly be true, and branches to the local label 'exit' if the - * condition is false. - * - * @param[in] aCondition A Boolean expression to be evaluated. - * - */ -#define otEXPECT(aCondition) \ - do \ - { \ - if (!(aCondition)) \ - { \ - goto exit; \ - } \ - } while (0) - -/** - * This checks for the specified condition, which is expected to - * commonly be true, and both executes @p anAction and branches to - * the local label 'exit' if the condition is false. - * - * @param[in] aCondition A Boolean expression to be evaluated. - * @param[in] aAction An expression or block to execute when the - * assertion fails. - * - */ -#define otEXPECT_ACTION(aCondition, aAction) \ - do \ - { \ - if (!(aCondition)) \ - { \ - aAction; \ - goto exit; \ - } \ - } while (0) - -/** - * This unconditionally executes @aAction and branches to the local - * label 'exit'. - * - * @note The use of this interface implies neither success nor - * failure for the overall exit status of the enclosing - * function body. - * - * @param[in] aAction An expression or block to execute. - * - */ -#define otEXIT_NOW(aAction) \ - do \ - { \ - aAction; \ - goto exit; \ - } while (0) - -#endif // CODE_UTILS_H diff --git a/src/posix/platform/entropy.c b/src/posix/platform/entropy.cpp similarity index 92% rename from src/posix/platform/entropy.c rename to src/posix/platform/entropy.cpp index c7d2523c9..4fad7c573 100644 --- a/src/posix/platform/entropy.c +++ b/src/posix/platform/entropy.cpp @@ -33,16 +33,15 @@ */ #include "openthread-posix-config.h" - -#include +#include "platform-posix.h" #include #include -#include "code_utils.h" -#include "platform-posix.h" - #include +#include + +#include "common/code_utils.hpp" #ifndef __SANITIZE_ADDRESS__ #define __SANITIZE_ADDRESS__ 0 @@ -99,13 +98,13 @@ otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength) FILE * file = NULL; size_t readLength; - otEXPECT_ACTION(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS); file = fopen("/dev/urandom", "rb"); - otEXPECT_ACTION(file != NULL, error = OT_ERROR_FAILED); + VerifyOrExit(file != NULL, error = OT_ERROR_FAILED); readLength = fread(aOutput, 1, aOutputLength, file); - otEXPECT_ACTION(readLength == aOutputLength, error = OT_ERROR_FAILED); + VerifyOrExit(readLength == aOutputLength, error = OT_ERROR_FAILED); exit: @@ -124,7 +123,7 @@ exit: * implementation below is only used to enable continuous * integration checks with Address Sanitizer enabled. */ - otEXPECT_ACTION(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aOutput && aOutputLength, error = OT_ERROR_INVALID_ARGS); for (uint16_t length = 0; length < aOutputLength; length++) { diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index 1bbd24b27..64183dd1d 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -55,8 +55,8 @@ #include #include -#include -#include +#include "common/code_utils.hpp" +#include "common/logging.hpp" #ifndef SOCKET_UTILS_DEFAULT_SHELL #define SOCKET_UTILS_DEFAULT_SHELL "/bin/sh" @@ -222,7 +222,7 @@ otError HdlcInterface::Write(const uint8_t *aFrame, uint16_t aLength) { otError error = OT_ERROR_NONE; #if OPENTHREAD_POSIX_VIRTUAL_TIME - platformSimSendRadioSpinelWriteEvent(aFrame, aLength); + virtualTimeSendRadioSpinelWriteEvent(aFrame, aLength); #else while (aLength) { @@ -256,8 +256,8 @@ otError HdlcInterface::WaitForFrame(const struct timeval &aTimeout) #if OPENTHREAD_POSIX_VIRTUAL_TIME struct Event event; - platformSimSendSleepEvent(&timeout); - platformSimReceiveEvent(&event); + virtualTimeSendSleepEvent(&timeout); + virtualTimeReceiveEvent(&event); switch (event.mEvent) { diff --git a/src/posix/platform/logging.c b/src/posix/platform/logging.cpp similarity index 100% rename from src/posix/platform/logging.c rename to src/posix/platform/logging.cpp diff --git a/src/posix/platform/misc.c b/src/posix/platform/misc.cpp similarity index 89% rename from src/posix/platform/misc.c rename to src/posix/platform/misc.cpp index 649d7b250..e26e96eec 100644 --- a/src/posix/platform/misc.c +++ b/src/posix/platform/misc.cpp @@ -37,7 +37,7 @@ #include -#include "code_utils.h" +#include "common/code_utils.hpp" #include "common/logging.hpp" static otPlatResetReason sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON; @@ -89,12 +89,12 @@ int SocketWithCloseExec(int aDomain, int aType, int aProtocol) int fd = -1; #ifdef __APPLE__ - otEXPECT_ACTION((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); + VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); - otEXPECT_ACTION((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)")); - otEXPECT_ACTION((rval = fcntl(fd, F_SETFD, rval | FD_CLOEXEC)) != -1, perror("fcntl(F_SETFD)")); + VerifyOrExit((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)")); + VerifyOrExit((rval = fcntl(fd, F_SETFD, rval | FD_CLOEXEC)) != -1, perror("fcntl(F_SETFD)")); #else - otEXPECT_ACTION((fd = socket(aDomain, aType | SOCK_CLOEXEC, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); + VerifyOrExit((fd = socket(aDomain, aType | SOCK_CLOEXEC, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); #endif exit: diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index f5c60bf78..70f6a5c14 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -38,7 +38,7 @@ #include #include #include -#if __linux__ +#ifdef __linux__ #include #include #include diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 08b687702..d61040dae 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -305,13 +305,13 @@ void platformNetifProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, c * This function initialize virtual time simulation. * */ -void platformSimInit(void); +void virtualTimeInit(void); /** * This function deinitialize virtual time simulation. * */ -void platformSimDeinit(void); +void virtualTimeDeinit(void); /** * This function performs virtual time simulation processing. @@ -321,7 +321,7 @@ void platformSimDeinit(void); * @param[in] aWriteFdSet A pointer to the write file descriptors. * */ -void platformSimProcess(otInstance * aInstance, +void virtualTimeProcess(otInstance * aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet); @@ -337,7 +337,7 @@ void platformSimProcess(otInstance * aInstance, * @param[inout] aTimeout A pointer to the timeout. * */ -void platformSimUpdateFdSet(fd_set * aReadFdSet, +void virtualTimeUpdateFdSet(fd_set * aReadFdSet, fd_set * aWriteFdSet, fd_set * aErrorFdSet, int * aMaxFd, @@ -350,7 +350,7 @@ void platformSimUpdateFdSet(fd_set * aReadFdSet, * @param[in] aLength Length of the spinel frame. * */ -void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength); +void virtualTimeSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength); /** * This function receives an event of virtual time simulation. @@ -358,7 +358,7 @@ void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength * @param[out] aEvent A pointer to the event receiving the event. * */ -void platformSimReceiveEvent(struct Event *aEvent); +void virtualTimeReceiveEvent(struct Event *aEvent); /** * This function sends sleep event through virtual time simulation. @@ -366,7 +366,7 @@ void platformSimReceiveEvent(struct Event *aEvent); * @param[in] aTimeout A pointer to the time sleeping. * */ -void platformSimSendSleepEvent(const struct timeval *aTimeout); +void virtualTimeSendSleepEvent(const struct timeval *aTimeout); /** * This function performs radio spinel processing of virtual time simulation. @@ -375,7 +375,7 @@ void platformSimSendSleepEvent(const struct timeval *aTimeout); * @param[in] aEvent A pointer to the current event. * */ -void platformSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent); +void virtualTimeRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent); /** * This function gets system time in microseconds without applying speed up factor. diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index a0dd5c09a..2372ba5a1 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -48,19 +48,20 @@ #include #include -#include -#include -#include -#include -#include -#include -#include #include #include #include #include #include +#include "common/code_utils.hpp" +#include "common/encoding.hpp" +#include "common/logging.hpp" +#include "common/new.hpp" +#include "common/settings.hpp" +#include "meshcop/dataset.hpp" +#include "meshcop/meshcop_tlvs.hpp" + #ifndef TX_WAIT_US #define TX_WAIT_US (5 * US_PER_S) #endif @@ -1888,7 +1889,7 @@ void ot::PosixApp::RadioSpinel::Process(const Event &aEvent) } } -void platformSimRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent) +void virtualTimeRadioSpinelProcess(otInstance *aInstance, const struct Event *aEvent) { sRadioSpinel.Process(*aEvent); OT_UNUSED_VARIABLE(aInstance); diff --git a/src/posix/platform/spi_interface.cpp b/src/posix/platform/spi_interface.cpp index 121079236..9dd4087c9 100644 --- a/src/posix/platform/spi_interface.cpp +++ b/src/posix/platform/spi_interface.cpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include diff --git a/src/posix/platform/system.c b/src/posix/platform/system.cpp similarity index 96% rename from src/posix/platform/system.c rename to src/posix/platform/system.cpp index 381695a90..eeaec5835 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.cpp @@ -49,7 +49,7 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig) otInstance *instance = NULL; #if OPENTHREAD_POSIX_VIRTUAL_TIME - platformSimInit(); + virtualTimeInit(); #endif platformAlarmInit(aPlatformConfig->mSpeedUpFactor); platformRadioInit(aPlatformConfig); @@ -70,7 +70,7 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig) void otSysDeinit(void) { #if OPENTHREAD_POSIX_VIRTUAL_TIME - platformSimDeinit(); + virtualTimeDeinit(); #endif platformRadioDeinit(); } @@ -121,7 +121,7 @@ void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop) &aMainloop->mMaxFd); #endif #if OPENTHREAD_POSIX_VIRTUAL_TIME - platformSimUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet, &aMainloop->mMaxFd, + virtualTimeUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet, &aMainloop->mMaxFd, &aMainloop->mTimeout); #else platformRadioUpdateFdSet(&aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mMaxFd, &aMainloop->mTimeout); @@ -160,7 +160,7 @@ int otSysMainloopPoll(otSysMainloopContext *aMainloop) if (noWrite) { - platformSimSendSleepEvent(&aMainloop->mTimeout); + virtualTimeSendSleepEvent(&aMainloop->mTimeout); } rval = select(aMainloop->mMaxFd + 1, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, @@ -180,7 +180,7 @@ int otSysMainloopPoll(otSysMainloopContext *aMainloop) void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop) { #if OPENTHREAD_POSIX_VIRTUAL_TIME - platformSimProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet); + virtualTimeProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet, &aMainloop->mErrorFdSet); #else platformRadioProcess(aInstance, &aMainloop->mReadFdSet, &aMainloop->mWriteFdSet); #endif diff --git a/src/posix/platform/uart.c b/src/posix/platform/uart.cpp similarity index 96% rename from src/posix/platform/uart.c rename to src/posix/platform/uart.cpp index af621181e..8b22f7e97 100644 --- a/src/posix/platform/uart.c +++ b/src/posix/platform/uart.cpp @@ -45,7 +45,6 @@ #include -#include "code_utils.h" #include "common/code_utils.hpp" #define OPENTHREAD_POSIX_APP_SOCKET_LOCK OPENTHREAD_POSIX_APP_SOCKET_BASENAME ".lock" @@ -68,7 +67,7 @@ otError otPlatUartEnable(void) int ret; // This allows implementing pseudo reset. - otEXPECT(sUartSocket == -1); + VerifyOrExit(sUartSocket == -1); sUartSocket = SocketWithCloseExec(AF_UNIX, SOCK_STREAM, 0); @@ -154,7 +153,7 @@ otError otPlatUartSend(const uint8_t *aBuf, uint16_t aBufLength) otError error = OT_ERROR_NONE; assert(sEnabled); - otEXPECT_ACTION(sWriteLength == 0, error = OT_ERROR_BUSY); + VerifyOrExit(sWriteLength == 0, error = OT_ERROR_BUSY); sWriteBuffer = aBuf; sWriteLength = aBufLength; @@ -170,7 +169,7 @@ otError otPlatUartFlush(void) void platformUartUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd) { - otEXPECT(sEnabled); + VerifyOrExit(sEnabled); if (aReadFdSet != NULL) { @@ -222,7 +221,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co ssize_t rval; int fd; - otEXPECT(sEnabled); + VerifyOrExit(sEnabled); #if OPENTHREAD_ENABLE_POSIX_APP_DAEMON if (FD_ISSET(sUartSocket, aErrorFdSet)) { @@ -241,7 +240,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co otPlatUartSendDone(); } - otEXPECT(sSessionSocket != -1); + VerifyOrExit(sSessionSocket != -1); if (FD_ISSET(sSessionSocket, aErrorFdSet)) { @@ -249,7 +248,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co sSessionSocket = -1; } - otEXPECT(sSessionSocket != -1); + VerifyOrExit(sSessionSocket != -1); fd = sSessionSocket; #else // OPENTHREAD_ENABLE_POSIX_APP_DAEMON @@ -285,7 +284,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co } close(sSessionSocket); sSessionSocket = -1; - otEXIT_NOW(); + ExitNow(); #else DieNowWithMessage("UART read", (rval < 0) ? OT_EXIT_ERROR_ERRNO : OT_EXIT_FAILURE); #endif @@ -306,13 +305,13 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co perror("UART write"); close(sSessionSocket); sSessionSocket = -1; - otEXIT_NOW(); + ExitNow(); #else DieNowWithMessage("UART write", OT_EXIT_ERROR_ERRNO); #endif } - otEXPECT(rval > 0); + VerifyOrExit(rval > 0); sWriteBuffer += (uint16_t)rval; sWriteLength -= (uint16_t)rval; diff --git a/src/posix/platform/sim.c b/src/posix/platform/virtual_time.cpp similarity index 89% rename from src/posix/platform/sim.c rename to src/posix/platform/virtual_time.cpp index 7d03e6cfe..1b0ef2b24 100644 --- a/src/posix/platform/sim.c +++ b/src/posix/platform/virtual_time.cpp @@ -53,7 +53,7 @@ static int sSockFd = -1; ///< Socket used to communicating with simulat static uint16_t sPortOffset = 0; ///< Port offset for simulation. static int sNodeId = 0; ///< Node id of this simulated device. -void platformSimInit(void) +void virtualTimeInit(void) { struct sockaddr_in sockaddr; char * offset; @@ -100,7 +100,7 @@ void platformSimInit(void) } } -void platformSimDeinit(void) +void virtualTimeDeinit(void) { if (sSockFd != -1) { @@ -109,7 +109,7 @@ void platformSimDeinit(void) } } -static void platformSimSendEvent(struct Event *aEvent, size_t aLength) +static void virtualTimeSendEvent(struct Event *aEvent, size_t aLength) { ssize_t rval; struct sockaddr_in sockaddr; @@ -127,7 +127,7 @@ static void platformSimSendEvent(struct Event *aEvent, size_t aLength) } } -void platformSimReceiveEvent(struct Event *aEvent) +void virtualTimeReceiveEvent(struct Event *aEvent) { ssize_t rval = recvfrom(sSockFd, aEvent, sizeof(*aEvent), 0, NULL, NULL); @@ -139,7 +139,7 @@ void platformSimReceiveEvent(struct Event *aEvent) sNow += aEvent->mDelay; } -void platformSimSendSleepEvent(const struct timeval *aTimeout) +void virtualTimeSendSleepEvent(const struct timeval *aTimeout) { struct Event event; @@ -147,10 +147,10 @@ void platformSimSendSleepEvent(const struct timeval *aTimeout) event.mEvent = OT_SIM_EVENT_ALARM_FIRED; event.mDataLength = 0; - platformSimSendEvent(&event, offsetof(struct Event, mData)); + virtualTimeSendEvent(&event, offsetof(struct Event, mData)); } -void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength) +void virtualTimeSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength) { struct Event event; @@ -160,10 +160,10 @@ void platformSimSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength memcpy(event.mData, aData, aLength); - platformSimSendEvent(&event, offsetof(struct Event, mData) + event.mDataLength); + virtualTimeSendEvent(&event, offsetof(struct Event, mData) + event.mDataLength); } -void platformSimUpdateFdSet(fd_set * aReadFdSet, +void virtualTimeUpdateFdSet(fd_set * aReadFdSet, fd_set * aWriteFdSet, fd_set * aErrorFdSet, int * aMaxFd, @@ -180,12 +180,14 @@ void platformSimUpdateFdSet(fd_set * aReadFdSet, } } -void platformSimProcess(otInstance * aInstance, +void virtualTimeProcess(otInstance * aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet) { - struct Event event = {0}; + struct Event event; + + memset(&event, 0, sizeof(event)); OT_UNUSED_VARIABLE(aInstance); OT_UNUSED_VARIABLE(aWriteFdSet); @@ -193,10 +195,10 @@ void platformSimProcess(otInstance * aInstance, if (FD_ISSET(sSockFd, aReadFdSet)) { - platformSimReceiveEvent(&event); + virtualTimeReceiveEvent(&event); } - platformSimRadioSpinelProcess(aInstance, &event); + virtualTimeRadioSpinelProcess(aInstance, &event); } uint64_t platformGetTime(void)