From 8a60da63d3daad311bba5d9601f5dd130dae4df5 Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 19 Nov 2026 09:41:45 +0530 Subject: [PATCH] fix: Dahua_RPC probe no longer reboots or moves the camera refs #6763 --- .../lib/ZoneMinder/Control/Dahua_RPC.pm | 45 +++++++++--- scripts/ZoneMinder/t/dahua_rpc_probe.t | 69 +++++++++++++++++++ 2 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 scripts/ZoneMinder/t/dahua_rpc_probe.t diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/Dahua_RPC.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/Dahua_RPC.pm index d9273b0cfb..1b2c58269f 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Control/Dahua_RPC.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/Dahua_RPC.pm @@ -431,6 +431,8 @@ sub set_config { # Probe the camera's capabilities: everything get_config returns, plus test # which interfaces respond vs. return InterfaceNotFound. Useful for # characterising an unknown Dahua/Amcrest model without writing ad-hoc scripts. +# Strictly read-only: it never sends a method that changes camera state (no +# reboot, no PTZ move), so it is safe to run against a live camera. # zmcontrol.pl --protocol Dahua_RPC --address admin:pass@IP --command probe sub probe { my $self = shift; @@ -454,9 +456,9 @@ sub probe { my $r = $self->rpc_call($m, {channel => 0}, object => $ptz_obj); $out{ptz}{$m} = ($r and $r->{result}) ? $r->{params} : ($r->{error}{message}//'?'); } - # Test a safe ptz.start (GotoPreset 0 — no physical movement on most models) - my $t = $self->rpc_call('ptz.start', {code=>'GotoPreset',arg1=>0,arg2=>0,arg3=>0,channel=>0}, object=>$ptz_obj); - $out{ptz}{'ptz.start(GotoPreset)'} = ($t and $t->{result}) ? 'accepted' : ($t->{error}{message}//'?'); + # No ptz.start test here: even GotoPreset can move the camera, and the + # getCurrentProtocolCaps/getStatus/getPresets calls above already show + # whether PTZ is available. } # --- Coaxial (light/siren/deterrence) --- @@ -472,13 +474,36 @@ sub probe { $out{config}{$n} = ($r and $r->{result}) ? $r->{params}{table} : ($r->{error}{message}//'?'); } - # --- Reachability of control methods (read-only tests) --- - for my $m (qw(magicBox.reboot system.listMethod)) { - my $r = $self->rpc_call($m); - # We only check if the method exists (reboot intentionally not sent; listMethod is safe) - $out{methods}{$m} = ($r and defined $r->{result}) - ? ($r->{result} ? 'available' : 'error: '.($r->{error}{message}//'?')) - : 'unreachable'; + # --- Method reachability (read-only) --- + # system.listMethod is safe and returns the method names the camera + # implements. magicBox.reboot would power-cycle the camera, so it is never + # sent: we only look for it in the returned list. + my $lm = $self->rpc_call('system.listMethod'); + my @methods; + if ($lm and $lm->{result}) { + # Dahua answers {"params":{"method":[names]}}; accept a bare list too. + my $p = $lm->{params}; + $p = $p->{method} if ref($p) eq 'HASH'; + @methods = map { ref($_) eq 'HASH' ? ($_->{method} // $_->{name} // '') : ($_ // '') } @$p + if ref($p) eq 'ARRAY'; + @methods = grep { length } @methods; + } + if ($lm and defined $lm->{result}) { + $out{methods}{'system.listMethod'} = $lm->{result} + ? 'available'.(@methods ? ': '.scalar(@methods).' methods' : '') + : 'error: '.($lm->{error}{message}//'?'); + } else { + $out{methods}{'system.listMethod'} = 'unreachable'; + } + + for my $m (qw(magicBox.reboot ptz.start configManager.setConfig)) { + if (@methods) { + $out{methods}{$m} = (grep { $_ eq $m } @methods) + ? 'present in method list' : 'not in method list'; + } else { + # Unknown/empty list format: report without guessing. + $out{methods}{$m} = 'not tested'; + } } return \%out; diff --git a/scripts/ZoneMinder/t/dahua_rpc_probe.t b/scripts/ZoneMinder/t/dahua_rpc_probe.t new file mode 100644 index 0000000000..186e0f18cb --- /dev/null +++ b/scripts/ZoneMinder/t/dahua_rpc_probe.t @@ -0,0 +1,69 @@ +use strict; +use warnings; +use Test::More tests => 9; + +require_ok('ZoneMinder::Control::Dahua_RPC'); + +# probe() is documented as a read-only capability probe. Stub rpc_call so it +# never touches a camera, and record every method it asks for. +sub make_stub { + return bless { + session => 'test', + rpc_id => 0, + RPCBase => 'http://127.0.0.1/', + ptz_object => undef, + ua => undef, + }, 'ZoneMinder::Control::Dahua_RPC'; +} + +my @methods; +my $stub = make_stub(); + +no warnings 'redefine'; +local *ZoneMinder::Control::Dahua_RPC::rpc_call = sub { + my ($self, $method, $params, %opts) = @_; + push @methods, $method; + return { result => 7, id => 1, params => {} } if $method eq 'ptz.factory.instance'; + if ($method eq 'system.listMethod') { + # Dahua returns the supported method names in params.method. + return { result => 1, id => 2, params => { method => [ + 'magicBox.getDeviceType', 'ptz.getStatus', 'ptz.start', + 'configManager.getConfig', + ] } }; + } + return { result => 1, id => 3, params => {} }; +}; +use warnings 'redefine'; + +my $out = $stub->probe(); + +is(scalar(grep { $_ eq 'magicBox.reboot' } @methods), 0, + 'probe never calls magicBox.reboot'); +is(scalar(grep { $_ eq 'ptz.start' } @methods), 0, + 'probe sends no ptz.start'); +is(scalar(grep { $_ eq 'system.listMethod' } @methods), 1, + 'probe calls system.listMethod exactly once'); +like($out->{methods}{'system.listMethod'}, qr/^available/, + 'probe reports the system.listMethod result'); +is($out->{methods}{'magicBox.reboot'}, 'not in method list', + 'probe reports magicBox.reboot as absent from the method list'); +is($out->{methods}{'ptz.start'}, 'present in method list', + 'probe finds methods in the list without calling them'); + +# Unknown list format: report listMethod only, do not guess about reboot. +my @methods2; +my $stub2 = make_stub(); +$stub2->{ptz_object} = 7; +local *ZoneMinder::Control::Dahua_RPC::rpc_call = sub { + my ($self, $method, $params, %opts) = @_; + push @methods2, $method; + return { result => 1, id => 1, params => 'opaque' } if $method eq 'system.listMethod'; + return { result => 1, id => 2, params => {} }; +}; + +my $out2 = $stub2->probe(); + +is(scalar(grep { $_ eq 'magicBox.reboot' } @methods2), 0, + 'probe never calls magicBox.reboot with an unknown list format'); +is($out2->{methods}{'magicBox.reboot'}, 'not tested', + 'unknown method list format leaves magicBox.reboot not tested');