|
|
#include "bosch_rcp.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/types.h>
#define LOG(X) m_pLog->Log("pBRCP", this, X)
#define TLOG(X) basicLogCollection::getInstance()->Log("pBRCP_T", NULL, X)
#ifdef DEBUG
#define LOG_ERROR(X) \
{ \ char _tBfrErrMSG2w34[512]; \ sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \ m_pLog->Log("pBRCP", this, _LOG_ERROR, _tBfrErrMSG2w34); \ }#else
#define LOG_ERROR(X) m_pLog->Log("pBRCP", this, _LOG_ERROR, X);
#endif
const char *BRPC_TYPE_TAG[8]={ "F_FLAG", "T_OCTET", "T_WORD", "T_INT", "T_DWORD", "P_OCTET", "P_STRING", "P_UNICODE"};
const char *BRPC_OP_TAG[2]={ "READ", "WRITE"};
inline bool checkBool(const char * A){ if (A == NULL) { return false; } return (A[0] == 'T' || A[0] == 't' || A[0] == '1');}
inline bool checkOnOff(const char * A){ if (A == NULL) { return false; } return (A[1] == 'N' || A[1] == 'n' || A[0] == '1');}
inline onvifOnOffAuto checkOnOffAuto(const char * A){ if (A == NULL) { return AUTO; } if (A[1] == 'N' || A[1] == 'n' || A[0] == '1') { return ON; } return OFF;}
boschRCP::boschRCP(){ m_pLog = basicLogCollection::getInstance(); m_pIpAddress = NULL; m_pUsername = NULL; m_pPassword = NULL; m_pMediaURI = NULL; m_pSnapshotURI = NULL; m_pDeviceURI = NULL; m_pPTZURI = NULL; m_pBaseURI = NULL; m_nDeltaTime = 0; m_pCURL = NULL; m_pProxyHost = NULL; m_nProxyPort = 0; m_pProxyUsername = NULL; m_pProxyPassword = NULL; m_pLastResponse = NULL; m_bIntransingent = true; m_nDiscoveryTimeout = 30;//PONVIF_DEFAULT_DISCOVERYTIMEOUT;
m_pDiscoveryBindIP = NULL; // PONVIF_DEFAULT_DISCOVERYBINDIP;
m_pDiscoveryMCastIP = NULL; // PONVIF_DEFAULT_DISCOVERYMCASTIP;
m_nDiscoveryMCastPort = 30;//PONVIF_DEFAULT_DISCOVERYMCASTPORT;
m_bDiscoveryHideDuplicates = true; m_pHttpAccessManager = NULL; m_sCapabilities.Analytics = NULL; m_sCapabilities.Device = NULL; m_sCapabilities.Events = NULL; m_sCapabilities.Imaging = NULL; m_sCapabilities.Media = NULL; m_sCapabilities.PTZ = NULL; m_sCapabilities.Extension = NULL; m_bAuthMode = 0; LOG("Constructor Done!");}
boschRCP::~boschRCP(){#ifdef DEBUG
LOG("Destructor begin");#endif
if (m_pIpAddress) { free(m_pIpAddress); } if (m_pUsername) { free(m_pUsername); } if (m_pPassword) { free(m_pPassword); } if (m_pMediaURI) { free(m_pMediaURI); } if (m_pSnapshotURI) { free(m_pSnapshotURI); } if (m_pDeviceURI) { free(m_pDeviceURI); } if (m_pPTZURI) { free(m_pPTZURI); } if (m_pBaseURI) { free(m_pBaseURI); } if (m_pProxyHost) { free(m_pProxyHost); } if (m_pProxyUsername) { free(m_pProxyUsername); } if (m_pProxyPassword) { free(m_pProxyPassword); } if (m_pLastResponse) { free(m_pLastResponse); } if (m_pDiscoveryBindIP) { free(m_pDiscoveryBindIP); } if (m_pDiscoveryMCastIP) { free(m_pDiscoveryMCastIP); } if (m_sCapabilities.Analytics) { free(m_sCapabilities.Analytics); } if (m_sCapabilities.Device) { free(m_sCapabilities.Device); } if (m_sCapabilities.Events) { free(m_sCapabilities.Events); } if (m_sCapabilities.Imaging) { free(m_sCapabilities.Imaging); } if (m_sCapabilities.Media) { free(m_sCapabilities.Media); } if (m_sCapabilities.PTZ) { free(m_sCapabilities.PTZ); } if (m_sCapabilities.Extension) { free(m_sCapabilities.Extension); } if (m_pCURL) {#ifdef DEBUG
LOG("Try Cleanup CURL!");#endif
curl_easy_cleanup(m_pCURL); m_pCURL = NULL; };#ifdef DEBUG
LOG("Destructor Done!");#endif
}
bool boschRCP::setProxyHost(CSTR proxyHost){ if (m_pProxyHost != NULL) { free(m_pProxyHost); } if (proxyHost == NULL || strlen(proxyHost) == 0) { m_pProxyHost = NULL; return true; }; m_pProxyHost = (CSTR) malloc(strlen(proxyHost) + 1); if (m_pProxyHost == NULL) { return false; } memcpy(m_pProxyHost, proxyHost, strlen(proxyHost) + 1); return true;}
CSTR boschRCP::getProxyHost(){ return m_pProxyHost;}
bool boschRCP::setProxyPort(int proxyPort){ m_nProxyPort = proxyPort; return true;}
int boschRCP::getProxyPort(void){ return m_nProxyPort;}
bool boschRCP::setProxyUsername(CSTR proxyUsername){ if (m_pProxyUsername != NULL) { free(m_pProxyUsername); } if (proxyUsername == NULL || strlen(proxyUsername) == 0) { m_pProxyUsername = NULL; return true; }; m_pProxyUsername = (CSTR) malloc(strlen(proxyUsername) + 1); if (m_pProxyUsername == NULL) { return false; } memcpy(m_pProxyUsername, proxyUsername, strlen(proxyUsername) + 1); return true;}
CSTR boschRCP::getProxyUsername(void){ return m_pProxyUsername;}
bool boschRCP::setProxyPassword(CSTR proxyPassword){ if (m_pProxyPassword != NULL) { free(m_pProxyPassword); } if (proxyPassword == NULL || strlen(proxyPassword) == 0) { m_pProxyPassword = NULL; return true; }; m_pProxyPassword = (CSTR) malloc(strlen(proxyPassword) + 1); if (m_pProxyPassword == NULL) { return false; } memcpy(m_pProxyPassword, proxyPassword, strlen(proxyPassword) + 1); return true;}
CSTR boschRCP::getProxyPassword(){ return m_pProxyPassword;}
bool boschRCP::setUsername(CSTR username){ if (m_pUsername != NULL) { free(m_pUsername); } if (username == NULL || strlen(username) == 0) { m_pUsername = NULL; return true; }; m_pUsername = (CSTR) malloc(strlen(username) + 1); if (m_pUsername == NULL) { return false; } memcpy(m_pUsername, username, strlen(username) + 1); return true;}
CSTR boschRCP::getUsername(void){ return m_pUsername;}
bool boschRCP::setPassword(CSTR password){ if (m_pPassword != NULL) { free(m_pPassword); } if (password == NULL || strlen(password) == 0) { m_pPassword = NULL; return true; }; m_pPassword = (CSTR) malloc(strlen(password) + 1); if (m_pPassword == NULL) { return false; } memcpy(m_pPassword, password, strlen(password) + 1); return true;}
CSTR boschRCP::getPassword(void){ return m_pPassword;}
CSTR boschRCP::getDeviceUri(void){ return m_pDeviceURI;}
bool boschRCP::setDeviceUri(CSTR deviceUri){ if (deviceUri == NULL || strlen(deviceUri) == 0){ if (m_pDeviceURI != NULL) free(m_pDeviceURI); m_pDeviceURI = NULL; return false; }; if (strstr(deviceUri, "http") == NULL){ LOG_ERROR("Malformed device URI"); if (m_pDeviceURI != NULL) free(m_pDeviceURI); m_pDeviceURI = NULL; return false; };
if (m_pDeviceURI != NULL) free(m_pDeviceURI); m_pDeviceURI = (CSTR) malloc(strlen(deviceUri) + 1); if (m_pDeviceURI == NULL){ return false; }; memcpy( m_pDeviceURI, deviceUri, strlen(deviceUri)); m_pDeviceURI[strlen(deviceUri)] = (char) 0;
// toDo: Must parsed Host/IP
char * tT = strstr(m_pDeviceURI, "/"); if (tT) { tT += 2; char * tB = strstr(tT, "/"); int len = tB - tT + 1; m_pIpAddress = (CSTR) malloc(len); if (m_pIpAddress) { len--; memcpy(m_pIpAddress, tT, len); m_pIpAddress[len] = (char) 0; LOG(m_pIpAddress); }; }; return true;}
CSTR boschRCP::getIPAddress(void){ return m_pIpAddress;}
bool boschRCP::setIPAddress(CSTR ipAddress){ if (ipAddress == NULL || strlen(ipAddress) == 0) { m_pIpAddress = NULL; return false; }; if (m_pIpAddress != NULL) { free(m_pIpAddress); } m_pIpAddress = (CSTR) malloc(strlen(ipAddress) + 1); if (m_pIpAddress == NULL) { return false; } memcpy(m_pIpAddress, ipAddress, strlen(ipAddress)); m_pIpAddress[strlen(ipAddress)] = (char) 0; int len = strlen(ipAddress) + 28; m_pDeviceURI = (CSTR) malloc(len + 1); if (m_pDeviceURI){ sprintf( m_pDeviceURI, BRCP_DEFAULT_URI_TPL, m_pIpAddress); LOG(m_pDeviceURI); }; return true;}
//bool boschRCP::setDiscoveryTimeout(int discoverytimeout)
//{
// m_nDiscoveryTimeout = discoverytimeout;
// return true;
//}
size_t boschRCP::createRequestString( CSTR buffer, uint16_t CMD, bool RW, int8_t pl_type, void *pl ){ if ( buffer == NULL) return 0; if (pl != NULL) { sprintf(buffer, "%s?command=0x%04x&type=%s&direction=%s", m_pDeviceURI, CMD, BRPC_TYPE_TAG[pl_type],(RW?"READ":"WRITE")); } else { sprintf(buffer, "%s?command=0x%04x&direction=READ", m_pDeviceURI, CMD ); }; size_t rValue = strlen( buffer ); return rValue;}
bool boschRCP::create(void){#ifdef DEBUG_EXTRA
LOG("Get Instance m_pHttpAccessManager");#endif
m_pHttpAccessManager = curlRequestsManager::getInstance(); if (m_pHttpAccessManager == NULL){ return false; };#ifdef DEBUG_EXTRA
LOG("CURL MANAGER OK");#endif
try{ struct tm * tTS; tTS = core_GetSystemDateAndTime(); // This is UTS DTS
if (tTS != NULL){#ifdef DEBUG
char tB[128]; 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); LOG(tB);#endif
// WTF ??!!
tTS->tm_year -= 1900; tTS->tm_mon -= 1;
time_t tTimeStamp = timegm(tTS); // mktime( tTS );
m_nDeltaTime = time(NULL) - tTimeStamp;#ifdef DEBUG
sprintf(tB, "TS:[%ld] D:[%ld]", tTimeStamp, m_nDeltaTime); LOG(tB);#endif
free(tTS); } else { return false; }; } catch (const std::exception& e) { LOG_ERROR("Catched Exception!"); return false; }; return true;}
/*
Public wrappers for a subset of ONVIF primitives*/struct tm * boschRCP::core_GetSystemDateAndTime(void){ struct tm * RetVal = NULL; char tURL[MAX_PATH]; if( createRequestString( tURL, BRCP_CMD_GET_DTS, true ) == 0 ){ return NULL; };#ifdef DEBUG_EXTRA
LOG("Before REQ");#endif
if ( _send_request( tURL, NULL, true)) { XMLResults tParseResults; XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults);#ifdef DEBUG_EXTRA
LOG("core_GetSystemDateAndTime");#endif
if (tParseResults.error) { char tBuffer[CTEXT]; sprintf(tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); LOG_ERROR(tBuffer); } else { RetVal = (struct tm *) malloc(sizeof(struct tm)); if (RetVal != NULL) { memset(RetVal, 0, sizeof(struct tm)); XMLNode zNode = response.getChildNode("Body", 0); if (! zNode.isEmpty()) { XMLNode fNode = zNode.getChildNode("GetSystemDateAndTimeResponse", 0); if (! fNode.isEmpty()) { XMLNode hNode = fNode.getChildNode("SystemDateAndTime", 0); if (! hNode.isEmpty()) { XMLNode tNode = hNode.getChildNode("UTCDateTime", 0); if (! tNode.isEmpty()) { RetVal->tm_isdst = 0; XMLNode ttNode = tNode.getChildNode("Time", 0); XMLNode dtNode = tNode.getChildNode("Date", 0); { XMLNode t = ttNode.getChildNode("Hour", 0); RetVal->tm_hour = atoi(t.getText(0)); } { XMLNode t = ttNode.getChildNode("Minute", 0); RetVal->tm_min = atoi(t.getText(0)); } { XMLNode t = ttNode.getChildNode("Second", 0); RetVal->tm_sec = atoi(t.getText(0)); } { XMLNode t = dtNode.getChildNode("Year", 0); RetVal->tm_year = atoi(t.getText(0)); } { XMLNode t = dtNode.getChildNode("Month", 0); RetVal->tm_mon = atoi(t.getText(0)); } { XMLNode t = dtNode.getChildNode("Day", 0); RetVal->tm_mday = atoi(t.getText(0)); } } else { LOG_ERROR("UTCDateTime is Not Found!"); }; } else { LOG_ERROR("SystemDateAndTime is Not Found!"); }; } else { LOG_ERROR("GetSystemDateAndTimeResponse is Not Found!"); }; } else { LOG_ERROR("Body is Not Found!"); LOG(m_pLastResponse); }; }; return RetVal; }; } else { LOG_ERROR("GetSystemDateAndTime: Communication error"); } return NULL;}
bool boschRCP::ptz_GetStatus(CSTR profileToken, ptzStatus * tStatus){ /*if (m_pPTZURI == NULL)
{ return false; } if (tStatus == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); memset(tStatus, 0, sizeof(ptzStatus)); sprintf(tBuffer, PONVIF_PACK_GET_PTZ_STATUS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken); if (_send_request(m_pPTZURI, tBuffer, true)) { XMLResults tParseResults; XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults); if (tParseResults.error) { char tBuf[CTEXT]; sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); LOG_ERROR(tBuf); } else { XMLNode aNode = response.getChildNode("Body", 0); if (aNode.isEmpty()) { return NULL; } XMLNode bNode = aNode.getChildNode("GetStatusResponse", 0); if (! bNode.isEmpty()) { XMLNode cNode = bNode.getChildNode("PTZStatus", 0); if (! cNode.isEmpty()) { XMLNode dNode = cNode.getChildNode("Position", 0); if (! dNode.isEmpty()) { XMLNode eNode = dNode.getChildNode("PanTilt", 0); if (! eNode.isEmpty()) { // XMLAttribute ZZ = eNode.getAttribute(1);
// LOG( ZZ.lpszName );
tStatus->pan = atof(eNode.getAttribute("x")); // ZZ.lpszValue );
// ZZ = eNode.getAttribute(2);
// LOG( ZZ.lpszName );
tStatus->tilt = atof(eNode.getAttribute("y")); // ZZ.lpszValue );
}; XMLNode fNode = dNode.getChildNode("Zoom", 0); if (! fNode.isEmpty()) { // XMLAttribute ZZ = eNode.getAttribute(1);
// LOG( ZZ.lpszName );
tStatus->zoom = atof(fNode.getAttribute("x")); // ZZ.lpszValue );
}; }; if (tStatus->pan != .0f || tStatus->tilt != .0f || tStatus->zoom != .0f) { XMLNode gNode = cNode.getChildNode("MoveStatus", 0); if (! gNode.isEmpty()) { tStatus->hasMoveStatus = true; XMLNode hNode = gNode.getChildNode("PanTilt", 0); if (! hNode.isEmpty()) { if (strstr(hNode.getText(0), "IDLE") != NULL) { tStatus->PanTiltIdle = true; } }; XMLNode iNode = gNode.getChildNode("Zoom", 0); if (! iNode.isEmpty()) { if (strstr(iNode.getText(0), "IDLE") != NULL) { tStatus->ZoomIdle = true; } }; }; } return true; }; }; }; };*/ return false;}
/*
bool boschRCP::core_GetDeviceInformation(){
char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken();
if( createRequestString( tURL, BRCP_CMD_GET_DTS ) == 0 ){ return NULL; }; sprintf(tBuffer, PONVIF_PACK_GET_DEV_INFO, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP); if (_send_request(m_pMediaURI, tBuffer)) { XMLResults tParseResults; XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults); LOG("RCPT:"); printf(m_pLastResponse); if (tParseResults.error) { char tBuf[CTEXT]; sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); LOG_ERROR(tBuf); } else { // return $response['Envelope']['Body']['GetDeviceInformationResponse'];
}; }; return false;}
bool boschRCP::ptz_GetPresets(CSTR profileToken){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); sprintf(tBuffer, PONVIF_PACK_GET_PRESETS, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken); if (_send_request(m_pPTZURI, tBuffer)) { // XMLResults tParseResults;
// XMLNode response = XMLNode::parseString( m_pLastResponse, "root", &tParseResults );
// LOG("RCPT:");
// LOG( m_pLastResponse );
// if( tParseResults.error ){
// char tBuf[CTEXT];
// sprintf( tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
// LOG_ERROR( tBuf );
// } else {
//$getpresetsresponse=$response['Envelope']['Body']['GetPresetsResponse']['Preset'];
//$presets=array();
// foreach ($getpresetsresponse as $preset) {
// $presets[]=array('Token'=>$preset['@attributes']['token'],
// 'Name'=>$preset['Name'],
// 'PTZPosition'=>$preset['PTZPosition']);
// }
// return $presets;
return true; //};
}; return false;}
bool boschRCP::ptz_GetNode(CSTR ptzNodeToken){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); sprintf(tBuffer, PONVIF_PACK_GET_PTZ_NODE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, ptzNodeToken); if (_send_request(m_pPTZURI, tBuffer)) { XMLResults tParseResults; XMLNode response = XMLNode::parseString(m_pLastResponse, "root", &tParseResults); LOG("RCPT:"); printf(m_pLastResponse); if (tParseResults.error) { char tBuf[CTEXT]; sprintf(tBuf, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn); LOG_ERROR(tBuf); } else { // return $response['Envelope']['Body']['GetNodeResponse'];
}; }; return false;}
bool boschRCP::ptz_GotoPreset(CSTR profileToken, CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); 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); return _send_request(m_pPTZURI, tBuffer, true);}
bool boschRCP::ptz_RemovePreset(CSTR profileToken, CSTR presetToken){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); sprintf(tBuffer, PONVIF_PACK_REMOVE_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetToken); return _send_request(m_pPTZURI, tBuffer, true);}
bool boschRCP::ptz_SetPreset(CSTR profileToken, CSTR presetName){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); sprintf(tBuffer, PONVIF_PACK_SET_PRESET, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, presetName); return _send_request(m_pPTZURI, tBuffer, true);}*/bool boschRCP::ptz_RelativeMove(CSTR profileToken, float translation_pantilt_x, float translation_pantilt_y, float speed_pantilt_x, float speed_pantilt_y){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken();
//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);
return _send_request( m_pPTZURI, tBuffer, true );}
bool boschRCP::ptz_RelativeMoveZoom(CSTR profileToken, float zoom, float speedZoom){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); //sprintf(tBuffer, PONVIF_PACK_RELATIVE_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom, speedZoom);
return _send_request(m_pPTZURI, tBuffer, true);}
bool boschRCP::ptz_AbsoluteMove(CSTR profileToken, float position_pantilt_x, float position_pantilt_y, float zoom){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); //sprintf(tBuffer, PONVIF_PACK_ABSOLUTE_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, position_pantilt_x, position_pantilt_y, zoom);
return _send_request(m_pPTZURI, tBuffer, false);}
bool boschRCP::ptz_ContinuousMove(CSTR profileToken, float velocity_pantilt_x, float velocity_pantilt_y){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); //sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_MOVE, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, velocity_pantilt_x, velocity_pantilt_y);
return _send_request(m_pPTZURI, tBuffer, true);}
bool boschRCP::ptz_ContinuousMoveZoom(CSTR profileToken, float zoom){ if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); //sprintf(tBuffer, PONVIF_PACK_CONTINUOUS_ZOOM, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken, zoom);
return _send_request(m_pPTZURI, tBuffer, true);}
bool boschRCP::ptz_Stop(CSTR profileToken, bool pantilt, bool zoom){ if ( pantilt || zoom ) profileToken = profileToken; if (m_pPTZURI == NULL) { return false; } char tBuffer[DEFAULT_BUFFER_SIZE]; accessToken tTOKEN; tTOKEN = _makeToken(); //sprintf(tBuffer, PONVIF_PACK_PTZ_STOP, tTOKEN.USERNAME, tTOKEN.PASSDIGEST, tTOKEN.NONCE, tTOKEN.TIMESTAMP, profileToken); //, pantilt, zoom );
return _send_request(m_pPTZURI, tBuffer, true);}
/*
Internal functions*/accessToken boschRCP::_makeToken(void){ time_t tTS = time(NULL) - m_nDeltaTime; return _passwordDigest(m_pUsername, m_pPassword, tTS);}
/*
void boschRCP::_getOnvifVersion(nodeCapability & caps){ if (caps.Device != NULL) { if (caps.Device->System != NULL) { if (caps.Device->System->SupportedVersions != NULL) { m_sOnvifVersion.Major = caps.Device->System->SupportedVersions->Major; m_sOnvifVersion.Minor = caps.Device->System->SupportedVersions->Minor; }; }; }; if (caps.PTZ != NULL) { m_pPTZURI = caps.PTZ->XAddr; }; if (caps.Media != NULL) { m_pMediaURI = caps.Media->XAddr; }; if (caps.Device != NULL) { m_pDeviceURI = caps.Device->XAddr; };} protected function _getActiveSources($videoSources,$profiles) { $sources=array();
if (isset($videoSources['@attributes'])) { // NVT is a camera
$sources[0]['sourcetoken']=$videoSources['@attributes']['token']; $this->_getProfileData($sources,0,$profiles); } else { // NVT is an encoder
for ($i=0;$i<count($videoSources);$i++) { if (strtolower($videoSources[$i]['@attributes']['SignalActive'])=='true') { $sources[$i]['sourcetoken']=$videoSources[$i]['@attributes']['token']; $this->_getProfileData($sources,$i,$profiles); } } // for
}
return $sources; }
void pOnvif::_getProfileData( sources,$i, XMLNode profiles ){ //$inprofile=0;
videoSourceProfile *tPtr; for( $y=0; $y < count($profiles); $y++ ){ if ($profiles[$y]['VideoSourceConfiguration']['SourceToken']==$sources[$i]['sourcetoken']) { $sources[$i][$inprofile]['profilename']=$profiles[$y]['Name']; $sources[$i][$inprofile]['profiletoken']=$profiles[$y]['@attributes']['token']; if ( isset($profiles[$y]['VideoEncoderConfiguration'])) { $sources[$i][$inprofile]['encoding']=$profiles[$y]['VideoEncoderConfiguration']['Encoding']; $sources[$i][$inprofile]['width']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Width']; $sources[$i][$inprofile]['height']=$profiles[$y]['VideoEncoderConfiguration']['Resolution']['Height']; $sources[$i][$inprofile]['fps']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['FrameRateLimit']; $sources[$i][$inprofile]['bitrate']=$profiles[$y]['VideoEncoderConfiguration']['RateControl']['BitrateLimit']; } if ( isset($profiles[$y]['PTZConfiguration'])) { $sources[$i][$inprofile]['ptz']['name']=$profiles[$y]['PTZConfiguration']['Name']; $sources[$i][$inprofile]['ptz']['nodetoken']=$profiles[$y]['PTZConfiguration']['NodeToken']; } $inprofile++; } }};*//*
protected function _xml2array($response) { $sxe = new SimpleXMLElement($response); $dom_sxe = dom_import_simplexml($sxe); $dom = new DOMDocument('1.0'); $dom_sxe = $dom->importNode($dom_sxe, true); $dom_sxe = $dom->appendChild($dom_sxe); $element = $dom->childNodes->item(0); foreach ($sxe->getDocNamespaces() as $name => $uri) { $element->removeAttributeNS($uri, $name); } $xmldata=$dom->saveXML(); $xmldata=substr($xmldata,strpos($xmldata,"<Envelope>")); $xmldata=substr($xmldata,0,strpos($xmldata,"</Envelope>")+strlen("</Envelope>")); $xml=simplexml_load_string($xmldata); $data=json_decode(json_encode((array)$xml),1); $data=array($xml->getName()=>$data); return $data;}*/
accessToken boschRCP::_passwordDigest(CSTR username, CSTR password, time_t timestamp, uint32_t nonce){ accessToken RetVal; char tTS[CTEXT]; unsigned char * tPDIGEST; if (timestamp == 0) { time_t t; struct tm * tmp; t = time(NULL); tmp = gmtime(&t); if (tmp == NULL) { LOG_ERROR("gmtime Error!"); }; if (strftime(tTS, 200, "%FT%TZ", tmp) == 0) { LOG_ERROR("strftime returned 0!"); }; } else { struct tm * tmp; tmp = gmtime(×tamp); if (tmp == NULL) { LOG_ERROR("gmtime Error!"); }; if (strftime(tTS, 200, "%FT%TZ", tmp) == 0) { LOG_ERROR("strftime returned 0!"); }; }; if (nonce == 0) { nonce = (uint32_t) rand(); }; unsigned char tBuff[CTEXT]; int cp = 0; int index = 0; for (; index < 4; index++) { tBuff[cp++] = ((unsigned char *) (&nonce))[index]; }; index = 0; while (tTS[index] != '\0') { tBuff[cp++] = (unsigned char) tTS[index]; index++; }; index = 0; while (password[index] != '\0') { tBuff[cp++] = (unsigned char) password[index]; index++; }; size_t baseOutLen; unsigned char tSHA[SHA_DIGEST_LENGTH]; SHA1(tBuff, cp, tSHA); // tPDIGEST = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*',$timestamp).pack('a*',$password))));
tPDIGEST = base64_encode(tSHA, SHA_DIGEST_LENGTH, &baseOutLen); // free( tSHA );
//$passdigest=base64_encode(sha1($nonce.$timestamp.$password,true)); // alternative
strncpy(RetVal.USERNAME, username, CTEXT); strncpy(RetVal.TIMESTAMP, tTS, CTEXT); strncpy(RetVal.PASSDIGEST, (const char *) tPDIGEST, CTEXT); free(tPDIGEST); tPDIGEST = base64_encode((unsigned char *) &nonce, 4, &baseOutLen); strncpy(RetVal.NONCE, (const char *) tPDIGEST, baseOutLen); RetVal.NONCE[baseOutLen] = '\0'; free(tPDIGEST); //$REQ['NONCE']=base64_encode($nonce); // alternative
return RetVal;}
void * boschRCP::parser(void * obj, void * ptr){ boschRCP * ME = (boschRCP *) obj; curlRequest * RQ = (curlRequest *) ptr;
if (ME) { if (ME->m_pLastResponse) { free(ME->m_pLastResponse); } ME->m_pLastResponse = (CSTR) malloc(RQ->getSize() + 1); if (ME->m_pLastResponse) { memcpy(ME->m_pLastResponse, RQ->getBuffer(), RQ->getSize()); ME->m_pLastResponse[RQ->getSize()] = (char) 0;#ifdef DEBUG_ONVIF_TRAFFIC
FILE * tf; tf = fopen("Traffic.txt", "a+"); fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n"); fprintf(tf, "<<<<< [%d] %s\n", RQ->getSize(), ME->m_pLastResponse); fprintf(tf, "~~~~~~~~~~~~~~~~~~~~\r\n"); fclose(tf);#endif
}; ME->parser_done = true; }; return NULL;}
bool boschRCP::_send_request(CSTR url, CSTR post_string, bool mustReturnData){#ifdef CURL_MANAGER
bool RetVal = false; if (m_pHttpAccessManager) { m_mResponseProtector.lock(); curlRequest * tRC; tRC = new curlRequest(url, (CSTR) "application/soap+xml; charset=utf-8;"); if (tRC != NULL) { #ifdef DEBUG_BRCP_TRAFFIC
FILE * tf; tf = fopen("Traffic.txt", "a+"); fprintf(tf, "--------------------\r\n"); if ( post_string != NULL ) { fprintf(tf, "REQ: %s (%d)\n", url, strlen(post_string)); fprintf(tf, ">>>>> %s\n", post_string); } else fprintf(tf, "REQ: %s\n", url ); fprintf(tf, "--------------------\r\n"); fclose(tf); mustReturnData = true; // In Debug Allways Read Return
#endif
tRC->setCredentials( m_pUsername, m_pPassword, m_bAuthMode ); if ( post_string != NULL ) { tRC->setPOST(post_string, strlen(post_string)); } if (mustReturnData) { tRC->setHandler((void *) this, boschRCP::parser, (void *) tRC ); if (m_pHttpAccessManager->accomplish(tRC)) { parser_done = false; uint8_t cnt = 200; // TO = 2 second
while (! RetVal && cnt--) { RetVal = parser_done; SLEEP(10); }; }; } else { RetVal = m_pHttpAccessManager->accomplish( tRC ); }; } else { LOG_ERROR("Cannot Create curlRequest!"); } m_mResponseProtector.unlock(); }; return RetVal;#else
char tBuffer[CTEXT]; if (m_pCURL == NULL) { if (m_pLastResponse == NULL) { m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE); }; m_pCURL = curl_easy_init(); if (m_pProxyHost != NULL && m_nProxyPort != 0) { curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost); curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort); if (m_pProxyUsername != NULL && m_pProxyPassword != NULL) { sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword); curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer); }; }; curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1); curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2); curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0); curl_easy_setopt(m_pCURL, CURLOPT_POST, 1); // curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
// curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
// curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1); curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1); // curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer); curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this); }; if (m_pCURL != NULL) { curl_easy_setopt(m_pCURL, CURLOPT_URL, url); struct curl_slist * sList = NULL; // Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
// Connection: Close
sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8"); sprintf(tBuffer, "Content-Length: %d", strlen(post_string)); sList = curl_slist_append(sList, tBuffer); curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList); curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string); sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword); curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer); switch (m_bAuthMode) { case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break; case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break; case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break; case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break; case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break; case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break; case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break; case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break; case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break; case 9: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY); break; // case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
}; #ifdef DEBUG
char tErrorBuffer[CURL_ERROR_SIZE]; curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer); #endif
m_pLastIndex = 0; if (curl_easy_perform(m_pCURL)) { LOG_ERROR("CURL Failure!"); #ifdef DEBUG
LOG_ERROR(tErrorBuffer); #endif
} else { m_pLastResponse[m_pLastIndex] = (char) 0; return true; }; m_pLastIndex = 0; }; return false;#endif
}
#ifndef CURL_MANAGER
int boschRCP::_writer(char * data, size_t size, size_t nmemb, boschRCP * Ptr){ if (Ptr == NULL) { return 0; } // TLOG("*");
memcpy(Ptr->m_pLastResponse + Ptr->m_pLastIndex, data, size * nmemb); Ptr->m_pLastIndex += size * nmemb; return size * nmemb;}#endif
bool boschRCP::setAuthMode(uint8_t Mode){ m_bAuthMode = Mode;#ifdef DEBUG
switch (m_bAuthMode) { case 0 : LOG("Set Auth CURLAUTH_BASIC"); break; case 1 : LOG("Set Auth CURLAUTH_DIGEST"); break; case 2 : LOG("Set Auth CURLAUTH_DIGEST_IE"); break; case 3 : LOG("Set Auth CURLAUTH_BEARER"); break; case 4 : LOG("Set Auth CURLAUTH_NEGOTIATE"); break; case 5 : LOG("Set Auth CURLAUTH_NTLM"); break; case 6 : LOG("Set Auth CURLAUTH_NTLM_WB"); break; case 7 : LOG("Set Auth CURLAUTH_ANY"); break; case 8 : LOG("Set Auth CURLAUTH_ANYSAFE"); break; case 9 : LOG("Set Auth CURLAUTH_ONLY"); break; case 10: LOG("Set Auth CURLAUTH_AWS_SIGV4"); break; };#endif
return true;}
#undef LOG
#undef TLOG
#undef LOG_ERROR
|