Skip to content

Commit

Permalink
Merge pull request #5 from unofficial-rev-port/SocketCAN
Browse files Browse the repository at this point in the history
Add SocketCAN functionality
  • Loading branch information
garrettsummerfi3ld authored Aug 30, 2024
2 parents 6576ec7 + 13e3871 commit 4b9c79b
Show file tree
Hide file tree
Showing 35 changed files with 1,007 additions and 28 deletions.
17 changes: 16 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@
"iterator": "cpp",
"sstream": "cpp",
"cctype": "cpp",
"concepts": "cpp"
"concepts": "cpp",
"inet.h": "c",
"cwctype": "cpp",
"array": "cpp",
"hash_map": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"set": "cpp",
"string_view": "cpp",
"cinttypes": "cpp"
},
"C_Cpp.default.configurationProvider": "vscode-wpilib",
"C_Cpp.errorSquiggles": "enabled"
Expand Down
4 changes: 2 additions & 2 deletions .wpilib/wpilib_preferences.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"enableCppIntellisense": true,
"currentLanguage": "cpp",
"projectYear": "Beta2020-2",
"projectYear": "2020",
"teamNumber": 9999
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ The output is placed at `~\releases\maven\release\com\revrobotics\usb\CANBridge-
6. Add release notes to the draft release
7. Publish the draft release

## Linux

> [!NOTE]
>
> This branch is a work in progress, testing does show that it does work, however this still might be some issues. If you do run across those issues, please create an issue so we can diagnose.
The latest firmware version will work with Linux if the `SocketCAN` and `gs_usb` drivers are enabled. The following udev rule will work to enable this:

Create a new file named and located at: `/etc/udev/rules.d/99-canbridge.rules`

```text
ACTION=="add", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="a30e", RUN+="/sbin/modprobe gs_usb" RUN+="/bin/sh -c 'echo 0483 a30e > /sys/bus/usb/drivers/gs_usb/new_id'"
```

## Changelog

The SDK Changelog can be viewed with [Changelog.md](Changelog.md).
11 changes: 9 additions & 2 deletions src/main/native/cpp/CANBridge.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -45,6 +45,10 @@

#include "rev/Drivers/SerialPort/SerialDriver.h"

#ifdef __linux__
#include "rev/Drivers/SocketCAN/SocketCANDriver.h"
#endif

#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

Expand All @@ -61,7 +65,9 @@ static const std::vector<rev::usb::CANDriver*> CANDriverList = {
#ifdef _WIN32
new rev::usb::CandleWinUSBDriver(),
#endif
new rev::usb::SerialDriver()
#ifdef __linux__
new rev::usb::SocketCANDriver(),
#endif
};

static std::vector<std::pair<std::unique_ptr<rev::usb::CANDevice>, rev::usb::CANBridge_CANFilter>> CANDeviceList = {};
Expand Down Expand Up @@ -312,3 +318,4 @@ void CANBridge_UnregisterDeviceFromHAL(const char* descriptor)
}



2 changes: 1 addition & 1 deletion src/main/native/cpp/CANBridgeUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/native/cpp/Drivers/Serial/SerialDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/native/cpp/Drivers/Serial/SerialDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion src/main/native/cpp/Drivers/Serial/SerialMessage.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
119 changes: 119 additions & 0 deletions src/main/native/cpp/Drivers/SocketCAN/SocketCANDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#ifdef __linux__

#include "rev/Drivers/SocketCAN/SocketCANDevice.h"

#include <map>
#include <string>
#include <thread>

#include <hal/simulation/CanData.h>
#include <hal/CAN.h>

namespace rev {
namespace usb {

SocketCANDevice::SocketCANDevice(std::string port) :
m_thread(port) {
m_descriptor = port;
// TODO: Get the name of the device, for now just hardcode the name
m_name = "SocketCAN Device";
m_thread.Start();
}

SocketCANDevice::~SocketCANDevice() {
m_thread.Stop();
}

std::string SocketCANDevice::GetName() const {
return m_name;
}

std::string SocketCANDevice::GetDescriptor() const {
return m_descriptor;
}

int SocketCANDevice::GetNumberOfErrors() {
return m_thread.GetNumberOfErrors();
}

int SocketCANDevice::GetId() const {
return 0;
}

CANStatus SocketCANDevice::SendCANMessage(const CANMessage& msg, int periodMs) {
m_thread.EnqueueMessage(msg, periodMs);
return m_thread.GetLastThreadError();
}

CANStatus SocketCANDevice::ReceiveCANMessage(std::shared_ptr<CANMessage>& msg, uint32_t messageID, uint32_t messageMask) {
CANStatus status = CANStatus::kTimeout;

// parse through the keys, find the messges the match, and return it
// The first in the message id, then the messages
std::map<uint32_t, std::shared_ptr<CANMessage>> messages;
m_thread.ReceiveMessage(messages);
std::shared_ptr<CANMessage> mostRecent;
for (auto& m : messages) {
if (
CANBridge_ProcessMask({m.second->GetMessageId(), 0}, m.first)
&& CANBridge_ProcessMask({messageID, messageMask}, m.first)
&& (!mostRecent || m.second->GetTimestampUs() > mostRecent->GetTimestampUs())
) {
mostRecent = m.second;
status = CANStatus::kOk;
}
}

if (status == CANStatus::kOk) {
msg = mostRecent;
status = m_thread.GetLastThreadError();
} else {
status = CANStatus::kError;
}


return status;
}

CANStatus SocketCANDevice::OpenStreamSession(uint32_t* sessionHandle, CANBridge_CANFilter filter, uint32_t maxSize) {
CANStatus stat = CANStatus::kOk;
m_thread.OpenStream(sessionHandle, filter, maxSize, &stat);
return m_thread.GetLastThreadError();
}

CANStatus SocketCANDevice::CloseStreamSession(uint32_t sessionHandle) {
m_thread.CloseStream(sessionHandle);
return m_thread.GetLastThreadError();
}

CANStatus SocketCANDevice::ReadStreamSession(uint32_t sessionHandle, struct HAL_CANStreamMessage* msgs, uint32_t messagesToRead, uint32_t* messagesRead) {
m_thread.ReadStream(sessionHandle, msgs, messagesToRead, messagesRead);
return m_thread.GetLastThreadError();
}

CANStatus SocketCANDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr) {
rev::usb::CANStatusDetails details;
m_thread.GetCANStatus(&details);
*percentBusUtilization = 0.0f;
*busOff = details.busOffCount;
*txFull = details.txFullCount;
*receiveErr = details.receiveErrCount;
*transmitErr = details.transmitErrCount;
return m_thread.GetLastThreadError();
}

CANStatus SocketCANDevice::GetCANDetailStatus(float* percentBusUtilization, uint32_t* busOff, uint32_t* txFull, uint32_t* receiveErr, uint32_t* transmitErr, uint32_t* lastErrorTime) {
return GetCANDetailStatus(percentBusUtilization, busOff, txFull, receiveErr, transmitErr);
}

bool SocketCANDevice::IsConnected() {
// Implementation
return true;
}

} // namespace usb
} // namespace rev


#else
#endif
94 changes: 94 additions & 0 deletions src/main/native/cpp/Drivers/SocketCAN/SocketCANDriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of REV Robotics nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef __linux__

#include "rev/Drivers/SocketCAN/SocketCANDriver.h"
#include "rev/Drivers/SocketCAN/SocketCANDevice.h"

#include <map>
#include <iostream>
#include <memory>

#include <net/if.h>

namespace rev {
namespace usb {

std::vector<CANDeviceDetail> SocketCANDriver::GetDevices()
{
std::vector<CANDeviceDetail> retval;

// TODO: Better way of doing this?
// find canx or vcanx interface names
struct if_nameindex *if_nidxs, *intf;

if_nidxs = if_nameindex();
if ( if_nidxs != NULL )
{
for (intf = if_nidxs; intf->if_index != 0 || intf->if_name != NULL; intf++)
{
char* buf = intf->if_name;

// Not possible can name, protect later compares
if (strnlen(buf, 4) < 4) {
continue;
}

// possibly vcanx
if (buf[0] == 'v') {
buf++;
}

if (strncmp(buf, "can", 3) == 0) {
std::string ifnameStr = std::string(intf->if_name);
retval.push_back( {ifnameStr, ifnameStr, this->GetName()} );
}
}

if_freenameindex(if_nidxs);
}

return retval;
}

std::unique_ptr<CANDevice> SocketCANDriver::CreateDeviceFromDescriptor(const char* descriptor)
{
try {
return std::make_unique<SocketCANDevice>(descriptor);
} catch(...) {
// do nothing if it failed
}
return std::unique_ptr<CANDevice>(nullptr);
}
} // namespace usb
} // namespace rev

#else
typedef int __ISOWarning__CLEAR_;
#endif // _WIN32
2 changes: 1 addition & 1 deletion src/main/native/cpp/ThreadUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 REV Robotics
* Copyright (c) 2019 - 2020 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down
Loading

0 comments on commit 4b9c79b

Please sign in to comment.