LWM2M: add update Firmware from terminal

This commit is contained in:
nickAS21 2021-06-07 21:15:12 +03:00
parent 99dc49db26
commit 9fd2d34e7c
4 changed files with 73 additions and 53 deletions

View File

@ -90,6 +90,7 @@ import static org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST;
import static org.eclipse.leshan.core.attributes.Attribute.OBJECT_VERSION; import static org.eclipse.leshan.core.attributes.Attribute.OBJECT_VERSION;
import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH; import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH;
import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.FAILED; import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.FAILED;
import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.INITIATED;
import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper.getValueFromKvProto; import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper.getValueFromKvProto;
import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.CLIENT_NOT_AUTHORIZED; import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.CLIENT_NOT_AUTHORIZED;
import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.DEVICE_ATTRIBUTES_REQUEST; import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.DEVICE_ATTRIBUTES_REQUEST;
@ -139,7 +140,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
public final LwM2mClientContext clientContext; public final LwM2mClientContext clientContext;
public final LwM2mTransportRequest lwM2mTransportRequest; public final LwM2mTransportRequest lwM2mTransportRequest;
private final Map<UUID, Long> rpcSubscriptions; private final Map<UUID, Long> rpcSubscriptions;
private final Map<Integer, String> getCoapResource; public final Map<String, Integer> firmwareUpdateState;
public DefaultLwM2MTransportMsgHandler(TransportService transportService, LwM2MTransportServerConfig config, LwM2mTransportServerHelper helper, public DefaultLwM2MTransportMsgHandler(TransportService transportService, LwM2MTransportServerConfig config, LwM2mTransportServerHelper helper,
LwM2mClientContext clientContext, LwM2mClientContext clientContext,
@ -155,7 +156,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
this.context = context; this.context = context;
this.adaptor = adaptor; this.adaptor = adaptor;
this.rpcSubscriptions = new ConcurrentHashMap<>(); this.rpcSubscriptions = new ConcurrentHashMap<>();
this.getCoapResource = new ConcurrentHashMap<>(); this.firmwareUpdateState = new ConcurrentHashMap<>();
this.sessionStore = sessionStore; this.sessionStore = sessionStore;
} }
@ -202,7 +203,9 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
.setSubscribeToRPC(TransportProtos.SubscribeToRPCMsg.newBuilder() .setSubscribeToRPC(TransportProtos.SubscribeToRPCMsg.newBuilder()
.setSessionType(TransportProtos.SessionType.ASYNC).build()) .setSessionType(TransportProtos.SessionType.ASYNC).build())
.build(), null); .build(), null);
if (!(this.firmwareUpdateState.containsKey(lwM2MClient.getEndpoint()) && this.firmwareUpdateState.get(lwM2MClient.getEndpoint()) == 3)) {
this.getInfoFirmwareUpdate(lwM2MClient, null); this.getInfoFirmwareUpdate(lwM2MClient, null);
}
this.getInfoSoftwareUpdate(lwM2MClient, null); this.getInfoSoftwareUpdate(lwM2MClient, null);
this.initLwM2mFromClientValue(registration, lwM2MClient); this.initLwM2mFromClientValue(registration, lwM2MClient);
this.sendLogsToThingsboard(LOG_LW2M_INFO + ": " + msgReg, registration.getId()); this.sendLogsToThingsboard(LOG_LW2M_INFO + ": " + msgReg, registration.getId());
@ -243,8 +246,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
this.sendLogsToThingsboard(LOG_LW2M_ERROR + ": Client update Registration", registration.getId()); this.sendLogsToThingsboard(LOG_LW2M_ERROR + ": Client update Registration", registration.getId());
} }
}); });
} } else {
else {
this.onRegistered(registration, null); this.onRegistered(registration, null);
} }
} catch (Throwable t) { } catch (Throwable t) {
@ -458,14 +460,15 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
// #1 // #1
this.checkRpcRequestTimeout(); this.checkRpcRequestTimeout();
UUID requestUUID = new UUID(toDeviceRpcRequestMsg.getRequestIdMSB(), toDeviceRpcRequestMsg.getRequestIdLSB()); UUID requestUUID = new UUID(toDeviceRpcRequestMsg.getRequestIdMSB(), toDeviceRpcRequestMsg.getRequestIdLSB());
log.warn("4) toDeviceRpcRequestMsg: [{}], sessionUUID: [{}]", toDeviceRpcRequestMsg, requestUUID); log.warn("4) toDeviceRpcRequestMsg: [{}], requestUUID: [{}]", toDeviceRpcRequestMsg, requestUUID);
String bodyParams = StringUtils.trimToNull(toDeviceRpcRequestMsg.getParams()) != null ? toDeviceRpcRequestMsg.getParams() : "null"; String bodyParams = StringUtils.trimToNull(toDeviceRpcRequestMsg.getParams()) != null ? toDeviceRpcRequestMsg.getParams() : "null";
LwM2mTypeOper lwM2mTypeOper = setValidTypeOper(toDeviceRpcRequestMsg.getMethodName()); LwM2mTypeOper lwM2mTypeOper = setValidTypeOper(toDeviceRpcRequestMsg.getMethodName());
// if (!this.rpcSubscriptions.containsKey(requestUUID)) { if (!this.rpcSubscriptions.containsKey(requestUUID)) {
this.rpcSubscriptions.put(requestUUID, toDeviceRpcRequestMsg.getExpirationTime()); this.rpcSubscriptions.put(requestUUID, toDeviceRpcRequestMsg.getExpirationTime());
LwM2mClientRpcRequest lwm2mClientRpcRequest = null; LwM2mClientRpcRequest lwm2mClientRpcRequest = null;
try { try {
Registration registration = clientContext.getClient(sessionInfo).getRegistration(); Registration registration = clientContext.getRegistration(sessionInfo);
if (registration != null) {
lwm2mClientRpcRequest = new LwM2mClientRpcRequest(lwM2mTypeOper, bodyParams, toDeviceRpcRequestMsg.getRequestId(), sessionInfo, registration, this); lwm2mClientRpcRequest = new LwM2mClientRpcRequest(lwM2mTypeOper, bodyParams, toDeviceRpcRequestMsg.getRequestId(), sessionInfo, registration, this);
if (lwm2mClientRpcRequest.getErrorMsg() != null) { if (lwm2mClientRpcRequest.getErrorMsg() != null) {
lwm2mClientRpcRequest.setResponseCode(BAD_REQUEST.name()); lwm2mClientRpcRequest.setResponseCode(BAD_REQUEST.name());
@ -474,20 +477,27 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
lwM2mTransportRequest.sendAllRequest(registration, lwm2mClientRpcRequest.getTargetIdVer(), lwm2mClientRpcRequest.getTypeOper(), lwM2mTransportRequest.sendAllRequest(registration, lwm2mClientRpcRequest.getTargetIdVer(), lwm2mClientRpcRequest.getTypeOper(),
null, null,
lwm2mClientRpcRequest.getValue() == null ? lwm2mClientRpcRequest.getParams() : lwm2mClientRpcRequest.getValue(), lwm2mClientRpcRequest.getValue() == null ? lwm2mClientRpcRequest.getParams() : lwm2mClientRpcRequest.getValue(),
this.config.getTimeout(), lwm2mClientRpcRequest); this.config.getBlockwiseLifetime(), lwm2mClientRpcRequest);
}
} else {
this.sentErrorRpcResponse(lwm2mClientRpcRequest, "registration == null", sessionInfo);
} }
} catch (Exception e) { } catch (Exception e) {
this.sentErrorRpcResponse(lwm2mClientRpcRequest, e.getMessage(), sessionInfo);
}
}
}
private void sentErrorRpcResponse(LwM2mClientRpcRequest lwm2mClientRpcRequest, String msgError, SessionInfoProto sessionInfo) {
if (lwm2mClientRpcRequest == null) { if (lwm2mClientRpcRequest == null) {
lwm2mClientRpcRequest = new LwM2mClientRpcRequest(); lwm2mClientRpcRequest = new LwM2mClientRpcRequest();
} }
lwm2mClientRpcRequest.setResponseCode(BAD_REQUEST.name()); lwm2mClientRpcRequest.setResponseCode(BAD_REQUEST.name());
if (lwm2mClientRpcRequest.getErrorMsg() == null) { if (lwm2mClientRpcRequest.getErrorMsg() == null) {
lwm2mClientRpcRequest.setErrorMsg(e.getMessage()); lwm2mClientRpcRequest.setErrorMsg(msgError);
} }
this.onToDeviceRpcResponse(lwm2mClientRpcRequest.getDeviceRpcResponseResultMsg(), sessionInfo); this.onToDeviceRpcResponse(lwm2mClientRpcRequest.getDeviceRpcResponseResultMsg(), sessionInfo);
} }
// }
}
private void checkRpcRequestTimeout() { private void checkRpcRequestTimeout() {
log.warn("4.1) before rpcSubscriptions.size(): [{}]", rpcSubscriptions.size()); log.warn("4.1) before rpcSubscriptions.size(): [{}]", rpcSubscriptions.size());
@ -1355,6 +1365,9 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
if (TransportProtos.ResponseStatus.SUCCESS.equals(response.getResponseStatus()) if (TransportProtos.ResponseStatus.SUCCESS.equals(response.getResponseStatus())
&& response.getType().equals(OtaPackageType.FIRMWARE.name())) { && response.getType().equals(OtaPackageType.FIRMWARE.name())) {
LwM2mFwSwUpdate fwUpdate = lwM2MClient.getFwUpdate(clientContext); LwM2mFwSwUpdate fwUpdate = lwM2MClient.getFwUpdate(clientContext);
if (rpcRequest != null) {
fwUpdate.setStateUpdate(INITIATED.name());
}
if (!FAILED.name().equals(fwUpdate.getStateUpdate())) { if (!FAILED.name().equals(fwUpdate.getStateUpdate())) {
log.warn("7) firmware start with ver: [{}]", response.getVersion()); log.warn("7) firmware start with ver: [{}]", response.getVersion());
fwUpdate.setRpcRequest(rpcRequest); fwUpdate.setRpcRequest(rpcRequest);
@ -1366,12 +1379,18 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
} else { } else {
fwUpdate.writeFwSwWare(handler, lwM2mTransportRequest); fwUpdate.writeFwSwWare(handler, lwM2mTransportRequest);
} }
} } else {
else { String msgError = String.format("OtaPackage device: %s, version: %s, stateUpdate: %s",
log.warn("7_1) OtaPackage [{}] [{}] [{}]", lwM2MClient.getDeviceName(), response.getVersion(), fwUpdate.getStateUpdate()); lwM2MClient.getDeviceName(), response.getVersion(), fwUpdate.getStateUpdate());
log.warn("7_1 [{}]", msgError);
} }
} else { } else {
log.trace("OtaPackage [{}] [{}]", lwM2MClient.getDeviceName(), response.getResponseStatus().toString()); String msgError = String.format("OtaPackage device: %s, responseStatus: %s",
lwM2MClient.getDeviceName(), response.getResponseStatus().toString());
log.trace(msgError);
if (rpcRequest != null) {
sentErrorRpcResponse(rpcRequest, msgError, sessionInfo);
}
} }
} }

