From 6ab9fd9dc758f4833bceb2eb74e65884ff6b4222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Wed, 26 May 2021 16:59:25 +0200 Subject: [PATCH] CAN: Fix ISO-TP net buffer handling Also setting ISOTP buffers to default values. --- .../arm/mppt_2420_hc/mppt_2420_hc_defconfig | 3 - .../arm/mppt_2420_rc/mppt_2420_rc_defconfig | 3 - .../arm/nucleo_g474re/nucleo_g474re_defconfig | 3 - src/ext/can.cpp | 76 ++++++++++--------- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig index 352a07d..6bb24ee 100644 --- a/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig @@ -62,6 +62,3 @@ CONFIG_CAN=y CONFIG_THINGSET_CAN=y CONFIG_ISOTP=y -CONFIG_ISOTP_RX_BUF_COUNT=2 -CONFIG_ISOTP_RX_BUF_SIZE=100 -CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2 diff --git a/boards/arm/mppt_2420_rc/mppt_2420_rc_defconfig b/boards/arm/mppt_2420_rc/mppt_2420_rc_defconfig index 480413b..fa6944e 100644 --- a/boards/arm/mppt_2420_rc/mppt_2420_rc_defconfig +++ b/boards/arm/mppt_2420_rc/mppt_2420_rc_defconfig @@ -64,6 +64,3 @@ CONFIG_CAN=y CONFIG_THINGSET_CAN=y CONFIG_ISOTP=y -CONFIG_ISOTP_RX_BUF_COUNT=1 -CONFIG_ISOTP_RX_BUF_SIZE=100 -CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2 diff --git a/boards/arm/nucleo_g474re/nucleo_g474re_defconfig b/boards/arm/nucleo_g474re/nucleo_g474re_defconfig index 86c82bf..d9910c4 100644 --- a/boards/arm/nucleo_g474re/nucleo_g474re_defconfig +++ b/boards/arm/nucleo_g474re/nucleo_g474re_defconfig @@ -63,6 +63,3 @@ CONFIG_CAN=y CONFIG_THINGSET_CAN=y CONFIG_ISOTP=y -CONFIG_ISOTP_RX_BUF_COUNT=2 -CONFIG_ISOTP_RX_BUF_SIZE=100 -CONFIG_ISOTP_RX_SF_FF_BUF_COUNT=2 diff --git a/src/ext/can.cpp b/src/ext/can.cpp index 4783655..7d161ec 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -19,10 +19,9 @@ #include LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL); -#include "hardware.h" // for software watchdog - -#include "thingset.h" #include "data_nodes.h" +#include "hardware.h" +#include "thingset.h" #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), can_en)) #define CAN_EN_GPIO DT_CHILD(DT_PATH(outputs), can_en) @@ -31,14 +30,17 @@ LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL); extern ThingSet ts; extern uint16_t can_node_addr; -const struct device *can_dev; +static const struct device *can_dev; #ifdef CONFIG_ISOTP #define RX_THREAD_STACK_SIZE 1024 #define RX_THREAD_PRIORITY 2 -const struct isotp_fc_opts fc_opts = {.bs = 8, .stmin = 0}; +const struct isotp_fc_opts fc_opts = { + .bs = 8, // block size + .stmin = 1 // minimum separation time = 100 ms +}; struct isotp_msg_id rx_addr = { .id_type = CAN_EXTENDED_IDENTIFIER, @@ -52,9 +54,10 @@ struct isotp_msg_id tx_addr = { .use_fixed_addr = 1, // enable SAE J1939 compatible addressing }; -struct isotp_recv_ctx recv_ctx; +static struct isotp_recv_ctx recv_ctx; +static struct isotp_send_ctx send_ctx; -void send_complette_cb(int error_nr, void *arg) +void send_complete_cb(int error_nr, void *arg) { ARG_UNUSED(arg); LOG_DBG("TX complete callback, err: %d", error_nr); @@ -62,57 +65,62 @@ void send_complette_cb(int error_nr, void *arg) void can_rx_thread() { - int ret, rem_len; - unsigned int received_len; + int ret, rem_len, resp_len; + unsigned int req_len; struct net_buf *buf; - static uint8_t rx_buffer[100]; + static uint8_t rx_buffer[600]; // large enough to receive a 512k flash page for DFU static uint8_t tx_buffer[500]; - // CAN node ID retrieved from EEPROM --> reset necessary after change via ThingSet serial - rx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_TARGET_SET(can_node_addr); - tx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_SOURCE_SET(can_node_addr); - while (1) { + /* re-assign address in every loop as it may have been changed via ThingSet */ + rx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | + TS_CAN_TARGET_SET(can_node_addr); + tx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | + TS_CAN_SOURCE_SET(can_node_addr); + ret = isotp_bind(&recv_ctx, can_dev, &rx_addr, &tx_addr, &fc_opts, K_FOREVER); if (ret != ISOTP_N_OK) { LOG_DBG("Failed to bind to rx ID %d [%d]", rx_addr.ext_id, ret); return; } - received_len = 0; + req_len = 0; do { rem_len = isotp_recv_net(&recv_ctx, &buf, K_FOREVER); if (rem_len < 0) { LOG_DBG("Receiving error [%d]", rem_len); break; } - if (received_len + buf->len <= sizeof(rx_buffer)) { - memcpy(&rx_buffer[received_len], buf->data, buf->len); - received_len += buf->len; - } - else { - LOG_DBG("RX buffer too small"); - break; + if (req_len + buf->len <= sizeof(rx_buffer)) { + memcpy(&rx_buffer[req_len], buf->data, buf->len); } + req_len += buf->len; net_buf_unref(buf); } while (rem_len); // we need to unbind the receive ctx so that control frames are received in the send ctx isotp_unbind(&recv_ctx); - if (received_len > 0) { - LOG_DBG("Got %d bytes via ISO-TP. Processing ThingSet message.", received_len); - LOG_DBG("RX buf: %x %x %x %x", rx_buffer[0], rx_buffer[1], rx_buffer[2], rx_buffer[3]); - int resp_len = ts.process(rx_buffer, received_len, tx_buffer, sizeof(tx_buffer)); + if (req_len > sizeof(rx_buffer)) { + LOG_DBG("RX buffer too small"); + tx_buffer[0] = TS_STATUS_REQUEST_TOO_LARGE; + resp_len = 1; + } + else if (req_len > 0 && rem_len == 0) { + LOG_INF("Got %d bytes via ISO-TP. Processing ThingSet message.", req_len); + resp_len = ts.process(rx_buffer, req_len, tx_buffer, sizeof(tx_buffer)); LOG_DBG("TX buf: %x %x %x %x", tx_buffer[0], tx_buffer[1], tx_buffer[2], tx_buffer[3]); + } + else { + tx_buffer[0] = TS_STATUS_INTERNAL_SERVER_ERR; + resp_len = 1; + } - if (resp_len > 0) { - static struct isotp_send_ctx send_ctx; - int ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len, - &recv_ctx.tx_addr, &recv_ctx.rx_addr, send_complette_cb, NULL); - if (ret != ISOTP_N_OK) { - LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret); - } + if (resp_len > 0) { + ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len, + &recv_ctx.tx_addr, &recv_ctx.rx_addr, send_complete_cb, NULL); + if (ret != ISOTP_N_OK) { + LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret); } } } @@ -130,7 +138,7 @@ void can_pub_isr(uint32_t err_flags, void *arg) void can_pub_thread() { - int wdt_channel = task_wdt_add(1100, task_wdt_callback, (void *)k_current_get()); + int wdt_channel = task_wdt_add(2000, task_wdt_callback, (void *)k_current_get()); unsigned int can_id; uint8_t can_data[8];