First implementation
This commit is contained in:
13
driver/CANBlastDriver/CANBlastDriver.pri
Normal file
13
driver/CANBlastDriver/CANBlastDriver.pri
Normal file
@@ -0,0 +1,13 @@
|
||||
CONFIG += c++11
|
||||
QT += network
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/CANBlasterDriver.cpp \
|
||||
$$PWD/CANBlasterInterface.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/CANBlasterDriver.h \
|
||||
$$PWD/CANBlasterInterface.h
|
||||
|
||||
FORMS +=
|
||||
|
||||
140
driver/CANBlastDriver/CANBlasterDriver.cpp
Normal file
140
driver/CANBlastDriver/CANBlasterDriver.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2022 Ethan Zonca
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include "CANBlasterDriver.h"
|
||||
#include "CANBlasterInterface.h"
|
||||
#include <core/Backend.h>
|
||||
#include <driver/GenericCanSetupPage.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QtNetwork/QUdpSocket>
|
||||
#include <QNetworkDatagram>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QList>
|
||||
|
||||
CANBlasterDriver::CANBlasterDriver(Backend &backend)
|
||||
: CanDriver(backend),
|
||||
setupPage(new GenericCanSetupPage())
|
||||
{
|
||||
QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
|
||||
}
|
||||
|
||||
CANBlasterDriver::~CANBlasterDriver() {
|
||||
}
|
||||
|
||||
bool CANBlasterDriver::update() {
|
||||
|
||||
deleteAllInterfaces();
|
||||
|
||||
// TODO: Listen for multicast packets for discovery of canblaster servers
|
||||
QUdpSocket udpSocket;
|
||||
QHostAddress groupAddress;
|
||||
groupAddress.setAddress("239.255.43.21");
|
||||
udpSocket.bind(QHostAddress::AnyIPv4, 20000, QUdpSocket::ShareAddress);
|
||||
udpSocket.joinMulticastGroup(groupAddress);
|
||||
|
||||
// Record start time
|
||||
struct timeval start_time;
|
||||
gettimeofday(&start_time,NULL);
|
||||
|
||||
fprintf(stderr, "CANblaster: start listen\r\n");
|
||||
|
||||
QList<QString> detected_servers;
|
||||
|
||||
while(1)
|
||||
{
|
||||
while (udpSocket.hasPendingDatagrams())
|
||||
{
|
||||
QNetworkDatagram res = udpSocket.receiveDatagram(1024);
|
||||
if(res.isValid())
|
||||
{
|
||||
QByteArray asd = res.data();
|
||||
qDebug() << asd;
|
||||
QJsonDocument document = QJsonDocument::fromJson(res.data());
|
||||
QJsonObject rootObj = document.object();
|
||||
|
||||
|
||||
if(rootObj.length() == 2 &&
|
||||
rootObj["protocol"].toString() == "CANblaster" &&
|
||||
rootObj["version"].toInt() == 1)
|
||||
{
|
||||
if(!detected_servers.contains(res.senderAddress().toString()))
|
||||
detected_servers.append(res.senderAddress().toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Invalid CANblaster server. Protocol: %s Version: %d \r\n", rootObj["protocol"].toString().toStdString().c_str(), rootObj["version"].toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,NULL);
|
||||
|
||||
// Iterate until timer expires
|
||||
if(tv.tv_sec - start_time.tv_sec > 2)
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "CANblaster: stop listen\r\n");
|
||||
|
||||
fprintf(stderr, "Found %d servers: \r\n", detected_servers.length());
|
||||
int interface_cnt = 0;
|
||||
|
||||
for(QString server: detected_servers)
|
||||
{
|
||||
fprintf(stderr, " - %s\r\n", server.toStdString().c_str());
|
||||
createOrUpdateInterface(interface_cnt++, server, false);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString CANBlasterDriver::getName() {
|
||||
return "CANblaster";
|
||||
}
|
||||
|
||||
|
||||
|
||||
CANBlasterInterface *CANBlasterDriver::createOrUpdateInterface(int index, QString name, bool fd_support) {
|
||||
|
||||
foreach (CanInterface *intf, getInterfaces()) {
|
||||
CANBlasterInterface *scif = dynamic_cast<CANBlasterInterface*>(intf);
|
||||
if (scif->getIfIndex() == index) {
|
||||
scif->setName(name);
|
||||
return scif;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CANBlasterInterface *scif = new CANBlasterInterface(this, index, name, fd_support);
|
||||
addInterface(scif);
|
||||
return scif;
|
||||
}
|
||||
43
driver/CANBlastDriver/CANBlasterDriver.h
Normal file
43
driver/CANBlastDriver/CANBlasterDriver.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanDriver.h>
|
||||
|
||||
class CANBlasterInterface;
|
||||
class SetupDialogInterfacePage;
|
||||
class GenericCanSetupPage;
|
||||
|
||||
class CANBlasterDriver: public CanDriver {
|
||||
public:
|
||||
CANBlasterDriver(Backend &backend);
|
||||
virtual ~CANBlasterDriver();
|
||||
|
||||
virtual QString getName();
|
||||
virtual bool update();
|
||||
|
||||
private:
|
||||
CANBlasterInterface *createOrUpdateInterface(int index, QString name, bool fd_support);
|
||||
GenericCanSetupPage *setupPage;
|
||||
};
|
||||
314
driver/CANBlastDriver/CANBlasterInterface.cpp
Normal file
314
driver/CANBlastDriver/CANBlasterInterface.cpp
Normal file
@@ -0,0 +1,314 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2022 Ethan Zonca
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CANBlasterInterface.h"
|
||||
|
||||
#include <core/Backend.h>
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <core/CanMessage.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QProcess>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <QNetworkDatagram>
|
||||
|
||||
|
||||
CANBlasterInterface::CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support)
|
||||
: CanInterface((CanDriver *)driver),
|
||||
_idx(index),
|
||||
_isOpen(false),
|
||||
_name(name),
|
||||
_ts_mode(ts_mode_SIOCSHWTSTAMP),
|
||||
_socket(NULL)
|
||||
{
|
||||
// Set defaults
|
||||
_settings.setBitrate(500000);
|
||||
_settings.setSamplePoint(875);
|
||||
|
||||
_config.supports_canfd = fd_support;
|
||||
|
||||
// Record start time
|
||||
gettimeofday(&_heartbeat_time,NULL);
|
||||
}
|
||||
|
||||
CANBlasterInterface::~CANBlasterInterface() {
|
||||
}
|
||||
|
||||
QString CANBlasterInterface::getDetailsStr() const {
|
||||
if(_config.supports_canfd)
|
||||
{
|
||||
return "CANBlaster client with CANFD support";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "CANBlaster client with standard CAN support";
|
||||
}
|
||||
}
|
||||
|
||||
QString CANBlasterInterface::getName() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
void CANBlasterInterface::setName(QString name) {
|
||||
_name = name;
|
||||
}
|
||||
|
||||
QList<CanTiming> CANBlasterInterface::getAvailableBitrates()
|
||||
{
|
||||
QList<CanTiming> retval;
|
||||
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
|
||||
QList<unsigned> bitrates_fd({0, 2000000, 5000000});
|
||||
|
||||
QList<unsigned> samplePoints({875});
|
||||
|
||||
unsigned i=0;
|
||||
foreach (unsigned br, bitrates) {
|
||||
foreach(unsigned br_fd, bitrates_fd) {
|
||||
foreach (unsigned sp, samplePoints) {
|
||||
retval << CanTiming(i++, br, br_fd, sp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
void CANBlasterInterface::applyConfig(const MeasurementInterface &mi)
|
||||
{
|
||||
// Save settings for port configuration
|
||||
_settings = mi;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool CANBlasterInterface::supportsTimingConfiguration()
|
||||
{
|
||||
return _config.supports_timing;
|
||||
}
|
||||
|
||||
bool CANBlasterInterface::supportsCanFD()
|
||||
{
|
||||
return _config.supports_canfd;
|
||||
}
|
||||
|
||||
bool CANBlasterInterface::supportsTripleSampling()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned CANBlasterInterface::getBitrate()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t CANBlasterInterface::getCapabilities()
|
||||
{
|
||||
uint32_t retval =
|
||||
CanInterface::capability_config_os |
|
||||
CanInterface::capability_listen_only |
|
||||
CanInterface::capability_auto_restart;
|
||||
|
||||
if (supportsCanFD()) {
|
||||
retval |= CanInterface::capability_canfd;
|
||||
}
|
||||
|
||||
if (supportsTripleSampling()) {
|
||||
retval |= CanInterface::capability_triple_sampling;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool CANBlasterInterface::updateStatistics()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t CANBlasterInterface::getState()
|
||||
{
|
||||
if(_isOpen)
|
||||
return state_ok;
|
||||
else
|
||||
return state_bus_off;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumRxFrames()
|
||||
{
|
||||
return _status.rx_count;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumRxErrors()
|
||||
{
|
||||
return _status.rx_errors;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumTxFrames()
|
||||
{
|
||||
return _status.tx_count;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumTxErrors()
|
||||
{
|
||||
return _status.tx_errors;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumRxOverruns()
|
||||
{
|
||||
return _status.rx_overruns;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getNumTxDropped()
|
||||
{
|
||||
return _status.tx_dropped;
|
||||
}
|
||||
|
||||
int CANBlasterInterface::getIfIndex() {
|
||||
return _idx;
|
||||
}
|
||||
|
||||
const char *CANBlasterInterface::cname()
|
||||
{
|
||||
return _name.toStdString().c_str();
|
||||
}
|
||||
|
||||
void CANBlasterInterface::open()
|
||||
{
|
||||
|
||||
// Start off with a fresh socket
|
||||
if(_socket != NULL)
|
||||
{
|
||||
delete _socket;
|
||||
}
|
||||
_socket = new QUdpSocket();
|
||||
|
||||
if(_socket->bind(QHostAddress::AnyIPv4, 20001))
|
||||
{
|
||||
_isOpen = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("CANBlaster Bind Failed!");
|
||||
_isOpen = false;
|
||||
}
|
||||
}
|
||||
|
||||
void CANBlasterInterface::close()
|
||||
{
|
||||
if(_socket != NULL && _socket->isOpen())
|
||||
{
|
||||
_socket->close();
|
||||
}
|
||||
_isOpen = false;
|
||||
}
|
||||
|
||||
bool CANBlasterInterface::isOpen()
|
||||
{
|
||||
return _isOpen;
|
||||
}
|
||||
|
||||
void CANBlasterInterface::sendMessage(const CanMessage &msg) {
|
||||
|
||||
}
|
||||
|
||||
bool CANBlasterInterface::readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms)
|
||||
{
|
||||
// Don't saturate the thread
|
||||
QThread().usleep(250);
|
||||
|
||||
// Record start time
|
||||
struct timeval now;
|
||||
gettimeofday(&now,NULL);
|
||||
|
||||
if(now.tv_sec - _heartbeat_time.tv_sec > 1)
|
||||
{
|
||||
|
||||
_heartbeat_time.tv_sec = now.tv_sec;
|
||||
|
||||
if(_isOpen)
|
||||
{
|
||||
QByteArray Data;
|
||||
Data.append("Heartbeat");
|
||||
_socket->writeDatagram(Data, QHostAddress(getName()), 20002);
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: This only works with standard CAN frames right now!
|
||||
|
||||
// Process all pending datagrams
|
||||
// TODO: Could switch to if from while but the caller expects 1 can frame
|
||||
if (_isOpen && _socket->hasPendingDatagrams())
|
||||
{
|
||||
can_frame frame;
|
||||
QHostAddress address;
|
||||
quint16 port;
|
||||
int res = _socket->readDatagram((char*)&frame, sizeof(can_frame), &address, &port);
|
||||
|
||||
// TODO: Read all bytes... to CANFD_MTU max
|
||||
// if (nbytes == CANFD_MTU) {
|
||||
// printf("got CAN FD frame with length %d\n", cfd.len);
|
||||
// /* cfd.flags contains valid data */
|
||||
// } else if (nbytes == CAN_MTU) {
|
||||
// printf("got Classical CAN frame with length %d\n", cfd.len);
|
||||
// /* cfd.flags is undefined */
|
||||
// } else {
|
||||
// fprintf(stderr, "read: invalid CAN(FD) frame\n");
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
if(res > 0)
|
||||
{
|
||||
// Set timestamp to current time
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,NULL);
|
||||
CanMessage msg;
|
||||
msg.setTimestamp(tv);
|
||||
|
||||
msg.setInterfaceId(getId());
|
||||
msg.setId(frame.can_id & CAN_ERR_MASK);
|
||||
msg.setBRS(false);
|
||||
msg.setErrorFrame(frame.can_id & CAN_ERR_FLAG);
|
||||
msg.setExtended(frame.can_id & CAN_EFF_FLAG);
|
||||
msg.setRTR(frame.can_id & CAN_RTR_FLAG);
|
||||
msg.setLength(frame.len);
|
||||
|
||||
for(int i=0; i<frame.len && i<CAN_MAX_DLEN; i++)
|
||||
{
|
||||
msg.setDataAt(i, frame.data[i]);
|
||||
}
|
||||
msglist.append(msg);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
237
driver/CANBlastDriver/CANBlasterInterface.h
Normal file
237
driver/CANBlastDriver/CANBlasterInterface.h
Normal file
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../CanInterface.h"
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <QtNetwork/QUdpSocket>
|
||||
#include <QTimer>
|
||||
|
||||
class CANBlasterDriver;
|
||||
|
||||
typedef struct {
|
||||
bool supports_canfd;
|
||||
bool supports_timing;
|
||||
uint32_t state;
|
||||
uint32_t base_freq;
|
||||
uint32_t sample_point;
|
||||
uint32_t ctrl_mode;
|
||||
uint32_t restart_ms;
|
||||
} can_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t can_state;
|
||||
|
||||
uint64_t rx_count;
|
||||
int rx_errors;
|
||||
uint64_t rx_overruns;
|
||||
|
||||
uint64_t tx_count;
|
||||
int tx_errors;
|
||||
uint64_t tx_dropped;
|
||||
} can_status_t;
|
||||
|
||||
class CANBlasterInterface: public CanInterface {
|
||||
public:
|
||||
CANBlasterInterface(CANBlasterDriver *driver, int index, QString name, bool fd_support);
|
||||
virtual ~CANBlasterInterface();
|
||||
|
||||
QString getDetailsStr() const;
|
||||
virtual QString getName() const;
|
||||
void setName(QString name);
|
||||
|
||||
virtual QList<CanTiming> getAvailableBitrates();
|
||||
|
||||
virtual void applyConfig(const MeasurementInterface &mi);
|
||||
|
||||
bool supportsTimingConfiguration();
|
||||
bool supportsCanFD();
|
||||
bool supportsTripleSampling();
|
||||
|
||||
virtual unsigned getBitrate();
|
||||
virtual uint32_t getCapabilities();
|
||||
|
||||
|
||||
virtual void open();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
|
||||
virtual void sendMessage(const CanMessage &msg);
|
||||
virtual bool readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms);
|
||||
|
||||
virtual bool updateStatistics();
|
||||
virtual uint32_t getState();
|
||||
virtual int getNumRxFrames();
|
||||
virtual int getNumRxErrors();
|
||||
virtual int getNumRxOverruns();
|
||||
|
||||
virtual int getNumTxFrames();
|
||||
virtual int getNumTxErrors();
|
||||
virtual int getNumTxDropped();
|
||||
|
||||
|
||||
int getIfIndex();
|
||||
|
||||
private:
|
||||
typedef enum {
|
||||
ts_mode_SIOCSHWTSTAMP,
|
||||
ts_mode_SIOCGSTAMPNS,
|
||||
ts_mode_SIOCGSTAMP
|
||||
} ts_mode_t;
|
||||
|
||||
int _idx;
|
||||
bool _isOpen;
|
||||
QString _name;
|
||||
|
||||
MeasurementInterface _settings;
|
||||
|
||||
can_config_t _config;
|
||||
can_status_t _status;
|
||||
ts_mode_t _ts_mode;
|
||||
|
||||
struct timeval _heartbeat_time;
|
||||
QUdpSocket* _socket;
|
||||
const char *cname();
|
||||
|
||||
};
|
||||
|
||||
|
||||
/* Duplicated from Linux can.h for compilation on Windows. Not a great solution. */
|
||||
|
||||
|
||||
/* special address description flags for the CAN_ID */
|
||||
#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
|
||||
#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
|
||||
#define CAN_ERR_FLAG 0x20000000U /* error message frame */
|
||||
|
||||
/* valid bits in CAN ID for frame formats */
|
||||
#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
|
||||
#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
|
||||
#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
|
||||
|
||||
/*
|
||||
* Controller Area Network Identifier structure
|
||||
*
|
||||
* bit 0-28 : CAN identifier (11/29 bit)
|
||||
* bit 29 : error message frame flag (0 = data frame, 1 = error message)
|
||||
* bit 30 : remote transmission request flag (1 = rtr frame)
|
||||
* bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
|
||||
*/
|
||||
typedef uint32_t canid_t;
|
||||
|
||||
#define CAN_SFF_ID_BITS 11
|
||||
#define CAN_EFF_ID_BITS 29
|
||||
|
||||
/*
|
||||
* Controller Area Network Error Message Frame Mask structure
|
||||
*
|
||||
* bit 0-28 : error class mask (see include/uapi/linux/can/error.h)
|
||||
* bit 29-31 : set to zero
|
||||
*/
|
||||
typedef uint32_t can_err_mask_t;
|
||||
|
||||
/* CAN payload length and DLC definitions according to ISO 11898-1 */
|
||||
#define CAN_MAX_DLC 8
|
||||
#define CAN_MAX_RAW_DLC 15
|
||||
#define CAN_MAX_DLEN 8
|
||||
|
||||
/* CAN FD payload length and DLC definitions according to ISO 11898-7 */
|
||||
#define CANFD_MAX_DLC 15
|
||||
#define CANFD_MAX_DLEN 64
|
||||
|
||||
/**
|
||||
* struct can_frame - Classical CAN frame structure (aka CAN 2.0B)
|
||||
* @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
|
||||
* @len: CAN frame payload length in byte (0 .. 8)
|
||||
* @can_dlc: deprecated name for CAN frame payload length in byte (0 .. 8)
|
||||
* @__pad: padding
|
||||
* @__res0: reserved / padding
|
||||
* @len8_dlc: optional DLC value (9 .. 15) at 8 byte payload length
|
||||
* len8_dlc contains values from 9 .. 15 when the payload length is
|
||||
* 8 bytes but the DLC value (see ISO 11898-1) is greater then 8.
|
||||
* CAN_CTRLMODE_CC_LEN8_DLC flag has to be enabled in CAN driver.
|
||||
* @data: CAN frame payload (up to 8 byte)
|
||||
*/
|
||||
struct can_frame {
|
||||
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
|
||||
union {
|
||||
/* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
|
||||
* was previously named can_dlc so we need to carry that
|
||||
* name for legacy support
|
||||
*/
|
||||
uint8_t len;
|
||||
uint8_t can_dlc; /* deprecated */
|
||||
} __attribute__((packed)); /* disable padding added in some ABIs */
|
||||
uint8_t __pad; /* padding */
|
||||
uint8_t __res0; /* reserved / padding */
|
||||
uint8_t len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
|
||||
uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
|
||||
};
|
||||
|
||||
/*
|
||||
* defined bits for canfd_frame.flags
|
||||
*
|
||||
* The use of struct canfd_frame implies the FD Frame (FDF) bit to
|
||||
* be set in the CAN frame bitstream on the wire. The FDF bit switch turns
|
||||
* the CAN controllers bitstream processor into the CAN FD mode which creates
|
||||
* two new options within the CAN FD frame specification:
|
||||
*
|
||||
* Bit Rate Switch - to indicate a second bitrate is/was used for the payload
|
||||
* Error State Indicator - represents the error state of the transmitting node
|
||||
*
|
||||
* As the CANFD_ESI bit is internally generated by the transmitting CAN
|
||||
* controller only the CANFD_BRS bit is relevant for real CAN controllers when
|
||||
* building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
|
||||
* sense for virtual CAN interfaces to test applications with echoed frames.
|
||||
*
|
||||
* The struct can_frame and struct canfd_frame intentionally share the same
|
||||
* layout to be able to write CAN frame content into a CAN FD frame structure.
|
||||
* When this is done the former differentiation via CAN_MTU / CANFD_MTU gets
|
||||
* lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of
|
||||
* using struct canfd_frame for mixed CAN / CAN FD content (dual use).
|
||||
* N.B. the Kernel APIs do NOT provide mixed CAN / CAN FD content inside of
|
||||
* struct canfd_frame therefore the CANFD_FDF flag is disregarded by Linux.
|
||||
*/
|
||||
#define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
|
||||
#define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
|
||||
#define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */
|
||||
|
||||
/**
|
||||
* struct canfd_frame - CAN flexible data rate frame structure
|
||||
* @can_id: CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
|
||||
* @len: frame payload length in byte (0 .. CANFD_MAX_DLEN)
|
||||
* @flags: additional flags for CAN FD
|
||||
* @__res0: reserved / padding
|
||||
* @__res1: reserved / padding
|
||||
* @data: CAN FD frame payload (up to CANFD_MAX_DLEN byte)
|
||||
*/
|
||||
struct canfd_frame {
|
||||
canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
|
||||
uint8_t len; /* frame payload length in byte */
|
||||
uint8_t flags; /* additional flags for CAN FD */
|
||||
uint8_t __res0; /* reserved / padding */
|
||||
uint8_t __res1; /* reserved / padding */
|
||||
uint8_t data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
|
||||
};
|
||||
|
||||
#define CAN_MTU (sizeof(struct can_frame))
|
||||
#define CANFD_MTU (sizeof(struct canfd_frame))
|
||||
107
driver/CanDriver.cpp
Normal file
107
driver/CanDriver.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CanDriver.h"
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanInterface.h>
|
||||
|
||||
CanDriver::CanDriver(Backend &backend)
|
||||
: _backend(backend)
|
||||
{
|
||||
setId(0);
|
||||
}
|
||||
|
||||
CanDriver::~CanDriver() {
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
Backend &CanDriver::backend() const
|
||||
{
|
||||
return _backend;
|
||||
}
|
||||
|
||||
void CanDriver::init(int id)
|
||||
{
|
||||
setId(id);
|
||||
}
|
||||
|
||||
void CanDriver::setId(int id)
|
||||
{
|
||||
_id = id;
|
||||
}
|
||||
|
||||
int CanDriver::id()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
QList<CanInterface *> CanDriver::getInterfaces() const
|
||||
{
|
||||
return _interfaces;
|
||||
}
|
||||
|
||||
CanInterfaceIdList CanDriver::getInterfaceIds() const
|
||||
{
|
||||
CanInterfaceIdList retval;
|
||||
foreach (CanInterface *intf, _interfaces) {
|
||||
retval.push_back(intf->getId());
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
CanInterface *CanDriver::getInterfaceById(CanInterfaceId id)
|
||||
{
|
||||
return _interfaces.value(id & 0xFF);
|
||||
}
|
||||
|
||||
CanInterfaceId CanDriver::addInterface(CanInterface *intf)
|
||||
{
|
||||
intf->setId((id()<<8) | _interfaces.size());
|
||||
_interfaces.push_back(intf);
|
||||
return intf->getId();
|
||||
}
|
||||
|
||||
void CanDriver::deleteInterface(CanInterface *intf)
|
||||
{
|
||||
delete intf;
|
||||
_interfaces.removeOne(intf);
|
||||
}
|
||||
|
||||
void CanDriver::deleteAllInterfaces()
|
||||
{
|
||||
for(CanInterface* interface: _interfaces)
|
||||
{
|
||||
delete interface;
|
||||
_interfaces.removeOne(interface);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
CanInterface *CanDriver::getInterfaceByName(QString ifName)
|
||||
{
|
||||
foreach (CanInterface *intf, _interfaces) {
|
||||
if (intf->getName() == ifName) {
|
||||
return intf;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
63
driver/CanDriver.h
Normal file
63
driver/CanDriver.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
class Backend;
|
||||
class CanInterface;
|
||||
|
||||
typedef uint16_t CanInterfaceId;
|
||||
typedef QList<uint16_t> CanInterfaceIdList;
|
||||
|
||||
class CanDriver {
|
||||
public:
|
||||
CanDriver(Backend &backend);
|
||||
virtual ~CanDriver();
|
||||
|
||||
Backend &backend() const;
|
||||
|
||||
void init(int id);
|
||||
|
||||
int id();
|
||||
|
||||
virtual QString getName() = 0;
|
||||
virtual bool update() = 0;
|
||||
|
||||
CanInterfaceIdList getInterfaceIds() const;
|
||||
QList<CanInterface*> getInterfaces() const;
|
||||
CanInterface *getInterfaceById(CanInterfaceId id);
|
||||
CanInterfaceId addInterface(CanInterface *intf);
|
||||
void deleteInterface(CanInterface *intf);
|
||||
void deleteAllInterfaces();
|
||||
|
||||
CanInterface *getInterfaceByName(QString ifName);
|
||||
|
||||
private:
|
||||
Backend &_backend;
|
||||
int _id;
|
||||
QList<CanInterface*> _interfaces;
|
||||
|
||||
void setId(int id);
|
||||
};
|
||||
101
driver/CanInterface.cpp
Normal file
101
driver/CanInterface.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CanInterface.h"
|
||||
|
||||
#include <QList>
|
||||
|
||||
CanInterface::CanInterface(CanDriver *driver)
|
||||
: _id(-1), _driver(driver)
|
||||
{
|
||||
}
|
||||
|
||||
CanInterface::~CanInterface() {
|
||||
}
|
||||
|
||||
CanDriver* CanInterface::getDriver() {
|
||||
return _driver;
|
||||
}
|
||||
|
||||
QString CanInterface::getDetailsStr() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
uint32_t CanInterface::getCapabilities()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
QList<CanTiming> CanInterface::getAvailableBitrates()
|
||||
{
|
||||
QList<CanTiming> retval;
|
||||
retval << CanTiming(0, 10000, 0, 875) \
|
||||
<< CanTiming(1, 20000, 0, 875) \
|
||||
<< CanTiming(2, 50000, 0, 875) \
|
||||
<< CanTiming(3, 83333, 0, 875) \
|
||||
<< CanTiming(4, 100000, 0, 875) \
|
||||
<< CanTiming(5, 125000, 0, 875) \
|
||||
<< CanTiming(6, 250000, 0, 875) \
|
||||
<< CanTiming(7, 500000, 0, 875) \
|
||||
<< CanTiming(8, 800000, 0, 875) \
|
||||
<< CanTiming(9, 1000000, 0, 875);
|
||||
return retval;
|
||||
}
|
||||
|
||||
void CanInterface::open() {
|
||||
}
|
||||
|
||||
void CanInterface::close() {
|
||||
}
|
||||
|
||||
bool CanInterface::isOpen()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CanInterface::updateStatistics()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QString CanInterface::getStateText()
|
||||
{
|
||||
switch (getState()) {
|
||||
case state_ok: return "ok";
|
||||
case state_warning: return "warning";
|
||||
case state_passive: return "error passive";
|
||||
case state_bus_off: return "bus off";
|
||||
case state_stopped: return "stopped";
|
||||
case state_unknown: return "unknown";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
CanInterfaceId CanInterface::getId() const
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
void CanInterface::setId(CanInterfaceId id)
|
||||
{
|
||||
_id = id;
|
||||
}
|
||||
92
driver/CanInterface.h
Normal file
92
driver/CanInterface.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <stdint.h>
|
||||
#include "CanDriver.h"
|
||||
#include "CanTiming.h"
|
||||
#include <QObject>
|
||||
|
||||
class CanMessage;
|
||||
class MeasurementInterface;
|
||||
|
||||
class CanInterface: public QObject {
|
||||
public:
|
||||
enum {
|
||||
state_ok,
|
||||
state_warning,
|
||||
state_passive,
|
||||
state_bus_off,
|
||||
state_stopped,
|
||||
state_unknown
|
||||
};
|
||||
|
||||
enum {
|
||||
capability_canfd = 0x01,
|
||||
capability_listen_only = 0x02,
|
||||
capability_triple_sampling = 0x04,
|
||||
capability_one_shot = 0x08,
|
||||
capability_auto_restart = 0x10,
|
||||
capability_config_os = 0x20
|
||||
};
|
||||
|
||||
public:
|
||||
CanInterface(CanDriver *driver);
|
||||
virtual ~CanInterface();
|
||||
virtual CanDriver *getDriver();
|
||||
virtual QString getName() const = 0;
|
||||
virtual QString getDetailsStr() const;
|
||||
|
||||
virtual void applyConfig(const MeasurementInterface &mi) = 0;
|
||||
|
||||
virtual unsigned getBitrate() = 0;
|
||||
|
||||
virtual uint32_t getCapabilities();
|
||||
virtual QList<CanTiming> getAvailableBitrates();
|
||||
|
||||
virtual void open();
|
||||
virtual void close();
|
||||
|
||||
virtual bool isOpen();
|
||||
|
||||
virtual void sendMessage(const CanMessage &msg) = 0;
|
||||
virtual bool readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms) = 0;
|
||||
|
||||
virtual bool updateStatistics();
|
||||
virtual uint32_t getState() = 0;
|
||||
virtual int getNumRxFrames() = 0;
|
||||
virtual int getNumRxErrors() = 0;
|
||||
virtual int getNumTxFrames() = 0;
|
||||
virtual int getNumTxErrors() = 0;
|
||||
virtual int getNumRxOverruns() = 0;
|
||||
virtual int getNumTxDropped() = 0;
|
||||
|
||||
QString getStateText();
|
||||
|
||||
CanInterfaceId getId() const;
|
||||
void setId(CanInterfaceId id);
|
||||
|
||||
private:
|
||||
CanInterfaceId _id;
|
||||
CanDriver *_driver;
|
||||
};
|
||||
97
driver/CanListener.cpp
Normal file
97
driver/CanListener.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CanListener.h"
|
||||
|
||||
#include <QThread>
|
||||
|
||||
#include <core/Backend.h>
|
||||
#include <core/CanTrace.h>
|
||||
#include <core/CanMessage.h>
|
||||
#include "CanInterface.h"
|
||||
|
||||
CanListener::CanListener(QObject *parent, Backend &backend, CanInterface &intf)
|
||||
: QObject(parent),
|
||||
_backend(backend),
|
||||
_intf(intf),
|
||||
_shouldBeRunning(true),
|
||||
_openComplete(false)
|
||||
{
|
||||
_thread = new QThread();
|
||||
}
|
||||
|
||||
CanListener::~CanListener()
|
||||
{
|
||||
delete _thread;
|
||||
}
|
||||
|
||||
CanInterfaceId CanListener::getInterfaceId()
|
||||
{
|
||||
return _intf.getId();
|
||||
}
|
||||
|
||||
CanInterface &CanListener::getInterface()
|
||||
{
|
||||
return _intf;
|
||||
}
|
||||
|
||||
void CanListener::run()
|
||||
{
|
||||
// Note: open and close done from run() so all operations take place in the same thread
|
||||
//CanMessage msg;
|
||||
QList<CanMessage> rxMessages;
|
||||
CanTrace *trace = _backend.getTrace();
|
||||
_intf.open();
|
||||
_openComplete = true;
|
||||
while (_shouldBeRunning) {
|
||||
if (_intf.readMessage(rxMessages, 1000)) {
|
||||
for(CanMessage msg: rxMessages)
|
||||
{
|
||||
trace->enqueueMessage(msg, false);
|
||||
}
|
||||
rxMessages.clear();
|
||||
}
|
||||
}
|
||||
_intf.close();
|
||||
_thread->quit();
|
||||
}
|
||||
|
||||
void CanListener::startThread()
|
||||
{
|
||||
moveToThread(_thread);
|
||||
connect(_thread, SIGNAL(started()), this, SLOT(run()));
|
||||
_thread->start();
|
||||
|
||||
// Wait for interface to be open before returning so that beginMeasurement is emitted after interface open
|
||||
while(!_openComplete)
|
||||
QThread().usleep(250);
|
||||
}
|
||||
|
||||
void CanListener::requestStop()
|
||||
{
|
||||
_shouldBeRunning = false;
|
||||
}
|
||||
|
||||
void CanListener::waitFinish()
|
||||
{
|
||||
requestStop();
|
||||
_thread->wait();
|
||||
}
|
||||
60
driver/CanListener.h
Normal file
60
driver/CanListener.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <driver/CanDriver.h>
|
||||
#include <driver/CanInterface.h>
|
||||
|
||||
class QThread;
|
||||
class CanMessage;
|
||||
class Backend;
|
||||
|
||||
class CanListener : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CanListener(QObject *parent, Backend &backend, CanInterface &intf);
|
||||
virtual ~CanListener();
|
||||
|
||||
CanInterfaceId getInterfaceId();
|
||||
CanInterface &getInterface();
|
||||
|
||||
signals:
|
||||
void messageReceived(const CanMessage &msg);
|
||||
|
||||
public slots:
|
||||
void run();
|
||||
|
||||
void startThread();
|
||||
void requestStop();
|
||||
void waitFinish();
|
||||
|
||||
private:
|
||||
Backend &_backend;
|
||||
CanInterface &_intf;
|
||||
bool _shouldBeRunning;
|
||||
bool _openComplete;
|
||||
QThread *_thread;
|
||||
|
||||
};
|
||||
49
driver/CanTiming.cpp
Normal file
49
driver/CanTiming.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "CanTiming.h"
|
||||
|
||||
CanTiming::CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint)
|
||||
: _id(id),
|
||||
_bitrate(bitrate),
|
||||
_bitrate_fd(bitrate_fd),
|
||||
_samplePoint(samplePoint)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned CanTiming::getId()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
unsigned CanTiming::getBitrate()
|
||||
{
|
||||
return _bitrate;
|
||||
}
|
||||
|
||||
unsigned CanTiming::getBitrateFD()
|
||||
{
|
||||
return _bitrate_fd;
|
||||
}
|
||||
|
||||
bool CanTiming::isCanFD()
|
||||
{
|
||||
return _bitrate_fd != 0;
|
||||
}
|
||||
|
||||
unsigned CanTiming::getSamplePoint()
|
||||
{
|
||||
return _samplePoint;
|
||||
}
|
||||
|
||||
QString CanTiming::getSamplePointStr(unsigned samplePoint)
|
||||
{
|
||||
return QString::number(samplePoint/10.0, 'f', 1).append("%");
|
||||
}
|
||||
|
||||
QString CanTiming::getTitle()
|
||||
{
|
||||
QString format = isCanFD() ? "%1/%3(FD) kBit/s, sample point %2" : "%1 kBit/s, sample point %2";
|
||||
return format.arg(
|
||||
QString::number(getBitrate()/1000, 'f', 2),
|
||||
getSamplePointStr(getSamplePoint()),
|
||||
QString::number(getBitrateFD()/1000, 'f', 2)
|
||||
);
|
||||
}
|
||||
37
driver/CanTiming.h
Normal file
37
driver/CanTiming.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef CANTIMING_H
|
||||
#define CANTIMING_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
class CanTiming
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* \brief CanTiming generic CAN timing description
|
||||
* \param id internal id to identify this timing for the corresponding CAN driver
|
||||
* \param bitrate bitrate in bits per second, e.g. 500000
|
||||
* \param bitrate_fd bitrate for the FD part, or zero this is not a CanFD timing
|
||||
* \param samplePoint sample point, e.g. 0.875
|
||||
*/
|
||||
CanTiming(unsigned id, unsigned bitrate, unsigned bitrate_fd, unsigned samplePoint);
|
||||
unsigned getId();
|
||||
unsigned getBitrate();
|
||||
unsigned getBitrateFD();
|
||||
bool isCanFD();
|
||||
unsigned getSamplePoint();
|
||||
static QString getSamplePointStr(unsigned samplePoint);
|
||||
|
||||
/*!
|
||||
* \brief getTitle get a brief one-line description of this timing
|
||||
* \return description, e.g. "500kBit/s, sample point 87.5%"
|
||||
*/
|
||||
QString getTitle();
|
||||
|
||||
private:
|
||||
unsigned _id;
|
||||
unsigned _bitrate;
|
||||
unsigned _bitrate_fd;
|
||||
unsigned _samplePoint;
|
||||
};
|
||||
|
||||
#endif // CANTIMING_H
|
||||
80
driver/CandleApiDriver/CandleApiDriver.cpp
Normal file
80
driver/CandleApiDriver/CandleApiDriver.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CandleApiDriver.h"
|
||||
#include "api/candle.h"
|
||||
|
||||
#include "CandleApiInterface.h"
|
||||
#include <driver/GenericCanSetupPage.h>
|
||||
|
||||
CandleApiDriver::CandleApiDriver(Backend &backend)
|
||||
: CanDriver(backend),
|
||||
setupPage(new GenericCanSetupPage(0))
|
||||
{
|
||||
QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
|
||||
}
|
||||
|
||||
QString CandleApiDriver::getName()
|
||||
{
|
||||
return "CandleAPI";
|
||||
}
|
||||
|
||||
bool CandleApiDriver::update()
|
||||
{
|
||||
deleteAllInterfaces();
|
||||
|
||||
candle_list_handle clist;
|
||||
uint8_t num_interfaces;
|
||||
candle_handle dev;
|
||||
|
||||
if (candle_list_scan(&clist)) {
|
||||
if (candle_list_length(clist, &num_interfaces)) {
|
||||
for (uint8_t i=0; i<num_interfaces; i++) {
|
||||
if (candle_dev_get(clist, i, &dev)) {
|
||||
CandleApiInterface *cif = findInterface(dev);
|
||||
|
||||
if (cif == NULL) {
|
||||
cif = new CandleApiInterface(this, dev);
|
||||
addInterface(cif);
|
||||
} else {
|
||||
cif->update(dev);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
candle_list_free(clist);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
CandleApiInterface *CandleApiDriver::findInterface(candle_handle dev)
|
||||
{
|
||||
foreach (CanInterface *intf, getInterfaces()) {
|
||||
CandleApiInterface *cif = dynamic_cast<CandleApiInterface*>(intf);
|
||||
if (cif->getPath() == std::wstring(candle_dev_get_path(dev))) {
|
||||
return cif;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
44
driver/CandleApiDriver/CandleApiDriver.h
Normal file
44
driver/CandleApiDriver/CandleApiDriver.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanDriver.h>
|
||||
#include "api/candle.h"
|
||||
|
||||
class CandleApiInterface;
|
||||
class GenericCanSetupPage;
|
||||
|
||||
class CandleApiDriver : public CanDriver
|
||||
{
|
||||
public:
|
||||
CandleApiDriver(Backend &backend);
|
||||
|
||||
virtual QString getName();
|
||||
virtual bool update();
|
||||
|
||||
private:
|
||||
GenericCanSetupPage *setupPage;
|
||||
CandleApiInterface *findInterface(candle_handle dev);
|
||||
|
||||
};
|
||||
|
||||
24
driver/CandleApiDriver/CandleApiDriver.pri
Normal file
24
driver/CandleApiDriver/CandleApiDriver.pri
Normal file
@@ -0,0 +1,24 @@
|
||||
QMAKE_CFLAGS += -std=gnu11
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/CandleApiDriver.cpp \
|
||||
$$PWD/CandleApiInterface.cpp \
|
||||
$$PWD/api/candle.c \
|
||||
$$PWD/api/candle_ctrl_req.c \
|
||||
$$PWD/CandleApiTiming.cpp
|
||||
|
||||
#win32:LIBS += -L$$PWD/api/ -lcandle_api
|
||||
win32: LIBS += -lSetupApi
|
||||
win32: LIBS += -lOle32
|
||||
win32: LIBS += -lwinusb
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/api/candle.h \
|
||||
$$PWD/CandleApiDriver.h \
|
||||
$$PWD/CandleApiInterface.h \
|
||||
$$PWD/api/candle_ctrl_req.h \
|
||||
$$PWD/api/candle_defs.h \
|
||||
$$PWD/api/ch_9.h \
|
||||
$$PWD/CandleApiTiming.h
|
||||
|
||||
FORMS +=
|
||||
391
driver/CandleApiDriver/CandleApiInterface.cpp
Normal file
391
driver/CandleApiDriver/CandleApiInterface.cpp
Normal file
@@ -0,0 +1,391 @@
|
||||
#include "CandleApiDriver.h"
|
||||
#include "CandleApiInterface.h"
|
||||
|
||||
CandleApiInterface::CandleApiInterface(CandleApiDriver *driver, candle_handle handle)
|
||||
: CanInterface(driver),
|
||||
_hostOffsetStart(0),
|
||||
_deviceTicksStart(0),
|
||||
_handle(handle),
|
||||
_backend(driver->backend()),
|
||||
_numRx(0),
|
||||
_numTx(0),
|
||||
_numTxErr(0)
|
||||
{
|
||||
_settings.setBitrate(500000);
|
||||
_settings.setSamplePoint(875);
|
||||
|
||||
|
||||
|
||||
// Timings for 170MHz processors (CANable 2.0)
|
||||
// Tseg1: 2..256 Tseg2: 2..128 sjw: 1..128 brp: 1..512
|
||||
// Note: as expressed below, Tseg1 does not include 1 count for prop phase
|
||||
_timings
|
||||
<< CandleApiTiming(170000000, 10000, 875, 68, 217, 31)
|
||||
<< CandleApiTiming(170000000, 20000, 875, 34, 217, 31)
|
||||
<< CandleApiTiming(170000000, 50000, 875, 17, 173, 25)
|
||||
<< CandleApiTiming(170000000, 83333, 875, 8, 221, 32)
|
||||
<< CandleApiTiming(170000000, 100000, 875, 10, 147, 21)
|
||||
<< CandleApiTiming(170000000, 125000, 875, 8, 147, 21)
|
||||
<< CandleApiTiming(170000000, 250000, 875, 4, 147, 21)
|
||||
<< CandleApiTiming(170000000, 500000, 875, 2, 147, 21)
|
||||
<< CandleApiTiming(170000000, 1000000, 875, 1, 147, 21);
|
||||
|
||||
|
||||
// Timings for 48MHz processors (CANable 0.X)
|
||||
_timings
|
||||
// sample point: 50.0%
|
||||
<< CandleApiTiming(48000000, 10000, 500, 300, 6, 8)
|
||||
<< CandleApiTiming(48000000, 20000, 500, 150, 6, 8)
|
||||
<< CandleApiTiming(48000000, 50000, 500, 60, 6, 8)
|
||||
<< CandleApiTiming(48000000, 83333, 500, 36, 6, 8)
|
||||
<< CandleApiTiming(48000000, 100000, 500, 30, 6, 8)
|
||||
<< CandleApiTiming(48000000, 125000, 500, 24, 6, 8)
|
||||
<< CandleApiTiming(48000000, 250000, 500, 12, 6, 8)
|
||||
<< CandleApiTiming(48000000, 500000, 500, 6, 6, 8)
|
||||
<< CandleApiTiming(48000000, 800000, 500, 3, 8, 9)
|
||||
<< CandleApiTiming(48000000, 1000000, 500, 3, 6, 8)
|
||||
|
||||
// sample point: 62.5%
|
||||
<< CandleApiTiming(48000000, 10000, 625, 300, 8, 6)
|
||||
<< CandleApiTiming(48000000, 20000, 625, 150, 8, 6)
|
||||
<< CandleApiTiming(48000000, 50000, 625, 60, 8, 6)
|
||||
<< CandleApiTiming(48000000, 83333, 625, 36, 8, 6)
|
||||
<< CandleApiTiming(48000000, 100000, 625, 30, 8, 6)
|
||||
<< CandleApiTiming(48000000, 125000, 625, 24, 8, 6)
|
||||
<< CandleApiTiming(48000000, 250000, 625, 12, 8, 6)
|
||||
<< CandleApiTiming(48000000, 500000, 625, 6, 8, 6)
|
||||
<< CandleApiTiming(48000000, 800000, 600, 4, 7, 6)
|
||||
<< CandleApiTiming(48000000, 1000000, 625, 3, 8, 6)
|
||||
|
||||
// sample point: 75.0%
|
||||
<< CandleApiTiming(48000000, 10000, 750, 300, 10, 4)
|
||||
<< CandleApiTiming(48000000, 20000, 750, 150, 10, 4)
|
||||
<< CandleApiTiming(48000000, 50000, 750, 60, 10, 4)
|
||||
<< CandleApiTiming(48000000, 83333, 750, 36, 10, 4)
|
||||
<< CandleApiTiming(48000000, 100000, 750, 30, 10, 4)
|
||||
<< CandleApiTiming(48000000, 125000, 750, 24, 10, 4)
|
||||
<< CandleApiTiming(48000000, 250000, 750, 12, 10, 4)
|
||||
<< CandleApiTiming(48000000, 500000, 750, 6, 10, 4)
|
||||
<< CandleApiTiming(48000000, 800000, 750, 3, 13, 5)
|
||||
<< CandleApiTiming(48000000, 1000000, 750, 3, 10, 4)
|
||||
|
||||
// sample point: 87.5%
|
||||
<< CandleApiTiming(48000000, 10000, 875, 300, 12, 2)
|
||||
<< CandleApiTiming(48000000, 20000, 875, 150, 12, 2)
|
||||
<< CandleApiTiming(48000000, 50000, 875, 60, 12, 2)
|
||||
<< CandleApiTiming(48000000, 83333, 875, 36, 12, 2)
|
||||
<< CandleApiTiming(48000000, 100000, 875, 30, 12, 2)
|
||||
<< CandleApiTiming(48000000, 125000, 875, 24, 12, 2)
|
||||
<< CandleApiTiming(48000000, 250000, 875, 12, 12, 2)
|
||||
<< CandleApiTiming(48000000, 500000, 875, 6, 12, 2)
|
||||
<< CandleApiTiming(48000000, 800000, 867, 4, 11, 2)
|
||||
<< CandleApiTiming(48000000, 1000000, 875, 3, 12, 2);
|
||||
|
||||
|
||||
_timings
|
||||
// sample point: 50.0%
|
||||
<< CandleApiTiming(16000000, 10000, 520, 64, 11, 12)
|
||||
<< CandleApiTiming(16000000, 20000, 500, 50, 6, 8)
|
||||
<< CandleApiTiming(16000000, 50000, 500, 20, 6, 8)
|
||||
<< CandleApiTiming(16000000, 83333, 500, 12, 6, 8)
|
||||
<< CandleApiTiming(16000000, 100000, 500, 10, 6, 8)
|
||||
<< CandleApiTiming(16000000, 125000, 500, 8, 6, 8)
|
||||
<< CandleApiTiming(16000000, 250000, 500, 4, 6, 8)
|
||||
<< CandleApiTiming(16000000, 500000, 500, 2, 6, 8)
|
||||
<< CandleApiTiming(16000000, 800000, 500, 1, 8, 10)
|
||||
<< CandleApiTiming(16000000, 1000000, 500, 1, 6, 8)
|
||||
|
||||
// sample point: 62.5%
|
||||
<< CandleApiTiming(16000000, 10000, 625, 64, 14, 9)
|
||||
<< CandleApiTiming(16000000, 20000, 625, 50, 8, 6)
|
||||
<< CandleApiTiming(16000000, 50000, 625, 20, 8, 6)
|
||||
<< CandleApiTiming(16000000, 83333, 625, 12, 8, 6)
|
||||
<< CandleApiTiming(16000000, 100000, 625, 10, 8, 6)
|
||||
<< CandleApiTiming(16000000, 125000, 625, 8, 8, 6)
|
||||
<< CandleApiTiming(16000000, 250000, 625, 4, 8, 6)
|
||||
<< CandleApiTiming(16000000, 500000, 625, 2, 8, 6)
|
||||
<< CandleApiTiming(16000000, 800000, 625, 1, 11, 7)
|
||||
<< CandleApiTiming(16000000, 1000000, 625, 1, 8, 6)
|
||||
|
||||
// sample point: 75.0%
|
||||
<< CandleApiTiming(16000000, 20000, 750, 50, 10, 4)
|
||||
<< CandleApiTiming(16000000, 50000, 750, 20, 10, 4)
|
||||
<< CandleApiTiming(16000000, 83333, 750, 12, 10, 4)
|
||||
<< CandleApiTiming(16000000, 100000, 750, 10, 10, 4)
|
||||
<< CandleApiTiming(16000000, 125000, 750, 8, 10, 4)
|
||||
<< CandleApiTiming(16000000, 250000, 750, 4, 10, 4)
|
||||
<< CandleApiTiming(16000000, 500000, 750, 2, 10, 4)
|
||||
<< CandleApiTiming(16000000, 800000, 750, 1, 13, 5)
|
||||
<< CandleApiTiming(16000000, 1000000, 750, 1, 10, 4)
|
||||
|
||||
// sample point: 87.5%
|
||||
<< CandleApiTiming(16000000, 20000, 875, 50, 12, 2)
|
||||
<< CandleApiTiming(16000000, 50000, 875, 20, 12, 2)
|
||||
<< CandleApiTiming(16000000, 83333, 875, 12, 12, 2)
|
||||
<< CandleApiTiming(16000000, 100000, 875, 10, 12, 2)
|
||||
<< CandleApiTiming(16000000, 125000, 875, 8, 12, 2)
|
||||
<< CandleApiTiming(16000000, 250000, 875, 4, 12, 2)
|
||||
<< CandleApiTiming(16000000, 500000, 875, 2, 12, 2)
|
||||
<< CandleApiTiming(16000000, 800000, 900, 2, 7, 1)
|
||||
<< CandleApiTiming(16000000, 1000000, 875, 1, 12, 2);
|
||||
}
|
||||
|
||||
CandleApiInterface::~CandleApiInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString CandleApiInterface::getName() const
|
||||
{
|
||||
return "candle" + QString::number(getId() & 0xFF);
|
||||
}
|
||||
|
||||
QString CandleApiInterface::getDetailsStr() const
|
||||
{
|
||||
return QString::fromStdWString(getPath());
|
||||
}
|
||||
|
||||
void CandleApiInterface::applyConfig(const MeasurementInterface &mi)
|
||||
{
|
||||
_settings = mi;
|
||||
}
|
||||
|
||||
unsigned CandleApiInterface::getBitrate()
|
||||
{
|
||||
return _settings.bitrate();
|
||||
}
|
||||
|
||||
uint32_t CandleApiInterface::getCapabilities()
|
||||
{
|
||||
candle_capability_t caps;
|
||||
|
||||
if (candle_channel_get_capabilities(_handle, 0, &caps)) {
|
||||
|
||||
uint32_t retval = 0;
|
||||
|
||||
if (caps.feature & CANDLE_MODE_LISTEN_ONLY) {
|
||||
retval |= CanInterface::capability_listen_only;
|
||||
}
|
||||
|
||||
if (caps.feature & CANDLE_MODE_ONE_SHOT) {
|
||||
retval |= CanInterface::capability_one_shot;
|
||||
}
|
||||
|
||||
if (caps.feature & CANDLE_MODE_TRIPLE_SAMPLE) {
|
||||
retval |= CanInterface::capability_triple_sampling;
|
||||
}
|
||||
|
||||
return retval;
|
||||
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
QList<CanTiming> CandleApiInterface::getAvailableBitrates()
|
||||
{
|
||||
QList<CanTiming> retval;
|
||||
|
||||
candle_capability_t caps;
|
||||
if (candle_channel_get_capabilities(_handle, 0, &caps)) {
|
||||
int i = 0;
|
||||
foreach (const CandleApiTiming t, _timings) {
|
||||
if (t.getBaseClk() == caps.fclk_can) {
|
||||
retval << CanTiming(i++, t.getBitrate(), 0, t.getSamplePoint());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool CandleApiInterface::setBitTiming(uint32_t bitrate, uint32_t samplePoint)
|
||||
{
|
||||
candle_capability_t caps;
|
||||
if (!candle_channel_get_capabilities(_handle, 0, &caps)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (const CandleApiTiming t, _timings) {
|
||||
if ( (t.getBaseClk() == caps.fclk_can)
|
||||
&& (t.getBitrate()==bitrate)
|
||||
&& (t.getSamplePoint()==samplePoint) )
|
||||
{
|
||||
candle_bittiming_t timing = t.getTiming();
|
||||
return candle_channel_set_timing(_handle, 0, &timing);
|
||||
}
|
||||
}
|
||||
|
||||
// no valid timing found
|
||||
return false;
|
||||
}
|
||||
|
||||
void CandleApiInterface::open()
|
||||
{
|
||||
if (!candle_dev_open(_handle)) {
|
||||
// TODO what?
|
||||
_isOpen = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!setBitTiming(_settings.bitrate(), _settings.samplePoint())) {
|
||||
// TODO what?
|
||||
_isOpen = false;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t flags = 0;
|
||||
if (_settings.isListenOnlyMode()) {
|
||||
flags |= CANDLE_MODE_LISTEN_ONLY;
|
||||
}
|
||||
if (_settings.isOneShotMode()) {
|
||||
flags |= CANDLE_MODE_ONE_SHOT;
|
||||
}
|
||||
if (_settings.isTripleSampling()) {
|
||||
flags |= CANDLE_MODE_TRIPLE_SAMPLE;
|
||||
}
|
||||
|
||||
_numRx = 0;
|
||||
_numTx = 0;
|
||||
_numTxErr = 0;
|
||||
|
||||
uint32_t t_dev;
|
||||
if (candle_dev_get_timestamp_us(_handle, &t_dev)) {
|
||||
_hostOffsetStart =
|
||||
_backend.getUsecsAtMeasurementStart() +
|
||||
_backend.getUsecsSinceMeasurementStart();
|
||||
_deviceTicksStart = t_dev;
|
||||
}
|
||||
|
||||
candle_channel_start(_handle, 0, flags);
|
||||
_isOpen = true;
|
||||
}
|
||||
|
||||
bool CandleApiInterface::isOpen()
|
||||
{
|
||||
return _isOpen;
|
||||
}
|
||||
|
||||
void CandleApiInterface::close()
|
||||
{
|
||||
candle_channel_stop(_handle, 0);
|
||||
candle_dev_close(_handle);
|
||||
_isOpen = false;
|
||||
}
|
||||
|
||||
void CandleApiInterface::sendMessage(const CanMessage &msg)
|
||||
{
|
||||
candle_frame_t frame;
|
||||
|
||||
frame.can_id = msg.getId();
|
||||
if (msg.isExtended()) {
|
||||
frame.can_id |= CANDLE_ID_EXTENDED;
|
||||
}
|
||||
if (msg.isRTR()) {
|
||||
frame.can_id |= CANDLE_ID_RTR;
|
||||
}
|
||||
|
||||
frame.can_dlc = msg.getLength();
|
||||
for (int i=0; i<8; i++) {
|
||||
frame.data[i] = msg.getByte(i);
|
||||
}
|
||||
|
||||
if (candle_frame_send(_handle, 0, &frame)) {
|
||||
_numTx++;
|
||||
} else {
|
||||
_numTxErr++;
|
||||
}
|
||||
}
|
||||
|
||||
bool CandleApiInterface::readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms)
|
||||
{
|
||||
candle_frame_t frame;
|
||||
CanMessage msg;
|
||||
|
||||
if (candle_frame_read(_handle, &frame, timeout_ms)) {
|
||||
|
||||
if (candle_frame_type(&frame)==CANDLE_FRAMETYPE_RECEIVE) {
|
||||
_numRx++;
|
||||
|
||||
msg.setInterfaceId(getId());
|
||||
msg.setErrorFrame(false);
|
||||
msg.setId(candle_frame_id(&frame));
|
||||
msg.setExtended(candle_frame_is_extended_id(&frame));
|
||||
msg.setRTR(candle_frame_is_rtr(&frame));
|
||||
|
||||
uint8_t dlc = candle_frame_dlc(&frame);
|
||||
uint8_t *data = candle_frame_data(&frame);
|
||||
msg.setLength(dlc);
|
||||
for (int i=0; i<dlc; i++) {
|
||||
msg.setByte(i, data[i]);
|
||||
}
|
||||
|
||||
uint32_t dev_ts = candle_frame_timestamp_us(&frame) - _deviceTicksStart;
|
||||
uint64_t ts_us = _hostOffsetStart + dev_ts;
|
||||
|
||||
uint64_t us_since_start = _backend.getUsecsSinceMeasurementStart();
|
||||
if (us_since_start > 0x180000000) { // device timestamp overflow must have happend at least once
|
||||
ts_us += us_since_start & 0xFFFFFFFF00000000;
|
||||
}
|
||||
|
||||
msg.setTimestamp(ts_us/1000000, ts_us % 1000000);
|
||||
msglist.append(msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CandleApiInterface::updateStatistics()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t CandleApiInterface::getState()
|
||||
{
|
||||
return CanInterface::state_ok;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumRxFrames()
|
||||
{
|
||||
return _numRx;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumRxErrors()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumTxFrames()
|
||||
{
|
||||
return _numTx;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumTxErrors()
|
||||
{
|
||||
return _numTxErr;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumRxOverruns()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CandleApiInterface::getNumTxDropped()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
wstring CandleApiInterface::getPath() const
|
||||
{
|
||||
return wstring(candle_dev_get_path(_handle));
|
||||
}
|
||||
|
||||
void CandleApiInterface::update(candle_handle dev)
|
||||
{
|
||||
candle_dev_free(_handle);
|
||||
_handle = dev;
|
||||
}
|
||||
|
||||
70
driver/CandleApiDriver/CandleApiInterface.h
Normal file
70
driver/CandleApiDriver/CandleApiInterface.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef CANDLEAPIINTERFACE_H
|
||||
#define CANDLEAPIINTERFACE_H
|
||||
|
||||
#include <driver/CanInterface.h>
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include "api/candle.h"
|
||||
#include "CandleApiTiming.h"
|
||||
#include <windows.h>
|
||||
#include <QList>
|
||||
|
||||
class CandleApiDriver;
|
||||
|
||||
using namespace std;
|
||||
|
||||
class CandleApiInterface : public CanInterface
|
||||
{
|
||||
public:
|
||||
CandleApiInterface(CandleApiDriver *driver, candle_handle handle);
|
||||
virtual ~CandleApiInterface();
|
||||
|
||||
virtual QString getName() const;
|
||||
virtual QString getDetailsStr() const;
|
||||
|
||||
virtual void applyConfig(const MeasurementInterface &mi);
|
||||
|
||||
virtual unsigned getBitrate();
|
||||
|
||||
virtual uint32_t getCapabilities();
|
||||
virtual QList<CanTiming> getAvailableBitrates();
|
||||
|
||||
virtual void open();
|
||||
virtual bool isOpen();
|
||||
virtual void close();
|
||||
|
||||
virtual void sendMessage(const CanMessage &msg);
|
||||
virtual bool readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms);
|
||||
|
||||
virtual bool updateStatistics();
|
||||
virtual uint32_t getState();
|
||||
virtual int getNumRxFrames();
|
||||
virtual int getNumRxErrors();
|
||||
virtual int getNumTxFrames();
|
||||
virtual int getNumTxErrors();
|
||||
virtual int getNumRxOverruns();
|
||||
virtual int getNumTxDropped();
|
||||
|
||||
wstring getPath() const;
|
||||
|
||||
void update(candle_handle dev);
|
||||
|
||||
private:
|
||||
|
||||
uint64_t _hostOffsetStart;
|
||||
uint32_t _deviceTicksStart;
|
||||
bool _isOpen;
|
||||
|
||||
candle_handle _handle;
|
||||
MeasurementInterface _settings;
|
||||
Backend &_backend;
|
||||
|
||||
uint64_t _numRx;
|
||||
uint64_t _numTx;
|
||||
uint64_t _numTxErr;
|
||||
|
||||
QList<CandleApiTiming> _timings;
|
||||
|
||||
bool setBitTiming(uint32_t bitrate, uint32_t samplePoint);
|
||||
};
|
||||
|
||||
#endif // CANDLEAPIINTERFACE_H
|
||||
33
driver/CandleApiDriver/CandleApiTiming.cpp
Normal file
33
driver/CandleApiDriver/CandleApiTiming.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "CandleApiTiming.h"
|
||||
|
||||
CandleApiTiming::CandleApiTiming(uint32_t baseClk, uint32_t bitrate, uint32_t samplePoint, uint32_t brp, uint32_t phase_seg1, uint32_t phase_seg2)
|
||||
: _baseClk(baseClk),
|
||||
_bitrate(bitrate),
|
||||
_samplePoint(samplePoint)
|
||||
{
|
||||
_timing.brp = brp;
|
||||
_timing.phase_seg1 = phase_seg1;
|
||||
_timing.phase_seg2 = phase_seg2;
|
||||
_timing.sjw = 1;
|
||||
_timing.prop_seg = 1;
|
||||
}
|
||||
|
||||
uint32_t CandleApiTiming::getBaseClk() const
|
||||
{
|
||||
return _baseClk;
|
||||
}
|
||||
|
||||
uint32_t CandleApiTiming::getBitrate() const
|
||||
{
|
||||
return _bitrate;
|
||||
}
|
||||
|
||||
uint32_t CandleApiTiming::getSamplePoint() const
|
||||
{
|
||||
return _samplePoint;
|
||||
}
|
||||
|
||||
candle_bittiming_t CandleApiTiming::getTiming() const
|
||||
{
|
||||
return _timing;
|
||||
}
|
||||
31
driver/CandleApiDriver/CandleApiTiming.h
Normal file
31
driver/CandleApiDriver/CandleApiTiming.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CANDLEAPITIMING_H
|
||||
#define CANDLEAPITIMING_H
|
||||
|
||||
#include "api/candle.h"
|
||||
|
||||
class CandleApiTiming
|
||||
{
|
||||
public:
|
||||
CandleApiTiming(
|
||||
uint32_t baseClk,
|
||||
uint32_t bitrate,
|
||||
uint32_t samplePoint,
|
||||
uint32_t brp,
|
||||
uint32_t phase_seg1,
|
||||
uint32_t phase_seg2
|
||||
);
|
||||
|
||||
uint32_t getBaseClk() const;
|
||||
uint32_t getBitrate() const;
|
||||
uint32_t getSamplePoint() const;
|
||||
candle_bittiming_t getTiming() const;
|
||||
|
||||
private:
|
||||
uint32_t _baseClk;
|
||||
uint32_t _bitrate;
|
||||
uint32_t _samplePoint;
|
||||
candle_bittiming_t _timing;
|
||||
|
||||
};
|
||||
|
||||
#endif // CANDLEAPITIMING_H
|
||||
66
driver/CandleApiDriver/api/LICENSE
Normal file
66
driver/CandleApiDriver/api/LICENSE
Normal file
@@ -0,0 +1,66 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
||||
|
||||
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
||||
|
||||
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
||||
|
||||
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
||||
|
||||
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
||||
|
||||
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
||||
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
||||
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
||||
d) Do one of the following:
|
||||
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
||||
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
||||
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
||||
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
||||
|
||||
589
driver/CandleApiDriver/api/candle.c
Normal file
589
driver/CandleApiDriver/api/candle.c
Normal file
@@ -0,0 +1,589 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "candle.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "candle_defs.h"
|
||||
#include "candle_ctrl_req.h"
|
||||
#include "ch_9.h"
|
||||
|
||||
static bool candle_dev_interal_open(candle_handle hdev);
|
||||
|
||||
static bool candle_read_di(HDEVINFO hdi, SP_DEVICE_INTERFACE_DATA interfaceData, candle_device_t *dev)
|
||||
{
|
||||
/* get required length first (this call always fails with an error) */
|
||||
ULONG requiredLength=0;
|
||||
SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, NULL, 0, &requiredLength, NULL);
|
||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
||||
dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS;
|
||||
return false;
|
||||
}
|
||||
|
||||
PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data =
|
||||
(PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, requiredLength);
|
||||
|
||||
if (detail_data != NULL) {
|
||||
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
||||
} else {
|
||||
dev->last_error = CANDLE_ERR_MALLOC;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool retval = true;
|
||||
ULONG length = requiredLength;
|
||||
if (!SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, detail_data, length, &requiredLength, NULL) ) {
|
||||
dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS2;
|
||||
retval = false;
|
||||
} else if (FAILED(StringCchCopy(dev->path, sizeof(dev->path), detail_data->DevicePath))) {
|
||||
dev->last_error = CANDLE_ERR_PATH_LEN;
|
||||
retval = false;
|
||||
}
|
||||
|
||||
LocalFree(detail_data);
|
||||
|
||||
if (!retval) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* try to open to read device infos and see if it is avail */
|
||||
if (candle_dev_interal_open(dev)) {
|
||||
dev->state = CANDLE_DEVSTATE_AVAIL;
|
||||
candle_dev_close(dev);
|
||||
} else {
|
||||
dev->state = CANDLE_DEVSTATE_INUSE;
|
||||
}
|
||||
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall candle_list_scan(candle_list_handle *list)
|
||||
{
|
||||
if (list==NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
candle_list_t *l = (candle_list_t *)calloc(1, sizeof(candle_list_t));
|
||||
*list = l;
|
||||
if (l==NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GUID guid;
|
||||
if (CLSIDFromString(L"{c15b4308-04d3-11e6-b3ea-6057189e6443}", &guid) != NOERROR) {
|
||||
l->last_error = CANDLE_ERR_CLSID;
|
||||
return false;
|
||||
}
|
||||
|
||||
HDEVINFO hdi = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
||||
if (hdi == INVALID_HANDLE_VALUE) {
|
||||
l->last_error = CANDLE_ERR_GET_DEVICES;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool rv = false;
|
||||
for (unsigned i=0; i<CANDLE_MAX_DEVICES; i++) {
|
||||
|
||||
SP_DEVICE_INTERFACE_DATA interfaceData;
|
||||
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
||||
|
||||
if (SetupDiEnumDeviceInterfaces(hdi, NULL, &guid, i, &interfaceData)) {
|
||||
|
||||
if (!candle_read_di(hdi, interfaceData, &l->dev[i])) {
|
||||
l->last_error = l->dev[i].last_error;
|
||||
rv = false;
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
DWORD err = GetLastError();
|
||||
if (err==ERROR_NO_MORE_ITEMS) {
|
||||
l->num_devices = i;
|
||||
l->last_error = CANDLE_ERR_OK;
|
||||
rv = true;
|
||||
} else {
|
||||
l->last_error = CANDLE_ERR_SETUPDI_IF_ENUM;
|
||||
rv = false;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
SetupDiDestroyDeviceInfoList(hdi);
|
||||
|
||||
return rv;
|
||||
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_list_free(candle_list_handle list)
|
||||
{
|
||||
free(list);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len)
|
||||
{
|
||||
candle_list_t *l = (candle_list_t *)list;
|
||||
*len = l->num_devices;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev)
|
||||
{
|
||||
candle_list_t *l = (candle_list_t *)list;
|
||||
if (l==NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dev_num >= CANDLE_MAX_DEVICES) {
|
||||
l->last_error = CANDLE_ERR_DEV_OUT_OF_RANGE;
|
||||
return false;
|
||||
}
|
||||
|
||||
candle_device_t *dev = calloc(1, sizeof(candle_device_t));
|
||||
*hdev = dev;
|
||||
if (dev==NULL) {
|
||||
l->last_error = CANDLE_ERR_MALLOC;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(dev, &l->dev[dev_num], sizeof(candle_device_t));
|
||||
l->last_error = CANDLE_ERR_OK;
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state)
|
||||
{
|
||||
if (hdev==NULL) {
|
||||
return false;
|
||||
} else {
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
*state = dev->state;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev)
|
||||
{
|
||||
if (hdev==NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
return dev->path;
|
||||
}
|
||||
}
|
||||
|
||||
static bool candle_dev_interal_open(candle_handle hdev)
|
||||
{
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
memset(dev->rxevents, 0, sizeof(dev->rxevents));
|
||||
memset(dev->rxurbs, 0, sizeof(dev->rxurbs));
|
||||
|
||||
dev->deviceHandle = CreateFile(
|
||||
dev->path,
|
||||
GENERIC_WRITE | GENERIC_READ,
|
||||
FILE_SHARE_WRITE | FILE_SHARE_READ,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
||||
NULL
|
||||
);
|
||||
|
||||
if (dev->deviceHandle == INVALID_HANDLE_VALUE) {
|
||||
dev->last_error = CANDLE_ERR_CREATE_FILE;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!WinUsb_Initialize(dev->deviceHandle, &dev->winUSBHandle)) {
|
||||
dev->last_error = CANDLE_ERR_WINUSB_INITIALIZE;
|
||||
goto close_handle;
|
||||
}
|
||||
|
||||
USB_INTERFACE_DESCRIPTOR ifaceDescriptor;
|
||||
if (!WinUsb_QueryInterfaceSettings(dev->winUSBHandle, 0, &ifaceDescriptor)) {
|
||||
dev->last_error = CANDLE_ERR_QUERY_INTERFACE;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
dev->interfaceNumber = ifaceDescriptor.bInterfaceNumber;
|
||||
unsigned pipes_found = 0;
|
||||
|
||||
for (uint8_t i=0; i<ifaceDescriptor.bNumEndpoints; i++) {
|
||||
|
||||
WINUSB_PIPE_INFORMATION pipeInfo;
|
||||
if (!WinUsb_QueryPipe(dev->winUSBHandle, 0, i, &pipeInfo)) {
|
||||
dev->last_error = CANDLE_ERR_QUERY_PIPE;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) {
|
||||
dev->bulkInPipe = pipeInfo.PipeId;
|
||||
pipes_found++;
|
||||
} else if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) {
|
||||
dev->bulkOutPipe = pipeInfo.PipeId;
|
||||
pipes_found++;
|
||||
} else {
|
||||
dev->last_error = CANDLE_ERR_PARSE_IF_DESCR;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (pipes_found != 2) {
|
||||
dev->last_error = CANDLE_ERR_PARSE_IF_DESCR;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
char use_raw_io = 1;
|
||||
if (!WinUsb_SetPipePolicy(dev->winUSBHandle, dev->bulkInPipe, RAW_IO, sizeof(use_raw_io), &use_raw_io)) {
|
||||
dev->last_error = CANDLE_ERR_SET_PIPE_RAW_IO;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
if (!candle_ctrl_set_host_format(dev)) {
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
if (!candle_ctrl_get_config(dev, &dev->dconf)) {
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
if (!candle_ctrl_get_capability(dev, 0, &dev->bt_const)) {
|
||||
dev->last_error = CANDLE_ERR_GET_BITTIMING_CONST;
|
||||
goto winusb_free;
|
||||
}
|
||||
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
|
||||
winusb_free:
|
||||
WinUsb_Free(dev->winUSBHandle);
|
||||
|
||||
close_handle:
|
||||
CloseHandle(dev->deviceHandle);
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
static bool candle_prepare_read(candle_device_t *dev, unsigned urb_num)
|
||||
{
|
||||
bool rc = WinUsb_ReadPipe(
|
||||
dev->winUSBHandle,
|
||||
dev->bulkInPipe,
|
||||
dev->rxurbs[urb_num].buf,
|
||||
sizeof(dev->rxurbs[urb_num].buf),
|
||||
NULL,
|
||||
&dev->rxurbs[urb_num].ovl
|
||||
);
|
||||
|
||||
if (rc || (GetLastError()!=ERROR_IO_PENDING)) {
|
||||
dev->last_error = CANDLE_ERR_PREPARE_READ;
|
||||
return false;
|
||||
} else {
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool candle_close_rxurbs(candle_device_t *dev)
|
||||
{
|
||||
for (unsigned i=0; i<CANDLE_URB_COUNT; i++) {
|
||||
if (dev->rxevents[i] != NULL) {
|
||||
CloseHandle(dev->rxevents[i]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool __stdcall DLL candle_dev_open(candle_handle hdev)
|
||||
{
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
if (candle_dev_interal_open(dev)) {
|
||||
for (unsigned i=0; i<CANDLE_URB_COUNT; i++) {
|
||||
HANDLE ev = CreateEvent(NULL, true, false, NULL);
|
||||
dev->rxevents[i] = ev;
|
||||
dev->rxurbs[i].ovl.hEvent = ev;
|
||||
if (!candle_prepare_read(dev, i)) {
|
||||
candle_close_rxurbs(dev);
|
||||
return false; // keep last_error from prepare_read call
|
||||
}
|
||||
}
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
} else {
|
||||
return false; // keep last_error from open_device call
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us)
|
||||
{
|
||||
return candle_ctrl_get_timestamp(hdev, timestamp_us);
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_dev_close(candle_handle hdev)
|
||||
{
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
candle_close_rxurbs(dev);
|
||||
|
||||
WinUsb_Free(dev->winUSBHandle);
|
||||
dev->winUSBHandle = NULL;
|
||||
CloseHandle(dev->deviceHandle);
|
||||
dev->deviceHandle = NULL;
|
||||
|
||||
dev->last_error = CANDLE_ERR_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_dev_free(candle_handle hdev)
|
||||
{
|
||||
free(hdev);
|
||||
return true;
|
||||
}
|
||||
|
||||
candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev)
|
||||
{
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
return dev->last_error;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels)
|
||||
{
|
||||
// TODO check if info was already read from device; try to do so; throw error...
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
*num_channels = dev->dconf.icount+1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap)
|
||||
{
|
||||
// TODO check if info was already read from device; try to do so; throw error...
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
memcpy(cap, &dev->bt_const.feature, sizeof(candle_capability_t));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data)
|
||||
{
|
||||
// TODO ensure device is open, check channel count..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
return candle_ctrl_set_bittiming(dev, ch, data);
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate)
|
||||
{
|
||||
// TODO ensure device is open, check channel count..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
if (dev->bt_const.fclk_can != 48000000) {
|
||||
/* this function only works for the candleLight base clock of 48MHz */
|
||||
dev->last_error = CANDLE_ERR_BITRATE_FCLK;
|
||||
return false;
|
||||
}
|
||||
|
||||
candle_bittiming_t t;
|
||||
t.prop_seg = 1;
|
||||
t.sjw = 1;
|
||||
t.phase_seg1 = 13 - t.prop_seg;
|
||||
t.phase_seg2 = 2;
|
||||
|
||||
switch (bitrate) {
|
||||
case 10000:
|
||||
t.brp = 300;
|
||||
break;
|
||||
|
||||
case 20000:
|
||||
t.brp = 150;
|
||||
break;
|
||||
|
||||
case 50000:
|
||||
t.brp = 60;
|
||||
break;
|
||||
|
||||
case 83333:
|
||||
t.brp = 36;
|
||||
break;
|
||||
|
||||
case 100000:
|
||||
t.brp = 30;
|
||||
break;
|
||||
|
||||
case 125000:
|
||||
t.brp = 24;
|
||||
break;
|
||||
|
||||
case 250000:
|
||||
t.brp = 12;
|
||||
break;
|
||||
|
||||
case 500000:
|
||||
t.brp = 6;
|
||||
break;
|
||||
|
||||
case 800000:
|
||||
t.brp = 4;
|
||||
t.phase_seg1 = 12 - t.prop_seg;
|
||||
t.phase_seg2 = 2;
|
||||
break;
|
||||
|
||||
case 1000000:
|
||||
t.brp = 3;
|
||||
break;
|
||||
|
||||
default:
|
||||
dev->last_error = CANDLE_ERR_BITRATE_UNSUPPORTED;
|
||||
return false;
|
||||
}
|
||||
|
||||
return candle_ctrl_set_bittiming(dev, ch, &t);
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags)
|
||||
{
|
||||
// TODO ensure device is open, check channel count..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
flags |= CANDLE_MODE_HW_TIMESTAMP;
|
||||
return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_START, flags);
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch)
|
||||
{
|
||||
// TODO ensure device is open, check channel count..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_RESET, 0);
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame)
|
||||
{
|
||||
// TODO ensure device is open, check channel count..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
unsigned long bytes_sent = 0;
|
||||
|
||||
frame->echo_id = 0;
|
||||
frame->channel = ch;
|
||||
|
||||
bool rc = WinUsb_WritePipe(
|
||||
dev->winUSBHandle,
|
||||
dev->bulkOutPipe,
|
||||
(uint8_t*)frame,
|
||||
sizeof(*frame),
|
||||
&bytes_sent,
|
||||
0
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SEND_FRAME;
|
||||
return rc;
|
||||
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms)
|
||||
{
|
||||
// TODO ensure device is open..
|
||||
candle_device_t *dev = (candle_device_t*)hdev;
|
||||
|
||||
DWORD wait_result = WaitForMultipleObjects(CANDLE_URB_COUNT, dev->rxevents, false, timeout_ms);
|
||||
if (wait_result == WAIT_TIMEOUT) {
|
||||
dev->last_error = CANDLE_ERR_READ_TIMEOUT;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( (wait_result < WAIT_OBJECT_0) || (wait_result >= WAIT_OBJECT_0 + CANDLE_URB_COUNT) ) {
|
||||
dev->last_error = CANDLE_ERR_READ_WAIT;
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD urb_num = wait_result - WAIT_OBJECT_0;
|
||||
DWORD bytes_transfered;
|
||||
|
||||
if (!WinUsb_GetOverlappedResult(dev->winUSBHandle, &dev->rxurbs[urb_num].ovl, &bytes_transfered, false)) {
|
||||
candle_prepare_read(dev, urb_num);
|
||||
dev->last_error = CANDLE_ERR_READ_RESULT;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bytes_transfered < sizeof(*frame)-4) {
|
||||
candle_prepare_read(dev, urb_num);
|
||||
dev->last_error = CANDLE_ERR_READ_SIZE;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bytes_transfered < sizeof(*frame)) {
|
||||
frame->timestamp_us = 0;
|
||||
}
|
||||
|
||||
memcpy(frame, dev->rxurbs[urb_num].buf, sizeof(*frame));
|
||||
|
||||
return candle_prepare_read(dev, urb_num);
|
||||
}
|
||||
|
||||
candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame)
|
||||
{
|
||||
if (frame->echo_id != 0xFFFFFFFF) {
|
||||
return CANDLE_FRAMETYPE_ECHO;
|
||||
};
|
||||
|
||||
if (frame->can_id & CANDLE_ID_ERR) {
|
||||
return CANDLE_FRAMETYPE_ERROR;
|
||||
}
|
||||
|
||||
return CANDLE_FRAMETYPE_RECEIVE;
|
||||
}
|
||||
|
||||
uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame)
|
||||
{
|
||||
return frame->can_id & 0x1FFFFFFF;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame)
|
||||
{
|
||||
return (frame->can_id & CANDLE_ID_EXTENDED) != 0;
|
||||
}
|
||||
|
||||
bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame)
|
||||
{
|
||||
return (frame->can_id & CANDLE_ID_RTR) != 0;
|
||||
}
|
||||
|
||||
uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame)
|
||||
{
|
||||
return frame->can_dlc;
|
||||
}
|
||||
|
||||
uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame)
|
||||
{
|
||||
return frame->data;
|
||||
}
|
||||
|
||||
uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame)
|
||||
{
|
||||
return frame->timestamp_us;
|
||||
}
|
||||
166
driver/CandleApiDriver/api/candle.h
Normal file
166
driver/CandleApiDriver/api/candle.h
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* candle_list_handle;
|
||||
typedef void* candle_handle;
|
||||
|
||||
typedef enum {
|
||||
CANDLE_DEVSTATE_AVAIL,
|
||||
CANDLE_DEVSTATE_INUSE
|
||||
} candle_devstate_t;
|
||||
|
||||
typedef enum {
|
||||
CANDLE_FRAMETYPE_UNKNOWN,
|
||||
CANDLE_FRAMETYPE_RECEIVE,
|
||||
CANDLE_FRAMETYPE_ECHO,
|
||||
CANDLE_FRAMETYPE_ERROR,
|
||||
CANDLE_FRAMETYPE_TIMESTAMP_OVFL
|
||||
} candle_frametype_t;
|
||||
|
||||
enum {
|
||||
CANDLE_ID_EXTENDED = 0x80000000,
|
||||
CANDLE_ID_RTR = 0x40000000,
|
||||
CANDLE_ID_ERR = 0x20000000
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
CANDLE_MODE_NORMAL = 0x00,
|
||||
CANDLE_MODE_LISTEN_ONLY = 0x01,
|
||||
CANDLE_MODE_LOOP_BACK = 0x02,
|
||||
CANDLE_MODE_TRIPLE_SAMPLE = 0x04,
|
||||
CANDLE_MODE_ONE_SHOT = 0x08,
|
||||
CANDLE_MODE_HW_TIMESTAMP = 0x10,
|
||||
} candle_mode_t;
|
||||
|
||||
typedef enum {
|
||||
CANDLE_ERR_OK = 0,
|
||||
CANDLE_ERR_CREATE_FILE = 1,
|
||||
CANDLE_ERR_WINUSB_INITIALIZE = 2,
|
||||
CANDLE_ERR_QUERY_INTERFACE = 3,
|
||||
CANDLE_ERR_QUERY_PIPE = 4,
|
||||
CANDLE_ERR_PARSE_IF_DESCR = 5,
|
||||
CANDLE_ERR_SET_HOST_FORMAT = 6,
|
||||
CANDLE_ERR_GET_DEVICE_INFO = 7,
|
||||
CANDLE_ERR_GET_BITTIMING_CONST = 8,
|
||||
CANDLE_ERR_PREPARE_READ = 9,
|
||||
CANDLE_ERR_SET_DEVICE_MODE = 10,
|
||||
CANDLE_ERR_SET_BITTIMING = 11,
|
||||
CANDLE_ERR_BITRATE_FCLK = 12,
|
||||
CANDLE_ERR_BITRATE_UNSUPPORTED = 13,
|
||||
CANDLE_ERR_SEND_FRAME = 14,
|
||||
CANDLE_ERR_READ_TIMEOUT = 15,
|
||||
CANDLE_ERR_READ_WAIT = 16,
|
||||
CANDLE_ERR_READ_RESULT = 17,
|
||||
CANDLE_ERR_READ_SIZE = 18,
|
||||
CANDLE_ERR_SETUPDI_IF_DETAILS = 19,
|
||||
CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20,
|
||||
CANDLE_ERR_MALLOC = 21,
|
||||
CANDLE_ERR_PATH_LEN = 22,
|
||||
CANDLE_ERR_CLSID = 23,
|
||||
CANDLE_ERR_GET_DEVICES = 24,
|
||||
CANDLE_ERR_SETUPDI_IF_ENUM = 25,
|
||||
CANDLE_ERR_SET_TIMESTAMP_MODE = 26,
|
||||
CANDLE_ERR_DEV_OUT_OF_RANGE = 27,
|
||||
CANDLE_ERR_GET_TIMESTAMP = 28,
|
||||
CANDLE_ERR_SET_PIPE_RAW_IO = 29
|
||||
} candle_err_t;
|
||||
|
||||
#pragma pack(push,1)
|
||||
|
||||
typedef struct {
|
||||
uint32_t echo_id;
|
||||
uint32_t can_id;
|
||||
uint8_t can_dlc;
|
||||
uint8_t channel;
|
||||
uint8_t flags;
|
||||
uint8_t reserved;
|
||||
uint8_t data[8];
|
||||
uint32_t timestamp_us;
|
||||
} candle_frame_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t feature;
|
||||
uint32_t fclk_can;
|
||||
uint32_t tseg1_min;
|
||||
uint32_t tseg1_max;
|
||||
uint32_t tseg2_min;
|
||||
uint32_t tseg2_max;
|
||||
uint32_t sjw_max;
|
||||
uint32_t brp_min;
|
||||
uint32_t brp_max;
|
||||
uint32_t brp_inc;
|
||||
} candle_capability_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t prop_seg;
|
||||
uint32_t phase_seg1;
|
||||
uint32_t phase_seg2;
|
||||
uint32_t sjw;
|
||||
uint32_t brp;
|
||||
} candle_bittiming_t;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#define DLL
|
||||
|
||||
bool __stdcall DLL candle_list_scan(candle_list_handle *list);
|
||||
bool __stdcall DLL candle_list_free(candle_list_handle list);
|
||||
bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len);
|
||||
|
||||
bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev);
|
||||
bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state);
|
||||
wchar_t __stdcall DLL *candle_dev_get_path(candle_handle hdev);
|
||||
bool __stdcall DLL candle_dev_open(candle_handle hdev);
|
||||
bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us);
|
||||
bool __stdcall DLL candle_dev_close(candle_handle hdev);
|
||||
bool __stdcall DLL candle_dev_free(candle_handle hdev);
|
||||
|
||||
bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels);
|
||||
bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap);
|
||||
bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data);
|
||||
bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate);
|
||||
bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags);
|
||||
bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch);
|
||||
|
||||
bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame);
|
||||
bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms);
|
||||
|
||||
candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame);
|
||||
uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame);
|
||||
bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame);
|
||||
bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame);
|
||||
uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame);
|
||||
uint8_t __stdcall DLL *candle_frame_data(candle_frame_t *frame);
|
||||
uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame);
|
||||
|
||||
candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
152
driver/CandleApiDriver/api/candle_ctrl_req.c
Normal file
152
driver/CandleApiDriver/api/candle_ctrl_req.c
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "candle_ctrl_req.h"
|
||||
#include "ch_9.h"
|
||||
|
||||
enum {
|
||||
CANDLE_BREQ_HOST_FORMAT = 0,
|
||||
CANDLE_BREQ_BITTIMING,
|
||||
CANDLE_BREQ_MODE,
|
||||
CANDLE_BREQ_BERR,
|
||||
CANDLE_BREQ_BT_CONST,
|
||||
CANDLE_BREQ_DEVICE_CONFIG,
|
||||
CANDLE_TIMESTAMP_GET,
|
||||
};
|
||||
|
||||
static bool usb_control_msg(WINUSB_INTERFACE_HANDLE hnd, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size)
|
||||
{
|
||||
WINUSB_SETUP_PACKET packet;
|
||||
memset(&packet, 0, sizeof(packet));
|
||||
|
||||
packet.Request = request;
|
||||
packet.RequestType = requesttype;
|
||||
packet.Value = value;
|
||||
packet.Index = index;
|
||||
packet.Length = size;
|
||||
|
||||
unsigned long bytes_sent = 0;
|
||||
return WinUsb_ControlTransfer(hnd, packet, (uint8_t*)data, size, &bytes_sent, 0);
|
||||
}
|
||||
|
||||
bool candle_ctrl_set_host_format(candle_device_t *dev)
|
||||
{
|
||||
candle_host_config_t hconf;
|
||||
hconf.byte_order = 0x0000beef;
|
||||
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_BREQ_HOST_FORMAT,
|
||||
USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
1,
|
||||
dev->interfaceNumber,
|
||||
&hconf,
|
||||
sizeof(hconf)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_HOST_FORMAT;
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags)
|
||||
{
|
||||
candle_device_mode_t dm;
|
||||
dm.mode = mode;
|
||||
dm.flags = flags;
|
||||
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_BREQ_MODE,
|
||||
USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
channel,
|
||||
dev->interfaceNumber,
|
||||
&dm,
|
||||
sizeof(dm)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_DEVICE_MODE;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf)
|
||||
{
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_BREQ_DEVICE_CONFIG,
|
||||
USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
1,
|
||||
dev->interfaceNumber,
|
||||
dconf,
|
||||
sizeof(*dconf)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_DEVICE_INFO;
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp)
|
||||
{
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_TIMESTAMP_GET,
|
||||
USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
1,
|
||||
dev->interfaceNumber,
|
||||
current_timestamp,
|
||||
sizeof(*current_timestamp)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_TIMESTAMP;
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data)
|
||||
{
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_BREQ_BT_CONST,
|
||||
USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
channel,
|
||||
0,
|
||||
data,
|
||||
sizeof(*data)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_BITTIMING_CONST;
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data)
|
||||
{
|
||||
bool rc = usb_control_msg(
|
||||
dev->winUSBHandle,
|
||||
CANDLE_BREQ_BITTIMING,
|
||||
USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
|
||||
channel,
|
||||
0,
|
||||
data,
|
||||
sizeof(*data)
|
||||
);
|
||||
|
||||
dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_BITTIMING;
|
||||
return rc;
|
||||
}
|
||||
37
driver/CandleApiDriver/api/candle_ctrl_req.h
Normal file
37
driver/CandleApiDriver/api/candle_ctrl_req.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "candle_defs.h"
|
||||
|
||||
enum {
|
||||
CANDLE_DEVMODE_RESET = 0,
|
||||
CANDLE_DEVMODE_START = 1
|
||||
};
|
||||
|
||||
bool candle_ctrl_set_host_format(candle_device_t *dev);
|
||||
bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags);
|
||||
bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf);
|
||||
bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data);
|
||||
bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data);
|
||||
bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp);
|
||||
|
||||
90
driver/CandleApiDriver/api/candle_defs.h
Normal file
90
driver/CandleApiDriver/api/candle_defs.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <windows.h>
|
||||
#include <winbase.h>
|
||||
#include <winusb.h>
|
||||
#include <setupapi.h>
|
||||
#include <devguid.h>
|
||||
#include <regstr.h>
|
||||
|
||||
#undef __CRT__NO_INLINE
|
||||
#include <strsafe.h>
|
||||
#define __CRT__NO_INLINE
|
||||
|
||||
#include "candle.h"
|
||||
|
||||
#define CANDLE_MAX_DEVICES 32
|
||||
#define CANDLE_URB_COUNT 30
|
||||
|
||||
#pragma pack(push,1)
|
||||
|
||||
typedef struct {
|
||||
uint32_t byte_order;
|
||||
} candle_host_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t reserved1;
|
||||
uint8_t reserved2;
|
||||
uint8_t reserved3;
|
||||
uint8_t icount;
|
||||
uint32_t sw_version;
|
||||
uint32_t hw_version;
|
||||
} candle_device_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t mode;
|
||||
uint32_t flags;
|
||||
} candle_device_mode_t;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
|
||||
typedef struct {
|
||||
OVERLAPPED ovl;
|
||||
uint8_t buf[128];
|
||||
} canlde_rx_urb;
|
||||
|
||||
typedef struct {
|
||||
wchar_t path[256];
|
||||
candle_devstate_t state;
|
||||
candle_err_t last_error;
|
||||
|
||||
HANDLE deviceHandle;
|
||||
WINUSB_INTERFACE_HANDLE winUSBHandle;
|
||||
UCHAR interfaceNumber;
|
||||
UCHAR bulkInPipe;
|
||||
UCHAR bulkOutPipe;
|
||||
|
||||
candle_device_config_t dconf;
|
||||
candle_capability_t bt_const;
|
||||
canlde_rx_urb rxurbs[CANDLE_URB_COUNT];
|
||||
HANDLE rxevents[CANDLE_URB_COUNT];
|
||||
} candle_device_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t num_devices;
|
||||
candle_err_t last_error;
|
||||
candle_device_t dev[CANDLE_MAX_DEVICES];
|
||||
} candle_list_t;
|
||||
37
driver/CandleApiDriver/api/ch_9.h
Normal file
37
driver/CandleApiDriver/api/ch_9.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de>
|
||||
|
||||
This file is part of the candle windows API.
|
||||
|
||||
This library is free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation, either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define USB_DIR_OUT 0 /* to device */
|
||||
#define USB_DIR_IN 0x80 /* to host */
|
||||
|
||||
#define USB_TYPE_MASK (0x03 << 5)
|
||||
#define USB_TYPE_STANDARD (0x00 << 5)
|
||||
#define USB_TYPE_CLASS (0x01 << 5)
|
||||
#define USB_TYPE_VENDOR (0x02 << 5)
|
||||
#define USB_TYPE_RESERVED (0x03 << 5)
|
||||
|
||||
#define USB_RECIP_MASK 0x1f
|
||||
#define USB_RECIP_DEVICE 0x00
|
||||
#define USB_RECIP_INTERFACE 0x01
|
||||
#define USB_RECIP_ENDPOINT 0x02
|
||||
#define USB_RECIP_OTHER 0x03
|
||||
172
driver/GenericCanSetupPage.cpp
Normal file
172
driver/GenericCanSetupPage.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "GenericCanSetupPage.h"
|
||||
#include "ui_GenericCanSetupPage.h"
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanInterface.h>
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <window/SetupDialog/SetupDialog.h>
|
||||
#include <QList>
|
||||
#include <QtAlgorithms>
|
||||
|
||||
GenericCanSetupPage::GenericCanSetupPage(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::GenericCanSetupPage),
|
||||
_mi(0),
|
||||
_enable_ui_updates(false)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
connect(ui->cbBitrate, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbSamplePoint, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbBitrateFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbSamplePointFD, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
|
||||
|
||||
connect(ui->cbConfigOS, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbListenOnly, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbOneShot, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbTripleSampling, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
|
||||
connect(ui->cbAutoRestart, SIGNAL(stateChanged(int)), this, SLOT(updateUI()));
|
||||
}
|
||||
|
||||
GenericCanSetupPage::~GenericCanSetupPage()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::onSetupDialogCreated(SetupDialog &dlg)
|
||||
{
|
||||
dlg.addPage(this);
|
||||
connect(&dlg, SIGNAL(onShowInterfacePage(SetupDialog&,MeasurementInterface*)), this, SLOT(onShowInterfacePage(SetupDialog&,MeasurementInterface*)));
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi)
|
||||
{
|
||||
_mi = mi;
|
||||
CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
|
||||
|
||||
_enable_ui_updates = false;
|
||||
|
||||
ui->laDriver->setText(intf->getDriver()->getName());
|
||||
ui->laInterface->setText(intf->getName());
|
||||
ui->laInterfaceDetails->setText(intf->getDetailsStr());
|
||||
|
||||
fillBitratesList(intf, _mi->bitrate());
|
||||
fillFdBitrate(intf, _mi->bitrate());
|
||||
fillSamplePointsForBitrate(intf, _mi->bitrate(), _mi->samplePoint());
|
||||
|
||||
|
||||
ui->cbConfigOS->setChecked(!_mi->doConfigure());
|
||||
ui->cbListenOnly->setChecked(_mi->isListenOnlyMode());
|
||||
ui->cbOneShot->setChecked(_mi->isOneShotMode());
|
||||
ui->cbTripleSampling->setChecked(_mi->isTripleSampling());
|
||||
ui->cbAutoRestart->setChecked(_mi->doAutoRestart());
|
||||
|
||||
disenableUI(_mi->doConfigure());
|
||||
dlg.displayPage(this);
|
||||
|
||||
_enable_ui_updates = true;
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::updateUI()
|
||||
{
|
||||
if (_enable_ui_updates && (_mi!=0)) {
|
||||
CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
|
||||
|
||||
_mi->setDoConfigure(!ui->cbConfigOS->isChecked());
|
||||
_mi->setListenOnlyMode(ui->cbListenOnly->isChecked());
|
||||
_mi->setOneShotMode(ui->cbOneShot->isChecked());
|
||||
_mi->setTripleSampling(ui->cbTripleSampling->isChecked());
|
||||
_mi->setAutoRestart(ui->cbAutoRestart->isChecked());
|
||||
_mi->setBitrate(ui->cbBitrate->currentData().toUInt());
|
||||
_mi->setSamplePoint(ui->cbSamplePoint->currentData().toUInt());
|
||||
|
||||
_enable_ui_updates = false;
|
||||
|
||||
disenableUI(_mi->doConfigure());
|
||||
fillSamplePointsForBitrate(
|
||||
intf,
|
||||
ui->cbBitrate->currentData().toUInt(),
|
||||
ui->cbSamplePoint->currentData().toUInt()
|
||||
);
|
||||
_enable_ui_updates = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::fillBitratesList(CanInterface *intf, unsigned selectedBitrate)
|
||||
{
|
||||
QList<uint32_t> bitrates;
|
||||
foreach (CanTiming t, intf->getAvailableBitrates()) {
|
||||
if (!bitrates.contains(t.getBitrate())) {
|
||||
bitrates.append(t.getBitrate());
|
||||
}
|
||||
}
|
||||
qSort(bitrates);
|
||||
|
||||
ui->cbBitrate->clear();
|
||||
foreach (uint32_t br, bitrates) {
|
||||
ui->cbBitrate->addItem(QString::number(br), br);
|
||||
}
|
||||
ui->cbBitrate->setCurrentText(QString::number(selectedBitrate));
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint)
|
||||
{
|
||||
QList<uint32_t> samplePoints;
|
||||
foreach(CanTiming t, intf->getAvailableBitrates()) {
|
||||
if (t.getBitrate() == selectedBitrate) {
|
||||
if (!samplePoints.contains(t.getSamplePoint())) {
|
||||
samplePoints.append(t.getSamplePoint());
|
||||
}
|
||||
}
|
||||
}
|
||||
qSort(samplePoints);
|
||||
|
||||
ui->cbSamplePoint->clear();
|
||||
foreach (uint32_t sp, samplePoints) {
|
||||
ui->cbSamplePoint->addItem(CanTiming::getSamplePointStr(sp), sp);
|
||||
}
|
||||
ui->cbSamplePoint->setCurrentText(CanTiming::getSamplePointStr(selectedSamplePoint));
|
||||
}
|
||||
|
||||
|
||||
void GenericCanSetupPage::fillFdBitrate(CanInterface *intf, unsigned selectedBitrate)
|
||||
{
|
||||
QList<uint32_t> fdBitrates;
|
||||
foreach(CanTiming t, intf->getAvailableBitrates()) {
|
||||
if (t.getBitrate() == selectedBitrate) {
|
||||
if (t.isCanFD() && !fdBitrates.contains(t.getBitrateFD())) {
|
||||
fdBitrates.append(t.getBitrateFD());
|
||||
}
|
||||
}
|
||||
}
|
||||
qSort(fdBitrates);
|
||||
|
||||
ui->cbBitrateFD->clear();
|
||||
foreach (uint32_t fd_br, fdBitrates) {
|
||||
ui->cbBitrateFD->addItem(QString::number(fd_br), fd_br);
|
||||
}
|
||||
}
|
||||
|
||||
void GenericCanSetupPage::disenableUI(bool enabled)
|
||||
{
|
||||
|
||||
CanInterface *intf = backend().getInterfaceById(_mi->canInterface());
|
||||
uint32_t caps = intf->getCapabilities();
|
||||
|
||||
ui->cbBitrate->setEnabled(enabled);
|
||||
ui->cbSamplePoint->setEnabled(enabled);
|
||||
ui->cbConfigOS->setEnabled(caps & CanInterface::capability_config_os);
|
||||
|
||||
ui->cbBitrateFD->setEnabled(enabled && (caps & CanInterface::capability_canfd));
|
||||
ui->cbSamplePointFD->setEnabled(enabled && (caps & CanInterface::capability_canfd));
|
||||
ui->cbListenOnly->setEnabled(enabled && (caps & CanInterface::capability_listen_only));
|
||||
ui->cbOneShot->setEnabled(enabled && (caps & CanInterface::capability_one_shot));
|
||||
ui->cbTripleSampling->setEnabled(enabled && (caps & CanInterface::capability_triple_sampling));
|
||||
ui->cbAutoRestart->setEnabled(enabled && (caps & CanInterface::capability_auto_restart));
|
||||
}
|
||||
|
||||
Backend &GenericCanSetupPage::backend()
|
||||
{
|
||||
return Backend::instance();
|
||||
}
|
||||
43
driver/GenericCanSetupPage.h
Normal file
43
driver/GenericCanSetupPage.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef GENERICCANSETUPPAGE_H
|
||||
#define GENERICCANSETUPPAGE_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class GenericCanSetupPage;
|
||||
}
|
||||
|
||||
class CanInterface;
|
||||
class SetupDialog;
|
||||
class MeasurementInterface;
|
||||
class Backend;
|
||||
|
||||
class GenericCanSetupPage : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit GenericCanSetupPage(QWidget *parent = 0);
|
||||
~GenericCanSetupPage();
|
||||
|
||||
public slots:
|
||||
void onSetupDialogCreated(SetupDialog &dlg);
|
||||
void onShowInterfacePage(SetupDialog &dlg, MeasurementInterface *mi);
|
||||
|
||||
private slots:
|
||||
void updateUI();
|
||||
|
||||
private:
|
||||
Ui::GenericCanSetupPage *ui;
|
||||
MeasurementInterface *_mi;
|
||||
bool _enable_ui_updates;
|
||||
|
||||
void fillBitratesList(CanInterface *intf, unsigned selectedBitrate);
|
||||
void fillSamplePointsForBitrate(CanInterface *intf, unsigned selectedBitrate, unsigned selectedSamplePoint);
|
||||
void fillFdBitrate(CanInterface *intf, unsigned selectedBitrate);
|
||||
void disenableUI(bool enabled);
|
||||
|
||||
Backend &backend();
|
||||
};
|
||||
|
||||
#endif // GENERICCANSETUPPAGE_H
|
||||
249
driver/GenericCanSetupPage.ui
Normal file
249
driver/GenericCanSetupPage.ui
Normal file
@@ -0,0 +1,249 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>GenericCanSetupPage</class>
|
||||
<widget class="QWidget" name="GenericCanSetupPage">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>795</width>
|
||||
<height>633</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>9</y>
|
||||
<width>66</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Driver:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="laDriver">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>9</y>
|
||||
<width>610</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>28</y>
|
||||
<width>82</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Interface:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="laInterface">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>28</y>
|
||||
<width>610</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_5">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>47</y>
|
||||
<width>117</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Interface Details:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="laInterfaceDetails">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>47</y>
|
||||
<width>610</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_7">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>84</y>
|
||||
<width>69</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Bitrate:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="cbBitrate">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>84</y>
|
||||
<width>170</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_8">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>110</y>
|
||||
<width>98</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Sample Point:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="cbSamplePoint">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>110</y>
|
||||
<width>170</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_9">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>150</y>
|
||||
<width>104</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>CanFD Bitrate:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="cbBitrateFD">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>150</y>
|
||||
<width>170</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_10">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>176</y>
|
||||
<width>130</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>CanFD SamplePoint:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="cbSamplePointFD">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>176</y>
|
||||
<width>170</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_11">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>9</x>
|
||||
<y>220</y>
|
||||
<width>41</width>
|
||||
<height>16</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Options:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QWidget" name="verticalLayoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>140</x>
|
||||
<y>220</y>
|
||||
<width>411</width>
|
||||
<height>126</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="vbOptions">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbConfigOS">
|
||||
<property name="text">
|
||||
<string>configured by operating system</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbListenOnly">
|
||||
<property name="text">
|
||||
<string>Listen only mode</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbOneShot">
|
||||
<property name="text">
|
||||
<string>One-Shot mode</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbTripleSampling">
|
||||
<property name="text">
|
||||
<string>Triple Sampling</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbAutoRestart">
|
||||
<property name="text">
|
||||
<string>Auto-Restart on bus off condition</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
107
driver/SLCANDriver/SLCANDriver.cpp
Normal file
107
driver/SLCANDriver/SLCANDriver.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2022 Ethan Zonca
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include "SLCANDriver.h"
|
||||
#include "SLCANInterface.h"
|
||||
#include <core/Backend.h>
|
||||
#include <driver/GenericCanSetupPage.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
//
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
|
||||
SLCANDriver::SLCANDriver(Backend &backend)
|
||||
: CanDriver(backend),
|
||||
setupPage(new GenericCanSetupPage())
|
||||
{
|
||||
QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
|
||||
}
|
||||
|
||||
SLCANDriver::~SLCANDriver() {
|
||||
}
|
||||
|
||||
bool SLCANDriver::update() {
|
||||
|
||||
deleteAllInterfaces();
|
||||
|
||||
int interface_cnt = 0;
|
||||
|
||||
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
|
||||
fprintf(stderr, "Name : %s \r\n", info.portName().toStdString().c_str());
|
||||
fprintf(stderr, " Description : %s \r\n", info.description().toStdString().c_str());
|
||||
fprintf(stderr, " Manufacturer: %s \r\n", info.manufacturer().toStdString().c_str());
|
||||
|
||||
if(info.vendorIdentifier() == 0xad50 && info.productIdentifier() == 0x60C4)
|
||||
{
|
||||
|
||||
perror(" ++ CANable 1.0 or similar ST USB CDC device detected");
|
||||
|
||||
// Create new slcan interface without FD support
|
||||
SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), false);
|
||||
interface_cnt++;
|
||||
}
|
||||
else if(info.vendorIdentifier() == 0x16D0 && info.productIdentifier() == 0x117E)
|
||||
{
|
||||
perror(" ++ CANable 2.0 detected");
|
||||
|
||||
// Create new slcan interface without FD support
|
||||
SLCANInterface *intf = createOrUpdateInterface(interface_cnt, info.portName(), true);
|
||||
interface_cnt++;
|
||||
}
|
||||
else
|
||||
{
|
||||
perror(" !! This is not a CANable device!");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString SLCANDriver::getName() {
|
||||
return "CANable SLCAN";
|
||||
}
|
||||
|
||||
|
||||
|
||||
SLCANInterface *SLCANDriver::createOrUpdateInterface(int index, QString name, bool fd_support) {
|
||||
|
||||
foreach (CanInterface *intf, getInterfaces()) {
|
||||
SLCANInterface *scif = dynamic_cast<SLCANInterface*>(intf);
|
||||
if (scif->getIfIndex() == index) {
|
||||
scif->setName(name);
|
||||
return scif;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SLCANInterface *scif = new SLCANInterface(this, index, name, fd_support);
|
||||
addInterface(scif);
|
||||
return scif;
|
||||
}
|
||||
43
driver/SLCANDriver/SLCANDriver.h
Normal file
43
driver/SLCANDriver/SLCANDriver.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanDriver.h>
|
||||
|
||||
class SLCANInterface;
|
||||
class SetupDialogInterfacePage;
|
||||
class GenericCanSetupPage;
|
||||
|
||||
class SLCANDriver: public CanDriver {
|
||||
public:
|
||||
SLCANDriver(Backend &backend);
|
||||
virtual ~SLCANDriver();
|
||||
|
||||
virtual QString getName();
|
||||
virtual bool update();
|
||||
|
||||
private:
|
||||
SLCANInterface *createOrUpdateInterface(int index, QString name, bool fd_support);
|
||||
GenericCanSetupPage *setupPage;
|
||||
};
|
||||
12
driver/SLCANDriver/SLCANDriver.pri
Normal file
12
driver/SLCANDriver/SLCANDriver.pri
Normal file
@@ -0,0 +1,12 @@
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/SLCANInterface.cpp \
|
||||
$$PWD/SLCANDriver.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/SLCANInterface.h \
|
||||
$$PWD/SLCANDriver.h
|
||||
|
||||
FORMS +=
|
||||
|
||||
742
driver/SLCANDriver/SLCANInterface.cpp
Normal file
742
driver/SLCANDriver/SLCANInterface.cpp
Normal file
@@ -0,0 +1,742 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2022 Ethan Zonca
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "SLCANInterface.h"
|
||||
|
||||
#include <core/Backend.h>
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <core/CanMessage.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QProcess>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QThread>
|
||||
|
||||
SLCANInterface::SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support)
|
||||
: CanInterface((CanDriver *)driver),
|
||||
_idx(index),
|
||||
_isOpen(false),
|
||||
_serport(NULL),
|
||||
_msg_queue(),
|
||||
_name(name),
|
||||
_rx_linbuf_ctr(0),
|
||||
_rxbuf_head(0),
|
||||
_rxbuf_tail(0),
|
||||
_ts_mode(ts_mode_SIOCSHWTSTAMP)
|
||||
{
|
||||
// Set defaults
|
||||
_settings.setBitrate(500000);
|
||||
_settings.setSamplePoint(875);
|
||||
|
||||
_config.supports_canfd = fd_support;
|
||||
}
|
||||
|
||||
SLCANInterface::~SLCANInterface() {
|
||||
}
|
||||
|
||||
QString SLCANInterface::getDetailsStr() const {
|
||||
if(_config.supports_canfd)
|
||||
{
|
||||
return "CANable with CANFD support";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "CANable with standard CAN support";
|
||||
}
|
||||
}
|
||||
|
||||
QString SLCANInterface::getName() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
void SLCANInterface::setName(QString name) {
|
||||
_name = name;
|
||||
}
|
||||
|
||||
QList<CanTiming> SLCANInterface::getAvailableBitrates()
|
||||
{
|
||||
QList<CanTiming> retval;
|
||||
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
|
||||
QList<unsigned> bitrates_fd({0, 2000000, 5000000});
|
||||
|
||||
QList<unsigned> samplePoints({875});
|
||||
|
||||
unsigned i=0;
|
||||
foreach (unsigned br, bitrates) {
|
||||
foreach(unsigned br_fd, bitrates_fd) {
|
||||
foreach (unsigned sp, samplePoints) {
|
||||
retval << CanTiming(i++, br, br_fd, sp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
void SLCANInterface::applyConfig(const MeasurementInterface &mi)
|
||||
{
|
||||
// Save settings for port configuration
|
||||
_settings = mi;
|
||||
}
|
||||
|
||||
bool SLCANInterface::updateStatus()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SLCANInterface::readConfig()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SLCANInterface::readConfigFromLink(rtnl_link *link)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SLCANInterface::supportsTimingConfiguration()
|
||||
{
|
||||
return _config.supports_timing;
|
||||
}
|
||||
|
||||
bool SLCANInterface::supportsCanFD()
|
||||
{
|
||||
return _config.supports_canfd;
|
||||
}
|
||||
|
||||
bool SLCANInterface::supportsTripleSampling()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned SLCANInterface::getBitrate()
|
||||
{
|
||||
return _settings.bitrate();
|
||||
}
|
||||
|
||||
uint32_t SLCANInterface::getCapabilities()
|
||||
{
|
||||
uint32_t retval =
|
||||
CanInterface::capability_config_os |
|
||||
CanInterface::capability_listen_only |
|
||||
CanInterface::capability_auto_restart;
|
||||
|
||||
if (supportsCanFD()) {
|
||||
retval |= CanInterface::capability_canfd;
|
||||
}
|
||||
|
||||
if (supportsTripleSampling()) {
|
||||
retval |= CanInterface::capability_triple_sampling;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool SLCANInterface::updateStatistics()
|
||||
{
|
||||
return updateStatus();
|
||||
}
|
||||
|
||||
uint32_t SLCANInterface::getState()
|
||||
{
|
||||
if(_isOpen)
|
||||
return state_ok;
|
||||
else
|
||||
return state_bus_off;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumRxFrames()
|
||||
{
|
||||
return _status.rx_count;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumRxErrors()
|
||||
{
|
||||
return _status.rx_errors;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumTxFrames()
|
||||
{
|
||||
return _status.tx_count;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumTxErrors()
|
||||
{
|
||||
return _status.tx_errors;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumRxOverruns()
|
||||
{
|
||||
return _status.rx_overruns;
|
||||
}
|
||||
|
||||
int SLCANInterface::getNumTxDropped()
|
||||
{
|
||||
return _status.tx_dropped;
|
||||
}
|
||||
|
||||
int SLCANInterface::getIfIndex() {
|
||||
return _idx;
|
||||
}
|
||||
|
||||
void SLCANInterface::open()
|
||||
{
|
||||
if(_serport != NULL)
|
||||
{
|
||||
delete _serport;
|
||||
}
|
||||
|
||||
_serport = new QSerialPort();
|
||||
|
||||
_serport_mutex.lock();
|
||||
_serport->setPortName(_name);
|
||||
_serport->setBaudRate(1000000);
|
||||
_serport->setDataBits(QSerialPort::Data8);
|
||||
_serport->setParity(QSerialPort::NoParity);
|
||||
_serport->setStopBits(QSerialPort::OneStop);
|
||||
_serport->setFlowControl(QSerialPort::NoFlowControl);
|
||||
_serport->setReadBufferSize(2048);
|
||||
if (_serport->open(QIODevice::ReadWrite)) {
|
||||
//perror("Serport connected!");
|
||||
} else {
|
||||
perror("Serport connect failed!");
|
||||
_serport_mutex.unlock();
|
||||
_isOpen = false;
|
||||
return;
|
||||
}
|
||||
_serport->flush();
|
||||
_serport->clear();
|
||||
|
||||
// Set the classic CAN bitrate
|
||||
switch(_settings.bitrate())
|
||||
{
|
||||
case 1000000:
|
||||
_serport->write("S8\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 750000:
|
||||
_serport->write("S7\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 500000:
|
||||
_serport->write("S6\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 250000:
|
||||
_serport->write("S5\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 125000:
|
||||
_serport->write("S4\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 100000:
|
||||
_serport->write("S3\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 83333:
|
||||
_serport->write("S9\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 50000:
|
||||
_serport->write("S2\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 20000:
|
||||
_serport->write("S1\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 10000:
|
||||
_serport->write("S0\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
default:
|
||||
// Default to 10k
|
||||
_serport->write("S0\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
}
|
||||
|
||||
_serport->waitForBytesWritten(300);
|
||||
|
||||
|
||||
|
||||
// Set configured BRS rate
|
||||
if(_config.supports_canfd)
|
||||
{
|
||||
switch(_settings.fdBitrate())
|
||||
{
|
||||
case 2000000:
|
||||
_serport->write("Y2\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
case 5000000:
|
||||
_serport->write("Y5\r", 3);
|
||||
_serport->flush();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_serport->waitForBytesWritten(300);
|
||||
|
||||
|
||||
// Open the port
|
||||
_serport->write("O\r\n", 3);
|
||||
_serport->flush();
|
||||
|
||||
_isOpen = true;
|
||||
|
||||
// Release port mutex
|
||||
_serport_mutex.unlock();
|
||||
}
|
||||
|
||||
void SLCANInterface::close()
|
||||
{
|
||||
_serport_mutex.lock();
|
||||
|
||||
if (_serport->isOpen())
|
||||
{
|
||||
// Close CAN port
|
||||
_serport->write("C\r", 2);
|
||||
_serport->flush();
|
||||
_serport->waitForBytesWritten(300);
|
||||
_serport->clear();
|
||||
_serport->close();
|
||||
}
|
||||
|
||||
_isOpen = false;
|
||||
_serport_mutex.unlock();
|
||||
}
|
||||
|
||||
bool SLCANInterface::isOpen()
|
||||
{
|
||||
return _isOpen;
|
||||
}
|
||||
|
||||
void SLCANInterface::sendMessage(const CanMessage &msg) {
|
||||
|
||||
// SLCAN_MTU plus null terminator
|
||||
char buf[SLCAN_MTU+1] = {0};
|
||||
|
||||
uint8_t msg_idx = 0;
|
||||
|
||||
// Message is FD
|
||||
// Add character for frame type
|
||||
if(msg.isFD())
|
||||
{
|
||||
if(msg.isBRS())
|
||||
{
|
||||
buf[msg_idx] = 'b';
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[msg_idx] = 'd';
|
||||
}
|
||||
}
|
||||
// Message is not FD
|
||||
// Add character for frame type
|
||||
else
|
||||
{
|
||||
if (msg.isRTR()) {
|
||||
buf[msg_idx] = 'r';
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[msg_idx] = 't';
|
||||
}
|
||||
}
|
||||
|
||||
// Assume standard identifier
|
||||
uint8_t id_len = SLCAN_STD_ID_LEN;
|
||||
uint32_t tmp = msg.getId();
|
||||
|
||||
// Check if extended
|
||||
if (msg.isExtended())
|
||||
{
|
||||
// Convert first char to upper case for extended frame
|
||||
buf[msg_idx] -= 32;
|
||||
id_len = SLCAN_EXT_ID_LEN;
|
||||
}
|
||||
msg_idx++;
|
||||
|
||||
// Add identifier to buffer
|
||||
for(uint8_t j = id_len; j > 0; j--)
|
||||
{
|
||||
// Add nibble to buffer
|
||||
buf[j] = (tmp & 0xF);
|
||||
tmp = tmp >> 4;
|
||||
msg_idx++;
|
||||
}
|
||||
|
||||
// Sanity check length
|
||||
int8_t bytes = msg.getLength();
|
||||
|
||||
|
||||
if(bytes < 0)
|
||||
return;
|
||||
if(bytes > 64)
|
||||
return;
|
||||
|
||||
// If canfd
|
||||
if(bytes > 8)
|
||||
{
|
||||
switch(bytes)
|
||||
{
|
||||
case 12:
|
||||
bytes = 0x9;
|
||||
break;
|
||||
case 16:
|
||||
bytes = 0xA;
|
||||
break;
|
||||
case 20:
|
||||
bytes = 0xB;
|
||||
break;
|
||||
case 24:
|
||||
bytes = 0xC;
|
||||
break;
|
||||
case 32:
|
||||
bytes = 0xD;
|
||||
break;
|
||||
case 48:
|
||||
bytes = 0xE;
|
||||
break;
|
||||
case 64:
|
||||
bytes = 0xF;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Add DLC to buffer
|
||||
buf[msg_idx++] = bytes;
|
||||
|
||||
// Add data bytes
|
||||
for (uint8_t j = 0; j < msg.getLength(); j++)
|
||||
{
|
||||
buf[msg_idx++] = (msg.getByte(j) >> 4);
|
||||
buf[msg_idx++] = (msg.getByte(j) & 0x0F);
|
||||
}
|
||||
|
||||
// Convert to ASCII (2nd character to end)
|
||||
for (uint8_t j = 1; j < msg_idx; j++)
|
||||
{
|
||||
if (buf[j] < 0xA) {
|
||||
buf[j] += 0x30;
|
||||
} else {
|
||||
buf[j] += 0x37;
|
||||
}
|
||||
}
|
||||
|
||||
// Add CR for slcan EOL
|
||||
buf[msg_idx++] = '\r';
|
||||
|
||||
// Ensure null termination
|
||||
buf[msg_idx] = '\0';
|
||||
|
||||
_msg_queue.append(QString(buf));
|
||||
|
||||
}
|
||||
|
||||
bool SLCANInterface::readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms)
|
||||
{
|
||||
// Don't saturate the thread. Read the buffer every 1ms.
|
||||
QThread().msleep(1);
|
||||
|
||||
// Transmit all items that are queued
|
||||
while(!_msg_queue.empty())
|
||||
{
|
||||
// Consume first item
|
||||
QString tmp = _msg_queue.front();
|
||||
_msg_queue.pop_front();
|
||||
|
||||
_serport_mutex.lock();
|
||||
// Write string to serial device
|
||||
_serport->write(tmp.toStdString().c_str(), tmp.length());
|
||||
_serport->flush();
|
||||
_serport->waitForBytesWritten(300);
|
||||
_serport_mutex.unlock();
|
||||
}
|
||||
|
||||
// RX doesn't work on windows unless we call this for some reason
|
||||
_serport->waitForReadyRead(1);
|
||||
|
||||
if(_serport->bytesAvailable())
|
||||
{
|
||||
// This is called when readyRead() is emitted
|
||||
QByteArray datas = _serport->readAll();
|
||||
_rxbuf_mutex.lock();
|
||||
for(int i=0; i<datas.count(); i++)
|
||||
{
|
||||
// If incrementing the head will hit the tail, we've filled the buffer. Reset and discard all data.
|
||||
if(((_rxbuf_head + 1) % RXCIRBUF_LEN) == _rxbuf_tail)
|
||||
{
|
||||
_rxbuf_head = 0;
|
||||
_rxbuf_tail = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Put inbound data at the head locatoin
|
||||
_rxbuf[_rxbuf_head] = datas.at(i);
|
||||
_rxbuf_head = (_rxbuf_head + 1) % RXCIRBUF_LEN; // Wrap at MTU
|
||||
}
|
||||
}
|
||||
_rxbuf_mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////
|
||||
|
||||
bool ret = false;
|
||||
_rxbuf_mutex.lock();
|
||||
while(_rxbuf_tail != _rxbuf_head)
|
||||
{
|
||||
// Save data if room
|
||||
if(_rx_linbuf_ctr < SLCAN_MTU)
|
||||
{
|
||||
_rx_linbuf[_rx_linbuf_ctr++] = _rxbuf[_rxbuf_tail];
|
||||
|
||||
// If we have a newline, then we just finished parsing a CAN message.
|
||||
if(_rxbuf[_rxbuf_tail] == '\r')
|
||||
{
|
||||
CanMessage msg;
|
||||
ret = parseMessage(msg);
|
||||
msglist.append(msg);
|
||||
_rx_linbuf_ctr = 0;
|
||||
}
|
||||
}
|
||||
// Discard data if not
|
||||
else
|
||||
{
|
||||
perror("Linbuf full");
|
||||
_rx_linbuf_ctr = 0;
|
||||
}
|
||||
|
||||
_rxbuf_tail = (_rxbuf_tail + 1) % RXCIRBUF_LEN;
|
||||
}
|
||||
_rxbuf_mutex.unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SLCANInterface::parseMessage(CanMessage &msg)
|
||||
{
|
||||
// Set timestamp to current time
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,NULL);
|
||||
msg.setTimestamp(tv);
|
||||
|
||||
// Defaults
|
||||
msg.setErrorFrame(0);
|
||||
msg.setInterfaceId(getId());
|
||||
msg.setId(0);
|
||||
|
||||
bool msg_is_fd = false;
|
||||
|
||||
// Convert from ASCII (2nd character to end)
|
||||
for (int i = 1; i < _rx_linbuf_ctr; i++)
|
||||
{
|
||||
// Lowercase letters
|
||||
if(_rx_linbuf[i] >= 'a')
|
||||
_rx_linbuf[i] = _rx_linbuf[i] - 'a' + 10;
|
||||
// Uppercase letters
|
||||
else if(_rx_linbuf[i] >= 'A')
|
||||
_rx_linbuf[i] = _rx_linbuf[i] - 'A' + 10;
|
||||
// Numbers
|
||||
else
|
||||
_rx_linbuf[i] = _rx_linbuf[i] - '0';
|
||||
}
|
||||
|
||||
|
||||
// Handle each incoming command
|
||||
switch(_rx_linbuf[0])
|
||||
{
|
||||
|
||||
// Transmit data frame command
|
||||
case 'T':
|
||||
msg.setExtended(1);
|
||||
break;
|
||||
case 't':
|
||||
msg.setExtended(0);
|
||||
break;
|
||||
|
||||
// Transmit remote frame command
|
||||
case 'r':
|
||||
msg.setExtended(0);
|
||||
msg.setRTR(1);
|
||||
break;
|
||||
case 'R':
|
||||
msg.setExtended(1);
|
||||
msg.setRTR(1);
|
||||
break;
|
||||
|
||||
// CANFD transmit - no BRS
|
||||
case 'd':
|
||||
msg.setExtended(0);
|
||||
msg_is_fd = true;
|
||||
break;
|
||||
case 'D':
|
||||
msg.setExtended(1);
|
||||
msg_is_fd = true;
|
||||
break;
|
||||
|
||||
// CANFD transmit - with BRS
|
||||
case 'b':
|
||||
msg.setExtended(0);
|
||||
msg_is_fd = true;
|
||||
break;
|
||||
case 'B':
|
||||
msg.setExtended(1);
|
||||
msg_is_fd = true;
|
||||
break;
|
||||
|
||||
|
||||
|
||||
// Invalid command
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start parsing at second byte (skip command byte)
|
||||
uint8_t parse_loc = 1;
|
||||
|
||||
// Default to standard id len
|
||||
uint8_t id_len = SLCAN_STD_ID_LEN;
|
||||
|
||||
// Update length if message is extended ID
|
||||
if(msg.isExtended())
|
||||
id_len = SLCAN_EXT_ID_LEN;
|
||||
|
||||
uint32_t id_tmp = 0;
|
||||
|
||||
// Iterate through ID bytes
|
||||
while(parse_loc <= id_len)
|
||||
{
|
||||
id_tmp *= 16;
|
||||
id_tmp += _rx_linbuf[parse_loc++];
|
||||
}
|
||||
|
||||
|
||||
msg.setId(id_tmp);
|
||||
|
||||
// Attempt to parse DLC and check sanity
|
||||
uint8_t dlc_code_raw = _rx_linbuf[parse_loc++];
|
||||
|
||||
// If dlc is too long for an FD frame
|
||||
if(msg_is_fd && dlc_code_raw > 0xF)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(!msg_is_fd && dlc_code_raw > 0x8)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(dlc_code_raw > 0x8)
|
||||
{
|
||||
switch(dlc_code_raw)
|
||||
{
|
||||
case 0x9:
|
||||
dlc_code_raw = 12;
|
||||
break;
|
||||
case 0xA:
|
||||
dlc_code_raw = 16;
|
||||
break;
|
||||
case 0xB:
|
||||
dlc_code_raw = 20;
|
||||
break;
|
||||
case 0xC:
|
||||
dlc_code_raw = 24;
|
||||
break;
|
||||
case 0xD:
|
||||
dlc_code_raw = 32;
|
||||
break;
|
||||
case 0xE:
|
||||
dlc_code_raw = 48;
|
||||
break;
|
||||
case 0xF:
|
||||
dlc_code_raw = 64;
|
||||
break;
|
||||
default:
|
||||
dlc_code_raw = 0;
|
||||
perror("Invalid length");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
msg.setLength(dlc_code_raw);
|
||||
|
||||
// Calculate number of bytes we expect in the message
|
||||
int8_t bytes_in_msg = dlc_code_raw;
|
||||
|
||||
if(bytes_in_msg < 0) {
|
||||
perror("Invalid length < 0");
|
||||
return false;
|
||||
}
|
||||
if(bytes_in_msg > 64) {
|
||||
perror("Invalid length > 64");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse data
|
||||
// TODO: Guard against walking off the end of the string!
|
||||
for (uint8_t i = 0; i < bytes_in_msg; i++)
|
||||
{
|
||||
msg.setByte(i, (_rx_linbuf[parse_loc] << 4) + _rx_linbuf[parse_loc+1]);
|
||||
parse_loc += 2;
|
||||
}
|
||||
|
||||
// Reset buffer
|
||||
_rx_linbuf_ctr = 0;
|
||||
_rx_linbuf[0] = '\0';
|
||||
return true;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
// FIXME
|
||||
if (_ts_mode == ts_mode_SIOCSHWTSTAMP) {
|
||||
// TODO implement me
|
||||
_ts_mode = ts_mode_SIOCGSTAMPNS;
|
||||
}
|
||||
|
||||
if (_ts_mode==ts_mode_SIOCGSTAMPNS) {
|
||||
if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) {
|
||||
msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000);
|
||||
} else {
|
||||
_ts_mode = ts_mode_SIOCGSTAMP;
|
||||
}
|
||||
}
|
||||
|
||||
if (_ts_mode==ts_mode_SIOCGSTAMP) {
|
||||
ioctl(_fd, SIOCGSTAMP, &tv_rcv);
|
||||
msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec);
|
||||
}*/
|
||||
|
||||
|
||||
}
|
||||
133
driver/SLCANDriver/SLCANInterface.h
Normal file
133
driver/SLCANDriver/SLCANInterface.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../CanInterface.h"
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QMutex>
|
||||
|
||||
// Maximum rx buffer len
|
||||
#define SLCAN_MTU 138 + 1 + 16 // canfd 64 frame plus \r plus some padding
|
||||
#define SLCAN_STD_ID_LEN 3
|
||||
#define SLCAN_EXT_ID_LEN 8
|
||||
|
||||
#define RXCIRBUF_LEN 8192 // Buffer for received serial data, serviced at 1ms intervals
|
||||
|
||||
class SLCANDriver;
|
||||
|
||||
typedef struct {
|
||||
bool supports_canfd;
|
||||
bool supports_timing;
|
||||
uint32_t state;
|
||||
uint32_t base_freq;
|
||||
uint32_t sample_point;
|
||||
uint32_t ctrl_mode;
|
||||
uint32_t restart_ms;
|
||||
} can_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t can_state;
|
||||
|
||||
uint64_t rx_count;
|
||||
int rx_errors;
|
||||
uint64_t rx_overruns;
|
||||
|
||||
uint64_t tx_count;
|
||||
int tx_errors;
|
||||
uint64_t tx_dropped;
|
||||
} can_status_t;
|
||||
|
||||
class SLCANInterface: public CanInterface {
|
||||
public:
|
||||
SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support);
|
||||
virtual ~SLCANInterface();
|
||||
|
||||
QString getDetailsStr() const;
|
||||
virtual QString getName() const;
|
||||
void setName(QString name);
|
||||
|
||||
virtual QList<CanTiming> getAvailableBitrates();
|
||||
|
||||
virtual void applyConfig(const MeasurementInterface &mi);
|
||||
virtual bool readConfig();
|
||||
virtual bool readConfigFromLink(struct rtnl_link *link);
|
||||
|
||||
bool supportsTimingConfiguration();
|
||||
bool supportsCanFD();
|
||||
bool supportsTripleSampling();
|
||||
|
||||
virtual unsigned getBitrate();
|
||||
virtual uint32_t getCapabilities();
|
||||
|
||||
|
||||
virtual void open();
|
||||
virtual void close();
|
||||
virtual bool isOpen();
|
||||
|
||||
virtual void sendMessage(const CanMessage &msg);
|
||||
virtual bool readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms);
|
||||
|
||||
virtual bool updateStatistics();
|
||||
virtual uint32_t getState();
|
||||
virtual int getNumRxFrames();
|
||||
virtual int getNumRxErrors();
|
||||
virtual int getNumRxOverruns();
|
||||
|
||||
virtual int getNumTxFrames();
|
||||
virtual int getNumTxErrors();
|
||||
virtual int getNumTxDropped();
|
||||
|
||||
int getIfIndex();
|
||||
|
||||
private:
|
||||
typedef enum {
|
||||
ts_mode_SIOCSHWTSTAMP,
|
||||
ts_mode_SIOCGSTAMPNS,
|
||||
ts_mode_SIOCGSTAMP
|
||||
} ts_mode_t;
|
||||
|
||||
int _idx;
|
||||
bool _isOpen;
|
||||
QSerialPort* _serport;
|
||||
QStringList _msg_queue;
|
||||
QMutex _serport_mutex;
|
||||
QString _name;
|
||||
char _rx_linbuf[SLCAN_MTU];
|
||||
int _rx_linbuf_ctr;
|
||||
|
||||
char _rxbuf[RXCIRBUF_LEN];
|
||||
uint32_t _rxbuf_head;
|
||||
uint32_t _rxbuf_tail;
|
||||
|
||||
QMutex _rxbuf_mutex;
|
||||
MeasurementInterface _settings;
|
||||
|
||||
can_config_t _config;
|
||||
can_status_t _status;
|
||||
ts_mode_t _ts_mode;
|
||||
|
||||
bool updateStatus();
|
||||
bool parseMessage(CanMessage &msg);
|
||||
|
||||
};
|
||||
99
driver/SocketCanDriver/SocketCanDriver.cpp
Normal file
99
driver/SocketCanDriver/SocketCanDriver.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include "SocketCanDriver.h"
|
||||
#include "SocketCanInterface.h"
|
||||
#include <core/Backend.h>
|
||||
#include <driver/GenericCanSetupPage.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_arp.h>
|
||||
#include <linux/can/netlink.h>
|
||||
#include <netlink/route/link.h>
|
||||
#include <netlink/route/link/can.h>
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
SocketCanDriver::SocketCanDriver(Backend &backend)
|
||||
: CanDriver(backend),
|
||||
setupPage(new GenericCanSetupPage())
|
||||
{
|
||||
QObject::connect(&backend, SIGNAL(onSetupDialogCreated(SetupDialog&)), setupPage, SLOT(onSetupDialogCreated(SetupDialog&)));
|
||||
}
|
||||
|
||||
SocketCanDriver::~SocketCanDriver() {
|
||||
}
|
||||
|
||||
bool SocketCanDriver::update() {
|
||||
|
||||
deleteAllInterfaces();
|
||||
|
||||
struct nl_sock *sock = nl_socket_alloc();
|
||||
struct nl_cache *cache;
|
||||
|
||||
nl_connect(sock, NETLINK_ROUTE);
|
||||
int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache);
|
||||
|
||||
if (result < 0) {
|
||||
log_error(QString("Could not access netlink device list: %1").arg(result));
|
||||
} else {
|
||||
|
||||
for (struct nl_object *obj = nl_cache_get_first(cache); obj!=0; obj=nl_cache_get_next(obj)) {
|
||||
struct rtnl_link *link = (struct rtnl_link *)obj;
|
||||
|
||||
if (rtnl_link_get_arptype(link)==ARPHRD_CAN) {
|
||||
SocketCanInterface *intf = createOrUpdateInterface(rtnl_link_get_ifindex(link), QString(rtnl_link_get_name(link)));
|
||||
intf->readConfigFromLink(link);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nl_cache_free(cache);
|
||||
nl_close(sock);
|
||||
nl_socket_free(sock);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString SocketCanDriver::getName() {
|
||||
return "SocketCAN";
|
||||
}
|
||||
|
||||
SocketCanInterface *SocketCanDriver::createOrUpdateInterface(int index, QString name) {
|
||||
|
||||
foreach (CanInterface *intf, getInterfaces()) {
|
||||
SocketCanInterface *scif = dynamic_cast<SocketCanInterface*>(intf);
|
||||
if (scif->getIfIndex() == index) {
|
||||
scif->setName(name);
|
||||
return scif;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SocketCanInterface *scif = new SocketCanInterface(this, index, name);
|
||||
addInterface(scif);
|
||||
return scif;
|
||||
}
|
||||
43
driver/SocketCanDriver/SocketCanDriver.h
Normal file
43
driver/SocketCanDriver/SocketCanDriver.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <core/Backend.h>
|
||||
#include <driver/CanDriver.h>
|
||||
|
||||
class SocketCanInterface;
|
||||
class SetupDialogInterfacePage;
|
||||
class GenericCanSetupPage;
|
||||
|
||||
class SocketCanDriver: public CanDriver {
|
||||
public:
|
||||
SocketCanDriver(Backend &backend);
|
||||
virtual ~SocketCanDriver();
|
||||
|
||||
virtual QString getName();
|
||||
virtual bool update();
|
||||
|
||||
private:
|
||||
SocketCanInterface *createOrUpdateInterface(int index, QString name);
|
||||
GenericCanSetupPage *setupPage;
|
||||
};
|
||||
12
driver/SocketCanDriver/SocketCanDriver.pri
Normal file
12
driver/SocketCanDriver/SocketCanDriver.pri
Normal file
@@ -0,0 +1,12 @@
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/SocketCanInterface.cpp \
|
||||
$$PWD/SocketCanDriver.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/SocketCanInterface.h \
|
||||
$$PWD/SocketCanDriver.h
|
||||
|
||||
FORMS +=
|
||||
|
||||
472
driver/SocketCanDriver/SocketCanInterface.cpp
Normal file
472
driver/SocketCanDriver/SocketCanInterface.cpp
Normal file
@@ -0,0 +1,472 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "SocketCanInterface.h"
|
||||
|
||||
#include <core/Backend.h>
|
||||
#include <core/MeasurementInterface.h>
|
||||
#include <core/CanMessage.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QProcess>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <linux/if.h>
|
||||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
#include <linux/can/netlink.h>
|
||||
#include <linux/sockios.h>
|
||||
#include <netlink/version.h>
|
||||
#include <netlink/route/link.h>
|
||||
#include <netlink/route/link/can.h>
|
||||
|
||||
SocketCanInterface::SocketCanInterface(SocketCanDriver *driver, int index, QString name)
|
||||
: CanInterface((CanDriver *)driver),
|
||||
_idx(index),
|
||||
_isOpen(false),
|
||||
_fd(0),
|
||||
_name(name),
|
||||
_ts_mode(ts_mode_SIOCSHWTSTAMP)
|
||||
{
|
||||
}
|
||||
|
||||
SocketCanInterface::~SocketCanInterface() {
|
||||
}
|
||||
|
||||
QString SocketCanInterface::getName() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
void SocketCanInterface::setName(QString name) {
|
||||
_name = name;
|
||||
}
|
||||
|
||||
QList<CanTiming> SocketCanInterface::getAvailableBitrates()
|
||||
{
|
||||
QList<CanTiming> retval;
|
||||
QList<unsigned> bitrates({10000, 20000, 50000, 83333, 100000, 125000, 250000, 500000, 800000, 1000000});
|
||||
QList<unsigned> samplePoints({500, 625, 750, 875});
|
||||
|
||||
unsigned i=0;
|
||||
foreach (unsigned br, bitrates) {
|
||||
foreach (unsigned sp, samplePoints) {
|
||||
retval << CanTiming(i++, br, 0, sp);
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
QString SocketCanInterface::buildIpRouteCmd(const MeasurementInterface &mi)
|
||||
{
|
||||
QStringList cmd;
|
||||
cmd.append("ip");
|
||||
cmd.append("link");
|
||||
cmd.append("set");
|
||||
cmd.append(getName());
|
||||
cmd.append("up");
|
||||
cmd.append("type");
|
||||
cmd.append("can");
|
||||
|
||||
cmd.append("bitrate");
|
||||
cmd.append(QString().number(mi.bitrate()));
|
||||
cmd.append("sample-point");
|
||||
cmd.append(QString().number((float)mi.samplePoint()/1000.0, 'f', 3));
|
||||
|
||||
if (mi.isCanFD()) {
|
||||
cmd.append("dbitrate");
|
||||
cmd.append(QString().number(mi.fdBitrate()));
|
||||
cmd.append("dsample-point");
|
||||
cmd.append(QString().number((float)mi.fdSamplePoint()/1000.0, 'f', 3));
|
||||
cmd.append("fd");
|
||||
cmd.append("on");
|
||||
}
|
||||
|
||||
cmd.append("restart-ms");
|
||||
if (mi.doAutoRestart()) {
|
||||
cmd.append(QString().number(mi.autoRestartMs()));
|
||||
} else {
|
||||
cmd.append("0");
|
||||
}
|
||||
|
||||
return cmd.join(' ');
|
||||
}
|
||||
|
||||
QStringList SocketCanInterface::buildCanIfConfigArgs(const MeasurementInterface &mi)
|
||||
{
|
||||
QStringList args;
|
||||
args << "-d";
|
||||
args << "-i" << getName();
|
||||
args << "-b" << QString::number(mi.bitrate());
|
||||
args << "-p" << QString::number(mi.samplePoint());
|
||||
args << "-u";
|
||||
return args;
|
||||
}
|
||||
|
||||
|
||||
void SocketCanInterface::applyConfig(const MeasurementInterface &mi)
|
||||
{
|
||||
if (!mi.doConfigure()) {
|
||||
log_info(QString("interface %1 not managed by cangaroo, not touching configuration").arg(getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
log_info(QString("calling canifconfig to reconfigure interface %1").arg(getName()));
|
||||
QStringList sl = buildCanIfConfigArgs(mi);
|
||||
sl.prepend("canifconfig");
|
||||
log_info(sl.join(" "));
|
||||
|
||||
QProcess canIfConfig;
|
||||
canIfConfig.start("canifconfig", buildCanIfConfigArgs(mi));
|
||||
if (!canIfConfig.waitForFinished()) {
|
||||
log_error(QString("timeout waiting for canifconfig"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (canIfConfig.exitStatus()!=QProcess::NormalExit) {
|
||||
log_error(QString("canifconfig crashed"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (canIfConfig.exitCode() != 0) {
|
||||
log_error(QString("canifconfig failed: ") + QString(canIfConfig.readAllStandardError()).trimmed());
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if (LIBNL_CURRENT<=216)
|
||||
#warning we need at least libnl3 version 3.2.22 to be able to get link status via netlink
|
||||
int rtnl_link_can_state(struct rtnl_link *link, uint32_t *state) {
|
||||
(void) link;
|
||||
(void) state;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool SocketCanInterface::updateStatus()
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
struct nl_sock *sock = nl_socket_alloc();
|
||||
struct nl_cache *cache;
|
||||
struct rtnl_link *link;
|
||||
uint32_t state;
|
||||
|
||||
_status.can_state = state_unknown;
|
||||
|
||||
nl_connect(sock, NETLINK_ROUTE);
|
||||
if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache) >= 0) {
|
||||
if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) {
|
||||
|
||||
_status.rx_count = rtnl_link_get_stat(link, RTNL_LINK_RX_PACKETS);
|
||||
_status.rx_overruns = rtnl_link_get_stat(link, RTNL_LINK_RX_OVER_ERR);
|
||||
_status.tx_count = rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS);
|
||||
_status.tx_dropped = rtnl_link_get_stat(link, RTNL_LINK_TX_DROPPED);
|
||||
|
||||
if (rtnl_link_is_can(link)) {
|
||||
if (rtnl_link_can_state(link, &state)==0) {
|
||||
_status.can_state = state;
|
||||
}
|
||||
_status.rx_errors = rtnl_link_can_berr_rx(link);
|
||||
_status.tx_errors = rtnl_link_can_berr_tx(link);
|
||||
} else {
|
||||
_status.rx_errors = 0;
|
||||
_status.tx_errors = 0;
|
||||
}
|
||||
retval = true;
|
||||
}
|
||||
}
|
||||
|
||||
nl_cache_free(cache);
|
||||
nl_close(sock);
|
||||
nl_socket_free(sock);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::readConfig()
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
struct nl_sock *sock = nl_socket_alloc();
|
||||
struct nl_cache *cache;
|
||||
struct rtnl_link *link;
|
||||
|
||||
nl_connect(sock, NETLINK_ROUTE);
|
||||
int result = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache);
|
||||
|
||||
if (result>=0) {
|
||||
if (rtnl_link_get_kernel(sock, _idx, 0, &link) == 0) {
|
||||
retval = readConfigFromLink(link);
|
||||
}
|
||||
}
|
||||
|
||||
nl_cache_free(cache);
|
||||
nl_close(sock);
|
||||
nl_socket_free(sock);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::readConfigFromLink(rtnl_link *link)
|
||||
{
|
||||
_config.state = state_unknown;
|
||||
_config.supports_canfd = (rtnl_link_get_mtu(link)==72);
|
||||
_config.supports_timing = rtnl_link_is_can(link);
|
||||
if (_config.supports_timing) {
|
||||
rtnl_link_can_freq(link, &_config.base_freq);
|
||||
rtnl_link_can_get_ctrlmode(link, &_config.ctrl_mode);
|
||||
rtnl_link_can_get_bittiming(link, &_config.bit_timing);
|
||||
rtnl_link_can_get_sample_point(link, &_config.sample_point);
|
||||
rtnl_link_can_get_restart_ms(link, &_config.restart_ms);
|
||||
} else {
|
||||
// maybe a vcan interface?
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::supportsTimingConfiguration()
|
||||
{
|
||||
return _config.supports_timing;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::supportsCanFD()
|
||||
{
|
||||
return _config.supports_canfd;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::supportsTripleSampling()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned SocketCanInterface::getBitrate() {
|
||||
if (readConfig()) {
|
||||
return _config.bit_timing.bitrate;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SocketCanInterface::getCapabilities()
|
||||
{
|
||||
uint32_t retval =
|
||||
CanInterface::capability_config_os |
|
||||
CanInterface::capability_listen_only |
|
||||
CanInterface::capability_auto_restart;
|
||||
|
||||
if (supportsCanFD()) {
|
||||
retval |= CanInterface::capability_canfd;
|
||||
}
|
||||
|
||||
if (supportsTripleSampling()) {
|
||||
retval |= CanInterface::capability_triple_sampling;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::updateStatistics()
|
||||
{
|
||||
return updateStatus();
|
||||
}
|
||||
|
||||
uint32_t SocketCanInterface::getState()
|
||||
{
|
||||
switch (_status.can_state) {
|
||||
case CAN_STATE_ERROR_ACTIVE: return state_ok;
|
||||
case CAN_STATE_ERROR_WARNING: return state_warning;
|
||||
case CAN_STATE_ERROR_PASSIVE: return state_passive;
|
||||
case CAN_STATE_BUS_OFF: return state_bus_off;
|
||||
case CAN_STATE_STOPPED: return state_stopped;
|
||||
default: return state_unknown;
|
||||
}
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumRxFrames()
|
||||
{
|
||||
return _status.rx_count;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumRxErrors()
|
||||
{
|
||||
return _status.rx_errors;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumTxFrames()
|
||||
{
|
||||
return _status.tx_count;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumTxErrors()
|
||||
{
|
||||
return _status.tx_errors;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumRxOverruns()
|
||||
{
|
||||
return _status.rx_overruns;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getNumTxDropped()
|
||||
{
|
||||
return _status.tx_dropped;
|
||||
}
|
||||
|
||||
int SocketCanInterface::getIfIndex() {
|
||||
return _idx;
|
||||
}
|
||||
|
||||
const char *SocketCanInterface::cname()
|
||||
{
|
||||
return _name.toStdString().c_str();
|
||||
}
|
||||
|
||||
void SocketCanInterface::open() {
|
||||
if((_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||
perror("Error while opening socket");
|
||||
_isOpen = false;
|
||||
}
|
||||
|
||||
struct ifreq ifr;
|
||||
struct sockaddr_can addr;
|
||||
strcpy(ifr.ifr_name, _name.toStdString().c_str());
|
||||
ioctl(_fd, SIOCGIFINDEX, &ifr);
|
||||
|
||||
addr.can_family = AF_CAN;
|
||||
addr.can_ifindex = ifr.ifr_ifindex;
|
||||
|
||||
if(bind(_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
perror("Error in socket bind");
|
||||
_isOpen = false;
|
||||
}
|
||||
|
||||
_isOpen = true;
|
||||
}
|
||||
|
||||
bool SocketCanInterface::isOpen()
|
||||
{
|
||||
return _isOpen;
|
||||
}
|
||||
|
||||
void SocketCanInterface::close() {
|
||||
::close(_fd);
|
||||
_isOpen = false;
|
||||
}
|
||||
|
||||
void SocketCanInterface::sendMessage(const CanMessage &msg) {
|
||||
struct can_frame frame;
|
||||
|
||||
frame.can_id = msg.getId();
|
||||
|
||||
if (msg.isExtended()) {
|
||||
frame.can_id |= CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
if (msg.isRTR()) {
|
||||
frame.can_id |= CAN_RTR_FLAG;
|
||||
}
|
||||
|
||||
if (msg.isErrorFrame()) {
|
||||
frame.can_id |= CAN_ERR_FLAG;
|
||||
}
|
||||
|
||||
uint8_t len = msg.getLength();
|
||||
if (len>8) { len = 8; }
|
||||
|
||||
frame.can_dlc = len;
|
||||
for (int i=0; i<len; i++) {
|
||||
frame.data[i] = msg.getByte(i);
|
||||
}
|
||||
|
||||
::write(_fd, &frame, sizeof(struct can_frame));
|
||||
}
|
||||
|
||||
bool SocketCanInterface::readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms) {
|
||||
|
||||
struct can_frame frame;
|
||||
struct timespec ts_rcv;
|
||||
struct timeval tv_rcv;
|
||||
struct timeval timeout;
|
||||
//struct ifreq hwtstamp;
|
||||
fd_set fdset;
|
||||
|
||||
timeout.tv_sec = timeout_ms / 1000;
|
||||
timeout.tv_usec = 1000 * (timeout_ms % 1000);
|
||||
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(_fd, &fdset);
|
||||
|
||||
CanMessage msg;
|
||||
|
||||
int rv = select(_fd+1, &fdset, NULL, NULL, &timeout);
|
||||
if (rv>0) {
|
||||
|
||||
if (read(_fd, &frame, sizeof(struct can_frame)) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_ts_mode == ts_mode_SIOCSHWTSTAMP) {
|
||||
// TODO implement me
|
||||
_ts_mode = ts_mode_SIOCGSTAMPNS;
|
||||
}
|
||||
|
||||
if (_ts_mode==ts_mode_SIOCGSTAMPNS) {
|
||||
if (ioctl(_fd, SIOCGSTAMPNS, &ts_rcv) == 0) {
|
||||
msg.setTimestamp(ts_rcv.tv_sec, ts_rcv.tv_nsec/1000);
|
||||
} else {
|
||||
_ts_mode = ts_mode_SIOCGSTAMP;
|
||||
}
|
||||
}
|
||||
|
||||
if (_ts_mode==ts_mode_SIOCGSTAMP) {
|
||||
ioctl(_fd, SIOCGSTAMP, &tv_rcv);
|
||||
msg.setTimestamp(tv_rcv.tv_sec, tv_rcv.tv_usec);
|
||||
}
|
||||
|
||||
msg.setId(frame.can_id);
|
||||
msg.setExtended((frame.can_id & CAN_EFF_FLAG)!=0);
|
||||
msg.setRTR((frame.can_id & CAN_RTR_FLAG)!=0);
|
||||
msg.setErrorFrame((frame.can_id & CAN_ERR_FLAG)!=0);
|
||||
msg.setInterfaceId(getId());
|
||||
|
||||
uint8_t len = frame.can_dlc;
|
||||
if (len>8) { len = 8; }
|
||||
|
||||
msg.setLength(len);
|
||||
for (int i=0; i<len; i++) {
|
||||
msg.setByte(i, frame.data[i]);
|
||||
}
|
||||
|
||||
msglist.append(msg);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
115
driver/SocketCanDriver/SocketCanInterface.h
Normal file
115
driver/SocketCanDriver/SocketCanInterface.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2015, 2016 Hubert Denkmair
|
||||
|
||||
This file is part of cangaroo.
|
||||
|
||||
cangaroo is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cangaroo is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../CanInterface.h"
|
||||
#include <linux/can/netlink.h>
|
||||
|
||||
class SocketCanDriver;
|
||||
|
||||
typedef struct {
|
||||
bool supports_canfd;
|
||||
bool supports_timing;
|
||||
uint32_t state;
|
||||
uint32_t base_freq;
|
||||
uint32_t sample_point;
|
||||
uint32_t ctrl_mode;
|
||||
uint32_t restart_ms;
|
||||
struct can_bittiming bit_timing;
|
||||
} can_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t can_state;
|
||||
|
||||
uint64_t rx_count;
|
||||
int rx_errors;
|
||||
uint64_t rx_overruns;
|
||||
|
||||
uint64_t tx_count;
|
||||
int tx_errors;
|
||||
uint64_t tx_dropped;
|
||||
} can_status_t;
|
||||
|
||||
class SocketCanInterface: public CanInterface {
|
||||
public:
|
||||
SocketCanInterface(SocketCanDriver *driver, int index, QString name);
|
||||
virtual ~SocketCanInterface();
|
||||
|
||||
virtual QString getName() const;
|
||||
void setName(QString name);
|
||||
|
||||
virtual QList<CanTiming> getAvailableBitrates();
|
||||
|
||||
virtual void applyConfig(const MeasurementInterface &mi);
|
||||
virtual bool readConfig();
|
||||
virtual bool readConfigFromLink(struct rtnl_link *link);
|
||||
|
||||
bool supportsTimingConfiguration();
|
||||
bool supportsCanFD();
|
||||
bool supportsTripleSampling();
|
||||
|
||||
virtual unsigned getBitrate();
|
||||
virtual uint32_t getCapabilities();
|
||||
|
||||
|
||||
virtual void open();
|
||||
virtual bool isOpen();
|
||||
virtual void close();
|
||||
|
||||
virtual void sendMessage(const CanMessage &msg);
|
||||
virtual bool readMessage(QList<CanMessage> &msglist, unsigned int timeout_ms);
|
||||
|
||||
virtual bool updateStatistics();
|
||||
virtual uint32_t getState();
|
||||
virtual int getNumRxFrames();
|
||||
virtual int getNumRxErrors();
|
||||
virtual int getNumRxOverruns();
|
||||
|
||||
virtual int getNumTxFrames();
|
||||
virtual int getNumTxErrors();
|
||||
virtual int getNumTxDropped();
|
||||
|
||||
|
||||
int getIfIndex();
|
||||
|
||||
private:
|
||||
typedef enum {
|
||||
ts_mode_SIOCSHWTSTAMP,
|
||||
ts_mode_SIOCGSTAMPNS,
|
||||
ts_mode_SIOCGSTAMP
|
||||
} ts_mode_t;
|
||||
|
||||
int _idx;
|
||||
bool _isOpen;
|
||||
int _fd;
|
||||
QString _name;
|
||||
|
||||
can_config_t _config;
|
||||
can_status_t _status;
|
||||
ts_mode_t _ts_mode;
|
||||
|
||||
const char *cname();
|
||||
bool updateStatus();
|
||||
|
||||
QString buildIpRouteCmd(const MeasurementInterface &mi);
|
||||
QStringList buildCanIfConfigArgs(const MeasurementInterface &mi);
|
||||
};
|
||||
16
driver/driver.pri
Normal file
16
driver/driver.pri
Normal file
@@ -0,0 +1,16 @@
|
||||
SOURCES += \
|
||||
$$PWD/CanInterface.cpp \
|
||||
$$PWD/CanListener.cpp \
|
||||
$$PWD/CanDriver.cpp \
|
||||
$$PWD/CanTiming.cpp \
|
||||
$$PWD/GenericCanSetupPage.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/CanInterface.h \
|
||||
$$PWD/CanListener.h \
|
||||
$$PWD/CanDriver.h \
|
||||
$$PWD/CanTiming.h \
|
||||
$$PWD/GenericCanSetupPage.h
|
||||
|
||||
FORMS += \
|
||||
$$PWD/GenericCanSetupPage.ui
|
||||
Reference in New Issue
Block a user