View File

@ -35,6 +35,8 @@ public interface LwM2mClientContext {
LwM2mClient getClient(TransportProtos.SessionInfoProto sessionInfo); LwM2mClient getClient(TransportProtos.SessionInfoProto sessionInfo);
Registration getRegistration(TransportProtos.SessionInfoProto sessionInfo);
LwM2mClient getOrRegister(Registration registration); LwM2mClient getOrRegister(Registration registration);
LwM2mClient registerOrUpdate(Registration registration); LwM2mClient registerOrUpdate(Registration registration);

View File

@ -91,32 +91,31 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
Predicate<LwM2mClient> isClientFilter = c -> Predicate<LwM2mClient> isClientFilter = c ->
(new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB())) (new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()))
.equals((new UUID(c.getSession().getSessionIdMSB(), c.getSession().getSessionIdLSB()))); .equals((new UUID(c.getSession().getSessionIdMSB(), c.getSession().getSessionIdLSB())));
// if (this.lwM2mClientsByEndpoint.size()>0) {
// lwM2mClient = this.lwM2mClientsByEndpoint.values().stream().filter(c ->
// (new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()))
// .equals((new UUID(c.getSession().getSessionIdMSB(), c.getSession().getSessionIdLSB())))
// ).findAny().get();
// }
if (this.lwM2mClientsByEndpoint.size() > 0) { if (this.lwM2mClientsByEndpoint.size() > 0) {
lwM2mClient = this.lwM2mClientsByEndpoint.values().stream().filter(isClientFilter).findAny().get(); lwM2mClient = this.lwM2mClientsByEndpoint.values().stream().filter(isClientFilter).findAny().orElse(null);
} }
// if (lwM2mClient == null && this.lwM2mClientsByRegistrationId.size() > 0) {
// lwM2mClient = this.lwM2mClientsByRegistrationId.values().stream().filter(c ->
// (new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()))
// .equals((new UUID(c.getSession().getSessionIdMSB(), c.getSession().getSessionIdLSB())))
// ).findAny().get();
// }
if (lwM2mClient == null && this.lwM2mClientsByRegistrationId.size() > 0) { if (lwM2mClient == null && this.lwM2mClientsByRegistrationId.size() > 0) {
lwM2mClient = this.lwM2mClientsByRegistrationId.values().stream().filter(isClientFilter).findAny().get(); lwM2mClient = this.lwM2mClientsByRegistrationId.values().stream().filter(isClientFilter).findAny().orElse(null);
} }
if (lwM2mClient == null) { if (lwM2mClient == null) {
log.warn("Device TimeOut? lwM2mClient is null."); log.warn("Device TimeOut? lwM2mClient is null.");
log.warn("SessionInfo input [{}], lwM2mClientsByEndpoint size: [{}]", sessionInfo, lwM2mClientsByEndpoint.values().size()); log.warn("SessionInfo input [{}], lwM2mClientsByEndpoint size: [{}] lwM2mClientsByRegistrationId: [{}]", sessionInfo, lwM2mClientsByEndpoint.values(), lwM2mClientsByRegistrationId.values());
log.error("", new RuntimeException()); log.error("", new RuntimeException());
} }
return lwM2mClient; return lwM2mClient;
} }
@Override
public Registration getRegistration(TransportProtos.SessionInfoProto sessionInfo) {
Registration registration = this.getClient(sessionInfo).getRegistration();
if (registration == null) {
log.warn("Device TimeOut? registration is null.");
log.warn("SessionInfo input [{}], lwM2mClientsByEndpoint size: [{}]", sessionInfo, lwM2mClientsByEndpoint.values().size());
log.error("", new RuntimeException());
}
return registration;
}
@Override @Override
public LwM2mClient registerOrUpdate(Registration registration) { public LwM2mClient registerOrUpdate(Registration registration) {
LwM2mClient lwM2MClient = lwM2mClientsByEndpoint.get(registration.getEndpoint()); LwM2mClient lwM2MClient = lwM2mClientsByEndpoint.get(registration.getEndpoint());
@ -207,8 +206,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
if (lwM2MClientProfile != null) { if (lwM2MClientProfile != null) {
profiles.put(deviceProfile.getUuidId(), lwM2MClientProfile); profiles.put(deviceProfile.getUuidId(), lwM2MClientProfile);
return lwM2MClientProfile; return lwM2MClientProfile;
} } else {
else {
return null; return null;
} }
} }

View File

@ -458,6 +458,7 @@ public class LwM2mFwSwUpdate {
if (value == LwM2mTransportUtil.StateFw.DOWNLOADED.code) { if (value == LwM2mTransportUtil.StateFw.DOWNLOADED.code) {
this.executeFwSwWare(handler, request); this.executeFwSwWare(handler, request);
} }
handler.firmwareUpdateState.put(lwM2MClient.getEndpoint(), value);
} }
if ((convertPathFromObjectIdToIdVer(FW_RESULT_ID, registration).equals(path))) { if ((convertPathFromObjectIdToIdVer(FW_RESULT_ID, registration).equals(path))) {
if (this.currentId != null && value == LwM2mTransportUtil.UpdateResultFw.INITIAL.code) { if (this.currentId != null && value == LwM2mTransportUtil.UpdateResultFw.INITIAL.code) {