|
|
|
@ -20,6 +20,7 @@ import com.google.protobuf.Descriptors; |
|
|
|
import com.google.protobuf.DynamicMessage; |
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.eclipse.californium.core.coap.CoAP; |
|
|
|
import org.eclipse.californium.core.coap.Request; |
|
|
|
import org.eclipse.californium.core.coap.Response; |
|
|
|
@ -28,7 +29,6 @@ import org.eclipse.californium.core.observe.ObserveRelation; |
|
|
|
import org.eclipse.californium.core.server.resources.CoapExchange; |
|
|
|
import org.eclipse.californium.core.server.resources.Resource; |
|
|
|
import org.eclipse.californium.core.server.resources.ResourceObserver; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.server.coapserver.CoapServerService; |
|
|
|
import org.thingsboard.server.coapserver.TbCoapDtlsSessionInfo; |
|
|
|
import org.thingsboard.server.common.data.DataConstants; |
|
|
|
@ -120,6 +120,8 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
|
|
processExchangeGetRequest(exchange, featureType.get()); |
|
|
|
} else if (featureType.get() == FeatureType.ATTRIBUTES) { |
|
|
|
processRequest(exchange, SessionMsgType.GET_ATTRIBUTES_REQUEST); |
|
|
|
} else if (featureType.get() == FeatureType.FIRMWARE) { |
|
|
|
processRequest(exchange, SessionMsgType.GET_FIRMWARE_REQUEST); |
|
|
|
} else { |
|
|
|
log.trace("Invalid feature type parameter"); |
|
|
|
exchange.respond(CoAP.ResponseCode.BAD_REQUEST); |
|
|
|
@ -201,7 +203,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
|
|
Request request = advanced.getRequest(); |
|
|
|
|
|
|
|
String dtlsSessionIdStr = request.getSourceContext().get(DTLS_SESSION_ID_KEY); |
|
|
|
if (!StringUtils.isEmpty(dtlsSessionIdStr)) { |
|
|
|
if (StringUtils.isNotEmpty(dtlsSessionIdStr)) { |
|
|
|
if (dtlsSessionIdMap != null) { |
|
|
|
TbCoapDtlsSessionInfo tbCoapDtlsSessionInfo = dtlsSessionIdMap |
|
|
|
.computeIfPresent(dtlsSessionIdStr, (dtlsSessionId, dtlsSessionInfo) -> { |
|
|
|
@ -323,6 +325,14 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
|
|
coapTransportAdaptor.convertToGetAttributes(sessionId, request), |
|
|
|
new CoapNoOpCallback(exchange)); |
|
|
|
break; |
|
|
|
case GET_FIRMWARE_REQUEST: |
|
|
|
TransportProtos.GetFirmwareRequestMsg requestMsg = TransportProtos.GetFirmwareRequestMsg.newBuilder() |
|
|
|
.setTenantIdMSB(sessionInfo.getTenantIdMSB()) |
|
|
|
.setTenantIdLSB(sessionInfo.getTenantIdLSB()) |
|
|
|
.setDeviceIdMSB(sessionInfo.getDeviceIdMSB()) |
|
|
|
.setDeviceIdLSB(sessionInfo.getDeviceIdLSB()).build(); |
|
|
|
transportContext.getTransportService().process(sessionInfo, requestMsg, new FirmwareCallback(exchange)); |
|
|
|
break; |
|
|
|
} |
|
|
|
} catch (AdaptorException e) { |
|
|
|
log.trace("[{}] Failed to decode message: ", sessionId, e); |
|
|
|
@ -424,6 +434,40 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class FirmwareCallback implements TransportServiceCallback<TransportProtos.GetFirmwareResponseMsg> { |
|
|
|
private final CoapExchange exchange; |
|
|
|
|
|
|
|
FirmwareCallback(CoapExchange exchange) { |
|
|
|
this.exchange = exchange; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onSuccess(TransportProtos.GetFirmwareResponseMsg msg) { |
|
|
|
String title = exchange.getQueryParameter("title"); |
|
|
|
String version = exchange.getQueryParameter("version"); |
|
|
|
if (msg.getResponseStatus().equals(TransportProtos.ResponseStatus.SUCCESS)) { |
|
|
|
if (msg.getTitle().equals(title) && msg.getVersion().equals(version)) { |
|
|
|
String firmwareId = new UUID(msg.getFirmwareIdMSB(), msg.getFirmwareIdLSB()).toString(); |
|
|
|
String strChunkSize = exchange.getQueryParameter("chunkSize"); |
|
|
|
String strChunk = exchange.getQueryParameter("chunk"); |
|
|
|
int chunkSize = StringUtils.isEmpty(strChunkSize) ? 0 : Integer.parseInt(strChunkSize); |
|
|
|
int chunk = StringUtils.isEmpty(strChunk) ? 0 : Integer.parseInt(strChunk); |
|
|
|
exchange.respond(CoAP.ResponseCode.CONTENT, transportContext.getFirmwareCacheReader().get(firmwareId, chunkSize, chunk)); |
|
|
|
} else { |
|
|
|
exchange.respond(CoAP.ResponseCode.BAD_REQUEST); |
|
|
|
} |
|
|
|
} else { |
|
|
|
exchange.respond(CoAP.ResponseCode.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onError(Throwable e) { |
|
|
|
log.warn("Failed to process request", e); |
|
|
|
exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static class CoapSessionListener implements SessionMsgListener { |
|
|
|
|
|
|
|
private final CoapExchange exchange; |
|
|
|
|