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.

727 lines
18 KiB

7 years ago
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 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
16 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 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
16 hours 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
16 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 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
16 hours 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
16 hours 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
16 hours ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
7 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
15 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 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. LOG("DONE");
  210. m_bStatus = 1;
  211. return true;
  212. }
  213. else
  214. {
  215. LOG_ERROR("pOnvif CREATE FAILURE!");
  216. delete(m_pOnVif);
  217. m_pOnVif = NULL;
  218. m_bStatus = 0;
  219. return false;
  220. };
  221. #else
  222. std::string A(m_pUser);
  223. std::string B(m_pPassword);
  224. std::string C(m_pDeviceURI);
  225. m_pDevice = new OnvifClientDevice(C, A, B);
  226. if (m_pDevice != NULL)
  227. {
  228. m_pPTZ = new OnvifClientPTZ(*m_pDevice);
  229. if (m_pPTZ != NULL)
  230. {
  231. _tptz__GetNodesResponse tNodes;
  232. int RetVal;
  233. RetVal = m_pPTZ->GetNodes(tNodes);
  234. if (RetVal == 0)
  235. {
  236. OnvifClientMedia media(*m_pDevice);
  237. _trt__GetProfilesResponse profiles;
  238. media.GetProfiles(profiles);
  239. m_pPTZToken = profiles.Profiles.at(0)->token; // PTZConfiguration->token;
  240. // m_pPTZToken = tNodes.PTZNode.at(0)->token;
  241. //_tptz__GetConfigurationOptionsResponse COResp;
  242. // int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken);
  243. //_tptz__GetConfigurationResponse configResponce;
  244. // configResponce.PTZConfiguration = new tt__PTZConfiguration();
  245. // m_pPTZ->GetConfiguration(configResponce);
  246. // m_pToken = configResponce.PTZConfiguration->token;
  247. setConnectionStatus(CS_CONNECTED);
  248. m_bStatus = 1;
  249. return true;
  250. };
  251. };
  252. };
  253. #endif
  254. return false;
  255. }
  256. #ifndef USE_PONVIF
  257. bool protocolPTZ_ONVIF::initCamera(void)
  258. {
  259. if (m_bStatus == 0)
  260. {
  261. return false;
  262. }
  263. // if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false;
  264. m_bLastCommandIssued = CMD_PTZ_INIT;
  265. m_pPTZ->Initialize();
  266. return true;
  267. }
  268. #endif
  269. bool protocolPTZ_ONVIF::move(uint32_t MoveMatrix)
  270. {
  271. // if( m_bStatus == 0 ) return false;
  272. #ifdef USE_PONVIF
  273. if (m_pProfile == NULL)
  274. {
  275. return false;
  276. }
  277. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  278. float Speed_x, Speed_y, Speed_z;
  279. if (MoveMatrix > 0)
  280. {
  281. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  282. {
  283. Speed_x = 0.0f;
  284. }
  285. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  286. {
  287. Speed_x = -m_dPanSpeed;
  288. }
  289. else
  290. {
  291. Speed_x = m_dPanSpeed;
  292. };
  293. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  294. {
  295. Speed_y = 0.0;
  296. }
  297. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  298. {
  299. Speed_y = m_dTiltSpeed;
  300. }
  301. else
  302. {
  303. Speed_y = -m_dTiltSpeed;
  304. };
  305. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  306. {
  307. Speed_z = 0.0;
  308. }
  309. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  310. {
  311. Speed_z = m_dZoomSpeed;
  312. }
  313. else
  314. {
  315. Speed_z = -m_dZoomSpeed;
  316. };
  317. // One request for all axes (two separate ContinuousMoves may stop each other)
  318. bool hasPT = ((MoveMatrix & (PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK)) != 0);
  319. bool hasZ = ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0);
  320. return commandResult(m_pOnVif->ptz_ContinuousMoveAll(m_pProfile->profiletoken, hasPT, Speed_x, Speed_y, hasZ, Speed_z));
  321. }
  322. return commandResult(m_pOnVif->ptz_Stop(m_pProfile->profiletoken, true, true));
  323. #else
  324. if (m_bStatus == 0)
  325. {
  326. return false;
  327. }
  328. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  329. _tptz__ContinuousMoveResponse ContMoveResponse;
  330. tt__PTZSpeed tSpeed;
  331. tSpeed.PanTilt = new tt__Vector2D();
  332. tSpeed.Zoom = new tt__Vector1D();
  333. LONG64 to = 1000;
  334. int RetVal;
  335. if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0)
  336. {
  337. tSpeed.PanTilt->x = 0.0;
  338. }
  339. else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0)
  340. {
  341. tSpeed.PanTilt->x = -m_dPanSpeed;
  342. }
  343. else
  344. {
  345. tSpeed.PanTilt->x = m_dPanSpeed;
  346. };
  347. if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0)
  348. {
  349. tSpeed.PanTilt->y = 0.0;
  350. }
  351. else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0)
  352. {
  353. tSpeed.PanTilt->y = m_dTiltSpeed;
  354. }
  355. else
  356. {
  357. tSpeed.PanTilt->y = -m_dTiltSpeed;
  358. };
  359. if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0)
  360. {
  361. tSpeed.Zoom->x = 0.0;
  362. }
  363. else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0)
  364. {
  365. tSpeed.Zoom->x = m_dZoomSpeed;
  366. }
  367. else
  368. {
  369. tSpeed.Zoom->x = -m_dZoomSpeed;
  370. };
  371. RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken);
  372. delete(tSpeed.PanTilt);
  373. delete(tSpeed.Zoom);
  374. if (RetVal)
  375. {
  376. return false;
  377. }
  378. return true;
  379. #endif
  380. }
  381. #include <math.h>
  382. #include <time.h>
  383. #ifdef USE_PONVIF
  384. /*
  385. Maps a pOnvif command result to the connection status: only a communication error means the camera is
  386. lost (m_bStatus = 0 -> validate() fails -> reconnect). A SOAP Fault / HTTP error is an answer from a
  387. reachable camera: the command failed, the connection is fine.
  388. */
  389. bool protocolPTZ_ONVIF::commandResult(bool ok)
  390. {
  391. if (ok)
  392. {
  393. m_bStatus = 1;
  394. return true;
  395. }
  396. if (m_pOnVif == NULL || m_pOnVif->getLastError() == PONVIF_ERR_COMM)
  397. {
  398. m_bStatus = 0;
  399. }
  400. return false;
  401. }
  402. static long ptzOnvifNowMs(void)
  403. {
  404. struct timespec tTS;
  405. clock_gettime(CLOCK_MONOTONIC, &tTS);
  406. return (long) tTS.tv_sec * 1000L + tTS.tv_nsec / 1000000L;
  407. }
  408. // Pan in the generic space wraps around (-1 == +1 on 360 degree cameras)
  409. static float ptzOnvifPanDistance(float a, float b)
  410. {
  411. float d = fabsf(a - b);
  412. if (d > 1.0f)
  413. {
  414. d = 2.0f - d;
  415. }
  416. return d;
  417. }
  418. /*
  419. Finds out once (after create) how the end of a move can be detected for this camera.
  420. The PTZ service capabilities (MoveStatus / StatusPosition attributes) are asked first; many cameras
  421. don't implement GetServiceCapabilities or leave the attributes out, so a real GetStatus decides.
  422. An explicit "false" in the capabilities wins over a GetStatus reply (some cameras send fixed values).
  423. */
  424. void protocolPTZ_ONVIF::detectStatusSupport(void)
  425. {
  426. m_bReportsMoveStatus = false;
  427. m_bReportsPosition = false;
  428. if (m_pOnVif == NULL || m_pProfile == NULL)
  429. {
  430. return;
  431. }
  432. int8_t capMoveStatus = -1;
  433. int8_t capStatusPosition = -1;
  434. m_pOnVif->ptz_GetServiceCapabilities(&capMoveStatus, &capStatusPosition);
  435. ptzStatus tVal;
  436. bool tProbe = m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal);
  437. m_bReportsMoveStatus = tProbe && tVal.hasMoveStatus && (capMoveStatus != 0);
  438. m_bReportsPosition = tProbe && tVal.hasPosition && (capStatusPosition != 0);
  439. char tB[256];
  440. snprintf(tB, sizeof(tB), "PTZ status support: MoveStatus=%s Position=%s (GetStatus %s, capabilities MoveStatus=%d StatusPosition=%d) -> %s",
  441. m_bReportsMoveStatus ? "yes" : "no", m_bReportsPosition ? "yes" : "no", tProbe ? "ok" : "failed", capMoveStatus, capStatusPosition,
  442. m_bReportsMoveStatus ? "wait for IDLE" : (m_bReportsPosition ? "wait for position" : "fixed wait"));
  443. LOG(tB);
  444. }
  445. bool protocolPTZ_ONVIF::fixedMoveWait(long alreadyWaitedMs)
  446. {
  447. long tRest = PTZ_ONVIF_FIXED_MOVE_WAIT_MS - alreadyWaitedMs;
  448. if (tRest > 0)
  449. {
  450. SLEEP(tRest);
  451. }
  452. return true;
  453. }
  454. /*
  455. Blocks until the last AbsoluteMove is finished (see the strategy at the top of protocolPTZ_ONVIF.h).
  456. Returns true when the move is over (or after the fixed wait / timeout), false only if there is no
  457. PTZ connection at all.
  458. */
  459. bool protocolPTZ_ONVIF::waitMoveIsDone(void)
  460. {
  461. if (m_pOnVif == NULL || m_pProfile == NULL)
  462. {
  463. return false;
  464. }
  465. if (! m_bReportsMoveStatus && ! m_bReportsPosition)
  466. {
  467. return fixedMoveWait(0);
  468. }
  469. long tStart = ptzOnvifNowMs();
  470. int tFailures = 0;
  471. bool tSeenMoving = false;
  472. bool tHavePrev = false;
  473. int tStableCount = 0;
  474. float tPrevPan = 0.0f;
  475. float tPrevTilt = 0.0f;
  476. float tPrevZoom = 0.0f;
  477. char tB[256];
  478. for (;;)
  479. {
  480. long tElapsed = ptzOnvifNowMs() - tStart;
  481. if (tElapsed >= PTZ_ONVIF_MOVE_TIMEOUT_MS)
  482. {
  483. LOG_ERROR("Timeout waiting for the end of the PTZ move!");
  484. return true;
  485. };
  486. ptzStatus tVal;
  487. if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal))
  488. {
  489. if (++tFailures >= PTZ_ONVIF_MAX_STATUS_FAILURES)
  490. {
  491. LOG_ERROR("GetStatus keeps failing, falling back to the fixed wait");
  492. return fixedMoveWait(ptzOnvifNowMs() - tStart);
  493. };
  494. SLEEP(PTZ_ONVIF_STATUS_POLL_MS);
  495. continue;
  496. };
  497. tFailures = 0;
  498. bool tNear = false;
  499. bool tStable = false;
  500. if (m_bReportsPosition && tVal.hasPosition)
  501. {
  502. tNear = m_bTargetValid && (ptzOnvifPanDistance(tVal.pan, m_fTargetPan) <= PTZ_ONVIF_POS_TOLERANCE) &&
  503. (fabsf(tVal.tilt - m_fTargetTilt) <= PTZ_ONVIF_POS_TOLERANCE) &&
  504. (! tVal.hasZoomPosition || fabsf(tVal.zoom - m_fTargetZoom) <= PTZ_ONVIF_POS_TOLERANCE);
  505. if (tHavePrev && ptzOnvifPanDistance(tVal.pan, tPrevPan) < PTZ_ONVIF_POS_STABLE_EPS && fabsf(tVal.tilt - tPrevTilt) < PTZ_ONVIF_POS_STABLE_EPS &&
  506. fabsf(tVal.zoom - tPrevZoom) < PTZ_ONVIF_POS_STABLE_EPS)
  507. {
  508. tStableCount++;
  509. }
  510. else
  511. {
  512. tStableCount = 0;
  513. };
  514. tPrevPan = tVal.pan;
  515. tPrevTilt = tVal.tilt;
  516. tPrevZoom = tVal.zoom;
  517. tHavePrev = true;
  518. tStable = (tStableCount >= PTZ_ONVIF_POS_STABLE_POLLS);
  519. };
  520. bool tAfterGrace = (tElapsed >= PTZ_ONVIF_MOVE_START_GRACE_MS);
  521. bool tDone = false;
  522. if (m_bReportsMoveStatus && tVal.hasMoveStatus)
  523. {
  524. // Right after the command a camera may still say IDLE: accept IDLE once it was seen
  525. // moving, once the target is reached, or after the start grace time.
  526. if (tVal.isMoving)
  527. {
  528. tSeenMoving = true;
  529. }
  530. else if (tSeenMoving || tNear || tAfterGrace)
  531. {
  532. tDone = true;
  533. };
  534. }
  535. else if (m_bReportsPosition && tVal.hasPosition)
  536. {
  537. tDone = tNear || (tStable && tAfterGrace);
  538. }
  539. else
  540. {
  541. // This reply carries neither MoveStatus nor Position
  542. return fixedMoveWait(tElapsed);
  543. };
  544. if (tDone)
  545. {
  546. #ifdef DEBUG_EXTRA
  547. snprintf(tB, sizeof(tB), "Finished Move in %ld ms (pos %.3f %.3f %.3f)", ptzOnvifNowMs() - tStart, tVal.pan, tVal.tilt, tVal.zoom);
  548. LOG(tB);
  549. #else
  550. (void) tB;
  551. #endif
  552. return true;
  553. };
  554. SLEEP(PTZ_ONVIF_STATUS_POLL_MS);
  555. };
  556. }
  557. #else
  558. bool protocolPTZ_ONVIF::waitMoveIsDone(void)
  559. {
  560. return false;
  561. }
  562. #endif
  563. bool protocolPTZ_ONVIF::goAbsolute(float dPan, float dTilt, float dZoom)
  564. {
  565. #ifdef USE_PONVIF
  566. // if (m_bStatus == 0) return false;
  567. #ifdef DEBUG_EXTRA
  568. LOG("ABS Move");
  569. #endif
  570. if (m_pProfile == NULL)
  571. {
  572. LOG_ERROR("No PTZ Profile!!");
  573. return false;
  574. };
  575. if (dZoom < 0.0f)
  576. {
  577. dZoom = 0.0f;
  578. }
  579. // Remember the target for waitMoveIsDone()
  580. m_fTargetPan = dPan;
  581. m_fTargetTilt = dTilt;
  582. m_fTargetZoom = dZoom;
  583. m_bTargetValid = true;
  584. // A SOAP Fault (e.g. position out of range) is logged by pOnvif; like before, the camera node is not
  585. // restarted because of it. Only a lost connection clears m_bStatus (validate() -> reconnect).
  586. commandResult(m_pOnVif->ptz_AbsoluteMove(m_pProfile->profiletoken, dPan, dTilt, dZoom));
  587. return true;
  588. #else
  589. if (m_bStatus == 0)
  590. {
  591. return false;
  592. }
  593. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  594. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  595. tt__PTZSpeed tSpeed;
  596. tt__PTZVector tPos;
  597. tSpeed.PanTilt = new tt__Vector2D();
  598. tSpeed.Zoom = new tt__Vector1D();
  599. tSpeed.PanTilt->x = 1.f;
  600. tSpeed.PanTilt->y = 1.f;
  601. tSpeed.Zoom->x = 1.f;
  602. tPos.PanTilt = new tt__Vector2D();
  603. tPos.Zoom = new tt__Vector1D();
  604. tPos.PanTilt->x = dPan;
  605. tPos.PanTilt->y = dTilt;
  606. tPos.Zoom->x = dZoom;
  607. int RetVal;
  608. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  609. delete(tSpeed.PanTilt);
  610. delete(tSpeed.Zoom);
  611. delete(tPos.PanTilt);
  612. delete(tPos.Zoom);
  613. return true;
  614. #endif
  615. }
  616. bool protocolPTZ_ONVIF::getCameraPresets(void)
  617. {
  618. LOG("GET PRESETS!");
  619. return m_pOnVif->ptz_GetPresets(m_pProfile->profiletoken);
  620. }
  621. bool protocolPTZ_ONVIF::goPreset(char * PresetToken)
  622. {
  623. #ifdef USE_PONVIF
  624. // if (m_bStatus == 0) return false;
  625. #ifdef DEBUG_EXTRA
  626. LOG("Preset Move");
  627. #endif
  628. if (m_pProfile == NULL)
  629. {
  630. LOG_ERROR("No PTZ Profile!!");
  631. return false;
  632. };
  633. // A preset move has no known target: waitMoveIsDone() must not compare with an old AbsoluteMove target
  634. m_bTargetValid = false;
  635. return commandResult(m_pOnVif->ptz_GotoPreset(m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f));
  636. #else
  637. if (m_bStatus == 0)
  638. {
  639. return false;
  640. }
  641. // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false;
  642. /*
  643. _tptz__AbsoluteMoveResponse AbsMoveResponse;
  644. tt__PTZSpeed tSpeed;
  645. tt__PTZVector tPos;
  646. tSpeed.PanTilt = new tt__Vector2D();
  647. tSpeed.Zoom = new tt__Vector1D();
  648. tSpeed.PanTilt->x = 1.f;
  649. tSpeed.PanTilt->y = 1.f;
  650. tSpeed.Zoom->x = 1.f;
  651. tPos.PanTilt = new tt__Vector2D();
  652. tPos.Zoom = new tt__Vector1D();
  653. tPos.PanTilt->x = dPan;
  654. tPos.PanTilt->y = dTilt;
  655. tPos.Zoom->x = dZoom;
  656. int RetVal;
  657. RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken);
  658. delete(tSpeed.PanTilt);
  659. delete(tSpeed.Zoom);
  660. delete(tPos.PanTilt);
  661. delete(tPos.Zoom);
  662. */
  663. return true;
  664. #endif
  665. }
  666. bool protocolPTZ_ONVIF::validate()
  667. {
  668. return (m_bStatus > 0);
  669. }
  670. /*
  671. Liveness check while the camera is idle: GetStatus on the PTZ profile. A camera that answers (even with a
  672. SOAP Fault, e.g. GetStatus not supported) is reachable; only a communication error marks it lost.
  673. */
  674. bool protocolPTZ_ONVIF::probe(void)
  675. {
  676. #ifdef USE_PONVIF
  677. if (m_pOnVif == NULL || m_pProfile == NULL)
  678. {
  679. m_bStatus = 0;
  680. return false;
  681. }
  682. ptzStatus tVal;
  683. if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal))
  684. {
  685. commandResult(false);
  686. }
  687. else
  688. {
  689. m_bStatus = 1;
  690. }
  691. #endif
  692. return validate();
  693. }
  694. #undef LOG
  695. #undef LOG_ERROR