Search in sources :

Example 16 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeDecoderTest method tlv_multi_instance_with_obj_link.

@Test
public void tlv_multi_instance_with_obj_link() throws Exception {
    LwM2mObject oObject = ((LwM2mObject) decoder.decode(ENCODED_OBJ66, ContentFormat.TLV, new LwM2mPath(66), model));
    assertObj66Instance(oObject);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 17 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject 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 18 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject 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)

Example 19 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class ObserveTest method can_observe_timestamped_object.

@Test
public void can_observe_timestamped_object() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // an observation response should have been sent
    Observation observation = observeResponse.getObservation();
    assertEquals("/3", observation.getPath().toString());
    assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
    Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
    assertTrue("We should have only on observation", observations.size() == 1);
    assertTrue("New observation is not there", observations.contains(observation));
    // *** HACK send time-stamped notification as Leshan client does not support it *** //
    // create time-stamped nodes
    TimestampedLwM2mNode mostRecentNode = new TimestampedLwM2mNode(System.currentTimeMillis(), new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Paris"))));
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    timestampedNodes.add(mostRecentNode);
    timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2, new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
    byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
    Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
    sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
    // *** Hack End *** //
    // verify result
    listener.waitForNotification(2000);
    assertTrue(listener.receivedNotify().get());
    assertEquals(mostRecentNode.getNode(), listener.getResponse().getContent());
    assertEquals(timestampedNodes, listener.getResponse().getTimestampedLwM2mNode());
    assertNotNull(listener.getResponse().getCoapResponse());
    assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : ArrayList(java.util.ArrayList) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mValueConverter(org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 20 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeDecoderTest method tlv_server_object_multi_instance_with_only_1_instance.

@Test
public void tlv_server_object_multi_instance_with_only_1_instance() throws Exception {
    LwM2mObject oObject = ((LwM2mObject) decoder.decode(ENCODED_SERVER, ContentFormat.TLV, new LwM2mPath(1), model));
    assertServerInstance(oObject);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Aggregations

LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)21 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)5 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)4 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 Tlv (org.eclipse.leshan.tlv.Tlv)3 JsonObject (com.eclipsesource.json.JsonObject)2 Collection (java.util.Collection)2 Entry (java.util.Map.Entry)2 Type (org.eclipse.leshan.core.model.ResourceModel.Type)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 Observation (org.eclipse.leshan.core.observation.Observation)2 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)2