diff --git a/Projects/la_mojo/build b/Projects/la_mojo/build index e30c9046..59699e61 100755 --- a/Projects/la_mojo/build +++ b/Projects/la_mojo/build @@ -4,7 +4,7 @@ #make clean -e "CFG=Debug" #make -e "CFG=Debug" -j6 -#make clean -e "CFG=Debug86" +make clean -e "CFG=Debug86" make -e "CFG=Debug86" -j4 #make clean -e "CFG=Release86" diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index 494988b6..079be40f 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -766,6 +766,10 @@ uint32_t sqliteDBC::addEvent( DBC_EVT_RECORD *ptr ){ case EVT_FLT_LATF_DISABLED: case EVT_FLT_LATF_SUSP_DET: case EVT_FLT_LATF_DIFF_DET: + case EVT_FLT_LATF_OP_DISABLED: + case EVT_FLT_LATF_OP_TD: + case EVT_FLT_LATF_OP_TG: + case EVT_FLT_LATF_OP_EXP: case EVT_OP_ACK_MASK: case EVT_OP_NACK_MASK: case EVT_OP_ON_MASK: @@ -2881,6 +2885,14 @@ TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){ if( it != cArr->end() ){ tVal->bTrackTrain = (bool)atoi( it->second.c_str() ); }; + it = cArr->find("AUX_IP"); + if( it != cArr->end() ){ + strncpy( tVal->aux_ip, it->second.c_str(), 15 ); + }; + //it = cArr->find("AUX_PORT"); + //if( it != cArr->end() ){ + // tVal->aux_port = atoi( it->second.c_str() ); + //}; it = cArr->find("DEBUG"); if( it != cArr->end() ){ tVal->bDebugMode = (bool)atoi( it->second.c_str() ); diff --git a/lib/App/common/dataBinder_structures.h b/lib/App/common/dataBinder_structures.h index 9ee7bf68..d32625c6 100644 --- a/lib/App/common/dataBinder_structures.h +++ b/lib/App/common/dataBinder_structures.h @@ -706,6 +706,8 @@ typedef struct S_TRAIN_FILTER_DATA{ uint32_t nTrainWidth; uint32_t nDecisionTime; uint32_t nSuspectTime; + char aux_ip[16]; + uint16_t aux_port; }TRAIN_FILTER_DATA; typedef struct S_VIDEO_SEGMENT_INFO{ diff --git a/lib/App/common/deviceNode2D.cpp b/lib/App/common/deviceNode2D.cpp index 6f0cb173..62e46b37 100644 --- a/lib/App/common/deviceNode2D.cpp +++ b/lib/App/common/deviceNode2D.cpp @@ -1328,7 +1328,8 @@ void device2DSensor::save_masks(){ } else transactionWaitResult(); break; }; - case LRF_WS_STAGE_3:{ + // V 3 4 ... B 5 6 .. E 7 + case LRF_WS_STAGE_5:{ m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER; if( m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ){ m_sShadowHelper.m_nCurrentPoint = 0; @@ -1348,7 +1349,7 @@ void device2DSensor::save_masks(){ if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) { LOG("MooMoo 1"); //if we have an shadows -> transfer else skeep - setStage( LRF_WS_STAGE_5 ); + setStage( LRF_WS_STAGE_7 ); } else { #ifdef DEBUG_EXTRA LOG("Skip Uploading Rays"); @@ -1358,7 +1359,7 @@ void device2DSensor::save_masks(){ }; break; }; - case LRF_WS_STAGE_4:{ + case LRF_WS_STAGE_6:{ m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER; #ifdef DEBUG_EXTRA char tBuff[128]; @@ -1372,11 +1373,12 @@ void device2DSensor::save_masks(){ } else transactionWaitResult(); } else { m_sShadowHelper.m_nCurrentPolygon ++; - setStage( LRF_WS_STAGE_3 ); + setStage( LRF_WS_STAGE_5 ); }; break; }; - case LRF_WS_STAGE_5:{ + + case LRF_WS_STAGE_3:{ m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER; #ifdef DEBUG_EXTRA char tBuff[128]; @@ -1389,7 +1391,7 @@ void device2DSensor::save_masks(){ } else transactionWaitResult(); break; }; - case LRF_WS_STAGE_6:{ + case LRF_WS_STAGE_4:{ m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER; #ifdef DEBUG_EXTRA char tBuff[128]; @@ -1407,7 +1409,7 @@ void device2DSensor::save_masks(){ m_sShadowHelper.m_nCurrentRayLine++; } while ( ( m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount == 0 ) && m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ); if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) { - setStage( LRF_WS_STAGE_5 ); + setStage( LRF_WS_STAGE_3 ); } else { // If not found -> finish upload shadow transactionSetResult( TRANSACTION_RESULT_OK ); diff --git a/lib/App/common/kernelEventFilterTrain.cpp b/lib/App/common/kernelEventFilterTrain.cpp index 7f882f03..61f93410 100644 --- a/lib/App/common/kernelEventFilterTrain.cpp +++ b/lib/App/common/kernelEventFilterTrain.cpp @@ -35,6 +35,15 @@ bool kernelEventFilterTrain::validate(){ return kernelEventFilter::validate(); }; +void kernelEventFilterTrain::createEvent( uint8_t EVT ){ + if( m_sTrainFilterData.bDebugMode ){ + //Train Position Update Event + createFilterEvent( (basicEventTypeID) EVT ); + // Proceed Event as normal + // return false; + }; +}; + bool kernelEventFilterTrain::validateTarget( uint32_t DevID, TARGET_DATA *ptr ){ if( m_bActive && ( ptr != NULL ) ){ #ifdef DEBUG_EXTRA @@ -55,6 +64,10 @@ bool kernelEventFilterTrain::validateTarget( uint32_t DevID, TARGET_DATA *ptr ){ m_sUpperZone.TrainOnTheField = false; m_sUpperZone.startAngle = 0; m_sUpperZone.endAngle = 0; +#ifdef SECOND_PEER_PORT + // Inform Other Peer + m_oOSP_Helper->setLocalStatus( m_sUpperZone.TrainOnTheField ); +#endif }; }; if( m_sUpperZone.SuspiciousUnconfirmed ){ @@ -89,7 +102,7 @@ bool kernelEventFilterTrain::validateTarget( uint32_t DevID, TARGET_DATA *ptr ){ }; }; - // Check Current Target + // Check Current Target float tAngleWidth; if( ptr->digital > 0 ) { tAngleWidth = ( 360.0f * ( ptr->area / (float)ptr->digital) ) / 12000.0f; @@ -115,6 +128,10 @@ bool kernelEventFilterTrain::validateTarget( uint32_t DevID, TARGET_DATA *ptr ){ } else { // Newly Detected Train m_sUpperZone.TrainOnTheField = true; +#ifdef SECOND_PEER_PORT + // Inform Other Peer + m_oOSP_Helper->setLocalStatus( m_sUpperZone.TrainOnTheField ); +#endif // Clear Suspicies - Train Detected! m_sUpperZone.SuspiciousObject = false; m_sUpperZone.SuspiciousUnconfirmed = false; @@ -135,6 +152,15 @@ bool kernelEventFilterTrain::validateTarget( uint32_t DevID, TARGET_DATA *ptr ){ if( m_sBottomZone.SuspiciousObject == false ){ // No Train is detected by Upper Sensor Yet if( m_sUpperZone.TrainOnTheField == false ){ +#ifdef SECOND_PEER_PORT + // Check Other Peer + if ( m_oOSP_Helper->getRemoteStatus() ){ + if( m_sTrainFilterData.bDebugMode ){ + createFilterEvent( EVT_FLT_LATF_OP_DISABLED ); + }; + return true; + }; +#endif //if( m_sBottomZone.TrainOnTheField == false ){ // If the object is suspicious if( m_sBottomZone.SuspiciousUnconfirmed == false ){ @@ -200,8 +226,18 @@ bool kernelEventFilterTrain::initFilter(){ memcpy( &m_sTrainFilterData, tData, sizeof( TRAIN_FILTER_DATA )); free( tData ); if( m_sTrainFilterData.nUpperSensorID && m_sTrainFilterData.nBottomSensorID ) { +#ifdef SECOND_PEER_PORT + m_oOSP_Helper = new TrainFilterOSP_Helper( m_sTrainFilterData.aux_ip ); + if( m_oOSP_Helper != NULL ){ + if( m_oOSP_Helper->create( this ) ){ + LOG("Init Filter Done!"); + m_bActive = true; + }; + }; +#else LOG("Init Filter Done!"); m_bActive = true; +#endif }; }; }; @@ -217,4 +253,172 @@ bool kernelEventFilterTrain::stopFilter(){ #ifdef SECOND_PEER_PORT -#endif \ No newline at end of file +#undef LOG +#undef LOG_ERROR + +#define LOG( X ) m_pLog->Log( "TTF-OSP", this, X ) +#ifdef DEBUG + #define LOG_ERROR( X ) {\ + char _tBfrErrMSG2w34[256];\ + sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ + m_pLog->Log( "TTF-OSP", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ + } +#else + #define LOG_ERROR( X ) m_pLog->Log( "TTF-OSP", this, _LOG_ERROR, X ); +#endif + +TrainFilterOSP_Helper::TrainFilterOSP_Helper( CSTR remote_ip ){ + m_pLog = basicLogCollection::getInstance(); + m_iSocketUDP = -1; + m_bStage = OSP_NONE; + m_bValid = false; + if ( remote_ip ) { + memcpy( m_cRemoteHostName, remote_ip, strlen( remote_ip )); + } else { + memset(m_cRemoteHostName, 0, 256); + }; +}; + +TrainFilterOSP_Helper::~TrainFilterOSP_Helper(){ + waitThreadEnd( true ); +}; + +//https://www.geeksforgeeks.org/udp-server-client-implementation-c/ + +bool TrainFilterOSP_Helper::create( kernelEventFilterTrain *MF ){ + bool rv = false; + m_pMasterFilter = MF; + if( ( m_iSocketUDP = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){ + LOG_ERROR("socket creation failed"); + } else { + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + // Filling server information + servaddr.sin_family = AF_INET; // IPv4 + servaddr.sin_addr.s_addr = INADDR_ANY; + servaddr.sin_port = htons( SECOND_PEER_PORT ); + // Bind the socket with the server address + if ( bind( m_iSocketUDP, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) { + LOG_ERROR("Port Bind Failed"); + } else rv = init(); + }; + return rv; +}; + +//Server Tread +void TrainFilterOSP_Helper::m_fThreadProc( void ){ + socklen_t len; + struct sockaddr_in cliaddr; + int n; + char buffer[255]; + memset( &cliaddr, 0, sizeof(cliaddr) ); + m_bThreadStatus = TH_WORKING; + while( m_bThreadStatus < TH_ENDED ){ + len = sizeof(cliaddr); + n = recvfrom( m_iSocketUDP, buffer, 255, MSG_DONTWAIT/*MSG_WAITALL*/, ( struct sockaddr *) &cliaddr, &len ); + if( n >= 0 ){ + char tB[512]; + buffer[n] = (char)0; + sprintf( tB, "From Other Peer: %s", buffer ); + LOG(tB); + m_mStatusProtector.lock(); + m_bRemoteStatus = (bool) atoi( buffer ); + m_mStatusProtector.unlock(); + m_lTOTS = MAXIMAL_INFO_VALID_TO; + m_bValid = true; + if( m_pMasterFilter ){ + // Create Event via Master Filter + if( m_bRemoteStatus ){ + m_pMasterFilter->createEvent( EVT_FLT_LATF_OP_TD ); + } else { + m_pMasterFilter->createEvent( EVT_FLT_LATF_OP_TG ); + }; + }; + //sendto( m_iSocketUDP, tB, strlen( tB ), MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len ); + //LOG("Hello message sent."); + } else { + if ( errno != EAGAIN || errno != EWOULDBLOCK ) { + m_bThreadStatus = TH_ENDED; + LOG_ERROR("Recieving Peer Error!"); + continue; + }; + }; + SLEEP( DEFAULT_THREAD_SLEEP_TIME ); + if ( m_lTOTS > DEFAULT_THREAD_SLEEP_TIME ) { + m_lTOTS -=DEFAULT_THREAD_SLEEP_TIME; + } else { + if ( m_bValid ) { + m_lTOTS = 0; + m_bValid = false; + if ( m_pMasterFilter ) { + LOG("EVT 3"); + // Create Event via Master Filter + m_pMasterFilter->createEvent( EVT_FLT_LATF_OP_EXP ); + }; + } + }; + }; + m_bStage = OSP_END; +}; + +bool TrainFilterOSP_Helper::getRemoteStatus(){ + if (!m_bValid) return false; + m_mStatusProtector.lock(); + bool rv = m_bRemoteStatus; + m_mStatusProtector.unlock(); + return rv; +}; + +bool TrainFilterOSP_Helper::setLocalStatus( bool train_on_line ){ + if ( m_bLocalStatus != train_on_line ) { + m_mStatusProtector.lock(); + m_bLocalStatus = train_on_line; + m_mStatusProtector.unlock(); + if( strlen( m_cRemoteHostName ) ){ + int sockfd; + char buffer[512]; + struct sockaddr_in servaddr; + + // Creating socket file descriptor + if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){ + LOG_ERROR("Socket Creation Failed!"); + return false; + }; + + memset(&servaddr, 0, sizeof(servaddr)); + + // Filling server information + servaddr.sin_family = AF_INET; + struct hostent *server = gethostbyname( m_cRemoteHostName ); + if( server == NULL ){ + sprintf( buffer, "ERROR, no such host [%s]", m_cRemoteHostName ); + LOG_ERROR( buffer ); + return false; + }; + //servaddr.sin_addr.s_addr = INADDR_ANY; + bcopy((char *)server->h_addr, (char *)&servaddr.sin_addr.s_addr, server->h_length ); + servaddr.sin_port = htons( SECOND_PEER_PORT ); + int n; + socklen_t len; + + sprintf( buffer, "%d", train_on_line ); + sendto( sockfd, (const char *)buffer, strlen(buffer), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr) ); + LOG( "Status message sent." ); + + //n = recvfrom( sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *) &servaddr, &len ); + //buffer[n] = '\0'; + //std::cout<<"Server :"< groupArray; @@ -15,17 +17,49 @@ typedef struct S_EFT_ZONE { float endAngle; } EFT_ZONE; +class kernelEventFilterTrain; + #ifdef SECOND_PEER_PORT + #include + #include + #include + #include + #include + + + + #define OSP_NONE 0x00 + #define OSP_INIT 0x01 + #define OSP_IDLE 0x02 + #define OSP_END 0xFF + + #define MAXIMAL_INFO_VALID_TO 60000 + + /* Other Sensor Peer Helper - checking if the paired sensor module has train detection */ class TrainFilterOSP_Helper: public basicThread{ private: - uint8_t m_bStage; + basicLogCollection * m_pLog; + kernelEventFilterTrain * m_pMasterFilter; + int m_iSocketUDP; + uint64_t m_lTOTS; + uint8_t m_bStage; + bool m_bLocalStatus; + bool m_bRemoteStatus; + bool m_bValid; + basicMutex m_mStatusProtector; + char m_cRemoteHostName[256]; protected: + virtual void m_fThreadProc( void ); public: - TrainFilterOSP_Helper(); + explicit TrainFilterOSP_Helper( CSTR remote_ip = NULL ); ~TrainFilterOSP_Helper(); + bool getRemoteStatus(); + bool setLocalStatus( bool train_on_line ); + bool create( kernelEventFilterTrain *MF = NULL ); + bool validate(); }; #endif @@ -47,6 +81,9 @@ class kernelEventFilterTrain: public kernelEventFilter{ virtual bool initFilter(); virtual bool stopFilter(); virtual bool validate(); +#ifdef SECOND_PEER_PORT + void createEvent( uint8_t EVT ); +#endif }; #endif \ No newline at end of file diff --git a/lib/components/base/basicEvent.h b/lib/components/base/basicEvent.h index 87964c01..19ebaacc 100644 --- a/lib/components/base/basicEvent.h +++ b/lib/components/base/basicEvent.h @@ -101,7 +101,11 @@ typedef enum E_basicEventTypeID { EVT_OP_NACK_MASK = 69, EVT_OP_ON_MASK = 70, EVT_OP_OFF_MASK = 71, - EVT_COUNT = 72 + EVT_FLT_LATF_OP_DISABLED= 72, + EVT_FLT_LATF_OP_TD = 73, + EVT_FLT_LATF_OP_TG = 74, + EVT_FLT_LATF_OP_EXP = 75, + EVT_COUNT = 76 } basicEventTypeID;