Files
panda-meb/drivers/windows/pandaJ2534DLL/J2534Connection.cpp
2017-09-29 00:16:02 -07:00

121 lines
3.9 KiB
C++

#include "stdafx.h"
#include "J2534Connection.h"
J2534Connection::J2534Connection(
std::shared_ptr<PandaJ2534Device> panda_dev,
unsigned long ProtocolID,
unsigned long Flags,
unsigned long BaudRate
) : panda_dev(panda_dev), ProtocolID(ProtocolID), Flags(Flags), BaudRate(BaudRate), port(0) {
InitializeCriticalSectionAndSpinCount(&this->message_access_lock, 0x00000400);
}
J2534Connection::~J2534Connection() {
DeleteCriticalSection(&this->message_access_lock);
}
long J2534Connection::PassThruReadMsgs(PASSTHRU_MSG *pMsg, unsigned long *pNumMsgs, unsigned long Timeout) {
*pNumMsgs = 0;
return STATUS_NOERROR;
}
long J2534Connection::PassThruWriteMsgs(PASSTHRU_MSG *pMsg, unsigned long *pNumMsgs, unsigned long Timeout) { return STATUS_NOERROR; }
long J2534Connection::PassThruStartPeriodicMsg(PASSTHRU_MSG *pMsg, unsigned long *pMsgID, unsigned long TimeInterval) { return STATUS_NOERROR; }
long J2534Connection::PassThruStopPeriodicMsg(unsigned long MsgID) { return STATUS_NOERROR; }
long J2534Connection::PassThruStartMsgFilter(unsigned long FilterType, PASSTHRU_MSG *pMaskMsg, PASSTHRU_MSG *pPatternMsg,
PASSTHRU_MSG *pFlowControlMsg, unsigned long *pFilterID) {
for (int i = 0; i < this->filters.size(); i++) {
if (filters[i] == nullptr) {
try {
auto newfilter = std::make_shared<J2534MessageFilter>(J2534MessageFilter(this, FilterType, pMaskMsg, pPatternMsg, pFlowControlMsg));
for (int check_idx = 0; check_idx < filters.size(); check_idx++) {
if (filters[check_idx] == nullptr) continue;
if (filters[check_idx] == newfilter) {
filters[i] = nullptr;
return ERR_NOT_UNIQUE;
}
}
*pFilterID = i;
filters[i] = newfilter;
return STATUS_NOERROR;
} catch (int e) {
return e;
}
}
}
return ERR_EXCEEDED_LIMIT;
}
long J2534Connection::PassThruStopMsgFilter(unsigned long FilterID) {
if (FilterID >= this->filters.size() || this->filters[FilterID] == nullptr)
return ERR_INVALID_FILTER_ID;
this->filters[FilterID] = nullptr;
return STATUS_NOERROR;
}
long J2534Connection::PassThruIoctl(unsigned long IoctlID, void *pInput, void *pOutput) {
return STATUS_NOERROR;
}
long J2534Connection::init5b(SBYTE_ARRAY* pInput, SBYTE_ARRAY* pOutput) { return STATUS_NOERROR; }
long J2534Connection::initFast(PASSTHRU_MSG* pInput, PASSTHRU_MSG* pOutput) { return STATUS_NOERROR; }
long J2534Connection::clearTXBuff() { return STATUS_NOERROR; }
long J2534Connection::clearRXBuff() {
this->messages = {};
return STATUS_NOERROR;
}
long J2534Connection::clearPeriodicMsgs() { return STATUS_NOERROR; }
long J2534Connection::clearMsgFilters() {
for (auto& filter : this->filters) filter = nullptr;
return STATUS_NOERROR;
}
long J2534Connection::setBaud(unsigned long baud) {
this->BaudRate = baud;
return STATUS_NOERROR;
}
unsigned long J2534Connection::getBaud() {
return this->BaudRate;
}
unsigned long J2534Connection::getProtocol() {
return this->ProtocolID;
}
unsigned long J2534Connection::getPort() {
return this->port;
}
//Works well as long as the protocol doesn't support flow control.
void J2534Connection::processMessage(const PASSTHRU_MSG_INTERNAL& msg) {
if (msg.ProtocolID != this->getProtocol()) return;
FILTER_RESULT filter_res = FILTER_RESULT_NEUTRAL;
if ((msg.RxStatus & TX_MSG_TYPE) == TX_MSG_TYPE) {
if (!this->loopback) return; // Optionally ignore loopbacks
filter_res = FILTER_RESULT_PASS;
} else {
for (auto filter : this->filters) {
if (filter == nullptr) continue;
FILTER_RESULT current_check_res = filter->check(msg);
if (current_check_res == FILTER_RESULT_BLOCK) return;
if (current_check_res == FILTER_RESULT_PASS) filter_res = FILTER_RESULT_PASS;
}
}
if (filter_res == FILTER_RESULT_PASS) {
EnterCriticalSection(&this->message_access_lock);
this->messages.push(msg);
LeaveCriticalSection(&this->message_access_lock);
}
}
unsigned long J2534Connection::getMinMsgLen() {
return 1;
}
unsigned long J2534Connection::getMaxMsgLen() {
return 4128;
}