-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy patharvgvdevice.c
2259 lines (1919 loc) · 89.5 KB
/
arvgvdevice.c
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
/* Aravis - Digital camera library
*
* Copyright © 2009-2025 Emmanuel Pacaud <[email protected]>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Emmanuel Pacaud <[email protected]>
*/
/**
* SECTION: arvgvdevice
* @short_description: GigEVision device
*/
#include <arvgvdeviceprivate.h>
#include <arvdeviceprivate.h>
#include <arvgc.h>
#include <arvgccommand.h>
#include <arvgcboolean.h>
#include <arvgcregisterdescriptionnode.h>
#include <arvdebugprivate.h>
#include <arvgvstreamprivate.h>
#include <arvgvcpprivate.h>
#include <arvgvspprivate.h>
#include <arvnetworkprivate.h>
#include <arvzip.h>
#include <arvstr.h>
#include <arvmiscprivate.h>
#include <arvenumtypes.h>
#include <string.h>
#include <stdlib.h>
/* Shared data (main thread - heartbeat) */
enum
{
PROP_0,
PROP_GV_DEVICE_INTERFACE_ADDRESS,
PROP_GV_DEVICE_DEVICE_ADDRESS,
PROP_GV_DEVICE_PACKET_SIZE_ADJUSTEMENT
};
typedef struct {
GMutex mutex;
guint16 packet_id;
GSocket *socket;
GSocketAddress *interface_address;
GSocketAddress *device_address;
GPollFD poll_in_event;
void *buffer;
unsigned int gvcp_n_retries;
unsigned int gvcp_timeout_ms;
gboolean is_controller;
} ArvGvDeviceIOData;
typedef struct {
GInetAddress *interface_address;
GInetAddress *device_address;
ArvGvDeviceIOData *io_data;
void *heartbeat_thread;
void *heartbeat_data;
ArvGc *genicam;
char *genicam_xml;
size_t genicam_xml_size;
gboolean is_big_endian_device;
gboolean is_packet_resend_supported;
gboolean is_write_memory_supported;
ArvGvStreamOption stream_options;
ArvGvPacketSizeAdjustment packet_size_adjustment;
gboolean first_stream_created;
gboolean init_success;
} ArvGvDevicePrivate ;
struct _ArvGvDevice {
ArvDevice device;
};
struct _ArvGvDeviceClass {
ArvDeviceClass parent_class;
};
G_DEFINE_TYPE_WITH_CODE (ArvGvDevice, arv_gv_device, ARV_TYPE_DEVICE, G_ADD_PRIVATE (ArvGvDevice))
static ArvDeviceError
arv_gvcp_error_to_device_error (ArvGvcpError code)
{
switch (code) {
case ARV_GVCP_ERROR_NOT_IMPLEMENTED:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_NOT_IMPLEMENTED;
case ARV_GVCP_ERROR_INVALID_PARAMETER:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_INVALID_PARAMETER;
case ARV_GVCP_ERROR_INVALID_ACCESS:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_INVALID_ADDRESS;
case ARV_GVCP_ERROR_WRITE_PROTECT:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_WRITE_PROTECT;
case ARV_GVCP_ERROR_BAD_ALIGNMENT:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_BAD_ALIGNMENT;
case ARV_GVCP_ERROR_ACCESS_DENIED:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_ACCESS_DENIED;
case ARV_GVCP_ERROR_BUSY:
return ARV_DEVICE_ERROR_PROTOCOL_ERROR_BUSY;
default:
break;
}
return ARV_DEVICE_ERROR_PROTOCOL_ERROR;
}
static gboolean
_send_cmd_and_receive_ack (ArvGvDeviceIOData *io_data, ArvGvcpCommand command,
guint64 address, size_t size, void *buffer, GError **error)
{
ArvGvcpCommand expected_ack_command;
ArvGvcpPacket *ack_packet = io_data->buffer;
ArvGvcpPacket *packet;
const char *operation;
size_t packet_size;
size_t ack_size;
unsigned int n_retries = 0;
gboolean success = FALSE;
ArvGvcpError command_error = ARV_GVCP_ERROR_NONE;
int count;
switch (command) {
case ARV_GVCP_COMMAND_READ_MEMORY_CMD:
operation = "read_memory";
expected_ack_command = ARV_GVCP_COMMAND_READ_MEMORY_ACK;
ack_size = arv_gvcp_packet_get_read_memory_ack_size (size);
break;
case ARV_GVCP_COMMAND_WRITE_MEMORY_CMD:
operation = "write_memory";
expected_ack_command = ARV_GVCP_COMMAND_WRITE_MEMORY_ACK;
ack_size = arv_gvcp_packet_get_write_memory_ack_size ();
break;
case ARV_GVCP_COMMAND_READ_REGISTER_CMD:
operation = "read_register";
expected_ack_command = ARV_GVCP_COMMAND_READ_REGISTER_ACK;
ack_size = arv_gvcp_packet_get_read_register_ack_size ();
break;
case ARV_GVCP_COMMAND_WRITE_REGISTER_CMD:
operation = "write_register";
expected_ack_command = ARV_GVCP_COMMAND_WRITE_REGISTER_ACK;
ack_size = arv_gvcp_packet_get_write_register_ack_size ();
break;
default:
g_assert_not_reached ();
}
g_return_val_if_fail (ack_size <= ARV_GV_DEVICE_BUFFER_SIZE, FALSE);
g_mutex_lock (&io_data->mutex);
io_data->packet_id = arv_gvcp_next_packet_id (io_data->packet_id);
switch (command) {
case ARV_GVCP_COMMAND_READ_MEMORY_CMD:
packet = arv_gvcp_packet_new_read_memory_cmd (address, size,
io_data->packet_id, &packet_size);
break;
case ARV_GVCP_COMMAND_WRITE_MEMORY_CMD:
packet = arv_gvcp_packet_new_write_memory_cmd (address, size, buffer,
io_data->packet_id, &packet_size);
break;
case ARV_GVCP_COMMAND_READ_REGISTER_CMD:
packet = arv_gvcp_packet_new_read_register_cmd (address,
io_data->packet_id, &packet_size);
break;
case ARV_GVCP_COMMAND_WRITE_REGISTER_CMD:
packet = arv_gvcp_packet_new_write_register_cmd (address, *((guint32 *) buffer),
io_data->packet_id, &packet_size);
break;
default:
g_assert_not_reached ();
}
do {
GError *local_error = NULL;
arv_gvcp_packet_debug (packet, ARV_DEBUG_LEVEL_TRACE);
success = g_socket_send_to (io_data->socket, io_data->device_address,
(const char *) packet, packet_size,
NULL, &local_error) >= 0;
if (success) {
gint timeout_ms;
gint64 timeout_stop_ms;
gboolean pending_ack;
gboolean expected_answer;
timeout_stop_ms = g_get_monotonic_time () / 1000 + io_data->gvcp_timeout_ms;
do {
pending_ack = FALSE;
timeout_ms = timeout_stop_ms - g_get_monotonic_time () / 1000;
if (timeout_ms < 0)
timeout_ms = 0;
success = TRUE;
success = success && g_poll (&io_data->poll_in_event, 1, timeout_ms) > 0;
if (success) {
arv_gpollfd_clear_one (&io_data->poll_in_event, io_data->socket);
count = g_socket_receive (io_data->socket, io_data->buffer,
ARV_GV_DEVICE_BUFFER_SIZE, NULL, &local_error);
} else
count = 0;
success = success && (count >= sizeof (ArvGvcpHeader));
if (success) {
ArvGvcpPacketType packet_type;
ArvGvcpCommand ack_command;
guint16 packet_id;
arv_gvcp_packet_debug (ack_packet, ARV_DEBUG_LEVEL_TRACE);
packet_type = arv_gvcp_packet_get_packet_type (ack_packet, count);
ack_command = arv_gvcp_packet_get_command (ack_packet, count);
packet_id = arv_gvcp_packet_get_packet_id (ack_packet, count);
if (ack_command == ARV_GVCP_COMMAND_PENDING_ACK &&
count >= arv_gvcp_packet_get_pending_ack_size ()) {
gint64 pending_ack_timeout_ms =
arv_gvcp_packet_get_pending_ack_timeout (ack_packet, count);
pending_ack = TRUE;
expected_answer = FALSE;
timeout_stop_ms = g_get_monotonic_time () / 1000 + pending_ack_timeout_ms;
arv_debug_device ("[GvDevice::%s] Pending ack timeout = %"
G_GINT64_FORMAT,
operation, pending_ack_timeout_ms);
} else if (packet_type == ARV_GVCP_PACKET_TYPE_ERROR ||
packet_type == ARV_GVCP_PACKET_TYPE_UNKNOWN_ERROR) {
expected_answer = ack_command == expected_ack_command &&
packet_id == io_data->packet_id;
if (!expected_answer) {
arv_info_device ("[GvDevice::%s] Unexpected answer (0x%02x)",
operation, packet_type);
} else
command_error = arv_gvcp_packet_get_packet_flags (ack_packet,
count);
} else {
expected_answer = packet_type == ARV_GVCP_PACKET_TYPE_ACK &&
ack_command == expected_ack_command &&
packet_id == io_data->packet_id &&
count >= ack_size;
if (!expected_answer) {
arv_info_device ("[GvDevice::%s] Unexpected answer (0x%02x)",
operation, packet_type);
}
}
} else {
expected_answer = FALSE;
if (local_error != NULL)
arv_warning_device ("[GvDevice::%s] Ack reception error: %s", operation,
local_error->message);
else
arv_warning_device ("[GvDevice::%s] Ack reception timeout", operation);
g_clear_error (&local_error);
}
} while (pending_ack || (!expected_answer && timeout_ms > 0));
success = success && expected_answer;
if (success && command_error == ARV_GVCP_ERROR_NONE) {
switch (command) {
case ARV_GVCP_COMMAND_READ_MEMORY_CMD:
memcpy (buffer,
arv_gvcp_packet_get_read_memory_ack_data (ack_packet),
size);
break;
case ARV_GVCP_COMMAND_WRITE_MEMORY_CMD:
break;
case ARV_GVCP_COMMAND_READ_REGISTER_CMD:
*((gint32 *) buffer) =
arv_gvcp_packet_get_read_register_ack_value (ack_packet, count);
break;
case ARV_GVCP_COMMAND_WRITE_REGISTER_CMD:
break;
default:
g_assert_not_reached ();
}
}
} else {
if (local_error != NULL)
arv_warning_device ("[GvDevice::%s] Command sending error: %s", operation, local_error->message);
g_clear_error (&local_error);
}
n_retries++;
} while (!success && n_retries < io_data->gvcp_n_retries);
arv_gvcp_packet_free (packet);
g_mutex_unlock (&io_data->mutex);
success = success && command_error == ARV_GVCP_ERROR_NONE;
if (!success) {
switch (command) {
case ARV_GVCP_COMMAND_READ_MEMORY_CMD:
memset (buffer, 0, size);
break;
case ARV_GVCP_COMMAND_WRITE_MEMORY_CMD:
break;
case ARV_GVCP_COMMAND_READ_REGISTER_CMD:
*((guint32 *) buffer) = 0;
break;
case ARV_GVCP_COMMAND_WRITE_REGISTER_CMD:
break;
default:
g_assert_not_reached ();
}
if (command_error != ARV_GVCP_ERROR_NONE)
g_set_error (error, ARV_DEVICE_ERROR, arv_gvcp_error_to_device_error (command_error),
"GigEVision %s error (%s)", operation,
arv_gvcp_error_to_string (command_error));
else
g_set_error (error, ARV_DEVICE_ERROR, ARV_DEVICE_ERROR_TIMEOUT,
"GigEVision %s timeout", operation);
}
return success;
}
static gboolean
_read_memory (ArvGvDeviceIOData *io_data, guint64 address, guint32 size, void *buffer, GError **error)
{
return _send_cmd_and_receive_ack (io_data, ARV_GVCP_COMMAND_READ_MEMORY_CMD,
address, size, buffer, error);
}
static gboolean
_write_memory (ArvGvDeviceIOData *io_data, guint64 address, guint32 size, void *buffer, GError **error)
{
return _send_cmd_and_receive_ack (io_data, ARV_GVCP_COMMAND_WRITE_MEMORY_CMD,
address, size, buffer, error);
}
static gboolean
_read_register (ArvGvDeviceIOData *io_data, guint32 address, guint32 *value_placeholder, GError **error)
{
return _send_cmd_and_receive_ack (io_data, ARV_GVCP_COMMAND_READ_REGISTER_CMD,
address, sizeof (guint32), value_placeholder, error);
}
static gboolean
_write_register (ArvGvDeviceIOData *io_data, guint32 address, guint32 value, GError **error)
{
return _send_cmd_and_receive_ack (io_data, ARV_GVCP_COMMAND_WRITE_REGISTER_CMD,
address, sizeof (guint32), &value, error);
}
static gboolean
arv_gv_device_read_memory (ArvDevice *device, guint64 address, guint32 size, void *buffer, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (ARV_GV_DEVICE (device));
int i;
gint32 block_size;
for (i = 0; i < (size + ARV_GVCP_DATA_SIZE_MAX - 1) / ARV_GVCP_DATA_SIZE_MAX; i++) {
block_size = MIN (ARV_GVCP_DATA_SIZE_MAX, size - i * ARV_GVCP_DATA_SIZE_MAX);
if (!_read_memory (priv->io_data,
address + i * ARV_GVCP_DATA_SIZE_MAX,
block_size, ((char *) buffer) + i * ARV_GVCP_DATA_SIZE_MAX, error))
return FALSE;
}
return TRUE;
}
static gboolean
arv_gv_device_write_memory (ArvDevice *device, guint64 address, guint32 size, const void *buffer, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (ARV_GV_DEVICE (device));
int i;
gint32 block_size;
for (i = 0; i < (size + ARV_GVCP_DATA_SIZE_MAX - 1) / ARV_GVCP_DATA_SIZE_MAX; i++) {
block_size = MIN (ARV_GVCP_DATA_SIZE_MAX, size - i * ARV_GVCP_DATA_SIZE_MAX);
if (!_write_memory (priv->io_data,
address + i * ARV_GVCP_DATA_SIZE_MAX,
block_size, ((char *) buffer) + i * ARV_GVCP_DATA_SIZE_MAX, error))
return FALSE;
}
return TRUE;
}
static gboolean
arv_gv_device_read_register (ArvDevice *device, guint64 address, guint32 *value, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (ARV_GV_DEVICE (device));
return _read_register (priv->io_data, address, value, error);
}
static gboolean
arv_gv_device_write_register (ArvDevice *device, guint64 address, guint32 value, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (ARV_GV_DEVICE (device));
return _write_register (priv->io_data, address, value, error);
}
/* Heartbeat thread */
typedef struct {
ArvGvDevice *gv_device;
ArvGvDeviceIOData *io_data;
int period_us;
GCancellable *cancellable;
} ArvGvDeviceHeartbeatData;
static void *
arv_gv_device_heartbeat_thread (void *data)
{
ArvGvDeviceHeartbeatData *thread_data = data;
ArvGvDeviceIOData *io_data = thread_data->io_data;
GPollFD poll_fd;
gboolean use_poll;
GTimer *timer;
guint32 value;
timer = g_timer_new ();
use_poll = g_cancellable_make_pollfd (thread_data->cancellable, &poll_fd);
do {
if (use_poll)
g_poll (&poll_fd, 1, thread_data->period_us / 1000);
else
g_usleep (thread_data->period_us);
if (io_data->is_controller) {
guint counter = 1;
/* TODO: Instead of reading the control register, Pylon does write the heartbeat
* timeout value, which is interresting, as doing this we could get an error
* ack packet which will indicate we lost the control access. */
g_timer_start (timer);
while (!_read_register (io_data, ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_OFFSET, &value, NULL) &&
g_timer_elapsed (timer, NULL) < ARV_GV_DEVICE_HEARTBEAT_RETRY_TIMEOUT_S &&
!g_cancellable_is_cancelled (thread_data->cancellable)) {
g_usleep (ARV_GV_DEVICE_HEARTBEAT_RETRY_DELAY_US);
counter++;
}
if (!g_cancellable_is_cancelled (thread_data->cancellable)) {
arv_debug_device ("[GvDevice::Heartbeat] Ack value = %d", value);
if (counter > 1)
arv_debug_device ("[GvDevice::Heartbeat] Tried %u times", counter);
if ((value & (ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_CONTROL |
ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_EXCLUSIVE)) == 0) {
arv_warning_device ("[GvDevice::Heartbeat] Control access lost");
arv_device_emit_control_lost_signal (ARV_DEVICE (thread_data->gv_device));
io_data->is_controller = FALSE;
}
} else
io_data->is_controller = FALSE;
}
} while (!g_cancellable_is_cancelled (thread_data->cancellable));
if (use_poll)
g_cancellable_release_fd (thread_data->cancellable);
g_timer_destroy (timer);
return NULL;
}
/* ArvGvDevice implemenation */
/**
* arv_gv_device_take_control:
* @gv_device: a #ArvGvDevice
* @error: a #GError placeholder, %NULL to ignore
*
* Returns: whether the control was successfully acquired
*
* Since: 0.8.3
*/
gboolean
arv_gv_device_take_control (ArvGvDevice *gv_device, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (gv_device);
gboolean success = TRUE;
success = arv_gv_device_write_register (ARV_DEVICE (gv_device),
ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_OFFSET,
ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_CONTROL,
error);
if (success)
priv->io_data->is_controller = TRUE;
else
arv_warning_device ("[GvDevice::take_control] Can't get control access");
return success;
}
/**
* arv_gv_device_leave_control:
* @gv_device: a #ArvGvDevice
* @error: a #GError placeholder, %NULL to ignore
*
* Returns: whether the control was successfully relinquished
*
* Since: 0.8.3
*/
gboolean
arv_gv_device_leave_control (ArvGvDevice *gv_device, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (gv_device);
gboolean success = TRUE;
success = arv_gv_device_write_register (ARV_DEVICE (gv_device),
ARV_GVBS_CONTROL_CHANNEL_PRIVILEGE_OFFSET,
0,
error);
if (success)
priv->io_data->is_controller = FALSE;
else
arv_warning_device ("[GvDevice::leave_control] Can't relinquish control access");
return success;
}
guint64
arv_gv_device_get_timestamp_tick_frequency (ArvGvDevice *gv_device, GError **error)
{
GError *local_error = NULL;
guint32 timestamp_tick_frequency_high;
guint32 timestamp_tick_frequency_low;
guint64 timestamp_tick_frequency;
g_return_val_if_fail (ARV_IS_GV_DEVICE (gv_device), 0);
arv_gv_device_read_register (ARV_DEVICE (gv_device),
ARV_GVBS_TIMESTAMP_TICK_FREQUENCY_HIGH_OFFSET,
×tamp_tick_frequency_high, &local_error);
if (local_error == NULL)
arv_gv_device_read_register (ARV_DEVICE (gv_device),
ARV_GVBS_TIMESTAMP_TICK_FREQUENCY_LOW_OFFSET,
×tamp_tick_frequency_low, &local_error);
if (local_error != NULL) {
g_propagate_error (error, local_error);
return 0;
}
timestamp_tick_frequency = ((guint64) timestamp_tick_frequency_high << 32) |
timestamp_tick_frequency_low;
return timestamp_tick_frequency;
}
guint
arv_gv_device_get_packet_size (ArvGvDevice *gv_device, GError **error)
{
return arv_device_get_integer_feature_value (ARV_DEVICE (gv_device), "ArvGevSCPSPacketSize", error);
}
void
arv_gv_device_set_packet_size (ArvGvDevice *gv_device, gint packet_size, GError **error)
{
g_return_if_fail (packet_size > 0);
arv_device_set_integer_feature_value (ARV_DEVICE (gv_device), "ArvGevSCPSPacketSize", packet_size, error);
}
static gboolean
test_packet_check (ArvDevice *device,
GPollFD *poll_fd,
GSocket *socket,
char *buffer,
guint max_size,
guint packet_size)
{
GError *error = NULL;
unsigned n_tries = 0;
int n_events;
size_t read_count;
do {
arv_device_execute_command (device, "ArvGevSCPSFireTestPacket", &error);
if (error != NULL) {
arv_warning_device("Test packet check fire failed (%s)", error->message);
g_clear_error(&error);
}
do {
n_events = g_poll (poll_fd, 1, 10);
if (n_events != 0) {
arv_gpollfd_clear_one (poll_fd, socket);
read_count = g_socket_receive (socket, buffer, max_size, NULL, NULL);
}
else
read_count = 0;
/* Discard late packets, read_count should be equal to packet size minus IP and UDP headers */
} while (n_events != 0 && read_count != (packet_size - ARV_GVSP_PACKET_UDP_OVERHEAD));
n_tries++;
} while (n_events == 0 && n_tries < 3);
return n_events != 0;
}
static guint
auto_packet_size (ArvGvDevice *gv_device, gboolean exit_early, GError **error)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (gv_device);
ArvDevice *device = ARV_DEVICE (gv_device);
ArvGcNode *node;
GSocket *socket;
GInetAddress *interface_address;
GSocketAddress *interface_socket_address;
GInetSocketAddress *local_address;
GPollFD poll_fd;
const guint8 *address_bytes;
guint16 port;
gboolean do_not_fragment;
guint max_size, min_size;
gint64 minimum, maximum, packet_size;
guint inc;
char *buffer;
guint last_size = 0;
gboolean success;
g_return_val_if_fail (ARV_IS_GV_DEVICE (gv_device), 1500);
node = arv_device_get_feature (device, "GevSCPSFireTestPacket");
if (!ARV_IS_GC_COMMAND (node) && !ARV_IS_GC_BOOLEAN (node)) {
arv_info_device ("[GvDevice::auto_packet_size] No GevSCPSFireTestPacket feature found");
return arv_device_get_integer_feature_value (device, "ArvGevSCPSPacketSize", error);
}
packet_size = arv_device_get_integer_feature_value (device, "ArvGevSCPSPacketSize", NULL);
/* PacketSize boundaries registers are device specific. Use the standard feature name for finding boundaries. If
* this feature is not present in the device Genicam data, it will fallback to the default definition inserted
* in arv_gv_device_load_genicam */
arv_device_get_integer_feature_bounds (device, "GevSCPSPacketSize", &minimum, &maximum, NULL);
inc = arv_device_get_integer_feature_increment (device, "GevSCPSPacketSize", NULL);
if (inc < 1)
inc = 1;
max_size = MIN (ARV_GVSP_MAXIMUM_PACKET_SIZE, maximum);
min_size = MAX (ARV_GVSP_MINIMUM_PACKET_SIZE, minimum);
if (max_size < min_size ||
inc > max_size - min_size) {
arv_warning_device ("[GvDevice::auto_packet_size] Invalid ArvGevSCPSPacketSize properties");
return arv_device_get_integer_feature_value (device, "ArvGevSCPSPacketSize", error);
}
interface_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (priv->io_data->interface_address));
socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL);
interface_socket_address = arv_socket_bind_with_range (socket, interface_address, 0, FALSE, NULL);
local_address = G_INET_SOCKET_ADDRESS (g_socket_get_local_address (socket, NULL));
port = g_inet_socket_address_get_port (local_address);
address_bytes = g_inet_address_to_bytes (interface_address);
arv_device_set_integer_feature_value (ARV_DEVICE (gv_device), "ArvGevSCDA", g_htonl (*((guint32 *) address_bytes)), NULL);
arv_device_set_integer_feature_value (ARV_DEVICE (gv_device), "ArvGevSCPHostPort", port, NULL);
g_clear_object (&local_address);
g_clear_object (&interface_socket_address);
do_not_fragment = arv_device_get_boolean_feature_value (device, "ArvGevSCPSDoNotFragment", NULL);
arv_device_set_boolean_feature_value (device, "ArvGevSCPSDoNotFragment", TRUE, NULL);
poll_fd.fd = g_socket_get_fd (socket);
poll_fd.events = G_IO_IN;
poll_fd.revents = 0;
arv_gpollfd_prepare_all (&poll_fd, 1);
buffer = g_malloc (max_size);
success = test_packet_check (device, &poll_fd, socket, buffer, max_size, packet_size);
/* When exit_early is set, the function only checks the current packet size is working.
* If not, the full automatic packet size adjustment is run. */
if (success && exit_early) {
arv_info_device ("[GvDevice::auto_packet_size] Current packet size check successfull "
"(%" G_GINT64_FORMAT " bytes)",
packet_size);
} else {
GError *local_error = NULL;
guint current_size = packet_size;
do {
if (current_size == last_size ||
min_size + inc > max_size)
break;
last_size = current_size;
arv_device_set_integer_feature_value (device, "ArvGevSCPSPacketSize", current_size, NULL);
current_size = arv_device_get_integer_feature_value (device, "ArvGevSCPSPacketSize", &local_error);
if (local_error != NULL)
break;
arv_info_device ("[GvDevice::auto_packet_size] Try packet size = %d (%d - min: %d - max: %d - inc: %d)",
current_size, last_size, min_size, max_size, inc);
success = test_packet_check (device, &poll_fd, socket, buffer, max_size, current_size);
if (success) {
packet_size = current_size;
if (current_size == max_size)
break;
min_size = current_size;
} else {
max_size = current_size;
}
current_size = min_size + (((max_size - min_size) / 2) / inc) * inc;
} while (TRUE);
if (local_error == NULL) {
arv_device_set_integer_feature_value (device, "ArvGevSCPSPacketSize", packet_size, error);
arv_info_device ("[GvDevice::auto_packet_size] Packet size set to %" G_GINT64_FORMAT " bytes",
packet_size);
} else {
g_propagate_error (error, local_error);
}
}
g_clear_pointer (&buffer, g_free);
g_clear_object (&socket);
arv_gpollfd_finish_all (&poll_fd, 1);
arv_device_set_boolean_feature_value (device, "ArvGevSCPSDoNotFragment", do_not_fragment, NULL);
return packet_size;
}
/**
* arv_gv_device_auto_packet_size:
* @gv_device: a #ArvGvDevice
* @error: a #GError placeholder, %NULL to ignore
*
* Automatically determine the biggest packet size that can be used data streaming, and set ArvGevSCPSPacketSize value
* accordingly. This function relies on the GevSCPSFireTestPacket feature.
*
* Returns: The automatic packet size, in bytes, or the current one if GevSCPSFireTestPacket is not supported.
*
* Since: 0.6.0
*/
guint
arv_gv_device_auto_packet_size (ArvGvDevice *gv_device, GError **error)
{
return auto_packet_size (gv_device, FALSE, error);
}
/**
* arv_gv_device_set_packet_size_adjustment:
* @gv_device: a #ArvGvDevice
* @adjustment: a #ArvGvPacketSizeAdjustment option
*
* Sets the option for the packet size adjustment happening at stream object creation. See
* arv_gv_device_auto_packet_size() for a description of the packet adjustment feature. The default behaviour is
* @ARV_GV_PACKET_SIZE_ADJUSTEMENT_ON_FAILURE_ONCE, which means the packet size is adjusted if the current packet size
* check fails, and only the first time arv_device_create_stream() is successfully called during @gv_device instance
* life.
*
* Since: 0.8.3
*/
void
arv_gv_device_set_packet_size_adjustment (ArvGvDevice *gv_device, ArvGvPacketSizeAdjustment adjustment)
{
ArvGvDevicePrivate *priv = arv_gv_device_get_instance_private (gv_device);
g_return_if_fail (ARV_IS_GV_DEVICE (gv_device));
priv->packet_size_adjustment = adjustment;
}
/**
* arv_gv_device_get_current_ip:
* @gv_device: a #ArvGvDevice
* @ip: (out) (optional): a IP address placeholder
* @mask: (out) (optional): a netmask placeholder
* @gateway: (out) (optional): a gateway IP address placeholder
* @error: a #GError placeholder, %NULL to ignore
*
* Get the current IP address setting of device.
*
* Returns: %TRUE on success
*
* Since: 0.8.22
*/
gboolean
arv_gv_device_get_current_ip (ArvGvDevice *gv_device,
GInetAddress **ip, GInetAddressMask **mask, GInetAddress **gateway,
GError **error)
{
GError *local_error = NULL;
guint32 be_ip_int;
guint32 be_mask_int;
guint32 be_gateway_int;
guint32 value;
GInetAddress *netmask;
g_return_val_if_fail (ARV_IS_GV_DEVICE (gv_device), FALSE);
if (ip != NULL) {
*ip = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevCurrentIPAddress", &local_error);
be_ip_int = g_htonl(value);
}
if (mask != NULL && local_error == NULL) {
*mask = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevCurrentSubnetMask", &local_error);
be_mask_int = g_htonl(value);
}
if (gateway != NULL && local_error == NULL) {
*gateway = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevCurrentDefaultGateway", &local_error);
be_gateway_int = g_htonl(value);
}
if (local_error != NULL) {
g_propagate_error(error, local_error);
return FALSE;
}
if (ip != NULL)
*ip = g_inet_address_new_from_bytes ((guint8 *) &be_ip_int, G_SOCKET_FAMILY_IPV4);
if (mask != NULL) {
netmask = g_inet_address_new_from_bytes ((guint8 *) &be_mask_int, G_SOCKET_FAMILY_IPV4);
*mask = g_inet_address_mask_new (netmask, 32, NULL);
g_object_unref (netmask);
}
if (gateway != NULL)
*gateway = g_inet_address_new_from_bytes ((guint8 *) &be_gateway_int, G_SOCKET_FAMILY_IPV4);
return TRUE;
}
/**
* arv_gv_device_get_persistent_ip:
* @gv_device: a #ArvGvDevice
* @ip: (out): a IP address placeholder
* @mask: (out) (optional): a netmask placeholder
* @gateway: (out) (optional): a gateway IP address placeholder
* @error: a #GError placeholder
*
* Get the persistent IP address setting of device.
*
* Returns: %TRUE on success
*
* Since: 0.8.22
*/
gboolean
arv_gv_device_get_persistent_ip (ArvGvDevice *gv_device,
GInetAddress **ip, GInetAddressMask **mask, GInetAddress **gateway,
GError **error)
{
GError *local_error = NULL;
guint32 be_ip_int;
guint32 be_mask_int;
guint32 be_gateway_int;
guint32 value;
GInetAddress *netmask;
g_return_val_if_fail (ARV_IS_GV_DEVICE (gv_device), FALSE);
if (ip != NULL) {
*ip = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevPersistentIPAddress", &local_error);
be_ip_int = g_htonl(value);
}
if (mask != NULL && local_error == NULL) {
*mask = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevPersistentSubnetMask", &local_error);
be_mask_int = g_htonl(value);
}
if (gateway != NULL && local_error == NULL) {
*gateway = NULL;
value = arv_device_get_integer_feature_value (ARV_DEVICE (gv_device),
"ArvGevPersistentDefaultGateway", &local_error);
be_gateway_int = g_htonl(value);
}
if (local_error != NULL) {
g_propagate_error(error, local_error);
return FALSE;
}
if (ip != NULL)
*ip = g_inet_address_new_from_bytes ((guint8 *) &be_ip_int, G_SOCKET_FAMILY_IPV4);
if (mask != NULL) {
netmask = g_inet_address_new_from_bytes ((guint8 *) &be_mask_int, G_SOCKET_FAMILY_IPV4);
*mask = g_inet_address_mask_new (netmask, 32, NULL);
g_object_unref (netmask);
}
if (gateway != NULL)
*gateway = g_inet_address_new_from_bytes ((guint8 *) &be_gateway_int, G_SOCKET_FAMILY_IPV4);
return TRUE;
}
/**
* arv_gv_device_set_persistent_ip:
* @gv_device: a #ArvGvDevice
* @ip: (nullable): IPv4 address
* @mask: (nullable): Netmask
* @gateway: (nullable): Gateway IPv4 address
* @error: a #GError placeholder
*
* Sets the persistent IP address to device.
* Also disable DHCP then enable persistent IP mode.
*
* Returns: %TRUE on success
*
* Since: 0.8.22
*/
gboolean
arv_gv_device_set_persistent_ip (ArvGvDevice *gv_device,
GInetAddress *ip, GInetAddressMask *mask, GInetAddress *gateway,
GError **error)
{
g_return_val_if_fail (ARV_IS_GV_DEVICE (gv_device), FALSE);
if (G_IS_INET_ADDRESS (ip)) {
GError *local_error = NULL;
const guint8 *ip_bytes;
guint32 be_value;
guint32 ip_int;