Skip to content

Commit 24b61c1

Browse files
committed
replace broken memcpy with std::copy, some reformatting
1 parent 4b2b607 commit 24b61c1

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/main/CanVendorSystec.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "CanVendorSystec.h"
22

3+
#include <algorithm>
34
#include <time.h>
45
#include <LogIt.h>
56
#include <iomanip>
7+
#include <vector>
68

79
std::mutex CanVendorSystec::m_handles_lock;
810
std::unordered_map<int, tUcanHandle> CanVendorSystec::m_handle_map;
@@ -104,43 +106,40 @@ CanReturnCode CanVendorSystec::vendor_close() noexcept {
104106
erase_module_handle(m_module_number);
105107
m_receive_thread_flag = false;
106108
if (m_SystecRxThread.joinable()) m_SystecRxThread.join();
107-
UcanDeinitCanEx (m_UcanHandle, (BYTE)m_channel_number);
109+
UcanDeinitCanEx (m_UcanHandle, (BYTE) m_channel_number);
108110
LOG(Log::DBG, CanLogIt::h()) << __FUNCTION__ << " closed successfully";
109111
return CanReturnCode::success;
110112
};
111113

112114
CanReturnCode CanVendorSystec::vendor_send(const CanFrame& frame) noexcept {
113-
bool rtr = frame.is_remote_request();
114115
uint32_t len = frame.length();
115-
char *message = frame.message().data();
116-
short cobID = frame.id();
117-
118-
LOG(Log::DBG, CanLogIt::h()) << "Sending message: [" << ( message == 0 ? "" : (const char *) message) << "], cobID: [" << cobID << "], Message Length: [" << static_cast<int>(len) << "]";
116+
std::vector<char> message = frame.message();
119117

120118
tCanMsgStruct can_msg_to_send;
121119
BYTE Status;
122120

123-
can_msg_to_send.m_dwID = cobID;
121+
can_msg_to_send.m_dwID = frame.id();
124122
can_msg_to_send.m_bDLC = len;
125123
can_msg_to_send.m_bFF = 0;
126-
if (rtr) {
127-
can_msg_to_send.m_bFF = USBCAN_MSG_FF_RTR;
124+
if (frame.is_remote_request()) {
125+
can_msg_to_send.m_bFF = USBCAN_MSG_FF_RTR;
128126
}
129127
int message_length_to_process;
130128
//If there is more than 8 characters to process, we process 8 of them in this iteration of the loop
131129
if (len > 8) {
132-
message_length_to_process = 8;
133-
LOG(Log::DBG, CanLogIt::h()) << "The length is more then 8 bytes, adjust to 8, ignore >8. len= " << len;
130+
message_length_to_process = 8;
131+
LOG(Log::DBG, CanLogIt::h()) << "The length is more than 8 bytes, adjust to 8, ignore > 8. len = " << len;
134132
} else {
135-
//Otherwise if there is less than 8 characters to process, we process all of them in this iteration of the loop
136-
message_length_to_process = len;
137-
if (len < 8) {
138-
LOG(Log::DBG, CanLogIt::h())<< "The length is less then 8 bytes, process only. len= " << len;
139-
}
133+
//Otherwise if there is less than 8 characters to process, we process all of them in this iteration of the loop
134+
message_length_to_process = len;
135+
if (len < 8) {
136+
LOG(Log::DBG, CanLogIt::h())<< "The length is less than 8 bytes, process only. len = " << len;
137+
}
140138
}
141139
can_msg_to_send.m_bDLC = message_length_to_process;
142-
memcpy(can_msg_to_send.m_bData, message, message_length_to_process);
143-
// MLOG(TRC,this) << "Channel Number: [" << m_channel_number << "], cobID: [" << can_msg_to_send.m_dwID << "], Message Length: [" << static_cast<int>(can_msg_to_send.m_bDLC) << "]";
140+
if (message_length_to_process)
141+
std::copy(message.begin(), message.begin() + message_length_to_process, can_msg_to_send.m_bData);
142+
144143
Status = UcanWriteCanMsgEx(m_UcanHandle, m_channel_number, &can_msg_to_send, NULL);
145144
if (Status != USBCAN_SUCCESSFUL) {
146145
LOG(Log::ERR, CanLogIt::h()) << "There was a problem when sending a message: "
@@ -225,7 +224,7 @@ int CanVendorSystec::SystecRxThread()
225224
tCanMsgStruct read_can_message;
226225
LOG(Log::DBG, CanLogIt::h()) << "SystecRxThread Started. m_receive_thread_flag = [" << m_receive_thread_flag <<"]";
227226
while (m_receive_thread_flag) {
228-
status = UcanReadCanMsgEx(m_UcanHandle, (BYTE *)&m_channel_number, &read_can_message, NULL);
227+
status = UcanReadCanMsgEx(m_UcanHandle, (BYTE *) &m_channel_number, &read_can_message, NULL);
229228
switch (status) {
230229
case USBCAN_WARN_SYS_RXOVERRUN: [[ fallthrough ]];
231230
case USBCAN_WARN_DLL_RXOVERRUN: [[ fallthrough ]];

0 commit comments

Comments
 (0)