J2534_WIN: CAN Tests.

master
Jessy Diamond Exum 2017-09-14 18:01:06 -07:00
parent 825e21e17e
commit 71b7f9427a
10 changed files with 479 additions and 14 deletions

View File

@ -0,0 +1,17 @@
#include "stdafx.h"
#include "Timer.h"
Timer::Timer()
{
start = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
}
// gets the time elapsed from construction.
long /*milliseconds*/ Timer::getTimePassed(){
// get the new time
auto end = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
// return the difference of the times
return (end - start).count();
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <chrono>
//Copied from https://stackoverflow.com/a/31488113
class Timer
{
using clock = std::chrono::system_clock;
using time_point_type = std::chrono::time_point < clock, std::chrono::milliseconds >;
public:
Timer();
// gets the time elapsed from construction.
long /*milliseconds*/ getTimePassed();
private:
time_point_type start;
};

View File

@ -1,18 +1,127 @@
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Loader4.h"
#include "pandaJ2534DLL/J2534_v0404.h"
#include "panda/panda.h"
#include "Timer.h"
#include <iomanip>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace pandaJ2534DLLTest
{
void write_ioctl(unsigned int chanid, unsigned int param, unsigned int val, const __LineInfo* pLineInfo = NULL) {
SCONFIG config = { param, val };
SCONFIG_LIST inconfig = { 1, &config };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruIoctl(chanid, SET_CONFIG, &inconfig, NULL), _T("Failed to set IOCTL."), pLineInfo);
}
std::vector<panda::PANDA_CAN_MSG> panda_recv_loop(std::unique_ptr<panda::Panda>& p, unsigned int num_expected, unsigned long timeout_ms = 500) {
std::vector<panda::PANDA_CAN_MSG> ret_messages;
Timer t = Timer();
while (t.getTimePassed() < timeout_ms) {
Sleep(100);
std::vector<panda::PANDA_CAN_MSG>msg_recv = p->can_recv();
if (msg_recv.size() > 0) {
ret_messages.insert(std::end(ret_messages), std::begin(msg_recv), std::end(msg_recv));
}
if (ret_messages.size() >= num_expected) break;
}
std::ostringstream stringStream;
stringStream << "j2534_recv_loop Broke at " << t.getTimePassed() << " ms size is " << ret_messages.size();
Logger::WriteMessage(stringStream.str().c_str());
Assert::AreEqual<unsigned long>(num_expected, ret_messages.size(), _T("Received wrong number of messages."));
return ret_messages;
}
void check_panda_can_msg(panda::PANDA_CAN_MSG& msgin, uint8_t bus, unsigned long addr, bool addr_29b,
bool is_receipt, std::string dat, const __LineInfo* pLineInfo = NULL) {
Assert::AreEqual<uint8_t>(bus, msgin.bus, _T("Wrong msg bus"), pLineInfo);
Assert::AreEqual<unsigned long>(addr, msgin.addr, _T("Wrong msg addr"), pLineInfo);
Assert::AreEqual<bool>(addr_29b, msgin.addr_29b, _T("Wrong msg 28b flag"), pLineInfo);
Assert::AreEqual<bool>(is_receipt, msgin.is_receipt, _T("Wrong msg receipt flag"), pLineInfo);
Assert::AreEqual<size_t>(dat.size(), msgin.len, _T("Wrong msg len"), pLineInfo);
Assert::AreEqual<std::string>(dat, std::string((char*)msgin.dat, msgin.len), _T("Wrong msg payload"), pLineInfo);
}
void J2534_send_msg_checked(unsigned long chanid, unsigned long ProtocolID, unsigned long RxStatus, unsigned long TxFlags,
unsigned long Timestamp, unsigned long DataSize, unsigned long ExtraDataIndex, const char* Data, const __LineInfo* pLineInfo = NULL) {
PASSTHRU_MSG msg = { ProtocolID, RxStatus, TxFlags, Timestamp, DataSize, ExtraDataIndex };
memcpy_s(msg.Data, 4128, Data, DataSize);
unsigned long msgcount = 1;
Assert::AreEqual<long>(STATUS_NOERROR, PassThruWriteMsgs(chanid, &msg, &msgcount, 0), _T("Failed to write message."), pLineInfo);
Assert::AreEqual<unsigned long>(1, msgcount, _T("Wrong message count after tx."), LINE_INFO());
}
long J2534_send_msg(unsigned long chanid, unsigned long ProtocolID, unsigned long RxStatus, unsigned long TxFlags,
unsigned long Timestamp, unsigned long DataSize, unsigned long ExtraDataIndex, const char* Data) {
PASSTHRU_MSG msg = { ProtocolID, RxStatus, TxFlags, Timestamp, DataSize, ExtraDataIndex };
memcpy_s(msg.Data, 4128, Data, DataSize);
unsigned long msgcount = 1;
return PassThruWriteMsgs(chanid, &msg, &msgcount, 0);
}
std::vector<PASSTHRU_MSG> j2534_recv_loop(unsigned int chanid, unsigned int num_expected, unsigned long timeout_ms = 500) {
std::vector<PASSTHRU_MSG> ret_messages;
PASSTHRU_MSG recvbuff[4] = {};
Timer t = Timer();
while (t.getTimePassed() < timeout_ms) {
unsigned long msgcount = 4;
unsigned int res = PassThruReadMsgs(chanid, recvbuff, &msgcount, 0);
if (res == ERR_BUFFER_EMPTY) continue;
Assert::IsFalse(msgcount > 4, _T("PassThruReadMsgs returned more data than the buffer could hold."));
Assert::AreEqual<long>(STATUS_NOERROR, res, _T("Failed to read message."));
if (msgcount > 0) {
for (unsigned int i = 0; i < msgcount; i++) {
ret_messages.push_back(recvbuff[i]);
}
}
if (ret_messages.size() >= num_expected) break;
}
std::ostringstream stringStream;
stringStream << "j2534_recv_loop Broke at " << t.getTimePassed() << " ms size is " << ret_messages.size();
Logger::WriteMessage(stringStream.str().c_str());
Assert::AreEqual<unsigned long>(num_expected, ret_messages.size(), _T("Received wrong number of messages."));
return ret_messages;
}
void check_J2534_can_msg(PASSTHRU_MSG& msgin, unsigned long ProtocolID, unsigned long RxStatus, unsigned long TxFlags,
unsigned long DataSize, unsigned long ExtraDataIndex, const char* Data, const __LineInfo* pLineInfo = NULL) {
Assert::AreEqual<size_t>(DataSize, msgin.DataSize, _T("Wrong msg len"), pLineInfo);
std::ostringstream logmsg;
logmsg << "Expected Hex (";
for (int i = 0; i < DataSize; i++) logmsg << std::hex << std::setw(2) << std::setfill('0') << int(Data[i] & 0xFF) << " ";
logmsg << "); Actual Hex (";
for (int i = 0; i < msgin.DataSize; i++) logmsg << std::hex << std::setw(2) << std::setfill('0') << int(((char*)msgin.Data)[i] & 0xFF) << " ";
logmsg << ")";
Logger::WriteMessage(logmsg.str().c_str());
Assert::AreEqual<std::string>(std::string(Data, DataSize), std::string((char*)msgin.Data, msgin.DataSize), _T("Wrong msg payload"), pLineInfo);
Assert::AreEqual<unsigned long>(ProtocolID, msgin.ProtocolID, _T("Wrong msg protocol"), pLineInfo);
Assert::AreEqual<unsigned long>(RxStatus, msgin.RxStatus, _T("Wrong msg receipt rxstatus"), pLineInfo);
Assert::AreEqual<unsigned long>(TxFlags, msgin.TxFlags, _T("Wrong msg receipt txflag"), pLineInfo);
Assert::AreEqual<unsigned long>(ExtraDataIndex, msgin.ExtraDataIndex, _T("Wrong msg ExtraDataIndex"), pLineInfo);
}
TEST_CLASS(J2534DLLInitialization)
{
public:
TEST_METHOD(J2534TestFunctionsExist)
TEST_CLASS_CLEANUP(deinit) {
UnloadJ2534Dll();
}
TEST_METHOD(J2534DriverInit)
{
long err = LoadJ2534Dll("PandaJ2534DLL.dll");
Assert::IsTrue(err == 0, _T("Library failed to load properly. Check the export names and library location."));
@ -36,15 +145,324 @@ namespace pandaJ2534DLLTest
UnloadJ2534Dll();
}
TEST_METHOD(J2534TestOpenDevice)
TEST_METHOD(J2534_OpenDevice__Empty)
{
long res = PassThruOpen("", &devid);
Assert::AreEqual<long>(res, STATUS_NOERROR, _T("Failed to open device."), LINE_INFO());
didopen = TRUE;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
}
TEST_METHOD(J2534_OpenDevice__J2534_2)
{
Assert::AreEqual<long>(STATUS_NOERROR, open_dev("J2534-2:"), _T("Failed to open device."), LINE_INFO());
}
TEST_METHOD(J2534_OpenDevice__SN)
{
auto pandas_available = panda::Panda::listAvailablePandas();
Assert::IsTrue(pandas_available.size() > 0, _T("No pandas detected."));
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(pandas_available[0].c_str()), _T("Failed to open device."), LINE_INFO());
auto pandas_available_2 = panda::Panda::listAvailablePandas();
for (auto panda_sn : pandas_available_2)
Assert::AreNotEqual(panda_sn, pandas_available[0]);
}
TEST_METHOD(J2534_CloseDevice)
{
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, close_dev(devid), _T("Failed to close device."), LINE_INFO());
Assert::AreEqual<long>(ERR_INVALID_DEVICE_ID, PassThruClose(devid), _T("The 2nd close should have failed with ERR_INVALID_DEVICE_ID."), LINE_INFO());
}
TEST_METHOD(J2534_ConnectDisconnect)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruDisconnect(chanid), _T("Failed to close channel."), LINE_INFO());
Assert::AreEqual<long>(ERR_INVALID_CHANNEL_ID, PassThruDisconnect(chanid), _T("The 2nd disconnect should have failed with ERR_INVALID_CHANNEL_ID."), LINE_INFO());
}
TEST_METHOD(J2534_ConnectInvalidProtocol)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(ERR_INVALID_PROTOCOL_ID, PassThruConnect(devid, 999, 0, 500000, &chanid),
_T("Did not report ERR_INVALID_PROTOCOL_ID."), LINE_INFO());
Assert::AreEqual<long>(ERR_INVALID_CHANNEL_ID, PassThruDisconnect(chanid), _T("The channel should not have been created."), LINE_INFO());
}
bool didopen = FALSE;
unsigned long devid;
unsigned long open_dev(const char* name, long assert_err = STATUS_NOERROR, TCHAR* failmsg = _T("Failed to open device.")) {
unsigned int res = PassThruOpen((void*)name, &devid);
if (res == STATUS_NOERROR) didopen = TRUE;
return res;
}
unsigned long close_dev(unsigned long devid) {
unsigned long res = PassThruClose(devid);
if (res == STATUS_NOERROR) didopen = FALSE;
return res;
}
};
TEST_CLASS(J2534DeviceCAN)
{
public:
TEST_METHOD_INITIALIZE(init) {
LoadJ2534Dll("PandaJ2534DLL.dll");
}
TEST_METHOD_CLEANUP(deinit) {
if (didopen) {
PassThruClose(devid);
didopen = FALSE;
}
UnloadJ2534Dll();
}
TEST_METHOD(J2534_CAN_Tx)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
J2534_send_msg_checked(chanid, CAN, 0, 0, 0, 6, 6, "\x0\x0\x3\xAB""HI", LINE_INFO());
std::vector<panda::PANDA_CAN_MSG> msg_recv = panda_recv_loop(p, 1);
check_panda_can_msg(msg_recv[0], 0, 0x3AB, FALSE, FALSE, "HI", LINE_INFO());
}
TEST_METHOD(J2534_CAN29b_Tx)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, CAN_29BIT_ID, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
Assert::AreEqual<long>(ERR_INVALID_MSG, J2534_send_msg(chanid, CAN, 0, 0, 0, 6, 6, "\x0\x0\x3\xAB""HI"), _T("11b address should fail to tx."), LINE_INFO());
J2534_send_msg_checked(chanid, CAN, 0, CAN_29BIT_ID, 0, 6, 6, "\x0\x0\x3\xAB""YO", LINE_INFO());
std::vector<panda::PANDA_CAN_MSG> msg_recv = panda_recv_loop(p, 1);
check_panda_can_msg(msg_recv[0], 0, 0x3AB, TRUE, FALSE, "YO", LINE_INFO());
}
TEST_METHOD(J2534_CAN11b29b_Tx)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, CAN_ID_BOTH, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
J2534_send_msg_checked(chanid, CAN, 0, 0, 0, 6, 6, "\x0\x0\x3\xAB""HI", LINE_INFO());
J2534_send_msg_checked(chanid, CAN, 0, CAN_29BIT_ID, 0, 6, 6, "\x0\x0\x3\xAB""YO", LINE_INFO());
std::vector<panda::PANDA_CAN_MSG> msg_recv = panda_recv_loop(p, 2);
check_panda_can_msg(msg_recv[0], 0, 0x3AB, FALSE, FALSE, "HI", LINE_INFO());
check_panda_can_msg(msg_recv[1], 0, 0x3AB, TRUE, FALSE, "YO", LINE_INFO());
}
TEST_METHOD(J2534_CAN_TxEcho)
{
unsigned long chanid;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
write_ioctl(chanid, LOOPBACK, FALSE, LINE_INFO()); // DISABLE J2534 ECHO/LOOPBACK
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."));
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
J2534_send_msg_checked(chanid, CAN, 0, 0, 0, 9, 9, "\x0\x0\x3\xAB""HIDOG", LINE_INFO());
auto msg_recv = panda_recv_loop(p, 1);
check_panda_can_msg(msg_recv[0], 0, 0x3AB, FALSE, FALSE, "HIDOG", LINE_INFO());
auto j2534_msg_recv = j2534_recv_loop(chanid, 0);
/////////////////////////////////
write_ioctl(chanid, LOOPBACK, TRUE, LINE_INFO()); // ENABLE J2534 ECHO/LOOPBACK
J2534_send_msg_checked(chanid, CAN, 0, 0, 0, 7, 7, "\x0\x0\x3\xAB""SUP", LINE_INFO());
msg_recv = panda_recv_loop(p, 1);
check_panda_can_msg(msg_recv[0], 0, 0x3AB, FALSE, FALSE, "SUP", LINE_INFO());
j2534_msg_recv = j2534_recv_loop(chanid, 1);
check_J2534_can_msg(j2534_msg_recv[0], CAN, TX_MSG_TYPE, 0, 3 + 4, 0, "\x0\x0\x3\xAB""SUP", LINE_INFO());
}
TEST_METHOD(J2534_CAN_RxAndPassAllFilters)
{
unsigned long chanid, filterid0;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
PASSTHRU_MSG filter = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, PASS_FILTER, &filter, &filter, 0, &filterid0), _T("Failed to create filter."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
p->can_send(0x1FA, FALSE, (const uint8_t*)"ABCDE", 5, panda::PANDA_CAN1);
p->can_send(0x2AC, FALSE, (const uint8_t*)"HIJKL", 5, panda::PANDA_CAN1);
auto j2534_msg_recv = j2534_recv_loop(chanid, 2);
check_J2534_can_msg(j2534_msg_recv[0], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x1\xFA""ABCDE", LINE_INFO());
check_J2534_can_msg(j2534_msg_recv[1], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x2\xAC""HIJKL", LINE_INFO());
}
TEST_METHOD(J2534_CAN_RxAndLimitedPassFilter)
{
unsigned long chanid, filterid0;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
PASSTHRU_MSG filter_mask = { CAN, 0, 0, 0, 4, 4, "\xFF\xFF\xFF\xFF" };
PASSTHRU_MSG filter = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x02\xAC" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, PASS_FILTER, &filter_mask, &filter, 0, &filterid0), _T("Failed to create filter."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
p->can_send(0x1FA, FALSE, (const uint8_t*)"ABCDE", 5, panda::PANDA_CAN1);
p->can_send(0x2AC, FALSE, (const uint8_t*)"HIJKL", 5, panda::PANDA_CAN1);
auto j2534_msg_recv = j2534_recv_loop(chanid, 1);
check_J2534_can_msg(j2534_msg_recv[0], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x2\xAC""HIJKL", LINE_INFO());
}
TEST_METHOD(J2534_CAN_RxAndPassBlockFilter)
{
unsigned long chanid, filterid0, filterid1;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
PASSTHRU_MSG filter_mask0 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
PASSTHRU_MSG filter0 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, PASS_FILTER, &filter_mask0, &filter0, 0, &filterid0), _T("Failed to create filter."), LINE_INFO());
PASSTHRU_MSG filter_mask1 = { CAN, 0, 0, 0, 4, 4, "\xFF\xFF\xFF\xFF" };
PASSTHRU_MSG filter1 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x02\xAC" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, BLOCK_FILTER, &filter_mask1, &filter1, 0, &filterid1), _T("Failed to create filter."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
p->can_send(0x1FA, FALSE, (const uint8_t*)"ABCDE", 5, panda::PANDA_CAN1);
p->can_send(0x2AC, FALSE, (const uint8_t*)"HIJKL", 5, panda::PANDA_CAN1);
p->can_send(0x3FA, FALSE, (const uint8_t*)"MNOPQ", 5, panda::PANDA_CAN1);
auto j2534_msg_recv = j2534_recv_loop(chanid, 2, 1000);
check_J2534_can_msg(j2534_msg_recv[0], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x1\xFA""ABCDE", LINE_INFO());
check_J2534_can_msg(j2534_msg_recv[1], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x3\xFA""MNOPQ", LINE_INFO());
}
TEST_METHOD(J2534_CAN_RxAndFilterBlockPass)
{
//Check that the order of the pass and block filter do not matter
unsigned long chanid, filterid0, filterid1;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
PASSTHRU_MSG filter_mask0 = { CAN, 0, 0, 0, 4, 4, "\xFF\xFF\xFF\xFF" };
PASSTHRU_MSG filter0 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x02\xAC" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, BLOCK_FILTER, &filter_mask0, &filter0, 0, &filterid0), _T("Failed to create filter."), LINE_INFO());
PASSTHRU_MSG filter_mask1 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
PASSTHRU_MSG filter1 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, PASS_FILTER, &filter_mask1, &filter1, 0, &filterid1), _T("Failed to create filter."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
p->can_send(0x1FA, FALSE, (const uint8_t*)"ABCDE", 5, panda::PANDA_CAN1);
p->can_send(0x2AC, FALSE, (const uint8_t*)"HIJKL", 5, panda::PANDA_CAN1); // Should not pass filter
p->can_send(0x3FA, FALSE, (const uint8_t*)"MNOPQ", 5, panda::PANDA_CAN1);
auto j2534_msg_recv = j2534_recv_loop(chanid, 2, 2000);
check_J2534_can_msg(j2534_msg_recv[0], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x1\xFA""ABCDE", LINE_INFO());
check_J2534_can_msg(j2534_msg_recv[1], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x3\xFA""MNOPQ", LINE_INFO());
}
TEST_METHOD(J2534_CAN_RxAndFilterRemoval)
{
//Check that the order of the pass and block filter do not matter
unsigned long chanid, filterid0, filterid1;
Assert::AreEqual<long>(STATUS_NOERROR, open_dev(""), _T("Failed to open device."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruConnect(devid, CAN, 0, 500000, &chanid), _T("Failed to open channel."), LINE_INFO());
PASSTHRU_MSG filter_mask0 = { CAN, 0, 0, 0, 4, 4, "\xFF\xFF\xFF\xFF" };
PASSTHRU_MSG filter0 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x02\xAC" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, BLOCK_FILTER, &filter_mask0, &filter0, 0, &filterid0), _T("Failed to create filter."), LINE_INFO());
PASSTHRU_MSG filter_mask1 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
PASSTHRU_MSG filter1 = { CAN, 0, 0, 0, 4, 4, "\x0\x0\x0\x0" };
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStartMsgFilter(chanid, PASS_FILTER, &filter_mask1, &filter1, 0, &filterid1), _T("Failed to create filter."), LINE_INFO());
Assert::AreEqual<long>(STATUS_NOERROR, PassThruStopMsgFilter(chanid, filterid0), _T("Failed to delete filter."), LINE_INFO());
auto p = panda::Panda::openPanda("");
Assert::IsTrue(p != nullptr, _T("Could not open 2nd device to test communication."), LINE_INFO());
p->set_can_speed_kbps(panda::PANDA_CAN1, 500);
p->set_safety_mode(panda::SAFETY_ALLOUTPUT);
p->can_clear(panda::PANDA_CAN_RX);
p->can_send(0x1FA, FALSE, (const uint8_t*)"ABCDE", 5, panda::PANDA_CAN1);
p->can_send(0x2AC, FALSE, (const uint8_t*)"HIJKL", 5, panda::PANDA_CAN1);
p->can_send(0x3FA, FALSE, (const uint8_t*)"MNOPQ", 5, panda::PANDA_CAN1);
auto j2534_msg_recv = j2534_recv_loop(chanid, 3, 1000);
check_J2534_can_msg(j2534_msg_recv[0], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x1\xFA""ABCDE", LINE_INFO());
check_J2534_can_msg(j2534_msg_recv[1], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x2\xAC""HIJKL", LINE_INFO());
check_J2534_can_msg(j2534_msg_recv[2], CAN, 0, 0, 5 + 4, 0, "\x0\x0\x3\xFA""MNOPQ", LINE_INFO());
}
bool didopen = FALSE;
unsigned long devid;
unsigned long open_dev(const char* name, long assert_err = STATUS_NOERROR, TCHAR* failmsg = _T("Failed to open device.")) {
unsigned int res = PassThruOpen((void*)name, &devid);
if (res == STATUS_NOERROR) didopen = TRUE;
return res;
}
};
}

View File

@ -91,6 +91,7 @@
<ClInclude Include="Loader4.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Timer.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Loader4.cpp" />
@ -99,6 +100,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="j2534_tests.cpp" />
<ClCompile Include="Timer.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\pandaJ2534DLL\pandaJ2534DLL.vcxproj">

View File

@ -27,6 +27,9 @@
<ClInclude Include="..\pandaJ2534DLL\J2534_v0404.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Timer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@ -38,5 +41,8 @@
<ClCompile Include="Loader4.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -2,7 +2,9 @@
#include "J2534Connection_CAN.h"
#include "Timer.h"
#define val_is_29bit(num) (((num) & CAN_29BIT_ID) == CAN_29BIT_ID)
#define check_bmask(num, mask)(((num) & mask) == mask)
#define val_is_29bit(num) check_bmask(num, CAN_29BIT_ID)
J2534Connection_CAN::J2534Connection_CAN(
panda::Panda* panda_dev,
@ -60,13 +62,13 @@ long J2534Connection_CAN::PassThruWriteMsgs(PASSTHRU_MSG *pMsg, unsigned long *p
return ERR_MSG_PROTOCOL_ID;
}
if (msg->DataSize < this->getMinMsgLen() || msg->DataSize > this->getMaxMsgLen() ||
(val_is_29bit(msg->TxFlags) != this->_is_29bit() && (this->Flags & CAN_ID_BOTH) != CAN_ID_BOTH)) {
(val_is_29bit(msg->TxFlags) != this->_is_29bit() && !check_bmask(this->Flags, CAN_ID_BOTH))) {
*pNumMsgs = msgnum;
return ERR_INVALID_MSG;
}
uint32_t addr = msg->Data[0] << 24 | msg->Data[1] << 16 | msg->Data[2] << 8 | msg->Data[3];
if (this->panda_dev->can_send(addr, this->_is_29bit(), &msg->Data[4], msg->DataSize - 4, panda::PANDA_CAN1) == FALSE) {
if (this->panda_dev->can_send(addr, val_is_29bit(msg->TxFlags), &msg->Data[4], msg->DataSize - 4, panda::PANDA_CAN1) == FALSE) {
*pNumMsgs = msgnum;
return ERR_INVALID_MSG;
}

View File

@ -72,7 +72,7 @@ FILTER_RESULT J2534MessageFilter::check(const PASSTHRU_MSG_INTERNAL& msg) {
case PASS_FILTER:
return matches ? FILTER_RESULT_PASS : FILTER_RESULT_NEUTRAL;
case BLOCK_FILTER:
return matches ? FILTER_RESULT_NEUTRAL : FILTER_RESULT_BLOCK;
return matches ? FILTER_RESULT_BLOCK: FILTER_RESULT_NEUTRAL;
default:
return matches ? FILTER_RESULT_PASS : FILTER_RESULT_NEUTRAL;
}

View File

@ -30,7 +30,7 @@ std::unique_ptr<PandaJ2534Device> PandaJ2534Device::openByName(std::string sn) {
DWORD PandaJ2534Device::closeChannel(unsigned long ChannelID) {
if (this->connections.size() <= ChannelID) return ERR_INVALID_CHANNEL_ID;
if (this->connections[ChannelID] == nullptr) return ERR_DEVICE_NOT_CONNECTED;
if (this->connections[ChannelID] == nullptr) return ERR_INVALID_CHANNEL_ID;
this->connections[ChannelID] = nullptr;
return STATUS_NOERROR;
}

View File

@ -10,7 +10,7 @@ Timer::Timer()
// gets the time elapsed from construction.
long /*milliseconds*/ Timer::getTimePassed(){
// get the new time
auto end = clock::now();
auto end = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
// return the difference of the times
return (end - start).count();

View File

@ -28,7 +28,7 @@ long ret_code(long code) {
long check_valid_DeviceID(unsigned long DeviceID) {
uint16_t dev_id = EXTRACT_DID(DeviceID);
if (pandas.size() <= dev_id || pandas[dev_id] == nullptr)
return ret_code(ERR_INVALID_CHANNEL_ID);
return ret_code(ERR_INVALID_DEVICE_ID);
return ret_code(STATUS_NOERROR);
}
@ -145,7 +145,9 @@ PANDAJ2534DLL_API long PTAPI PassThruConnect(unsigned long DeviceID, unsigned lo
}
PANDAJ2534DLL_API long PTAPI PassThruDisconnect(unsigned long ChannelID) {
#pragma EXPORT
if (check_valid_DeviceID(ChannelID) != STATUS_NOERROR) return J25334LastError;
unsigned long res = check_valid_DeviceID(ChannelID);
if (res == ERR_INVALID_DEVICE_ID) return ret_code(ERR_INVALID_CHANNEL_ID);
if (res != STATUS_NOERROR) return J25334LastError;
return ret_code(get_device(ChannelID)->closeChannel(EXTRACT_CID(ChannelID)));
}
PANDAJ2534DLL_API long PTAPI PassThruReadMsgs(unsigned long ChannelID, PASSTHRU_MSG *pMsg,