forked from REVrobotics/CANBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from unofficial-rev-port/SocketCAN
Add SocketCAN functionality
- Loading branch information
Showing
35 changed files
with
1,007 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/native/cpp/Drivers/CandleWinUSB/CandleWinUSBDevice.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/native/cpp/Drivers/CandleWinUSB/CandleWinUSBDriver.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/native/cpp/Drivers/SocketCAN/SocketCANDevice.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.