-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathSettings.java
More file actions
378 lines (373 loc) · 16.6 KB
/
Copy pathSettings.java
File metadata and controls
378 lines (373 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
// Filename: $HeadURL$
//
// Version: $Revision$,
// $Date$
// $Author$
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of the License.
// Gurux Device Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// More information of Gurux products: http://www.gurux.org
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
package gurux.dlms.simulator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import gurux.common.GXCmdParameter;
import gurux.common.GXCommon;
import gurux.common.IGXMedia;
import gurux.common.enums.TraceLevel;
import gurux.dlms.GXDLMSClient;
import gurux.dlms.GXSimpleEntry;
import gurux.dlms.enums.Authentication;
import gurux.dlms.enums.InterfaceType;
import gurux.dlms.enums.Security;
import gurux.dlms.enums.Standard;
import gurux.dlms.secure.GXDLMSSecureClient;
import gurux.net.GXNet;
import gurux.serial.GXSerial;
public class Settings {
public IGXMedia media = null;
public TraceLevel trace = TraceLevel.INFO;
public boolean iec = false;
public GXDLMSSecureClient client = new GXDLMSSecureClient(true);
String invocationCounter;
// Objects to read.
public List<Map.Entry<String, Integer>> readObjects =
new ArrayList<Map.Entry<String, Integer>>();
public int serverCount = 1;
// Simulator file.
public String outputFile = null;
// Simulator file.
public String inputFile = null;
// All meters are using the same port number.
public boolean exclusive = false;
/**
* Show help.
*/
static void showHelp() {
System.out.println("Gurux DLMS Simulator.");
System.out.println("Server parameters:");
System.out
.println(" -t [Error, Warning, Info, Verbose] Trace messages.");
System.out.println(" -p Start port number. Default is 4060.");
System.out.println(" -N Amount of the TCP/IP servers. Default is 1.");
System.out.println(" -X All meters are using the same port.");
System.out.println(" -w WRAPPER profile is used. HDLC is default.");
System.out.println(" -S Serial port.");
System.out.println(" -x input XML file.");
System.out.println(
" -r [sn, sn]\t Short name or Logican Name (default) referencing is used.");
System.out.println("Example:");
System.out.println("Start DLMS simulator using TCP/IP.");
System.out.println(
"Gurux.Dlms.Simulator.Net -p [Meter Port No] -x meter-template.xml");
System.out.println(
"------------------------------------------------------------------------");
System.out.println("Reading parameters:");
System.out.println(" -h \t host name or IP address.");
System.out.println(" -p \t port number or name (Example: 1000).");
System.out.println(" -S \t serial port.");
System.out.println(" -i IEC is a start protocol.");
System.out.println(
" -a \t Authentication (None, Low, High, HighMd5, HighSha1, HighGMac, HighSha256).");
System.out.println(" -P \t Password for authentication.");
System.out.println(" -c \t Client address. (Default: 16)");
System.out.println(" -s \t Server address. (Default: 1)");
System.out.println(" -n \t Server address as serial number.");
System.out.println(
" -r [sn, ln]\t Short name or Logical Name (default) referencing is used.");
System.out.println(" -w WRAPPER profile is used. HDLC is default.");
System.out
.println(" -t [Error, Warning, Info, Verbose] Trace messages.");
System.out.println(
" -g \"0.0.1.0.0.255:1; 0.0.1.0.0.255:2\" Get selected object(s) with given attribute index.");
System.out.println(
" -C \t Security Level. (None, Authentication, Encrypted, AuthenticationEncryption)");
System.out.println(
" -v \t Invocation counter data object Logical Name. Ex. 0.0.43.1.1.255");
System.out.println(" -I \t Auto increase invoke ID");
System.out.println(
" -o \t Cache association view to make reading faster. Ex. -o C:\\device.xml");
System.out.println(
" -T \t System title that is used with chiphering. Ex -D 4775727578313233");
System.out.println(
" -A \t Authentication key that is used with chiphering. Ex -D D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF");
System.out.println(
" -B \t Block cipher key that is used with chiphering. Ex -D 000102030405060708090A0B0C0D0E0F");
System.out.println(
" -D \t Dedicated key that is used with chiphering. Ex -D 00112233445566778899AABBCCDDEEFF");
System.out.println(
" -d \t Used DLMS standard. Ex -d India (DLMS, India, Italy, SaudiArabia, IDIS)");
System.out.println("Example:");
System.out.println("Read DLMS device using TCP/IP connection.");
System.out.println(
"Gurux.Dlms.Simulator.Net -c 16 -s 1 -h [Meter IP Address] -p [Meter Port No] -o meter-template.xml");
}
static int getParameters(String[] args, Settings settings) {
List<GXCmdParameter> parameters = GXCommon.getParameters(args,
"h:p:c:s:r:iIt:a:p:wP:g:S:n:C:v:o:T:A:B:D:d:l:F:r:x:N:Xx:");
GXNet net = null;
for (GXCmdParameter it : parameters) {
switch (it.getTag()) {
case 'w':
settings.client.setInterfaceType(InterfaceType.WRAPPER);
break;
case 'r':
if ("sn".compareTo(it.getValue()) == 0) {
settings.client.setUseLogicalNameReferencing(false);
} else if ("ln".compareTo(it.getValue()) == 0) {
settings.client.setUseLogicalNameReferencing(true);
} else {
throw new IllegalArgumentException(
"Invalid reference option.");
}
break;
case 'h':
// Host address.
if (settings.media == null) {
settings.media = new GXNet();
}
net = (GXNet) settings.media;
net.setHostName(it.getValue());
break;
case 't':
// Trace.
if ("Error".compareTo(it.getValue()) == 0)
settings.trace = TraceLevel.ERROR;
else if ("Warning".compareTo(it.getValue()) == 0)
settings.trace = TraceLevel.WARNING;
else if ("Info".compareTo(it.getValue()) == 0)
settings.trace = TraceLevel.INFO;
else if ("Verbose".compareTo(it.getValue()) == 0)
settings.trace = TraceLevel.VERBOSE;
else if ("Off".compareTo(it.getValue()) == 0)
settings.trace = TraceLevel.OFF;
else
throw new IllegalArgumentException(
"Invalid Trace level option '" + it.getValue()
+ "'. (Error, Warning, Info, Verbose, Off).");
break;
case 'p':
// Port.
if (settings.media == null) {
settings.media = new GXNet();
}
net = (GXNet) settings.media;
net.setPort(Integer.parseInt(it.getValue()));
break;
case 'P':// Password
settings.client.setPassword(it.getValue().getBytes());
break;
case 'i':
// IEC.
settings.iec = true;
break;
case 'I':
// AutoIncreaseInvokeID.
settings.client.setAutoIncreaseInvokeID(true);
break;
case 'C':
if ("None".compareTo(it.getValue()) == 0) {
settings.client.getCiphering().setSecurity(Security.NONE);
} else if ("Authentication".compareTo(it.getValue()) == 0) {
settings.client.getCiphering()
.setSecurity(Security.AUTHENTICATION);
} else if ("Encryption".compareTo(it.getValue()) == 0) {
settings.client.getCiphering()
.setSecurity(Security.ENCRYPTION);
} else if ("AuthenticationEncryption"
.compareTo(it.getValue()) == 0) {
settings.client.getCiphering()
.setSecurity(Security.AUTHENTICATION_ENCRYPTION);
} else {
throw new IllegalArgumentException(
"Invalid Ciphering option '" + it.getValue()
+ "'. (None, Authentication, Encryption, AuthenticationEncryption)");
}
break;
case 'T':
settings.client.getCiphering()
.setSystemTitle(GXCommon.hexToBytes(it.getValue()));
break;
case 'A':
settings.client.getCiphering().setAuthenticationKey(
GXCommon.hexToBytes(it.getValue()));
break;
case 'B':
settings.client.getCiphering()
.setBlockCipherKey(GXCommon.hexToBytes(it.getValue()));
break;
case 'D':
settings.client.getCiphering()
.setDedicatedKey(GXCommon.hexToBytes(it.getValue()));
break;
case 'F':
settings.client.getCiphering().setInvocationCounter(
Integer.parseInt(it.getValue().trim()));
break;
case 'o':
settings.outputFile = it.getValue();
break;
case 'x':
settings.inputFile = it.getValue();
break;
case 'd':
try {
settings.client
.setStandard(Standard.valueOfString(it.getValue()));
if (settings.client.getStandard() == Standard.ITALY
|| settings.client.getStandard() == Standard.INDIA
|| settings.client
.getStandard() == Standard.SAUDI_ARABIA) {
settings.client.setUseUtc2NormalTime(true);
}
} catch (Exception e) {
throw new IllegalArgumentException(
"Invalid DLMS standard option: '" + it.getValue()
+ "'. (DLMS, India, Italy, SaudiArabia, IDIS)");
}
break;
case 'N':
settings.serverCount = Integer.parseInt(it.getValue());
break;
case 'X':
settings.exclusive = true;
break;
case 'v':
settings.invocationCounter = it.getValue();
// TODO:
// GXDLMSObject.ValidateLogicalName(settings.invocationCounter);
break;
case 'g':
// Get (read) selected objects.
for (String o : it.getValue().split("[;,]")) {
String[] tmp = o.split("[:]");
if (tmp.length != 2) {
throw new IllegalArgumentException(
"Invalid Logical name or attribute index.");
}
settings.readObjects.add(new GXSimpleEntry<String, Integer>(
tmp[0].trim(), Integer.parseInt(tmp[1].trim())));
}
break;
case 'S':// Serial Port
settings.media = new GXSerial();
GXSerial serial = (GXSerial) settings.media;
serial.setPortName(it.getValue());
break;
case 'a':
try {
settings.client.setAuthentication(
Authentication.valueOfString(it.getValue()));
} catch (Exception e) {
throw new IllegalArgumentException(
"Invalid Authentication option: '" + it.getValue()
+ "'. (None, Low, High, HighMd5, HighSha1, HighGmac, HighSha256)");
}
break;
case 'c':
settings.client
.setClientAddress(Integer.parseInt(it.getValue()));
break;
case 's':
settings.client
.setServerAddress(Integer.parseInt(it.getValue()));
break;
case 'l':
settings.client.setServerAddress(GXDLMSClient.getServerAddress(
Integer.parseInt(it.getValue()),
settings.client.getServerAddress()));
break;
case 'n':
settings.client.setServerAddress(GXDLMSClient
.getServerAddress(Integer.parseInt(it.getValue())));
break;
case '?':
switch (it.getTag()) {
case 'c':
throw new IllegalArgumentException(
"Missing mandatory client option.");
case 's':
throw new IllegalArgumentException(
"Missing mandatory server option.");
case 'h':
throw new IllegalArgumentException(
"Missing mandatory host name option.");
case 'p':
throw new IllegalArgumentException(
"Missing mandatory port option.");
case 'r':
throw new IllegalArgumentException(
"Missing mandatory reference option.");
case 'a':
throw new IllegalArgumentException(
"Missing mandatory authentication option.");
case 'S':
throw new IllegalArgumentException(
"Missing mandatory Serial port option.\n");
case 't':
throw new IllegalArgumentException(
"Missing mandatory trace option.\n");
case 'g':
throw new IllegalArgumentException(
"Missing mandatory OBIS code option.");
case 'C':
throw new IllegalArgumentException(
"Missing mandatory Ciphering option.");
case 'v':
throw new IllegalArgumentException(
"Missing mandatory invocation counter logical name option.");
case 'T':
throw new IllegalArgumentException(
"Missing mandatory system title option.");
case 'A':
throw new IllegalArgumentException(
"Missing mandatory authentication key option.");
case 'B':
throw new IllegalArgumentException(
"Missing mandatory block cipher key option.");
case 'D':
throw new IllegalArgumentException(
"Missing mandatory dedicated key option.");
case 'd':
throw new IllegalArgumentException(
"Missing mandatory DLMS standard option.");
default:
showHelp();
return 1;
}
default:
showHelp();
return 1;
}
}
if (settings.media == null) {
showHelp();
return 1;
}
return 0;
}
}