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.

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