Commit b0f5ff7c639a8ed5e39aeb420d25d5b810778281

Authored by nickAS21
1 parent dfa4433c

LWM2M: add RPC FirmwareUpdate

... ... @@ -196,8 +196,8 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
196 196 .setSubscribeToRPC(TransportProtos.SubscribeToRPCMsg.newBuilder().build())
197 197 .build();
198 198 transportService.process(msg, null);
199   - this.getInfoFirmwareUpdate(lwM2MClient);
200   - this.getInfoSoftwareUpdate(lwM2MClient);
  199 + this.getInfoFirmwareUpdate(lwM2MClient, null);
  200 + this.getInfoSoftwareUpdate(lwM2MClient, null);
201 201 this.initLwM2mFromClientValue(registration, lwM2MClient);
202 202 this.sendLogsToThingsboard(LOG_LW2M_INFO + ": Client create after Registration", registration.getId());
203 203 } else {
... ... @@ -286,7 +286,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
286 286 @Override
287 287 public void setCancelObservationsAll(Registration registration) {
288 288 if (registration != null) {
289   - lwM2mTransportRequest.sendAllRequest(registration, null, OBSERVE_CANCEL_ALL,
  289 + this.lwM2mTransportRequest.sendAllRequest(registration, null, OBSERVE_CANCEL_ALL,
290 290 null, null, this.config.getTimeout(), null);
291 291 }
292 292 }
... ... @@ -335,7 +335,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
335 335 READ, pathIdVer, value);
336 336 this.sendLogsToThingsboard(msg, registration.getId());
337 337 rpcRequest.setValueMsg(String.format("%s", value));
338   - this.sentRpcRequest(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE);
  338 + this.sentRpcResponse(rpcRequest, response.getCode().getName(), (String) value, LOG_LW2M_VALUE);
339 339 }
340 340
341 341 /**
... ... @@ -362,12 +362,12 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
362 362 && (!valueNew.equals(lwM2MClient.getFwUpdate().getCurrentVersion())))
363 363 || (FirmwareUtil.getAttributeKey(FirmwareType.FIRMWARE, FirmwareKey.TITLE).equals(pathName)
364 364 && (!valueNew.equals(lwM2MClient.getFwUpdate().getCurrentTitle())))) {
365   - this.getInfoFirmwareUpdate(lwM2MClient);
  365 + this.getInfoFirmwareUpdate(lwM2MClient, null);
366 366 } else if ((FirmwareUtil.getAttributeKey(FirmwareType.SOFTWARE, FirmwareKey.VERSION).equals(pathName)
367 367 && (!valueNew.equals(lwM2MClient.getSwUpdate().getCurrentVersion())))
368 368 || (FirmwareUtil.getAttributeKey(FirmwareType.SOFTWARE, FirmwareKey.TITLE).equals(pathName)
369 369 && (!valueNew.equals(lwM2MClient.getSwUpdate().getCurrentTitle())))) {
370   - this.getInfoSoftwareUpdate(lwM2MClient);
  370 + this.getInfoSoftwareUpdate(lwM2MClient, null);
371 371 }
372 372 if (pathIdVer != null) {
373 373 ResourceModel resourceModel = lwM2MClient.getResourceModel(pathIdVer, this.config
... ... @@ -484,7 +484,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
484 484 rpcSubscriptionsToRemove.forEach(rpcSubscriptions::remove);
485 485 }
486 486
487   - public void sentRpcRequest(Lwm2mClientRpcRequest rpcRequest, String requestCode, String msg, String typeMsg) {
  487 + public void sentRpcResponse(Lwm2mClientRpcRequest rpcRequest, String requestCode, String msg, String typeMsg) {
488 488 rpcRequest.setResponseCode(requestCode);
489 489 if (LOG_LW2M_ERROR.equals(typeMsg)) {
490 490 rpcRequest.setInfoMsg(null);
... ... @@ -507,7 +507,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
507 507
508 508 @Override
509 509 public void onToDeviceRpcResponse(TransportProtos.ToDeviceRpcResponseMsg toDeviceResponse, SessionInfoProto sessionInfo) {
510   - log.warn ("5) nToDeviceRpcResponse: [{}], sessionUUID: [{}]", toDeviceResponse, new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()));
  510 + log.warn ("5) onToDeviceRpcResponse: [{}], sessionUUID: [{}]", toDeviceResponse, new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB()));
511 511 transportService.process(sessionInfo, toDeviceResponse, null);
512 512 }
513 513
... ... @@ -1358,21 +1358,28 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
1358 1358 }
1359 1359 }
1360 1360
1361   - public void getInfoFirmwareUpdate(LwM2mClient lwM2MClient) {
  1361 + public void getInfoFirmwareUpdate(LwM2mClient lwM2MClient, Lwm2mClientRpcRequest rpcRequest) {
1362 1362 if (lwM2MClient.getRegistration().getSupportedVersion(FW_ID) != null) {
1363 1363 SessionInfoProto sessionInfo = this.getSessionInfoOrCloseSession(lwM2MClient);
1364 1364 if (sessionInfo != null) {
1365   - transportService.process(sessionInfo, createFirmwareRequestMsg(sessionInfo, FirmwareType.FIRMWARE.name()),
  1365 + DefaultLwM2MTransportMsgHandler handler = this;
  1366 + this.transportService.process(sessionInfo, createFirmwareRequestMsg(sessionInfo, FirmwareType.FIRMWARE.name()),
1366 1367 new TransportServiceCallback<>() {
1367 1368 @Override
1368 1369 public void onSuccess(TransportProtos.GetFirmwareResponseMsg response) {
1369 1370 if (TransportProtos.ResponseStatus.SUCCESS.equals(response.getResponseStatus())
1370 1371 && response.getType().equals(FirmwareType.FIRMWARE.name())) {
1371   - log.warn ("7) firmware start with ver: [{}]",response.getVersion());
  1372 + log.warn ("7) firmware start with ver: [{}]", response.getVersion());
  1373 + lwM2MClient.getFwUpdate().setRpcRequest(rpcRequest);
1372 1374 lwM2MClient.getFwUpdate().setCurrentVersion(response.getVersion());
1373 1375 lwM2MClient.getFwUpdate().setCurrentTitle(response.getTitle());
1374 1376 lwM2MClient.getFwUpdate().setCurrentId(new FirmwareId(new UUID(response.getFirmwareIdMSB(), response.getFirmwareIdLSB())).getId());
1375   - lwM2MClient.getFwUpdate().sendReadObserveInfo(lwM2mTransportRequest);
  1377 + if (rpcRequest == null) {
  1378 + lwM2MClient.getFwUpdate().sendReadObserveInfo(lwM2mTransportRequest);
  1379 + }
  1380 + else {
  1381 + lwM2MClient.getFwUpdate().writeFwSwWare(handler, lwM2mTransportRequest);
  1382 + }
1376 1383 } else {
1377 1384 log.trace("Firmware [{}] [{}]", lwM2MClient.getDeviceName(), response.getResponseStatus().toString());
1378 1385 }
... ... @@ -1387,21 +1394,28 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
1387 1394 }
1388 1395 }
1389 1396
1390   - public void getInfoSoftwareUpdate(LwM2mClient lwM2MClient) {
  1397 + public void getInfoSoftwareUpdate(LwM2mClient lwM2MClient, Lwm2mClientRpcRequest rpcRequest) {
1391 1398 if (lwM2MClient.getRegistration().getSupportedVersion(SW_ID) != null) {
1392 1399 SessionInfoProto sessionInfo = this.getSessionInfoOrCloseSession(lwM2MClient);
1393 1400 if (sessionInfo != null) {
1394   - DefaultLwM2MTransportMsgHandler serviceImpl = this;
  1401 + DefaultLwM2MTransportMsgHandler handler = this;
1395 1402 transportService.process(sessionInfo, createFirmwareRequestMsg(sessionInfo, FirmwareType.SOFTWARE.name()),
1396 1403 new TransportServiceCallback<>() {
1397 1404 @Override
1398 1405 public void onSuccess(TransportProtos.GetFirmwareResponseMsg response) {
1399 1406 if (TransportProtos.ResponseStatus.SUCCESS.equals(response.getResponseStatus())
1400 1407 && response.getType().equals(FirmwareType.SOFTWARE.name())) {
  1408 + lwM2MClient.getSwUpdate().setRpcRequest(rpcRequest);
1401 1409 lwM2MClient.getSwUpdate().setCurrentVersion(response.getVersion());
1402 1410 lwM2MClient.getSwUpdate().setCurrentTitle(response.getTitle());
1403 1411 lwM2MClient.getSwUpdate().setCurrentId(new FirmwareId(new UUID(response.getFirmwareIdMSB(), response.getFirmwareIdLSB())).getId());
1404 1412 lwM2MClient.getSwUpdate().sendReadObserveInfo(lwM2mTransportRequest);
  1413 + if (rpcRequest == null) {
  1414 + lwM2MClient.getSwUpdate().sendReadObserveInfo(lwM2mTransportRequest);
  1415 + }
  1416 + else {
  1417 + lwM2MClient.getSwUpdate().writeFwSwWare(handler, lwM2mTransportRequest);
  1418 + }
1405 1419 } else {
1406 1420 log.trace("Software [{}] [{}]", lwM2MClient.getDeviceName(), response.getResponseStatus().toString());
1407 1421 }
... ...
... ... @@ -82,10 +82,8 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.L
82 82 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_VALUE;
83 83 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper;
84 84 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.DISCOVER;
85   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.DISCOVER_ALL;
86 85 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.EXECUTE;
87 86 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_CANCEL;
88   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_CANCEL_ALL;
89 87 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
90 88 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_ATTRIBUTES;
91 89 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_REPLACE;
... ... @@ -134,7 +132,7 @@ public class LwM2mTransportRequest {
134 132 ContentFormat contentFormat = contentFormatName != null ? ContentFormat.fromName(contentFormatName.toUpperCase()) : ContentFormat.DEFAULT;
135 133 LwM2mClient lwM2MClient = this.lwM2mClientContext.getOrRegister(registration);
136 134 LwM2mPath resultIds = target != null ? new LwM2mPath(target) : null;
137   - if (!OBSERVE_READ_ALL.name().equals(typeOper.name()) && resultIds != null && registration != null && resultIds.getObjectId() >= 0 && lwM2MClient != null) {
  135 + if (!OBSERVE_CANCEL.name().equals(typeOper.name()) && resultIds != null && registration != null && resultIds.getObjectId() >= 0 && lwM2MClient != null) {
138 136 if (lwM2MClient.isValidObjectVersion(targetIdVer)) {
139 137 timeoutInMs = timeoutInMs > 0 ? timeoutInMs : DEFAULT_TIMEOUT;
140 138 DownlinkRequest request = createRequest(registration, lwM2MClient, typeOper, contentFormat, target,
... ... @@ -153,47 +151,66 @@ public class LwM2mTransportRequest {
153 151 } else if (WRITE_UPDATE.name().equals(typeOper.name())) {
154 152 if (lwm2mClientRpcRequest != null) {
155 153 String errorMsg = String.format("Path %s params is not valid", targetIdVer);
156   - handler.sentRpcRequest(lwm2mClientRpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
  154 + handler.sentRpcResponse(lwm2mClientRpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
157 155 }
158 156 } else if (WRITE_REPLACE.name().equals(typeOper.name()) || EXECUTE.name().equals(typeOper.name())) {
159 157 if (lwm2mClientRpcRequest != null) {
160 158 String errorMsg = String.format("Path %s object model is absent", targetIdVer);
161   - handler.sentRpcRequest(lwm2mClientRpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
  159 + handler.sentRpcResponse(lwm2mClientRpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
162 160 }
163 161 } else if (!OBSERVE_CANCEL.name().equals(typeOper.name())) {
164 162 log.error("[{}], [{}] - [{}] error SendRequest", registration.getEndpoint(), typeOper.name(), targetIdVer);
165 163 if (lwm2mClientRpcRequest != null) {
166 164 ResourceModel resourceModel = lwM2MClient.getResourceModel(targetIdVer, this.config.getModelProvider());
167 165 String errorMsg = resourceModel == null ? String.format("Path %s not found in object version", targetIdVer) : "SendRequest - null";
168   - this.handler.sentRpcRequest(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
  166 + this.handler.sentRpcResponse(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
169 167 }
170 168 }
171 169 } else if (lwm2mClientRpcRequest != null) {
172 170 String errorMsg = String.format("Path %s not found in object version", targetIdVer);
173   - this.handler.sentRpcRequest(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
  171 + this.handler.sentRpcResponse(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
174 172 }
175   - } else if (OBSERVE_READ_ALL.name().equals(typeOper.name()) || DISCOVER_ALL.name().equals(typeOper.name())) {
176   - Set<String> paths;
177   - if (OBSERVE_READ_ALL.name().equals(typeOper.name())) {
178   - Set<Observation> observations = context.getServer().getObservationService().getObservations(registration);
179   - paths = observations.stream().map(observation -> observation.getPath().toString()).collect(Collectors.toUnmodifiableSet());
180   - } else {
181   - assert registration != null;
182   - Link[] objectLinks = registration.getSortedObjectLinks();
183   - paths = Arrays.stream(objectLinks).map(Link::toString).collect(Collectors.toUnmodifiableSet());
184   - }
185   - String msg = String.format("%s: type operation %s paths - %s", LOG_LW2M_INFO,
186   - typeOper.name(), paths);
187   - this.handler.sendLogsToThingsboard(msg, registration.getId());
188   - if (lwm2mClientRpcRequest != null) {
189   - String valueMsg = String.format("Paths - %s", paths);
190   - this.handler.sentRpcRequest(lwm2mClientRpcRequest, CONTENT.name(), valueMsg, LOG_LW2M_VALUE);
  173 + } else {
  174 + switch (typeOper) {
  175 + case OBSERVE_READ_ALL:
  176 + case DISCOVER_ALL:
  177 + Set<String> paths;
  178 + if (OBSERVE_READ_ALL.name().equals(typeOper.name())) {
  179 + Set<Observation> observations = context.getServer().getObservationService().getObservations(registration);
  180 + paths = observations.stream().map(observation -> observation.getPath().toString()).collect(Collectors.toUnmodifiableSet());
  181 + } else {
  182 + assert registration != null;
  183 + Link[] objectLinks = registration.getSortedObjectLinks();
  184 + paths = Arrays.stream(objectLinks).map(Link::toString).collect(Collectors.toUnmodifiableSet());
  185 + }
  186 + String msg = String.format("%s: type operation %s paths - %s", LOG_LW2M_INFO,
  187 + typeOper.name(), paths);
  188 + this.handler.sendLogsToThingsboard(msg, registration.getId());
  189 + if (lwm2mClientRpcRequest != null) {
  190 + String valueMsg = String.format("Paths - %s", paths);
  191 + this.handler.sentRpcResponse(lwm2mClientRpcRequest, CONTENT.name(), valueMsg, LOG_LW2M_VALUE);
  192 + }
  193 + break;
  194 + case OBSERVE_CANCEL:
  195 + case OBSERVE_CANCEL_ALL:
  196 + int observeCancelCnt = 0;
  197 + String observeCancelMsg = null;
  198 + if (OBSERVE_CANCEL.name().equals(typeOper)) {
  199 + observeCancelCnt = context.getServer().getObservationService().cancelObservations(registration, target);
  200 + observeCancelMsg = String.format("%s: type operation %s paths: %s count: %d", LOG_LW2M_INFO,
  201 + OBSERVE_CANCEL.name(), target, observeCancelCnt);
  202 + } else {
  203 + observeCancelCnt = context.getServer().getObservationService().cancelObservations(registration);
  204 + observeCancelMsg = String.format("%s: type operation %s paths: All count: %d", LOG_LW2M_INFO,
  205 + OBSERVE_CANCEL.name(), observeCancelCnt);
  206 + }
  207 + this.afterObserveCancel(registration, observeCancelCnt, observeCancelMsg, lwm2mClientRpcRequest);
  208 + break;
  209 + // lwm2mClientRpcRequest != null
  210 + case FW_UPDATE:
  211 + this.handler.getInfoFirmwareUpdate(lwM2MClient, lwm2mClientRpcRequest);
  212 + break;
191 213 }
192   - } else if (OBSERVE_CANCEL_ALL.name().equals(typeOper.name())) {
193   - int observeCancelCnt = context.getServer().getObservationService().cancelObservations(registration);
194   - String observeCancelMsgAll = String.format("%s: type operation %s paths: All count: %d", LOG_LW2M_INFO,
195   - OBSERVE_CANCEL.name(), observeCancelCnt);
196   - this.afterObserveCancel(registration, observeCancelCnt, observeCancelMsgAll, lwm2mClientRpcRequest);
197 214 }
198 215 } catch (Exception e) {
199 216 String msg = String.format("%s: type operation %s %s", LOG_LW2M_ERROR,
... ... @@ -201,7 +218,7 @@ public class LwM2mTransportRequest {
201 218 handler.sendLogsToThingsboard(msg, registration.getId());
202 219 if (lwm2mClientRpcRequest != null) {
203 220 String errorMsg = String.format("Path %s type operation %s %s", targetIdVer, typeOper.name(), e.getMessage());
204   - handler.sentRpcRequest(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
  221 + handler.sentRpcResponse(lwm2mClientRpcRequest, NOT_FOUND.getName(), errorMsg, LOG_LW2M_ERROR);
205 222 }
206 223 }
207 224 }
... ... @@ -234,17 +251,6 @@ public class LwM2mTransportRequest {
234 251 request = new ObserveRequest(contentFormat, resultIds.getObjectId());
235 252 }
236 253 break;
237   - case OBSERVE_CANCEL:
238   - /*
239   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, POST_TYPE_OPER_OBSERVE_CANCEL, null, null, null, null, context.getTimeout());
240   - At server side this will not remove the observation from the observation store, to do it you need to use
241   - {@code ObservationService#cancelObservation()}
242   - */
243   - int observeCancelCnt = context.getServer().getObservationService().cancelObservations(registration, target);
244   - String observeCancelMsg = String.format("%s: type operation %s paths: %s count: %d", LOG_LW2M_INFO,
245   - OBSERVE_CANCEL.name(), target, observeCancelCnt);
246   - this.afterObserveCancel(registration, observeCancelCnt, observeCancelMsg, rpcRequest);
247   - break;
248 254 case EXECUTE:
249 255 ResourceModel resourceModelExecute = lwM2MClient.getResourceModel(targetIdVer, this.config.getModelProvider());
250 256 if (resourceModelExecute != null) {
... ... @@ -343,7 +349,7 @@ public class LwM2mTransportRequest {
343 349 }
344 350 /** Not Found */
345 351 if (rpcRequest != null) {
346   - handler.sentRpcRequest(rpcRequest, response.getCode().getName(), response.getErrorMessage(), LOG_LW2M_ERROR);
  352 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), response.getErrorMessage(), LOG_LW2M_ERROR);
