Skip to content

Commit 1fd30bd

Browse files
committed
Fix #112, BinaryEncoder DataValue now encoded as defined in the spec
1 parent a94dcba commit 1fd30bd

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/org/opcfoundation/ua/encoding/binary/BinaryEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2083,11 +2083,12 @@ public void putDataValue(String fieldName, DataValue v)
20832083
if (v.getServerPicoseconds()!=null && !v.getServerPicoseconds().equals(UnsignedShort.MIN_VALUE)) mask |= 0x20;
20842084

20852085
putSByte(null, mask);
2086+
// NOTE!! The order of fields differ from the "order" of the mask, see spec 1.04 Part 6 section 5.2.2.17 for DataValue encoding
20862087
if ((mask & 1) == 1) putVariant(null, v.getValue());
20872088
if ((mask & 2) == 2) putStatusCode(null, v.getStatusCode());
20882089
if ((mask & 4) == 4) putDateTime(null, v.getSourceTimestamp());
2089-
if ((mask & 8) == 8) putDateTime(null, v.getServerTimestamp());
20902090
if ((mask & 0x10) == 0x10) putUInt16(null, v.getSourcePicoseconds());
2091+
if ((mask & 8) == 8) putDateTime(null, v.getServerTimestamp());
20912092
if ((mask & 0x20) == 0x20) putUInt16(null, v.getServerPicoseconds());
20922093
}
20932094

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.opcfoundation.ua.encoding.binary;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.opcfoundation.ua.builtintypes.DataValue;
7+
import org.opcfoundation.ua.builtintypes.DateTime;
8+
import org.opcfoundation.ua.builtintypes.StatusCode;
9+
import org.opcfoundation.ua.builtintypes.UnsignedShort;
10+
import org.opcfoundation.ua.builtintypes.Variant;
11+
12+
public class BinaryEncoderTest {
13+
14+
@Test
15+
public void encodeDecodeDataValue() throws Exception {
16+
//For GH#112
17+
//Test that encoded and then decoded DataValue are equal
18+
//NOTE this should be later refactored to check the raw binary form as well
19+
20+
//Setting values that force encoding, i.e. not default values that are handed by the encoding mask
21+
DateTime dt = DateTime.fromMillis(100000); //some time that is not min or max
22+
UnsignedShort tenPicoSeconds = UnsignedShort.valueOf(10); //100 picoseconds, something that is not 0
23+
final DataValue dv = new DataValue(new Variant(Integer.valueOf(1)), StatusCode.BAD, dt, tenPicoSeconds, dt, tenPicoSeconds);
24+
EncoderCalc calc = new EncoderCalc();
25+
calc.putDataValue(null, dv);
26+
int len = calc.length;
27+
byte[] buf = new byte[len];
28+
BinaryEncoder enc = new BinaryEncoder(buf);
29+
enc.putDataValue(null, dv);
30+
31+
//System.out.println(CryptoUtil.toHex(buf));
32+
33+
BinaryDecoder dec = new BinaryDecoder(buf);
34+
final DataValue dv2 = dec.getDataValue(null);
35+
36+
//System.out.println(dv);
37+
//System.out.println(dv2);
38+
assertEquals(dv, dv2);
39+
40+
}
41+
42+
}

0 commit comments

Comments
 (0)