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.

1878 lines
89 KiB

7 years ago
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
  1. #include "ponvif.h"
  2. #include <fcntl.h>
  3. #include <sys/types.h> /* See NOTES */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  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. char _tBfrErrMSG2w34[256];\
  16. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  17. m_pLog->Log( "PONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  18. }
  19. #else
  20. #define LOG_ERROR( X ) m_pLog->Log( "PONVIF", this, _LOG_ERROR, X );
  21. #endif
  22. inline bool checkBool( const char *A ){
  23. if(A == NULL) return false;
  24. return ( A[0]=='T' || A[0] == 't' || A[0] == '1' );
  25. };
  26. inline bool checkOnOff( const char *A ){
  27. if(A == NULL) return false;
  28. return ( A[1]=='N' || A[1] == 'n' || A[0] == '1' );
  29. };
  30. inline onvifOnOffAuto checkOnOffAuto( const char *A ){
  31. if(A == NULL) return AUTO;
  32. if( A[1]=='N' || A[1] == 'n' || A[0] == '1' ) return ON;
  33. return OFF;
  34. };
  35. pOnvif::pOnvif(){
  36. m_pLog = basicLogCollection::getInstance();
  37. m_pIpAddress = NULL;
  38. m_pUsername = NULL;
  39. m_pPassword = NULL;
  40. m_pMediaURI = NULL;
  41. m_pSnapshotURI = NULL;
  42. m_pDeviceURI = NULL;
  43. m_pPTZURI = NULL;
  44. m_pBaseURI = NULL;
  45. m_nDeltaTime=0;
  46. m_pCURL = NULL;
  47. m_pProxyHost = NULL;
  48. m_nProxyPort = 0;
  49. m_pProxyUsername = NULL;
  50. m_pProxyPassword = NULL;
  51. m_pLastResponse = NULL;
  52. m_bIntransingent = true;
  53. m_nDiscoveryTimeout = PONVIF_DEFAULT_DISCOVERYTIMEOUT;
  54. m_pDiscoveryBindIP = NULL;//PONVIF_DEFAULT_DISCOVERYBINDIP;
  55. m_pDiscoveryMCastIP = NULL;//PONVIF_DEFAULT_DISCOVERYMCASTIP;
  56. m_nDiscoveryMCastPort = PONVIF_DEFAULT_DISCOVERYMCASTPORT;
  57. m_bDiscoveryHideDuplicates = true;
  58. m_pHttpAccessManager = NULL;
  59. m_sCapabilities.Analytics = NULL;
  60. m_sCapabilities.Device = NULL;
  61. m_sCapabilities.Events = NULL;
  62. m_sCapabilities.Imaging = NULL;
  63. m_sCapabilities.Media = NULL;
  64. m_sCapabilities.PTZ = NULL;
  65. m_sCapabilities.Extension = NULL;
  66. LOG("Constructor Done!");
  67. };
  68. pOnvif::~pOnvif(){
  69. #ifdef DEBUG
  70. LOG("Destructor begin");
  71. #endif
  72. if(m_pIpAddress) free(m_pIpAddress);
  73. if(m_pUsername) free(m_pUsername);
  74. if(m_pPassword) free(m_pPassword);
  75. if(m_pMediaURI) free(m_pMediaURI);
  76. if(m_pSnapshotURI) free(m_pSnapshotURI);
  77. if(m_pDeviceURI) free(m_pDeviceURI);
  78. if(m_pPTZURI) free(m_pPTZURI);
  79. if(m_pBaseURI) free(m_pBaseURI);
  80. if(m_pProxyHost) free(m_pProxyHost);
  81. if(m_pProxyUsername) free(m_pProxyUsername);
  82. if(m_pProxyPassword) free(m_pProxyPassword);
  83. if(m_pLastResponse) free(m_pLastResponse);
  84. if(m_pDiscoveryBindIP) free(m_pDiscoveryBindIP);
  85. if(m_pDiscoveryMCastIP) free(m_pDiscoveryMCastIP);
  86. if(m_sCapabilities.Analytics) free(m_sCapabilities.Analytics);
  87. if(m_sCapabilities.Device) free(m_sCapabilities.Device);
  88. if(m_sCapabilities.Events) free(m_sCapabilities.Events);
  89. if(m_sCapabilities.Imaging) free(m_sCapabilities.Imaging);
  90. if(m_sCapabilities.Media) free(m_sCapabilities.Media);
  91. if(m_sCapabilities.PTZ) free(m_sCapabilities.PTZ);
  92. if(m_sCapabilities.Extension) free(m_sCapabilities.Extension);
  93. if(m_pCURL){
  94. #ifdef DEBUG
  95. LOG( "Try Cleanup CURL!");
  96. #endif
  97. curl_easy_cleanup(m_pCURL);
  98. m_pCURL = NULL;
  99. };
  100. #ifdef DEBUG
  101. LOG("Destructor Done!");
  102. #endif
  103. };
  104. bool pOnvif::setProxyHost( CSTR proxyHost ){
  105. if( m_pProxyHost != NULL ) free( m_pProxyHost );
  106. if( proxyHost == NULL || strlen(proxyHost) == 0 ){
  107. m_pProxyHost = NULL;
  108. return true;
  109. };
  110. m_pProxyHost = (CSTR) malloc( strlen(proxyHost)+1);
  111. if( m_pProxyHost == NULL ) return false;
  112. memcpy(m_pProxyHost,proxyHost,strlen(proxyHost)+1);
  113. return true;
  114. };
  115. CSTR pOnvif::getProxyHost(){
  116. return m_pProxyHost;
  117. };
  118. bool pOnvif::setProxyPort( int proxyPort ){
  119. m_nProxyPort = proxyPort;
  120. return true;
  121. };
  122. int pOnvif::getProxyPort( void ){
  123. return m_nProxyPort;
  124. };
  125. bool pOnvif::setProxyUsername( CSTR proxyUsername ){
  126. if( m_pProxyUsername != NULL ) free( m_pProxyUsername );
  127. if( proxyUsername == NULL || strlen(proxyUsername) == 0 ){
  128. m_pProxyUsername = NULL;
  129. return true;
  130. };
  131. m_pProxyUsername = (CSTR) malloc( strlen(proxyUsername)+1);
  132. if( m_pProxyUsername == NULL ) return false;
  133. memcpy(m_pProxyUsername,proxyUsername,strlen(proxyUsername)+1);
  134. return true;
  135. };
  136. CSTR pOnvif::getProxyUsername( void ){
  137. return m_pProxyUsername;
  138. };
  139. bool pOnvif::setProxyPassword( CSTR proxyPassword ){
  140. if( m_pProxyPassword != NULL ) free( m_pProxyPassword );
  141. if( proxyPassword == NULL || strlen(proxyPassword) == 0 ){
  142. m_pProxyPassword = NULL;
  143. return true;
  144. };
  145. m_pProxyPassword = (CSTR) malloc( strlen(proxyPassword)+1);
  146. if( m_pProxyPassword == NULL ) return false;
  147. memcpy(m_pProxyPassword,proxyPassword,strlen(proxyPassword)+1);
  148. return true;
  149. };
  150. CSTR pOnvif::getProxyPassword(){
  151. return m_pProxyPassword;
  152. };
  153. bool pOnvif::setUsername( CSTR username ){
  154. if( m_pUsername != NULL ) free( m_pUsername );
  155. if( username == NULL || strlen(username) == 0 ){
  156. m_pUsername = NULL;
  157. return true;
  158. };
  159. m_pUsername = (CSTR) malloc( strlen(username)+1);
  160. if( m_pUsername == NULL ) return false;
  161. memcpy(m_pUsername,username,strlen(username)+1);
  162. return true;
  163. };
  164. CSTR pOnvif::getUsername( void ) {
  165. return m_pUsername;
  166. };
  167. bool pOnvif::setPassword( CSTR password ){
  168. if( m_pPassword != NULL ) free( m_pPassword );
  169. if( password == NULL || strlen(password) == 0 ){
  170. m_pPassword = NULL;
  171. return true;
  172. };
  173. m_pPassword = (CSTR) malloc( strlen(password)+1);
  174. if( m_pPassword == NULL ) return false;
  175. memcpy(m_pPassword,password,strlen(password)+1);
  176. return true;
  177. };
  178. CSTR pOnvif::getPassword( void ){
  179. return m_pPassword;
  180. };
  181. CSTR pOnvif::getDeviceUri( void ){
  182. return m_pDeviceURI;
  183. };
  184. bool pOnvif::setDeviceUri( CSTR deviceUri ){
  185. if( deviceUri == NULL || strlen(deviceUri) == 0 ){
  186. m_pDeviceURI = NULL;
  187. return false;
  188. };
  189. if( strstr( deviceUri, "http" ) == NULL ) {
  190. LOG_ERROR("Malformed device URI");
  191. return false;
  192. };
  193. if( m_pDeviceURI != NULL ) free( m_pDeviceURI );
  194. m_pDeviceURI = (CSTR) malloc( strlen(deviceUri)+1);
  195. if( m_pDeviceURI == NULL ) return false;
  196. memcpy(m_pDeviceURI,deviceUri,strlen(deviceUri));
  197. m_pDeviceURI[strlen(deviceUri)] = (char)0;
  198. //toDo: Must parsed Host/IP
  199. char *tT = strstr( m_pDeviceURI, "/" );
  200. if( tT ) {
  201. tT+=2;
  202. char *tB = strstr( tT, "/" );
  203. int len = tB - tT+1;
  204. m_pIpAddress = (CSTR) malloc( len );
  205. if( m_pIpAddress ) {
  206. len--;
  207. memcpy( m_pIpAddress, tT, len );
  208. m_pIpAddress[len]=(char)0;
  209. LOG( m_pIpAddress );
  210. };
  211. };
  212. return true;
  213. };
  214. CSTR pOnvif::getIPAddress( void ){
  215. return m_pIpAddress;
  216. };
  217. bool pOnvif::setIPAddress( CSTR ipAddress ){
  218. if( ipAddress == NULL || strlen(ipAddress) == 0 ){
  219. m_pIpAddress = NULL;
  220. return false;
  221. };
  222. if( m_pIpAddress != NULL ) free( m_pIpAddress );
  223. m_pIpAddress = (CSTR) malloc( strlen(ipAddress)+1);
  224. if( m_pIpAddress == NULL ) return false;
  225. memcpy( m_pIpAddress, ipAddress, strlen(ipAddress));
  226. m_pIpAddress[strlen(ipAddress)]=(char)0;
  227. //ToDo: Must be created URI ... sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  228. int len = strlen(ipAddress) + 28;
  229. m_pDeviceURI = (CSTR) malloc( len+1);
  230. if(m_pDeviceURI) {
  231. sprintf( m_pDeviceURI, "http://%s/onvif/device_service", m_pIpAddress );
  232. LOG( m_pDeviceURI );
  233. };
  234. return true;
  235. };
  236. videoSources *pOnvif::getSources( void ){
  237. return &m_vVideoSources;
  238. };
  239. videoSourceProfiles *pOnvif::getProfiles( void ){
  240. return &m_vVideoProfiles;
  241. };
  242. videoSourceProfiles *pOnvif::getActiveProfiles( void ){
  243. return &m_vActiveSources;
  244. };
  245. CSTR pOnvif::getMediaUri( void ){
  246. return m_pMediaURI;
  247. };
  248. CSTR pOnvif::getPTZUri( void ){
  249. return m_pPTZURI;
  250. };
  251. CSTR pOnvif::getBaseUrl( void ){
  252. return m_pBaseURI;
  253. };
  254. void pOnvif::getSupportedVersion( void ){ };
  255. nodeCapability *pOnvif::getCapabilities( void ){
  256. return &m_sCapabilities;
  257. };
  258. bool pOnvif::setBreakOnError( bool intransingent ){
  259. m_bIntransingent = intransingent;
  260. return true;
  261. };
  262. CSTR pOnvif::getLastResponse( void ){
  263. return m_pLastResponse;
  264. };
  265. bool pOnvif::setDiscoveryTimeout( int discoverytimeout ){
  266. m_nDiscoveryTimeout = discoverytimeout;
  267. return true;
  268. };
  269. bool pOnvif::setDiscoveryBindIp( CSTR discoverybindip ){
  270. if( m_pDiscoveryBindIP != NULL ) free( m_pDiscoveryBindIP );
  271. if( discoverybindip == NULL || strlen(discoverybindip) == 0 ){
  272. m_pDiscoveryBindIP = NULL;
  273. return true;
  274. };
  275. m_pDiscoveryBindIP = (CSTR) malloc( strlen(discoverybindip)+1);
  276. if( m_pDiscoveryBindIP == NULL ) return false;
  277. memcpy(m_pDiscoveryBindIP,discoverybindip,strlen(discoverybindip)+1);
  278. return true;
  279. };
  280. bool pOnvif::setDiscoveryMcastIp( CSTR discoverymcastip ){
  281. if( m_pDiscoveryMCastIP != NULL ) free( m_pDiscoveryMCastIP );
  282. if( discoverymcastip == NULL || strlen(discoverymcastip) == 0 ){
  283. m_pDiscoveryMCastIP = NULL;
  284. return true;
  285. };
  286. m_pDiscoveryMCastIP = (CSTR) malloc( strlen(discoverymcastip)+1);
  287. if( m_pDiscoveryMCastIP == NULL ) return false;
  288. memcpy(m_pDiscoveryMCastIP,discoverymcastip,strlen(discoverymcastip)+1);
  289. return true;
  290. };
  291. bool pOnvif::setDiscoveryMcastPort( int discoverymcastport ){
  292. m_nDiscoveryMCastPort = discoverymcastport;
  293. return true;
  294. };
  295. bool pOnvif::setDiscoveryHideDuplicates( bool discoveryhideduplicates ){
  296. m_bDiscoveryHideDuplicates = discoveryhideduplicates;
  297. return true;
  298. };
  299. CSTR pOnvif::getSnapshotUri( void ){
  300. return m_pSnapshotURI;
  301. };
  302. /*
  303. WS-Discovery
  304. https://www.geeksforgeeks.org/udp-server-client-implementation-c/
  305. */
  306. discoveryResults pOnvif::discover( void ){
  307. discoveryResults RetVal;
  308. time_t tTimeout = time( NULL ) + m_nDiscoveryTimeout;
  309. CSTR tPostString = PONVIF_PACK_DISCOVERY;
  310. struct sockaddr_in tSockAddr;
  311. struct hostent * h_info;
  312. #ifdef __linux__
  313. int tSocket;
  314. #else
  315. int tSocket;
  316. #endif
  317. int RetCode;
  318. char Response[DEFAULT_BUFFER_SIZE];
  319. try {
  320. //SOL_UDP = 17
  321. tSocket = socket(AF_INET, SOCK_DGRAM, 0 );
  322. if( tSocket < 0 ){
  323. LOG_ERROR("ERROR creating socket");
  324. return RetVal;
  325. };
  326. LOG("Socket Created!");
  327. memset(&tSockAddr, 0, sizeof( struct sockaddr_in ) );
  328. h_info = gethostbyname ( m_pDiscoveryBindIP );
  329. if ( h_info == NULL ){
  330. LOG_ERROR( "Some throuble with networking on this machine (Cannot Get Hostname)!" );
  331. return RetVal;
  332. };
  333. LOG("Host Found!");
  334. //*****************************************
  335. //* there we building our network address *
  336. //*****************************************
  337. memcpy( h_info->h_addr, &(tSockAddr.sin_addr), h_info->h_length );
  338. tSockAddr.sin_family = h_info->h_addrtype;
  339. tSockAddr.sin_port = htons( 20000 + ( 20000 * rand() ) / RAND_MAX );
  340. RetCode = bind( tSocket , (const sockaddr*)&tSockAddr, sizeof( struct sockaddr_in ) );
  341. if ( RetCode != 0 ){
  342. LOG_ERROR( "Error Binding to Port/Address" );
  343. return RetVal;
  344. };
  345. LOG("Binding Done!");
  346. #ifdef __linux__
  347. //setsockopt( tSocket, IPPROTO_IP, MCAST_JOIN_GROUP, );
  348. int tVal = 1;
  349. setsockopt(tSocket, SOL_SOCKET, SO_BROADCAST, &tVal, sizeof(int));
  350. //socket_sendto($sock, $post_string, strlen(tPostString), 0, $this->discoverymcastip, $this->discoverymcastport);
  351. sendto(tSocket, tPostString, strlen(tPostString), 0, (const sockaddr*)&tSockAddr, sizeof(struct sockaddr_in));
  352. LOG("Broadcast Ping Sent!");
  353. fcntl( tSocket, F_SETFL, fcntl(tSocket, F_GETFL, 0) | O_NONBLOCK );
  354. LOG("Socket Set Not Bloking Mode!");
  355. #else
  356. //setsockopt( tSocket, IPPROTO_IP, MCAST_JOIN_GROUP, );
  357. bool tVal = 1;
  358. setsockopt(tSocket, SOL_SOCKET, SO_BROADCAST, (const char *)&tVal, sizeof(bool));
  359. //socket_sendto($sock, $post_string, strlen(tPostString), 0, $this->discoverymcastip, $this->discoverymcastport);
  360. sendto(tSocket, tPostString, strlen(tPostString), 0, (const sockaddr*)&tSockAddr, sizeof(struct sockaddr_in));
  361. LOG("Broadcast Ping Sent!");
  362. u_long arg = 1;
  363. if (ioctlsocket(tSocket, FIONBIO, &arg) < 0) {
  364. char tBuf[128];
  365. sprintf(tBuf, "Error ioctlsocket(..., FIONBIO) (%s)", strerror(errno));
  366. LOG_ERROR(tBuf);
  367. return RetVal;
  368. };
  369. #endif
  370. while( time( NULL ) < tTimeout ){
  371. //n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
  372. tVal = recvfrom( tSocket, Response, DEFAULT_BUFFER_SIZE, 0, NULL, NULL );
  373. if( tVal > 0 ){
  374. Response[tVal] = '\0';
  375. //if( FALSE !== socket_recvfrom( $sock, $response, 9999, 0, $from, $this->discoverymcastport )){
  376. //if( Response != NULL && $response != $post_string){
  377. LOG(Response);
  378. XMLNode node = XMLNode::parseString( Response, "root" );
  379. //$response = $this->_xml2array($response);
  380. //if(!isFault( node )){
  381. //$response['Envelope']['Body']['ProbeMatches']['ProbeMatch']['IPAddr'] = $from;
  382. if( m_bDiscoveryHideDuplicates ){
  383. // $result[$from] = $response['Envelope']['Body']['ProbeMatches']['ProbeMatch'];
  384. }else{
  385. // $result[] = $response['Envelope']['Body']['ProbeMatches']['ProbeMatch'];
  386. };
  387. // };
  388. //};
  389. //};
  390. };
  391. };
  392. #ifdef __linux__
  393. close( tSocket );
  394. #else
  395. closesocket(tSocket);
  396. #endif
  397. } catch ( exception e ) {
  398. LOG_ERROR( "Communication at Discovery Exception Catched!");
  399. };
  400. return RetVal;
  401. };
  402. /*
  403. Public functions (basic initialization method and other collaterals)
  404. */
  405. bool pOnvif::create( void ) {
  406. m_pHttpAccessManager = curlRequestsManager::getInstance();
  407. if( m_pHttpAccessManager == NULL ) return false;
  408. LOG("CURL MANAGER OK");
  409. try {
  410. struct tm * tTS;
  411. tTS = core_GetSystemDateAndTime();
  412. if( tTS != NULL ){
  413. time_t tTimeStamp = mktime( tTS );
  414. m_nDeltaTime = time(NULL) - tTimeStamp;// -5;
  415. free( tTS );
  416. } else return false;
  417. } catch ( exception e ) {
  418. LOG_ERROR("Catched Exception!");
  419. return false;
  420. };
  421. if( !core_GetCapabilities() ){
  422. LOG_ERROR("Error Getting Caps!");
  423. return false;
  424. };
  425. _getOnvifVersion( m_sCapabilities );
  426. // Update some Vars Online!
  427. //preg_match("/^http(.*)onvif\//",$this->mediauri,$matches);
  428. //m_pBaseURI = $matches[0];
  429. if( media_GetVideoSources() ){
  430. if( media_GetProfiles() ) {
  431. //_getActiveSources( m_vVideoSources, m_vVideoProfiles);
  432. return true;
  433. };
  434. };
  435. return false;
  436. };
  437. /*
  438. Public wrappers for a subset of ONVIF primitives
  439. */
  440. struct tm *pOnvif::core_GetSystemDateAndTime( void ) {
  441. struct tm * RetVal = NULL;
  442. char tURL[CTEXT];
  443. if( m_pDeviceURI != NULL ){
  444. sprintf(tURL, m_pDeviceURI );
  445. } else {
  446. if( m_pIpAddress != NULL ){
  447. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  448. } else {
  449. return NULL;
  450. };
  451. };
  452. LOG("Before REQ");
  453. if( _send_request( tURL, PONVIF_PACK_GET_SYS_DTS, true ) ){
  454. XMLResults tParseResults;
  455. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  456. #ifdef DEBUG
  457. LOG("core_GetSystemDateAndTime");
  458. #endif
  459. if( tParseResults.error ){
  460. char tBuffer[CTEXT];
  461. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  462. LOG_ERROR( tBuffer );
  463. } else {
  464. RetVal = ( struct tm * ) malloc( sizeof(struct tm) );
  465. if( RetVal!= NULL ) {
  466. memset( RetVal, 0, sizeof(struct tm) );
  467. XMLNode zNode = response.getChildNode("Body", 0);
  468. XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0);
  469. XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0);
  470. XMLNode tNode = hNode.getChildNode("UTCDateTime", 0);
  471. if( !tNode.isEmpty() ) {
  472. RetVal->tm_isdst = 0;
  473. XMLNode ttNode = tNode.getChildNode( "Time", 0 );
  474. XMLNode dtNode = tNode.getChildNode( "Date", 0 );
  475. {
  476. XMLNode t = ttNode.getChildNode("Hour",0);
  477. RetVal->tm_hour = atoi( t.getText(0) );
  478. }
  479. {
  480. XMLNode t = ttNode.getChildNode("Minute",0);
  481. RetVal->tm_min = atoi( t.getText(0) );
  482. }
  483. {
  484. XMLNode t = ttNode.getChildNode("Second",0);
  485. RetVal->tm_sec = atoi( t.getText(0) );
  486. }
  487. {
  488. XMLNode t = dtNode.getChildNode("Year",0);
  489. RetVal->tm_year = atoi( t.getText(0) );
  490. }
  491. {
  492. XMLNode t = dtNode.getChildNode("Month",0);
  493. RetVal->tm_mon = atoi( t.getText(0) );
  494. }
  495. {
  496. XMLNode t = dtNode.getChildNode("Day",0);
  497. RetVal->tm_mday = atoi( t.getText(0) );
  498. }
  499. };
  500. };
  501. return RetVal;
  502. };
  503. } else LOG_ERROR( "GetSystemDateAndTime: Communication error" );
  504. return NULL;
  505. };
  506. // Based on https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl
  507. bool pOnvif::core_GetCapabilities( void ){
  508. char tBuffer[DEFAULT_BUFFER_SIZE];
  509. accessToken tTOKEN;
  510. tTOKEN = _makeToken();
  511. char tURL[CTEXT];
  512. if (m_pDeviceURI != NULL) {
  513. sprintf(tURL, m_pDeviceURI);
  514. } else {
  515. if (m_pIpAddress != NULL) {
  516. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  517. } else {
  518. return NULL;
  519. };
  520. };
  521. //char tSTR[CTEXT];
  522. sprintf( tBuffer, PONVIF_PACK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  523. //sprintf( tSTR, PONVIF_PCK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  524. //build_package( tBuffer, PONVIF_ENVELOPE_SIM, tSTR );
  525. if( _send_request( tURL, tBuffer ) ){
  526. XMLResults tParseResults;
  527. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  528. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  529. LOG("RCPT:");
  530. printf(m_pLastResponse);
  531. #endif
  532. if( tParseResults.error != eXMLErrorNone ){
  533. char tBuf[CTEXT];
  534. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  535. LOG_ERROR( tBuf );
  536. } else {
  537. XMLNode zNode = response.getChildNode("Body", 0);
  538. if( zNode.isEmpty() ) return false;
  539. XMLNode tNode = zNode.getChildNode("GetCapabilitiesResponse", 0);
  540. if( !tNode.isEmpty() ) {
  541. XMLNode tCaps = tNode.getChildNode("Capabilities", 0);
  542. if( !tCaps.isEmpty() ){
  543. {
  544. XMLNode t = tCaps.getChildNode("Analytics", 0);
  545. if( !t.isEmpty() ) {
  546. m_sCapabilities.Analytics = (onvifAnalyticsCapabilities *) malloc(sizeof(onvifAnalyticsCapabilities));
  547. if( m_sCapabilities.Analytics != NULL ) {
  548. memset( m_sCapabilities.Analytics, 0, sizeof(onvifAnalyticsCapabilities));
  549. {
  550. XMLNode c = t.getChildNode("XAddr", 0);
  551. sprintf( m_sCapabilities.Analytics->XAddr,"%s", c.getText(0) );
  552. }
  553. {
  554. XMLNode c = t.getChildNode("RuleSupport", 0);
  555. m_sCapabilities.Analytics->RuleSupport = checkBool(c.getText(0));
  556. }
  557. {
  558. XMLNode c = t.getChildNode("AnalyticsModuleSupport", 0);
  559. m_sCapabilities.Analytics->AnalyticsModuleSupport = checkBool(c.getText(0));
  560. }
  561. };
  562. };
  563. }
  564. {
  565. XMLNode t = tCaps.getChildNode("Device", 0);
  566. if( !t.isEmpty() ) {
  567. m_sCapabilities.Device = (onvifDeviceCapabilities *) malloc(sizeof(onvifDeviceCapabilities));
  568. if( m_sCapabilities.Device != NULL ) {
  569. memset( m_sCapabilities.Device, 0, sizeof(onvifDeviceCapabilities));
  570. {
  571. XMLNode c = t.getChildNode("XAddr", 0);
  572. sprintf( m_sCapabilities.Device->XAddr,"%s", c.getText(0) );
  573. }
  574. {
  575. XMLNode c = t.getChildNode("Network", 0);
  576. if( !c.isEmpty() ) {
  577. m_sCapabilities.Device->Network = (onvifNetworkCapabilities *) malloc(sizeof(onvifNetworkCapabilities));
  578. if( m_sCapabilities.Device->Network!= NULL ) {
  579. memset( m_sCapabilities.Device->Network, 0 , sizeof(onvifNetworkCapabilities));
  580. {
  581. XMLNode n = c.getChildNode("IPFilter", 0);
  582. m_sCapabilities.Device->Network->IPFilter = checkBool(n.getText(0));
  583. }
  584. {
  585. XMLNode n = c.getChildNode("ZeroConfiguration", 0);
  586. m_sCapabilities.Device->Network->ZeroConfiguration = checkBool(n.getText(0));
  587. }
  588. {
  589. XMLNode n = c.getChildNode("IPVersion6", 0);
  590. m_sCapabilities.Device->Network->IPVersion6 = checkBool(n.getText(0));
  591. }
  592. {
  593. XMLNode n = c.getChildNode("DynDNS", 0);
  594. m_sCapabilities.Device->Network->DynDNS = checkBool(n.getText(0));
  595. }
  596. {
  597. XMLNode n = c.getChildNode("Extension", 0);
  598. if( !n.isEmpty() ) {
  599. m_sCapabilities.Device->Network->Extension = (onvifNetworkCapabilitiesExtension *) malloc( sizeof(onvifNetworkCapabilitiesExtension));
  600. if( m_sCapabilities.Device->Network->Extension!= NULL ) {
  601. memset(m_sCapabilities.Device->Network->Extension, 0 ,sizeof(onvifNetworkCapabilitiesExtension));
  602. {
  603. XMLNode x = n.getChildNode("Dot11Configuration", 0);
  604. m_sCapabilities.Device->Network->Extension->Dot11Configuration = checkBool(x.getText(0));
  605. }
  606. };
  607. };
  608. }
  609. };
  610. };
  611. }
  612. {
  613. XMLNode c = t.getChildNode("System", 0);
  614. if( !c.isEmpty() ) {
  615. m_sCapabilities.Device->System = (onvifSystemCapabilities*) malloc(sizeof(onvifSystemCapabilities));
  616. if( m_sCapabilities.Device->System != NULL ) {
  617. memset(m_sCapabilities.Device->System, 0, sizeof(onvifSystemCapabilities));
  618. {
  619. XMLNode n = c.getChildNode("DiscoveryResolve", 0);
  620. m_sCapabilities.Device->System->DiscoveryResolve = checkBool(n.getText(0));
  621. }
  622. {
  623. XMLNode n = c.getChildNode("DiscoveryBye", 0);
  624. m_sCapabilities.Device->System->DiscoveryBye = checkBool(n.getText(0));
  625. }
  626. {
  627. XMLNode n = c.getChildNode("RemoteDiscovery", 0);
  628. m_sCapabilities.Device->System->RemoteDiscovery = checkBool(n.getText(0));
  629. }
  630. {
  631. XMLNode n = c.getChildNode("SystemBackup", 0);
  632. m_sCapabilities.Device->System->SystemBackup = checkBool(n.getText(0));
  633. }
  634. {
  635. XMLNode n = c.getChildNode("SystemLogging", 0);
  636. m_sCapabilities.Device->System->SystemLogging = checkBool(n.getText(0));
  637. }
  638. {
  639. XMLNode n = c.getChildNode("FirmwareUpgrade", 0);
  640. m_sCapabilities.Device->System->FirmwareUpgrade = checkBool(n.getText(0));
  641. }
  642. {
  643. XMLNode n = c.getChildNode("SupportedVersions", 0);
  644. if( !n.isEmpty() ) {
  645. m_sCapabilities.Device->System->SupportedVersions = (onvifSupportedVersions*) malloc(sizeof(onvifSupportedVersions));
  646. if( m_sCapabilities.Device->System->SupportedVersions!=NULL) {
  647. memset(m_sCapabilities.Device->System->SupportedVersions, 0, sizeof(onvifSupportedVersions));
  648. {
  649. XMLNode x = n.getChildNode("Major", 0);
  650. m_sCapabilities.Device->System->SupportedVersions->Major = atoi(x.getText(0));
  651. }
  652. {
  653. XMLNode x = n.getChildNode("Minor", 0);
  654. m_sCapabilities.Device->System->SupportedVersions->Minor = atoi(x.getText(0));
  655. }
  656. };
  657. };
  658. }
  659. {
  660. XMLNode n = c.getChildNode("Extension", 0);
  661. if( !n.isEmpty() ) {
  662. m_sCapabilities.Device->System->Extension = (onvifSystemCapabilitiesExtension*) malloc(sizeof(onvifSystemCapabilitiesExtension));
  663. if( m_sCapabilities.Device->System->Extension!=NULL) {
  664. memset(m_sCapabilities.Device->System->Extension, 0, sizeof(onvifSystemCapabilitiesExtension));
  665. {
  666. XMLNode x = n.getChildNode("HttpFirmwareUpgrade", 0);
  667. m_sCapabilities.Device->System->Extension->HttpFirmwareUpgrade = checkBool(x.getText(0));
  668. }
  669. {
  670. XMLNode x = n.getChildNode("HttpSystemBackup", 0);
  671. m_sCapabilities.Device->System->Extension->HttpSystemBackup = checkBool(x.getText(0));
  672. }
  673. {
  674. XMLNode x = n.getChildNode("HttpSystemLogging", 0);
  675. m_sCapabilities.Device->System->Extension->HttpSystemLogging = checkBool(x.getText(0));
  676. }
  677. {
  678. XMLNode x = n.getChildNode("HttpSupportInformation", 0);
  679. m_sCapabilities.Device->System->Extension->HttpSupportInformation = checkBool(x.getText(0));
  680. }
  681. };
  682. };
  683. }
  684. };
  685. };
  686. }
  687. {
  688. XMLNode c = t.getChildNode("IO", 0);
  689. if( !c.isEmpty() ) {
  690. m_sCapabilities.Device->IO = (onvifIOCapabilities*) malloc(sizeof(onvifIOCapabilities));
  691. if(m_sCapabilities.Device->IO != NULL) {
  692. memset(m_sCapabilities.Device->IO, 0, sizeof(onvifIOCapabilities));
  693. {
  694. XMLNode n = c.getChildNode("InputConnectors", 0);
  695. m_sCapabilities.Device->IO->InputConnectors = atoi(n.getText(0));
  696. }
  697. {
  698. XMLNode n = c.getChildNode("RelayOutputs", 0);
  699. m_sCapabilities.Device->IO->RelayOutputs = atoi(n.getText(0));
  700. }
  701. {
  702. XMLNode n = c.getChildNode("Extension", 0);
  703. if( !n.isEmpty() ) {
  704. m_sCapabilities.Device->IO->Extension = (onvifIOCapabilitiesExtension*) malloc(sizeof(onvifIOCapabilitiesExtension));
  705. if(m_sCapabilities.Device->IO->Extension!=NULL) {
  706. memset(m_sCapabilities.Device->IO->Extension, 0, sizeof(onvifIOCapabilitiesExtension));
  707. {
  708. XMLNode x = n.getChildNode("Auxiliary", 0);
  709. m_sCapabilities.Device->IO->Extension->Auxiliary = checkBool(x.getText(0));
  710. }
  711. };
  712. };
  713. }
  714. };
  715. };
  716. }
  717. {
  718. XMLNode c = t.getChildNode("Security", 0);
  719. if( !c.isEmpty() ) {
  720. m_sCapabilities.Device->Security = (onvifSecurityCapabilities*) malloc(sizeof(onvifSecurityCapabilities));
  721. if(m_sCapabilities.Device->Security!=NULL) {
  722. memset(m_sCapabilities.Device->Security, 0, sizeof(onvifSecurityCapabilities));
  723. {
  724. XMLNode n = c.getChildNode("TLS1.1", 0);
  725. m_sCapabilities.Device->Security->TLS1_1 = checkBool(n.getText(0));
  726. }
  727. {
  728. XMLNode n = c.getChildNode("TLS1.2", 0);
  729. m_sCapabilities.Device->Security->TLS1_2 = checkBool(n.getText(0));
  730. }
  731. {
  732. XMLNode n = c.getChildNode("OnboardKeyGeneration", 0);
  733. m_sCapabilities.Device->Security->OnboardKeyGeneration = checkBool(n.getText(0));
  734. }
  735. {
  736. XMLNode n = c.getChildNode("AccessPolicyConfig", 0);
  737. m_sCapabilities.Device->Security->AccessPolicyConfig = checkBool(n.getText(0));
  738. }
  739. {
  740. XMLNode n = c.getChildNode("X.509Token", 0);
  741. m_sCapabilities.Device->Security->X_509Token = checkBool(n.getText(0));
  742. }
  743. {
  744. XMLNode n = c.getChildNode("SAMLToken", 0);
  745. m_sCapabilities.Device->Security->SAMLToken = checkBool(n.getText(0));
  746. }
  747. {
  748. XMLNode n = c.getChildNode("KerberosToken", 0);
  749. m_sCapabilities.Device->Security->KerberosToken = checkBool(n.getText(0));
  750. }
  751. {
  752. XMLNode n = c.getChildNode("RELToken", 0);
  753. m_sCapabilities.Device->Security->RELToken = checkBool(n.getText(0));
  754. }
  755. {
  756. XMLNode n = c.getChildNode("Extension", 0);
  757. if(!n.isEmpty()){
  758. m_sCapabilities.Device->Security->Extension = (onvifSecurityCapabilitiesExtension*) malloc(sizeof(onvifSecurityCapabilitiesExtension));
  759. if(m_sCapabilities.Device->Security->Extension!=NULL) {
  760. memset(m_sCapabilities.Device->Security->Extension, 0, sizeof(onvifSecurityCapabilitiesExtension));
  761. {
  762. XMLNode x = n.getChildNode("TLS1.0", 0);
  763. m_sCapabilities.Device->Security->Extension->TLS1_0 = checkBool(x.getText(0));
  764. }
  765. };
  766. };
  767. }
  768. };
  769. };
  770. }
  771. };
  772. };
  773. }
  774. {
  775. XMLNode t = tCaps.getChildNode("Events", 0);
  776. if( !t.isEmpty() ) {
  777. m_sCapabilities.Events = (onvifEventCapabilities *) malloc(sizeof(onvifEventCapabilities));
  778. if( m_sCapabilities.Events != NULL ) {
  779. memset( m_sCapabilities.Events, 0, sizeof(onvifEventCapabilities));
  780. {
  781. XMLNode c = t.getChildNode("XAddr", 0);
  782. sprintf(m_sCapabilities.Events->XAddr,"%s", c.getText(0));
  783. }
  784. {
  785. XMLNode c = t.getChildNode("WSSubscriptionPolicySupport", 0);
  786. m_sCapabilities.Events->WSSubscriptionPolicySupport = checkBool(c.getText(0));
  787. }
  788. {
  789. XMLNode c = t.getChildNode("WSPullPointSupport", 0);
  790. m_sCapabilities.Events->WSPullPointSupport = checkBool(c.getText(0));
  791. }
  792. {
  793. XMLNode c = t.getChildNode("WSPausableSubscriptionManagerInterfaceSupport", 0);
  794. m_sCapabilities.Events->WSPausableSubscriptionManagerInterfaceSupport = checkBool(c.getText(0));
  795. }
  796. };
  797. };
  798. }
  799. {
  800. XMLNode t = tCaps.getChildNode("Imaging", 0);
  801. if( !t.isEmpty() ) {
  802. m_sCapabilities.Imaging = (onvifImagingCapabilities *) malloc(sizeof(onvifImagingCapabilities));
  803. if( m_sCapabilities.Imaging != NULL ) {
  804. memset( m_sCapabilities.Imaging, 0, sizeof(onvifImagingCapabilities));
  805. {
  806. XMLNode c = t.getChildNode("XAddr", 0);
  807. sprintf(m_sCapabilities.Imaging->XAddr,"%s", c.getText(0));
  808. }
  809. };
  810. };
  811. }
  812. {
  813. XMLNode t = tCaps.getChildNode("Media", 0);
  814. if( !t.isEmpty() ) {
  815. m_sCapabilities.Media = (onvifMediaCapabilities *) malloc(sizeof(onvifMediaCapabilities));
  816. if( m_sCapabilities.Media != NULL ) {
  817. memset( m_sCapabilities.Media, 0, sizeof(onvifMediaCapabilities));
  818. {
  819. XMLNode c = t.getChildNode("XAddr", 0);
  820. sprintf(m_sCapabilities.Media->XAddr,"%s", c.getText(0));
  821. }
  822. {
  823. XMLNode c = t.getChildNode("StreamingCapabilities", 0);
  824. if( !c.isEmpty() ) {
  825. m_sCapabilities.Media->StreamingCapabilities = (onvifRealTimeStreamingCapabilities*) malloc(sizeof(onvifRealTimeStreamingCapabilities));
  826. if(m_sCapabilities.Media->StreamingCapabilities!=NULL) {
  827. memset(m_sCapabilities.Media->StreamingCapabilities, 0, sizeof(onvifRealTimeStreamingCapabilities));
  828. {
  829. XMLNode n = c.getChildNode("RTPMulticast", 0);
  830. m_sCapabilities.Media->StreamingCapabilities->RTPMulticast = checkBool(n.getText(0));
  831. }
  832. {
  833. XMLNode n = c.getChildNode("RTP_TCP", 0);
  834. m_sCapabilities.Media->StreamingCapabilities->RTP_TCP = checkBool(n.getText(0));
  835. }
  836. {
  837. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  838. m_sCapabilities.Media->StreamingCapabilities->RTP_RTSP_TCP = checkBool(n.getText(0));
  839. }
  840. };
  841. };
  842. }
  843. {
  844. XMLNode c = t.getChildNode("Extension", 0);
  845. if( !c.isEmpty() ) {
  846. m_sCapabilities.Media->Extension = (onvifMediaCapabilitiesExtension*) malloc(sizeof(onvifMediaCapabilitiesExtension));
  847. if(m_sCapabilities.Media->Extension!=NULL) {
  848. memset(m_sCapabilities.Media->Extension, 0, sizeof(onvifMediaCapabilitiesExtension));
  849. {
  850. XMLNode n = c.getChildNode("ProfileCapabilities", 0);
  851. if(!n.isEmpty()){
  852. m_sCapabilities.Media->Extension->ProfileCapabilities = (onvifProfileCapabilities*) malloc(sizeof(onvifProfileCapabilities));
  853. if(m_sCapabilities.Media->Extension->ProfileCapabilities!=NULL) {
  854. memset(m_sCapabilities.Media->Extension->ProfileCapabilities, 0, sizeof(onvifProfileCapabilities));
  855. {
  856. XMLNode x = n.getChildNode("MaximumNumberOfProfiles", 0);
  857. m_sCapabilities.Media->Extension->ProfileCapabilities->MaximumNumberOfProfiles = atoi(x.getText(0));
  858. }
  859. };
  860. };
  861. }
  862. };
  863. };
  864. }
  865. };
  866. };
  867. }
  868. {
  869. XMLNode t = tCaps.getChildNode("PTZ", 0);
  870. if( !t.isEmpty() ) {
  871. m_sCapabilities.PTZ = (onvifPTZCapabilities *) malloc(sizeof(onvifPTZCapabilities));
  872. if( m_sCapabilities.PTZ != NULL ) {
  873. memset( m_sCapabilities.PTZ, 0, sizeof(onvifPTZCapabilities));
  874. {
  875. XMLNode c = t.getChildNode("XAddr", 0);
  876. sprintf(m_sCapabilities.PTZ->XAddr,"%s", c.getText(0));
  877. }
  878. };
  879. };
  880. }
  881. {
  882. XMLNode t = tCaps.getChildNode("Extension", 0);
  883. if( !t.isEmpty() ) {
  884. m_sCapabilities.Extension = (onvifCapabilitiesExtension *) malloc(sizeof(onvifCapabilitiesExtension));
  885. if( m_sCapabilities.Extension != NULL ) {
  886. memset( m_sCapabilities.Extension, 0, sizeof(onvifCapabilitiesExtension));
  887. {
  888. XMLNode c = t.getChildNode("DeviceIO", 0);
  889. if( !c.isEmpty() ) {
  890. m_sCapabilities.Extension->DeviceIO = (onvifDeviceIOCapabilities*) malloc(sizeof(onvifDeviceIOCapabilities));
  891. if(m_sCapabilities.Extension->DeviceIO!=NULL) {
  892. memset(m_sCapabilities.Extension->DeviceIO, 0, sizeof(onvifDeviceIOCapabilities));
  893. {
  894. XMLNode n = c.getChildNode("XAddr", 0);
  895. sprintf(m_sCapabilities.Extension->DeviceIO->XAddr, "%s", n.getText(0));
  896. }
  897. {
  898. XMLNode n = c.getChildNode("VideoSources", 0);
  899. m_sCapabilities.Extension->DeviceIO->VideoSources = atoi(n.getText(0));
  900. }
  901. {
  902. XMLNode n = c.getChildNode("VideoOutputs", 0);
  903. m_sCapabilities.Extension->DeviceIO->VideoOutputs = atoi(n.getText(0));
  904. }
  905. {
  906. XMLNode n = c.getChildNode("AudioSources", 0);
  907. m_sCapabilities.Extension->DeviceIO->AudioSources = atoi(n.getText(0));
  908. }
  909. {
  910. XMLNode n = c.getChildNode("AudioOutputs", 0);
  911. m_sCapabilities.Extension->DeviceIO->AudioOutputs = atoi(n.getText(0));
  912. }
  913. {
  914. XMLNode n = c.getChildNode("RelayOutputs", 0);
  915. m_sCapabilities.Extension->DeviceIO->RelayOutputs = atoi(n.getText(0));
  916. }
  917. };
  918. };
  919. }
  920. {
  921. XMLNode c = t.getChildNode("Display", 0);
  922. if( !c.isEmpty() ) {
  923. m_sCapabilities.Extension->Display = (onvifDisplayCapabilities*) malloc(sizeof(onvifDisplayCapabilities));
  924. if(m_sCapabilities.Extension->Display!=NULL) {
  925. memset(m_sCapabilities.Extension->Display, 0, sizeof(onvifDisplayCapabilities));
  926. {
  927. XMLNode n = c.getChildNode("XAddr", 0);
  928. sprintf(m_sCapabilities.Extension->Display->XAddr, "%s", n.getText(0));
  929. }
  930. {
  931. XMLNode n = c.getChildNode("FixedLayout", 0);
  932. m_sCapabilities.Extension->Display->FixedLayout = checkBool(n.getText(0));
  933. }
  934. };
  935. };
  936. }
  937. {
  938. XMLNode c = t.getChildNode("Recording", 0);
  939. if( !c.isEmpty() ) {
  940. m_sCapabilities.Extension->Recording = (onvifRecordingCapabilities*) malloc(sizeof(onvifRecordingCapabilities));
  941. if(m_sCapabilities.Extension->Recording!=NULL) {
  942. memset(m_sCapabilities.Extension->Recording, 0, sizeof(onvifRecordingCapabilities));
  943. {
  944. XMLNode n = c.getChildNode("XAddr", 0);
  945. sprintf(m_sCapabilities.Extension->Recording->XAddr, "%s", n.getText(0));
  946. }
  947. {
  948. XMLNode n = c.getChildNode("ReceiverSource", 0);
  949. m_sCapabilities.Extension->Recording->ReceiverSource = checkBool(n.getText(0));
  950. }
  951. {
  952. XMLNode n = c.getChildNode("MediaProfileSource", 0);
  953. m_sCapabilities.Extension->Recording->MediaProfileSource = checkBool(n.getText(0));
  954. }
  955. {
  956. XMLNode n = c.getChildNode("DynamicRecordings", 0);
  957. m_sCapabilities.Extension->Recording->DynamicRecordings = checkBool(n.getText(0));
  958. }
  959. {
  960. XMLNode n = c.getChildNode("DynamicTracks", 0);
  961. m_sCapabilities.Extension->Recording->DynamicTracks = checkBool(n.getText(0));
  962. }
  963. {
  964. XMLNode n = c.getChildNode("MaxStringLength", 0);
  965. m_sCapabilities.Extension->Recording->MaxStringLength = atoi(n.getText(0));
  966. }
  967. };
  968. };
  969. }
  970. {
  971. XMLNode c = t.getChildNode("Search", 0);
  972. if( !c.isEmpty() ) {
  973. m_sCapabilities.Extension->Search = (onvifSearchCapabilities*) malloc(sizeof(onvifSearchCapabilities));
  974. if(m_sCapabilities.Extension->Search!=NULL) {
  975. memset(m_sCapabilities.Extension->Search, 0, sizeof(onvifSearchCapabilities));
  976. {
  977. XMLNode n = c.getChildNode("XAddr", 0);
  978. sprintf(m_sCapabilities.Extension->Search->XAddr, "%s", n.getText(0));
  979. }
  980. {
  981. XMLNode n = c.getChildNode("MetadataSearch", 0);
  982. m_sCapabilities.Extension->Search->MetadataSearch = checkBool(n.getText(0));
  983. }
  984. };
  985. };
  986. }
  987. {
  988. XMLNode c = t.getChildNode("Replay", 0);
  989. if( !c.isEmpty() ) {
  990. m_sCapabilities.Extension->Replay = (onvifReplayCapabilities*) malloc(sizeof(onvifReplayCapabilities));
  991. if(m_sCapabilities.Extension->Replay!=NULL) {
  992. memset(m_sCapabilities.Extension->Replay, 0, sizeof(onvifReplayCapabilities));
  993. {
  994. XMLNode n = c.getChildNode("XAddr", 0);
  995. sprintf(m_sCapabilities.Extension->Replay->XAddr, "%s", n.getText(0));
  996. }
  997. };
  998. };
  999. }
  1000. {
  1001. XMLNode c = t.getChildNode("Receiver", 0);
  1002. if( !c.isEmpty() ) {
  1003. m_sCapabilities.Extension->Receiver = (onvifReceiverCapabilities*) malloc(sizeof(onvifReceiverCapabilities));
  1004. if(m_sCapabilities.Extension->Receiver!=NULL) {
  1005. memset(m_sCapabilities.Extension->Receiver, 0, sizeof(onvifReceiverCapabilities));
  1006. {
  1007. XMLNode n = c.getChildNode("XAddr", 0);
  1008. sprintf(m_sCapabilities.Extension->Receiver->XAddr, "%s", n.getText(0));
  1009. }
  1010. {
  1011. XMLNode n = c.getChildNode("RTP_Multicast", 0);
  1012. m_sCapabilities.Extension->Receiver->RTP_Multicast = checkBool(n.getText(0));
  1013. }
  1014. {
  1015. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1016. m_sCapabilities.Extension->Receiver->RTP_TCP = checkBool(n.getText(0));
  1017. }
  1018. {
  1019. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1020. m_sCapabilities.Extension->Receiver->RTP_RTSP_TCP = checkBool(n.getText(0));
  1021. }
  1022. {
  1023. XMLNode n = c.getChildNode("SupportedReceivers", 0);
  1024. m_sCapabilities.Extension->Receiver->SupportedReceivers = atoi(n.getText(0));
  1025. }
  1026. {
  1027. XMLNode n = c.getChildNode("MaximumRTSPURILength", 0);
  1028. m_sCapabilities.Extension->Receiver->MaximumRTSPURILength = atoi(n.getText(0));
  1029. }
  1030. };
  1031. };
  1032. }
  1033. {
  1034. XMLNode c = t.getChildNode("AnalyticsDevice", 0);
  1035. if( !c.isEmpty() ) {
  1036. m_sCapabilities.Extension->AnalyticsDevice = (onvifAnalyticsDeviceCapabilities*) malloc(sizeof(onvifAnalyticsDeviceCapabilities));
  1037. if(m_sCapabilities.Extension->AnalyticsDevice!=NULL) {
  1038. memset(m_sCapabilities.Extension->AnalyticsDevice, 0, sizeof(onvifAnalyticsDeviceCapabilities));
  1039. {
  1040. XMLNode n = c.getChildNode("XAddr", 0);
  1041. sprintf(m_sCapabilities.Extension->AnalyticsDevice->XAddr, "%s", n.getText(0));
  1042. }
  1043. {
  1044. XMLNode n = c.getChildNode("RuleSupport", 0);
  1045. m_sCapabilities.Extension->AnalyticsDevice->RuleSupport = checkBool(n.getText(0));
  1046. }
  1047. };
  1048. };
  1049. }
  1050. };
  1051. };
  1052. }
  1053. return true;
  1054. } else {
  1055. LOG("Capabilities is Empty!");
  1056. };
  1057. } else {
  1058. LOG("GetCapabilitiesResponse is Empty!");
  1059. };
  1060. };
  1061. }
  1062. return false;
  1063. };
  1064. // based on https://www.onvif.org/ver20/imaging/wsdl/imaging.wsdl
  1065. bool pOnvif::media_GetVideoSources( void ){
  1066. if( m_pMediaURI==NULL ) return false;
  1067. char tBuffer[DEFAULT_BUFFER_SIZE];
  1068. accessToken tTOKEN;
  1069. tTOKEN = _makeToken();
  1070. sprintf( tBuffer, PONVIF_PACK_GET_VIDEO_SOURCES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1071. if( _send_request( m_pMediaURI, tBuffer ) ){
  1072. XMLResults tParseResults;
  1073. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1074. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1075. LOG("RCPT:");
  1076. printf(m_pLastResponse);
  1077. #endif
  1078. if( tParseResults.error ){
  1079. char tBuf[CTEXT];
  1080. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1081. LOG_ERROR( tBuf );
  1082. } else {
  1083. XMLNode zNode = response.getChildNode("Body", 0);
  1084. if( zNode.isEmpty() ) return false;
  1085. XMLNode tNode = zNode.getChildNode("GetVideoSourcesResponse", 0);
  1086. if( !tNode.isEmpty() ) {
  1087. for( int index = 0; index < tNode.nChildNode(); index++ ){
  1088. XMLNode cNode = tNode.getChildNode("VideoSources", index );
  1089. if(!cNode.isEmpty()) {
  1090. videoSource *tPtr;
  1091. tPtr = (videoSource*) malloc(sizeof(videoSource));
  1092. if( tPtr != NULL ) {
  1093. sprintf( tPtr->token, "%s", cNode.getAttribute("token",0));
  1094. {
  1095. XMLNode nNode = cNode.getChildNode("Framerate", 0 );
  1096. tPtr->Framerate = (float)atof( nNode.getText(0));
  1097. }
  1098. {
  1099. XMLNode nNode = cNode.getChildNode("Resolution", 0 );
  1100. if( !nNode.isEmpty() ) {
  1101. {
  1102. XMLNode xNode = nNode.getChildNode("Width", 0 );
  1103. tPtr->Resolution.Width = atoi(xNode.getText(0));
  1104. }
  1105. {
  1106. XMLNode xNode = nNode.getChildNode("Height", 0 );
  1107. tPtr->Resolution.Height = atoi(xNode.getText(0));
  1108. }
  1109. };
  1110. }
  1111. {
  1112. XMLNode nNode = cNode.getChildNode("Imaging", 0 );
  1113. if( !nNode.isEmpty() ) {
  1114. tPtr->Imaging = (onvifImagingSettings*) malloc(sizeof(onvifImagingSettings));
  1115. if( tPtr->Imaging != NULL) {
  1116. memset(tPtr->Imaging, 0, sizeof(onvifImagingSettings));
  1117. {
  1118. XMLNode xNode = nNode.getChildNode("BacklightCompensation", 0 );
  1119. if(!xNode.isEmpty()) {
  1120. tPtr->Imaging->BacklightCompensation = (onvifBacklightCompensation*)malloc(sizeof(onvifBacklightCompensation));
  1121. if(tPtr->Imaging->BacklightCompensation!=NULL) {
  1122. memset(tPtr->Imaging->BacklightCompensation, 0, sizeof(onvifBacklightCompensation));
  1123. {
  1124. XMLNode rNode = xNode.getChildNode("Mode", 0 );
  1125. tPtr->Imaging->BacklightCompensation->Mode = checkOnOff(rNode.getText(0));
  1126. }
  1127. {
  1128. XMLNode rNode = xNode.getChildNode("Level", 0 );
  1129. tPtr->Imaging->BacklightCompensation->Level = (float)atof(rNode.getText(0));
  1130. }
  1131. };
  1132. };
  1133. }
  1134. {
  1135. XMLNode xNode = nNode.getChildNode("Brightness", 0 );
  1136. tPtr->Imaging->Brightness = (float)atoi(xNode.getText(0));
  1137. }
  1138. {
  1139. XMLNode xNode = nNode.getChildNode("ColorSaturation", 0 );
  1140. tPtr->Imaging->ColorSaturation = (float)atoi(xNode.getText(0));
  1141. }
  1142. {
  1143. XMLNode xNode = nNode.getChildNode("Contrast", 0 );
  1144. tPtr->Imaging->Contrast = (float)atoi(xNode.getText(0));
  1145. }
  1146. {
  1147. XMLNode xNode = nNode.getChildNode("Sharpness", 0 );
  1148. tPtr->Imaging->Sharpness = (float)atoi(xNode.getText(0));
  1149. }
  1150. {
  1151. XMLNode xNode = nNode.getChildNode("IrCutFilter", 0 );
  1152. tPtr->Imaging->IrCutFilter = checkOnOffAuto(xNode.getText(0));
  1153. }
  1154. };
  1155. };
  1156. }
  1157. m_vVideoSources.push_back( tPtr );
  1158. };
  1159. };
  1160. };
  1161. return true;
  1162. } else {
  1163. LOG_ERROR("GetVideoSourcesResponse EMPTY!");
  1164. };
  1165. };
  1166. } else {
  1167. LOG_ERROR( "_send_request Failure!" );
  1168. };
  1169. return false;
  1170. };
  1171. bool pOnvif::media_GetProfiles( void ){
  1172. char tBuffer[DEFAULT_BUFFER_SIZE];
  1173. accessToken tTOKEN;
  1174. tTOKEN = _makeToken();
  1175. sprintf( tBuffer, PONVIF_PACK_GET_PROFILES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1176. if( _send_request( m_pMediaURI, tBuffer ) ){
  1177. XMLResults tParseResults;
  1178. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1179. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1180. LOG("RCPT:");
  1181. printf(m_pLastResponse);
  1182. #endif
  1183. if( tParseResults.error ){
  1184. char tBuf[CTEXT];
  1185. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1186. LOG_ERROR( tBuf );
  1187. } else {
  1188. XMLNode zNode = response.getChildNode("Body", 0);
  1189. if( zNode.isEmpty() ) return false;
  1190. XMLNode tNode = zNode.getChildNode("GetProfilesResponse", 0);
  1191. if( !tNode.isEmpty() ) {
  1192. for( int index = 0; index < tNode.nChildNode(); index++ ){
  1193. XMLNode cNode = tNode.getChildNode("Profiles", index );
  1194. if(!cNode.isEmpty()) {
  1195. videoSourceProfile *tPtr;
  1196. tPtr = (videoSourceProfile*) malloc(sizeof(videoSourceProfile));
  1197. if( tPtr != NULL ) {
  1198. memset(tPtr, 0, sizeof(videoSourceProfile));
  1199. sprintf(tPtr->profiletoken, "%s", cNode.getAttribute("token",0));
  1200. {
  1201. XMLNode nNode = cNode.getChildNode("Name", 0 );
  1202. sprintf(tPtr->profilename, "%s", nNode.getText(0));
  1203. }
  1204. {
  1205. XMLNode nNode = cNode.getChildNode("VideoSourceConfiguration", 0 );
  1206. if(!nNode.isEmpty()) {
  1207. XMLNode zNode = nNode.getChildNode("SourceToken", 0 );
  1208. sprintf(tPtr->sourcetoken, "%s", zNode.getText(0));
  1209. };
  1210. }
  1211. {
  1212. XMLNode nNode = cNode.getChildNode("VideoEncoderConfiguration", 0 );
  1213. if(!nNode.isEmpty()) {
  1214. {
  1215. XMLNode zNode = nNode.getChildNode("Encoding", 0 );
  1216. sprintf(tPtr->encoding, "%s", zNode.getText(0));
  1217. }
  1218. {
  1219. XMLNode zNode = nNode.getChildNode("Resolution", 0 );
  1220. {
  1221. XMLNode jNode = zNode.getChildNode("Width", 0 );
  1222. tPtr->width = atoi(jNode.getText(0));
  1223. }
  1224. {
  1225. XMLNode jNode = zNode.getChildNode("Height", 0 );
  1226. tPtr->height = atoi(jNode.getText(0));
  1227. }
  1228. }
  1229. {
  1230. XMLNode zNode = nNode.getChildNode("RateControl", 0 );
  1231. {
  1232. XMLNode jNode = zNode.getChildNode("FrameRateLimit", 0 );
  1233. tPtr->fps = atoi(jNode.getText(0));
  1234. }
  1235. {
  1236. XMLNode jNode = zNode.getChildNode("BitrateLimit", 0 );
  1237. tPtr->bitrate = atoi(jNode.getText(0));
  1238. }
  1239. }
  1240. };
  1241. }
  1242. m_vVideoProfiles.push_back(tPtr);
  1243. };
  1244. };
  1245. };
  1246. return true;
  1247. };
  1248. };
  1249. };
  1250. return false;
  1251. };
  1252. bool pOnvif::media_GetServices( void ){
  1253. char tBuffer[DEFAULT_BUFFER_SIZE];
  1254. accessToken tTOKEN;
  1255. tTOKEN = _makeToken();
  1256. sprintf( tBuffer, PONVIF_PACK_GET_SERVICES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1257. if( _send_request( m_pMediaURI, tBuffer ) ){
  1258. XMLResults tParseResults;
  1259. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1260. LOG("RCPT:");
  1261. printf(m_pLastResponse);
  1262. if( tParseResults.error ){
  1263. char tBuf[CTEXT];
  1264. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1265. LOG_ERROR( tBuf );
  1266. } else {
  1267. };
  1268. };
  1269. return false;
  1270. };
  1271. bool pOnvif::core_GetDeviceInformation() {
  1272. char tBuffer[DEFAULT_BUFFER_SIZE];
  1273. accessToken tTOKEN;
  1274. tTOKEN = _makeToken();
  1275. sprintf( tBuffer, PONVIF_PACK_GET_DEV_INFO, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1276. if( _send_request( m_pMediaURI, tBuffer ) ){
  1277. XMLResults tParseResults;
  1278. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1279. LOG("RCPT:");
  1280. printf(m_pLastResponse);
  1281. if( tParseResults.error ){
  1282. char tBuf[CTEXT];
  1283. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1284. LOG_ERROR( tBuf );
  1285. } else {
  1286. //return $response['Envelope']['Body']['GetDeviceInformationResponse'];
  1287. };
  1288. };
  1289. return false;
  1290. };
  1291. CSTR pOnvif::media_GetStreamUri( CSTR profileToken, CSTR stream, CSTR protocol ){
  1292. char tBuffer[DEFAULT_BUFFER_SIZE];
  1293. accessToken tTOKEN;
  1294. tTOKEN = _makeToken();
  1295. sprintf( tBuffer, PONVIF_PACK_GET_STREAM_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, stream, protocol );
  1296. if( _send_request( m_pMediaURI, tBuffer ) ){
  1297. XMLResults tParseResults;
  1298. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1299. LOG("RCPT:");
  1300. printf(m_pLastResponse);
  1301. if( tParseResults.error ){
  1302. char tBuf[CTEXT];
  1303. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1304. LOG_ERROR( tBuf );
  1305. } else {
  1306. // Parse
  1307. //return $response['Envelope']['Body']['GetStreamUriResponse']['MediaUri']['Uri'];
  1308. };
  1309. };
  1310. return NULL;
  1311. };
  1312. CSTR pOnvif::media_GetSnapshotUri( CSTR profileToken ){
  1313. char tBuffer[DEFAULT_BUFFER_SIZE];
  1314. accessToken tTOKEN;
  1315. tTOKEN = _makeToken();
  1316. sprintf( tBuffer, PONVIF_PACK_GET_PICTURE_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1317. if( _send_request( m_pMediaURI, tBuffer ) ){
  1318. XMLResults tParseResults;
  1319. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1320. LOG("RCPT:");
  1321. printf(m_pLastResponse);
  1322. if( tParseResults.error ){
  1323. char tBuf[CTEXT];
  1324. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1325. LOG_ERROR( tBuf );
  1326. } else {
  1327. //Parse
  1328. //return $response['Envelope']['Body']['GetSnapshotUriResponse']['MediaUri']['Uri'];
  1329. };
  1330. };
  1331. return NULL;
  1332. };
  1333. bool pOnvif::ptz_GetStatus( CSTR profileToken, ptzStatus *tStatus ){
  1334. if( m_pPTZURI==NULL ) return false;
  1335. if( tStatus == NULL ) return false;
  1336. char tBuffer[DEFAULT_BUFFER_SIZE];
  1337. accessToken tTOKEN;
  1338. tTOKEN = _makeToken();
  1339. sprintf( tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1340. if( _send_request( m_pPTZURI, tBuffer, true ) ){
  1341. XMLResults tParseResults;
  1342. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1343. if( tParseResults.error ){
  1344. char tBuf[CTEXT];
  1345. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1346. LOG_ERROR( tBuf );
  1347. } else {
  1348. XMLNode aNode = response.getChildNode("Body", 0);
  1349. if( aNode.isEmpty() ) return NULL;
  1350. XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0);
  1351. if( !bNode.isEmpty() ) {
  1352. XMLNode cNode = bNode.getChildNode("PTZStatus", 0 );
  1353. if(!cNode.isEmpty()) {
  1354. memset( tStatus, 0, sizeof(ptzStatus));
  1355. XMLNode dNode = cNode.getChildNode("Position", 0 );
  1356. if(!dNode.isEmpty()) {
  1357. XMLNode eNode = dNode.getChildNode("PanTilt", 0 );
  1358. if( !eNode.isEmpty()) {
  1359. //XMLAttribute ZZ = eNode.getAttribute(1);
  1360. //LOG( ZZ.lpszName );
  1361. tStatus->pan = atof( eNode.getAttribute("x") ); //ZZ.lpszValue );
  1362. //ZZ = eNode.getAttribute(2);
  1363. //LOG( ZZ.lpszName );
  1364. tStatus->tilt = atof( eNode.getAttribute("y") );// ZZ.lpszValue );
  1365. };
  1366. XMLNode fNode = dNode.getChildNode("Zoom", 0 );
  1367. if( !fNode.isEmpty()) {
  1368. //XMLAttribute ZZ = eNode.getAttribute(1);
  1369. //LOG( ZZ.lpszName );
  1370. tStatus->zoom = atof( fNode.getAttribute("x") ); //ZZ.lpszValue );
  1371. };
  1372. };
  1373. XMLNode gNode = cNode.getChildNode("MoveStatus", 0 );
  1374. if(!gNode.isEmpty()){
  1375. tStatus->hasMoveStatus = true;
  1376. XMLNode hNode = gNode.getChildNode("PanTilt", 0 );
  1377. if( !hNode.isEmpty()) {
  1378. if( strstr( hNode.getText(0),"IDLE" ) != NULL ) tStatus->PanTiltIdle = true;
  1379. };
  1380. XMLNode iNode = gNode.getChildNode("Zoom", 0 );
  1381. if( !iNode.isEmpty()) {
  1382. if( strstr( iNode.getText(0),"IDLE" ) != NULL ) tStatus->ZoomIdle = true;
  1383. };
  1384. };
  1385. return true;
  1386. };
  1387. };
  1388. };
  1389. };
  1390. return false;
  1391. };
  1392. bool pOnvif::ptz_GetPresets( CSTR profileToken ){
  1393. if( m_pPTZURI==NULL ) return false;
  1394. char tBuffer[DEFAULT_BUFFER_SIZE];
  1395. accessToken tTOKEN;
  1396. tTOKEN = _makeToken();
  1397. sprintf( tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1398. if( _send_request( m_pPTZURI, tBuffer ) ){
  1399. XMLResults tParseResults;
  1400. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1401. LOG("RCPT:");
  1402. printf(m_pLastResponse);
  1403. if( tParseResults.error ){
  1404. char tBuf[CTEXT];
  1405. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1406. LOG_ERROR( tBuf );
  1407. } else {
  1408. //$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
  1409. //$presets=array();
  1410. //foreach ($getpresetsresponse as $preset) {
  1411. // $presets[]=array('Token'=>$preset['@attributes']['token'],
  1412. // 'Name'=>$preset['Name'],
  1413. // 'PTZPosition'=>$preset['PTZPosition']);
  1414. //}
  1415. //return $presets;
  1416. };
  1417. };
  1418. return false;
  1419. };
  1420. bool pOnvif::ptz_GetNode( CSTR ptzNodeToken ) {
  1421. if( m_pPTZURI == NULL ) return false;
  1422. char tBuffer[DEFAULT_BUFFER_SIZE];
  1423. accessToken tTOKEN;
  1424. tTOKEN = _makeToken();
  1425. sprintf( tBuffer, PONVIF_PACK_GET_PTZ_NODE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, ptzNodeToken );
  1426. if( _send_request( m_pPTZURI, tBuffer ) ){
  1427. XMLResults tParseResults;
  1428. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1429. LOG("RCPT:");
  1430. printf(m_pLastResponse);
  1431. if( tParseResults.error ){
  1432. char tBuf[CTEXT];
  1433. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1434. LOG_ERROR( tBuf );
  1435. } else {
  1436. //return $response['Envelope']['Body']['GetNodeResponse'];
  1437. };
  1438. };
  1439. return false;
  1440. };
  1441. bool pOnvif::ptz_GotoPreset( CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x ){
  1442. if( m_pPTZURI == NULL ) return false;
  1443. char tBuffer[DEFAULT_BUFFER_SIZE];
  1444. accessToken tTOKEN;
  1445. tTOKEN = _makeToken();
  1446. 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 );
  1447. return _send_request( m_pPTZURI, tBuffer, true );
  1448. };
  1449. bool pOnvif::ptz_RemovePreset( CSTR profileToken, CSTR presetToken ){
  1450. if( m_pPTZURI == NULL ) return false;
  1451. char tBuffer[DEFAULT_BUFFER_SIZE];
  1452. accessToken tTOKEN;
  1453. tTOKEN = _makeToken();
  1454. sprintf( tBuffer, PONVIF_PACK_REMOVE_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken );
  1455. return _send_request( m_pPTZURI, tBuffer, true );
  1456. };
  1457. bool pOnvif::ptz_SetPreset( CSTR profileToken, CSTR presetName ){
  1458. if( m_pPTZURI == NULL ) return false;
  1459. char tBuffer[DEFAULT_BUFFER_SIZE];
  1460. accessToken tTOKEN;
  1461. tTOKEN = _makeToken();
  1462. sprintf( tBuffer, PONVIF_PACK_SET_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetName );
  1463. return _send_request( m_pPTZURI, tBuffer, true );
  1464. };
  1465. bool pOnvif::ptz_RelativeMove( CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y ){
  1466. if( m_pPTZURI == NULL ) return false;
  1467. char tBuffer[DEFAULT_BUFFER_SIZE];
  1468. accessToken tTOKEN;
  1469. tTOKEN = _makeToken();
  1470. 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 );
  1471. return _send_request( m_pPTZURI, tBuffer, true );
  1472. };
  1473. bool pOnvif::ptz_RelativeMoveZoom( CSTR profileToken, float zoom, float speedZoom ){
  1474. if( m_pPTZURI == NULL ) return false;
  1475. char tBuffer[DEFAULT_BUFFER_SIZE];
  1476. accessToken tTOKEN;
  1477. tTOKEN = _makeToken();
  1478. sprintf( tBuffer, PONVIF_PACK_RELATIVE_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom, speedZoom );
  1479. return _send_request( m_pPTZURI, tBuffer, true );
  1480. };
  1481. bool pOnvif::ptz_AbsoluteMove( CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom ){
  1482. if( m_pPTZURI == NULL ) return false;
  1483. char tBuffer[DEFAULT_BUFFER_SIZE];
  1484. accessToken tTOKEN;
  1485. tTOKEN = _makeToken();
  1486. sprintf( tBuffer, PONVIF_PACK_ABSOLUTE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, position_pantilt_x, position_pantilt_y, zoom );
  1487. return _send_request( m_pPTZURI, tBuffer, false );
  1488. };
  1489. bool pOnvif::ptz_ContinuousMove( CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y ){
  1490. if( m_pPTZURI == NULL ) return false;
  1491. char tBuffer[DEFAULT_BUFFER_SIZE];
  1492. accessToken tTOKEN;
  1493. tTOKEN = _makeToken();
  1494. sprintf( tBuffer, PONVIF_PACK_CONTINUOUS_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, velocity_pantilt_x, velocity_pantilt_y );
  1495. return _send_request( m_pPTZURI, tBuffer, true );
  1496. };
  1497. bool pOnvif::ptz_ContinuousMoveZoom( CSTR profileToken, float zoom ){
  1498. if( m_pPTZURI == NULL ) return false;
  1499. char tBuffer[DEFAULT_BUFFER_SIZE];
  1500. accessToken tTOKEN;
  1501. tTOKEN = _makeToken();
  1502. sprintf( tBuffer, PONVIF_PACK_CONTINUOUS_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom );
  1503. return _send_request( m_pPTZURI, tBuffer, true );
  1504. };
  1505. bool pOnvif::ptz_Stop( CSTR profileToken, bool pantilt, bool zoom ){
  1506. if( m_pPTZURI == NULL ) return false;
  1507. char tBuffer[DEFAULT_BUFFER_SIZE];
  1508. accessToken tTOKEN;
  1509. tTOKEN = _makeToken();
  1510. sprintf( tBuffer, PONVIF_PACK_PTZ_STOP, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );//, pantilt, zoom );
  1511. return _send_request( m_pPTZURI, tBuffer, true );
  1512. };
  1513. /*
  1514. Internal functions
  1515. */
  1516. accessToken pOnvif::_makeToken( void ){
  1517. time_t tTS = time( NULL ) - m_nDeltaTime;
  1518. return _passwordDigest( m_pUsername, m_pPassword, tTS );
  1519. };
  1520. void pOnvif::_getOnvifVersion( nodeCapability &caps ){
  1521. if( caps.Device != NULL ) {
  1522. if( caps.Device->System != NULL ) {
  1523. if( caps.Device->System->SupportedVersions != NULL ) {
  1524. m_sOnvifVersion.Major = caps.Device->System->SupportedVersions->Major;
  1525. m_sOnvifVersion.Minor = caps.Device->System->SupportedVersions->Minor;
  1526. };
  1527. };
  1528. };
  1529. if( caps.PTZ != NULL ) {
  1530. m_pPTZURI = caps.PTZ->XAddr;
  1531. };
  1532. if( caps.Media != NULL ) {
  1533. m_pMediaURI = caps.Media->XAddr;
  1534. };
  1535. if( caps.Device != NULL ) {
  1536. m_pDeviceURI = caps.Device->XAddr;
  1537. };
  1538. };
  1539. /*
  1540. protected function _getActiveSources($videoSources,$profiles) {
  1541. $sources=array();
  1542. if (isset($videoSources['@attributes'])) {
  1543. // NVT is a camera
  1544. $sources[0]['sourcetoken']=$videoSources['@attributes']['token'];
  1545. $this->_getProfileData($sources,0,$profiles);
  1546. }
  1547. else {
  1548. // NVT is an encoder
  1549. for ($i=0;$i<count($videoSources);$i++) {
  1550. if (strtolower($videoSources[$i]['@attributes']['SignalActive'])=='true') {
  1551. $sources[$i]['sourcetoken']=$videoSources[$i]['@attributes']['token'];
  1552. $this->_getProfileData($sources,$i,$profiles);
  1553. }
  1554. } // for
  1555. }
  1556. return $sources;
  1557. }
  1558. void pOnvif::_getProfileData( sources,$i, XMLNode profiles ){
  1559. //$inprofile=0;
  1560. videoSourceProfile *tPtr;
  1561. for( $y=0; $y < count($profiles); $y++ ){
  1562. if ($profiles[$y]['VideoSourceConfiguration']['SourceToken']==$sources[$i]['sourcetoken']) {
  1563. $sources[$i][$inprofile]['profilename']=$profiles[$y]['Name'];
  1564. $sources[$i][$inprofile]['profiletoken']=$profiles[$y]['@attributes']['token'];
  1565. if ( isset($profiles[$y]['VideoEncoderConfiguration'])) {
  1566. $sources[$i][$inprofile]['encoding']=$profiles[$y]['VideoEncoderConfiguration']['Encoding'];
  1567. $sources[$i][$inprofile]['width']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Width'];
  1568. $sources[$i][$inprofile]['height']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Height'];
  1569. $sources[$i][$inprofile]['fps']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['FrameRateLimit'];
  1570. $sources[$i][$inprofile]['bitrate']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['BitrateLimit'];
  1571. }
  1572. if ( isset($profiles[$y]['PTZConfiguration'])) {
  1573. $sources[$i][$inprofile]['ptz']['name']=$profiles[$y]['PTZConfiguration']['Name'];
  1574. $sources[$i][$inprofile]['ptz']['nodetoken']=$profiles[$y]['PTZConfiguration']['NodeToken'];
  1575. }
  1576. $inprofile++;
  1577. }
  1578. }
  1579. };
  1580. */
  1581. /*
  1582. protected function _xml2array($response) {
  1583. $sxe = new SimpleXMLElement($response);
  1584. $dom_sxe = dom_import_simplexml($sxe);
  1585. $dom = new DOMDocument('1.0');
  1586. $dom_sxe = $dom->importNode($dom_sxe, true);
  1587. $dom_sxe = $dom->appendChild($dom_sxe);
  1588. $element = $dom->childNodes->item(0);
  1589. foreach ($sxe->getDocNamespaces() as $name => $uri) {
  1590. $element->removeAttributeNS($uri, $name);
  1591. }
  1592. $xmldata=$dom->saveXML();
  1593. $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>"));
  1594. $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>"));
  1595. $xml=simplexml_load_string($xmldata);
  1596. $data=json_decode(json_encode((array)$xml),1);
  1597. $data=array($xml->getName()=>$data);
  1598. return $data;
  1599. }
  1600. */
  1601. accessToken pOnvif::_passwordDigest( CSTR username, CSTR password, time_t timestamp, uint32_t nonce ) {
  1602. accessToken RetVal;
  1603. char tTS[CTEXT];
  1604. unsigned char * tPDIGEST;
  1605. if( timestamp == 0 ){
  1606. time_t t;
  1607. struct tm * tmp;
  1608. t = time( NULL );
  1609. tmp = gmtime( &t );
  1610. if( tmp == NULL ){
  1611. LOG_ERROR("gmtime Error!");
  1612. };
  1613. if( strftime( tTS, 200, "%FT%T.000Z", tmp )==0 ){
  1614. LOG_ERROR("strftime returned 0!");
  1615. };
  1616. } else {
  1617. struct tm * tmp;
  1618. tmp = gmtime( &timestamp );
  1619. if( tmp == NULL ){
  1620. LOG_ERROR("gmtime Error!");
  1621. };
  1622. if( strftime( tTS, 200, "%FT%T.000Z", tmp )==0 ){
  1623. LOG_ERROR("strftime returned 0!");
  1624. };
  1625. };
  1626. if( nonce == 0 ){
  1627. nonce = (uint32_t) rand();
  1628. };
  1629. unsigned char tBuff[CTEXT];
  1630. int cp = 0;
  1631. int index = 0;
  1632. for( ; index < 4; index++ ) {
  1633. tBuff[cp++] = ((unsigned char *)(&nonce))[index];
  1634. };
  1635. index = 0;
  1636. while( tTS[index] != '\0' ) {
  1637. tBuff[cp++] = (unsigned char) tTS[index];
  1638. index++;
  1639. };
  1640. index = 0;
  1641. while( password[index] != '\0' ) {
  1642. tBuff[cp++] = (unsigned char) password[index];
  1643. index++;
  1644. };
  1645. size_t baseOutLen;
  1646. unsigned char tSHA[SHA_DIGEST_LENGTH];
  1647. SHA1( tBuff, cp, tSHA );
  1648. //tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
  1649. tPDIGEST = base64_encode( tSHA, SHA_DIGEST_LENGTH, &baseOutLen );
  1650. //free( tSHA );
  1651. //$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
  1652. strncpy( RetVal.USERNAME, username, CTEXT );
  1653. strncpy( RetVal.TIMESTAMP, tTS, CTEXT );
  1654. strncpy( RetVal.PASSDIGEST, (const char*)tPDIGEST, CTEXT );
  1655. free( tPDIGEST );
  1656. tPDIGEST = base64_encode( (unsigned char *)&nonce, 4 , &baseOutLen );
  1657. strncpy( RetVal.NONCE, (const char*)tPDIGEST, baseOutLen );
  1658. RetVal.NONCE[baseOutLen] = '\0';
  1659. free( tPDIGEST );
  1660. //$REQ['NONCE']=base64_encode($nonce); // alternative
  1661. return RetVal;
  1662. };
  1663. void *pOnvif::parser( void *obj, void *ptr ){
  1664. pOnvif * ME = (pOnvif*)obj;
  1665. curlRequest * RQ = (curlRequest*)ptr;
  1666. if( ME ) {
  1667. if( ME->m_pLastResponse ) free( ME->m_pLastResponse );
  1668. ME->m_pLastResponse = (CSTR) malloc( RQ->getSize()+1);
  1669. if( ME->m_pLastResponse ) {
  1670. memcpy( ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize() );
  1671. ME->m_pLastResponse[RQ->getSize()] = (char)0;
  1672. #ifdef DEBUG_ONVIF_TRAFFIC
  1673. FILE *tf;
  1674. tf = fopen("Traffic.txt", "a+");
  1675. fprintf( tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  1676. fprintf( tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse );
  1677. fprintf( tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  1678. fclose(tf);
  1679. #endif
  1680. };
  1681. ME->parser_done = true;
  1682. };
  1683. return NULL;
  1684. };
  1685. bool pOnvif::_send_request( CSTR url, CSTR post_string, bool mustReturnData ){
  1686. #ifdef CURL_MANAGER
  1687. bool RetVal = false;
  1688. if( m_pHttpAccessManager ){
  1689. m_mResponseProtector.lock();
  1690. curlRequest *tRC;//application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime"
  1691. tRC = new curlRequest( url, (CSTR)"application/soap+xml; charset=utf-8;" );
  1692. if( tRC != NULL ){
  1693. #ifdef DEBUG_ONVIF_TRAFFIC
  1694. FILE *tf;
  1695. tf = fopen("Traffic.txt", "a+");
  1696. fprintf( tf, "--------------------\r\n");
  1697. fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string) );
  1698. fprintf( tf, ">>>>> %s\n", post_string );
  1699. fprintf( tf, "--------------------\r\n");
  1700. fclose(tf);
  1701. mustReturnData = true; //In Debug Allways Read Return
  1702. #endif
  1703. tRC->setCredentials( m_pUsername, m_pPassword, false );
  1704. tRC->setPOST( post_string, strlen(post_string) );
  1705. if( mustReturnData ) {
  1706. tRC->setHandler( (void*)this, pOnvif::parser, (void*)tRC );
  1707. if( m_pHttpAccessManager->accomplish( tRC ) ){
  1708. parser_done = false;
  1709. uint8_t cnt = 200; // TO = 2 second
  1710. while( !RetVal && cnt-- ){
  1711. RetVal = parser_done;
  1712. SLEEP( 10 );
  1713. };
  1714. };
  1715. } else {
  1716. RetVal = m_pHttpAccessManager->accomplish( tRC );
  1717. };
  1718. };
  1719. m_mResponseProtector.unlock();
  1720. };
  1721. return RetVal;
  1722. #else
  1723. char tBuffer[CTEXT];
  1724. if( m_pCURL == NULL ) {
  1725. if( m_pLastResponse == NULL ){
  1726. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  1727. };
  1728. m_pCURL = curl_easy_init();
  1729. if( m_pProxyHost!= NULL && m_nProxyPort!= 0 ){
  1730. curl_easy_setopt( m_pCURL, CURLOPT_PROXY, m_pProxyHost );
  1731. curl_easy_setopt( m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort );
  1732. if( m_pProxyUsername != NULL && m_pProxyPassword != NULL ){
  1733. sprintf( tBuffer,"%s:%s", m_pProxyUsername, m_pProxyPassword );
  1734. curl_easy_setopt( m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer );
  1735. };
  1736. };
  1737. curl_easy_setopt( m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  1738. curl_easy_setopt( m_pCURL, CURLOPT_TIMEOUT, 2);
  1739. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYPEER, 0 );
  1740. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYHOST, 0 );
  1741. curl_easy_setopt( m_pCURL, CURLOPT_VERBOSE, 0 );
  1742. curl_easy_setopt( m_pCURL, CURLOPT_POST, 1 );
  1743. //curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  1744. //curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  1745. //curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  1746. curl_easy_setopt( m_pCURL, CURLOPT_TCP_FASTOPEN, 1 );
  1747. curl_easy_setopt( m_pCURL, CURLOPT_TCP_NODELAY, 1 );
  1748. //curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  1749. curl_easy_setopt( m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer );
  1750. curl_easy_setopt( m_pCURL, CURLOPT_WRITEDATA, this );
  1751. };
  1752. if( m_pCURL != NULL ){
  1753. curl_easy_setopt( m_pCURL, CURLOPT_URL, url );
  1754. struct curl_slist *sList = NULL;
  1755. //Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  1756. //Connection: Close
  1757. sList = curl_slist_append( sList, "Content-Type: text/xml; charset=utf-8" );
  1758. sprintf( tBuffer, "Content-Length: %d", strlen(post_string) );
  1759. sList = curl_slist_append( sList, tBuffer );
  1760. curl_easy_setopt( m_pCURL, CURLOPT_HTTPHEADER, sList );
  1761. curl_easy_setopt( m_pCURL, CURLOPT_POSTFIELDS, post_string);
  1762. sprintf( tBuffer,"%s:%s", m_pUsername, m_pPassword );
  1763. curl_easy_setopt( m_pCURL, CURLOPT_USERPWD, tBuffer );
  1764. #ifdef DEBUG
  1765. char tErrorBuffer[CURL_ERROR_SIZE];
  1766. curl_easy_setopt( m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer );
  1767. #endif
  1768. m_pLastIndex = 0;
  1769. if( curl_easy_perform( m_pCURL ) ) {
  1770. LOG_ERROR("CURL Failure!");
  1771. #ifdef DEBUG
  1772. LOG_ERROR( tErrorBuffer );
  1773. #endif
  1774. } else {
  1775. m_pLastResponse[m_pLastIndex] = (char)0;
  1776. return true;
  1777. };
  1778. m_pLastIndex = 0;
  1779. };
  1780. return false;
  1781. #endif
  1782. };
  1783. #ifndef CURL_MANAGER
  1784. int pOnvif::_writer( char *data, size_t size, size_t nmemb, pOnvif *Ptr ){
  1785. if( Ptr == NULL ) return 0;
  1786. //TLOG("*");
  1787. memcpy( (Ptr->m_pLastResponse + Ptr->m_pLastIndex), data, size*nmemb );
  1788. Ptr->m_pLastIndex += size*nmemb;
  1789. return size*nmemb;
  1790. };
  1791. #endif
  1792. #undef LOG
  1793. #undef TLOG
  1794. #undef LOG_ERROR