-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTransferClient.cs
More file actions
536 lines (433 loc) · 18.9 KB
/
FileTransferClient.cs
File metadata and controls
536 lines (433 loc) · 18.9 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System.Diagnostics;
using System.Text;
using System.Management.Automation;
using Kaenx.Konnect.Classes;
using Kaenx.Konnect.EMI.DataMessages;
using Kaenx.Konnect.Exceptions;
using System.Reflection.Metadata;
namespace KnxFileTransferClient.Lib;
public class FileTransferClient
{
private BusDevice device;
private const int ObjectIndex = 159;
private const int packageOverhead = 6;
public enum FtmCommands
{
Format,
Exists,
Rename,
FileUpload = 40,
FileDownload,
FileDelete,
FileInfo,
DirList = 80,
DirCreate,
DirDelete,
Cancel = 90,
GetVersion = 100,
FwUpdate,
CheckFeatures
}
public enum FtmFeatures
{
Resume = 1,
FirmwareUpdate = 2
}
public delegate void ProcessChangedHandler(int percent, int speed, int time);
public event ProcessChangedHandler? ProcessChanged;
public delegate void PrintInfoHandler(string info);
public event PrintInfoHandler? PrintInfo;
public delegate void ErrorHandler(Exception exception);
public event ErrorHandler? OnError;
public FileTransferClient(BusDevice _device) => device = _device;
public static int GetVersionMajor()
{
return typeof(FileTransferClient).Assembly.GetName().Version?.Major ?? -1;
}
public static int GetVersionMinor()
{
return typeof(FileTransferClient).Assembly.GetName().Version?.Minor ?? -1;
}
public static int GetVersionBuild()
{
return typeof(FileTransferClient).Assembly.GetName().Version?.Build ?? -1;
}
private long procSize = 0;
private long procPos = 0;
private DateTime procTime;
private List<int> procSpeed = new List<int>();
private void HandleProcess(int length)
{
procPos += length;
int perc = (int)Math.Floor((procPos*100) / (double)procSize);
double time = (DateTime.Now - procTime).TotalMilliseconds;
procTime = DateTime.Now;
int speed = (int)Math.Floor(length / (time / 1000));
procSpeed.Add(speed);
if(procSpeed.Count > 20)
procSpeed.RemoveAt(0);
int x = 0;
foreach(int s in procSpeed)
x += s;
x = (int)(x / procSpeed.Count);
int left = (int)Math.Floor((procSize - procPos) / (double)x);
ProcessChanged?.Invoke(perc, x, left);
}
public async Task<bool> CheckFeature(FtmFeatures feature)
{
try
{
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.CheckFeatures);
return res != null && (res.Data[0] & (byte)feature) != 0;
} catch {
// No response means no feature
return false;
}
}
public async Task<SemanticVersion> CheckVersion()
{
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.GetVersion);
if(res == null)
throw new Exception("No response for Version Request");
int major = BitConverter.ToInt16(new byte[] { res.Data[1], res.Data[0]});
int minor = BitConverter.ToInt16(new byte[] { res.Data[3], res.Data[2]});
int build = BitConverter.ToInt16(new byte[] { res.Data[5], res.Data[4] });
SemanticVersion version = new SemanticVersion(major, minor, build);
if(major != GetVersionMajor())
throw new Exception("Incompatible Remote MajorVersion: " + version);
return version;
}
public async Task Format()
{
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.Format);
if(res == null)
throw new Exception("No response for Format Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
}
public async Task<bool> Exists(string path, bool force)
{
byte[] buffer = UTF8Encoding.UTF8.GetBytes(path + char.MinValue);
if(!force && device.MaxFrameLength < buffer.Length + 2)
throw new Exception($"The Path is to long ({buffer.Length + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.Exists, buffer);
if(res == null)
throw new Exception("No response for Exists Request");
if(res.Data[0] == 0x00)
return res.Data[1] == 0x01;
throw new FileTransferException(res.Data[0]);
}
public async Task Rename(string path, string newpath, bool force)
{
List<byte> data = new List<byte>();
data.AddRange(UTF8Encoding.UTF8.GetBytes(path + char.MinValue));
data.AddRange(UTF8Encoding.UTF8.GetBytes(newpath + char.MinValue));
if(!force && device.MaxFrameLength < data.Count + 2)
throw new Exception($"Both Paths are to long ({data.Count + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.Rename, data.ToArray());
if(res == null)
throw new Exception("No response for Rename Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
}
public async Task FileUpload(string path, byte[] file, int length, short start_sequence, bool force)
{
using(MemoryStream stream = new MemoryStream(file))
await FileUpload(path, stream, length, start_sequence, force);
}
public async Task FileUpload(string local, string host, int length, short start_sequence, bool force)
{
using(FileStream stream = File.Open(local, FileMode.Open))
await FileUpload(host, stream, length, start_sequence, force);
}
public async Task<FileInfo> FileInfo(string path, bool force)
{
byte[] buffer = UTF8Encoding.UTF8.GetBytes(path + char.MinValue);
if(!force && device.MaxFrameLength < buffer.Length + 2)
throw new Exception($"The Path is to long ({buffer.Length + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileInfo, buffer);
if(res == null)
throw new Exception("No response for FileInfo Request");
if(res.Data[0] == 0x00)
{
int size = BitConverter.ToInt32(res.Data.Skip(1).Take(4).Reverse().ToArray(), 0);
FileInfo info = new FileInfo(size, res.Data.Skip(5).Take(4).ToArray());
return info;
}
else
throw new FileTransferException(res.Data[0]);
}
public async Task FileUpload(string path, Stream stream, int length, short start_sequence, bool force)
{
Stopwatch sw = new Stopwatch();
sw.Start();
procSpeed.Clear();
procSize = stream.Length;
procPos = 0;
procTime = DateTime.Now;
short sequence = 0;
int payloadSize = length - packageOverhead;
//Console.WriteLine($"overhead: {packageOverhead}, payload: {payloadSize}");
int maxCounter = (int)Math.Ceiling((double)stream.Length / payloadSize);
int minNeeded = (int)Math.Ceiling((double)stream.Length / 0xFFFF);
if(maxCounter > 0xFFFF)
throw new Exception($"File can not be transfered with the given pkg size (min {minNeeded + 6}; is {length})");
bool canResume = await CheckFeature(FileTransferClient.FtmFeatures.Resume);
SemanticVersion version = await CheckVersion();
List<byte> data = new List<byte>();
data.AddRange(BitConverter.GetBytes(sequence));
if (version <= new SemanticVersion(0, 1, 4))
data.Add((byte)(length - 3));
else
data.Add((byte)(length - packageOverhead));
if(canResume)
{
data.Add((byte)(start_sequence != 1 ? 1 : 0));
}
data.AddRange(UTF8Encoding.UTF8.GetBytes(path + char.MinValue));
if(!force && device.MaxFrameLength < data.Count + 2)
throw new Exception($"The Path is to long ({data.Count + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileUpload, data.ToArray());
if(res == null)
throw new Exception("No response for FileUpload Request");
sequence = start_sequence;
if (sequence != 1)
{
stream.Seek((sequence - 1) * payloadSize, SeekOrigin.Begin);
procPos = (sequence - 1) * payloadSize;
}
PrintInfo?.Invoke("Dateigröße: " + procSize + " bytes");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
int readed = 0;
int errorCount = 0;
while(true)
{
if(errorCount == 0)
{
byte[] buffer = new byte[payloadSize];
readed = stream.Read(buffer, 0, payloadSize);
if(readed == 0)
break;
data.Clear();
data.AddRange(BitConverter.GetBytes(sequence));
data.Add((byte)readed);
data.AddRange(buffer);
}
try
{
res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileUpload, data.ToArray());
if (res == null)
throw new Exception("No response for FileUpload Request");
if (res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
int respSeq = BitConverter.ToUInt16(new byte[] { res.Data[2], res.Data[1] });
if (respSeq != sequence)
throw new SequenceMissmatchException($"Falsche Sequenz (Req: {sequence:X4} / Res: {respSeq:X4})");
int crcreq = CRC16.Get(data.ToArray());
int crcresp = (res.Data[3] << 8) | res.Data[4];
if (crcreq != crcresp)
throw new Exception($"Falscher CRC (Req: {crcreq:X4} / Res: {crcresp:X4}) [{sequence:X4}]");
}
catch (FileTransferException ex)
{
throw new Exception(ex.Message, ex);
}
catch (SequenceMissmatchException ex)
{
errorCount++;
OnError?.Invoke(ex);
if (errorCount > 3)
throw new Exception("To many errors");
PrintInfo?.Invoke("Warte 3s...");
await Task.Delay(3000);
continue;
}
catch (DeviceNotConnectedException ex)
{
errorCount++;
OnError?.Invoke(ex);
if (errorCount > 3)
throw new Exception("To many errors");
await device.Connect();
continue;
}
catch (InterfaceNotConnectedException ex)
{
errorCount++;
OnError?.Invoke(ex);
if (errorCount > 3)
throw new Exception("To many errors");
PrintInfo?.Invoke("Interface neu verbinden...");
await device.InterfaceReset();
continue;
}
catch (InterfaceException ex)
{
errorCount++;
OnError?.Invoke(ex);
if (errorCount > 3)
throw new Exception("To many errors");
PrintInfo?.Invoke("Warte 3s...");
await Task.Delay(3000);
continue;
}
catch (Exception ex)
{
errorCount++;
OnError?.Invoke(ex);
if (errorCount > 3)
throw new Exception("To many errors");
continue;
}
errorCount = 0;
sequence++;
HandleProcess(readed);
}
// TODO should we really wait for data?
await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileUpload, new byte[] {0xFF, 0xFF});
sw.Stop();
int xspeed = (int)(stream.Length / sw.Elapsed.TotalSeconds);
PrintInfo?.Invoke($"Abgeschlossen in {sw.Elapsed.Minutes}:{sw.Elapsed.Seconds:D2} ({xspeed:D3} bytes/s)");
}
public async Task FileDownload(string path, byte[] file, int length, bool force)
{
using(MemoryStream stream = new MemoryStream(file))
await FileDownload(path, stream, length, force);
}
public async Task FileDownload(string path, string file, int length, bool force)
{
using(FileStream stream = File.Open(file, FileMode.OpenOrCreate))
await FileDownload(path, stream, length, force);
}
public async Task FileDownload(string path, Stream stream, int length, bool force)
{
Stopwatch sw = new Stopwatch();
sw.Start();
procSpeed.Clear();
procPos = 0;
procTime = DateTime.Now;
short sequence = 0;
List<byte> data = new List<byte>();
data.AddRange(BitConverter.GetBytes(sequence));
data.Add((byte)length);
data.AddRange(UTF8Encoding.UTF8.GetBytes(path + char.MinValue));
if(!force && device.MaxFrameLength < data.Count + 2)
throw new Exception($"The Path is to long ({data.Count + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileDownload, data.ToArray());
if(res == null)
throw new Exception("No response for FileDownload Request");
sequence++;
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
procSize = BitConverter.ToInt32(res.Data.Skip(1).Take(4).Reverse().ToArray(), 0);
PrintInfo?.Invoke("Dateigröße: " + procSize + " bytes");
int errorCount = 0;
while(true)
{
try
{
res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileDownload, BitConverter.GetBytes(sequence));
if(res == null)
throw new Exception("No response for FileDownload Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
int crcreq = CRC16.Get(res.Data.Skip(1).Take(res.Data[3] + 3).ToArray());
int crcresp = (res.Data[res.Data.Count() - 2] << 8) | res.Data[res.Data.Count() -1];
if (crcreq != crcresp)
throw new Exception($"Falscher CRC (Req: {crcreq:X4} / Res: {crcresp:X4})");
}
catch(FileTransferException ex)
{
throw new FileTransferException(ex.Message, ex, ex.ErrorCode);
}
catch(Exception ex)
{
errorCount++;
OnError?.Invoke(ex);
if(errorCount > 3)
throw new Exception("To many errors");
continue;
}
sequence++;
stream.Write(res.Data, 4, res.Data.Length - 6);
stream.Flush();
HandleProcess(length - 6);
if(res.Data.Length < length)
{
stream.Flush();
break;
}
}
sw.Stop();
int xspeed = (int)(stream.Length / sw.Elapsed.TotalSeconds);
PrintInfo?.Invoke($"Abgeschlossen in {sw.Elapsed.Minutes}:{sw.Elapsed.Seconds:D2} ({xspeed:D3} bytes/s)");
}
public async Task FileDelete(string path, bool force)
{
byte[] buffer = UTF8Encoding.UTF8.GetBytes(path + char.MinValue);
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.FileDelete, buffer);
if(res == null)
throw new Exception("No response for FileDelete Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
}
public async Task<List<FileTransferPath>> List(string path, bool force)
{
List<FileTransferPath> list = new List<FileTransferPath>();
byte[] data = ASCIIEncoding.ASCII.GetBytes(path + char.MinValue);
if(!force && device.MaxFrameLength < data.Length + 2)
throw new Exception($"The Path is to long ({data.Length + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.DirList, data);
if(res == null)
throw new Exception("No response for List Request");
bool hasData = true;
while(hasData)
{
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
string name = ASCIIEncoding.ASCII.GetString(res.Data.Skip(2).ToArray());
switch(res.Data[1])
{
case 0x00:
hasData = false;
break;
case 0x01:
list.Add(new FileTransferPath(name, true));
break;
case 0x02:
list.Add(new FileTransferPath(name, false));
break;
}
if(hasData)
{
res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.DirList);
if(res == null)
throw new Exception("No response for List Request");
}
}
return list;
}
public async Task DirCreate(string path, bool force)
{
byte[] buffer = UTF8Encoding.UTF8.GetBytes(path + char.MinValue);
if(!force && device.MaxFrameLength < buffer.Length + 2)
throw new Exception($"The Path is to long ({buffer.Length + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.DirCreate, buffer);
if(res == null)
throw new Exception("No response for DirCreate Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
}
public async Task DirDelete(string path, bool force)
{
byte[] buffer = UTF8Encoding.UTF8.GetBytes(path + char.MinValue);
if(!force && device.MaxFrameLength < buffer.Length + 2)
throw new Exception($"The Path is to long ({buffer.Length + 2}) for the MaxAPDU of {device.MaxFrameLength}");
FunctionPropertyStateResponse res = await device.InvokeFunctionProperty(ObjectIndex, (int)FtmCommands.DirDelete, buffer);
if(res == null)
throw new Exception("No response for DirDelete Request");
if(res.Data[0] != 0x00)
throw new FileTransferException(res.Data[0]);
}
}