use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeDecoderTest method assertServerInstance.
private void assertServerInstance(LwM2mObject oObject) {
assertEquals(1, oObject.getId());
assertEquals(1, oObject.getInstances().size());
LwM2mObjectInstance oInstance0 = oObject.getInstance(0);
assertEquals(1L, oInstance0.getResource(0).getValue());
assertEquals(86400L, oInstance0.getResource(1).getValue());
assertEquals(true, oInstance0.getResource(6).getValue());
assertEquals("U", oInstance0.getResource(7).getValue());
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class LwM2mNodeDecoderTest method json_custom_object_instance.
@Test
public void json_custom_object_instance() throws CodecException {
// json content for instance 0 of device object
StringBuilder b = new StringBuilder();
b.append("{\"e\":[");
b.append("{\"n\":\"0\",\"sv\":\"a string\"},");
b.append("{\"n\":\"1\",\"v\":10.5},");
b.append("{\"n\":\"2\",\"bv\":true}]}");
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(), ContentFormat.JSON, new LwM2mPath(1024, 0), model);
assertEquals(0, oInstance.getId());
assertEquals("a string", oInstance.getResource(0).getValue());
assertEquals(10.5, oInstance.getResource(1).getValue());
assertEquals(true, oInstance.getResource(2).getValue());
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class ObjectEnabler method doWrite.
@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
LwM2mPath path = request.getPath();
// Manage Object case
if (path.isObject()) {
for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), instanceNode));
} else {
doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
}
}
return BootstrapWriteResponse.success();
}
// Manage Instance case
if (path.isObjectInstance()) {
LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), instanceNode));
} else {
doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
}
return BootstrapWriteResponse.success();
}
// Manage resource case
LwM2mResource resource = (LwM2mResource) request.getNode();
LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
if (instanceEnabler == null) {
doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
} else {
instanceEnabler.write(path.getResourceId(), resource);
}
return BootstrapWriteResponse.success();
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class ObjectEnabler method doRead.
@Override
protected ReadResponse doRead(ServerIdentity identity, ReadRequest request) {
LwM2mPath path = request.getPath();
// Manage Object case
if (path.isObject()) {
List<LwM2mObjectInstance> lwM2mObjectInstances = new ArrayList<>();
for (Entry<Integer, LwM2mInstanceEnabler> entry : instances.entrySet()) {
lwM2mObjectInstances.add(getLwM2mObjectInstance(entry.getKey(), entry.getValue(), identity, false));
}
return ReadResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances));
}
// Manage Instance case
LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
if (instance == null)
return ReadResponse.notFound();
if (path.getResourceId() == null) {
return ReadResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, false));
}
// Manage Resource case
return instance.read(path.getResourceId());
}
use of org.eclipse.leshan.core.node.LwM2mObjectInstance in project leshan by eclipse.
the class SecurityObjectPskStore method getKey.
@Override
public byte[] getKey(String identity) {
if (identity == null)
return null;
byte[] res = null;
LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
for (LwM2mObjectInstance security : securities.getInstances().values()) {
long securityMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
if (// psk
securityMode == SecurityMode.PSK.code) {
byte[] pskIdentity = (byte[]) security.getResource(SEC_PUBKEY_IDENTITY).getValue();
if (Arrays.equals(identity.getBytes(), pskIdentity)) {
if (res == null) {
// we continue to check if the is duplication
res = (byte[]) security.getResource(SEC_SECRET_KEY).getValue();
} else {
LOG.warn("There is several security object instance with the same psk identity : '{}'", identity);
// we find 1 duplication and warn for it no need to continue.
return res;
}
}
}
}
return res;
}
Aggregations