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.

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