You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

738 lines
18 KiB

8 years ago
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
la_mojo pOnvif: ONVIF compliance fixes (report points 1-6) 1. Faults are no longer reported as success: every request waits for the answer, checks the HTTP status and the SOAP Fault (logged with Code/Subcode/Reason); getLastError()/getLastFault() tell the caller. protocolPTZ_ONVIF clears m_bStatus (-> reconnect) only on communication errors, not on a Fault from a reachable camera. AbsoluteMove now waits for its answer too. 2. Request/reply race: already fixed in #11 (checked). 3. NULL-safe parsing of optional elements (safeCopy/safeAtoi/safeAtof), videoSource zero-filled, Imaging values parsed as float, IrCutFilter AUTO no longer read as OFF, missing XAddr no longer becomes "(null)", GetSystemDateAndTime without UTCDateTime no longer breaks the token time (falls back to the local clock). 4. GetProfiles parses PTZConfiguration; the PTZ profile is the first one that has it (fallback to profile 0 with an error log); create() fails cleanly when the device has no profiles instead of throwing. 5. Pan/tilt and zoom in one ContinuousMove with Timeout PT10S (PONVIF_CONTINUOUS_MOVE_TIMEOUT_S); Stop honours its pantilt/zoom flags. 6. WS-Security: 16-byte RAND_bytes nonce, no fixed-size digest buffer, header only when a user name is set (anonymous devices), user name and all string arguments XML-escaped. Requests are built by _buildRequest() from body templates. HTTP auth mode 0 now uses CURLAUTH_ANY (sends no password until the device asks; Digest preferred) instead of Basic; no HTTP auth without credentials. Also: Content-Type without the trailing ';', GetServices/ GetDeviceInformation sent to the device service, printf format-string bugs on device replies fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
8 years ago
​
​
​
la_mojo cameras: track PTZ health and re-initialise a failed PTZ protocol PTZ health per camera (deviceNodeCamera): - PTZ_HEALTH_INFO: state (NONE / INIT / OK / FAILED), failure and recovery counters, consecutive failed initialisations, time of the last state change / failure, next retry and the last error text. getPtzHealth() returns a copy (mutex-protected, any thread). - getPtzStatus() - already sent to the remote server as CAMERA_STATUS_PTZ_OK - now reflects it: 1 for OK (or no PTZ protocol), 0 while PTZ is initialising or failed. Before it was 1 as soon as the state machine left CAM_WS_INIT, whatever the protocol did. Failure detection and re-initialisation: - While idle (CAM_WS_READY) the camera checks the protocol's validate() every loop and calls the new protocolPTZ::probe() every 30 s (ONVIF: GetStatus; a SOAP Fault still means "reachable", only a communication error marks the camera lost). Default probe() is the protocol's validate(), so serial protocols are unchanged. - On failure the PTZ protocol is recreated through CAM_WS_INIT: first attempt at once, then 5, 10, 20, 40, 60 s (capped) between attempts; recoveries are logged and counted. - A failed target-driven goAbsolute/goPreset no longer ends the camera thread (CAM_WS_END rebuilt the whole node): for ONVIF a command the camera rejected (connection OK) only drops that ROI, anything else re-initialises the PTZ protocol. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
  1. #include "protocolPTZ_ONVIF.h"
  2. #include "../../hardware/uart/serialPort.h"
  3. #define LOG(X) m_pLog->Log("ONVIF", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("ONVIF", this, _LOG_ERROR, X)
  5. protocolPTZ_ONVIF::protocolPTZ_ONVIF(config_setting_t * Settings) :
  6. protocolPTZ(),
  7. m_mInquiryMutex()
  8. {
  9. m_pLog = basicLogCollection::getInstance();
  10. m_bLastCommandIssued = CMD_PTZ_NOOP;
  11. m_pSettings = Settings;
  12. m_bAuthMode = 0;
  13. #ifdef USE_PONVIF
  14. m_pOnVif = NULL;
  15. m_pProfile = NULL;
  16. m_bReportsMoveStatus = false;
  17. m_bReportsPosition = false;
  18. m_bTargetValid = false;
  19. m_fTargetPan = 0.0f;
  20. m_fTargetTilt = 0.0f;
  21. m_fTargetZoom = 0.0f;
  22. #else
  23. m_pDevice = NULL;
  24. m_pPTZ = NULL;
  25. #endif
  26. memset(m_pDeviceURI, 0, MAX_PATH_LEN);
  27. memset(m_pUser, 0, MAX_CRED_LEN);
  28. memset(m_pPassword, 0, MAX_CRED_LEN);
  29. }
  30. protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode) :
  31. protocolPTZ(),
  32. m_mInquiryMutex()
  33. {
  34. m_pLog = basicLogCollection::getInstance();
  35. m_bLastCommandIssued = CMD_PTZ_NOOP;
  36. m_pSettings = NULL;
  37. #ifdef USE_PONVIF
  38. m_pOnVif = NULL;
  39. m_pProfile = NULL;
  40. m_bReportsMoveStatus = false;
  41. m_bReportsPosition = false;
  42. m_bTargetValid = false;
  43. m_fTargetPan = 0.0f;
  44. m_fTargetTilt = 0.0f;
  45. m_fTargetZoom = 0.0f;
  46. #else
  47. m_pDevice = NULL;
  48. m_pPTZ = NULL;
  49. #endif
  50. LOG(ip);
  51. LOG(username);
  52. LOG(password);
  53. m_bAuthMode = AuthMode;
  54. sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode);
  55. LOG(m_pDeviceURI);
  56. if (port == 80)
  57. {
  58. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip);
  59. }
  60. else
  61. {
  62. sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port);
  63. }
  64. LOG(m_pDeviceURI);
  65. strncpy(m_pUser, username, MAX_CRED_LEN);
  66. strncpy(m_pPassword, password, MAX_CRED_LEN);
  67. }
  68. protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode) :
  69. protocolPTZ(),
  70. m_mInquiryMutex()
  71. {
  72. m_pLog = basicLogCollection::getInstance();
  73. m_bLastCommandIssued = CMD_PTZ_NOOP;
  74. m_pSettings = NULL;
  75. #ifdef USE_PONVIF
  76. m_pOnVif = NULL;
  77. m_pProfile = NULL;
  78. m_bReportsMoveStatus = false;
  79. m_bReportsPosition = false;
  80. m_bTargetValid = false;
  81. m_fTargetPan = 0.0f;
  82. m_fTargetTilt = 0.0f;
  83. m_fTargetZoom = 0.0f;
  84. #else
  85. m_pDevice = NULL;
  86. m_pPTZ = NULL;
  87. #endif
  88. LOG(ctl_url);
  89. LOG(username);
  90. LOG(password);
  91. m_bAuthMode = AuthMode;
  92. sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode);
  93. LOG(m_pDeviceURI);
  94. strncpy(m_pDeviceURI, ctl_url, 250);
  95. strncpy(m_pUser, username, MAX_CRED_LEN);
  96. strncpy(m_pPassword, password, MAX_CRED_LEN);
  97. }
  98. protocolPTZ_ONVIF::~protocolPTZ_ONVIF() {}
  99. bool protocolPTZ_ONVIF::setIPAddress(CSTR ip)
  100. {
  101. if (ip == NULL)
  102. {
  103. return false;
  104. }
  105. if (strlen(ip) == 0)
  106. {
  107. return false;
  108. }
  109. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip);
  110. // strncpy( m_pDeviceURI, ip, MAX_PATH_LEN );
  111. return true;
  112. }
  113. bool protocolPTZ_ONVIF::setUsername(CSTR username)
  114. {
  115. if (username == NULL)
  116. {
  117. return false;
  118. }
  119. if (strlen(username) == 0)
  120. {
  121. return false;
  122. }
  123. strncpy(m_pUser, username, MAX_CRED_LEN);
  124. return true;
  125. }
  126. bool protocolPTZ_ONVIF::setPassword(CSTR password)
  127. {
  128. if (password == NULL)
  129. {
  130. return false;
  131. }
  132. if (strlen(password) == 0)
  133. {
  134. return false;
  135. }
  136. strncpy(m_pPassword, password, MAX_CRED_LEN);
  137. return true;
  138. }
  139. bool protocolPTZ_ONVIF::setAuthMode(uint8_t Mode)
  140. {
  141. m_bAuthMode = Mode;
  142. return true;
  143. }
  144. bool protocolPTZ_ONVIF::create(void)
  145. {
  146. if (! protocolPTZ::create())
  147. {
  148. return false;
  149. }
  150. if (m_pSettings != NULL)
  151. {
  152. config_setting_t * pSetting;
  153. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER);
  154. if (pSetting == NULL)
  155. {
  156. return false;
  157. }
  158. sprintf(m_pUser, "%s", config_setting_get_string(pSetting));
  159. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS);
  160. if (pSetting == NULL)
  161. {
  162. return false;
  163. }
  164. sprintf(m_pPassword, "%s", config_setting_get_string(pSetting));
  165. pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI);
  166. if (pSetting == NULL)
  167. {
  168. return false;
  169. }
  170. sprintf(m_pDeviceURI, "%s", config_setting_get_string(pSetting));
  171. };
  172. #ifdef DEBUG
  173. LOG(m_pDeviceURI);
  174. #endif
  175. if (strlen(m_pDeviceURI) < 4)
  176. {
  177. LOG_ERROR("No Device URI Is Set!");
  178. return false;
  179. };
  180. #ifdef USE_PONVIF
  181. m_pOnVif = new pOnvif();
  182. if (m_pOnVif == NULL)
  183. {
  184. LOG_ERROR("pOnvif NEW FAILURE!");
  185. m_bStatus = 0;
  186. return false;
  187. };
  188. m_pOnVif->setDeviceUri(m_pDeviceURI);
  189. m_pOnVif->setUsername(m_pUser);
  190. m_pOnVif->setPassword(m_pPassword);
  191. m_pOnVif->setAuthMode(m_bAuthMode);
  192. LOG("PROTOCREATE");
  193. if (m_pOnVif->create())
  194. {
  195. // PTZ commands need a profile with a PTZConfiguration
  196. m_pProfile = m_pOnVif->getPTZProfile();
  197. if (m_pProfile == NULL)
  198. {
  199. // Non-conformant devices sometimes accept PTZ on a profile without PTZConfiguration: keep the old behaviour
  200. m_pProfile = m_pOnVif->getProfiles()->at(0);
  201. LOG_ERROR("No media profile with a PTZConfiguration, using the first profile");
  202. }
  203. {
  204. char tB[CTEXT * 2];
  205. snprintf(tB, sizeof(tB), "PTZ profile: %s (%s)", m_pProfile->profiletoken, m_pProfile->profilename);
  206. LOG(tB);
  207. }
  208. detectStatusSupport();
  209. // Speed space of the node: AbsoluteMove at the maximal speed of it (generic 0..1 if not read)
  210. {
  211. bool tRead = m_pOnVif->ptz_GetNode(m_pProfile->ptznodetoken);
  212. const ptzSpeedSpace & tPT = m_pOnVif->getPanTiltSpeedSpace();
  213. const ptzSpeedSpace & tZM = m_pOnVif->getZoomSpeedSpace();
  214. char tB[CTEXT * 4];
  215. snprintf(tB, sizeof(tB), "PTZ speed (%s): PanTilt %.120s max %.3f, Zoom %.120s max %.3f", tRead ? "from node" : "generic default", tPT.uri, tPT.max, tZM.uri, tZM.max);
  216. LOG(tB);
  217. }
  218. // Camera ranges, to compare with the coordinates of the AbsoluteMove requests
  219. m_pOnVif->ptz_LogRanges(m_pProfile->profiletoken);
  220. LOG("DONE");
  221. m_bStatus = 1;
  222. return true;
  223. }
  224. else
  225. {
  226. LOG_ERROR("pOnvif CREATE FAILURE!");
  227. delete(m_pOnVif);
  228. m_pOnVif = NULL;
  229. m_bStatus = 0;
  230. return false;
  231. };
  232. #else
  233. std::string A(m_pUser);
  234. std::string B(m_pPassword);
  235. std::string C(m_pDeviceURI);
  236. m_pDevice = new OnvifClientDevice(C, A, B);
  237. if (m_pDevice != NULL)
  238. {
  239. m_pPTZ = new OnvifClientPTZ(*m_pDevice);
  240. if (m_pPTZ != NULL)
  241. {
  242. _tptz__GetNodesResponse tNodes;
  243. int RetVal;
  244. RetVal = m_pPTZ->GetNodes(tNodes);
  245. if (RetVal == 0)
  246. {
  247. OnvifClientMedia media(*m_pDevice);
  248. _trt__GetProfilesResponse profiles;
  249. media.GetProfiles(profiles);
  250. m_pPTZToken = profiles.Profiles.at(0)->token; // PTZConfiguration->token;
  251. // m_pPTZToken = tNodes.PTZNode.at(0)->token;
  252. //_tptz__GetConfigurationOptionsResponse COResp;
  253. // int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
  254. //_tptz__GetConfigurationResponse configResponce;
  255. // configResponce.PTZConfiguration = new tt__PTZConfiguration();
  256. // m_pPTZ->GetConfiguration(configResponce);
  257. // m_pToken = configResponce.PTZConfiguration->token;
  258. setConnectionStatus(CS_CONNECTED);
  259. m_bStatus = 1;
  260. return true;
  261. };
  262. };
  263. };
  264. #endif
  265. return false;
  266. }
  267. #ifndef USE_PONVIF
  268. bool protocolPTZ_ONVIF::initCamera(void)
  269. {
  270. if (m_bStatus == 0)
  271. {
  272. return false;
  273. }
  274. // if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  275. m_bLastCommandIssued = CMD_PTZ_INIT;
  276. m_pPTZ->Initialize();
  277. return true;
  278. }
  279. #endif
  280. bool protocolPTZ_ONVIF::move(uint32_t MoveMatrix)
  281. {
  282. // if( m_bStatus == 0 ) return false;
  283. #ifdef USE_PONVIF
  284. if (m_pProfile == NULL)
  285. {
  286. return false;
  287. }
  288. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  289. float Speed_x, Speed_y, Speed_z;
  290. if (MoveMatrix > 0)
  291. {
  292. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  293. {
  294. Speed_x = 0.0f;
  295. }
  296. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  297. {
  298. Speed_x = -m_dPanSpeed;
  299. }
  300. else
  301. {
  302. Speed_x = m_dPanSpeed;
  303. };
  304. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  305. {
  306. Speed_y = 0.0;
  307. }
  308. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  309. {
  310. Speed_y = m_dTiltSpeed;
  311. }
  312. else
  313. {
  314. Speed_y = -m_dTiltSpeed;
  315. };
  316. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  317. {
  318. Speed_z = 0.0;
  319. }
  320. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  321. {
  322. Speed_z = m_dZoomSpeed;
  323. }
  324. else
  325. {
  326. Speed_z = -m_dZoomSpeed;
  327. };
  328. // One request for all axes (two separate ContinuousMoves may stop each other)
  329. bool hasPT = ((MoveMatrix & (PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK)) != 0);
  330. bool hasZ = ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0);
  331. return commandResult(m_pOnVif->ptz_ContinuousMoveAll(m_pProfile->profiletoken, hasPT, Speed_x, Speed_y, hasZ, Speed_z));
  332. }
  333. return commandResult(m_pOnVif->ptz_Stop(m_pProfile->profiletoken, true, true));
  334. #else
  335. if (m_bStatus == 0)
  336. {
  337. return false;
  338. }
  339. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  340. _tptz__ContinuousMoveResponse ContMoveResponse;
  341. tt__PTZSpeed tSpeed;
  342. tSpeed.PanTilt = new tt__Vector2D();
  343. tSpeed.Zoom = new tt__Vector1D();
  344. LONG64 to = 1000;
  345. int RetVal;
  346. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  347. {
  348. tSpeed.PanTilt->x = 0.0;
  349. }
  350. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  351. {
  352. tSpeed.PanTilt->x = -m_dPanSpeed;
  353. }
  354. else
  355. {
  356. tSpeed.PanTilt->x = m_dPanSpeed;
  357. };
  358. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  359. {
  360. tSpeed.PanTilt->y = 0.0;
  361. }
  362. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  363. {
  364. tSpeed.PanTilt->y = m_dTiltSpeed;
  365. }
  366. else
  367. {
  368. tSpeed.PanTilt->y = -m_dTiltSpeed;
  369. };
  370. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  371. {
  372. tSpeed.Zoom->x = 0.0;
  373. }
  374. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  375. {
  376. tSpeed.Zoom->x = m_dZoomSpeed;
  377. }
  378. else
  379. {
  380. tSpeed.Zoom->x = -m_dZoomSpeed;
  381. };
  382. RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
  383. delete(tSpeed.PanTilt);
  384. delete(tSpeed.Zoom);
  385. if (RetVal)
  386. {
  387. return false;
  388. }
  389. return true;
  390. #endif
  391. }
  392. #include <math.h>
  393. #include <time.h>
  394. #ifdef USE_PONVIF
  395. /*
  396. Maps a pOnvif command result to the connection status: only a communication error means the camera is
  397. lost (m_bStatus = 0 -> validate() fails -> reconnect). A SOAP Fault / HTTP error is an answer from a
  398. reachable camera: the command failed, the connection is fine.
  399. */
  400. bool protocolPTZ_ONVIF::commandResult(bool ok)
  401. {
  402. if (ok)
  403. {
  404. m_bStatus = 1;
  405. return true;
  406. }
  407. if (m_pOnVif == NULL || m_pOnVif->getLastError() == PONVIF_ERR_COMM)
  408. {
  409. m_bStatus = 0;
  410. }
  411. return false;
  412. }
  413. static long ptzOnvifNowMs(void)
  414. {
  415. struct timespec tTS;
  416. clock_gettime(CLOCK_MONOTONIC, &tTS);
  417. return (long) tTS.tv_sec * 1000L + tTS.tv_nsec / 1000000L;
  418. }
  419. // Pan in the generic space wraps around (-1 == +1 on 360 degree cameras)
  420. static float ptzOnvifPanDistance(float a, float b)
  421. {
  422. float d = fabsf(a - b);
  423. if (d > 1.0f)
  424. {
  425. d = 2.0f - d;
  426. }
  427. return d;
  428. }
  429. /*
  430. Finds out once (after create) how the end of a move can be detected for this camera.
  431. The PTZ service capabilities (MoveStatus / StatusPosition attributes) are asked first; many cameras
  432. don't implement GetServiceCapabilities or leave the attributes out, so a real GetStatus decides.
  433. An explicit "false" in the capabilities wins over a GetStatus reply (some cameras send fixed values).
  434. */
  435. void protocolPTZ_ONVIF::detectStatusSupport(void)
  436. {
  437. m_bReportsMoveStatus = false;
  438. m_bReportsPosition = false;
  439. if (m_pOnVif == NULL || m_pProfile == NULL)
  440. {
  441. return;
  442. }
  443. int8_t capMoveStatus = -1;
  444. int8_t capStatusPosition = -1;
  445. m_pOnVif->ptz_GetServiceCapabilities(&capMoveStatus, &capStatusPosition);
  446. ptzStatus tVal;
  447. bool tProbe = m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal);
  448. m_bReportsMoveStatus = tProbe && tVal.hasMoveStatus && (capMoveStatus != 0);
  449. m_bReportsPosition = tProbe && tVal.hasPosition && (capStatusPosition != 0);
  450. char tB[256];
  451. snprintf(tB, sizeof(tB), "PTZ status support: MoveStatus=%s Position=%s (GetStatus %s, capabilities MoveStatus=%d StatusPosition=%d) -> %s",
  452. m_bReportsMoveStatus ? "yes" : "no", m_bReportsPosition ? "yes" : "no", tProbe ? "ok" : "failed", capMoveStatus, capStatusPosition,
  453. m_bReportsMoveStatus ? "wait for IDLE" : (m_bReportsPosition ? "wait for position" : "fixed wait"));
  454. LOG(tB);
  455. }
  456. bool protocolPTZ_ONVIF::fixedMoveWait(long alreadyWaitedMs)
  457. {
  458. long tRest = PTZ_ONVIF_FIXED_MOVE_WAIT_MS - alreadyWaitedMs;
  459. if (tRest > 0)
  460. {
  461. SLEEP(tRest);
  462. }
  463. return true;
  464. }
  465. /*
  466. Blocks until the last AbsoluteMove is finished (see the strategy at the top of protocolPTZ_ONVIF.h).
  467. Returns true when the move is over (or after the fixed wait / timeout), false only if there is no
  468. PTZ connection at all.
  469. */
  470. bool protocolPTZ_ONVIF::waitMoveIsDone(void)
  471. {
  472. if (m_pOnVif == NULL || m_pProfile == NULL)
  473. {
  474. return false;
  475. }
  476. if (! m_bReportsMoveStatus && ! m_bReportsPosition)
  477. {
  478. return fixedMoveWait(0);
  479. }
  480. long tStart = ptzOnvifNowMs();
  481. int tFailures = 0;
  482. bool tSeenMoving = false;
  483. bool tHavePrev = false;
  484. int tStableCount = 0;
  485. float tPrevPan = 0.0f;
  486. float tPrevTilt = 0.0f;
  487. float tPrevZoom = 0.0f;
  488. char tB[256];
  489. for (;;)
  490. {
  491. long tElapsed = ptzOnvifNowMs() - tStart;
  492. if (tElapsed >= PTZ_ONVIF_MOVE_TIMEOUT_MS)
  493. {
  494. LOG_ERROR("Timeout waiting for the end of the PTZ move!");
  495. return true;
  496. };
  497. ptzStatus tVal;
  498. if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal))
  499. {
  500. if (++tFailures >= PTZ_ONVIF_MAX_STATUS_FAILURES)
  501. {
  502. LOG_ERROR("GetStatus keeps failing, falling back to the fixed wait");
  503. return fixedMoveWait(ptzOnvifNowMs() - tStart);
  504. };
  505. SLEEP(PTZ_ONVIF_STATUS_POLL_MS);
  506. continue;
  507. };
  508. tFailures = 0;
  509. bool tNear = false;
  510. bool tStable = false;
  511. if (m_bReportsPosition && tVal.hasPosition)
  512. {
  513. tNear = m_bTargetValid && (ptzOnvifPanDistance(tVal.pan, m_fTargetPan) <= PTZ_ONVIF_POS_TOLERANCE) &&
  514. (fabsf(tVal.tilt - m_fTargetTilt) <= PTZ_ONVIF_POS_TOLERANCE) &&
  515. (! tVal.hasZoomPosition || fabsf(tVal.zoom - m_fTargetZoom) <= PTZ_ONVIF_POS_TOLERANCE);
  516. if (tHavePrev && ptzOnvifPanDistance(tVal.pan, tPrevPan) < PTZ_ONVIF_POS_STABLE_EPS && fabsf(tVal.tilt - tPrevTilt) < PTZ_ONVIF_POS_STABLE_EPS &&
  517. fabsf(tVal.zoom - tPrevZoom) < PTZ_ONVIF_POS_STABLE_EPS)
  518. {
  519. tStableCount++;
  520. }
  521. else
  522. {
  523. tStableCount = 0;
  524. };
  525. tPrevPan = tVal.pan;
  526. tPrevTilt = tVal.tilt;
  527. tPrevZoom = tVal.zoom;
  528. tHavePrev = true;
  529. tStable = (tStableCount >= PTZ_ONVIF_POS_STABLE_POLLS);
  530. };
  531. bool tAfterGrace = (tElapsed >= PTZ_ONVIF_MOVE_START_GRACE_MS);
  532. bool tDone = false;
  533. if (m_bReportsMoveStatus && tVal.hasMoveStatus)
  534. {
  535. // Right after the command a camera may still say IDLE: accept IDLE once it was seen
  536. // moving, once the target is reached, or after the start grace time.
  537. if (tVal.isMoving)
  538. {
  539. tSeenMoving = true;
  540. }
  541. else if (tSeenMoving || tNear || tAfterGrace)
  542. {
  543. tDone = true;
  544. };
  545. }
  546. else if (m_bReportsPosition && tVal.hasPosition)
  547. {
  548. tDone = tNear || (tStable && tAfterGrace);
  549. }
  550. else
  551. {
  552. // This reply carries neither MoveStatus nor Position
  553. return fixedMoveWait(tElapsed);
  554. };
  555. if (tDone)
  556. {
  557. #ifdef DEBUG_EXTRA
  558. snprintf(tB, sizeof(tB), "Finished Move in %ld ms (pos %.3f %.3f %.3f)", ptzOnvifNowMs() - tStart, tVal.pan, tVal.tilt, tVal.zoom);
  559. LOG(tB);
  560. #else
  561. (void) tB;
  562. #endif
  563. return true;
  564. };
  565. SLEEP(PTZ_ONVIF_STATUS_POLL_MS);
  566. };
  567. }
  568. #else
  569. bool protocolPTZ_ONVIF::waitMoveIsDone(void)
  570. {
  571. return false;
  572. }
  573. #endif
  574. bool protocolPTZ_ONVIF::goAbsolute(float dPan, float dTilt, float dZoom)
  575. {
  576. #ifdef USE_PONVIF
  577. // if (m_bStatus == 0) return false;
  578. #ifdef DEBUG_EXTRA
  579. LOG("ABS Move");
  580. #endif
  581. if (m_pProfile == NULL)
  582. {
  583. LOG_ERROR("No PTZ Profile!!");
  584. return false;
  585. };
  586. if (dZoom < 0.0f)
  587. {
  588. dZoom = 0.0f;
  589. }
  590. // Remember the target for waitMoveIsDone()
  591. m_fTargetPan = dPan;
  592. m_fTargetTilt = dTilt;
  593. m_fTargetZoom = dZoom;
  594. m_bTargetValid = true;
  595. // A SOAP Fault (e.g. position out of range) is logged by pOnvif; like before, the camera node is not
  596. // restarted because of it. Only a lost connection clears m_bStatus (validate() -> reconnect).
  597. commandResult(m_pOnVif->ptz_AbsoluteMove(m_pProfile->profiletoken, dPan, dTilt, dZoom));
  598. return true;
  599. #else
  600. if (m_bStatus == 0)
  601. {
  602. return false;
  603. }
  604. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  605. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  606. tt__PTZSpeed tSpeed;
  607. tt__PTZVector tPos;
  608. tSpeed.PanTilt = new tt__Vector2D();
  609. tSpeed.Zoom = new tt__Vector1D();
  610. tSpeed.PanTilt->x = 1.f;
  611. tSpeed.PanTilt->y = 1.f;
  612. tSpeed.Zoom->x = 1.f;
  613. tPos.PanTilt = new tt__Vector2D();
  614. tPos.Zoom = new tt__Vector1D();
  615. tPos.PanTilt->x = dPan;
  616. tPos.PanTilt->y = dTilt;
  617. tPos.Zoom->x = dZoom;
  618. int RetVal;
  619. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  620. delete(tSpeed.PanTilt);
  621. delete(tSpeed.Zoom);
  622. delete(tPos.PanTilt);
  623. delete(tPos.Zoom);
  624. return true;
  625. #endif
  626. }
  627. bool protocolPTZ_ONVIF::getCameraPresets(void)
  628. {
  629. LOG("GET PRESETS!");
  630. return m_pOnVif->ptz_GetPresets(m_pProfile->profiletoken);
  631. }
  632. bool protocolPTZ_ONVIF::goPreset(char * PresetToken)
  633. {
  634. #ifdef USE_PONVIF
  635. // if (m_bStatus == 0) return false;
  636. #ifdef DEBUG_EXTRA
  637. LOG("Preset Move");
  638. #endif
  639. if (m_pProfile == NULL)
  640. {
  641. LOG_ERROR("No PTZ Profile!!");
  642. return false;
  643. };
  644. // A preset move has no known target: waitMoveIsDone() must not compare with an old AbsoluteMove target
  645. m_bTargetValid = false;
  646. return commandResult(m_pOnVif->ptz_GotoPreset(m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f));
  647. #else
  648. if (m_bStatus == 0)
  649. {
  650. return false;
  651. }
  652. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  653. /*
  654. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  655. tt__PTZSpeed tSpeed;
  656. tt__PTZVector tPos;
  657. tSpeed.PanTilt = new tt__Vector2D();
  658. tSpeed.Zoom = new tt__Vector1D();
  659. tSpeed.PanTilt->x = 1.f;
  660. tSpeed.PanTilt->y = 1.f;
  661. tSpeed.Zoom->x = 1.f;
  662. tPos.PanTilt = new tt__Vector2D();
  663. tPos.Zoom = new tt__Vector1D();
  664. tPos.PanTilt->x = dPan;
  665. tPos.PanTilt->y = dTilt;
  666. tPos.Zoom->x = dZoom;
  667. int RetVal;
  668. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  669. delete(tSpeed.PanTilt);
  670. delete(tSpeed.Zoom);
  671. delete(tPos.PanTilt);
  672. delete(tPos.Zoom);
  673. */
  674. return true;
  675. #endif
  676. }
  677. bool protocolPTZ_ONVIF::validate()
  678. {
  679. return (m_bStatus > 0);
  680. }
  681. /*
  682. Liveness check while the camera is idle: GetStatus on the PTZ profile. A camera that answers (even with a
  683. SOAP Fault, e.g. GetStatus not supported) is reachable; only a communication error marks it lost.
  684. */
  685. bool protocolPTZ_ONVIF::probe(void)
  686. {
  687. #ifdef USE_PONVIF
  688. if (m_pOnVif == NULL || m_pProfile == NULL)
  689. {
  690. m_bStatus = 0;
  691. return false;
  692. }
  693. ptzStatus tVal;
  694. if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal))
  695. {
  696. commandResult(false);
  697. }
  698. else
  699. {
  700. m_bStatus = 1;
  701. }
  702. #endif
  703. return validate();
  704. }
  705. #undef LOG
  706. #undef LOG_ERROR