347 353 }
348 354 /** Not Found
349 355 set setClient_fw_info... = empty
... ... @@ -385,7 +391,7 @@ public class LwM2mTransportRequest {
385 391 handler.sendLogsToThingsboard(msg, registration.getId());
386 392 log.error("[{}] [{}] - [{}] error SendRequest", request.getClass().getName().toString(), request.getPath().toString(), e.toString());
387 393 if (rpcRequest != null) {
388   - handler.sentRpcRequest(rpcRequest, CoAP.CodeClass.ERROR_RESPONSE.name(), e.getMessage(), LOG_LW2M_ERROR);
  394 + handler.sentRpcResponse(rpcRequest, CoAP.CodeClass.ERROR_RESPONSE.name(), e.getMessage(), LOG_LW2M_ERROR);
389 395 }
390 396 });
391 397 }
... ... @@ -431,7 +437,7 @@ public class LwM2mTransportRequest {
431 437 log.error("Path: [{}] type: [{}] value: [{}] errorMsg: [{}]]", patn, type, value, e.toString());
432 438 if (rpcRequest != null) {
433 439 String errorMsg = String.format("NumberFormatException: Resource path - %s type - %s value - %s", patn, type, value);
434   - handler.sentRpcRequest(rpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
  440 + handler.sentRpcResponse(rpcRequest, BAD_REQUEST.getName(), errorMsg, LOG_LW2M_ERROR);
435 441 }
436 442 return null;
437 443 }
... ... @@ -462,44 +468,48 @@ public class LwM2mTransportRequest {
462 468 if (response instanceof ReadResponse) {
463 469 handler.onUpdateValueAfterReadResponse(registration, pathIdVer, (ReadResponse) response, rpcRequest);
464 470 } else if (response instanceof DeleteResponse) {
465   - log.warn("[{}] Path [{}] DeleteResponse 5_Send", pathIdVer, response);
  471 + log.warn("11) [{}] Path [{}] DeleteResponse", pathIdVer, response);
  472 + if (rpcRequest != null) {
  473 + rpcRequest.setInfoMsg(null);
  474 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), null, null);
  475 + }
466 476 } else if (response instanceof DiscoverResponse) {
467   - String discoverValue = Link.serialize(((DiscoverResponse)response).getObjectLinks());
  477 + String discoverValue = Link.serialize(((DiscoverResponse) response).getObjectLinks());
468 478 msgLog = String.format("%s: type operation: %s path: %s value: %s",
469 479 LOG_LW2M_INFO, DISCOVER.name(), request.getPath().toString(), discoverValue);
470 480 handler.sendLogsToThingsboard(msgLog, registration.getId());
471 481 log.warn("DiscoverResponse: [{}]", (DiscoverResponse) response);
472 482 if (rpcRequest != null) {
473   - handler.sentRpcRequest(rpcRequest, response.getCode().getName(), discoverValue, LOG_LW2M_VALUE);
  483 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), discoverValue, LOG_LW2M_VALUE);
474 484 }
475 485 } else if (response instanceof ExecuteResponse) {
476   - log.warn("[{}] Path [{}] ExecuteResponse 7_Send", pathIdVer, response);
  486 + msgLog = String.format("%s: type operation: %s path: %s",
  487 + LOG_LW2M_INFO, EXECUTE.name(), request.getPath().toString());
  488 + log.warn("9) [{}] ", msgLog);
  489 + handler.sendLogsToThingsboard(msgLog, registration.getId());
  490 + if (rpcRequest != null) {
  491 + msgLog = String.format("Start %s path: %S. Preparation finished: %s", EXECUTE.name(), path, rpcRequest.getInfoMsg());
  492 + rpcRequest.setInfoMsg(msgLog);
  493 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), path, LOG_LW2M_INFO);
  494 + }
  495 +
477 496 } else if (response instanceof WriteAttributesResponse) {
478 497 msgLog = String.format("%s: type operation: %s path: %s value: %s",
479 498 LOG_LW2M_INFO, WRITE_ATTRIBUTES.name(), request.getPath().toString(), ((WriteAttributesRequest) request).getAttributes().toString());
480 499 handler.sendLogsToThingsboard(msgLog, registration.getId());
481   - log.warn("[{}] Path [{}] WriteAttributesResponse 8_Send", pathIdVer, response);
  500 + log.warn("12) [{}] Path [{}] WriteAttributesResponse", pathIdVer, response);
482 501 if (rpcRequest != null) {
483   - handler.sentRpcRequest(rpcRequest, response.getCode().getName(), response.toString(), LOG_LW2M_VALUE);
  502 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), response.toString(), LOG_LW2M_VALUE);
484 503 }
485 504 } else if (response instanceof WriteResponse) {
486   - log.warn("[{}] Path [{}] WriteResponse 9_Send", pathIdVer, response);
487   - this.infoWriteResponse(registration, response, request);
  505 + msgLog = String.format("Type operation: Write path: %s", pathIdVer);
  506 + log.warn("10) [{}] response: [{}]", msgLog, response);
  507 + this.infoWriteResponse(registration, response, request, rpcRequest);
488 508 handler.onWriteResponseOk(registration, pathIdVer, (WriteRequest) request);
489 509 }
490   - if (rpcRequest != null) {
491   - if (response instanceof ExecuteResponse
492   - || response instanceof WriteAttributesResponse
493   - || response instanceof DeleteResponse) {
494   - rpcRequest.setInfoMsg(null);
495   - handler.sentRpcRequest(rpcRequest, response.getCode().getName(), null, null);
496   - } else if (response instanceof WriteResponse) {
497   - handler.sentRpcRequest(rpcRequest, response.getCode().getName(), null, LOG_LW2M_INFO);
498   - }
499   - }
500 510 }
501 511
502   - private void infoWriteResponse(Registration registration, LwM2mResponse response, DownlinkRequest request) {
  512 + private void infoWriteResponse(Registration registration, LwM2mResponse response, DownlinkRequest request, Lwm2mClientRpcRequest rpcRequest) {
503 513 try {
504 514 LwM2mNode node = ((WriteRequest) request).getNode();
505 515 String msg = null;
... ... @@ -517,12 +527,12 @@ public class LwM2mTransportRequest {
517 527 if (singleResource.getType() == ResourceModel.Type.STRING) {
518 528 valueLength = ((String) singleResource.getValue()).length();
519 529 value = ((String) singleResource.getValue())
520   - .substring(Math.min(valueLength, config.getLogMaxLength()));
  530 + .substring(Math.min(valueLength, config.getLogMaxLength())).trim();
521 531
522 532 } else {
523 533 valueLength = ((byte[]) singleResource.getValue()).length;
524 534 value = new String(Arrays.copyOf(((byte[]) singleResource.getValue()),
525   - Math.min(valueLength, config.getLogMaxLength())));
  535 + Math.min(valueLength, config.getLogMaxLength()))).trim();
526 536 }
527 537 value = valueLength > config.getLogMaxLength() ? value + "..." : value;
528 538 msg = String.format("%s: Update finished successfully: Lwm2m code - %d Resource path: %s length: %s value: %s",
... ... @@ -538,6 +548,12 @@ public class LwM2mTransportRequest {
538 548 handler.sendLogsToThingsboard(msg, registration.getId());
539 549 if (request.getPath().toString().equals(FW_PACKAGE_ID) || request.getPath().toString().equals(SW_PACKAGE_ID)) {
540 550 this.afterWriteSuccessFwSwUpdate(registration, request);
  551 + if (rpcRequest != null) {
  552 + rpcRequest.setInfoMsg(msg);
  553 + }
  554 + }
  555 + else if (rpcRequest != null) {
  556 + handler.sentRpcResponse(rpcRequest, response.getCode().getName(), msg, LOG_LW2M_INFO);
541 557 }
542 558 }
543 559 } catch (Exception e) {
... ... @@ -558,7 +574,7 @@ public class LwM2mTransportRequest {
558 574 }
559 575 if (request.getPath().toString().equals(SW_PACKAGE_ID) && lwM2MClient.getSwUpdate() != null) {
560 576 lwM2MClient.getSwUpdate().setStateUpdate(DOWNLOADED.name());
561   - lwM2MClient.getSwUpdate().sendLogs(this.handler,WRITE_REPLACE.name(), LOG_LW2M_INFO, null);
  577 + lwM2MClient.getSwUpdate().sendLogs(this.handler, WRITE_REPLACE.name(), LOG_LW2M_INFO, null);
562 578 }
563 579 }
564 580
... ... @@ -592,7 +608,7 @@ public class LwM2mTransportRequest {
592 608 log.warn("[{}]", observeCancelMsg);
593 609 if (rpcRequest != null) {
594 610 rpcRequest.setInfoMsg(String.format("Count: %d", observeCancelCnt));
595   - handler.sentRpcRequest(rpcRequest, CONTENT.name(), null, LOG_LW2M_INFO);
  611 + handler.sentRpcResponse(rpcRequest, CONTENT.name(), null, LOG_LW2M_INFO);
596 612 }
597 613 }
598 614 }
... ...
... ... @@ -229,11 +229,12 @@ public class LwM2mTransportUtil {
229 229 */
230 230 WRITE_UPDATE(9, "WriteUpdate"),
231 231 WRITE_ATTRIBUTES(10, "WriteAttributes"),
232   - DELETE(11, "Delete");
  232 + DELETE(11, "Delete"),
