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