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.

965 lines
26 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
5 months ago
​
​
​
​
5 months ago
​
5 months ago
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
5 months ago
5 months ago
​
​
5 months ago
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
5 months ago
​
​
​
5 months ago
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
5 months ago
​
5 months ago
5 months ago
​
5 months ago
​
5 months ago
​
5 months ago
​
5 months ago
​
​
​
​
​
5 months ago
​
5 months ago
​
​
5 months ago
​
5 months ago
​
5 months ago
​
​
5 months ago
5 months ago
​
5 months ago
​
5 months ago
5 months ago
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
5 months ago
​
​
5 months ago
​
​
5 months ago
​
​
​
5 months ago
5 months ago
​
5 months ago
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "bosch_rcp.h"
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h> /* See NOTES */
  7. #include <sys/types.h>
  8. #define LOG(X) m_pLog->Log("pBRCP", this, X)
  9. #define TLOG(X) basicLogCollection::getInstance()->Log("pBRCP_T", NULL, X)
  10. #ifdef DEBUG
  11. #define LOG_ERROR(X) \
  12. { \
  13. char _tBfrErrMSG2w34[512]; \
  14. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  15. m_pLog->Log("pBRCP", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  16. }
  17. #else
  18. #define LOG_ERROR(X) m_pLog->Log("pBRCP", this, _LOG_ERROR, X);
  19. #endif
  20. const char *BRPC_TYPE_TAG[8]={
  21. "F_FLAG",
  22. "T_OCTET",
  23. "T_WORD",
  24. "T_INT",
  25. "T_DWORD",
  26. "P_OCTET",
  27. "P_STRING",
  28. "P_UNICODE"
  29. };
  30. const char *BRPC_OP_TAG[2]={
  31. "WRITE",
  32. "READ"
  33. };
  34. #define F_FLAG 0
  35. #define T_OCTET 1
  36. #define T_WORD 2
  37. #define T_INT 3
  38. #define T_DWORD 4
  39. #define P_OCTET 5
  40. #define P_STRING 6
  41. #define P_UNICODE 7
  42. boschRCP::boschRCP(){
  43. m_pLog = basicLogCollection::getInstance();
  44. m_pIpAddress = NULL;
  45. m_pUsername = NULL;
  46. m_pPassword = NULL;
  47. m_pDeviceURI = NULL;
  48. m_pBaseURI = NULL;
  49. m_nDeltaTime = 0;
  50. m_pCURL = NULL;
  51. m_pProxyHost = NULL;
  52. m_nProxyPort = 0;
  53. m_pProxyUsername = NULL;
  54. m_pProxyPassword = NULL;
  55. m_pLastResponse = NULL;
  56. m_bIntransingent = true;
  57. m_pHttpAccessManager = NULL;
  58. m_bAuthMode = 0;
  59. LOG("Constructor Done!");
  60. }
  61. boschRCP::~boschRCP(){
  62. #ifdef DEBUG
  63. LOG("Destructor begin");
  64. #endif
  65. if (m_pIpAddress) free(m_pIpAddress);
  66. if (m_pUsername) free(m_pUsername);
  67. if (m_pPassword) free(m_pPassword);
  68. if (m_pDeviceURI) free(m_pDeviceURI);
  69. if (m_pProxyHost) free(m_pProxyHost);
  70. if (m_pProxyUsername) free(m_pProxyUsername);
  71. if (m_pProxyPassword) free(m_pProxyPassword);
  72. if (m_pLastResponse) free(m_pLastResponse);
  73. if (m_pCURL){
  74. #ifdef DEBUG
  75. LOG("Try Cleanup CURL!");
  76. #endif
  77. curl_easy_cleanup(m_pCURL);
  78. m_pCURL = NULL;
  79. };
  80. #ifdef DEBUG
  81. LOG("Destructor Done!");
  82. #endif
  83. }
  84. bool boschRCP::setProxyHost(CSTR proxyHost)
  85. {
  86. if (m_pProxyHost != NULL)
  87. {
  88. free(m_pProxyHost);
  89. }
  90. if (proxyHost == NULL || strlen(proxyHost) == 0)
  91. {
  92. m_pProxyHost = NULL;
  93. return true;
  94. };
  95. m_pProxyHost = (CSTR) malloc(strlen(proxyHost) + 1);
  96. if (m_pProxyHost == NULL)
  97. {
  98. return false;
  99. }
  100. memcpy(m_pProxyHost, proxyHost, strlen(proxyHost) + 1);
  101. return true;
  102. }
  103. CSTR boschRCP::getProxyHost()
  104. {
  105. return m_pProxyHost;
  106. }
  107. bool boschRCP::setProxyPort(int proxyPort)
  108. {
  109. m_nProxyPort = proxyPort;
  110. return true;
  111. }
  112. int boschRCP::getProxyPort(void)
  113. {
  114. return m_nProxyPort;
  115. }
  116. bool boschRCP::setProxyUsername(CSTR proxyUsername)
  117. {
  118. if (m_pProxyUsername != NULL)
  119. {
  120. free(m_pProxyUsername);
  121. }
  122. if (proxyUsername == NULL || strlen(proxyUsername) == 0)
  123. {
  124. m_pProxyUsername = NULL;
  125. return true;
  126. };
  127. m_pProxyUsername = (CSTR) malloc(strlen(proxyUsername) + 1);
  128. if (m_pProxyUsername == NULL)
  129. {
  130. return false;
  131. }
  132. memcpy(m_pProxyUsername, proxyUsername, strlen(proxyUsername) + 1);
  133. return true;
  134. }
  135. CSTR boschRCP::getProxyUsername(void)
  136. {
  137. return m_pProxyUsername;
  138. }
  139. bool boschRCP::setProxyPassword(CSTR proxyPassword)
  140. {
  141. if (m_pProxyPassword != NULL)
  142. {
  143. free(m_pProxyPassword);
  144. }
  145. if (proxyPassword == NULL || strlen(proxyPassword) == 0)
  146. {
  147. m_pProxyPassword = NULL;
  148. return true;
  149. };
  150. m_pProxyPassword = (CSTR) malloc(strlen(proxyPassword) + 1);
  151. if (m_pProxyPassword == NULL)
  152. {
  153. return false;
  154. }
  155. memcpy(m_pProxyPassword, proxyPassword, strlen(proxyPassword) + 1);
  156. return true;
  157. }
  158. CSTR boschRCP::getProxyPassword()
  159. {
  160. return m_pProxyPassword;
  161. }
  162. bool boschRCP::setUsername(CSTR username)
  163. {
  164. if (m_pUsername != NULL)
  165. {
  166. free(m_pUsername);
  167. }
  168. if (username == NULL || strlen(username) == 0)
  169. {
  170. m_pUsername = NULL;
  171. return true;
  172. };
  173. m_pUsername = (CSTR) malloc(strlen(username) + 1);
  174. if (m_pUsername == NULL)
  175. {
  176. return false;
  177. }
  178. memcpy(m_pUsername, username, strlen(username) + 1);
  179. return true;
  180. }
  181. CSTR boschRCP::getUsername(void)
  182. {
  183. return m_pUsername;
  184. }
  185. bool boschRCP::setPassword(CSTR password)
  186. {
  187. if (m_pPassword != NULL)
  188. {
  189. free(m_pPassword);
  190. }
  191. if (password == NULL || strlen(password) == 0)
  192. {
  193. m_pPassword = NULL;
  194. return true;
  195. };
  196. m_pPassword = (CSTR) malloc(strlen(password) + 1);
  197. if (m_pPassword == NULL)
  198. {
  199. return false;
  200. }
  201. memcpy(m_pPassword, password, strlen(password) + 1);
  202. return true;
  203. }
  204. CSTR boschRCP::getPassword(void)
  205. {
  206. return m_pPassword;
  207. }
  208. CSTR boschRCP::getDeviceUri(void)
  209. {
  210. return m_pDeviceURI;
  211. }
  212. bool boschRCP::setDeviceUri(CSTR deviceUri)
  213. {
  214. if (deviceUri == NULL || strlen(deviceUri) == 0){
  215. if (m_pDeviceURI != NULL) free(m_pDeviceURI);
  216. m_pDeviceURI = NULL;
  217. return false;
  218. };
  219. if (strstr(deviceUri, "http") == NULL){
  220. LOG_ERROR("Malformed device URI");
  221. if (m_pDeviceURI != NULL) free(m_pDeviceURI);
  222. m_pDeviceURI = NULL;
  223. return false;
  224. };
  225. if (m_pDeviceURI != NULL) free(m_pDeviceURI);
  226. m_pDeviceURI = (CSTR) malloc(strlen(deviceUri) + 1);
  227. if (m_pDeviceURI == NULL){
  228. return false;
  229. };
  230. memcpy( m_pDeviceURI, deviceUri, strlen(deviceUri));
  231. m_pDeviceURI[strlen(deviceUri)] = (char) 0;
  232. LOG("Device URI set explicitly");
  233. LOG( deviceUri );
  234. // toDo: Must parsed Host/IP
  235. char * tT = strstr(m_pDeviceURI, "/");
  236. if (tT){
  237. tT += 2;
  238. char * tB = strstr(tT, "/");
  239. int len = tB - tT + 1;
  240. m_pIpAddress = (CSTR) malloc(len);
  241. if (m_pIpAddress){
  242. len--;
  243. memcpy(m_pIpAddress, tT, len);
  244. m_pIpAddress[len] = (char) 0;
  245. LOG("Host/Ip");
  246. LOG(m_pIpAddress);
  247. };
  248. };
  249. return true;
  250. }
  251. CSTR boschRCP::getIPAddress(void)
  252. {
  253. return m_pIpAddress;
  254. }
  255. bool boschRCP::updateDevURI( void ){
  256. if( m_pIpAddress == NULL ) return false;
  257. if( m_pDeviceURI ) free( m_pDeviceURI );
  258. char tBuffer[512];
  259. //if ( m_pUsername ) {
  260. // if ( m_pPassword ) {
  261. // sprintf(tBuffer, BRCP_DEFAULT_URI_TPL, m_pUsername, m_pPassword, m_pIpAddress );
  262. // } else {
  263. // sprintf(tBuffer, BRCP_DEFAULT_URI_TPL, m_pUsername, "", m_pIpAddress );
  264. // };
  265. //} else {
  266. sprintf( tBuffer, BRCP_DEFAULT_URI_TPL_NA, m_pIpAddress );
  267. //};
  268. size_t len = strlen( tBuffer );
  269. m_pDeviceURI = (CSTR)malloc(len + 1);
  270. if( m_pDeviceURI ){
  271. strncpy( m_pDeviceURI, tBuffer, len );
  272. m_pDeviceURI[len] = (char)0;
  273. return true;
  274. };
  275. return false;
  276. }
  277. bool boschRCP::setIPAddress( CSTR ipAddress ){
  278. if( m_pIpAddress!= NULL) free(m_pIpAddress);
  279. if( ipAddress == NULL || strlen(ipAddress) == 0) return false;
  280. size_t len = strlen(ipAddress);
  281. m_pIpAddress = (CSTR) malloc( len + 1);
  282. if( m_pIpAddress == NULL ) return false;
  283. strncpy( m_pIpAddress, ipAddress, len );
  284. m_pIpAddress[len] = (char) 0;
  285. return updateDevURI();
  286. }
  287. size_t boschRCP::createRequestString( CSTR buffer, uint16_t CMD, bool RW, int8_t pl_type, void *pl ){
  288. if ( buffer == NULL) return 0;
  289. if (pl != NULL) {
  290. sprintf(buffer, "%s?command=0x%04x&type=%s&direction=%s&payload=%s",
  291. m_pDeviceURI,
  292. CMD,
  293. BRPC_TYPE_TAG[pl_type],
  294. BRPC_OP_TAG[RW],
  295. (CSTR)pl
  296. );
  297. } else {
  298. sprintf(buffer, "%s?command=0x%04x&direction=READ", m_pDeviceURI, CMD );
  299. };
  300. size_t rValue = strlen( buffer );
  301. return rValue;
  302. }
  303. bool boschRCP::create(void){
  304. #ifdef DEBUG_EXTRA
  305. LOG("Get Instance m_pHttpAccessManager");
  306. #endif
  307. m_pHttpAccessManager = curlRequestsManager::getInstance();
  308. if (m_pHttpAccessManager == NULL){
  309. return false;
  310. };
  311. #ifdef DEBUG_EXTRA
  312. LOG("CURL MANAGER OK");
  313. #endif
  314. try{
  315. struct tm * tTS;
  316. tTS = core_GetSystemDateAndTime();
  317. // This is UTS DTS
  318. if (tTS != NULL){
  319. #ifdef DEBUG
  320. char tB[128];
  321. sprintf(tB, "TS from dev: %04d-%02d-%02d %02d:%02d:%02d", tTS->tm_year, tTS->tm_mon, tTS->tm_mday, tTS->tm_hour, tTS->tm_min, tTS->tm_sec);
  322. LOG(tB);
  323. #endif
  324. // WTF ??!!
  325. tTS->tm_year -= 1900;
  326. tTS->tm_mon -= 1;
  327. time_t tTimeStamp = timegm(tTS); // mktime( tTS );
  328. m_nDeltaTime = time(NULL) - tTimeStamp;
  329. #ifdef DEBUG
  330. sprintf(tB, "TS:[%ld] D:[%ld]", tTimeStamp, m_nDeltaTime);
  331. LOG(tB);
  332. #endif
  333. free(tTS);
  334. } else {
  335. return true; //false;
  336. };
  337. } catch (const std::exception& e) {
  338. LOG_ERROR("Catched Exception!");
  339. return false;
  340. };
  341. return true;
  342. }
  343. /*
  344. Public wrappers for a subset of ONVIF primitives
  345. */
  346. struct tm * boschRCP::core_GetSystemDateAndTime(void)
  347. {
  348. struct tm * RetVal = NULL;
  349. char tURL[MAX_PATH];
  350. if( createRequestString( tURL, BRCP_CMD_GET_DTS, true ) == 0 ){
  351. return NULL;
  352. };
  353. #ifdef DEBUG_EXTRA
  354. LOG("Before REQ");
  355. #endif
  356. if ( _send_request( tURL, NULL, true))
  357. {
  358. XMLResults tParseResults;
  359. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  360. #ifdef DEBUG_EXTRA
  361. LOG("core_GetSystemDateAndTime");
  362. #endif
  363. if (tParseResults.error)
  364. {
  365. char tBuffer[CTEXT];
  366. sprintf(tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  367. LOG_ERROR(tBuffer);
  368. }
  369. else
  370. {
  371. RetVal = (struct tm *) malloc(sizeof(struct tm));
  372. if (RetVal != NULL)
  373. {
  374. memset(RetVal, 0, sizeof(struct tm));
  375. XMLNode zNode = response.getChildNode("Body", 0);
  376. if (! zNode.isEmpty())
  377. {
  378. XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0);
  379. if (! fNode.isEmpty())
  380. {
  381. XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0);
  382. if (! hNode.isEmpty())
  383. {
  384. XMLNode tNode = hNode.getChildNode("UTCDateTime", 0);
  385. if (! tNode.isEmpty())
  386. {
  387. RetVal->tm_isdst = 0;
  388. XMLNode ttNode = tNode.getChildNode("Time", 0);
  389. XMLNode dtNode = tNode.getChildNode("Date", 0);
  390. {
  391. XMLNode t = ttNode.getChildNode("Hour", 0);
  392. RetVal->tm_hour = atoi(t.getText(0));
  393. }
  394. {
  395. XMLNode t = ttNode.getChildNode("Minute", 0);
  396. RetVal->tm_min = atoi(t.getText(0));
  397. }
  398. {
  399. XMLNode t = ttNode.getChildNode("Second", 0);
  400. RetVal->tm_sec = atoi(t.getText(0));
  401. }
  402. {
  403. XMLNode t = dtNode.getChildNode("Year", 0);
  404. RetVal->tm_year = atoi(t.getText(0));
  405. }
  406. {
  407. XMLNode t = dtNode.getChildNode("Month", 0);
  408. RetVal->tm_mon = atoi(t.getText(0));
  409. }
  410. {
  411. XMLNode t = dtNode.getChildNode("Day", 0);
  412. RetVal->tm_mday = atoi(t.getText(0));
  413. }
  414. }
  415. else
  416. {
  417. LOG_ERROR("UTCDateTime is Not Found!");
  418. };
  419. }
  420. else
  421. {
  422. LOG_ERROR("SystemDateAndTime is Not Found!");
  423. };
  424. }
  425. else
  426. {
  427. LOG_ERROR("GetSystemDateAndTimeResponse is Not Found!");
  428. };
  429. }
  430. else
  431. {
  432. //LOG_ERROR("Body is Not Found!");
  433. //LOG(m_pLastResponse);
  434. };
  435. };
  436. return RetVal;
  437. };
  438. } else {
  439. LOG_ERROR("GetSystemDateAndTime: Communication error");
  440. }
  441. return NULL;
  442. }
  443. bool boschRCP::ptz_GetStatus( ptzStatus * tStatus ){
  444. if (tStatus == NULL) return false;
  445. char tBuffer[DEFAULT_BUFFER_SIZE];
  446. memset( tStatus, 0, sizeof(ptzStatus) );
  447. //sprintf(tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  448. /*
  449. if (_send_request(m_pPTZURI, tBuffer, true )){
  450. XMLResults tParseResults;
  451. XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);
  452. if (tParseResults.error){
  453. char tBuf[CTEXT];
  454. sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn);
  455. LOG_ERROR(tBuf);
  456. } else {
  457. XMLNode aNode = response.getChildNode("Body", 0);
  458. if (aNode.isEmpty())
  459. {
  460. return NULL;
  461. }
  462. XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0);
  463. if (! bNode.isEmpty())
  464. {
  465. XMLNode cNode = bNode.getChildNode("PTZStatus", 0);
  466. if (! cNode.isEmpty())
  467. {
  468. XMLNode dNode = cNode.getChildNode("Position", 0);
  469. if (! dNode.isEmpty())
  470. {
  471. XMLNode eNode = dNode.getChildNode("PanTilt", 0);
  472. if (! eNode.isEmpty())
  473. {
  474. // XMLAttribute ZZ = eNode.getAttribute(1);
  475. // LOG( ZZ.lpszName );
  476. tStatus->pan = atof(eNode.getAttribute("x")); // ZZ.lpszValue );
  477. // ZZ = eNode.getAttribute(2);
  478. // LOG( ZZ.lpszName );
  479. tStatus->tilt = atof(eNode.getAttribute("y")); // ZZ.lpszValue );
  480. };
  481. XMLNode fNode = dNode.getChildNode("Zoom", 0);
  482. if (! fNode.isEmpty())
  483. {
  484. // XMLAttribute ZZ = eNode.getAttribute(1);
  485. // LOG( ZZ.lpszName );
  486. tStatus->zoom = atof(fNode.getAttribute("x")); // ZZ.lpszValue );
  487. };
  488. };
  489. if (tStatus->pan != .0f || tStatus->tilt != .0f || tStatus->zoom != .0f)
  490. {
  491. XMLNode gNode = cNode.getChildNode("MoveStatus", 0);
  492. if (! gNode.isEmpty())
  493. {
  494. tStatus->hasMoveStatus = true;
  495. XMLNode hNode = gNode.getChildNode("PanTilt", 0);
  496. if (! hNode.isEmpty())
  497. {
  498. if (strstr(hNode.getText(0), "IDLE") != NULL)
  499. {
  500. tStatus->PanTiltIdle = true;
  501. }
  502. };
  503. XMLNode iNode = gNode.getChildNode("Zoom", 0);
  504. if (! iNode.isEmpty())
  505. {
  506. if (strstr(iNode.getText(0), "IDLE") != NULL)
  507. {
  508. tStatus->ZoomIdle = true;
  509. }
  510. };
  511. };
  512. }
  513. return true;
  514. };
  515. };
  516. };
  517. };
  518. */
  519. return false;
  520. }
  521. /*
  522. bool boschRCP::ptz_GetPresets(CSTR profileToken)
  523. {
  524. if (m_pPTZURI == NULL)
  525. {
  526. return false;
  527. }
  528. char tBuffer[DEFAULT_BUFFER_SIZE];
  529. accessToken tTOKEN;
  530. tTOKEN = _makeToken();
  531. sprintf(tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken);
  532. if (_send_request(m_pPTZURI, tBuffer))
  533. {
  534. // XMLResults tParseResults;
  535. // XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
  536. // LOG("RCPT:");
  537. // LOG( m_pLastResponse );
  538. // if( tParseResults.error ){
  539. // char tBuf[CTEXT];
  540. // sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  541. // LOG_ERROR( tBuf );
  542. // } else {
  543. //$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
  544. //$presets=array();
  545. // foreach ($getpresetsresponse as $preset) {
  546. // $presets[]=array('Token'=>$preset['@attributes']['token'],
  547. // 'Name'=>$preset['Name'],
  548. // 'PTZPosition'=>$preset['PTZPosition']);
  549. // }
  550. // return $presets;
  551. return true;
  552. //};
  553. };
  554. return false;
  555. }
  556. */
  557. // here the IN coordinates are OnVIF -1 ... 0 ... 1 except zoom 0 .. 1
  558. bool boschRCP::ptz_AbsoluteMove( float position_pantilt_x, float position_pantilt_y, float zoom){
  559. char tBuffer[DEFAULT_BUFFER_SIZE];
  560. char ptzCOM[512];
  561. #ifdef DEBUG_EXTRA
  562. sprintf( tBuffer, " Pan: %0.3f Tilt: %0.3f Zoom: %0.3f", position_pantilt_x, position_pantilt_y, zoom );
  563. LOG( tBuffer );
  564. #endif
  565. uint16_t pPos = ((uint16_t) ( 18000.0 + 18000.0 * position_pantilt_x)) % 36000;
  566. uint16_t tPos = ((uint16_t) ( 9000.0 + 9000.0 * position_pantilt_y)) % 36000;
  567. uint16_t zPos = ( uint16_t )( 16384 * zoom );
  568. #ifdef DEBUG_EXTRA
  569. sprintf( tBuffer, " CPOS: %d Tilt: %d Zoom: %d", pPos, tPos, zPos );
  570. LOG( tBuffer );
  571. #endif
  572. sprintf( ptzCOM,"0x80000601D102%04X%04X%04X0000&num=1", pPos, tPos, zPos );
  573. if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
  574. return _send_request( tBuffer, NULL, false );
  575. }
  576. bool boschRCP::ptz_ContinuousMove( float velocity_pantilt_x, float velocity_pantilt_y){
  577. char tBuffer[DEFAULT_BUFFER_SIZE];
  578. return _send_request( tBuffer, tBuffer, true);
  579. }
  580. bool boschRCP::ptz_ContinuousMoveZoom( float speedZoom ){
  581. char tBuffer[DEFAULT_BUFFER_SIZE];
  582. char ptzCOM[512];
  583. uint8_t Speed;
  584. // 1 - 15
  585. if ( speedZoom < 0 ) {
  586. Speed = 1 + (speedZoom * -14.0f);
  587. sprintf( ptzCOM,"0x80000601108500000%01X&num=1", Speed );
  588. } else {
  589. Speed = 1 + (speedZoom * 14.0f);
  590. sprintf( ptzCOM,"0x80000601108500008%01X&num=1", Speed );
  591. };
  592. if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
  593. LOG( tBuffer );
  594. return _send_request( tBuffer, NULL, false );
  595. }
  596. bool boschRCP::ptz_Stop( bool pantilt, bool zoom){
  597. char tBuffer[DEFAULT_BUFFER_SIZE];
  598. if( pantilt || zoom ){
  599. char ptzCOM[512];
  600. sprintf( ptzCOM,"0x800006011085000000&num=1" );
  601. if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
  602. return _send_request( tBuffer, NULL, false );
  603. };
  604. return false;
  605. }
  606. bool boschRCP::ptz_GotoPreset( CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x){
  607. char tBuffer[DEFAULT_BUFFER_SIZE];
  608. return _send_request( tBuffer, NULL, false );
  609. }
  610. bool boschRCP::ptz_RemovePreset( CSTR presetToken){
  611. char tBuffer[DEFAULT_BUFFER_SIZE];
  612. return _send_request( tBuffer, NULL, false );
  613. }
  614. bool boschRCP::ptz_SetPreset( CSTR presetName){
  615. char tBuffer[DEFAULT_BUFFER_SIZE];
  616. return _send_request( tBuffer, NULL, false );
  617. }
  618. /*
  619. Internal functions
  620. */
  621. /*
  622. protected function _xml2array($response) {
  623. $sxe = new SimpleXMLElement($response);
  624. $dom_sxe = dom_import_simplexml($sxe);
  625. $dom = new DOMDocument('1.0');
  626. $dom_sxe = $dom->importNode($dom_sxe, true);
  627. $dom_sxe = $dom->appendChild($dom_sxe);
  628. $element = $dom->childNodes->item(0);
  629. foreach ($sxe->getDocNamespaces() as $name => $uri) {
  630. $element->removeAttributeNS($uri, $name);
  631. }
  632. $xmldata=$dom->saveXML();
  633. $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>"));
  634. $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>"));
  635. $xml=simplexml_load_string($xmldata);
  636. $data=json_decode(json_encode((array)$xml),1);
  637. $data=array($xml->getName()=>$data);
  638. return $data;
  639. }
  640. */
  641. accessToken boschRCP::_passwordDigest(CSTR username, CSTR password, time_t timestamp, uint32_t nonce)
  642. {
  643. accessToken RetVal;
  644. char tTS[CTEXT];
  645. unsigned char * tPDIGEST;
  646. if (timestamp == 0)
  647. {
  648. time_t t;
  649. struct tm * tmp;
  650. t = time(NULL);
  651. tmp = gmtime(&t);
  652. if (tmp == NULL)
  653. {
  654. LOG_ERROR("gmtime Error!");
  655. };
  656. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  657. {
  658. LOG_ERROR("strftime returned 0!");
  659. };
  660. }
  661. else
  662. {
  663. struct tm * tmp;
  664. tmp = gmtime(&timestamp);
  665. if (tmp == NULL)
  666. {
  667. LOG_ERROR("gmtime Error!");
  668. };
  669. if (strftime(tTS, 200, "%FT%TZ", tmp) == 0)
  670. {
  671. LOG_ERROR("strftime returned 0!");
  672. };
  673. };
  674. if (nonce == 0)
  675. {
  676. nonce = (uint32_t) rand();
  677. };
  678. unsigned char tBuff[CTEXT];
  679. int cp = 0;
  680. int index = 0;
  681. for (; index < 4; index++)
  682. {
  683. tBuff[cp++] = ((unsigned char *) (&nonce))[index];
  684. };
  685. index = 0;
  686. while (tTS[index] != '\0')
  687. {
  688. tBuff[cp++] = (unsigned char) tTS[index];
  689. index++;
  690. };
  691. index = 0;
  692. while (password[index] != '\0')
  693. {
  694. tBuff[cp++] = (unsigned char) password[index];
  695. index++;
  696. };
  697. size_t baseOutLen;
  698. unsigned char tSHA[SHA_DIGEST_LENGTH];
  699. SHA1(tBuff, cp, tSHA);
  700. // tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
  701. tPDIGEST = base64_encode(tSHA, SHA_DIGEST_LENGTH, &baseOutLen);
  702. // free( tSHA );
  703. //$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
  704. strncpy(RetVal.USERNAME, username, CTEXT);
  705. strncpy(RetVal.TIMESTAMP, tTS, CTEXT);
  706. strncpy(RetVal.PASSDIGEST, (const char *) tPDIGEST, CTEXT);
  707. free(tPDIGEST);
  708. tPDIGEST = base64_encode((unsigned char *) &nonce, 4, &baseOutLen);
  709. strncpy(RetVal.NONCE, (const char *) tPDIGEST, baseOutLen);
  710. RetVal.NONCE[baseOutLen] = '\0';
  711. free(tPDIGEST);
  712. //$REQ['NONCE']=base64_encode($nonce); // alternative
  713. return RetVal;
  714. }
  715. void * boschRCP::parser(void * obj, void * ptr)
  716. {
  717. boschRCP * ME = (boschRCP *) obj;
  718. curlRequest * RQ = (curlRequest *) ptr;
  719. if (ME)
  720. {
  721. if (ME->m_pLastResponse)
  722. {
  723. free(ME->m_pLastResponse);
  724. }
  725. ME->m_pLastResponse = (CSTR) malloc(RQ->getSize() + 1);
  726. if (ME->m_pLastResponse)
  727. {
  728. memcpy(ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize());
  729. ME->m_pLastResponse[RQ->getSize()] = (char) 0;
  730. #ifdef DEBUG_BRCP_TRAFFIC
  731. FILE * tf;
  732. tf = fopen("Traffic.txt", "a+");
  733. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  734. fprintf(tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse);
  735. fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n");
  736. fclose(tf);
  737. #endif
  738. };
  739. ME->parser_done = true;
  740. };
  741. return NULL;
  742. }
  743. bool boschRCP::_send_request(CSTR url, CSTR post_string, bool mustReturnData){
  744. #ifdef CURL_MANAGER
  745. bool RetVal = false;
  746. if (m_pHttpAccessManager){
  747. m_mResponseProtector.lock();
  748. curlRequest * tRC;
  749. tRC = new curlRequest(url, (CSTR) "application/soap+xml; charset=utf-8;");
  750. if (tRC != NULL){
  751. #ifdef DEBUG_BRCP_TRAFFIC
  752. FILE * tf;
  753. tf = fopen("Traffic.txt", "a+");
  754. fprintf(tf, "--------------------\r\n");
  755. if ( post_string != NULL ) {
  756. fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string));
  757. fprintf( tf, ">>>>> %s\n", post_string );
  758. } else fprintf(tf, "REQ: %s\n", url );
  759. fprintf(tf,"Creds: [%s]:[%s]\n",m_pUsername, m_pPassword );
  760. fprintf(tf, "--------------------\r\n");
  761. fclose(tf);
  762. mustReturnData = true; // In Debug Allways Read Return
  763. #endif
  764. tRC->setCredentials( m_pUsername, m_pPassword, m_bAuthMode );
  765. if( post_string != NULL ){
  766. tRC->setPOST(post_string, strlen(post_string));
  767. };
  768. if (mustReturnData){
  769. tRC->setHandler((void *) this, boschRCP::parser, (void *) tRC );
  770. if( m_pHttpAccessManager->accomplish(tRC) ){
  771. parser_done = false;
  772. uint8_t cnt = 200; // TO = 2 second
  773. while (! RetVal && cnt--){
  774. RetVal = parser_done;
  775. SLEEP(10);
  776. };
  777. };
  778. } else {
  779. RetVal = m_pHttpAccessManager->accomplish( tRC );
  780. };
  781. } else {
  782. LOG_ERROR("Cannot Create curlRequest!");
  783. };
  784. m_mResponseProtector.unlock();
  785. };
  786. return RetVal;
  787. #else
  788. char tBuffer[CTEXT];
  789. if (m_pCURL == NULL)
  790. {
  791. if (m_pLastResponse == NULL)
  792. {
  793. m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
  794. };
  795. m_pCURL = curl_easy_init();
  796. if (m_pProxyHost != NULL && m_nProxyPort != 0)
  797. {
  798. curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
  799. curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
  800. if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
  801. {
  802. sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
  803. curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
  804. };
  805. };
  806. curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
  807. curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
  808. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
  809. curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
  810. curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
  811. curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
  812. // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
  813. // curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
  814. // curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
  815. curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
  816. curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
  817. // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  818. curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
  819. curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
  820. };
  821. if (m_pCURL != NULL)
  822. {
  823. curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
  824. struct curl_slist * sList = NULL;
  825. // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
  826. // Connection: Close
  827. sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
  828. sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
  829. sList = curl_slist_append(sList, tBuffer);
  830. curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
  831. curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
  832. sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
  833. curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
  834. switch (m_bAuthMode)
  835. {
  836. case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
  837. case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
  838. case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
  839. case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
  840. case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
  841. case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
  842. case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
  843. case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
  844. case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
  845. case 9:
  846. curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
  847. break;
  848. // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
  849. };
  850. #ifdef DEBUG
  851. char tErrorBuffer[CURL_ERROR_SIZE];
  852. curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
  853. #endif
  854. m_pLastIndex = 0;
  855. if (curl_easy_perform(m_pCURL))
  856. {
  857. LOG_ERROR("CURL Failure!");
  858. #ifdef DEBUG
  859. LOG_ERROR(tErrorBuffer);
  860. #endif
  861. }
  862. else
  863. {
  864. m_pLastResponse[m_pLastIndex] = (char) 0;
  865. return true;
  866. };
  867. m_pLastIndex = 0;
  868. };
  869. return false;
  870. #endif
  871. }
  872. #ifndef CURL_MANAGER
  873. int boschRCP::_writer(char * data, size_t size, size_t nmemb, boschRCP * Ptr)
  874. {
  875. if (Ptr == NULL)
  876. {
  877. return 0;
  878. }
  879. // TLOG("*");
  880. memcpy(Ptr->m_pLastResponse + Ptr->m_pLastIndex, data, size * nmemb);
  881. Ptr->m_pLastIndex += size * nmemb;
  882. return size * nmemb;
  883. }
  884. #endif
  885. bool boschRCP::setAuthMode(uint8_t Mode)
  886. {
  887. m_bAuthMode = Mode;
  888. #ifdef DEBUG
  889. switch (m_bAuthMode)
  890. {
  891. case 0 : LOG("Set Auth CURLAUTH_BASIC"); break;
  892. case 1 : LOG("Set Auth CURLAUTH_DIGEST"); break;
  893. case 2 : LOG("Set Auth CURLAUTH_DIGEST_IE"); break;
  894. case 3 : LOG("Set Auth CURLAUTH_BEARER"); break;
  895. case 4 : LOG("Set Auth CURLAUTH_NEGOTIATE"); break;
  896. case 5 : LOG("Set Auth CURLAUTH_NTLM"); break;
  897. case 6 : LOG("Set Auth CURLAUTH_NTLM_WB"); break;
  898. case 7 : LOG("Set Auth CURLAUTH_ANY"); break;
  899. case 8 : LOG("Set Auth CURLAUTH_ANYSAFE"); break;
  900. case 9 : LOG("Set Auth CURLAUTH_ONLY"); break;
  901. case 10: LOG("Set Auth CURLAUTH_AWS_SIGV4"); break;
  902. };
  903. #endif
  904. return true;
  905. }
  906. #undef LOG
  907. #undef TLOG
  908. #undef LOG_ERROR