Search in sources :

Example 26 with LwM2mObjectInstance

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance)

Example 27 with LwM2mObjectInstance

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Example 28 with LwM2mObjectInstance

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();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 29 with LwM2mObjectInstance

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject)

Example 30 with LwM2mObjectInstance

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;
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Aggregations

LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)49 Test (org.junit.Test)30 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)9 ArrayList (java.util.ArrayList)8 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)8 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)4 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)4 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)4 JsonObject (com.eclipsesource.json.JsonObject)3 Collection (java.util.Collection)3