private boolean handleErrorResponse(byte error, byte nodeId, String funcStr, boolean sleepcheck) { Node node = getNode(nodeId); if (error == Defs.TRANSMIT_COMPLETE_NOROUTE) { logs.add(String.format("Error: %s failed. No route found.", funcStr)); // if (node != null) { // node.setNodeAlive(false); // } } else if (error == Defs.TRANSMIT_COMPLETE_NO_ACK) { logs.add(String.format("Error: %s failed. No ACK found.", funcStr)); if (queue.getCurrentMsg() != null) { if (moveMsgToWakeUpQueue(SafeCast.nodeIdFromMsg(queue.getCurrentMsg()), sleepcheck)) { return true; } } } else if (error == Defs.TRANSMIT_COMPLETE_FAIL) { logs.add(String.format("Error: %s failed. Network busy.", funcStr)); } else if (error == Defs.TRANSMIT_COMPLETE_NOT_IDLE) { logs.add(String.format("Error: %s failed. Network busy.", funcStr)); } if (node != null && node.incErrorCount() >= 3) { node.setNodeAlive(false); } return false; }
public boolean handleApplicationUpdateRequest(byte[] data) { boolean messageRemoved = false; byte nodeId = data[3]; Node node = getNode(nodeId); Node tnode = null; if (node != null && !node.isNodeAlive()) { node.setNodeAlive(true); } switch (data[2]) { case Defs.UPDATE_STATE_SUC_ID: logs.add(String.format("Update SUC Id node %d", SafeCast.toInt(nodeId))); sucNodeId = nodeId; break; case Defs.UPDATE_STATE_DELETE_DONE: logs.add(String.format("Remove node %d", SafeCast.toInt(nodeId))); removeNode(nodeId); break; case Defs.UPDATE_STATE_NEW_ID_ASSIGNED: logs.add(String.format("Add node %d", SafeCast.toInt(nodeId))); addNode(nodeId); break; case Defs.UPDATE_STATE_ROUTING_PENDING: logs.add(String.format("Routing pending node %d", nodeId)); break; case Defs.UPDATE_STATE_NODE_INFO_REQ_FAILED: if (queue.getCurrentMsg() != null) { logs.add( String.format( "Update failed node %d", SafeCast.nodeIdFromMsg(queue.getCurrentMsg()))); tnode = getNode(SafeCast.nodeIdFromMsg(queue.getCurrentMsg())); if (tnode != null) { tnode.queryStageRetry(QueryStage.NodeInfo, (byte) 2); if (moveMsgToWakeUpQueue(tnode.getNodeId(), true)) { messageRemoved = true; } } } break; case Defs.UPDATE_STATE_NODE_INFO_REQ_DONE: logs.add(String.format("Update done node %d", nodeId)); break; case Defs.UPDATE_STATE_NODE_INFO_RECEIVED: logs.add(String.format("Update info receive node %d", nodeId)); if (node != null) { node.updateNodeInfo( Arrays.copyOfRange(data, 8, data.length), (byte) ((SafeCast.toInt(data[4]) - 3))); } break; } if (messageRemoved) { queue.removeExpectedAndACK(); } return messageRemoved; }
// ----------------------------------------------------------------------------------------- // Request Methods // ----------------------------------------------------------------------------------------- public void handleSendDataRequest(byte[] data, boolean replication) { byte nodeId = SafeCast.nodeIdFromMsg(queue.getCurrentMsg()); if (data[2] != queue.getExpectedCallbackId()) { logs.add( String.format( "Unexpected callback id: received %d != %d", SafeCast.toInt(data[2]), SafeCast.toInt(queue.getExpectedCallbackId()))); } else { Node node = getNode(nodeId); if (node != null && queue.getCurrentMsg() != null) { if (data[3] != 0) { if (!handleErrorResponse( data[3], nodeId, replication ? "ZW_REPLICATION_END_DATA" : "ZW_SEND_DATA", !replication)) { if (queue.getCurrentMsg().isNoOperation() && (node.getQueryStage() == QueryStage.Probe1 || node.getQueryStage() == QueryStage.Probe2)) { node.queryStageRetry(node.getQueryStage(), (byte) 3); } } } else { if (queue.getCurrentMsg().isWakeUpNoMoreInformationCommand()) { WakeUp wu = (WakeUp) node.getCommandClassManager().getCommandClass(WakeUp.COMMAND_CLASS_ID); if (wu != null) { wu.setAwake(false); } } if (!node.isNodeAlive()) { node.setNodeAlive(true); } } } queue.setExpectedCallbackId((byte) 0); } }
public void handleGetRoutingInfoResponse(byte[] data) { Node node = getNode(SafeCast.nodeIdFromMsg(queue.getCurrentMsg())); if (node != null) { node.setNeighbors(Arrays.copyOfRange(data, 2, 29)); logs.add( String.format( "FUNC_ID_ZW_GET_ROUTING_INFO -- Neighbors of node %d are:", node.getNodeId())); boolean neighbors = false; for (int i = 0; i < 29; i++) { for (int j = 0; j < 8; j++) { if ((data[2 + i] & (0x01 << j)) != 0) { logs.add(String.format("--- Node %d", (i << 3) + j + 1)); neighbors = true; } } } if (!neighbors) { logs.add(String.format("no neighbor on node %d", node.getNodeId())); } } }
public boolean moveMsgToWakeUpQueue(byte targetNodeId, boolean move) { Node node = getNode(targetNodeId); if (node != null && !node.isListeningDevice() && !node.isFrequentListeningDevice() && primaryController != null && targetNodeId != primaryController.getNodeId()) { WakeUp wu = (WakeUp) node.getCommandClassManager().getCommandClass(WakeUp.COMMAND_CLASS_ID); if (wu != null) { wu.setAwake(false); if (move) { if (queue.getCurrentControllerCmd() != null) { queue.removeCurrentMsg(); } if (queue.getCurrentMsg() != null) { Msg msg = queue.getCurrentMsg(); if (targetNodeId == SafeCast.nodeIdFromMsg(msg)) { if (!msg.isWakeUpNoMoreInformationCommand() && !msg.isNoOperation()) { QueueItem item = new QueueItem(); item.setCommand(QueueCommand.SendMessage); item.setMsg(msg); wu.queueItem(item); } queue.removeCurrentMsg(); } } for (int i = 0; i < queue.size(); ++i) { Iterator<QueueItem> iter = queue.getQueue().iterator(); while (iter.hasNext()) { boolean remove = false; QueueItem it = iter.next(); if (it.getCommand() == QueueCommand.SendMessage && targetNodeId == SafeCast.nodeIdFromMsg(it.getMsg())) { if (!it.getMsg().isWakeUpNoMoreInformationCommand() && !it.getMsg().isNoOperation()) { wu.queueItem(it); } else { it.setMsg(null); } remove = true; } else if (it.getCommand() == QueueCommand.QueryStageComplete && targetNodeId == SafeCast.nodeIdFromMsg(it.getMsg())) { wu.queueItem(it); remove = true; } else if (it.getCommand() == QueueCommand.Controller && targetNodeId == SafeCast.nodeIdFromMsg(it.getMsg())) { wu.queueItem(it); remove = true; } if (remove) { iter.remove(); } } } if (queue.getCurrentControllerCmd() != null) { primaryController.updateControllerState(ControllerState.Sleeping); queue.sendControllerCommand(queue.getCurrentControllerCmd()); } return true; } } } return false; }