-
Notifications
You must be signed in to change notification settings - Fork 32
/
Gvcp.cs
1198 lines (1068 loc) · 45.2 KB
/
Gvcp.cs
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using GenICam;
using GigeVision.Core.Enums;
using GigeVision.Core.Exceptions;
using GigeVision.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using GigeVision.Core.Models;
using Converter = GigeVision.Core.Models.Converter;
namespace GigeVision.Core.Services
{
/// <summary>
/// Max GVCP payload = 540 bytes and must be multiple of 32
/// </summary>
public class Gvcp : IGvcp
{
private List<CameraInformation> cameraInfoList;
private string cameraIP = "";
private ushort gvcpRequestID = 1;
private bool isHeartBeatThreadRunning;
private XmlHelper xmlHelper;
/// <summary>
/// Gvcp constructor, initializes camera IP, and try to get register values
/// </summary>
/// <param name="ip"></param>
public Gvcp(string ip)
{
CameraIp = ip;
}
/// <summary>
/// Gvcp constructor, initializes camera IP and socket timeout, and try to get register values
/// </summary>
/// <param name="ip"></param>
/// <param name="socketReadTimeoutInMilliseconds"></param>
public Gvcp(string ip, int socketReadTimeoutInMilliseconds)
{
CameraIp = ip;
ReceiveTimeoutInMilliseconds = socketReadTimeoutInMilliseconds;
}
/// <summary>
/// Default GVCP constructor
/// </summary>
public Gvcp()
{
ReceiveTimeoutInMilliseconds = 1000;
}
/// <summary>
/// The socket read timeout in milliseconds. Set -1 for infinite timeout
/// </summary>
public int ReceiveTimeoutInMilliseconds { get; set; }
/// <summary>
/// Camera IP, whenever changed, library tries to get latest register values
/// </summary>
public string CameraIp
{
get => cameraIP;
set
{
if (ValidateIp(value))
{
if (value != cameraIP)
{
cameraIP = value;
Reconnect();
CameraIpChanged?.Invoke(null, null);
}
}
}
}
/// <summary>
/// Event fired whenever camera IP changed: used to get registers
/// </summary>
public EventHandler CameraIpChanged { get; set; }
public List<ICategory> CategoryDictionary { get; private set; }
/// <summary>
/// Control socket
/// </summary>
public UdpClient ControlSocket { get; private set; }
/// <summary>
/// It can be for any thing, to update fps to check devices
/// </summary>
public EventHandler ElapsedOneSecond { get; set; }
public bool IsKeepingAlive { get; set; }
public bool IsLoadingXml { get; private set; }
public bool IsXmlFileLoaded { get; private set; }
/// <summary>
/// Controlling port of GVCP
/// </summary>
public int PortControl { get; set; }
/// <summary>
/// GVCP Port
/// </summary>
public int PortGvcp { get => 3956; }
/// <summary>
/// If true, heartbeat command will be sent to the devices after regular interval
/// </summary>
#region Status Commands
public Dictionary<string, (IPValue, IRegister)> RegistersDictionary { get; set; }
/// <summary>
/// Check camera status
/// </summary>
/// <param name="ip">IP Camera</param>
/// <returns>Camera Status: Available/InControl or Unavailable</returns>
public async Task<CameraStatus> CheckCameraStatusAsync(string ip)
{
if (ValidateIp(ip))
{
var cameraStatusPacket = await ReadRegisterAsync(ip, GvcpRegister.GevCCP).ConfigureAwait(false);
return cameraStatusPacket.RegisterValue switch
{
0 => CameraStatus.Available,
2 => CameraStatus.InControl,
_ => CameraStatus.UnAvailable,
};
}
else
{
return CameraStatus.UnAvailable;
}
}
/// <summary>
/// Check camera status
/// </summary>
/// <returns>Camera Status: Available/InControl or Unavailable</returns>
public async Task<CameraStatus> CheckCameraStatusAsync()
{
return await CheckCameraStatusAsync(CameraIp).ConfigureAwait(false);
}
/// <summary>
/// Forces the IP of camera to be changed to the given IP
/// </summary>
/// <param name="macAddress">MAC address of the camera</param>
/// <param name="iPToSet">IP of camera that needs to be set</param>
/// <returns>Success Status</returns>
public async Task<bool> ForceIPAsync(byte[] macAddress, string iPToSet, string netmask)
{
var forceIpCommand = new byte[64];
var forceIPCommandHeader = new byte[] { 0x42, 0x01, 0x00, 0x04 };
Array.Copy(forceIPCommandHeader, 0, forceIpCommand, 0, forceIPCommandHeader.Length);//4bytes, TotalLength=4
var packetSize = BitConverter.GetBytes((short)(56));
Array.Reverse(packetSize, 0, 2);
Array.Copy(packetSize, 0, forceIpCommand, 4, packetSize.Length);//2bytes, TotalLength=6
int packetId = 12;
var packetIdBytes = BitConverter.GetBytes((short)(packetId));
Array.Reverse(packetIdBytes, 0, 2);
Array.Copy(packetIdBytes, 0, forceIpCommand, 6, packetIdBytes.Length);//2bytes, TotalLength=8
Array.Copy(macAddress, 0, forceIpCommand, 10, macAddress.Length);//6bytes, TotalLength = (2 Reserved bytes) + (6 bytes Mac Address) + (12 reserved bytes) = 28
var ipBytes = BitConverter.GetBytes(Converter.ConvertIpToNumber(iPToSet));
Array.Reverse(ipBytes, 0, 4);
Array.Copy(ipBytes, 0, forceIpCommand, 28, 4);//4bytes, TotalLength= 32 + (12 reserved bytes) = 44
var maskBytes = BitConverter.GetBytes(Converter.ConvertIpToNumber(netmask));
Array.Reverse(maskBytes, 0, 4);
Array.Copy(maskBytes, 0, forceIpCommand, 44, 4);//4bytes, TotalLength= 48 + (12 reserved bytes) = 60
var gateWayBytes = new byte[4];
Array.Copy(ipBytes, 0, gateWayBytes, 0, 4);
gateWayBytes[3] = 0x01;
Array.Copy(gateWayBytes, 0, forceIpCommand, 60, 4);//4bytes, TotalLength= 64
var broadCast = SendBroadCastPacket(forceIpCommand, ParseReply);
bool status = false;
void ParseReply(IPEndPoint endPoint, byte[] data)
{
if (data?.Length > 5)
{
if (data[3] == 0x05 && data[0] == 0 && data[1] == 0) //ForceIp acknowledgment
{
status = true;
}
}
}
await broadCast;
return status;
}
/// <summary>
/// Forces the IP to the camera
/// </summary>
/// <param name="macAddress">Mac Address of Camera</param>
/// <param name="iPToSet">IP to set</param>
/// <returns></returns>
public async Task<bool> ForceIPAsync(string macAddress, string iPToSet, string netmask)
{
return await ForceIPAsync(Converter.HexStringToByteArray(macAddress), iPToSet, netmask).ConfigureAwait(false);
}
/// <summary>
/// It will get all the devices from the network and then fires the event for updated list
/// </summary>
/// <param name="listUpdated"></param>
public async void GetAllGigeDevicesInNetworkAsnyc(Action<List<CameraInformation>> listUpdated, string networkIP = "")
{
var list = await GetAllGigeDevicesInNetworkAsnyc().ConfigureAwait(false);
listUpdated?.Invoke(list);
}
/// <summary>
/// It will get all the devices from the network and returns the Camera list
/// </summary>
public async Task<List<CameraInformation>> GetAllGigeDevicesInNetworkAsnyc(string networkIP = "")
{
cameraInfoList = new List<CameraInformation>();
GvcpCommand discovery = new(GvcpCommandType.Discovery);
var data = SendBroadCastPacket(discovery.CommandBytes, DiscoveryReception, networkIP);
await data;
return cameraInfoList;
}
public async Task<(IPValue pValue, IRegister register)> GetRegister(string name)
{
(IPValue pValue, IRegister register) tuple = new(null, null);
var category = await xmlHelper.GetRegisterByName(name);
if (category == null)
{
return tuple;
}
if (category.PValue is IPValue pValue)
{
tuple.pValue = pValue;
}
if (category.PValue is IRegister register)
{
tuple.register = register;
}
return tuple;
}
public async Task<ICategory> GetRegisterCategory(string name)
{
return await xmlHelper.GetRegisterByName(name);
}
/// <summary>
/// Leaves to control if in control
/// </summary>
/// <returns>True if leaves the control</returns>
public async Task<bool> LeaveControl()
{
IsKeepingAlive = false;
while (isHeartBeatThreadRunning)
{
await Task.Delay(100).ConfigureAwait(false);
}
int retryCount = 3;
GvcpReply reply;
do
{
reply = await WriteRegisterAsync(GvcpRegister.GevCCP, 0).ConfigureAwait(false);
if (--retryCount == 0)
{
break;
}
}
while (reply.Status != GvcpStatus.GEV_STATUS_SUCCESS);
return true;
}
/// <summary>
/// Reads all register of camera
/// </summary>
/// <param name="cameraIp">Camera IP</param>
/// <returns>Register dictionary</returns>
public async Task ReadAllRegisterAddressFromCameraAsync(string cameraIp)
{
try
{
IsLoadingXml = true;
if (!ValidateIp(cameraIp)) throw new InvalidIpException();
//loading the XML file
XmlDocument xml = new XmlDocument();
xml.Load(await GetXmlFileFromCamera(cameraIp).ConfigureAwait(false));
var xmlHelper = new XmlHelper(xml, new GenPort(this));
await xmlHelper.LoadUp();
CategoryDictionary = xmlHelper.CategoryDictionary;
if (xmlHelper.CategoryDictionary != null)
{
if (xmlHelper.CategoryDictionary.Count > 0)
{
RegistersDictionary = new Dictionary<string, (IPValue, IRegister)>();
await ReadAllRegisters(CategoryDictionary);
}
};
}
catch (Exception ex)
{
throw ex;
}
finally
{
IsLoadingXml = false;
}
}
/// <summary>
/// Reads all register of camera
/// </summary>
/// <returns>Register dictionary</returns>
public async Task ReadAllRegisterAddressFromCameraAsync()
{
await ReadAllRegisterAddressFromCameraAsync(CameraIp);
}
public async Task ReadAllRegisterAddressFromCameraAsync(IGvcp gvcp)
{
await ReadAllRegisterAddressFromCameraAsync(gvcp.CameraIp);
}
public async Task<GvcpReply> ReadMemoryAsync(string ip, byte[] memoryAddress, ushort count)
{
if (ValidateIp(ip))
{
GvcpCommand command = new(memoryAddress, GvcpCommandType.ReadMem, requestID: gvcpRequestID++, count: count);
using UdpClient socket = new();
socket.Client.ReceiveTimeout = ReceiveTimeoutInMilliseconds;
socket.Connect(ip, 3956);
return await SendGvcpCommand(socket, command).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Error = "IP is not valid" };
}
}
public async Task<GvcpReply> ReadMemoryAsync(string memoryAddressOrKey, ushort count)
{
return await ReadMemoryAsync(CameraIp, Converter.RegisterStringToByteArray(memoryAddressOrKey), count).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <param name="ip"></param>
/// <param name="registerAddress"></param>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string ip, byte[] registerAddress)
{
if (ValidateIp(ip))
{
GvcpCommand command = new(registerAddress, GvcpCommandType.ReadReg, requestID: gvcpRequestID++);
using UdpClient socket = new();
socket.Client.ReceiveTimeout = 1000;
socket.Connect(ip, 3956);
return await SendGvcpCommand(socket, command).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Error = "IP is not valid" };
}
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(GvcpRegister register)
{
return await ReadRegisterAsync(Converter.RegisterStringToByteArray(register.ToString("X"))).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string Ip, string registerAddress)
{
return await ReadRegisterAsync(Ip, Converter.RegisterStringToByteArray(registerAddress)).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(byte[] registerAddress)
{
return await ReadRegisterAsync(CameraIp, registerAddress).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string registerAddressOrKey)
{
return await ReadRegisterAsync(CameraIp, Converter.RegisterStringToByteArray(registerAddressOrKey)).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string Ip, string[] registerAddresses)
{
return await ReadRegisterAsync(Ip, Converter.RegisterStringsToByteArray(registerAddresses)).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string[] registerAddresses)
{
return await ReadRegisterAsync(CameraIp, Converter.RegisterStringsToByteArray(registerAddresses)).ConfigureAwait(false);
}
/// <summary>
/// Read Register
/// </summary>
/// <returns>Command Reply</returns>
public async Task<GvcpReply> ReadRegisterAsync(string Ip, GvcpRegister register)
{
return await ReadRegisterAsync(Ip, register.ToString("X")).ConfigureAwait(false);
}
public async Task<bool> ReadXmlFileAsync(string ip = null)
{
if (string.IsNullOrEmpty(ip))
{
ip = CameraIp;
}
else
{
if (ValidateIp(ip) is false)
{
throw new InvalidIpException();
}
}
XmlDocument xml = new XmlDocument();
xml.Load(await GetXmlFileFromCamera(ip).ConfigureAwait(false));
xmlHelper = new XmlHelper(xml, new GenPort(this));
try
{
await xmlHelper.LoadUp();
IsXmlFileLoaded = true;
}
catch (Exception)
{
IsXmlFileLoaded = false;
}
return IsXmlFileLoaded;
}
/// <summary>
/// Takes control of the devices
/// </summary>
/// <param name="KeepAlive">
/// If true thread will continuously send heartbeat command to keep the devices in control
/// </param>
/// <returns>Control Status</returns>
public async Task<bool> TakeControl(bool KeepAlive = true)
{
bool controlStatus = false;
Reconnect();
if (await GetControlAsync(ControlSocket).ConfigureAwait(false))
{
controlStatus = true;
}
else
{
return controlStatus;
}
if (KeepAlive)
{
GvcpReply reply;
int retryCount = 5;
do
{
reply = await WriteRegisterAsync(GvcpRegister.HeartbeatTimeout, 10000).ConfigureAwait(false);
if (--retryCount == 0)
{
break;
}
}
while (reply.Status != GvcpStatus.GEV_STATUS_SUCCESS);
IsKeepingAlive = true;
RunHeartbeatThread();
}
return controlStatus;
}
/// <summary>
/// Write Memory
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteMemoryAsync(string memoryAddress, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(memoryAddress), GvcpCommandType.WriteMem, valueToWrite, gvcpRequestID++);
return await WriteMemory(ControlSocket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(UdpClient socket, byte[] registerAddress, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddress, GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(UdpClient socket, string registerAddress, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(registerAddress), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(UdpClient socket, string[] registerAddress, uint[] valuesToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddress, valuesToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(UdpClient socket, GvcpRegister register, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(register.ToString("X")), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string Ip, byte[] registerAddress, uint valueToWrite)
{
UdpClient socket = new UdpClient(Ip, PortGvcp);
socket.Client.ReceiveTimeout = 1000;
if (await GetControlAsync(socket).ConfigureAwait(false))
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddress, GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Status = GvcpStatus.GEV_STATUS_ACCESS_DENIED };
}
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string Ip, string registerAddress, uint valueToWrite)
{
UdpClient socket = new UdpClient(Ip, PortGvcp);
socket.Client.ReceiveTimeout = 1000;
if (await GetControlAsync(socket).ConfigureAwait(false))
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(registerAddress), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Status = GvcpStatus.GEV_STATUS_ACCESS_DENIED };
}
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string Ip, string[] registerAddress, uint[] valuesToWrite)
{
// await Task.Delay(100).ConfigureAwait(false);
UdpClient socket = new UdpClient(Ip, PortGvcp);
socket.Client.ReceiveTimeout = 1000;
if (await GetControlAsync(socket).ConfigureAwait(false))
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddress, valuesToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Status = GvcpStatus.GEV_STATUS_ACCESS_DENIED };
}
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string Ip, GvcpRegister register, uint valueToWrite)
{
UdpClient socket = new UdpClient(Ip, PortGvcp);
socket.Client.ReceiveTimeout = 1000;
if (await GetControlAsync(socket).ConfigureAwait(false))
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(register.ToString("X")), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(socket, gvcpCommand).ConfigureAwait(false);
}
else
{
return new GvcpReply() { Status = GvcpStatus.GEV_STATUS_ACCESS_DENIED };
}
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(byte[] registerAddressOrKey, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddressOrKey, GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(ControlSocket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string registerAddress, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(registerAddress), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(ControlSocket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(string[] registerAddress, uint[] valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(registerAddress, valueToWrite, gvcpRequestID++);
return await WriteRegister(ControlSocket, gvcpCommand).ConfigureAwait(false);
}
/// <summary>
/// Write Register
/// </summary>
/// <returns>Command Status</returns>
public async Task<GvcpReply> WriteRegisterAsync(GvcpRegister register, uint valueToWrite)
{
GvcpCommand gvcpCommand = new GvcpCommand(Converter.RegisterStringToByteArray(register.ToString("X")), GvcpCommandType.WriteReg, valueToWrite, gvcpRequestID++);
return await WriteRegister(ControlSocket, gvcpCommand).ConfigureAwait(false);
}
private static async Task<GvcpReply> SendGvcpCommand(UdpClient socketTx, GvcpCommand command)
{
GvcpReply gvcpReply = new();
await socketTx.SendAsync(command.CommandBytes, command.Length).ConfigureAwait(false);
gvcpReply.IsSent = true;
Task<UdpReceiveResult> reply = socketTx.ReceiveAsync();
if (await Task.WhenAny(reply, Task.Delay(socketTx.Client.ReceiveTimeout)).ConfigureAwait(false) == reply)
{
gvcpReply.DetectCommand(reply.Result.Buffer);
gvcpReply.IPSender = reply.Result.RemoteEndPoint.Address.ToString();
gvcpReply.PortSender = reply.Result.RemoteEndPoint.Port;
}
return gvcpReply;
}
/// <summary>
/// This function will send UDP packet to the socket (IP/port)
/// </summary>
/// <param name="socket"></param>
/// <param name="inputCommand"></param>
/// <param name="replySize"></param>
/// <returns></returns>
private static async Task<byte[]> SendUdp(UdpClient socket, byte[] inputCommand, bool replyRequired = false, string optionalCommandNameForInformation = "UDP")
{
var reply = Array.Empty<byte>();
var task = new Task(
delegate
{
try
{
socket.Send(inputCommand, inputCommand.Length);
if (replyRequired)
{
var endPointMCU = new IPEndPoint(IPAddress.Any, ((IPEndPoint)socket.Client.RemoteEndPoint).Port);
reply = socket.Receive(ref endPointMCU);
}
}
catch (Exception)
{
reply = null;
}
});
task.Start();
await task.ConfigureAwait(false);
return reply;
}
private static Stream UnZipEncodedZipFile(byte[] encodedZipFile)
{
var xmlZipStream = new MemoryStream(encodedZipFile);
try
{
ZipArchive zipFile = new ZipArchive(xmlZipStream, ZipArchiveMode.Read, false);
Stream unZipFile = new MemoryStream();
foreach (var entry in zipFile.Entries)
{
if (entry.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
unZipFile = entry.Open();
}
}
return unZipFile;
}
catch
{
throw;
}
}
private CameraInformation DecodeDiscoveryPacket(byte[] discoveryPacket)
{
var cameraInfo = new CameraInformation()
{
IP = discoveryPacket[44].ToString() + "." + discoveryPacket[45].ToString() + "." + discoveryPacket[46].ToString() + "." + discoveryPacket[47].ToString()
};
StringBuilder MAC = new StringBuilder(18);
for (int index = 0; index < 6; index++)
{
MAC.AppendFormat("{0:x2}", discoveryPacket[18 + index]);
MAC.Append(":");
}
MAC.Remove(17, 1);//Removing the last colon
Array.Copy(discoveryPacket, 18, cameraInfo.MacAddress, 0, 6);
cameraInfo.MAC = MAC.ToString();
var manufacturer = new byte[32];
Array.Copy(discoveryPacket, 80, manufacturer, 0, 32);
cameraInfo.ManufacturerName = Encoding.ASCII.GetString(manufacturer);
cameraInfo.ManufacturerName = cameraInfo.ManufacturerName.Replace("\0", "");
var model = new byte[32];
Array.Copy(discoveryPacket, 80 + 32, model, 0, 32);
cameraInfo.Model = Encoding.ASCII.GetString(model);
cameraInfo.Model = cameraInfo.Model.Replace("\0", "");
var version = new byte[32];
Array.Copy(discoveryPacket, 80 + 32 + 32, version, 0, 32);
cameraInfo.Version = Encoding.ASCII.GetString(version);
cameraInfo.Version = cameraInfo.Version.Replace("\0", "");
var manufacturerSpecificInfo = new byte[48];
Array.Copy(discoveryPacket, 80 + 32 + 32 + 32, manufacturerSpecificInfo, 0, 48);
cameraInfo.ManufacturerSpecificInformation = Encoding.ASCII.GetString(manufacturerSpecificInfo);
cameraInfo.ManufacturerSpecificInformation = cameraInfo.ManufacturerSpecificInformation.Replace("\0", "");
var serialNumber = new byte[16];
Array.Copy(discoveryPacket, 80 + 32 + 32 + 32 + 48, serialNumber, 0, 16);
cameraInfo.SerialNumber = Encoding.ASCII.GetString(serialNumber);
cameraInfo.SerialNumber = cameraInfo.SerialNumber.Replace("\0", "");
var userDefinedName = new byte[16];
Array.Copy(discoveryPacket, 80 + 32 + 32 + 32 + 48 + 16, userDefinedName, 0, 16);
cameraInfo.UserDefinedName = Encoding.ASCII.GetString(userDefinedName);
cameraInfo.UserDefinedName = cameraInfo.UserDefinedName.Replace("\0", "");
return cameraInfo;
}
/// <summary>
/// Deconds and Add camera in camera list
/// </summary>
/// <param name="localEndpoint">Sender End point</param>
/// <param name="data">Data</param>
private void DiscoveryReception(IPEndPoint localEndpoint, byte[] data)
{
var camera = DecodeDiscoveryPacket(data);
camera.NetworkIP = localEndpoint.Address.ToString();
cameraInfoList.Add(camera);
}
#region Read All Registers Address XML
private async Task<bool> GetControlAsync(UdpClient socket)
{
var currentStatus = await ReadRegisterAsync(Converter.RegisterStringToByteArray(GvcpRegister.GevCCP.ToString("X"))).ConfigureAwait(false);
if (currentStatus.IsValid)
{
if (currentStatus.RegisterValue == 0)//Its free and can be controlled
{
GvcpCommand controlCommand = new(Converter.RegisterStringToByteArray(GvcpRegister.GevCCP.ToString("X")),
GvcpCommandType.WriteReg, 2, gvcpRequestID++);
var reply = await SendGvcpCommand(socket, controlCommand).ConfigureAwait(false);
return reply.Status == GvcpStatus.GEV_STATUS_SUCCESS;
}
}
return false;
}
private (string, int, int) GetFileDetails(string Message)
{
string[] words = Message.Split(';');
string fileName = words[0].Remove(0, 6);
int fileAddress = Int32.Parse(words[1], System.Globalization.NumberStyles.HexNumber);
int fileLength = Int32.Parse(words[2], System.Globalization.NumberStyles.HexNumber);
return (fileName, fileAddress, fileLength);
}
private async Task<(byte[] data, string fileName)> GetRawXmlFileFromCamera(string IP)
{
return await Task.Run(() =>
{
UdpClient client = new UdpClient();
client.Client.ReceiveTimeout = 1000;
//connecting to the server
client.Connect(IP, PortGvcp);
byte[] commandCCP = new byte[] { 0x42, 0x00, 0x00, 0x82, 0x00, 0x08, 0x10, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x02 };
//sending the packet
// client.Send(commandCCP, commandCCP.Length);
Task.Delay(100);
//preparing the header for sending
byte[] gvcpHeader = GetReadMessageHeader(0x0200); //GevFirstURL = 0x0200
//sending the packet
client.Send(gvcpHeader, gvcpHeader.Length);
gvcpRequestID++;
int packetSize = 512 + 24; //512 for the original payload size and 24 for the header
byte[] count = { 0x02, 0x18 }; //Number of bytes to read from device memory it must be multiple of 4 bytes
IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), PortGvcp);
byte[] recivedData = client.Receive(ref server);
if (recivedData.Length < 12) throw new Exception("Access denied");
string localFile = Encoding.ASCII.GetString(recivedData, 12, recivedData.Length - 12);
var (fileName, fileAddress, fileLength) = GetFileDetails(localFile);
//finding the last packet length
var lastPacket = (fileLength % packetSize);
if (lastPacket % 4 != 0)
{
fileLength -= lastPacket;
double tempLastPcaket = ((double)lastPacket / 4);
lastPacket = (int)(Math.Ceiling(tempLastPcaket) * 4);
fileLength += lastPacket;
}
byte[] encodedZipFile = new byte[fileLength];
if (recivedData[0] == (byte)0x00)
{
for (int i = 0; i < fileLength; i += packetSize)
{
count = i == (fileLength - lastPacket) ? BitConverter.GetBytes(lastPacket) : BitConverter.GetBytes(packetSize);
byte[] requestID = BitConverter.GetBytes(gvcpRequestID);
byte[] tempFileAddress = BitConverter.GetBytes(fileAddress + i);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(tempFileAddress);
Array.Reverse(requestID);
Array.Reverse(count);
}
byte[] readFileHeader = { 0x42, 0x01, 0x00, 0x84, 0x00, 0x08,
requestID[0], requestID[1],
tempFileAddress[0], tempFileAddress[1], tempFileAddress[2], tempFileAddress[3],
count[0], count[1], count[2], count[3] };
client.Send(readFileHeader, readFileHeader.Length);
recivedData = client.Receive(ref server);
if (recivedData[0] == (byte)0x00)
{
Array.Copy(recivedData, 12, encodedZipFile, i, recivedData.Length - 12);
gvcpRequestID++;
}
else
break;
}
}
return (encodedZipFile, fileName);
}).ConfigureAwait(false);
}
private byte[] GetReadMessageHeader(int register)
{
//converting register and requestID into bytes
byte[] tempRegister = BitConverter.GetBytes(register);
byte[] requestID = BitConverter.GetBytes((short)gvcpRequestID);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(tempRegister);
Array.Reverse(requestID);
}
//preparing the header for sending
byte[] gvcpHeader = { 0x42, 0x01, 0x00, 0x84, 0x00, 0x08, requestID[0], requestID[1], 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00 };
return gvcpHeader;
}
private async Task<Stream> GetXmlFileFromCamera(string ip)
{
// initializing the variables
//loop to get the zip file data in bytes
Stream xmlFile = new MemoryStream();
(var fileData, var fileName) = await GetRawXmlFileFromCamera(ip).ConfigureAwait(false);
var fileNameParts = fileName.Split('.');
if (fileData.Length != 0)
{
switch (fileNameParts[fileNameParts.Length - 1])
{
case "xml":
xmlFile = new MemoryStream(fileData);
break;
case "zip":
//converting the zip file from bytes to stream
xmlFile = UnZipEncodedZipFile(fileData);
break;
default:
break;
}
}
gvcpRequestID++;
return xmlFile;
}
public async Task SaveXmlFileFromCamera(string path, string ip = null)
{
if (string.IsNullOrEmpty(ip))
{
ip = CameraIp;
}
else
{
if (ValidateIp(ip) is false)
{
throw new InvalidIpException();
}
}
(var fileData, var fileName) = await GetRawXmlFileFromCamera(ip).ConfigureAwait(false);
var filePath = Path.Combine(path, fileName);
try
{
File.WriteAllBytes(filePath, fileData);
}
catch (Exception ex)
{