1 | /********************************************************************
|
---|
2 | // created: 2008/2/11 - 12:55
|
---|
3 | // filename: CanGateway.cpp
|
---|
4 | //
|
---|
5 | // author: Gerald Dherbomez
|
---|
6 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
7 | //
|
---|
8 | // version: $Id: $
|
---|
9 | //
|
---|
10 | // purpose: Decoding of the CAN bus
|
---|
11 | //
|
---|
12 | *********************************************************************/
|
---|
13 |
|
---|
14 |
|
---|
15 | #include "Pacpus/kernel/ComponentFactory.h"
|
---|
16 | #include "Pacpus/kernel/DbiteFileTypes.h"
|
---|
17 |
|
---|
18 | using namespace pacpus;
|
---|
19 |
|
---|
20 | #include "CanGateway.h"
|
---|
21 |
|
---|
22 | #include <QDebug>
|
---|
23 |
|
---|
24 | #include <QSemaphore>
|
---|
25 | #include <QList>
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | #include "../CanGateway/CanDecoderBase.h"
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | ////////////////////////////////////////////////////////////////////////////////
|
---|
34 | /// Construct the factory
|
---|
35 | ComponentFactory<CanGateway> sFactory("CanGateway");
|
---|
36 |
|
---|
37 | ////////////////////////////////////////////////////////////////////////////////
|
---|
38 | /// Constructor
|
---|
39 | CanGateway::CanGateway(QString name)
|
---|
40 | : ComponentBase(name)
|
---|
41 | {
|
---|
42 | counter_ = 0;
|
---|
43 | // tcpServer_ = NULL;
|
---|
44 | //subscribers_ = new QMultiHash<int, ComponentBase *>;
|
---|
45 | }
|
---|
46 |
|
---|
47 | ////////////////////////////////////////////////////////////////////////////////
|
---|
48 | /// Destructor
|
---|
49 | CanGateway::~CanGateway()
|
---|
50 | {
|
---|
51 | }
|
---|
52 |
|
---|
53 | /************************************************************************/
|
---|
54 | /* Start function
|
---|
55 | /************************************************************************/
|
---|
56 | void CanGateway::startActivity()
|
---|
57 | {
|
---|
58 | counter_ = 0;
|
---|
59 | THREAD_ALIVE = true;
|
---|
60 |
|
---|
61 | // set the exhange parameters for incoming CAN frames
|
---|
62 | canIf_.setExchangeBuffer(incomingCanFrames_, INCOMINGCANFRAMES_SIZE);
|
---|
63 | canIf_.setSignalSempahore(&semaphore_);
|
---|
64 | if (source_ == "shMem")
|
---|
65 | canIf_.setSource(Win32CanInterface::SharedMemory);
|
---|
66 | else if (source_ == "vector")
|
---|
67 | {
|
---|
68 | canIf_.setSource(Win32CanInterface::VectorCard);
|
---|
69 | // open the interface
|
---|
70 | if (!canIf_.openInterface(channel_, speed_))
|
---|
71 | qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
|
---|
72 | }
|
---|
73 | else if (source_ == "vectorXL")
|
---|
74 | {
|
---|
75 | canIf_.setSource(Win32CanInterface::XLVectorCard);
|
---|
76 | // open the interface
|
---|
77 | if (!canIf_.openInterface(channel_, speed_))
|
---|
78 | qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
|
---|
79 | }
|
---|
80 | else if (source_ == "peak")
|
---|
81 | {
|
---|
82 | canIf_.setSource(Win32CanInterface::PeakCard);
|
---|
83 | // open interface
|
---|
84 | if (canIf_.openInterface(port_, accessMode_)==0)
|
---|
85 | qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_);
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | qCritical("Error in the source property of the component, bad value");
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // start the 2 threads: reception thread and decoding thread
|
---|
94 | canIf_.start();
|
---|
95 | start();
|
---|
96 |
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | /************************************************************************/
|
---|
103 | /* Stop function
|
---|
104 | /************************************************************************/
|
---|
105 | void CanGateway::stopActivity()
|
---|
106 | {
|
---|
107 | counter_ = 0;
|
---|
108 | canIf_.stop();
|
---|
109 | if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL"))
|
---|
110 | canIf_.closeInterface(channel_);
|
---|
111 | canIf_.wait();
|
---|
112 |
|
---|
113 | // we stop the decoding thread
|
---|
114 | THREAD_ALIVE = false;
|
---|
115 | semaphore_.release(); // to release the waiting of new CAN frame
|
---|
116 |
|
---|
117 | if (!wait(1000))
|
---|
118 | {
|
---|
119 | terminate();
|
---|
120 | qDebug() << "The thread" << componentName << "seems blocked, it has been killed";
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | /************************************************************************/
|
---|
127 | /* Configuration of the component
|
---|
128 | /************************************************************************/
|
---|
129 | ComponentBase::COMPONENT_CONFIGURATION CanGateway::configureComponent(XmlComponentConfig config)
|
---|
130 | {
|
---|
131 | // FIXME: use string instead of char[]
|
---|
132 | // Peak driver, default values
|
---|
133 | strcpy(port_,"/dev/pcanusb0");
|
---|
134 | strcpy(accessMode_,"ReadOnly");
|
---|
135 |
|
---|
136 | channel_ = param.getProperty("channel").toInt() - 1;
|
---|
137 | speed_ = param.getProperty("speed").toInt() * 1000;
|
---|
138 | source_ = param.getProperty("source");
|
---|
139 | if (source_ =="peak")
|
---|
140 | {
|
---|
141 | if (param.getProperty("port")!= NULL)
|
---|
142 | strcpy(port_,param.getProperty("port").toStdString().c_str());
|
---|
143 | if (param.getProperty("mode")!= NULL)
|
---|
144 | strcpy(accessMode_,param.getProperty("mode").toStdString().c_str());
|
---|
145 | }
|
---|
146 | recording = (param.getProperty("recording") == "true" ? true : false);
|
---|
147 |
|
---|
148 | return ComponentBase::CONFIGURED_OK;
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | /************************************************************************/
|
---|
154 | /* The main loop of the thread
|
---|
155 | /************************************************************************/
|
---|
156 | void CanGateway::run()
|
---|
157 | {
|
---|
158 | counter_ = 0;
|
---|
159 | qDebug() << componentName << "thread is started";
|
---|
160 |
|
---|
161 | if (recording) {
|
---|
162 | rawCanFile_.open(componentName.toStdString() + "_rawcan.dbt", WriteMode, CARMEN_CAN_RAW , sizeof( CanFrame ) );
|
---|
163 | }
|
---|
164 |
|
---|
165 | tic();
|
---|
166 |
|
---|
167 | while (THREAD_ALIVE)
|
---|
168 | {
|
---|
169 | semaphore_.acquire();
|
---|
170 | if (!THREAD_ALIVE)
|
---|
171 | {
|
---|
172 | continue; // user asks stopping the thread
|
---|
173 | }
|
---|
174 |
|
---|
175 | //displayData(incomingCanFrames_[counter_].frame.data, incomingCanFrames_[counter_].frame.dlc, incomingCanFrames_[counter_].frame.id);
|
---|
176 | if (recording)
|
---|
177 | {
|
---|
178 | rawCanFile_.writeRecord(incomingCanFrames_[counter_].time, incomingCanFrames_[counter_].timerange,
|
---|
179 | reinterpret_cast<const char *>(&(incomingCanFrames_[counter_].frame)), sizeof(CanFrame));
|
---|
180 | }
|
---|
181 |
|
---|
182 | setState(ComponentBase::MONITOR_OK);
|
---|
183 |
|
---|
184 | //printf("id:%x\n",incomingCanFrames_[counter_].frame.id);
|
---|
185 |
|
---|
186 | /* switch (incomingCanFrames_[counter_].frame.id)
|
---|
187 | {
|
---|
188 | default:
|
---|
189 | // unknown identifier
|
---|
190 | break;
|
---|
191 | };
|
---|
192 |
|
---|
193 | */
|
---|
194 |
|
---|
195 | dispatchCanFrame(incomingCanFrames_[counter_]);
|
---|
196 |
|
---|
197 | counter_++;
|
---|
198 | counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (recording)
|
---|
202 | {
|
---|
203 | rawCanFile_.close();
|
---|
204 | }
|
---|
205 |
|
---|
206 | qDebug() << componentName << "thread is stopped";
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|