233 233
234 234 // only for RPC
  235 + FW_UPDATE(12,"FirmwareUpdate");
235 236 // FW_READ_INFO(12, "FirmwareReadInfo"),
236   -// FW_UPDATE(13, "FirmwareUpdate"),
  237 +
237 238 // SW_READ_INFO(15, "SoftwareReadInfo"),
238 239 // SW_UPDATE(16, "SoftwareUpdate"),
239 240 // SW_UNINSTALL(18, "SoftwareUninstall");
... ...
... ... @@ -32,6 +32,7 @@ import java.util.List;
32 32 import java.util.UUID;
33 33 import java.util.concurrent.CopyOnWriteArrayList;
34 34
  35 +import static org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT;
35 36 import static org.thingsboard.server.common.data.firmware.FirmwareKey.STATE;
36 37 import static org.thingsboard.server.common.data.firmware.FirmwareType.FIRMWARE;
37 38 import static org.thingsboard.server.common.data.firmware.FirmwareType.SOFTWARE;
... ... @@ -103,6 +104,9 @@ public class LwM2mFwSwUpdate {
103 104 @Getter
104 105 @Setter
105 106 private final List<String> pendingInfoRequestsStart;
  107 + @Getter
  108 + @Setter
  109 + private volatile Lwm2mClientRpcRequest rpcRequest;
106 110
107 111 public LwM2mFwSwUpdate(LwM2mClient lwM2MClient, FirmwareType type) {
108 112 this.lwM2MClient = lwM2MClient;
... ... @@ -153,17 +157,30 @@ public class LwM2mFwSwUpdate {
153 157 * Send FsSw to Lwm2mClient:
154 158 * before operation Write: fw_state = DOWNLOADING
155 159 */
156   - private void writeFwSwWare(DefaultLwM2MTransportMsgHandler handler, LwM2mTransportRequest request) {
157   - this.stateUpdate = FirmwareUpdateStatus.DOWNLOADING.name();
158   -
159   - this.sendLogs(handler, WRITE_REPLACE.name(), LOG_LW2M_INFO, null);
160   - int chunkSize = 0;
161   - int chunk = 0;
162   - byte[] firmwareChunk = handler.firmwareDataCache.get(this.currentId.toString(), chunkSize, chunk);
163   - String targetIdVer = convertPathFromObjectIdToIdVer(this.pathPackageId, this.lwM2MClient.getRegistration());
164   - log.warn ("8) firmware send save to : [{}]", targetIdVer);
165   - request.sendAllRequest(lwM2MClient.getRegistration(), targetIdVer, WRITE_REPLACE, ContentFormat.OPAQUE.getName(),
166   - firmwareChunk, handler.config.getTimeout(), null);
  160 + public void writeFwSwWare(DefaultLwM2MTransportMsgHandler handler, LwM2mTransportRequest request) {
  161 + if (this.currentId != null) {
  162 + this.stateUpdate = FirmwareUpdateStatus.DOWNLOADING.name();
  163 + this.sendLogs(handler, WRITE_REPLACE.name(), LOG_LW2M_INFO, null);
  164 + int chunkSize = 0;
  165 + int chunk = 0;
  166 + byte[] firmwareChunk = handler.firmwareDataCache.get(this.currentId.toString(), chunkSize, chunk);
  167 + String targetIdVer = convertPathFromObjectIdToIdVer(this.pathPackageId, this.lwM2MClient.getRegistration());
  168 + String fwMsg = String.format("%s: Start type operation %s paths: %s", LOG_LW2M_INFO,
  169 + LwM2mTransportUtil.LwM2mTypeOper.FW_UPDATE.name(), FW_PACKAGE_ID);
  170 + handler.sendLogsToThingsboard(fwMsg, lwM2MClient.getRegistration().getId());
  171 + log.warn("8) Start firmware Update. Send save to: [{}] ver: [{}] path: [{}]", this.lwM2MClient.getDeviceName(), this.currentVersion, targetIdVer);
  172 + request.sendAllRequest(this.lwM2MClient.getRegistration(), targetIdVer, WRITE_REPLACE, ContentFormat.OPAQUE.getName(),
  173 + firmwareChunk, handler.config.getTimeout(), this.rpcRequest);
  174 + }
  175 + else {
  176 + String msgError = "FirmWareId is null.";
  177 + log.warn("6) [{}]", msgError);
  178 + if (this.rpcRequest != null) {
  179 + handler.sentRpcResponse(this.rpcRequest, CONTENT.name(), msgError, LOG_LW2M_ERROR);
  180 + }
  181 + log.error (msgError);
  182 + this.sendLogs(handler, WRITE_REPLACE.name(), LOG_LW2M_ERROR, msgError);
  183 + }
167 184 }
168 185
169 186 public void sendLogs(DefaultLwM2MTransportMsgHandler handler, String typeOper, String typeInfo, String msgError) {
... ... @@ -186,7 +203,7 @@ public class LwM2mFwSwUpdate {
186 203 this.setStateUpdate(UPDATING.name());
187 204 this.sendLogs(handler, EXECUTE.name(), LOG_LW2M_INFO, null);
188 205 request.sendAllRequest(this.lwM2MClient.getRegistration(), this.pathInstallId, EXECUTE, ContentFormat.TLV.getName(),
189   - null, 0, null);
  206 + null, 0, this.rpcRequest);
190 207 }
191 208
192 209 /**
... ... @@ -348,7 +365,7 @@ public class LwM2mFwSwUpdate {
348 365 this.pathResultId, this.lwM2MClient.getRegistration()));
349 366 this.pendingInfoRequestsStart.forEach(pathIdVer -> {
350 367 request.sendAllRequest(this.lwM2MClient.getRegistration(), pathIdVer, OBSERVE, ContentFormat.TLV.getName(),
351   - null, 0, null);
  368 + null, 0, this.rpcRequest);
352 369 });
353 370
354 371 }
... ...
... ... @@ -39,6 +39,7 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.K
39 39 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper;
40 40 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.DISCOVER_ALL;
41 41 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.EXECUTE;
  42 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.FW_UPDATE;
42 43 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_CANCEL;
43 44 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
44 45 import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_ATTRIBUTES;
... ... @@ -140,7 +141,8 @@ public class Lwm2mClientRpcRequest {
140 141 if (this.getTargetIdVer() == null
141 142 && !(OBSERVE_READ_ALL == this.getTypeOper()
142 143 || DISCOVER_ALL == this.getTypeOper()
143   - || OBSERVE_CANCEL == this.getTypeOper())) {
  144 + || OBSERVE_CANCEL == this.getTypeOper()
  145 + || FW_UPDATE == this.getTypeOper())) {
144 146 this.setErrorMsg(TARGET_ID_VER_KEY + " and " +
145 147 KEY_NAME_KEY + " is null or bad format");
146 148 }
... ...