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.

2511 lines
75 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "ponvif.h"
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h> /* See NOTES */
  7. #include <sys/types.h>
  8. // http://dspace.nplg.gov.ge/handle/1234/3051
  9. // http://dspace.nplg.gov.ge/handle/1234/3057
  10. // http://dspace.nplg.gov.ge/handle/1234/3061
  11. #define LOG(X) m_pLog->Log("PONVIF", this, X)
  12. #define TLOG(X) basicLogCollection::getInstance()->Log("PONVIF_T", NULL, X)
  13. #ifdef DEBUG
  14. #define LOG_ERROR(X) \
  15. { \
  16. char _tBfrErrMSG2w34[512]; \
  17. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  18. m_pLog->Log("PONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  19. }
  20. #else
  21. #define LOG_ERROR(X) m_pLog->Log("PONVIF", this, _LOG_ERROR, X);
  22. #endif
  23. inline bool checkBool(const char * A)
  24. {
  25. if (A == NULL)
  26. {
  27. return false;
  28. }
  29. return (A[0] == 'T' || A[0] == 't' || A[0] == '1');
  30. }
  31. inline bool checkOnOff(const char * A)
  32. {
  33. if (A == NULL)
  34. {
  35. return false;
  36. }
  37. return (A[1] == 'N' || A[1] == 'n' || A[0] == '1');
  38. }
  39. inline onvifOnOffAuto checkOnOffAuto(const char * A)
  40. {
  41. if (A == NULL)
  42. {
  43. return AUTO;
  44. }
  45. if (A[1] == 'N' || A[1] == 'n' || A[0] == '1')
  46. {
  47. return ON;
  48. }
  49. return OFF;
  50. }
  51. pOnvif::pOnvif()
  52. {
  53. m_pLog = basicLogCollection::getInstance();
  54. m_pIpAddress = NULL;
  55. m_pUsername = NULL;
  56. m_pPassword = NULL;
  57. m_pMediaURI = NULL;
  58. m_pSnapshotURI = NULL;
  59. m_pDeviceURI = NULL;
  60. m_pPTZURI = NULL;
  61. m_pBaseURI = NULL;
  62. m_nDeltaTime = 0;
  63. m_pCURL = NULL;
  64. m_pProxyHost = NULL;
  65. m_nProxyPort = 0;
  66. m_pProxyUsername = NULL;
  67. m_pProxyPassword = NULL;
  68. m_pLastResponse = NULL;
  69. m_bIntransingent = true;
  70. m_nDiscoveryTimeout = PONVIF_DEFAULT_DISCOVERYTIMEOUT;
  71. m_pDiscoveryBindIP = NULL; // PONVIF_DEFAULT_DISCOVERYBINDIP;
  72. m_pDiscoveryMCastIP = NULL; // PONVIF_DEFAULT_DISCOVERYMCASTIP;
  73. m_nDiscoveryMCastPort = PONVIF_DEFAULT_DISCOVERYMCASTPORT;
  74. m_bDiscoveryHideDuplicates = true;
  75. m_pHttpAccessManager = NULL;
  76. memset( &m_sCapabilities, 0, sizeof( nodeCapability ) );
  77. m_bAuthMode = 0;
  78. LOG("Constructor Done!");
  79. }
  80. void pOnvif::cleanNodeCapability( nodeCapability *ptr ){
  81. LOG("A");
  82. if ( ptr->Analytics ) free( ptr->Analytics );
  83. LOG("B");
  84. if ( ptr->Device ){
  85. LOG("1");
  86. if ( ptr->Device->Network ){
  87. LOG("1a");
  88. if ( ptr->Device->Network->Extension) {
  89. if ( ptr->Device->Network->Extension->Extension ) free ( ptr->Device->Network->Extension->Extension );
  90. free( ptr->Device->Network->Extension );
  91. };
  92. free( ptr->Device->Network );
  93. };
  94. LOG("2");
  95. if ( ptr->Device->System ){
  96. LOG("2a");
  97. if( ptr->Device->System->SupportedVersions ) free( ptr->Device->System->SupportedVersions );
  98. if ( ptr->Device->System->Extension) {
  99. if ( ptr->Device->System->Extension->Extension ) free ( ptr->Device->System->Extension->Extension );
  100. free( ptr->Device->System->Extension );
  101. };
  102. free( ptr->Device->System );
  103. };
  104. LOG("3");
  105. if ( ptr->Device->IO ){
  106. LOG("3a");
  107. if ( ptr->Device->IO->Extension) {
  108. if( ptr->Device->IO->Extension->AuxiliaryCommands ) free( ptr->Device->IO->Extension->AuxiliaryCommands );
  109. if ( ptr->Device->IO->Extension->Extension ) free ( ptr->Device->IO->Extension->Extension );
  110. free( ptr->Device->IO->Extension );
  111. };
  112. free( ptr->Device->IO );
  113. };
  114. LOG("4");
  115. if ( ptr->Device->Security ){
  116. LOG("4a");
  117. if ( ptr->Device->Security->Extension) {
  118. if ( ptr->Device->Security->Extension->Extension ) free ( ptr->Device->Security->Extension->Extension );
  119. free( ptr->Device->Security->Extension );
  120. };
  121. free( ptr->Device->Security );
  122. };
  123. LOG("5");
  124. if ( ptr->Device->Extension ) {
  125. LOG("5a");
  126. free( ptr->Device->Extension );
  127. }
  128. LOG("6");
  129. free( ptr->Device );
  130. LOG("62");
  131. };
  132. LOG("C");
  133. if ( ptr->Events ) free( ptr->Events );
  134. LOG("D");
  135. if ( ptr->Imaging ) free( ptr->Imaging );
  136. LOG("E");
  137. if ( ptr->Media ){
  138. if ( ptr->Media->StreamingCapabilities ) {
  139. if ( ptr->Media->StreamingCapabilities->Extension ) free( ptr->Media->StreamingCapabilities->Extension );
  140. free( ptr->Media->StreamingCapabilities );
  141. };
  142. if ( ptr->Media->Extension ) {
  143. if ( ptr->Media->Extension->ProfileCapabilities ) free( ptr->Media->Extension->ProfileCapabilities );
  144. free( ptr->Media->Extension );
  145. };
  146. };
  147. LOG("F");
  148. if ( ptr->PTZ ) free( ptr->PTZ );
  149. LOG("G");
  150. if ( ptr->Extension ){
  151. if ( ptr->Extension->DeviceIO ) free( ptr->Extension->DeviceIO );
  152. if ( ptr->Extension->Display ) free( ptr->Extension->Display );
  153. if ( ptr->Extension->Recording ) free( ptr->Extension->Recording );
  154. if ( ptr->Extension->Search ) free( ptr->Extension->Search );
  155. if ( ptr->Extension->Replay ) free( ptr->Extension->Replay );
  156. if ( ptr->Extension->Receiver ) free( ptr->Extension->Receiver );
  157. if ( ptr->Extension->AnalyticsDevice ) {
  158. if ( ptr->Extension->AnalyticsDevice->Extension ) free( ptr->Extension->AnalyticsDevice->Extension );
  159. free(ptr->Extension->AnalyticsDevice);
  160. };
  161. if ( ptr->Extension->Extensions ) free( ptr->Extension->Extensions );
  162. free(ptr->Extension);
  163. };
  164. };
  165. pOnvif::~pOnvif()
  166. {
  167. #ifdef DEBUG
  168. LOG("Destructor begin");
  169. #endif
  170. if (m_pIpAddress)
  171. {
  172. LOG("1");
  173. free(m_pIpAddress);
  174. }
  175. if (m_pUsername)
  176. {
  177. LOG("2");
  178. free(m_pUsername);
  179. }
  180. if (m_pPassword)
  181. {
  182. LOG("3");
  183. free(m_pPassword);
  184. }
  185. //if (m_pMediaURI)
  186. //{
  187. // LOG("4");
  188. // free(m_pMediaURI);
  189. //}
  190. if (m_pSnapshotURI)
  191. {
  192. LOG("5");
  193. free(m_pSnapshotURI);
  194. }
  195. //if (m_pDeviceURI)
  196. //{
  197. // LOG("6");
  198. // free(m_pDeviceURI);
  199. //}
  200. //if (m_pPTZURI)
  201. //{
  202. // LOG("7");
  203. // free(m_pPTZURI);
  204. //}
  205. if (m_pBaseURI)
  206. {
  207. LOG("8");
  208. free(m_pBaseURI);
  209. }
  210. if (m_pProxyHost)
  211. {
  212. LOG("9");
  213. free(m_pProxyHost);
  214. }
  215. if (m_pProxyUsername)
  216. {
  217. LOG("10");
  218. free(m_pProxyUsername);
  219. }
  220. if (m_pProxyPassword)
  221. {
  222. LOG("11");
  223. free(m_pProxyPassword);
  224. }
  225. if (m_pLastResponse)
  226. {
  227. LOG("12");
  228. free(m_pLastResponse);
  229. }
  230. if (m_pDiscoveryBindIP)
  231. {
  232. LOG("13");
  233. free(m_pDiscoveryBindIP);
  234. }
  235. if (m_pDiscoveryMCastIP)
  236. {
  237. LOG("14");
  238. free(m_pDiscoveryMCastIP);
  239. }
  240. cleanNodeCapability( &m_sCapabilities );
  241. LOG("CNC Done!");
  242. if (m_pCURL)
  243. {
  244. #ifdef DEBUG
  245. LOG("Try Cleanup CURL!");
  246. #endif
  247. curl_easy_cleanup(m_pCURL);
  248. m_pCURL = NULL;
  249. };
  250. #ifdef DEBUG
  251. LOG("Destructor Done!");
  252. #endif
  253. }
  254. bool pOnvif::setProxyHost(CSTR proxyHost)
  255. {
  256. if (m_pProxyHost != NULL)
  257. {
  258. free(m_pProxyHost);
  259. }
  260. if (proxyHost == NULL || strlen(proxyHost) == 0)
  261. {
  262. m_pProxyHost = NULL;
  263. return true;
  264. };
  265. m_pProxyHost = (CSTR) malloc(strlen(proxyHost) + 1);
  266. if (m_pProxyHost == NULL)
  267. {
  268. return false;
  269. }
  270. memcpy(m_pProxyHost, proxyHost, strlen(proxyHost) + 1);
  271. return true;
  272. }
  273. CSTR pOnvif::getProxyHost()
  274. {
  275. return m_pProxyHost;
  276. }
  277. bool pOnvif::setProxyPort(int proxyPort)
  278. {
  279. m_nProxyPort = proxyPort;
  280. return true;
  281. }
  282. int pOnvif::getProxyPort(void)
  283. {
  284. return m_nProxyPort;
  285. }
  286. bool pOnvif::setProxyUsername(CSTR proxyUsername)
  287. {
  288. if (m_pProxyUsername != NULL)
  289. {
  290. free(m_pProxyUsername);
  291. }
  292. if (proxyUsername == NULL || strlen(proxyUsername) == 0)
  293. {
  294. m_pProxyUsername = NULL;
  295. return true;
  296. };
  297. m_pProxyUsername = (CSTR) malloc(strlen(proxyUsername) + 1);
  298. if (m_pProxyUsername == NULL)
  299. {
  300. return false;
  301. }
  302. memcpy(m_pProxyUsername, proxyUsername, strlen(proxyUsername) + 1);
  303. return true;
  304. }
  305. CSTR pOnvif::getProxyUsername(void)
  306. {
  307. return m_pProxyUsername;
  308. }
  309. bool pOnvif::setProxyPassword(CSTR proxyPassword)
  310. {
  311. if (m_pProxyPassword != NULL)
  312. {
  313. free(m_pProxyPassword);
  314. }
  315. if (proxyPassword == NULL || strlen(proxyPassword) == 0)
  316. {
  317. m_pProxyPassword = NULL;
  318. return true;
  319. };
  320. m_pProxyPassword = (CSTR) malloc(strlen(proxyPassword) + 1);
  321. if (m_pProxyPassword == NULL)
  322. {
  323. return false;
  324. }
  325. memcpy(m_pProxyPassword, proxyPassword, strlen(proxyPassword) + 1);
  326. return true;
  327. }
  328. CSTR pOnvif::getProxyPassword()
  329. {
  330. return m_pProxyPassword;
  331. }
  332. bool pOnvif::setUsername(CSTR username)
  333. {
  334. if (m_pUsername != NULL)
  335. {
  336. free(m_pUsername);
  337. }
  338. if (username == NULL || strlen(username) == 0)
  339. {
  340. m_pUsername = NULL;
  341. return true;
  342. };
  343. m_pUsername = (CSTR) malloc(strlen(username) + 1);
  344. if (m_pUsername == NULL)
  345. {
  346. return false;
  347. }
  348. memcpy(m_pUsername, username, strlen(username) + 1);
  349. return true;
  350. }
  351. CSTR pOnvif::getUsername(void)
  352. {
  353. return m_pUsername;
  354. }
  355. bool pOnvif::setPassword(CSTR password)
  356. {
  357. if (m_pPassword != NULL)
  358. {
  359. free(m_pPassword);
  360. }
  361. if (password == NULL || strlen(password) == 0)
  362. {
  363. m_pPassword = NULL;
  364. return true;
  365. };
  366. m_pPassword = (CSTR) malloc(strlen(password) + 1);
  367. if (m_pPassword == NULL)
  368. {
  369. return false;
  370. }
  371. memcpy(m_pPassword, password, strlen(password) + 1);
  372. return true;
  373. }
  374. CSTR pOnvif::getPassword(void)
  375. {
  376. return m_pPassword;
  377. }
  378. CSTR pOnvif::getDeviceUri(void)
  379. {
  380. return m_pDeviceURI;
  381. }
  382. bool pOnvif::setDeviceUri(CSTR deviceUri)
  383. {
  384. if (deviceUri == NULL || strlen(deviceUri) == 0)
  385. {
  386. m_pDeviceURI = NULL;
  387. return false;
  388. };
  389. if (strstr(deviceUri, "http") == NULL)
  390. {
  391. LOG_ERROR("Malformed device URI");
  392. return false;
  393. };
  394. if (m_pDeviceURI != NULL)
  395. {
  396. free(m_pDeviceURI);
  397. }
  398. m_pDeviceURI = (CSTR) malloc(strlen(deviceUri) + 1);
  399. if (m_pDeviceURI == NULL)
  400. {
  401. return false;
  402. }
  403. memcpy(m_pDeviceURI, deviceUri, strlen(deviceUri));
  404. m_pDeviceURI[strlen(deviceUri)] = (char) 0;
  405. // toDo: Must parsed Host/IP
  406. char * tT = strstr(m_pDeviceURI, "/");
  407. if (tT)
  408. {
  409. tT += 2;
  410. char * tB = strstr(tT, "/");
  411. int len = tB - tT + 1;
  412. m_pIpAddress = (CSTR) malloc(len);
  413. if (m_pIpAddress)
  414. {
  415. len--;
  416. memcpy(m_pIpAddress, tT, len);
  417. m_pIpAddress[len] = (char) 0;
  418. LOG(m_pIpAddress);
  419. };
  420. };
  421. return true;
  422. }
  423. CSTR pOnvif::getIPAddress(void)
  424. {
  425. return m_pIpAddress;
  426. }
  427. bool pOnvif::setIPAddress(CSTR ipAddress)
  428. {
  429. if (ipAddress == NULL || strlen(ipAddress) == 0)
  430. {
  431. m_pIpAddress = NULL;
  432. return false;
  433. };
  434. if (m_pIpAddress != NULL)
  435. {
  436. free(m_pIpAddress);
  437. }
  438. m_pIpAddress = (CSTR) malloc(strlen(ipAddress) + 1);
  439. if (m_pIpAddress == NULL)
  440. {
  441. return false;
  442. }
  443. memcpy(m_pIpAddress, ipAddress, strlen(ipAddress));
  444. m_pIpAddress[strlen(ipAddress)] = (char) 0;
  445. // ToDo: Must be created URI ... sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  446. int len = strlen(ipAddress) + 28;
  447. m_pDeviceURI = (CSTR) malloc(len + 1);
  448. if (m_pDeviceURI)
  449. {
  450. sprintf(m_pDeviceURI, "http://%s/onvif/device_service", m_pIpAddress);
  451. LOG(m_pDeviceURI);
  452. };
  453. return true;
  454. }
  455. videoSources * pOnvif::getSources(void)
  456. {
  457. return &m_vVideoSources;
  458. }
  459. videoSourceProfiles * pOnvif::getProfiles(void)
  460. {
  461. return &m_vVideoProfiles;
  462. }
  463. videoSourceProfiles * pOnvif::getActiveProfiles(void)
  464. {
  465. return &m_vActiveSources;
  466. }
  467. CSTR pOnvif::getMediaUri(void)
  468. {
  469. return m_pMediaURI;
  470. }
  471. CSTR pOnvif::getPTZUri(void)
  472. {
  473. return m_pPTZURI;
  474. }
  475. CSTR pOnvif::getBaseUrl(void)
  476. {
  477. return m_pBaseURI;
  478. }
  479. void pOnvif::getSupportedVersion(void) {}
  480. nodeCapability * pOnvif::getCapabilities(void)
  481. {
  482. return &m_sCapabilities;
  483. }
  484. bool pOnvif::setBreakOnError(bool intransingent)
  485. {
  486. m_bIntransingent = intransingent;
  487. return true;
  488. }
  489. CSTR pOnvif::getLastResponse(void)
  490. {
  491. return m_pLastResponse;
  492. }
  493. bool pOnvif::setDiscoveryTimeout(int discoverytimeout)
  494. {
  495. m_nDiscoveryTimeout = discoverytimeout;
  496. return true;
  497. }
  498. bool pOnvif::setDiscoveryBindIp(CSTR discoverybindip)
  499. {
  500. if (m_pDiscoveryBindIP != NULL)
  501. {
  502. free(m_pDiscoveryBindIP);
  503. }
  504. if (discoverybindip == NULL || strlen(discoverybindip) == 0)
  505. {
  506. m_pDiscoveryBindIP = NULL;
  507. return true;
  508. };
  509. m_pDiscoveryBindIP = (CSTR) malloc(strlen(discoverybindip) + 1);
  510. if (m_pDiscoveryBindIP == NULL)
  511. {
  512. return false;
  513. }
  514. memcpy(m_pDiscoveryBindIP, discoverybindip, strlen(discoverybindip) + 1);
  515. return true;
  516. }
  517. bool pOnvif::setDiscoveryMcastIp(CSTR discoverymcastip)
  518. {
  519. if (m_pDiscoveryMCastIP != NULL)
  520. {
  521. free(m_pDiscoveryMCastIP);
  522. }
  523. if (discoverymcastip == NULL || strlen(discoverymcastip) == 0)
  524. {
  525. m_pDiscoveryMCastIP = NULL;
  526. return true;
  527. };
  528. m_pDiscoveryMCastIP = (CSTR) malloc(strlen(discoverymcastip) + 1);
  529. if (m_pDiscoveryMCastIP == NULL)
  530. {
  531. return false;
  532. }
  533. memcpy(m_pDiscoveryMCastIP, discoverymcastip, strlen(discoverymcastip) + 1);
  534. return true;
  535. }
  536. bool pOnvif::setDiscoveryMcastPort(int discoverymcastport)
  537. {
  538. m_nDiscoveryMCastPort = discoverymcastport;
  539. return true;
  540. }
  541. bool pOnvif::setDiscoveryHideDuplicates(bool discoveryhideduplicates)
  542. {
  543. m_bDiscoveryHideDuplicates = discoveryhideduplicates;
  544. return true;
  545. }
  546. CSTR pOnvif::getSnapshotUri(void)
  547. {
  548. return m_pSnapshotURI;
  549. }
  550. /*
  551. WS-Discovery
  552. https://www.geeksforgeeks.org/udp-server-client-implementation-c/
  553. */
  554. discoveryResults pOnvif::discover(void)
  555. {
  556. discoveryResults RetVal;
  557. time_t tTimeout = time(NULL) + m_nDiscoveryTimeout;
  558. CSTR tPostString = PONVIF_PACK_DISCOVERY;
  559. struct sockaddr_in tSockAddr;
  560. struct hostent * h_info;
  561. #ifdef __linux__
  562. int tSocket;
  563. #else
  564. int tSocket;
  565. #endif
  566. int RetCode;
  567. char Response[DEFAULT_BUFFER_SIZE];
  568. try
  569. {
  570. // SOL_UDP = 17
  571. tSocket = socket(AF_INET, SOCK_DGRAM, 0);
  572. if (tSocket < 0)
  573. {
  574. LOG_ERROR("ERROR creating socket");
  575. return RetVal;
  576. };
  577. LOG("Socket Created!");
  578. memset(&tSockAddr, 0, sizeof(struct sockaddr_in));
  579. h_info = gethostbyname(m_pDiscoveryBindIP);
  580. if (h_info == NULL)
  581. {
  582. LOG_ERROR("Some throuble with networking on this machine (Cannot Get Hostname)!");
  583. return RetVal;
  584. };
  585. LOG("Host Found!");
  586. //*****************************************
  587. //* there we building our network address *
  588. //*****************************************
  589. memcpy(h_info->h_addr, &(tSockAddr.sin_addr), h_info->h_length);
  590. tSockAddr.sin_family = h_info->h_addrtype;
  591. tSockAddr.sin_port = htons(20000 + (20000 * rand()) / RAND_MAX);
  592. RetCode = bind(tSocket, (const sockaddr *) &tSockAddr, sizeof(struct sockaddr_in));
  593. if (RetCode != 0)
  594. {
  595. LOG_ERROR("Error Binding to Port/Address");
  596. return RetVal;
  597. };
  598. LOG("Binding Done!");
  599. #ifdef __linux__
  600. // setsockopt( tSocket, IPPROTO_IP, MCAST_JOIN_GROUP, );
  601. int tVal = 1;
  602. setsockopt(tSocket, SOL_SOCKET, SO_BROADCAST, &tVal, sizeof(int));
  603. // socket_sendto($sock, $post_string, strlen(tPostString), 0, $this->discoverymcastip, $this->discoverymcastport);
  604. sendto(tSocket, tPostString, strlen(tPostString), 0, (const sockaddr *) &tSockAddr, sizeof(struct sockaddr_in));
  605. LOG("Broadcast Ping Sent!");
  606. fcntl(tSocket, F_SETFL, fcntl(tSocket, F_GETFL, 0) | O_NONBLOCK);
  607. LOG("Socket Set Not Bloking Mode!");
  608. #else
  609. // setsockopt( tSocket, IPPROTO_IP, MCAST_JOIN_GROUP, );
  610. bool tVal = 1;
  611. setsockopt(tSocket, SOL_SOCKET, SO_BROADCAST, (const char *) &tVal, sizeof(bool));
  612. // socket_sendto($sock, $post_string, strlen(tPostString), 0, $this->discoverymcastip, $this->discoverymcastport);
  613. sendto(tSocket, tPostString, strlen(tPostString), 0, (const sockaddr *) &tSockAddr, sizeof(struct sockaddr_in));
  614. LOG("Broadcast Ping Sent!");
  615. u_long arg = 1;
  616. if (ioctlsocket(tSocket, FIONBIO, &arg) < 0)
  617. {
  618. char tBuf[128];
  619. sprintf(tBuf, "Error ioctlsocket(..., FIONBIO) (%s)", strerror(errno));
  620. LOG_ERROR(tBuf);
  621. return RetVal;
  622. };
  623. #endif
  624. while (time(NULL) < tTimeout)
  625. {
  626. // n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
  627. tVal = recvfrom(tSocket, Response, DEFAULT_BUFFER_SIZE, 0, NULL, NULL);
  628. if (tVal > 0)
  629. {
  630. Response[tVal] = '\0';
  631. // if( FALSE !== socket_recvfrom( $sock, $response, 9999, 0, $from, $this->discoverymcastport )){
  632. // if( Response != NULL && $response != $post_string){
  633. LOG(Response);
  634. XMLNode node = XMLNode::parseString(Response, "root");
  635. //$response = $this->_xml2array($response);
  636. // if(!isFault( node )){
  637. //$response['Envelope']['Body']['ProbeMatches']['ProbeMatch']['IPAddr'] = $from;
  638. if (m_bDiscoveryHideDuplicates)
  639. {
  640. // $result[$from] = $response['Envelope']['Body']['ProbeMatches']['ProbeMatch'];
  641. }
  642. else
  643. {
  644. // $result[] = $response['Envelope']['Body']['ProbeMatches']['ProbeMatch'];
  645. };
  646. // };
  647. //};
  648. //};
  649. };
  650. };
  651. #ifdef __linux__
  652. close(tSocket);
  653. #else
  654. closesocket(tSocket);
  655. #endif
  656. }
  657. catch ( const std::exception& e)
  658. {
  659. LOG_ERROR("Communication at Discovery Exception Catched!");
  660. };
  661. return RetVal;
  662. }
  663. /*
  664. Public functions (basic initialization method and other collaterals)
  665. */
  666. bool pOnvif::create(void){
  667. #ifdef DEBUG_EXTRA
  668. LOG("Get Instance m_pHttpAccessManager");
  669. #endif
  670. m_pHttpAccessManager = curlRequestsManager::getInstance();
  671. if (m_pHttpAccessManager == NULL) {
  672. return false;
  673. }
  674. #ifdef DEBUG_EXTRA
  675. LOG("CURL MANAGER OK");
  676. #endif
  677. try {
  678. struct tm * tTS;
  679. tTS = core_GetSystemDateAndTime();
  680. // This is UTS DTS
  681. if (tTS != NULL) {
  682. #ifdef DEBUG
  683. char tB[128];
  684. sprintf(tB, "TS from dev: %04d-%02d-%02d %02d:%02d:%02d", tTS->tm_year, tTS->tm_mon, tTS->tm_mday, tTS->tm_hour, tTS->tm_min, tTS->tm_sec);
  685. LOG(tB);
  686. #endif
  687. // WTF ??!!
  688. tTS->tm_year -= 1900;
  689. tTS->tm_mon -= 1;
  690. time_t tTimeStamp = timegm(tTS); // mktime( tTS );
  691. m_nDeltaTime = time(NULL) - tTimeStamp;
  692. #ifdef DEBUG
  693. sprintf(tB, "TS:[%ld] D:[%ld]", tTimeStamp, m_nDeltaTime);
  694. LOG(tB);
  695. #endif
  696. free(tTS);
  697. } else {
  698. return false;
  699. };
  700. } catch (const std::exception& e) {
  701. LOG_ERROR("Catched Exception!");
  702. return false;
  703. };
  704. // Are we need that all at beginning?
  705. if (! core_GetCapabilities())
  706. {
  707. LOG_ERROR("Error Getting Caps!");
  708. return false;
  709. };
  710. _getOnvifVersion(m_sCapabilities);
  711. // Update some Vars Online!
  712. // preg_match("/^http(.*)onvif\//",$this->mediauri,$matches);
  713. // m_pBaseURI = $matches[0];
  714. if (media_GetVideoSources()){
  715. if (media_GetProfiles()){
  716. //_getActiveSources( m_vVideoSources, m_vVideoProfiles);
  717. return true;
  718. };
  719. };
  720. LOG_ERROR("Error Profiles!");
  721. return false;
  722. }
  723. /*
  724. Public wrappers for a subset of ONVIF primitives
  725. */
  726. struct tm * pOnvif::core_GetSystemDateAndTime(void)
  727. {
  728. struct tm * RetVal = NULL;
  729. char tURL[CTEXT];
  730. if (m_pDeviceURI != NULL)
  731. {
  732. sprintf(tURL, m_pDeviceURI);
  733. }
  734. else
  735. {
  736. if (m_pIpAddress != NULL)
  737. {
  738. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  739. }
  740. else
  741. {
  742. return NULL;
  743. };
  744. };
  745. #ifdef DEBUG_EXTRA
  746. LOG("Before REQ");
  747. #endif
  748. if (_send_request(tURL, PONVIF_PACK_GET_SYS_DTS, true))
  749. {
  750. XMLResults tParseResults;
  751. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  752. #ifdef DEBUG_EXTRA
  753. LOG("core_GetSystemDateAndTime");
  754. #endif
  755. if (tParseResults.error)
  756. {
  757. char tBuffer[CTEXT];
  758. sprintf(tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  759. LOG_ERROR(tBuffer);
  760. }
  761. else
  762. {
  763. RetVal = (struct tm *) malloc(sizeof(struct tm));
  764. if (RetVal != NULL)
  765. {
  766. memset(RetVal, 0, sizeof(struct tm));
  767. XMLNode zNode = response.getChildNode("Body", 0);
  768. if (! zNode.isEmpty())
  769. {
  770. XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0);
  771. if (! fNode.isEmpty())
  772. {
  773. XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0);
  774. if (! hNode.isEmpty())
  775. {
  776. XMLNode tNode = hNode.getChildNode("UTCDateTime", 0);
  777. if (! tNode.isEmpty())
  778. {
  779. RetVal->tm_isdst = 0;
  780. XMLNode ttNode = tNode.getChildNode("Time", 0);
  781. XMLNode dtNode = tNode.getChildNode("Date", 0);
  782. {
  783. XMLNode t = ttNode.getChildNode("Hour", 0);
  784. RetVal->tm_hour = atoi(t.getText(0));
  785. }
  786. {
  787. XMLNode t = ttNode.getChildNode("Minute", 0);
  788. RetVal->tm_min = atoi(t.getText(0));
  789. }
  790. {
  791. XMLNode t = ttNode.getChildNode("Second", 0);
  792. RetVal->tm_sec = atoi(t.getText(0));
  793. }
  794. {
  795. XMLNode t = dtNode.getChildNode("Year", 0);
  796. RetVal->tm_year = atoi(t.getText(0));
  797. }
  798. {
  799. XMLNode t = dtNode.getChildNode("Month", 0);
  800. RetVal->tm_mon = atoi(t.getText(0));
  801. }
  802. {
  803. XMLNode t = dtNode.getChildNode("Day", 0);
  804. RetVal->tm_mday = atoi(t.getText(0));
  805. }
  806. }
  807. else
  808. {
  809. LOG_ERROR("UTCDateTime is Not Found!");
  810. };
  811. }
  812. else
  813. {
  814. LOG_ERROR("SystemDateAndTime is Not Found!");
  815. };
  816. }
  817. else
  818. {
  819. LOG_ERROR("GetSystemDateAndTimeResponse is Not Found!");
  820. };
  821. }
  822. else
  823. {
  824. LOG_ERROR("Body is Not Found!");
  825. LOG(m_pLastResponse);
  826. };
  827. };
  828. return RetVal;
  829. };
  830. }
  831. else
  832. {
  833. LOG_ERROR("GetSystemDateAndTime: Communication error");
  834. }
  835. return NULL;
  836. }
  837. // Based on https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl
  838. bool pOnvif::core_GetCapabilities(void)
  839. {
  840. char tBuffer[DEFAULT_BUFFER_SIZE];
  841. accessToken tTOKEN;
  842. tTOKEN = _makeToken();
  843. char tURL[CTEXT];
  844. if (m_pDeviceURI != NULL)
  845. {
  846. sprintf(tURL, m_pDeviceURI);
  847. }
  848. else
  849. {
  850. if (m_pIpAddress != NULL)
  851. {
  852. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  853. }
  854. else
  855. {
  856. return NULL;
  857. };
  858. };
  859. // char tSTR[CTEXT];
  860. sprintf(tBuffer, PONVIF_PACK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  861. // sprintf( tSTR, PONVIF_PCK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  862. // build_package( tBuffer, PONVIF_ENVELOPE_SIM, tSTR );
  863. if (_send_request(tURL, tBuffer))
  864. {
  865. XMLResults tParseResults;
  866. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  867. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  868. LOG("RCPT:");
  869. printf(m_pLastResponse);
  870. #endif
  871. if (tParseResults.error != eXMLErrorNone)
  872. {
  873. char tBuf[CTEXT];
  874. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  875. LOG_ERROR(tBuf);
  876. }
  877. else
  878. {
  879. XMLNode zNode = response.getChildNode("Body", 0);
  880. if (zNode.isEmpty())
  881. {
  882. return false;
  883. }
  884. XMLNode tNode = zNode.getChildNode("GetCapabilitiesResponse", 0);
  885. if (! tNode.isEmpty())
  886. {
  887. XMLNode tCaps = tNode.getChildNode("Capabilities", 0);
  888. if (! tCaps.isEmpty())
  889. {
  890. {
  891. XMLNode t = tCaps.getChildNode("Analytics", 0);
  892. if (! t.isEmpty())
  893. {
  894. m_sCapabilities.Analytics = (onvifAnalyticsCapabilities *) malloc(sizeof(onvifAnalyticsCapabilities));
  895. if (m_sCapabilities.Analytics != NULL)
  896. {
  897. memset(m_sCapabilities.Analytics, 0, sizeof(onvifAnalyticsCapabilities));
  898. {
  899. XMLNode c = t.getChildNode("XAddr", 0);
  900. sprintf(m_sCapabilities.Analytics->XAddr, "%s", c.getText(0));
  901. }
  902. {
  903. XMLNode c = t.getChildNode("RuleSupport", 0);
  904. m_sCapabilities.Analytics->RuleSupport = checkBool(c.getText(0));
  905. }
  906. {
  907. XMLNode c = t.getChildNode("AnalyticsModuleSupport", 0);
  908. m_sCapabilities.Analytics->AnalyticsModuleSupport = checkBool(c.getText(0));
  909. }
  910. };
  911. };
  912. }
  913. {
  914. XMLNode t = tCaps.getChildNode("Device", 0);
  915. if (! t.isEmpty())
  916. {
  917. m_sCapabilities.Device = (onvifDeviceCapabilities *) malloc(sizeof(onvifDeviceCapabilities));
  918. if (m_sCapabilities.Device != NULL)
  919. {
  920. memset(m_sCapabilities.Device, 0, sizeof(onvifDeviceCapabilities));
  921. {
  922. XMLNode c = t.getChildNode("XAddr", 0);
  923. sprintf(m_sCapabilities.Device->XAddr, "%s", c.getText(0));
  924. }
  925. {
  926. XMLNode c = t.getChildNode("Network", 0);
  927. if (! c.isEmpty())
  928. {
  929. m_sCapabilities.Device->Network = (onvifNetworkCapabilities *) malloc(sizeof(onvifNetworkCapabilities));
  930. if (m_sCapabilities.Device->Network != NULL)
  931. {
  932. memset(m_sCapabilities.Device->Network, 0, sizeof(onvifNetworkCapabilities));
  933. {
  934. XMLNode n = c.getChildNode("IPFilter", 0);
  935. m_sCapabilities.Device->Network->IPFilter = checkBool(n.getText(0));
  936. }
  937. {
  938. XMLNode n = c.getChildNode("ZeroConfiguration", 0);
  939. m_sCapabilities.Device->Network->ZeroConfiguration = checkBool(n.getText(0));
  940. }
  941. {
  942. XMLNode n = c.getChildNode("IPVersion6", 0);
  943. m_sCapabilities.Device->Network->IPVersion6 = checkBool(n.getText(0));
  944. }
  945. {
  946. XMLNode n = c.getChildNode("DynDNS", 0);
  947. m_sCapabilities.Device->Network->DynDNS = checkBool(n.getText(0));
  948. }
  949. {
  950. XMLNode n = c.getChildNode("Extension", 0);
  951. if (! n.isEmpty())
  952. {
  953. m_sCapabilities.Device->Network->Extension = (onvifNetworkCapabilitiesExtension *) malloc(sizeof(onvifNetworkCapabilitiesExtension));
  954. if (m_sCapabilities.Device->Network->Extension != NULL)
  955. {
  956. memset(m_sCapabilities.Device->Network->Extension, 0, sizeof(onvifNetworkCapabilitiesExtension));
  957. {
  958. XMLNode x = n.getChildNode("Dot11Configuration", 0);
  959. m_sCapabilities.Device->Network->Extension->Dot11Configuration = checkBool(x.getText(0));
  960. }
  961. };
  962. };
  963. }
  964. };
  965. };
  966. }
  967. {
  968. XMLNode c = t.getChildNode("System", 0);
  969. if (! c.isEmpty())
  970. {
  971. m_sCapabilities.Device->System = (onvifSystemCapabilities *) malloc(sizeof(onvifSystemCapabilities));
  972. if (m_sCapabilities.Device->System != NULL)
  973. {
  974. memset(m_sCapabilities.Device->System, 0, sizeof(onvifSystemCapabilities));
  975. {
  976. XMLNode n = c.getChildNode("DiscoveryResolve", 0);
  977. m_sCapabilities.Device->System->DiscoveryResolve = checkBool(n.getText(0));
  978. }
  979. {
  980. XMLNode n = c.getChildNode("DiscoveryBye", 0);
  981. m_sCapabilities.Device->System->DiscoveryBye = checkBool(n.getText(0));
  982. }
  983. {
  984. XMLNode n = c.getChildNode("RemoteDiscovery", 0);
  985. m_sCapabilities.Device->System->RemoteDiscovery = checkBool(n.getText(0));
  986. }
  987. {
  988. XMLNode n = c.getChildNode("SystemBackup", 0);
  989. m_sCapabilities.Device->System->SystemBackup = checkBool(n.getText(0));
  990. }
  991. {
  992. XMLNode n = c.getChildNode("SystemLogging", 0);
  993. m_sCapabilities.Device->System->SystemLogging = checkBool(n.getText(0));
  994. }
  995. {
  996. XMLNode n = c.getChildNode("FirmwareUpgrade", 0);
  997. m_sCapabilities.Device->System->FirmwareUpgrade = checkBool(n.getText(0));
  998. }
  999. {
  1000. XMLNode n = c.getChildNode("SupportedVersions", 0);
  1001. if (! n.isEmpty())
  1002. {
  1003. m_sCapabilities.Device->System->SupportedVersions = (onvifSupportedVersions *) malloc(sizeof(onvifSupportedVersions));
  1004. if (m_sCapabilities.Device->System->SupportedVersions != NULL)
  1005. {
  1006. memset(m_sCapabilities.Device->System->SupportedVersions, 0, sizeof(onvifSupportedVersions));
  1007. {
  1008. XMLNode x = n.getChildNode("Major", 0);
  1009. m_sCapabilities.Device->System->SupportedVersions->Major = atoi(x.getText(0));
  1010. }
  1011. {
  1012. XMLNode x = n.getChildNode("Minor", 0);
  1013. m_sCapabilities.Device->System->SupportedVersions->Minor = atoi(x.getText(0));
  1014. }
  1015. };
  1016. };
  1017. }
  1018. {
  1019. XMLNode n = c.getChildNode("Extension", 0);
  1020. if (! n.isEmpty())
  1021. {
  1022. m_sCapabilities.Device->System->Extension = (onvifSystemCapabilitiesExtension *) malloc(sizeof(onvifSystemCapabilitiesExtension));
  1023. if (m_sCapabilities.Device->System->Extension != NULL)
  1024. {
  1025. memset(m_sCapabilities.Device->System->Extension, 0, sizeof(onvifSystemCapabilitiesExtension));
  1026. {
  1027. XMLNode x = n.getChildNode("HttpFirmwareUpgrade", 0);
  1028. m_sCapabilities.Device->System->Extension->HttpFirmwareUpgrade = checkBool(x.getText(0));
  1029. }
  1030. {
  1031. XMLNode x = n.getChildNode("HttpSystemBackup", 0);
  1032. m_sCapabilities.Device->System->Extension->HttpSystemBackup = checkBool(x.getText(0));
  1033. }
  1034. {
  1035. XMLNode x = n.getChildNode("HttpSystemLogging", 0);
  1036. m_sCapabilities.Device->System->Extension->HttpSystemLogging = checkBool(x.getText(0));
  1037. }
  1038. {
  1039. XMLNode x = n.getChildNode("HttpSupportInformation", 0);
  1040. m_sCapabilities.Device->System->Extension->HttpSupportInformation = checkBool(x.getText(0));
  1041. }
  1042. };
  1043. };
  1044. }
  1045. };
  1046. };
  1047. }
  1048. {
  1049. XMLNode c = t.getChildNode("IO", 0);
  1050. if (! c.isEmpty())
  1051. {
  1052. m_sCapabilities.Device->IO = (onvifIOCapabilities *) malloc(sizeof(onvifIOCapabilities));
  1053. if (m_sCapabilities.Device->IO != NULL)
  1054. {
  1055. memset(m_sCapabilities.Device->IO, 0, sizeof(onvifIOCapabilities));
  1056. {
  1057. XMLNode n = c.getChildNode("InputConnectors", 0);
  1058. m_sCapabilities.Device->IO->InputConnectors = atoi(n.getText(0));
  1059. }
  1060. {
  1061. XMLNode n = c.getChildNode("RelayOutputs", 0);
  1062. m_sCapabilities.Device->IO->RelayOutputs = atoi(n.getText(0));
  1063. }
  1064. {
  1065. XMLNode n = c.getChildNode("Extension", 0);
  1066. if (! n.isEmpty())
  1067. {
  1068. m_sCapabilities.Device->IO->Extension = (onvifIOCapabilitiesExtension *) malloc(sizeof(onvifIOCapabilitiesExtension));
  1069. if (m_sCapabilities.Device->IO->Extension != NULL)
  1070. {
  1071. memset(m_sCapabilities.Device->IO->Extension, 0, sizeof(onvifIOCapabilitiesExtension));
  1072. {
  1073. XMLNode x = n.getChildNode("Auxiliary", 0);
  1074. m_sCapabilities.Device->IO->Extension->Auxiliary = checkBool(x.getText(0));
  1075. }
  1076. };
  1077. };
  1078. }
  1079. };
  1080. };
  1081. }
  1082. {
  1083. XMLNode c = t.getChildNode("Security", 0);
  1084. if (! c.isEmpty())
  1085. {
  1086. m_sCapabilities.Device->Security = (onvifSecurityCapabilities *) malloc(sizeof(onvifSecurityCapabilities));
  1087. if (m_sCapabilities.Device->Security != NULL)
  1088. {
  1089. memset(m_sCapabilities.Device->Security, 0, sizeof(onvifSecurityCapabilities));
  1090. {
  1091. XMLNode n = c.getChildNode("TLS1.1", 0);
  1092. m_sCapabilities.Device->Security->TLS1_1 = checkBool(n.getText(0));
  1093. }
  1094. {
  1095. XMLNode n = c.getChildNode("TLS1.2", 0);
  1096. m_sCapabilities.Device->Security->TLS1_2 = checkBool(n.getText(0));
  1097. }
  1098. {
  1099. XMLNode n = c.getChildNode("OnboardKeyGeneration", 0);
  1100. m_sCapabilities.Device->Security->OnboardKeyGeneration = checkBool(n.getText(0));
  1101. }
  1102. {
  1103. XMLNode n = c.getChildNode("AccessPolicyConfig", 0);
  1104. m_sCapabilities.Device->Security->AccessPolicyConfig = checkBool(n.getText(0));
  1105. }
  1106. {
  1107. XMLNode n = c.getChildNode("X.509Token", 0);
  1108. m_sCapabilities.Device->Security->X_509Token = checkBool(n.getText(0));
  1109. }
  1110. {
  1111. XMLNode n = c.getChildNode("SAMLToken", 0);
  1112. m_sCapabilities.Device->Security->SAMLToken = checkBool(n.getText(0));
  1113. }
  1114. {
  1115. XMLNode n = c.getChildNode("KerberosToken", 0);
  1116. m_sCapabilities.Device->Security->KerberosToken = checkBool(n.getText(0));
  1117. }
  1118. {
  1119. XMLNode n = c.getChildNode("RELToken", 0);
  1120. m_sCapabilities.Device->Security->RELToken = checkBool(n.getText(0));
  1121. }
  1122. {
  1123. XMLNode n = c.getChildNode("Extension", 0);
  1124. if (! n.isEmpty())
  1125. {
  1126. m_sCapabilities.Device->Security->Extension = (onvifSecurityCapabilitiesExtension *) malloc(sizeof(onvifSecurityCapabilitiesExtension));
  1127. if (m_sCapabilities.Device->Security->Extension != NULL)
  1128. {
  1129. memset(m_sCapabilities.Device->Security->Extension, 0, sizeof(onvifSecurityCapabilitiesExtension));
  1130. {
  1131. XMLNode x = n.getChildNode("TLS1.0", 0);
  1132. m_sCapabilities.Device->Security->Extension->TLS1_0 = checkBool(x.getText(0));
  1133. }
  1134. };
  1135. };
  1136. }
  1137. };
  1138. };
  1139. }
  1140. };
  1141. };
  1142. }
  1143. {
  1144. XMLNode t = tCaps.getChildNode("Events", 0);
  1145. if (! t.isEmpty())
  1146. {
  1147. m_sCapabilities.Events = (onvifEventCapabilities *) malloc(sizeof(onvifEventCapabilities));
  1148. if (m_sCapabilities.Events != NULL)
  1149. {
  1150. memset(m_sCapabilities.Events, 0, sizeof(onvifEventCapabilities));
  1151. {
  1152. XMLNode c = t.getChildNode("XAddr", 0);
  1153. sprintf(m_sCapabilities.Events->XAddr, "%s", c.getText(0));
  1154. }
  1155. {
  1156. XMLNode c = t.getChildNode("WSSubscriptionPolicySupport", 0);
  1157. m_sCapabilities.Events->WSSubscriptionPolicySupport = checkBool(c.getText(0));
  1158. }
  1159. {
  1160. XMLNode c = t.getChildNode("WSPullPointSupport", 0);
  1161. m_sCapabilities.Events->WSPullPointSupport = checkBool(c.getText(0));
  1162. }
  1163. {
  1164. XMLNode c = t.getChildNode("WSPausableSubscriptionManagerInterfaceSupport", 0);
  1165. m_sCapabilities.Events->WSPausableSubscriptionManagerInterfaceSupport = checkBool(c.getText(0));
  1166. }
  1167. };
  1168. };
  1169. }
  1170. {
  1171. XMLNode t = tCaps.getChildNode("Imaging", 0);
  1172. if (! t.isEmpty())
  1173. {
  1174. m_sCapabilities.Imaging = (onvifImagingCapabilities *) malloc(sizeof(onvifImagingCapabilities));
  1175. if (m_sCapabilities.Imaging != NULL)
  1176. {
  1177. memset(m_sCapabilities.Imaging, 0, sizeof(onvifImagingCapabilities));
  1178. {
  1179. XMLNode c = t.getChildNode("XAddr", 0);
  1180. sprintf(m_sCapabilities.Imaging->XAddr, "%s", c.getText(0));
  1181. }
  1182. };
  1183. };
  1184. }
  1185. {
  1186. XMLNode t = tCaps.getChildNode("Media", 0);
  1187. if (! t.isEmpty())
  1188. {
  1189. m_sCapabilities.Media = (onvifMediaCapabilities *) malloc(sizeof(onvifMediaCapabilities));
  1190. if (m_sCapabilities.Media != NULL)
  1191. {
  1192. memset(m_sCapabilities.Media, 0, sizeof(onvifMediaCapabilities));
  1193. {
  1194. XMLNode c = t.getChildNode("XAddr", 0);
  1195. sprintf(m_sCapabilities.Media->XAddr, "%s", c.getText(0));
  1196. }
  1197. {
  1198. XMLNode c = t.getChildNode("StreamingCapabilities", 0);
  1199. if (! c.isEmpty())
  1200. {
  1201. m_sCapabilities.Media->StreamingCapabilities = (onvifRealTimeStreamingCapabilities *) malloc(sizeof(onvifRealTimeStreamingCapabilities));
  1202. if (m_sCapabilities.Media->StreamingCapabilities != NULL)
  1203. {
  1204. memset(m_sCapabilities.Media->StreamingCapabilities, 0, sizeof(onvifRealTimeStreamingCapabilities));
  1205. {
  1206. XMLNode n = c.getChildNode("RTPMulticast", 0);
  1207. m_sCapabilities.Media->StreamingCapabilities->RTPMulticast = checkBool(n.getText(0));
  1208. }
  1209. {
  1210. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1211. m_sCapabilities.Media->StreamingCapabilities->RTP_TCP = checkBool(n.getText(0));
  1212. }
  1213. {
  1214. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1215. m_sCapabilities.Media->StreamingCapabilities->RTP_RTSP_TCP = checkBool(n.getText(0));
  1216. }
  1217. };
  1218. };
  1219. }
  1220. {
  1221. XMLNode c = t.getChildNode("Extension", 0);
  1222. if (! c.isEmpty())
  1223. {
  1224. m_sCapabilities.Media->Extension = (onvifMediaCapabilitiesExtension *) malloc(sizeof(onvifMediaCapabilitiesExtension));
  1225. if (m_sCapabilities.Media->Extension != NULL)
  1226. {
  1227. memset(m_sCapabilities.Media->Extension, 0, sizeof(onvifMediaCapabilitiesExtension));
  1228. {
  1229. XMLNode n = c.getChildNode("ProfileCapabilities", 0);
  1230. if (! n.isEmpty())
  1231. {
  1232. m_sCapabilities.Media->Extension->ProfileCapabilities = (onvifProfileCapabilities *) malloc(sizeof(onvifProfileCapabilities));
  1233. if (m_sCapabilities.Media->Extension->ProfileCapabilities != NULL)
  1234. {
  1235. memset(m_sCapabilities.Media->Extension->ProfileCapabilities, 0, sizeof(onvifProfileCapabilities));
  1236. {
  1237. XMLNode x = n.getChildNode("MaximumNumberOfProfiles", 0);
  1238. m_sCapabilities.Media->Extension->ProfileCapabilities->MaximumNumberOfProfiles = atoi(x.getText(0));
  1239. }
  1240. };
  1241. };
  1242. }
  1243. };
  1244. };
  1245. }
  1246. };
  1247. };
  1248. }
  1249. {
  1250. XMLNode t = tCaps.getChildNode("PTZ", 0);
  1251. if (! t.isEmpty())
  1252. {
  1253. m_sCapabilities.PTZ = (onvifPTZCapabilities *) malloc(sizeof(onvifPTZCapabilities));
  1254. if (m_sCapabilities.PTZ != NULL)
  1255. {
  1256. memset(m_sCapabilities.PTZ, 0, sizeof(onvifPTZCapabilities));
  1257. {
  1258. XMLNode c = t.getChildNode("XAddr", 0);
  1259. sprintf(m_sCapabilities.PTZ->XAddr, "%s", c.getText(0));
  1260. }
  1261. };
  1262. };
  1263. }
  1264. {
  1265. XMLNode t = tCaps.getChildNode("Extension", 0);
  1266. if (! t.isEmpty())
  1267. {
  1268. m_sCapabilities.Extension = (onvifCapabilitiesExtension *) malloc(sizeof(onvifCapabilitiesExtension));
  1269. if (m_sCapabilities.Extension != NULL)
  1270. {
  1271. memset(m_sCapabilities.Extension, 0, sizeof(onvifCapabilitiesExtension));
  1272. {
  1273. XMLNode c = t.getChildNode("DeviceIO", 0);
  1274. if (! c.isEmpty())
  1275. {
  1276. m_sCapabilities.Extension->DeviceIO = (onvifDeviceIOCapabilities *) malloc(sizeof(onvifDeviceIOCapabilities));
  1277. if (m_sCapabilities.Extension->DeviceIO != NULL)
  1278. {
  1279. memset(m_sCapabilities.Extension->DeviceIO, 0, sizeof(onvifDeviceIOCapabilities));
  1280. {
  1281. XMLNode n = c.getChildNode("XAddr", 0);
  1282. sprintf(m_sCapabilities.Extension->DeviceIO->XAddr, "%s", n.getText(0));
  1283. }
  1284. {
  1285. XMLNode n = c.getChildNode("VideoSources", 0);
  1286. m_sCapabilities.Extension->DeviceIO->VideoSources = atoi(n.getText(0));
  1287. }
  1288. {
  1289. XMLNode n = c.getChildNode("VideoOutputs", 0);
  1290. m_sCapabilities.Extension->DeviceIO->VideoOutputs = atoi(n.getText(0));
  1291. }
  1292. {
  1293. XMLNode n = c.getChildNode("AudioSources", 0);
  1294. m_sCapabilities.Extension->DeviceIO->AudioSources = atoi(n.getText(0));
  1295. }
  1296. {
  1297. XMLNode n = c.getChildNode("AudioOutputs", 0);
  1298. m_sCapabilities.Extension->DeviceIO->AudioOutputs = atoi(n.getText(0));
  1299. }
  1300. {
  1301. XMLNode n = c.getChildNode("RelayOutputs", 0);
  1302. m_sCapabilities.Extension->DeviceIO->RelayOutputs = atoi(n.getText(0));
  1303. }
  1304. };
  1305. };
  1306. }
  1307. {
  1308. XMLNode c = t.getChildNode("Display", 0);
  1309. if (! c.isEmpty())
  1310. {
  1311. m_sCapabilities.Extension->Display = (onvifDisplayCapabilities *) malloc(sizeof(onvifDisplayCapabilities));
  1312. if (m_sCapabilities.Extension->Display != NULL)
  1313. {
  1314. memset(m_sCapabilities.Extension->Display, 0, sizeof(onvifDisplayCapabilities));
  1315. {
  1316. XMLNode n = c.getChildNode("XAddr", 0);
  1317. sprintf(m_sCapabilities.Extension->Display->XAddr, "%s", n.getText(0));
  1318. }
  1319. {
  1320. XMLNode n = c.getChildNode("FixedLayout", 0);
  1321. m_sCapabilities.Extension->Display->FixedLayout = checkBool(n.getText(0));
  1322. }
  1323. };
  1324. };
  1325. }
  1326. {
  1327. XMLNode c = t.getChildNode("Recording", 0);
  1328. if (! c.isEmpty())
  1329. {
  1330. m_sCapabilities.Extension->Recording = (onvifRecordingCapabilities *) malloc(sizeof(onvifRecordingCapabilities));
  1331. if (m_sCapabilities.Extension->Recording != NULL)
  1332. {
  1333. memset(m_sCapabilities.Extension->Recording, 0, sizeof(onvifRecordingCapabilities));
  1334. {
  1335. XMLNode n = c.getChildNode("XAddr", 0);
  1336. sprintf(m_sCapabilities.Extension->Recording->XAddr, "%s", n.getText(0));
  1337. }
  1338. {
  1339. XMLNode n = c.getChildNode("ReceiverSource", 0);
  1340. m_sCapabilities.Extension->Recording->ReceiverSource = checkBool(n.getText(0));
  1341. }
  1342. {
  1343. XMLNode n = c.getChildNode("MediaProfileSource", 0);
  1344. m_sCapabilities.Extension->Recording->MediaProfileSource = checkBool(n.getText(0));
  1345. }
  1346. {
  1347. XMLNode n = c.getChildNode("DynamicRecordings", 0);
  1348. m_sCapabilities.Extension->Recording->DynamicRecordings = checkBool(n.getText(0));
  1349. }
  1350. {
  1351. XMLNode n = c.getChildNode("DynamicTracks", 0);
  1352. m_sCapabilities.Extension->Recording->DynamicTracks = checkBool(n.getText(0));
  1353. }
  1354. {
  1355. XMLNode n = c.getChildNode("MaxStringLength", 0);
  1356. m_sCapabilities.Extension->Recording->MaxStringLength = atoi(n.getText(0));
  1357. }
  1358. };
  1359. };
  1360. }
  1361. {
  1362. XMLNode c = t.getChildNode("Search", 0);
  1363. if (! c.isEmpty())
  1364. {
  1365. m_sCapabilities.Extension->Search = (onvifSearchCapabilities *) malloc(sizeof(onvifSearchCapabilities));
  1366. if (m_sCapabilities.Extension->Search != NULL)
  1367. {
  1368. memset(m_sCapabilities.Extension->Search, 0, sizeof(onvifSearchCapabilities));
  1369. {
  1370. XMLNode n = c.getChildNode("XAddr", 0);
  1371. sprintf(m_sCapabilities.Extension->Search->XAddr, "%s", n.getText(0));
  1372. }
  1373. {
  1374. XMLNode n = c.getChildNode("MetadataSearch", 0);
  1375. m_sCapabilities.Extension->Search->MetadataSearch = checkBool(n.getText(0));
  1376. }
  1377. };
  1378. };
  1379. }
  1380. {
  1381. XMLNode c = t.getChildNode("Replay", 0);
  1382. if (! c.isEmpty())
  1383. {
  1384. m_sCapabilities.Extension->Replay = (onvifReplayCapabilities *) malloc(sizeof(onvifReplayCapabilities));
  1385. if (m_sCapabilities.Extension->Replay != NULL)
  1386. {
  1387. memset(m_sCapabilities.Extension->Replay, 0, sizeof(onvifReplayCapabilities));
  1388. {
  1389. XMLNode n = c.getChildNode("XAddr", 0);
  1390. sprintf(m_sCapabilities.Extension->Replay->XAddr, "%s", n.getText(0));
  1391. }
  1392. };
  1393. };
  1394. }
  1395. {
  1396. XMLNode c = t.getChildNode("Receiver", 0);
  1397. if (! c.isEmpty())
  1398. {
  1399. m_sCapabilities.Extension->Receiver = (onvifReceiverCapabilities *) malloc(sizeof(onvifReceiverCapabilities));
  1400. if (m_sCapabilities.Extension->Receiver != NULL)
  1401. {
  1402. memset(m_sCapabilities.Extension->Receiver, 0, sizeof(onvifReceiverCapabilities));
  1403. {
  1404. XMLNode n = c.getChildNode("XAddr", 0);
  1405. sprintf(m_sCapabilities.Extension->Receiver->XAddr, "%s", n.getText(0));
  1406. }
  1407. {
  1408. XMLNode n = c.getChildNode("RTP_Multicast", 0);
  1409. m_sCapabilities.Extension->Receiver->RTP_Multicast = checkBool(n.getText(0));
  1410. }
  1411. {
  1412. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1413. m_sCapabilities.Extension->Receiver->RTP_TCP = checkBool(n.getText(0));
  1414. }
  1415. {
  1416. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1417. m_sCapabilities.Extension->Receiver->RTP_RTSP_TCP = checkBool(n.getText(0));
  1418. }
  1419. {
  1420. XMLNode n = c.getChildNode("SupportedReceivers", 0);
  1421. m_sCapabilities.Extension->Receiver->SupportedReceivers = atoi(n.getText(0));
  1422. }
  1423. {
  1424. XMLNode n = c.getChildNode("MaximumRTSPURILength", 0);
  1425. m_sCapabilities.Extension->Receiver->MaximumRTSPURILength = atoi(n.getText(0));
  1426. }
  1427. };
  1428. };
  1429. }
  1430. {
  1431. XMLNode c = t.getChildNode("AnalyticsDevice", 0);
  1432. if (! c.isEmpty())
  1433. {
  1434. m_sCapabilities.Extension->AnalyticsDevice = (onvifAnalyticsDeviceCapabilities *) malloc(sizeof(onvifAnalyticsDeviceCapabilities));
  1435. if (m_sCapabilities.Extension->AnalyticsDevice != NULL)
  1436. {
  1437. memset(m_sCapabilities.Extension->AnalyticsDevice, 0, sizeof(onvifAnalyticsDeviceCapabilities));
  1438. {
  1439. XMLNode n = c.getChildNode("XAddr", 0);
  1440. sprintf(m_sCapabilities.Extension->AnalyticsDevice->XAddr, "%s", n.getText(0));
  1441. }
  1442. {
  1443. XMLNode n = c.getChildNode("RuleSupport", 0);
  1444. m_sCapabilities.Extension->AnalyticsDevice->RuleSupport = checkBool(n.getText(0));
  1445. }
  1446. };
  1447. };
  1448. }
  1449. };
  1450. };
  1451. }
  1452. return true;
  1453. }
  1454. else
  1455. {
  1456. LOG("Capabilities is Empty!");
  1457. };
  1458. }
  1459. else
  1460. {
  1461. LOG("GetCapabilitiesResponse is Empty!");
  1462. };
  1463. };
  1464. }
  1465. return false;
  1466. }
  1467. // based on https://www.onvif.org/ver20/imaging/wsdl/imaging.wsdl
  1468. bool pOnvif::media_GetVideoSources(void)
  1469. {
  1470. if (m_pMediaURI == NULL)
  1471. {
  1472. return false;
  1473. }
  1474. char tBuffer[DEFAULT_BUFFER_SIZE];
  1475. accessToken tTOKEN;
  1476. tTOKEN = _makeToken();
  1477. sprintf(tBuffer, PONVIF_PACK_GET_VIDEO_SOURCES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1478. if (_send_request(m_pMediaURI, tBuffer))
  1479. {
  1480. XMLResults tParseResults;
  1481. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1482. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1483. LOG("RCPT:");
  1484. printf(m_pLastResponse);
  1485. #endif
  1486. if (tParseResults.error)
  1487. {
  1488. char tBuf[CTEXT];
  1489. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1490. LOG_ERROR(tBuf);
  1491. }
  1492. else
  1493. {
  1494. XMLNode zNode = response.getChildNode("Body", 0);
  1495. if (zNode.isEmpty())
  1496. {
  1497. return false;
  1498. }
  1499. XMLNode tNode = zNode.getChildNode("GetVideoSourcesResponse", 0);
  1500. if (! tNode.isEmpty())
  1501. {
  1502. for (int index = 0; index < tNode.nChildNode(); index++)
  1503. {
  1504. XMLNode cNode = tNode.getChildNode("VideoSources", index);
  1505. if (! cNode.isEmpty())
  1506. {
  1507. videoSource * tPtr;
  1508. tPtr = (videoSource *) malloc(sizeof(videoSource));
  1509. if (tPtr != NULL)
  1510. {
  1511. sprintf(tPtr->token, "%s", cNode.getAttribute("token", 0));
  1512. {
  1513. XMLNode nNode = cNode.getChildNode("Framerate", 0);
  1514. tPtr->Framerate = (float) atof(nNode.getText(0));
  1515. }
  1516. {
  1517. XMLNode nNode = cNode.getChildNode("Resolution", 0);
  1518. if (! nNode.isEmpty())
  1519. {
  1520. {
  1521. XMLNode xNode = nNode.getChildNode("Width", 0);
  1522. tPtr->Resolution.Width = atoi(xNode.getText(0));
  1523. }
  1524. {
  1525. XMLNode xNode = nNode.getChildNode("Height", 0);
  1526. tPtr->Resolution.Height = atoi(xNode.getText(0));
  1527. }
  1528. };
  1529. }
  1530. {
  1531. XMLNode nNode = cNode.getChildNode("Imaging", 0);
  1532. if (! nNode.isEmpty())
  1533. {
  1534. tPtr->Imaging = (onvifImagingSettings *) malloc(sizeof(onvifImagingSettings));
  1535. if (tPtr->Imaging != NULL)
  1536. {
  1537. memset(tPtr->Imaging, 0, sizeof(onvifImagingSettings));
  1538. {
  1539. XMLNode xNode = nNode.getChildNode("BacklightCompensation", 0);
  1540. if (! xNode.isEmpty())
  1541. {
  1542. tPtr->Imaging->BacklightCompensation = (onvifBacklightCompensation *) malloc(sizeof(onvifBacklightCompensation));
  1543. if (tPtr->Imaging->BacklightCompensation != NULL)
  1544. {
  1545. memset(tPtr->Imaging->BacklightCompensation, 0, sizeof(onvifBacklightCompensation));
  1546. {
  1547. XMLNode rNode = xNode.getChildNode("Mode", 0);
  1548. tPtr->Imaging->BacklightCompensation->Mode = checkOnOff(rNode.getText(0));
  1549. }
  1550. {
  1551. XMLNode rNode = xNode.getChildNode("Level", 0);
  1552. tPtr->Imaging->BacklightCompensation->Level = (float) atof(rNode.getText(0));
  1553. }
  1554. };
  1555. };
  1556. }
  1557. {
  1558. XMLNode xNode = nNode.getChildNode("Brightness", 0);
  1559. tPtr->Imaging->Brightness = (float) atoi(xNode.getText(0));
  1560. }
  1561. {
  1562. XMLNode xNode = nNode.getChildNode("ColorSaturation", 0);
  1563. tPtr->Imaging->ColorSaturation = (float) atoi(xNode.getText(0));
  1564. }
  1565. {
  1566. XMLNode xNode = nNode.getChildNode("Contrast", 0);
  1567. tPtr->Imaging->Contrast = (float) atoi(xNode.getText(0));
  1568. }
  1569. {
  1570. XMLNode xNode = nNode.getChildNode("Sharpness", 0);
  1571. tPtr->Imaging->Sharpness = (float) atoi(xNode.getText(0));
  1572. }
  1573. {
  1574. XMLNode xNode = nNode.getChildNode("IrCutFilter", 0);
  1575. tPtr->Imaging->IrCutFilter = checkOnOffAuto(xNode.getText(0));
  1576. }
  1577. };
  1578. };
  1579. }
  1580. m_vVideoSources.push_back(tPtr);
  1581. };
  1582. };
  1583. };
  1584. return true;
  1585. }
  1586. else
  1587. {
  1588. LOG_ERROR("GetVideoSourcesResponse EMPTY!");
  1589. };
  1590. };
  1591. }
  1592. else
  1593. {
  1594. LOG_ERROR("_send_request Failure!");
  1595. };
  1596. return false;
  1597. }
  1598. bool pOnvif::media_GetProfiles(void)
  1599. {
  1600. char tBuffer[DEFAULT_BUFFER_SIZE];
  1601. accessToken tTOKEN;
  1602. tTOKEN = _makeToken();
  1603. sprintf(tBuffer, PONVIF_PACK_GET_PROFILES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1604. if (_send_request(m_pMediaURI, tBuffer))
  1605. {
  1606. XMLResults tParseResults;
  1607. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1608. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1609. LOG("RCPT:");
  1610. printf(m_pLastResponse);
  1611. #endif
  1612. if (tParseResults.error)
  1613. {
  1614. char tBuf[CTEXT];
  1615. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1616. LOG_ERROR(tBuf);
  1617. }
  1618. else
  1619. {
  1620. XMLNode zNode = response.getChildNode("Body", 0);
  1621. if (zNode.isEmpty())
  1622. {
  1623. return false;
  1624. }
  1625. XMLNode tNode = zNode.getChildNode("GetProfilesResponse", 0);
  1626. if (! tNode.isEmpty())
  1627. {
  1628. for (int index = 0; index < tNode.nChildNode(); index++)
  1629. {
  1630. XMLNode cNode = tNode.getChildNode("Profiles", index);
  1631. if (! cNode.isEmpty())
  1632. {
  1633. videoSourceProfile * tPtr;
  1634. tPtr = (videoSourceProfile *) malloc(sizeof(videoSourceProfile));
  1635. if (tPtr != NULL)
  1636. {
  1637. memset(tPtr, 0, sizeof(videoSourceProfile));
  1638. sprintf(tPtr->profiletoken, "%s", cNode.getAttribute("token", 0));
  1639. {
  1640. XMLNode nNode = cNode.getChildNode("Name", 0);
  1641. sprintf(tPtr->profilename, "%s", nNode.getText(0));
  1642. }
  1643. {
  1644. XMLNode nNode = cNode.getChildNode("VideoSourceConfiguration", 0);
  1645. if (! nNode.isEmpty())
  1646. {
  1647. XMLNode zNode = nNode.getChildNode("SourceToken", 0);
  1648. sprintf(tPtr->sourcetoken, "%s", zNode.getText(0));
  1649. };
  1650. }
  1651. {
  1652. XMLNode nNode = cNode.getChildNode("VideoEncoderConfiguration", 0);
  1653. if (! nNode.isEmpty())
  1654. {
  1655. {
  1656. XMLNode zNode = nNode.getChildNode("Encoding", 0);
  1657. sprintf(tPtr->encoding, "%s", zNode.getText(0));
  1658. }
  1659. {
  1660. XMLNode zNode = nNode.getChildNode("Resolution", 0);
  1661. {
  1662. XMLNode jNode = zNode.getChildNode("Width", 0);
  1663. tPtr->width = atoi(jNode.getText(0));
  1664. }
  1665. {
  1666. XMLNode jNode = zNode.getChildNode("Height", 0);
  1667. tPtr->height = atoi(jNode.getText(0));
  1668. }
  1669. }
  1670. {
  1671. XMLNode zNode = nNode.getChildNode("RateControl", 0);
  1672. {
  1673. XMLNode jNode = zNode.getChildNode("FrameRateLimit", 0);
  1674. tPtr->fps = atoi(jNode.getText(0));
  1675. }
  1676. {
  1677. XMLNode jNode = zNode.getChildNode("BitrateLimit", 0);
  1678. tPtr->bitrate = atoi(jNode.getText(0));
  1679. }
  1680. }
  1681. };
  1682. }
  1683. m_vVideoProfiles.push_back(tPtr);
  1684. };
  1685. };
  1686. };
  1687. return true;
  1688. };
  1689. };
  1690. };
  1691. return false;
  1692. }
  1693. bool pOnvif::media_GetServices(void)
  1694. {
  1695. char tBuffer[DEFAULT_BUFFER_SIZE];
  1696. accessToken tTOKEN;
  1697. tTOKEN = _makeToken();
  1698. sprintf(tBuffer, PONVIF_PACK_GET_SERVICES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1699. if (_send_request(m_pMediaURI, tBuffer))
  1700. {
  1701. XMLResults tParseResults;
  1702. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1703. LOG("RCPT:");
  1704. printf(m_pLastResponse);
  1705. if (tParseResults.error)
  1706. {
  1707. char tBuf[CTEXT];
  1708. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1709. LOG_ERROR(tBuf);
  1710. }
  1711. else
  1712. {
  1713. };
  1714. };
  1715. return false;
  1716. }
  1717. bool pOnvif::core_GetDeviceInformation()
  1718. {
  1719. char tBuffer[DEFAULT_BUFFER_SIZE];
  1720. accessToken tTOKEN;
  1721. tTOKEN = _makeToken();
  1722. sprintf(tBuffer, PONVIF_PACK_GET_DEV_INFO, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1723. if (_send_request(m_pMediaURI, tBuffer))
  1724. {
  1725. XMLResults tParseResults;
  1726. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1727. LOG("RCPT:");
  1728. printf(m_pLastResponse);
  1729. if (tParseResults.error)
  1730. {
  1731. char tBuf[CTEXT];
  1732. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1733. LOG_ERROR(tBuf);
  1734. }
  1735. else
  1736. {
  1737. // return $response['Envelope']['Body']['GetDeviceInformationResponse'];
  1738. };
  1739. };
  1740. return false;
  1741. }
  1742. CSTR pOnvif::media_GetStreamUri(CSTR profileToken, CSTR stream, CSTR protocol)
  1743. {
  1744. char tBuffer[DEFAULT_BUFFER_SIZE];
  1745. accessToken tTOKEN;
  1746. tTOKEN = _makeToken();
  1747. sprintf(tBuffer, PONVIF_PACK_GET_STREAM_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, stream, protocol, profileToken);
  1748. if (_send_request(m_pMediaURI, tBuffer))
  1749. {
  1750. XMLResults tParseResults;
  1751. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1752. LOG("RCPT:");
  1753. printf(m_pLastResponse);
  1754. if (tParseResults.error)
  1755. {
  1756. char tBuf[CTEXT];
  1757. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1758. LOG_ERROR(tBuf);
  1759. }
  1760. else
  1761. {
  1762. // Parse
  1763. // return $response['Envelope']['Body']['GetStreamUriResponse']['MediaUri']['Uri'];
  1764. };
  1765. };
  1766. return NULL;
  1767. }
  1768. CSTR pOnvif::media_GetSnapshotUri(CSTR profileToken)
  1769. {
  1770. char tBuffer[DEFAULT_BUFFER_SIZE];
  1771. accessToken tTOKEN;
  1772. tTOKEN = _makeToken();
  1773. sprintf(tBuffer, PONVIF_PACK_GET_PICTURE_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1774. if (_send_request(m_pMediaURI, tBuffer))
  1775. {
  1776. XMLResults tParseResults;
  1777. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1778. LOG("RCPT:");
  1779. printf(m_pLastResponse);
  1780. if (tParseResults.error)
  1781. {
  1782. char tBuf[CTEXT];
  1783. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1784. LOG_ERROR(tBuf);
  1785. }
  1786. else
  1787. {
  1788. // Parse
  1789. // return $response['Envelope']['Body']['GetSnapshotUriResponse']['MediaUri']['Uri'];
  1790. };
  1791. };
  1792. return NULL;
  1793. }
  1794. bool pOnvif::ptz_GetStatus(CSTR profileToken, ptzStatus * tStatus)
  1795. {
  1796. if (m_pPTZURI == NULL)
  1797. {
  1798. return false;
  1799. }
  1800. if (tStatus == NULL)
  1801. {
  1802. return false;
  1803. }
  1804. char tBuffer[DEFAULT_BUFFER_SIZE];
  1805. accessToken tTOKEN;
  1806. tTOKEN = _makeToken();
  1807. memset(tStatus, 0, sizeof(ptzStatus));
  1808. sprintf(tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1809. if (_send_request(m_pPTZURI, tBuffer, true))
  1810. {
  1811. XMLResults tParseResults;
  1812. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1813. if (tParseResults.error)
  1814. {
  1815. char tBuf[CTEXT];
  1816. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1817. LOG_ERROR(tBuf);
  1818. }
  1819. else
  1820. {
  1821. XMLNode aNode = response.getChildNode("Body", 0);
  1822. if (aNode.isEmpty())
  1823. {
  1824. return NULL;
  1825. }
  1826. XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0);
  1827. if (! bNode.isEmpty())
  1828. {
  1829. XMLNode cNode = bNode.getChildNode("PTZStatus", 0);
  1830. if (! cNode.isEmpty())
  1831. {
  1832. XMLNode dNode = cNode.getChildNode("Position", 0);
  1833. if (! dNode.isEmpty())
  1834. {
  1835. XMLNode eNode = dNode.getChildNode("PanTilt", 0);
  1836. if (! eNode.isEmpty())
  1837. {
  1838. // XMLAttribute ZZ = eNode.getAttribute(1);
  1839. // LOG( ZZ.lpszName );
  1840. tStatus->pan = atof(eNode.getAttribute("x")); // ZZ.lpszValue );
  1841. // ZZ = eNode.getAttribute(2);
  1842. // LOG( ZZ.lpszName );
  1843. tStatus->tilt = atof(eNode.getAttribute("y")); // ZZ.lpszValue );
  1844. };
  1845. XMLNode fNode = dNode.getChildNode("Zoom", 0);
  1846. if (! fNode.isEmpty())
  1847. {
  1848. // XMLAttribute ZZ = eNode.getAttribute(1);
  1849. // LOG( ZZ.lpszName );
  1850. tStatus->zoom = atof(fNode.getAttribute("x")); // ZZ.lpszValue );
  1851. };
  1852. };
  1853. if (tStatus->pan != .0f || tStatus->tilt != .0f || tStatus->zoom != .0f)
  1854. {
  1855. XMLNode gNode = cNode.getChildNode("MoveStatus", 0);
  1856. if (! gNode.isEmpty())
  1857. {
  1858. tStatus->hasMoveStatus = true;
  1859. XMLNode hNode = gNode.getChildNode("PanTilt", 0);
  1860. if (! hNode.isEmpty())
  1861. {
  1862. if (strstr(hNode.getText(0), "IDLE") != NULL)
  1863. {
  1864. tStatus->PanTiltIdle = true;
  1865. }
  1866. };
  1867. XMLNode iNode = gNode.getChildNode("Zoom", 0);
  1868. if (! iNode.isEmpty())
  1869. {
  1870. if (strstr(iNode.getText(0), "IDLE") != NULL)
  1871. {
  1872. tStatus->ZoomIdle = true;
  1873. }
  1874. };
  1875. };
  1876. }
  1877. return true;
  1878. };
  1879. };
  1880. };
  1881. };
  1882. return false;
  1883. }
  1884. bool pOnvif::ptz_GetPresets(CSTR profileToken)
  1885. {
  1886. if (m_pPTZURI == NULL)
  1887. {
  1888. return false;
  1889. }
  1890. char tBuffer[DEFAULT_BUFFER_SIZE];
  1891. accessToken tTOKEN;
  1892. tTOKEN = _makeToken();
  1893. sprintf(tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1894. if (_send_request(m_pPTZURI, tBuffer))
  1895. {
  1896. // XMLResults tParseResults;
  1897. // XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1898. // LOG("RCPT:");
  1899. // LOG( m_pLastResponse );
  1900. // if( tParseResults.error ){
  1901. // char tBuf[CTEXT];
  1902. // sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1903. // LOG_ERROR( tBuf );
  1904. // } else {
  1905. //$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
  1906. //$presets=array();
  1907. // foreach ($getpresetsresponse as $preset) {
  1908. // $presets[]=array('Token'=>$preset['@attributes']['token'],
  1909. // 'Name'=>$preset['Name'],
  1910. // 'PTZPosition'=>$preset['PTZPosition']);
  1911. // }
  1912. // return $presets;
  1913. return true;
  1914. //};
  1915. };
  1916. return false;
  1917. }
  1918. bool pOnvif::ptz_GetNode(CSTR ptzNodeToken)
  1919. {
  1920. if (m_pPTZURI == NULL)
  1921. {
  1922. return false;
  1923. }
  1924. char tBuffer[DEFAULT_BUFFER_SIZE];
  1925. accessToken tTOKEN;
  1926. tTOKEN = _makeToken();
  1927. sprintf(tBuffer, PONVIF_PACK_GET_PTZ_NODE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, ptzNodeToken);
  1928. if (_send_request(m_pPTZURI, tBuffer))
  1929. {
  1930. XMLResults tParseResults;
  1931. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1932. LOG("RCPT:");
  1933. printf(m_pLastResponse);
  1934. if (tParseResults.error)
  1935. {
  1936. char tBuf[CTEXT];
  1937. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1938. LOG_ERROR(tBuf);
  1939. }
  1940. else
  1941. {
  1942. // return $response['Envelope']['Body']['GetNodeResponse'];
  1943. };
  1944. };
  1945. return false;
  1946. }
  1947. bool pOnvif::ptz_GotoPreset(CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x)
  1948. {
  1949. if (m_pPTZURI == NULL)
  1950. {
  1951. return false;
  1952. }
  1953. char tBuffer[DEFAULT_BUFFER_SIZE];
  1954. accessToken tTOKEN;
  1955. tTOKEN = _makeToken();
  1956. sprintf(tBuffer, PONVIF_PACK_GOTO_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken, speed_pantilt_x, speed_pantilt_y, speed_zoom_x);
  1957. return _send_request(m_pPTZURI, tBuffer, true);
  1958. }
  1959. bool pOnvif::ptz_RemovePreset(CSTR profileToken, CSTR presetToken)
  1960. {
  1961. if (m_pPTZURI == NULL)
  1962. {
  1963. return false;
  1964. }
  1965. char tBuffer[DEFAULT_BUFFER_SIZE];
  1966. accessToken tTOKEN;
  1967. tTOKEN = _makeToken();
  1968. sprintf(tBuffer, PONVIF_PACK_REMOVE_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken);
  1969. return _send_request(m_pPTZURI, tBuffer, true);
  1970. }
  1971. bool pOnvif::ptz_SetPreset(CSTR profileToken, CSTR presetName)
  1972. {
  1973. if (m_pPTZURI == NULL)
  1974. {
  1975. return false;
  1976. }
  1977. char tBuffer[DEFAULT_BUFFER_SIZE];
  1978. accessToken tTOKEN;
  1979. tTOKEN = _makeToken();
  1980. sprintf(tBuffer, PONVIF_PACK_SET_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetName);
  1981. return _send_request(m_pPTZURI, tBuffer, true);
  1982. }
  1983. bool pOnvif::ptz_RelativeMove(CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y)
  1984. {
  1985. if (m_pPTZURI == NULL)
  1986. {
  1987. return false;
  1988. }
  1989. char tBuffer[DEFAULT_BUFFER_SIZE];
  1990. accessToken tTOKEN;
  1991. tTOKEN = _makeToken();
  1992. sprintf(tBuffer, PONVIF_PACK_RELATIVE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, translation_pantilt_x, translation_pantilt_y, speed_pantilt_x, speed_pantilt_y);
  1993. return _send_request(m_pPTZURI, tBuffer, true);
  1994. }
  1995. bool pOnvif::ptz_RelativeMoveZoom(CSTR profileToken, float zoom, float speedZoom)
  1996. {
  1997. if (m_pPTZURI == NULL)
  1998. {
  1999. return false;
  2000. }
  2001. char tBuffer[DEFAULT_BUFFER_SIZE];
  2002. accessToken tTOKEN;
  2003. tTOKEN = _makeToken();
  2004. sprintf(tBuffer, PONVIF_PACK_RELATIVE_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom, speedZoom);
  2005. return _send_request(m_pPTZURI, tBuffer, true);
  2006. }
  2007. bool pOnvif::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom)
  2008. {
  2009. if (m_pPTZURI == NULL)
  2010. {
  2011. return false;
  2012. }
  2013. char tBuffer[DEFAULT_BUFFER_SIZE];
  2014. accessToken tTOKEN;
  2015. tTOKEN = _makeToken();
  2016. sprintf(tBuffer, PONVIF_PACK_ABSOLUTE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, position_pantilt_x, position_pantilt_y, zoom);
  2017. return _send_request(m_pPTZURI, tBuffer, false);
  2018. }
  2019. bool pOnvif::ptz_ContinuousMove(CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y)
  2020. {
  2021. if (m_pPTZURI == NULL)
  2022. {
  2023. return false;
  2024. }
  2025. char tBuffer[DEFAULT_BUFFER_SIZE];
  2026. accessToken tTOKEN;
  2027. tTOKEN = _makeToken();
  2028. sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, velocity_pantilt_x, velocity_pantilt_y);
  2029. return _send_request(m_pPTZURI, tBuffer, true);
  2030. }
  2031. bool pOnvif::ptz_ContinuousMoveZoom(CSTR profileToken, float zoom)
  2032. {
  2033. if (m_pPTZURI == NULL)
  2034. {
  2035. return false;
  2036. }
  2037. char tBuffer[DEFAULT_BUFFER_SIZE];
  2038. accessToken tTOKEN;
  2039. tTOKEN = _makeToken();
  2040. sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom);
  2041. return _send_request(m_pPTZURI, tBuffer, true);
  2042. }
  2043. bool pOnvif::ptz_Stop(CSTR profileToken, bool pantilt, bool zoom)
  2044. {
  2045. if ( pantilt || zoom ) profileToken = profileToken;
  2046. if (m_pPTZURI == NULL)
  2047. {
  2048. return false;
  2049. }
  2050. char tBuffer[DEFAULT_BUFFER_SIZE];
  2051. accessToken tTOKEN;
  2052. tTOKEN = _makeToken();
  2053. sprintf(tBuffer, PONVIF_PACK_PTZ_STOP, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken); //, pantilt, zoom );
  2054. return _send_request(m_pPTZURI, tBuffer, true);
  2055. }
  2056. /*
  2057. Internal functions
  2058. */
  2059. accessToken pOnvif::_makeToken(void)
  2060. {
  2061. time_t tTS = time(NULL) - m_nDeltaTime;
  2062. return _passwordDigest(m_pUsername, m_pPassword, tTS);
  2063. }
  2064. void pOnvif::_getOnvifVersion(nodeCapability & caps)
  2065. {
  2066. if (caps.Device != NULL)
  2067. {
  2068. if (caps.Device->System != NULL)
  2069. {
  2070. if (caps.Device->System->SupportedVersions != NULL)
  2071. {
  2072. m_sOnvifVersion.Major = caps.Device->System->SupportedVersions->Major;
  2073. m_sOnvifVersion.Minor = caps.Device->System->SupportedVersions->Minor;
  2074. };
  2075. };
  2076. };
  2077. if (caps.PTZ != NULL)
  2078. {
  2079. m_pPTZURI = caps.PTZ->XAddr;
  2080. };
  2081. if (caps.Media != NULL)
  2082. {
  2083. m_pMediaURI = caps.Media->XAddr;
  2084. };
  2085. if (caps.Device != NULL)
  2086. {
  2087. m_pDeviceURI = caps.Device->XAddr;
  2088. };
  2089. }
  2090. /*
  2091. protected function _getActiveSources($videoSources,$profiles) {
  2092. $sources=array();
  2093. if (isset($videoSources['@attributes'])) {
  2094. // NVT is a camera
  2095. $sources[0]['sourcetoken']=$videoSources['@attributes']['token'];
  2096. $this->_getProfileData($sources,0,$profiles);
  2097. }
  2098. else {
  2099. // NVT is an encoder
  2100. for ($i=0;$i<count($videoSources);$i++) {
  2101. if (strtolower($videoSources[$i]['@attributes']['SignalActive'])=='true') {
  2102. $sources[$i]['sourcetoken']=$videoSources[$i]['@attributes']['token'];
  2103. $this->_getProfileData($sources,$i,$profiles);
  2104. }
  2105. } // for
  2106. }
  2107. return $sources;
  2108. }
  2109. void pOnvif::_getProfileData( sources,$i, XMLNode profiles ){
  2110. //$inprofile=0;
  2111. videoSourceProfile *tPtr;
  2112. for( $y=0; $y < count($profiles); $y++ ){
  2113. if ($profiles[$y]['VideoSourceConfiguration']['SourceToken']==$sources[$i]['sourcetoken']) {
  2114. $sources[$i][$inprofile]['profilename']=$profiles[$y]['Name'];
  2115. $sources[$i][$inprofile]['profiletoken']=$profiles[$y]['@attributes']['token'];
  2116. if ( isset($profiles[$y]['VideoEncoderConfiguration'])) {
  2117. $sources[$i][$inprofile]['encoding']=$profiles[$y]['VideoEncoderConfiguration']['Encoding'];
  2118. $sources[$i][$inprofile]['width']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Width'];
  2119. $sources[$i][$inprofile]['height']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Height'];
  2120. $sources[$i][$inprofile]['fps']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['FrameRateLimit'];
  2121. $sources[$i][$inprofile]['bitrate']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['BitrateLimit'];
  2122. }
  2123. if ( isset($profiles[$y]['PTZConfiguration'])) {
  2124. $sources[$i][$inprofile]['ptz']['name']=$profiles[$y]['PTZConfiguration']['Name'];
  2125. $sources[$i][$inprofile]['ptz']['nodetoken']=$profiles[$y]['PTZConfiguration']['NodeToken'];
  2126. }
  2127. $inprofile++;
  2128. }
  2129. }
  2130. };
  2131. */
  2132. /*
  2133. protected function _xml2array($response) {
  2134. $sxe = new SimpleXMLElement($response);
  2135. $dom_sxe = dom_import_simplexml($sxe);
  2136. $dom = new DOMDocument('1.0');
  2137. $dom_sxe = $dom->importNode($dom_sxe, true);
  2138. $dom_sxe = $dom->appendChild($dom_sxe);
  2139. $element = $dom->childNodes->item(0);
  2140. foreach ($sxe->getDocNamespaces() as $name => $uri) {
  2141. $element->removeAttributeNS($uri, $name);
  2142. }
  2143. $xmldata=$dom->saveXML();
  2144. $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>"));
  2145. $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>"));
  2146. $xml=simplexml_load_string($xmldata);
  2147. $data=json_decode(json_encode((array)$xml),1);
  2148. $data=array($xml->getName()=>$data);
  2149. return $data;
  2150. }
  2151. */
  2152. accessToken pOnvif::_passwordDigest(CSTR username, CSTR password, time_t timestamp, uint32_t nonce)
  2153. {
  2154. accessToken RetVal;
  2155. char tTS[CTEXT];
  2156. unsigned char * tPDIGEST;
  2157. if (timestamp == 0)
  2158. {
  2159. time_t t;
  2160. struct tm * tmp;
  2161. t = time(NULL);
  2162. tmp = gmtime(&t);
  2163. if (tmp == NULL)
  2164. {
  2165. LOG_ERROR("gmtime Error!");
  2166. };
  2167. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  2168. {
  2169. LOG_ERROR("strftime returned 0!");
  2170. };
  2171. }
  2172. else
  2173. {
  2174. struct tm * tmp;
  2175. tmp = gmtime(&timestamp);
  2176. if (tmp == NULL)
  2177. {
  2178. LOG_ERROR("gmtime Error!");
  2179. };
  2180. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  2181. {
  2182. LOG_ERROR("strftime returned 0!");
  2183. };
  2184. };
  2185. if (nonce == 0)
  2186. {
  2187. nonce = (uint32_t) rand();
  2188. };
  2189. unsigned char tBuff[CTEXT];
  2190. int cp = 0;
  2191. int index = 0;
  2192. for (; index < 4; index++)
  2193. {
  2194. tBuff[cp++] = ((unsigned char *) (&nonce))[index];
  2195. };
  2196. index = 0;
  2197. while (tTS[index] != '\0')
  2198. {
  2199. tBuff[cp++] = (unsigned char) tTS[index];
  2200. index++;
  2201. };
  2202. index = 0;
  2203. while (password[index] != '\0')
  2204. {
  2205. tBuff[cp++] = (unsigned char) password[index];
  2206. index++;
  2207. };
  2208. size_t baseOutLen;
  2209. unsigned char tSHA[SHA_DIGEST_LENGTH];
  2210. SHA1(tBuff, cp, tSHA);
  2211. // tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
  2212. tPDIGEST = base64_encode(tSHA, SHA_DIGEST_LENGTH, &baseOutLen);
  2213. // free( tSHA );
  2214. //$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
  2215. strncpy(RetVal.USERNAME, username, CTEXT);
  2216. strncpy(RetVal.TIMESTAMP, tTS, CTEXT);
  2217. strncpy(RetVal.PASSDIGEST, (const char *) tPDIGEST, CTEXT);
  2218. free(tPDIGEST);
  2219. tPDIGEST = base64_encode((unsigned char *) &nonce, 4, &baseOutLen);
  2220. strncpy(RetVal.NONCE, (const char *) tPDIGEST, baseOutLen);
  2221. RetVal.NONCE[baseOutLen] = '\0';
  2222. free(tPDIGEST);
  2223. //$REQ['NONCE']=base64_encode($nonce); // alternative
  2224. return RetVal;
  2225. }
  2226. void * pOnvif::parser(void * obj, void * ptr)
  2227. {
  2228. pOnvif * ME = (pOnvif *) obj;
  2229. curlRequest * RQ = (curlRequest *) ptr;
  2230. if (ME)
  2231. {
  2232. if (ME->m_pLastResponse)
  2233. {
  2234. free(ME->m_pLastResponse);
  2235. }
  2236. ME->m_pLastResponse = (CSTR) malloc(RQ->getSize() + 1);
  2237. if (ME->m_pLastResponse)
  2238. {
  2239. memcpy(ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize());
  2240. ME->m_pLastResponse[RQ->getSize()] = (char) 0;
  2241. #ifdef DEBUG_ONVIF_TRAFFIC
  2242. FILE * tf;
  2243. tf = fopen("Traffic.txt", "a+");
  2244. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  2245. fprintf(tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse);
  2246. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  2247. fclose(tf);
  2248. #endif
  2249. };
  2250. ME->parser_done = true;
  2251. };
  2252. return NULL;
  2253. }
  2254. bool pOnvif::_send_request(CSTR url, CSTR post_string, bool mustReturnData)
  2255. {
  2256. #ifdef CURL_MANAGER
  2257. bool RetVal = false;
  2258. if (m_pHttpAccessManager)
  2259. {
  2260. m_mResponseProtector.lock();
  2261. curlRequest * tRC; // application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime"
  2262. tRC = new curlRequest(url, (CSTR) "application/soap+xml; charset=utf-8;");
  2263. if (tRC != NULL)
  2264. {
  2265. #ifdef DEBUG_ONVIF_TRAFFIC
  2266. FILE * tf;
  2267. tf = fopen("Traffic.txt", "a+");
  2268. fprintf(tf, "--------------------\r\n");
  2269. fprintf(tf, "REQ: %s (%d)\n", url, strlen(post_string));
  2270. fprintf(tf, ">>>>> %s\n", post_string);
  2271. fprintf(tf, "--------------------\r\n");
  2272. fclose(tf);
  2273. mustReturnData = true; // In Debug Allways Read Return
  2274. #endif
  2275. tRC->setCredentials(m_pUsername, m_pPassword, m_bAuthMode);
  2276. tRC->setPOST(post_string, strlen(post_string));
  2277. if (mustReturnData)
  2278. {
  2279. tRC->setHandler((void *) this, pOnvif::parser, (void *) tRC);
  2280. if (m_pHttpAccessManager->accomplish(tRC))
  2281. {
  2282. parser_done = false;
  2283. uint8_t cnt = 200; // TO = 2 second
  2284. while (! RetVal && cnt--)
  2285. {
  2286. RetVal = parser_done;
  2287. SLEEP(10);
  2288. };
  2289. };
  2290. }
  2291. else
  2292. {
  2293. RetVal = m_pHttpAccessManager->accomplish(tRC);
  2294. };
  2295. }
  2296. else
  2297. {
  2298. LOG_ERROR("Cannot Create curlRequest!");
  2299. }
  2300. m_mResponseProtector.unlock();
  2301. };
  2302. return RetVal;
  2303. #else
  2304. char tBuffer[CTEXT];
  2305. if (m_pCURL == NULL)
  2306. {
  2307. if (m_pLastResponse == NULL)
  2308. {
  2309. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  2310. };
  2311. m_pCURL = curl_easy_init();
  2312. if (m_pProxyHost != NULL && m_nProxyPort != 0)
  2313. {
  2314. curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
  2315. curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
  2316. if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
  2317. {
  2318. sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
  2319. curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
  2320. };
  2321. };
  2322. curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  2323. curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
  2324. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
  2325. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
  2326. curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
  2327. curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
  2328. // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  2329. // curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  2330. // curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  2331. curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
  2332. curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
  2333. // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  2334. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
  2335. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  2336. };
  2337. if (m_pCURL != NULL)
  2338. {
  2339. curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
  2340. struct curl_slist * sList = NULL;
  2341. // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  2342. // Connection: Close
  2343. sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
  2344. sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
  2345. sList = curl_slist_append(sList, tBuffer);
  2346. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
  2347. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
  2348. sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
  2349. curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
  2350. switch (m_bAuthMode)
  2351. {
  2352. case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
  2353. case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
  2354. case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
  2355. case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
  2356. case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
  2357. case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
  2358. case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
  2359. case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
  2360. case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
  2361. case 9:
  2362. curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
  2363. break;
  2364. // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  2365. };
  2366. #ifdef DEBUG
  2367. char tErrorBuffer[CURL_ERROR_SIZE];
  2368. curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
  2369. #endif
  2370. m_pLastIndex = 0;
  2371. if (curl_easy_perform(m_pCURL))
  2372. {
  2373. LOG_ERROR("CURL Failure!");
  2374. #ifdef DEBUG
  2375. LOG_ERROR(tErrorBuffer);
  2376. #endif
  2377. }
  2378. else
  2379. {
  2380. m_pLastResponse[m_pLastIndex] = (char) 0;
  2381. return true;
  2382. };
  2383. m_pLastIndex = 0;
  2384. };
  2385. return false;
  2386. #endif
  2387. }
  2388. #ifndef CURL_MANAGER
  2389. int pOnvif::_writer(char * data, size_t size, size_t nmemb, pOnvif * Ptr)
  2390. {
  2391. if (Ptr == NULL)
  2392. {
  2393. return 0;
  2394. }
  2395. // TLOG("*");
  2396. memcpy(Ptr->m_pLastResponse + Ptr->m_pLastIndex, data, size * nmemb);
  2397. Ptr->m_pLastIndex += size * nmemb;
  2398. return size * nmemb;
  2399. }
  2400. #endif
  2401. bool pOnvif::setAuthMode(uint8_t Mode)
  2402. {
  2403. m_bAuthMode = Mode;
  2404. #ifdef DEBUG
  2405. switch (m_bAuthMode)
  2406. {
  2407. case 0 : LOG("Set Auth CURLAUTH_BASIC"); break;
  2408. case 1 : LOG("Set Auth CURLAUTH_DIGEST"); break;
  2409. case 2 : LOG("Set Auth CURLAUTH_DIGEST_IE"); break;
  2410. case 3 : LOG("Set Auth CURLAUTH_BEARER"); break;
  2411. case 4 : LOG("Set Auth CURLAUTH_NEGOTIATE"); break;
  2412. case 5 : LOG("Set Auth CURLAUTH_NTLM"); break;
  2413. case 6 : LOG("Set Auth CURLAUTH_NTLM_WB"); break;
  2414. case 7 : LOG("Set Auth CURLAUTH_ANY"); break;
  2415. case 8 : LOG("Set Auth CURLAUTH_ANYSAFE"); break;
  2416. case 9 : LOG("Set Auth CURLAUTH_ONLY"); break;
  2417. case 10: LOG("Set Auth CURLAUTH_AWS_SIGV4"); break;
  2418. };
  2419. #endif
  2420. return true;
  2421. }
  2422. #undef LOG
  2423. #undef TLOG
  2424. #undef LOG_ERROR