@Override public void visit(LwM2mObject object) { LOG.trace("Encoding Object {} into JSON", object); // Validate request path if (!requestPath.isObject()) { throw new CodecException("Invalid request path %s for JSON object encoding", requestPath); } // Create resources resourceList = new ArrayList<>(); for (LwM2mObjectInstance instance : object.getInstances().values()) { for (LwM2mResource resource : instance.getResources().values()) { String prefixPath = Integer.toString(instance.getId()) + "/" + Integer.toString(resource.getId()); resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } } }
instances[i] = context.deserialize(array.get(i), LwM2mNode.class); node = new LwM2mObject(id, instances);
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding object {} into TLV", object); Tlv[] tlvs; // encoded as an array of instances tlvs = new Tlv[object.getInstances().size()]; int i = 0; for (Entry<Integer, LwM2mObjectInstance> instance : object.getInstances().entrySet()) { Tlv[] resources = encodeResources(instance.getValue().getResources().values(), new LwM2mPath(object.getId(), instance.getKey())); tlvs[i] = new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getKey()); i++; } try { out.write(TlvEncoder.encode(tlvs).array()); } catch (IOException e) { throw new RuntimeException(e); } }
element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values())); } else if (typeOfSrc == LwM2mObjectInstance.class) { element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values()));
node = new LwM2mObject(baseName.getObjectId(), instances); } else if (nodeClass == LwM2mObjectInstance.class) {
@Override public String getIdentity(InetSocketAddress inetAddress) { if (inetAddress == null) return 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 (securityMode == SecurityMode.PSK.code) { try { URI uri = new URI((String) security.getResource(SEC_SERVER_URI).getValue()); if (inetAddress.equals(ServerInfo.getAddress(uri))) { byte[] pskIdentity = (byte[]) security.getResource(SEC_PUBKEY_IDENTITY).getValue(); return new String(pskIdentity); } } catch (URISyntaxException e) { LOG.error(String.format("Invalid URI %s", (String) security.getResource(SEC_SERVER_URI).getValue()), e); } } } return null; }
@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()); }
@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 (securityMode == SecurityMode.PSK.code) // psk { 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; }
@Override protected ObserveResponse doObserve(final ServerIdentity identity, final ObserveRequest request) { final 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, true)); } return ObserveResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances)); } // Manage Instance case final LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); if (instance == null) return ObserveResponse.notFound(); if (path.getResourceId() == null) { return ObserveResponse .success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, true)); } // Manage Resource case return instance.observe(path.getResourceId()); }
o.add("kind", "object"); JsonObject instances = Json.object(); for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) { instances.add(String.valueOf(instance.getId()), jSerialize(instance));
instances.add(instance); return new LwM2mObject(id, instances);
o.add("kind", "object"); JsonObject instances = Json.object(); for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) { instances.add(String.valueOf(instance.getId()), jSerialize(instance));
instances.add(instance); return new LwM2mObject(id, instances);
LwM2mObject servers = (LwM2mObject) serverEnabler.read(SYSTEM, new ReadRequest(SERVER)).getContent(); for (LwM2mObjectInstance security : securities.getInstances().values()) { try { if ((boolean) security.getResource(SEC_BOOTSTRAP).getValue()) { for (LwM2mObjectInstance server : servers.getInstances().values()) { if (info.serverId == (Long) server.getResource(SRV_SERVER_ID).getValue()) { info.lifetime = (long) server.getResource(SRV_LIFETIME).getValue();
return (T) new LwM2mObject(path.getObjectId(), instances.values());
for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) { LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId()); if (instanceEnabler == null) {