#include "protocolPTZ_BRCP.h" #include "../../hardware/uart/serialPort.h" #define LOG(X) m_pLog->Log("BRCP", this, X) #define LOG_ERROR(X) m_pLog->Log("BRCP", this, _LOG_ERROR, X) protocolPTZ_BRCP::protocolPTZ_BRCP(config_setting_t * Settings) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = Settings; m_bAuthMode = 0; boschRCP = NULL; m_pProfile = NULL; memset(m_pDeviceURI, 0, MAX_PATH_LEN); memset(m_pUser, 0, MAX_CRED_LEN); memset(m_pPassword, 0, MAX_CRED_LEN); } protocolPTZ_BRCP::protocolPTZ_BRCP(CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = NULL; boschRCP = NULL; m_pProfile = NULL; LOG(ip); LOG(username); LOG(password); m_bAuthMode = AuthMode; sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode); LOG(m_pDeviceURI); if (port == 80) { sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip); } else { sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port); } LOG(m_pDeviceURI); strncpy(m_pUser, username, MAX_CRED_LEN); strncpy(m_pPassword, password, MAX_CRED_LEN); } protocolPTZ_BRCP::protocolPTZ_BRCP(CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = NULL; boschRCP = NULL; m_pProfile = NULL; LOG(ctl_url); LOG(username); LOG(password); m_bAuthMode = AuthMode; sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode); LOG(m_pDeviceURI); strncpy(m_pDeviceURI, ctl_url, 250); strncpy(m_pUser, username, MAX_CRED_LEN); strncpy(m_pPassword, password, MAX_CRED_LEN); } protocolPTZ_BRCP::~protocolPTZ_BRCP() {} bool protocolPTZ_BRCP::setIPAddress(CSTR ip) { if (ip == NULL) { return false; } if (strlen(ip) == 0) { return false; } sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip); // strncpy( m_pDeviceURI, ip, MAX_PATH_LEN ); return true; } bool protocolPTZ_BRCP::setUsername(CSTR username) { if (username == NULL) { return false; } if (strlen(username) == 0) { return false; } strncpy(m_pUser, username, MAX_CRED_LEN); return true; } bool protocolPTZ_BRCP::setPassword(CSTR password) { if (password == NULL) { return false; } if (strlen(password) == 0) { return false; } strncpy(m_pPassword, password, MAX_CRED_LEN); return true; } bool protocolPTZ_BRCP::setAuthMode(uint8_t Mode) { m_bAuthMode = Mode; return true; } bool protocolPTZ_BRCP::create(void) { if (! protocolPTZ::create()) { return false; } if (m_pSettings != NULL) { config_setting_t * pSetting; pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER); if (pSetting == NULL) { return false; } sprintf(m_pUser, "%s", config_setting_get_string(pSetting)); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS); if (pSetting == NULL) { return false; } sprintf(m_pPassword, "%s", config_setting_get_string(pSetting)); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI); if (pSetting == NULL) { return false; } sprintf(m_pDeviceURI, "%s", config_setting_get_string(pSetting)); }; #ifdef DEBUG LOG(m_pDeviceURI); #endif if (strlen(m_pDeviceURI) < 4) { LOG_ERROR("No Device URI Is Set!"); return false; }; #ifdef USE_PONVIF boschRCP = new boschRCP(); if (boschRCP == NULL) { LOG_ERROR("boschRCP NEW FAILURE!"); m_bStatus = 0; return false; }; boschRCP->setDeviceUri(m_pDeviceURI); boschRCP->setUsername(m_pUser); boschRCP->setPassword(m_pPassword); boschRCP->setAuthMode(m_bAuthMode); LOG("PROTOCREATE"); if (boschRCP->create()) { m_pProfile = boschRCP->getProfiles()->at(0); LOG("DONE"); m_bStatus = 1; return true; } else { LOG_ERROR("pOnvif CREATE FAILURE!"); delete(m_pOnVif); m_pOnVif = NULL; m_bStatus = 0; return false; }; #else std::string A(m_pUser); std::string B(m_pPassword); std::string C(m_pDeviceURI); m_pDevice = new OnvifClientDevice(C, A, B); if (m_pDevice != NULL) { m_pPTZ = new OnvifClientPTZ(*m_pDevice); if (m_pPTZ != NULL) { _tptz__GetNodesResponse tNodes; int RetVal; RetVal = m_pPTZ->GetNodes(tNodes); if (RetVal == 0) { OnvifClientMedia media(*m_pDevice); _trt__GetProfilesResponse profiles; media.GetProfiles(profiles); m_pPTZToken = profiles.Profiles.at(0)->token; // PTZConfiguration->token; // m_pPTZToken = tNodes.PTZNode.at(0)->token; //_tptz__GetConfigurationOptionsResponse COResp; // int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken); //_tptz__GetConfigurationResponse configResponce; // configResponce.PTZConfiguration = new tt__PTZConfiguration(); // m_pPTZ->GetConfiguration(configResponce); // m_pToken = configResponce.PTZConfiguration->token; setConnectionStatus(CS_CONNECTED); m_bStatus = 1; return true; }; }; }; #endif return false; } #ifndef USE_PONVIF bool protocolPTZ_BRCP::initCamera(void) { if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false; m_bLastCommandIssued = CMD_PTZ_INIT; m_pPTZ->Initialize(); return true; } #endif bool protocolPTZ_BRCP::move(uint32_t MoveMatrix) { // if( m_bStatus == 0 ) return false; #ifdef USE_PONVIF if (m_pProfile == NULL) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; float Speed_x, Speed_y, Speed_z; if (MoveMatrix > 0) { if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0) { Speed_x = 0.0f; } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0) { Speed_x = -m_dPanSpeed; } else { Speed_x = m_dPanSpeed; }; if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0) { Speed_y = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0) { Speed_y = m_dTiltSpeed; } else { Speed_y = -m_dTiltSpeed; }; if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0) { Speed_z = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0) { Speed_z = m_dZoomSpeed; } else { Speed_z = -m_dZoomSpeed; }; bool res = true; if ((MoveMatrix & (PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK)) != 0) { res = m_pOnVif->ptz_ContinuousMove(m_pProfile->profiletoken, Speed_x, Speed_y); }; if (res) { if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0) { res = m_pOnVif->ptz_ContinuousMoveZoom(m_pProfile->profiletoken, Speed_z); }; }; m_bStatus = (res) ? 1 : 0; } else { m_bStatus = (m_pOnVif->ptz_Stop(m_pProfile->profiletoken, true, true)) ? 1 : 0; }; return (m_bStatus > 0); #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; _tptz__ContinuousMoveResponse ContMoveResponse; tt__PTZSpeed tSpeed; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); LONG64 to = 1000; int RetVal; if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0) { tSpeed.PanTilt->x = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0) { tSpeed.PanTilt->x = -m_dPanSpeed; } else { tSpeed.PanTilt->x = m_dPanSpeed; }; if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0) { tSpeed.PanTilt->y = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0) { tSpeed.PanTilt->y = m_dTiltSpeed; } else { tSpeed.PanTilt->y = -m_dTiltSpeed; }; if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0) { tSpeed.Zoom->x = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0) { tSpeed.Zoom->x = m_dZoomSpeed; } else { tSpeed.Zoom->x = -m_dZoomSpeed; }; RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); if (RetVal) { return false; } return true; #endif } #include bool protocolPTZ_BRCP::waitMoveIsDone(void) { ptzStatus tVal; uint8_t TO = 12; // 3.6 second max while (m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal) && TO) { if (tVal.hasMoveStatus) { if (tVal.PanTiltIdle && tVal.ZoomIdle) { break; } } else { // if( ( fabs(tVal.pan - dPan) < 0.0005 ) && ( fabs( tVal.tilt - dTilt) < 0.0005 ) ) break;//&& ( fabs( tVal.zoom - dZoom ) < 0.05 ) ) break; }; SLEEP(333); TO--; }; if (TO) { #ifdef DEBUG_EXTRA LOG("Finished Move!"); #endif return true; #ifdef DEBUG_EXTRA } else { LOG_ERROR("Error Fetching PTZ Status!"); #endif }; return false; } bool protocolPTZ_BRCP::goAbsolute(float dPan, float dTilt, float dZoom) { #ifdef USE_PONVIF // if (m_bStatus == 0) return false; #ifdef DEBUG_EXTRA LOG("ABS Move"); #endif if (m_pProfile == NULL) { LOG_ERROR("No PTZ Profile!!"); return false; }; if (dZoom < 0.0f) { dZoom = 0.0f; } if (m_pOnVif->ptz_AbsoluteMove(m_pProfile->profiletoken, dPan, dTilt, dZoom)) { }; return true; #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; _tptz__AbsoluteMoveResponse AbsMoveResponse; tt__PTZSpeed tSpeed; tt__PTZVector tPos; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); tSpeed.PanTilt->x = 1.f; tSpeed.PanTilt->y = 1.f; tSpeed.Zoom->x = 1.f; tPos.PanTilt = new tt__Vector2D(); tPos.Zoom = new tt__Vector1D(); tPos.PanTilt->x = dPan; tPos.PanTilt->y = dTilt; tPos.Zoom->x = dZoom; int RetVal; RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); delete(tPos.PanTilt); delete(tPos.Zoom); return true; #endif } bool protocolPTZ_BRCP::getCameraPresets(void) { LOG("GET PRESETS!"); return m_pOnVif->ptz_GetPresets(m_pProfile->profiletoken); } bool protocolPTZ_BRCP::goPreset(char * PresetToken) { #ifdef USE_PONVIF // if (m_bStatus == 0) return false; #ifdef DEBUG_EXTRA LOG("Preset Move"); #endif if (m_pProfile == NULL) { LOG_ERROR("No PTZ Profile!!"); return false; }; if (! m_pOnVif->ptz_GotoPreset(m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f)) { m_bStatus = 0; } return (m_bStatus > 0); #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; /* _tptz__AbsoluteMoveResponse AbsMoveResponse; tt__PTZSpeed tSpeed; tt__PTZVector tPos; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); tSpeed.PanTilt->x = 1.f; tSpeed.PanTilt->y = 1.f; tSpeed.Zoom->x = 1.f; tPos.PanTilt = new tt__Vector2D(); tPos.Zoom = new tt__Vector1D(); tPos.PanTilt->x = dPan; tPos.PanTilt->y = dTilt; tPos.Zoom->x = dZoom; int RetVal; RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); delete(tPos.PanTilt); delete(tPos.Zoom); */ return true; #endif } bool protocolPTZ_BRCP::validate() { return (m_bStatus > 0); } #undef LOG #undef LOG_ERROR