Skip to content

Commit

Permalink
[spinel] allow registering callback to handle errors of radio spinel
Browse files Browse the repository at this point in the history
  • Loading branch information
gytxxsy committed Sep 23, 2024
1 parent bd310db commit f34134b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/lib/spinel/radio_spinel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ RadioSpinel::RadioSpinel(void)
#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
, mVendorRestorePropertiesCallback(nullptr)
, mVendorRestorePropertiesContext(nullptr)
, mVendorHandleErrorCallback(nullptr)
, mVendorHandleErrorContext(nullptr)
#endif
, mTimeSyncEnabled(false)
, mTimeSyncOn(false)
Expand Down Expand Up @@ -198,6 +200,13 @@ otError RadioSpinel::CheckSpinelVersion(void)
{
LogCrit("Spinel version mismatch - Posix:%d.%d, RCP:%d.%d", SPINEL_PROTOCOL_VERSION_THREAD_MAJOR,
SPINEL_PROTOCOL_VERSION_THREAD_MINOR, versionMajor, versionMinor);

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif
DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}

Expand All @@ -210,12 +219,26 @@ void RadioSpinel::InitializeCaps(bool &aSupportsRcpApiVersion, bool &aSupportsRc
if (!GetSpinelDriver().CoprocessorHasCap(SPINEL_CAP_CONFIG_RADIO))
{
LogCrit("The co-processor isn't a RCP!");

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif
DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}

if (!GetSpinelDriver().CoprocessorHasCap(SPINEL_CAP_MAC_RAW))
{
LogCrit("RCP capability list does not include support for radio/raw mode");

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif
DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}

Expand Down Expand Up @@ -251,6 +274,13 @@ otError RadioSpinel::CheckRadioCapabilities(otRadioCaps aRequiredRadioCaps)
}
}

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif

DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}

Expand Down Expand Up @@ -279,6 +309,13 @@ otError RadioSpinel::CheckRcpApiVersion(bool aSupportsRcpApiVersion, bool aSuppo
LogCrit("RCP and host are using incompatible API versions");
LogCrit("RCP API Version %u is older than min required by host %u", rcpApiVersion,
SPINEL_MIN_HOST_SUPPORTED_RCP_API_VERSION);

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif
DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}
}
Expand All @@ -299,6 +336,13 @@ otError RadioSpinel::CheckRcpApiVersion(bool aSupportsRcpApiVersion, bool aSuppo
LogCrit("RCP and host are using incompatible API versions");
LogCrit("RCP requires min host API version %u but host is older and at version %u", minHostRcpApiVersion,
SPINEL_RCP_API_VERSION);

#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
if (mVendorHandleErrorCallback)
{
mVendorHandleErrorCallback(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE, mVendorHandleErrorContext);
}
#endif
DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE);
}
}
Expand Down Expand Up @@ -629,6 +673,12 @@ void RadioSpinel::SetVendorRestorePropertiesCallback(otRadioSpinelVendorRestoreP
mVendorRestorePropertiesCallback = aCallback;
mVendorRestorePropertiesContext = aContext;
}

void RadioSpinel::SetVendorHandleErrorCallback(otRadioSpinelVendorHandleErrorCallback aCallback, void *aContext)
{
mVendorHandleErrorCallback = aCallback;
mVendorHandleErrorContext = aContext;
}
#endif

SpinelDriver &RadioSpinel::GetSpinelDriver(void) const
Expand Down
24 changes: 23 additions & 1 deletion src/lib/spinel/radio_spinel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ class RadioSpinel : private Logger
* A callback type for restoring vendor properties.
*
*/
typedef void (*otRadioSpinelVendorRestorePropertiesCallback)(void *context);
typedef void (*otRadioSpinelVendorRestorePropertiesCallback)(void *aContext);

/**
* Registers a callback to restore vendor properties.
Expand All @@ -1104,6 +1104,25 @@ class RadioSpinel : private Logger
*
*/
void SetVendorRestorePropertiesCallback(otRadioSpinelVendorRestorePropertiesCallback aCallback, void *aContext);

/**
* A callback type for handling error of radio spinel.
*
*/
typedef void (*otRadioSpinelVendorHandleErrorCallback)(uint8_t aExitCode, void *aContext);

/**
* Registers a callback to handle error of radio spinel.
*
* This function is used to register a callback to handle radio spinel errors. When a radio spinel error occurs that
* cannot be resolved by a restart (e.g., RCP version mismatch), the user can handle the error through the callback
* (such as OTA) instead of letting the program crash directly.
*
* @param[in] aCallback The callback.
* @param[in] aContext The context.
*
*/
void SetVendorHandleErrorCallback(otRadioSpinelVendorHandleErrorCallback aCallback, void *aContext);
#endif // OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE

/**
Expand Down Expand Up @@ -1347,6 +1366,9 @@ class RadioSpinel : private Logger
#if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE
otRadioSpinelVendorRestorePropertiesCallback mVendorRestorePropertiesCallback;
void *mVendorRestorePropertiesContext;

otRadioSpinelVendorHandleErrorCallback mVendorHandleErrorCallback;
void *mVendorHandleErrorContext;
#endif

bool mTimeSyncEnabled : 1;
Expand Down

0 comments on commit f34134b

Please sign in to comment.