Skip to content

Commit

Permalink
GH-1434: Use LWM2M v1.1 default object version way at server side.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 14, 2023
1 parent 332d487 commit cd22391
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
5 changes: 5 additions & 0 deletions leshan-core/src/main/java/org/eclipse/leshan/core/LwM2m.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public class Version implements Comparable<Version> {
protected final Short major;
protected final Short minor;

public Version(int major, int minor) {
this.major = (short) major;
this.minor = (short) minor;
}

public Version(Short major, Short minor) {
this.major = major;
this.minor = minor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* Copyright (c) 2023 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.model;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
import org.eclipse.leshan.core.LwM2m.Version;
import org.eclipse.leshan.core.LwM2mId;

/**
* A registry which contains all version of LWM2M object defined in core LWM2M specification.
* <p>
* When there is inconsistency between 2 patch version of the specification. (e.g. LWM2M v1.1 and v1.1.1 does not
* contains same version of CONNECTIVITY_MONITORING object), then we use the last backward compatible one.
* <p>
* See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/561
*/
public class LwM2mCoreObjectVersionRegistry {

protected Map<LwM2mVersion, Map<Integer, Version>> lwm2mVerionToObjectRegistry;

public LwM2mCoreObjectVersionRegistry() {
lwm2mVerionToObjectRegistry = new HashMap<>();

// Add version of core object from LWM2M v1.0
{
Map<Integer, Version> objectIdToVersion = new HashMap<>();
lwm2mVerionToObjectRegistry.put(LwM2mVersion.V1_0, objectIdToVersion);

objectIdToVersion.put(LwM2mId.SECURITY, new Version(1, 0));
objectIdToVersion.put(LwM2mId.SERVER, new Version(1, 0));
objectIdToVersion.put(LwM2mId.ACCESS_CONTROL, new Version(1, 0));
objectIdToVersion.put(LwM2mId.DEVICE, new Version(1, 0));
objectIdToVersion.put(LwM2mId.CONNECTIVITY_MONITORING, new Version(1, 0));
objectIdToVersion.put(LwM2mId.FIRMWARE, new Version(1, 0));
objectIdToVersion.put(LwM2mId.LOCATION, new Version(1, 0));
objectIdToVersion.put(LwM2mId.CONNECTIVITY_STATISTICS, new Version(1, 0));
}

// Add version of core object from LWM2M v1.1
{
Map<Integer, Version> objectIdToVersion = new HashMap<>();
lwm2mVerionToObjectRegistry.put(LwM2mVersion.V1_1, objectIdToVersion);

// Since LWM2M v1.0
objectIdToVersion.put(LwM2mId.SECURITY, new Version(1, 1));
objectIdToVersion.put(LwM2mId.SERVER, new Version(1, 1));
objectIdToVersion.put(LwM2mId.ACCESS_CONTROL, new Version(1, 0));
objectIdToVersion.put(LwM2mId.DEVICE, //
// new Version(1, 0)); ---> in lwm2m v1.1
new Version(1, 1)); // ---> in lwm2m v1.1.1
objectIdToVersion.put(LwM2mId.CONNECTIVITY_MONITORING, //
// new Version(1, 1)); ---> in lwm2m v1.1
new Version(1, 2)); // ---> in lwm2m v1.1.1
objectIdToVersion.put(LwM2mId.FIRMWARE, new Version(1, 0));
objectIdToVersion.put(LwM2mId.LOCATION, new Version(1, 0));
objectIdToVersion.put(LwM2mId.CONNECTIVITY_STATISTICS, new Version(1, 0));

// Added in v1.1
objectIdToVersion.put(LwM2mId.OSCORE, new Version(1, 0));
}
}

public Version getDefaultVersion(int objectId, LwM2mVersion lwM2mVersion) {
Map<Integer, Version> objectIdToVersion = lwm2mVerionToObjectRegistry.get(lwM2mVersion);
if (objectIdToVersion != null) {
return objectIdToVersion.get(objectId);
}
return null;
}

public boolean isDefaultVersion(Version objectVersion, int objectId, LwM2mVersion lwM2mVersion) {
Map<Integer, Version> objectIdToVersion = lwm2mVerionToObjectRegistry.get(lwM2mVersion);
if (objectIdToVersion != null) {
Version registeredVersion = objectIdToVersion.get(objectId);
if (registeredVersion == null)
return false;
else
return registeredVersion.equals(objectVersion);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import org.eclipse.leshan.core.link.lwm2m.MixedLwM2mLink;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttribute;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributes;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.LwM2mCoreObjectVersionRegistry;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.request.ContentFormat;

public class DefaultRegistrationDataExtractor implements RegistrationDataExtractor {

protected LwM2mCoreObjectVersionRegistry versionRegistry = new LwM2mCoreObjectVersionRegistry();

@Override
public RegistrationData extractDataFromObjectLinks(Link[] objectLinks, LwM2mVersion lwM2mVersion) {
RegistrationData data = new RegistrationData();
Expand Down Expand Up @@ -150,6 +152,14 @@ protected void addSupportedObject(LwM2mVersion lwM2mVersion, Link link, LwM2mPat
}

protected Version getDefaultVersion(LwM2mVersion lwM2mVersion, int objectId) {
return new Version(ObjectModel.DEFAULT_VERSION);
// Implements : https://github.com/eclipse/leshan/issues/1434 behavior

Version defaultVersion = versionRegistry.getDefaultVersion(objectId, lwM2mVersion);
if (defaultVersion != null) {
return defaultVersion;
} else {
// this is not a core object, use v1.0
return Version.V1_0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,37 @@ public void test_object_links_with_text_in_lwm2m_path() throws LinkParseExceptio
});
}

@Test
public void test_object_links_with_version_for_lwm2m_v1_1() throws LinkParseException {
Registration reg = given_a_registration_with_object_link_like("</1/0>,</2>,</3>,</3/0>,</4>,</5>,</6>,</7>",
LwM2mVersion.V1_1);

// check root path
assertEquals("/", reg.getRootPath());

// Ensure supported objects are correct
Map<Integer, Version> supportedObject = reg.getSupportedObject();
assertEquals(7, supportedObject.size());
assertEquals(new Version("1.1"), supportedObject.get(1));
assertEquals(new Version("1.0"), supportedObject.get(2));
assertEquals(new Version("1.1"), supportedObject.get(3));
assertEquals(new Version("1.2"), supportedObject.get(4));
assertEquals(new Version("1.0"), supportedObject.get(5));
assertEquals(new Version("1.0"), supportedObject.get(6));
assertEquals(new Version("1.0"), supportedObject.get(7));

// ensure available instances are correct
Set<LwM2mPath> availableInstances = reg.getAvailableInstances();
assertEquals(2, availableInstances.size());
assertTrue(availableInstances.containsAll(Arrays.asList(new LwM2mPath(1, 0), new LwM2mPath(3, 0))));
}

private Registration given_a_registration_with_object_link_like(String objectLinks) throws LinkParseException {
return given_a_registration_with_object_link_like(objectLinks, LwM2mVersion.V1_0);
}

private Registration given_a_registration_with_object_link_like(String objectLinks, LwM2mVersion version)
throws LinkParseException {
Builder builder = new Registration.Builder("id", "endpoint",
Identity.unsecure(InetSocketAddress.createUnresolved("localhost", 0)),
EndpointUriUtil.createUri("coap://localhost:5683"));
Expand All @@ -255,7 +285,7 @@ private Registration given_a_registration_with_object_link_like(String objectLin
builder.objectLinks(links);

RegistrationData dataFromObjectLinks = new DefaultRegistrationDataExtractor().extractDataFromObjectLinks(links,
LwM2mVersion.V1_0);
version);
builder.rootPath(dataFromObjectLinks.getAlternatePath());
builder.supportedContentFormats(dataFromObjectLinks.getSupportedContentFormats());
builder.supportedObjects(dataFromObjectLinks.getSupportedObjects());
Expand Down

0 comments on commit cd22391

Please sign in to comment.