diff --git a/configure.ac b/configure.ac index 197eb337d..b432a233a 100644 --- a/configure.ac +++ b/configure.ac @@ -425,11 +425,11 @@ AC_SUBST(OPENTHREAD_ENABLE_CLI_MTD) # NCP app # AC_ARG_ENABLE(ncp-app, - [AS_HELP_STRING([--enable-ncp-app],[Enable NCP support (no|mtd|ftd|all) @<:@default=no@:>@.])], + [AS_HELP_STRING([--enable-ncp-app],[Enable NCP support (no|mtd|ftd|radio|all) @<:@default=no@:>@.])], [ # Map all & both to yes case "${enableval}" in - all|both) + all) enableval=yes ;; @@ -444,18 +444,28 @@ AC_ARG_ENABLE(ncp-app, enable_ncp_app=${enableval} enable_ncp_app_mtd=${enableval} enable_ncp_app_ftd=${enableval} + enable_ncp_app_radio=${enableval} ;; mtd) enable_ncp_app=yes enable_ncp_app_mtd=yes enable_ncp_app_ftd=no + enable_ncp_app_radio=no ;; ftd) enable_ncp_app=yes enable_ncp_app_mtd=no enable_ncp_app_ftd=yes + enable_ncp_app_radio=no + ;; + + radio) + enable_ncp_app=yes + enable_ncp_app_mtd=no + enable_ncp_app_ftd=no + enable_ncp_app_radio=yes ;; *) @@ -467,6 +477,7 @@ AC_ARG_ENABLE(ncp-app, enable_ncp_app=no enable_ncp_app_ftd=no enable_ncp_app_mtd=no + enable_ncp_app_radio=no ] ) @@ -477,14 +488,18 @@ AC_MSG_CHECKING([should NCP support ftd]) AC_MSG_RESULT(${enable_ncp_app_ftd}) AC_MSG_CHECKING([should NCP support mtd]) AC_MSG_RESULT(${enable_ncp_app_mtd}) +AC_MSG_CHECKING([should NCP support radio-only]) +AC_MSG_RESULT(${enable_ncp_app_radio}) AC_SUBST(OPENTHREAD_ENABLE_NCP) AC_SUBST(OPENTHREAD_ENABLE_NCP_MTD) AC_SUBST(OPENTHREAD_ENABLE_NCP_FTD) +AC_SUBST(OPENTHREAD_ENABLE_NCP_RADIO) -AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP], [test "${enable_ncp_app}" == "yes"]) -AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP_MTD], [test "${enable_ncp_app_mtd}" == "yes"]) -AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP_FTD], [test "${enable_ncp_app_ftd}" == "yes"]) +AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP], [test "${enable_ncp_app}" == "yes"]) +AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP_MTD], [test "${enable_ncp_app_mtd}" == "yes"]) +AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP_FTD], [test "${enable_ncp_app_ftd}" == "yes"]) +AM_CONDITIONAL([OPENTHREAD_ENABLE_NCP_RADIO], [test "${enable_ncp_app_radio}" == "yes"]) # # NCP BUS - how does the NCP talk to the host? diff --git a/examples/apps/ncp/Makefile.am b/examples/apps/ncp/Makefile.am index b9556f5c9..802e814e2 100644 --- a/examples/apps/ncp/Makefile.am +++ b/examples/apps/ncp/Makefile.am @@ -51,8 +51,11 @@ SOURCES_COMMON += \ main.c \ $(NULL) +LDADD_MBEDTLS = \ + $(NULL) + if OPENTHREAD_ENABLE_BUILTIN_MBEDTLS -LDADD_COMMON += \ +LDADD_MBEDTLS += \ $(top_builddir)/third_party/mbedtls/libmbedcrypto.a \ $(NULL) endif # OPENTHREAD_ENABLE_BUILTIN_MBEDTLS @@ -83,8 +86,10 @@ ot_ncp_ftd_LDADD = \ $(top_builddir)/src/ncp/libopenthread-ncp-ftd.a \ $(top_builddir)/src/core/libopenthread-ftd.a \ $(LDADD_COMMON) \ + $(LDADD_MBEDTLS) \ $(top_builddir)/src/core/libopenthread-ftd.a \ $(LDADD_COMMON) \ + $(LDADD_MBEDTLS) \ $(NULL) ot_ncp_ftd_LDFLAGS = \ @@ -113,8 +118,10 @@ ot_ncp_mtd_LDADD = \ $(top_builddir)/src/ncp/libopenthread-ncp-mtd.a \ $(top_builddir)/src/core/libopenthread-mtd.a \ $(LDADD_COMMON) \ + $(LDADD_MBEDTLS) \ $(top_builddir)/src/core/libopenthread-mtd.a \ $(LDADD_COMMON) \ + $(LDADD_MBEDTLS) \ $(NULL) ot_ncp_mtd_LDFLAGS = \ @@ -129,9 +136,40 @@ ot_ncp_mtd_SOURCES = \ $(SOURCES_COMMON) \ $(NULL) +if OPENTHREAD_ENABLE_NCP_RADIO +bin_PROGRAMS += \ + ot-ncp-radio \ + $(NULL) +endif + +ot_ncp_radio_CPPFLAGS = \ + $(CPPFLAGS_COMMON) \ + $(NULL) + +ot_ncp_radio_LDADD = \ + $(top_builddir)/src/ncp/libopenthread-ncp-radio.a \ + $(top_builddir)/src/core/libopenthread-radio.a \ + $(LDADD_COMMON) \ + $(top_builddir)/src/core/libopenthread-radio.a \ + $(LDADD_COMMON) \ + $(NULL) + +ot_ncp_radio_LDFLAGS = \ + $(LDFLAGS_COMMON) \ + $(NULL) + +ot_ncp_radio_LIBTOOLFLAGS = \ + $(LIBTOOLFLAGS_COMMON) \ + $(NULL) + +ot_ncp_radio_SOURCES = \ + $(SOURCES_COMMON) \ + $(NULL) + if OPENTHREAD_ENABLE_LINKER_MAP ot_ncp_ftd_LDFLAGS += -Wl,-Map=ot-ncp-ftd.map ot_ncp_mtd_LDFLAGS += -Wl,-Map=ot-ncp-mtd.map +ot_ncp_radio_LDFLAGS += -Wl,-Map=ot-ncp-radio.map endif if OPENTHREAD_BUILD_COVERAGE diff --git a/examples/platforms/da15000/platform.c b/examples/platforms/da15000/platform.c index b093b7376..ad3cea081 100644 --- a/examples/platforms/da15000/platform.c +++ b/examples/platforms/da15000/platform.c @@ -143,6 +143,7 @@ bool PlatformPseudoResetWasRequested(void) return false; } +#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && (OPENTHREAD_ENABLE_COMMISSIONER || OPENTHREAD_ENABLE_JOINER) static sys_clk_t ClkGet(void) { sys_clk_t clk = sysclk_RC16; @@ -277,13 +278,16 @@ static void StateChangedCallback(uint32_t aFlags, void *aContext) } } } +#endif // (OPENTHREAD_MTD || OPENTHREAD_FTD) && (OPENTHREAD_ENABLE_COMMISSIONER || OPENTHREAD_ENABLE_JOINER) void PlatformProcessDrivers(otInstance *aInstance) { if (sInstance == NULL) { sInstance = aInstance; +#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && (OPENTHREAD_ENABLE_COMMISSIONER || OPENTHREAD_ENABLE_JOINER) otSetStateChangedCallback(aInstance, StateChangedCallback, 0); +#endif // (OPENTHREAD_MTD || OPENTHREAD_FTD) && (OPENTHREAD_ENABLE_COMMISSIONER || OPENTHREAD_ENABLE_JOINER) } da15000UartProcess(); diff --git a/examples/platforms/posix/openthread-core-posix-config.h b/examples/platforms/posix/openthread-core-posix-config.h index 0566539b1..0f664810b 100644 --- a/examples/platforms/posix/openthread-core-posix-config.h +++ b/examples/platforms/posix/openthread-core-posix-config.h @@ -53,6 +53,26 @@ #define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED #endif +#if OPENTHREAD_RADIO +/** + * @def OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT + * + * Define to 1 if you want to enable software ACK timeout logic. + * + */ +#define OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT 1 + +/** + * @def OPENTHREAD_CONFIG_ENABLE_SOFTWARE_RETRANSMIT + * + * Define to 1 if you want to enable software retransmission logic. + * + * Applicable only if raw link layer API is enabled (i.e., `OPENTHREAD_ENABLE_RAW_LINK_API` is set). + * + */ +#define OPENTHREAD_CONFIG_ENABLE_SOFTWARE_RETRANSMIT 1 +#endif // OPENTHREAD_RADIO + /** * @def OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER * diff --git a/include/openthread/link.h b/include/openthread/link.h index a8799ddc7..6d44e537b 100644 --- a/include/openthread/link.h +++ b/include/openthread/link.h @@ -292,6 +292,18 @@ OTAPI void OTCALL otLinkSetPollPeriod(otInstance *aInstance, uint32_t aPollPerio */ OTAPI otShortAddress OTCALL otLinkGetShortAddress(otInstance *aInstance); +/** + * Set the Short Address for address filtering. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aShortAddress The IEEE 802.15.4 Short Address. + * + * @retval OT_ERROR_NONE If successful. + * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. + * + */ +OTAPI otError OTCALL otLinkSetShortAddress(otInstance *aInstance, uint16_t aShortAddress); + /** * This function gets the address mode of MAC filter. * diff --git a/include/openthread/link_raw.h b/include/openthread/link_raw.h index 495d4fb8c..5226d3f7b 100644 --- a/include/openthread/link_raw.h +++ b/include/openthread/link_raw.h @@ -75,42 +75,6 @@ otError otLinkRawSetEnable(otInstance *aInstance, bool aEnabled); */ bool otLinkRawIsEnabled(otInstance *aInstance); -/** - * This function set the IEEE 802.15.4 PAN ID. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aPanId The IEEE 802.15.4 PAN ID. - * - * @retval OT_ERROR_NONE If successful. - * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. - * - */ -otError otLinkRawSetPanId(otInstance *aInstance, uint16_t aPanId); - -/** - * This function sets the IEEE 802.15.4 Extended Address. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aExtAddress A pointer to the IEEE 802.15.4 Extended Address. - * - * @retval OT_ERROR_NONE If successful. - * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. - * - */ -otError otLinkRawSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress); - -/** - * Set the Short Address for address filtering. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aShortAddress The IEEE 802.15.4 Short Address. - * - * @retval OT_ERROR_NONE If successful. - * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. - * - */ -otError otLinkRawSetShortAddress(otInstance *aInstance, uint16_t aShortAddress); - /** * This function gets the status of promiscuous mode. * @@ -163,14 +127,13 @@ typedef void(OTCALL *otLinkRawReceiveDone)(otInstance *aInstance, otRadioFrame * * Turn on the radio. * * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aChannel The channel to use for receiving. * @param[in] aCallback A pointer to a function called on receipt of a IEEE 802.15.4 frame. * * @retval OT_ERROR_NONE Successfully transitioned to Receive. * @retval OT_ERROR_INVALID_STATE The radio was disabled or transmitting. * */ -otError otLinkRawReceive(otInstance *aInstance, uint8_t aChannel, otLinkRawReceiveDone aCallback); +otError otLinkRawReceive(otInstance *aInstance, otLinkRawReceiveDone aCallback); /** * The radio transitions from Transmit to Receive. diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 06c55aa6d..8d86f072b 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -31,6 +31,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am lib_LIBRARIES = \ libopenthread-ftd.a \ libopenthread-mtd.a \ + libopenthread-radio.a \ $(NULL) CPPFLAGS_COMMON = \ @@ -38,6 +39,10 @@ CPPFLAGS_COMMON = \ $(OPENTHREAD_TARGET_DEFINES) \ $(NULL) +libopenthread_radio_a_CPPFLAGS = \ + $(CPPFLAGS_COMMON) \ + -DOPENTHREAD_RADIO=1 \ + $(NULL) libopenthread_ftd_a_CPPFLAGS = \ $(CPPFLAGS_COMMON) \ @@ -197,6 +202,20 @@ SOURCES_COMMON = \ utils/slaac_address.cpp \ $(NULL) +libopenthread_radio_a_SOURCES = \ + api/instance_api.cpp \ + api/link_raw_api.cpp \ + api/message_api.cpp \ + api/tasklet_api.cpp \ + common/instance.cpp \ + common/locator.cpp \ + common/logging.cpp \ + common/message.cpp \ + common/tasklet.cpp \ + common/timer.cpp \ + mac/mac_frame.cpp \ + thread/link_quality.cpp \ + $(NULL) libopenthread_mtd_a_SOURCES = \ $(SOURCES_COMMON) \ diff --git a/src/core/api/instance_api.cpp b/src/core/api/instance_api.cpp index fac04cac9..11b0deaea 100644 --- a/src/core/api/instance_api.cpp +++ b/src/core/api/instance_api.cpp @@ -36,8 +36,8 @@ #include "openthread-core-config.h" #include +#include #include -#include #include "common/instance.hpp" #include "common/logging.hpp" @@ -64,18 +64,34 @@ otInstance *otInstanceInitSingle(void) bool otInstanceIsInitialized(otInstance *aInstance) { +#if OPENTHREAD_MTD || OPENTHREAD_FTD Instance &instance = *static_cast(aInstance); return instance.IsInitialized(); +#else + OT_UNUSED_VARIABLE(aInstance); + return true; +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD } void otInstanceFinalize(otInstance *aInstance) { +#if OPENTHREAD_MTD || OPENTHREAD_FTD Instance &instance = *static_cast(aInstance); - instance.Finalize(); +#else + OT_UNUSED_VARIABLE(aInstance); +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD } +void otInstanceReset(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + instance.Reset(); +} + +#if OPENTHREAD_MTD || OPENTHREAD_FTD otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext) { Instance &instance = *static_cast(aInstance); @@ -90,13 +106,6 @@ void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback a instance.GetNotifier().RemoveCallback(aCallback, aContext); } -void otInstanceReset(otInstance *aInstance) -{ - Instance &instance = *static_cast(aInstance); - - instance.Reset(); -} - void otInstanceFactoryReset(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); @@ -110,6 +119,7 @@ otError otInstanceErasePersistentInfo(otInstance *aInstance) return instance.ErasePersistentInfo(); } +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD otLogLevel otGetDynamicLogLevel(otInstance *aInstance) { @@ -143,3 +153,38 @@ otError otSetDynamicLogLevel(otInstance *aInstance, otLogLevel aLogLevel) return error; } + +const char *otGetVersionString(void) +{ +/** + * PLATFORM_VERSION_ATTR_PREFIX and PLATFORM_VERSION_ATTR_SUFFIX are + * intended to be used to specify compiler directives to indicate + * what linker section the platform version string should be stored. + * + * This is useful for specifying an exact locaiton of where the version + * string will be located so that it can be easily retrieved from the + * raw firmware image. + * + * If PLATFORM_VERSION_ATTR_PREFIX is unspecified, the keyword `static` + * is used instead. + * + * If both are unspecified, the location of the string in the firmware + * image will be undefined and may change. + */ + +#ifdef PLATFORM_VERSION_ATTR_PREFIX + PLATFORM_VERSION_ATTR_PREFIX +#else + static +#endif + const char sVersion[] = PACKAGE_NAME "/" PACKAGE_VERSION "; " OPENTHREAD_CONFIG_PLATFORM_INFO +#if defined(__DATE__) + "; " __DATE__ " " __TIME__ +#endif +#ifdef PLATFORM_VERSION_ATTR_SUFFIX + PLATFORM_VERSION_ATTR_SUFFIX +#endif + ; // Trailing semicolon to end statement. + + return sVersion; +} diff --git a/src/core/api/link_raw.hpp b/src/core/api/link_raw.hpp index 005512aa7..343c8a051 100644 --- a/src/core/api/link_raw.hpp +++ b/src/core/api/link_raw.hpp @@ -55,72 +55,190 @@ public: /** * This constructor initializes the object. * + * @param[in] aInstance A reference to the OpenThread instance. + * */ explicit LinkRaw(Instance &aInstance); /** * This method returns true if the raw link-layer is enabled. * + * @returns true if enabled, false otherwise. + * */ - bool IsEnabled() { return mEnabled; } + bool IsEnabled(void) const { return mEnabled; } /** * This method enables/disables the raw link-layer. * + * @param[in] aEnabled Whether enable raw link-layer. + * + * @retval OT_ERROR_INVALID_STATE Thread stack is enabled. + * @retval OT_ERROR_NONE Successfully enabled raw link. + * */ - void SetEnabled(bool aEnabled) { mEnabled = aEnabled; } + otError SetEnabled(bool aEnabled); /** * This method returns the capabilities of the raw link-layer. * + * @returns The radio capability bit vector. The stack enables or disables some functions based on this value. + * */ - otRadioCaps GetCaps(); + otRadioCaps GetCaps(void) const; /** * This method starts a (recurring) Receive on the link-layer. * + * @param[in] aCallback A pointer to a function called on receipt of a IEEE 802.15.4 frame. + * + * @retval OT_ERROR_NONE Successfully transitioned to Receive. + * @retval OT_ERROR_INVALID_STATE The radio was disabled or transmitting. + * */ - otError Receive(uint8_t aChannel, otLinkRawReceiveDone aCallback); + otError Receive(otLinkRawReceiveDone aCallback); /** * This method invokes the mReceiveDoneCallback, if set. * + * @param[in] aFrame A pointer to the received frame or NULL if the receive operation failed. + * @param[in] aError OT_ERROR_NONE when successfully received a frame, OT_ERROR_ABORT when reception + * was aborted and a frame was not received, OT_ERROR_NO_BUFS when a frame could not be + * received due to lack of rx buffer space. + * */ void InvokeReceiveDone(otRadioFrame *aFrame, otError aError); /** * This method starts a (single) Transmit on the link-layer. * + * @param[in] aFrame A pointer to the frame that was transmitted. + * @param[in] aCallback A pointer to a function called on completion of the transmission. + * + * @retval OT_ERROR_NONE Successfully transitioned to Transmit. + * @retval OT_ERROR_INVALID_STATE The radio was not in the Receive state. + * */ otError Transmit(otRadioFrame *aFrame, otLinkRawTransmitDone aCallback); /** * This method invokes the mTransmitDoneCallback, if set. * + * @param[in] aFrame A pointer to the frame that was transmitted. + * @param[in] aAckFrame A pointer to the ACK frame, NULL if no ACK was received. + * @param[in] aError OT_ERROR_NONE when the frame was transmitted, OT_ERROR_NO_ACK when the frame was + * transmitted but no ACK was received, OT_ERROR_CHANNEL_ACCESS_FAILURE when the transmission + * could not take place due to activity on the channel, OT_ERROR_ABORT when transmission was + * aborted for other reasons. + * */ void InvokeTransmitDone(otRadioFrame *aFrame, otRadioFrame *aAckFrame, otError aError); /** * This method starts a (single) Enery Scan on the link-layer. * + * @param[in] aScanChannel The channel to perform the energy scan on. + * @param[in] aScanDuration The duration, in milliseconds, for the channel to be scanned. + * @param[in] aCallback A pointer to a function called on completion of a scanned channel. + * + * @retval OT_ERROR_NONE Successfully started scanning the channel. + * @retval OT_ERROR_NOT_IMPLEMENTED The radio doesn't support energy scanning. + * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. + * */ otError EnergyScan(uint8_t aScanChannel, uint16_t aScanDuration, otLinkRawEnergyScanDone aCallback); /** * This method invokes the mEnergyScanDoneCallback, if set. * + * @param[in] aEnergyScanMaxRssi The max RSSI for energy scan. + * */ void InvokeEnergyScanDone(int8_t aEnergyScanMaxRssi); -private: - otError DoTransmit(otRadioFrame *aFrame); + /** + * This method is called when the transmission has started. + * + * @param[in] aFrame A pointer to the frame that is being transmitted. + * + */ + void TransmitStarted(otRadioFrame *aFrame); - otInstance & mInstance; - bool mEnabled; - uint8_t mReceiveChannel; - otLinkRawReceiveDone mReceiveDoneCallback; - otLinkRawTransmitDone mTransmitDoneCallback; - otLinkRawEnergyScanDone mEnergyScanDoneCallback; + /** + * This function returns the short address. + * + * @returns short address. + * + */ + uint16_t GetShortAddress(void) const { return mShortAddress; } + + /** + * This method updates short address. + * + * @param[in] aShortAddress The short address. + * + * @retval OT_ERROR_NONE If successful. + * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. + * + */ + otError SetShortAddress(uint16_t aShortAddress); + + /** + * This function returns PANID. + * + * @returns PANID. + * + */ + uint16_t GetPanId(void) const { return mPanId; } + + /** + * This method updates PANID. + * + * @param[in] aPanId The PANID. + * + * @retval OT_ERROR_NONE If successful. + * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. + * + */ + otError SetPanId(uint16_t aPanId); + + /** + * This method gets the current receiving channel. + * + * @returns Current receiving channel. + * + */ + uint8_t GetChannel(void) const { return mReceiveChannel; } + + /** + * This method sets the receiving channel. + * + * @param[in] aChannel The channel to use for receiving. + * + */ + otError SetChannel(uint8_t aChannel); + + /** + * This function returns the extended address. + * + * @returns A reference to the extended address. + * + */ + const otExtAddress &GetExtAddress(void) const { return mExtAddress; } + + /** + * This method updates extended address. + * + * @param[in] aExtAddress The extended address. + * + * @retval OT_ERROR_NONE If successful. + * @retval OT_ERROR_INVALID_STATE If the raw link-layer isn't enabled. + * + */ + otError SetExtAddress(const otExtAddress &aExtAddress); + +private: + otInstance &mInstance; #if OPENTHREAD_LINKRAW_TIMER_REQUIRED @@ -166,6 +284,15 @@ private: void HandleEnergyScanTask(void); #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN + + otExtAddress mExtAddress; + uint16_t mPanId; + uint16_t mShortAddress; + bool mEnabled; + uint8_t mReceiveChannel; + otLinkRawReceiveDone mReceiveDoneCallback; + otLinkRawTransmitDone mTransmitDoneCallback; + otLinkRawEnergyScanDone mEnergyScanDoneCallback; }; } // namespace ot diff --git a/src/core/api/link_raw_api.cpp b/src/core/api/link_raw_api.cpp index 41df8d1d6..85f593148 100644 --- a/src/core/api/link_raw_api.cpp +++ b/src/core/api/link_raw_api.cpp @@ -38,264 +38,14 @@ #include "common/logging.hpp" #include "common/owner-locator.hpp" #include "common/random.hpp" +#include "mac/mac.hpp" + +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API using namespace ot; -#if OPENTHREAD_ENABLE_RAW_LINK_API - -otError otLinkRawSetEnable(otInstance *aInstance, bool aEnabled) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(!instance.GetThreadNetif().IsUp(), error = OT_ERROR_INVALID_STATE); - - otLogInfoPlat(aInstance, "LinkRaw Enabled=%d", aEnabled ? 1 : 0); - - instance.GetLinkRaw().SetEnabled(aEnabled); - -exit: - return error; -} - -bool otLinkRawIsEnabled(otInstance *aInstance) -{ - Instance &instance = *static_cast(aInstance); - - return instance.GetLinkRaw().IsEnabled(); -} - -otError otLinkRawSetPanId(otInstance *aInstance, uint16_t aPanId) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otPlatRadioSetPanId(aInstance, aPanId); - -exit: - return error; -} - -otError otLinkRawSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress) -{ - otError error = OT_ERROR_NONE; - Instance & instance = *static_cast(aInstance); - otExtAddress address; - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - for (size_t i = 0; i < sizeof(address); i++) - { - address.m8[i] = aExtAddress->m8[7 - i]; - } - - otPlatRadioSetExtendedAddress(aInstance, &address); - -exit: - return error; -} - -otError otLinkRawSetShortAddress(otInstance *aInstance, uint16_t aShortAddress) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otPlatRadioSetShortAddress(aInstance, aShortAddress); - -exit: - return error; -} - -bool otLinkRawGetPromiscuous(otInstance *aInstance) -{ - return otPlatRadioGetPromiscuous(aInstance); -} - -otError otLinkRawSetPromiscuous(otInstance *aInstance, bool aEnable) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otLogInfoPlat(aInstance, "LinkRaw Promiscuous=%d", aEnable ? 1 : 0); - - otPlatRadioSetPromiscuous(aInstance, aEnable); - -exit: - return error; -} - -otError otLinkRawSleep(otInstance *aInstance) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otLogInfoPlat(aInstance, "LinkRaw Sleep"); - - error = otPlatRadioSleep(aInstance); - -exit: - return error; -} - -otError otLinkRawReceive(otInstance *aInstance, uint8_t aChannel, otLinkRawReceiveDone aCallback) -{ - Instance &instance = *static_cast(aInstance); - - otLogInfoPlat(aInstance, "LinkRaw Recv (Channel %d)", aChannel); - return instance.GetLinkRaw().Receive(aChannel, aCallback); -} - -otRadioFrame *otLinkRawGetTransmitBuffer(otInstance *aInstance) -{ - otRadioFrame *buffer = NULL; - Instance & instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled()); - - buffer = otPlatRadioGetTransmitBuffer(aInstance); - -exit: - return buffer; -} - -otError otLinkRawTransmit(otInstance *aInstance, otRadioFrame *aFrame, otLinkRawTransmitDone aCallback) -{ - Instance &instance = *static_cast(aInstance); - - otLogInfoPlat(aInstance, "LinkRaw Transmit (%d bytes on channel %d)", aFrame->mLength, aFrame->mChannel); - return instance.GetLinkRaw().Transmit(aFrame, aCallback); -} - -int8_t otLinkRawGetRssi(otInstance *aInstance) -{ - return otPlatRadioGetRssi(aInstance); -} - -otRadioCaps otLinkRawGetCaps(otInstance *aInstance) -{ - Instance &instance = *static_cast(aInstance); - - return instance.GetLinkRaw().GetCaps(); -} - -otError otLinkRawEnergyScan(otInstance * aInstance, - uint8_t aScanChannel, - uint16_t aScanDuration, - otLinkRawEnergyScanDone aCallback) -{ - Instance &instance = *static_cast(aInstance); - - return instance.GetLinkRaw().EnergyScan(aScanChannel, aScanDuration, aCallback); -} - -otError otLinkRawSrcMatchEnable(otInstance *aInstance, bool aEnable) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otPlatRadioEnableSrcMatch(aInstance, aEnable); - -exit: - return error; -} - -otError otLinkRawSrcMatchAddShortEntry(otInstance *aInstance, const uint16_t aShortAddress) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - error = otPlatRadioAddSrcMatchShortEntry(aInstance, aShortAddress); - -exit: - return error; -} - -otError otLinkRawSrcMatchAddExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - error = otPlatRadioAddSrcMatchExtEntry(aInstance, aExtAddress); - -exit: - return error; -} - -otError otLinkRawSrcMatchClearShortEntry(otInstance *aInstance, const uint16_t aShortAddress) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - error = otPlatRadioClearSrcMatchShortEntry(aInstance, aShortAddress); - -exit: - return error; -} - -otError otLinkRawSrcMatchClearExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - error = otPlatRadioClearSrcMatchExtEntry(aInstance, aExtAddress); - -exit: - return error; -} - -otError otLinkRawSrcMatchClearShortEntries(otInstance *aInstance) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otPlatRadioClearSrcMatchShortEntries(aInstance); - -exit: - return error; -} - -otError otLinkRawSrcMatchClearExtEntries(otInstance *aInstance) -{ - otError error = OT_ERROR_NONE; - Instance &instance = *static_cast(aInstance); - - VerifyOrExit(instance.GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); - - otPlatRadioClearSrcMatchExtEntries(aInstance); - -exit: - return error; -} - -namespace ot { - LinkRaw::LinkRaw(Instance &aInstance) : mInstance(aInstance) - , mEnabled(false) - , mReceiveChannel(OPENTHREAD_CONFIG_DEFAULT_CHANNEL) - , mReceiveDoneCallback(NULL) - , mTransmitDoneCallback(NULL) - , mEnergyScanDoneCallback(NULL) #if OPENTHREAD_LINKRAW_TIMER_REQUIRED , mTimer(aInstance, &LinkRaw::HandleTimer, this) , mTimerReason(kTimerReasonNone) @@ -306,12 +56,100 @@ LinkRaw::LinkRaw(Instance &aInstance) #if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN , mEnergyScanTask(aInstance, &LinkRaw::HandleEnergyScanTask, this) #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN + , mReceiveChannel(OPENTHREAD_CONFIG_DEFAULT_CHANNEL) + , mReceiveDoneCallback(NULL) + , mTransmitDoneCallback(NULL) + , mEnergyScanDoneCallback(NULL) { // Query the capabilities to check asserts (void)GetCaps(); } -otRadioCaps LinkRaw::GetCaps() +otError LinkRaw::SetEnabled(bool aEnabled) +{ + otError error = OT_ERROR_NONE; + + otLogInfoPlat(mInstance, "LinkRaw Enabled=%d", aEnabled ? 1 : 0); + +#if OPENTHREAD_MTD || OPENTHREAD_FTD + VerifyOrExit(!static_cast(mInstance).GetThreadNetif().IsUp(), error = OT_ERROR_INVALID_STATE); +#endif + + if (aEnabled) + { + otPlatRadioEnable(&mInstance); + } + else + { + otPlatRadioDisable(&mInstance); + } + + mEnabled = aEnabled; + +#if OPENTHREAD_MTD || OPENTHREAD_FTD +exit: +#endif + return error; +} + +otError LinkRaw::SetPanId(uint16_t aPanId) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otPlatRadioSetPanId(&mInstance, aPanId); + mPanId = aPanId; + +exit: + return error; +} + +otError LinkRaw::SetChannel(uint8_t aChannel) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(IsEnabled(), error = OT_ERROR_INVALID_STATE); + + mReceiveChannel = aChannel; + +exit: + return error; +} + +otError LinkRaw::SetExtAddress(const otExtAddress &aExtAddress) +{ + otExtAddress addr; + otError error = OT_ERROR_NONE; + + VerifyOrExit(IsEnabled(), error = OT_ERROR_INVALID_STATE); + + for (size_t i = 0; i < sizeof(addr); i++) + { + addr.m8[i] = aExtAddress.m8[7 - i]; + } + + otPlatRadioSetExtendedAddress(&mInstance, &addr); + mExtAddress = aExtAddress; + +exit: + return error; +} + +otError LinkRaw::SetShortAddress(uint16_t aShortAddress) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otPlatRadioSetShortAddress(&mInstance, aShortAddress); + mShortAddress = aShortAddress; + +exit: + return error; +} + +otRadioCaps LinkRaw::GetCaps(void) const { otRadioCaps RadioCaps = otPlatRadioGetCaps(&mInstance); @@ -320,31 +158,30 @@ otRadioCaps LinkRaw::GetCaps() #if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT assert((RadioCaps & OT_RADIO_CAPS_ACK_TIMEOUT) == 0); - RadioCaps = (otRadioCaps)(RadioCaps | OT_RADIO_CAPS_ACK_TIMEOUT); + RadioCaps = static_cast(RadioCaps | OT_RADIO_CAPS_ACK_TIMEOUT); #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT #if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_RETRANSMIT assert((RadioCaps & OT_RADIO_CAPS_TRANSMIT_RETRIES) == 0); - RadioCaps = (otRadioCaps)(RadioCaps | OT_RADIO_CAPS_TRANSMIT_RETRIES); + RadioCaps = static_cast(RadioCaps | OT_RADIO_CAPS_TRANSMIT_RETRIES); #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_RETRANSMIT #if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN assert((RadioCaps & OT_RADIO_CAPS_ENERGY_SCAN) == 0); - RadioCaps = (otRadioCaps)(RadioCaps | OT_RADIO_CAPS_ENERGY_SCAN); + RadioCaps = static_cast(RadioCaps | OT_RADIO_CAPS_ENERGY_SCAN); #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN return RadioCaps; } -otError LinkRaw::Receive(uint8_t aChannel, otLinkRawReceiveDone aCallback) +otError LinkRaw::Receive(otLinkRawReceiveDone aCallback) { otError error = OT_ERROR_INVALID_STATE; if (mEnabled) { - mReceiveChannel = aChannel; mReceiveDoneCallback = aCallback; - error = otPlatRadioReceive(&mInstance, aChannel); + error = otPlatRadioReceive(&mInstance, mReceiveChannel); } return error; @@ -357,13 +194,12 @@ void LinkRaw::InvokeReceiveDone(otRadioFrame *aFrame, otError aError) if (aError == OT_ERROR_NONE) { otLogInfoPlat(&mInstance, "LinkRaw Invoke Receive Done (%d bytes)", aFrame->mLength); + mReceiveDoneCallback(&mInstance, aFrame, aError); } else { otLogWarnPlat(&mInstance, "LinkRaw Invoke Receive Done (err=0x%x)", aError); } - - mReceiveDoneCallback(&mInstance, aFrame, aError); } } @@ -385,33 +221,13 @@ otError LinkRaw::Transmit(otRadioFrame *aFrame, otLinkRawTransmitDone aCallback) error = OT_ERROR_NONE; #else // Let the hardware do the transmission logic - error = DoTransmit(aFrame); + error = otPlatRadioTransmit(&mInstance, aFrame); #endif } return error; } -otError LinkRaw::DoTransmit(otRadioFrame *aFrame) -{ - otError error = otPlatRadioTransmit(&mInstance, aFrame); - -#if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT - - // If we are implementing the ACK timeout logic, start a timer here (if ACK request) - // to fire if we don't get a transmit done callback in time. - if (static_cast(aFrame)->GetAckRequest()) - { - otLogDebgPlat(aInstance, "LinkRaw Starting AckTimeout Timer"); - mTimerReason = kTimerReasonAckTimeout; - mTimer.Start(Mac::kAckTimeout); - } - -#endif - - return error; -} - void LinkRaw::InvokeTransmitDone(otRadioFrame *aFrame, otRadioFrame *aAckFrame, otError aError) { otLogDebgPlat(&mInstance, "LinkRaw Transmit Done (err=0x%x)", aError); @@ -500,13 +316,32 @@ otError LinkRaw::EnergyScan(uint8_t aScanChannel, uint16_t aScanDuration, otLink void LinkRaw::InvokeEnergyScanDone(int8_t aEnergyScanMaxRssi) { - if (mEnergyScanDoneCallback) + if (IsEnabled() && mEnergyScanDoneCallback) { mEnergyScanDoneCallback(&mInstance, aEnergyScanMaxRssi); mEnergyScanDoneCallback = NULL; } } +void LinkRaw::TransmitStarted(otRadioFrame *aFrame) +{ +#if OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ACK_TIMEOUT + + // If we are implementing the ACK timeout logic, start a timer here (if ACK request) + // to fire if we don't get a transmit done callback in time. + if (static_cast(aFrame)->GetAckRequest() && + !(otPlatRadioGetCaps(&mInstance) & OT_RADIO_CAPS_ACK_TIMEOUT)) + { + otLogDebgPlat(aInstance, "LinkRaw Starting AckTimeout Timer"); + mTimerReason = kTimerReasonAckTimeout; + mTimer.Start(Mac::kAckTimeout); + } + +#else + OT_UNUSED_VARIABLE(aFrame); +#endif +} + #if OPENTHREAD_LINKRAW_TIMER_REQUIRED void LinkRaw::HandleTimer(Timer &aTimer) @@ -542,7 +377,7 @@ void LinkRaw::HandleTimer(void) otRadioFrame *aFrame = otPlatRadioGetTransmitBuffer(&mInstance); // Start the transmit now - otError error = DoTransmit(aFrame); + otError error = otPlatRadioTransmit(&mInstance, aFrame); if (error != OT_ERROR_NONE) { @@ -630,6 +465,259 @@ void LinkRaw::HandleEnergyScanTask(void) #endif // OPENTHREAD_CONFIG_ENABLE_SOFTWARE_ENERGY_SCAN -} // namespace ot +otError otLinkRawSetEnable(otInstance *aInstance, bool aEnabled) +{ + return static_cast(aInstance)->GetLinkRaw().SetEnabled(aEnabled); +} -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +bool otLinkRawIsEnabled(otInstance *aInstance) +{ + return static_cast(aInstance)->GetLinkRaw().IsEnabled(); +} + +otError otLinkSetShortAddress(otInstance *aInstance, uint16_t aShortAddress) +{ + return static_cast(aInstance)->GetLinkRaw().SetShortAddress(aShortAddress); +} + +bool otLinkRawGetPromiscuous(otInstance *aInstance) +{ + return otPlatRadioGetPromiscuous(aInstance); +} + +otError otLinkRawSetPromiscuous(otInstance *aInstance, bool aEnable) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otLogInfoPlat(aInstance, "LinkRaw Promiscuous=%d", aEnable ? 1 : 0); + + otPlatRadioSetPromiscuous(aInstance, aEnable); + +exit: + return error; +} + +otError otLinkRawSleep(otInstance *aInstance) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otLogInfoPlat(aInstance, "LinkRaw Sleep"); + + error = otPlatRadioSleep(aInstance); + +exit: + return error; +} + +otError otLinkRawReceive(otInstance *aInstance, otLinkRawReceiveDone aCallback) +{ + otLogInfoPlat(aInstance, "LinkRaw Recv (Channel %d)", aChannel); + return static_cast(aInstance)->GetLinkRaw().Receive(aCallback); +} + +otRadioFrame *otLinkRawGetTransmitBuffer(otInstance *aInstance) +{ + otRadioFrame *buffer = NULL; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled()); + + buffer = otPlatRadioGetTransmitBuffer(aInstance); + +exit: + return buffer; +} + +otError otLinkRawTransmit(otInstance *aInstance, otRadioFrame *aFrame, otLinkRawTransmitDone aCallback) +{ + otLogInfoPlat(aInstance, "LinkRaw Transmit (%d bytes on channel %d)", aFrame->mLength, aFrame->mChannel); + return static_cast(aInstance)->GetLinkRaw().Transmit(aFrame, aCallback); +} + +int8_t otLinkRawGetRssi(otInstance *aInstance) +{ + return otPlatRadioGetRssi(aInstance); +} + +otRadioCaps otLinkRawGetCaps(otInstance *aInstance) +{ + return static_cast(aInstance)->GetLinkRaw().GetCaps(); +} + +otError otLinkRawEnergyScan(otInstance * aInstance, + uint8_t aScanChannel, + uint16_t aScanDuration, + otLinkRawEnergyScanDone aCallback) +{ + return static_cast(aInstance)->GetLinkRaw().EnergyScan(aScanChannel, aScanDuration, aCallback); +} + +otError otLinkRawSrcMatchEnable(otInstance *aInstance, bool aEnable) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otPlatRadioEnableSrcMatch(aInstance, aEnable); + +exit: + return error; +} + +otError otLinkRawSrcMatchAddShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + error = otPlatRadioAddSrcMatchShortEntry(aInstance, aShortAddress); + +exit: + return error; +} + +otError otLinkRawSrcMatchAddExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) +{ + otExtAddress addr; + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + for (uint8_t i = 0; i < sizeof(addr); i++) + { + addr.m8[i] = aExtAddress->m8[sizeof(addr) - 1 - i]; + } + + error = otPlatRadioAddSrcMatchExtEntry(aInstance, &addr); + +exit: + return error; +} + +otError otLinkRawSrcMatchClearShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + error = otPlatRadioClearSrcMatchShortEntry(aInstance, aShortAddress); + +exit: + return error; +} + +otError otLinkRawSrcMatchClearExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) +{ + otExtAddress addr; + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + for (uint8_t i = 0; i < sizeof(addr); i++) + { + addr.m8[i] = aExtAddress->m8[sizeof(addr) - 1 - i]; + } + + error = otPlatRadioClearSrcMatchExtEntry(aInstance, &addr); + +exit: + return error; +} + +otError otLinkRawSrcMatchClearShortEntries(otInstance *aInstance) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otPlatRadioClearSrcMatchShortEntries(aInstance); + +exit: + return error; +} + +otError otLinkRawSrcMatchClearExtEntries(otInstance *aInstance) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(static_cast(aInstance)->GetLinkRaw().IsEnabled(), error = OT_ERROR_INVALID_STATE); + + otPlatRadioClearSrcMatchExtEntries(aInstance); + +exit: + return error; +} + +#if OPENTHREAD_RADIO +void otPlatRadioReceiveDone(otInstance *aInstance, otRadioFrame *aFrame, otError aError) +{ + static_cast(aInstance)->GetLinkRaw().InvokeReceiveDone(aFrame, aError); +} + +void otPlatRadioTxDone(otInstance *aInstance, otRadioFrame *aFrame, otRadioFrame *aAckFrame, otError aError) +{ + static_cast(aInstance)->GetLinkRaw().InvokeTransmitDone(aFrame, aAckFrame, aError); +} + +void otPlatRadioTxStarted(otInstance *aInstance, otRadioFrame *aFrame) +{ + static_cast(aInstance)->GetLinkRaw().TransmitStarted(aFrame); +} + +void otPlatRadioEnergyScanDone(otInstance *aInstance, int8_t aEnergyScanMaxRssi) +{ + VerifyOrExit(otInstanceIsInitialized(aInstance)); + + static_cast(aInstance)->GetLinkRaw().InvokeEnergyScanDone(aEnergyScanMaxRssi); + +exit: + return; +} + +otDeviceRole otThreadGetDeviceRole(otInstance *aInstance) +{ + OT_UNUSED_VARIABLE(aInstance); + return OT_DEVICE_ROLE_DISABLED; +} + +uint8_t otLinkGetChannel(otInstance *aInstance) +{ + return static_cast(aInstance)->GetLinkRaw().GetChannel(); +} + +otError otLinkSetChannel(otInstance *aInstance, uint8_t aChannel) +{ + return static_cast(aInstance)->GetLinkRaw().SetChannel(aChannel); +} + +otPanId otLinkGetPanId(otInstance *aInstance) +{ + return static_cast(aInstance)->GetLinkRaw().GetPanId(); +} + +otError otLinkSetPanId(otInstance *aInstance, uint16_t aPanId) +{ + return static_cast(aInstance)->GetLinkRaw().SetPanId(aPanId); +} + +const otExtAddress *otLinkGetExtendedAddress(otInstance *aInstance) +{ + return &static_cast(aInstance)->GetLinkRaw().GetExtAddress(); +} + +otError otLinkSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtAddress) +{ + return static_cast(aInstance)->GetLinkRaw().SetExtAddress(*aExtAddress); +} + +uint16_t otLinkGetShortAddress(otInstance *aInstance) +{ + return static_cast(aInstance)->GetLinkRaw().GetShortAddress(); +} + +#endif // OPENTHREAD_RADIO + +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API diff --git a/src/core/api/message_api.cpp b/src/core/api/message_api.cpp index 4e3da3d5d..99e9718c5 100644 --- a/src/core/api/message_api.cpp +++ b/src/core/api/message_api.cpp @@ -162,6 +162,7 @@ exit: return next; } +#if OPENTHREAD_MTD || OPENTHREAD_FTD void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) { uint16_t messages, buffers; @@ -181,8 +182,8 @@ void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) instance.GetThreadNetif().GetMeshForwarder().GetResolvingQueue().GetInfo(aBufferInfo->mArpMessages, aBufferInfo->mArpBuffers); #else - aBufferInfo->mArpMessages = 0; - aBufferInfo->mArpBuffers = 0; + aBufferInfo->mArpMessages = 0; + aBufferInfo->mArpBuffers = 0; #endif instance.GetThreadNetif().GetIp6().GetSendQueue().GetInfo(aBufferInfo->mIp6Messages, aBufferInfo->mIp6Buffers); @@ -220,3 +221,4 @@ void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) aBufferInfo->mApplicationCoapBuffers = 0; #endif } +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index ec87a4268..5340f540a 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -405,41 +405,6 @@ exit: return error; } -const char *otGetVersionString(void) -{ -/** - * PLATFORM_VERSION_ATTR_PREFIX and PLATFORM_VERSION_ATTR_SUFFIX are - * intended to be used to specify compiler directives to indicate - * what linker section the platform version string should be stored. - * - * This is useful for specifying an exact locaiton of where the version - * string will be located so that it can be easily retrieved from the - * raw firmware image. - * - * If PLATFORM_VERSION_ATTR_PREFIX is unspecified, the keyword `static` - * is used instead. - * - * If both are unspecified, the location of the string in the firmware - * image will be undefined and may change. - */ - -#ifdef PLATFORM_VERSION_ATTR_PREFIX - PLATFORM_VERSION_ATTR_PREFIX -#else - static -#endif - const char sVersion[] = PACKAGE_NAME "/" PACKAGE_VERSION "; " OPENTHREAD_CONFIG_PLATFORM_INFO -#if defined(__DATE__) - "; " __DATE__ " " __TIME__ -#endif -#ifdef PLATFORM_VERSION_ATTR_SUFFIX - PLATFORM_VERSION_ATTR_SUFFIX -#endif - ; // Trailing semicolon to end statement. - - return sVersion; -} - #if OPENTHREAD_FTD || OPENTHREAD_ENABLE_MTD_NETWORK_DIAGNOSTIC void otThreadSetReceiveDiagnosticGetCallback(otInstance * aInstance, otReceiveDiagnosticGetCallback aCallback, diff --git a/src/core/common/instance.cpp b/src/core/common/instance.cpp index af950d2fe..82778d169 100644 --- a/src/core/common/instance.cpp +++ b/src/core/common/instance.cpp @@ -50,27 +50,22 @@ static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(Instance), uint64_t); #endif Instance::Instance(void) - : mActiveScanCallback(NULL) + : mTimerMilliScheduler(*this) +#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER + , mTimerMicroScheduler(*this) +#endif +#if OPENTHREAD_MTD || OPENTHREAD_FTD + , mActiveScanCallback(NULL) , mActiveScanCallbackContext(NULL) , mEnergyScanCallback(NULL) , mEnergyScanCallbackContext(NULL) , mNotifier(*this) , mSettings(*this) - , mTimerMilliScheduler(*this) -#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER - , mTimerMicroScheduler(*this) -#endif , mIp6(*this) , mThreadNetif(*this) -#if OPENTHREAD_ENABLE_RAW_LINK_API - , mLinkRaw(*this) -#endif #if OPENTHREAD_ENABLE_APPLICATION_COAP , mApplicationCoap(*this) #endif -#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL - , mLogLevel(static_cast(OPENTHREAD_CONFIG_LOG_LEVEL)) -#endif #if OPENTHREAD_ENABLE_CHANNEL_MONITOR , mChannelMonitor(*this) #endif @@ -78,6 +73,13 @@ Instance::Instance(void) , mChannelManager(*this) #endif , mMessagePool(*this) +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API + , mLinkRaw(*this) +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL + , mLogLevel(static_cast(OPENTHREAD_CONFIG_LOG_LEVEL)) +#endif , mIsInitialized(false) { } @@ -128,9 +130,15 @@ exit: #endif // OPENTHREAD_ENABLE_MULTIPLE_INSTANCES +void Instance::Reset(void) +{ + otPlatReset(this); +} + void Instance::AfterInit(void) { mIsInitialized = true; +#if OPENTHREAD_MTD || OPENTHREAD_FTD // Restore datasets and network information @@ -153,8 +161,10 @@ void Instance::AfterInit(void) } #endif +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD } +#if OPENTHREAD_MTD || OPENTHREAD_FTD void Instance::Finalize(void) { VerifyOrExit(mIsInitialized == true); @@ -168,11 +178,6 @@ exit: return; } -void Instance::Reset(void) -{ - otPlatReset(this); -} - void Instance::FactoryReset(void) { GetSettings().Wipe(); @@ -225,11 +230,6 @@ template <> Notifier &Instance::Get(void) return GetNotifier(); } -template <> TaskletScheduler &Instance::Get(void) -{ - return GetTaskletScheduler(); -} - template <> MeshForwarder &Instance::Get(void) { return GetThreadNetif().GetMeshForwarder(); @@ -367,13 +367,6 @@ template <> Coap::CoapSecure &Instance::Get(void) } #endif -#if OPENTHREAD_ENABLE_RAW_LINK_API -template <> LinkRaw &Instance::Get(void) -{ - return GetLinkRaw(); -} -#endif - #if OPENTHREAD_ENABLE_DHCP6_CLIENT template <> Dhcp6::Dhcp6Client &Instance::Get(void) { @@ -412,4 +405,18 @@ template <> Utils::ChannelManager &Instance::Get(void) } #endif +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD + +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API +template <> LinkRaw &Instance::Get(void) +{ + return GetLinkRaw(); +} +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API + +template <> TaskletScheduler &Instance::Get(void) +{ + return GetTaskletScheduler(); +} + } // namespace ot diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index c51b0b944..cbb9aab54 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -42,9 +42,11 @@ #include #include -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API #include "api/link_raw.hpp" +#include "common/message.hpp" #endif +#if OPENTHREAD_FTD || OPENTHREAD_MTD #include "coap/coap.hpp" #include "common/code_utils.hpp" #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES @@ -62,6 +64,7 @@ #if OPENTHREAD_ENABLE_CHANNEL_MONITOR #include "utils/channel_monitor.hpp" #endif +#endif // OPENTHREAD_FTD || OPENTHREAD_MTD /** * @addtogroup core-instance @@ -137,14 +140,6 @@ public: */ bool IsInitialized(void) const { return mIsInitialized; } - /** - * This method finalizes the OpenThread instance. - * - * This method should be called when OpenThread instance is no longer in use. - * - */ - void Finalize(void); - /** * This method triggers a platform reset. * @@ -155,21 +150,30 @@ public: void Reset(void); /** - * This method deletes all the settings stored in non-volatile memory, and then triggers a platform reset. + * This method returns a reference to the timer milli scheduler object. + * + * @returns A reference to the timer milli scheduler object. * */ - void FactoryReset(void); + TimerMilliScheduler &GetTimerMilliScheduler(void) { return mTimerMilliScheduler; } + +#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER + /** + * This method returns a reference to the timer micro scheduler object. + * + * @returns A reference to the timer micro scheduler object. + * + */ + TimerMicroScheduler &GetTimerMicroScheduler(void) { return mTimerMicroScheduler; } +#endif /** - * This method erases all the OpenThread persistent info (network settings) stored in non-volatile memory. + * This method returns a reference to the tasklet scheduler object. * - * Erase is successful/allowed only if the device is in `disabled` state/role. - * - * @retval OT_ERROR_NONE All persistent info/state was erased successfully. - * @retval OT_ERROR_INVALID_STATE Device is not in `disabled` state/role. + * @returns A reference to the tasklet scheduler object. * */ - otError ErasePersistentInfo(void); + TaskletScheduler &GetTaskletScheduler(void) { return mTaskletScheduler; } #if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL /** @@ -189,6 +193,32 @@ public: void SetDynamicLogLevel(otLogLevel aLogLevel) { mLogLevel = aLogLevel; } #endif +#if OPENTHREAD_MTD || OPENTHREAD_FTD + /** + * This method finalizes the OpenThread instance. + * + * This method should be called when OpenThread instance is no longer in use. + * + */ + void Finalize(void); + + /** + * This method deletes all the settings stored in non-volatile memory, and then triggers a platform reset. + * + */ + void FactoryReset(void); + + /** + * This method erases all the OpenThread persistent info (network settings) stored in non-volatile memory. + * + * Erase is successful/allowed only if the device is in `disabled` state/role. + * + * @retval OT_ERROR_NONE All persistent info/state was erased successfully. + * @retval OT_ERROR_INVALID_STATE Device is not in `disabled` state/role. + * + */ + otError ErasePersistentInfo(void); + /** * This method registers the active scan callback. * @@ -243,32 +273,6 @@ public: */ Settings &GetSettings(void) { return mSettings; } - /** - * This method returns a reference to the tasklet scheduler object. - * - * @returns A reference to the tasklet scheduler object. - * - */ - TaskletScheduler &GetTaskletScheduler(void) { return mTaskletScheduler; } - - /** - * This method returns a reference to the timer milli scheduler object. - * - * @returns A reference to the timer milli scheduler object. - * - */ - TimerMilliScheduler &GetTimerMilliScheduler(void) { return mTimerMilliScheduler; } - -#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER - /** - * This method returns a reference to the timer micro scheduler object. - * - * @returns A reference to the timer micro scheduler object. - * - */ - TimerMicroScheduler &GetTimerMicroScheduler(void) { return mTimerMicroScheduler; } -#endif - #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES /** * This method returns a reference to the MbedTlsHeap object. @@ -295,16 +299,6 @@ public: */ ThreadNetif &GetThreadNetif(void) { return mThreadNetif; } -#if OPENTHREAD_ENABLE_RAW_LINK_API - /** - * This method returns a reference to LinkRaw object. - * - * @returns A reference to the LinkRaw object. - * - */ - LinkRaw &GetLinkRaw(void) { return mLinkRaw; } -#endif - #if OPENTHREAD_ENABLE_APPLICATION_COAP /** * This method returns a reference to application COAP object. @@ -342,6 +336,7 @@ public: * */ MessagePool &GetMessagePool(void) { return mMessagePool; } +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD /** * This template method returns a reference to a given `Type` object belonging to the OpenThread instance. @@ -360,10 +355,27 @@ public: */ template Type &Get(void); +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API + /** + * This method returns a reference to LinkRaw object. + * + * @returns A reference to the LinkRaw object. + * + */ + LinkRaw &GetLinkRaw(void) { return mLinkRaw; } +#endif + private: Instance(void); void AfterInit(void); + TimerMilliScheduler mTimerMilliScheduler; +#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER + TimerMicroScheduler mTimerMicroScheduler; +#endif + TaskletScheduler mTaskletScheduler; + +#if OPENTHREAD_MTD || OPENTHREAD_FTD otHandleActiveScanResult mActiveScanCallback; void * mActiveScanCallbackContext; otHandleEnergyScanResult mEnergyScanCallback; @@ -372,13 +384,6 @@ private: Notifier mNotifier; Settings mSettings; - TaskletScheduler mTaskletScheduler; - TimerMilliScheduler mTimerMilliScheduler; - -#if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER - TimerMicroScheduler mTimerMicroScheduler; -#endif - #if !OPENTHREAD_ENABLE_MULTIPLE_INSTANCES Crypto::MbedTls mMbedTls; Crypto::Heap mMbedTlsHeap; @@ -387,18 +392,10 @@ private: Ip6::Ip6 mIp6; ThreadNetif mThreadNetif; -#if OPENTHREAD_ENABLE_RAW_LINK_API - LinkRaw mLinkRaw; -#endif - #if OPENTHREAD_ENABLE_APPLICATION_COAP Coap::ApplicationCoap mApplicationCoap; #endif -#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL - otLogLevel mLogLevel; -#endif - #if OPENTHREAD_ENABLE_CHANNEL_MONITOR Utils::ChannelMonitor mChannelMonitor; #endif @@ -408,7 +405,14 @@ private: #endif MessagePool mMessagePool; - bool mIsInitialized; +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API + LinkRaw mLinkRaw; +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_CONFIG_ENABLE_DYNAMIC_LOG_LEVEL + otLogLevel mLogLevel; +#endif + bool mIsInitialized; }; /** diff --git a/src/core/common/locator.cpp b/src/core/common/locator.cpp index 40dc1f1e5..92cd4258d 100644 --- a/src/core/common/locator.cpp +++ b/src/core/common/locator.cpp @@ -47,6 +47,7 @@ Instance &InstanceLocator::GetInstance(void) const } #endif +#if OPENTHREAD_MTD || OPENTHREAD_FTD Ip6::Ip6 &InstanceLocator::GetIp6(void) const { return GetInstance().GetIp6(); @@ -61,5 +62,6 @@ Notifier &InstanceLocator::GetNotifier(void) const { return GetInstance().GetNotifier(); } +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD } // namespace ot diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 35f1a46e1..574e7eb74 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -145,6 +145,7 @@ void MessagePool::FreeBuffers(Buffer *aBuffer) otError MessagePool::ReclaimBuffers(int aNumBuffers) { +#if OPENTHREAD_MTD || OPENTHREAD_FTD while (aNumBuffers > GetFreeBufferCount()) { MeshForwarder &meshForwarder = GetInstance().GetThreadNetif().GetMeshForwarder(); @@ -152,6 +153,7 @@ otError MessagePool::ReclaimBuffers(int aNumBuffers) } exit: +#endif // OPENTHREAD_MTD || OPENTHREAD_FTD // First comparison is to get around issues with comparing // signed and unsigned numbers, if aNumBuffers is negative then diff --git a/src/core/openthread-core-default-config.h b/src/core/openthread-core-default-config.h index 47b86147a..5b9283d65 100644 --- a/src/core/openthread-core-default-config.h +++ b/src/core/openthread-core-default-config.h @@ -831,8 +831,12 @@ * */ #ifndef OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE +#if OPENTHREAD_RADIO +#define OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE 512 +#else #define OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE 1300 #endif +#endif // OPENTHREAD_CONFIG_NCP_UART_RX_BUFFER_SIZE /** * @def OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE @@ -841,8 +845,12 @@ * */ #ifndef OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE +#if OPENTHREAD_RADIO +#define OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE 512 +#else #define OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE 1300 #endif +#endif // OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE /** * @def OPENTHREAD_CONFIG_NCP_SPINEL_ENCRYPTER_EXTRA_DATA_SIZE diff --git a/src/ncp/Makefile.am b/src/ncp/Makefile.am index 0e84d71ec..caba7075e 100644 --- a/src/ncp/Makefile.am +++ b/src/ncp/Makefile.am @@ -37,6 +37,7 @@ EXTRA_DIST = \ lib_LIBRARIES = \ libopenthread-ncp-mtd.a \ libopenthread-ncp-ftd.a \ + libopenthread-ncp-radio.a \ $(NULL) @@ -60,6 +61,11 @@ libopenthread_ncp_ftd_a_CPPFLAGS = \ $(COMMON_CPPFLAGS) \ $(NULL) +libopenthread_ncp_radio_a_CPPFLAGS = \ + -DOPENTHREAD_RADIO=1 \ + $(COMMON_CPPFLAGS) \ + $(NULL) + COMMON_SOURCES = \ changed_props_set.cpp \ changed_props_set.hpp \ @@ -111,6 +117,10 @@ libopenthread_ncp_ftd_a_SOURCES = \ $(COMMON_SOURCES) \ $(NULL) +libopenthread_ncp_radio_a_SOURCES = \ + $(COMMON_SOURCES) \ + $(NULL) + include_HEADERS = \ $(NULL) diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index e71027662..fa1f41977 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -37,18 +37,14 @@ #include #include - +#include #include #include - #include #include #include "common/code_utils.hpp" #include "common/debug.hpp" -#include "common/instance.hpp" -#include "mac/mac_frame.hpp" -#include "net/ip6.hpp" namespace ot { namespace Ncp { @@ -261,6 +257,13 @@ const NcpBase::PropertyHandlerEntry NcpBase::mGetPropertyHandlerTable[] = const NcpBase::PropertyHandlerEntry NcpBase::mSetPropertyHandlerTable[] = { +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API + NCP_SET_PROP_HANDLER_ENTRY(MAC_15_4_SADDR), + NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_ENABLED), + NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_SHORT_ADDRESSES), + NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_EXTENDED_ADDRESSES), + NCP_SET_PROP_HANDLER_ENTRY(PHY_ENABLED), +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API NCP_SET_PROP_HANDLER_ENTRY(POWER_STATE), NCP_SET_PROP_HANDLER_ENTRY(MCU_POWER_STATE), NCP_SET_PROP_HANDLER_ENTRY(UNSOL_UPDATE_FILTER), @@ -270,13 +273,6 @@ const NcpBase::PropertyHandlerEntry NcpBase::mSetPropertyHandlerTable[] = NCP_SET_PROP_HANDLER_ENTRY(MAC_15_4_PANID), NCP_SET_PROP_HANDLER_ENTRY(MAC_15_4_LADDR), NCP_SET_PROP_HANDLER_ENTRY(MAC_RAW_STREAM_ENABLED), -#if OPENTHREAD_ENABLE_RAW_LINK_API - NCP_SET_PROP_HANDLER_ENTRY(MAC_15_4_SADDR), - NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_ENABLED), - NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_SHORT_ADDRESSES), - NCP_SET_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_EXTENDED_ADDRESSES), - NCP_SET_PROP_HANDLER_ENTRY(PHY_ENABLED), -#endif // OPENTHREAD_ENABLE_RAW_LINK_API #if OPENTHREAD_MTD || OPENTHREAD_FTD NCP_SET_PROP_HANDLER_ENTRY(MAC_DATA_POLL_PERIOD), NCP_SET_PROP_HANDLER_ENTRY(MAC_SCAN_MASK), @@ -360,7 +356,7 @@ const NcpBase::PropertyHandlerEntry NcpBase::mSetPropertyHandlerTable[] = const NcpBase::PropertyHandlerEntry NcpBase::mInsertPropertyHandlerTable[] = { NCP_INSERT_PROP_HANDLER_ENTRY(UNSOL_UPDATE_FILTER), -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API NCP_INSERT_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_SHORT_ADDRESSES), NCP_INSERT_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_EXTENDED_ADDRESSES), #endif @@ -390,7 +386,7 @@ const NcpBase::PropertyHandlerEntry NcpBase::mInsertPropertyHandlerTable[] = const NcpBase::PropertyHandlerEntry NcpBase::mRemovePropertyHandlerTable[] = { NCP_REMOVE_PROP_HANDLER_ENTRY(UNSOL_UPDATE_FILTER), -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API NCP_REMOVE_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_SHORT_ADDRESSES), NCP_REMOVE_PROP_HANDLER_ENTRY(MAC_SRC_MATCH_EXTENDED_ADDRESSES), #endif @@ -574,9 +570,8 @@ NcpBase::NcpBase(Instance *aInstance): #if OPENTHREAD_FTD mPreferredRouteId(0), #endif -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API mCurTransmitTID(0), - mCurReceiveChannel(OPENTHREAD_CONFIG_DEFAULT_CHANNEL), mCurScanChannel(kInvalidScanChannel), mSrcMatchEnabled(false), #endif // OPENTHREAD_ENABLE_RAW_LINK_API @@ -1227,7 +1222,7 @@ bool NcpBase::HandlePropertySetForSpecialProperties(uint8_t aHeader, spinel_prop ExitNow(aError = SetPropertyHandler_THREAD_COMMISSIONER_ENABLED(aHeader)); #endif -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API case SPINEL_PROP_STREAM_RAW: ExitNow(aError = SetPropertyHandler_STREAM_RAW(aHeader)); #endif @@ -1528,6 +1523,31 @@ exit: // MARK: Individual Property Getters and Setters // ---------------------------------------------------------------------------- +#if OPENTHREAD_ENABLE_DIAG + +otError NcpBase::SetPropertyHandler_NEST_STREAM_MFG(uint8_t aHeader) +{ + const char *string = NULL; + const char *output = NULL; + otError error = OT_ERROR_NONE; + + error = mDecoder.ReadUtf8(string); + + VerifyOrExit(error == OT_ERROR_NONE, error = WriteLastStatusFrame(aHeader, ThreadErrorToSpinelStatus(error))); + + output = otDiagProcessCmdLine(string); + + // Prepare the response + SuccessOrExit(error = mEncoder.BeginFrame(aHeader, SPINEL_CMD_PROP_VALUE_IS, SPINEL_PROP_NEST_STREAM_MFG)); + SuccessOrExit(error = mEncoder.WriteUtf8(output)); + SuccessOrExit(error = mEncoder.EndFrame()); + +exit: + return error; +} + +#endif // OPENTHREAD_ENABLE_DIAG + otError NcpBase::GetPropertyHandler_PHY_ENABLED(void) { #if OPENTHREAD_ENABLE_RAW_LINK_API @@ -1549,27 +1569,20 @@ otError NcpBase::SetPropertyHandler_PHY_CHAN(void) SuccessOrExit(error = mDecoder.ReadUintPacked(channel)); -#if OPENTHREAD_MTD || OPENTHREAD_FTD error = otLinkSetChannel(mInstance, static_cast(channel)); -#endif -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API SuccessOrExit(error); - // Cache the channel. If the raw link layer isn't enabled yet, the otSetChannel call - // doesn't call into the radio layer to set the channel. We will have to do it - // manually whenever the radios are enabled and/or raw stream is enabled. - mCurReceiveChannel = static_cast(channel); - // Make sure we are update the receiving channel if raw link is enabled and we have raw // stream enabled already if (otLinkRawIsEnabled(mInstance) && mIsRawStreamEnabled) { - error = otLinkRawReceive(mInstance, mCurReceiveChannel, &NcpBase::LinkRawReceiveDone); + error = otLinkRawReceive(mInstance, &NcpBase::LinkRawReceiveDone); } -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API exit: return error; @@ -1663,13 +1676,13 @@ otError NcpBase::SetPropertyHandler_MAC_RAW_STREAM_ENABLED(void) SuccessOrExit(error = mDecoder.ReadBool(enabled)); -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API if (otLinkRawIsEnabled(mInstance)) { if (enabled) { - error = otLinkRawReceive(mInstance, mCurReceiveChannel, &NcpBase::LinkRawReceiveDone); + error = otLinkRawReceive(mInstance, &NcpBase::LinkRawReceiveDone); } else { @@ -1677,7 +1690,7 @@ otError NcpBase::SetPropertyHandler_MAC_RAW_STREAM_ENABLED(void) } } -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API mIsRawStreamEnabled = enabled; @@ -1797,7 +1810,7 @@ otError NcpBase::GetPropertyHandler_CAPS(void) SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_CAP_MCU_POWER_STATE)); #endif -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_CAP_MAC_RAW)); #endif @@ -1969,7 +1982,7 @@ otError NcpBase::SetPropertyHandler_POWER_STATE(void) otError NcpBase::GetPropertyHandler_HWADDR(void) { otExtAddress hwAddr; - otLinkGetFactoryAssignedIeeeEui64(mInstance, &hwAddr); + otPlatRadioGetIeeeEui64(mInstance, hwAddr.m8); return mEncoder.WriteEui64(hwAddr); } diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index 2eded4876..dd6551823 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -236,7 +236,7 @@ protected: static void HandleRawFrame(const otRadioFrame *aFrame, void *aContext); void HandleRawFrame(const otRadioFrame *aFrame); -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API static void LinkRawReceiveDone(otInstance *aInstance, otRadioFrame *aFrame, otError aError); void LinkRawReceiveDone(otRadioFrame *aFrame, otError aError); @@ -247,7 +247,7 @@ protected: static void LinkRawEnergyScanDone(otInstance *aInstance, int8_t aEnergyScanMaxRssi); void LinkRawEnergyScanDone(int8_t aEnergyScanMaxRssi); -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API #if OPENTHREAD_MTD || OPENTHREAD_FTD static void HandleStateChanged(uint32_t aFlags, void *aContext); @@ -396,7 +396,7 @@ protected: // -------------------------------------------------------------------------- // Raw Link API Properties -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API NCP_SET_PROP_HANDLER(PHY_ENABLED); @@ -410,7 +410,7 @@ protected: NCP_INSERT_PROP_HANDLER(MAC_SRC_MATCH_EXTENDED_ADDRESSES); NCP_REMOVE_PROP_HANDLER(MAC_SRC_MATCH_EXTENDED_ADDRESSES); -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API // -------------------------------------------------------------------------- // MTD (or FTD) Properties @@ -689,7 +689,7 @@ protected: otError SetPropertyHandler_THREAD_COMMISSIONER_ENABLED(uint8_t aHeader); #endif // OPENTHREAD_FTD -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API otError SetPropertyHandler_STREAM_RAW(uint8_t aHeader); #endif @@ -767,9 +767,8 @@ protected: uint8_t mPreferredRouteId; #endif -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API uint8_t mCurTransmitTID; - uint8_t mCurReceiveChannel; int8_t mCurScanChannel; bool mSrcMatchEnabled; #endif // OPENTHREAD_ENABLE_RAW_LINK_API diff --git a/src/ncp/ncp_base_ftd.cpp b/src/ncp/ncp_base_ftd.cpp index a474a0732..be217788b 100644 --- a/src/ncp/ncp_base_ftd.cpp +++ b/src/ncp/ncp_base_ftd.cpp @@ -30,6 +30,7 @@ * This file implements full thread device specified Spinel interface to the OpenThread stack. */ +#include #include "ncp_base.hpp" #if OPENTHREAD_ENABLE_CHANNEL_MANAGER @@ -41,7 +42,6 @@ #include #include #include -#include #include #if OPENTHREAD_ENABLE_TMF_PROXY @@ -54,7 +54,6 @@ #if OPENTHREAD_ENABLE_COMMISSIONER #include "meshcop/commissioner.hpp" #endif -#include "net/ip6.hpp" #if OPENTHREAD_FTD namespace ot { diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index bf63a01fe..65767b1d1 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -30,6 +30,8 @@ * This file implements minimal thread device required Spinel interface to the OpenThread stack. */ +#include + #include "ncp_base.hpp" #if OPENTHREAD_ENABLE_BORDER_ROUTER @@ -2265,31 +2267,6 @@ exit: return error; } -#if OPENTHREAD_ENABLE_DIAG - -otError NcpBase::SetPropertyHandler_NEST_STREAM_MFG(uint8_t aHeader) -{ - const char *string = NULL; - const char *output = NULL; - otError error = OT_ERROR_NONE; - - error = mDecoder.ReadUtf8(string); - - VerifyOrExit(error == OT_ERROR_NONE, error = WriteLastStatusFrame(aHeader, ThreadErrorToSpinelStatus(error))); - - output = otDiagProcessCmdLine(string); - - // Prepare the response - SuccessOrExit(error = mEncoder.BeginFrame(aHeader, SPINEL_CMD_PROP_VALUE_IS, SPINEL_PROP_NEST_STREAM_MFG)); - SuccessOrExit(error = mEncoder.WriteUtf8(output)); - SuccessOrExit(error = mEncoder.EndFrame()); - -exit: - return error; -} - -#endif // OPENTHREAD_ENABLE_DIAG - otError NcpBase::InsertPropertyHandler_THREAD_ASSISTING_PORTS(void) { otError error = OT_ERROR_NONE; @@ -2678,13 +2655,13 @@ otError NcpBase::SetPropertyHandler_MAC_SCAN_STATE(void) break; case SPINEL_SCAN_STATE_BEACON: -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API if (otLinkRawIsEnabled(mInstance)) { error = OT_ERROR_NOT_IMPLEMENTED; } else -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API { error = otLinkActiveScan( mInstance, @@ -2699,7 +2676,7 @@ otError NcpBase::SetPropertyHandler_MAC_SCAN_STATE(void) break; case SPINEL_SCAN_STATE_ENERGY: -#if OPENTHREAD_ENABLE_RAW_LINK_API +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API if (otLinkRawIsEnabled(mInstance)) { uint8_t scanChannel; @@ -2720,7 +2697,7 @@ otError NcpBase::SetPropertyHandler_MAC_SCAN_STATE(void) ); } else -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API { error = otLinkEnergyScan( mInstance, diff --git a/src/ncp/ncp_base_radio.cpp b/src/ncp/ncp_base_radio.cpp index 124149114..92ba00f39 100644 --- a/src/ncp/ncp_base_radio.cpp +++ b/src/ncp/ncp_base_radio.cpp @@ -32,23 +32,19 @@ #include "ncp_base.hpp" -#include - -#include -#include - +#include +#include #include #include - -#include #include #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/instance.hpp" -#include "net/ip6.hpp" +#include "mac/mac_frame.hpp" + +#if OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API -#if OPENTHREAD_ENABLE_RAW_LINK_API namespace ot { namespace Ncp { @@ -162,7 +158,7 @@ void NcpBase::LinkRawEnergyScanDone(int8_t aEnergyScanMaxRssi) // Make sure we are back listening on the original receive channel, // since the energy scan could have been on a different channel. - otLinkRawReceive(mInstance, mCurReceiveChannel, &NcpBase::LinkRawReceiveDone); + otLinkRawReceive(mInstance, &NcpBase::LinkRawReceiveDone); SuccessOrExit( mEncoder.BeginFrame( @@ -327,7 +323,7 @@ otError NcpBase::SetPropertyHandler_PHY_ENABLED(void) // If we have raw stream enabled already, start receiving if (error == OT_ERROR_NONE && mIsRawStreamEnabled) { - error = otLinkRawReceive(mInstance, mCurReceiveChannel, &NcpBase::LinkRawReceiveDone); + error = otLinkRawReceive(mInstance, &NcpBase::LinkRawReceiveDone); } } @@ -342,7 +338,7 @@ otError NcpBase::SetPropertyHandler_MAC_15_4_SADDR(void) SuccessOrExit(error = mDecoder.ReadUint16(shortAddress)); - error = otLinkRawSetShortAddress(mInstance, shortAddress); + error = otLinkSetShortAddress(mInstance, shortAddress); exit: return error; @@ -395,4 +391,4 @@ exit: } // namespace Ncp } // namespace ot -#endif // OPENTHREAD_ENABLE_RAW_LINK_API +#endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API