Skip to content

Commit

Permalink
Fix a bug with message flag sts on reception
Browse files Browse the repository at this point in the history
Relates to [MACCAN-106]
  • Loading branch information
mac-can committed Jan 15, 2022
1 parent d4b7b26 commit 4809b28
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
33 changes: 29 additions & 4 deletions Sources/Wrapper/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ static const char version[] = "CAN API V3 for PEAK PCAN-USB Interfaces, Version

/* ----------- options ------------------------------------------------
*/

#if (OPTION_CAN_2_0_ONLY != 0)
#error Compilation with legacy CAN 2.0 frame format!
#endif

#if (OPTION_CANAPI_PCBUSB_DYLIB != 0)
__attribute__((constructor))
static void _initializer() {
Expand Down Expand Up @@ -712,6 +717,9 @@ int can_read(int handle, can_msg_t *msg, uint16_t timeout)
return CANERR_NULLPTR;
if (can[handle].status.can_stopped) // must be running
return CANERR_OFFLINE;
memset(msg, 0, sizeof(can_msg_t)); // invalidate the message
msg->id = 0xFFFFFFFFU;
msg->sts = 1;

// blocking read
#if !defined(_WIN32) && !defined(_WIN64)
Expand Down Expand Up @@ -747,6 +755,7 @@ int can_read(int handle, can_msg_t *msg, uint16_t timeout)
}
if (!can[handle].mode.fdoe) { // CAN 2.0 message:
if ((can_msg.MSGTYPE & PCAN_MESSAGE_STATUS)) {
// TODO: encode status message (status)
can[handle].status.bus_off = (can_msg.DATA[3] & PCAN_ERROR_BUSOFF) != PCAN_ERROR_OK;
can[handle].status.bus_error = (can_msg.DATA[3] & PCAN_ERROR_BUSPASSIVE) != PCAN_ERROR_OK;
can[handle].status.warning_level = (can_msg.DATA[3] & PCAN_ERROR_BUSWARNING) != PCAN_ERROR_OK;
Expand All @@ -755,6 +764,7 @@ int can_read(int handle, can_msg_t *msg, uint16_t timeout)
return CANERR_RX_EMPTY; // receiver empty
}
if ((can_msg.MSGTYPE & PCAN_MESSAGE_ERRFRAME)) {
// TODO: encode status message (error frame)
can[handle].status.receiver_empty = 1;
can[handle].counters.err++;
return CANERR_ERR_FRAME; // error frame received
Expand All @@ -765,6 +775,7 @@ int can_read(int handle, can_msg_t *msg, uint16_t timeout)
msg->fdf = 0;
msg->brs = 0;
msg->esi = 0;
msg->sts = 0;
msg->dlc = (uint8_t)can_msg.LEN;
memcpy(msg->data, can_msg.DATA, CAN_MAX_LEN);
msec = ((uint64_t)timestamp.millis_overflow << 32) + (uint64_t)timestamp.millis;
Expand All @@ -791,6 +802,7 @@ int can_read(int handle, can_msg_t *msg, uint16_t timeout)
msg->fdf = (can_msg_fd.MSGTYPE & PCAN_MESSAGE_FD) ? 1 : 0;
msg->brs = (can_msg_fd.MSGTYPE & PCAN_MESSAGE_BRS) ? 1 : 0;
msg->esi = (can_msg_fd.MSGTYPE & PCAN_MESSAGE_ESI) ? 1 : 0;
msg->sts = 0;
msg->dlc = (uint8_t)can_msg_fd.DLC;
memcpy(msg->data, can_msg_fd.DATA, CANFD_MAX_LEN);
msg->timestamp.tv_sec = (time_t)(timestamp_fd / 1000000ull);
Expand Down Expand Up @@ -1363,10 +1375,9 @@ static int lib_parameter(uint16_t param, void *value, size_t nbyte)
case CANPROP_GET_BITRATE: // active bit-rate of the CAN controller (can_bitrate_t)
case CANPROP_GET_SPEED: // active bus speed of the CAN controller (can_speed_t)
case CANPROP_GET_STATUS: // current status register of the CAN controller (uint8_t)
case CANPROP_GET_BUSLOAD: // current bus load of the CAN controller (uint8_t)
case CANPROP_GET_BUSLOAD: // current bus load of the CAN controller (uint16_t)
case CANPROP_GET_NUM_CHANNELS: // numbers of CAN channels on the CAN interface (uint8_t)
case CANPROP_GET_CAN_CHANNEL: // active CAN channel on the CAN interface (uint8_t)
case CANPROP_GET_CAN_CLOCKS: // supported CAN clocks (in [Hz]) (int32_t[64])
case CANPROP_GET_TX_COUNTER: // total number of sent messages (uint64_t)
case CANPROP_GET_RX_COUNTER: // total number of reveiced messages (uint64_t)
case CANPROP_GET_ERR_COUNTER: // total number of reveiced error frames (uint64_t)
Expand Down Expand Up @@ -1496,14 +1507,22 @@ static int drv_parameter(int handle, uint16_t param, void *value, size_t nbyte)
}
}
break;
case CANPROP_GET_BUSLOAD: // current bus load of the CAN controller (uint8_t)
case CANPROP_GET_BUSLOAD: // current bus load of the CAN controller (uint16_t)
if (nbyte >= sizeof(uint8_t)) {
if ((rc = can_busload(handle, &load, NULL)) == CANERR_NOERROR) {
*(uint8_t*)value = (uint8_t)load;
if (nbyte > sizeof(uint8_t))
*(uint16_t*)value = (uint16_t)load * 100U; // 0 - 10000 ==> 0.00% - 100.00%
else
*(uint8_t*)value = (uint8_t)load; // 0 - 100 ==> 0.00% - 100.00%
rc = CANERR_NOERROR;
}
}
break;
case CANPROP_GET_NUM_CHANNELS: // numbers of CAN channels on the CAN interface (uint8_t)
case CANPROP_GET_CAN_CHANNEL: // active CAN channel on the CAN interface (uint8_t)
// TODO: insert coin here
rc = CANERR_NOTSUPP;
break;
case CANPROP_GET_TX_COUNTER: // total number of sent messages (uint64_t)
if (nbyte >= sizeof(uint64_t)) {
*(uint64_t*)value = (uint64_t)can[handle].counters.tx;
Expand All @@ -1522,6 +1541,12 @@ static int drv_parameter(int handle, uint16_t param, void *value, size_t nbyte)
rc = CANERR_NOERROR;
}
break;
case CANPROP_GET_RCV_QUEUE_SIZE: // maximum number of message the receive queue can hold (uint32_t)
case CANPROP_GET_RCV_QUEUE_HIGH: // maximum number of message the receive queue has hold (uint32_t)
case CANPROP_GET_RCV_QUEUE_OVFL: // overflow counter of the receive queue (uint64_t)
// note: cannot be determined
rc = CANERR_NOTSUPP; // FIXME: ?
break;
default:
if ((CANPROP_GET_VENDOR_PROP <= param) && // get a vendor-specific property value (void*)
(param < (CANPROP_GET_VENDOR_PROP + CANPROP_VENDOR_PROP_RANGE))) {
Expand Down
22 changes: 10 additions & 12 deletions Trial/Sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ int main(int argc, const char * argv[]) {
fprintf(stdout, "%s", (retVal == CCanApi::IllegalParameter) ? " (warning: Op.-Mode not supported)\n" : "\n");
}
if (option_info) {
retVal = myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS, (void*)&u8Val, sizeof(uint8_t));
if (retVal == CCanApi::NoError)
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS): value = %d\n", u8Val);
//else [optional property]
// fprintf(stderr, "+++ error: myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS) returned %i\n", retVal);
retVal = myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL, (void*)&u8Val, sizeof(uint8_t));
if (retVal == CCanApi::NoError)
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL): value = %u\n", u8Val + 1U);
//else [optional property]
// fprintf(stderr, "+++ error: myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL) returned %i\n", retVal);
retVal = myDriver.GetProperty(CANPROP_GET_DEVICE_TYPE, (void *)&i32Val, sizeof(int32_t));
if (retVal == CCanApi::NoError)
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_DEVICE_TYPE): value = %d\n", i32Val);
Expand All @@ -299,18 +309,6 @@ int main(int argc, const char * argv[]) {
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_DEVICE_DLLNAME): value = '%s'\n", szVal);
else
fprintf(stderr, "+++ error: myDriver.GetProperty(CANPROP_GET_DEVICE_DLLNAME) returned %i\n", retVal);
#if (0)
retVal = myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS, (void *)&u8Val, sizeof(uint8_t));
if (retVal == CCanApi::NoError)
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS): value = %d\n", u8Val);
else
fprintf(stderr, "+++ error: myDriver.GetProperty(CANPROP_GET_NUM_CHANNELS) returned %i\n", retVal);
retVal = myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL, (void *)&u8Val, sizeof(uint8_t));
if (retVal == CCanApi::NoError)
fprintf(stdout, ">>> myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL): value = %u\n", u8Val + 1U);
else
fprintf(stderr, "+++ error: myDriver.GetProperty(CANPROP_GET_CAN_CHANNEL) returned %i\n", retVal);
#endif
// vendor-specific properties
retVal = myDriver.GetProperty(PEAKCAN_PROPERTY_DEVICE_ID, (void *)&u32Val, sizeof(uint32_t));
if (retVal == CCanApi::NoError)
Expand Down
2 changes: 1 addition & 1 deletion Utilities/can_moni/Sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,5 +839,5 @@ static void version(FILE *stream, const char *program)
{
fprintf(stdout, "%s\n%s\n\n%s\n\n", APPLICATION, COPYRIGHT, LICENSE);
(void)program;
fprintf(stream, "Written by Uwe Vogt, UV Software, Berlin <http://www.uv-software.com/>\n");
fprintf(stream, "Written by Uwe Vogt, UV Software, Berlin <https://www.mac-can.net/>\n");
}
5 changes: 4 additions & 1 deletion Utilities/can_test/Sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ uint64_t CCanDriver::TransmitterTest(time_t duration, CANAPI_OpMode_t opMode, ui
uint64_t errors = 0;
uint64_t calls = 0;

memset(&message, 0, sizeof(CANAPI_Message_t));

fprintf(stderr, "\nPress ^C to abort.\n");
message.id = id;
message.xtd = 0;
Expand Down Expand Up @@ -793,6 +795,7 @@ uint64_t CCanDriver::TransmitterTest(uint64_t count, CANAPI_OpMode_t opMode, boo
uint64_t calls = 0;

srand((unsigned int)time(NULL));
memset(&message, 0, sizeof(CANAPI_Message_t));

fprintf(stderr, "\nPress ^C to abort.\n");
message.id = id;
Expand Down Expand Up @@ -1005,5 +1008,5 @@ static void version(FILE *stream, const char *program)
{
fprintf(stdout, "%s\n%s\n\n%s\n\n", APPLICATION, COPYRIGHT, LICENSE);
(void)program;
fprintf(stream, "Written by Uwe Vogt, UV Software, Berlin <http://www.uv-software.com/>\n");
fprintf(stream, "Written by Uwe Vogt, UV Software, Berlin <https://www.mac-can.net/>\n");
}

0 comments on commit 4809b28

Please sign in to comment.