Skip to main content

viva_gige/
consts.rs

1//! GigE Vision's vocabulary: GVCP opcodes and header fields, bootstrap
2//! register addresses and bits, and the protocol's limits.
3//!
4//! Public as a namespace on purpose (ADR-0030): the fake camera imports these
5//! rather than keeping copies. A constant is vocabulary; what a register
6//! means, and how a packet is encoded, each side still derives from the
7//! specification on its own.
8
9use std::time::Duration;
10
11/// The key byte every GVCP command starts with.
12pub const GVCP_CMD_KEY: u8 = 0x42;
13/// Size of a GVCP command or acknowledgement header.
14pub const GVCP_HEADER_SIZE: usize = 8;
15/// Command flag bit 0: the sender wants an acknowledgement.
16pub const FLAG_ACK_REQUIRED: u8 = 0x01;
17/// Command flag bit 4 on an event command: 64-bit block ids (GigE Vision 2.0).
18pub const FLAG_EXTENDED_IDS: u8 = 0x10;
19
20/// GVCP control port as defined by the GigE Vision specification (section 7.3).
21pub const PORT: u16 = 3956;
22/// Opcode of the discovery command.
23pub const DISCOVERY_COMMAND: u16 = 0x0002;
24/// Opcode of the discovery acknowledgement.
25pub const DISCOVERY_ACK: u16 = 0x0003;
26/// Opcode of the FORCEIP command.
27pub const FORCEIP_COMMAND: u16 = 0x0004;
28/// Opcode of the FORCEIP acknowledgement.
29pub const FORCEIP_ACK: u16 = 0x0005;
30/// Opcode of `PACKETRESEND_CMD`, which asks for no acknowledgement: the
31/// answer is the packets themselves, on the stream channel. See
32/// [`encode_packet_resend`](crate::encode_packet_resend).
33pub const PACKET_RESEND_COMMAND: u16 = 0x0040;
34/// Flag-byte bit 4 on `PACKETRESEND_CMD`: the payload carries a 64-bit
35/// block id and 32-bit packet ids (GigE Vision 2.0 extended ids). The same
36/// bit means "allow broadcast acknowledge" on `DISCOVERY_CMD`.
37pub const PACKET_RESEND_EXTENDED_IDS: u8 = 0x10;
38/// Opcode of the PENDING_ACK acknowledgement (GigE Vision 1.2, section 18.5).
39///
40/// A device that cannot complete a command within the controller's timeout
41/// answers with this instead of the real acknowledgement, asking for more
42/// time. GenCP models the same mechanism as its own command id `0x0805`
43/// (`viva_gencp::PENDING_ACK_COMMAND`), never as a status (ADR-0020).
44pub const PENDING_ACK: u16 = 0x0089;
45
46// ── Event channel and action commands ───────────────────────────────
47//
48// These four opcodes are the ones a device sends *to* us on the message
49// channel, plus the two we broadcast for actions. They live here rather
50// than in `action`/`message` because keeping every GVCP opcode in one
51// table is what makes a collision visible: `ACTION_COMMAND` was 0x0080 —
52// `READ_REGISTER` — for as long as it had its own private constant.
53// GigE Vision 2.0 section 18; corroborated by Wireshark's `packet-gvcp.c`
54// (`GVCP_ACTION_CMD`, `GVCP_EVENT_CMD`, `GVCP_EVENTDATA_CMD`) and, for the
55// register commands it shadowed, `../aravis/src/arvgvcpprivate.h`.
56
57/// Opcode of the event command sent by a device on the message channel.
58pub const EVENT_COMMAND: u16 = 0x00C0;
59/// Opcode of the acknowledgement a controller returns for an `EVENT_CMD`.
60pub const EVENT_ACK: u16 = 0x00C1;
61/// Opcode of the event command that carries device-specific event data.
62pub const EVENTDATA_COMMAND: u16 = 0x00C2;
63/// Opcode of the acknowledgement returned for an `EVENTDATA_CMD`.
64pub const EVENTDATA_ACK: u16 = 0x00C3;
65/// Opcode of the action command.
66pub const ACTION_COMMAND: u16 = 0x0100;
67/// Opcode of the action acknowledgement.
68pub const ACTION_ACK: u16 = 0x0101;
69
70/// Size of one event entry in an `EVENT_CMD` with 16-bit block IDs.
71pub const EVENT_ENTRY: usize = 16;
72/// Size of one event entry in an `EVENT_CMD` with 64-bit block IDs.
73///
74/// GigE Vision 2.0 extended block IDs, signalled by bit 4 of the GVCP
75/// flags byte.
76pub const EVENT_ENTRY_EXTENDED: usize = 24;
77
78/// Current IP configuration flags register.
79///
80/// Bit 2 = DHCP, bit 1 = persistent IP, bit 0 = LLA.
81pub const CURRENT_IP_CONFIG: u64 = 0x0014;
82
83/// Persistent IP address register (4 bytes at the end of a 16-byte block).
84pub const PERSISTENT_IP_ADDRESS: u64 = 0x064C;
85/// Persistent subnet mask register.
86pub const PERSISTENT_SUBNET_MASK: u64 = 0x065C;
87/// Persistent default gateway register.
88pub const PERSISTENT_DEFAULT_GATEWAY: u64 = 0x066C;
89
90/// Address of the Control Channel Privilege (CCP) register.
91///
92/// A controller must write `CONTROL_PRIVILEGE` to this register before the
93/// device accepts stream configuration or acquisition commands.
94pub const CONTROL_CHANNEL_PRIVILEGE: u64 = 0x0a00;
95/// CCP value claiming exclusive control.
96pub const CCP_CONTROL: u32 = 1 << 1;
97/// CCP value indicating an exclusive owner.
98pub const CCP_EXCLUSIVE: u32 = 1 << 0;
99/// Bits of the CCP register that mean "this application is the controller".
100pub const CCP_CONTROLLER_BITS: u32 = CCP_CONTROL | CCP_EXCLUSIVE;
101/// Primary Application Port: the UDP port of the application holding
102/// control, in the low half, where GVCP Capability bit `0x0800_0000` says
103/// the device has it. Wireshark: `GVCP_PRIMARY_APPLICATION_PORT`.
104pub const PRIMARY_APPLICATION_PORT: u64 = 0x0a04;
105/// Primary Application IP address. Wireshark:
106/// `GVCP_PRIMARY_APPLICATION_IP_ADDRESS`.
107pub const PRIMARY_APPLICATION_IP: u64 = 0x0a14;
108
109/// Heartbeat timeout register (`GevHeartbeatTimeout`), in milliseconds.
110///
111/// A device that has granted control privilege releases it again if it
112/// receives no GVCP command from the controlling application within this
113/// window. GVSP image traffic does not count — a stream can be running at
114/// full rate while the control channel times out underneath it.
115pub const HEARTBEAT_TIMEOUT: u64 = 0x0938;
116
117/// Number of stream channels the device implements
118/// (`GevNumberOfStreamChannels`). Wireshark: `GVCP_NUMBER_OF_STREAM_CHANNELS`.
119pub const NUMBER_OF_STREAM_CHANNELS: u64 = 0x0904;
120/// GVCP Capability: one bit per optional command or feature. Wireshark:
121/// `GVCP_CAPABILITY`.
122pub const GVCP_CAPABILITY: u64 = 0x0934;
123/// Capability bit 29 — counting from the most significant bit as bit 0,
124/// as the specification does — `PACKETRESEND_CMD` supported. Wireshark's
125/// `hf_gvcp_capability_packetresend` masks `0x00000004`; aravis reads the
126/// same bit (`ARV_GVBS_GVCP_CAPABILITY_PACKET_RESEND`, `1 << 2`).
127pub const CAPABILITY_PACKET_RESEND: u32 = 0x0000_0004;
128/// Capability bit 26 — `PENDING_ACK` supported. Wireshark:
129/// `hf_gvcp_capability_pending`, mask `0x00000020`.
130pub const CAPABILITY_PENDING_ACK: u32 = 0x0000_0020;
131/// GVCP Configuration (`GevGVCPConfiguration`): optional behaviour the
132/// controller switches on. Wireshark: `GVCP_CONFIGURATION`.
133pub const GVCP_CONFIGURATION: u64 = 0x0954;
134/// Configuration bit 30 — `PENDING_ACK` enabled; a device sends one only
135/// once this is set. Wireshark: `hf_gvcp_configuration_pending_ack_enable`,
136/// mask `0x00000002`.
137pub const CONFIGURATION_PENDING_ACK_ENABLE: u32 = 0x0000_0002;
138/// Timestamp tick frequency, high 32 bits (`GevTimestampTickFrequencyHigh`).
139pub const TIMESTAMP_TICK_FREQUENCY_HIGH: u64 = 0x093C;
140/// Timestamp tick frequency, low 32 bits (`GevTimestampTickFrequencyLow`).
141/// `GevSCPD` counts in these ticks.
142pub const TIMESTAMP_TICK_FREQUENCY_LOW: u64 = 0x0940;
143
144// ── Message channel bootstrap registers ─────────────────────────────
145//
146// These sit in the 0x0B00 block, next to CCP at 0x0A00 and the stream
147// channels at 0x0D00. They previously read 0x0900_0200 / 0x0900_0204,
148// which is not a bootstrap address at all: 0x0900 is
149// `GevNumberOfMessageChannels`, and the low half was being used as if it
150// were a base. Every event-channel write therefore landed ~150 MB into
151// the device's register space. Wireshark's `packet-gvcp.c`
152// (`GVCP_MC_DESTINATION_PORT`, `GVCP_MC_DESTINATION_ADDRESS`) gives the
153// real values, and the CCP and stream-channel addresses we already had
154// right corroborate the scheme.
155
156/// Number of message channels the device implements (`GevNumberOfMessageChannels`).
157pub const NUMBER_OF_MESSAGE_CHANNELS: u64 = 0x0000_0900;
158/// Message channel destination port register (`GevMCP`).
159///
160/// A 32-bit register; the UDP port occupies the low 16 bits.
161pub const MESSAGE_DESTINATION_PORT: u64 = 0x0000_0B00;
162/// Message channel destination address register (`GevMCDA`).
163pub const MESSAGE_DESTINATION_ADDRESS: u64 = 0x0000_0B10;
164/// Message channel transmission timeout in milliseconds (`GevMCTT`).
165pub const MESSAGE_CHANNEL_TIMEOUT: u64 = 0x0000_0B14;
166/// Message channel retry count (`GevMCRC`).
167pub const MESSAGE_CHANNEL_RETRY_COUNT: u64 = 0x0000_0B18;
168
169/// Maximum number of bytes we read per GenCP `ReadMem` operation.
170pub const GENCP_MAX_BLOCK: usize = 512;
171/// Additional bytes that accompany a GenCP `WriteMem` block.
172pub const GENCP_WRITE_OVERHEAD: usize = 8;
173
174/// Default timeout for control transactions.
175pub const CONTROL_TIMEOUT: Duration = Duration::from_millis(500);
176/// Maximum number of automatic retries for a control transaction.
177pub const MAX_RETRIES: usize = 4;
178/// Maximum number of consecutive PENDING_ACKs honoured for one command.
179///
180/// A device is free to keep asking for more time; this bounds a
181/// misbehaving one that never finishes.
182pub const MAX_PENDING_ACKS: usize = 100;
183/// Ceiling on the extension a single PENDING_ACK may request.
184///
185/// The field is 16-bit milliseconds, so the wire maximum is ~65 s. Cap it
186/// well below that: an honest device asks for tens or hundreds of
187/// milliseconds, and this keeps a garbage value from hanging the caller.
188pub const MAX_PENDING_ACK_WAIT: Duration = Duration::from_secs(10);
189/// Base delay used for retry backoff.
190pub const RETRY_BASE_DELAY: Duration = Duration::from_millis(20);
191/// Upper bound for the random jitter added to the retry delay (inclusive).
192pub const RETRY_JITTER: Duration = Duration::from_millis(10);
193
194/// Maximum number of bytes captured while listening for discovery responses.
195pub const DISCOVERY_BUFFER: usize = 2048;
196
197/// Base register for stream channel 0 (GigE Vision bootstrap register map).
198///
199/// The GigE Vision specification defines stream channel bootstrap registers
200/// starting at 0x0d00. Note: some cameras may use different offsets declared
201/// in their GenICam XML (e.g. SFNC `GevSCDA` nodes). The bootstrap offsets
202/// here match the aravis implementation and the GigE Vision 2.x standard.
203pub const STREAM_CHANNEL_BASE: u64 = 0x0d00;
204/// Stride in bytes between successive stream channel blocks.
205pub const STREAM_CHANNEL_STRIDE: u64 = 0x40;
206/// Offset for `GevSCPHostPort` within a stream channel block.
207pub const STREAM_DESTINATION_PORT: u64 = 0x00;
208/// Offset for `GevSCPSPacketSize` within a stream channel block.
209pub const STREAM_PACKET_SIZE: u64 = 0x04;
210/// Offset for `GevSCPD` (packet delay) within a stream channel block.
211pub const STREAM_PACKET_DELAY: u64 = 0x08;
212/// Offset for `GevSCDA` (stream destination IP address) within a stream channel block.
213pub const STREAM_DESTINATION_ADDRESS: u64 = 0x18;
214
215// ── Bootstrap registers (GigE Vision 2.x, the bootstrap register map) ────
216
217/// `Version`: the GigE Vision version the device implements.
218pub const VERSION: u64 = 0x0000;
219/// `Device Mode`: endianness, character set, device class.
220pub const DEVICE_MODE: u64 = 0x0004;
221/// `Device MAC Address – High`: the MAC's top two bytes, right-aligned.
222pub const DEVICE_MAC_ADDRESS_HIGH: u64 = 0x0008;
223/// `Device MAC Address – Low`: the MAC's bottom four bytes.
224pub const DEVICE_MAC_ADDRESS_LOW: u64 = 0x000C;
225/// `Supported IP Configuration`: LLA, DHCP, persistent IP.
226pub const SUPPORTED_IP_CONFIG: u64 = 0x0010;
227/// `Current IP Address`.
228pub const CURRENT_IP_ADDRESS: u64 = 0x0024;
229/// `Current Subnet Mask`.
230pub const CURRENT_SUBNET_MASK: u64 = 0x0034;
231/// `Current Default Gateway`.
232pub const CURRENT_DEFAULT_GATEWAY: u64 = 0x0044;
233/// `First URL`: where the device's GenApi XML is (512 bytes, a string).
234pub const FIRST_URL: u64 = 0x0200;
235/// `Second URL`: an alternative to [`FIRST_URL`].
236pub const SECOND_URL: u64 = 0x0400;
237/// Length of [`FIRST_URL`] and [`SECOND_URL`] in bytes.
238pub const URL_LEN: usize = 512;
239/// `GVCP Capability` bit 31: several registers in one `READREG`/`WRITEREG`.
240pub const CAPABILITY_CONCATENATION: u32 = 0x0000_0001;
241/// `GVCP Capability` bit 30: `WRITEMEM` is supported.
242pub const CAPABILITY_WRITEMEM: u32 = 0x0000_0002;
243/// `GVCP Capability` bit 4: the primary application's port and address are
244/// readable at [`PRIMARY_APPLICATION_PORT`] and [`PRIMARY_APPLICATION_IP`].
245/// Mask from Wireshark's `hf_gvcp_capability_ccp_application_portip`.
246pub const CAPABILITY_CCP_APPLICATION_PORT_IP: u32 = 0x0800_0000;
247
248/// The bits of `GevSCPSPacketSize` that hold the packet size.
249///
250/// Bit 31 fires a test packet and bit 30 sets do-not-fragment; bits 29-16 are
251/// reserved. Masking matters on the read side as much as the write side — a
252/// device that leaves the do-not-fragment bit set would otherwise read back as
253/// a packet size of over a billion.
254pub const STREAM_PACKET_SIZE_MASK: u32 = 0xFFFF;
255
256/// `GevSCPSPacketSize` bit 31: emit one test packet of the requested size.
257///
258/// The only way to discover that a size *both endpoints accept* cannot cross
259/// the link between them — a register read reports what the device stored, not
260/// what the network will carry
261/// ([#112](https://github.com/VitalyVorobyev/viva-genicam/issues/112)).
262pub const SCPS_FIRE_TEST_PACKET: u32 = 0x8000_0000;
263
264/// `GevSCPSPacketSize` bit 30: set do-not-fragment on transmitted packets.
265///
266/// Paired with the test packet so a path that would *fragment* the datagram
267/// drops it instead. Without it a probe can succeed on a link that then
268/// delivers reassembled fragments, which is slower than the smaller size it
269/// talked us out of.
270pub const SCPS_DO_NOT_FRAGMENT: u32 = 0x4000_0000;
271
272#[cfg(test)]
273mod tests {
274    use super::*;
275
276    /// The values, written out as literals from the GigE Vision bootstrap
277    /// register map and opcode table rather than from these constants
278    /// (ADR-0019), corroborated by aravis's `arvgvcpprivate.h`
279    /// (`ARV_GVBS_*_OFFSET`, `ArvGvcpCommand`). The fake cameras import the
280    /// names; this table is what keeps a wrong value from passing in both.
281    #[test]
282    fn the_vocabulary_matches_the_specification() {
283        let addresses: &[(&str, u64, u64)] = &[
284            ("Version", VERSION, 0x0000),
285            ("Device Mode", DEVICE_MODE, 0x0004),
286            ("Device MAC Address High", DEVICE_MAC_ADDRESS_HIGH, 0x0008),
287            ("Device MAC Address Low", DEVICE_MAC_ADDRESS_LOW, 0x000C),
288            ("Supported IP Configuration", SUPPORTED_IP_CONFIG, 0x0010),
289            ("Current IP Configuration", CURRENT_IP_CONFIG, 0x0014),
290            ("Current IP Address", CURRENT_IP_ADDRESS, 0x0024),
291            ("Current Subnet Mask", CURRENT_SUBNET_MASK, 0x0034),
292            ("Current Default Gateway", CURRENT_DEFAULT_GATEWAY, 0x0044),
293            ("First URL", FIRST_URL, 0x0200),
294            ("Second URL", SECOND_URL, 0x0400),
295            ("Persistent IP Address", PERSISTENT_IP_ADDRESS, 0x064C),
296            ("Persistent Subnet Mask", PERSISTENT_SUBNET_MASK, 0x065C),
297            (
298                "Persistent Default Gateway",
299                PERSISTENT_DEFAULT_GATEWAY,
300                0x066C,
301            ),
302            (
303                "Number of Message Channels",
304                NUMBER_OF_MESSAGE_CHANNELS,
305                0x0900,
306            ),
307            (
308                "Number of Stream Channels",
309                NUMBER_OF_STREAM_CHANNELS,
310                0x0904,
311            ),
312            ("GVCP Capability", GVCP_CAPABILITY, 0x0934),
313            ("Heartbeat Timeout", HEARTBEAT_TIMEOUT, 0x0938),
314            (
315                "Timestamp Tick Frequency High",
316                TIMESTAMP_TICK_FREQUENCY_HIGH,
317                0x093C,
318            ),
319            (
320                "Timestamp Tick Frequency Low",
321                TIMESTAMP_TICK_FREQUENCY_LOW,
322                0x0940,
323            ),
324            ("GVCP Configuration", GVCP_CONFIGURATION, 0x0954),
325            (
326                "Control Channel Privilege",
327                CONTROL_CHANNEL_PRIVILEGE,
328                0x0A00,
329            ),
330            ("Primary Application Port", PRIMARY_APPLICATION_PORT, 0x0A04),
331            ("Primary Application IP", PRIMARY_APPLICATION_IP, 0x0A14),
332            ("Message Channel Port", MESSAGE_DESTINATION_PORT, 0x0B00),
333            (
334                "Message Channel Destination",
335                MESSAGE_DESTINATION_ADDRESS,
336                0x0B10,
337            ),
338            (
339                "Stream Channel 0 Port",
340                STREAM_CHANNEL_BASE + STREAM_DESTINATION_PORT,
341                0x0D00,
342            ),
343            (
344                "Stream Channel 0 Packet Size",
345                STREAM_CHANNEL_BASE + STREAM_PACKET_SIZE,
346                0x0D04,
347            ),
348            (
349                "Stream Channel 0 Packet Delay",
350                STREAM_CHANNEL_BASE + STREAM_PACKET_DELAY,
351                0x0D08,
352            ),
353            (
354                "Stream Channel 0 Destination",
355                STREAM_CHANNEL_BASE + STREAM_DESTINATION_ADDRESS,
356                0x0D18,
357            ),
358        ];
359        for &(name, actual, spec) in addresses {
360            assert_eq!(actual, spec, "{name}");
361        }
362        let opcodes: &[(&str, u16, u16)] = &[
363            ("DISCOVERY_CMD", DISCOVERY_COMMAND, 0x0002),
364            ("DISCOVERY_ACK", DISCOVERY_ACK, 0x0003),
365            ("FORCEIP_CMD", FORCEIP_COMMAND, 0x0004),
366            ("FORCEIP_ACK", FORCEIP_ACK, 0x0005),
367            ("PACKETRESEND_CMD", PACKET_RESEND_COMMAND, 0x0040),
368            ("PENDING_ACK", PENDING_ACK, 0x0089),
369            ("EVENT_CMD", EVENT_COMMAND, 0x00C0),
370            ("EVENT_ACK", EVENT_ACK, 0x00C1),
371            ("EVENTDATA_CMD", EVENTDATA_COMMAND, 0x00C2),
372            ("EVENTDATA_ACK", EVENTDATA_ACK, 0x00C3),
373            ("ACTION_CMD", ACTION_COMMAND, 0x0100),
374            ("ACTION_ACK", ACTION_ACK, 0x0101),
375        ];
376        for &(name, actual, spec) in opcodes {
377            assert_eq!(actual, spec, "{name}");
378        }
379        assert_eq!(GVCP_CMD_KEY, 0x42);
380        assert_eq!((CCP_EXCLUSIVE, CCP_CONTROL), (1 << 0, 1 << 1));
381        assert_eq!(
382            (
383                CAPABILITY_CONCATENATION,
384                CAPABILITY_WRITEMEM,
385                CAPABILITY_PACKET_RESEND
386            ),
387            (1 << 0, 1 << 1, 1 << 2)
388        );
389        assert_eq!(CAPABILITY_PENDING_ACK, 1 << 5);
390        assert_eq!(
391            (SCPS_FIRE_TEST_PACKET, SCPS_DO_NOT_FRAGMENT),
392            (1 << 31, 1 << 30)
393        );
394    }
395}