Skip to content

Commit ca17601

Browse files
committed
Attempt to fix warnings as reported by Codacy.
1 parent 08af5d4 commit ca17601

3 files changed

Lines changed: 40 additions & 33 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ write code that is portable across different platforms and CAN hardware.
1010
It is not yet fully functional and may contain bugs and the API is
1111
not yet stable and may change without deprecation.
1212

13-
## Hardware donation? Yes, please!
13+
## Hardware donation? Yes, please
1414

1515
Every great abstraction layer is only as good as the hardware it has been tested
1616
against. If you have CAN interfaces collecting dust on a shelf - be it a

src/drivers/CANvenient_Kvaser.c

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,20 @@ int kvaser_find_interfaces(void)
7878
return -1;
7979
}
8080
ch_info->channel = i;
81-
ch_info->handle = canINVALID_HANDLE;
81+
ch_info->handle = canINVALID_HANDLE;
8282

83-
can_interface[i].name = (char*)malloc(strlen(device_name) + 1);
83+
can_interface[i].name = (char*)malloc(strnlen(device_name, sizeof(device_name)) + 1);
8484
if (NULL == can_interface[i].name)
8585
{
8686
set_error_reason("Memory allocation failed.");
8787
free(ch_info);
8888
return -1;
8989
}
90-
strcpy_s(can_interface[i].name, strlen(device_name) + 1, device_name);
90+
strncpy_s(can_interface[i].name, strlen(device_name) + 1, device_name, _TRUNCATE);
9191

9292
can_interface[i].internal = ch_info;
93-
can_interface[i].vendor = CAN_VENDOR_KVASER;
94-
can_interface[i].opened = 0;
93+
can_interface[i].vendor = CAN_VENDOR_KVASER;
94+
can_interface[i].opened = 0;
9595
can_interface[i].baudrate = CAN_BAUD_1M;
9696
}
9797

@@ -131,7 +131,7 @@ int kvaser_open(int index)
131131
return -1;
132132
}
133133

134-
ch_info->handle = hnd;
134+
ch_info->handle = hnd;
135135
can_interface[index].opened = 1;
136136
return 0;
137137

@@ -155,7 +155,7 @@ void kvaser_close(int index)
155155

156156
canBusOff(ch_info->handle);
157157
canClose(ch_info->handle);
158-
ch_info->handle = canINVALID_HANDLE;
158+
ch_info->handle = canINVALID_HANDLE;
159159
can_interface[index].opened = 0;
160160

161161
#else
@@ -225,9 +225,9 @@ int kvaser_recv(int index, struct can_frame* frame, u64* timestamp)
225225
return -1;
226226
}
227227

228-
frame->can_id = (canid_t)id;
228+
frame->can_id = (canid_t)id;
229229
frame->can_dlc = (u8)dlc;
230-
*timestamp = (u64)time * 1000ULL; /* ms to µs */
230+
*timestamp = (u64)time * 1000ULL; /* ms to µs */
231231

232232
return 0;
233233

@@ -245,21 +245,36 @@ static long baudrate_to_canlib(enum can_baudrate baud)
245245
{
246246
switch (baud)
247247
{
248-
case CAN_BAUD_1M: return canBITRATE_1M;
249-
case CAN_BAUD_800K: return canBITRATE_1M; /* no 800K predefined, fall back to 1M */
250-
case CAN_BAUD_500K: return canBITRATE_500K;
251-
case CAN_BAUD_250K: return canBITRATE_250K;
252-
case CAN_BAUD_125K: return canBITRATE_125K;
253-
case CAN_BAUD_100K: return canBITRATE_100K;
254-
case CAN_BAUD_95K: return canBITRATE_83K; /* closest predefined */
255-
case CAN_BAUD_83K: return canBITRATE_83K;
256-
case CAN_BAUD_50K: return canBITRATE_50K;
257-
case CAN_BAUD_47K: return canBITRATE_50K; /* closest predefined */
258-
case CAN_BAUD_33K: return canBITRATE_62K; /* closest predefined */
259-
case CAN_BAUD_20K: return canBITRATE_10K; /* closest predefined */
260-
case CAN_BAUD_10K: return canBITRATE_10K;
261-
case CAN_BAUD_5K: return canBITRATE_10K; /* closest predefined */
262-
default: return canBITRATE_1M;
248+
case CAN_BAUD_1M:
249+
return canBITRATE_1M;
250+
case CAN_BAUD_800K:
251+
return canBITRATE_1M; /* no 800K predefined, fall back to 1M */
252+
case CAN_BAUD_500K:
253+
return canBITRATE_500K;
254+
case CAN_BAUD_250K:
255+
return canBITRATE_250K;
256+
case CAN_BAUD_125K:
257+
return canBITRATE_125K;
258+
case CAN_BAUD_100K:
259+
return canBITRATE_100K;
260+
case CAN_BAUD_95K:
261+
return canBITRATE_83K; /* closest predefined */
262+
case CAN_BAUD_83K:
263+
return canBITRATE_83K;
264+
case CAN_BAUD_50K:
265+
return canBITRATE_50K;
266+
case CAN_BAUD_47K:
267+
return canBITRATE_50K; /* closest predefined */
268+
case CAN_BAUD_33K:
269+
return canBITRATE_62K; /* closest predefined */
270+
case CAN_BAUD_20K:
271+
return canBITRATE_10K; /* closest predefined */
272+
case CAN_BAUD_10K:
273+
return canBITRATE_10K;
274+
case CAN_BAUD_5K:
275+
return canBITRATE_10K; /* closest predefined */
276+
default:
277+
return canBITRATE_1M;
263278
}
264279
}
265280

src/tests/example.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
#include <stdio.h>
1313
#include <stdlib.h>
1414

15-
#ifdef _WIN32
16-
#include <windows.h>
17-
#define sleep_ms(ms) Sleep(ms)
18-
#else
19-
#include <unistd.h>
20-
#define sleep_ms(ms) usleep((ms) * 1000)
21-
#endif
22-
2315
int main()
2416
{
2517
if (0 == can_find_interfaces())

0 commit comments

Comments
 (0)