use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeDecoderTest method tlv_single_instance_with_obj_link.
@Test
public void tlv_single_instance_with_obj_link() throws Exception {
LwM2mObjectInstance oInstance = ((LwM2mObjectInstance) decoder.decode(ENCODED_OBJ65, ContentFormat.TLV, new LwM2mPath(65, 0), model));
assertObj65Instance(oInstance);
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeTlvDecoder method parseTlv.
@SuppressWarnings("unchecked")
private static <T extends LwM2mNode> T parseTlv(Tlv[] tlvs, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
LOG.trace("Parsing TLV content for path {}: {}", path, tlvs);
// Object
if (nodeClass == LwM2mObject.class) {
List<LwM2mObjectInstance> instances = new ArrayList<>();
// is it an array of TLV resources?
if (//
tlvs.length > 0 && (tlvs[0].getType() == TlvType.MULTIPLE_RESOURCE || tlvs[0].getType() == TlvType.RESOURCE_VALUE)) {
ObjectModel oModel = model.getObjectModel(path.getObjectId());
if (oModel == null) {
LOG.warn("No model for object {}. The tlv is decoded assuming this is a single instance object", path.getObjectId());
instances.add(parseObjectInstanceTlv(tlvs, path.getObjectId(), 0, model));
} else if (!oModel.multiple) {
instances.add(parseObjectInstanceTlv(tlvs, path.getObjectId(), 0, model));
} else {
throw new CodecException("Object instance TLV is mandatory for multiple instances object [path:%s]", path);
}
} else {
for (Tlv tlv : tlvs) {
if (tlv.getType() != TlvType.OBJECT_INSTANCE)
throw new CodecException("Expected TLV of type OBJECT_INSTANCE but was %s [path:%s]", tlv.getType().name(), path);
instances.add(parseObjectInstanceTlv(tlv.getChildren(), path.getObjectId(), tlv.getIdentifier(), model));
}
}
return (T) new LwM2mObject(path.getObjectId(), instances);
} else // Object instance
if (nodeClass == LwM2mObjectInstance.class) {
if (tlvs.length == 1 && tlvs[0].getType() == TlvType.OBJECT_INSTANCE) {
if (path.isObjectInstance() && tlvs[0].getIdentifier() != path.getObjectInstanceId()) {
throw new CodecException("Id conflict between path [%s] and instance TLV [%d]", path, tlvs[0].getIdentifier());
}
// object instance TLV
return (T) parseObjectInstanceTlv(tlvs[0].getChildren(), path.getObjectId(), tlvs[0].getIdentifier(), model);
} else {
// array of TLV resources
// try to retrieve the instanceId from the path or the model
Integer instanceId = path.getObjectInstanceId();
if (instanceId == null) {
// single instance object?
ObjectModel oModel = model.getObjectModel(path.getObjectId());
if (oModel != null && !oModel.multiple) {
instanceId = 0;
} else {
instanceId = LwM2mObjectInstance.UNDEFINED;
}
}
return (T) parseObjectInstanceTlv(tlvs, path.getObjectId(), instanceId, model);
}
} else // Resource
if (nodeClass == LwM2mResource.class) {
ResourceModel resourceModel = model.getResourceModel(path.getObjectId(), path.getResourceId());
if (tlvs.length == 0 && resourceModel != null && !resourceModel.multiple) {
// else we consider this is a multi-instance resource
throw new CodecException("TLV payload is mandatory for single resource %s", path);
} else if (tlvs.length == 1 && tlvs[0].getType() != TlvType.RESOURCE_INSTANCE) {
if (path.isResource() && path.getResourceId() != tlvs[0].getIdentifier()) {
throw new CodecException("Id conflict between path [%s] and resource TLV [%s]", path, tlvs[0].getIdentifier());
}
return (T) parseResourceTlv(tlvs[0], path.getObjectId(), path.getObjectInstanceId(), model);
} else {
Type expectedRscType = getResourceType(path, model);
return (T) LwM2mMultipleResource.newResource(path.getResourceId(), parseTlvValues(tlvs, expectedRscType, path), expectedRscType);
}
} else {
throw new IllegalArgumentException("invalid node class: " + nodeClass);
}
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class ClientServlet method doPost.
/**
* {@inheritDoc}
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] path = StringUtils.split(req.getPathInfo(), '/');
String clientEndpoint = path[0];
// /clients/endPoint/LWRequest/observe : do LightWeight M2M observe request on a given client.
if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
try {
String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/observe");
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
// get content format
String contentFormatParam = req.getParameter(FORMAT_PARAM);
ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
// create & process request
ObserveRequest request = new ObserveRequest(contentFormat, target);
ObserveResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
// /clients/endPoint/LWRequest : do LightWeight M2M execute request on a given client.
if (path.length == 4) {
try {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
ExecuteRequest request = new ExecuteRequest(target, IOUtils.toString(req.getInputStream()));
ExecuteResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
// /clients/endPoint/LWRequest : do LightWeight M2M create request on a given client.
if (2 <= path.length && path.length <= 3) {
try {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
// get content format
String contentFormatParam = req.getParameter(FORMAT_PARAM);
ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
// create & process request
LwM2mNode node = extractLwM2mNode(target, req);
if (node instanceof LwM2mObjectInstance) {
CreateRequest request = new CreateRequest(contentFormat, target, (LwM2mObjectInstance) node);
CreateResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
throw new IllegalArgumentException("payload must contain an object instance");
}
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeSerializer method serialize.
@Override
public JsonElement serialize(LwM2mNode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject element = new JsonObject();
element.addProperty("id", src.getId());
if (typeOfSrc == LwM2mObject.class) {
element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values()));
} else if (typeOfSrc == LwM2mObjectInstance.class) {
element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
} else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) {
LwM2mResource rsc = (LwM2mResource) src;
if (rsc.isMultiInstances()) {
JsonObject values = new JsonObject();
for (Entry<Integer, ?> entry : rsc.getValues().entrySet()) {
if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
values.add(entry.getKey().toString(), context.serialize(Hex.encodeHex((byte[]) entry.getValue())));
} else {
values.add(entry.getKey().toString(), context.serialize(entry.getValue()));
}
}
element.add("values", values);
} else {
if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) {
element.add("value", context.serialize(Hex.encodeHex((byte[]) rsc.getValue())));
} else {
element.add("value", context.serialize(rsc.getValue()));
}
}
}
return element;
}
Aggregations