Permalink
Browse files

use canifconfig to configure can bitrates & sample point, take interf…

…aces up
  • Loading branch information...
HubertD committed May 22, 2016
1 parent ca95b34 commit 237252eaced43c90624140fbbd74d73d87ad7734
View
@@ -63,10 +63,10 @@ void print_usage(char *program_name)
" -d bring interface down before configuration\n"
" -u bring interface up after configuration\n"
" -b <n> set bitrate to n bits per second\n"
" -p <n> set sample point to n/1000, e.g. 875 for 87.5%\n"
" -p <n> set sample point to n/1000, e.g. 875 for 87.5%%\n"
" -f <on|off> enable/disable CanFD\n"
" -B <n> set fd bitrate to n bits per second\n"
" -P <n> set fd sample point to n/1000, e.g. 875 for 87.5%\n"
" -P <n> set fd sample point to n/1000, e.g. 875 for 87.5%%\n"
" -r <n> set auto restart time to n milliseconds, 0 for disable\n"
" -l <on|off> enable/disable listen-only mode\n"
" -o <on|off> enable/disable one-shot mode\n"
@@ -30,6 +30,7 @@
#include <time.h>
#include <QString>
#include <QStringList>
#include <QProcess>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
@@ -79,52 +80,75 @@ QList<CanTiming> SocketCanInterface::getAvailableBitrates()
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;
}
if (getBitrate() == mi.bitrate()) { // TODO carefully compare active configuration with the desired one
log_info(QString("interface %1 already configured correctly, not touching configuration").arg(getName()));
} else {
log_info(QString("taking down interface %1 for configuration").arg(getName()));
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");
}
log_info(QString("calling canifconfig to reconfigure interface %1").arg(getName()));
cmd.append("restart-ms");
if (mi.doAutoRestart()) {
cmd.append(QString().number(mi.autoRestartMs()));
} else {
cmd.append("0");
}
QProcess canIfConfig;
canIfConfig.start("canifconfig", buildCanIfConfigArgs(mi));
if (!canIfConfig.waitForFinished()) {
log_error(QString("timeout waiting for canifconfig"));
return;
}
log_info(cmd.join(' '));
if (canIfConfig.exitStatus() != 0) {
log_error(QString("canifconfig failed:"));
log_error(canIfConfig.readAllStandardError());
return;
}
}
#if (LIBNL_CURRENT<=216)
@@ -177,7 +201,6 @@ bool SocketCanInterface::updateStatus()
return retval;
}
bool SocketCanInterface::readConfig()
{
bool retval = false;
@@ -107,4 +107,7 @@ class SocketCanInterface: public CanInterface {
const char *cname();
bool updateStatus();
QString buildIpRouteCmd(const MeasurementInterface &mi);
QStringList buildCanIfConfigArgs(const MeasurementInterface &mi);
};

0 comments on commit 237252e

Please sign in to comment.