ivdriver_can_nvidia_agx.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "ivdriver_can_nvidia_agx.h"
  2. iv::ivdriver_can_nvidia_agx * gc;
  3. iv::canstate::canstate proCanSt;
  4. void Listencansend0(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  5. {
  6. iv::can::canmsg msg;
  7. if(false == msg.ParseFromArray(strdata,nSize))
  8. {
  9. std::cout<<"Listencansend Parse fail."<<std::endl;
  10. return;
  11. }
  12. gc->sendmsg(0,msg);
  13. }
  14. void Listencansend1(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  15. {
  16. iv::can::canmsg msg;
  17. if(false == msg.ParseFromArray(strdata,nSize))
  18. {
  19. std::cout<<"Listencansend Parse fail."<<std::endl;
  20. return;
  21. }
  22. gc->sendmsg(1,msg);
  23. }
  24. namespace iv {
  25. ivdriver_can_nvidia_agx::ivdriver_can_nvidia_agx(std::string strxmlpath)
  26. {
  27. gc = this;
  28. iv::xmlparam::Xmlparam xp(strxmlpath);
  29. std::string strmemsend0 = xp.GetParam("cansend0_agx","cansend0");
  30. std::string strmemsend1 = xp.GetParam("cansend1_agx","cansend1");
  31. std::string strmemrecv0 = xp.GetParam("canrecv0_agx","canrecv0");
  32. std::string strmemrecv1 = xp.GetParam("canrecv1_agx","canrecv1");
  33. // std::string strmemrecv0,strmemrecv1,strmemsend0,strmemsend1;
  34. mparecv0 = iv::modulecomm::RegisterSend(strmemrecv0.data(),100000,3);
  35. mparecv1 = iv::modulecomm::RegisterSend(strmemrecv1.data(),100000,3);
  36. mpcanState = iv::modulecomm::RegisterSend("canstate",18,3);
  37. mpasend0 = iv::modulecomm::RegisterRecv(strmemsend0.data(),Listencansend0);
  38. mpasend1 = iv::modulecomm::RegisterRecv(strmemsend1.data(),Listencansend1);
  39. mpcan = new nvcan();
  40. mspcan.reset(mpcan);
  41. // connect(mpcan,SIGNAL(SIG_CANOPENSTATE(bool,int,const char*)),this,SLOT(onCANState(bool,int,const char*)));
  42. mpcan->startdev();
  43. }
  44. void ivdriver_can_nvidia_agx::sendmsg(int index, iv::can::canmsg xmsg)
  45. {
  46. if((index <0)||(index > 1))
  47. {
  48. mpcan->mfault->SetFaultState(1, 0, "sendmsg index error");
  49. mpcan->mivlog->error("sendmsg index err");
  50. std::cout<<"canctrl::sendmsg index error"<<std::endl;
  51. return;
  52. }
  53. std::vector<basecan_msg> * psendmsgvector;
  54. QMutex * pMutex;
  55. if(index == 0)
  56. {
  57. pMutex = &mMutexcan1;
  58. psendmsgvector = &msendmsgvector1;
  59. }
  60. if(index == 1)
  61. {
  62. pMutex = &mMutexcan2;
  63. psendmsgvector = &msendmsgvector2;
  64. }
  65. if(psendmsgvector->size() > SENDMSGBUFSIZE)
  66. {
  67. mpcan->mivlog->warn("sendmsg buf full");
  68. return;
  69. }
  70. pMutex->lock();
  71. if(psendmsgvector->size() > 1000)psendmsgvector->clear();
  72. int i;
  73. for(i=0;i<xmsg.rawmsg_size();i++)
  74. {
  75. basecan_msg sendmsg;
  76. iv::can::canraw x;
  77. x.CopyFrom(xmsg.rawmsg(i));
  78. sendmsg.id = x.id();
  79. sendmsg.isExtern = x.bext();
  80. sendmsg.isRemote = x.bremote();
  81. int nlen = x.len();
  82. if((nlen < 0) || (nlen > 8))
  83. {
  84. nlen = 0;
  85. mpcan->mivlog->error("sendmsg nlen err");
  86. mpcan->mfault->SetFaultState(1, 0, "sendmsg nlen err");
  87. }
  88. sendmsg.nLen = nlen;
  89. if(sendmsg.nLen > 0)
  90. {
  91. memcpy(sendmsg.data,x.data().data(),sendmsg.nLen);
  92. }
  93. psendmsgvector->push_back(sendmsg);
  94. }
  95. pMutex->unlock();
  96. }
  97. void ivdriver_can_nvidia_agx::modulerun()
  98. {
  99. QTime xTime;
  100. xTime.start();
  101. int nOldTime = xTime.elapsed();
  102. int i;
  103. while(mbrun)
  104. {
  105. if(mpcan->IsOpen())
  106. {
  107. basecan_msg xmsg[2500];
  108. int nRec1,nRec2,nSend1,nSend2;
  109. if((nRec1 =mpcan->GetMessage(0,xmsg,2500))>0)
  110. {
  111. sharecanmsg(mparecv0,xmsg,nRec1,0);
  112. }
  113. if((nRec2 =mpcan->GetMessage(1,xmsg,2500))>0)
  114. {
  115. sharecanmsg(mparecv1,xmsg,nRec2,1);
  116. }
  117. nSend1 = 0;
  118. nSend2 = 0;
  119. mMutexcan1.lock();
  120. for(i=0;i<msendmsgvector1.size();i++)
  121. {
  122. mpcan->SetMessage(0,&(msendmsgvector1.at(i)));
  123. }
  124. msendmsgvector1.clear();
  125. mMutexcan1.unlock();
  126. mMutexcan2.lock();
  127. for(i=0;i<msendmsgvector2.size();i++)
  128. {
  129. mpcan->SetMessage(1,&(msendmsgvector2.at(i)));
  130. }
  131. msendmsgvector2.clear();
  132. mMutexcan2.unlock();
  133. proCanSt.set_b_canstate(true);
  134. int nsize = proCanSt.ByteSize();
  135. char * strdata = new char[proCanSt.ByteSize()];
  136. if(proCanSt.SerializePartialToArray(strdata,nsize))
  137. {
  138. iv::modulecomm::ModuleSendMsg(mpcanState,strdata,nsize);
  139. }
  140. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  141. // msleep(5);
  142. }
  143. else
  144. {
  145. proCanSt.set_b_canstate(false);
  146. int nsize = proCanSt.ByteSize();
  147. char * strdata = new char[proCanSt.ByteSize()];
  148. if(proCanSt.SerializePartialToArray(strdata,nsize))
  149. {
  150. iv::modulecomm::ModuleSendMsg(mpcanState,strdata,nsize);
  151. }
  152. delete strdata;
  153. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  154. // msleep(1);
  155. mpcan->mivlog->error("%s open can card fail",__func__);
  156. }
  157. mpcan->mfault->SetFaultState(0, 0, "ok");
  158. }
  159. }
  160. void ivdriver_can_nvidia_agx::sharecanmsg(void *xpa, basecan_msg * pxmsg,int ncount,int nch)
  161. {
  162. iv::can::canmsg xmsg;
  163. int i;
  164. for(i=0;i<ncount;i++)
  165. {
  166. iv::can::canraw * praw = xmsg.add_rawmsg();
  167. praw->set_id(pxmsg[i].id);
  168. praw->set_data(pxmsg[i].data,8);
  169. praw->set_bext(pxmsg[i].isExtern);
  170. praw->set_bremote(pxmsg[i].isRemote);
  171. praw->set_rectime(QDateTime::currentMSecsSinceEpoch());
  172. praw->set_len(pxmsg[i].nLen);
  173. }
  174. xmsg.set_channel(nch);
  175. xmsg.set_index(mindex[nch]);
  176. mindex[nch]++;
  177. int nsize = xmsg.ByteSize();
  178. char * strdata = new char[xmsg.ByteSize()];
  179. if(xmsg.SerializePartialToArray(strdata,nsize))
  180. {
  181. iv::modulecomm::ModuleSendMsg(xpa,strdata,nsize);
  182. }
  183. else
  184. {
  185. mpcan->mivlog->error("canctrl::sharecanmsg serialize error");
  186. mpcan->mfault->SetFaultState(1, 0, "sharecanmsg serialize error");
  187. }
  188. delete strdata;
  189. }
  190. }