Skip to content

Commit

Permalink
add support of OPAQUE format
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 11, 2015
1 parent aa02658 commit c8dd5c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public static LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath p
if (rDesc != null && rDesc.multiple) {
format = ContentFormat.TLV;
} else {
format = ContentFormat.TEXT;
if (rDesc.type == Type.OPAQUE) {
format = ContentFormat.OPAQUE;
} else {
format = ContentFormat.TEXT;
}
}
} else {
// HACK: client should return a content type
Expand Down Expand Up @@ -95,9 +99,17 @@ public static LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath p
} catch (TlvException e) {
throw new InvalidValueException("Unable to decode tlv.", path, e);
}
case OPAQUE:
// single resource value
Validate.notNull(path.getResourceId());
ResourceModel desc = model.getResourceModel(path.getObjectId(), path.getResourceId());
if (desc != null && desc.type != Type.OPAQUE) {
throw new InvalidValueException(
"Invalid content format, OPAQUE can only be used for single OPAQUE resource", path);
}
return new LwM2mResource(path.getResourceId(), Value.newBinaryValue(content));
case JSON:
case LINK:
case OPAQUE:
throw new InvalidValueException("Content format " + format + " not yet implemented", path);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public static byte[] encode(LwM2mNode node, ContentFormat format, LwM2mPath path
node.accept(textEncoder);
encoded = textEncoder.encoded;
break;
case OPAQUE:
NodeOpaqueEncoder opaqueEncoder = new NodeOpaqueEncoder();
opaqueEncoder.objectId = path.getObjectId();
opaqueEncoder.model = model;
node.accept(opaqueEncoder);
encoded = opaqueEncoder.encoded;
break;
case JSON:
throw new IllegalArgumentException("JSON content format not supported");
default:
Expand Down Expand Up @@ -213,6 +220,38 @@ public void visit(LwM2mResource resource) {
}
}

private static class NodeOpaqueEncoder implements LwM2mNodeVisitor {

int objectId;
LwM2mModel model;

byte[] encoded = null;

@Override
public void visit(LwM2mObject object) {
throw new IllegalArgumentException("Object cannot be encoded in opaque format");
}

@Override
public void visit(LwM2mObjectInstance instance) {
throw new IllegalArgumentException("Object instance cannot be encoded in opaque format");
}

@Override
public void visit(LwM2mResource resource) {
if (resource.isMultiInstances()) {
throw new IllegalArgumentException("Mulitple instances resource cannot be encoded in opaque format");
}
ResourceModel rSpec = model.getResourceModel(objectId, resource.getId());
if (rSpec != null && rSpec.type != Type.OPAQUE) {
throw new IllegalArgumentException("Only single opaque resource can be encoded in opaque format");
}
LOG.trace("Encoding resource {} into text", resource);
Value<?> val = convertValue(resource.getValue(), Type.OPAQUE);
encoded = (byte[]) val.value;
}
}

private static Value<?> convertValue(Value<?> value, Type expectedType) {
if (expectedType == null) {
// unknown resource, trusted value
Expand Down

0 comments on commit c8dd5c8

Please sign in to comment.