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.

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