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.

2445 lines
72 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. 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. #ifdef DEBUG_EXTRA
  601. LOG("Get Instance m_pHttpAccessManager");
  602. #endif
  603. m_pHttpAccessManager = curlRequestsManager::getInstance();
  604. if (m_pHttpAccessManager == NULL) {
  605. return false;
  606. }
  607. #ifdef DEBUG_EXTRA
  608. LOG("CURL MANAGER OK");
  609. #endif
  610. try {
  611. struct tm * tTS;
  612. tTS = core_GetSystemDateAndTime();
  613. // This is UTS DTS
  614. if (tTS != NULL) {
  615. #ifdef DEBUG
  616. char tB[128];
  617. 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);
  618. LOG(tB);
  619. #endif
  620. // WTF ??!!
  621. tTS->tm_year -= 1900;
  622. tTS->tm_mon -= 1;
  623. time_t tTimeStamp = timegm(tTS); // mktime( tTS );
  624. m_nDeltaTime = time(NULL) - tTimeStamp;
  625. #ifdef DEBUG
  626. sprintf(tB, "TS:[%ld] D:[%ld]", tTimeStamp, m_nDeltaTime);
  627. LOG(tB);
  628. #endif
  629. free(tTS);
  630. } else {
  631. return false;
  632. };
  633. } catch (const std::exception& e) {
  634. LOG_ERROR("Catched Exception!");
  635. return false;
  636. };
  637. // Are we need that all at beginning?
  638. if (! core_GetCapabilities())
  639. {
  640. LOG_ERROR("Error Getting Caps!");
  641. return false;
  642. };
  643. _getOnvifVersion(m_sCapabilities);
  644. // Update some Vars Online!
  645. // preg_match("/^http(.*)onvif\//",$this->mediauri,$matches);
  646. // m_pBaseURI = $matches[0];
  647. if (media_GetVideoSources())
  648. {
  649. if (media_GetProfiles())
  650. {
  651. //_getActiveSources( m_vVideoSources, m_vVideoProfiles);
  652. return true;
  653. };
  654. };
  655. LOG_ERROR("Error Profiles!");
  656. return true;
  657. }
  658. /*
  659. Public wrappers for a subset of ONVIF primitives
  660. */
  661. struct tm * pOnvif::core_GetSystemDateAndTime(void)
  662. {
  663. struct tm * RetVal = NULL;
  664. char tURL[CTEXT];
  665. if (m_pDeviceURI != NULL)
  666. {
  667. sprintf(tURL, m_pDeviceURI);
  668. }
  669. else
  670. {
  671. if (m_pIpAddress != NULL)
  672. {
  673. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  674. }
  675. else
  676. {
  677. return NULL;
  678. };
  679. };
  680. #ifdef DEBUG_EXTRA
  681. LOG("Before REQ");
  682. #endif
  683. if (_send_request(tURL, PONVIF_PACK_GET_SYS_DTS, true))
  684. {
  685. XMLResults tParseResults;
  686. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  687. #ifdef DEBUG_EXTRA
  688. LOG("core_GetSystemDateAndTime");
  689. #endif
  690. if (tParseResults.error)
  691. {
  692. char tBuffer[CTEXT];
  693. sprintf(tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  694. LOG_ERROR(tBuffer);
  695. }
  696. else
  697. {
  698. RetVal = (struct tm *) malloc(sizeof(struct tm));
  699. if (RetVal != NULL)
  700. {
  701. memset(RetVal, 0, sizeof(struct tm));
  702. XMLNode zNode = response.getChildNode("Body", 0);
  703. if (! zNode.isEmpty())
  704. {
  705. XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0);
  706. if (! fNode.isEmpty())
  707. {
  708. XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0);
  709. if (! hNode.isEmpty())
  710. {
  711. XMLNode tNode = hNode.getChildNode("UTCDateTime", 0);
  712. if (! tNode.isEmpty())
  713. {
  714. RetVal->tm_isdst = 0;
  715. XMLNode ttNode = tNode.getChildNode("Time", 0);
  716. XMLNode dtNode = tNode.getChildNode("Date", 0);
  717. {
  718. XMLNode t = ttNode.getChildNode("Hour", 0);
  719. RetVal->tm_hour = atoi(t.getText(0));
  720. }
  721. {
  722. XMLNode t = ttNode.getChildNode("Minute", 0);
  723. RetVal->tm_min = atoi(t.getText(0));
  724. }
  725. {
  726. XMLNode t = ttNode.getChildNode("Second", 0);
  727. RetVal->tm_sec = atoi(t.getText(0));
  728. }
  729. {
  730. XMLNode t = dtNode.getChildNode("Year", 0);
  731. RetVal->tm_year = atoi(t.getText(0));
  732. }
  733. {
  734. XMLNode t = dtNode.getChildNode("Month", 0);
  735. RetVal->tm_mon = atoi(t.getText(0));
  736. }
  737. {
  738. XMLNode t = dtNode.getChildNode("Day", 0);
  739. RetVal->tm_mday = atoi(t.getText(0));
  740. }
  741. }
  742. else
  743. {
  744. LOG_ERROR("UTCDateTime is Not Found!");
  745. };
  746. }
  747. else
  748. {
  749. LOG_ERROR("SystemDateAndTime is Not Found!");
  750. };
  751. }
  752. else
  753. {
  754. LOG_ERROR("GetSystemDateAndTimeResponse is Not Found!");
  755. };
  756. }
  757. else
  758. {
  759. LOG_ERROR("Body is Not Found!");
  760. LOG(m_pLastResponse);
  761. };
  762. };
  763. return RetVal;
  764. };
  765. }
  766. else
  767. {
  768. LOG_ERROR("GetSystemDateAndTime: Communication error");
  769. }
  770. return NULL;
  771. }
  772. // Based on https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl
  773. bool pOnvif::core_GetCapabilities(void)
  774. {
  775. char tBuffer[DEFAULT_BUFFER_SIZE];
  776. accessToken tTOKEN;
  777. tTOKEN = _makeToken();
  778. char tURL[CTEXT];
  779. if (m_pDeviceURI != NULL)
  780. {
  781. sprintf(tURL, m_pDeviceURI);
  782. }
  783. else
  784. {
  785. if (m_pIpAddress != NULL)
  786. {
  787. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  788. }
  789. else
  790. {
  791. return NULL;
  792. };
  793. };
  794. // char tSTR[CTEXT];
  795. sprintf(tBuffer, PONVIF_PACK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  796. // sprintf( tSTR, PONVIF_PCK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  797. // build_package( tBuffer, PONVIF_ENVELOPE_SIM, tSTR );
  798. if (_send_request(tURL, tBuffer))
  799. {
  800. XMLResults tParseResults;
  801. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  802. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  803. LOG("RCPT:");
  804. printf(m_pLastResponse);
  805. #endif
  806. if (tParseResults.error != eXMLErrorNone)
  807. {
  808. char tBuf[CTEXT];
  809. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  810. LOG_ERROR(tBuf);
  811. }
  812. else
  813. {
  814. XMLNode zNode = response.getChildNode("Body", 0);
  815. if (zNode.isEmpty())
  816. {
  817. return false;
  818. }
  819. XMLNode tNode = zNode.getChildNode("GetCapabilitiesResponse", 0);
  820. if (! tNode.isEmpty())
  821. {
  822. XMLNode tCaps = tNode.getChildNode("Capabilities", 0);
  823. if (! tCaps.isEmpty())
  824. {
  825. {
  826. XMLNode t = tCaps.getChildNode("Analytics", 0);
  827. if (! t.isEmpty())
  828. {
  829. m_sCapabilities.Analytics = (onvifAnalyticsCapabilities *) malloc(sizeof(onvifAnalyticsCapabilities));
  830. if (m_sCapabilities.Analytics != NULL)
  831. {
  832. memset(m_sCapabilities.Analytics, 0, sizeof(onvifAnalyticsCapabilities));
  833. {
  834. XMLNode c = t.getChildNode("XAddr", 0);
  835. sprintf(m_sCapabilities.Analytics->XAddr, "%s", c.getText(0));
  836. }
  837. {
  838. XMLNode c = t.getChildNode("RuleSupport", 0);
  839. m_sCapabilities.Analytics->RuleSupport = checkBool(c.getText(0));
  840. }
  841. {
  842. XMLNode c = t.getChildNode("AnalyticsModuleSupport", 0);
  843. m_sCapabilities.Analytics->AnalyticsModuleSupport = checkBool(c.getText(0));
  844. }
  845. };
  846. };
  847. }
  848. {
  849. XMLNode t = tCaps.getChildNode("Device", 0);
  850. if (! t.isEmpty())
  851. {
  852. m_sCapabilities.Device = (onvifDeviceCapabilities *) malloc(sizeof(onvifDeviceCapabilities));
  853. if (m_sCapabilities.Device != NULL)
  854. {
  855. memset(m_sCapabilities.Device, 0, sizeof(onvifDeviceCapabilities));
  856. {
  857. XMLNode c = t.getChildNode("XAddr", 0);
  858. sprintf(m_sCapabilities.Device->XAddr, "%s", c.getText(0));
  859. }
  860. {
  861. XMLNode c = t.getChildNode("Network", 0);
  862. if (! c.isEmpty())
  863. {
  864. m_sCapabilities.Device->Network = (onvifNetworkCapabilities *) malloc(sizeof(onvifNetworkCapabilities));
  865. if (m_sCapabilities.Device->Network != NULL)
  866. {
  867. memset(m_sCapabilities.Device->Network, 0, sizeof(onvifNetworkCapabilities));
  868. {
  869. XMLNode n = c.getChildNode("IPFilter", 0);
  870. m_sCapabilities.Device->Network->IPFilter = checkBool(n.getText(0));
  871. }
  872. {
  873. XMLNode n = c.getChildNode("ZeroConfiguration", 0);
  874. m_sCapabilities.Device->Network->ZeroConfiguration = checkBool(n.getText(0));
  875. }
  876. {
  877. XMLNode n = c.getChildNode("IPVersion6", 0);
  878. m_sCapabilities.Device->Network->IPVersion6 = checkBool(n.getText(0));
  879. }
  880. {
  881. XMLNode n = c.getChildNode("DynDNS", 0);
  882. m_sCapabilities.Device->Network->DynDNS = checkBool(n.getText(0));
  883. }
  884. {
  885. XMLNode n = c.getChildNode("Extension", 0);
  886. if (! n.isEmpty())
  887. {
  888. m_sCapabilities.Device->Network->Extension = (onvifNetworkCapabilitiesExtension *) malloc(sizeof(onvifNetworkCapabilitiesExtension));
  889. if (m_sCapabilities.Device->Network->Extension != NULL)
  890. {
  891. memset(m_sCapabilities.Device->Network->Extension, 0, sizeof(onvifNetworkCapabilitiesExtension));
  892. {
  893. XMLNode x = n.getChildNode("Dot11Configuration", 0);
  894. m_sCapabilities.Device->Network->Extension->Dot11Configuration = checkBool(x.getText(0));
  895. }
  896. };
  897. };
  898. }
  899. };
  900. };
  901. }
  902. {
  903. XMLNode c = t.getChildNode("System", 0);
  904. if (! c.isEmpty())
  905. {
  906. m_sCapabilities.Device->System = (onvifSystemCapabilities *) malloc(sizeof(onvifSystemCapabilities));
  907. if (m_sCapabilities.Device->System != NULL)
  908. {
  909. memset(m_sCapabilities.Device->System, 0, sizeof(onvifSystemCapabilities));
  910. {
  911. XMLNode n = c.getChildNode("DiscoveryResolve", 0);
  912. m_sCapabilities.Device->System->DiscoveryResolve = checkBool(n.getText(0));
  913. }
  914. {
  915. XMLNode n = c.getChildNode("DiscoveryBye", 0);
  916. m_sCapabilities.Device->System->DiscoveryBye = checkBool(n.getText(0));
  917. }
  918. {
  919. XMLNode n = c.getChildNode("RemoteDiscovery", 0);
  920. m_sCapabilities.Device->System->RemoteDiscovery = checkBool(n.getText(0));
  921. }
  922. {
  923. XMLNode n = c.getChildNode("SystemBackup", 0);
  924. m_sCapabilities.Device->System->SystemBackup = checkBool(n.getText(0));
  925. }
  926. {
  927. XMLNode n = c.getChildNode("SystemLogging", 0);
  928. m_sCapabilities.Device->System->SystemLogging = checkBool(n.getText(0));
  929. }
  930. {
  931. XMLNode n = c.getChildNode("FirmwareUpgrade", 0);
  932. m_sCapabilities.Device->System->FirmwareUpgrade = checkBool(n.getText(0));
  933. }
  934. {
  935. XMLNode n = c.getChildNode("SupportedVersions", 0);
  936. if (! n.isEmpty())
  937. {
  938. m_sCapabilities.Device->System->SupportedVersions = (onvifSupportedVersions *) malloc(sizeof(onvifSupportedVersions));
  939. if (m_sCapabilities.Device->System->SupportedVersions != NULL)
  940. {
  941. memset(m_sCapabilities.Device->System->SupportedVersions, 0, sizeof(onvifSupportedVersions));
  942. {
  943. XMLNode x = n.getChildNode("Major", 0);
  944. m_sCapabilities.Device->System->SupportedVersions->Major = atoi(x.getText(0));
  945. }
  946. {
  947. XMLNode x = n.getChildNode("Minor", 0);
  948. m_sCapabilities.Device->System->SupportedVersions->Minor = atoi(x.getText(0));
  949. }
  950. };
  951. };
  952. }
  953. {
  954. XMLNode n = c.getChildNode("Extension", 0);
  955. if (! n.isEmpty())
  956. {
  957. m_sCapabilities.Device->System->Extension = (onvifSystemCapabilitiesExtension *) malloc(sizeof(onvifSystemCapabilitiesExtension));
  958. if (m_sCapabilities.Device->System->Extension != NULL)
  959. {
  960. memset(m_sCapabilities.Device->System->Extension, 0, sizeof(onvifSystemCapabilitiesExtension));
  961. {
  962. XMLNode x = n.getChildNode("HttpFirmwareUpgrade", 0);
  963. m_sCapabilities.Device->System->Extension->HttpFirmwareUpgrade = checkBool(x.getText(0));
  964. }
  965. {
  966. XMLNode x = n.getChildNode("HttpSystemBackup", 0);
  967. m_sCapabilities.Device->System->Extension->HttpSystemBackup = checkBool(x.getText(0));
  968. }
  969. {
  970. XMLNode x = n.getChildNode("HttpSystemLogging", 0);
  971. m_sCapabilities.Device->System->Extension->HttpSystemLogging = checkBool(x.getText(0));
  972. }
  973. {
  974. XMLNode x = n.getChildNode("HttpSupportInformation", 0);
  975. m_sCapabilities.Device->System->Extension->HttpSupportInformation = checkBool(x.getText(0));
  976. }
  977. };
  978. };
  979. }
  980. };
  981. };
  982. }
  983. {
  984. XMLNode c = t.getChildNode("IO", 0);
  985. if (! c.isEmpty())
  986. {
  987. m_sCapabilities.Device->IO = (onvifIOCapabilities *) malloc(sizeof(onvifIOCapabilities));
  988. if (m_sCapabilities.Device->IO != NULL)
  989. {
  990. memset(m_sCapabilities.Device->IO, 0, sizeof(onvifIOCapabilities));
  991. {
  992. XMLNode n = c.getChildNode("InputConnectors", 0);
  993. m_sCapabilities.Device->IO->InputConnectors = atoi(n.getText(0));
  994. }
  995. {
  996. XMLNode n = c.getChildNode("RelayOutputs", 0);
  997. m_sCapabilities.Device->IO->RelayOutputs = atoi(n.getText(0));
  998. }
  999. {
  1000. XMLNode n = c.getChildNode("Extension", 0);
  1001. if (! n.isEmpty())
  1002. {
  1003. m_sCapabilities.Device->IO->Extension = (onvifIOCapabilitiesExtension *) malloc(sizeof(onvifIOCapabilitiesExtension));
  1004. if (m_sCapabilities.Device->IO->Extension != NULL)
  1005. {
  1006. memset(m_sCapabilities.Device->IO->Extension, 0, sizeof(onvifIOCapabilitiesExtension));
  1007. {
  1008. XMLNode x = n.getChildNode("Auxiliary", 0);
  1009. m_sCapabilities.Device->IO->Extension->Auxiliary = checkBool(x.getText(0));
  1010. }
  1011. };
  1012. };
  1013. }
  1014. };
  1015. };
  1016. }
  1017. {
  1018. XMLNode c = t.getChildNode("Security", 0);
  1019. if (! c.isEmpty())
  1020. {
  1021. m_sCapabilities.Device->Security = (onvifSecurityCapabilities *) malloc(sizeof(onvifSecurityCapabilities));
  1022. if (m_sCapabilities.Device->Security != NULL)
  1023. {
  1024. memset(m_sCapabilities.Device->Security, 0, sizeof(onvifSecurityCapabilities));
  1025. {
  1026. XMLNode n = c.getChildNode("TLS1.1", 0);
  1027. m_sCapabilities.Device->Security->TLS1_1 = checkBool(n.getText(0));
  1028. }
  1029. {
  1030. XMLNode n = c.getChildNode("TLS1.2", 0);
  1031. m_sCapabilities.Device->Security->TLS1_2 = checkBool(n.getText(0));
  1032. }
  1033. {
  1034. XMLNode n = c.getChildNode("OnboardKeyGeneration", 0);
  1035. m_sCapabilities.Device->Security->OnboardKeyGeneration = checkBool(n.getText(0));
  1036. }
  1037. {
  1038. XMLNode n = c.getChildNode("AccessPolicyConfig", 0);
  1039. m_sCapabilities.Device->Security->AccessPolicyConfig = checkBool(n.getText(0));
  1040. }
  1041. {
  1042. XMLNode n = c.getChildNode("X.509Token", 0);
  1043. m_sCapabilities.Device->Security->X_509Token = checkBool(n.getText(0));
  1044. }
  1045. {
  1046. XMLNode n = c.getChildNode("SAMLToken", 0);
  1047. m_sCapabilities.Device->Security->SAMLToken = checkBool(n.getText(0));
  1048. }
  1049. {
  1050. XMLNode n = c.getChildNode("KerberosToken", 0);
  1051. m_sCapabilities.Device->Security->KerberosToken = checkBool(n.getText(0));
  1052. }
  1053. {
  1054. XMLNode n = c.getChildNode("RELToken", 0);
  1055. m_sCapabilities.Device->Security->RELToken = checkBool(n.getText(0));
  1056. }
  1057. {
  1058. XMLNode n = c.getChildNode("Extension", 0);
  1059. if (! n.isEmpty())
  1060. {
  1061. m_sCapabilities.Device->Security->Extension = (onvifSecurityCapabilitiesExtension *) malloc(sizeof(onvifSecurityCapabilitiesExtension));
  1062. if (m_sCapabilities.Device->Security->Extension != NULL)
  1063. {
  1064. memset(m_sCapabilities.Device->Security->Extension, 0, sizeof(onvifSecurityCapabilitiesExtension));
  1065. {
  1066. XMLNode x = n.getChildNode("TLS1.0", 0);
  1067. m_sCapabilities.Device->Security->Extension->TLS1_0 = checkBool(x.getText(0));
  1068. }
  1069. };
  1070. };
  1071. }
  1072. };
  1073. };
  1074. }
  1075. };
  1076. };
  1077. }
  1078. {
  1079. XMLNode t = tCaps.getChildNode("Events", 0);
  1080. if (! t.isEmpty())
  1081. {
  1082. m_sCapabilities.Events = (onvifEventCapabilities *) malloc(sizeof(onvifEventCapabilities));
  1083. if (m_sCapabilities.Events != NULL)
  1084. {
  1085. memset(m_sCapabilities.Events, 0, sizeof(onvifEventCapabilities));
  1086. {
  1087. XMLNode c = t.getChildNode("XAddr", 0);
  1088. sprintf(m_sCapabilities.Events->XAddr, "%s", c.getText(0));
  1089. }
  1090. {
  1091. XMLNode c = t.getChildNode("WSSubscriptionPolicySupport", 0);
  1092. m_sCapabilities.Events->WSSubscriptionPolicySupport = checkBool(c.getText(0));
  1093. }
  1094. {
  1095. XMLNode c = t.getChildNode("WSPullPointSupport", 0);
  1096. m_sCapabilities.Events->WSPullPointSupport = checkBool(c.getText(0));
  1097. }
  1098. {
  1099. XMLNode c = t.getChildNode("WSPausableSubscriptionManagerInterfaceSupport", 0);
  1100. m_sCapabilities.Events->WSPausableSubscriptionManagerInterfaceSupport = checkBool(c.getText(0));
  1101. }
  1102. };
  1103. };
  1104. }
  1105. {
  1106. XMLNode t = tCaps.getChildNode("Imaging", 0);
  1107. if (! t.isEmpty())
  1108. {
  1109. m_sCapabilities.Imaging = (onvifImagingCapabilities *) malloc(sizeof(onvifImagingCapabilities));
  1110. if (m_sCapabilities.Imaging != NULL)
  1111. {
  1112. memset(m_sCapabilities.Imaging, 0, sizeof(onvifImagingCapabilities));
  1113. {
  1114. XMLNode c = t.getChildNode("XAddr", 0);
  1115. sprintf(m_sCapabilities.Imaging->XAddr, "%s", c.getText(0));
  1116. }
  1117. };
  1118. };
  1119. }
  1120. {
  1121. XMLNode t = tCaps.getChildNode("Media", 0);
  1122. if (! t.isEmpty())
  1123. {
  1124. m_sCapabilities.Media = (onvifMediaCapabilities *) malloc(sizeof(onvifMediaCapabilities));
  1125. if (m_sCapabilities.Media != NULL)
  1126. {
  1127. memset(m_sCapabilities.Media, 0, sizeof(onvifMediaCapabilities));
  1128. {
  1129. XMLNode c = t.getChildNode("XAddr", 0);
  1130. sprintf(m_sCapabilities.Media->XAddr, "%s", c.getText(0));
  1131. }
  1132. {
  1133. XMLNode c = t.getChildNode("StreamingCapabilities", 0);
  1134. if (! c.isEmpty())
  1135. {
  1136. m_sCapabilities.Media->StreamingCapabilities = (onvifRealTimeStreamingCapabilities *) malloc(sizeof(onvifRealTimeStreamingCapabilities));
  1137. if (m_sCapabilities.Media->StreamingCapabilities != NULL)
  1138. {
  1139. memset(m_sCapabilities.Media->StreamingCapabilities, 0, sizeof(onvifRealTimeStreamingCapabilities));
  1140. {
  1141. XMLNode n = c.getChildNode("RTPMulticast", 0);
  1142. m_sCapabilities.Media->StreamingCapabilities->RTPMulticast = checkBool(n.getText(0));
  1143. }
  1144. {
  1145. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1146. m_sCapabilities.Media->StreamingCapabilities->RTP_TCP = checkBool(n.getText(0));
  1147. }
  1148. {
  1149. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1150. m_sCapabilities.Media->StreamingCapabilities->RTP_RTSP_TCP = checkBool(n.getText(0));
  1151. }
  1152. };
  1153. };
  1154. }
  1155. {
  1156. XMLNode c = t.getChildNode("Extension", 0);
  1157. if (! c.isEmpty())
  1158. {
  1159. m_sCapabilities.Media->Extension = (onvifMediaCapabilitiesExtension *) malloc(sizeof(onvifMediaCapabilitiesExtension));
  1160. if (m_sCapabilities.Media->Extension != NULL)
  1161. {
  1162. memset(m_sCapabilities.Media->Extension, 0, sizeof(onvifMediaCapabilitiesExtension));
  1163. {
  1164. XMLNode n = c.getChildNode("ProfileCapabilities", 0);
  1165. if (! n.isEmpty())
  1166. {
  1167. m_sCapabilities.Media->Extension->ProfileCapabilities = (onvifProfileCapabilities *) malloc(sizeof(onvifProfileCapabilities));
  1168. if (m_sCapabilities.Media->Extension->ProfileCapabilities != NULL)
  1169. {
  1170. memset(m_sCapabilities.Media->Extension->ProfileCapabilities, 0, sizeof(onvifProfileCapabilities));
  1171. {
  1172. XMLNode x = n.getChildNode("MaximumNumberOfProfiles", 0);
  1173. m_sCapabilities.Media->Extension->ProfileCapabilities->MaximumNumberOfProfiles = atoi(x.getText(0));
  1174. }
  1175. };
  1176. };
  1177. }
  1178. };
  1179. };
  1180. }
  1181. };
  1182. };
  1183. }
  1184. {
  1185. XMLNode t = tCaps.getChildNode("PTZ", 0);
  1186. if (! t.isEmpty())
  1187. {
  1188. m_sCapabilities.PTZ = (onvifPTZCapabilities *) malloc(sizeof(onvifPTZCapabilities));
  1189. if (m_sCapabilities.PTZ != NULL)
  1190. {
  1191. memset(m_sCapabilities.PTZ, 0, sizeof(onvifPTZCapabilities));
  1192. {
  1193. XMLNode c = t.getChildNode("XAddr", 0);
  1194. sprintf(m_sCapabilities.PTZ->XAddr, "%s", c.getText(0));
  1195. }
  1196. };
  1197. };
  1198. }
  1199. {
  1200. XMLNode t = tCaps.getChildNode("Extension", 0);
  1201. if (! t.isEmpty())
  1202. {
  1203. m_sCapabilities.Extension = (onvifCapabilitiesExtension *) malloc(sizeof(onvifCapabilitiesExtension));
  1204. if (m_sCapabilities.Extension != NULL)
  1205. {
  1206. memset(m_sCapabilities.Extension, 0, sizeof(onvifCapabilitiesExtension));
  1207. {
  1208. XMLNode c = t.getChildNode("DeviceIO", 0);
  1209. if (! c.isEmpty())
  1210. {
  1211. m_sCapabilities.Extension->DeviceIO = (onvifDeviceIOCapabilities *) malloc(sizeof(onvifDeviceIOCapabilities));
  1212. if (m_sCapabilities.Extension->DeviceIO != NULL)
  1213. {
  1214. memset(m_sCapabilities.Extension->DeviceIO, 0, sizeof(onvifDeviceIOCapabilities));
  1215. {
  1216. XMLNode n = c.getChildNode("XAddr", 0);
  1217. sprintf(m_sCapabilities.Extension->DeviceIO->XAddr, "%s", n.getText(0));
  1218. }
  1219. {
  1220. XMLNode n = c.getChildNode("VideoSources", 0);
  1221. m_sCapabilities.Extension->DeviceIO->VideoSources = atoi(n.getText(0));
  1222. }
  1223. {
  1224. XMLNode n = c.getChildNode("VideoOutputs", 0);
  1225. m_sCapabilities.Extension->DeviceIO->VideoOutputs = atoi(n.getText(0));
  1226. }
  1227. {
  1228. XMLNode n = c.getChildNode("AudioSources", 0);
  1229. m_sCapabilities.Extension->DeviceIO->AudioSources = atoi(n.getText(0));
  1230. }
  1231. {
  1232. XMLNode n = c.getChildNode("AudioOutputs", 0);
  1233. m_sCapabilities.Extension->DeviceIO->AudioOutputs = atoi(n.getText(0));
  1234. }
  1235. {
  1236. XMLNode n = c.getChildNode("RelayOutputs", 0);
  1237. m_sCapabilities.Extension->DeviceIO->RelayOutputs = atoi(n.getText(0));
  1238. }
  1239. };
  1240. };
  1241. }
  1242. {
  1243. XMLNode c = t.getChildNode("Display", 0);
  1244. if (! c.isEmpty())
  1245. {
  1246. m_sCapabilities.Extension->Display = (onvifDisplayCapabilities *) malloc(sizeof(onvifDisplayCapabilities));
  1247. if (m_sCapabilities.Extension->Display != NULL)
  1248. {
  1249. memset(m_sCapabilities.Extension->Display, 0, sizeof(onvifDisplayCapabilities));
  1250. {
  1251. XMLNode n = c.getChildNode("XAddr", 0);
  1252. sprintf(m_sCapabilities.Extension->Display->XAddr, "%s", n.getText(0));
  1253. }
  1254. {
  1255. XMLNode n = c.getChildNode("FixedLayout", 0);
  1256. m_sCapabilities.Extension->Display->FixedLayout = checkBool(n.getText(0));
  1257. }
  1258. };
  1259. };
  1260. }
  1261. {
  1262. XMLNode c = t.getChildNode("Recording", 0);
  1263. if (! c.isEmpty())
  1264. {
  1265. m_sCapabilities.Extension->Recording = (onvifRecordingCapabilities *) malloc(sizeof(onvifRecordingCapabilities));
  1266. if (m_sCapabilities.Extension->Recording != NULL)
  1267. {
  1268. memset(m_sCapabilities.Extension->Recording, 0, sizeof(onvifRecordingCapabilities));
  1269. {
  1270. XMLNode n = c.getChildNode("XAddr", 0);
  1271. sprintf(m_sCapabilities.Extension->Recording->XAddr, "%s", n.getText(0));
  1272. }
  1273. {
  1274. XMLNode n = c.getChildNode("ReceiverSource", 0);
  1275. m_sCapabilities.Extension->Recording->ReceiverSource = checkBool(n.getText(0));
  1276. }
  1277. {
  1278. XMLNode n = c.getChildNode("MediaProfileSource", 0);
  1279. m_sCapabilities.Extension->Recording->MediaProfileSource = checkBool(n.getText(0));
  1280. }
  1281. {
  1282. XMLNode n = c.getChildNode("DynamicRecordings", 0);
  1283. m_sCapabilities.Extension->Recording->DynamicRecordings = checkBool(n.getText(0));
  1284. }
  1285. {
  1286. XMLNode n = c.getChildNode("DynamicTracks", 0);
  1287. m_sCapabilities.Extension->Recording->DynamicTracks = checkBool(n.getText(0));
  1288. }
  1289. {
  1290. XMLNode n = c.getChildNode("MaxStringLength", 0);
  1291. m_sCapabilities.Extension->Recording->MaxStringLength = atoi(n.getText(0));
  1292. }
  1293. };
  1294. };
  1295. }
  1296. {
  1297. XMLNode c = t.getChildNode("Search", 0);
  1298. if (! c.isEmpty())
  1299. {
  1300. m_sCapabilities.Extension->Search = (onvifSearchCapabilities *) malloc(sizeof(onvifSearchCapabilities));
  1301. if (m_sCapabilities.Extension->Search != NULL)
  1302. {
  1303. memset(m_sCapabilities.Extension->Search, 0, sizeof(onvifSearchCapabilities));
  1304. {
  1305. XMLNode n = c.getChildNode("XAddr", 0);
  1306. sprintf(m_sCapabilities.Extension->Search->XAddr, "%s", n.getText(0));
  1307. }
  1308. {
  1309. XMLNode n = c.getChildNode("MetadataSearch", 0);
  1310. m_sCapabilities.Extension->Search->MetadataSearch = checkBool(n.getText(0));
  1311. }
  1312. };
  1313. };
  1314. }
  1315. {
  1316. XMLNode c = t.getChildNode("Replay", 0);
  1317. if (! c.isEmpty())
  1318. {
  1319. m_sCapabilities.Extension->Replay = (onvifReplayCapabilities *) malloc(sizeof(onvifReplayCapabilities));
  1320. if (m_sCapabilities.Extension->Replay != NULL)
  1321. {
  1322. memset(m_sCapabilities.Extension->Replay, 0, sizeof(onvifReplayCapabilities));
  1323. {
  1324. XMLNode n = c.getChildNode("XAddr", 0);
  1325. sprintf(m_sCapabilities.Extension->Replay->XAddr, "%s", n.getText(0));
  1326. }
  1327. };
  1328. };
  1329. }
  1330. {
  1331. XMLNode c = t.getChildNode("Receiver", 0);
  1332. if (! c.isEmpty())
  1333. {
  1334. m_sCapabilities.Extension->Receiver = (onvifReceiverCapabilities *) malloc(sizeof(onvifReceiverCapabilities));
  1335. if (m_sCapabilities.Extension->Receiver != NULL)
  1336. {
  1337. memset(m_sCapabilities.Extension->Receiver, 0, sizeof(onvifReceiverCapabilities));
  1338. {
  1339. XMLNode n = c.getChildNode("XAddr", 0);
  1340. sprintf(m_sCapabilities.Extension->Receiver->XAddr, "%s", n.getText(0));
  1341. }
  1342. {
  1343. XMLNode n = c.getChildNode("RTP_Multicast", 0);
  1344. m_sCapabilities.Extension->Receiver->RTP_Multicast = checkBool(n.getText(0));
  1345. }
  1346. {
  1347. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1348. m_sCapabilities.Extension->Receiver->RTP_TCP = checkBool(n.getText(0));
  1349. }
  1350. {
  1351. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1352. m_sCapabilities.Extension->Receiver->RTP_RTSP_TCP = checkBool(n.getText(0));
  1353. }
  1354. {
  1355. XMLNode n = c.getChildNode("SupportedReceivers", 0);
  1356. m_sCapabilities.Extension->Receiver->SupportedReceivers = atoi(n.getText(0));
  1357. }
  1358. {
  1359. XMLNode n = c.getChildNode("MaximumRTSPURILength", 0);
  1360. m_sCapabilities.Extension->Receiver->MaximumRTSPURILength = atoi(n.getText(0));
  1361. }
  1362. };
  1363. };
  1364. }
  1365. {
  1366. XMLNode c = t.getChildNode("AnalyticsDevice", 0);
  1367. if (! c.isEmpty())
  1368. {
  1369. m_sCapabilities.Extension->AnalyticsDevice = (onvifAnalyticsDeviceCapabilities *) malloc(sizeof(onvifAnalyticsDeviceCapabilities));
  1370. if (m_sCapabilities.Extension->AnalyticsDevice != NULL)
  1371. {
  1372. memset(m_sCapabilities.Extension->AnalyticsDevice, 0, sizeof(onvifAnalyticsDeviceCapabilities));
  1373. {
  1374. XMLNode n = c.getChildNode("XAddr", 0);
  1375. sprintf(m_sCapabilities.Extension->AnalyticsDevice->XAddr, "%s", n.getText(0));
  1376. }
  1377. {
  1378. XMLNode n = c.getChildNode("RuleSupport", 0);
  1379. m_sCapabilities.Extension->AnalyticsDevice->RuleSupport = checkBool(n.getText(0));
  1380. }
  1381. };
  1382. };
  1383. }
  1384. };
  1385. };
  1386. }
  1387. return true;
  1388. }
  1389. else
  1390. {
  1391. LOG("Capabilities is Empty!");
  1392. };
  1393. }
  1394. else
  1395. {
  1396. LOG("GetCapabilitiesResponse is Empty!");
  1397. };
  1398. };
  1399. }
  1400. return false;
  1401. }
  1402. // based on https://www.onvif.org/ver20/imaging/wsdl/imaging.wsdl
  1403. bool pOnvif::media_GetVideoSources(void)
  1404. {
  1405. if (m_pMediaURI == NULL)
  1406. {
  1407. return false;
  1408. }
  1409. char tBuffer[DEFAULT_BUFFER_SIZE];
  1410. accessToken tTOKEN;
  1411. tTOKEN = _makeToken();
  1412. sprintf(tBuffer, PONVIF_PACK_GET_VIDEO_SOURCES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1413. if (_send_request(m_pMediaURI, tBuffer))
  1414. {
  1415. XMLResults tParseResults;
  1416. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1417. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1418. LOG("RCPT:");
  1419. printf(m_pLastResponse);
  1420. #endif
  1421. if (tParseResults.error)
  1422. {
  1423. char tBuf[CTEXT];
  1424. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1425. LOG_ERROR(tBuf);
  1426. }
  1427. else
  1428. {
  1429. XMLNode zNode = response.getChildNode("Body", 0);
  1430. if (zNode.isEmpty())
  1431. {
  1432. return false;
  1433. }
  1434. XMLNode tNode = zNode.getChildNode("GetVideoSourcesResponse", 0);
  1435. if (! tNode.isEmpty())
  1436. {
  1437. for (int index = 0; index < tNode.nChildNode(); index++)
  1438. {
  1439. XMLNode cNode = tNode.getChildNode("VideoSources", index);
  1440. if (! cNode.isEmpty())
  1441. {
  1442. videoSource * tPtr;
  1443. tPtr = (videoSource *) malloc(sizeof(videoSource));
  1444. if (tPtr != NULL)
  1445. {
  1446. sprintf(tPtr->token, "%s", cNode.getAttribute("token", 0));
  1447. {
  1448. XMLNode nNode = cNode.getChildNode("Framerate", 0);
  1449. tPtr->Framerate = (float) atof(nNode.getText(0));
  1450. }
  1451. {
  1452. XMLNode nNode = cNode.getChildNode("Resolution", 0);
  1453. if (! nNode.isEmpty())
  1454. {
  1455. {
  1456. XMLNode xNode = nNode.getChildNode("Width", 0);
  1457. tPtr->Resolution.Width = atoi(xNode.getText(0));
  1458. }
  1459. {
  1460. XMLNode xNode = nNode.getChildNode("Height", 0);
  1461. tPtr->Resolution.Height = atoi(xNode.getText(0));
  1462. }
  1463. };
  1464. }
  1465. {
  1466. XMLNode nNode = cNode.getChildNode("Imaging", 0);
  1467. if (! nNode.isEmpty())
  1468. {
  1469. tPtr->Imaging = (onvifImagingSettings *) malloc(sizeof(onvifImagingSettings));
  1470. if (tPtr->Imaging != NULL)
  1471. {
  1472. memset(tPtr->Imaging, 0, sizeof(onvifImagingSettings));
  1473. {
  1474. XMLNode xNode = nNode.getChildNode("BacklightCompensation", 0);
  1475. if (! xNode.isEmpty())
  1476. {
  1477. tPtr->Imaging->BacklightCompensation = (onvifBacklightCompensation *) malloc(sizeof(onvifBacklightCompensation));
  1478. if (tPtr->Imaging->BacklightCompensation != NULL)
  1479. {
  1480. memset(tPtr->Imaging->BacklightCompensation, 0, sizeof(onvifBacklightCompensation));
  1481. {
  1482. XMLNode rNode = xNode.getChildNode("Mode", 0);
  1483. tPtr->Imaging->BacklightCompensation->Mode = checkOnOff(rNode.getText(0));
  1484. }
  1485. {
  1486. XMLNode rNode = xNode.getChildNode("Level", 0);
  1487. tPtr->Imaging->BacklightCompensation->Level = (float) atof(rNode.getText(0));
  1488. }
  1489. };
  1490. };
  1491. }
  1492. {
  1493. XMLNode xNode = nNode.getChildNode("Brightness", 0);
  1494. tPtr->Imaging->Brightness = (float) atoi(xNode.getText(0));
  1495. }
  1496. {
  1497. XMLNode xNode = nNode.getChildNode("ColorSaturation", 0);
  1498. tPtr->Imaging->ColorSaturation = (float) atoi(xNode.getText(0));
  1499. }
  1500. {
  1501. XMLNode xNode = nNode.getChildNode("Contrast", 0);
  1502. tPtr->Imaging->Contrast = (float) atoi(xNode.getText(0));
  1503. }
  1504. {
  1505. XMLNode xNode = nNode.getChildNode("Sharpness", 0);
  1506. tPtr->Imaging->Sharpness = (float) atoi(xNode.getText(0));
  1507. }
  1508. {
  1509. XMLNode xNode = nNode.getChildNode("IrCutFilter", 0);
  1510. tPtr->Imaging->IrCutFilter = checkOnOffAuto(xNode.getText(0));
  1511. }
  1512. };
  1513. };
  1514. }
  1515. m_vVideoSources.push_back(tPtr);
  1516. };
  1517. };
  1518. };
  1519. return true;
  1520. }
  1521. else
  1522. {
  1523. LOG_ERROR("GetVideoSourcesResponse EMPTY!");
  1524. };
  1525. };
  1526. }
  1527. else
  1528. {
  1529. LOG_ERROR("_send_request Failure!");
  1530. };
  1531. return false;
  1532. }
  1533. bool pOnvif::media_GetProfiles(void)
  1534. {
  1535. char tBuffer[DEFAULT_BUFFER_SIZE];
  1536. accessToken tTOKEN;
  1537. tTOKEN = _makeToken();
  1538. sprintf(tBuffer, PONVIF_PACK_GET_PROFILES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1539. if (_send_request(m_pMediaURI, tBuffer))
  1540. {
  1541. XMLResults tParseResults;
  1542. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1543. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1544. LOG("RCPT:");
  1545. printf(m_pLastResponse);
  1546. #endif
  1547. if (tParseResults.error)
  1548. {
  1549. char tBuf[CTEXT];
  1550. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1551. LOG_ERROR(tBuf);
  1552. }
  1553. else
  1554. {
  1555. XMLNode zNode = response.getChildNode("Body", 0);
  1556. if (zNode.isEmpty())
  1557. {
  1558. return false;
  1559. }
  1560. XMLNode tNode = zNode.getChildNode("GetProfilesResponse", 0);
  1561. if (! tNode.isEmpty())
  1562. {
  1563. for (int index = 0; index < tNode.nChildNode(); index++)
  1564. {
  1565. XMLNode cNode = tNode.getChildNode("Profiles", index);
  1566. if (! cNode.isEmpty())
  1567. {
  1568. videoSourceProfile * tPtr;
  1569. tPtr = (videoSourceProfile *) malloc(sizeof(videoSourceProfile));
  1570. if (tPtr != NULL)
  1571. {
  1572. memset(tPtr, 0, sizeof(videoSourceProfile));
  1573. sprintf(tPtr->profiletoken, "%s", cNode.getAttribute("token", 0));
  1574. {
  1575. XMLNode nNode = cNode.getChildNode("Name", 0);
  1576. sprintf(tPtr->profilename, "%s", nNode.getText(0));
  1577. }
  1578. {
  1579. XMLNode nNode = cNode.getChildNode("VideoSourceConfiguration", 0);
  1580. if (! nNode.isEmpty())
  1581. {
  1582. XMLNode zNode = nNode.getChildNode("SourceToken", 0);
  1583. sprintf(tPtr->sourcetoken, "%s", zNode.getText(0));
  1584. };
  1585. }
  1586. {
  1587. XMLNode nNode = cNode.getChildNode("VideoEncoderConfiguration", 0);
  1588. if (! nNode.isEmpty())
  1589. {
  1590. {
  1591. XMLNode zNode = nNode.getChildNode("Encoding", 0);
  1592. sprintf(tPtr->encoding, "%s", zNode.getText(0));
  1593. }
  1594. {
  1595. XMLNode zNode = nNode.getChildNode("Resolution", 0);
  1596. {
  1597. XMLNode jNode = zNode.getChildNode("Width", 0);
  1598. tPtr->width = atoi(jNode.getText(0));
  1599. }
  1600. {
  1601. XMLNode jNode = zNode.getChildNode("Height", 0);
  1602. tPtr->height = atoi(jNode.getText(0));
  1603. }
  1604. }
  1605. {
  1606. XMLNode zNode = nNode.getChildNode("RateControl", 0);
  1607. {
  1608. XMLNode jNode = zNode.getChildNode("FrameRateLimit", 0);
  1609. tPtr->fps = atoi(jNode.getText(0));
  1610. }
  1611. {
  1612. XMLNode jNode = zNode.getChildNode("BitrateLimit", 0);
  1613. tPtr->bitrate = atoi(jNode.getText(0));
  1614. }
  1615. }
  1616. };
  1617. }
  1618. m_vVideoProfiles.push_back(tPtr);
  1619. };
  1620. };
  1621. };
  1622. return true;
  1623. };
  1624. };
  1625. };
  1626. return false;
  1627. }
  1628. bool pOnvif::media_GetServices(void)
  1629. {
  1630. char tBuffer[DEFAULT_BUFFER_SIZE];
  1631. accessToken tTOKEN;
  1632. tTOKEN = _makeToken();
  1633. sprintf(tBuffer, PONVIF_PACK_GET_SERVICES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1634. if (_send_request(m_pMediaURI, tBuffer))
  1635. {
  1636. XMLResults tParseResults;
  1637. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1638. LOG("RCPT:");
  1639. printf(m_pLastResponse);
  1640. if (tParseResults.error)
  1641. {
  1642. char tBuf[CTEXT];
  1643. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1644. LOG_ERROR(tBuf);
  1645. }
  1646. else
  1647. {
  1648. };
  1649. };
  1650. return false;
  1651. }
  1652. bool pOnvif::core_GetDeviceInformation()
  1653. {
  1654. char tBuffer[DEFAULT_BUFFER_SIZE];
  1655. accessToken tTOKEN;
  1656. tTOKEN = _makeToken();
  1657. sprintf(tBuffer, PONVIF_PACK_GET_DEV_INFO, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP);
  1658. if (_send_request(m_pMediaURI, tBuffer))
  1659. {
  1660. XMLResults tParseResults;
  1661. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1662. LOG("RCPT:");
  1663. printf(m_pLastResponse);
  1664. if (tParseResults.error)
  1665. {
  1666. char tBuf[CTEXT];
  1667. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1668. LOG_ERROR(tBuf);
  1669. }
  1670. else
  1671. {
  1672. // return $response['Envelope']['Body']['GetDeviceInformationResponse'];
  1673. };
  1674. };
  1675. return false;
  1676. }
  1677. CSTR pOnvif::media_GetStreamUri(CSTR profileToken, CSTR stream, CSTR protocol)
  1678. {
  1679. char tBuffer[DEFAULT_BUFFER_SIZE];
  1680. accessToken tTOKEN;
  1681. tTOKEN = _makeToken();
  1682. sprintf(tBuffer, PONVIF_PACK_GET_STREAM_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, stream, protocol, profileToken);
  1683. if (_send_request(m_pMediaURI, tBuffer))
  1684. {
  1685. XMLResults tParseResults;
  1686. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1687. LOG("RCPT:");
  1688. printf(m_pLastResponse);
  1689. if (tParseResults.error)
  1690. {
  1691. char tBuf[CTEXT];
  1692. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1693. LOG_ERROR(tBuf);
  1694. }
  1695. else
  1696. {
  1697. // Parse
  1698. // return $response['Envelope']['Body']['GetStreamUriResponse']['MediaUri']['Uri'];
  1699. };
  1700. };
  1701. return NULL;
  1702. }
  1703. CSTR pOnvif::media_GetSnapshotUri(CSTR profileToken)
  1704. {
  1705. char tBuffer[DEFAULT_BUFFER_SIZE];
  1706. accessToken tTOKEN;
  1707. tTOKEN = _makeToken();
  1708. sprintf(tBuffer, PONVIF_PACK_GET_PICTURE_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1709. if (_send_request(m_pMediaURI, tBuffer))
  1710. {
  1711. XMLResults tParseResults;
  1712. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1713. LOG("RCPT:");
  1714. printf(m_pLastResponse);
  1715. if (tParseResults.error)
  1716. {
  1717. char tBuf[CTEXT];
  1718. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1719. LOG_ERROR(tBuf);
  1720. }
  1721. else
  1722. {
  1723. // Parse
  1724. // return $response['Envelope']['Body']['GetSnapshotUriResponse']['MediaUri']['Uri'];
  1725. };
  1726. };
  1727. return NULL;
  1728. }
  1729. bool pOnvif::ptz_GetStatus(CSTR profileToken, ptzStatus * tStatus)
  1730. {
  1731. if (m_pPTZURI == NULL)
  1732. {
  1733. return false;
  1734. }
  1735. if (tStatus == NULL)
  1736. {
  1737. return false;
  1738. }
  1739. char tBuffer[DEFAULT_BUFFER_SIZE];
  1740. accessToken tTOKEN;
  1741. tTOKEN = _makeToken();
  1742. memset(tStatus, 0, sizeof(ptzStatus));
  1743. sprintf(tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1744. if (_send_request(m_pPTZURI, tBuffer, true))
  1745. {
  1746. XMLResults tParseResults;
  1747. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1748. if (tParseResults.error)
  1749. {
  1750. char tBuf[CTEXT];
  1751. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1752. LOG_ERROR(tBuf);
  1753. }
  1754. else
  1755. {
  1756. XMLNode aNode = response.getChildNode("Body", 0);
  1757. if (aNode.isEmpty())
  1758. {
  1759. return NULL;
  1760. }
  1761. XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0);
  1762. if (! bNode.isEmpty())
  1763. {
  1764. XMLNode cNode = bNode.getChildNode("PTZStatus", 0);
  1765. if (! cNode.isEmpty())
  1766. {
  1767. XMLNode dNode = cNode.getChildNode("Position", 0);
  1768. if (! dNode.isEmpty())
  1769. {
  1770. XMLNode eNode = dNode.getChildNode("PanTilt", 0);
  1771. if (! eNode.isEmpty())
  1772. {
  1773. // XMLAttribute ZZ = eNode.getAttribute(1);
  1774. // LOG( ZZ.lpszName );
  1775. tStatus->pan = atof(eNode.getAttribute("x")); // ZZ.lpszValue );
  1776. // ZZ = eNode.getAttribute(2);
  1777. // LOG( ZZ.lpszName );
  1778. tStatus->tilt = atof(eNode.getAttribute("y")); // ZZ.lpszValue );
  1779. };
  1780. XMLNode fNode = dNode.getChildNode("Zoom", 0);
  1781. if (! fNode.isEmpty())
  1782. {
  1783. // XMLAttribute ZZ = eNode.getAttribute(1);
  1784. // LOG( ZZ.lpszName );
  1785. tStatus->zoom = atof(fNode.getAttribute("x")); // ZZ.lpszValue );
  1786. };
  1787. };
  1788. if (tStatus->pan != .0f || tStatus->tilt != .0f || tStatus->zoom != .0f)
  1789. {
  1790. XMLNode gNode = cNode.getChildNode("MoveStatus", 0);
  1791. if (! gNode.isEmpty())
  1792. {
  1793. tStatus->hasMoveStatus = true;
  1794. XMLNode hNode = gNode.getChildNode("PanTilt", 0);
  1795. if (! hNode.isEmpty())
  1796. {
  1797. if (strstr(hNode.getText(0), "IDLE") != NULL)
  1798. {
  1799. tStatus->PanTiltIdle = true;
  1800. }
  1801. };
  1802. XMLNode iNode = gNode.getChildNode("Zoom", 0);
  1803. if (! iNode.isEmpty())
  1804. {
  1805. if (strstr(iNode.getText(0), "IDLE") != NULL)
  1806. {
  1807. tStatus->ZoomIdle = true;
  1808. }
  1809. };
  1810. };
  1811. }
  1812. return true;
  1813. };
  1814. };
  1815. };
  1816. };
  1817. return false;
  1818. }
  1819. bool pOnvif::ptz_GetPresets(CSTR profileToken)
  1820. {
  1821. if (m_pPTZURI == NULL)
  1822. {
  1823. return false;
  1824. }
  1825. char tBuffer[DEFAULT_BUFFER_SIZE];
  1826. accessToken tTOKEN;
  1827. tTOKEN = _makeToken();
  1828. sprintf(tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  1829. if (_send_request(m_pPTZURI, tBuffer))
  1830. {
  1831. // XMLResults tParseResults;
  1832. // XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1833. // LOG("RCPT:");
  1834. // LOG( m_pLastResponse );
  1835. // if( tParseResults.error ){
  1836. // char tBuf[CTEXT];
  1837. // sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1838. // LOG_ERROR( tBuf );
  1839. // } else {
  1840. //$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
  1841. //$presets=array();
  1842. // foreach ($getpresetsresponse as $preset) {
  1843. // $presets[]=array('Token'=>$preset['@attributes']['token'],
  1844. // 'Name'=>$preset['Name'],
  1845. // 'PTZPosition'=>$preset['PTZPosition']);
  1846. // }
  1847. // return $presets;
  1848. return true;
  1849. //};
  1850. };
  1851. return false;
  1852. }
  1853. bool pOnvif::ptz_GetNode(CSTR ptzNodeToken)
  1854. {
  1855. if (m_pPTZURI == NULL)
  1856. {
  1857. return false;
  1858. }
  1859. char tBuffer[DEFAULT_BUFFER_SIZE];
  1860. accessToken tTOKEN;
  1861. tTOKEN = _makeToken();
  1862. sprintf(tBuffer, PONVIF_PACK_GET_PTZ_NODE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, ptzNodeToken);
  1863. if (_send_request(m_pPTZURI, tBuffer))
  1864. {
  1865. XMLResults tParseResults;
  1866. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  1867. LOG("RCPT:");
  1868. printf(m_pLastResponse);
  1869. if (tParseResults.error)
  1870. {
  1871. char tBuf[CTEXT];
  1872. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  1873. LOG_ERROR(tBuf);
  1874. }
  1875. else
  1876. {
  1877. // return $response['Envelope']['Body']['GetNodeResponse'];
  1878. };
  1879. };
  1880. return false;
  1881. }
  1882. bool pOnvif::ptz_GotoPreset(CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x)
  1883. {
  1884. if (m_pPTZURI == NULL)
  1885. {
  1886. return false;
  1887. }
  1888. char tBuffer[DEFAULT_BUFFER_SIZE];
  1889. accessToken tTOKEN;
  1890. tTOKEN = _makeToken();
  1891. 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);
  1892. return _send_request(m_pPTZURI, tBuffer, true);
  1893. }
  1894. bool pOnvif::ptz_RemovePreset(CSTR profileToken, CSTR presetToken)
  1895. {
  1896. if (m_pPTZURI == NULL)
  1897. {
  1898. return false;
  1899. }
  1900. char tBuffer[DEFAULT_BUFFER_SIZE];
  1901. accessToken tTOKEN;
  1902. tTOKEN = _makeToken();
  1903. sprintf(tBuffer, PONVIF_PACK_REMOVE_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken);
  1904. return _send_request(m_pPTZURI, tBuffer, true);
  1905. }
  1906. bool pOnvif::ptz_SetPreset(CSTR profileToken, CSTR presetName)
  1907. {
  1908. if (m_pPTZURI == NULL)
  1909. {
  1910. return false;
  1911. }
  1912. char tBuffer[DEFAULT_BUFFER_SIZE];
  1913. accessToken tTOKEN;
  1914. tTOKEN = _makeToken();
  1915. sprintf(tBuffer, PONVIF_PACK_SET_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetName);
  1916. return _send_request(m_pPTZURI, tBuffer, true);
  1917. }
  1918. bool pOnvif::ptz_RelativeMove(CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y)
  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_RELATIVE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, translation_pantilt_x, translation_pantilt_y, speed_pantilt_x, speed_pantilt_y);
  1928. return _send_request(m_pPTZURI, tBuffer, true);
  1929. }
  1930. bool pOnvif::ptz_RelativeMoveZoom(CSTR profileToken, float zoom, float speedZoom)
  1931. {
  1932. if (m_pPTZURI == NULL)
  1933. {
  1934. return false;
  1935. }
  1936. char tBuffer[DEFAULT_BUFFER_SIZE];
  1937. accessToken tTOKEN;
  1938. tTOKEN = _makeToken();
  1939. sprintf(tBuffer, PONVIF_PACK_RELATIVE_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom, speedZoom);
  1940. return _send_request(m_pPTZURI, tBuffer, true);
  1941. }
  1942. bool pOnvif::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom)
  1943. {
  1944. if (m_pPTZURI == NULL)
  1945. {
  1946. return false;
  1947. }
  1948. char tBuffer[DEFAULT_BUFFER_SIZE];
  1949. accessToken tTOKEN;
  1950. tTOKEN = _makeToken();
  1951. sprintf(tBuffer, PONVIF_PACK_ABSOLUTE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, position_pantilt_x, position_pantilt_y, zoom);
  1952. return _send_request(m_pPTZURI, tBuffer, false);
  1953. }
  1954. bool pOnvif::ptz_ContinuousMove(CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y)
  1955. {
  1956. if (m_pPTZURI == NULL)
  1957. {
  1958. return false;
  1959. }
  1960. char tBuffer[DEFAULT_BUFFER_SIZE];
  1961. accessToken tTOKEN;
  1962. tTOKEN = _makeToken();
  1963. sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, velocity_pantilt_x, velocity_pantilt_y);
  1964. return _send_request(m_pPTZURI, tBuffer, true);
  1965. }
  1966. bool pOnvif::ptz_ContinuousMoveZoom(CSTR profileToken, float zoom)
  1967. {
  1968. if (m_pPTZURI == NULL)
  1969. {
  1970. return false;
  1971. }
  1972. char tBuffer[DEFAULT_BUFFER_SIZE];
  1973. accessToken tTOKEN;
  1974. tTOKEN = _makeToken();
  1975. sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom);
  1976. return _send_request(m_pPTZURI, tBuffer, true);
  1977. }
  1978. bool pOnvif::ptz_Stop(CSTR profileToken, bool pantilt, bool zoom)
  1979. {
  1980. if ( pantilt || zoom ) profileToken = profileToken;
  1981. if (m_pPTZURI == NULL)
  1982. {
  1983. return false;
  1984. }
  1985. char tBuffer[DEFAULT_BUFFER_SIZE];
  1986. accessToken tTOKEN;
  1987. tTOKEN = _makeToken();
  1988. sprintf(tBuffer, PONVIF_PACK_PTZ_STOP, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken); //, pantilt, zoom );
  1989. return _send_request(m_pPTZURI, tBuffer, true);
  1990. }
  1991. /*
  1992. Internal functions
  1993. */
  1994. accessToken pOnvif::_makeToken(void)
  1995. {
  1996. time_t tTS = time(NULL) - m_nDeltaTime;
  1997. return _passwordDigest(m_pUsername, m_pPassword, tTS);
  1998. }
  1999. void pOnvif::_getOnvifVersion(nodeCapability & caps)
  2000. {
  2001. if (caps.Device != NULL)
  2002. {
  2003. if (caps.Device->System != NULL)
  2004. {
  2005. if (caps.Device->System->SupportedVersions != NULL)
  2006. {
  2007. m_sOnvifVersion.Major = caps.Device->System->SupportedVersions->Major;
  2008. m_sOnvifVersion.Minor = caps.Device->System->SupportedVersions->Minor;
  2009. };
  2010. };
  2011. };
  2012. if (caps.PTZ != NULL)
  2013. {
  2014. m_pPTZURI = caps.PTZ->XAddr;
  2015. };
  2016. if (caps.Media != NULL)
  2017. {
  2018. m_pMediaURI = caps.Media->XAddr;
  2019. };
  2020. if (caps.Device != NULL)
  2021. {
  2022. m_pDeviceURI = caps.Device->XAddr;
  2023. };
  2024. }
  2025. /*
  2026. protected function _getActiveSources($videoSources,$profiles) {
  2027. $sources=array();
  2028. if (isset($videoSources['@attributes'])) {
  2029. // NVT is a camera
  2030. $sources[0]['sourcetoken']=$videoSources['@attributes']['token'];
  2031. $this->_getProfileData($sources,0,$profiles);
  2032. }
  2033. else {
  2034. // NVT is an encoder
  2035. for ($i=0;$i<count($videoSources);$i++) {
  2036. if (strtolower($videoSources[$i]['@attributes']['SignalActive'])=='true') {
  2037. $sources[$i]['sourcetoken']=$videoSources[$i]['@attributes']['token'];
  2038. $this->_getProfileData($sources,$i,$profiles);
  2039. }
  2040. } // for
  2041. }
  2042. return $sources;
  2043. }
  2044. void pOnvif::_getProfileData( sources,$i, XMLNode profiles ){
  2045. //$inprofile=0;
  2046. videoSourceProfile *tPtr;
  2047. for( $y=0; $y < count($profiles); $y++ ){
  2048. if ($profiles[$y]['VideoSourceConfiguration']['SourceToken']==$sources[$i]['sourcetoken']) {
  2049. $sources[$i][$inprofile]['profilename']=$profiles[$y]['Name'];
  2050. $sources[$i][$inprofile]['profiletoken']=$profiles[$y]['@attributes']['token'];
  2051. if ( isset($profiles[$y]['VideoEncoderConfiguration'])) {
  2052. $sources[$i][$inprofile]['encoding']=$profiles[$y]['VideoEncoderConfiguration']['Encoding'];
  2053. $sources[$i][$inprofile]['width']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Width'];
  2054. $sources[$i][$inprofile]['height']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Height'];
  2055. $sources[$i][$inprofile]['fps']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['FrameRateLimit'];
  2056. $sources[$i][$inprofile]['bitrate']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['BitrateLimit'];
  2057. }
  2058. if ( isset($profiles[$y]['PTZConfiguration'])) {
  2059. $sources[$i][$inprofile]['ptz']['name']=$profiles[$y]['PTZConfiguration']['Name'];
  2060. $sources[$i][$inprofile]['ptz']['nodetoken']=$profiles[$y]['PTZConfiguration']['NodeToken'];
  2061. }
  2062. $inprofile++;
  2063. }
  2064. }
  2065. };
  2066. */
  2067. /*
  2068. protected function _xml2array($response) {
  2069. $sxe = new SimpleXMLElement($response);
  2070. $dom_sxe = dom_import_simplexml($sxe);
  2071. $dom = new DOMDocument('1.0');
  2072. $dom_sxe = $dom->importNode($dom_sxe, true);
  2073. $dom_sxe = $dom->appendChild($dom_sxe);
  2074. $element = $dom->childNodes->item(0);
  2075. foreach ($sxe->getDocNamespaces() as $name => $uri) {
  2076. $element->removeAttributeNS($uri, $name);
  2077. }
  2078. $xmldata=$dom->saveXML();
  2079. $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>"));
  2080. $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>"));
  2081. $xml=simplexml_load_string($xmldata);
  2082. $data=json_decode(json_encode((array)$xml),1);
  2083. $data=array($xml->getName()=>$data);
  2084. return $data;
  2085. }
  2086. */
  2087. accessToken pOnvif::_passwordDigest(CSTR username, CSTR password, time_t timestamp, uint32_t nonce)
  2088. {
  2089. accessToken RetVal;
  2090. char tTS[CTEXT];
  2091. unsigned char * tPDIGEST;
  2092. if (timestamp == 0)
  2093. {
  2094. time_t t;
  2095. struct tm * tmp;
  2096. t = time(NULL);
  2097. tmp = gmtime(&t);
  2098. if (tmp == NULL)
  2099. {
  2100. LOG_ERROR("gmtime Error!");
  2101. };
  2102. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  2103. {
  2104. LOG_ERROR("strftime returned 0!");
  2105. };
  2106. }
  2107. else
  2108. {
  2109. struct tm * tmp;
  2110. tmp = gmtime(&timestamp);
  2111. if (tmp == NULL)
  2112. {
  2113. LOG_ERROR("gmtime Error!");
  2114. };
  2115. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  2116. {
  2117. LOG_ERROR("strftime returned 0!");
  2118. };
  2119. };
  2120. if (nonce == 0)
  2121. {
  2122. nonce = (uint32_t) rand();
  2123. };
  2124. unsigned char tBuff[CTEXT];
  2125. int cp = 0;
  2126. int index = 0;
  2127. for (; index < 4; index++)
  2128. {
  2129. tBuff[cp++] = ((unsigned char *) (&nonce))[index];
  2130. };
  2131. index = 0;
  2132. while (tTS[index] != '\0')
  2133. {
  2134. tBuff[cp++] = (unsigned char) tTS[index];
  2135. index++;
  2136. };
  2137. index = 0;
  2138. while (password[index] != '\0')
  2139. {
  2140. tBuff[cp++] = (unsigned char) password[index];
  2141. index++;
  2142. };
  2143. size_t baseOutLen;
  2144. unsigned char tSHA[SHA_DIGEST_LENGTH];
  2145. SHA1(tBuff, cp, tSHA);
  2146. // tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
  2147. tPDIGEST = base64_encode(tSHA, SHA_DIGEST_LENGTH, &baseOutLen);
  2148. // free( tSHA );
  2149. //$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
  2150. strncpy(RetVal.USERNAME, username, CTEXT);
  2151. strncpy(RetVal.TIMESTAMP, tTS, CTEXT);
  2152. strncpy(RetVal.PASSDIGEST, (const char *) tPDIGEST, CTEXT);
  2153. free(tPDIGEST);
  2154. tPDIGEST = base64_encode((unsigned char *) &nonce, 4, &baseOutLen);
  2155. strncpy(RetVal.NONCE, (const char *) tPDIGEST, baseOutLen);
  2156. RetVal.NONCE[baseOutLen] = '\0';
  2157. free(tPDIGEST);
  2158. //$REQ['NONCE']=base64_encode($nonce); // alternative
  2159. return RetVal;
  2160. }
  2161. void * pOnvif::parser(void * obj, void * ptr)
  2162. {
  2163. pOnvif * ME = (pOnvif *) obj;
  2164. curlRequest * RQ = (curlRequest *) ptr;
  2165. if (ME)
  2166. {
  2167. if (ME->m_pLastResponse)
  2168. {
  2169. free(ME->m_pLastResponse);
  2170. }
  2171. ME->m_pLastResponse = (CSTR) malloc(RQ->getSize() + 1);
  2172. if (ME->m_pLastResponse)
  2173. {
  2174. memcpy(ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize());
  2175. ME->m_pLastResponse[RQ->getSize()] = (char) 0;
  2176. #ifdef DEBUG_ONVIF_TRAFFIC
  2177. FILE * tf;
  2178. tf = fopen("Traffic.txt", "a+");
  2179. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  2180. fprintf(tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse);
  2181. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  2182. fclose(tf);
  2183. #endif
  2184. };
  2185. ME->parser_done = true;
  2186. };
  2187. return NULL;
  2188. }
  2189. bool pOnvif::_send_request(CSTR url, CSTR post_string, bool mustReturnData)
  2190. {
  2191. #ifdef CURL_MANAGER
  2192. bool RetVal = false;
  2193. if (m_pHttpAccessManager)
  2194. {
  2195. m_mResponseProtector.lock();
  2196. curlRequest * tRC; // application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime"
  2197. tRC = new curlRequest(url, (CSTR) "application/soap+xml; charset=utf-8;");
  2198. if (tRC != NULL)
  2199. {
  2200. #ifdef DEBUG_ONVIF_TRAFFIC
  2201. FILE * tf;
  2202. tf = fopen("Traffic.txt", "a+");
  2203. fprintf(tf, "--------------------\r\n");
  2204. fprintf(tf, "REQ: %s (%d)\n", url, strlen(post_string));
  2205. fprintf(tf, ">>>>> %s\n", post_string);
  2206. fprintf(tf, "--------------------\r\n");
  2207. fclose(tf);
  2208. mustReturnData = true; // In Debug Allways Read Return
  2209. #endif
  2210. tRC->setCredentials(m_pUsername, m_pPassword, m_bAuthMode);
  2211. tRC->setPOST(post_string, strlen(post_string));
  2212. if (mustReturnData)
  2213. {
  2214. tRC->setHandler((void *) this, pOnvif::parser, (void *) tRC);
  2215. if (m_pHttpAccessManager->accomplish(tRC))
  2216. {
  2217. parser_done = false;
  2218. uint8_t cnt = 200; // TO = 2 second
  2219. while (! RetVal && cnt--)
  2220. {
  2221. RetVal = parser_done;
  2222. SLEEP(10);
  2223. };
  2224. };
  2225. }
  2226. else
  2227. {
  2228. RetVal = m_pHttpAccessManager->accomplish(tRC);
  2229. };
  2230. }
  2231. else
  2232. {
  2233. LOG_ERROR("Cannot Create curlRequest!");
  2234. }
  2235. m_mResponseProtector.unlock();
  2236. };
  2237. return RetVal;
  2238. #else
  2239. char tBuffer[CTEXT];
  2240. if (m_pCURL == NULL)
  2241. {
  2242. if (m_pLastResponse == NULL)
  2243. {
  2244. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  2245. };
  2246. m_pCURL = curl_easy_init();
  2247. if (m_pProxyHost != NULL && m_nProxyPort != 0)
  2248. {
  2249. curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
  2250. curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
  2251. if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
  2252. {
  2253. sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
  2254. curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
  2255. };
  2256. };
  2257. curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  2258. curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
  2259. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
  2260. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
  2261. curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
  2262. curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
  2263. // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  2264. // curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  2265. // curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  2266. curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
  2267. curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
  2268. // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  2269. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
  2270. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  2271. };
  2272. if (m_pCURL != NULL)
  2273. {
  2274. curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
  2275. struct curl_slist * sList = NULL;
  2276. // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  2277. // Connection: Close
  2278. sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
  2279. sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
  2280. sList = curl_slist_append(sList, tBuffer);
  2281. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
  2282. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
  2283. sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
  2284. curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
  2285. switch (m_bAuthMode)
  2286. {
  2287. case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
  2288. case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
  2289. case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
  2290. case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
  2291. case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
  2292. case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
  2293. case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
  2294. case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
  2295. case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
  2296. case 9:
  2297. curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
  2298. break;
  2299. // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  2300. };
  2301. #ifdef DEBUG
  2302. char tErrorBuffer[CURL_ERROR_SIZE];
  2303. curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
  2304. #endif
  2305. m_pLastIndex = 0;
  2306. if (curl_easy_perform(m_pCURL))
  2307. {
  2308. LOG_ERROR("CURL Failure!");
  2309. #ifdef DEBUG
  2310. LOG_ERROR(tErrorBuffer);
  2311. #endif
  2312. }
  2313. else
  2314. {
  2315. m_pLastResponse[m_pLastIndex] = (char) 0;
  2316. return true;
  2317. };
  2318. m_pLastIndex = 0;
  2319. };
  2320. return false;
  2321. #endif
  2322. }
  2323. #ifndef CURL_MANAGER
  2324. int pOnvif::_writer(char * data, size_t size, size_t nmemb, pOnvif * Ptr)
  2325. {
  2326. if (Ptr == NULL)
  2327. {
  2328. return 0;
  2329. }
  2330. // TLOG("*");
  2331. memcpy(Ptr->m_pLastResponse + Ptr->m_pLastIndex, data, size * nmemb);
  2332. Ptr->m_pLastIndex += size * nmemb;
  2333. return size * nmemb;
  2334. }
  2335. #endif
  2336. bool pOnvif::setAuthMode(uint8_t Mode)
  2337. {
  2338. m_bAuthMode = Mode;
  2339. #ifdef DEBUG
  2340. switch (m_bAuthMode)
  2341. {
  2342. case 0 : LOG("Set Auth CURLAUTH_BASIC"); break;
  2343. case 1 : LOG("Set Auth CURLAUTH_DIGEST"); break;
  2344. case 2 : LOG("Set Auth CURLAUTH_DIGEST_IE"); break;
  2345. case 3 : LOG("Set Auth CURLAUTH_BEARER"); break;
  2346. case 4 : LOG("Set Auth CURLAUTH_NEGOTIATE"); break;
  2347. case 5 : LOG("Set Auth CURLAUTH_NTLM"); break;
  2348. case 6 : LOG("Set Auth CURLAUTH_NTLM_WB"); break;
  2349. case 7 : LOG("Set Auth CURLAUTH_ANY"); break;
  2350. case 8 : LOG("Set Auth CURLAUTH_ANYSAFE"); break;
  2351. case 9 : LOG("Set Auth CURLAUTH_ONLY"); break;
  2352. case 10: LOG("Set Auth CURLAUTH_AWS_SIGV4"); break;
  2353. };
  2354. #endif
  2355. return true;
  2356. }
  2357. #undef LOG
  2358. #undef TLOG
  2359. #undef LOG_ERROR