private Tlv encodeResource(LwM2mResource resource, LwM2mPath resourcePath) {
ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId());
Type expectedType = rSpec != null ? rSpec.type : resource.getType();
Tlv rTlv;
if (resource.isMultiInstances()) {
Tlv[] instances = new Tlv[resource.getValues().size()];
int i = 0;
for (Entry<Integer, ?> entry : resource.getValues().entrySet()) {
LwM2mPath resourceInstancePath = resourcePath.append(entry.getKey());
Object convertedValue = converter.convertValue(entry.getValue(), resource.getType(), expectedType,
resourceInstancePath);
instances[i] = new Tlv(TlvType.RESOURCE_INSTANCE, null,
this.encodeTlvValue(convertedValue, expectedType, resourceInstancePath), entry.getKey());
i++;
}
rTlv = new Tlv(TlvType.MULTIPLE_RESOURCE, instances, null, resource.getId());
} else {
Object convertedValue = converter.convertValue(resource.getValue(), resource.getType(), expectedType,
resourcePath);
rTlv = new Tlv(TlvType.RESOURCE_VALUE, null,
this.encodeTlvValue(convertedValue, expectedType, resourcePath), resource.getId());
}
return rTlv;
}