Search in sources :

Example 1 with ObjectLink

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

the class TlvDecoder method decodeObjlnk.

/**
 * Decodes a byte array into a objlnk value.
 */
public static ObjectLink decodeObjlnk(byte[] value) throws TlvException {
    ByteBuffer bff = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    bff.put(value);
    int val1 = bff.getShort(0) & 0xFFFF;
    int val2 = bff.getShort(2) & 0xFFFF;
    return new ObjectLink(val1, val2);
}
Also used : ByteBuffer(java.nio.ByteBuffer) ObjectLink(org.eclipse.leshan.core.node.ObjectLink)

Example 2 with ObjectLink

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

the class WriteTest method can_write_single_instance_objlnk_resource_in_text.

@Test
public void can_write_single_instance_objlnk_resource_in_text() throws InterruptedException {
    // Write objlnk resource in TEXT format
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(ContentFormat.TEXT, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_SINGLE_INSTANCE_RESOURCE_ID, new ObjectLink(10245, 0)));
    // Verify Write result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // Reading back the written OBJLNK value
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(ContentFormat.TEXT, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_SINGLE_INSTANCE_RESOURCE_ID));
    LwM2mSingleResource resource = (LwM2mSingleResource) readResponse.getContent();
    // verify read value
    assertEquals(((ObjectLink) resource.getValue()).getObjectId(), 10245);
    assertEquals(((ObjectLink) resource.getValue()).getObjectInstanceId(), 0);
}
Also used : LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 3 with ObjectLink

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

the class WriteTest method can_write_multi_instance_objlnk_resource_in_tlv.

@Test
public void can_write_multi_instance_objlnk_resource_in_tlv() throws InterruptedException {
    Map<Integer, ObjectLink> neighbourCellReport = new HashMap<>();
    neighbourCellReport.put(0, new ObjectLink(10245, 1));
    neighbourCellReport.put(1, new ObjectLink(10242, 2));
    neighbourCellReport.put(2, new ObjectLink(10244, 3));
    // Write objlnk resource in TLV format
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(ContentFormat.TLV, IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_MULTI_INSTANCE_RESOURCE_ID, neighbourCellReport, Type.OBJLNK));
    // Verify Write result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // Reading back the written OBJLNK value
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(IntegrationTestHelper.TEST_OBJECT_ID, 0, IntegrationTestHelper.OBJLNK_MULTI_INSTANCE_RESOURCE_ID));
    LwM2mMultipleResource resource = (LwM2mMultipleResource) readResponse.getContent();
    // verify read value
    assertEquals(((ObjectLink) resource.getValue(0)).getObjectId(), 10245);
    assertEquals(((ObjectLink) resource.getValue(0)).getObjectInstanceId(), 1);
    assertEquals(((ObjectLink) resource.getValue(1)).getObjectId(), 10242);
    assertEquals(((ObjectLink) resource.getValue(1)).getObjectInstanceId(), 2);
    assertEquals(((ObjectLink) resource.getValue(2)).getObjectId(), 10244);
    assertEquals(((ObjectLink) resource.getValue(2)).getObjectInstanceId(), 3);
}
Also used : LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) HashMap(java.util.HashMap) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 4 with ObjectLink

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

the class TlvDecoderTest method decode_object_link.

@Test
public void decode_object_link() throws TlvException {
    String dataStr = "12345678";
    byte[] bytes = Hex.decodeHex(dataStr.toCharArray());
    ObjectLink objlnk = TlvDecoder.decodeObjlnk(bytes);
    assertEquals(0x1234, objlnk.getObjectId());
    assertEquals(0x5678, objlnk.getObjectInstanceId());
    dataStr = "ffffffff";
    bytes = Hex.decodeHex(dataStr.toCharArray());
    objlnk = TlvDecoder.decodeObjlnk(bytes);
    assertEquals(0xffff, objlnk.getObjectId());
    assertEquals(0xffff, objlnk.getObjectInstanceId());
}
Also used : ObjectLink(org.eclipse.leshan.core.node.ObjectLink) Test(org.junit.Test)

Example 5 with ObjectLink

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

the class LwM2mNodeDecoderTest method tlv_invalid_multi_resource_2_instance_with_the_same_id.

@Test(expected = CodecException.class)
public void tlv_invalid_multi_resource_2_instance_with_the_same_id() {
    Tlv resInstance1 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(100, 1)), 0);
    Tlv resInstance2 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(101, 2)), 0);
    Tlv multiResource = new Tlv(TlvType.MULTIPLE_RESOURCE, new Tlv[] { resInstance1, resInstance2 }, null, 22);
    byte[] content = TlvEncoder.encode(new Tlv[] { multiResource }).array();
    decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 22), model);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ObjectLink(org.eclipse.leshan.core.node.ObjectLink) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Aggregations

ObjectLink (org.eclipse.leshan.core.node.ObjectLink)8 Test (org.junit.Test)5 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)3 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)3 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 LwM2mMultipleResource (org.eclipse.leshan.core.node.LwM2mMultipleResource)1 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 Tlv (org.eclipse.leshan.tlv.Tlv)1