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.

3617 lines
174 KiB

4 years ago
​
​
​
​
​
4 years ago
3 years ago
​
4 years ago
3 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
4 years ago
​
4 years ago
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
3 years ago
​
​
​
​
4 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
3 years ago
​
​
​
​
​
4 years ago
4 years ago
3 years ago
​
4 years ago
​
3 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
3 years ago
​
4 years ago
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
3 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
4 years ago
​
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
​
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
​
​
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
​
​
4 years ago
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
4 years ago
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
3 years ago
4 years ago
4 years ago
3 years ago
​
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
3 years ago
4 years ago
4 years ago
​
​
​
3 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
​
4 years ago
​
​
4 years ago
​
4 years ago
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
4 years ago
​
4 years ago
​
​
4 years ago
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
4 years ago
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
4 years ago
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
3 years ago
4 years ago
3 years ago
​
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
​
4 years ago
​
4 years ago
​
​
3 years ago
4 years ago
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
​
4 years ago
​
4 years ago
4 years ago
4 years ago
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
​
4 years ago
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
4 years ago
​
​
​
3 years ago
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
3 years ago
  1. #include "deviceNode2D.h"
  2. #include "hardwareResourceSerialProvider.h"
  3. #include "kernelPrototype.h"
  4. #include "hardwareManager.h"
  5. #define LGS_MAX_DETECTOR_RANGE 2047
  6. #define DEBUG_EXTRA
  7. #undef LOG
  8. #undef LOG_ERROR
  9. #define LOG( X ) deviceNode::m_pLog->Log( "NODE2D", this, X )
  10. #ifdef DEBUG
  11. #define LOG_ERROR( X ) {\
  12. char _tBfrErrMSG2w34[256];\
  13. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  14. deviceNode::m_pLog->Log( "NODE2D", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  15. }
  16. #else
  17. #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "NODE2D", this, _LOG_ERROR, X );
  18. #endif
  19. #define DELAY_10MSW( X ) { for( uint8_t iii = X; iii && ( m_bThreadStatus < TH_ENDED ) && ( LRF_WS_WAIT == getStage() ); iii-- ) SLEEP( 10 ); }
  20. device2DSensor::device2DSensor( uint32_t id ):deviceNodeSensor( id ), protocolLGS_2D(){
  21. m_pPort = NULL;
  22. m_bPortType = 0;
  23. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  24. m_nProcessingCmdId = 0;
  25. m_bDeviceIsSynced = false;
  26. m_bForceSync = false; // To Skip Settings and FS readout
  27. memset( m_vDrawRanges, 0, sizeof( m_vDrawRanges ));
  28. memset( &m_sShadowHelper, 0 , sizeof(SHADOW_IO_HELPER));
  29. memset( &m_sShadow, 0 , sizeof(SHADOW));
  30. cleanupDeviceStructures();
  31. if( m_sLRF_DATA.uart_id ) {
  32. DBC_UART_DATA *tUART = NULL;
  33. tUART = m_pDBC->getUartData( m_sLRF_DATA.uart_id );
  34. if( tUART!= NULL ) {
  35. m_bPortType = tUART->type;
  36. free( tUART );
  37. };
  38. };
  39. };
  40. void device2DSensor::cleanupDeviceStructures( void ){
  41. //setWM( LRF_WM_NONE );
  42. //setWM( LRF_WM_NONE ); // To prevent OWM keeping prev value
  43. m_bActualDeviceWM = MODE_NONE;
  44. m_bRequestedDeviceWM = MODE_NONE;
  45. //m_bSupportedFS = false;
  46. m_bDeviceIsSynced = false;
  47. m_nSleepTime = LGS_DEFAULT_SLEEP_TIME;
  48. #ifdef DEBUG_EXTRA
  49. LOG("Clean Basic Structures!");
  50. #endif
  51. //m_eCurrentSequence = NO_SEQUENCE;
  52. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  53. m_bInitialFlips = LGS_INITIAL_FLIPS;
  54. m_nCurrentAngle = 0;
  55. m_bDrawType = 0;
  56. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  57. memset( m_tSN, 0, 33 );
  58. memset( m_tSV, 0, 65 );
  59. cleanShadowStructs();
  60. if( m_sGetFileTransfers.helpers.size() ){
  61. for( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx < m_sGetFileTransfers.helpers.end(); idx++ ) {
  62. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  63. free( (*idx) );
  64. };
  65. m_sGetFileTransfers.helpers.clear();
  66. };
  67. m_sGetFileTransfers.current = 0;
  68. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  69. //memset( m_pFsPackageBuffer, 0, MAX_PATH );
  70. //m_bCurrentOperationFS = 0;
  71. //memset( &m_sInFSData, 0, sizeof(FS_DATA_EX));
  72. //memset( &m_sOutFSData, 0, sizeof(FS_DATA_EX));
  73. //if( m_pFSHelper ) m_pFSHelper->resetFS(); //ctrl_iteration()
  74. #ifdef DEBUG_EXTRA
  75. LOG("cleanupDeviceStructures DONE");
  76. #endif
  77. };
  78. void device2DSensor::cleanShadowStructs(){
  79. #ifdef DEBUG_EXTRA
  80. LOG("Clean Shadow Structures!");
  81. #endif
  82. if ( m_sShadow.pPolygones ) {
  83. for (int i = 0; i < m_sShadow.m_nPolygonCount; i++) if (m_sShadow.pPolygones[i].pPoint) free( m_sShadow.pPolygones[i].pPoint );
  84. free(m_sShadow.pPolygones);
  85. };
  86. for (int i = 0; i < 360; i++) if (m_sShadow.ray_lines[i].rays ) free(m_sShadow.ray_lines[i].rays );
  87. m_bDeviceIsSynced = false;
  88. //SetWM twice to clean OWM status
  89. //setWM( LRF_WM_INIT );
  90. //setWM( LRF_WM_INIT );
  91. m_bActualDeviceWM = MODE_NONE;
  92. m_bRequestedDeviceWM = MODE_NONE;
  93. m_bSerialStringValid = false;
  94. m_bVersionStringValid = false;
  95. memset(&m_sShadow, 0, sizeof(SHADOW));
  96. memset(&m_sShadowHelper, 0, sizeof(SHADOW_IO_HELPER));
  97. #ifdef DEBUG_EXTRA
  98. LOG("cleanShadowStructs DONE");
  99. #endif
  100. };
  101. device2DSensor::~device2DSensor(){
  102. #ifdef DEBUG
  103. LOG("Stoping Service");
  104. #endif
  105. stopService();
  106. #ifdef DEBUG
  107. LOG("Service is Stoped");
  108. LOG("Node Destructor Ends!");
  109. #endif
  110. //if( m_pFSHelper ) delete ( m_pFSHelper );
  111. };
  112. void device2DSensor::updateDataStatus( uint8_t ds, uint8_t nodestatus ){
  113. if ( m_pDBC == NULL ) return;
  114. if ( m_sNodeData.node_id ) {
  115. if( ds < ARGUS_DS_CONNECTING ) {
  116. m_pDBC->setNodeOffline( m_sNodeData.node_id );
  117. } else {
  118. m_pDBC->setNodeOnline( m_sNodeData.node_id, nodestatus );
  119. };
  120. };
  121. if ( m_sLRF_DATA.id ) {
  122. m_sLRF_DATA.data_status = ds;
  123. m_pDBC->setLRFDataStatus( m_sLRF_DATA.id, ds );
  124. };
  125. };
  126. bool device2DSensor::startService() {
  127. if( m_sLRF_DATA.id == 0 ){
  128. LOG_ERROR("No DATA From Database!");
  129. return false;
  130. };
  131. if( setDeviceID( m_sLRF_DATA.device_id ) ){
  132. if( !create() ){
  133. LOG_ERROR("Protocol <<< Create >>> Failure!");
  134. return false;
  135. };
  136. };
  137. if( m_sLRF_DATA.filter_id > 0 ) {
  138. #ifdef DEBUG
  139. LOG("DEFINED FILTER");
  140. #endif
  141. kernelPrototype *AK = kernelPrototype::getInstance();
  142. if( AK==NULL ){
  143. LOG_ERROR("KERNEL <<< GRAB >>> Failure!");
  144. return false;
  145. };
  146. m_pTargetFilter = AK->getFilter( m_sLRF_DATA.filter_id );
  147. };
  148. updateDataStatus( ARGUS_DS_DISCONNECTED );
  149. //m_pFSHelper = new FS_IO_HELPER();
  150. return init();
  151. };
  152. bool device2DSensor::stopService() {
  153. LOG( "Kiling Thread");
  154. waitThreadEnd( true );
  155. LOG( "Thread is Dead!");
  156. updateDataStatus( ARGUS_DS_DISCONNECTED );
  157. //m_pDBC->setNodeOffline( m_sNodeData.node_id );
  158. LOG( "Start Clenup Structs!");
  159. cleanupDeviceStructures();
  160. LOG( "Clenup Structs Done!");
  161. return true;
  162. };
  163. bool device2DSensor::validate(){
  164. if( m_pDBC == NULL ) return false;
  165. // Check Settings
  166. if( !deviceNodeSensor::validate() ) return false;
  167. return true;
  168. };
  169. void device2DSensor::connectMode(){
  170. #ifdef DEBUG_EXTRA
  171. LOG("Connect MODE");
  172. #endif
  173. // Reset State
  174. if( initWModeFunction( LRF_WM_CONNECTION ) ) broadcastDisconnected();
  175. switch( getPPStatus() ){
  176. case STG_PUSH:
  177. LOG("Push");
  178. switch( getStage() ){
  179. case LRF_WS_INIT:{
  180. updateDataStatus( ARGUS_DS_DISCONNECTED );
  181. m_pDBC->setNodeOffline( m_sNodeData.node_id );
  182. m_bInitialFlips = LGS_INITIAL_FLIPS;
  183. hardwareManager *tHM = hardwareManager::getInstance();
  184. if( tHM != NULL ) {
  185. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sLRF_DATA.uart_id );
  186. if( tRES != NULL ) {
  187. #ifdef DEBUG_EXTRA
  188. LOG("Have Res!");
  189. #endif
  190. if( tRES->isValid() ) {
  191. m_pPort = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  192. #ifdef DEBUG_EXTRA
  193. //Bind To Proto
  194. char tB[64];
  195. sprintf( tB, "PortOBJ: %p", m_pPort );
  196. LOG( tB );
  197. #endif
  198. if( !setTransport( m_pPort, false )){
  199. LOG_ERROR("Binding Port Failure!");
  200. } else {
  201. #ifdef DEBUG_EXTRA
  202. LOG("Port Bind Sucessfully Done");
  203. #endif
  204. updateDataStatus( ARGUS_DS_CONNECTING );
  205. m_bActualDeviceWM = MODE_NONE;
  206. m_bRequestedDeviceWM = MODE_NONE;
  207. transactionSetResult( TRANSACTION_RESULT_OK );
  208. break;
  209. };
  210. #ifdef DEBUG_EXTRA
  211. } else {
  212. LOG("Res is INVALID?!");
  213. #endif
  214. };
  215. #ifdef DEBUG_EXTRA
  216. } else {
  217. char tB[256];
  218. sprintf( tB, "HWM Has No UART PORT[%d]!", m_sLRF_DATA.uart_id );
  219. LOG( tB );
  220. #endif
  221. };
  222. } else {
  223. LOG_ERROR("Can not access Hardware Manager");
  224. };
  225. setTransport( NULL, false );
  226. DELAY_10MS( 500 ); // 500 ms
  227. break;
  228. };
  229. case LRF_WS_STAGE_1:{
  230. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  231. cleanupDeviceStructures();
  232. m_bRequestedDeviceWM = MODE_DETECTOR;
  233. if( !setMode( MODE_DETECTOR ) ) {
  234. LOG_ERROR("Fall Back to Disconnected!");
  235. updateDataStatus( ARGUS_DS_DISCONNECTED );
  236. setStage( LRF_WS_INIT );
  237. } else transactionWaitResult();
  238. break;
  239. };
  240. case LRF_WS_STAGE_2: setWM( LRF_WM_HANDSHAKE ); break;
  241. default: setStage( LRF_WS_INIT ); break;
  242. };
  243. break;
  244. case STG_WAIT: waitStateFunction( LRF_WS_STAGE_1 ); break;
  245. case STG_POOL: poolStateFunction(); break;
  246. };
  247. };
  248. void device2DSensor::handshakeMode(){
  249. #ifdef DEBUG_EXTRA
  250. LOG("Handshake MODE");
  251. #endif
  252. // Reset State
  253. if( initWModeFunction( LRF_WM_HANDSHAKE ) ) {
  254. updateDataStatus( ARGUS_DS_GET_INFO );
  255. broadcastConnected();
  256. };
  257. switch( getPPStatus() ){
  258. case STG_PUSH:{
  259. switch( getStage() ) {
  260. case LRF_WS_INIT:{
  261. if( m_bActualDeviceWM != MODE_DETECTOR ){
  262. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  263. m_bRequestedDeviceWM = MODE_DETECTOR;
  264. if( !setMode( MODE_DETECTOR ) ) {
  265. m_pDBC->setNodeOffline( m_sNodeData.node_id );
  266. LOG_ERROR("Fall Back to Disconnected!");
  267. setWM( LRF_WM_CONNECTION );
  268. } else transactionWaitResult();
  269. } else transactionSetResult( TRANSACTION_RESULT_OK );
  270. break;
  271. };
  272. case LRF_WS_STAGE_1:{
  273. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  274. if( !getSerialNumber() ) {
  275. LOG_ERROR("Fall Back to Disconnected!");
  276. setWM( LRF_WM_CONNECTION );
  277. } else transactionWaitResult();
  278. break;
  279. };
  280. case LRF_WS_STAGE_2:{
  281. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  282. if( !getVersionString() ) {
  283. LOG_ERROR("Fall Back to Disconnected!");
  284. setWM( LRF_WM_CONNECTION );
  285. } else transactionWaitResult();
  286. break;
  287. };
  288. case LRF_WS_STAGE_3:
  289. if( m_bDeviceIsSynced ){
  290. updateDataStatus( ARGUS_DS_READY );
  291. setWM( LRF_WM_IDLE );
  292. } else {
  293. setWM( LRF_WM_SYNC_UP );
  294. };
  295. break;
  296. };
  297. break;
  298. };
  299. case STG_WAIT: waitStateFunction(); break;
  300. case STG_POOL: poolStateFunction(); break;
  301. };
  302. };
  303. bool device2DSensor::initWModeFunction( uint8_t wm ){
  304. if( getOWM() != wm ) {
  305. setWM( wm );
  306. setWM( wm );
  307. setStage( LRF_WS_INIT );
  308. transactionResetResult();
  309. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  310. return true;
  311. };
  312. return false;
  313. };
  314. void device2DSensor::readMode(){
  315. #ifdef DEBUG_EXTRA
  316. LOG("READ");
  317. #endif
  318. // Reset State
  319. initWModeFunction( LRF_WM_SYNC_UP );
  320. switch( getPPStatus() ){
  321. case STG_PUSH:{
  322. switch( getStage() ) {
  323. case LRF_WS_INIT:{
  324. LOG("TryP");
  325. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  326. if( !getDetectionProfile() ) {
  327. LOG_ERROR("Fall Back to Disconnected!");
  328. setWM( LRF_WM_CONNECTION );
  329. } else transactionWaitResult();
  330. break;
  331. };
  332. case LRF_WS_STAGE_1:{
  333. LOG("Get PM");
  334. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  335. if( !getParameters( true ) ) {
  336. LOG_ERROR("Fall Back to Disconnected!");
  337. setWM( LRF_WM_CONNECTION );
  338. } else transactionWaitResult();
  339. break;
  340. };
  341. case LRF_WS_STAGE_2:{
  342. LOG("Get PS");
  343. if( m_sNodeData.type == ARGUS_ALS_SENSOR ) { // Read Slave Just for ALS series
  344. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  345. if( !getParameters( false ) ) {
  346. LOG_ERROR("Fall Back to Disconnected!");
  347. setWM( LRF_WM_CONNECTION );
  348. } else transactionWaitResult();
  349. } else transactionSetResult( TRANSACTION_RESULT_OK );
  350. break;
  351. };
  352. case LRF_WS_STAGE_3:{
  353. LOG("Get SHIFU");
  354. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  355. if( !getShadowInfo( m_sShadowHelper.m_bSequence ) ) {
  356. LOG_ERROR("Fall Back to Disconnected!");
  357. setWM( LRF_WM_CONNECTION );
  358. } else transactionWaitResult();
  359. break;
  360. };
  361. case LRF_WS_STAGE_4:{
  362. LOG("Get POLY");
  363. // Have Info -> If No Poly - Skeep to go get DC
  364. if( m_sShadow.pPolygones ){
  365. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  366. if( !getShadowPoly( m_sShadowHelper.m_bSequence, m_sShadowHelper.m_nCurrentPolygon ) ){
  367. LOG_ERROR("Fall Back to Disconnected!");
  368. setWM( LRF_WM_CONNECTION );
  369. } else transactionWaitResult();
  370. } else transactionSetResult( TRANSACTION_RESULT_OK );
  371. break;
  372. };
  373. case LRF_WS_STAGE_5:{
  374. LOG("Get POPO");
  375. if ( m_sShadow.pPolygones ) {
  376. if (m_sShadowHelper.m_nCurrentPoint < m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount) {
  377. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  378. if( !getShadowPolyPoint( m_sShadowHelper.m_bSequence, m_sShadowHelper.m_nCurrentPolygon, m_sShadowHelper.m_nCurrentPoint ) ) {
  379. LOG_ERROR("Fall Back to Disconnected!");
  380. setWM( LRF_WM_CONNECTION );
  381. } else transactionWaitResult();
  382. } else {
  383. if ( m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ) {
  384. m_sShadowHelper.m_nCurrentPolygon++;
  385. setStage( LRF_WS_STAGE_4 ); // Recheck next Poly
  386. } else transactionSetResult( TRANSACTION_RESULT_OK );
  387. };
  388. } else transactionSetResult( TRANSACTION_RESULT_OK );
  389. break;
  390. };
  391. case LRF_WS_STAGE_6:{
  392. LOG("Get RELAY");
  393. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  394. if( !getRelayParameters() ) {
  395. LOG_ERROR("Fall Back to Disconnected!");
  396. setWM( LRF_WM_CONNECTION );
  397. } else transactionWaitResult();
  398. break;
  399. };
  400. case LRF_WS_STAGE_7: setWM( LRF_WM_IDLE ); break;
  401. default: setStage( LRF_WS_INIT ); break;
  402. };
  403. break;
  404. };
  405. case STG_WAIT: waitStateFunction(); break;
  406. case STG_POOL: poolStateFunction(); break;
  407. };
  408. };
  409. void device2DSensor::syncMode(){
  410. #ifdef DEBUG_EXTRA
  411. LOG("SYNC");
  412. #endif
  413. // Reset State
  414. if( initWModeFunction( LRF_WM_SYNC_DOWN ) ){
  415. updateDataStatus( ARGUS_DS_BUSY );
  416. memset( m_pTGENFILENAMES[0], 0, 33 );
  417. memset( m_pTGENFILENAMES[1], 0, 33 );
  418. memset( m_pTGENFILENAMES[2], 0, 33 );
  419. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  420. m_bTGENFILECNT = 0;
  421. m_bTGENFILE = 0;
  422. m_sGetFileTransfers.current = 0;
  423. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  424. if ( m_sGetFileTransfers.helpers.size() ) {
  425. for ( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx< m_sGetFileTransfers.helpers.end(); idx++ ){
  426. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  427. free( (*idx) );
  428. };
  429. m_sGetFileTransfers.helpers.clear();
  430. };
  431. };
  432. switch( getPPStatus() ){
  433. case STG_PUSH:{
  434. switch( getStage() ) {
  435. case LRF_WS_INIT:{
  436. if( m_bTGENFILECNT == 0 ){
  437. // Generate INI file and Send via GETFILE
  438. if( generateIniFile( m_bTGENFILE ) ){
  439. GETFILE_IO_HELPER *tH = NULL;
  440. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  441. if ( tH ) {
  442. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  443. if ( strcasestr( m_tSV, "LFSS" ) != NULL ) {
  444. #ifdef DEBUG
  445. LOG("Generating LFSS config");
  446. #endif
  447. strncpy( tH->destName, "LFSS.ini", 16 );
  448. } else {
  449. if ( strcasestr( m_tSV, "PLS" ) != NULL ) {
  450. #ifdef DEBUG
  451. LOG("Generating PLS config");
  452. #endif
  453. strncpy( tH->destName, "PLS.ini", 16 );
  454. } else {
  455. if ( strcasestr( m_tSV, "MND" ) != NULL ) {
  456. #ifdef DEBUG
  457. LOG("Generating MND config");
  458. #endif
  459. strncpy( tH->destName, "MND.ini", 16 );
  460. } else {
  461. if ( strcasestr( m_tSV, "FOD" ) != NULL ) {
  462. #ifdef DEBUG
  463. LOG("Generating FOD config");
  464. #endif
  465. strncpy( tH->destName, "FOD.ini", 16 );
  466. } else {
  467. #ifdef DEBUG
  468. LOG("Generating ALS/LFS (USER.ini) config");
  469. #endif
  470. strncpy( tH->destName, "USER.ini", 16 );
  471. };
  472. };
  473. };
  474. };
  475. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  476. struct stat st;
  477. char tB[256];
  478. sprintf( tB, "data/uploads/%s", tH->sourceName );
  479. stat( tB, &st );
  480. tH->fileSize = st.st_size;
  481. tH->state = GF_OP_OK;
  482. tH->db_id = 0;
  483. tH->index = 0x04;
  484. m_sGetFileTransfers.helpers.push_back( tH );
  485. m_bTGENFILE++;
  486. m_bTGENFILECNT++;
  487. };
  488. } else {
  489. LOG("Gen File 1 Failure!");
  490. };
  491. if( generateRestartFile( m_bTGENFILE ) ){
  492. GETFILE_IO_HELPER *tH = NULL;
  493. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  494. if ( tH ) {
  495. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  496. strncpy( tH->destName, "updateMZ.bat", 16 );
  497. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  498. struct stat st;
  499. char tB[256];
  500. sprintf( tB, "data/uploads/%s", tH->sourceName );
  501. stat( tB, &st );
  502. tH->fileSize = st.st_size;
  503. tH->state = GF_OP_OK;
  504. tH->db_id = 0;
  505. tH->index = 0x04;
  506. m_sGetFileTransfers.helpers.push_back( tH );
  507. m_bTGENFILE++;
  508. m_bTGENFILECNT++;
  509. };
  510. } else {
  511. LOG("Gen File 2 Failure!");
  512. };
  513. };
  514. cleanShadowStructs();
  515. if( m_pDBC->fillShadow2D( m_sNodeData.aux_id, m_sShadow ) ){
  516. LOG("Loadded From DB");
  517. if( rasterizeShadow() ){
  518. LOG("Mask is Rasterized");
  519. if ( m_bActualDeviceWM != MODE_MASK ) {
  520. LOG("Set Mask Mode");
  521. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  522. m_bRequestedDeviceWM = MODE_MASK;
  523. if( !setMode( MODE_MASK ) ) {
  524. LOG_ERROR("Fall Back to Disconnected!");
  525. setWM( LRF_WM_CONNECTION );
  526. } else transactionWaitResult();
  527. } else {
  528. transactionSetResult( TRANSACTION_RESULT_OK );
  529. };
  530. } else {
  531. setStage( LRF_WS_STAGE_7 );
  532. };
  533. } else {
  534. setStage( LRF_WS_STAGE_7 );
  535. };
  536. break;
  537. };
  538. case LRF_WS_STAGE_1:{
  539. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  540. if( m_bActualDeviceWM != MODE_MASK ) {
  541. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  542. m_bRequestedDeviceWM = MODE_MASK;
  543. if( !setMode( MODE_MASK ) ) {
  544. LOG_ERROR("Fall Back to Disconnected!");
  545. setWM( LRF_WM_CONNECTION );
  546. } else transactionWaitResult();
  547. } else {
  548. setStage( LRF_WS_STAGE_1 );
  549. };
  550. break;
  551. };
  552. case LRF_WS_STAGE_2:{
  553. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  554. if (!setShadowInfo( m_sShadowHelper.m_bSequence, m_sShadow )) {
  555. LOG_ERROR("Fall Back to Disconnected!");
  556. setWM( LRF_WM_CONNECTION );
  557. } else transactionWaitResult();
  558. break;
  559. };
  560. case LRF_WS_STAGE_3:{
  561. if( m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ){
  562. m_sShadowHelper.m_nCurrentPoint = 0;
  563. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  564. if(!setShadowPoly( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentPolygon)){
  565. LOG_ERROR("Fall Back to Disconnected!");
  566. setWM( LRF_WM_CONNECTION );
  567. } else transactionWaitResult();
  568. } else {
  569. m_sShadowHelper.m_nCurrentRayLine = 0;
  570. //skip all empty angles of binary shadow
  571. while( ( m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount == 0 ) && m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  572. m_sShadowHelper.m_nCurrentRayLine++;
  573. };
  574. if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  575. //if we have an shadows -> transfer else skeep
  576. setStage( LRF_WS_STAGE_5 );
  577. } else {
  578. setStage( LRF_WS_STAGE_7 );
  579. };
  580. };
  581. break;
  582. };
  583. case LRF_WS_STAGE_4:{
  584. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  585. #ifdef DEBUG_EXTRA
  586. char tBuff[128];
  587. sprintf( tBuff,"Send Point %d of %d", (m_sShadowHelper.m_nCurrentPoint + 1), m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount );
  588. LOG( tBuff );
  589. #endif
  590. if( m_sShadowHelper.m_nCurrentPoint < m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount ){
  591. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  592. if( !setShadowPolyPoint( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentPolygon, m_sShadowHelper.m_nCurrentPoint) ){
  593. LOG_ERROR("Fall Back to Disconnected!");
  594. setWM( LRF_WM_CONNECTION );
  595. } else transactionWaitResult();
  596. } else {
  597. m_sShadowHelper.m_nCurrentPolygon ++;
  598. setStage( LRF_WS_STAGE_3 );
  599. };
  600. break;
  601. };
  602. case LRF_WS_STAGE_5:{
  603. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  604. #ifdef DEBUG_EXTRA
  605. char tBuff[128];
  606. sprintf( tBuff,"Send Shadow for angle %d [%d rays]", m_sShadowHelper.m_nCurrentRayLine, m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount );
  607. LOG( tBuff );
  608. #endif
  609. if( !setShadowRay( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentRayLine ) ){
  610. LOG_ERROR("Fall Back to Disconnected!");
  611. setWM( LRF_WM_CONNECTION );
  612. } else transactionWaitResult();
  613. break;
  614. };
  615. case LRF_WS_STAGE_6:{
  616. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  617. #ifdef DEBUG_EXTRA
  618. char tBuff[128];
  619. sprintf( tBuff,"Send Shadow RayLine %d for andle %d", m_sShadowHelper.m_nCurrentRay, m_sShadowHelper.m_nCurrentRayLine );
  620. LOG( tBuff );
  621. #endif
  622. if( m_sShadowHelper.m_nCurrentRay < m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount ) {
  623. if( !setShadowRayLine( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentRayLine, m_sShadowHelper.m_nCurrentRay ) ){
  624. LOG_ERROR("Fall Back to Disconnected!");
  625. setWM( LRF_WM_CONNECTION );
  626. } else transactionWaitResult();
  627. } else {
  628. do {
  629. m_sShadowHelper.m_nCurrentRayLine++;
  630. } while ( ( m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount == 0 ) && m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES );
  631. if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  632. setStage( LRF_WS_STAGE_5 );
  633. } else {
  634. // If not found -> finish upload shadow
  635. transactionSetResult( TRANSACTION_RESULT_OK );
  636. };
  637. };
  638. break;
  639. };
  640. case LRF_WS_STAGE_7:{
  641. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  642. if (!setShadowDone( m_sShadowHelper.m_bSequence )) {
  643. LOG_ERROR("Fall Back to Disconnected!");
  644. setWM( LRF_WM_CONNECTION );
  645. } else transactionWaitResult();
  646. break;
  647. };
  648. case LRF_WS_STAGE_8:{
  649. LOG("Send Master Profile!");
  650. //ANODE_WM_2D_SET_MASTER_PARAM
  651. PLGS_2D_PARAMETERS tData = m_pDBC->getLRFDetectionProfile( m_sNodeData.aux_id, m_nActiveDetectionProfile, true );
  652. if ( tData ) {
  653. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER << 2;
  654. if (!setParameters( tData, true, m_nActiveDetectionProfile)) {
  655. LOG_ERROR("Fall Back to Disconnected!");
  656. setWM( LRF_WM_CONNECTION );
  657. } else transactionWaitResult();
  658. free( tData );
  659. } else transactionSetResult( TRANSACTION_RESULT_OK );
  660. break;
  661. };
  662. case LRF_WS_STAGE_9:{
  663. LOG("Send Slave Profile!");
  664. //ANODE_WM_2D_SET_SLAVE_PARAM
  665. PLGS_2D_PARAMETERS tData = m_pDBC->getLRFDetectionProfile( m_sNodeData.aux_id, m_nActiveDetectionProfile, false );
  666. if ( tData ) {
  667. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER << 2;
  668. if (!setParameters( tData, false, m_nActiveDetectionProfile)) {
  669. LOG_ERROR("Fall Back to Disconnected!");
  670. setWM( LRF_WM_CONNECTION );
  671. } else transactionWaitResult();
  672. free( tData );
  673. } else transactionSetResult( TRANSACTION_RESULT_OK );
  674. break;
  675. };
  676. case LRF_WS_STAGE_10:{
  677. //ANODE_WM_2D_SET_GETFILE_MODE
  678. if ( m_bActualDeviceWM != MODE_GETFILE ) {
  679. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  680. m_bRequestedDeviceWM = MODE_GETFILE;
  681. if( !setMode( MODE_GETFILE ) ) {
  682. LOG_ERROR("Fall Back to Disconnected!");
  683. setWM( LRF_WM_CONNECTION );
  684. } else transactionWaitResult();
  685. } else {
  686. transactionSetResult( TRANSACTION_RESULT_OK );
  687. };
  688. break;
  689. };
  690. case LRF_WS_STAGE_11:{
  691. if( m_sGetFileTransfers.helpers.size() ){
  692. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index = ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index + 0x04) % GF_OP_INDEX;
  693. switch( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state ) {
  694. case GF_OP_CLOSE:{
  695. // After Sending check if have other file to send
  696. if( m_sGetFileTransfers.current < ( m_sGetFileTransfers.helpers.size() - 1 ) ) {
  697. m_sGetFileTransfers.current++;
  698. } else {
  699. //switch( m_sGetFileTransfers.transfer_ending_mode ) {
  700. // case GF_EM_ISSUE_RESET:{
  701. // break;
  702. // };
  703. // case GF_EM_ISSUE_SAVE_FW:{
  704. // break;
  705. // }
  706. // default: setStage( LRF_WS_STAGE_7 );
  707. //};
  708. transactionSetResult( TRANSACTION_RESULT_OK );
  709. return;
  710. };
  711. };
  712. case GF_OP_OK:{
  713. // INIT Sending File
  714. char tB[256];
  715. sprintf( tB, "data/uploads/%s", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->sourceName );
  716. #ifdef DEBUG
  717. LOG(tB);
  718. #endif
  719. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = fopen( tB, "rb" );
  720. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd ) {
  721. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_OPEN;
  722. #ifdef DEBUG_EXTRA
  723. LOG("OPEN -> Send Info");
  724. #endif
  725. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = strlen( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName );
  726. memcpy(
  727. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  728. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName,
  729. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size
  730. );
  731. } else {
  732. #ifdef DEBUG_EXTRA
  733. LOG("NOPEN -> Cancel");
  734. #endif
  735. //memset( &m_sGetFileHelper, 0, sizeof( GETFILE_IO_HELPER ));
  736. transactionSetResult( TRANSACTION_RESULT_OK );
  737. return;
  738. };
  739. break;
  740. };
  741. case GF_OP_OPEN:{
  742. #ifdef DEBUG_EXTRA
  743. LOG("Info -> Data");
  744. #endif
  745. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_DATA;
  746. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  747. 1,
  748. 48,
  749. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  750. );
  751. #ifdef DEBUG_EXTRA
  752. char tB[64];
  753. sprintf( tB,"%d of %d bytes", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize );
  754. LOG( tB );
  755. #endif
  756. break;
  757. };
  758. case GF_OP_DATA:{
  759. long int pos = ftell( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  760. uint8_t CP = (uint8_t)((((float)pos)*100.0f)/((float)m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize));
  761. #ifdef DEBUG
  762. char tB[64];
  763. if ( pos != EOF ){
  764. sprintf( tB, "%d of %d bytes %d%%", pos, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize, CP );
  765. LOG( tB );
  766. } else {
  767. LOG("File Ended!");
  768. };
  769. #endif
  770. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  771. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, CP);
  772. }
  773. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  774. 1,
  775. 48,
  776. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  777. );
  778. #ifdef DEBUG
  779. sprintf( tB,"File Reads %d bytes.", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size );
  780. LOG( tB );
  781. #endif
  782. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size == 0 ) {
  783. #ifdef DEBUG
  784. LOG("Change to Close");
  785. #endif
  786. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_CLOSE;
  787. fclose( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  788. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = NULL;
  789. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  790. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, 200);
  791. };
  792. };
  793. break;
  794. };
  795. };
  796. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  797. if( !sendGF_package(
  798. ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state | m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ),
  799. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size,
  800. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data
  801. )
  802. ){
  803. LOG_ERROR("Fall Back to Disconnected!");
  804. setWM( LRF_WM_CONNECTION );
  805. } else transactionWaitResult();
  806. } else {
  807. // Exit From GetFile Mode --> No need --> Reset Device
  808. transactionSetResult( TRANSACTION_RESULT_OK );
  809. };
  810. break;
  811. };
  812. case LRF_WS_STAGE_12:{
  813. // Remove Generated Files
  814. if( m_bTGENFILECNT ){
  815. for( uint8_t idx = 0; idx < m_bTGENFILECNT; idx++){
  816. char tName[256];
  817. sprintf( tName, "data/uploads/%s", m_pTGENFILENAMES[idx]);
  818. unlink( tName );
  819. };
  820. };
  821. transactionSetResult( TRANSACTION_RESULT_OK );
  822. break;
  823. };
  824. case LRF_WS_STAGE_13:{
  825. //Issue Full Reset
  826. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  827. if ( !doFullReset() ) {
  828. LOG_ERROR("Fall Back to Disconnected!");
  829. setWM( LRF_WM_CONNECTION );
  830. } else transactionWaitResult();
  831. break;
  832. };
  833. case LRF_WS_STAGE_14: setWM( LRF_WM_IDLE ); break;
  834. default: setStage( LRF_WS_INIT ); break;
  835. };
  836. break;
  837. };
  838. case STG_WAIT: waitStateFunction(); break;
  839. case STG_POOL: poolStateFunction(); break;
  840. };
  841. };
  842. void device2DSensor::save_ini(){
  843. #ifdef DEBUG_EXTRA
  844. LOG("Save HW ini");
  845. #endif
  846. // Reset State
  847. if( initWModeFunction( LRF_WM_SAVE_HW ) ){
  848. updateDataStatus( ARGUS_DS_BUSY );
  849. memset( m_pTGENFILENAMES[0], 0, 33 );
  850. memset( m_pTGENFILENAMES[1], 0, 33 );
  851. memset( m_pTGENFILENAMES[2], 0, 33 );
  852. m_bTGENFILECNT = 0;
  853. m_bTGENFILE = 0;
  854. m_sGetFileTransfers.current = 0;
  855. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  856. if ( m_sGetFileTransfers.helpers.size() ) {
  857. for ( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx< m_sGetFileTransfers.helpers.end(); idx++ ){
  858. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  859. free( (*idx) );
  860. };
  861. m_sGetFileTransfers.helpers.clear();
  862. };
  863. };
  864. switch( getPPStatus() ){
  865. case STG_PUSH:{
  866. switch( getStage() ) {
  867. case LRF_WS_INIT:{
  868. if( m_bTGENFILECNT == 0 ){
  869. // Generate INI file and Send via GETFILE
  870. LOG("Gen III");
  871. if( generateIniFile( m_bTGENFILE ) ){
  872. GETFILE_IO_HELPER *tH = NULL;
  873. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  874. if ( tH ) {
  875. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  876. if ( strcasestr( m_tSV, "LFSS" ) != NULL ) {
  877. #ifdef DEBUG
  878. LOG("Generating LFSS config");
  879. #endif
  880. strncpy( tH->destName, "LFSS.ini", 16 );
  881. } else {
  882. if ( strcasestr( m_tSV, "PLS" ) != NULL ) {
  883. #ifdef DEBUG
  884. LOG("Generating PLS config");
  885. #endif
  886. strncpy( tH->destName, "PLS.ini", 16 );
  887. } else {
  888. if ( strcasestr( m_tSV, "MND" ) != NULL ) {
  889. #ifdef DEBUG
  890. LOG("Generating MND config");
  891. #endif
  892. strncpy( tH->destName, "MND.ini", 16 );
  893. } else {
  894. if ( strcasestr( m_tSV, "FOD" ) != NULL ) {
  895. #ifdef DEBUG
  896. LOG("Generating FOD config");
  897. #endif
  898. strncpy( tH->destName, "FOD.ini", 16 );
  899. } else {
  900. #ifdef DEBUG
  901. LOG("Generating ALS/LFS (USER.ini) config");
  902. #endif
  903. strncpy( tH->destName, "USER.ini", 16 );
  904. };
  905. };
  906. };
  907. };
  908. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  909. struct stat st;
  910. char tB[256];
  911. sprintf( tB, "data/uploads/%s", tH->sourceName );
  912. stat( tB, &st );
  913. tH->fileSize = st.st_size;
  914. tH->state = GF_OP_OK;
  915. tH->db_id = 0;
  916. tH->index = 0x04;
  917. m_sGetFileTransfers.helpers.push_back( tH );
  918. m_bTGENFILE++;
  919. m_bTGENFILECNT++;
  920. };
  921. } else {
  922. LOG_ERROR("Gen File 1 Failure!");
  923. };
  924. if( generateRestartFile( m_bTGENFILE ) ){
  925. GETFILE_IO_HELPER *tH = NULL;
  926. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  927. if ( tH ) {
  928. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  929. strncpy( tH->destName, "updateMZ.bat", 16 );
  930. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  931. struct stat st;
  932. char tB[256];
  933. sprintf( tB, "data/uploads/%s", tH->sourceName );
  934. stat( tB, &st );
  935. tH->fileSize = st.st_size;
  936. tH->state = GF_OP_OK;
  937. tH->db_id = 0;
  938. tH->index = 0x04;
  939. m_sGetFileTransfers.helpers.push_back( tH );
  940. m_bTGENFILE++;
  941. m_bTGENFILECNT++;
  942. };
  943. } else {
  944. LOG_ERROR("Gen File 2 Failure!");
  945. };
  946. };
  947. transactionSetResult( TRANSACTION_RESULT_OK );
  948. break;
  949. };
  950. case LRF_WS_STAGE_1:{
  951. //ANODE_WM_2D_SET_GETFILE_MODE
  952. if ( m_bActualDeviceWM != MODE_GETFILE ) {
  953. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  954. m_bRequestedDeviceWM = MODE_GETFILE;
  955. if( !setMode( MODE_GETFILE ) ) {
  956. LOG_ERROR("Fall Back to Disconnected!");
  957. setWM( LRF_WM_CONNECTION );
  958. } else transactionWaitResult();
  959. } else {
  960. transactionSetResult( TRANSACTION_RESULT_OK );
  961. };
  962. break;
  963. };
  964. case LRF_WS_STAGE_2:{
  965. if( m_sGetFileTransfers.helpers.size() ){
  966. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index = ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index + 0x04) % GF_OP_INDEX;
  967. switch( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state ) {
  968. case GF_OP_CLOSE:{
  969. // After Sending check if have other file to send
  970. if( m_sGetFileTransfers.current < ( m_sGetFileTransfers.helpers.size() - 1 ) ) {
  971. m_sGetFileTransfers.current++;
  972. } else {
  973. //switch( m_sGetFileTransfers.transfer_ending_mode ) {
  974. // case GF_EM_ISSUE_RESET:{
  975. // break;
  976. // };
  977. // case GF_EM_ISSUE_SAVE_FW:{
  978. // break;
  979. // }
  980. // default: setStage( LRF_WS_STAGE_7 );
  981. //};
  982. transactionSetResult( TRANSACTION_RESULT_OK );
  983. return;
  984. };
  985. };
  986. case GF_OP_OK:{
  987. // INIT Sending File
  988. char tB[256];
  989. sprintf( tB, "data/uploads/%s", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->sourceName );
  990. #ifdef DEBUG
  991. LOG(tB);
  992. #endif
  993. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = fopen( tB, "rb" );
  994. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd ) {
  995. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_OPEN;
  996. #ifdef DEBUG_EXTRA
  997. LOG("OPEN -> Send Info");
  998. #endif
  999. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = strlen( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName );
  1000. memcpy(
  1001. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1002. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName,
  1003. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size
  1004. );
  1005. } else {
  1006. #ifdef DEBUG_EXTRA
  1007. LOG("NOPEN -> Cancel");
  1008. #endif
  1009. //memset( &m_sGetFileHelper, 0, sizeof( GETFILE_IO_HELPER ));
  1010. transactionSetResult( TRANSACTION_RESULT_OK );
  1011. return;
  1012. };
  1013. break;
  1014. };
  1015. case GF_OP_OPEN:{
  1016. #ifdef DEBUG_EXTRA
  1017. LOG("Info -> Data");
  1018. #endif
  1019. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_DATA;
  1020. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1021. 1,
  1022. 48,
  1023. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1024. );
  1025. #ifdef DEBUG_EXTRA
  1026. char tB[64];
  1027. sprintf( tB,"%d of %d bytes", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize );
  1028. LOG( tB );
  1029. #endif
  1030. break;
  1031. };
  1032. case GF_OP_DATA:{
  1033. long int pos = ftell( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1034. uint8_t CP = (uint8_t)((((float)pos)*100.0f)/((float)m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize));
  1035. #ifdef DEBUG
  1036. char tB[64];
  1037. if ( pos != EOF ){
  1038. sprintf( tB, "%d of %d bytes %d%%", pos, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize, CP );
  1039. LOG( tB );
  1040. } else {
  1041. LOG("File Ended!");
  1042. };
  1043. #endif
  1044. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1045. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, CP);
  1046. }
  1047. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1048. 1,
  1049. 48,
  1050. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1051. );
  1052. #ifdef DEBUG
  1053. sprintf( tB,"File Reads %d bytes.", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size );
  1054. LOG( tB );
  1055. #endif
  1056. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size == 0 ) {
  1057. #ifdef DEBUG
  1058. LOG("Change to Close");
  1059. #endif
  1060. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_CLOSE;
  1061. fclose( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1062. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = NULL;
  1063. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1064. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, 200);
  1065. };
  1066. };
  1067. break;
  1068. };
  1069. };
  1070. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1071. if( !sendGF_package(
  1072. ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state | m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ),
  1073. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size,
  1074. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data
  1075. )
  1076. ){
  1077. LOG_ERROR("Fall Back to Disconnected!");
  1078. setWM( LRF_WM_CONNECTION );
  1079. } else transactionWaitResult();
  1080. } else {
  1081. // Exit From GetFile Mode --> No need --> Reset Device
  1082. transactionSetResult( TRANSACTION_RESULT_OK );
  1083. };
  1084. break;
  1085. };
  1086. case LRF_WS_STAGE_3:{
  1087. // Remove Generated Files
  1088. if( m_bTGENFILECNT ){
  1089. for( uint8_t idx = 0; idx < m_bTGENFILECNT; idx++){
  1090. char tName[256];
  1091. sprintf( tName, "data/uploads/%s", m_pTGENFILENAMES[idx]);
  1092. unlink( tName );
  1093. };
  1094. };
  1095. transactionSetResult( TRANSACTION_RESULT_OK );
  1096. break;
  1097. };
  1098. case LRF_WS_STAGE_4:{
  1099. //Issue Full Reset
  1100. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1101. if ( !doFullReset() ) {
  1102. LOG_ERROR("Fall Back to Disconnected!");
  1103. setWM( LRF_WM_CONNECTION );
  1104. } else transactionWaitResult();
  1105. break;
  1106. };
  1107. case LRF_WS_STAGE_13: setWM( LRF_WM_IDLE ); break;
  1108. default: setStage( LRF_WS_INIT ); break;
  1109. };
  1110. break;
  1111. };
  1112. case STG_WAIT: waitStateFunction(); break;
  1113. case STG_POOL: poolStateFunction(); break;
  1114. };
  1115. };
  1116. void device2DSensor::save_detection(){
  1117. };
  1118. void device2DSensor::save_masks(){
  1119. #ifdef DEBUG_EXTRA
  1120. LOG("SAVE MASKS");
  1121. #endif
  1122. // Reset State
  1123. if( initWModeFunction( LRF_WM_SAVE_MASKS ) ){
  1124. updateDataStatus( ARGUS_DS_BUSY );
  1125. memset( m_pTGENFILENAMES[0], 0, 33 );
  1126. memset( m_pTGENFILENAMES[1], 0, 33 );
  1127. memset( m_pTGENFILENAMES[2], 0, 33 );
  1128. m_bTGENFILECNT = 0;
  1129. m_bTGENFILE = 0;
  1130. m_sGetFileTransfers.current = 0;
  1131. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  1132. if ( m_sGetFileTransfers.helpers.size() ) {
  1133. for ( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx< m_sGetFileTransfers.helpers.end(); idx++ ){
  1134. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  1135. free( (*idx) );
  1136. };
  1137. m_sGetFileTransfers.helpers.clear();
  1138. };
  1139. };
  1140. switch( getPPStatus() ){
  1141. case STG_PUSH:{
  1142. switch( getStage() ) {
  1143. case LRF_WS_INIT:{
  1144. if( m_bTGENFILECNT == 0 ){
  1145. if( generateRestartFile( m_bTGENFILE ) ){
  1146. GETFILE_IO_HELPER *tH = NULL;
  1147. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  1148. if ( tH ) {
  1149. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  1150. strncpy( tH->destName, "updateMZ.bat", 16 );
  1151. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  1152. struct stat st;
  1153. char tB[256];
  1154. sprintf( tB, "data/uploads/%s", tH->sourceName );
  1155. stat( tB, &st );
  1156. tH->fileSize = st.st_size;
  1157. tH->state = GF_OP_OK;
  1158. tH->db_id = 0;
  1159. tH->index = 0x04;
  1160. m_sGetFileTransfers.helpers.push_back( tH );
  1161. m_bTGENFILE++;
  1162. m_bTGENFILECNT++;
  1163. };
  1164. } else {
  1165. LOG_ERROR("Gen UpdateMZ Failure!");
  1166. };
  1167. };
  1168. cleanShadowStructs();
  1169. if( m_pDBC->fillShadow2D( m_sLRF_DATA.id, m_sShadow ) ){
  1170. if( rasterizeShadow() ){
  1171. #ifdef DEBUG_EXTRA
  1172. makeShadowPic("data/shadow.png");
  1173. #endif
  1174. transactionSetResult( TRANSACTION_RESULT_OK );
  1175. } else {
  1176. setStage( LRF_WS_STAGE_11 );
  1177. };
  1178. } else {
  1179. setStage( LRF_WS_STAGE_11 );
  1180. };
  1181. break;
  1182. };
  1183. case LRF_WS_STAGE_1:{
  1184. if ( m_bActualDeviceWM != MODE_GETFILE ) {
  1185. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1186. m_bRequestedDeviceWM = MODE_GETFILE;
  1187. if( !setMode( MODE_GETFILE ) ) {
  1188. LOG_ERROR("Fall Back to Disconnected!");
  1189. setWM( LRF_WM_CONNECTION );
  1190. } else transactionWaitResult();
  1191. } else {
  1192. transactionSetResult( TRANSACTION_RESULT_OK );
  1193. };
  1194. break;
  1195. };
  1196. case LRF_WS_STAGE_2:{
  1197. if( m_sGetFileTransfers.helpers.size() ){
  1198. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index = ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index + 0x04) % GF_OP_INDEX;
  1199. switch( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state ) {
  1200. case GF_OP_CLOSE:{
  1201. // After Sending check if have other file to send
  1202. if( m_sGetFileTransfers.current < ( m_sGetFileTransfers.helpers.size() - 1 ) ) {
  1203. m_sGetFileTransfers.current++;
  1204. } else {
  1205. //switch( m_sGetFileTransfers.transfer_ending_mode ) {
  1206. // case GF_EM_ISSUE_RESET:{
  1207. // break;
  1208. // };
  1209. // case GF_EM_ISSUE_SAVE_FW:{
  1210. // break;
  1211. // }
  1212. // default: setStage( LRF_WS_STAGE_7 );
  1213. //};
  1214. transactionSetResult( TRANSACTION_RESULT_OK );
  1215. return;
  1216. };
  1217. };
  1218. case GF_OP_OK:{
  1219. // INIT Sending File
  1220. char tB[256];
  1221. sprintf( tB, "data/uploads/%s", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->sourceName );
  1222. #ifdef DEBUG
  1223. LOG(tB);
  1224. #endif
  1225. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = fopen( tB, "rb" );
  1226. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd ) {
  1227. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_OPEN;
  1228. #ifdef DEBUG_EXTRA
  1229. LOG("OPEN -> Send Info");
  1230. #endif
  1231. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = strlen( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName );
  1232. memcpy(
  1233. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1234. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName,
  1235. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size
  1236. );
  1237. } else {
  1238. #ifdef DEBUG_EXTRA
  1239. LOG("NOPEN -> Cancel");
  1240. #endif
  1241. //memset( &m_sGetFileHelper, 0, sizeof( GETFILE_IO_HELPER ));
  1242. transactionSetResult( TRANSACTION_RESULT_OK );
  1243. return;
  1244. };
  1245. break;
  1246. };
  1247. case GF_OP_OPEN:{
  1248. #ifdef DEBUG_EXTRA
  1249. LOG("Info -> Data");
  1250. #endif
  1251. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_DATA;
  1252. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1253. 1,
  1254. 48,
  1255. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1256. );
  1257. #ifdef DEBUG_EXTRA
  1258. char tB[64];
  1259. sprintf( tB,"%d of %d bytes", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize );
  1260. LOG( tB );
  1261. #endif
  1262. break;
  1263. };
  1264. case GF_OP_DATA:{
  1265. long int pos = ftell( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1266. uint8_t CP = (uint8_t)((((float)pos)*100.0f)/((float)m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize));
  1267. #ifdef DEBUG_EXTRA
  1268. char tB[64];
  1269. if ( pos != EOF ){
  1270. sprintf( tB, "%d of %d bytes %d%%", pos, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize, CP );
  1271. LOG( tB );
  1272. } else {
  1273. LOG("File Ended!");
  1274. };
  1275. #endif
  1276. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1277. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, CP);
  1278. }
  1279. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1280. 1,
  1281. 48,
  1282. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1283. );
  1284. #ifdef DEBUG_EXTRA
  1285. sprintf( tB,"File Reads %d bytes.", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size );
  1286. LOG( tB );
  1287. #endif
  1288. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size == 0 ) {
  1289. #ifdef DEBUG_EXTRA
  1290. LOG("Change to Close");
  1291. #endif
  1292. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_CLOSE;
  1293. fclose( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1294. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = NULL;
  1295. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1296. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, 200);
  1297. };
  1298. };
  1299. break;
  1300. };
  1301. };
  1302. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1303. if( !sendGF_package(
  1304. ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state | m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ),
  1305. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size,
  1306. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data
  1307. )
  1308. ){
  1309. LOG_ERROR("Fall Back to Disconnected!");
  1310. setWM( LRF_WM_CONNECTION );
  1311. } else transactionWaitResult();
  1312. } else {
  1313. // Exit From GetFile Mode --> No need --> Reset Device
  1314. transactionSetResult( TRANSACTION_RESULT_OK );
  1315. };
  1316. break;
  1317. };
  1318. case LRF_WS_STAGE_3:{
  1319. // Remove Generated Files
  1320. if( m_bTGENFILECNT ){
  1321. for( uint8_t idx = 0; idx < m_bTGENFILECNT; idx++){
  1322. char tName[256];
  1323. sprintf( tName, "data/uploads/%s", m_pTGENFILENAMES[idx]);
  1324. unlink( tName );
  1325. };
  1326. };
  1327. transactionSetResult( TRANSACTION_RESULT_OK );
  1328. break;
  1329. };
  1330. case LRF_WS_STAGE_4:{
  1331. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1332. if( m_bActualDeviceWM != MODE_MASK ) {
  1333. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1334. m_bRequestedDeviceWM = MODE_MASK;
  1335. if( !setMode( MODE_MASK ) ) {
  1336. LOG_ERROR("Fall Back to Disconnected!");
  1337. setWM( LRF_WM_CONNECTION );
  1338. } else transactionWaitResult();
  1339. } else {
  1340. transactionSetResult( TRANSACTION_RESULT_OK );
  1341. };
  1342. break;
  1343. };
  1344. case LRF_WS_STAGE_5:{
  1345. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1346. if (!setShadowInfo( m_sShadowHelper.m_bSequence, m_sShadow )) {
  1347. LOG_ERROR("Fall Back to Disconnected!");
  1348. setWM( LRF_WM_CONNECTION );
  1349. } else transactionWaitResult();
  1350. break;
  1351. };
  1352. case LRF_WS_STAGE_6:{
  1353. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1354. if( m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ){
  1355. m_sShadowHelper.m_nCurrentPoint = 0;
  1356. if(!setShadowPoly( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentPolygon)){
  1357. LOG_ERROR("Fall Back to Disconnected!");
  1358. setWM( LRF_WM_CONNECTION );
  1359. } else transactionWaitResult();
  1360. } else {
  1361. #ifdef DEBUG_EXTRA
  1362. LOG("Start Send Rays!");
  1363. #endif
  1364. m_sShadowHelper.m_nCurrentRayLine = 0;
  1365. //skip all empty angles of binary shadow
  1366. while( ( m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount == 0 ) && m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  1367. m_sShadowHelper.m_nCurrentRayLine++;
  1368. };
  1369. if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  1370. //if we have an shadows -> transfer else skeep
  1371. setStage( LRF_WS_STAGE_8 );
  1372. } else {
  1373. #ifdef DEBUG_EXTRA
  1374. LOG("Skip Uploading Rays");
  1375. #endif
  1376. setStage( LRF_WS_STAGE_10 );
  1377. };
  1378. };
  1379. break;
  1380. };
  1381. case LRF_WS_STAGE_7:{
  1382. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1383. #ifdef DEBUG_EXTRA
  1384. char tBuff[128];
  1385. sprintf( tBuff,"Send Point %d of %d", (m_sShadowHelper.m_nCurrentPoint + 1), m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount );
  1386. LOG( tBuff );
  1387. #endif
  1388. if( m_sShadowHelper.m_nCurrentPoint < m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount ){
  1389. if( !setShadowPolyPoint( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentPolygon, m_sShadowHelper.m_nCurrentPoint) ){
  1390. LOG_ERROR("Fall Back to Disconnected!");
  1391. setWM( LRF_WM_CONNECTION );
  1392. } else transactionWaitResult();
  1393. } else {
  1394. m_sShadowHelper.m_nCurrentPolygon ++;
  1395. setStage( LRF_WS_STAGE_6 );
  1396. };
  1397. break;
  1398. };
  1399. case LRF_WS_STAGE_8:{
  1400. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1401. #ifdef DEBUG_EXTRA
  1402. char tBuff[128];
  1403. sprintf( tBuff,"Send Shadow for angle %d [%d rays]", m_sShadowHelper.m_nCurrentRayLine, m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount );
  1404. LOG( tBuff );
  1405. #endif
  1406. if( !setShadowRay( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentRayLine ) ){
  1407. LOG_ERROR("Fall Back to Disconnected!");
  1408. setWM( LRF_WM_CONNECTION );
  1409. } else transactionWaitResult();
  1410. break;
  1411. };
  1412. case LRF_WS_STAGE_9:{
  1413. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1414. #ifdef DEBUG_EXTRA
  1415. char tBuff[128];
  1416. sprintf( tBuff,"Send Shadow RayLine %d for andle %d", m_sShadowHelper.m_nCurrentRay, m_sShadowHelper.m_nCurrentRayLine );
  1417. LOG( tBuff );
  1418. #endif
  1419. if( m_sShadowHelper.m_nCurrentRay < m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount ) {
  1420. if( !setShadowRayLine( m_sShadowHelper.m_bSequence, m_sShadow, m_sShadowHelper.m_nCurrentRayLine, m_sShadowHelper.m_nCurrentRay ) ){
  1421. LOG_ERROR("Fall Back to Disconnected!");
  1422. setWM( LRF_WM_CONNECTION );
  1423. } else transactionWaitResult();
  1424. } else {
  1425. // search next not empty angle
  1426. do {
  1427. m_sShadowHelper.m_nCurrentRayLine++;
  1428. } while ( ( m_sShadow.ray_lines[m_sShadowHelper.m_nCurrentRayLine].nCount == 0 ) && m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES );
  1429. if ( m_sShadowHelper.m_nCurrentRayLine < MAX_2D_SHADOW_RAY_LINES ) {
  1430. setStage( LRF_WS_STAGE_8 );
  1431. } else {
  1432. // If not found -> finish upload shadow
  1433. transactionSetResult( TRANSACTION_RESULT_OK );
  1434. };
  1435. };
  1436. break;
  1437. };
  1438. case LRF_WS_STAGE_10:{
  1439. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1440. if (!setShadowDone( m_sShadowHelper.m_bSequence )) {
  1441. LOG_ERROR("Fall Back to Disconnected!");
  1442. setWM( LRF_WM_CONNECTION );
  1443. } else transactionWaitResult();
  1444. break;
  1445. };
  1446. case LRF_WS_STAGE_11: setWM( LRF_WM_IDLE ); break;
  1447. default: setStage( LRF_WS_INIT ); break;
  1448. };
  1449. break;
  1450. };
  1451. case STG_WAIT: waitStateFunction(); break;
  1452. case STG_POOL: poolStateFunction(); break;
  1453. };
  1454. };
  1455. void device2DSensor::save_eprom(){
  1456. };
  1457. void device2DSensor::getfileMode(){
  1458. #ifdef DEBUG_EXTRA
  1459. LOG("GETFILE");
  1460. #endif
  1461. // Reset State
  1462. initWModeFunction( LRF_WM_FILE );
  1463. switch( getPPStatus() ){
  1464. case STG_PUSH:{
  1465. switch( getStage() ) {
  1466. case LRF_WS_INIT:{
  1467. m_nCurrentAngle = 0;
  1468. if ( m_bActualDeviceWM != MODE_GETFILE ) {
  1469. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1470. m_bRequestedDeviceWM = MODE_GETFILE;
  1471. if( !setMode( MODE_GETFILE ) ) {
  1472. LOG_ERROR("Fall Back to Disconnected!");
  1473. setWM( LRF_WM_CONNECTION );
  1474. } else transactionWaitResult();
  1475. } else {
  1476. transactionSetResult( TRANSACTION_RESULT_OK );
  1477. };
  1478. break;
  1479. };
  1480. case LRF_WS_STAGE_1:{
  1481. if( m_sGetFileTransfers.helpers.size() ){
  1482. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index = ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index + 0x04) % GF_OP_INDEX;
  1483. switch( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state ) {
  1484. case GF_OP_CLOSE:{
  1485. // After Sending check if have other file to send
  1486. if( m_sGetFileTransfers.current < ( m_sGetFileTransfers.helpers.size() - 1 ) ) {
  1487. m_sGetFileTransfers.current++;
  1488. } else {
  1489. //switch( m_sGetFileTransfers.transfer_ending_mode ) {
  1490. // case GF_EM_ISSUE_RESET:{
  1491. // break;
  1492. // };
  1493. // case GF_EM_ISSUE_SAVE_FW:{
  1494. // break;
  1495. // }
  1496. // default: setStage( LRF_WS_STAGE_7 );
  1497. //};
  1498. transactionSetResult( TRANSACTION_RESULT_OK );
  1499. return;
  1500. };
  1501. };
  1502. case GF_OP_OK:{
  1503. // INIT Sending File
  1504. char tB[256];
  1505. #ifdef DEBUG_EXTRA
  1506. sprintf( tB, "data/uploads/%s", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->sourceName );
  1507. LOG(tB);
  1508. #endif
  1509. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = fopen( tB, "rb" );
  1510. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd ) {
  1511. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_OPEN;
  1512. #ifdef DEBUG_EXTRA
  1513. LOG("OPEN -> Send Info");
  1514. #endif
  1515. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = strlen( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName );
  1516. memcpy(
  1517. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1518. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName,
  1519. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size
  1520. );
  1521. } else {
  1522. #ifdef DEBUG_EXTRA
  1523. LOG("NOPEN -> Cancel");
  1524. #endif
  1525. //memset( &m_sGetFileHelper, 0, sizeof( GETFILE_IO_HELPER ));
  1526. transactionSetResult( TRANSACTION_RESULT_OK );
  1527. return;
  1528. };
  1529. break;
  1530. };
  1531. case GF_OP_OPEN:{
  1532. #ifdef DEBUG_EXTRA
  1533. LOG("Info -> Data");
  1534. #endif
  1535. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_DATA;
  1536. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1537. 1,
  1538. 48,
  1539. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1540. );
  1541. #ifdef DEBUG_EXTRA
  1542. char tB[64];
  1543. sprintf( tB,"%d of %d bytes", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize );
  1544. LOG( tB );
  1545. #endif
  1546. break;
  1547. };
  1548. case GF_OP_DATA:{
  1549. long int pos = ftell( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1550. uint8_t CP = (uint8_t)((((float)pos)*100.0f)/((float)m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize));
  1551. #ifdef DEBUG_EXTRA
  1552. char tB[64];
  1553. if ( pos != EOF ){
  1554. sprintf( tB, "%d of %d bytes %d%%", pos, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize, CP );
  1555. LOG( tB );
  1556. } else {
  1557. LOG("File Ended!");
  1558. };
  1559. #endif
  1560. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1561. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, CP);
  1562. }
  1563. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1564. 1,
  1565. 48,
  1566. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1567. );
  1568. #ifdef DEBUG_EXTRA
  1569. sprintf( tB,"File Reads %d bytes.", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size );
  1570. LOG( tB );
  1571. #endif
  1572. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size == 0 ) {
  1573. #ifdef DEBUG_EXTRA
  1574. LOG("Change to Close");
  1575. #endif
  1576. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_CLOSE;
  1577. fclose( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1578. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = NULL;
  1579. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1580. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, 200);
  1581. };
  1582. };
  1583. break;
  1584. };
  1585. };
  1586. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1587. if( !sendGF_package(
  1588. ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state | m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ),
  1589. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size,
  1590. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data
  1591. )
  1592. ){
  1593. LOG_ERROR("Fall Back to Disconnected!");
  1594. setWM( LRF_WM_CONNECTION );
  1595. } else transactionWaitResult();
  1596. } else {
  1597. // Exit From GetFile Mode
  1598. transactionSetResult( TRANSACTION_RESULT_OK );
  1599. };
  1600. break;
  1601. };
  1602. case LRF_WS_STAGE_2:
  1603. updateDataStatus( ARGUS_DS_READY );
  1604. setWM( LRF_WM_IDLE );
  1605. break;
  1606. };
  1607. break;
  1608. };
  1609. case STG_WAIT: waitStateFunction(); break;
  1610. case STG_POOL: poolStateFunction(); break;
  1611. };
  1612. };
  1613. void device2DSensor::drawMode(){
  1614. #ifdef DEBUG_EXTRA
  1615. LOG("DRAW");
  1616. #endif
  1617. // Reset State
  1618. initWModeFunction( LRF_WM_BORDER );
  1619. switch( getPPStatus() ){
  1620. case STG_PUSH:{
  1621. switch( getStage() ) {
  1622. case LRF_WS_INIT:{
  1623. m_nCurrentAngle = 0;
  1624. if ( m_bActualDeviceWM != MODE_BORDER ) {
  1625. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1626. m_bRequestedDeviceWM = MODE_BORDER;
  1627. if( !setMode( MODE_BORDER ) ) {
  1628. LOG_ERROR("Fall Back to Disconnected!");
  1629. setWM( LRF_WM_CONNECTION );
  1630. } else transactionWaitResult();
  1631. } else {
  1632. transactionSetResult( TRANSACTION_RESULT_OK );
  1633. };
  1634. break;
  1635. };
  1636. case LRF_WS_STAGE_1:{
  1637. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1638. if( !getFastDraw( m_nCurrentAngle ) ) {
  1639. LOG_ERROR("Fall Back to Disconnected!");
  1640. setWM( LRF_WM_CONNECTION );
  1641. } else transactionWaitResult();
  1642. break;
  1643. };
  1644. case LRF_WS_STAGE_2:{
  1645. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1646. if ( !setMode( MODE_DETECTOR ) ) {
  1647. LOG_ERROR("Fall Back to Disconnected!");
  1648. setWM( LRF_WM_CONNECTION );
  1649. } else transactionWaitResult();
  1650. break;
  1651. };
  1652. case LRF_WS_STAGE_3:{
  1653. updateDataStatus( ARGUS_DS_READY );
  1654. setWM( LRF_WM_IDLE );
  1655. break;
  1656. };
  1657. };
  1658. break;
  1659. };
  1660. case STG_WAIT: waitStateFunction(); break;
  1661. case STG_POOL: poolStateFunction(); break;
  1662. };
  1663. };
  1664. void device2DSensor::maskMode(){
  1665. #ifdef DEBUG_EXTRA
  1666. LOG("MASKOP-ShortSet+MZ");
  1667. #endif
  1668. if( initWModeFunction( LRF_WM_STATUS_MASK ) ){
  1669. //m_bCurShadowMask = m_pDBC->getLRFMasksStatus( m_sLRF_DATA.id );
  1670. #ifdef DEBUG_EXTRA
  1671. char tBuf[64];
  1672. sprintf( tBuf, "CMask: 0x%02X", m_bCurShadowMask );
  1673. LOG( tBuf );
  1674. #endif
  1675. updateDataStatus( ARGUS_DS_BUSY );
  1676. memset( m_pTGENFILENAMES[0], 0, 33 );
  1677. memset( m_pTGENFILENAMES[1], 0, 33 );
  1678. memset( m_pTGENFILENAMES[2], 0, 33 );
  1679. m_bTGENFILECNT = 0;
  1680. m_bTGENFILE = 0;
  1681. m_sGetFileTransfers.current = 0;
  1682. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  1683. if ( m_sGetFileTransfers.helpers.size() ) {
  1684. for ( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx< m_sGetFileTransfers.helpers.end(); idx++ ){
  1685. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  1686. free( (*idx) );
  1687. };
  1688. m_sGetFileTransfers.helpers.clear();
  1689. };
  1690. };
  1691. switch( getPPStatus() ){
  1692. case STG_PUSH:{
  1693. switch( getStage() ) {
  1694. case LRF_WS_INIT:{
  1695. if( m_bTGENFILECNT == 0 ){
  1696. if( generateRestartFile( m_bTGENFILE ) ){
  1697. GETFILE_IO_HELPER *tH = NULL;
  1698. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  1699. if ( tH ) {
  1700. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  1701. strncpy( tH->destName, "updateMZ.bat", 16 );
  1702. strncpy( tH->sourceName, m_pTGENFILENAMES[m_bTGENFILE], 32 );
  1703. struct stat st;
  1704. char tB[256];
  1705. sprintf( tB, "data/uploads/%s", tH->sourceName );
  1706. stat( tB, &st );
  1707. tH->fileSize = st.st_size;
  1708. tH->state = GF_OP_OK;
  1709. tH->db_id = 0;
  1710. tH->index = 0x04;
  1711. m_sGetFileTransfers.helpers.push_back( tH );
  1712. m_bTGENFILE++;
  1713. m_bTGENFILECNT++;
  1714. };
  1715. } else {
  1716. LOG_ERROR("Gen UpdateMZ Failure!");
  1717. };
  1718. };
  1719. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1720. m_bCurShadowMask = m_pDBC->getLRFMasksStatus( m_sLRF_DATA.id );
  1721. if ( !setShadowStatus( m_bCurShadowMask ) ) {
  1722. LOG_ERROR("Fall Back to Disconnected!");
  1723. setWM( LRF_WM_CONNECTION );
  1724. } else transactionWaitResult();
  1725. break;
  1726. };
  1727. case LRF_WS_STAGE_1:{
  1728. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1729. #ifdef DEBUG_EXTRA
  1730. LOG("Sending Update MZ!");
  1731. #endif
  1732. if ( m_bActualDeviceWM != MODE_GETFILE ) {
  1733. m_bRequestedDeviceWM = MODE_GETFILE;
  1734. if( !setMode( MODE_GETFILE ) ) {
  1735. LOG_ERROR("Fall Back to Disconnected!");
  1736. setWM( LRF_WM_CONNECTION );
  1737. } else transactionWaitResult();
  1738. } else {
  1739. transactionSetResult( TRANSACTION_RESULT_OK );
  1740. };
  1741. break;
  1742. };
  1743. case LRF_WS_STAGE_2:{
  1744. if( m_sGetFileTransfers.helpers.size() ){
  1745. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index = ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index + 0x04) % GF_OP_INDEX;
  1746. switch( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state ) {
  1747. case GF_OP_CLOSE:{
  1748. // After Sending check if have other file to send
  1749. if( m_sGetFileTransfers.current < ( m_sGetFileTransfers.helpers.size() - 1 ) ) {
  1750. m_sGetFileTransfers.current++;
  1751. } else {
  1752. //switch( m_sGetFileTransfers.transfer_ending_mode ) {
  1753. // case GF_EM_ISSUE_RESET:{
  1754. // break;
  1755. // };
  1756. // case GF_EM_ISSUE_SAVE_FW:{
  1757. // break;
  1758. // }
  1759. // default: setStage( LRF_WS_STAGE_7 );
  1760. //};
  1761. transactionSetResult( TRANSACTION_RESULT_OK );
  1762. return;
  1763. };
  1764. };
  1765. case GF_OP_OK:{
  1766. // INIT Sending File
  1767. char tB[256];
  1768. #ifdef DEBUG_EXTRA
  1769. sprintf( tB, "data/uploads/%s", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->sourceName );
  1770. LOG(tB);
  1771. #endif
  1772. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = fopen( tB, "rb" );
  1773. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd ) {
  1774. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_OPEN;
  1775. #ifdef DEBUG_EXTRA
  1776. LOG("OPEN -> Send Info");
  1777. #endif
  1778. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = strlen( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName );
  1779. memcpy(
  1780. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1781. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->destName,
  1782. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size
  1783. );
  1784. } else {
  1785. #ifdef DEBUG_EXTRA
  1786. LOG("NOPEN -> Cancel");
  1787. #endif
  1788. //memset( &m_sGetFileHelper, 0, sizeof( GETFILE_IO_HELPER ));
  1789. transactionSetResult( TRANSACTION_RESULT_OK );
  1790. return;
  1791. };
  1792. break;
  1793. };
  1794. case GF_OP_OPEN:{
  1795. #ifdef DEBUG_EXTRA
  1796. LOG("Info -> Data");
  1797. #endif
  1798. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_DATA;
  1799. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1800. 1,
  1801. 48,
  1802. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1803. );
  1804. #ifdef DEBUG_EXTRA
  1805. char tB[64];
  1806. sprintf( tB,"%d of %d bytes", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize );
  1807. LOG( tB );
  1808. #endif
  1809. break;
  1810. };
  1811. case GF_OP_DATA:{
  1812. long int pos = ftell( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1813. uint8_t CP = (uint8_t)((((float)pos)*100.0f)/((float)m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize));
  1814. #ifdef DEBUG_EXTRA
  1815. char tB[64];
  1816. if ( pos != EOF ){
  1817. sprintf( tB, "%d of %d bytes %d%%", pos, m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fileSize, CP );
  1818. LOG( tB );
  1819. } else {
  1820. LOG("File Ended!");
  1821. };
  1822. #endif
  1823. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1824. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, CP);
  1825. }
  1826. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size = fread( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data,
  1827. 1,
  1828. 48,
  1829. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd
  1830. );
  1831. #ifdef DEBUG_EXTRA
  1832. sprintf( tB,"File Reads %d bytes.", m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size );
  1833. LOG( tB );
  1834. #endif
  1835. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size == 0 ) {
  1836. #ifdef DEBUG_EXTRA
  1837. LOG("Change to Close");
  1838. #endif
  1839. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state = GF_OP_CLOSE;
  1840. fclose( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd );
  1841. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->fd = NULL;
  1842. if ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id ) {
  1843. m_pDBC->updateLRFUpload(m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->db_id, 200);
  1844. };
  1845. };
  1846. break;
  1847. };
  1848. };
  1849. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1850. if( !sendGF_package(
  1851. ( m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->state | m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ),
  1852. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data_size,
  1853. m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->data
  1854. )
  1855. ){
  1856. LOG_ERROR("Fall Back to Disconnected!");
  1857. setWM( LRF_WM_CONNECTION );
  1858. } else transactionWaitResult();
  1859. } else {
  1860. // Exit From GetFile Mode --> No need --> Reset Device
  1861. transactionSetResult( TRANSACTION_RESULT_OK );
  1862. };
  1863. break;
  1864. };
  1865. case LRF_WS_STAGE_3:{
  1866. // Remove Generated Files
  1867. if( m_bTGENFILECNT ){
  1868. for( uint8_t idx = 0; idx < m_bTGENFILECNT; idx++){
  1869. char tName[256];
  1870. sprintf( tName, "data/uploads/%s", m_pTGENFILENAMES[idx]);
  1871. unlink( tName );
  1872. };
  1873. };
  1874. transactionSetResult( TRANSACTION_RESULT_OK );
  1875. break;
  1876. };
  1877. case LRF_WS_STAGE_4:{
  1878. //Issue Full Reset
  1879. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1880. if ( !doFullReset() ) {
  1881. LOG_ERROR("Fall Back to Disconnected!");
  1882. setWM( LRF_WM_CONNECTION );
  1883. } else transactionWaitResult();
  1884. break;
  1885. };
  1886. case LRF_WS_STAGE_5:{
  1887. //setWM( LRF_WM_HANDSHAKE );
  1888. setWM( LRF_WM_IDLE );
  1889. break;
  1890. };
  1891. default: setStage( LRF_WS_INIT ); break;
  1892. };
  1893. break;
  1894. };
  1895. case STG_WAIT: waitStateFunction(); break;
  1896. case STG_POOL: poolStateFunction(); break;
  1897. };
  1898. };
  1899. void device2DSensor::alarmMode(){
  1900. #ifdef DEBUG_EXTRA
  1901. LOG("ALRM");
  1902. #endif
  1903. //Reset State
  1904. if( initWModeFunction( LRF_WM_ALARM ) ) if( m_sLRF_DATA.data_status!= ARGUS_DS_ALARMED ) updateDataStatus( ARGUS_DS_ALARMED );
  1905. switch( getPPStatus() ){
  1906. case STG_PUSH:
  1907. switch( getStage() ) {
  1908. case LRF_WS_INIT:{
  1909. if ( m_bActualDeviceWM != MODE_DETECTOR ) {
  1910. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1911. m_bRequestedDeviceWM = MODE_DETECTOR;
  1912. if( !setMode( MODE_DETECTOR ) ) {
  1913. LOG_ERROR("Fall Back to Disconnected!");
  1914. setWM( LRF_WM_CONNECTION );
  1915. } else transactionWaitResult();
  1916. } else {
  1917. transactionSetResult( TRANSACTION_RESULT_OK );
  1918. };
  1919. break;
  1920. };
  1921. case LRF_WS_STAGE_1:{
  1922. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1923. if( !getState2() ) {
  1924. LOG_ERROR("Fall Back to Disconnected!");
  1925. setWM( LRF_WM_CONNECTION );
  1926. } else transactionWaitResult();
  1927. break;
  1928. };
  1929. case LRF_WS_STAGE_2:{
  1930. setStage( LRF_WS_STAGE_1 );
  1931. break;
  1932. };
  1933. default: setStage( LRF_WS_INIT ); break;
  1934. };
  1935. break;
  1936. case STG_WAIT: waitStateFunction(); break;
  1937. case STG_POOL: poolStateFunction(); break;
  1938. };
  1939. };
  1940. void device2DSensor::idleMode(){
  1941. #ifdef DEBUG_EXTRA
  1942. LOG("IDLE");
  1943. #endif
  1944. //Reset State
  1945. initWModeFunction( LRF_WM_IDLE );
  1946. switch( getPPStatus() ){
  1947. case STG_PUSH:
  1948. switch( getStage() ) {
  1949. case LRF_WS_INIT:{
  1950. if ( m_bActualDeviceWM != MODE_DETECTOR ) {
  1951. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1952. m_bRequestedDeviceWM = MODE_DETECTOR;
  1953. if( !setMode( MODE_DETECTOR ) ) {
  1954. LOG_ERROR("Fall Back to Disconnected!");
  1955. setWM( LRF_WM_CONNECTION );
  1956. } else transactionWaitResult();
  1957. } else {
  1958. transactionSetResult( TRANSACTION_RESULT_OK );
  1959. };
  1960. break;
  1961. };
  1962. case LRF_WS_STAGE_1:{
  1963. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1964. if( !getState2() ) {
  1965. LOG_ERROR("Fall Back to Disconnected!");
  1966. setWM( LRF_WM_CONNECTION );
  1967. } else transactionWaitResult();
  1968. break;
  1969. };
  1970. case LRF_WS_STAGE_2:{
  1971. setStage( LRF_WS_STAGE_1 );
  1972. DELAY_10MS( 10 ); // 100 ms
  1973. break;
  1974. };
  1975. default: setStage( LRF_WS_INIT ); break;
  1976. };
  1977. break;
  1978. case STG_WAIT: waitStateFunction(); break;
  1979. case STG_POOL: poolStateFunction(); break;
  1980. };
  1981. };
  1982. void device2DSensor::proceedDBCommands(){
  1983. if( DBC_PUSHED_CMD *tCMD = m_pDBC->getNodePushedCMD( m_sNodeData.node_id ) ) {
  1984. m_nProcessingCmdId = tCMD->id;
  1985. m_pDBC->updateNodePushedCMD( m_nProcessingCmdId, DBC_CMD_STARTED );
  1986. switch( tCMD->CMD ) {
  1987. case ARGUS_LRF_PCMD_FULL_RESET:{
  1988. if ( m_bActualDeviceWM == MODE_DETECTOR ) {
  1989. DELAY_10MS(10); // 100 ms
  1990. if( !doFullReset()) {
  1991. LOG_ERROR("Fall Back to Disconnected!");
  1992. setWM( LRF_WM_CONNECTION );
  1993. } else {
  1994. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  1995. updateDataStatus( ARGUS_DS_BUSY );
  1996. transactionWaitResult();
  1997. };
  1998. #ifdef DEBUG_EXTRA
  1999. } else {
  2000. LOG("Wrong Mode! Not DETECTOR!");
  2001. #endif
  2002. };
  2003. break;
  2004. };
  2005. case ARGUS_LRF_PCMD_RESET:{
  2006. if ( m_bActualDeviceWM == MODE_DETECTOR ) {
  2007. DELAY_10MS(10); // 100 ms
  2008. if( !doReset()) {
  2009. LOG_ERROR("Fall Back to Disconnected!");
  2010. setWM( LRF_WM_CONNECTION );
  2011. } else {
  2012. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  2013. updateDataStatus( ARGUS_DS_STUDY );
  2014. transactionWaitResult();
  2015. };
  2016. #ifdef DEBUG_EXTRA
  2017. } else {
  2018. LOG("Wrong Mode! Not DETECTOR!");
  2019. #endif
  2020. };
  2021. break;
  2022. };
  2023. case ARGUS_LRF_PCMD_UPLOAD_FILE:{
  2024. #ifdef DEBUG_EXTRA
  2025. LOG( "CMD UPLOAD" );
  2026. #endif
  2027. DBC_INT_LIST *pUploads = m_pDBC->listLRFUploads( m_sLRF_DATA.id );
  2028. m_sGetFileTransfers.current = 0;
  2029. m_sGetFileTransfers.transfer_ending_mode = GF_EM_DO_NOTHING;
  2030. if ( m_sGetFileTransfers.helpers.size() ) {
  2031. for ( VECTOR_GF_HELPERS::iterator idx = m_sGetFileTransfers.helpers.begin(); idx< m_sGetFileTransfers.helpers.end(); idx++ ){
  2032. if ( (*idx)->fd != NULL ) fclose( (*idx)->fd );
  2033. free( (*idx) );
  2034. };
  2035. m_sGetFileTransfers.helpers.clear();
  2036. };
  2037. if (pUploads != NULL) {
  2038. for( DBC_INT_LIST::iterator idx = pUploads->begin(); idx < pUploads->end(); idx++ ){
  2039. DBC_LRF_UPLOAD_DATA *tData = m_pDBC->getLRFUploadData((*idx));
  2040. if ( tData ) {
  2041. GETFILE_IO_HELPER *tH = NULL;
  2042. tH = (GETFILE_IO_HELPER *) malloc(sizeof(GETFILE_IO_HELPER));
  2043. if ( tH ) {
  2044. memset( tH, 0, sizeof(GETFILE_IO_HELPER) );
  2045. strncpy( tH->destName, tData->fname, 16 );
  2046. strncpy( tH->sourceName, tData->md5, 32 );
  2047. struct stat st;
  2048. char tB[256];
  2049. sprintf( tB, "data/uploads/%s", tH->sourceName );
  2050. stat( tB, &st );
  2051. tH->fileSize = st.st_size;
  2052. tH->state = GF_OP_OK;
  2053. tH->db_id = (*idx);
  2054. tH->index = 0x04;
  2055. if( tData->transfer_mode > m_sGetFileTransfers.transfer_ending_mode ) m_sGetFileTransfers.transfer_ending_mode = tData->transfer_mode;
  2056. m_sGetFileTransfers.helpers.push_back(tH);
  2057. };
  2058. //m_pDBC->updateLRFUpload( (*idx), 1 );
  2059. } else {
  2060. LOG_ERROR( "No Upload Data Fetched From DB!" );
  2061. };
  2062. };
  2063. pUploads->clear();
  2064. delete( pUploads );
  2065. updateDataStatus( ARGUS_DS_BUSY );
  2066. setWM( LRF_WM_FILE );
  2067. };
  2068. break;
  2069. };
  2070. case ARGUS_LRF_PCMD_DRAW:{
  2071. if ( m_bActualDeviceWM == MODE_DETECTOR ) {
  2072. if( m_sLRF_DATA.data_status == ARGUS_DS_READY ){
  2073. setWM( LRF_WM_BORDER );
  2074. updateDataStatus( ARGUS_DS_BUSY );
  2075. };
  2076. #ifdef DEBUG_EXTRA
  2077. } else {
  2078. LOG("Wrong Mode! Not DETECTOR!");
  2079. #endif
  2080. };
  2081. break;
  2082. };
  2083. //Masks Deletion
  2084. case 75:
  2085. case 76:
  2086. case 77:
  2087. case 78:
  2088. case 79:
  2089. case 80:{
  2090. setWM( LRF_WM_SAVE_MASKS );
  2091. break;
  2092. };
  2093. case ARGUS_LRF_PCMD_READ_ALL:{
  2094. if ( m_bActualDeviceWM == MODE_DETECTOR ) {
  2095. setWM( LRF_WM_SYNC_UP );
  2096. updateDataStatus( ARGUS_DS_BUSY );
  2097. #ifdef DEBUG_EXTRA
  2098. } else {
  2099. LOG("Wrong Mode! Not DETECTOR!");
  2100. #endif
  2101. };
  2102. break;
  2103. };
  2104. case ARGUS_LRF_PCMD_SAVE_SETTINGS_0:{
  2105. setWM( LRF_WM_SAVE_HW );
  2106. break;
  2107. };
  2108. case ARGUS_LRF_PCMD_SAVE_SETTINGS_1:{
  2109. setWM( LRF_WM_SAVE_DETECTION );
  2110. break;
  2111. };
  2112. case ARGUS_LRF_PCMD_SAVE_SETTINGS_2:{
  2113. setWM( LRF_WM_SAVE_MASKS );
  2114. break;
  2115. };
  2116. case ARGUS_LRF_PCMD_SAVE_SETTINGS_3:{
  2117. setWM( LRF_WM_SAVE_EPROM );
  2118. break;
  2119. };
  2120. case ARGUS_LRF_PCMD_MASK_STATUS_SET:{
  2121. setWM( LRF_WM_STATUS_MASK );
  2122. break;
  2123. };
  2124. case ARGUS_LRF_PCMD_SAVE_ALL:
  2125. case ARGUS_LRF_PCMD_UPDATE_ALL:{
  2126. if ( m_bActualDeviceWM == MODE_DETECTOR ) {
  2127. setWM( LRF_WM_SYNC_DOWN );
  2128. updateDataStatus( ARGUS_DS_BUSY );
  2129. #ifdef DEBUG_EXTRA
  2130. } else {
  2131. LOG("Wrong Mode! Not DETECTOR!");
  2132. #endif
  2133. };
  2134. break;
  2135. };
  2136. /*
  2137. case ARGUS_LRF_PCMD_SET_DP_0:{
  2138. if( tCMD->db_cmd ) m_nProcessingCmdId = tCMD->db_cmd; else m_nProcessingCmdId = 0;
  2139. m_nActiveDetectionProfile = 0;
  2140. setWM( ANODE_WM_2D_SET_MASTER_PARAM );
  2141. updateDataStatus( ARGUS_DS_BUSY );
  2142. break;
  2143. };
  2144. */
  2145. case ARGUS_LRF_ROI_ACTIVATION:{
  2146. uint16_t sz = strlen(tCMD->PARAM);
  2147. if( sz ){
  2148. uint16_t AZ, RANGE, WIDTH;
  2149. uint8_t LRFID = 0;
  2150. AZ = atoi(strstr(tCMD->PARAM, ":") + 1) - m_sLRF_DATA.base_shift;
  2151. RANGE = atoi( tCMD->PARAM );
  2152. WIDTH = 8888;
  2153. #ifdef DEBUG_EXTRA
  2154. char tBuff[128];
  2155. sprintf( tBuff, "ROI AZ: %d RANGE: %d WIDTH: %d", AZ, RANGE, WIDTH );
  2156. LOG( tBuff );
  2157. #endif
  2158. EVENT_CONTAINER * bEVT = NULL;
  2159. TARGET_DATA * tData = prepareTargetData( AZ, RANGE, WIDTH, LRFID );
  2160. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_ROI_ACTIVE, NULL, NULL, tData );
  2161. if( bEVT != NULL ) {
  2162. m_pEventHeap->broadcast( bEVT );
  2163. } else {
  2164. free( tData );
  2165. };
  2166. };
  2167. break;
  2168. };
  2169. default:
  2170. char KKK[256];
  2171. sprintf( KKK, "Unknown DBC command[%d]",tCMD->CMD );
  2172. LOG( KKK );
  2173. m_pDBC->updateNodePushedCMD( m_nProcessingCmdId, DBC_CMD_FINISHED );
  2174. };
  2175. free( tCMD );
  2176. };
  2177. };
  2178. bool device2DSensor::validateCommunicationRessource(){
  2179. if( getConnectionStatus() != CS_CONNECTED ){
  2180. #ifdef DEBUG_EXTRA
  2181. LOG("Connection ->Disconnected!");
  2182. #endif
  2183. updateDataStatus( ARGUS_DS_DISCONNECTED );
  2184. setWM( LRF_WM_CONNECTION );
  2185. return false;
  2186. };
  2187. return true;
  2188. };
  2189. //bool device2DSensor::runSequence( COMMAND_SEQUENCE_TYPE SequenceID ){
  2190. // if ( m_eCurrentSequence!= NO_SEQUENCE ) return false;
  2191. // m_bSequenceStep = 0;
  2192. // m_eCurrentSequence = SequenceID;
  2193. // return true;
  2194. //};
  2195. #define SIZE_IDX 1
  2196. #define DEV_ID_IDX 2
  2197. #define CMD_IDX 3
  2198. #define SUB_CMD_IDX 4
  2199. #define DATA_BEGIN_IDX 5
  2200. #define PRINTOUT {for( int z=0; z < Package->m_nSize; z++ ) { \
  2201. sprintf( tBuff, "P[%d]:0x%02X", z, Package->m_pData[z]); \
  2202. LOG( tBuff ); \
  2203. };}
  2204. bool device2DSensor::packetHandler( protocolPackage *Package ){
  2205. uint8_t bCWM = getWM();
  2206. uint8_t bOWM = getOWM();
  2207. LRF_WORK_STAGE ePSTG = getPrevStage();
  2208. uint8_t cDeviceID = Package->m_pData[DEV_ID_IDX];
  2209. if( ( cDeviceID == m_sLRF_DATA.device_id ) || ( m_sLRF_DATA.device_id == 0 ) ) {
  2210. #ifdef DEBUG
  2211. char tBuff[256];
  2212. #ifdef DEBUG_EXTRA
  2213. sprintf( tBuff, "DEV-> Code 0x%02X",Package->m_pData[CMD_IDX] );
  2214. LOG( tBuff );
  2215. #endif
  2216. #endif
  2217. // Have Some Replay from device -> Reset Timeouts
  2218. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  2219. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  2220. switch( Package->m_pData[CMD_IDX] ) {
  2221. case CMD_STATE: transactionSetResult( TRANSACTION_RESULT_OK ); break;
  2222. case CMD_QUIT: transactionSetResult( TRANSACTION_RESULT_OK ); break;
  2223. case CMD_RESET: transactionSetResult( TRANSACTION_RESULT_OK ); break;
  2224. case CMD_BORDER:{
  2225. if ((Package->m_pData[SUB_CMD_IDX] & BD_ERRORMASK) == 0x00) {
  2226. uint16_t AZ;
  2227. uint16_t RANGE_A;
  2228. uint16_t RANGE_B;
  2229. AZ = ( ( ( Package->m_pData[SUB_CMD_IDX] & 0x10 ) >> 4 ) << 8 ) | Package->m_pData[5];
  2230. RANGE_A = ( ( ( Package->m_pData[SUB_CMD_IDX] & 0x0C ) >> 2 ) << 8 ) | Package->m_pData[6];
  2231. RANGE_B = ( ( Package->m_pData[SUB_CMD_IDX] & 0x03 ) << 8 ) | Package->m_pData[7];
  2232. if( AZ < 359 ) {
  2233. m_vDrawRanges[m_nCurrentAngle]=( RANGE_A << 16 ) | RANGE_B;
  2234. m_nCurrentAngle = AZ+1;
  2235. if( m_nProcessingCmdId ){
  2236. m_pDBC->updateNodePushedCMD( m_nProcessingCmdId, (( ( 100 * AZ ) / 360 ) - 1 ) | 1 );
  2237. m_nProcessingCmdId = 0;
  2238. };
  2239. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2240. } else {
  2241. m_nCurrentAngle = 0;
  2242. handleDeviceDraw( 0, (void *)m_vDrawRanges, 360 * sizeof(uint32_t) );
  2243. if( m_nProcessingCmdId ){
  2244. m_pDBC->updateNodePushedCMD(m_nProcessingCmdId, 100 );
  2245. m_nProcessingCmdId = 0;
  2246. };
  2247. transactionSetResult( TRANSACTION_RESULT_OK );
  2248. };
  2249. } else {
  2250. switch((Package->m_pData[SUB_CMD_IDX] & BD_ERRORMASK)){
  2251. case BD_NOTREADY: {
  2252. #ifdef DEBUG_EXTRA
  2253. LOG_ERROR("DNR");
  2254. #endif
  2255. m_nCurrentAngle = 0;
  2256. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2257. break;
  2258. };
  2259. case BD_ERRORANGLE:{
  2260. #ifdef DEBUG_EXTRA
  2261. LOG_ERROR("EAN");
  2262. #endif
  2263. transactionSetResult( TRANSACTION_RESULT_OK );
  2264. break;
  2265. };
  2266. case BD_ERRORMODE:{
  2267. #ifdef DEBUG_EXTRA
  2268. LOG_ERROR("EMO");
  2269. #endif
  2270. transactionSetResult( TRANSACTION_RESULT_OK );
  2271. break;
  2272. };
  2273. };
  2274. };
  2275. break;
  2276. };
  2277. case CMD_MODE:{
  2278. m_bActualDeviceWM = (E_MODES) Package->m_pData[SUB_CMD_IDX];
  2279. #ifdef DEBUG_EXTRA
  2280. sprintf( tBuff, "Requested Mode:0x%02X Device Mode:0x%02X", m_bRequestedDeviceWM, m_bActualDeviceWM );
  2281. LOG( tBuff );
  2282. #endif
  2283. transactionSetResult( m_bRequestedDeviceWM == m_bActualDeviceWM );
  2284. break;
  2285. };
  2286. case CMD_GETFILE:{
  2287. #ifdef DEBUG_EXTRA
  2288. LOG("GF PKG");
  2289. #endif
  2290. // Sync mode Only
  2291. if( ( Package->m_pData[SUB_CMD_IDX] & GF_ERRORMASK ) != 0 ){
  2292. if( ( Package->m_pData[SUB_CMD_IDX] & GF_ERRORMODE ) != 0 ){
  2293. LOG_ERROR("Error: GF-Wrong WM!");
  2294. } else {
  2295. if( ( Package->m_pData[SUB_CMD_IDX] & GF_ERRORFILE ) != 0 ){
  2296. LOG_ERROR("Error: GF-File Error!");
  2297. } else {
  2298. LOG_ERROR("Error: GF-File gen Error!");
  2299. };
  2300. };
  2301. transactionSetResult( TRANSACTION_RESULT_OK );
  2302. } else {
  2303. if ( ( Package->m_pData[SUB_CMD_IDX] & GF_OP_INDEX ) == m_sGetFileTransfers.helpers[m_sGetFileTransfers.current]->index ) {
  2304. if(( Package->m_pData[SUB_CMD_IDX] & GF_OP_MASK ) == GF_OP_OK ){
  2305. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2306. } else {
  2307. LOG_ERROR("Error: GF-File Error(index)!");
  2308. transactionSetResult( TRANSACTION_RESULT_OK );
  2309. };
  2310. } else {
  2311. LOG_ERROR("Error: GF-File Error(operation)!");
  2312. transactionSetResult( TRANSACTION_RESULT_OK );
  2313. };
  2314. };
  2315. break;
  2316. };
  2317. case CMD_RESTART: transactionSetResult( TRANSACTION_RESULT_OK ); break;
  2318. case CMD_PARAMETERS:{
  2319. #ifdef DEBUG_EXTRA
  2320. LOG("PARAMETERS");
  2321. sprintf(tBuff, " Stage:\t%d", bCWM );
  2322. LOG(tBuff);
  2323. #endif
  2324. if( bCWM == LRF_WM_SYNC_DOWN ) {
  2325. if( Package->m_pData[SUB_CMD_IDX] == 0x80 ){
  2326. LOG("CMD MASTER PARAMETERS ACK");
  2327. //setStage( LRF_WS_STAGE_8 );
  2328. } else {
  2329. LOG("CMD SLAVE PARAMETERS ACK");
  2330. //setStage( LRF_WS_DELAY );
  2331. };
  2332. } else {
  2333. LOG("READ PARAMS FROM DEVICE");
  2334. LGS_2D_PARAMETERS tParams;
  2335. bool bMaster = true;
  2336. //RaspberyPi - not Intel - cannot copy
  2337. //memcpy( &tParams, Package->m_pData + DATA_BEGIN_IDX, sizeof( LGS_2D_PARAMETERS ) );
  2338. tParams.nOrder = ( Package->m_pData[5] << 8 ) | Package->m_pData[6];
  2339. tParams.nOffset = ( Package->m_pData[7] << 8 ) | Package->m_pData[8];
  2340. tParams.nDelta = ( Package->m_pData[9] << 8 ) | Package->m_pData[10];
  2341. tParams.nWidth = ( Package->m_pData[11] << 8 ) | Package->m_pData[12];
  2342. tParams.nFlip = ( Package->m_pData[13] << 8 ) | Package->m_pData[14];
  2343. tParams.nSun = ( Package->m_pData[15] << 8 ) | Package->m_pData[16];
  2344. tParams.bSST = Package->m_pData[17];
  2345. if( Package->m_pData[SUB_CMD_IDX] != 0x80 ) bMaster = false;
  2346. #ifdef DEBUG
  2347. if( bMaster ) LOG("CMD MASTER PARAMETERS"); else LOG("CMD SLAVE PARAMETERS");
  2348. sprintf(tBuff, " SST:\t%d", tParams.bSST );
  2349. LOG(tBuff);
  2350. sprintf(tBuff, " DELTA:\t%d", tParams.nDelta );
  2351. LOG(tBuff);
  2352. sprintf(tBuff, " Flip:\t%d", tParams.nFlip );
  2353. LOG(tBuff);
  2354. sprintf(tBuff, " OFFSET:\t%d", tParams.nOffset );
  2355. LOG(tBuff);
  2356. sprintf(tBuff, " ORDER:\t%d", tParams.nOrder );
  2357. LOG(tBuff);
  2358. sprintf(tBuff, " SUN:\t%d", tParams.nSun );
  2359. LOG(tBuff);
  2360. sprintf(tBuff, " WIDTH:\t%d", tParams.nWidth );
  2361. LOG(tBuff);
  2362. #endif
  2363. if( m_sLRF_DATA.force_settings ){
  2364. #ifdef DEBUG_EXTRA
  2365. LOG("Force Settings!");
  2366. #endif
  2367. // Check if Settings Are Equal
  2368. PLGS_2D_PARAMETERS chDATA = m_pDBC->getLRFDetectionProfile( m_sLRF_DATA.id, m_nActiveDetectionProfile, bMaster );
  2369. if ( chDATA == NULL ) {
  2370. LOG("No DB Profile!");
  2371. // No Profile in DB -> Set Once!
  2372. m_pDBC->updateLRFDetectionProfile( m_sNodeData.aux_id, m_nActiveDetectionProfile, bMaster, tParams.nOrder, tParams.nOffset, tParams.nDelta, tParams.nWidth, tParams.nFlip, tParams.nSun, tParams.bSST );
  2373. if( bMaster && m_sNodeData.type == ARGUS_ALS_SENSOR ){
  2374. LOG("Go Get Slave!");
  2375. //setStage( LRF_WS_STAGE_2 );
  2376. } else {
  2377. LOG("Go Get HZ!");
  2378. //setStage( LRF_WS_STAGE_3 );
  2379. };
  2380. } else {
  2381. LOG("Have Settings Go Idle Mode??!");
  2382. //setWM( LRF_WM_IDLE );
  2383. LOG("Go Get HZ!");
  2384. //setStage( LRF_WS_STAGE_3 );
  2385. free( chDATA );
  2386. };
  2387. } else {
  2388. m_pDBC->updateLRFDetectionProfile( m_sNodeData.aux_id, m_nActiveDetectionProfile, bMaster, tParams.nOrder, tParams.nOffset, tParams.nDelta, tParams.nWidth, tParams.nFlip, tParams.nSun, tParams.bSST );
  2389. if( bMaster && m_sNodeData.type == ARGUS_ALS_SENSOR ){
  2390. LOG("Go Get Slave!");
  2391. //setStage( LRF_WS_STAGE_2 );
  2392. } else {
  2393. LOG("Go Get HZ!");
  2394. //setStage( LRF_WS_STAGE_3 );
  2395. };
  2396. };
  2397. };
  2398. transactionSetResult( TRANSACTION_RESULT_OK );
  2399. break;
  2400. };
  2401. case CMD_GET_RELAY_PARAMETERS:{
  2402. LOG("R-Params!");
  2403. transactionSetResult( TRANSACTION_RESULT_OK );
  2404. break;
  2405. };
  2406. case CMD_GET_SERIAL_NUMBER:{
  2407. uint8_t tSize = Package->m_pData[SUB_CMD_IDX + 1];
  2408. memcpy( m_tSN, Package->m_pData + SUB_CMD_IDX + 2, tSize );
  2409. m_tSN[tSize] = (char)0;
  2410. #ifdef DEBUG_EXTRA
  2411. LOG("CMD_GET_SERIAL_NUMBER");
  2412. LOG( m_tSN );
  2413. #endif
  2414. //Checking SN of actual connected device with one stored in database
  2415. //in case there is new device -> the SN in database will be updated
  2416. if( strncmp( m_sLRF_DATA.serial, m_tSN, tSize ) == 0 ) {
  2417. // Equal
  2418. #ifdef DEBUG_EXTRA
  2419. LOG("SN is OK");
  2420. #endif
  2421. m_bSerialStringValid = true;
  2422. } else {
  2423. LOG_ERROR( m_sLRF_DATA.serial );
  2424. LOG_ERROR("SN is INVALID!");
  2425. LOG_ERROR( m_tSN );
  2426. m_bSerialStringValid = false;
  2427. };
  2428. transactionSetResult( TRANSACTION_RESULT_OK );
  2429. break;
  2430. };
  2431. case CMD_GET_SOFTWARE_VERSION:{
  2432. if ( bCWM == LRF_WM_HANDSHAKE ) {
  2433. uint8_t tSize = Package->m_pData[SUB_CMD_IDX];
  2434. memcpy( m_tSV, Package->m_pData + SUB_CMD_IDX + 1, tSize );
  2435. m_tSV[tSize] = (char)0;
  2436. #ifdef DEBUG_EXTRA
  2437. LOG("CMD_GET_SOFTWARE_VERSION");
  2438. LOG( m_tSV );
  2439. #endif
  2440. if ( strstr(m_tSV,"FD") != NULL ) {
  2441. #ifdef DEBUG_EXTRA
  2442. LOG("Freeze Disabled!");
  2443. #endif
  2444. m_pDBC->updateLRFFrozenStatus( m_sLRF_DATA.id, false, false );
  2445. } else {
  2446. if ( strstr(m_tSV,"FF") != NULL ) {
  2447. #ifdef DEBUG_EXTRA
  2448. LOG("TH is Frozen!");
  2449. #endif
  2450. m_pDBC->updateLRFFrozenStatus( m_sLRF_DATA.id, true, true );
  2451. } else {
  2452. if ( strstr(m_tSV,"FS") != NULL ) {
  2453. #ifdef DEBUG_EXTRA
  2454. LOG("TH is Study!");
  2455. #endif
  2456. m_pDBC->updateLRFFrozenStatus( m_sLRF_DATA.id, true, true );
  2457. } else {
  2458. #ifdef DEBUG_EXTRA
  2459. LOG("No Frost Version!");
  2460. #endif
  2461. m_pDBC->updateLRFFrozenStatus( m_sLRF_DATA.id, false, false );
  2462. };
  2463. };
  2464. };
  2465. if (strncmp(m_sLRF_DATA.version, m_tSV, tSize) == 0) {
  2466. #ifdef DEBUG_EXTRA
  2467. LOG("Version is OK");
  2468. #endif
  2469. m_bVersionStringValid = true;
  2470. } else {
  2471. LOG_ERROR( m_sLRF_DATA.version );
  2472. LOG_ERROR("Version is INVALID!");
  2473. LOG_ERROR( m_tSV );
  2474. m_bVersionStringValid = false;
  2475. };
  2476. if( m_bVersionStringValid && m_bSerialStringValid ) {
  2477. // The Device is OK
  2478. m_pDBC->setNodeOnline( m_sNodeData.node_id, NODE_STATUS_WORK );
  2479. if ( m_bForceSync ) {
  2480. m_bDeviceIsSynced = false; // Do we need it here ?
  2481. //setWM( LRF_WM_SYNC_UP );
  2482. } else {
  2483. m_bDeviceIsSynced = true;
  2484. //setStage( LRF_WS_STAGE_3 ); // Read Mask Statuses
  2485. };
  2486. } else {
  2487. // Will Work even if SN different
  2488. m_bDeviceIsSynced = false;
  2489. //m_bVersionStringValid = true;
  2490. //m_bSerialStringValid = true;
  2491. //m_pDBC->updateLRFSerial( m_sLRF_DATA.id, m_tSN );
  2492. //m_pDBC->updateLRFVerison( m_sLRF_DATA.id, m_tSV );
  2493. m_pDBC->setNodeOnline( m_sNodeData.node_id, NODE_STATUS_WORK );
  2494. //proceed to next stage
  2495. //setWM( LRF_WM_SYNC_UP );
  2496. };
  2497. m_bVersionStringValid = true;
  2498. m_bSerialStringValid = true;
  2499. m_pDBC->updateLRFSerial( m_sLRF_DATA.id, m_tSN );
  2500. m_pDBC->updateLRFVerison( m_sLRF_DATA.id, m_tSV );
  2501. transactionSetResult( TRANSACTION_RESULT_OK );
  2502. }
  2503. break;
  2504. };
  2505. case CMD_UNSUPPORTED:{
  2506. #ifdef DEBUG_EXTRA
  2507. LOG("CMD_UNSUPPORTED!!");
  2508. #endif
  2509. transactionSetResult( TRANSACTION_RESULT_OK );
  2510. break;
  2511. };
  2512. case CMD_FULL_RESET:{
  2513. #ifdef DEBUG_EXTRA
  2514. LOG("Full Reset ACK");
  2515. #endif
  2516. transactionSetResult( TRANSACTION_RESULT_OK );
  2517. break;
  2518. };
  2519. case CMD_STATE_2:{
  2520. // Throw <<Tail>> Package on Wrong Mode
  2521. if ( !( bCWM == LRF_WM_ALARM || bCWM == LRF_WM_IDLE ) ) break;
  2522. #ifdef DEBUG_EXTRA
  2523. //LOG("CMD State 2");
  2524. #endif
  2525. //Parsning sub commagds
  2526. uint8_t cIDX = SUB_CMD_IDX;
  2527. #ifdef DEBUG_EXTRA
  2528. //if( Package->m_nSize > 8 ) {
  2529. // sprintf( tBuff, "Pack Size: %d", Package->m_nSize );
  2530. // LOG( tBuff );
  2531. // PRINTOUT;
  2532. //};
  2533. #endif
  2534. m_bDeviceIsSynced = true; // At this point all data structs must be synced?!
  2535. //setStage( LRF_WS_STAGE_2 );
  2536. while( Package->m_nSize > ( cIDX + 2 ) ) {
  2537. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  2538. switch( Package->m_pData[cIDX+1] ) {
  2539. case 0x00:{
  2540. #ifdef DEBUG_EXTRA
  2541. LOG("SUB PACKAGE - Intruder detected");
  2542. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2543. LOG( tBuff );
  2544. #endif
  2545. uint16_t AZ, RANGE;
  2546. uint32_t WIDTH;
  2547. uint8_t LRFID = 0;
  2548. AZ = (( Package->m_pData[cIDX + 3] >> 3 ) << 8 ) | Package->m_pData[cIDX + 4];
  2549. RANGE = (( Package->m_pData[cIDX + 3] & 0x07 ) << 8 ) | Package->m_pData[cIDX + 5];
  2550. WIDTH = 8888;
  2551. #ifdef DEBUG_EXTRA
  2552. sprintf( tBuff, "AZ: %d RANGE: %d WIDTH: %d", AZ, RANGE, WIDTH );
  2553. LOG( tBuff );
  2554. #endif
  2555. EVENT_CONTAINER * bEVT = NULL;
  2556. TARGET_DATA * tData = prepareTargetData( AZ, RANGE, WIDTH, LRFID );
  2557. if( m_pTargetFilter != NULL ) {
  2558. if( m_pTargetFilter->validateTarget( m_sNodeData.node_id, tData ) ){
  2559. free( tData );
  2560. //Proceed Statuss of target
  2561. if ( LRF_WM_ALARM != getWM()) {
  2562. setWM(LRF_WM_ALARM);
  2563. };
  2564. cIDX += 6;
  2565. break;
  2566. };
  2567. };
  2568. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_TARGET_2D_WHITE, NULL, NULL, tData );
  2569. if( bEVT != NULL ) {
  2570. m_pEventHeap->broadcast(bEVT);
  2571. } else {
  2572. free( tData );
  2573. };
  2574. if ( LRF_WM_ALARM != getWM()) {
  2575. setWM(LRF_WM_ALARM);
  2576. };
  2577. cIDX += 6;
  2578. break;
  2579. };
  2580. case 0x02:{
  2581. #ifdef DEBUG_EXTRA
  2582. LOG("SUB PACKAGE - Maintenance notification");
  2583. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2584. LOG( tBuff );
  2585. #endif
  2586. EVENT_CONTAINER *bEVT = NULL;
  2587. if ( ( Package->m_pData[cIDX+2] & 0x02 ) == 0x02 ) {
  2588. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_ERR_LRF_NO_IRQ, NULL, NULL, NULL );
  2589. } else if ( ( Package->m_pData[cIDX+2] & 0x02 ) == 0x04 ) {
  2590. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_ERR_LRF_NO_ZERO, NULL, NULL, NULL );
  2591. } else if ( ( Package->m_pData[cIDX+2] & 0x02 ) == 0x10 ) {
  2592. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_ERR_LRF_VOLTAGE, NULL, NULL, NULL );
  2593. };
  2594. if( bEVT != NULL ) {
  2595. m_pEventHeap->broadcast(bEVT);
  2596. };
  2597. cIDX += 3;
  2598. break;
  2599. };
  2600. case 0x03:{
  2601. #ifdef DEBUG_EXTRA
  2602. LOG("SUB PACKAGE - LRF problems notification");
  2603. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2604. LOG( tBuff );
  2605. #endif
  2606. EVENT_CONTAINER *bEVT = NULL;
  2607. if ( ( Package->m_pData[cIDX+3] & 0x01 ) == 0x01 ) {
  2608. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_ERR_LRF_BROKEN, NULL, NULL, NULL);
  2609. } else if( ( Package->m_pData[cIDX+3] & 0x02 ) == 0x02 ){
  2610. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_ERR_OVERFLOW, NULL, NULL, NULL);
  2611. } else if( ( Package->m_pData[cIDX+3] & 0x04 ) == 0x04 ){
  2612. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_SST_OVERFLOW, NULL, NULL, NULL);
  2613. };
  2614. if( bEVT != NULL ) {
  2615. m_pEventHeap->broadcast(bEVT);
  2616. };
  2617. cIDX += 4;
  2618. break;
  2619. };
  2620. case 0x04:{
  2621. #ifdef DEBUG_EXTRA
  2622. LOG("SUB PACKAGE - Error Angle notification");
  2623. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2624. LOG( tBuff );
  2625. #endif
  2626. EVENT_CONTAINER *bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_ERR_LRF_ANGLE, NULL, NULL, NULL );
  2627. if( bEVT != NULL ) {
  2628. m_pEventHeap->broadcast(bEVT);
  2629. };
  2630. cIDX += 4;
  2631. break;
  2632. };
  2633. case 0x05:{
  2634. #ifdef DEBUG_EXTRA
  2635. LOG("SUB PACKAGE - Flip Complete notification");
  2636. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2637. LOG( tBuff );
  2638. #endif
  2639. // Evt Broadcasting
  2640. broadcastFlip( 0 );
  2641. if( m_sLRF_DATA.data_status != ARGUS_DS_READY ) updateDataStatus( ARGUS_DS_READY );
  2642. if( bCWM == LRF_WM_ALARM ){
  2643. // Clear Alarm Mode to Idle
  2644. bCWM = LRF_WM_IDLE;
  2645. setWM( LRF_WM_IDLE );
  2646. };
  2647. if( m_bInitialFlips ){
  2648. m_bInitialFlips--;
  2649. } else {
  2650. #ifdef DEBUG_EXTRA
  2651. LOG("INITIAL FLIPS ARE DONE!");
  2652. #endif
  2653. if( bCWM == LRF_WM_IDLE ){
  2654. // If not in Allert Mode
  2655. if ( m_sLRF_DATA.auto_draw > 0 ) {
  2656. #ifdef DEBUG_EXTRA
  2657. LOG("Start AutoDraw!");
  2658. #endif
  2659. m_bInitialFlips = m_sLRF_DATA.auto_draw;
  2660. setWM( LRF_WM_BORDER );
  2661. } else {
  2662. #ifdef DEBUG_EXTRA
  2663. LOG("No AutoDraw Defined!");
  2664. #endif
  2665. };
  2666. #ifdef DEBUG_EXTRA
  2667. } else {
  2668. LOG_ERROR("Wrong WM Mode! (possible error in init process)");
  2669. #endif
  2670. };
  2671. };
  2672. cIDX += 5;
  2673. break;
  2674. };
  2675. case 0x06:{
  2676. #ifdef DEBUG_EXTRA
  2677. LOG("SUB PACKAGE - System visibility changes");
  2678. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2679. LOG( tBuff );
  2680. #endif
  2681. EVENT_CONTAINER *bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_LRF_VIS_CHANGED, NULL, NULL, NULL );
  2682. if( bEVT != NULL ) {
  2683. m_pEventHeap->broadcast(bEVT);
  2684. };
  2685. cIDX += 4;
  2686. break;
  2687. };
  2688. case 0x07:{
  2689. #ifdef DEBUG_EXTRA
  2690. LOG("SUB PACKAGE - Intruder with Width detected");
  2691. sprintf( tBuff, "Sub Size: %d", Package->m_pData[cIDX] );
  2692. LOG( tBuff );
  2693. #endif
  2694. uint16_t AZ, RANGE;
  2695. uint32_t WIDTH;
  2696. AZ = (( Package->m_pData[cIDX + 3] >> 3 ) << 8 ) | Package->m_pData[cIDX + 4];
  2697. RANGE = (( Package->m_pData[cIDX + 3] & 0x07 ) << 8 ) | Package->m_pData[cIDX + 5];
  2698. WIDTH = ( Package->m_pData[cIDX + 6] << 24 ) | ( Package->m_pData[cIDX + 7] << 16 ) | ( Package->m_pData[cIDX + 8] << 8 ) | Package->m_pData[cIDX + 9];
  2699. #ifdef DEBUG_EXTRA
  2700. sprintf( tBuff, "AZ: %d RANGE: %d WIDTH: %d", AZ, RANGE, WIDTH );
  2701. LOG( tBuff );
  2702. #endif
  2703. EVENT_CONTAINER * bEVT = NULL;
  2704. TARGET_DATA * tData = prepareTargetData( AZ, RANGE, WIDTH );
  2705. if( m_pTargetFilter != NULL ) {
  2706. if( m_pTargetFilter->validateTarget( m_sNodeData.node_id, tData ) ){
  2707. free( tData );
  2708. if( m_sLRF_DATA.data_status!= ARGUS_DS_ALARMED ) updateDataStatus( ARGUS_DS_ALARMED );
  2709. //Proceed Statuses Of Target
  2710. //if ( !m_bSessionStarted ) startSession();
  2711. //m_nSleepTime = LGS_ALERT_SLEEP_TIME;
  2712. if ( LRF_WM_ALARM != getWM()) {
  2713. setWM(LRF_WM_ALARM);
  2714. };
  2715. cIDX += 10;
  2716. break;
  2717. };
  2718. };
  2719. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_TARGET_2D_WHITE, NULL, NULL, tData );
  2720. if( bEVT != NULL ) {
  2721. m_pEventHeap->broadcast( bEVT );
  2722. } else {
  2723. free( tData );
  2724. };
  2725. if( m_sLRF_DATA.data_status!= ARGUS_DS_ALARMED ) updateDataStatus( ARGUS_DS_ALARMED );
  2726. //m_nSleepTime = LGS_ALERT_SLEEP_TIME;
  2727. if ( LRF_WM_ALARM != getWM()) {
  2728. setWM(LRF_WM_ALARM);
  2729. };
  2730. cIDX += 10;
  2731. break;
  2732. };
  2733. default:{
  2734. #ifdef DEBUG_EXTRA
  2735. LOG("SUB PACKAGE - Unhandled One!");
  2736. sprintf( tBuff, "Code: %d Sub Size: %d", Package->m_pData[cIDX+1], Package->m_pData[cIDX] );
  2737. LOG( tBuff );
  2738. #endif
  2739. cIDX += Package->m_pData[cIDX] + 3;
  2740. break;
  2741. };
  2742. };
  2743. };
  2744. //setStage( LRF_WS_STAGE_1 );
  2745. transactionSetResult( TRANSACTION_RESULT_OK );
  2746. break;
  2747. };
  2748. case CMD_GET_HZ_DATA:{
  2749. //HZ_PACKET tPack = (PHZ_PACKET) Package->m_pData + SUB_CMD_IDX;
  2750. switch( Package->m_pData[DATA_BEGIN_IDX] ) {
  2751. case HZIT_EVT_GET_HEADER:{
  2752. HZ_EVT_GET_HEADER pC;
  2753. pC.bSubSize = Package->m_pData[SUB_CMD_IDX];
  2754. pC.bType = Package->m_pData[DATA_BEGIN_IDX];
  2755. pC.bSequence = Package->m_pData[DATA_BEGIN_IDX+1];
  2756. pC.wPolygonCount = Package->m_pData[DATA_BEGIN_IDX+3];
  2757. pC.wPolygonCount = ( pC.wPolygonCount << 8 ) | Package->m_pData[DATA_BEGIN_IDX+2];
  2758. pC.wRayCount = Package->m_pData[DATA_BEGIN_IDX+5];
  2759. pC.wRayCount = ( pC.wRayCount << 8 ) | Package->m_pData[DATA_BEGIN_IDX+4];
  2760. pC.dwTypeOn = Package->m_pData[DATA_BEGIN_IDX+9];
  2761. pC.dwTypeOn = ( pC.dwTypeOn << 8 ) | Package->m_pData[DATA_BEGIN_IDX+8];
  2762. pC.dwTypeOn = ( pC.dwTypeOn << 8 ) | Package->m_pData[DATA_BEGIN_IDX+7];
  2763. pC.dwTypeOn = ( pC.dwTypeOn << 8 ) | Package->m_pData[DATA_BEGIN_IDX+6];
  2764. #ifdef DEBUG_EXTRA
  2765. sprintf( tBuff, "Poly Cnt: %d Ray Cnt: %d Status: 0x%08X", pC.wPolygonCount, pC.wRayCount, pC.dwTypeOn );
  2766. LOG( tBuff );
  2767. #endif
  2768. m_sShadowHelper.m_bSequence = Package->m_pData[DATA_BEGIN_IDX+1];
  2769. cleanShadowStructs();
  2770. if( pC.wPolygonCount ){
  2771. m_sShadow.m_nEnabled = pC.dwTypeOn;
  2772. // Set Statuses of Masks ?? Now ??? ->
  2773. for( uint8_t MaskLayer=0; MaskLayer < 5; MaskLayer++ ) {
  2774. uint32_t tMaskID = m_pDBC->getLRFMaskID( m_sLRF_DATA.id , 0, MaskLayer );
  2775. if ( tMaskID ) {
  2776. m_pDBC->setLRFMaskStatus( tMaskID, ( m_sShadow.m_nEnabled & (1<<MaskLayer) ) );
  2777. };
  2778. };
  2779. m_sShadow.pPolygones = (SHADOW_POLY *) calloc(pC.wPolygonCount,sizeof(SHADOW_POLY));
  2780. if ( m_sShadow.pPolygones ) {
  2781. memset( m_sShadow.pPolygones, 0, pC.wPolygonCount*sizeof(SHADOW_POLY) );
  2782. m_sShadow.m_nPolygonCount = pC.wPolygonCount;
  2783. //setStage( LRF_WS_STAGE_4 );
  2784. //setWM(ANODE_WM_2D_GET_SHADOW_POLY_INFO);
  2785. } else {
  2786. LOG_ERROR("Error Alocation Memory foe Shadow!");
  2787. //setWM( LRF_WM_IDLE );
  2788. //updateDataStatus( ARGUS_DS_STUDY );
  2789. };
  2790. } else cleanShadowInDB();
  2791. transactionSetResult( TRANSACTION_RESULT_OK );
  2792. break;
  2793. };
  2794. case HZIT_EVT_GET_POLYGON:{
  2795. uint16_t wPointCount = Package->m_pData[DATA_BEGIN_IDX+4];
  2796. wPointCount = ( wPointCount << 8 ) | Package->m_pData[DATA_BEGIN_IDX+3];
  2797. m_sShadowHelper.m_bSequence = Package->m_pData[DATA_BEGIN_IDX+1];
  2798. #ifdef DEBUG_EXTRA
  2799. sprintf( tBuff, "Poly[%d] Info: Points %d type: %d",
  2800. m_sShadowHelper.m_nCurrentPolygon,
  2801. wPointCount, Package->m_pData[DATA_BEGIN_IDX+2]
  2802. );
  2803. LOG( tBuff );
  2804. #endif
  2805. if ( wPointCount ) {
  2806. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].bType = Package->m_pData[DATA_BEGIN_IDX+2];
  2807. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint = (HZ_POINT*)calloc( wPointCount, sizeof(HZ_POINT));
  2808. if ( m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint ) {
  2809. memset(m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint, 0, wPointCount*sizeof(HZ_POINT));
  2810. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount = wPointCount;
  2811. m_sShadowHelper.m_nCurrentPoint = 0;
  2812. //setWM( ANODE_WM_2D_GET_SHADOW_POLY_POINT );
  2813. } else LOG_ERROR("Error Alloc Memory for Polygone points!");
  2814. //setWM( LRF_WM_IDLE ); cant quit now - mat be need read DC
  2815. //};
  2816. //Start Point Transfers or Skeep to Relay Settings
  2817. transactionSetResult( TRANSACTION_RESULT_OK );
  2818. } else {
  2819. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint = NULL;
  2820. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount = 0;
  2821. if( m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ) {
  2822. m_sShadowHelper.m_nCurrentPolygon++;
  2823. // Request next polygone
  2824. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2825. } else {
  2826. // Proceed Read of the points (no points -> Skeep )
  2827. transactionSetResult( TRANSACTION_RESULT_OK );
  2828. };
  2829. };
  2830. break;
  2831. };
  2832. case HZIT_EVT_GET_POLYGON_POINT:{
  2833. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wX = Package->m_pData[DATA_BEGIN_IDX+3];
  2834. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wX = (m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wX << 8 ) | Package->m_pData[DATA_BEGIN_IDX+2];
  2835. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wY = Package->m_pData[DATA_BEGIN_IDX+5];
  2836. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wY = (m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wY << 8 ) | Package->m_pData[DATA_BEGIN_IDX+4];
  2837. #ifdef DEBUG_EXTRA
  2838. sprintf( tBuff, "PolyPoint[%d] of %d X: %d Y: %d",
  2839. m_sShadowHelper.m_nCurrentPoint,
  2840. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount,
  2841. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wX,
  2842. m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].pPoint[m_sShadowHelper.m_nCurrentPoint].wY
  2843. );
  2844. LOG( tBuff );
  2845. #endif
  2846. m_sShadowHelper.m_nCurrentPoint++;
  2847. m_sShadowHelper.m_bSequence = Package->m_pData[DATA_BEGIN_IDX+1];
  2848. if( m_sShadowHelper.m_nCurrentPoint < m_sShadow.pPolygones[m_sShadowHelper.m_nCurrentPolygon].wCount ) {
  2849. //LOG("Back To Read Nex Poly Point!");
  2850. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2851. //setStage( LRF_WS_STAGE_5 );
  2852. //setWM( ANODE_WM_2D_GET_SHADOW_POLY_POINT );
  2853. } else {
  2854. #ifdef DEBUG_EXTRA
  2855. LOG("Done Load Poly Points");
  2856. #endif
  2857. transactionSetResult( TRANSACTION_RESULT_OK );
  2858. m_sShadowHelper.m_nCurrentPolygon++;
  2859. if(m_sShadowHelper.m_nCurrentPolygon < m_sShadow.m_nPolygonCount ) {
  2860. //setStage( LRF_WS_STAGE_4 );
  2861. //setWM( ANODE_WM_2D_GET_SHADOW_POLY_INFO );
  2862. transactionSetResult( TRANSACTION_RESULT_OK );
  2863. } else {
  2864. saveShadowToDB();
  2865. #ifdef DEBUG_EXTRA
  2866. //rasterizeShadow();
  2867. //makeShadowPic("shadow.png");
  2868. #endif
  2869. setWM( LRF_WM_IDLE );
  2870. updateDataStatus( ARGUS_DS_READY );
  2871. };
  2872. };
  2873. break;
  2874. };
  2875. case HZIT_EVT_GET_RAY:{
  2876. HZ_EVT_GET_RAY pC;
  2877. LOG("RAY!");
  2878. setWM( LRF_WM_IDLE );
  2879. break;
  2880. };
  2881. case HZIT_EVT_GET_RAY_LINE:{
  2882. HZ_EVT_GET_RAY_LINE pC;
  2883. LOG("RAY LINE!");
  2884. setWM( LRF_WM_IDLE );
  2885. break;
  2886. };
  2887. case HZIT_EVT_ERROR:{
  2888. HZ_EVT_ERROR pC;
  2889. LOG("ERROR GET SHADOW PING_PONG!");
  2890. setWM( LRF_WM_IDLE );
  2891. break;
  2892. };
  2893. };
  2894. break;
  2895. };
  2896. case CMD_SET_HZ_DATA:{
  2897. #ifdef DEBUG_EXTRA
  2898. sprintf( tBuff,"Replay Set HZ Code: 0x%02X", Package->m_pData[DATA_BEGIN_IDX] );
  2899. LOG( tBuff );
  2900. #endif
  2901. m_sShadowHelper.m_bSequence = Package->m_pData[DATA_BEGIN_IDX+1];
  2902. switch( Package->m_pData[DATA_BEGIN_IDX] ) {
  2903. case HZIT_EVT_SET_HEADER:
  2904. #ifdef DEBUG_EXTRA
  2905. LOG("Header OK");
  2906. #endif
  2907. transactionSetResult( TRANSACTION_RESULT_OK );
  2908. break;
  2909. case HZIT_EVT_SET_POLYGON:
  2910. #ifdef DEBUG_EXTRA
  2911. LOG("Poligon Added OK");
  2912. #endif
  2913. transactionSetResult( TRANSACTION_RESULT_OK );
  2914. break;
  2915. case HZIT_EVT_SET_POLYGON_POINT:
  2916. #ifdef DEBUG_EXTRA
  2917. LOG("PolyPoint Added OK");
  2918. #endif
  2919. m_sShadowHelper.m_nCurrentPoint++;
  2920. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2921. break;
  2922. case HZIT_EVT_SET_RAY:
  2923. m_sShadowHelper.m_nCurrentRay = 0;
  2924. #ifdef DEBUG_EXTRA
  2925. LOG("Rey Added OK");
  2926. #endif
  2927. transactionSetResult( TRANSACTION_RESULT_OK );
  2928. break;
  2929. case HZIT_EVT_SET_RAY_LINE:
  2930. #ifdef DEBUG_EXTRA
  2931. LOG("Ray Line Added OK");
  2932. #endif
  2933. m_sShadowHelper.m_nCurrentRay++;
  2934. transactionSetResult( TRANSACTION_RESULT_REPEAT );
  2935. break;
  2936. case HZIT_EVT_SET_DONE:
  2937. #ifdef DEBUG_EXTRA
  2938. LOG("Cleanup!");
  2939. #endif
  2940. memset( &m_sShadowHelper, 0, sizeof(SHADOW_IO_HELPER) );
  2941. m_bOpRetryCnt = 128;
  2942. updateDataStatus( ARGUS_DS_READY );
  2943. transactionSetResult( TRANSACTION_RESULT_OK );
  2944. break;
  2945. case HZIT_EVT_ERROR:{
  2946. HZ_EVT_ERROR pC;
  2947. #ifdef DEBUG_EXTRA
  2948. LOG_ERROR("ERROR SET SHADOW PING_PONG!");
  2949. #endif
  2950. //setWM( ANODE_WM_2D_PING );
  2951. memset( &m_sShadowHelper, 0, sizeof(SHADOW_IO_HELPER) );
  2952. //setStage( LRF_WS_STAGE_7 );
  2953. updateDataStatus( ARGUS_DS_READY );
  2954. transactionSetResult( TRANSACTION_RESULT_OK );
  2955. break;
  2956. };
  2957. };
  2958. break;
  2959. };
  2960. case CMD_SET_HZ_ENABLE:{
  2961. transactionSetResult( TRANSACTION_RESULT_OK );
  2962. break;
  2963. };
  2964. case CMD_GET_ACTIVE_PROFILE:{
  2965. //if ( ePSTG == LRF_WS_INIT ) {
  2966. //m_bMultiProfileSupported = true;
  2967. m_nActiveDetectionProfile = 0;
  2968. //setStage( LRF_WS_STAGE_1 );
  2969. //};
  2970. transactionSetResult( TRANSACTION_RESULT_OK );
  2971. break;
  2972. };
  2973. };
  2974. return true;
  2975. #ifdef DEBUG_EXTRA
  2976. } else {
  2977. LOG("Not Mine Package!");
  2978. #endif
  2979. };
  2980. return false;
  2981. };
  2982. #undef PRINTOUT
  2983. /*
  2984. bool device2DSensor::downloadFile( CSTR fname ){
  2985. if( fname == NULL ) return false;
  2986. uint8_t len = strlen( fname );
  2987. if( len == 0 ) return false;
  2988. if( m_bSupportedFS ) {
  2989. m_sOutFSData.nCommand = FS_CMD_SEND_CMD;
  2990. m_sOutFSData.sendcmd.nOrder = 0;
  2991. m_sOutFSData.sendcmd.nLast = 1;
  2992. m_sOutFSData.sendcmd.nPosition = 0;
  2993. m_sOutFSData.sendcmd.nSubSize = len + 4; // Size of FS_PACK
  2994. //Header
  2995. m_sOutFSData.sendcmd.pSubBuffer[0] = LG_FS_OP_OPEN_FILE;
  2996. m_sOutFSData.sendcmd.pSubBuffer[1] = len + 1; //Size of FS_PATH
  2997. m_sOutFSData.sendcmd.pSubBuffer[2] = 0x00;
  2998. // FS_PATH Begins here
  2999. m_sOutFSData.sendcmd.pSubBuffer[3] = len;
  3000. memcpy( &m_sOutFSData.sendcmd.pSubBuffer[4], fname, len );
  3001. if( sendFS_package( &m_sOutFSData ) ){
  3002. return true;
  3003. };
  3004. };
  3005. return false;
  3006. };
  3007. */
  3008. TARGET_DATA *device2DSensor::prepareTargetData( uint16_t AZ, uint16_t RANGE, uint32_t WIDTH, uint8_t LRFID ){
  3009. TARGET_DATA* tVal = NULL;
  3010. systemAnchor* tSystem = systemAnchor::getInstance();
  3011. if( tSystem != NULL ) {
  3012. tVal = (TARGET_DATA*)malloc(sizeof(TARGET_DATA));
  3013. if (tVal != NULL) {
  3014. uint16_t cX, cY;
  3015. memset( tVal, 0, sizeof(TARGET_DATA) );
  3016. tVal->source_position.lat = m_oPosition.getLat();
  3017. tVal->source_position.lon = m_oPosition.getLon();
  3018. tVal->source_position.alt = m_oPosition.getAlt();
  3019. tVal->local_target.r = m_sLRF_DATA.max_range / 2048.0f * (float)RANGE;
  3020. AZ = ( AZ + m_sLRF_DATA.base_shift ) % 6000;
  3021. //tVal->local_target.fi = 360.0f - (360.0f / 6000.0f) * (float) AZ;
  3022. tVal->local_target.tetta = 90.0f;
  3023. tVal->local_target.fi = (360.0f / 6000.0f) * (float) AZ;
  3024. tVal->area = WIDTH;
  3025. tVal->lrf_id = LRFID;//0; // Always Master?
  3026. tVal->digital = RANGE;
  3027. // local - C3D_SPH_DEGREES
  3028. // system - C3D_DECART
  3029. // global - C3D_WGS84_LOC
  3030. tSystem->convertToSystem( &m_oPosition, tVal->local_target, tVal->system_target );
  3031. if ( tSystem->beLocal() ) {
  3032. tVal->global_target.lat = tVal->system_target.y;
  3033. tVal->global_target.lon = tVal->system_target.x;
  3034. tVal->global_target.alt = tVal->system_target.z;
  3035. } else {
  3036. //ToDo: Fix Convert!
  3037. tSystem->convertToGlobal(&m_oPosition, tVal->local_target, tVal->global_target);
  3038. //tSystem->convertToGlobal( &m_oPosition, tVal->system_target, tVal->global_target );
  3039. };
  3040. #ifdef DEBUG_EXTRA
  3041. char tB[128];
  3042. sprintf(tB, "tCords Local: %0.3f %0.3f %0.3f", tVal->local_target.r, tVal->local_target.fi, tVal->local_target.tetta );
  3043. LOG( tB );
  3044. sprintf(tB, "tCords System: %0.3f %0.3f %0.3f", tVal->system_target.x, tVal->system_target.y, tVal->system_target.z );
  3045. LOG( tB );
  3046. sprintf(tB, "tCords Global: %0.3f %0.3f %0.3f", tVal->global_target.lat, tVal->global_target.lon, tVal->global_target.alt );
  3047. LOG( tB );
  3048. #endif
  3049. };
  3050. };
  3051. return tVal;
  3052. };
  3053. void device2DSensor::simulateTarget( void ){
  3054. uint16_t AZ, RANGE;
  3055. uint32_t WIDTH;
  3056. AZ = uint16_t ( rand() * ( 6000.0f /(float) RAND_MAX ));
  3057. RANGE = uint16_t ( rand() * ( 2048.0f /(float) RAND_MAX ));
  3058. WIDTH = 4000;
  3059. TARGET_DATA * tData = prepareTargetData( AZ, RANGE, WIDTH );
  3060. if( tData != NULL ){
  3061. if( m_pTargetFilter != NULL ) {
  3062. if( !m_pTargetFilter->validateTarget( m_sNodeData.node_id, tData ) ){
  3063. free( tData );
  3064. return;
  3065. };
  3066. };
  3067. EVENT_CONTAINER * bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_TARGET_2D_WHITE, NULL, NULL, tData );
  3068. if (bEVT != NULL) {
  3069. m_pEventHeap->broadcast( bEVT );
  3070. };
  3071. };
  3072. };
  3073. /*
  3074. void device2DSensor::saveRawDrawData( uint16_t alngles ){
  3075. CSTR tID = getUniqID_MD5();
  3076. if( tID!= NULL ) {
  3077. char cPATH[256];
  3078. FILE *fp;
  3079. sprintf( cPATH, "data/device_draws/%s.raw", tID );
  3080. fp = fopen( cPATH, "wb");
  3081. if( fp != NULL ) {
  3082. uint32_t tVal = alngles;
  3083. fwrite( &tVal, 4, 1, fp );
  3084. fwrite( m_vDrawRanges, 4, alngles, fp );
  3085. fclose( fp );
  3086. m_pDBC->addLRFDraw( m_sNodeData.node_id, alngles, tID );
  3087. } else {
  3088. LOG_ERROR("Cannot open DRAW file!");
  3089. }
  3090. free( tID );
  3091. };
  3092. };
  3093. */
  3094. inline int DBL_CMP( const double& a, const double& b, const double quality = 1e-10 ){
  3095. if( fabs(a - b) <= fabs(quality) ) return 0;
  3096. if( a > b ) return 1;
  3097. return -1;
  3098. };
  3099. typedef struct S_VERTEX{
  3100. double x, y, r;
  3101. BOOL f;
  3102. int n;
  3103. }VERTEX, *PVERTEX;
  3104. typedef struct S_SEGMENT{
  3105. int pPoints[2];
  3106. int nIndex;
  3107. }SEGMENT, *PSEGMENT;
  3108. typedef struct S_SEGMENTS{
  3109. PVERTEX pPoints;
  3110. int nCount;
  3111. }SEGMENTS, *PSEGMENTS;
  3112. BOOL CrossProduct( PVERTEX ln0, PVERTEX ln1, PVERTEX pCross, bool fCheck0 = true, bool fCheck1 = true ){
  3113. #undef B0
  3114. double A0, A1, B0, B1, X, Y;
  3115. if( DBL_CMP(ln0[0].x, ln0[1].x) == 0 ){
  3116. A0 = DBL_MAX;
  3117. B0 = ln0[0].x;
  3118. } else {
  3119. A0 = (ln0[0].y - ln0[1].y) / (ln0[0].x - ln0[1].x);
  3120. B0 = ln0[0].y - A0 * ln0[0].x;
  3121. };
  3122. if(DBL_CMP(ln1[0].x , ln1[1].x) == 0){
  3123. A1 = DBL_MAX;
  3124. B1 = ln1[0].x;
  3125. } else {
  3126. A1 = (ln1[0].y - ln1[1].y) / (ln1[0].x - ln1[1].x);
  3127. B1 = ln1[0].y - A1 * ln1[0].x;
  3128. };
  3129. if(A0 == DBL_MAX){
  3130. if(A1 == DBL_MAX) return FALSE;
  3131. X = B0;
  3132. Y = A1 * X + B1;
  3133. } else {
  3134. if(A1 == DBL_MAX) X = B1;
  3135. else {
  3136. if(DBL_CMP( A1, A0 ) != 0) X = (B0 - B1) / (A1 - A0); else return FALSE;
  3137. };
  3138. Y = A0 * X + B0;
  3139. };
  3140. double y = Y, x = X;
  3141. if( fCheck0 ){
  3142. double x0 = ln0[0].x, x1 = ln0[1].x;
  3143. double y0 = ln0[0].y, y1 = ln0[1].y;
  3144. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1)) return FALSE;
  3145. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1)) return FALSE;
  3146. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1)) return FALSE;
  3147. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1)) return FALSE;
  3148. };
  3149. if( fCheck1 ){
  3150. double x0 = ln1[0].x, x1 = ln1[1].x;
  3151. double y0 = ln1[0].y, y1 = ln1[1].y;
  3152. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1)) return FALSE;
  3153. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1)) return FALSE;
  3154. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1)) return FALSE;
  3155. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1)) return FALSE;
  3156. };
  3157. pCross->x = X;
  3158. pCross->y = Y;
  3159. return TRUE;
  3160. };
  3161. BOOL PtInLine( PVERTEX ln, VERTEX& p ){
  3162. double A, B;
  3163. if(DBL_CMP(ln[0].x, ln[1].x) == 0){
  3164. A = DBL_MAX;
  3165. B = ln[0].x;
  3166. } else {
  3167. A = (ln[0].y - ln[1].y) / (ln[0].x - ln[1].x);
  3168. B = ln[0].y - A * ln[0].x;
  3169. };
  3170. if(A == DBL_MAX) {
  3171. if( DBL_CMP(p.x, B) == 0) return TRUE;
  3172. } else {
  3173. if(DBL_CMP(p.x * A + B, p.y) == 0) return TRUE;
  3174. };
  3175. return FALSE;
  3176. };
  3177. int comparePoints( const void *arg1, const void *arg2 ){
  3178. if(((PVERTEX)arg1)->r > ((PVERTEX)arg2)->r) return +1;
  3179. if(((PVERTEX)arg1)->r < ((PVERTEX)arg2)->r) return -1;
  3180. return 0;
  3181. };
  3182. void AddPointToSegment( PSEGMENTS pSegments, int nIndex, PVERTEX pPoint){
  3183. PVERTEX pNew = new VERTEX[pSegments[nIndex].nCount + 1];
  3184. memcpy( pNew, pSegments[nIndex].pPoints, sizeof(VERTEX) * pSegments[nIndex].nCount );
  3185. memcpy( pNew + pSegments[nIndex].nCount, pPoint, sizeof(VERTEX));
  3186. if(pSegments[nIndex].pPoints) delete pSegments[nIndex].pPoints;
  3187. pSegments[nIndex].pPoints = pNew;
  3188. pSegments[nIndex].nCount++;
  3189. };
  3190. void StickSegments( PSEGMENTS pSegments, int nIndex ){
  3191. if( pSegments[nIndex].nCount ){
  3192. qsort( pSegments[nIndex].pPoints, pSegments[nIndex].nCount, sizeof(VERTEX), comparePoints );
  3193. PVERTEX pTemp = new VERTEX[pSegments[nIndex].nCount];
  3194. PVERTEX pPoints = pSegments[nIndex].pPoints;
  3195. int f, nT = 0;
  3196. pTemp[nT++] = pPoints[0];
  3197. f = pPoints[0].f;
  3198. for(int i = 1; i < pSegments[nIndex].nCount;){
  3199. f += pPoints[i].f;
  3200. if(f == 0){
  3201. pTemp[nT++] = pPoints[i++];
  3202. if(i < pSegments[nIndex].nCount){
  3203. pTemp[nT++] = pPoints[i];
  3204. f = pPoints[i++].f;
  3205. };
  3206. } else i++;
  3207. };
  3208. if(pSegments[nIndex].pPoints) delete[] pSegments[nIndex].pPoints;
  3209. pSegments[nIndex].pPoints = new VERTEX[nT];
  3210. memcpy( pSegments[nIndex].pPoints, pTemp, sizeof(VERTEX) * nT );
  3211. pSegments[nIndex].nCount = nT;
  3212. if(pTemp) delete[] pTemp;
  3213. };
  3214. };
  3215. int compareSegments(const void *arg1, const void *arg2){
  3216. if(((PSEGMENT)arg1)->nIndex > ((PSEGMENT)arg2)->nIndex) return +1;
  3217. if(((PSEGMENT)arg1)->nIndex < ((PSEGMENT)arg2)->nIndex) return -1;
  3218. if(((PSEGMENT)arg1)->pPoints[0] > ((PSEGMENT)arg2)->pPoints[0]) return +1;
  3219. if(((PSEGMENT)arg1)->pPoints[0] < ((PSEGMENT)arg2)->pPoints[0]) return -1;
  3220. return 0;
  3221. };
  3222. bool pnpoly( uint16_t nvert, float *vertx, float *verty, float testx, float testy ){
  3223. bool c = false;
  3224. for( uint16_t i = 0, j = nvert-1; i < nvert; j = i++ ) if ( ((verty[i]>testy) != (verty[j]>testy)) && (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) ) c = !c;
  3225. return c;
  3226. };
  3227. bool device2DSensor::PtInPolygons( TEST_POINT& point, uint8_t F ){
  3228. for( uint16_t i = 0; i < m_sShadow.m_nPolygonCount; i++)
  3229. if( F == m_sShadow.pPolygones[i].bType )
  3230. if ( PtInPolygon( m_sShadow.pPolygones[i], point ) ) return true;
  3231. return false;
  3232. };
  3233. bool device2DSensor::PtInPolygon( SHADOW_POLY& Poly, TEST_POINT& point ){
  3234. if( Poly.wCount < 3 ) return false;
  3235. float *vertx = (float *) calloc( Poly.wCount, sizeof(float));
  3236. float *verty = (float *) calloc( Poly.wCount, sizeof(float));
  3237. for( int z =0; z < Poly.wCount; z++ ) {
  3238. vertx[z] = (float)Poly.pPoint[z].wX;
  3239. verty[z] = (float)Poly.pPoint[z].wY;
  3240. };
  3241. if ( pnpoly( Poly.wCount, vertx, verty, point.x, point.y )){
  3242. free(vertx);
  3243. free(verty);
  3244. return true;
  3245. };
  3246. free(vertx);
  3247. free(verty);
  3248. return false;
  3249. };
  3250. bool device2DSensor::rasterizeShadow(){
  3251. const double TWO_PI = 6.2831853071796;
  3252. const double ONE_PI = 3.1415926535898;
  3253. bool masked[6] = {0};
  3254. uint16_t cMin[6] = {0};
  3255. uint16_t cMax[6] = {0};
  3256. double k = TWO_PI /((double)MAX_2D_SHADOW_RAY_LINES );
  3257. double z_shift = 2.0 * k * ((double)m_sLRF_DATA.base_shift );
  3258. for( uint16_t i=0; i < MAX_2D_SHADOW_RAY_LINES; i++ ) {
  3259. double ca = TWO_PI - ( k * i + z_shift ); // must be checked sign!!
  3260. double sin_val = sin( ca );
  3261. double cos_val = cos( ca );
  3262. memset( &masked, 0, 5*sizeof( bool ) );
  3263. memset( &cMin, 0, 5*sizeof( uint16_t ) );
  3264. memset( &cMax, 0, 5*sizeof( uint16_t ) );
  3265. // 1 - to close polygones that going outside
  3266. for( uint16_t range_digital = 0; range_digital < 2047; range_digital++ ) {
  3267. TEST_POINT TP;
  3268. TP.x = (float)(cos_val*((double)range_digital));
  3269. TP.y = (float)(sin_val*((double)range_digital));
  3270. for( uint8_t z=0; z < 6; z++ ){ // Mask ID
  3271. if( masked[z] ) {
  3272. if(!PtInPolygons( TP, z )){
  3273. masked[z] = false;
  3274. cMax[z] = range_digital;
  3275. if( cMin[z] != cMax[z] ) { //Filter Null Masks
  3276. m_sShadow.ray_lines[i].rays = (SHADOW_RAY *)reallocarray(m_sShadow.ray_lines[i].rays, m_sShadow.ray_lines[i].nCount + 1, sizeof(SHADOW_RAY));
  3277. if ( m_sShadow.ray_lines[i].rays == NULL ) {
  3278. LOG_ERROR("Ray realloc failure!");
  3279. m_sShadow.ray_lines[i].nCount = 0;
  3280. return false;
  3281. };
  3282. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].nMin = cMin[z];
  3283. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].nMax = cMax[z];
  3284. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].bType = z;
  3285. m_sShadow.ray_lines[i].nCount++;
  3286. #ifdef DEBUG_EXTRA
  3287. char tB[128];
  3288. sprintf( tB ,"New Ray: Mask[%d] angle(line)[%d] min[%d] max[%d] cnt[%d]", z, i, cMin[z], cMax[z], m_sShadow.ray_lines[i].nCount );
  3289. LOG(tB);
  3290. #endif
  3291. }
  3292. };
  3293. } else {
  3294. if(PtInPolygons( TP, z )){
  3295. masked[z] = true;
  3296. cMin[z] = range_digital;
  3297. };
  3298. };
  3299. };
  3300. };
  3301. // to clode poligones that goed outside
  3302. for( uint8_t z=0; z < 5; z++ ){
  3303. if ( masked[z] ) {
  3304. masked[z] = false;
  3305. m_sShadow.ray_lines[i].rays = (SHADOW_RAY *) reallocarray( m_sShadow.ray_lines[i].rays, m_sShadow.ray_lines[i].nCount+1, sizeof(SHADOW_RAY) );
  3306. if ( m_sShadow.ray_lines[i].rays == NULL ) {
  3307. LOG_ERROR("Ray realloc failure!");
  3308. m_sShadow.ray_lines[i].nCount = 0;
  3309. return false;
  3310. };
  3311. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].nMin = cMin[z];
  3312. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].nMax = 2047;
  3313. m_sShadow.ray_lines[i].rays[m_sShadow.ray_lines[i].nCount].bType = z;
  3314. m_sShadow.ray_lines[i].nCount++;
  3315. #ifdef DEBUG_EXTRA
  3316. char tB[128];
  3317. sprintf( tB ,"Ray: Mask[%d] angle[%d] min[%d] max[%d]", z, i, cMin[z], 2047 );
  3318. LOG(tB);
  3319. #endif
  3320. };
  3321. };
  3322. };
  3323. return true;
  3324. };
  3325. bool device2DSensor::cleanShadowInDB( void ){
  3326. for( uint8_t MaskLayer=0; MaskLayer < 5; MaskLayer++ ) {
  3327. if( uint32_t tMaskID = m_pDBC->getLRFMaskID( m_sLRF_DATA.id , 0, MaskLayer ) ) m_pDBC->cleanLRFMask( tMaskID, true );
  3328. };
  3329. };
  3330. bool device2DSensor::loadShadowFromDB( void ){
  3331. };
  3332. bool device2DSensor::saveShadowToDB( void ){
  3333. for( uint8_t MaskLayer=0; MaskLayer < 5; MaskLayer++ ) {
  3334. #ifdef DEBUG_EXTRA
  3335. LOG( "Adding Layer To DATABASE");
  3336. #endif
  3337. uint32_t tMaskID = m_pDBC->getLRFMaskID( m_sLRF_DATA.id , 0, MaskLayer );
  3338. if( tMaskID ) {
  3339. #ifdef DEBUG_EXTRA
  3340. LOG("Have Old Mask -> Delete Old!");
  3341. #endif
  3342. m_pDBC->cleanLRFMask( tMaskID, true );
  3343. tMaskID = 0;
  3344. };
  3345. for( uint8_t i = 0; i < m_sShadow.m_nPolygonCount; i++ ){
  3346. if( m_sShadow.pPolygones[i].bType == MaskLayer ){
  3347. if( m_sShadow.pPolygones[i].pPoint ){
  3348. if( tMaskID == 0 ) {
  3349. #ifdef DEBUG_EXTRA
  3350. LOG("No Mask -> Create One!");
  3351. #endif
  3352. tMaskID = m_pDBC->addLRFMask( m_sLRF_DATA.id, 0, MaskLayer );
  3353. };
  3354. if( tMaskID ) {
  3355. uint32_t tPolyID = 0;
  3356. #ifdef DEBUG_EXTRA
  3357. LOG("Creating New Polygone!");
  3358. #endif
  3359. switch( MaskLayer ) {
  3360. case 0: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 255, 0, 255); break;
  3361. case 1: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 255, 255, 0); break;
  3362. case 2: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 0, 255, 255); break;
  3363. case 3: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 0, 255, 0); break;
  3364. case 4: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 0, 0, 255); break;
  3365. default: tPolyID = m_pDBC->addLRFMaskPolygone(tMaskID, 0, 0, 0 ); break;
  3366. };
  3367. if ( tPolyID ) {
  3368. #ifdef DEBUG_EXTRA
  3369. LOG("Add Points to a created Polygone!");
  3370. #endif
  3371. for ( uint16_t k=0;k < m_sShadow.pPolygones[i].wCount; k++ ) {
  3372. m_pDBC->addLRFMaskPolygonePoint(
  3373. tPolyID,
  3374. m_sShadow.pPolygones[i].pPoint[k].wX,
  3375. m_sShadow.pPolygones[i].pPoint[k].wY
  3376. );
  3377. };
  3378. } else LOG("No Polygone Created !!!!");
  3379. } else LOG("No Mask Created !!!!");
  3380. };
  3381. };
  3382. };
  3383. if ( tMaskID ) {
  3384. m_pDBC->setLRFMaskStatus( tMaskID, ( m_sShadow.m_nEnabled & (1<<MaskLayer) ) );
  3385. };
  3386. };
  3387. };
  3388. #ifdef DEBUG
  3389. void device2DSensor::makeShadowPic( CSTR picname ){
  3390. cv::Scalar zone_colors[6] = { cv::Scalar(0, 0, 255),
  3391. cv::Scalar(255, 20, 255),
  3392. cv::Scalar(0, 255, 255),
  3393. cv::Scalar(0, 255, 0),
  3394. cv::Scalar(255, 0, 0),
  3395. cv::Scalar(120, 120, 120)
  3396. };
  3397. const double TWO_PI = 6.2831853071796;
  3398. double k = TWO_PI /((double)MAX_2D_SHADOW_RAY_LINES );
  3399. double z_shift = k * ((double)m_sLRF_DATA.base_shift );
  3400. cv::Mat tPic(512, 512, CV_8UC3, cv::Scalar(20, 20, 20));
  3401. double cx = 256.0;
  3402. double cy = 256.0;
  3403. double r = 255.0/2048.0;
  3404. for ( int i = 0; i < 360; i++ ) {
  3405. double ca = k * i + z_shift; // must be checked sign!!
  3406. double sin_val = sin( ca );
  3407. double cos_val = cos( ca );
  3408. cv::line( tPic, cv::Point( cx, cy ), cv::Point( cx+cos_val*256, cy+sin_val*256 ), zone_colors[5], 1, cv::LINE_AA );
  3409. if( m_sShadow.ray_lines[i].nCount ){
  3410. for ( int z=0; z < m_sShadow.ray_lines[i].nCount; z++ ) {
  3411. int r1 = m_sShadow.ray_lines[i].rays[z].nMin;
  3412. int r2 = m_sShadow.ray_lines[i].rays[z].nMax;
  3413. cv::Scalar color = zone_colors[m_sShadow.ray_lines[i].rays[z].bType];
  3414. int x1 = cx+(cos_val*r1*r);
  3415. int y1 = cy+(sin_val*r1*r);
  3416. int x2 = cx+(cos_val*r2*r);
  3417. int y2 = cy+(sin_val*r2*r);
  3418. cv::line(tPic, cv::Point(x1, y1), cv::Point( x2, y2), color, 1, cv::LINE_AA);
  3419. };
  3420. };
  3421. };
  3422. vector<int> compression_params;
  3423. compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
  3424. compression_params.push_back(9);
  3425. //compression_params.push_back( cv::IMWRITE_JPEG_QUALITY );
  3426. //compression_params.push_back( m_sEmbedVideoData.compression_quality );
  3427. vector<uchar> buf;
  3428. cv::imencode( ".png", tPic, buf, compression_params );
  3429. FILE *fd;
  3430. fd = fopen( picname, "wb");
  3431. if ( fd ) {
  3432. fwrite( buf.data(), 1, buf.size(), fd );
  3433. fclose( fd );
  3434. };
  3435. buf.clear();
  3436. };
  3437. #endif
  3438. void device2DSensor::waitStateFunction( LRF_WORK_STAGE boboStage ){
  3439. if( m_bTimeOutCounter ){
  3440. m_bTimeOutCounter--;
  3441. DELAY_10MS( LGS_DEFAULT_WS_TIME );
  3442. } else {
  3443. if( m_bOpRetryCnt ){
  3444. #ifdef DEBUG_EXTRA
  3445. LOG("COM ATTEMPT RECOVER");
  3446. #endif
  3447. m_bOpRetryCnt--;
  3448. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  3449. registerCommError( false );
  3450. LRF_WORK_STAGE pS = getPrevStage();
  3451. if ( pS != LRF_WS_INIT ) {
  3452. setStage( pS, false );
  3453. } else {
  3454. if ( boboStage != LRF_WS_INIT ) {
  3455. setStage( boboStage, false );
  3456. } else {
  3457. setWM( LRF_WM_CONNECTION );
  3458. return;
  3459. };
  3460. };
  3461. } else {
  3462. LOG_ERROR("Communication Wait State TO!");
  3463. registerCommError( true );
  3464. //setStage( boboStage );
  3465. setWM( LRF_WM_CONNECTION );
  3466. return;
  3467. };
  3468. DELAY_10MS( LGS_DEFAULT_WS_TIME );
  3469. transactionResetResult();
  3470. };
  3471. };
  3472. void device2DSensor::poolStateFunction(){
  3473. goNextStage();
  3474. transactionResetResult();
  3475. };
  3476. #undef SIZE_IDX
  3477. #undef DEV_ID_IDX
  3478. #undef CMD_IDX
  3479. #undef SUB_CMD_IDX
  3480. #undef DATA_BEGIN_IDX
  3481. #undef LOG
  3482. #undef LOG_ERROR
  3483. #undef DEBUG_EXTRA