Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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
14#ifndef ZEPHYR_INCLUDE_ISOTP_H_
15#define ZEPHYR_INCLUDE_ISOTP_H_
16
24#include <drivers/can.h>
25#include <zephyr/types.h>
26#include <net/buf.h>
27
28/*
29 * Abbreviations
30 * BS Block Size
31 * CAN_DL CAN LL data size
32 * CF Consecutive Frame
33 * CTS Continue to send
34 * DLC Data length code
35 * FC Flow Control
36 * FF First Frame
37 * FS Flow Status
38 * AE Address Extension
39 * SA Source Address
40 * TA Target Address
41 */
42
43/*
44 * N_Result according to ISO 15765-2:2016
45 * ISOTP_ prefix is used to be zephyr conform
46 */
47
49#define ISOTP_N_OK 0
50
52#define ISOTP_N_TIMEOUT_A -1
53
55#define ISOTP_N_TIMEOUT_BS -2
56
58#define ISOTP_N_TIMEOUT_CR -3
59
61#define ISOTP_N_WRONG_SN -4
62
64#define ISOTP_N_INVALID_FS -5
65
67#define ISOTP_N_UNEXP_PDU -6
68
70#define ISOTP_N_WFT_OVRN -7
71
73#define ISOTP_N_BUFFER_OVERFLW -8
74
76#define ISOTP_N_ERROR -9
77
81#define ISOTP_NO_FREE_FILTER -10
82
84#define ISOTP_NO_NET_BUF_LEFT -11
85
87#define ISOTP_NO_BUF_DATA_LEFT -12
88
90#define ISOTP_NO_CTX_LEFT -13
91
93#define ISOTP_RECV_TIMEOUT -14
94
95/*
96 * CAN ID filtering for ISO-TP fixed addressing according to SAE J1939
97 *
98 * Format of 29-bit CAN identifier:
99 * ------------------------------------------------------
100 * | 28 .. 26 | 25 | 24 | 23 .. 16 | 15 .. 8 | 7 .. 0 |
101 * ------------------------------------------------------
102 * | Priority | EDP | DP | N_TAtype | N_TA | N_SA |
103 * ------------------------------------------------------
104 */
105
107#define ISOTP_FIXED_ADDR_SA_POS (0U)
108
110#define ISOTP_FIXED_ADDR_SA_MASK (0xFF << ISOTP_FIXED_ADDR_SA_POS)
111
113#define ISOTP_FIXED_ADDR_TA_POS (8U)
114
116#define ISOTP_FIXED_ADDR_TA_MASK (0xFF << ISOTP_FIXED_ADDR_TA_POS)
117
119#define ISOTP_FIXED_ADDR_PRIO_POS (26U)
120
122#define ISOTP_FIXED_ADDR_PRIO_MASK (0x7 << ISOTP_FIXED_ADDR_PRIO_POS)
123
124/* CAN filter RX mask to match any priority and source address (SA) */
125#define ISOTP_FIXED_ADDR_RX_MASK (0x03FFFF00)
126
127#ifdef __cplusplus
128extern "C" {
129#endif
130
143 union {
146 };
155};
156
157/*
158 * STmin is split in two valid ranges:
159 * 0-127: 0ms-127ms
160 * 128-240: Reserved
161 * 241-249: 100us-900us (multiples of 100us)
162 * 250- : Reserved
163 */
164
173};
174
175typedef void (*isotp_tx_callback_t)(int error_nr, void *arg);
176
177struct isotp_send_ctx;
178struct isotp_recv_ctx;
179
199int isotp_bind(struct isotp_recv_ctx *ctx, const struct device *can_dev,
200 const struct isotp_msg_id *rx_addr,
201 const struct isotp_msg_id *tx_addr,
202 const struct isotp_fc_opts *opts,
204
215void isotp_unbind(struct isotp_recv_ctx *ctx);
216
234int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len,
236
255int isotp_recv_net(struct isotp_recv_ctx *ctx, struct net_buf **buffer,
257
279int isotp_send(struct isotp_send_ctx *ctx, const struct device *can_dev,
280 const uint8_t *data, size_t len,
281 const struct isotp_msg_id *tx_addr,
282 const struct isotp_msg_id *rx_addr,
283 isotp_tx_callback_t complete_cb, void *cb_arg);
284
285#ifdef CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS
304int isotp_send_ctx_buf(const struct device *can_dev,
305 const uint8_t *data, size_t len,
306 const struct isotp_msg_id *tx_addr,
307 const struct isotp_msg_id *rx_addr,
308 isotp_tx_callback_t complete_cb, void *cb_arg,
310
329int isotp_send_net_ctx_buf(const struct device *can_dev,
330 struct net_buf *data,
331 const struct isotp_msg_id *tx_addr,
332 const struct isotp_msg_id *rx_addr,
333 isotp_tx_callback_t complete_cb, void *cb_arg,
335
336#endif /*CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS*/
337
338#if defined(CONFIG_ISOTP_USE_TX_BUF) && \
339 defined(CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS)
359int isotp_send_buf(const struct device *can_dev,
360 const uint8_t *data, size_t len,
361 const struct isotp_msg_id *tx_addr,
362 const struct isotp_msg_id *rx_addr,
363 isotp_tx_callback_t complete_cb, void *cb_arg,
365#endif
366
369struct isotp_callback {
371 void *arg;
372};
373
374struct isotp_send_ctx {
375 int filter_id;
376 uint32_t error_nr;
377 const struct device *can_dev;
378 union {
379 struct net_buf *buf;
380 struct {
381 const uint8_t *data;
382 size_t len;
383 };
384 };
385 struct k_work work;
386 struct _timeout timeout;
387 union {
388 struct isotp_callback fin_cb;
389 struct k_sem fin_sem;
390 };
391 struct isotp_fc_opts opts;
393 uint8_t tx_backlog;
394 struct isotp_msg_id rx_addr;
395 struct isotp_msg_id tx_addr;
396 uint8_t wft;
397 uint8_t bs;
398 uint8_t sn : 4;
399 uint8_t is_net_buf : 1;
400 uint8_t is_ctx_slab : 1;
401 uint8_t has_callback: 1;
402};
403
404struct isotp_recv_ctx {
405 int filter_id;
406 const struct device *can_dev;
407 struct net_buf *buf;
408 struct net_buf *act_frag;
409 sys_snode_t alloc_node;
410 uint32_t length;
411 int error_nr;
412 struct k_work work;
413 struct _timeout timeout;
414 struct k_fifo fifo;
415 struct isotp_msg_id rx_addr;
416 struct isotp_msg_id tx_addr;
417 struct isotp_fc_opts opts;
419 uint8_t bs;
420 uint8_t wft;
421 uint8_t sn_expected : 4;
422};
423
430#ifdef __cplusplus
431}
432#endif
433
434#endif /* ZEPHYR_INCLUDE_ISOTP_H_ */
ZTEST_BMEM int timeout
Definition: main.c:31
Public APIs for the CAN drivers.
struct k_fifo fifo
Definition: errno.c:43
void
Definition: eswifi_shell.c:15
int isotp_recv(struct isotp_recv_ctx *ctx, uint8_t *data, size_t len, k_timeout_t timeout)
Read out received data from fifo.
int isotp_bind(struct isotp_recv_ctx *ctx, 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.
void isotp_unbind(struct isotp_recv_ctx *ctx)
Unbind a context from the interface.
int isotp_send(struct isotp_send_ctx *ctx, 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 *ctx, struct net_buf **buffer, k_timeout_t timeout)
Get the net buffer on data reception.
void(* isotp_tx_callback_t)(int error_nr, void *arg)
Definition: isotp.h:175
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
state
Definition: http_parser_state.h:30
Buffer management.
static struct k_work work[2]
Definition: main.c:16
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
ISO-TP frame control options struct.
Definition: isotp.h:170
uint8_t stmin
Definition: isotp.h:172
uint8_t bs
Definition: isotp.h:171
ISO-TP message id struct.
Definition: isotp.h:136
uint32_t ext_id
Definition: isotp.h:145
uint8_t id_type
Definition: isotp.h:150
uint8_t ext_addr
Definition: isotp.h:148
uint8_t use_fixed_addr
Definition: isotp.h:154
uint8_t use_ext_addr
Definition: isotp.h:152
uint32_t std_id
Definition: isotp.h:144
Definition: kernel.h:2153
Kernel timeout type.
Definition: sys_clock.h:65
A structure used to submit work.
Definition: kernel.h:3623
Network buffer representation.
Definition: buf.h:919
static fdata_t data[2]
Definition: test_fifo_contexts.c:15