procsm_if.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "procsm_if.h"
  2. #include <QTimer>
  3. #include <iostream>
  4. procsm_if_readthread::procsm_if_readthread(procsm *pPSM,SMCallBack pCall,const char * strsmname)
  5. {
  6. mpPSM = pPSM;
  7. mpCall = pCall;
  8. strncpy(mstrsmname,strsmname,255);
  9. #ifdef USEDBUS
  10. bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
  11. if(bconnect == false)
  12. {
  13. std::cout<<"procsm_if_readthread::procsm_if_readthread bconect is false"<<std::endl;
  14. }
  15. #endif
  16. }
  17. procsm_if_readthread::procsm_if_readthread(procsm *pPSM,ModuleFun xFun,const char * strsmname)
  18. {
  19. mpPSM = pPSM;
  20. mFun = xFun;
  21. strncpy(mstrsmname,strsmname,255);
  22. mbFunPlus = true;
  23. #ifdef USEDBUS
  24. bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adc.adciv.modulecomm", strsmname,this,SLOT(onNewMsg(int)));
  25. if(bconnect == false)
  26. {
  27. std::cout<<"procsm_if_readthread::procsm_if_readthread bconect is false"<<std::endl;
  28. mbDBUSOK = false;
  29. QTimer * timer = new QTimer();
  30. timer->setTimerType(Qt::PreciseTimer);
  31. delete timer;
  32. }
  33. #endif
  34. }
  35. #ifdef USELCM
  36. void procsm_if_readthread::handlerMethod(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
  37. {
  38. qDebug("lcm receiv data. ");
  39. mxindex++;
  40. QDateTime dt = QDateTime::currentDateTime();
  41. if(mbFunPlus)
  42. {
  43. mFun((char *)rbuf->data,rbuf->data_size,mxindex,&dt,mstrsmname);
  44. }
  45. else
  46. {
  47. (*mpCall)((char *)rbuf->data,rbuf->data_size,mxindex,&dt,mstrsmname);
  48. }
  49. }
  50. #endif
  51. void procsm_if_readthread::puaseread()
  52. {
  53. mbRun = false;
  54. }
  55. void procsm_if_readthread::continueread()
  56. {
  57. mbRun = true;
  58. }
  59. void procsm_if_readthread::run()
  60. {
  61. #ifdef USELCM
  62. mlcm.subscribe(mstrsmname,&procsm_if_readthread::handlerMethod,this);
  63. while(!QThread::isInterruptionRequested())
  64. {
  65. mlcm.handle();
  66. }
  67. return;
  68. #endif
  69. QTime xTime;
  70. xTime.start();
  71. unsigned int nBufLen = 1;
  72. unsigned int nRead;
  73. char * str = new char[nBufLen];
  74. unsigned int index =0;
  75. QDateTime *pdt = new QDateTime();
  76. bool bAttach = false;
  77. while(!QThread::isInterruptionRequested())
  78. {
  79. if(mbRun == false)
  80. {
  81. msleep(10);
  82. continue;
  83. }
  84. if(bAttach == false)
  85. {
  86. bAttach = mpPSM->AttachMem();
  87. if(bAttach == false)
  88. {
  89. msleep(100);
  90. continue;
  91. }
  92. else
  93. {
  94. index = mpPSM->getcurrentnext();
  95. }
  96. }
  97. int nRtn = mpPSM->readmsg(index,str,nBufLen,&nRead,pdt);
  98. if(nRtn == 0)
  99. {
  100. #ifdef USEDBUS
  101. if(mbDBUSOK == true)
  102. {
  103. mWaitMutex.lock();
  104. mwc.wait(&mWaitMutex,10);
  105. mWaitMutex.unlock();
  106. }
  107. else
  108. {
  109. msleep(1);
  110. }
  111. #else
  112. msleep(1);
  113. #endif
  114. }
  115. else
  116. {
  117. if(nRtn == -1)
  118. {
  119. nBufLen = nRead;
  120. delete str;
  121. if(nBufLen < 1)nBufLen = 1;
  122. str = new char[nBufLen];
  123. }
  124. else
  125. {
  126. if(nRtn == -2)
  127. {
  128. index = mpPSM->getcurrentnext();
  129. }
  130. else
  131. {
  132. if(nRtn >0)
  133. {
  134. if(mbFunPlus)
  135. {
  136. mFun(str,nRtn,index,pdt,mstrsmname);
  137. }
  138. else
  139. {
  140. (*mpCall)(str,nRtn,index,pdt,mstrsmname);
  141. }
  142. index++;
  143. }
  144. else
  145. {
  146. usleep(100);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. delete str;
  153. delete pdt;
  154. // qDebug("Thread finish.");
  155. }
  156. #ifdef USEDBUS
  157. void procsm_if_readthread::onNewMsg(int x)
  158. {
  159. if(x == 100)std::cout<<x<<std::endl;
  160. mwc.wakeAll();
  161. // qDebug("wake");
  162. }
  163. #endif
  164. procsm_if::procsm_if(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode)
  165. {
  166. strncpy(mstrsmname,strsmname,255);
  167. #ifdef USELCM
  168. if(nMode == procsm::ModeWrite)
  169. {
  170. }
  171. else
  172. {
  173. }
  174. return;
  175. #endif
  176. mpPSM = new procsm(strsmname,nBufSize,nMaxPacCount,nMode);
  177. mnType = nMode;
  178. mTimer.setTimerType(Qt::PreciseTimer);
  179. }
  180. procsm_if::~procsm_if()
  181. {
  182. if(mnType == procsm::ModeRead)
  183. {
  184. mpReadThread->requestInterruption();
  185. while(!mpReadThread->isFinished())
  186. {
  187. }
  188. delete mpReadThread;
  189. }
  190. delete mpPSM;
  191. }
  192. int procsm_if::writemsg(const char *str, const unsigned int nSize)
  193. {
  194. if(mbRun == false)return -2;
  195. #ifdef USELCM
  196. int nres = mlcm.publish(mstrsmname,str,nSize);
  197. qDebug("publish message. res = %d",nres);
  198. return 0;
  199. #endif
  200. if(mnType == procsm::ModeRead)return -1; //this is listen.
  201. return mpPSM->writemsg(str,nSize);
  202. }
  203. #ifdef USELCM
  204. void procsm_if::handlerMethod(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
  205. {
  206. qDebug("receiv data. ");
  207. }
  208. #endif
  209. int procsm_if::listenmsg(SMCallBack pCall)
  210. {
  211. //#ifdef USELCM
  212. //// mlcm.subscribe(mstrsmname,&handlerMethod2);
  213. // mlcm.subscribe(mstrsmname,&procsm_if::handlerMethod,this);
  214. // while(true)
  215. // {
  216. // mlcm.handle();
  217. // }
  218. // return 0;
  219. //#endif
  220. if(mnType == procsm::ModeWrite)return -1; //listening.
  221. mpReadThread = new procsm_if_readthread(mpPSM,pCall,mstrsmname);
  222. // mpReadThread->setPriority(QThread::TimeCriticalPriority);
  223. // mpReadThread->start();
  224. mpReadThread->start(QThread::HighestPriority);
  225. // mnType = 1;
  226. return 0;
  227. }
  228. int procsm_if::listenmsg(ModuleFun xFun)
  229. {
  230. //#ifdef USELCM
  231. // mlcm.subscribe(mstrsmname,&procsm_if::handlerMethod,this);
  232. // while(true)
  233. // {
  234. // mlcm.handle();
  235. // }
  236. // return 0;
  237. //#endif
  238. if(mnType == procsm::ModeWrite)return -1; //listening.
  239. mpReadThread = new procsm_if_readthread(mpPSM,xFun,mstrsmname);
  240. // mpReadThread->setPriority(QThread::TimeCriticalPriority);
  241. // mpReadThread->start();
  242. mpReadThread->start(QThread::HighestPriority);
  243. // mnType = 1;
  244. return 0;
  245. }
  246. void procsm_if::stoplisten()
  247. {
  248. if(mnType != 1)return;
  249. mpReadThread->requestInterruption();
  250. while(!mpReadThread->isFinished());
  251. mnType = 0;
  252. // mpReadThread->deleteLater();
  253. qDebug("stop listen ok");
  254. }
  255. void procsm_if::pausecomm()
  256. {
  257. mbRun = false;
  258. if(mnType == procsm::ModeRead)
  259. {
  260. mpReadThread->puaseread();
  261. }
  262. }
  263. void procsm_if::continuecomm()
  264. {
  265. mbRun = true;
  266. if(mnType == procsm::ModeRead)
  267. {
  268. mpReadThread->continueread();
  269. }
  270. }