Loading threadnetwork/aidl/default/socket_interface.cpp +39 −18 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ #include <vector> #include "common/code_utils.hpp" #include "openthread/error.h" #include "openthread/openthread-system.h" #include "platform-posix.h" Loading @@ -56,14 +57,9 @@ SocketInterface::SocketInterface(const ot::Url::Url& aRadioUrl) otError SocketInterface::Init(ReceiveFrameCallback aCallback, void* aCallbackContext, RxFrameBuffer& aFrameBuffer) { otError error = OT_ERROR_NONE; otError error = InitSocket(); VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY); WaitForSocketFileCreated(mRadioUrl.GetPath()); mSockFd = OpenFile(mRadioUrl); VerifyOrExit(mSockFd != -1, error = OT_ERROR_FAILED); VerifyOrExit(error == OT_ERROR_NONE); mReceiveFrameCallback = aCallback; mReceiveFrameContext = aCallbackContext; Loading Loading @@ -155,9 +151,22 @@ otError SocketInterface::WaitForHardwareResetCompletion(uint32_t aTimeoutMs) { VerifyOrExit(!mIsHardwareResetting, error = OT_ERROR_FAILED); exit: return error; } otError SocketInterface::InitSocket() { otError error = OT_ERROR_NONE; int retries = 0; VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY); while (retries++ < kMaxRetriesForSocketInit) { WaitForSocketFileCreated(mRadioUrl.GetPath()); mSockFd = OpenFile(mRadioUrl); VerifyOrExit(mSockFd != -1, error = OT_ERROR_FAILED); VerifyOrExit(mSockFd == -1); } error = OT_ERROR_FAILED; exit: return error; Loading @@ -168,11 +177,16 @@ void SocketInterface::UpdateFdSet(void* aMainloopContext) { assert(context != nullptr); VerifyOrExit(mSockFd != -1); FD_SET(mSockFd, &context->mReadFdSet); if (context->mMaxFd < mSockFd) { context->mMaxFd = mSockFd; } exit: return; } void SocketInterface::Process(const void* aMainloopContext) { Loading @@ -181,9 +195,14 @@ void SocketInterface::Process(const void* aMainloopContext) { assert(context != nullptr); VerifyOrExit(mSockFd != -1); if (FD_ISSET(mSockFd, &context->mReadFdSet)) { Read(); } exit: return; } otError SocketInterface::HardwareReset(void) { Loading @@ -198,22 +217,24 @@ otError SocketInterface::HardwareReset(void) { void SocketInterface::Read(void) { uint8_t buffer[kMaxFrameSize]; ssize_t rval; VerifyOrExit(mSockFd != -1); ssize_t rval = TEMP_FAILURE_RETRY(read(mSockFd, buffer, sizeof(buffer))); rval = TEMP_FAILURE_RETRY(read(mSockFd, buffer, sizeof(buffer))); if (rval > 0) { ProcessReceivedData(buffer, static_cast<uint16_t>(rval)); } else if (rval < 0) { DieNow(OT_EXIT_ERROR_ERRNO); } else { if (mIsHardwareResetting) { LogInfo("Socket connection is closed due to hardware reset."); LogWarn("Socket connection is closed by remote, isHardwareReset: %d", mIsHardwareResetting); ResetStates(); } else { LogCrit("Socket connection is closed by remote."); exit(OT_EXIT_FAILURE); } InitSocket(); } exit: return; } void SocketInterface::Write(const uint8_t* aFrame, uint16_t aLength) { Loading threadnetwork/aidl/default/socket_interface.hpp +10 −0 Original line number Diff line number Diff line Loading @@ -246,6 +246,15 @@ class SocketInterface : public ot::Spinel::SpinelInterface, */ otError WaitForHardwareResetCompletion(uint32_t aTimeoutMs); /** * Initialize socket * * @retval TRUE Socket initialization is successful. * @retval FALSE Socket initialization is failed. * */ otError InitSocket(); /** * Reset socket interface to intitial state. * Loading @@ -257,6 +266,7 @@ class SocketInterface : public ot::Spinel::SpinelInterface, ///< descriptor to become available. kMaxRetriesForSocketCloseCheck = 3, ///< Maximum retry times for checking ///< if socket is closed. kMaxRetriesForSocketInit = 3, ///< Maximum retry times for socket initialization. }; ReceiveFrameCallback mReceiveFrameCallback; Loading Loading
threadnetwork/aidl/default/socket_interface.cpp +39 −18 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ #include <vector> #include "common/code_utils.hpp" #include "openthread/error.h" #include "openthread/openthread-system.h" #include "platform-posix.h" Loading @@ -56,14 +57,9 @@ SocketInterface::SocketInterface(const ot::Url::Url& aRadioUrl) otError SocketInterface::Init(ReceiveFrameCallback aCallback, void* aCallbackContext, RxFrameBuffer& aFrameBuffer) { otError error = OT_ERROR_NONE; otError error = InitSocket(); VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY); WaitForSocketFileCreated(mRadioUrl.GetPath()); mSockFd = OpenFile(mRadioUrl); VerifyOrExit(mSockFd != -1, error = OT_ERROR_FAILED); VerifyOrExit(error == OT_ERROR_NONE); mReceiveFrameCallback = aCallback; mReceiveFrameContext = aCallbackContext; Loading Loading @@ -155,9 +151,22 @@ otError SocketInterface::WaitForHardwareResetCompletion(uint32_t aTimeoutMs) { VerifyOrExit(!mIsHardwareResetting, error = OT_ERROR_FAILED); exit: return error; } otError SocketInterface::InitSocket() { otError error = OT_ERROR_NONE; int retries = 0; VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY); while (retries++ < kMaxRetriesForSocketInit) { WaitForSocketFileCreated(mRadioUrl.GetPath()); mSockFd = OpenFile(mRadioUrl); VerifyOrExit(mSockFd != -1, error = OT_ERROR_FAILED); VerifyOrExit(mSockFd == -1); } error = OT_ERROR_FAILED; exit: return error; Loading @@ -168,11 +177,16 @@ void SocketInterface::UpdateFdSet(void* aMainloopContext) { assert(context != nullptr); VerifyOrExit(mSockFd != -1); FD_SET(mSockFd, &context->mReadFdSet); if (context->mMaxFd < mSockFd) { context->mMaxFd = mSockFd; } exit: return; } void SocketInterface::Process(const void* aMainloopContext) { Loading @@ -181,9 +195,14 @@ void SocketInterface::Process(const void* aMainloopContext) { assert(context != nullptr); VerifyOrExit(mSockFd != -1); if (FD_ISSET(mSockFd, &context->mReadFdSet)) { Read(); } exit: return; } otError SocketInterface::HardwareReset(void) { Loading @@ -198,22 +217,24 @@ otError SocketInterface::HardwareReset(void) { void SocketInterface::Read(void) { uint8_t buffer[kMaxFrameSize]; ssize_t rval; VerifyOrExit(mSockFd != -1); ssize_t rval = TEMP_FAILURE_RETRY(read(mSockFd, buffer, sizeof(buffer))); rval = TEMP_FAILURE_RETRY(read(mSockFd, buffer, sizeof(buffer))); if (rval > 0) { ProcessReceivedData(buffer, static_cast<uint16_t>(rval)); } else if (rval < 0) { DieNow(OT_EXIT_ERROR_ERRNO); } else { if (mIsHardwareResetting) { LogInfo("Socket connection is closed due to hardware reset."); LogWarn("Socket connection is closed by remote, isHardwareReset: %d", mIsHardwareResetting); ResetStates(); } else { LogCrit("Socket connection is closed by remote."); exit(OT_EXIT_FAILURE); } InitSocket(); } exit: return; } void SocketInterface::Write(const uint8_t* aFrame, uint16_t aLength) { Loading
threadnetwork/aidl/default/socket_interface.hpp +10 −0 Original line number Diff line number Diff line Loading @@ -246,6 +246,15 @@ class SocketInterface : public ot::Spinel::SpinelInterface, */ otError WaitForHardwareResetCompletion(uint32_t aTimeoutMs); /** * Initialize socket * * @retval TRUE Socket initialization is successful. * @retval FALSE Socket initialization is failed. * */ otError InitSocket(); /** * Reset socket interface to intitial state. * Loading @@ -257,6 +266,7 @@ class SocketInterface : public ot::Spinel::SpinelInterface, ///< descriptor to become available. kMaxRetriesForSocketCloseCheck = 3, ///< Maximum retry times for checking ///< if socket is closed. kMaxRetriesForSocketInit = 3, ///< Maximum retry times for socket initialization. }; ReceiveFrameCallback mReceiveFrameCallback; Loading