Skip to content

Commit

Permalink
Avoid NPE following ASH error (#1435)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson authored Sep 10, 2024
1 parent 5da5a48 commit 0d46a58
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -869,17 +869,16 @@ public EzspTransaction sendEzspTransaction(EzspTransaction ezspTransaction, long
Future<EzspFrame> futureResponse = sendEzspRequestAsync(ezspTransaction);
if (futureResponse == null) {
logger.debug("ASH: Error sending EZSP transaction: Future is null");
return null;
}

try {
futureResponse.get(timeout, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
futureResponse.cancel(true);
logger.debug("ASH interrupted in sendRequest while sending {}", ezspTransaction.getRequest());
} catch (TimeoutException e) {
futureResponse.cancel(true);
logger.debug("Sending EZSP transaction timed out after {} seconds", timeout);
} else {
try {
futureResponse.get(timeout, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException e) {
futureResponse.cancel(true);
logger.debug("ASH interrupted in sendRequest while sending {}", ezspTransaction.getRequest());
} catch (TimeoutException e) {
futureResponse.cancel(true);
logger.debug("Sending EZSP transaction timed out after {} seconds", timeout);
}
}

return ezspTransaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public interface EzspTransaction {
* response indicating the final state of the transaction.
*
* @return {@link EmberStatus} indicating the transaction completion state or
* {@link EmberStatus#EMBED_UNKNOWN_STATUS} on error.
* {@link EmberStatus#UNKNOWN} on error.
*/
EmberStatus getStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand All @@ -33,6 +32,7 @@
import com.zsmartsystems.zigbee.dongle.ember.ezsp.EzspFrameRequest;
import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspVersionRequest;
import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspVersionResponse;
import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EmberStatus;
import com.zsmartsystems.zigbee.dongle.ember.internal.EzspFrameHandler;
import com.zsmartsystems.zigbee.dongle.ember.internal.transaction.EzspSingleResponseTransaction;
import com.zsmartsystems.zigbee.dongle.ember.internal.transaction.EzspTransaction;
Expand Down Expand Up @@ -170,7 +170,7 @@ public void write(int value) {

@Override
public void write(int[] bytes) {
for(int val : bytes) {
for (int val : bytes) {
outputData.add(val);
}
}
Expand Down Expand Up @@ -318,4 +318,19 @@ public void handleReset() throws Exception {

Mockito.verify(ezspHandler, Mockito.timeout(TIMEOUT)).handleLinkStateChange(true);
}

@Test
public void sendEzspTransactionNullCheck() {
EzspFrameHandler ezspHandler = Mockito.mock(EzspFrameHandler.class);
AshFrameHandler frameHandler = new AshFrameHandler(ezspHandler);

frameHandler.setClosing();
EzspVersionRequest request = new EzspVersionRequest();
EzspTransaction transaction = frameHandler
.sendEzspTransaction(new EzspSingleResponseTransaction(request, EzspVersionResponse.class));

assertNotNull(transaction);
assertEquals(EmberStatus.UNKNOWN, transaction.getStatus());
assertNull(transaction.getResponse());
}
}

0 comments on commit 0d46a58

Please sign in to comment.