Skip to content

Commit f80536e

Browse files
committed
1 parent 6e50097 commit f80536e

File tree

7 files changed

+150
-15
lines changed

7 files changed

+150
-15
lines changed

development/src/main/java/gurux/dlms/GXDLMS.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,9 @@ public static List<byte[]> getSnMessages(final GXDLMSSNParameters p)
12411241
java.util.ArrayList<byte[]> messages =
12421242
new java.util.ArrayList<byte[]>();
12431243
byte frame = 0x0;
1244-
if (p.getCommand() == Command.INFORMATION_REPORT) {
1244+
if (p.getCommand() == Command.INFORMATION_REPORT
1245+
|| p.getCommand() == Command.DATA_NOTIFICATION) {
12451246
frame = 0x13;
1246-
} else if (p.getCommand() == Command.NONE) {
1247-
frame = p.getSettings().getNextSend(true);
12481247
}
12491248
do {
12501249
getSNPdu(p, reply);

development/src/main/java/gurux/dlms/GXDLMSClient.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,9 @@ public final byte[][] write(final Object name, final Object value,
17561756
* Bad padding exception.
17571757
* @throws IllegalBlockSizeException
17581758
* Illegal block size exception.
1759+
* @deprecated use {@link writeList} instead.
17591760
*/
1760-
public final byte[][] writeList(final List<GXWriteItem> list)
1761+
public final byte[][] writeList2(final List<GXWriteItem> list)
17611762
throws InvalidKeyException, NoSuchAlgorithmException,
17621763
NoSuchPaddingException, InvalidAlgorithmParameterException,
17631764
IllegalBlockSizeException, BadPaddingException {
@@ -1823,6 +1824,97 @@ public final byte[][] writeList(final List<GXWriteItem> list)
18231824
return reply.toArray(new byte[0][0]);
18241825
}
18251826

1827+
/**
1828+
* Write list of COSEM objects.
1829+
*
1830+
* @param list
1831+
* DLMS objects to write.
1832+
* @return Write request as byte array.
1833+
* @throws NoSuchPaddingException
1834+
* No such padding exception.
1835+
* @throws NoSuchAlgorithmException
1836+
* No such algorithm exception.
1837+
* @throws InvalidAlgorithmParameterException
1838+
* Invalid algorithm parameter exception.
1839+
* @throws InvalidKeyException
1840+
* Invalid key exception.
1841+
* @throws BadPaddingException
1842+
* Bad padding exception.
1843+
* @throws IllegalBlockSizeException
1844+
* Illegal block size exception.
1845+
*/
1846+
public final byte[][]
1847+
writeList(final List<Entry<GXDLMSObject, Integer>> list)
1848+
throws InvalidKeyException, NoSuchAlgorithmException,
1849+
NoSuchPaddingException, InvalidAlgorithmParameterException,
1850+
IllegalBlockSizeException, BadPaddingException {
1851+
if (!getNegotiatedConformance()
1852+
.contains(Conformance.MULTIPLE_REFERENCES)) {
1853+
throw new IllegalArgumentException(
1854+
"Meter doesn't support multiple objects writing with one request.");
1855+
}
1856+
if (list == null || list.isEmpty()) {
1857+
throw new IllegalArgumentException("Invalid parameter.");
1858+
}
1859+
Object value;
1860+
List<byte[]> reply;
1861+
settings.resetBlockIndex();
1862+
if (autoIncreaseInvokeID) {
1863+
settings.setInvokeID((byte) ((settings.getInvokeID() + 1) & 0xF));
1864+
}
1865+
GXByteBuffer data = new GXByteBuffer();
1866+
GXByteBuffer bb = new GXByteBuffer();
1867+
if (this.getUseLogicalNameReferencing()) {
1868+
// Add length.
1869+
bb.setUInt8(list.size());
1870+
for (Entry<GXDLMSObject, Integer> it : list) {
1871+
// CI.
1872+
bb.setUInt16(it.getKey().getObjectType().getValue());
1873+
bb.set(GXCommon
1874+
.logicalNameToBytes(it.getKey().getLogicalName()));
1875+
// Attribute ID.
1876+
bb.setUInt8(it.getValue());
1877+
// Attribute selector is not used.
1878+
bb.setUInt8(0);
1879+
}
1880+
} else {
1881+
for (Entry<GXDLMSObject, Integer> it : list) {
1882+
// Add variable type.
1883+
bb.setUInt8(2);
1884+
int sn = GXCommon.intValue(it.getKey().getShortName());
1885+
sn += (it.getValue() - 1) * 8;
1886+
bb.setUInt16(sn);
1887+
}
1888+
}
1889+
// Write values.
1890+
GXCommon.setObjectCount(list.size(), bb);
1891+
for (Entry<GXDLMSObject, Integer> it : list) {
1892+
ValueEventArgs e = new ValueEventArgs(settings, it.getKey(),
1893+
it.getValue(), 0, null);
1894+
value = it.getKey().getValue(settings, e);
1895+
DataType type = it.getKey().getDataType(it.getValue());
1896+
if ((type == null || type == DataType.NONE) && value != null) {
1897+
type = GXDLMSConverter.getDLMSDataType(value);
1898+
if (type == DataType.NONE) {
1899+
throw new GXDLMSException("Invalid parameter. "
1900+
+ " In java value type must give.");
1901+
}
1902+
}
1903+
GXCommon.setData(settings, data, type, value);
1904+
}
1905+
if (this.getUseLogicalNameReferencing()) {
1906+
GXDLMSLNParameters p = new GXDLMSLNParameters(settings, 0,
1907+
Command.SET_REQUEST, SetRequestType.WITH_LIST, bb, data,
1908+
0xff, Command.NONE);
1909+
reply = GXDLMS.getLnMessages(p);
1910+
} else {
1911+
GXDLMSSNParameters p = new GXDLMSSNParameters(settings,
1912+
Command.WRITE_REQUEST, list.size(), 4, bb, data);
1913+
reply = GXDLMS.getSnMessages(p);
1914+
}
1915+
return reply.toArray(new byte[0][0]);
1916+
}
1917+
18261918
/**
18271919
* Generates a read message.
18281920
*

development/src/main/java/gurux/dlms/GXDLMSSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class GXDLMSSettings {
7777
/**
7878
* Server receiver frame sequence starting number.
7979
*/
80-
static final short SERVER_START_RECEIVER_FRAME_SEQUENCE = 0xEE;
80+
static final short SERVER_START_RECEIVER_FRAME_SEQUENCE = 0xFE;
8181

8282
/**
8383
* Client sender frame sequence starting number.

development/src/main/java/gurux/dlms/GXDLMSTranslator.java

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public class GXDLMSTranslator {
131131
* System title.
132132
*/
133133
private byte[] systemTitle;
134+
135+
/**
136+
* Server system title.
137+
*/
138+
private byte[] serverSystemTitle;
139+
140+
/**
141+
* Dedicated key.
142+
*/
143+
private byte[] dedicatedKey;
144+
134145
/**
135146
* Block cipher key.
136147
*/
@@ -209,6 +220,36 @@ public final void setSystemTitle(final byte[] value) {
209220
systemTitle = value;
210221
}
211222

223+
/**
224+
* @return Server system title.
225+
*/
226+
public byte[] getServerSystemTitle() {
227+
return serverSystemTitle;
228+
}
229+
230+
/**
231+
* @param value
232+
* Server system title.
233+
*/
234+
public void setServerSystemTitle(final byte[] value) {
235+
serverSystemTitle = value;
236+
}
237+
238+
/**
239+
* @return Dedicated key.
240+
*/
241+
public byte[] getDedicatedKey() {
242+
return dedicatedKey;
243+
}
244+
245+
/**
246+
* @param value
247+
* Dedicated key.
248+
*/
249+
public void setDedicatedKey(final byte[] value) {
250+
dedicatedKey = value;
251+
}
252+
212253
/**
213254
* @return Block cipher key.
214255
*/
@@ -515,17 +556,20 @@ public final byte[] getPdu(final GXByteBuffer value)
515556
return data.getData().array();
516557
}
517558

518-
private GXCiphering getCiphering(final boolean force) {
559+
private void getCiphering(final GXDLMSSettings settings,
560+
final boolean force) {
519561
if (force || security != Security.NONE) {
520562
GXCiphering c = new GXCiphering(systemTitle);
521563
c.setSecurity(security);
522564
c.setSystemTitle(systemTitle);
523565
c.setBlockCipherKey(blockCipherKey);
524566
c.setAuthenticationKey(authenticationKey);
525567
c.setInvocationCounter(invocationCounter);
526-
return c;
568+
c.setDedicatedKey(dedicatedKey);
569+
settings.setSourceSystemTitle(serverSystemTitle);
570+
settings.setCipher(c);
527571
}
528-
return null;
572+
settings.setCipher(null);
529573
}
530574

531575
/**
@@ -624,7 +668,7 @@ public final String messageToXml(final GXByteBuffer value)
624668
data.setXml(xml);
625669
int offset = value.position();
626670
GXDLMSSettings settings = new GXDLMSSettings(true);
627-
settings.setCipher(getCiphering(true));
671+
getCiphering(settings, true);
628672
// If HDLC framing.
629673
if (value.getUInt8(value.position()) == 0x7e) {
630674
settings.setInterfaceType(InterfaceType.HDLC);
@@ -917,7 +961,7 @@ String pduToXml(final GXDLMSTranslatorStructure xml,
917961
}
918962
try {
919963
GXDLMSSettings settings = new GXDLMSSettings(true);
920-
settings.setCipher(getCiphering(false));
964+
getCiphering(settings, false);
921965
GXReplyData data = new GXReplyData();
922966
short cmd = value.getUInt8();
923967
String str;
@@ -935,7 +979,7 @@ String pduToXml(final GXDLMSTranslatorStructure xml,
935979
case Command.INITIATE_RESPONSE:
936980
value.position(0);
937981
settings = new GXDLMSSettings(false);
938-
settings.setCipher(getCiphering(true));
982+
getCiphering(settings, true);
939983
GXAPDU.parseInitiate(true, settings, settings.getCipher(),
940984
value, xml);
941985
break;
@@ -946,7 +990,7 @@ String pduToXml(final GXDLMSTranslatorStructure xml,
946990
case Command.AARE:
947991
value.position(0);
948992
settings = new GXDLMSSettings(false);
949-
settings.setCipher(getCiphering(true));
993+
getCiphering(settings, true);
950994
GXAPDU.parsePDU(settings, settings.getCipher(), value, xml);
951995
break;
952996
case Command.GET_REQUEST:

development/src/main/java/gurux/dlms/objects/GXDLMSMBusClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public GXDLMSMBusClient(final String ln, final int sn) {
9999
super(ObjectType.MBUS_CLIENT, ln, sn);
100100
captureDefinition =
101101
new java.util.ArrayList<java.util.Map.Entry<String, String>>();
102+
setVersion(1);
102103
}
103104

104105
/**

development/src/main/java/gurux/dlms/secure/GXSecure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static byte[] decryptAesKeyWrapping(final byte[] input, final byte[] kek) {
212212
for (int k = 1; t != 0; k++) {
213213
byte v = (byte) t;
214214
buf[IV.length - k] ^= v;
215-
t = (int) ((int) t >> 8);
215+
t = t >> 8;
216216
}
217217
buf = cipher.doFinal(buf);
218218
System.arraycopy(buf, 0, a, 0, 8);

gurux.dlms.server.example.java/src/gurux/dlms/server/example/GXDLMSBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,7 @@ void addIp4Setup() {
544544
GXDLMSIp4Setup ip4 = new GXDLMSIp4Setup();
545545
// Get FIRST local IP address.
546546
try {
547-
ip4.setIPAddress(
548-
InetAddress.getLocalHost().getHostAddress().toString());
547+
ip4.setIPAddress(InetAddress.getLocalHost());
549548
} catch (UnknownHostException e) {
550549
throw new RuntimeException(e.getMessage());
551550
}

0 commit comments

Comments
 (0)