-
Notifications
You must be signed in to change notification settings - Fork 2
/
canal.cpp
409 lines (336 loc) · 12.3 KB
/
canal.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
MIT License
Copyright (c) 2000-2023 Åke Hedman, Grodans Paradis AB
Copyright (c) 2005-2023 Gediminas Simanskis, Rusoku technologijos UAB ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "windows.h"
#include "include/canal.h"
#include "include/canal_macro.h"
#include "include/debug.h"
#include "include/CDllDrvObj.h"
#include "include/CAN_interface_list.h"
#include <strsafe.h>
//https://learn.microsoft.com/en-us/windows/win32/Dlls/dynamic-link-library-best-practices
static CDllDrvObj* theApp;
//CAN_DEV_LIST CanDevList;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: {
theApp = new CDllDrvObj();
theApp->InitInstance();
break;
}
case DLL_THREAD_ATTACH: {
break;
}
case DLL_THREAD_DETACH: {
break;
}
case DLL_PROCESS_DETACH: {
if (lpReserved != nullptr) {
break; // do not do cleanup if process termination scenario
}
theApp->RemoveAllObjects();
delete theApp;
break;
default:
break;
}
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetDeviceList
//
DllExport int WINAPI CanalGetDeviceList(pcanal_dev_list canalDeviceList, int canalDeviceListSize){
if(canalDeviceList == nullptr)
return CANAL_ERROR_MEMORY;
CAN_DEV_LIST canDeviceList = {};
unsigned int devcnt;
CAN_interface_list(&canDeviceList);
devcnt = canDeviceList.canDevCount % CANAL_DEVLIST_SIZE_MAX;
canalDeviceList->canDevCount = devcnt;
for(unsigned int x = 0; x < devcnt; x++){
canalDeviceList->canDevInfo[x].DeviceId = 0;
canalDeviceList->canDevInfo[x].pid = canDeviceList.canDevInfo[x].pid;
canalDeviceList->canDevInfo[x].vid = canDeviceList.canDevInfo[x].vid;
StringCbCopyA(canalDeviceList->canDevInfo[x].SerialNumber,
sizeof(canalDeviceList->canDevInfo[x].SerialNumber)/sizeof(canalDeviceList->canDevInfo[x].SerialNumber[0]),
canDeviceList.canDevInfo[x].SerialNumber);
}
return CANAL_ERROR_SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////
// CanalOpen
//
DllExport long WINAPI CanalOpen(const char *pDevice, unsigned long flags)
{
long h = 0;
auto pDrvObj = new CTouCANObj();
if((h = theApp->AddDriverObject(pDrvObj)) <= 0) {
h = 0;
delete pDrvObj;
} else {
if (pDrvObj->Open(pDevice, flags, TRUE) == FALSE) {
theApp->RemoveDriverObject(h);
h = 0;
}
}
return h;
}
///////////////////////////////////////////////////////////////////////////////
// CanalClose
//
DllExport long WINAPI CanalClose(long handle)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj)
return CANAL_ERROR_MEMORY;
pDrvObj->Close();
theApp->RemoveDriverObject(handle);
return CANAL_ERROR_SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetLevel
//
DllExport long WINAPI CanalGetLevel(long handle)
{
return CANAL_LEVEL_STANDARD;
}
///////////////////////////////////////////////////////////////////////////////
// CanalSend
//
DllExport int WINAPI CanalSend(long handle, PCANALMSG pCanalMsg)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->WriteMsg(pCanalMsg));
}
///////////////////////////////////////////////////////////////////////////////
// CanalSend blocking
//
DllExport int WINAPI CanalBlockingSend(long handle, PCANALMSG pCanalMsg, unsigned long timeout)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->WriteMsgBlocking(pCanalMsg, timeout));
}
///////////////////////////////////////////////////////////////////////////////
// CanalReceive
//
DllExport int WINAPI CanalReceive(long handle, PCANALMSG pCanalMsg)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->ReadMsg(pCanalMsg));
}
///////////////////////////////////////////////////////////////////////////////
// CanalReceive blocking
//
DllExport int WINAPI CanalBlockingReceive(long handle, PCANALMSG pCanalMsg, unsigned long timeout)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->ReadMsgBlocking(pCanalMsg, timeout));
}
///////////////////////////////////////////////////////////////////////////////
// CanalDataAvailable
//
DllExport int WINAPI CanalDataAvailable(long handle)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return pDrvObj->DataAvailable();
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetStatus
//
DllExport int WINAPI CanalGetStatus(long handle, PCANALSTATUS pCanalStatus)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj)
return CANAL_ERROR_MEMORY;
return (pDrvObj->GetStatus(pCanalStatus));
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetStatistics
//
DllExport int WINAPI CanalGetStatistics(long handle, PCANALSTATISTICS pCanalStatistics)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj)
return CANAL_ERROR_MEMORY;
return (pDrvObj->GetStatistics(pCanalStatistics));
}
///////////////////////////////////////////////////////////////////////////////
// CanalSetFilter
//
DllExport int WINAPI CanalSetFilter(long handle, unsigned long filter)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->SetFilter(filter) ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC);
}
///////////////////////////////////////////////////////////////////////////////
// CanalSetMask
//
DllExport int WINAPI CanalSetMask(long handle, unsigned long mask)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->SetMask(mask) ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC);
}
///////////////////////////////////////////////////////////////////////////////
// CanalSetBaudrate
//
DllExport int WINAPI CanalSetBaudrate(long handle, unsigned long baudrate)
{
// Not supported in this DLL
return CANAL_ERROR_NOT_SUPPORTED;
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetVersion
//
DllExport unsigned long WINAPI CanalGetVersion(void)
{
unsigned long version;
unsigned char* p = (unsigned char *)&version;
*p = CANAL_MAIN_VERSION;
*(p + 1) = CANAL_MINOR_VERSION;
*(p + 2) = CANAL_SUB_VERSION;
*(p + 3) = 0;
return version;
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetDllVersion
//
DllExport unsigned long WINAPI CanalGetDllVersion(void)
{
unsigned long version;
unsigned char *p = (unsigned char *)&version;
*p = DLL_MAIN_VERSION;
*(p + 1) = DLL_MINOR_VERSION;
*(p + 2) = DLL_SUB_VERSION;
*(p + 3) = 0;
return version;
}
///////////////////////////////////////////////////////////////////////////////
// CanalGetVendorString
//
DllExport const char* WINAPI CanalGetVendorString(void)
{
static char r_str[256];
CTouCANObj *pDrvObj = theApp->GetDriverObject(1681);
if (nullptr == pDrvObj)
return nullptr;
strcpy_s(r_str, 256, pDrvObj->GetVendorString());
return r_str;
}
///////////////////////////////////////////////////////////////////////////////
//Get CANAL driver properties
//
DllExport const char* WINAPI CanalGetDriverInfo(void)
{
return "\n";
}
//---------------------------- A type CANAL API list ------------------------------
/*!
Set the 11bit filter ID, Mask for a CANAL channel
@param handle Handle to open physical CANAL channel.
@return zero on success or error-code on failure.
*/
DllExport int WINAPI CanalSetFilter11bit(long handle, Filter_Type_TypeDef type, unsigned long list, unsigned long mask)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->SetFilter11bit(type, list, mask));
}
/*!
Set the 29bit filter ID, Mask for a CANAL channel
@param handle Handle to open physical CANAL channel.
@return zero on success or error-code on failure.
*/
DllExport int WINAPI CanalSetFilter29bit(long handle, Filter_Type_TypeDef type, unsigned long list, unsigned long mask)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->SetFilter29bit(type, list, mask)); // ? CANAL_ERROR_SUCCESS : CANAL_ERROR_GENERIC);
}
/*!
Get bootloader ver
@param handle Handle to open physical CANAL channel.
@return zero on success or error-code on failure.
*/
DllExport int WINAPI CanalGetBootloaderVersion(long handle, unsigned long *bootloader_version)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetBootloaderVersion(bootloader_version));
}
DllExport int WINAPI CanalGetHardwareVersion(long handle, unsigned long *hardware_version)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetHardwareVersion(hardware_version));
}
DllExport int WINAPI CanalGetFirmwareVersion(long handle, unsigned long *firmware_version)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetFirmwareVersion(firmware_version));
}
DllExport int WINAPI CanalGetSerialNumber(long handle, unsigned long *serial)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetSerialNumber(serial));
}
DllExport int WINAPI CanalGetVidPid(long handle, unsigned long *vidpid)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetVidPid(vidpid));
}
DllExport int WINAPI CanalGetDeviceId(long handle, unsigned long *deviceid)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return 0;
return (pDrvObj->GetDeviceId(deviceid));
}
DllExport int WINAPI CanalGetVendor(long handle, unsigned int size, char *vendor)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->GetVendor(size, vendor));
}
DllExport int WINAPI CanalInterfaceStart(long handle)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->InterfaceStart());
}
DllExport int WINAPI CanalInterfaceStop(long handle)
{
CTouCANObj *pDrvObj = theApp->GetDriverObject(handle);
if (nullptr == pDrvObj) return CANAL_ERROR_MEMORY;
return (pDrvObj->InterfaceStop());
}