Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
#4 Parsed ErrorCodes
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Apr 8, 2021
1 parent e8582ae commit e5680e6
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/main/java/no/entra/bacnet/error/ErrorClassType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package no.entra.bacnet.error;

import no.entra.bacnet.octet.Octet;

import static java.lang.Integer.parseInt;

public enum ErrorClassType {
device(0),
object(1),
property(2),
resources(3),
security(4),
services(5),
vt(6),
communication(7);
private int ErrorClassTypeInt;

public static ErrorClassType fromErrorClassTypeInt(int ErrorClassTypeInt) {
for (ErrorClassType type : values()) {
if (type.getErrorClassTypeInt() == ErrorClassTypeInt) {
return type;
}
}
return null;
}

public static ErrorClassType fromChar(char nibble) throws NumberFormatException {
int ErrorClassTypeInt = parseInt(String.valueOf(nibble), 16);
ErrorClassType ErrorClassType = fromErrorClassTypeInt(ErrorClassTypeInt);
return ErrorClassType;
}

public static ErrorClassType fromOctet(Octet ErrorClassTypeOctet) throws NumberFormatException {
if (ErrorClassTypeOctet == null) {
return null;
}
Integer ErrorClassTypeInt = parseInt(ErrorClassTypeOctet.toString(), 16);
ErrorClassType ErrorClassType = fromErrorClassTypeInt(ErrorClassTypeInt.intValue());
return ErrorClassType;
}


public int getErrorClassTypeInt() {
return ErrorClassTypeInt;
}

// enum constructor - cannot be public or protected
private ErrorClassType(int ErrorClassTypeInt) {
this.ErrorClassTypeInt = ErrorClassTypeInt;
}
}
171 changes: 171 additions & 0 deletions src/main/java/no/entra/bacnet/error/ErrorCodeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package no.entra.bacnet.error;

import no.entra.bacnet.octet.Octet;

import static java.lang.Integer.parseInt;

