-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorBackend.cpp
More file actions
353 lines (318 loc) · 14.4 KB
/
Copy pathVectorBackend.cpp
File metadata and controls
353 lines (318 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "VectorBackend.h"
#include <cstring>
#include <memory>
namespace cantrip {
namespace {
template <typename FnPtr>
bool bindProc(HMODULE mod, const char* name, FnPtr* out) {
*out = reinterpret_cast<FnPtr>(GetProcAddress(mod, name));
return *out != nullptr;
}
// vxlapi64.dll isn't always on the standard DLL search path even when
// Vector software is installed - confirmed on a real dev machine with
// Vector CANalyzer + Vector Platform Manager installed, where the DLL only
// exists under Platform Manager's own private driver folder. The dedicated
// "Vector XL Driver Library" redistributable does register it system-wide,
// so the bare-name load is tried first and should succeed on most setups;
// this is a fallback for the specific case observed here, not a guarantee
// it covers every possible Vector install layout.
HMODULE loadVxlApi() {
if (HMODULE mod = LoadLibraryA("vxlapi64.dll")) return mod;
return LoadLibraryA(
"C:\\Program Files (x86)\\Vector Platform Manager\\VtpDrivers\\Common\\vxlapi64.dll");
}
} // namespace
VectorBackend::VectorBackend(HMODULE module) : module_(module) {}
VectorBackend::~VectorBackend() {
for (auto& [channel, port] : portByChannel_) {
pDeactivateChannel_(port, channel);
pClosePort_(port);
}
if (pCloseDriver_) pCloseDriver_();
}
std::unique_ptr<VectorBackend> VectorBackend::load(std::string* error) {
HMODULE mod = loadVxlApi();
if (!mod) {
if (error) *error = "vxlapi64.dll not found. Install Vector's XL Driver "
"Library or a Vector driver package.";
return nullptr;
}
auto backend = std::unique_ptr<VectorBackend>(new VectorBackend(mod));
bool ok = true;
ok &= bindProc(mod, "xlOpenDriver", &backend->pOpenDriver_);
ok &= bindProc(mod, "xlCloseDriver", &backend->pCloseDriver_);
ok &= bindProc(mod, "xlGetDriverConfig", &backend->pGetDriverConfig_);
ok &= bindProc(mod, "xlOpenPort", &backend->pOpenPort_);
ok &= bindProc(mod, "xlClosePort", &backend->pClosePort_);
ok &= bindProc(mod, "xlActivateChannel", &backend->pActivateChannel_);
ok &= bindProc(mod, "xlDeactivateChannel", &backend->pDeactivateChannel_);
ok &= bindProc(mod, "xlCanSetChannelBitrate", &backend->pCanSetChannelBitrate_);
ok &= bindProc(mod, "xlReceive", &backend->pReceive_);
ok &= bindProc(mod, "xlCanTransmit", &backend->pCanTransmit_);
ok &= bindProc(mod, "xlGetErrorString", &backend->pGetErrorString_);
ok &= bindProc(mod, "xlCanFdSetConfiguration", &backend->pCanFdSetConfiguration_);
ok &= bindProc(mod, "xlCanReceive", &backend->pCanReceive_);
ok &= bindProc(mod, "xlCanTransmitEx", &backend->pCanTransmitEx_);
if (!ok) {
if (error) *error = "vxlapi64.dll found but missing expected exports; "
"wrong DLL version?";
FreeLibrary(mod);
return nullptr;
}
XLstatus status = backend->pOpenDriver_();
if (status != XL_SUCCESS) {
if (error) *error = backend->describeStatus(status);
FreeLibrary(mod);
return nullptr;
}
return backend;
}
std::string VectorBackend::describeStatus(XLstatus status) const {
if (pGetErrorString_) {
if (XLstringType text = pGetErrorString_(status)) {
return std::string(text);
}
}
return "Vector XL error " + std::to_string(status);
}
std::vector<CanChannelInfo> VectorBackend::enumerateChannels() const {
std::vector<CanChannelInfo> result;
auto driverConfig = std::make_unique<XLdriverConfig>();
XLstatus status = pGetDriverConfig_(driverConfig.get());
if (status != XL_SUCCESS) return result;
for (unsigned int i = 0; i < driverConfig->channelCount; ++i) {
const XLchannelConfig& ch = driverConfig->channel[i];
// channelBusCapabilities lists which bus types this channel could
// be activated as; only offer channels that can run classic CAN.
if (!(ch.channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN)) continue;
std::string name(ch.name, strnlen(ch.name, sizeof(ch.name)));
result.push_back(CanChannelInfo{ch.channelMask, name, true});
}
return result;
}
bool VectorBackend::initialize(uint64_t channelId, const CanBitrateConfig& config, std::string* error) {
auto accessMask = static_cast<XLaccess>(channelId);
char appName[XL_MAX_APPNAME] = "CANtrip";
XLportHandle portHandle = XL_INVALID_PORTHANDLE;
XLaccess permissionMask = accessMask;
// FD-capable ports must be opened negotiating interface version V4, not
// the V3 used for classic - confirmed against python-can's real Vector
// backend (verified source, see VectorBackend.h). Without this,
// xlCanReceive fails on every call with XL_ERR_INVALID_ACCESS even
// though xlOpenPort/xlCanFdSetConfiguration/xlActivateChannel all
// report success - a genuinely undocumented-in-the-header requirement
// this project hit for real, not a hypothetical.
const unsigned int interfaceVersion = config.fd ? XL_INTERFACE_VERSION_V4 : XL_INTERFACE_VERSION;
XLstatus status = pOpenPort_(&portHandle, appName, accessMask, &permissionMask,
8192, interfaceVersion, XL_BUS_TYPE_CAN);
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
return false;
}
if (config.fd) {
// XLcanFdConf takes bit-timing tick values (sjw/tseg1/tseg2), not a
// prescaler - unlike PCAN-Basic's init string, Vector's struct has
// no BRP field, so config.nominalTiming.brp/dataTiming.brp are
// unused here; the driver derives its own prescaler from bitrate +
// (1+tseg1+tseg2). Timing is computed by CanBitTiming (see
// AVlabsCanBackend.h) rather than the fixed defaults this backend
// used before real bit-timing support existed.
XLcanFdConf fdConf{};
fdConf.arbitrationBitRate = config.nominalBitrateBps;
fdConf.sjwAbr = config.nominalTiming.sjw;
fdConf.tseg1Abr = config.nominalTiming.tseg1;
fdConf.tseg2Abr = config.nominalTiming.tseg2;
fdConf.dataBitRate = config.dataBitrateBps;
fdConf.sjwDbr = config.dataTiming.sjw;
fdConf.tseg1Dbr = config.dataTiming.tseg1;
fdConf.tseg2Dbr = config.dataTiming.tseg2;
status = pCanFdSetConfiguration_(portHandle, accessMask, &fdConf);
} else {
status = pCanSetChannelBitrate_(portHandle, accessMask, config.nominalBitrateBps);
}
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
pClosePort_(portHandle);
return false;
}
status = pActivateChannel_(portHandle, accessMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_NONE);
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
pClosePort_(portHandle);
return false;
}
portByChannel_[accessMask] = portHandle;
fdByChannel_[accessMask] = config.fd;
return true;
}
void VectorBackend::uninitialize(uint64_t channelId) {
auto accessMask = static_cast<XLaccess>(channelId);
auto it = portByChannel_.find(accessMask);
if (it == portByChannel_.end()) return;
pDeactivateChannel_(it->second, accessMask);
pClosePort_(it->second);
portByChannel_.erase(it);
fdByChannel_.erase(accessMask);
}
bool VectorBackend::readFrame(uint64_t channelId, CanFrame* out, std::string* error) {
auto accessMask = static_cast<XLaccess>(channelId);
auto it = portByChannel_.find(accessMask);
if (it == portByChannel_.end()) {
if (error) *error = "channel not initialized";
return false;
}
bool fd = fdByChannel_.count(accessMask) ? fdByChannel_[accessMask] : false;
return fd ? readFd(it->second, out, error) : readClassic(it->second, out, error);
}
bool VectorBackend::writeFrame(uint64_t channelId, const CanFrame& frame, std::string* error) {
auto accessMask = static_cast<XLaccess>(channelId);
auto it = portByChannel_.find(accessMask);
if (it == portByChannel_.end()) {
if (error) *error = "channel not initialized";
return false;
}
bool fd = fdByChannel_.count(accessMask) ? fdByChannel_[accessMask] : false;
return fd ? writeFd(it->second, accessMask, frame, error) : writeClassic(it->second, accessMask, frame, error);
}
bool VectorBackend::readClassic(XLportHandle port, CanFrame* out, std::string* error) const {
XLevent event{};
unsigned int eventCount = 1;
XLstatus status = pReceive_(port, &eventCount, &event);
if (status == XL_ERR_QUEUE_IS_EMPTY || eventCount == 0) {
return false; // no frame available right now, not an error
}
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
return false;
}
if (event.tag != XL_RECEIVE_MSG) {
return false; // chip-state/other non-data event; nothing to decode
}
const s_xl_can_msg& msg = event.tagData.msg;
// Classic error frames arrive as a normal XL_RECEIVE_MSG event with this
// flag set (confirmed in the real vxlapi.h - XL_CAN_MSG_FLAG_ERROR_FRAME),
// not as a separate event type. No further detail is available at this
// level (unlike the FD path below), so this reports an unspecified
// protocol violation - still enough for auto-detect to tell "errors are
// happening" from "clean bus".
if (msg.flags & XL_CAN_MSG_FLAG_ERROR_FRAME) {
out->error = true;
out->id = CanErr::kProt;
std::memset(out->data, 0, sizeof(out->data));
out->timestampUs = event.timeStamp / 1000ull;
return true;
}
out->extended = (msg.id & XL_CAN_EXT_MSG_ID) != 0;
out->id = msg.id & ~XL_CAN_EXT_MSG_ID;
out->rtr = (msg.flags & XL_CAN_MSG_FLAG_REMOTE_FRAME) != 0;
out->fd = false;
out->brs = false;
out->esi = false;
out->dlc = static_cast<uint8_t>(msg.dlc);
std::memcpy(out->data, msg.data, sizeof(msg.data));
// XLevent timestamps are nanoseconds since driver start; CanFrame wants
// microseconds.
out->timestampUs = event.timeStamp / 1000ull;
return true;
}
bool VectorBackend::readFd(XLportHandle port, CanFrame* out, std::string* error) const {
XLcanRxEvent event{};
XLstatus status = pCanReceive_(port, &event);
if (status == XL_ERR_QUEUE_IS_EMPTY) {
return false; // no frame available right now, not an error
}
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
return false;
}
// XL_CAN_EV_TAG_RX_ERROR carries real per-error detail (XL_CAN_EV_ERROR::
// errorCode, confirmed in the real vxlapi.h) - map it to the closest
// SocketCAN CAN_ERR_* bit rather than dropping it silently, the same
// way this exact event was used to diagnose a real FD sample-point
// mismatch on a live Vector VN7640 earlier this project. ACK/NACK are
// their own top-level error class in the SocketCAN convention, not a
// CAN_ERR_PROT sub-type; anything else without a specific mapped bit
// still reports as an unspecified protocol violation rather than being
// dropped, since "some kind of error happened" is still useful signal
// (e.g. for auto-detect telling a clean bus from a mismatched one).
if (event.tag == XL_CAN_EV_TAG_RX_ERROR) {
const XL_CAN_EV_ERROR& err = event.tagData.canError;
out->error = true;
std::memset(out->data, 0, sizeof(out->data));
switch (err.errorCode) {
case XL_CAN_ERRC_ACK_ERROR:
case XL_CAN_ERRC_NACK_ERROR:
out->id = CanErr::kAck;
break;
case XL_CAN_ERRC_BIT_ERROR:
out->id = CanErr::kProt;
out->data[2] = CanErr::kProtBit;
break;
case XL_CAN_ERRC_FORM_ERROR:
out->id = CanErr::kProt;
out->data[2] = CanErr::kProtForm;
break;
case XL_CAN_ERRC_STUFF_ERROR:
out->id = CanErr::kProt;
out->data[2] = CanErr::kProtStuff;
break;
case XL_CAN_ERRC_OVLD_ERROR:
out->id = CanErr::kProt;
out->data[2] = CanErr::kProtOverload;
break;
default:
out->id = CanErr::kProt;
break;
}
out->timestampUs = event.timeStampSync / 1000ull;
return true;
}
if (event.tag != XL_CAN_EV_TAG_RX_OK) {
return false; // chip-state/other non-data event; nothing to decode
}
const XL_CAN_EV_RX_MSG& msg = event.tagData.canRxOkMsg;
out->extended = (msg.canId & XL_CAN_EXT_MSG_ID) != 0;
out->id = msg.canId & ~XL_CAN_EXT_MSG_ID;
out->rtr = (msg.msgFlags & XL_CAN_RXMSG_FLAG_RTR) != 0;
out->fd = (msg.msgFlags & XL_CAN_RXMSG_FLAG_EDL) != 0;
out->brs = (msg.msgFlags & XL_CAN_RXMSG_FLAG_BRS) != 0;
out->esi = (msg.msgFlags & XL_CAN_RXMSG_FLAG_ESI) != 0;
out->dlc = msg.dlc;
std::memcpy(out->data, msg.data, sizeof(msg.data));
// timeStampSync is nanoseconds; CanFrame wants microseconds.
out->timestampUs = event.timeStampSync / 1000ull;
return true;
}
bool VectorBackend::writeClassic(XLportHandle port, XLaccess accessMask, const CanFrame& frame, std::string* error) const {
XLevent event{};
event.tag = XL_TRANSMIT_MSG;
event.tagData.msg.id = frame.extended ? (frame.id | XL_CAN_EXT_MSG_ID) : frame.id;
event.tagData.msg.flags = frame.rtr ? XL_CAN_MSG_FLAG_REMOTE_FRAME : 0;
event.tagData.msg.dlc = frame.dlc;
std::memcpy(event.tagData.msg.data, frame.data, sizeof(event.tagData.msg.data));
unsigned int eventCount = 1;
XLstatus status = pCanTransmit_(port, accessMask, &eventCount, &event);
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
return false;
}
return true;
}
bool VectorBackend::writeFd(XLportHandle port, XLaccess accessMask, const CanFrame& frame, std::string* error) const {
XLcanTxEvent event{};
event.tag = XL_CAN_EV_TAG_TX_MSG;
event.tagData.canMsg.canId = frame.extended ? (frame.id | XL_CAN_EXT_MSG_ID) : frame.id;
event.tagData.canMsg.msgFlags = XL_CAN_TXMSG_FLAG_EDL
| (frame.brs ? XL_CAN_TXMSG_FLAG_BRS : 0)
| (frame.rtr ? XL_CAN_TXMSG_FLAG_RTR : 0);
event.tagData.canMsg.dlc = frame.dlc;
std::memcpy(event.tagData.canMsg.data, frame.data, sizeof(event.tagData.canMsg.data));
unsigned int sentCount = 0;
XLstatus status = pCanTransmitEx_(port, accessMask, 1, &sentCount, &event);
if (status != XL_SUCCESS) {
if (error) *error = describeStatus(status);
return false;
}
return true;
}
} // namespace cantrip