@@ -99,7 +99,7 @@ CHIP_ERROR AndroidLogDownloadFromNode::SendRetrieveLogsRequest(Messaging::Exchan
9999 if (err != CHIP_NO_ERROR)
100100 {
101101 ChipLogError (Controller, " Make BDX URI failure : %" CHIP_ERROR_FORMAT, err.Format ());
102- FinishLogDownloadFromNode (err);
102+ FinishLogDownloadFromNode (static_cast < void *>( this ), err);
103103 }
104104
105105 mBdxReceiver =
@@ -132,18 +132,14 @@ void AndroidLogDownloadFromNode::OnDeviceConnectedFn(void * context, Messaging::
132132 if (err != CHIP_NO_ERROR)
133133 {
134134 ChipLogError (Controller, " Log Download failure : %" CHIP_ERROR_FORMAT, err.Format ());
135- self-> FinishLogDownloadFromNode (err);
135+ FinishLogDownloadFromNode (context, err);
136136 }
137137}
138138
139139void AndroidLogDownloadFromNode::OnDeviceConnectionFailureFn (void * context, const ScopedNodeId & peerId, CHIP_ERROR err)
140140{
141141 ChipLogProgress (Controller, " OnDeviceConnectionFailureFn: %" CHIP_ERROR_FORMAT, err.Format ());
142-
143- auto * self = static_cast <AndroidLogDownloadFromNode *>(context);
144- VerifyOrReturn (self != nullptr , ChipLogProgress (Controller, " Device connected failure callback with null context. Ignoring" ));
145-
146- self->FinishLogDownloadFromNode (err);
142+ FinishLogDownloadFromNode (context, err);
147143}
148144
149145void AndroidLogDownloadFromNode::OnResponseRetrieveLogs (void * context,
@@ -162,25 +158,25 @@ void AndroidLogDownloadFromNode::OnResponseRetrieveLogs(void * context,
162158 {
163159 CHIP_ERROR err = CHIP_NO_ERROR;
164160 self->OnTransferCallback (self->mController ->GetFabricIndex (), self->mRemoteNodeId , data.logContent , &err);
165- self-> FinishLogDownloadFromNode (err);
161+ FinishLogDownloadFromNode (context, err);
166162 }
167163 else if (data.status == StatusEnum::kNoLogs )
168164 {
169165 CHIP_ERROR err = CHIP_NO_ERROR;
170166 self->OnTransferCallback (self->mController ->GetFabricIndex (), self->mRemoteNodeId , ByteSpan (), &err);
171- self-> FinishLogDownloadFromNode (err);
167+ FinishLogDownloadFromNode (context, err);
172168 }
173169 else if (data.status == StatusEnum::kBusy )
174170 {
175- self-> FinishLogDownloadFromNode (CHIP_ERROR_BUSY);
171+ FinishLogDownloadFromNode (context, CHIP_ERROR_BUSY);
176172 }
177173 else if (data.status == StatusEnum::kDenied )
178174 {
179- self-> FinishLogDownloadFromNode (CHIP_ERROR_ACCESS_DENIED);
175+ FinishLogDownloadFromNode (context, CHIP_ERROR_ACCESS_DENIED);
180176 }
181177 else
182178 {
183- self-> FinishLogDownloadFromNode (CHIP_ERROR_INVALID_DATA_LIST);
179+ FinishLogDownloadFromNode (context, CHIP_ERROR_INVALID_DATA_LIST);
184180 }
185181}
186182
@@ -191,48 +187,60 @@ void AndroidLogDownloadFromNode::OnCommandFailure(void * context, CHIP_ERROR err
191187 auto * self = static_cast <AndroidLogDownloadFromNode *>(context);
192188 VerifyOrReturn (self != nullptr , ChipLogProgress (Controller, " Send command failure callback with null context. Ignoring" ));
193189
194- self-> FinishLogDownloadFromNode (err);
190+ FinishLogDownloadFromNode (context, err);
195191}
196192
197- void AndroidLogDownloadFromNode::FinishLogDownloadFromNode (CHIP_ERROR err)
193+ void AndroidLogDownloadFromNode::FinishLogDownloadFromNode (void * context, CHIP_ERROR err)
198194{
199- CHIP_ERROR jniErr = CHIP_NO_ERROR;
200- if (mBdxReceiver != nullptr )
195+ auto * self = static_cast <AndroidLogDownloadFromNode *>(context);
196+ VerifyOrReturn (self != nullptr , ChipLogProgress (Controller, " Finish Log Download with null context. Ignoring" ));
197+
198+ if (self->mBdxReceiver != nullptr )
201199 {
202- if (mTimeout > 0 )
200+ if (self-> mTimeout > 0 && err != CHIP_ERROR_TIMEOUT )
203201 {
204- mBdxReceiver ->CancelBDXTransferTimeout ();
202+ self-> mBdxReceiver ->CancelBDXTransferTimeout ();
205203 }
206- delete mBdxReceiver ;
207- mBdxReceiver = nullptr ;
204+ delete self-> mBdxReceiver ;
205+ self-> mBdxReceiver = nullptr ;
208206 }
209207
210- JNIEnv * env = JniReferences::GetInstance ().GetEnvForCurrentThread ();
208+ CHIP_ERROR jniErr = CHIP_NO_ERROR;
209+ JNIEnv * env = JniReferences::GetInstance ().GetEnvForCurrentThread ();
211210 JniLocalReferenceScope scope (env);
212211
212+ jobject jCallback = self->mJavaCallback .ObjectRef ();
213+ jint jFabricIndex = self->mController ->GetFabricIndex ();
214+ jlong jremoteNodeId = self->mRemoteNodeId ;
215+
216+ VerifyOrExit (env != nullptr , ChipLogError (Controller, " Could not get JNIEnv for current thread" ));
217+
213218 if (err == CHIP_NO_ERROR)
214219 {
215220 ChipLogProgress (Controller, " Log Download succeeded." );
216221 jmethodID onSuccessMethod;
217222 // Java method signature : boolean onSuccess(int fabricIndex, long nodeId)
218- jniErr = JniReferences::GetInstance ().FindMethod (env, mJavaCallback . ObjectRef () , " onSuccess" , " (IJ)V" , &onSuccessMethod);
223+ jniErr = JniReferences::GetInstance ().FindMethod (env, jCallback , " onSuccess" , " (IJ)V" , &onSuccessMethod);
219224
220- VerifyOrReturn (jniErr == CHIP_NO_ERROR, ChipLogError (Controller, " Could not find onSuccess method" ));
225+ VerifyOrExit (jniErr == CHIP_NO_ERROR, ChipLogError (Controller, " Could not find onSuccess method" ));
221226
222- env->CallVoidMethod (mJavaCallback .ObjectRef (), onSuccessMethod, static_cast <jint>(mController ->GetFabricIndex ()),
223- static_cast <jlong>(mRemoteNodeId ));
224- return ;
227+ env->CallVoidMethod (jCallback, onSuccessMethod, jFabricIndex, jremoteNodeId);
225228 }
229+ else
230+ {
231+ ChipLogError (Controller, " Log Download Failed : %" CHIP_ERROR_FORMAT, err.Format ());
226232
227- ChipLogError (Controller, " Log Download Failed : %" CHIP_ERROR_FORMAT, err.Format ());
233+ jmethodID onErrorMethod;
234+ // Java method signature : void onError(int fabricIndex, long nodeId, long errorCode)
235+ jniErr = JniReferences::GetInstance ().FindMethod (env, jCallback, " onError" , " (IJJ)V" , &onErrorMethod);
236+ VerifyOrExit (jniErr == CHIP_NO_ERROR, ChipLogError (Controller, " Could not find onError method" ));
228237
229- jmethodID onErrorMethod;
230- // Java method signature : void onError(int fabricIndex, long nodeId, long errorCode)
231- jniErr = JniReferences::GetInstance ().FindMethod (env, mJavaCallback .ObjectRef (), " onError" , " (IJJ)V" , &onErrorMethod);
232- VerifyOrReturn (jniErr == CHIP_NO_ERROR, ChipLogError (Controller, " Could not find onError method" ));
238+ env->CallVoidMethod (jCallback, onErrorMethod, jFabricIndex, jremoteNodeId, static_cast <jlong>(err.AsInteger ()));
239+ }
233240
234- env->CallVoidMethod (mJavaCallback .ObjectRef (), onErrorMethod, static_cast <jint>(mController ->GetFabricIndex ()),
235- static_cast <jlong>(mRemoteNodeId ), static_cast <jlong>(err.AsInteger ()));
241+ exit:
242+ // Finish this function, this object will be deleted.
243+ delete self;
236244}
237245
238246void AndroidLogDownloadFromNode::OnBdxTransferCallback (void * context, FabricIndex fabricIndex, NodeId remoteNodeId,
@@ -277,7 +285,7 @@ void AndroidLogDownloadFromNode::OnBdxTransferSuccessCallback(void * context, Fa
277285 auto * self = static_cast <AndroidLogDownloadFromNode *>(context);
278286 VerifyOrReturn (self != nullptr , ChipLogProgress (Controller, " Send command failure callback with null context. Ignoring" ));
279287
280- self-> FinishLogDownloadFromNode (CHIP_NO_ERROR);
288+ FinishLogDownloadFromNode (context, CHIP_NO_ERROR);
281289}
282290
283291void AndroidLogDownloadFromNode::OnBdxTransferFailureCallback (void * context, FabricIndex fabricIndex, NodeId remoteNodeId,
@@ -288,7 +296,7 @@ void AndroidLogDownloadFromNode::OnBdxTransferFailureCallback(void * context, Fa
288296 auto * self = static_cast <AndroidLogDownloadFromNode *>(context);
289297 VerifyOrReturn (self != nullptr , ChipLogProgress (Controller, " Send command failure callback with null context. Ignoring" ));
290298
291- self-> FinishLogDownloadFromNode (status);
299+ FinishLogDownloadFromNode (context, status);
292300}
293301} // namespace Controller
294302} // namespace chip
0 commit comments