udp_can.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "udp_can.h"
  2. #include <thread>
  3. #include "message_queue.h"
  4. #define GET_BIT(x, bit) ((x & (1 << bit)) >> bit)
  5. int16_t ChangeINT(unsigned char h,unsigned char l)
  6. {
  7. int16_t hi=(int16_t)(h&0x00FF);
  8. int16_t low= (int16_t)(l&0x00FF);
  9. return (hi<<8)|low;
  10. }
  11. CUdpCanSensor::CUdpCanSensor(CMessageQueue* q):_message(q)
  12. {
  13. _run = false;
  14. }
  15. void CUdpCanSensor::Notify(int8_t * buffer,int32_t size)
  16. {
  17. if(_run)
  18. {
  19. if(!_message->_GSML)
  20. {
  21. int16_t m_cmd = ChangeINT(buffer[1],buffer[0]);
  22. if(m_cmd == RemoNet::CC_CANMSG)
  23. {
  24. _curTick = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  25. _message->SetTickLive(_curTick);
  26. RemoNet::CCCanMsg Req;
  27. Req.ParseFromArray(buffer + 4, size - 4);
  28. cannet_busframe* frames = (cannet_busframe*)alloca(sizeof(cannet_busframe) * Req.frams_size());
  29. for (int32_t i = 0; i < Req.frams_size(); i++)
  30. {
  31. auto& frame = Req.frams(i);
  32. frames[i].canid = frame.canid();
  33. frames[i].dlc = frame.dlc();
  34. memcpy(frames[i].data, frame.data().data(), frame.dlc());
  35. //std::cout << (int)m_cmd << " " << frames[i].canid << " " << (int)frames[i].dlc << std::endl;
  36. }
  37. _message->_CanBusVehicle->Get()->OnMessage(frames,Req.frams_size());
  38. }
  39. else if(m_cmd==RemoNet::CC_Ping)
  40. {
  41. RemoNet::CCPing Req;
  42. Req.ParseFromArray(buffer + 4,size - 4);
  43. CIOBuffer * pBuffer=CIOBuffer::Alloc();
  44. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  45. message->cmd=MessageType::Ping;
  46. message->param_l=Req.tick();
  47. RemoNet::CCPing Rep;
  48. Rep.set_tick(message->param_l);
  49. CIOBuffer Buffer;
  50. MessageHead Head;
  51. Head.Command = RemoNet::CC_Ping;
  52. Head.Length = Rep.ByteSizeLong();
  53. Head.Serialize(Buffer.Buffer);
  54. auto ptr = Buffer.Buffer + MessageHead::Size();
  55. Rep.SerializeToArray(ptr, Head.Length);
  56. Buffer.Length = Head.Length + MessageHead::Size();
  57. _udpcan->Write(&Buffer);
  58. pBuffer->Release();
  59. }
  60. }
  61. else
  62. {
  63. uint8_t temp = ':',temp1 = ',',*p = NULL,*p1 = NULL;
  64. p = (uint8_t *)strchr((const char *)buffer,temp);
  65. unsigned long m_time = strtol((const char *)p + 1,NULL,10);
  66. p = (uint8_t *)strchr((const char *)buffer,temp1);
  67. unsigned long m_startrevord = strtol((const char *)p + 1,NULL,10);
  68. p1 = (uint8_t *)strchr((const char *)p + 1,temp1);
  69. unsigned long m_deviceid = strtol((const char *)p1 + 1,NULL,10);
  70. _message->SetGsmlInfo(m_time,m_startrevord,m_deviceid);
  71. }
  72. }
  73. }
  74. void CUdpCanSensor::Start()
  75. {
  76. if(!_run)
  77. {
  78. //_udp_thread = std::thread(&CUdpCanSensor::Run, this);
  79. _run = true;
  80. }
  81. }
  82. void CUdpCanSensor::Run()
  83. {
  84. }
  85. void CUdpCanSensor::SetSensorSocket(SensorSocket<CUdpCanSensor>* udpcan)
  86. {
  87. _udpcan = udpcan;
  88. }
  89. void CUdpCanSensor::Stop()
  90. {
  91. if (_run)
  92. {
  93. _run = false;
  94. //_udp_thread.join();
  95. }
  96. }