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.

2453 lines
72 KiB

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