open source driving agent
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.2 KiB

#include "stdafx.h"
#include "PandaJ2534Device.h"
#include "J2534Frame.h"
PandaJ2534Device::PandaJ2534Device(std::unique_ptr<panda::Panda> new_panda) : txInProgress(FALSE) {
this->panda = std::move(new_panda);
this->panda->set_esp_power(FALSE);
this->panda->set_safety_mode(panda::SAFETY_ALLOUTPUT);
this->panda->set_can_loopback(FALSE);
this->panda->set_alt_setting(0);
this->thread_kill_event = CreateEvent(NULL, TRUE, FALSE, NULL);
DWORD canListenThreadID;
this->can_recv_handle = CreateThread(NULL, 0, _can_recv_threadBootstrap, (LPVOID)this, 0, &canListenThreadID);
DWORD canProcessThreadID;
this->can_process_handle = CreateThread(NULL, 0, _can_process_threadBootstrap, (LPVOID)this, 0, &canProcessThreadID);
DWORD flowControlSendThreadID;
this->flow_control_wakeup_event = CreateEvent(NULL, TRUE, FALSE, NULL);
this->flow_control_thread_handle = CreateThread(NULL, 0, _msg_tx_threadBootstrap, (LPVOID)this, 0, &flowControlSendThreadID);
};
PandaJ2534Device::~PandaJ2534Device() {
SetEvent(this->thread_kill_event);
DWORD res = WaitForSingleObject(this->can_recv_handle, INFINITE);
CloseHandle(this->can_recv_handle);
res = WaitForSingleObject(this->can_process_handle, INFINITE);
CloseHandle(this->can_process_handle);
res = WaitForSingleObject(this->flow_control_thread_handle, INFINITE);
CloseHandle(this->flow_control_thread_handle);
CloseHandle(this->flow_control_wakeup_event);
CloseHandle(this->thread_kill_event);
}
std::shared_ptr<PandaJ2534Device> PandaJ2534Device::openByName(std::string sn) {
auto p = panda::Panda::openPanda("");
if (p == nullptr)
return nullptr;
return std::unique_ptr<PandaJ2534Device>(new PandaJ2534Device(std::move(p)));
}
DWORD PandaJ2534Device::closeChannel(unsigned long ChannelID) {
if (this->connections.size() <= ChannelID) return ERR_INVALID_CHANNEL_ID;
if (this->connections[ChannelID] == nullptr) return ERR_INVALID_CHANNEL_ID;
this->connections[ChannelID] = nullptr;
return STATUS_NOERROR;
}
DWORD PandaJ2534Device::addChannel(std::shared_ptr<J2534Connection>& conn, unsigned long* channel_id) {
int channel_index = -1;
for (unsigned int i = 0; i < this->connections.size(); i++)
if (this->connections[i] == nullptr) {
channel_index = i;
break;
}
if (channel_index == -1) {
if (this->connections.size() == 0xFFFF) //channelid max 16 bits
return ERR_FAILED; //Too many channels
this->connections.push_back(nullptr);
channel_index = this->connections.size() - 1;
}
this->connections[channel_index] = conn;
*channel_id = channel_index;
return STATUS_NOERROR;
}
DWORD PandaJ2534Device::can_recv_thread() {
this->panda->can_clear(panda::PANDA_CAN_RX);
this->panda->can_rx_q_push(this->thread_kill_event);
return 0;
}
DWORD PandaJ2534Device::can_process_thread() {
panda::PANDA_CAN_MSG msg_recv[CAN_RX_MSG_LEN];
while (true) {
if (!WaitForSingleObject(this->thread_kill_event, 0)) {
break;
}
int count = 0;
this->panda->can_rx_q_pop(msg_recv, count);
if (count == 0) {
continue;
}
Squashed 'panda/' changes from ae816c104..45d0d286f 45d0d286f remove whitespace (#255) e49d0dbbd Pedal: use avg between tracks (#253) c597dcc89 VERSION update after health packet changes and minor misra test change 01072bec0 Misra 11.x: pointer usage. (#250) fd68c26ab Propagate can_push errors (#249) ce1daf206 Misra check only done for EON config 70d4fd7f6 cleanup docker container on failure 04756a037 Turning back Misra check: unvoluntarily change fcb1208e1 fix weird code in USB_ReadPacket b983cc8fe Re-wrote test_misra.sh 0b1920636 Misra 17.7: The value returned by a function having non-void return type shall be used. We should hang on initial failed safety_set_mode 06ee8bd2d Ignore Misra 5.4 until cppcheck bug (?) is fixed 4be858224 Update cppcheck commit and pass predefined params to avoid impossible combinations of configs f45dd04c6 cppcheck: ignore redundantAssignment and selfAssignment for registers in llcan.h 9ce631155 Misra 12.1 (operator order) and 10.4 (incompatible essential types) fixes, arised after properly checking UID_BASE config 1cd34e564 Explicitly set the define and undefine for unknown configs in misra checks 5a024999d remove esp flash from run_automated_tests.sh 23e3684d7 Cppcheck unused functions (#247) c97d60be6 Removed bad language b031480a7 Missed adding Dockerfile 91ff6bb21 Run language checker in CI 205ec342a Improved language checker f7bbab072 Language checker test d9d0a62f2 Misra 5.5: missed this change from previous PR 85fa3c02a Misra 5.5: Identifiers shall be distinct from macro names (#246) 190d604a0 Pedal: 2 minor fixes to Misra 15.7 (else needed) and 17.7 (non-void output must be used) 8ea01ff12 Pedal: no built-in functions to avoid puts and putc re-definitions 1f40d1e6a Misra pedal (#245) a4554e3e6 Ignore advisory Misra 19.2: the union keyword should not be used e6dc4172b Minor indent error 247e128b4 Fix strict compiler on bootstub build ba6856921 Removed build strict test and enabled -Werror flag da11f0fa0 safety replay: update openpilot-tools after logreader fix fc8b9e49b Cppcheck: also check pedal f7bd2c2f3 Misra 10.4: fix last 2 violations (#242) 9be5fdead finished misra 17.8 (#241) 3c3aba3db Misra 10.4: Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category (#240) f2a3a177b Misra 15_7: fix what seems to be a false positive of cppcheck 812ace538 Misra 15_7: if … else if constructs should be terminated with an else clause (#230) 79e9735ae rename 5_wifi_udp.py 3c3ff0c03 Update Jenkinsfile (#239) 1bd928437 Misra 17.7: the value returned by a function having non-void return shall be used (#237) 18c9e88bc Merge pull request #238 from commaai/misra_17_8 7ac321de4 Merge pull request #235 from commaai/memxxx 004e543cc Jenkins: run EON test first 4bff28620 Merge branch 'memxxx' of github.com:commaai/panda into memxxx 7cd80de23 typo 385e33b32 12.1 regression 955842bae WIP ea908cbb7 10_1 violations: Operands shall not be of an inappropriate essential type (#233) fa3303805 Fix Misra 5.3: An identifier declared in an inner scope shall not hide an identifier declared in an outer scope (#236) ebfe1c512 Merge branch 'master' of github.com:commaai/panda into memxxx 8c11470ef Fix EON test case 64e18e841 fix inverted logic to differentiate between dev and EON panda builds 36755a0fd Merge branch 'master' of github.com:commaai/panda into memxxx e2981d612 skip wifi tests for EON panda build db2eec98c Merge branch 'master' of github.com:commaai/panda into memxxx 11257e79a Ignore test 5_wifi_udp.py_ignore: too unreliable for now 6973c2a81 fix wifi tests cf6985ad1 memxxx function changes to be Misra compliant 3a6cd2930 wifi threshold perc back to 20%. Problem wasn't this. d92a03528 faster docker build for safety replay 3e9469b3e Fixing tests after min->MIN change ecb9b6c01 Revert "Misra 10 1: Operands shall not be of an inappropriate essential type (#232)" 8732e4faf Misra 10 1: Operands shall not be of an inappropriate essential type (#232) git-subtree-dir: panda git-subtree-split: 45d0d286f4b5c893590a98ec1438b8a143b9605d old-commit-hash: 1ff59baea717f88e8038c01ead864ad5acb85f35
6 years ago
for (int i = 0; i < count; i++) {
auto msg_in = msg_recv[i];
J2534Frame msg_out(msg_in);
if (msg_in.is_receipt) {
synchronized(task_queue_mutex) {
if (txMsgsAwaitingEcho.size() > 0) {
auto msgtx = txMsgsAwaitingEcho.front();
if (auto conn = msgtx->connection.lock()) {
if (conn->isProtoCan() && conn->getPort() == msg_in.bus) {
if (msgtx->checkTxReceipt(msg_out)) {
//Things to check:
// Frame not for this msg: Drop frame and alert. Error?
// Frame is for this msg, more tx frames required after a FC frame: Wait for FC frame to come and trigger next tx.
// Frame is for this msg, more tx frames required: Schedule next tx frame.
// Frame is for this msg, and is the final frame of the msg: Let conn process full msg, If another msg from this conn is available, register it.
txMsgsAwaitingEcho.pop(); //Remove the TX object and schedule record.
if (msgtx->isFinished()) {
this->removeConnectionTopAction(conn, msgtx);
} else {
if (msgtx->txReady()) { //Not finished, ready to send next frame.
msgtx->schedule(msg_in.recv_time_point, TRUE);
this->insertActionIntoTaskList(msgtx);
} else {
//Not finished, but next frame not ready (maybe waiting for flow control).
//Do not schedule more messages from this connection.
//this->ConnTxSet.erase(conn);
//Removing this means new messages queued can kickstart the queue and overstep the current message.
}
}
}
}
} else {
//Connection has died. Clear out the tx entry from device records.
txMsgsAwaitingEcho.pop();
this->ConnTxSet.erase(conn); //connection is already dead, no need to schedule future tx msgs.
}
}
}
} else {
for (auto& conn : this->connections)
if (conn != nullptr && conn->isProtoCan() && conn->getPort() == msg_in.bus)
conn->processMessage(msg_out);
}
}
}
return 0;
}
DWORD PandaJ2534Device::msg_tx_thread() {
const HANDLE subscriptions[] = { this->flow_control_wakeup_event, this->thread_kill_event };
DWORD sleepDuration = INFINITE;
while (TRUE) {
DWORD res = WaitForMultipleObjects(2, subscriptions, FALSE, sleepDuration);
if (res == WAIT_OBJECT_0 + 1) return 0;
if (res != WAIT_OBJECT_0 && res != WAIT_TIMEOUT) {
printf("Got an unexpected wait result in flow_control_write_thread. Res: %d; GetLastError: %d\n. Terminating thread.", res, GetLastError());
return 0;
}
ResetEvent(this->flow_control_wakeup_event);
while (TRUE) {
synchronized(task_queue_mutex) { //implemented with for loop. Consumes breaks.
if (this->task_queue.size() == 0) {
sleepDuration = INFINITE;
goto break_flow_ctrl_loop;
}
if (std::chrono::steady_clock::now() >= this->task_queue.front()->expire) {
auto task = this->task_queue.front(); //Get the scheduled tx record.
this->task_queue.pop_front();
task->execute();
} else { //Ran out of things that need to be sent now. Sleep!
auto time_diff = std::chrono::duration_cast<std::chrono::milliseconds>
(this->task_queue.front()->expire - std::chrono::steady_clock::now());
sleepDuration = MAX(1, time_diff.count());
goto break_flow_ctrl_loop;
}
}
}
break_flow_ctrl_loop:
continue;
}
return 0;
}
//Place the Action in the task queue based on the Action's expiration time,
//then signal the thread that processes actions.
void PandaJ2534Device::insertActionIntoTaskList(std::shared_ptr<Action> action) {
synchronized(task_queue_mutex) {
auto iter = this->task_queue.begin();
for (; iter != this->task_queue.end(); iter++) {
if (action->expire < (*iter)->expire) break;
}
this->task_queue.insert(iter, action);
}
SetEvent(this->flow_control_wakeup_event);
}
void PandaJ2534Device::scheduleAction(std::shared_ptr<Action> msg, BOOL startdelayed) {
if(startdelayed)
msg->scheduleImmediateDelay();
else
msg->scheduleImmediate();
this->insertActionIntoTaskList(msg);
}
void PandaJ2534Device::registerConnectionTx(std::shared_ptr<J2534Connection> conn) {
synchronized(connTXSet_mutex) {
auto ret = this->ConnTxSet.insert(conn);
if (ret.second == FALSE) return; //Conn already exists.
this->scheduleAction(conn->txbuff.front());
}
}
void PandaJ2534Device::unstallConnectionTx(std::shared_ptr<J2534Connection> conn) {
synchronized(connTXSet_mutex) {
auto ret = this->ConnTxSet.insert(conn);
if (ret.second == TRUE) return; //Conn already exists.
this->insertActionIntoTaskList(conn->txbuff.front());
}
}
void PandaJ2534Device::removeConnectionTopAction(std::shared_ptr<J2534Connection> conn, std::shared_ptr<MessageTx> msg) {
synchronized(task_queue_mutex) {
if (conn->txbuff.size() == 0)
return;
if (conn->txbuff.front() != msg)
return;
conn->txbuff.pop(); //Remove the top TX message from the connection tx queue.
//Remove the connection from the active connection list if no more messages are scheduled with this connection.
if (conn->txbuff.size() == 0) {
//Update records showing the connection no longer has a tx record scheduled.
this->ConnTxSet.erase(conn);
} else {
//Add the next scheduled tx from this conn
this->scheduleAction(conn->txbuff.front());
}
}
}