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

#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]={
"WRITE",
"READ"
};
#define F_FLAG 0
#define T_OCTET 1
#define T_WORD 2
#define T_INT 3
#define T_DWORD 4
#define P_OCTET 5
#define P_STRING 6
#define P_UNICODE 7
boschRCP::boschRCP(){
m_pLog = basicLogCollection::getInstance();
m_pIpAddress = NULL;
m_pUsername = NULL;
m_pPassword = NULL;
m_pDeviceURI = 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_pHttpAccessManager = 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_pDeviceURI) free(m_pDeviceURI);
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_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;
LOG("Device URI set explicitly");
LOG( deviceUri );
// 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("Host/Ip");
LOG(m_pIpAddress);
};
};
return true;
}
CSTR boschRCP::getIPAddress(void)
{
return m_pIpAddress;
}
bool boschRCP::updateDevURI( void ){
if( m_pIpAddress == NULL ) return false;
if( m_pDeviceURI ) free( m_pDeviceURI );
char tBuffer[512];
//if ( m_pUsername ) {
// if ( m_pPassword ) {
// sprintf(tBuffer, BRCP_DEFAULT_URI_TPL, m_pUsername, m_pPassword, m_pIpAddress );
// } else {
// sprintf(tBuffer, BRCP_DEFAULT_URI_TPL, m_pUsername, "", m_pIpAddress );
// };
//} else {
sprintf( tBuffer, BRCP_DEFAULT_URI_TPL_NA, m_pIpAddress );
//};
size_t len = strlen( tBuffer );
m_pDeviceURI = (CSTR)malloc(len + 1);
if( m_pDeviceURI ){
strncpy( m_pDeviceURI, tBuffer, len );
m_pDeviceURI[len] = (char)0;
return true;
};
return false;
}
bool boschRCP::setIPAddress( CSTR ipAddress ){
if( m_pIpAddress!= NULL) free(m_pIpAddress);
if( ipAddress == NULL || strlen(ipAddress) == 0) return false;
size_t len = strlen(ipAddress);
m_pIpAddress = (CSTR) malloc( len + 1);
if( m_pIpAddress == NULL ) return false;
strncpy( m_pIpAddress, ipAddress, len );
m_pIpAddress[len] = (char) 0;
return updateDevURI();
}
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&payload=%s",
m_pDeviceURI,
CMD,
BRPC_TYPE_TAG[pl_type],
BRPC_OP_TAG[RW],
(CSTR)pl
);
} 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 true; //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( ptzStatus * tStatus ){
if (tStatus == NULL) return false;
char tBuffer[DEFAULT_BUFFER_SIZE];
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::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;
}
*/
// here the IN coordinates are OnVIF -1 ... 0 ... 1 except zoom 0 .. 1
bool boschRCP::ptz_AbsoluteMove( float position_pantilt_x, float position_pantilt_y, float zoom){
char tBuffer[DEFAULT_BUFFER_SIZE];
char ptzCOM[512];
#ifdef DEBUG_EXTRA
sprintf( tBuffer, " Pan: %0.3f Tilt: %0.3f Zoom: %0.3f", position_pantilt_x, position_pantilt_y, zoom );
LOG( tBuffer );
#endif
uint16_t pPos = ((uint16_t) ( 18000.0 + 18000.0 * position_pantilt_x)) % 36000;
uint16_t tPos = ((uint16_t) ( 9000.0 + 9000.0 * position_pantilt_y)) % 36000;
uint16_t zPos = ( uint16_t )( 16384 * zoom );
#ifdef DEBUG_EXTRA
sprintf( tBuffer, " CPOS: %d Tilt: %d Zoom: %d", pPos, tPos, zPos );
LOG( tBuffer );
#endif
sprintf( ptzCOM,"0x80000601D102%04X%04X%04X0000&num=1", pPos, tPos, zPos );
if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
return _send_request( tBuffer, NULL, false );
}
bool boschRCP::ptz_ContinuousMove( float velocity_pantilt_x, float velocity_pantilt_y){
char tBuffer[DEFAULT_BUFFER_SIZE];
return _send_request( tBuffer, tBuffer, true);
}
bool boschRCP::ptz_ContinuousMoveZoom( float speedZoom ){
char tBuffer[DEFAULT_BUFFER_SIZE];
char ptzCOM[512];
uint8_t Speed;
// 1 - 15
if ( speedZoom < 0 ) {
Speed = 1 + (speedZoom * -14.0f);
sprintf( ptzCOM,"0x80000601108500000%01X&num=1", Speed );
} else {
Speed = 1 + (speedZoom * 14.0f);
sprintf( ptzCOM,"0x80000601108500008%01X&num=1", Speed );
};
if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
LOG( tBuffer );
return _send_request( tBuffer, NULL, false );
}
bool boschRCP::ptz_Stop( bool pantilt, bool zoom){
char tBuffer[DEFAULT_BUFFER_SIZE];
if( pantilt || zoom ){
char ptzCOM[512];
sprintf( ptzCOM,"0x800006011085000000&num=1" );
if( createRequestString( tBuffer, BRCP_CMD_PTZ_DRIVE, false, P_OCTET, ptzCOM ) == 0 ) return false;
return _send_request( tBuffer, NULL, false );
};
return false;
}
bool boschRCP::ptz_GotoPreset( CSTR presetToken, float speed_pantilt_x, float speed_pantilt_y, float speed_zoom_x){
char tBuffer[DEFAULT_BUFFER_SIZE];
return _send_request( tBuffer, NULL, false );
}
bool boschRCP::ptz_RemovePreset( CSTR presetToken){
char tBuffer[DEFAULT_BUFFER_SIZE];
return _send_request( tBuffer, NULL, false );
}
bool boschRCP::ptz_SetPreset( CSTR presetName){
char tBuffer[DEFAULT_BUFFER_SIZE];
return _send_request( tBuffer, NULL, false );
}
/*
Internal functions
*/
/*
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(&timestamp);
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_BRCP_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,"Creds: [%s]:[%s]\n",m_pUsername, m_pPassword );
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