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.

402 lines
14 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolInternalLRF3D.h"
  2. #define LOG( X ) m_pLog->Log( "protocolLRF3D", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG234[256];\
  6. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, _tBfrErrMSG234 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D", this, _LOG_ERROR, X );
  11. #endif
  12. protocolInternalLRF3D::protocolInternalLRF3D(): protocolParser(){
  13. m_pLog = basicLogCollection::getInstance();
  14. m_nStatus = 0;
  15. };
  16. protocolInternalLRF3D::~protocolInternalLRF3D(){};
  17. int protocolInternalLRF3D::packetChecker( U8 *Buffer, size_t BufferSize ){
  18. #ifdef DUBUG_COMMUNICATION
  19. char tmp[32];
  20. int i;
  21. for( i=0;i< BufferSize; i++ ) {
  22. sprintf( tmp, " 0x%02X", Buffer[i] );
  23. LOG(tmp);
  24. };
  25. #endif
  26. if( Buffer[0] == 0xAA ){
  27. if( BufferSize > 5 ) {
  28. #ifdef PRU_SLAVE
  29. if ((Buffer[1] & 0x80) == 0x80 ){
  30. #else
  31. if ((Buffer[1] & 0x80) == 0x00 ){
  32. #endif
  33. size_t pLength;
  34. pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
  35. if( BufferSize >= ( pLength + 4 ) ) {
  36. U8 pCheckSum;
  37. pCheckSum = checksumLGS( Buffer, pLength+3 );
  38. if( Buffer[pLength+3] == pCheckSum ) {
  39. #ifdef DUBUG_COMMUNICATION
  40. LOG((CSTR)"PacketChecker: Packet Is Recognized (0)");
  41. #endif
  42. return 0; // Ok
  43. } else {
  44. #ifdef DUBUG_COMMUNICATION
  45. LOG((CSTR)"PacketChecker: Packet Not Recognized CHK SUM(-1)");
  46. #endif
  47. return -1; // To shift
  48. };
  49. };
  50. } else {
  51. #ifdef DUBUG_COMMUNICATION
  52. LOG((CSTR)"PacketChecker: Packet Not Recognized TAG (-1)");
  53. #endif
  54. return -1; // To shiftSeverity Code Description Project File Line Suppression State
  55. };
  56. };
  57. #ifdef DUBUG_COMMUNICATION
  58. LOG((CSTR)"PacketChecker: Packet Not Recognized SMALL SIZE(1)");
  59. #endif
  60. return 1; // To grow
  61. };
  62. #ifdef DUBUG_COMMUNICATION
  63. LOG((CSTR)"PacketChecker: Packet Not Recognized START TAG(-1)");
  64. #endif
  65. return -1; // To shift
  66. };
  67. protocolPackage *protocolInternalLRF3D::packetExtractor( U8 *Buffer, size_t BufferSize ){
  68. protocolPackage *RetVal;
  69. #ifdef DUBUG_COMMUNICATION
  70. char tmp[32];
  71. int i;
  72. for( i=0;i< BufferSize; i++ ) {
  73. sprintf( tmp, " 0x%02X", Buffer[i] );
  74. LOG(tmp);
  75. };
  76. #endif
  77. if( Buffer[0] == 0xAA ){
  78. if( BufferSize > 5 ){
  79. #ifdef PRU_SLAVE
  80. if ((Buffer[1] & 0x80) == 0x80 ) {
  81. #else
  82. if ((Buffer[1] & 0x80) == 0x00 ) {
  83. #endif
  84. size_t pLength;
  85. pLength = ((Buffer[1] & 0x7F) << 8) | Buffer[2];
  86. if( BufferSize >= ( pLength+4 ) ) {
  87. U8 pCheckSum;
  88. pCheckSum = checksumLGS(Buffer, pLength+3 );
  89. if( Buffer[pLength+3] == pCheckSum ) {
  90. RetVal = (protocolPackage *) malloc(sizeof(protocolPackage));
  91. if( RetVal == NULL ) return NULL;
  92. RetVal->m_pData =(U8*)malloc( pLength+4 );
  93. if( RetVal->m_pData == NULL ) {
  94. free( RetVal );
  95. return NULL;
  96. };
  97. RetVal->m_nSize = pLength+4;
  98. RetVal->m_nCode = Buffer[3];
  99. memcpy( RetVal->m_pData, Buffer, RetVal->m_nSize );
  100. #ifdef DUBUG_COMMUNICATION
  101. LOG((CSTR)"PacketExtractor: Packet Is Extracted (0)");
  102. #endif
  103. return RetVal;
  104. };
  105. };
  106. } else {
  107. LOG_ERROR("TAG mismatch!");
  108. };
  109. };
  110. #ifdef DUBUG_COMMUNICATION
  111. LOG((CSTR)"PacketExtractor: Packet Not Extracted(1)!!!");
  112. #endif
  113. return NULL;
  114. };
  115. #ifdef DUBUG_COMMUNICATION
  116. LOG((CSTR)"PacketExtractor: Packet Not Extracted(-1)");
  117. #endif
  118. return NULL;
  119. };
  120. uint32_t protocolInternalLRF3D::getStatus(){
  121. uint32_t RetVal;
  122. m_mStatusProtector.lock();
  123. RetVal = m_nStatus;
  124. m_mStatusProtector.unlock();
  125. return RetVal;
  126. };
  127. #undef LOG
  128. #undef LOG_ERROR
  129. #ifdef PRU_SLAVE
  130. #define LOG( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, X )
  131. #ifdef DEBUG
  132. #define LOG_ERROR( X ) {\
  133. char _tBfrErrMSG234[256];\
  134. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  135. m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, _tBfrErrMSG234 );\
  136. }
  137. #else
  138. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_SLAVE", this, _LOG_ERROR, X );
  139. #endif
  140. protocolInternalLRF3D_SLAVE::protocolInternalLRF3D_SLAVE() : protocolInternalLRF3D() {
  141. m_pLog = basicLogCollection::getInstance();
  142. m_nTargetCount = 0;
  143. memset( m_pTargets, 0, sizeof(targetRecord) * TWI_TARGET_QUEUE_SIZE );
  144. for( uint8_t i = 0; i < 16; i++ ) {
  145. m_pThresholds[ i ] = (thresholdNode *) malloc( TH_SIZE_BYTES );
  146. if( m_pThresholds[ i ] != NULL ) {
  147. memset( m_pThresholds[ i ], 0, TH_SIZE_BYTES );
  148. };
  149. };
  150. };
  151. protocolInternalLRF3D_SLAVE::~protocolInternalLRF3D_SLAVE(){
  152. for( uint8_t i = 0; i < 16; i++ ) {
  153. if( m_pThresholds[ i ] != NULL ) free( m_pThresholds[ i ] );
  154. };
  155. };
  156. bool protocolInternalLRF3D_SLAVE::create( void ){
  157. return protocolInternalLRF3D::create();
  158. };
  159. bool protocolInternalLRF3D_SLAVE::setTrasholdData( uint8_t lrf_id, uint32_t *ptr ){
  160. if( ptr == NULL ) return false;
  161. if( m_pThresholds[ lrf_id ] == NULL ) return false;
  162. m_mData.lock();
  163. memcpy( m_pThresholds[ lrf_id ], ptr, TH_SIZE_BYTES );
  164. m_nStatus |= (( 1 << lrf_id ) << 8 );
  165. m_mData.unlock();
  166. return false;
  167. };
  168. bool protocolInternalLRF3D_SLAVE::enqueueTarget( targetRecord *ptr ){
  169. if( ptr == NULL ) return false;
  170. m_mTargetProtector.lock();
  171. if( m_nTargetCount < TWI_TARGET_QUEUE_SIZE ) {
  172. memcpy( &m_pTargets[m_nTargetCount], ptr, sizeof(targetRecord) );
  173. m_nTargetCount++;
  174. m_nStatus = ( m_nStatus & 0x00fffffe ) | ( m_nTargetCount << 24 );
  175. } else {
  176. memcpy( &m_pTargets[0], &m_pTargets[1], (TWI_TARGET_QUEUE_SIZE-1)*sizeof(targetRecord) );
  177. memcpy( &m_pTargets[TWI_TARGET_QUEUE_SIZE-1], ptr, sizeof(targetRecord) );
  178. m_nStatus |= 1; //Target Overflow flag
  179. };
  180. m_mTargetProtector.unlock();
  181. return true;
  182. };
  183. bool protocolInternalLRF3D_SLAVE::sendTrasholdData( uint8_t lrf_id, uint32_t Idx ){
  184. if( m_pThresholds[ lrf_id ] == NULL ) return false;
  185. uint32_t c_mask = (( 1 << lrf_id ) << 8 );
  186. if( ( m_nStatus & c_mask ) == 0 ) return false;
  187. uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE];
  188. uint32_t node_index = Idx;
  189. uint32_t index = 8;
  190. Buffer[3] = TWIN_TH_DATA;
  191. Buffer[4] = ( node_index >> 24 ) & 0xFF;
  192. Buffer[5] = ( node_index >> 16 ) & 0xFF;
  193. Buffer[6] = ( node_index >> 8 ) & 0xFF;
  194. Buffer[7] = ( node_index & 0xFF );
  195. while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 5 ) ) && ( node_index < TH_SIZE_NODES ) ){
  196. Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 24 ) & 0xFF;//0xFE; // to clear Sunbit data
  197. Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 16 ) & 0xFF;
  198. Buffer[index++] = (m_pThresholds[ lrf_id ][ node_index ].data >> 8 ) & 0xFF;
  199. Buffer[index++] = m_pThresholds[ lrf_id ][ node_index ].data & 0xFF;
  200. node_index++;
  201. };
  202. #ifdef DEBUG
  203. LOG("TH Data Segment");
  204. #endif
  205. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) );
  206. };
  207. bool protocolInternalLRF3D_SLAVE::sendTargets( void ){
  208. uint8_t Buffer[PROTOCOL_PARSER_BUFFER_SIZE];
  209. if( m_nTargetCount > 0 ) {
  210. Buffer[3] = TWIN_TARGETS_DATA;
  211. uint32_t index = 5;
  212. uint8_t target_index = 0;
  213. m_mTargetProtector.lock();
  214. while ( ( index < ( TWI_MAX_PACKAGE_SIZE_BYTES - 21 ) ) && ( target_index < m_nTargetCount ) ){
  215. Buffer[index++] = (m_pTargets[ target_index ].width >> 24 ) & 0xFF;
  216. Buffer[index++] = (m_pTargets[ target_index ].width >> 16 ) & 0xFF;
  217. Buffer[index++] = (m_pTargets[ target_index ].width >> 8 ) & 0xFF;
  218. Buffer[index++] = m_pTargets[ target_index ].width & 0xFF;
  219. Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 24 ) & 0xFF;
  220. Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 16 ) & 0xFF;
  221. Buffer[index++] = (m_pTargets[ target_index ].begin_id >> 8 ) & 0xFF;
  222. Buffer[index++] = m_pTargets[ target_index ].begin_id & 0xFF;
  223. Buffer[index++] = (m_pTargets[ target_index ].end_id >> 8 ) & 0xFF;
  224. Buffer[index++] = m_pTargets[ target_index ].end_id & 0xFF;
  225. Buffer[index++] = (m_pTargets[ target_index ].end_id >> 8 ) & 0xFF;
  226. Buffer[index++] = m_pTargets[ target_index ].end_id & 0xFF;
  227. Buffer[index++] = (m_pTargets[ target_index ].range_min >> 24 ) & 0xFF;
  228. Buffer[index++] = (m_pTargets[ target_index ].range_min >> 16 ) & 0xFF;
  229. Buffer[index++] = (m_pTargets[ target_index ].range_max >> 8 ) & 0xFF;
  230. Buffer[index++] = m_pTargets[ target_index ].range_max & 0xFF;
  231. Buffer[index++] = (m_pTargets[ target_index ].status >> 24 ) & 0xFF;
  232. Buffer[index++] = (m_pTargets[ target_index ].status >> 16 ) & 0xFF;
  233. Buffer[index++] = (m_pTargets[ target_index ].status >> 8 ) & 0xFF;
  234. Buffer[index++] = m_pTargets[ target_index ].status & 0xFF;
  235. target_index++;
  236. };
  237. m_nTargetCount -= target_index;
  238. m_nStatus = ( m_nStatus & 0x00fffffe ) | ( m_nTargetCount << 24 );
  239. m_mTargetProtector.unlock();
  240. Buffer[4] = target_index;
  241. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, index ) );
  242. } else {
  243. Buffer[3] = TWIN_TARGETS_DATA;
  244. Buffer[4] = 0;
  245. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 5 ) );
  246. };
  247. };
  248. bool protocolInternalLRF3D_SLAVE::sendStatus( void ){
  249. if( m_oDataTransport == NULL ) return false;
  250. uint8_t Buffer[16];
  251. Buffer[3] = TWIN_STATUS_ACK;
  252. Buffer[4] = ( m_nStatus >> 24 ) & 0xFF;
  253. Buffer[5] = ( m_nStatus >> 16 ) & 0xFF;
  254. Buffer[6] = ( m_nStatus >> 8 ) & 0xFF;
  255. Buffer[7] = ( m_nStatus & 0xFF );
  256. #ifdef DEBUG
  257. LOG("Status ACK");
  258. #endif
  259. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
  260. };
  261. bool protocolInternalLRF3D_SLAVE::packetHandler( protocolPackage *Package ){
  262. if( Package == NULL ) return false;
  263. uint8_t cCMD = Package->m_pData[3];
  264. switch( cCMD ) {
  265. case TWIN_STATUS_REQ:{
  266. sendStatus();
  267. break;
  268. };
  269. };
  270. return true;
  271. };
  272. #else // PRU_SLAVE
  273. #define LOG( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, X )
  274. #ifdef DEBUG
  275. #define LOG_ERROR( X ) {\
  276. char _tBfrErrMSG234[256];\
  277. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  278. m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, _tBfrErrMSG234 );\
  279. }
  280. #else
  281. #define LOG_ERROR( X ) m_pLog->Log( "protocolLRF3D_MASTER", this, _LOG_ERROR, X );
  282. #endif
  283. protocolInternalLRF3D_MASTER::protocolInternalLRF3D_MASTER(){
  284. m_pLog = basicLogCollection::getInstance();
  285. };
  286. protocolInternalLRF3D_MASTER::~protocolInternalLRF3D_MASTER(){};
  287. bool protocolInternalLRF3D_MASTER::create( void ){
  288. return protocolInternalLRF3D::create();
  289. };
  290. bool protocolInternalLRF3D_MASTER::ackDataSegment( uint32_t dataIdx ){
  291. if( m_oDataTransport == NULL ) return false;
  292. uint8_t Buffer[16];
  293. Buffer[3] = TWIN_TH_DATA;
  294. Buffer[4] = ( dataIdx >> 8 ) & 0xFF;
  295. Buffer[5] = ( dataIdx & 0xFF );
  296. #ifdef DEBUG
  297. LOG("Treshold Data Seg");
  298. #endif
  299. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 6 ) );
  300. };
  301. bool protocolInternalLRF3D_MASTER::ackTargets( uint32_t pkgId ){
  302. if( m_oDataTransport == NULL ) return false;
  303. uint8_t Buffer[16];
  304. Buffer[3] = TWIN_TARGETS_REQ;
  305. Buffer[4] = ( pkgId >> 24 ) & 0xFF;
  306. Buffer[5] = ( pkgId >> 16 ) & 0xFF;
  307. Buffer[6] = ( pkgId >> 8 ) & 0xFF;
  308. Buffer[7] = ( pkgId & 0xFF );
  309. #ifdef DEBUG
  310. LOG("Target Ack");
  311. #endif
  312. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 8 ) );
  313. };
  314. bool protocolInternalLRF3D_MASTER::requestTargets(void){
  315. if( m_oDataTransport == NULL ) return false;
  316. uint8_t Buffer[16];
  317. Buffer[3] = TWIN_TARGETS_REQ;
  318. #ifdef DEBUG
  319. LOG("Target Req");
  320. #endif
  321. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 4 ) );
  322. };
  323. bool protocolInternalLRF3D_MASTER::requestTrasholdData( uint8_t lrf_id ){
  324. if( m_oDataTransport == NULL ) return false;
  325. uint8_t Buffer[16];
  326. Buffer[3] = TWIN_TH_GETDATA;
  327. Buffer[4] = lrf_id;
  328. #ifdef DEBUG
  329. LOG("Treshold Req");
  330. #endif
  331. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 5 ) );
  332. };
  333. bool protocolInternalLRF3D_MASTER::requestStatus( void ){
  334. if( m_oDataTransport == NULL ) return false;
  335. uint8_t Buffer[16];
  336. Buffer[3] = TWIN_STATUS_REQ;
  337. #ifdef DEBUG
  338. LOG("Status Req");
  339. #endif
  340. return m_oDataTransport->sendData( Buffer, FinishUpPackage( Buffer, 4 ) );
  341. };
  342. bool protocolInternalLRF3D_MASTER::packetHandler( protocolPackage *Package ){
  343. if( Package == NULL ) return false;
  344. uint8_t cCMD = Package->m_pData[3];
  345. switch( cCMD ) {
  346. case TWIN_STATUS_ACK:{
  347. uint32_t tStatus;
  348. #ifdef DEBUG
  349. LOG("Status ACK reseived");
  350. #endif
  351. tStatus = ( Package->m_pData[4] << 24 ) | ( Package->m_pData[5] << 16 ) | ( Package->m_pData[6] << 8 ) | Package->m_pData[7];
  352. m_mStatusProtector.lock();
  353. m_nStatus = tStatus;
  354. m_mStatusProtector.unlock();
  355. break;
  356. };
  357. case TWIN_TH_DATA:{
  358. };
  359. };
  360. m_mLastTypeProtector.lock();
  361. m_bLastPackageType = (TWIN_PCMD) cCMD;
  362. m_mLastTypeProtector.unlock();
  363. return true;
  364. };
  365. TWIN_PCMD protocolInternalLRF3D_MASTER::getLastPackageType( void ){
  366. TWIN_PCMD RetVal;
  367. m_mLastTypeProtector.lock();
  368. RetVal = m_bLastPackageType;
  369. m_mLastTypeProtector.unlock();
  370. return RetVal;
  371. };
  372. #endif
  373. #undef LOG
  374. #undef LOG_ERROR