@@ -52,6 +52,10 @@ SpinelDriver::SpinelDriver(void)
5252 , mSpinelVersionMajor (-1 )
5353 , mSpinelVersionMinor (-1 )
5454 , mIsCoprocessorReady (false )
55+ #if OPENTHREAD_SPINEL_CONFIG_COPROCESSOR_RESET_FAILURE_CALLBACK_ENABLE
56+ , mCoprocessorResetFailureCallback (nullptr )
57+ , mCoprocessorResetFailureContext (nullptr )
58+ #endif
5559{
5660 memset (mVersion , 0 , sizeof (mVersion ));
5761
@@ -119,6 +123,15 @@ otError SpinelDriver::SendReset(uint8_t aResetType)
119123 return error;
120124}
121125
126+ #if OPENTHREAD_SPINEL_CONFIG_COPROCESSOR_RESET_FAILURE_CALLBACK_ENABLE
127+ void SpinelDriver::SetCoprocessorResetFailureCallback (otSpinelDriverCoprocessorResetFailureCallback aCallback,
128+ void *aContext)
129+ {
130+ mCoprocessorResetFailureCallback = aCallback;
131+ mCoprocessorResetFailureContext = aContext;
132+ }
133+ #endif
134+
122135void SpinelDriver::ResetCoprocessor (bool aSoftwareReset)
123136{
124137 bool hardwareReset;
@@ -129,7 +142,13 @@ void SpinelDriver::ResetCoprocessor(bool aSoftwareReset)
129142 VerifyOrExit (!mIsCoprocessorReady , resetDone = true );
130143#endif
131144
132- mWaitingKey = SPINEL_PROP_LAST_STATUS;
145+ // Before resetting the co-processor, mIsCoprocessorReady must be set to false.
146+ // Otherwise, in some cases, the host may misinterpret the reset result.
147+ // For example, if mIsCoprocessorReady is true when calling ResetCoprocessor,
148+ // and the reset fails but the co-processor sends some invalid data,`WaitResponse`
149+ // may mistakenly return OT_ERROR_NONE, causing the host to believe the reset succeeded.
150+ mIsCoprocessorReady = false ;
151+ mWaitingKey = SPINEL_PROP_LAST_STATUS;
133152
134153 if (aSoftwareReset && (SendReset (SPINEL_RESET_STACK) == OT_ERROR_NONE) && (WaitResponse () == OT_ERROR_NONE))
135154 {
@@ -160,6 +179,12 @@ void SpinelDriver::ResetCoprocessor(bool aSoftwareReset)
160179 if (!resetDone)
161180 {
162181 LogCrit (" Failed to reset co-processor!" );
182+ #if OPENTHREAD_SPINEL_CONFIG_COPROCESSOR_RESET_FAILURE_CALLBACK_ENABLE
183+ if (mCoprocessorResetFailureCallback )
184+ {
185+ mCoprocessorResetFailureCallback (mCoprocessorResetFailureContext );
186+ }
187+ #endif
163188 DieNow (OT_EXIT_FAILURE);
164189 }
165190}
0 commit comments