use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method write_boolean_resource.
private void write_boolean_resource(ContentFormat format) throws InterruptedException {
// write resource
boolean expectedvalue = true;
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, BOOLEAN_RESOURCE_ID, expectedvalue));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read resource to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, BOOLEAN_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class WriteTest method write_integer_resource.
private void write_integer_resource(ContentFormat format) throws InterruptedException {
// write resource
long expectedvalue = 999l;
WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, INTEGER_RESOURCE_ID, expectedvalue));
// verify result
assertEquals(ResponseCode.CHANGED, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// read resource to check the value changed
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, INTEGER_RESOURCE_ID));
LwM2mResource resource = (LwM2mResource) readResponse.getContent();
assertEquals(expectedvalue, resource.getValue());
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class DownlinkRequestSerDes method deserialize.
public static DownlinkRequest<?> deserialize(JsonObject o) {
String kind = o.getString("kind", null);
String path = o.getString("path", null);
switch(kind) {
case "observe":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ObserveRequest(ContentFormat.fromCode(format), path);
}
case "delete":
return new DeleteRequest(path);
case "discover":
return new DiscoverRequest(path);
case "create":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
Collection<LwM2mResource> resources = new ArrayList<>();
JsonArray jResources = (JsonArray) o.get("resources");
for (JsonValue jResource : jResources) {
LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
resources.add(resource);
}
return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
}
case "execute":
String parameters = o.getString("parameters", null);
return new ExecuteRequest(path, parameters);
case "writeAttributes":
{
String observeSpec = o.getString("observeSpec", null);
return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
}
case "write":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
}
case "read":
{
int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
return new ReadRequest(ContentFormat.fromCode(format), path);
}
default:
throw new IllegalStateException("Invalid request missing kind attribute");
}
}
use of org.eclipse.leshan.core.request.ReadRequest 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;
}
use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.
the class CreateTest method cannot_create_instance_with_extraneous_resources.
// TODO we must probably implement this.
@Ignore
@Test
public void cannot_create_instance_with_extraneous_resources() throws InterruptedException {
// create ACL instance
LwM2mObjectInstance instance = new LwM2mObjectInstance(0, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123), LwM2mSingleResource.newIntegerResource(50, 123)));
CreateRequest request = new CreateRequest(2, instance);
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), request);
// verify result
assertEquals(ResponseCode.BAD_REQUEST, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// try to read to check if the instance is not created
// client registration
ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(2, 0));
assertEquals(ResponseCode.NOT_FOUND, readResponse.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Aggregations