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.

1959 lines
91 KiB

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