|
|
|
|
@ -20,26 +20,33 @@
|
|
|
|
|
|
|
|
|
|
#include <icsneo/icsneocpp.h>
|
|
|
|
|
#include <icsneo/communication/message/neomessage.h>
|
|
|
|
|
#include <icsneo/communication/message/message.h>
|
|
|
|
|
#include <icsneo/communication/network.h>
|
|
|
|
|
#include <icsneo/communication/message/callback/canmessagecallback.h>
|
|
|
|
|
#include <generated/buildinfo.h>
|
|
|
|
|
|
|
|
|
|
#define LOG(LVL, MSG) do{if(runningAsDaemon) syslog(LVL, MSG); \
|
|
|
|
|
else fprintf(stderr, MSG);}while(0)
|
|
|
|
|
#define LOGF(LVL, MSG, ...) do{if(runningAsDaemon) syslog(LVL, MSG, __VA_ARGS__); \
|
|
|
|
|
else fprintf(stderr, MSG, __VA_ARGS__);}while(0)
|
|
|
|
|
#define LOG(LVL, MSG) do{if(runningAsDaemon) syslog(LVL, MSG); \
|
|
|
|
|
else fprintf(stderr, MSG);}while(0)
|
|
|
|
|
#define LOGF(LVL, MSG, ...) do{if(runningAsDaemon) syslog(LVL, MSG, __VA_ARGS__); \
|
|
|
|
|
else fprintf(stderr, MSG, __VA_ARGS__);}while(0)
|
|
|
|
|
|
|
|
|
|
#define SIOCSADDIF 0x3001
|
|
|
|
|
#define SIOCSREMOVEIF 0x3002
|
|
|
|
|
#define SIOCGSHAREDMEMSIZE 0x3003
|
|
|
|
|
#define SIOCSMSGSWRITTEN 0x3004
|
|
|
|
|
#define SIOCGMAXIFACES 0x3005
|
|
|
|
|
#define SIOCGVERSION 0x3006
|
|
|
|
|
#define SIOCGCLIENTVEROK 0x3007
|
|
|
|
|
#define SIOCSADDCANIF 0x3001
|
|
|
|
|
#define SIOCSADDETHIF 0x3002
|
|
|
|
|
#define SIOCSREMOVECANIF 0x3003
|
|
|
|
|
#define SIOCSREMOVEETHIF 0x3004
|
|
|
|
|
#define SIOCGSHAREDMEMSIZE 0x3005
|
|
|
|
|
#define SIOCSMSGSWRITTEN 0x3006
|
|
|
|
|
#define SIOCGMAXIFACES 0x3007
|
|
|
|
|
#define SIOCGVERSION 0x3008
|
|
|
|
|
#define SIOCGCLIENTVEROK 0x3009
|
|
|
|
|
#define SIOCSBAUDRATE 0x300A
|
|
|
|
|
|
|
|
|
|
#define RX_BOX_SIZE (sharedMemSize / (maxInterfaces * 2))
|
|
|
|
|
#define TX_BOX_SIZE (sharedMemSize / 4)
|
|
|
|
|
#define GET_RX_BOX(DEVICE_INDEX) (reinterpret_cast<uint8_t*>(sharedMemory) + (RX_BOX_SIZE * DEVICE_INDEX))
|
|
|
|
|
#define GET_TX_BOX(INDEX) (reinterpret_cast<uint8_t*>(sharedMemory) + (sharedMemSize / 2) + (INDEX * TX_BOX_SIZE))
|
|
|
|
|
#define RX_BOX_SIZE (sharedMemSize / (maxInterfaces * 2))
|
|
|
|
|
#define TX_BOX_SIZE (sharedMemSize / 4)
|
|
|
|
|
#define GET_RX_BOX(DEVICE_INDEX) (reinterpret_cast<uint8_t*>(sharedMemory) + (RX_BOX_SIZE * DEVICE_INDEX))
|
|
|
|
|
#define GET_TX_BOX(INDEX) (reinterpret_cast<uint8_t*>(sharedMemory) + (sharedMemSize / 2) + (INDEX * TX_BOX_SIZE))
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_SCAN_INTERVAL_MS 1000
|
|
|
|
|
|
|
|
|
|
bool runningAsDaemon = false;
|
|
|
|
|
int driver = 0; // /dev/intrepid_netdevice
|
|
|
|
|
@ -49,6 +56,8 @@ int driverPatch = 0;
|
|
|
|
|
int maxInterfaces = 0; // From driver
|
|
|
|
|
int sharedMemSize = 0; // From driver
|
|
|
|
|
void* sharedMemory = nullptr;
|
|
|
|
|
std::string serialFilter;
|
|
|
|
|
int scanIntervalMs = DEFAULT_SCAN_INTERVAL_MS;
|
|
|
|
|
|
|
|
|
|
std::atomic<bool> stopRunning(false);
|
|
|
|
|
|
|
|
|
|
@ -60,10 +69,16 @@ struct intrepid_pending_tx_info {
|
|
|
|
|
|
|
|
|
|
class NetworkInterface {
|
|
|
|
|
public:
|
|
|
|
|
NetworkInterface(const std::string& desiredName) : name(desiredName) {
|
|
|
|
|
NetworkInterface(const std::string& desiredName, icsneo::Network::Type device) : type(device), name(desiredName) {
|
|
|
|
|
char ifname[IFALIASZ + 1] = {0};
|
|
|
|
|
strncpy(ifname, name.c_str(), IFALIASZ);
|
|
|
|
|
kernelHandle = ioctl(driver, SIOCSADDIF, ifname);
|
|
|
|
|
|
|
|
|
|
if(device == icsneo::Network::Type::CAN) {
|
|
|
|
|
kernelHandle = ioctl(driver, SIOCSADDCANIF, ifname); // this will call the intrepid_dev_ioctl()
|
|
|
|
|
} else if(device == icsneo::Network::Type::Ethernet) {
|
|
|
|
|
kernelHandle = ioctl(driver, SIOCSADDETHIF, ifname); // this will call the intrepid_dev_ioctl()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(openedSuccessfully()) {
|
|
|
|
|
rxBox = GET_RX_BOX(kernelHandle);
|
|
|
|
|
rxBoxCurrentPosition = rxBox;
|
|
|
|
|
@ -71,12 +86,36 @@ public:
|
|
|
|
|
}
|
|
|
|
|
~NetworkInterface() {
|
|
|
|
|
if(openedSuccessfully()) {
|
|
|
|
|
int res = 0;
|
|
|
|
|
LOGF(LOG_DEBUG, "Removing device %s with handle %d\n", name.c_str(), kernelHandle);
|
|
|
|
|
int res = ioctl(driver, SIOCSREMOVEIF, kernelHandle);
|
|
|
|
|
if(type == icsneo::Network::Type::CAN) {
|
|
|
|
|
res = ioctl(driver, SIOCSREMOVECANIF, kernelHandle);
|
|
|
|
|
} else if(type == icsneo::Network::Type::Ethernet) {
|
|
|
|
|
res = ioctl(driver, SIOCSREMOVEETHIF, kernelHandle);
|
|
|
|
|
}
|
|
|
|
|
LOGF(LOG_DEBUG, "Removed device %s with handle %d, result %d\n", name.c_str(), kernelHandle, res);
|
|
|
|
|
} else
|
|
|
|
|
LOG(LOG_DEBUG, "Removing interface which was not opened successfully\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool reportBaudrates(int64_t baudrate, int64_t fd_baudrate) {
|
|
|
|
|
struct baudrate_info {
|
|
|
|
|
int handle;
|
|
|
|
|
int64_t baudrates[2];
|
|
|
|
|
} info;
|
|
|
|
|
|
|
|
|
|
info.handle = kernelHandle;
|
|
|
|
|
info.baudrates[0] = baudrate;
|
|
|
|
|
/* set fd baudrate to zero if equal to baudrate
|
|
|
|
|
* this will disable fd mode in kernel */
|
|
|
|
|
info.baudrates[1] = (fd_baudrate==baudrate)?0:fd_baudrate;
|
|
|
|
|
if (ioctl(driver, SIOCSBAUDRATE, &info) != 0) {
|
|
|
|
|
LOGF(LOG_INFO, "Unable to set baudrate for device %s\n", name.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
NetworkInterface(const NetworkInterface&) = delete;
|
|
|
|
|
NetworkInterface& operator =(const NetworkInterface&) = delete;
|
|
|
|
|
|
|
|
|
|
@ -85,29 +124,42 @@ public:
|
|
|
|
|
const std::string& getName() const { return name; }
|
|
|
|
|
uint8_t* getRxBox() { return rxBox; }
|
|
|
|
|
const uint8_t* getRxBox() const { return rxBox; }
|
|
|
|
|
void addReceivedMessageToQueue(const std::shared_ptr<icsneo::Message>& msg) {
|
|
|
|
|
auto neomessage = icsneo::CreateNeoMessage(msg);
|
|
|
|
|
size_t bytesNeeded = sizeof(neomessage) + neomessage.length;
|
|
|
|
|
std::lock_guard<std::mutex> lg(rxBoxLock);
|
|
|
|
|
if(ssize_t((rxBoxCurrentPosition - rxBox) + bytesNeeded) > RX_BOX_SIZE) {
|
|
|
|
|
// fail, too big!
|
|
|
|
|
LOG(LOG_DEBUG, "box too small\n");
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void addReceivedMessageToQueue(const std::shared_ptr<icsneo::Frame>& msg) {
|
|
|
|
|
const auto neomessageGeneric = icsneo::CreateNeoMessage(msg);
|
|
|
|
|
if(neomessageGeneric.messageType != neomessagetype_t(icsneo::Message::Type::Frame)) {
|
|
|
|
|
LOG(LOG_DEBUG, "could not create a neomessage_can_t\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
memcpy(rxBoxCurrentPosition, &neomessage, sizeof(neomessage));
|
|
|
|
|
rxBoxCurrentPosition += sizeof(neomessage);
|
|
|
|
|
memcpy(rxBoxCurrentPosition, neomessage.data, neomessage.length);
|
|
|
|
|
rxBoxCurrentPosition += neomessage.length;
|
|
|
|
|
rxBoxMessageCount++;
|
|
|
|
|
if(ioctl(driver, SIOCSMSGSWRITTEN, (kernelHandle << 16) | rxBoxMessageCount) < 0) {
|
|
|
|
|
LOGF(LOG_DEBUG, "send ioctl failed %d %zu\n", kernelHandle, rxBoxMessageCount);
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(msg->network.getType() == icsneo::Network::Type::CAN || msg->network.getType() == icsneo::Network::Type::Ethernet) {
|
|
|
|
|
|
|
|
|
|
const auto& neomessage = *reinterpret_cast<const T*>(&neomessageGeneric);
|
|
|
|
|
|
|
|
|
|
size_t bytesNeeded = sizeof(neomessage) + neomessage.length;
|
|
|
|
|
std::lock_guard<std::mutex> lg(rxBoxLock);
|
|
|
|
|
if(ssize_t((rxBoxCurrentPosition - rxBox) + bytesNeeded) > RX_BOX_SIZE) {
|
|
|
|
|
// fail, too big!
|
|
|
|
|
LOG(LOG_DEBUG, "box too small\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
memcpy(rxBoxCurrentPosition, &neomessage, sizeof(neomessage));
|
|
|
|
|
rxBoxCurrentPosition += sizeof(neomessage);
|
|
|
|
|
memcpy(rxBoxCurrentPosition, neomessage.data, neomessage.length);
|
|
|
|
|
rxBoxCurrentPosition += neomessage.length;
|
|
|
|
|
rxBoxMessageCount++;
|
|
|
|
|
if(ioctl(driver, SIOCSMSGSWRITTEN, (kernelHandle << 16) | rxBoxMessageCount) < 0) {
|
|
|
|
|
LOGF(LOG_DEBUG, "send ioctl failed %d %zu\n", kernelHandle, rxBoxMessageCount);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rxBoxCurrentPosition = rxBox;
|
|
|
|
|
rxBoxMessageCount = 0;
|
|
|
|
|
}
|
|
|
|
|
rxBoxCurrentPosition = rxBox;
|
|
|
|
|
rxBoxMessageCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
icsneo::Network::Type type;
|
|
|
|
|
std::string name;
|
|
|
|
|
int kernelHandle = -1;
|
|
|
|
|
std::mutex rxBoxLock;
|
|
|
|
|
@ -170,7 +222,7 @@ std::string sanitizeInterfaceName(std::string str) {
|
|
|
|
|
|
|
|
|
|
void header() {
|
|
|
|
|
std::cout << "The libicsneo SocketCAN Usermode Daemon\n";
|
|
|
|
|
std::cout << "Copyright Intrepid Control Systems, Inc. 2019\n\n";
|
|
|
|
|
std::cout << "Copyright Intrepid Control Systems, Inc. 2025\n\n";
|
|
|
|
|
std::cout << "Daemon v";
|
|
|
|
|
std::cout << (int)ICSNEO_SOCKETCAN_BUILD_MAJOR << '.' << (int)ICSNEO_SOCKETCAN_BUILD_MINOR << '.' << (int)ICSNEO_SOCKETCAN_BUILD_PATCH;
|
|
|
|
|
if(ICSNEO_SOCKETCAN_BUILD_METADATA[0] != '\0')
|
|
|
|
|
@ -187,12 +239,14 @@ void header() {
|
|
|
|
|
|
|
|
|
|
void usage(std::string executableName) {
|
|
|
|
|
std::cerr << "The libicsneo SocketCAN Usermode Daemon\n";
|
|
|
|
|
std::cerr << "Copyright 2019-2020 Intrepid Control Systems, Inc.\n\n";
|
|
|
|
|
std::cerr << "Copyright 2019-2025 Intrepid Control Systems, Inc.\n\n";
|
|
|
|
|
std::cerr << "Usage: " << executableName << " [option]\n\n";
|
|
|
|
|
std::cerr << "Options:\n";
|
|
|
|
|
std::cerr << "\t-d, --daemon\t\tRun as a daemon in the background\n";
|
|
|
|
|
std::cerr << "\t-h, -?, --help, --usage\t\tShow this help page\n";
|
|
|
|
|
std::cerr << "\t --devices\t\tList supported devices\n";
|
|
|
|
|
std::cerr << "\t-d, --daemon\t\t\tRun as a daemon in the background\n";
|
|
|
|
|
std::cerr << "\t-h, -?, --help, --usage\t\t\tShow this help page\n";
|
|
|
|
|
std::cerr << "\t --devices\t\t\tList supported devices\n";
|
|
|
|
|
std::cerr << "\t --filter <serial>\t\tOnly connect to devices with serial\n\t\t\t\t\t\tnumbers starting with this filter\n";
|
|
|
|
|
std::cerr << "\t --scan-interval-ms <interval>\tDevice scan interval in ms\n\t\t\t\t\t\tIf 0, only a single scan is performed\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void terminateSignal(int signal) {
|
|
|
|
|
@ -215,9 +269,14 @@ void searchForDevices() {
|
|
|
|
|
if(alreadyOpen)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const std::string serial = dev->getSerial();
|
|
|
|
|
|
|
|
|
|
// If we have a serial filter, make sure our serial starts with the given filter
|
|
|
|
|
if(!serialFilter.empty() && serial.rfind(serialFilter, 0) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Now open the device
|
|
|
|
|
OpenDevice newDevice(dev);
|
|
|
|
|
const std::string serial = newDevice.device->getSerial();
|
|
|
|
|
Lazy<bool> firstTimeFailedToOpen([&serial]() {
|
|
|
|
|
return std::find(failedToOpen.begin(), failedToOpen.end(), serial) == failedToOpen.end();
|
|
|
|
|
});
|
|
|
|
|
@ -229,28 +288,29 @@ void searchForDevices() {
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the supported CAN networks
|
|
|
|
|
|
|
|
|
|
// Get the supported networks
|
|
|
|
|
auto supportedNetworks = newDevice.device->getSupportedRXNetworks();
|
|
|
|
|
supportedNetworks.erase(std::remove_if(supportedNetworks.begin(), supportedNetworks.end(), [](const icsneo::Network& net) -> bool {
|
|
|
|
|
return net.getType() != icsneo::Network::Type::CAN;// Only want CAN networks
|
|
|
|
|
return net.getType() != icsneo::Network::Type::CAN && net.getType() != icsneo::Network::Type::Ethernet;
|
|
|
|
|
}), supportedNetworks.end());
|
|
|
|
|
if(supportedNetworks.empty()) {
|
|
|
|
|
if(firstTimeFailedToOpen) {
|
|
|
|
|
LOGF(LOG_INFO, "%s has no supported CAN networks\n", newDevice.device->describe().c_str());
|
|
|
|
|
LOGF(LOG_INFO, "%s has no supported networks\n", newDevice.device->describe().c_str());
|
|
|
|
|
failedToOpen.push_back(serial);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a network interface for each CAN network
|
|
|
|
|
// Create a network interface for each network
|
|
|
|
|
for(const auto& net : supportedNetworks) {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << sanitizeInterfaceName(icsneo::Network::GetNetIDString(net.getNetID())) << "_" << serial;
|
|
|
|
|
std::string interfaceName(ss.str());
|
|
|
|
|
if(firstTimeFailedToOpen)
|
|
|
|
|
LOGF(LOG_INFO, "Creating network interface %s\n", interfaceName.c_str());
|
|
|
|
|
newDevice.interfaces[net.getNetID()] = std::make_shared<NetworkInterface>(interfaceName);
|
|
|
|
|
|
|
|
|
|
newDevice.interfaces[net.getNetID()] = std::make_shared<NetworkInterface>(interfaceName, net.getType());
|
|
|
|
|
LOGF(LOG_INFO, "Created network interface %s\n", interfaceName.c_str());
|
|
|
|
|
}
|
|
|
|
|
bool failedToCreateNetworkInterfaces = false;
|
|
|
|
|
@ -267,12 +327,21 @@ void searchForDevices() {
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (driverMinor > 0) {
|
|
|
|
|
for(const auto& net : supportedNetworks) {
|
|
|
|
|
if (net.getType() != icsneo::Network::Type::CAN)
|
|
|
|
|
continue;
|
|
|
|
|
newDevice.interfaces[net.getNetID()]->reportBaudrates(
|
|
|
|
|
newDevice.device->settings->getBaudrateFor(net.getNetID()),
|
|
|
|
|
newDevice.device->settings->getFDBaudrateFor(net.getNetID())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create rx listener
|
|
|
|
|
newDevice.device->addMessageCallback(icsneo::CANMessageCallback([serial](std::shared_ptr<icsneo::Message> message) {
|
|
|
|
|
if(message->transmitted)
|
|
|
|
|
return;
|
|
|
|
|
auto canMessage = std::static_pointer_cast<icsneo::CANMessage>(message);
|
|
|
|
|
newDevice.device->addMessageCallback(std::make_shared<icsneo::MessageCallback>([serial](std::shared_ptr<icsneo::Message> message) {
|
|
|
|
|
const auto frame = std::static_pointer_cast<icsneo::Frame>(message);
|
|
|
|
|
const auto messageType = frame->network.getType();
|
|
|
|
|
const OpenDevice* openDevice = nullptr;
|
|
|
|
|
std::lock_guard<std::mutex> lg(openDevicesMutex);
|
|
|
|
|
for(const auto& dev : openDevices) {
|
|
|
|
|
@ -281,13 +350,17 @@ void searchForDevices() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!openDevice) {
|
|
|
|
|
LOG(LOG_ERR, "Dropping message, no open device\n");
|
|
|
|
|
if(frame->type != icsneo::Message::Type::Frame) {
|
|
|
|
|
LOG(LOG_ERR, "Dropping message: received invalid message type, expected RawMessage\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo might throw
|
|
|
|
|
openDevice->interfaces.at(canMessage->network.getNetID())->addReceivedMessageToQueue(canMessage);
|
|
|
|
|
if(messageType == icsneo::Network::Type::CAN) {
|
|
|
|
|
openDevice->interfaces.at(frame->network.getNetID())->addReceivedMessageToQueue<neomessage_can_t>(frame);
|
|
|
|
|
} else if(messageType == icsneo::Network::Type::Ethernet) {
|
|
|
|
|
openDevice->interfaces.at(frame->network.getNetID())->addReceivedMessageToQueue<neomessage_eth_t>(frame);
|
|
|
|
|
} else
|
|
|
|
|
LOG(LOG_ERR, "Dropping message, only CAN and Ethernet are currently supported\n");
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
LOGF(LOG_INFO, "%s connected\n", newDevice.device->describe().c_str());
|
|
|
|
|
@ -339,18 +412,16 @@ void searchForDevices() {
|
|
|
|
|
void deviceSearchThread() {
|
|
|
|
|
while(!stopRunning) {
|
|
|
|
|
searchForDevices();
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
|
if(scanIntervalMs == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(scanIntervalMs));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
if(argc > 2) {
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return EX_USAGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(argc == 2) {
|
|
|
|
|
const std::string arg = argv[1];
|
|
|
|
|
for(int i = 1; i != argc; i++) {
|
|
|
|
|
const std::string arg = argv[i];
|
|
|
|
|
if(arg == "-d" || arg == "--daemon") {
|
|
|
|
|
runningAsDaemon = true;
|
|
|
|
|
} else if(arg == "-h" || arg == "--help" || arg == "-?" || arg == "--usage") {
|
|
|
|
|
@ -362,6 +433,24 @@ int main(int argc, char** argv) {
|
|
|
|
|
for(auto& dev : icsneo::GetSupportedDevices())
|
|
|
|
|
std::cout << '\t' << dev << std::endl;
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
} else if(arg == "--filter" && i + 1 <= argc) {
|
|
|
|
|
serialFilter = argv[++i];
|
|
|
|
|
transform(serialFilter.begin(), serialFilter.end(), serialFilter.begin(), ::toupper);
|
|
|
|
|
} else if(arg == "--scan-interval-ms" && i + 1 <= argc) {
|
|
|
|
|
try {
|
|
|
|
|
scanIntervalMs = std::stoi(argv[++i]);
|
|
|
|
|
} catch (const std::invalid_argument& e) {
|
|
|
|
|
std::cerr << "Invalid input for scan-interval-ms\n";
|
|
|
|
|
return EX_USAGE;
|
|
|
|
|
} catch (const std::out_of_range& e) {
|
|
|
|
|
std::cerr << "Out of range input for scan-interval-ms\n";
|
|
|
|
|
return EX_USAGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(scanIntervalMs < 0) {
|
|
|
|
|
std::cerr << "Invalid input for scan-interval-ms\n";
|
|
|
|
|
return EX_USAGE;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
return EX_USAGE;
|
|
|
|
|
@ -405,7 +494,7 @@ int main(int argc, char** argv) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
std::cout << "Driver v" << driverMajor << '.' << driverMinor << '.' << driverPatch << "\n\n";
|
|
|
|
|
if(driverMajor > 2) {
|
|
|
|
|
if(driverMajor > 3) {
|
|
|
|
|
LOG(LOG_ERR, "This version of the usermode daemon is too old to work with this driver\nPlease ensure that both the usermode daemon and kernel driver are up to date\n");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
@ -445,7 +534,7 @@ int main(int argc, char** argv) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::thread searchThread(deviceSearchThread);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(!stopRunning) {
|
|
|
|
|
fd_set fds;
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
|
@ -465,7 +554,7 @@ int main(int argc, char** argv) {
|
|
|
|
|
// Call read() to find out which box they're in and how many
|
|
|
|
|
struct intrepid_pending_tx_info info;
|
|
|
|
|
ssize_t r = read(driver, &info, sizeof(info));
|
|
|
|
|
if (r == -1) {
|
|
|
|
|
if(r == -1) {
|
|
|
|
|
LOGF(LOG_ERR, "Error waiting for tx messages: %s\n", strerror(errno));
|
|
|
|
|
stopRunning = true;
|
|
|
|
|
break;
|
|
|
|
|
@ -473,14 +562,47 @@ int main(int argc, char** argv) {
|
|
|
|
|
LOGF(LOG_ERR, "Unexpected number of bytes read, expected %d got %d\n", (int)sizeof(info), (int)r);
|
|
|
|
|
stopRunning = true;
|
|
|
|
|
break;
|
|
|
|
|
} else if (info.tx_box_index < 0) {
|
|
|
|
|
// Baudrate changed in kernel
|
|
|
|
|
int dev_idx = -(info.tx_box_index + 1);
|
|
|
|
|
LOGF(LOG_INFO, "Baudrate change, device %d, baudrate %d fd_baudrate %ld\n",
|
|
|
|
|
dev_idx, info.count, info.bytes);
|
|
|
|
|
/* fd baudrate is zero if fd mode is disabled in kernel
|
|
|
|
|
* set fd baudrate equal to baudrate */
|
|
|
|
|
if (info.bytes == 0) {
|
|
|
|
|
info.bytes = info.count;
|
|
|
|
|
}
|
|
|
|
|
for(auto& dev : openDevices) {
|
|
|
|
|
for(auto& netifPair : dev.interfaces) {
|
|
|
|
|
auto netid = netifPair.first;
|
|
|
|
|
if(netifPair.second->getKernelHandle() != dev_idx)
|
|
|
|
|
continue;
|
|
|
|
|
if (! dev.device->settings->setBaudrateFor(netid, info.count) ) {
|
|
|
|
|
LOGF(LOG_ERR, "Unable to set baudrate for device %s\n",
|
|
|
|
|
netifPair.second->getName().c_str());
|
|
|
|
|
} else if (! dev.device->settings->setFDBaudrateFor(netid, info.bytes)) {
|
|
|
|
|
LOGF(LOG_ERR, "Unable to set fd baudrate for device %s\n",
|
|
|
|
|
netifPair.second->getName().c_str());
|
|
|
|
|
} else if (! dev.device->settings->apply()) {
|
|
|
|
|
LOGF(LOG_ERR, "Unable to apply settings for device %s\n",
|
|
|
|
|
netifPair.second->getName().c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Send!
|
|
|
|
|
uint8_t* currentPosition = GET_TX_BOX(info.tx_box_index);
|
|
|
|
|
while(info.count--) {
|
|
|
|
|
neomessage_t* msg = reinterpret_cast<neomessage_t*>(currentPosition);
|
|
|
|
|
currentPosition += sizeof(neomessage_t);
|
|
|
|
|
neomessage_frame_t* msg = reinterpret_cast<neomessage_frame_t*>(currentPosition);
|
|
|
|
|
currentPosition += sizeof(neomessage_frame_t);
|
|
|
|
|
msg->data = currentPosition;
|
|
|
|
|
currentPosition += msg->length;
|
|
|
|
|
|
|
|
|
|
if(msg->type != neonettype_t(icsneo::Network::Type::CAN) && msg->type != neonettype_t(icsneo::Network::Type::Ethernet)) {
|
|
|
|
|
LOG(LOG_ERR, "Message dropped, kernel sent a non-CAN/Ethernet message\n");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool sent = false;
|
|
|
|
|
std::lock_guard<std::mutex> lg(openDevicesMutex);
|
|
|
|
|
for(auto& dev : openDevices) {
|
|
|
|
|
@ -488,8 +610,9 @@ int main(int argc, char** argv) {
|
|
|
|
|
if(netifPair.second->getKernelHandle() != msg->netid)
|
|
|
|
|
continue;
|
|
|
|
|
msg->netid = static_cast<uint16_t>(netifPair.first);
|
|
|
|
|
auto tx = icsneo::CreateMessageFromNeoMessage(msg);
|
|
|
|
|
if(!dev.device->transmit(tx))
|
|
|
|
|
auto txMsg = icsneo::CreateMessageFromNeoMessage(reinterpret_cast<neomessage_t*>(msg));
|
|
|
|
|
auto tx = std::dynamic_pointer_cast<icsneo::Frame>(txMsg);
|
|
|
|
|
if(!tx || !dev.device->transmit(tx))
|
|
|
|
|
break;
|
|
|
|
|
sent = true;
|
|
|
|
|
break;
|
|
|
|
|
@ -508,4 +631,4 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
|
|
LOG(LOG_INFO, "\nExiting...\n");
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|