Skip to content

Commit fa84eb7

Browse files
Merge remote-tracking branch 'upstream/copilot/move-onvif-class-to-header' into onvif_cleanup
2 parents 6423afa + 77afd58 commit fa84eb7

File tree

3 files changed

+104
-67
lines changed

3 files changed

+104
-67
lines changed

src/zm_monitor.h

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@
4040
#include <vector>
4141
#include <curl/curl.h>
4242

43-
#ifdef WITH_GSOAP
44-
#include "soapPullPointSubscriptionBindingProxy.h"
45-
#include "plugin/wsseapi.h"
46-
#include "plugin/wsaapi.h"
47-
#include <openssl/err.h>
48-
#endif
43+
#include "zm_monitor_onvif.h"
4944

5045
class Group;
5146
class MonitorLinkExpression;
@@ -327,56 +322,6 @@ class Monitor : public std::enable_shared_from_this<Monitor> {
327322
};
328323
protected:
329324

330-
class ONVIF {
331-
protected:
332-
Monitor *parent;
333-
bool alarmed;
334-
bool healthy;
335-
std::string last_topic;
336-
std::string last_value;
337-
void SetNoteSet(Event::StringSet &noteSet);
338-
#ifdef WITH_GSOAP
339-
struct soap *soap = nullptr;
340-
_tev__CreatePullPointSubscription request;
341-
_tev__CreatePullPointSubscriptionResponse response;
342-
_tev__PullMessages tev__PullMessages;
343-
_tev__PullMessagesResponse tev__PullMessagesResponse;
344-
_wsnt__Renew wsnt__Renew;
345-
_wsnt__RenewResponse wsnt__RenewResponse;
346-
PullPointSubscriptionBindingProxy proxyEvent;
347-
void set_credentials(struct soap *soap);
348-
bool try_usernametoken_auth; // Track if we should try plain auth
349-
int retry_count; // Track retry attempts
350-
int max_retries; // Maximum retry attempts before giving up
351-
std::string discovered_event_endpoint; // Store discovered endpoint
352-
SystemTimePoint last_retry_time; // Time of last retry attempt
353-
354-
// Configurable timeout values (can be set via onvif_options)
355-
std::string pull_timeout; // Default "PT20S"
356-
std::string subscription_timeout; // Default "PT60S"
357-
358-
// Helper methods
359-
bool parse_event_message(wsnt__NotificationMessageHolderType *msg, std::string &topic, std::string &value, std::string &operation);
360-
bool matches_topic_filter(const std::string &topic, const std::string &filter);
361-
void parse_onvif_options(); // Parse options from parent->onvif_options
362-
int get_retry_delay(); // Calculate exponential backoff delay
363-
#endif
364-
std::unordered_map<std::string, std::string> alarms;
365-
std::mutex alarms_mutex;
366-
public:
367-
explicit ONVIF(Monitor *parent_);
368-
~ONVIF();
369-
void start();
370-
void WaitForMessage();
371-
bool isAlarmed() {
372-
std::unique_lock<std::mutex> lck(alarms_mutex);
373-
return alarmed;
374-
};
375-
void setAlarmed(bool p_alarmed) { alarmed = p_alarmed; };
376-
bool isHealthy() const { return healthy; };
377-
void setNotes(Event::StringSet &noteSet) { SetNoteSet(noteSet); };
378-
};
379-
380325
class AmcrestAPI {
381326
private:
382327
Monitor *parent;

src/zm_monitor_onvif.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ZoneMinder Monitor::ONVIF Class Implementation
2+
// ZoneMinder ONVIF Class Implementation
33
// Copyright (C) 2024 ZoneMinder Inc
44
//
55
// This program is free software; you can redistribute it and/or
@@ -17,6 +17,7 @@
1717
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
//
1919

20+
#include "zm_monitor_onvif.h"
2021
#include "zm_monitor.h"
2122

2223
#include <cstring>
@@ -48,7 +49,7 @@ std::string SOAP_STRINGS[] = {
4849
"SOAP_FAULT", // 12
4950
};
5051

51-
Monitor::ONVIF::ONVIF(Monitor *parent_) :
52+
ONVIF::ONVIF(Monitor *parent_) :
5253
parent(parent_)
5354
,alarmed(false)
5455
,healthy(false)
@@ -67,7 +68,7 @@ Monitor::ONVIF::ONVIF(Monitor *parent_) :
6768
#endif
6869
}
6970

