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.

1895 lines
90 KiB

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