Skip to content

Commit

Permalink
GH-1451: Support Negative value for GT and LT attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 8, 2023
1 parent 16f69b1 commit 953687f
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ public DoubleAttributeModel(String coRELinkParam, Attachment attachment, Set<Ass

/**
* <pre>
* 1*DIGIT ["." 1*DIGIT]
* ["-"] 1*DIGIT ["." 1*DIGIT]
* </pre>
*/
@Override
public <E extends Throwable> LwM2mAttribute<Double> consumeAttributeValue(StringParser<E> parser) throws E {
// parse Value
int start = parser.getPosition();
if (parser.nextCharIs('-')) {
parser.consumeNextChar();
}
parser.consumeDIGIT();
while (parser.nextCharIsDIGIT()) {
parser.consumeNextChar();
Expand All @@ -60,7 +63,7 @@ public <E extends Throwable> LwM2mAttribute<Double> consumeAttributeValue(String
parser.getStringToParse());
return null;
} catch (IllegalArgumentException e) {
parser.raiseException(e, "%s value '%s' is not a valid in %s", getName(), strValue,
parser.raiseException(e, "%s value '%s' is not a valid Double in %s", getName(), strValue,
parser.getStringToParse());
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ public LongAttributeModel(String coRELinkParam, Attachment attachment, Set<Assig

/**
* <pre>
* 1 * DIGIT
* ["-"] 1 * DIGIT
* </pre>
*/
@Override
public <E extends Throwable> LwM2mAttribute<Long> consumeAttributeValue(StringParser<E> parser) throws E {
// parse Value
int start = parser.getPosition();
if (parser.nextCharIs('-')) {
parser.consumeNextChar();
}
parser.consumeDIGIT();
while (parser.nextCharIsDIGIT()) {
parser.consumeNextChar();
Expand All @@ -53,7 +56,7 @@ public <E extends Throwable> LwM2mAttribute<Long> consumeAttributeValue(StringPa
parser.getStringToParse());
return null;
} catch (IllegalArgumentException e) {
parser.raiseException(e, "%s value '%s' is not a valid in %s", getName(), strValue,
parser.raiseException(e, "%s value '%s' is not a valid long in %s", getName(), strValue,
parser.getStringToParse());
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.EnumSet;
import java.util.Map;

import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
import org.eclipse.leshan.core.LwM2m.Version;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.link.attributes.InvalidAttributeException;
import org.eclipse.leshan.core.model.LwM2mModel;
Expand All @@ -29,7 +31,7 @@
public final class LwM2mAttributes {

// dim
public static final LongAttributeModel DIMENSION = new LongAttributeModel(//
public static final LwM2mAttributeModel<Long> DIMENSION = new PositiveLongAttributeModel(//
"dim", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE), //
Expand Down Expand Up @@ -60,7 +62,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// ssid
public static final LongAttributeModel SHORT_SERVER_ID = new LongAttributeModel(//
public static final LwM2mAttributeModel<Long> SHORT_SERVER_ID = new PositiveLongAttributeModel(//
"ssid", //
Attachment.OBJECT_INSTANCE, //
EnumSet.of(AssignationLevel.OBJECT_INSTANCE), //
Expand Down Expand Up @@ -95,7 +97,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// uri
public static final StringAttributeModel SERVER_URI = new StringAttributeModel(//
public static final LwM2mAttributeModel<String> SERVER_URI = new StringAttributeModel(//
"uri", //
Attachment.OBJECT_INSTANCE, //
EnumSet.of(AssignationLevel.OBJECT_INSTANCE), //
Expand All @@ -116,11 +118,13 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// ver
public static final ObjectVersionAttributeModel OBJECT_VERSION = new ObjectVersionAttributeModel();
public static final LwM2mAttributeModel<Version> OBJECT_VERSION = new ObjectVersionAttributeModel();
// lwm2m
public static final LwM2mVersionAttributeModel ENABLER_VERSION = new LwM2mVersionAttributeModel();
public static final LwM2mAttributeModel<LwM2mVersion> ENABLER_VERSION = new LwM2mVersionAttributeModel();
// pmin
public static final LwM2mAttributeModel<Long> MINIMUM_PERIOD = new LongAttributeModel(//
// TODO : wait for confirmation before to move to PositiveDouble
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> MINIMUM_PERIOD = new PositiveLongAttributeModel(//
"pmin", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE), //
Expand All @@ -146,7 +150,9 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// pmax
public static final LongAttributeModel MAXIMUM_PERIOD = new LongAttributeModel( //
// TODO : wait for confirmation before to move to PositiveDouble
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> MAXIMUM_PERIOD = new PositiveLongAttributeModel( //
"pmax", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE), //
Expand All @@ -172,6 +178,8 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// gt
// LWM2M v1.1.1 doesn't allow negative value but this is a bug in the specification
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Double> GREATER_THAN = new DoubleAttributeModel(//
"gt", //
Attachment.RESOURCE, //
Expand Down Expand Up @@ -203,6 +211,8 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// lt
// LWM2M v1.1.1 doesn't allow negative value but this is a bug in the specification
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Double> LESSER_THAN = new DoubleAttributeModel( //
"lt", //
Attachment.RESOURCE, //
Expand Down Expand Up @@ -234,7 +244,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// st
public static final LwM2mAttributeModel<Double> STEP = new DoubleAttributeModel(//
public static final LwM2mAttributeModel<Double> STEP = new PositiveDoubleAttributeModel(//
"st", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE), //
Expand Down Expand Up @@ -265,7 +275,9 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// epmin
public static final LwM2mAttributeModel<Long> EVALUATE_MINIMUM_PERIOD = new LongAttributeModel(//
// TODO : wait for confirmation before to move to PositiveDouble
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> EVALUATE_MINIMUM_PERIOD = new PositiveLongAttributeModel(//
"epmin", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE), //
Expand All @@ -291,7 +303,9 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
};
};
// epmax
public static final LwM2mAttributeModel<Long> EVALUATE_MAXIMUM_PERIOD = new LongAttributeModel( //
// TODO : wait for confirmation before to move to PositiveDouble
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> EVALUATE_MAXIMUM_PERIOD = new PositiveLongAttributeModel( //
"epmax", //
Attachment.RESOURCE,
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE), //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2022 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.link.lwm2m.attributes;

import java.util.Set;

import org.eclipse.leshan.core.parser.StringParser;

/**
* A Generic Attribute of type Double (for positive value only).
*/
public class PositiveDoubleAttributeModel extends LwM2mAttributeModel<Double> {

public PositiveDoubleAttributeModel(String coRELinkParam, Attachment attachment,
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
}

@Override
public String getInvalidValueCause(Double value) {
if (value < 0) {
return String.format("'%s' attribute value must not be negative", getName());
}
return null;
}

/**
* <pre>
* 1*DIGIT ["." 1*DIGIT]
* </pre>
*/
@Override
public <E extends Throwable> LwM2mAttribute<Double> consumeAttributeValue(StringParser<E> parser) throws E {
// parse Value
int start = parser.getPosition();
parser.consumeDIGIT();
while (parser.nextCharIsDIGIT()) {
parser.consumeNextChar();
}
if (parser.nextCharIs('.')) {
parser.consumeNextChar();
parser.consumeDIGIT();
while (parser.nextCharIsDIGIT()) {
parser.consumeNextChar();
}
}
int end = parser.getPosition();

// create attribute
String strValue = parser.substring(start, end);
try {
return new LwM2mAttribute<Double>(this, Double.parseDouble(strValue));
} catch (NumberFormatException e) {
parser.raiseException(e, "%s value '%s' is not a valid positive double in %s", getName(), strValue,
parser.getStringToParse());
return null;
} catch (IllegalArgumentException e) {
parser.raiseException(e, "%s value '%s' is not a valid positive double in %s", getName(), strValue,
parser.getStringToParse());
return null;
}
}

@Override
public LwM2mAttribute<Double> createEmptyAttribute() {
return new LwM2mAttribute<Double>(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 2022 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.link.lwm2m.attributes;

import java.util.Set;

import org.eclipse.leshan.core.parser.StringParser;

/**
* A Generic Attribute of type Long (for positive value only).
*/
public class PositiveLongAttributeModel extends LwM2mAttributeModel<Long> {

public PositiveLongAttributeModel(String coRELinkParam, Attachment attachment,
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
}

@Override
public String getInvalidValueCause(Long value) {
if (value < 0) {
return String.format("'%s' attribute value must not be negative", getName());
}
return null;
}

/**
* <pre>
* 1 * DIGIT
* </pre>
*/
@Override
public <E extends Throwable> LwM2mAttribute<Long> consumeAttributeValue(StringParser<E> parser) throws E {
// parse Value
int start = parser.getPosition();
parser.consumeDIGIT();
while (parser.nextCharIsDIGIT()) {
parser.consumeNextChar();
}
int end = parser.getPosition();

// create attribute
String strValue = parser.substring(start, end);
try {
return new LwM2mAttribute<Long>(this, Long.parseLong(strValue));
} catch (NumberFormatException e) {
parser.raiseException(e, "%s value '%s' is not a valid positive long in %s", getName(), strValue,
parser.getStringToParse());
return null;
} catch (IllegalArgumentException e) {
parser.raiseException(e, "%s value '%s' is not a value positive long in %s", getName(), strValue,
parser.getStringToParse());
return null;
}
}

@Override
public LwM2mAttribute<Long> createEmptyAttribute() {
return new LwM2mAttribute<Long>(this);
}
}

0 comments on commit 953687f

Please sign in to comment.