diff --git a/threadnetwork/aidl/default/Android.bp b/threadnetwork/aidl/default/Android.bp index 736e808b49..8b938d2cb3 100644 --- a/threadnetwork/aidl/default/Android.bp +++ b/threadnetwork/aidl/default/Android.bp @@ -29,6 +29,7 @@ cc_defaults { "openthread-hdlc", "openthread-platform", "openthread-posix", + "openthread-spi", "openthread-url", ], @@ -68,6 +69,7 @@ cc_fuzz { "openthread-hdlc", "openthread-platform", "openthread-posix", + "openthread-spi", "openthread-url", ], diff --git a/threadnetwork/aidl/default/thread_chip.cpp b/threadnetwork/aidl/default/thread_chip.cpp index e542c7c335..94d1e93cbe 100644 --- a/threadnetwork/aidl/default/thread_chip.cpp +++ b/threadnetwork/aidl/default/thread_chip.cpp @@ -21,17 +21,36 @@ #include #include +#include "hdlc_interface.hpp" +#include "spi_interface.hpp" + namespace aidl { namespace android { namespace hardware { namespace threadnetwork { -ThreadChip::ThreadChip(char* url) - : mUrl(), - mInterface(handleReceivedFrame, this, mRxFrameBuffer), - mRxFrameBuffer(), - mCallback(nullptr) { +ThreadChip::ThreadChip(char* url) : mUrl(), mRxFrameBuffer(), mCallback(nullptr) { + static const char kHdlcProtocol[] = "spinel+hdlc"; + static const char kSpiProtocol[] = "spinel+spi"; + const char* protocol; + CHECK_EQ(mUrl.Init(url), 0); + + protocol = mUrl.GetProtocol(); + CHECK_NE(protocol, nullptr); + + if (memcmp(protocol, kSpiProtocol, strlen(kSpiProtocol)) == 0) { + mSpinelInterface = std::make_shared(handleReceivedFrameJump, this, + mRxFrameBuffer); + } else if (memcmp(protocol, kHdlcProtocol, strlen(kHdlcProtocol)) == 0) { + mSpinelInterface = std::make_shared(handleReceivedFrameJump, this, + mRxFrameBuffer); + } else { + ALOGE("The protocol \"%s\" is not supported!", protocol); + exit(1); + } + + CHECK_NE(mSpinelInterface, nullptr); } void ThreadChip::clientDeathCallback(void* context) { @@ -43,7 +62,7 @@ void ThreadChip::clientDeathCallback(void) { close(); } -void ThreadChip::handleReceivedFrame(void* context) { +void ThreadChip::handleReceivedFrameJump(void* context) { static_cast(context)->handleReceivedFrame(); } @@ -70,7 +89,7 @@ ndk::ScopedAStatus ThreadChip::open(const std::shared_ptr& mBinderDeathRecipient = AIBinder_DeathRecipient_new(clientDeathCallback); VerifyOrExit(AIBinder_linkToDeath(binder, mBinderDeathRecipient, this) == STATUS_OK, status = errorStatus(ERROR_FAILED, "Failed to link the binder to death")); - VerifyOrExit(mInterface.Init(mUrl) == OT_ERROR_NONE, + VerifyOrExit(mSpinelInterface->Init(mUrl) == OT_ERROR_NONE, status = errorStatus(ERROR_FAILED, "Failed to initialize the interface")); mCallback = in_callback; @@ -94,7 +113,7 @@ exit: ndk::ScopedAStatus ThreadChip::close() { VerifyOrExit(mCallback != nullptr); mCallback = nullptr; - mInterface.Deinit(); + mSpinelInterface->Deinit(); ot::Posix::Mainloop::Manager::Get().Remove(*this); @@ -113,8 +132,8 @@ ndk::ScopedAStatus ThreadChip::sendSpinelFrame(const std::vector& in_fr VerifyOrExit(mCallback != nullptr, status = errorStatus(ERROR_FAILED, "The interface is not open")); - error = mInterface.SendFrame(reinterpret_cast(in_frame.data()), - in_frame.size()); + error = mSpinelInterface->SendFrame(reinterpret_cast(in_frame.data()), + in_frame.size()); if (error == OT_ERROR_NONE) { status = ndk::ScopedAStatus::ok(); } else if (error == OT_ERROR_NO_BUFS) { @@ -134,20 +153,20 @@ exit: } ndk::ScopedAStatus ThreadChip::reset() { - mInterface.HardwareReset(); + mSpinelInterface->HardwareReset(); ALOGI("reset()"); return ndk::ScopedAStatus::ok(); } void ThreadChip::Update(otSysMainloopContext& context) { if (mCallback != nullptr) { - mInterface.UpdateFdSet(&context); + mSpinelInterface->UpdateFdSet(&context); } } void ThreadChip::Process(const otSysMainloopContext& context) { if (mCallback != nullptr) { - mInterface.Process(&context); + mSpinelInterface->Process(&context); } } diff --git a/threadnetwork/aidl/default/thread_chip.hpp b/threadnetwork/aidl/default/thread_chip.hpp index d93dfef22e..294190a658 100644 --- a/threadnetwork/aidl/default/thread_chip.hpp +++ b/threadnetwork/aidl/default/thread_chip.hpp @@ -19,7 +19,6 @@ #include #include -#include "hdlc_interface.hpp" #include "lib/spinel/spinel_interface.hpp" #include "mainloop.hpp" @@ -45,12 +44,12 @@ class ThreadChip : public BnThreadChip, ot::Posix::Mainloop::Source { private: static void clientDeathCallback(void* context); void clientDeathCallback(void); - static void handleReceivedFrame(void* context); + static void handleReceivedFrameJump(void* context); void handleReceivedFrame(void); ndk::ScopedAStatus errorStatus(int32_t error, const char* message); ot::Url::Url mUrl; - ot::Posix::HdlcInterface mInterface; + std::shared_ptr mSpinelInterface; ot::Spinel::SpinelInterface::RxFrameBuffer mRxFrameBuffer; std::shared_ptr mCallback; AIBinder_DeathRecipient* mBinderDeathRecipient; diff --git a/threadnetwork/aidl/default/utils.cpp b/threadnetwork/aidl/default/utils.cpp index b4da7d76f5..1cb42ec624 100644 --- a/threadnetwork/aidl/default/utils.cpp +++ b/threadnetwork/aidl/default/utils.cpp @@ -36,6 +36,45 @@ void otLogWarnPlat(const char* format, ...) { va_end(args); } +void otLogNotePlat(const char* format, ...) { + va_list args; + + va_start(args, format); + __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args); + va_end(args); +} + +void otLogInfoPlat(const char* format, ...) { + va_list args; + + va_start(args, format); + __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, format, args); + va_end(args); +} + +void otLogDebgPlat(const char* format, ...) { + va_list args; + + va_start(args, format); + __android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, format, args); + va_end(args); +} + +void otDumpDebgPlat(const char* aText, const void* aData, uint16_t aDataLength) { + constexpr uint16_t kBufSize = 512; + char buf[kBufSize]; + + if ((aText != nullptr) && (aData != nullptr)) { + const uint8_t* data = reinterpret_cast(aData); + + for (uint16_t i = 0; (i < aDataLength) && (i < (kBufSize - 1) / 3); i++) { + snprintf(buf + (i * 3), (kBufSize - 1) - (i * 3), "%02x ", data[i]); + } + + __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s: %s", aText, buf); + } +} + OT_TOOL_WEAK void otPlatAlarmMilliFired(otInstance* aInstance) { OT_UNUSED_VARIABLE(aInstance); }