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.

1952 lines
91 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 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. LOG("Get Instance m_pHttpAccessManager");
  408. m_pHttpAccessManager = curlRequestsManager::getInstance();
  409. if( m_pHttpAccessManager == NULL ) return false;
  410. #ifdef DEBUG
  411. LOG("CURL MANAGER OK");
  412. #endif
  413. try {
  414. struct tm * tTS;
  415. tTS = core_GetSystemDateAndTime();
  416. if( tTS != NULL ){
  417. #ifdef DEBUG
  418. char tB[128];
  419. sprintf( tB, "TS from dev: %04d-%02d-%02d %02d:%02d:%02d",
  420. tTS->tm_year, tTS->tm_mon, tTS->tm_mday,
  421. tTS->tm_hour, tTS->tm_min, tTS->tm_sec );
  422. LOG( tB );
  423. #endif
  424. // WTF ??!!
  425. tTS->tm_year -= 1900;
  426. tTS->tm_mon -= 1;
  427. time_t tTimeStamp = mktime( tTS );
  428. m_nDeltaTime = time(NULL) - tTimeStamp;
  429. #ifdef DEBUG
  430. sprintf( tB,"TS:[%d] D:[%d]", tTimeStamp, m_nDeltaTime );
  431. LOG(tB);
  432. #endif
  433. free( tTS );
  434. } else return false;
  435. } catch ( exception e ) {
  436. LOG_ERROR("Catched Exception!");
  437. return false;
  438. };
  439. /*
  440. if( !core_GetCapabilities() ){
  441. LOG_ERROR("Error Getting Caps!");
  442. return false;
  443. };
  444. _getOnvifVersion( m_sCapabilities );
  445. // Update some Vars Online!
  446. //preg_match("/^http(.*)onvif\//",$this->mediauri,$matches);
  447. //m_pBaseURI = $matches[0];
  448. if( media_GetVideoSources() ){
  449. if( media_GetProfiles() ) {
  450. //_getActiveSources( m_vVideoSources, m_vVideoProfiles);
  451. return true;
  452. };
  453. };
  454. return false;
  455. */
  456. return true;
  457. };
  458. /*
  459. Public wrappers for a subset of ONVIF primitives
  460. */
  461. struct tm *pOnvif::core_GetSystemDateAndTime( void ) {
  462. struct tm * RetVal = NULL;
  463. char tURL[CTEXT];
  464. if( m_pDeviceURI != NULL ){
  465. sprintf(tURL, m_pDeviceURI );
  466. } else {
  467. if( m_pIpAddress != NULL ){
  468. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  469. } else {
  470. return NULL;
  471. };
  472. };
  473. #ifdef DEBUG_EXTRA
  474. LOG("Before REQ");
  475. #endif
  476. if( _send_request( tURL, PONVIF_PACK_GET_SYS_DTS, true ) ){
  477. XMLResults tParseResults;
  478. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  479. #ifdef DEBUG_EXTRA
  480. LOG("core_GetSystemDateAndTime");
  481. #endif
  482. if( tParseResults.error ){
  483. char tBuffer[CTEXT];
  484. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  485. LOG_ERROR( tBuffer );
  486. } else {
  487. RetVal = ( struct tm * ) malloc( sizeof(struct tm) );
  488. if( RetVal!= NULL ) {
  489. memset( RetVal, 0, sizeof(struct tm) );
  490. XMLNode zNode = response.getChildNode("Body", 0);
  491. if ( !zNode.isEmpty() ) {
  492. XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0);
  493. if ( !fNode.isEmpty() ) {
  494. XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0);
  495. if ( !hNode.isEmpty() ) {
  496. XMLNode tNode = hNode.getChildNode("UTCDateTime", 0);
  497. if( !tNode.isEmpty() ) {
  498. RetVal->tm_isdst = 0;
  499. XMLNode ttNode = tNode.getChildNode( "Time", 0 );
  500. XMLNode dtNode = tNode.getChildNode( "Date", 0 );
  501. {
  502. XMLNode t = ttNode.getChildNode("Hour",0);
  503. RetVal->tm_hour = atoi( t.getText(0) );
  504. }
  505. {
  506. XMLNode t = ttNode.getChildNode("Minute",0);
  507. RetVal->tm_min = atoi( t.getText(0) );
  508. }
  509. {
  510. XMLNode t = ttNode.getChildNode("Second",0);
  511. RetVal->tm_sec = atoi( t.getText(0) );
  512. }
  513. {
  514. XMLNode t = dtNode.getChildNode("Year",0);
  515. RetVal->tm_year = atoi( t.getText(0) );
  516. }
  517. {
  518. XMLNode t = dtNode.getChildNode("Month",0);
  519. RetVal->tm_mon = atoi( t.getText(0) );
  520. }
  521. {
  522. XMLNode t = dtNode.getChildNode("Day",0);
  523. RetVal->tm_mday = atoi( t.getText(0) );
  524. }
  525. } else {
  526. LOG_ERROR("UTCDateTime is Not Found!");
  527. };
  528. } else {
  529. LOG_ERROR("SystemDateAndTime is Not Found!");
  530. };
  531. } else {
  532. LOG_ERROR("GetSystemDateAndTimeResponse is Not Found!");
  533. };
  534. } else {
  535. LOG_ERROR("Body is Not Found!");
  536. LOG( m_pLastResponse );
  537. };
  538. };
  539. return RetVal;
  540. };
  541. } else LOG_ERROR( "GetSystemDateAndTime: Communication error" );
  542. return NULL;
  543. };
  544. // Based on https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl
  545. bool pOnvif::core_GetCapabilities( void ){
  546. char tBuffer[DEFAULT_BUFFER_SIZE];
  547. accessToken tTOKEN;
  548. tTOKEN = _makeToken();
  549. char tURL[CTEXT];
  550. if (m_pDeviceURI != NULL) {
  551. sprintf(tURL, m_pDeviceURI);
  552. } else {
  553. if (m_pIpAddress != NULL) {
  554. sprintf(tURL, "http://%s/onvif/device_service", m_pIpAddress);
  555. } else {
  556. return NULL;
  557. };
  558. };
  559. //char tSTR[CTEXT];
  560. sprintf( tBuffer, PONVIF_PACK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  561. //sprintf( tSTR, PONVIF_PCK_GET_CAPABILITIES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  562. //build_package( tBuffer, PONVIF_ENVELOPE_SIM, tSTR );
  563. if( _send_request( tURL, tBuffer ) ){
  564. XMLResults tParseResults;
  565. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  566. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  567. LOG("RCPT:");
  568. printf(m_pLastResponse);
  569. #endif
  570. if( tParseResults.error != eXMLErrorNone ){
  571. char tBuf[CTEXT];
  572. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  573. LOG_ERROR( tBuf );
  574. } else {
  575. XMLNode zNode = response.getChildNode("Body", 0);
  576. if( zNode.isEmpty() ) return false;
  577. XMLNode tNode = zNode.getChildNode("GetCapabilitiesResponse", 0);
  578. if( !tNode.isEmpty() ) {
  579. XMLNode tCaps = tNode.getChildNode("Capabilities", 0);
  580. if( !tCaps.isEmpty() ){
  581. {
  582. XMLNode t = tCaps.getChildNode("Analytics", 0);
  583. if( !t.isEmpty() ) {
  584. m_sCapabilities.Analytics = (onvifAnalyticsCapabilities *) malloc(sizeof(onvifAnalyticsCapabilities));
  585. if( m_sCapabilities.Analytics != NULL ) {
  586. memset( m_sCapabilities.Analytics, 0, sizeof(onvifAnalyticsCapabilities));
  587. {
  588. XMLNode c = t.getChildNode("XAddr", 0);
  589. sprintf( m_sCapabilities.Analytics->XAddr,"%s", c.getText(0) );
  590. }
  591. {
  592. XMLNode c = t.getChildNode("RuleSupport", 0);
  593. m_sCapabilities.Analytics->RuleSupport = checkBool(c.getText(0));
  594. }
  595. {
  596. XMLNode c = t.getChildNode("AnalyticsModuleSupport", 0);
  597. m_sCapabilities.Analytics->AnalyticsModuleSupport = checkBool(c.getText(0));
  598. }
  599. };
  600. };
  601. }
  602. {
  603. XMLNode t = tCaps.getChildNode("Device", 0);
  604. if( !t.isEmpty() ) {
  605. m_sCapabilities.Device = (onvifDeviceCapabilities *) malloc(sizeof(onvifDeviceCapabilities));
  606. if( m_sCapabilities.Device != NULL ) {
  607. memset( m_sCapabilities.Device, 0, sizeof(onvifDeviceCapabilities));
  608. {
  609. XMLNode c = t.getChildNode("XAddr", 0);
  610. sprintf( m_sCapabilities.Device->XAddr,"%s", c.getText(0) );
  611. }
  612. {
  613. XMLNode c = t.getChildNode("Network", 0);
  614. if( !c.isEmpty() ) {
  615. m_sCapabilities.Device->Network = (onvifNetworkCapabilities *) malloc(sizeof(onvifNetworkCapabilities));
  616. if( m_sCapabilities.Device->Network!= NULL ) {
  617. memset( m_sCapabilities.Device->Network, 0 , sizeof(onvifNetworkCapabilities));
  618. {
  619. XMLNode n = c.getChildNode("IPFilter", 0);
  620. m_sCapabilities.Device->Network->IPFilter = checkBool(n.getText(0));
  621. }
  622. {
  623. XMLNode n = c.getChildNode("ZeroConfiguration", 0);
  624. m_sCapabilities.Device->Network->ZeroConfiguration = checkBool(n.getText(0));
  625. }
  626. {
  627. XMLNode n = c.getChildNode("IPVersion6", 0);
  628. m_sCapabilities.Device->Network->IPVersion6 = checkBool(n.getText(0));
  629. }
  630. {
  631. XMLNode n = c.getChildNode("DynDNS", 0);
  632. m_sCapabilities.Device->Network->DynDNS = checkBool(n.getText(0));
  633. }
  634. {
  635. XMLNode n = c.getChildNode("Extension", 0);
  636. if( !n.isEmpty() ) {
  637. m_sCapabilities.Device->Network->Extension = (onvifNetworkCapabilitiesExtension *) malloc( sizeof(onvifNetworkCapabilitiesExtension));
  638. if( m_sCapabilities.Device->Network->Extension!= NULL ) {
  639. memset(m_sCapabilities.Device->Network->Extension, 0 ,sizeof(onvifNetworkCapabilitiesExtension));
  640. {
  641. XMLNode x = n.getChildNode("Dot11Configuration", 0);
  642. m_sCapabilities.Device->Network->Extension->Dot11Configuration = checkBool(x.getText(0));
  643. }
  644. };
  645. };
  646. }
  647. };
  648. };
  649. }
  650. {
  651. XMLNode c = t.getChildNode("System", 0);
  652. if( !c.isEmpty() ) {
  653. m_sCapabilities.Device->System = (onvifSystemCapabilities*) malloc(sizeof(onvifSystemCapabilities));
  654. if( m_sCapabilities.Device->System != NULL ) {
  655. memset(m_sCapabilities.Device->System, 0, sizeof(onvifSystemCapabilities));
  656. {
  657. XMLNode n = c.getChildNode("DiscoveryResolve", 0);
  658. m_sCapabilities.Device->System->DiscoveryResolve = checkBool(n.getText(0));
  659. }
  660. {
  661. XMLNode n = c.getChildNode("DiscoveryBye", 0);
  662. m_sCapabilities.Device->System->DiscoveryBye = checkBool(n.getText(0));
  663. }
  664. {
  665. XMLNode n = c.getChildNode("RemoteDiscovery", 0);
  666. m_sCapabilities.Device->System->RemoteDiscovery = checkBool(n.getText(0));
  667. }
  668. {
  669. XMLNode n = c.getChildNode("SystemBackup", 0);
  670. m_sCapabilities.Device->System->SystemBackup = checkBool(n.getText(0));
  671. }
  672. {
  673. XMLNode n = c.getChildNode("SystemLogging", 0);
  674. m_sCapabilities.Device->System->SystemLogging = checkBool(n.getText(0));
  675. }
  676. {
  677. XMLNode n = c.getChildNode("FirmwareUpgrade", 0);
  678. m_sCapabilities.Device->System->FirmwareUpgrade = checkBool(n.getText(0));
  679. }
  680. {
  681. XMLNode n = c.getChildNode("SupportedVersions", 0);
  682. if( !n.isEmpty() ) {
  683. m_sCapabilities.Device->System->SupportedVersions = (onvifSupportedVersions*) malloc(sizeof(onvifSupportedVersions));
  684. if( m_sCapabilities.Device->System->SupportedVersions!=NULL) {
  685. memset(m_sCapabilities.Device->System->SupportedVersions, 0, sizeof(onvifSupportedVersions));
  686. {
  687. XMLNode x = n.getChildNode("Major", 0);
  688. m_sCapabilities.Device->System->SupportedVersions->Major = atoi(x.getText(0));
  689. }
  690. {
  691. XMLNode x = n.getChildNode("Minor", 0);
  692. m_sCapabilities.Device->System->SupportedVersions->Minor = atoi(x.getText(0));
  693. }
  694. };
  695. };
  696. }
  697. {
  698. XMLNode n = c.getChildNode("Extension", 0);
  699. if( !n.isEmpty() ) {
  700. m_sCapabilities.Device->System->Extension = (onvifSystemCapabilitiesExtension*) malloc(sizeof(onvifSystemCapabilitiesExtension));
  701. if( m_sCapabilities.Device->System->Extension!=NULL) {
  702. memset(m_sCapabilities.Device->System->Extension, 0, sizeof(onvifSystemCapabilitiesExtension));
  703. {
  704. XMLNode x = n.getChildNode("HttpFirmwareUpgrade", 0);
  705. m_sCapabilities.Device->System->Extension->HttpFirmwareUpgrade = checkBool(x.getText(0));
  706. }
  707. {
  708. XMLNode x = n.getChildNode("HttpSystemBackup", 0);
  709. m_sCapabilities.Device->System->Extension->HttpSystemBackup = checkBool(x.getText(0));
  710. }
  711. {
  712. XMLNode x = n.getChildNode("HttpSystemLogging", 0);
  713. m_sCapabilities.Device->System->Extension->HttpSystemLogging = checkBool(x.getText(0));
  714. }
  715. {
  716. XMLNode x = n.getChildNode("HttpSupportInformation", 0);
  717. m_sCapabilities.Device->System->Extension->HttpSupportInformation = checkBool(x.getText(0));
  718. }
  719. };
  720. };
  721. }
  722. };
  723. };
  724. }
  725. {
  726. XMLNode c = t.getChildNode("IO", 0);
  727. if( !c.isEmpty() ) {
  728. m_sCapabilities.Device->IO = (onvifIOCapabilities*) malloc(sizeof(onvifIOCapabilities));
  729. if(m_sCapabilities.Device->IO != NULL) {
  730. memset(m_sCapabilities.Device->IO, 0, sizeof(onvifIOCapabilities));
  731. {
  732. XMLNode n = c.getChildNode("InputConnectors", 0);
  733. m_sCapabilities.Device->IO->InputConnectors = atoi(n.getText(0));
  734. }
  735. {
  736. XMLNode n = c.getChildNode("RelayOutputs", 0);
  737. m_sCapabilities.Device->IO->RelayOutputs = atoi(n.getText(0));
  738. }
  739. {
  740. XMLNode n = c.getChildNode("Extension", 0);
  741. if( !n.isEmpty() ) {
  742. m_sCapabilities.Device->IO->Extension = (onvifIOCapabilitiesExtension*) malloc(sizeof(onvifIOCapabilitiesExtension));
  743. if(m_sCapabilities.Device->IO->Extension!=NULL) {
  744. memset(m_sCapabilities.Device->IO->Extension, 0, sizeof(onvifIOCapabilitiesExtension));
  745. {
  746. XMLNode x = n.getChildNode("Auxiliary", 0);
  747. m_sCapabilities.Device->IO->Extension->Auxiliary = checkBool(x.getText(0));
  748. }
  749. };
  750. };
  751. }
  752. };
  753. };
  754. }
  755. {
  756. XMLNode c = t.getChildNode("Security", 0);
  757. if( !c.isEmpty() ) {
  758. m_sCapabilities.Device->Security = (onvifSecurityCapabilities*) malloc(sizeof(onvifSecurityCapabilities));
  759. if(m_sCapabilities.Device->Security!=NULL) {
  760. memset(m_sCapabilities.Device->Security, 0, sizeof(onvifSecurityCapabilities));
  761. {
  762. XMLNode n = c.getChildNode("TLS1.1", 0);
  763. m_sCapabilities.Device->Security->TLS1_1 = checkBool(n.getText(0));
  764. }
  765. {
  766. XMLNode n = c.getChildNode("TLS1.2", 0);
  767. m_sCapabilities.Device->Security->TLS1_2 = checkBool(n.getText(0));
  768. }
  769. {
  770. XMLNode n = c.getChildNode("OnboardKeyGeneration", 0);
  771. m_sCapabilities.Device->Security->OnboardKeyGeneration = checkBool(n.getText(0));
  772. }
  773. {
  774. XMLNode n = c.getChildNode("AccessPolicyConfig", 0);
  775. m_sCapabilities.Device->Security->AccessPolicyConfig = checkBool(n.getText(0));
  776. }
  777. {
  778. XMLNode n = c.getChildNode("X.509Token", 0);
  779. m_sCapabilities.Device->Security->X_509Token = checkBool(n.getText(0));
  780. }
  781. {
  782. XMLNode n = c.getChildNode("SAMLToken", 0);
  783. m_sCapabilities.Device->Security->SAMLToken = checkBool(n.getText(0));
  784. }
  785. {
  786. XMLNode n = c.getChildNode("KerberosToken", 0);
  787. m_sCapabilities.Device->Security->KerberosToken = checkBool(n.getText(0));
  788. }
  789. {
  790. XMLNode n = c.getChildNode("RELToken", 0);
  791. m_sCapabilities.Device->Security->RELToken = checkBool(n.getText(0));
  792. }
  793. {
  794. XMLNode n = c.getChildNode("Extension", 0);
  795. if(!n.isEmpty()){
  796. m_sCapabilities.Device->Security->Extension = (onvifSecurityCapabilitiesExtension*) malloc(sizeof(onvifSecurityCapabilitiesExtension));
  797. if(m_sCapabilities.Device->Security->Extension!=NULL) {
  798. memset(m_sCapabilities.Device->Security->Extension, 0, sizeof(onvifSecurityCapabilitiesExtension));
  799. {
  800. XMLNode x = n.getChildNode("TLS1.0", 0);
  801. m_sCapabilities.Device->Security->Extension->TLS1_0 = checkBool(x.getText(0));
  802. }
  803. };
  804. };
  805. }
  806. };
  807. };
  808. }
  809. };
  810. };
  811. }
  812. {
  813. XMLNode t = tCaps.getChildNode("Events", 0);
  814. if( !t.isEmpty() ) {
  815. m_sCapabilities.Events = (onvifEventCapabilities *) malloc(sizeof(onvifEventCapabilities));
  816. if( m_sCapabilities.Events != NULL ) {
  817. memset( m_sCapabilities.Events, 0, sizeof(onvifEventCapabilities));
  818. {
  819. XMLNode c = t.getChildNode("XAddr", 0);
  820. sprintf(m_sCapabilities.Events->XAddr,"%s", c.getText(0));
  821. }
  822. {
  823. XMLNode c = t.getChildNode("WSSubscriptionPolicySupport", 0);
  824. m_sCapabilities.Events->WSSubscriptionPolicySupport = checkBool(c.getText(0));
  825. }
  826. {
  827. XMLNode c = t.getChildNode("WSPullPointSupport", 0);
  828. m_sCapabilities.Events->WSPullPointSupport = checkBool(c.getText(0));
  829. }
  830. {
  831. XMLNode c = t.getChildNode("WSPausableSubscriptionManagerInterfaceSupport", 0);
  832. m_sCapabilities.Events->WSPausableSubscriptionManagerInterfaceSupport = checkBool(c.getText(0));
  833. }
  834. };
  835. };
  836. }
  837. {
  838. XMLNode t = tCaps.getChildNode("Imaging", 0);
  839. if( !t.isEmpty() ) {
  840. m_sCapabilities.Imaging = (onvifImagingCapabilities *) malloc(sizeof(onvifImagingCapabilities));
  841. if( m_sCapabilities.Imaging != NULL ) {
  842. memset( m_sCapabilities.Imaging, 0, sizeof(onvifImagingCapabilities));
  843. {
  844. XMLNode c = t.getChildNode("XAddr", 0);
  845. sprintf(m_sCapabilities.Imaging->XAddr,"%s", c.getText(0));
  846. }
  847. };
  848. };
  849. }
  850. {
  851. XMLNode t = tCaps.getChildNode("Media", 0);
  852. if( !t.isEmpty() ) {
  853. m_sCapabilities.Media = (onvifMediaCapabilities *) malloc(sizeof(onvifMediaCapabilities));
  854. if( m_sCapabilities.Media != NULL ) {
  855. memset( m_sCapabilities.Media, 0, sizeof(onvifMediaCapabilities));
  856. {
  857. XMLNode c = t.getChildNode("XAddr", 0);
  858. sprintf(m_sCapabilities.Media->XAddr,"%s", c.getText(0));
  859. }
  860. {
  861. XMLNode c = t.getChildNode("StreamingCapabilities", 0);
  862. if( !c.isEmpty() ) {
  863. m_sCapabilities.Media->StreamingCapabilities = (onvifRealTimeStreamingCapabilities*) malloc(sizeof(onvifRealTimeStreamingCapabilities));
  864. if(m_sCapabilities.Media->StreamingCapabilities!=NULL) {
  865. memset(m_sCapabilities.Media->StreamingCapabilities, 0, sizeof(onvifRealTimeStreamingCapabilities));
  866. {
  867. XMLNode n = c.getChildNode("RTPMulticast", 0);
  868. m_sCapabilities.Media->StreamingCapabilities->RTPMulticast = checkBool(n.getText(0));
  869. }
  870. {
  871. XMLNode n = c.getChildNode("RTP_TCP", 0);
  872. m_sCapabilities.Media->StreamingCapabilities->RTP_TCP = checkBool(n.getText(0));
  873. }
  874. {
  875. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  876. m_sCapabilities.Media->StreamingCapabilities->RTP_RTSP_TCP = checkBool(n.getText(0));
  877. }
  878. };
  879. };
  880. }
  881. {
  882. XMLNode c = t.getChildNode("Extension", 0);
  883. if( !c.isEmpty() ) {
  884. m_sCapabilities.Media->Extension = (onvifMediaCapabilitiesExtension*) malloc(sizeof(onvifMediaCapabilitiesExtension));
  885. if(m_sCapabilities.Media->Extension!=NULL) {
  886. memset(m_sCapabilities.Media->Extension, 0, sizeof(onvifMediaCapabilitiesExtension));
  887. {
  888. XMLNode n = c.getChildNode("ProfileCapabilities", 0);
  889. if(!n.isEmpty()){
  890. m_sCapabilities.Media->Extension->ProfileCapabilities = (onvifProfileCapabilities*) malloc(sizeof(onvifProfileCapabilities));
  891. if(m_sCapabilities.Media->Extension->ProfileCapabilities!=NULL) {
  892. memset(m_sCapabilities.Media->Extension->ProfileCapabilities, 0, sizeof(onvifProfileCapabilities));
  893. {
  894. XMLNode x = n.getChildNode("MaximumNumberOfProfiles", 0);
  895. m_sCapabilities.Media->Extension->ProfileCapabilities->MaximumNumberOfProfiles = atoi(x.getText(0));
  896. }
  897. };
  898. };
  899. }
  900. };
  901. };
  902. }
  903. };
  904. };
  905. }
  906. {
  907. XMLNode t = tCaps.getChildNode("PTZ", 0);
  908. if( !t.isEmpty() ) {
  909. m_sCapabilities.PTZ = (onvifPTZCapabilities *) malloc(sizeof(onvifPTZCapabilities));
  910. if( m_sCapabilities.PTZ != NULL ) {
  911. memset( m_sCapabilities.PTZ, 0, sizeof(onvifPTZCapabilities));
  912. {
  913. XMLNode c = t.getChildNode("XAddr", 0);
  914. sprintf(m_sCapabilities.PTZ->XAddr,"%s", c.getText(0));
  915. }
  916. };
  917. };
  918. }
  919. {
  920. XMLNode t = tCaps.getChildNode("Extension", 0);
  921. if( !t.isEmpty() ) {
  922. m_sCapabilities.Extension = (onvifCapabilitiesExtension *) malloc(sizeof(onvifCapabilitiesExtension));
  923. if( m_sCapabilities.Extension != NULL ) {
  924. memset( m_sCapabilities.Extension, 0, sizeof(onvifCapabilitiesExtension));
  925. {
  926. XMLNode c = t.getChildNode("DeviceIO", 0);
  927. if( !c.isEmpty() ) {
  928. m_sCapabilities.Extension->DeviceIO = (onvifDeviceIOCapabilities*) malloc(sizeof(onvifDeviceIOCapabilities));
  929. if(m_sCapabilities.Extension->DeviceIO!=NULL) {
  930. memset(m_sCapabilities.Extension->DeviceIO, 0, sizeof(onvifDeviceIOCapabilities));
  931. {
  932. XMLNode n = c.getChildNode("XAddr", 0);
  933. sprintf(m_sCapabilities.Extension->DeviceIO->XAddr, "%s", n.getText(0));
  934. }
  935. {
  936. XMLNode n = c.getChildNode("VideoSources", 0);
  937. m_sCapabilities.Extension->DeviceIO->VideoSources = atoi(n.getText(0));
  938. }
  939. {
  940. XMLNode n = c.getChildNode("VideoOutputs", 0);
  941. m_sCapabilities.Extension->DeviceIO->VideoOutputs = atoi(n.getText(0));
  942. }
  943. {
  944. XMLNode n = c.getChildNode("AudioSources", 0);
  945. m_sCapabilities.Extension->DeviceIO->AudioSources = atoi(n.getText(0));
  946. }
  947. {
  948. XMLNode n = c.getChildNode("AudioOutputs", 0);
  949. m_sCapabilities.Extension->DeviceIO->AudioOutputs = atoi(n.getText(0));
  950. }
  951. {
  952. XMLNode n = c.getChildNode("RelayOutputs", 0);
  953. m_sCapabilities.Extension->DeviceIO->RelayOutputs = atoi(n.getText(0));
  954. }
  955. };
  956. };
  957. }
  958. {
  959. XMLNode c = t.getChildNode("Display", 0);
  960. if( !c.isEmpty() ) {
  961. m_sCapabilities.Extension->Display = (onvifDisplayCapabilities*) malloc(sizeof(onvifDisplayCapabilities));
  962. if(m_sCapabilities.Extension->Display!=NULL) {
  963. memset(m_sCapabilities.Extension->Display, 0, sizeof(onvifDisplayCapabilities));
  964. {
  965. XMLNode n = c.getChildNode("XAddr", 0);
  966. sprintf(m_sCapabilities.Extension->Display->XAddr, "%s", n.getText(0));
  967. }
  968. {
  969. XMLNode n = c.getChildNode("FixedLayout", 0);
  970. m_sCapabilities.Extension->Display->FixedLayout = checkBool(n.getText(0));
  971. }
  972. };
  973. };
  974. }
  975. {
  976. XMLNode c = t.getChildNode("Recording", 0);
  977. if( !c.isEmpty() ) {
  978. m_sCapabilities.Extension->Recording = (onvifRecordingCapabilities*) malloc(sizeof(onvifRecordingCapabilities));
  979. if(m_sCapabilities.Extension->Recording!=NULL) {
  980. memset(m_sCapabilities.Extension->Recording, 0, sizeof(onvifRecordingCapabilities));
  981. {
  982. XMLNode n = c.getChildNode("XAddr", 0);
  983. sprintf(m_sCapabilities.Extension->Recording->XAddr, "%s", n.getText(0));
  984. }
  985. {
  986. XMLNode n = c.getChildNode("ReceiverSource", 0);
  987. m_sCapabilities.Extension->Recording->ReceiverSource = checkBool(n.getText(0));
  988. }
  989. {
  990. XMLNode n = c.getChildNode("MediaProfileSource", 0);
  991. m_sCapabilities.Extension->Recording->MediaProfileSource = checkBool(n.getText(0));
  992. }
  993. {
  994. XMLNode n = c.getChildNode("DynamicRecordings", 0);
  995. m_sCapabilities.Extension->Recording->DynamicRecordings = checkBool(n.getText(0));
  996. }
  997. {
  998. XMLNode n = c.getChildNode("DynamicTracks", 0);
  999. m_sCapabilities.Extension->Recording->DynamicTracks = checkBool(n.getText(0));
  1000. }
  1001. {
  1002. XMLNode n = c.getChildNode("MaxStringLength", 0);
  1003. m_sCapabilities.Extension->Recording->MaxStringLength = atoi(n.getText(0));
  1004. }
  1005. };
  1006. };
  1007. }
  1008. {
  1009. XMLNode c = t.getChildNode("Search", 0);
  1010. if( !c.isEmpty() ) {
  1011. m_sCapabilities.Extension->Search = (onvifSearchCapabilities*) malloc(sizeof(onvifSearchCapabilities));
  1012. if(m_sCapabilities.Extension->Search!=NULL) {
  1013. memset(m_sCapabilities.Extension->Search, 0, sizeof(onvifSearchCapabilities));
  1014. {
  1015. XMLNode n = c.getChildNode("XAddr", 0);
  1016. sprintf(m_sCapabilities.Extension->Search->XAddr, "%s", n.getText(0));
  1017. }
  1018. {
  1019. XMLNode n = c.getChildNode("MetadataSearch", 0);
  1020. m_sCapabilities.Extension->Search->MetadataSearch = checkBool(n.getText(0));
  1021. }
  1022. };
  1023. };
  1024. }
  1025. {
  1026. XMLNode c = t.getChildNode("Replay", 0);
  1027. if( !c.isEmpty() ) {
  1028. m_sCapabilities.Extension->Replay = (onvifReplayCapabilities*) malloc(sizeof(onvifReplayCapabilities));
  1029. if(m_sCapabilities.Extension->Replay!=NULL) {
  1030. memset(m_sCapabilities.Extension->Replay, 0, sizeof(onvifReplayCapabilities));
  1031. {
  1032. XMLNode n = c.getChildNode("XAddr", 0);
  1033. sprintf(m_sCapabilities.Extension->Replay->XAddr, "%s", n.getText(0));
  1034. }
  1035. };
  1036. };
  1037. }
  1038. {
  1039. XMLNode c = t.getChildNode("Receiver", 0);
  1040. if( !c.isEmpty() ) {
  1041. m_sCapabilities.Extension->Receiver = (onvifReceiverCapabilities*) malloc(sizeof(onvifReceiverCapabilities));
  1042. if(m_sCapabilities.Extension->Receiver!=NULL) {
  1043. memset(m_sCapabilities.Extension->Receiver, 0, sizeof(onvifReceiverCapabilities));
  1044. {
  1045. XMLNode n = c.getChildNode("XAddr", 0);
  1046. sprintf(m_sCapabilities.Extension->Receiver->XAddr, "%s", n.getText(0));
  1047. }
  1048. {
  1049. XMLNode n = c.getChildNode("RTP_Multicast", 0);
  1050. m_sCapabilities.Extension->Receiver->RTP_Multicast = checkBool(n.getText(0));
  1051. }
  1052. {
  1053. XMLNode n = c.getChildNode("RTP_TCP", 0);
  1054. m_sCapabilities.Extension->Receiver->RTP_TCP = checkBool(n.getText(0));
  1055. }
  1056. {
  1057. XMLNode n = c.getChildNode("RTP_RTSP_TCP", 0);
  1058. m_sCapabilities.Extension->Receiver->RTP_RTSP_TCP = checkBool(n.getText(0));
  1059. }
  1060. {
  1061. XMLNode n = c.getChildNode("SupportedReceivers", 0);
  1062. m_sCapabilities.Extension->Receiver->SupportedReceivers = atoi(n.getText(0));
  1063. }
  1064. {
  1065. XMLNode n = c.getChildNode("MaximumRTSPURILength", 0);
  1066. m_sCapabilities.Extension->Receiver->MaximumRTSPURILength = atoi(n.getText(0));
  1067. }
  1068. };
  1069. };
  1070. }
  1071. {
  1072. XMLNode c = t.getChildNode("AnalyticsDevice", 0);
  1073. if( !c.isEmpty() ) {
  1074. m_sCapabilities.Extension->AnalyticsDevice = (onvifAnalyticsDeviceCapabilities*) malloc(sizeof(onvifAnalyticsDeviceCapabilities));
  1075. if(m_sCapabilities.Extension->AnalyticsDevice!=NULL) {
  1076. memset(m_sCapabilities.Extension->AnalyticsDevice, 0, sizeof(onvifAnalyticsDeviceCapabilities));
  1077. {
  1078. XMLNode n = c.getChildNode("XAddr", 0);
  1079. sprintf(m_sCapabilities.Extension->AnalyticsDevice->XAddr, "%s", n.getText(0));
  1080. }
  1081. {
  1082. XMLNode n = c.getChildNode("RuleSupport", 0);
  1083. m_sCapabilities.Extension->AnalyticsDevice->RuleSupport = checkBool(n.getText(0));
  1084. }
  1085. };
  1086. };
  1087. }
  1088. };
  1089. };
  1090. }
  1091. return true;
  1092. } else {
  1093. LOG("Capabilities is Empty!");
  1094. };
  1095. } else {
  1096. LOG("GetCapabilitiesResponse is Empty!");
  1097. };
  1098. };
  1099. }
  1100. return false;
  1101. };
  1102. // based on https://www.onvif.org/ver20/imaging/wsdl/imaging.wsdl
  1103. bool pOnvif::media_GetVideoSources( void ){
  1104. if( m_pMediaURI==NULL ) return false;
  1105. char tBuffer[DEFAULT_BUFFER_SIZE];
  1106. accessToken tTOKEN;
  1107. tTOKEN = _makeToken();
  1108. sprintf( tBuffer, PONVIF_PACK_GET_VIDEO_SOURCES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1109. if( _send_request( m_pMediaURI, tBuffer ) ){
  1110. XMLResults tParseResults;
  1111. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1112. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1113. LOG("RCPT:");
  1114. printf(m_pLastResponse);
  1115. #endif
  1116. if( tParseResults.error ){
  1117. char tBuf[CTEXT];
  1118. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1119. LOG_ERROR( tBuf );
  1120. } else {
  1121. XMLNode zNode = response.getChildNode("Body", 0);
  1122. if( zNode.isEmpty() ) return false;
  1123. XMLNode tNode = zNode.getChildNode("GetVideoSourcesResponse", 0);
  1124. if( !tNode.isEmpty() ) {
  1125. for( int index = 0; index < tNode.nChildNode(); index++ ){
  1126. XMLNode cNode = tNode.getChildNode("VideoSources", index );
  1127. if(!cNode.isEmpty()) {
  1128. videoSource *tPtr;
  1129. tPtr = (videoSource*) malloc(sizeof(videoSource));
  1130. if( tPtr != NULL ) {
  1131. sprintf( tPtr->token, "%s", cNode.getAttribute("token",0));
  1132. {
  1133. XMLNode nNode = cNode.getChildNode("Framerate", 0 );
  1134. tPtr->Framerate = (float)atof( nNode.getText(0));
  1135. }
  1136. {
  1137. XMLNode nNode = cNode.getChildNode("Resolution", 0 );
  1138. if( !nNode.isEmpty() ) {
  1139. {
  1140. XMLNode xNode = nNode.getChildNode("Width", 0 );
  1141. tPtr->Resolution.Width = atoi(xNode.getText(0));
  1142. }
  1143. {
  1144. XMLNode xNode = nNode.getChildNode("Height", 0 );
  1145. tPtr->Resolution.Height = atoi(xNode.getText(0));
  1146. }
  1147. };
  1148. }
  1149. {
  1150. XMLNode nNode = cNode.getChildNode("Imaging", 0 );
  1151. if( !nNode.isEmpty() ) {
  1152. tPtr->Imaging = (onvifImagingSettings*) malloc(sizeof(onvifImagingSettings));
  1153. if( tPtr->Imaging != NULL) {
  1154. memset(tPtr->Imaging, 0, sizeof(onvifImagingSettings));
  1155. {
  1156. XMLNode xNode = nNode.getChildNode("BacklightCompensation", 0 );
  1157. if(!xNode.isEmpty()) {
  1158. tPtr->Imaging->BacklightCompensation = (onvifBacklightCompensation*)malloc(sizeof(onvifBacklightCompensation));
  1159. if(tPtr->Imaging->BacklightCompensation!=NULL) {
  1160. memset(tPtr->Imaging->BacklightCompensation, 0, sizeof(onvifBacklightCompensation));
  1161. {
  1162. XMLNode rNode = xNode.getChildNode("Mode", 0 );
  1163. tPtr->Imaging->BacklightCompensation->Mode = checkOnOff(rNode.getText(0));
  1164. }
  1165. {
  1166. XMLNode rNode = xNode.getChildNode("Level", 0 );
  1167. tPtr->Imaging->BacklightCompensation->Level = (float)atof(rNode.getText(0));
  1168. }
  1169. };
  1170. };
  1171. }
  1172. {
  1173. XMLNode xNode = nNode.getChildNode("Brightness", 0 );
  1174. tPtr->Imaging->Brightness = (float)atoi(xNode.getText(0));
  1175. }
  1176. {
  1177. XMLNode xNode = nNode.getChildNode("ColorSaturation", 0 );
  1178. tPtr->Imaging->ColorSaturation = (float)atoi(xNode.getText(0));
  1179. }
  1180. {
  1181. XMLNode xNode = nNode.getChildNode("Contrast", 0 );
  1182. tPtr->Imaging->Contrast = (float)atoi(xNode.getText(0));
  1183. }
  1184. {
  1185. XMLNode xNode = nNode.getChildNode("Sharpness", 0 );
  1186. tPtr->Imaging->Sharpness = (float)atoi(xNode.getText(0));
  1187. }
  1188. {
  1189. XMLNode xNode = nNode.getChildNode("IrCutFilter", 0 );
  1190. tPtr->Imaging->IrCutFilter = checkOnOffAuto(xNode.getText(0));
  1191. }
  1192. };
  1193. };
  1194. }
  1195. m_vVideoSources.push_back( tPtr );
  1196. };
  1197. };
  1198. };
  1199. return true;
  1200. } else {
  1201. LOG_ERROR("GetVideoSourcesResponse EMPTY!");
  1202. };
  1203. };
  1204. } else {
  1205. LOG_ERROR( "_send_request Failure!" );
  1206. };
  1207. return false;
  1208. };
  1209. bool pOnvif::media_GetProfiles( void ){
  1210. char tBuffer[DEFAULT_BUFFER_SIZE];
  1211. accessToken tTOKEN;
  1212. tTOKEN = _makeToken();
  1213. sprintf( tBuffer, PONVIF_PACK_GET_PROFILES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1214. if( _send_request( m_pMediaURI, tBuffer ) ){
  1215. XMLResults tParseResults;
  1216. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1217. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  1218. LOG("RCPT:");
  1219. printf(m_pLastResponse);
  1220. #endif
  1221. if( tParseResults.error ){
  1222. char tBuf[CTEXT];
  1223. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1224. LOG_ERROR( tBuf );
  1225. } else {
  1226. XMLNode zNode = response.getChildNode("Body", 0);
  1227. if( zNode.isEmpty() ) return false;
  1228. XMLNode tNode = zNode.getChildNode("GetProfilesResponse", 0);
  1229. if( !tNode.isEmpty() ) {
  1230. for( int index = 0; index < tNode.nChildNode(); index++ ){
  1231. XMLNode cNode = tNode.getChildNode("Profiles", index );
  1232. if(!cNode.isEmpty()) {
  1233. videoSourceProfile *tPtr;
  1234. tPtr = (videoSourceProfile*) malloc(sizeof(videoSourceProfile));
  1235. if( tPtr != NULL ) {
  1236. memset(tPtr, 0, sizeof(videoSourceProfile));
  1237. sprintf(tPtr->profiletoken, "%s", cNode.getAttribute("token",0));
  1238. {
  1239. XMLNode nNode = cNode.getChildNode("Name", 0 );
  1240. sprintf(tPtr->profilename, "%s", nNode.getText(0));
  1241. }
  1242. {
  1243. XMLNode nNode = cNode.getChildNode("VideoSourceConfiguration", 0 );
  1244. if(!nNode.isEmpty()) {
  1245. XMLNode zNode = nNode.getChildNode("SourceToken", 0 );
  1246. sprintf(tPtr->sourcetoken, "%s", zNode.getText(0));
  1247. };
  1248. }
  1249. {
  1250. XMLNode nNode = cNode.getChildNode("VideoEncoderConfiguration", 0 );
  1251. if(!nNode.isEmpty()) {
  1252. {
  1253. XMLNode zNode = nNode.getChildNode("Encoding", 0 );
  1254. sprintf(tPtr->encoding, "%s", zNode.getText(0));
  1255. }
  1256. {
  1257. XMLNode zNode = nNode.getChildNode("Resolution", 0 );
  1258. {
  1259. XMLNode jNode = zNode.getChildNode("Width", 0 );
  1260. tPtr->width = atoi(jNode.getText(0));
  1261. }
  1262. {
  1263. XMLNode jNode = zNode.getChildNode("Height", 0 );
  1264. tPtr->height = atoi(jNode.getText(0));
  1265. }
  1266. }
  1267. {
  1268. XMLNode zNode = nNode.getChildNode("RateControl", 0 );
  1269. {
  1270. XMLNode jNode = zNode.getChildNode("FrameRateLimit", 0 );
  1271. tPtr->fps = atoi(jNode.getText(0));
  1272. }
  1273. {
  1274. XMLNode jNode = zNode.getChildNode("BitrateLimit", 0 );
  1275. tPtr->bitrate = atoi(jNode.getText(0));
  1276. }
  1277. }
  1278. };
  1279. }
  1280. m_vVideoProfiles.push_back(tPtr);
  1281. };
  1282. };
  1283. };
  1284. return true;
  1285. };
  1286. };
  1287. };
  1288. return false;
  1289. };
  1290. bool pOnvif::media_GetServices( void ){
  1291. char tBuffer[DEFAULT_BUFFER_SIZE];
  1292. accessToken tTOKEN;
  1293. tTOKEN = _makeToken();
  1294. sprintf( tBuffer, PONVIF_PACK_GET_SERVICES, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1295. if( _send_request( m_pMediaURI, tBuffer ) ){
  1296. XMLResults tParseResults;
  1297. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1298. LOG("RCPT:");
  1299. printf(m_pLastResponse);
  1300. if( tParseResults.error ){
  1301. char tBuf[CTEXT];
  1302. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1303. LOG_ERROR( tBuf );
  1304. } else {
  1305. };
  1306. };
  1307. return false;
  1308. };
  1309. bool pOnvif::core_GetDeviceInformation() {
  1310. char tBuffer[DEFAULT_BUFFER_SIZE];
  1311. accessToken tTOKEN;
  1312. tTOKEN = _makeToken();
  1313. sprintf( tBuffer, PONVIF_PACK_GET_DEV_INFO, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP );
  1314. if( _send_request( m_pMediaURI, tBuffer ) ){
  1315. XMLResults tParseResults;
  1316. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1317. LOG("RCPT:");
  1318. printf(m_pLastResponse);
  1319. if( tParseResults.error ){
  1320. char tBuf[CTEXT];
  1321. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1322. LOG_ERROR( tBuf );
  1323. } else {
  1324. //return $response['Envelope']['Body']['GetDeviceInformationResponse'];
  1325. };
  1326. };
  1327. return false;
  1328. };
  1329. CSTR pOnvif::media_GetStreamUri( CSTR profileToken, CSTR stream, CSTR protocol ){
  1330. char tBuffer[DEFAULT_BUFFER_SIZE];
  1331. accessToken tTOKEN;
  1332. tTOKEN = _makeToken();
  1333. sprintf( tBuffer, PONVIF_PACK_GET_STREAM_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, stream, protocol );
  1334. if( _send_request( m_pMediaURI, tBuffer ) ){
  1335. XMLResults tParseResults;
  1336. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1337. LOG("RCPT:");
  1338. printf(m_pLastResponse);
  1339. if( tParseResults.error ){
  1340. char tBuf[CTEXT];
  1341. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1342. LOG_ERROR( tBuf );
  1343. } else {
  1344. // Parse
  1345. //return $response['Envelope']['Body']['GetStreamUriResponse']['MediaUri']['Uri'];
  1346. };
  1347. };
  1348. return NULL;
  1349. };
  1350. CSTR pOnvif::media_GetSnapshotUri( CSTR profileToken ){
  1351. char tBuffer[DEFAULT_BUFFER_SIZE];
  1352. accessToken tTOKEN;
  1353. tTOKEN = _makeToken();
  1354. sprintf( tBuffer, PONVIF_PACK_GET_PICTURE_URI, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1355. if( _send_request( m_pMediaURI, tBuffer ) ){
  1356. XMLResults tParseResults;
  1357. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1358. LOG("RCPT:");
  1359. printf(m_pLastResponse);
  1360. if( tParseResults.error ){
  1361. char tBuf[CTEXT];
  1362. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1363. LOG_ERROR( tBuf );
  1364. } else {
  1365. //Parse
  1366. //return $response['Envelope']['Body']['GetSnapshotUriResponse']['MediaUri']['Uri'];
  1367. };
  1368. };
  1369. return NULL;
  1370. };
  1371. bool pOnvif::ptz_GetStatus( CSTR profileToken, ptzStatus *tStatus ){
  1372. if( m_pPTZURI==NULL ) return false;
  1373. if( tStatus == NULL ) return false;
  1374. char tBuffer[DEFAULT_BUFFER_SIZE];
  1375. accessToken tTOKEN;
  1376. tTOKEN = _makeToken();
  1377. sprintf( tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1378. if( _send_request( m_pPTZURI, tBuffer, true ) ){
  1379. XMLResults tParseResults;
  1380. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1381. if( tParseResults.error ){
  1382. char tBuf[CTEXT];
  1383. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1384. LOG_ERROR( tBuf );
  1385. } else {
  1386. XMLNode aNode = response.getChildNode("Body", 0);
  1387. if( aNode.isEmpty() ) return NULL;
  1388. XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0);
  1389. if( !bNode.isEmpty() ) {
  1390. XMLNode cNode = bNode.getChildNode("PTZStatus", 0 );
  1391. if(!cNode.isEmpty()) {
  1392. memset( tStatus, 0, sizeof(ptzStatus));
  1393. XMLNode dNode = cNode.getChildNode("Position", 0 );
  1394. if(!dNode.isEmpty()) {
  1395. XMLNode eNode = dNode.getChildNode("PanTilt", 0 );
  1396. if( !eNode.isEmpty()) {
  1397. //XMLAttribute ZZ = eNode.getAttribute(1);
  1398. //LOG( ZZ.lpszName );
  1399. tStatus->pan = atof( eNode.getAttribute("x") ); //ZZ.lpszValue );
  1400. //ZZ = eNode.getAttribute(2);
  1401. //LOG( ZZ.lpszName );
  1402. tStatus->tilt = atof( eNode.getAttribute("y") );// ZZ.lpszValue );
  1403. };
  1404. XMLNode fNode = dNode.getChildNode("Zoom", 0 );
  1405. if( !fNode.isEmpty()) {
  1406. //XMLAttribute ZZ = eNode.getAttribute(1);
  1407. //LOG( ZZ.lpszName );
  1408. tStatus->zoom = atof( fNode.getAttribute("x") ); //ZZ.lpszValue );
  1409. };
  1410. };
  1411. XMLNode gNode = cNode.getChildNode("MoveStatus", 0 );
  1412. if(!gNode.isEmpty()){
  1413. tStatus->hasMoveStatus = true;
  1414. XMLNode hNode = gNode.getChildNode("PanTilt", 0 );
  1415. if( !hNode.isEmpty()) {
  1416. if( strstr( hNode.getText(0),"IDLE" ) != NULL ) tStatus->PanTiltIdle = true;
  1417. };
  1418. XMLNode iNode = gNode.getChildNode("Zoom", 0 );
  1419. if( !iNode.isEmpty()) {
  1420. if( strstr( iNode.getText(0),"IDLE" ) != NULL ) tStatus->ZoomIdle = true;
  1421. };
  1422. };
  1423. return true;
  1424. };
  1425. };
  1426. };
  1427. };
  1428. return false;
  1429. };
  1430. bool pOnvif::ptz_GetPresets( CSTR profileToken ){
  1431. if( m_pPTZURI==NULL ) return false;
  1432. char tBuffer[DEFAULT_BUFFER_SIZE];
  1433. accessToken tTOKEN;
  1434. tTOKEN = _makeToken();
  1435. sprintf( tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );
  1436. if( _send_request( m_pPTZURI, tBuffer ) ){
  1437. XMLResults tParseResults;
  1438. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1439. LOG("RCPT:");
  1440. printf(m_pLastResponse);
  1441. if( tParseResults.error ){
  1442. char tBuf[CTEXT];
  1443. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1444. LOG_ERROR( tBuf );
  1445. } else {
  1446. //$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
  1447. //$presets=array();
  1448. //foreach ($getpresetsresponse as $preset) {
  1449. // $presets[]=array('Token'=>$preset['@attributes']['token'],
  1450. // 'Name'=>$preset['Name'],
  1451. // 'PTZPosition'=>$preset['PTZPosition']);
  1452. //}
  1453. //return $presets;
  1454. };
  1455. };
  1456. return false;
  1457. };
  1458. bool pOnvif::ptz_GetNode( CSTR ptzNodeToken ) {
  1459. if( m_pPTZURI == NULL ) return false;
  1460. char tBuffer[DEFAULT_BUFFER_SIZE];
  1461. accessToken tTOKEN;
  1462. tTOKEN = _makeToken();
  1463. sprintf( tBuffer, PONVIF_PACK_GET_PTZ_NODE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, ptzNodeToken );
  1464. if( _send_request( m_pPTZURI, tBuffer ) ){
  1465. XMLResults tParseResults;
  1466. XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  1467. LOG("RCPT:");
  1468. printf(m_pLastResponse);
  1469. if( tParseResults.error ){
  1470. char tBuf[CTEXT];
  1471. sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  1472. LOG_ERROR( tBuf );
  1473. } else {
  1474. //return $response['Envelope']['Body']['GetNodeResponse'];
  1475. };
  1476. };
  1477. return false;
  1478. };
  1479. bool pOnvif::ptz_GotoPreset( CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x ){
  1480. if( m_pPTZURI == NULL ) return false;
  1481. char tBuffer[DEFAULT_BUFFER_SIZE];
  1482. accessToken tTOKEN;
  1483. tTOKEN = _makeToken();
  1484. 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 );
  1485. return _send_request( m_pPTZURI, tBuffer, true );
  1486. };
  1487. bool pOnvif::ptz_RemovePreset( CSTR profileToken, CSTR presetToken ){
  1488. if( m_pPTZURI == NULL ) return false;
  1489. char tBuffer[DEFAULT_BUFFER_SIZE];
  1490. accessToken tTOKEN;
  1491. tTOKEN = _makeToken();
  1492. sprintf( tBuffer, PONVIF_PACK_REMOVE_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken );
  1493. return _send_request( m_pPTZURI, tBuffer, true );
  1494. };
  1495. bool pOnvif::ptz_SetPreset( CSTR profileToken, CSTR presetName ){
  1496. if( m_pPTZURI == NULL ) return false;
  1497. char tBuffer[DEFAULT_BUFFER_SIZE];
  1498. accessToken tTOKEN;
  1499. tTOKEN = _makeToken();
  1500. sprintf( tBuffer, PONVIF_PACK_SET_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetName );
  1501. return _send_request( m_pPTZURI, tBuffer, true );
  1502. };
  1503. bool pOnvif::ptz_RelativeMove( CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y ){
  1504. if( m_pPTZURI == NULL ) return false;
  1505. char tBuffer[DEFAULT_BUFFER_SIZE];
  1506. accessToken tTOKEN;
  1507. tTOKEN = _makeToken();
  1508. 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 );
  1509. return _send_request( m_pPTZURI, tBuffer, true );
  1510. };
  1511. bool pOnvif::ptz_RelativeMoveZoom( CSTR profileToken, float zoom, float speedZoom ){
  1512. if( m_pPTZURI == NULL ) return false;
  1513. char tBuffer[DEFAULT_BUFFER_SIZE];
  1514. accessToken tTOKEN;
  1515. tTOKEN = _makeToken();
  1516. sprintf( tBuffer, PONVIF_PACK_RELATIVE_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom, speedZoom );
  1517. return _send_request( m_pPTZURI, tBuffer, true );
  1518. };
  1519. bool pOnvif::ptz_AbsoluteMove( CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom ){
  1520. if( m_pPTZURI == NULL ) return false;
  1521. char tBuffer[DEFAULT_BUFFER_SIZE];
  1522. accessToken tTOKEN;
  1523. tTOKEN = _makeToken();
  1524. sprintf( tBuffer, PONVIF_PACK_ABSOLUTE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, position_pantilt_x, position_pantilt_y, zoom );
  1525. return _send_request( m_pPTZURI, tBuffer, false );
  1526. };
  1527. bool pOnvif::ptz_ContinuousMove( CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y ){
  1528. if( m_pPTZURI == NULL ) return false;
  1529. char tBuffer[DEFAULT_BUFFER_SIZE];
  1530. accessToken tTOKEN;
  1531. tTOKEN = _makeToken();
  1532. sprintf( tBuffer, PONVIF_PACK_CONTINUOUS_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, velocity_pantilt_x, velocity_pantilt_y );
  1533. return _send_request( m_pPTZURI, tBuffer, true );
  1534. };
  1535. bool pOnvif::ptz_ContinuousMoveZoom( CSTR profileToken, float zoom ){
  1536. if( m_pPTZURI == NULL ) return false;
  1537. char tBuffer[DEFAULT_BUFFER_SIZE];
  1538. accessToken tTOKEN;
  1539. tTOKEN = _makeToken();
  1540. sprintf( tBuffer, PONVIF_PACK_CONTINUOUS_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom );
  1541. return _send_request( m_pPTZURI, tBuffer, true );
  1542. };
  1543. bool pOnvif::ptz_Stop( CSTR profileToken, bool pantilt, bool zoom ){
  1544. if( m_pPTZURI == NULL ) return false;
  1545. char tBuffer[DEFAULT_BUFFER_SIZE];
  1546. accessToken tTOKEN;
  1547. tTOKEN = _makeToken();
  1548. sprintf( tBuffer, PONVIF_PACK_PTZ_STOP, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken );//, pantilt, zoom );
  1549. return _send_request( m_pPTZURI, tBuffer, true );
  1550. };
  1551. /*
  1552. Internal functions
  1553. */
  1554. accessToken pOnvif::_makeToken( void ){
  1555. time_t tTS = time( NULL ) - m_nDeltaTime;
  1556. return _passwordDigest( m_pUsername, m_pPassword, tTS );
  1557. };
  1558. void pOnvif::_getOnvifVersion( nodeCapability &caps ){
  1559. if( caps.Device != NULL ) {
  1560. if( caps.Device->System != NULL ) {
  1561. if( caps.Device->System->SupportedVersions != NULL ) {
  1562. m_sOnvifVersion.Major = caps.Device->System->SupportedVersions->Major;
  1563. m_sOnvifVersion.Minor = caps.Device->System->SupportedVersions->Minor;
  1564. };
  1565. };
  1566. };
  1567. if( caps.PTZ != NULL ) {
  1568. m_pPTZURI = caps.PTZ->XAddr;
  1569. };
  1570. if( caps.Media != NULL ) {
  1571. m_pMediaURI = caps.Media->XAddr;
  1572. };
  1573. if( caps.Device != NULL ) {
  1574. m_pDeviceURI = caps.Device->XAddr;
  1575. };
  1576. };
  1577. /*
  1578. protected function _getActiveSources($videoSources,$profiles) {
  1579. $sources=array();
  1580. if (isset($videoSources['@attributes'])) {
  1581. // NVT is a camera
  1582. $sources[0]['sourcetoken']=$videoSources['@attributes']['token'];
  1583. $this->_getProfileData($sources,0,$profiles);
  1584. }
  1585. else {
  1586. // NVT is an encoder
  1587. for ($i=0;$i<count($videoSources);$i++) {
  1588. if (strtolower($videoSources[$i]['@attributes']['SignalActive'])=='true') {
  1589. $sources[$i]['sourcetoken']=$videoSources[$i]['@attributes']['token'];
  1590. $this->_getProfileData($sources,$i,$profiles);
  1591. }
  1592. } // for
  1593. }
  1594. return $sources;
  1595. }
  1596. void pOnvif::_getProfileData( sources,$i, XMLNode profiles ){
  1597. //$inprofile=0;
  1598. videoSourceProfile *tPtr;
  1599. for( $y=0; $y < count($profiles); $y++ ){
  1600. if ($profiles[$y]['VideoSourceConfiguration']['SourceToken']==$sources[$i]['sourcetoken']) {
  1601. $sources[$i][$inprofile]['profilename']=$profiles[$y]['Name'];
  1602. $sources[$i][$inprofile]['profiletoken']=$profiles[$y]['@attributes']['token'];
  1603. if ( isset($profiles[$y]['VideoEncoderConfiguration'])) {
  1604. $sources[$i][$inprofile]['encoding']=$profiles[$y]['VideoEncoderConfiguration']['Encoding'];
  1605. $sources[$i][$inprofile]['width']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Width'];
  1606. $sources[$i][$inprofile]['height']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Height'];
  1607. $sources[$i][$inprofile]['fps']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['FrameRateLimit'];
  1608. $sources[$i][$inprofile]['bitrate']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['BitrateLimit'];
  1609. }
  1610. if ( isset($profiles[$y]['PTZConfiguration'])) {
  1611. $sources[$i][$inprofile]['ptz']['name']=$profiles[$y]['PTZConfiguration']['Name'];
  1612. $sources[$i][$inprofile]['ptz']['nodetoken']=$profiles[$y]['PTZConfiguration']['NodeToken'];
  1613. }
  1614. $inprofile++;
  1615. }
  1616. }
  1617. };
  1618. */
  1619. /*
  1620. protected function _xml2array($response) {
  1621. $sxe = new SimpleXMLElement($response);
  1622. $dom_sxe = dom_import_simplexml($sxe);
  1623. $dom = new DOMDocument('1.0');
  1624. $dom_sxe = $dom->importNode($dom_sxe, true);
  1625. $dom_sxe = $dom->appendChild($dom_sxe);
  1626. $element = $dom->childNodes->item(0);
  1627. foreach ($sxe->getDocNamespaces() as $name => $uri) {
  1628. $element->removeAttributeNS($uri, $name);
  1629. }
  1630. $xmldata=$dom->saveXML();
  1631. $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>"));
  1632. $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>"));
  1633. $xml=simplexml_load_string($xmldata);
  1634. $data=json_decode(json_encode((array)$xml),1);
  1635. $data=array($xml->getName()=>$data);
  1636. return $data;
  1637. }
  1638. */
  1639. accessToken pOnvif::_passwordDigest( CSTR username, CSTR password, time_t timestamp, uint32_t nonce ) {
  1640. accessToken RetVal;
  1641. char tTS[CTEXT];
  1642. unsigned char * tPDIGEST;
  1643. if( timestamp == 0 ){
  1644. time_t t;
  1645. struct tm * tmp;
  1646. t = time( NULL );
  1647. tmp = gmtime( &t );
  1648. if( tmp == NULL ){
  1649. LOG_ERROR("gmtime Error!");
  1650. };
  1651. if( strftime( tTS, 200, "%FT%TZ", tmp )==0 ){
  1652. LOG_ERROR("strftime returned 0!");
  1653. };
  1654. } else {
  1655. struct tm * tmp;
  1656. tmp = gmtime( &timestamp );
  1657. if( tmp == NULL ){
  1658. LOG_ERROR("gmtime Error!");
  1659. };
  1660. if( strftime( tTS, 200, "%FT%TZ", tmp )==0 ){
  1661. LOG_ERROR("strftime returned 0!");
  1662. };
  1663. };
  1664. if( nonce == 0 ){
  1665. nonce = (uint32_t) rand();
  1666. };
  1667. unsigned char tBuff[CTEXT];
  1668. int cp = 0;
  1669. int index = 0;
  1670. for( ; index < 4; index++ ) {
  1671. tBuff[cp++] = ((unsigned char *)(&nonce))[index];
  1672. };
  1673. index = 0;
  1674. while( tTS[index] != '\0' ) {
  1675. tBuff[cp++] = (unsigned char) tTS[index];
  1676. index++;
  1677. };
  1678. index = 0;
  1679. while( password[index] != '\0' ) {
  1680. tBuff[cp++] = (unsigned char) password[index];
  1681. index++;
  1682. };
  1683. size_t baseOutLen;
  1684. unsigned char tSHA[SHA_DIGEST_LENGTH];
  1685. SHA1( tBuff, cp, tSHA );
  1686. //tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
  1687. tPDIGEST = base64_encode( tSHA, SHA_DIGEST_LENGTH, &baseOutLen );
  1688. //free( tSHA );
  1689. //$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
  1690. strncpy( RetVal.USERNAME, username, CTEXT );
  1691. strncpy( RetVal.TIMESTAMP, tTS, CTEXT );
  1692. strncpy( RetVal.PASSDIGEST, (const char*)tPDIGEST, CTEXT );
  1693. free( tPDIGEST );
  1694. tPDIGEST = base64_encode( (unsigned char *)&nonce, 4 , &baseOutLen );
  1695. strncpy( RetVal.NONCE, (const char*)tPDIGEST, baseOutLen );
  1696. RetVal.NONCE[baseOutLen] = '\0';
  1697. free( tPDIGEST );
  1698. //$REQ['NONCE']=base64_encode($nonce); // alternative
  1699. return RetVal;
  1700. };
  1701. void *pOnvif::parser( void *obj, void *ptr ){
  1702. pOnvif * ME = (pOnvif*)obj;
  1703. curlRequest * RQ = (curlRequest*)ptr;
  1704. if( ME ) {
  1705. if( ME->m_pLastResponse ) free( ME->m_pLastResponse );
  1706. ME->m_pLastResponse = (CSTR) malloc( RQ->getSize()+1);
  1707. if( ME->m_pLastResponse ) {
  1708. memcpy( ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize() );
  1709. ME->m_pLastResponse[RQ->getSize()] = (char)0;
  1710. #ifdef DEBUG_ONVIF_TRAFFIC
  1711. FILE *tf;
  1712. tf = fopen("Traffic.txt", "a+");
  1713. fprintf( tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  1714. fprintf( tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse );
  1715. fprintf( tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  1716. fclose(tf);
  1717. #endif
  1718. };
  1719. ME->parser_done = true;
  1720. };
  1721. return NULL;
  1722. };
  1723. bool pOnvif::_send_request( CSTR url, CSTR post_string, bool mustReturnData ){
  1724. #ifdef CURL_MANAGER
  1725. bool RetVal = false;
  1726. if( m_pHttpAccessManager ){
  1727. m_mResponseProtector.lock();
  1728. curlRequest *tRC;//application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime"
  1729. tRC = new curlRequest( url, (CSTR)"application/soap+xml; charset=utf-8;" );
  1730. if( tRC != NULL ){
  1731. #ifdef DEBUG_ONVIF_TRAFFIC
  1732. FILE *tf;
  1733. tf = fopen("Traffic.txt", "a+");
  1734. fprintf( tf, "--------------------\r\n");
  1735. fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string) );
  1736. fprintf( tf, ">>>>> %s\n", post_string );
  1737. fprintf( tf, "--------------------\r\n");
  1738. fclose(tf);
  1739. mustReturnData = true; //In Debug Allways Read Return
  1740. #endif
  1741. tRC->setCredentials( m_pUsername, m_pPassword, m_bAuthMode );
  1742. tRC->setPOST( post_string, strlen(post_string) );
  1743. if( mustReturnData ) {
  1744. tRC->setHandler( (void*)this, pOnvif::parser, (void*)tRC );
  1745. if( m_pHttpAccessManager->accomplish( tRC ) ){
  1746. parser_done = false;
  1747. uint8_t cnt = 200; // TO = 2 second
  1748. while( !RetVal && cnt-- ){
  1749. RetVal = parser_done;
  1750. SLEEP( 10 );
  1751. };
  1752. };
  1753. } else {
  1754. RetVal = m_pHttpAccessManager->accomplish( tRC );
  1755. };
  1756. } else LOG_ERROR("Cannot Create curlRequest!");
  1757. m_mResponseProtector.unlock();
  1758. };
  1759. return RetVal;
  1760. #else
  1761. char tBuffer[CTEXT];
  1762. if( m_pCURL == NULL ) {
  1763. if( m_pLastResponse == NULL ){
  1764. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  1765. };
  1766. m_pCURL = curl_easy_init();
  1767. if( m_pProxyHost!= NULL && m_nProxyPort!= 0 ){
  1768. curl_easy_setopt( m_pCURL, CURLOPT_PROXY, m_pProxyHost );
  1769. curl_easy_setopt( m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort );
  1770. if( m_pProxyUsername != NULL && m_pProxyPassword != NULL ){
  1771. sprintf( tBuffer,"%s:%s", m_pProxyUsername, m_pProxyPassword );
  1772. curl_easy_setopt( m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer );
  1773. };
  1774. };
  1775. curl_easy_setopt( m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  1776. curl_easy_setopt( m_pCURL, CURLOPT_TIMEOUT, 2);
  1777. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYPEER, 0 );
  1778. curl_easy_setopt( m_pCURL, CURLOPT_SSL_VERIFYHOST, 0 );
  1779. curl_easy_setopt( m_pCURL, CURLOPT_VERBOSE, 0 );
  1780. curl_easy_setopt( m_pCURL, CURLOPT_POST, 1 );
  1781. //curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  1782. //curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  1783. //curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  1784. curl_easy_setopt( m_pCURL, CURLOPT_TCP_FASTOPEN, 1 );
  1785. curl_easy_setopt( m_pCURL, CURLOPT_TCP_NODELAY, 1 );
  1786. //curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  1787. curl_easy_setopt( m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer );
  1788. curl_easy_setopt( m_pCURL, CURLOPT_WRITEDATA, this );
  1789. };
  1790. if( m_pCURL != NULL ){
  1791. curl_easy_setopt( m_pCURL, CURLOPT_URL, url );
  1792. struct curl_slist *sList = NULL;
  1793. //Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  1794. //Connection: Close
  1795. sList = curl_slist_append( sList, "Content-Type: text/xml; charset=utf-8" );
  1796. sprintf( tBuffer, "Content-Length: %d", strlen(post_string) );
  1797. sList = curl_slist_append( sList, tBuffer );
  1798. curl_easy_setopt( m_pCURL, CURLOPT_HTTPHEADER, sList );
  1799. curl_easy_setopt( m_pCURL, CURLOPT_POSTFIELDS, post_string);
  1800. sprintf( tBuffer,"%s:%s", m_pUsername, m_pPassword );
  1801. curl_easy_setopt( m_pCURL, CURLOPT_USERPWD, tBuffer );
  1802. switch ( m_bAuthMode ) {
  1803. case 0: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); break;
  1804. case 1: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST ); break;
  1805. case 2: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE ); break;
  1806. case 3: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER ); break;
  1807. case 4: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE ); break;
  1808. case 5: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM ); break;
  1809. case 6: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB ); break;
  1810. case 7: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); break;
  1811. case 8: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE ); break;
  1812. case 9: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY ); break;
  1813. //case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  1814. };
  1815. #ifdef DEBUG
  1816. char tErrorBuffer[CURL_ERROR_SIZE];
  1817. curl_easy_setopt( m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer );
  1818. #endif
  1819. m_pLastIndex = 0;
  1820. if( curl_easy_perform( m_pCURL ) ) {
  1821. LOG_ERROR("CURL Failure!");
  1822. #ifdef DEBUG
  1823. LOG_ERROR( tErrorBuffer );
  1824. #endif
  1825. } else {
  1826. m_pLastResponse[m_pLastIndex] = (char)0;
  1827. return true;
  1828. };
  1829. m_pLastIndex = 0;
  1830. };
  1831. return false;
  1832. #endif
  1833. };
  1834. #ifndef CURL_MANAGER
  1835. int pOnvif::_writer( char *data, size_t size, size_t nmemb, pOnvif *Ptr ){
  1836. if( Ptr == NULL ) return 0;
  1837. //TLOG("*");
  1838. memcpy( (Ptr->m_pLastResponse + Ptr->m_pLastIndex), data, size*nmemb );
  1839. Ptr->m_pLastIndex += size*nmemb;
  1840. return size*nmemb;
  1841. };
  1842. #endif
  1843. bool pOnvif::setAuthMode( uint8_t Mode ){
  1844. m_bAuthMode = Mode;
  1845. #ifdef DEBUG
  1846. switch ( m_bAuthMode ) {
  1847. case 0: LOG("Set Auth CURLAUTH_BASIC" ); break;
  1848. case 1: LOG("Set Auth CURLAUTH_DIGEST" ); break;
  1849. case 2: LOG("Set Auth CURLAUTH_DIGEST_IE" ); break;
  1850. case 3: LOG("Set Auth CURLAUTH_BEARER" ); break;
  1851. case 4: LOG("Set Auth CURLAUTH_NEGOTIATE" ); break;
  1852. case 5: LOG("Set Auth CURLAUTH_NTLM" ); break;
  1853. case 6: LOG("Set Auth CURLAUTH_NTLM_WB" ); break;
  1854. case 7: LOG("Set Auth CURLAUTH_ANY" ); break;
  1855. case 8: LOG("Set Auth CURLAUTH_ANYSAFE" ); break;
  1856. case 9: LOG("Set Auth CURLAUTH_ONLY" ); break;
  1857. case 10: LOG("Set Auth CURLAUTH_AWS_SIGV4" ); break;
  1858. };
  1859. #endif
  1860. return true;
  1861. };
  1862. #undef LOG
  1863. #undef TLOG
  1864. #undef LOG_ERROR