70-
Monitor::ONVIF::~ONVIF() {
71+
ONVIF::~ONVIF() {
7172
#ifdef WITH_GSOAP
7273
if (soap != nullptr) {
7374
Debug(1, "ONVIF: Tearing Down");
@@ -105,7 +106,7 @@ Monitor::ONVIF::~ONVIF() {
105106
#endif
106107
}
107108

108-
void Monitor::ONVIF::start() {
109+
void ONVIF::start() {
109110
#ifdef WITH_GSOAP
110111
tev__PullMessages.Timeout = pull_timeout.c_str();
111112
tev__PullMessages.MessageLimit = 10;
@@ -321,7 +322,7 @@ void Monitor::ONVIF::start() {
321322
#endif
322323
}
323324

324-
void Monitor::ONVIF::WaitForMessage() {
325+
void ONVIF::WaitForMessage() {
325326
#ifdef WITH_GSOAP
326327
set_credentials(soap);
327328

@@ -521,7 +522,7 @@ void Monitor::ONVIF::WaitForMessage() {
521522
// pull_timeout=PT20S - Timeout for PullMessages requests
522523
// subscription_timeout=PT60S - Timeout for subscription renewal
523524
// max_retries=5 - Maximum retry attempts
524-
void Monitor::ONVIF::parse_onvif_options() {
525+
void ONVIF::parse_onvif_options() {
525526
if (parent->onvif_options.empty()) {
526527
return;
527528
}
@@ -573,7 +574,7 @@ void Monitor::ONVIF::parse_onvif_options() {
573574

574575
// Calculate exponential backoff delay for retries
575576
// Returns delay in seconds: min(2^retry_count, ONVIF_RETRY_DELAY_CAP)
576-
int Monitor::ONVIF::get_retry_delay() {
577+
int ONVIF::get_retry_delay() {
577578
// Use safe approach to avoid integer overflow
578579
if (retry_count >= ONVIF_RETRY_EXPONENT_LIMIT) {
579580
return ONVIF_RETRY_DELAY_CAP; // 2^9 = 512, cap at 5 minutes
@@ -586,7 +587,7 @@ int Monitor::ONVIF::get_retry_delay() {
586587
}
587588

588589
//ONVIF Set Credentials
589-
void Monitor::ONVIF::set_credentials(struct soap *soap) {
590+
void ONVIF::set_credentials(struct soap *soap) {
590591
soap_wsse_delete_Security(soap);
591592
soap_wsse_add_Timestamp(soap, "Time", 10);
592593

@@ -605,7 +606,7 @@ void Monitor::ONVIF::set_credentials(struct soap *soap) {
605606
}
606607

607608
// Helper function to parse event messages with flexible XML structure handling
608-
bool Monitor::ONVIF::parse_event_message(wsnt__NotificationMessageHolderType *msg,
609+
bool ONVIF::parse_event_message(wsnt__NotificationMessageHolderType *msg,
609610
std::string &topic,
610611
std::string &value,
611612
std::string &operation) {
@@ -731,7 +732,7 @@ bool Monitor::ONVIF::parse_event_message(wsnt__NotificationMessageHolderType *ms
731732
}
732733

733734
// Helper function for hierarchical topic matching with wildcard support
734-
bool Monitor::ONVIF::matches_topic_filter(const std::string &topic, const std::string &filter) {
735+
bool ONVIF::matches_topic_filter(const std::string &topic, const std::string &filter) {
735736
if (filter.empty()) {
736737
return true; // Empty filter matches all
737738
}
@@ -823,7 +824,7 @@ int SOAP_ENV__Fault(struct soap *soap, char *faultcode, char *faultstring, char
823824
}
824825
#endif
825826

826-
void Monitor::ONVIF::SetNoteSet(Event::StringSet &noteSet) {
827+
void ONVIF::SetNoteSet(Event::StringSet &noteSet) {
827828
#ifdef WITH_GSOAP
828829
std::unique_lock<std::mutex> lck(alarms_mutex);
829830
if (alarms.empty()) return;

src/zm_monitor_onvif.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// ZoneMinder Monitor ONVIF Class Interface
3+
// Copyright (C) 2024 ZoneMinder Inc
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 2
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
//
19+
20+
#ifndef ZM_MONITOR_ONVIF_H
21+
#define ZM_MONITOR_ONVIF_H
22+
23+
#include <mutex>
24+
#include <string>
25+
#include <unordered_map>
26+
#include "zm_event.h"
27+
#include "zm_time.h"
28+
29+
#ifdef WITH_GSOAP
30+
#include "soapPullPointSubscriptionBindingProxy.h"
31+
#include "plugin/wsseapi.h"
32+
#include "plugin/wsaapi.h"
33+
#include <openssl/err.h>
34+
#endif
35+
36+
// Forward declaration
37+
class Monitor;
38+
39+
class ONVIF {
40+
friend class Monitor;
41+
42+
protected:
43+
Monitor *parent;
44+
bool alarmed;
45+
bool healthy;
46+
std::string last_topic;
47+
std::string last_value;
48+
void SetNoteSet(Event::StringSet &noteSet);
49+
#ifdef WITH_GSOAP
50+
struct soap *soap = nullptr;
51+
_tev__CreatePullPointSubscription request;
52+
_tev__CreatePullPointSubscriptionResponse response;
53+
_tev__PullMessages tev__PullMessages;
54+
_tev__PullMessagesResponse tev__PullMessagesResponse;
55+
_wsnt__Renew wsnt__Renew;
56+
_wsnt__RenewResponse wsnt__RenewResponse;
57+
PullPointSubscriptionBindingProxy proxyEvent;
58+
void set_credentials(struct soap *soap);
59+
bool try_usernametoken_auth; // Track if we should try plain auth
60+
int retry_count; // Track retry attempts
61+
int max_retries; // Maximum retry attempts before giving up
62+
std::string discovered_event_endpoint; // Store discovered endpoint
63+
SystemTimePoint last_retry_time; // Time of last retry attempt
64+
65+
// Configurable timeout values (can be set via onvif_options)
66+
std::string pull_timeout; // Default "PT20S"
67+
std::string subscription_timeout; // Default "PT60S"
68+
69+
// Helper methods
70+
bool parse_event_message(wsnt__NotificationMessageHolderType *msg, std::string &topic, std::string &value, std::string &operation);
71+
bool matches_topic_filter(const std::string &topic, const std::string &filter);
72+
void parse_onvif_options(); // Parse options from parent->onvif_options
73+
int get_retry_delay(); // Calculate exponential backoff delay
74+
#endif
75+
std::unordered_map<std::string, std::string> alarms;
76+
std::mutex alarms_mutex;
77+
public:
78+
explicit ONVIF(Monitor *parent_);
79+
~ONVIF();
80+
void start();
81+
void WaitForMessage();
82+
bool isAlarmed() {
83+
std::unique_lock<std::mutex> lck(alarms_mutex);
84+
return alarmed;
85+
};
86+
void setAlarmed(bool p_alarmed) { alarmed = p_alarmed; };
87+
bool isHealthy() const { return healthy; };
88+
void setNotes(Event::StringSet &noteSet) { SetNoteSet(noteSet); };
89+
};
90+
91+
#endif // ZM_MONITOR_ONVIF_H

0 commit comments

Comments
 (0)