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.

1941 lines
93 KiB

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