public enum ErrorCodeType {
AbortApduTooLong(123),
AbortApplicationExceededReplyTime(124),
AbortBufferOverflow(51),
AbortInsufficientSecurity(135),
AbortInvalidApduInThisState(52),
AbortOther(56),
AbortOutOfResources(125),
AbortPreemptedByHigherPriorityTask(53),
AbortProprietary(55),
AbortSecurityError(136),
AbortSegmentationNotSupported(54),
AbortTsmTimeout(126),
AbortWindowSizeOutOfRange(127),
AccessDenied(85),
AddressingError(115),
BadDestinationAddress(86),
BadDestinationDeviceId(87),
BadSignature(88),
BadSourceAddress(89),
BadTimestamp(90),
Busy(82),
CannotUseKey(91),
CannotVerifyMessageId(92),
CharacterSetNotSupported(41),
CommunicationDisabled(83),
ConfigurationInProgress(2),
CorrectKeyRevision(93),
CovSubscriptionFailed(43),
DatatypeNotSupported(47),
DeleteFdtEntryFailed(120),
DestinationDeviceIdRequired(94),
DeviceBusy(3),
DistributeBroadcastFailed(121),
DuplicateEntry(137),
DuplicateMessage(95),
DuplicateName(48),
DuplicateObjectId(49),
DynamicCreationNotSupported(4),
EncryptionNotConfigured(96),
EncryptionRequired(97),
FileAccessDenied(5),
FileFull(128),
InconsistentConfiguration(129),
InconsistentObjectType(130),
InconsistentParameters(7),
InconsistentSelectionCriterion(8),
IncorrectKey(98),
InternalError(131),
InvalidArrayIndex(42),
InvalidConfigurationData(46),
InvalidDataType(9),
InvalidEventState(73),
InvalidFileAccessMethod(10),
InvalidFileStartPosition(11),
InvalidKeyData(99),
InvalidParameterDataType(13),
InvalidTag(57),
InvalidTimestamp(14),
InvalidValueInThisState(138),
KeyUpdateInProgress(100),
ListElementNotFound(81),
LogBufferFull(75),
LoggedValuePurged(76),
MalformedMessage(101),
MessageTooLong(113),
MissingRequiredParameter(16),
NetworkDown(58),
NoAlarmConfigured(74),
NoObjectsOfSpecifiedType(17),
NoPropertySpecified(77),
NoSpaceForObject(18),
NoSpaceToAddListElement(19),
NoSpaceToWriteProperty(20),
NoVtSessionsAvailable(21),
NotConfigured(132),
NotConfiguredForTriggeredLogging(78),
NotCovProperty(44),
NotKeyServer(102),
NotRouterToDnet(110),
ObjectDeletionNotPermitted(23),
ObjectIdentifierAlreadyExists(24),
OperationalProblem(25),
OptionalFunctionalityNotSupported(45),
Other(0),
OutOfMemory(133),
ParameterOutOfRange(80),
PasswordFailure(26),
PropertyIsNotAList(22),
PropertyIsNotAnArray(50),
ReadAccessDenied(27),
ReadBdtFailed(117),
ReadFdtFailed(119),
RegisterForeignDeviceFailed(118),
RejectBufferOverflow(59),
RejectInconsistentParameters(60),
RejectInvalidParameterDataType(61),
RejectInvalidTag(62),
RejectMissingRequiredParameter(63),
RejectOther(69),
RejectParameterOutOfRange(64),
RejectProprietary(68),
RejectTooManyArguments(65),
RejectUndefinedEnumeration(66),
RejectUnrecognizedService(67),
RouterBusy(111),
SecurityError(114),
SecurityNotConfigured(103),
ServiceRequestDenied(29),
SourceSecurityRequired(104),
Success(84),
Timeout(30),
TooManyKeys(105),
UnknownAuthenticationType(106),
UnknownDevice(70),
UnknownFileSize(122),
UnknownKey(107),
UnknownKeyRevision(108),
UnknownNetworkMessage(112),
UnknownObject(31),
UnknownProperty(32),
UnknownRoute(71),
UnknownSourceMessage(109),
UnknownSubscription(79),
UnknownVtClass(34),
UnknownVtSession(35),
UnsupportedObjectType(36),
ValueNotInitialized(72),
ValueOutOfRange(37),
ValueTooLong(134),
VtSessionAlreadyClosed(38),
VtSessionTerminationFailure(39),
WriteAccessDenied(40),
WriteBdtFailed(116);

private int ErrorCodeTypeInt;

public static ErrorCodeType fromErrorCodeTypeInt(int ErrorCodeTypeInt) {
for (ErrorCodeType type : values()) {
if (type.getErrorCodeTypeInt() == ErrorCodeTypeInt) {
return type;
}
}
return null;
}

public static ErrorCodeType fromOctet(Octet errorCodeTypeOctet) throws NumberFormatException {
if (errorCodeTypeOctet == null) {
return null;
}
Integer ErrorCodeTypeInt = parseInt(errorCodeTypeOctet.toString(), 16);
ErrorCodeType ErrorCodeType = fromErrorCodeTypeInt(ErrorCodeTypeInt.intValue());
return ErrorCodeType;
}


public int getErrorCodeTypeInt() {
return ErrorCodeTypeInt;
}

// enum constructor - cannot be public or protected
private ErrorCodeType(int ErrorCodeTypeInt) {
this.ErrorCodeTypeInt = ErrorCodeTypeInt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ public enum PropertyIdentifier {
WriteStatus("172"),
ZoneFrom("140"),
ZoneMembers("a5"),
ZoneTo("141");
ZoneTo("141"),
XxError("fff");

private String propertyIdentifierHex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import no.entra.bacnet.apdu.ApplicationTag;
import no.entra.bacnet.apdu.SDContextTag;
import no.entra.bacnet.apdu.ValueType;
import no.entra.bacnet.error.ErrorClassType;
import no.entra.bacnet.error.ErrorCodeType;
import no.entra.bacnet.mappers.MapperResult;
import no.entra.bacnet.objects.ObjectId;
import no.entra.bacnet.objects.ObjectIdMapper;
Expand All @@ -11,8 +13,10 @@
import no.entra.bacnet.properties.PropertyIdentifier;
import org.slf4j.Logger;

import static no.entra.bacnet.apdu.SDContextTag.TAG4START;
import static no.entra.bacnet.apdu.SDContextTag.TAG5START;
import java.util.HashMap;
import java.util.Map;

import static no.entra.bacnet.apdu.SDContextTag.*;
import static no.entra.bacnet.mappers.StringMapper.parseCharStringExtended;
import static no.entra.bacnet.utils.HexUtils.toInt;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -52,6 +56,8 @@ public class ReadPropertyResultParser {
private static final Logger log = getLogger(ReadPropertyResultParser.class);

public static final char ExtendedValue = '5';
public static final String ERROR_CODE = "ErrorCode";
public static final String ERROR_CLASS = "ErrorClass";

public static ReadPropertyResult parse(String hexString) throws BacnetParserException {
OctetReader propertyReader = new OctetReader(hexString);
Expand Down Expand Up @@ -111,11 +117,32 @@ public static ReadPropertyResult parse(String hexString) throws BacnetParserExce
default:
throw new IllegalArgumentException("Not implemented yet, " + valueType);
}
if (propertyReader.unprocessedHexString().startsWith(TAG4END.toString())) {
propertyReader.next();
}
readPropertyResult.setUnparsedHexString(propertyReader.unprocessedHexString());
} else if (readResultOctet.equals(TAG5START)) {
//property-access-error
//FIXME Error handling on ReadPropertyMultiple
throw new IllegalArgumentException("Not implemented yet");
Octet applicationTagOctet = propertyReader.next();
ApplicationTag applicationTag = new ApplicationTag(applicationTagOctet);
if (applicationTag.findValueType().equals(ValueType.Enumerated)) {
Octet errorClassOctet = propertyReader.next();
ErrorClassType errorClass = ErrorClassType.fromChar(errorClassOctet.getSecondNibble());
Map<String, Enum> errorMap = new HashMap<>();
errorMap.put(ERROR_CLASS, errorClass);
readPropertyResult.addReadResult(PropertyIdentifier.XxError, errorMap);
applicationTagOctet = propertyReader.next();
applicationTag = new ApplicationTag(applicationTagOctet);
if (applicationTag.findValueType().equals(ValueType.Enumerated)) {
Octet errorCodeOctet = propertyReader.next();
ErrorCodeType errorCode = ErrorCodeType.fromOctet(errorCodeOctet);
errorMap.put(ERROR_CODE, errorCode);
}
if (propertyReader.unprocessedHexString().startsWith(TAG5END.toString())) {
propertyReader.next();
}
readPropertyResult.setUnparsedHexString(propertyReader.unprocessedHexString());
}
} else {
readPropertyResult.setInitialHexString(hexString);
readPropertyResult.setUnparsedHexString(propertyReader.unprocessedHexString());
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/no/entra/bacnet/error/ErrorClassTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package no.entra.bacnet.error;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ErrorClassTypeTest {

@Test
void toStringTest() {
assertEquals("device", ErrorClassType.device.toString());
}
}
13 changes: 13 additions & 0 deletions src/test/java/no/entra/bacnet/error/ErrorCodeTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package no.entra.bacnet.error;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ErrorCodeTypeTest {

@Test
void toStringTest() {
assertEquals("UnknownProperty", ErrorCodeType.UnknownProperty.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package no.entra.bacnet.services;

import no.entra.bacnet.error.ErrorClassType;
import no.entra.bacnet.error.ErrorCodeType;
import no.entra.bacnet.objects.ObjectId;
import no.entra.bacnet.properties.PropertyIdentifier;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static no.entra.bacnet.objects.ObjectType.AnalogValue;
import static no.entra.bacnet.services.ReadPropertyResultParser.ERROR_CLASS;
import static no.entra.bacnet.services.ReadPropertyResultParser.ERROR_CODE;
import static org.junit.jupiter.api.Assertions.*;

/*
Expand Down Expand Up @@ -67,4 +71,16 @@ void parseObjectProperty() throws BacnetParserException {
assertEquals(PropertyIdentifier.Description, readPropertyResult.getPropertyIdentifier());
assertEquals("FW Series Bacnet Device", readPropertyResult.getReadResult().get(PropertyIdentifier.Description));
}

@Test
void parseErrorObject() throws BacnetParserException {
String hexString = "29555e910291205f";
ReadPropertyResult readPropertyResult = ReadPropertyResultParser.parse(hexString);
assertNotNull(readPropertyResult);
assertTrue(readPropertyResult.isParsedOk());
Map<String, Enum> errorMap = (Map<String, Enum>) readPropertyResult.getReadResult().get(PropertyIdentifier.XxError);
assertNotNull(errorMap);
assertEquals(ErrorClassType.property, errorMap.get(ERROR_CLASS));
assertEquals(ErrorCodeType.UnknownProperty, errorMap.get(ERROR_CODE));
}
}

0 comments on commit e5680e6

Please sign in to comment.