diff --git a/include/zephyr/canbus/isotp.h b/include/zephyr/canbus/isotp.h index aed5306b16a..d162b8dc531 100644 --- a/include/zephyr/canbus/isotp.h +++ b/include/zephyr/canbus/isotp.h @@ -234,7 +234,7 @@ struct isotp_recv_ctx; * When calling this routine, a filter is applied in the CAN device, and the * context is initialized. The context must be valid until calling unbind. * - * @param ctx Context to store the internal states. + * @param rctx Context to store the internal states. * @param can_dev The CAN device to be used for sending and receiving. * @param rx_addr Identifier for incoming data. * @param tx_addr Identifier for FC frames. @@ -244,7 +244,7 @@ struct isotp_recv_ctx; * @retval ISOTP_N_OK on success * @retval ISOTP_NO_FREE_FILTER if CAN device has no filters left. */ -int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev, +int isotp_bind(struct isotp_recv_ctx *rctx, const struct device *can_dev, const struct isotp_msg_id *rx_addr, const struct isotp_msg_id *tx_addr, const struct isotp_fc_opts *opts, @@ -258,9 +258,9 @@ int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev, * buffers are freed. * The context can be discarded safely after calling this function. * - * @param ctx Context that should be unbound. + * @param rctx Context that should be unbound. */ -void isotp_unbind(struct isotp_recv_ctx *ctx); +void isotp_unbind(struct isotp_recv_ctx *rctx); /** * @brief Read out received data from fifo. @@ -270,7 +270,7 @@ void isotp_unbind(struct isotp_recv_ctx *ctx); * If an error occurs, the function returns a negative number and leaves the * data buffer unchanged. * - * @param ctx Context that is already bound. + * @param rctx Context that is already bound. * @param data Pointer to a buffer where the data is copied to. * @param len Size of the buffer. * @param timeout Timeout for incoming data. @@ -279,8 +279,7 @@ void isotp_unbind(struct isotp_recv_ctx *ctx); * @retval ISOTP_RECV_TIMEOUT when "timeout" timed out * @retval ISOTP_N_* on error */ -int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, - k_timeout_t timeout); +int isotp_recv(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len, k_timeout_t timeout); /** * @brief Get the net buffer on data reception @@ -292,7 +291,7 @@ int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, * The net-buffers are referenced and must be freed with net_buf_unref after the * data is processed. * - * @param ctx Context that is already bound. + * @param rctx Context that is already bound. * @param buffer Pointer where the net_buf pointer is written to. * @param timeout Timeout for incoming data. * @@ -300,8 +299,7 @@ int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, * @retval ISOTP_RECV_TIMEOUT when "timeout" timed out * @retval ISOTP_N_* on error */ -int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer, - k_timeout_t timeout); +int isotp_recv_net(struct isotp_recv_ctx *rctx, struct net_buf **buffer, k_timeout_t timeout); /** * @brief Send data @@ -312,7 +310,7 @@ int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer, * If a complete_cb is given, this function is non-blocking, and the callback * is called on completion with the return value as a parameter. * - * @param ctx Context to store the internal states. + * @param sctx Context to store the internal states. * @param can_dev The CAN device to be used for sending and receiving. * @param data Data to be sent. * @param len Length of the data to be sent. @@ -324,7 +322,7 @@ int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer, * @retval ISOTP_N_OK on success * @retval ISOTP_N_* on error */ -int isotp_send(struct isotp_send_ctx *ctx, const struct device *can_dev, +int isotp_send(struct isotp_send_ctx *sctx, const struct device *can_dev, const uint8_t *data, size_t len, const struct isotp_msg_id *tx_addr, const struct isotp_msg_id *rx_addr, diff --git a/subsys/canbus/isotp/isotp.c b/subsys/canbus/isotp/isotp.c index 0ef6fce1e8c..710487950c6 100644 --- a/subsys/canbus/isotp/isotp.c +++ b/subsys/canbus/isotp/isotp.c @@ -39,7 +39,7 @@ NET_BUF_POOL_VAR_DEFINE(isotp_tx_pool, CONFIG_ISOTP_TX_BUF_COUNT, CONFIG_ISOTP_BUF_TX_DATA_POOL_SIZE, 0, NULL); #endif -static void receive_state_machine(struct isotp_recv_ctx *ctx); +static void receive_state_machine(struct isotp_recv_ctx *rctx); static inline void prepare_frame(struct can_frame *frame, struct isotp_msg_id *addr) { @@ -64,46 +64,46 @@ static inline void prepare_filter(struct can_filter *filter, struct isotp_msg_id */ static void receive_pool_free(struct net_buf *buf) { - struct isotp_recv_ctx *ctx; - sys_snode_t *ctx_node; + struct isotp_recv_ctx *rctx; + sys_snode_t *rctx_node; net_buf_destroy(buf); - SYS_SLIST_FOR_EACH_NODE(&global_ctx.alloc_list, ctx_node) { - ctx = CONTAINER_OF(ctx_node, struct isotp_recv_ctx, alloc_node); - k_work_submit(&ctx->work); + SYS_SLIST_FOR_EACH_NODE(&global_ctx.alloc_list, rctx_node) { + rctx = CONTAINER_OF(rctx_node, struct isotp_recv_ctx, alloc_node); + k_work_submit(&rctx->work); } } static void receive_ff_sf_pool_free(struct net_buf *buf) { - struct isotp_recv_ctx *ctx; - sys_snode_t *ctx_node; + struct isotp_recv_ctx *rctx; + sys_snode_t *rctx_node; net_buf_destroy(buf); - SYS_SLIST_FOR_EACH_NODE(&global_ctx.ff_sf_alloc_list, ctx_node) { - ctx = CONTAINER_OF(ctx_node, struct isotp_recv_ctx, alloc_node); - k_work_submit(&ctx->work); + SYS_SLIST_FOR_EACH_NODE(&global_ctx.ff_sf_alloc_list, rctx_node) { + rctx = CONTAINER_OF(rctx_node, struct isotp_recv_ctx, alloc_node); + k_work_submit(&rctx->work); } } -static inline void receive_report_error(struct isotp_recv_ctx *ctx, int err) +static inline void receive_report_error(struct isotp_recv_ctx *rctx, int err) { - ctx->state = ISOTP_RX_STATE_ERR; - ctx->error_nr = err; + rctx->state = ISOTP_RX_STATE_ERR; + rctx->error_nr = err; } static void receive_can_tx(const struct device *dev, int error, void *arg) { - struct isotp_recv_ctx *ctx = (struct isotp_recv_ctx *)arg; + struct isotp_recv_ctx *rctx = (struct isotp_recv_ctx *)arg; ARG_UNUSED(dev); if (error != 0) { LOG_ERR("Error sending FC frame (%d)", error); - receive_report_error(ctx, ISOTP_N_ERROR); - k_work_submit(&ctx->work); + receive_report_error(rctx, ISOTP_N_ERROR); + k_work_submit(&rctx->work); } } @@ -134,7 +134,7 @@ static inline uint32_t receive_get_sf_length(struct net_buf *buf, bool fdf) return len; } -static void receive_send_fc(struct isotp_recv_ctx *ctx, uint8_t fs) +static void receive_send_fc(struct isotp_recv_ctx *rctx, uint8_t fs) { struct can_frame frame; uint8_t *data = frame.data; @@ -143,15 +143,15 @@ static void receive_send_fc(struct isotp_recv_ctx *ctx, uint8_t fs) __ASSERT_NO_MSG(!(fs & ISOTP_PCI_TYPE_MASK)); - prepare_frame(&frame, &ctx->tx_addr); + prepare_frame(&frame, &rctx->tx_addr); - if ((ctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - *data++ = ctx->tx_addr.ext_addr; + if ((rctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + *data++ = rctx->tx_addr.ext_addr; } *data++ = ISOTP_PCI_TYPE_FC | fs; - *data++ = ctx->opts.bs; - *data++ = ctx->opts.stmin; + *data++ = rctx->opts.bs; + *data++ = rctx->opts.stmin; payload_len = data - frame.data; #ifdef CONFIG_ISOTP_ENABLE_TX_PADDING @@ -163,11 +163,11 @@ static void receive_send_fc(struct isotp_recv_ctx *ctx, uint8_t fs) frame.dlc = can_bytes_to_dlc(payload_len); #endif - ret = can_send(ctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), receive_can_tx, ctx); + ret = can_send(rctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), receive_can_tx, rctx); if (ret) { LOG_ERR("Can't send FC, (%d)", ret); - receive_report_error(ctx, ISOTP_N_TIMEOUT_A); - receive_state_machine(ctx); + receive_report_error(rctx, ISOTP_N_TIMEOUT_A); + receive_state_machine(rctx); } } @@ -206,172 +206,163 @@ static inline struct net_buf *receive_alloc_buffer_chain(uint32_t len) static void receive_timeout_handler(struct k_timer *timer) { - struct isotp_recv_ctx *ctx = CONTAINER_OF(timer, struct isotp_recv_ctx, timer); + struct isotp_recv_ctx *rctx = CONTAINER_OF(timer, struct isotp_recv_ctx, timer); - switch (ctx->state) { + switch (rctx->state) { case ISOTP_RX_STATE_WAIT_CF: LOG_ERR("Timeout while waiting for CF"); - receive_report_error(ctx, ISOTP_N_TIMEOUT_CR); + receive_report_error(rctx, ISOTP_N_TIMEOUT_CR); break; case ISOTP_RX_STATE_TRY_ALLOC: - ctx->state = ISOTP_RX_STATE_SEND_WAIT; + rctx->state = ISOTP_RX_STATE_SEND_WAIT; break; } - k_work_submit(&ctx->work); + k_work_submit(&rctx->work); } -static int receive_alloc_buffer(struct isotp_recv_ctx *ctx) +static int receive_alloc_buffer(struct isotp_recv_ctx *rctx) { struct net_buf *buf = NULL; - if (ctx->opts.bs == 0) { + if (rctx->opts.bs == 0) { /* Alloc all buffers because we can't wait during reception */ - buf = receive_alloc_buffer_chain(ctx->length); + buf = receive_alloc_buffer_chain(rctx->length); } else { /* Alloc the minimum of the remaining length and bytes of one block */ - uint32_t len = MIN(ctx->length, ctx->opts.bs * (ctx->rx_addr.dl - 1)); + uint32_t len = MIN(rctx->length, rctx->opts.bs * (rctx->rx_addr.dl - 1)); buf = receive_alloc_buffer_chain(len); } if (!buf) { - k_timer_start(&ctx->timer, K_MSEC(ISOTP_ALLOC_TIMEOUT_MS), K_NO_WAIT); + k_timer_start(&rctx->timer, K_MSEC(ISOTP_ALLOC_TIMEOUT_MS), K_NO_WAIT); - if (ctx->wft == ISOTP_WFT_FIRST) { + if (rctx->wft == ISOTP_WFT_FIRST) { LOG_DBG("Allocation failed. Append to alloc list"); - ctx->wft = 0; - sys_slist_append(&global_ctx.alloc_list, - &ctx->alloc_node); + rctx->wft = 0; + sys_slist_append(&global_ctx.alloc_list, &rctx->alloc_node); } else { LOG_DBG("Allocation failed. Send WAIT frame"); - ctx->state = ISOTP_RX_STATE_SEND_WAIT; - receive_state_machine(ctx); + rctx->state = ISOTP_RX_STATE_SEND_WAIT; + receive_state_machine(rctx); } return -1; } - if (ctx->state == ISOTP_RX_STATE_TRY_ALLOC) { - k_timer_stop(&ctx->timer); - ctx->wft = ISOTP_WFT_FIRST; - sys_slist_find_and_remove(&global_ctx.alloc_list, - &ctx->alloc_node); + if (rctx->state == ISOTP_RX_STATE_TRY_ALLOC) { + k_timer_stop(&rctx->timer); + rctx->wft = ISOTP_WFT_FIRST; + sys_slist_find_and_remove(&global_ctx.alloc_list, &rctx->alloc_node); } - if (ctx->opts.bs != 0) { - ctx->buf = buf; + if (rctx->opts.bs != 0) { + rctx->buf = buf; } else { - net_buf_frag_insert(ctx->buf, buf); + net_buf_frag_insert(rctx->buf, buf); } - ctx->act_frag = buf; + rctx->act_frag = buf; return 0; } -static void receive_state_machine(struct isotp_recv_ctx *ctx) +static void receive_state_machine(struct isotp_recv_ctx *rctx) { int ret; uint32_t *ud_rem_len; - switch (ctx->state) { + switch (rctx->state) { case ISOTP_RX_STATE_PROCESS_SF: - ctx->length = receive_get_sf_length(ctx->buf, - (ctx->rx_addr.flags & ISOTP_MSG_FDF) != 0); - ud_rem_len = net_buf_user_data(ctx->buf); + rctx->length = receive_get_sf_length(rctx->buf, + (rctx->rx_addr.flags & ISOTP_MSG_FDF) != 0); + ud_rem_len = net_buf_user_data(rctx->buf); *ud_rem_len = 0; - LOG_DBG("SM process SF of length %d", ctx->length); - net_buf_put(&ctx->fifo, ctx->buf); - ctx->state = ISOTP_RX_STATE_RECYCLE; - receive_state_machine(ctx); + LOG_DBG("SM process SF of length %d", rctx->length); + net_buf_put(&rctx->fifo, rctx->buf); + rctx->state = ISOTP_RX_STATE_RECYCLE; + receive_state_machine(rctx); break; case ISOTP_RX_STATE_PROCESS_FF: - ctx->length = receive_get_ff_length(ctx->buf); - LOG_DBG("SM process FF. Length: %d", ctx->length); - ctx->length -= ctx->buf->len; - if (ctx->opts.bs == 0 && - ctx->length > CONFIG_ISOTP_RX_BUF_COUNT * - CONFIG_ISOTP_RX_BUF_SIZE) { - LOG_ERR("Pkt length is %d but buffer has only %d bytes", - ctx->length, - CONFIG_ISOTP_RX_BUF_COUNT * - CONFIG_ISOTP_RX_BUF_SIZE); - receive_report_error(ctx, ISOTP_N_BUFFER_OVERFLW); - receive_state_machine(ctx); + rctx->length = receive_get_ff_length(rctx->buf); + LOG_DBG("SM process FF. Length: %d", rctx->length); + rctx->length -= rctx->buf->len; + if (rctx->opts.bs == 0 && + rctx->length > CONFIG_ISOTP_RX_BUF_COUNT * CONFIG_ISOTP_RX_BUF_SIZE) { + LOG_ERR("Pkt length is %d but buffer has only %d bytes", rctx->length, + CONFIG_ISOTP_RX_BUF_COUNT * CONFIG_ISOTP_RX_BUF_SIZE); + receive_report_error(rctx, ISOTP_N_BUFFER_OVERFLW); + receive_state_machine(rctx); break; } - if (ctx->opts.bs) { - ctx->bs = ctx->opts.bs; - ud_rem_len = net_buf_user_data(ctx->buf); - *ud_rem_len = ctx->length; - net_buf_put(&ctx->fifo, ctx->buf); + if (rctx->opts.bs) { + rctx->bs = rctx->opts.bs; + ud_rem_len = net_buf_user_data(rctx->buf); + *ud_rem_len = rctx->length; + net_buf_put(&rctx->fifo, rctx->buf); } - ctx->wft = ISOTP_WFT_FIRST; - ctx->state = ISOTP_RX_STATE_TRY_ALLOC; + rctx->wft = ISOTP_WFT_FIRST; + rctx->state = ISOTP_RX_STATE_TRY_ALLOC; __fallthrough; case ISOTP_RX_STATE_TRY_ALLOC: LOG_DBG("SM try to allocate"); - k_timer_stop(&ctx->timer); - ret = receive_alloc_buffer(ctx); + k_timer_stop(&rctx->timer); + ret = receive_alloc_buffer(rctx); if (ret) { LOG_DBG("SM allocation failed. Wait for free buffer"); break; } - ctx->state = ISOTP_RX_STATE_SEND_FC; + rctx->state = ISOTP_RX_STATE_SEND_FC; __fallthrough; case ISOTP_RX_STATE_SEND_FC: LOG_DBG("SM send CTS FC frame"); - receive_send_fc(ctx, ISOTP_PCI_FS_CTS); - k_timer_start(&ctx->timer, K_MSEC(ISOTP_CR_TIMEOUT_MS), K_NO_WAIT); - ctx->state = ISOTP_RX_STATE_WAIT_CF; + receive_send_fc(rctx, ISOTP_PCI_FS_CTS); + k_timer_start(&rctx->timer, K_MSEC(ISOTP_CR_TIMEOUT_MS), K_NO_WAIT); + rctx->state = ISOTP_RX_STATE_WAIT_CF; break; case ISOTP_RX_STATE_SEND_WAIT: - if (++ctx->wft < CONFIG_ISOTP_WFTMAX) { - LOG_DBG("Send wait frame number %d", ctx->wft); - receive_send_fc(ctx, ISOTP_PCI_FS_WAIT); - k_timer_start(&ctx->timer, K_MSEC(ISOTP_ALLOC_TIMEOUT_MS), K_NO_WAIT); - ctx->state = ISOTP_RX_STATE_TRY_ALLOC; + if (++rctx->wft < CONFIG_ISOTP_WFTMAX) { + LOG_DBG("Send wait frame number %d", rctx->wft); + receive_send_fc(rctx, ISOTP_PCI_FS_WAIT); + k_timer_start(&rctx->timer, K_MSEC(ISOTP_ALLOC_TIMEOUT_MS), K_NO_WAIT); + rctx->state = ISOTP_RX_STATE_TRY_ALLOC; break; } - sys_slist_find_and_remove(&global_ctx.alloc_list, - &ctx->alloc_node); - LOG_ERR("Sent %d wait frames. Giving up to alloc now", - ctx->wft); - receive_report_error(ctx, ISOTP_N_BUFFER_OVERFLW); + sys_slist_find_and_remove(&global_ctx.alloc_list, &rctx->alloc_node); + LOG_ERR("Sent %d wait frames. Giving up to alloc now", rctx->wft); + receive_report_error(rctx, ISOTP_N_BUFFER_OVERFLW); __fallthrough; case ISOTP_RX_STATE_ERR: - LOG_DBG("SM ERR state. err nr: %d", ctx->error_nr); - k_timer_stop(&ctx->timer); + LOG_DBG("SM ERR state. err nr: %d", rctx->error_nr); + k_timer_stop(&rctx->timer); - if (ctx->error_nr == ISOTP_N_BUFFER_OVERFLW) { - receive_send_fc(ctx, ISOTP_PCI_FS_OVFLW); + if (rctx->error_nr == ISOTP_N_BUFFER_OVERFLW) { + receive_send_fc(rctx, ISOTP_PCI_FS_OVFLW); } - k_fifo_cancel_wait(&ctx->fifo); - net_buf_unref(ctx->buf); - ctx->buf = NULL; - ctx->state = ISOTP_RX_STATE_RECYCLE; + k_fifo_cancel_wait(&rctx->fifo); + net_buf_unref(rctx->buf); + rctx->buf = NULL; + rctx->state = ISOTP_RX_STATE_RECYCLE; __fallthrough; case ISOTP_RX_STATE_RECYCLE: LOG_DBG("SM recycle context for next message"); - ctx->buf = net_buf_alloc_fixed(&isotp_rx_sf_ff_pool, K_NO_WAIT); - if (!ctx->buf) { + rctx->buf = net_buf_alloc_fixed(&isotp_rx_sf_ff_pool, K_NO_WAIT); + if (!rctx->buf) { LOG_DBG("No free context. Append to waiters list"); - sys_slist_append(&global_ctx.ff_sf_alloc_list, - &ctx->alloc_node); + sys_slist_append(&global_ctx.ff_sf_alloc_list, &rctx->alloc_node); break; } - sys_slist_find_and_remove(&global_ctx.ff_sf_alloc_list, - &ctx->alloc_node); - ctx->state = ISOTP_RX_STATE_WAIT_FF_SF; + sys_slist_find_and_remove(&global_ctx.ff_sf_alloc_list, &rctx->alloc_node); + rctx->state = ISOTP_RX_STATE_WAIT_FF_SF; __fallthrough; case ISOTP_RX_STATE_UNBOUND: break; @@ -383,13 +374,12 @@ static void receive_state_machine(struct isotp_recv_ctx *ctx) static void receive_work_handler(struct k_work *item) { - struct isotp_recv_ctx *ctx = CONTAINER_OF(item, struct isotp_recv_ctx, - work); + struct isotp_recv_ctx *rctx = CONTAINER_OF(item, struct isotp_recv_ctx, work); - receive_state_machine(ctx); + receive_state_machine(rctx); } -static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) +static void process_ff_sf(struct isotp_recv_ctx *rctx, struct can_frame *frame) { int index = 0; uint8_t sf_len; @@ -397,24 +387,24 @@ static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) uint32_t rx_sa; /* ISO-TP fixed source address (if used) */ uint8_t can_dl = can_dlc_to_bytes(frame->dlc); - if ((ctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - if (frame->data[index++] != ctx->rx_addr.ext_addr) { + if ((rctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + if (frame->data[index++] != rctx->rx_addr.ext_addr) { return; } } - if ((ctx->rx_addr.flags & ISOTP_MSG_FIXED_ADDR) != 0) { + if ((rctx->rx_addr.flags & ISOTP_MSG_FIXED_ADDR) != 0) { /* store actual CAN ID used by the sender */ - ctx->rx_addr.ext_id = frame->id; + rctx->rx_addr.ext_id = frame->id; /* replace TX target address with RX source address */ rx_sa = (frame->id & ISOTP_FIXED_ADDR_SA_MASK) >> ISOTP_FIXED_ADDR_SA_POS; - ctx->tx_addr.ext_id &= ~(ISOTP_FIXED_ADDR_TA_MASK); - ctx->tx_addr.ext_id |= rx_sa << ISOTP_FIXED_ADDR_TA_POS; + rctx->tx_addr.ext_id &= ~(ISOTP_FIXED_ADDR_TA_MASK); + rctx->tx_addr.ext_id |= rx_sa << ISOTP_FIXED_ADDR_TA_POS; /* use same priority for TX as in received message */ if (ISOTP_FIXED_ADDR_PRIO_MASK) { - ctx->tx_addr.ext_id &= ~(ISOTP_FIXED_ADDR_PRIO_MASK); - ctx->tx_addr.ext_id |= frame->id & ISOTP_FIXED_ADDR_PRIO_MASK; + rctx->tx_addr.ext_id &= ~(ISOTP_FIXED_ADDR_PRIO_MASK); + rctx->tx_addr.ext_id |= frame->id & ISOTP_FIXED_ADDR_PRIO_MASK; } } @@ -427,9 +417,9 @@ static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) } payload_len = can_dl; - ctx->state = ISOTP_RX_STATE_PROCESS_FF; - ctx->rx_addr.dl = can_dl; - ctx->sn_expected = 1; + rctx->state = ISOTP_RX_STATE_PROCESS_FF; + rctx->rx_addr.dl = can_dl; + rctx->sn_expected = 1; break; case ISOTP_PCI_TYPE_SF: @@ -444,7 +434,7 @@ static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) sf_len = frame->data[index] & ISOTP_PCI_SF_DL_MASK; /* Single frames > 8 bytes (CAN-FD only) */ - if (IS_ENABLED(CONFIG_CAN_FD_MODE) && (ctx->rx_addr.flags & ISOTP_MSG_FDF) != 0 && + if (IS_ENABLED(CONFIG_CAN_FD_MODE) && (rctx->rx_addr.flags & ISOTP_MSG_FDF) != 0 && can_dl > ISOTP_4BIT_SF_MAX_CAN_DL) { if (sf_len != 0) { LOG_INF("SF DL invalid. Ignore"); @@ -461,7 +451,7 @@ static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) return; } - ctx->state = ISOTP_RX_STATE_PROCESS_SF; + rctx->state = ISOTP_RX_STATE_PROCESS_SF; break; default: @@ -469,40 +459,39 @@ static void process_ff_sf(struct isotp_recv_ctx *ctx, struct can_frame *frame) return; } - net_buf_add_mem(ctx->buf, &frame->data[index], payload_len - index); + net_buf_add_mem(rctx->buf, &frame->data[index], payload_len - index); } -static inline void receive_add_mem(struct isotp_recv_ctx *ctx, uint8_t *data, - size_t len) +static inline void receive_add_mem(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len) { - size_t tailroom = net_buf_tailroom(ctx->act_frag); + size_t tailroom = net_buf_tailroom(rctx->act_frag); if (tailroom >= len) { - net_buf_add_mem(ctx->act_frag, data, len); + net_buf_add_mem(rctx->act_frag, data, len); return; } /* Use next fragment that is already allocated*/ - net_buf_add_mem(ctx->act_frag, data, tailroom); - ctx->act_frag = ctx->act_frag->frags; - if (!ctx->act_frag) { + net_buf_add_mem(rctx->act_frag, data, tailroom); + rctx->act_frag = rctx->act_frag->frags; + if (!rctx->act_frag) { LOG_ERR("No fragment left to append data"); - receive_report_error(ctx, ISOTP_N_BUFFER_OVERFLW); + receive_report_error(rctx, ISOTP_N_BUFFER_OVERFLW); return; } - net_buf_add_mem(ctx->act_frag, data + tailroom, len - tailroom); + net_buf_add_mem(rctx->act_frag, data + tailroom, len - tailroom); } -static void process_cf(struct isotp_recv_ctx *ctx, struct can_frame *frame) +static void process_cf(struct isotp_recv_ctx *rctx, struct can_frame *frame) { - uint32_t *ud_rem_len = (uint32_t *)net_buf_user_data(ctx->buf); + uint32_t *ud_rem_len = (uint32_t *)net_buf_user_data(rctx->buf); int index = 0; uint32_t data_len; uint8_t can_dl = can_dlc_to_bytes(frame->dlc); - if ((ctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - if (frame->data[index++] != ctx->rx_addr.ext_addr) { + if ((rctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + if (frame->data[index++] != rctx->rx_addr.ext_addr) { return; } } @@ -510,17 +499,17 @@ static void process_cf(struct isotp_recv_ctx *ctx, struct can_frame *frame) if ((frame->data[index] & ISOTP_PCI_TYPE_MASK) != ISOTP_PCI_TYPE_CF) { LOG_DBG("Waiting for CF but got something else (%d)", frame->data[index] >> ISOTP_PCI_TYPE_POS); - receive_report_error(ctx, ISOTP_N_UNEXP_PDU); - k_work_submit(&ctx->work); + receive_report_error(rctx, ISOTP_N_UNEXP_PDU); + k_work_submit(&rctx->work); return; } - k_timer_start(&ctx->timer, K_MSEC(ISOTP_CR_TIMEOUT_MS), K_NO_WAIT); + k_timer_start(&rctx->timer, K_MSEC(ISOTP_CR_TIMEOUT_MS), K_NO_WAIT); - if ((frame->data[index++] & ISOTP_PCI_SN_MASK) != ctx->sn_expected++) { + if ((frame->data[index++] & ISOTP_PCI_SN_MASK) != rctx->sn_expected++) { LOG_ERR("Sequence number mismatch"); - receive_report_error(ctx, ISOTP_N_WRONG_SN); - k_work_submit(&ctx->work); + receive_report_error(rctx, ISOTP_N_WRONG_SN); + k_work_submit(&rctx->work); return; } @@ -528,7 +517,7 @@ static void process_cf(struct isotp_recv_ctx *ctx, struct can_frame *frame) /* AUTOSAR requirement SWS_CanTp_00346 */ if (can_dl < ISOTP_PADDED_FRAME_DL_MIN) { LOG_ERR("CF DL invalid"); - receive_report_error(ctx, ISOTP_N_ERROR); + receive_report_error(rctx, ISOTP_N_ERROR); return; } #endif @@ -536,50 +525,50 @@ static void process_cf(struct isotp_recv_ctx *ctx, struct can_frame *frame) /* First frame defines the RX data length, consecutive frames * must have the same length (except the last frame) */ - if (can_dl != ctx->rx_addr.dl && ctx->length > can_dl - index) { + if (can_dl != rctx->rx_addr.dl && rctx->length > can_dl - index) { LOG_ERR("CF DL invalid"); - receive_report_error(ctx, ISOTP_N_ERROR); + receive_report_error(rctx, ISOTP_N_ERROR); return; } LOG_DBG("Got CF irq. Appending data"); - data_len = MIN(ctx->length, can_dl - index); - receive_add_mem(ctx, &frame->data[index], data_len); - ctx->length -= data_len; - LOG_DBG("%d bytes remaining", ctx->length); + data_len = MIN(rctx->length, can_dl - index); + receive_add_mem(rctx, &frame->data[index], data_len); + rctx->length -= data_len; + LOG_DBG("%d bytes remaining", rctx->length); - if (ctx->length == 0) { - ctx->state = ISOTP_RX_STATE_RECYCLE; + if (rctx->length == 0) { + rctx->state = ISOTP_RX_STATE_RECYCLE; *ud_rem_len = 0; - net_buf_put(&ctx->fifo, ctx->buf); + net_buf_put(&rctx->fifo, rctx->buf); return; } - if (ctx->opts.bs && !--ctx->bs) { + if (rctx->opts.bs && !--rctx->bs) { LOG_DBG("Block is complete. Allocate new buffer"); - ctx->bs = ctx->opts.bs; - *ud_rem_len = ctx->length; - net_buf_put(&ctx->fifo, ctx->buf); - ctx->state = ISOTP_RX_STATE_TRY_ALLOC; + rctx->bs = rctx->opts.bs; + *ud_rem_len = rctx->length; + net_buf_put(&rctx->fifo, rctx->buf); + rctx->state = ISOTP_RX_STATE_TRY_ALLOC; } } static void receive_can_rx(const struct device *dev, struct can_frame *frame, void *arg) { - struct isotp_recv_ctx *ctx = (struct isotp_recv_ctx *)arg; + struct isotp_recv_ctx *rctx = (struct isotp_recv_ctx *)arg; ARG_UNUSED(dev); - switch (ctx->state) { + switch (rctx->state) { case ISOTP_RX_STATE_WAIT_FF_SF: - __ASSERT_NO_MSG(ctx->buf); - process_ff_sf(ctx, frame); + __ASSERT_NO_MSG(rctx->buf); + process_ff_sf(rctx, frame); break; case ISOTP_RX_STATE_WAIT_CF: - process_cf(ctx, frame); + process_cf(rctx, frame); /* still waiting for more CF */ - if (ctx->state == ISOTP_RX_STATE_WAIT_CF) { + if (rctx->state == ISOTP_RX_STATE_WAIT_CF) { return; } @@ -587,40 +576,39 @@ static void receive_can_rx(const struct device *dev, struct can_frame *frame, vo case ISOTP_RX_STATE_RECYCLE: LOG_ERR("Got a frame but was not yet ready for a new one"); - receive_report_error(ctx, ISOTP_N_BUFFER_OVERFLW); + receive_report_error(rctx, ISOTP_N_BUFFER_OVERFLW); break; default: LOG_INF("Got a frame in a state where it is unexpected."); } - k_work_submit(&ctx->work); + k_work_submit(&rctx->work); } -static inline int add_ff_sf_filter(struct isotp_recv_ctx *ctx) +static inline int add_ff_sf_filter(struct isotp_recv_ctx *rctx) { struct can_filter filter; uint32_t mask; - if ((ctx->rx_addr.flags & ISOTP_MSG_FIXED_ADDR) != 0) { + if ((rctx->rx_addr.flags & ISOTP_MSG_FIXED_ADDR) != 0) { mask = ISOTP_FIXED_ADDR_RX_MASK; } else { mask = CAN_EXT_ID_MASK; } - prepare_filter(&filter, &ctx->rx_addr, mask); + prepare_filter(&filter, &rctx->rx_addr, mask); - ctx->filter_id = can_add_rx_filter(ctx->can_dev, receive_can_rx, ctx, - &filter); - if (ctx->filter_id < 0) { - LOG_ERR("Error adding FF filter [%d]", ctx->filter_id); + rctx->filter_id = can_add_rx_filter(rctx->can_dev, receive_can_rx, rctx, &filter); + if (rctx->filter_id < 0) { + LOG_ERR("Error adding FF filter [%d]", rctx->filter_id); return ISOTP_NO_FREE_FILTER; } return 0; } -int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev, +int isotp_bind(struct isotp_recv_ctx *rctx, const struct device *can_dev, const struct isotp_msg_id *rx_addr, const struct isotp_msg_id *tx_addr, const struct isotp_fc_opts *opts, @@ -629,22 +617,22 @@ int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev, can_mode_t cap; int ret; - __ASSERT(ctx, "ctx is NULL"); + __ASSERT(rctx, "rctx is NULL"); __ASSERT(can_dev, "CAN device is NULL"); __ASSERT(rx_addr && tx_addr, "RX or TX addr is NULL"); __ASSERT(opts, "OPTS is NULL"); - ctx->can_dev = can_dev; - ctx->rx_addr = *rx_addr; - ctx->tx_addr = *tx_addr; - k_fifo_init(&ctx->fifo); + rctx->can_dev = can_dev; + rctx->rx_addr = *rx_addr; + rctx->tx_addr = *tx_addr; + k_fifo_init(&rctx->fifo); __ASSERT(opts->stmin < ISOTP_STMIN_MAX, "STmin limit"); __ASSERT(opts->stmin <= ISOTP_STMIN_MS_MAX || opts->stmin >= ISOTP_STMIN_US_BEGIN, "STmin reserved"); - ctx->opts = *opts; - ctx->state = ISOTP_RX_STATE_WAIT_FF_SF; + rctx->opts = *opts; + rctx->state = ISOTP_RX_STATE_WAIT_FF_SF; if ((rx_addr->flags & ISOTP_MSG_FDF) != 0 || (tx_addr->flags & ISOTP_MSG_FDF) != 0) { ret = can_get_capabilities(can_dev, &cap); @@ -655,68 +643,65 @@ int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev, } LOG_DBG("Binding to addr: 0x%x. Responding on 0x%x", - ctx->rx_addr.ext_id, ctx->tx_addr.ext_id); + rctx->rx_addr.ext_id, rctx->tx_addr.ext_id); - ctx->buf = net_buf_alloc_fixed(&isotp_rx_sf_ff_pool, timeout); - if (!ctx->buf) { + rctx->buf = net_buf_alloc_fixed(&isotp_rx_sf_ff_pool, timeout); + if (!rctx->buf) { LOG_ERR("No buffer for FF left"); return ISOTP_NO_NET_BUF_LEFT; } - ret = add_ff_sf_filter(ctx); + ret = add_ff_sf_filter(rctx); if (ret) { LOG_ERR("Can't add filter for binding"); - net_buf_unref(ctx->buf); - ctx->buf = NULL; + net_buf_unref(rctx->buf); + rctx->buf = NULL; return ret; } - k_work_init(&ctx->work, receive_work_handler); - k_timer_init(&ctx->timer, receive_timeout_handler, NULL); + k_work_init(&rctx->work, receive_work_handler); + k_timer_init(&rctx->timer, receive_timeout_handler, NULL); return ISOTP_N_OK; } -void isotp_unbind(struct isotp_recv_ctx *ctx) +void isotp_unbind(struct isotp_recv_ctx *rctx) { struct net_buf *buf; - if (ctx->filter_id >= 0 && ctx->can_dev) { - can_remove_rx_filter(ctx->can_dev, ctx->filter_id); + if (rctx->filter_id >= 0 && rctx->can_dev) { + can_remove_rx_filter(rctx->can_dev, rctx->filter_id); } - k_timer_stop(&ctx->timer); + k_timer_stop(&rctx->timer); - sys_slist_find_and_remove(&global_ctx.ff_sf_alloc_list, - &ctx->alloc_node); - sys_slist_find_and_remove(&global_ctx.alloc_list, - &ctx->alloc_node); + sys_slist_find_and_remove(&global_ctx.ff_sf_alloc_list, &rctx->alloc_node); + sys_slist_find_and_remove(&global_ctx.alloc_list, &rctx->alloc_node); - ctx->state = ISOTP_RX_STATE_UNBOUND; + rctx->state = ISOTP_RX_STATE_UNBOUND; - while ((buf = net_buf_get(&ctx->fifo, K_NO_WAIT))) { + while ((buf = net_buf_get(&rctx->fifo, K_NO_WAIT))) { net_buf_unref(buf); } - k_fifo_cancel_wait(&ctx->fifo); + k_fifo_cancel_wait(&rctx->fifo); - if (ctx->buf) { - net_buf_unref(ctx->buf); + if (rctx->buf) { + net_buf_unref(rctx->buf); } LOG_DBG("Unbound"); } -int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer, - k_timeout_t timeout) +int isotp_recv_net(struct isotp_recv_ctx *rctx, struct net_buf **buffer, k_timeout_t timeout) { struct net_buf *buf; int ret; - buf = net_buf_get(&ctx->fifo, timeout); + buf = net_buf_get(&rctx->fifo, timeout); if (!buf) { - ret = ctx->error_nr ? ctx->error_nr : ISOTP_RECV_TIMEOUT; - ctx->error_nr = 0; + ret = rctx->error_nr ? rctx->error_nr : ISOTP_RECV_TIMEOUT; + rctx->error_nr = 0; return ret; } @@ -726,17 +711,16 @@ int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer, return *(uint32_t *)net_buf_user_data(buf); } -int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, - k_timeout_t timeout) +int isotp_recv(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len, k_timeout_t timeout) { size_t copied, to_copy; int err; - if (!ctx->recv_buf) { - ctx->recv_buf = net_buf_get(&ctx->fifo, timeout); - if (!ctx->recv_buf) { - err = ctx->error_nr ? ctx->error_nr : ISOTP_RECV_TIMEOUT; - ctx->error_nr = 0; + if (!rctx->recv_buf) { + rctx->recv_buf = net_buf_get(&rctx->fifo, timeout); + if (!rctx->recv_buf) { + err = rctx->error_nr ? rctx->error_nr : ISOTP_RECV_TIMEOUT; + rctx->error_nr = 0; return err; } @@ -744,16 +728,16 @@ int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, /* traverse fragments and delete them after copying the data */ copied = 0; - while (ctx->recv_buf && copied < len) { - to_copy = MIN(len - copied, ctx->recv_buf->len); - memcpy((uint8_t *)data + copied, ctx->recv_buf->data, to_copy); + while (rctx->recv_buf && copied < len) { + to_copy = MIN(len - copied, rctx->recv_buf->len); + memcpy((uint8_t *)data + copied, rctx->recv_buf->data, to_copy); - if (ctx->recv_buf->len == to_copy) { + if (rctx->recv_buf->len == to_copy) { /* point recv_buf to next frag */ - ctx->recv_buf = net_buf_frag_del(NULL, ctx->recv_buf); + rctx->recv_buf = net_buf_frag_del(NULL, rctx->recv_buf); } else { /* pull received data from remaining frag(s) */ - net_buf_pull(ctx->recv_buf, to_copy); + net_buf_pull(rctx->recv_buf, to_copy); } copied += to_copy; @@ -762,58 +746,57 @@ int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, return copied; } -static inline void send_report_error(struct isotp_send_ctx *ctx, uint32_t err) +static inline void send_report_error(struct isotp_send_ctx *sctx, uint32_t err) { - ctx->state = ISOTP_TX_ERR; - ctx->error_nr = err; + sctx->state = ISOTP_TX_ERR; + sctx->error_nr = err; } static void send_can_tx_cb(const struct device *dev, int error, void *arg) { - struct isotp_send_ctx *ctx = (struct isotp_send_ctx *)arg; + struct isotp_send_ctx *sctx = (struct isotp_send_ctx *)arg; ARG_UNUSED(dev); - ctx->tx_backlog--; - k_sem_give(&ctx->tx_sem); + sctx->tx_backlog--; + k_sem_give(&sctx->tx_sem); - if (ctx->state == ISOTP_TX_WAIT_BACKLOG) { - if (ctx->tx_backlog > 0) { + if (sctx->state == ISOTP_TX_WAIT_BACKLOG) { + if (sctx->tx_backlog > 0) { return; } - ctx->state = ISOTP_TX_WAIT_FIN; + sctx->state = ISOTP_TX_WAIT_FIN; } - k_work_submit(&ctx->work); + k_work_submit(&sctx->work); } static void send_timeout_handler(struct k_timer *timer) { - struct isotp_send_ctx *ctx = CONTAINER_OF(timer, struct isotp_send_ctx, timer); + struct isotp_send_ctx *sctx = CONTAINER_OF(timer, struct isotp_send_ctx, timer); - if (ctx->state != ISOTP_TX_SEND_CF) { - send_report_error(ctx, ISOTP_N_TIMEOUT_BS); + if (sctx->state != ISOTP_TX_SEND_CF) { + send_report_error(sctx, ISOTP_N_TIMEOUT_BS); LOG_ERR("Reception of next FC has timed out"); } - k_work_submit(&ctx->work); + k_work_submit(&sctx->work); } -static void send_process_fc(struct isotp_send_ctx *ctx, - struct can_frame *frame) +static void send_process_fc(struct isotp_send_ctx *sctx, struct can_frame *frame) { uint8_t *data = frame->data; - if ((ctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - if (ctx->rx_addr.ext_addr != *data++) { + if ((sctx->rx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + if (sctx->rx_addr.ext_addr != *data++) { return; } } if ((*data & ISOTP_PCI_TYPE_MASK) != ISOTP_PCI_TYPE_FC) { LOG_ERR("Got unexpected PDU expected FC"); - send_report_error(ctx, ISOTP_N_UNEXP_PDU); + send_report_error(sctx, ISOTP_N_UNEXP_PDU); return; } @@ -821,104 +804,104 @@ static void send_process_fc(struct isotp_send_ctx *ctx, /* AUTOSAR requirement SWS_CanTp_00349 */ if (frame->dlc < ISOTP_PADDED_FRAME_DL_MIN) { LOG_ERR("FC DL invalid. Ignore"); - send_report_error(ctx, ISOTP_N_ERROR); + send_report_error(sctx, ISOTP_N_ERROR); return; } #endif switch (*data++ & ISOTP_PCI_FS_MASK) { case ISOTP_PCI_FS_CTS: - ctx->state = ISOTP_TX_SEND_CF; - ctx->wft = 0; - ctx->tx_backlog = 0; - k_sem_reset(&ctx->tx_sem); - ctx->opts.bs = *data++; - ctx->opts.stmin = *data++; - ctx->bs = ctx->opts.bs; - LOG_DBG("Got CTS. BS: %d, STmin: %d", ctx->opts.bs, - ctx->opts.stmin); + sctx->state = ISOTP_TX_SEND_CF; + sctx->wft = 0; + sctx->tx_backlog = 0; + k_sem_reset(&sctx->tx_sem); + sctx->opts.bs = *data++; + sctx->opts.stmin = *data++; + sctx->bs = sctx->opts.bs; + LOG_DBG("Got CTS. BS: %d, STmin: %d", sctx->opts.bs, + sctx->opts.stmin); break; case ISOTP_PCI_FS_WAIT: LOG_DBG("Got WAIT frame"); - k_timer_start(&ctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); - if (ctx->wft >= CONFIG_ISOTP_WFTMAX) { + k_timer_start(&sctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); + if (sctx->wft >= CONFIG_ISOTP_WFTMAX) { LOG_INF("Got to many wait frames"); - send_report_error(ctx, ISOTP_N_WFT_OVRN); + send_report_error(sctx, ISOTP_N_WFT_OVRN); } - ctx->wft++; + sctx->wft++; break; case ISOTP_PCI_FS_OVFLW: LOG_ERR("Got overflow FC frame"); - send_report_error(ctx, ISOTP_N_BUFFER_OVERFLW); + send_report_error(sctx, ISOTP_N_BUFFER_OVERFLW); break; default: - send_report_error(ctx, ISOTP_N_INVALID_FS); + send_report_error(sctx, ISOTP_N_INVALID_FS); } } static void send_can_rx_cb(const struct device *dev, struct can_frame *frame, void *arg) { - struct isotp_send_ctx *ctx = (struct isotp_send_ctx *)arg; + struct isotp_send_ctx *sctx = (struct isotp_send_ctx *)arg; ARG_UNUSED(dev); - if (ctx->state == ISOTP_TX_WAIT_FC) { - k_timer_stop(&ctx->timer); - send_process_fc(ctx, frame); + if (sctx->state == ISOTP_TX_WAIT_FC) { + k_timer_stop(&sctx->timer); + send_process_fc(sctx, frame); } else { LOG_ERR("Got unexpected PDU"); - send_report_error(ctx, ISOTP_N_UNEXP_PDU); + send_report_error(sctx, ISOTP_N_UNEXP_PDU); } - k_work_submit(&ctx->work); + k_work_submit(&sctx->work); } -static size_t get_send_ctx_data_len(struct isotp_send_ctx *ctx) +static size_t get_send_ctx_data_len(struct isotp_send_ctx *sctx) { - return ctx->is_net_buf ? net_buf_frags_len(ctx->buf) : ctx->len; + return sctx->is_net_buf ? net_buf_frags_len(sctx->buf) : sctx->len; } -static const uint8_t *get_send_ctx_data(struct isotp_send_ctx *ctx) +static const uint8_t *get_send_ctx_data(struct isotp_send_ctx *sctx) { - if (ctx->is_net_buf) { - return ctx->buf->data; + if (sctx->is_net_buf) { + return sctx->buf->data; } else { - return ctx->data; + return sctx->data; } } -static void pull_send_ctx_data(struct isotp_send_ctx *ctx, size_t len) +static void pull_send_ctx_data(struct isotp_send_ctx *sctx, size_t len) { - if (ctx->is_net_buf) { - net_buf_pull_mem(ctx->buf, len); + if (sctx->is_net_buf) { + net_buf_pull_mem(sctx->buf, len); } else { - ctx->data += len; - ctx->len -= len; + sctx->data += len; + sctx->len -= len; } } -static inline int send_sf(struct isotp_send_ctx *ctx) +static inline int send_sf(struct isotp_send_ctx *sctx) { struct can_frame frame; - size_t len = get_ctx_data_length(ctx); + size_t len = get_send_ctx_data_len(sctx); int index = 0; int ret; const uint8_t *data; - prepare_frame(&frame, &ctx->tx_addr); + prepare_frame(&frame, &sctx->tx_addr); - data = get_data_ctx(ctx); - pull_data_ctx(ctx, len); + data = get_send_ctx_data(sctx); + pull_send_ctx_data(sctx, len); - if ((ctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - frame.data[index++] = ctx->tx_addr.ext_addr; + if ((sctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + frame.data[index++] = sctx->tx_addr.ext_addr; } - if (IS_ENABLED(CONFIG_CAN_FD_MODE) && (ctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && + if (IS_ENABLED(CONFIG_CAN_FD_MODE) && (sctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && len > ISOTP_4BIT_SF_MAX_CAN_DL - 1 - index) { frame.data[index++] = ISOTP_PCI_TYPE_SF; frame.data[index++] = len; @@ -926,7 +909,7 @@ static inline int send_sf(struct isotp_send_ctx *ctx) frame.data[index++] = ISOTP_PCI_TYPE_SF | len; } - if (len > ctx->tx_addr.dl - index) { + if (len > sctx->tx_addr.dl - index) { LOG_ERR("SF len does not fit DL"); return -ENOSPC; } @@ -934,7 +917,7 @@ static inline int send_sf(struct isotp_send_ctx *ctx) memcpy(&frame.data[index], data, len); if (IS_ENABLED(CONFIG_ISOTP_ENABLE_TX_PADDING) || - (IS_ENABLED(CONFIG_CAN_FD_MODE) && (ctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && + (IS_ENABLED(CONFIG_CAN_FD_MODE) && (sctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && len + index > ISOTP_PADDED_FRAME_DL_MIN)) { /* AUTOSAR requirements SWS_CanTp_00348 / SWS_CanTp_00351. * Mandatory for ISO-TP CAN-FD frames > 8 bytes. @@ -947,25 +930,25 @@ static inline int send_sf(struct isotp_send_ctx *ctx) frame.dlc = can_bytes_to_dlc(len + index); } - ctx->state = ISOTP_TX_SEND_SF; - ret = can_send(ctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, ctx); + sctx->state = ISOTP_TX_SEND_SF; + ret = can_send(sctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, sctx); return ret; } -static inline int send_ff(struct isotp_send_ctx *ctx) +static inline int send_ff(struct isotp_send_ctx *sctx) { struct can_frame frame; int index = 0; - size_t len = get_send_ctx_data_len(ctx); + size_t len = get_send_ctx_data_len(sctx); int ret; const uint8_t *data; - prepare_frame(&frame, &ctx->tx_addr); + prepare_frame(&frame, &sctx->tx_addr); - frame.dlc = can_bytes_to_dlc(ctx->tx_addr.dl); + frame.dlc = can_bytes_to_dlc(sctx->tx_addr.dl); - if ((ctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - frame.data[index++] = ctx->tx_addr.ext_addr; + if ((sctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + frame.data[index++] = sctx->tx_addr.ext_addr; } if (len > 0xFFF) { @@ -983,16 +966,16 @@ static inline int send_ff(struct isotp_send_ctx *ctx) /* According to ISO FF has sn 0 and is incremented to one * although it's not part of the FF frame */ - ctx->sn = 1; - data = get_send_ctx_data(ctx); - pull_send_ctx_data(ctx, ctx->tx_addr.dl - index); - memcpy(&frame.data[index], data, ctx->tx_addr.dl - index); + sctx->sn = 1; + data = get_send_ctx_data(sctx); + pull_send_ctx_data(sctx, sctx->tx_addr.dl - index); + memcpy(&frame.data[index], data, sctx->tx_addr.dl - index); - ret = can_send(ctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, ctx); + ret = can_send(sctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, sctx); return ret; } -static inline int send_cf(struct isotp_send_ctx *ctx) +static inline int send_cf(struct isotp_send_ctx *sctx) { struct can_frame frame; int index = 0; @@ -1001,23 +984,23 @@ static inline int send_cf(struct isotp_send_ctx *ctx) int rem_len; const uint8_t *data; - prepare_frame(&frame, &ctx->tx_addr); + prepare_frame(&frame, &sctx->tx_addr); - if ((ctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { - frame.data[index++] = ctx->tx_addr.ext_addr; + if ((sctx->tx_addr.flags & ISOTP_MSG_EXT_ADDR) != 0) { + frame.data[index++] = sctx->tx_addr.ext_addr; } /*sn wraps around at 0xF automatically because it has a 4 bit size*/ - frame.data[index++] = ISOTP_PCI_TYPE_CF | ctx->sn; + frame.data[index++] = ISOTP_PCI_TYPE_CF | sctx->sn; - rem_len = get_send_ctx_data_len(ctx); - len = MIN(rem_len, ctx->tx_addr.dl - index); + rem_len = get_send_ctx_data_len(sctx); + len = MIN(rem_len, sctx->tx_addr.dl - index); rem_len -= len; - data = get_send_ctx_data(ctx); + data = get_send_ctx_data(sctx); memcpy(&frame.data[index], data, len); if (IS_ENABLED(CONFIG_ISOTP_ENABLE_TX_PADDING) || - (IS_ENABLED(CONFIG_CAN_FD_MODE) && (ctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && + (IS_ENABLED(CONFIG_CAN_FD_MODE) && (sctx->tx_addr.flags & ISOTP_MSG_FDF) != 0 && len + index > ISOTP_PADDED_FRAME_DL_MIN)) { /* AUTOSAR requirements SWS_CanTp_00348 / SWS_CanTp_00351. * Mandatory for ISO-TP CAN-FD frames > 8 bytes. @@ -1030,12 +1013,12 @@ static inline int send_cf(struct isotp_send_ctx *ctx) frame.dlc = can_bytes_to_dlc(len + index); } - ret = can_send(ctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, ctx); + ret = can_send(sctx->can_dev, &frame, K_MSEC(ISOTP_A_TIMEOUT_MS), send_can_tx_cb, sctx); if (ret == 0) { - ctx->sn++; - pull_send_ctx_data(ctx, len); - ctx->bs--; - ctx->tx_backlog++; + sctx->sn++; + pull_send_ctx_data(sctx, len); + sctx->bs--; + sctx->tx_backlog++; } ret = ret ? ret : rem_len; @@ -1043,28 +1026,28 @@ static inline int send_cf(struct isotp_send_ctx *ctx) } #ifdef CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS -static inline void free_send_ctx(struct isotp_send_ctx **ctx) +static inline void free_send_ctx(struct isotp_send_ctx **sctx) { - if ((*ctx)->is_net_buf) { - net_buf_unref((*ctx)->buf); - (*ctx)->buf = NULL; + if ((*sctx)->is_net_buf) { + net_buf_unref((*sctx)->buf); + (*sctx)->buf = NULL; } - if ((*ctx)->is_ctx_slab) { - k_mem_slab_free(&ctx_slab, (void *)*ctx); + if ((*sctx)->is_ctx_slab) { + k_mem_slab_free(&ctx_slab, (void *)*sctx); } } -static int alloc_send_ctx(struct isotp_send_ctx **ctx, k_timeout_t timeout) +static int alloc_send_ctx(struct isotp_send_ctx **sctx, k_timeout_t timeout) { int ret; - ret = k_mem_slab_alloc(&ctx_slab, (void **)ctx, timeout); + ret = k_mem_slab_alloc(&ctx_slab, (void **)sctx, timeout); if (ret) { return ISOTP_NO_CTX_LEFT; } - (*ctx)->is_ctx_slab = 1; + (*sctx)->is_ctx_slab = 1; return 0; } @@ -1087,56 +1070,56 @@ static k_timeout_t stmin_to_timeout(uint8_t stmin) return K_MSEC(stmin); } -static void send_state_machine(struct isotp_send_ctx *ctx) +static void send_state_machine(struct isotp_send_ctx *sctx) { int ret; - switch (ctx->state) { + switch (sctx->state) { case ISOTP_TX_SEND_FF: - send_ff(ctx); - k_timer_start(&ctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); - ctx->state = ISOTP_TX_WAIT_FC; + send_ff(sctx); + k_timer_start(&sctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); + sctx->state = ISOTP_TX_WAIT_FC; LOG_DBG("SM send FF"); break; case ISOTP_TX_SEND_CF: LOG_DBG("SM send CF"); - k_timer_stop(&ctx->timer); + k_timer_stop(&sctx->timer); do { - ret = send_cf(ctx); + ret = send_cf(sctx); if (!ret) { - ctx->state = ISOTP_TX_WAIT_BACKLOG; + sctx->state = ISOTP_TX_WAIT_BACKLOG; break; } if (ret < 0) { LOG_ERR("Failed to send CF"); - send_report_error(ctx, ret == -EAGAIN ? + send_report_error(sctx, ret == -EAGAIN ? ISOTP_N_TIMEOUT_A : ISOTP_N_ERROR); break; } - if (ctx->opts.bs && !ctx->bs) { - k_timer_start(&ctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); - ctx->state = ISOTP_TX_WAIT_FC; + if (sctx->opts.bs && !sctx->bs) { + k_timer_start(&sctx->timer, K_MSEC(ISOTP_BS_TIMEOUT_MS), K_NO_WAIT); + sctx->state = ISOTP_TX_WAIT_FC; LOG_DBG("BS reached. Wait for FC again"); break; - } else if (ctx->opts.stmin) { - ctx->state = ISOTP_TX_WAIT_ST; + } else if (sctx->opts.stmin) { + sctx->state = ISOTP_TX_WAIT_ST; break; } /* Ensure FIFO style transmission of CF */ - k_sem_take(&ctx->tx_sem, K_FOREVER); + k_sem_take(&sctx->tx_sem, K_FOREVER); } while (ret > 0); break; case ISOTP_TX_WAIT_ST: - k_timer_start(&ctx->timer, stmin_to_timeout(ctx->opts.stmin), K_NO_WAIT); - ctx->state = ISOTP_TX_SEND_CF; + k_timer_start(&sctx->timer, stmin_to_timeout(sctx->opts.stmin), K_NO_WAIT); + sctx->state = ISOTP_TX_SEND_CF; LOG_DBG("SM wait ST"); break; @@ -1146,21 +1129,21 @@ static void send_state_machine(struct isotp_send_ctx *ctx) case ISOTP_TX_SEND_SF: __fallthrough; case ISOTP_TX_WAIT_FIN: - if (ctx->filter_id >= 0) { - can_remove_rx_filter(ctx->can_dev, ctx->filter_id); + if (sctx->filter_id >= 0) { + can_remove_rx_filter(sctx->can_dev, sctx->filter_id); } LOG_DBG("SM finish"); - k_timer_stop(&ctx->timer); + k_timer_stop(&sctx->timer); - if (ctx->has_callback) { - ctx->fin_cb.cb(ctx->error_nr, ctx->fin_cb.arg); - free_send_ctx(&ctx); + if (sctx->has_callback) { + sctx->fin_cb.cb(sctx->error_nr, sctx->fin_cb.arg); + free_send_ctx(&sctx); } else { - k_sem_give(&ctx->fin_sem); + k_sem_give(&sctx->fin_sem); } - ctx->state = ISOTP_TX_STATE_RESET; + sctx->state = ISOTP_TX_STATE_RESET; break; default: @@ -1170,29 +1153,28 @@ static void send_state_machine(struct isotp_send_ctx *ctx) static void send_work_handler(struct k_work *item) { - struct isotp_send_ctx *ctx = CONTAINER_OF(item, struct isotp_send_ctx, - work); + struct isotp_send_ctx *sctx = CONTAINER_OF(item, struct isotp_send_ctx, work); - send_state_machine(ctx); + send_state_machine(sctx); } -static inline int add_fc_filter(struct isotp_send_ctx *ctx) +static inline int add_fc_filter(struct isotp_send_ctx *sctx) { struct can_filter filter; - prepare_filter(&filter, &ctx->rx_addr, CAN_EXT_ID_MASK); + prepare_filter(&filter, &sctx->rx_addr, CAN_EXT_ID_MASK); - ctx->filter_id = can_add_rx_filter(ctx->can_dev, send_can_rx_cb, ctx, + sctx->filter_id = can_add_rx_filter(sctx->can_dev, send_can_rx_cb, sctx, &filter); - if (ctx->filter_id < 0) { - LOG_ERR("Error adding FC filter [%d]", ctx->filter_id); + if (sctx->filter_id < 0) { + LOG_ERR("Error adding FC filter [%d]", sctx->filter_id); return ISOTP_NO_FREE_FILTER; } return 0; } -static int send(struct isotp_send_ctx *ctx, const struct device *can_dev, +static int send(struct isotp_send_ctx *sctx, const struct device *can_dev, const struct isotp_msg_id *tx_addr, const struct isotp_msg_id *rx_addr, isotp_tx_callback_t complete_cb, void *cb_arg) @@ -1201,7 +1183,7 @@ static int send(struct isotp_send_ctx *ctx, const struct device *can_dev, size_t len; int ret; - __ASSERT_NO_MSG(ctx); + __ASSERT_NO_MSG(sctx); __ASSERT_NO_MSG(can_dev); __ASSERT_NO_MSG(rx_addr && tx_addr); @@ -1214,29 +1196,29 @@ static int send(struct isotp_send_ctx *ctx, const struct device *can_dev, } if (complete_cb) { - ctx->fin_cb.cb = complete_cb; - ctx->fin_cb.arg = cb_arg; - ctx->has_callback = 1; + sctx->fin_cb.cb = complete_cb; + sctx->fin_cb.arg = cb_arg; + sctx->has_callback = 1; } else { - k_sem_init(&ctx->fin_sem, 0, 1); - ctx->has_callback = 0; + k_sem_init(&sctx->fin_sem, 0, 1); + sctx->has_callback = 0; } - k_sem_init(&ctx->tx_sem, 0, 1); - ctx->can_dev = can_dev; - ctx->tx_addr = *tx_addr; - ctx->rx_addr = *rx_addr; - ctx->error_nr = ISOTP_N_OK; - ctx->wft = 0; - k_work_init(&ctx->work, send_work_handler); - k_timer_init(&ctx->timer, send_timeout_handler, NULL); + k_sem_init(&sctx->tx_sem, 0, 1); + sctx->can_dev = can_dev; + sctx->tx_addr = *tx_addr; + sctx->rx_addr = *rx_addr; + sctx->error_nr = ISOTP_N_OK; + sctx->wft = 0; + k_work_init(&sctx->work, send_work_handler); + k_timer_init(&sctx->timer, send_timeout_handler, NULL); - switch (ctx->tx_addr.dl) { + switch (sctx->tx_addr.dl) { case 0: - if ((ctx->tx_addr.flags & ISOTP_MSG_FDF) == 0) { - ctx->tx_addr.dl = 8; + if ((sctx->tx_addr.flags & ISOTP_MSG_FDF) == 0) { + sctx->tx_addr.dl = 8; } else { - ctx->tx_addr.dl = 64; + sctx->tx_addr.dl = 64; } __fallthrough; case 8: @@ -1248,65 +1230,65 @@ static int send(struct isotp_send_ctx *ctx, const struct device *can_dev, case 32: case 48: case 64: - if ((ctx->tx_addr.flags & ISOTP_MSG_FDF) == 0) { + if ((sctx->tx_addr.flags & ISOTP_MSG_FDF) == 0) { LOG_ERR("TX_DL > 8 only supported with FD mode"); return ISOTP_N_ERROR; } break; default: - LOG_ERR("Invalid TX_DL: %u", ctx->tx_addr.dl); + LOG_ERR("Invalid TX_DL: %u", sctx->tx_addr.dl); return ISOTP_N_ERROR; } - len = get_send_ctx_data_len(ctx); + len = get_send_ctx_data_len(sctx); LOG_DBG("Send %zu bytes to addr 0x%x and listen on 0x%x", len, - ctx->tx_addr.ext_id, ctx->rx_addr.ext_id); + sctx->tx_addr.ext_id, sctx->rx_addr.ext_id); /* Single frames > 8 bytes use an additional byte for length (CAN-FD only) */ - if (len > ctx->tx_addr.dl - (((tx_addr->flags & ISOTP_MSG_EXT_ADDR) != 0) ? 2 : 1) - - ((ctx->tx_addr.dl > ISOTP_4BIT_SF_MAX_CAN_DL) ? 1 : 0)) { - ret = add_fc_filter(ctx); + if (len > sctx->tx_addr.dl - (((tx_addr->flags & ISOTP_MSG_EXT_ADDR) != 0) ? 2 : 1) - + ((sctx->tx_addr.dl > ISOTP_4BIT_SF_MAX_CAN_DL) ? 1 : 0)) { + ret = add_fc_filter(sctx); if (ret) { LOG_ERR("Can't add fc filter: %d", ret); - free_send_ctx(&ctx); + free_send_ctx(&sctx); return ret; } LOG_DBG("Starting work to send FF"); - ctx->state = ISOTP_TX_SEND_FF; - k_work_submit(&ctx->work); + sctx->state = ISOTP_TX_SEND_FF; + k_work_submit(&sctx->work); } else { LOG_DBG("Sending single frame"); - ctx->filter_id = -1; - ret = send_sf(ctx); + sctx->filter_id = -1; + ret = send_sf(sctx); if (ret) { - free_send_ctx(&ctx); + free_send_ctx(&sctx); return ret == -EAGAIN ? ISOTP_N_TIMEOUT_A : ISOTP_N_ERROR; } } if (!complete_cb) { - k_sem_take(&ctx->fin_sem, K_FOREVER); - ret = ctx->error_nr; - free_send_ctx(&ctx); + k_sem_take(&sctx->fin_sem, K_FOREVER); + ret = sctx->error_nr; + free_send_ctx(&sctx); return ret; } return ISOTP_N_OK; } -int isotp_send(struct isotp_send_ctx *ctx, const struct device *can_dev, +int isotp_send(struct isotp_send_ctx *sctx, const struct device *can_dev, const uint8_t *data, size_t len, const struct isotp_msg_id *tx_addr, const struct isotp_msg_id *rx_addr, isotp_tx_callback_t complete_cb, void *cb_arg) { - ctx->data = data; - ctx->len = len; - ctx->is_ctx_slab = 0; - ctx->is_net_buf = 0; + sctx->data = data; + sctx->len = len; + sctx->is_ctx_slab = 0; + sctx->is_net_buf = 0; - return send(ctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); + return send(sctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); } #ifdef CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS @@ -1318,21 +1300,21 @@ int isotp_send_ctx_buf(const struct device *can_dev, isotp_tx_callback_t complete_cb, void *cb_arg, k_timeout_t timeout) { - struct isotp_send_ctx *ctx; + struct isotp_send_ctx *sctx; int ret; __ASSERT_NO_MSG(data); - ret = alloc_send_ctx(&ctx, timeout); + ret = alloc_send_ctx(&sctx, timeout); if (ret) { return ret; } - ctx->data = data; - ctx->len = len; - ctx->is_net_buf = 0; + sctx->data = data; + sctx->len = len; + sctx->is_net_buf = 0; - return send(ctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); + return send(sctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); } int isotp_send_net_ctx_buf(const struct device *can_dev, @@ -1342,20 +1324,20 @@ int isotp_send_net_ctx_buf(const struct device *can_dev, isotp_tx_callback_t complete_cb, void *cb_arg, k_timeout_t timeout) { - struct isotp_send_ctx *ctx; + struct isotp_send_ctx *sctx; int ret; __ASSERT_NO_MSG(data); - ret = alloc_send_ctx(&ctx, timeout); + ret = alloc_send_ctx(&sctx, timeout); if (ret) { return ret; } - ctx->is_net_buf = 1; - ctx->buf = data; + sctx->is_net_buf = 1; + sctx->buf = data; - return send(ctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); + return send(sctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); } #ifdef CONFIG_ISOTP_USE_TX_BUF @@ -1366,29 +1348,29 @@ int isotp_send_buf(const struct device *can_dev, isotp_tx_callback_t complete_cb, void *cb_arg, k_timeout_t timeout) { - struct isotp_send_ctx *ctx; + struct isotp_send_ctx *sctx; struct net_buf *buf; int ret; __ASSERT_NO_MSG(data); - ret = alloc_send_ctx(&ctx, timeout); + ret = alloc_send_ctx(&sctx, timeout); if (ret) { return ret; } buf = net_buf_alloc_len(&isotp_tx_pool, len, timeout); if (!buf) { - k_mem_slab_free(&ctx_slab, (void *)ctx); + k_mem_slab_free(&ctx_slab, (void *)sctx); return ISOTP_NO_BUF_DATA_LEFT; } net_buf_add_mem(buf, data, len); - ctx->is_net_buf = 1; - ctx->buf = buf; + sctx->is_net_buf = 1; + sctx->buf = buf; - return send(ctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); + return send(sctx, can_dev, tx_addr, rx_addr, complete_cb, cb_arg); } #endif /*CONFIG_ISOTP_USE_TX_BUF*/ #endif /*CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS*/