Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
isotp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Alexander Wachter
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13
14#ifndef ZEPHYR_INCLUDE_CANBUS_ISOTP_H_
15#define ZEPHYR_INCLUDE_CANBUS_ISOTP_H_
16
25
26#include <zephyr/drivers/can.h>
27#include <zephyr/types.h>
28#include <zephyr/net_buf.h>
29
30/*
31 * Abbreviations
32 * BS Block Size
33 * CAN_DL CAN LL data size
34 * CF Consecutive Frame
35 * CTS Continue to send
36 * DLC Data length code
37 * FC Flow Control
38 * FF First Frame
39 * SF Single Frame
40 * FS Flow Status
41 * AE Address Extension
42 * SN Sequence Number
43 * ST Separation time
44 * SA Source Address
45 * TA Target Address
46 * RX_DL CAN RX LL data size
47 * TX_DL CAN TX LL data size
48 * PCI Process Control Information
49 */
50
51/*
52 * N_Result according to ISO 15765-2:2016
53 * ISOTP_ prefix is used to be zephyr conform
54 */
55
57#define ISOTP_N_OK 0
58
60#define ISOTP_N_TIMEOUT_A -1
61
63#define ISOTP_N_TIMEOUT_BS -2
64
66#define ISOTP_N_TIMEOUT_CR -3
67
69#define ISOTP_N_WRONG_SN -4
70
72#define ISOTP_N_INVALID_FS -5
73
75#define ISOTP_N_UNEXP_PDU -6
76
78#define ISOTP_N_WFT_OVRN -7
79
81#define ISOTP_N_BUFFER_OVERFLW -8
82
84#define ISOTP_N_ERROR -9
85
87
89#define ISOTP_NO_FREE_FILTER -10
90
92#define ISOTP_NO_NET_BUF_LEFT -11
93
95#define ISOTP_NO_BUF_DATA_LEFT -12
96
98#define ISOTP_NO_CTX_LEFT -13
99
101#define ISOTP_RECV_TIMEOUT -14
102
103/*
104 * CAN ID filtering for ISO-TP fixed addressing according to SAE J1939
105 *
106 * Format of 29-bit CAN identifier:
107 * ------------------------------------------------------
108 * | 28 .. 26 | 25 | 24 | 23 .. 16 | 15 .. 8 | 7 .. 0 |
109 * ------------------------------------------------------
110 * | Priority | EDP | DP | N_TAtype | N_TA | N_SA |
111 * ------------------------------------------------------
112 */
113
115#define ISOTP_FIXED_ADDR_SA_POS (CONFIG_ISOTP_FIXED_ADDR_SA_POS)
116
118#define ISOTP_FIXED_ADDR_SA_MASK (CONFIG_ISOTP_FIXED_ADDR_SA_MASK)
119
121#define ISOTP_FIXED_ADDR_TA_POS (CONFIG_ISOTP_FIXED_ADDR_TA_POS)
122
124#define ISOTP_FIXED_ADDR_TA_MASK (CONFIG_ISOTP_FIXED_ADDR_TA_MASK)
125
127#define ISOTP_FIXED_ADDR_PRIO_POS (CONFIG_ISOTP_FIXED_ADDR_PRIO_POS)
128
130#define ISOTP_FIXED_ADDR_PRIO_MASK (CONFIG_ISOTP_FIXED_ADDR_PRIO_MASK)
131
133#define ISOTP_FIXED_ADDR_RX_MASK (CONFIG_ISOTP_FIXED_ADDR_RX_MASK)
134
135#ifdef __cplusplus
136extern "C" {
137#endif
138
145
147#define ISOTP_MSG_EXT_ADDR BIT(0)
148
153#define ISOTP_MSG_FIXED_ADDR BIT(1)
154
156#define ISOTP_MSG_IDE BIT(2)
157
159#define ISOTP_MSG_FDF BIT(3)
160
162#define ISOTP_MSG_BRS BIT(4)
163
165
198
199/*
200 * STmin is split in two valid ranges:
201 * 0-127: 0ms-127ms
202 * 128-240: Reserved
203 * 241-249: 100us-900us (multiples of 100us)
204 * 250- : Reserved
205 */
206
216
225typedef void (*isotp_tx_callback_t)(int error_nr, void *arg);
226
227struct isotp_send_ctx;
228struct isotp_recv_ctx;
229
249int isotp_bind(struct isotp_recv_ctx *rctx, const struct device *can_dev,
250 const struct isotp_msg_id *rx_addr,
251 const struct isotp_msg_id *tx_addr,
252 const struct isotp_fc_opts *opts,
253 k_timeout_t timeout);
254
265void isotp_unbind(struct isotp_recv_ctx *rctx);
266
284int isotp_recv(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len, k_timeout_t timeout);
285
304int isotp_recv_net(struct isotp_recv_ctx *rctx, struct net_buf **buffer, k_timeout_t timeout);
305
327int isotp_send(struct isotp_send_ctx *sctx, const struct device *can_dev,
328 const uint8_t *data, size_t len,
329 const struct isotp_msg_id *tx_addr,
330 const struct isotp_msg_id *rx_addr,
331 isotp_tx_callback_t complete_cb, void *cb_arg);
332
333#ifdef CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS
352int isotp_send_ctx_buf(const struct device *can_dev,
353 const uint8_t *data, size_t len,
354 const struct isotp_msg_id *tx_addr,
355 const struct isotp_msg_id *rx_addr,
356 isotp_tx_callback_t complete_cb, void *cb_arg,
357 k_timeout_t timeout);
358
377int isotp_send_net_ctx_buf(const struct device *can_dev,
378 struct net_buf *data,
379 const struct isotp_msg_id *tx_addr,
380 const struct isotp_msg_id *rx_addr,
381 isotp_tx_callback_t complete_cb, void *cb_arg,
382 k_timeout_t timeout);
383
384#endif /*CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS*/
385
386#if defined(CONFIG_ISOTP_USE_TX_BUF) && \
387 defined(CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS)
407int isotp_send_buf(const struct device *can_dev,
408 const uint8_t *data, size_t len,
409 const struct isotp_msg_id *tx_addr,
410 const struct isotp_msg_id *rx_addr,
411 isotp_tx_callback_t complete_cb, void *cb_arg,
412 k_timeout_t timeout);
413#endif
414
416
417struct isotp_callback {
419 void *arg;
420};
421
422struct isotp_send_ctx {
423 int filter_id;
424 uint32_t error_nr;
425 const struct device *can_dev;
426 union {
427 struct net_buf *buf;
428 struct {
429 const uint8_t *data;
430 size_t len;
431 };
432 };
433 struct k_work work;
434 struct k_timer timer;
435 union {
436 struct isotp_callback fin_cb;
437 struct k_sem fin_sem;
438 };
439 struct isotp_fc_opts opts;
441 uint8_t tx_backlog;
442 struct k_sem tx_sem;
443 struct isotp_msg_id rx_addr;
444 struct isotp_msg_id tx_addr;
445 uint8_t wft;
446 uint8_t bs;
447 uint8_t sn : 4;
448 uint8_t is_net_buf : 1;
449 uint8_t is_ctx_slab : 1;
450 uint8_t has_callback: 1;
451};
452
453struct isotp_recv_ctx {
454 int filter_id;
455 const struct device *can_dev;
456 struct net_buf *buf;
457 struct net_buf *act_frag;
458 /* buffer currently processed in isotp_recv */
459 struct net_buf *recv_buf;
460 sys_snode_t alloc_node;
461 uint32_t length;
462 int error_nr;
463 struct k_work work;
464 struct k_timer timer;
465 struct k_fifo fifo;
466 struct isotp_msg_id rx_addr;
467 struct isotp_msg_id tx_addr;
468 struct isotp_fc_opts opts;
470 uint8_t bs;
471 uint8_t wft;
472 uint8_t sn_expected : 4;
473};
474
476
480
481#ifdef __cplusplus
482}
483#endif
484
485#endif /* ZEPHYR_INCLUDE_CANBUS_ISOTP_H_ */
Header file for Controller Area Network (CAN) controller driver API.
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)
Send data.
int isotp_recv_net(struct isotp_recv_ctx *rctx, struct net_buf **buffer, k_timeout_t timeout)
Get the net buffer on data reception.
void isotp_unbind(struct isotp_recv_ctx *rctx)
Unbind a context from the interface.
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, k_timeout_t timeout)
Bind an address to a receiving context.
int isotp_recv(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len, k_timeout_t timeout)
Read out received data from fifo.
void(* isotp_tx_callback_t)(int error_nr, void *arg)
Transmission callback.
Definition isotp.h:225
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:42
Buffer management.
state
Definition parser_state.h:29
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
ISO-TP frame control options struct.
Definition isotp.h:212
uint8_t stmin
Minimum separation time.
Definition isotp.h:214
uint8_t bs
Block size.
Definition isotp.h:213
ISO-TP message id struct.
Definition isotp.h:171
uint8_t flags
Flags.
Definition isotp.h:196
uint32_t ext_id
Definition isotp.h:180
uint8_t ext_addr
ISO-TP extended address (if used).
Definition isotp.h:183
uint32_t std_id
Definition isotp.h:179
uint8_t dl
ISO-TP frame data length (TX_DL for TX address or RX_DL for RX address).
Definition isotp.h:194
Kernel timeout type.
Definition clock.h:65
Network buffer representation.
Definition net_buf.h:1015