Zephyr API Documentation 4.0.0-rc2
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
14#ifndef ZEPHYR_INCLUDE_CANBUS_ISOTP_H_
15#define ZEPHYR_INCLUDE_CANBUS_ISOTP_H_
16
24#include <zephyr/drivers/can.h>
25#include <zephyr/types.h>
26#include <zephyr/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 * SF Single Frame
38 * FS Flow Status
39 * AE Address Extension
40 * SN Sequence Number
41 * ST Separation time
42 * SA Source Address
43 * TA Target Address
44 * RX_DL CAN RX LL data size
45 * TX_DL CAN TX LL data size
46 * PCI Process Control Information
47 */
48
49/*
50 * N_Result according to ISO 15765-2:2016
51 * ISOTP_ prefix is used to be zephyr conform
52 */
53
55#define ISOTP_N_OK 0
56
58#define ISOTP_N_TIMEOUT_A -1
59
61#define ISOTP_N_TIMEOUT_BS -2
62
64#define ISOTP_N_TIMEOUT_CR -3
65
67#define ISOTP_N_WRONG_SN -4
68
70#define ISOTP_N_INVALID_FS -5
71
73#define ISOTP_N_UNEXP_PDU -6
74
76#define ISOTP_N_WFT_OVRN -7
77
79#define ISOTP_N_BUFFER_OVERFLW -8
80
82#define ISOTP_N_ERROR -9
83
87#define ISOTP_NO_FREE_FILTER -10
88
90#define ISOTP_NO_NET_BUF_LEFT -11
91
93#define ISOTP_NO_BUF_DATA_LEFT -12
94
96#define ISOTP_NO_CTX_LEFT -13
97
99#define ISOTP_RECV_TIMEOUT -14
100
101/*
102 * CAN ID filtering for ISO-TP fixed addressing according to SAE J1939
103 *
104 * Format of 29-bit CAN identifier:
105 * ------------------------------------------------------
106 * | 28 .. 26 | 25 | 24 | 23 .. 16 | 15 .. 8 | 7 .. 0 |
107 * ------------------------------------------------------
108 * | Priority | EDP | DP | N_TAtype | N_TA | N_SA |
109 * ------------------------------------------------------
110 */
111
113#define ISOTP_FIXED_ADDR_SA_POS (CONFIG_ISOTP_FIXED_ADDR_SA_POS)
114
116#define ISOTP_FIXED_ADDR_SA_MASK (CONFIG_ISOTP_FIXED_ADDR_SA_MASK)
117
119#define ISOTP_FIXED_ADDR_TA_POS (CONFIG_ISOTP_FIXED_ADDR_TA_POS)
120
122#define ISOTP_FIXED_ADDR_TA_MASK (CONFIG_ISOTP_FIXED_ADDR_TA_MASK)
123
125#define ISOTP_FIXED_ADDR_PRIO_POS (CONFIG_ISOTP_FIXED_ADDR_PRIO_POS)
126
128#define ISOTP_FIXED_ADDR_PRIO_MASK (CONFIG_ISOTP_FIXED_ADDR_PRIO_MASK)
129
131#define ISOTP_FIXED_ADDR_RX_MASK (CONFIG_ISOTP_FIXED_ADDR_RX_MASK)
132
133#ifdef __cplusplus
134extern "C" {
135#endif
136
145#define ISOTP_MSG_EXT_ADDR BIT(0)
146
151#define ISOTP_MSG_FIXED_ADDR BIT(1)
152
154#define ISOTP_MSG_IDE BIT(2)
155
157#define ISOTP_MSG_FDF BIT(3)
158
160#define ISOTP_MSG_BRS BIT(4)
161
196
197/*
198 * STmin is split in two valid ranges:
199 * 0-127: 0ms-127ms
200 * 128-240: Reserved
201 * 241-249: 100us-900us (multiples of 100us)
202 * 250- : Reserved
203 */
204
214
223typedef void (*isotp_tx_callback_t)(int error_nr, void *arg);
224
225struct isotp_send_ctx;
226struct isotp_recv_ctx;
227
247int isotp_bind(struct isotp_recv_ctx *rctx, const struct device *can_dev,
248 const struct isotp_msg_id *rx_addr,
249 const struct isotp_msg_id *tx_addr,
250 const struct isotp_fc_opts *opts,
251 k_timeout_t timeout);
252
263void isotp_unbind(struct isotp_recv_ctx *rctx);
264
282int isotp_recv(struct isotp_recv_ctx *rctx, uint8_t *data, size_t len, k_timeout_t timeout);
283
302int isotp_recv_net(struct isotp_recv_ctx *rctx, struct net_buf **buffer, k_timeout_t timeout);
303
325int isotp_send(struct isotp_send_ctx *sctx, const struct device *can_dev,
326 const uint8_t *data, size_t len,
327 const struct isotp_msg_id *tx_addr,
328 const struct isotp_msg_id *rx_addr,
329 isotp_tx_callback_t complete_cb, void *cb_arg);
330
331#ifdef CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS
350int isotp_send_ctx_buf(const struct device *can_dev,
351 const uint8_t *data, size_t len,
352 const struct isotp_msg_id *tx_addr,
353 const struct isotp_msg_id *rx_addr,
354 isotp_tx_callback_t complete_cb, void *cb_arg,
355 k_timeout_t timeout);
356
375int isotp_send_net_ctx_buf(const struct device *can_dev,
376 struct net_buf *data,
377 const struct isotp_msg_id *tx_addr,
378 const struct isotp_msg_id *rx_addr,
379 isotp_tx_callback_t complete_cb, void *cb_arg,
380 k_timeout_t timeout);
381
382#endif /*CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS*/
383
384#if defined(CONFIG_ISOTP_USE_TX_BUF) && \
385 defined(CONFIG_ISOTP_ENABLE_CONTEXT_BUFFERS)
405int isotp_send_buf(const struct device *can_dev,
406 const uint8_t *data, size_t len,
407 const struct isotp_msg_id *tx_addr,
408 const struct isotp_msg_id *rx_addr,
409 isotp_tx_callback_t complete_cb, void *cb_arg,
410 k_timeout_t timeout);
411#endif
412
415struct isotp_callback {
417 void *arg;
418};
419
420struct isotp_send_ctx {
421 int filter_id;
422 uint32_t error_nr;
423 const struct device *can_dev;
424 union {
425 struct net_buf *buf;
426 struct {
427 const uint8_t *data;
428 size_t len;
429 };
430 };
431 struct k_work work;
432 struct k_timer timer;
433 union {
434 struct isotp_callback fin_cb;
435 struct k_sem fin_sem;
436 };
437 struct isotp_fc_opts opts;
439 uint8_t tx_backlog;
440 struct k_sem tx_sem;
441 struct isotp_msg_id rx_addr;
442 struct isotp_msg_id tx_addr;
443 uint8_t wft;
444 uint8_t bs;
445 uint8_t sn : 4;
446 uint8_t is_net_buf : 1;
447 uint8_t is_ctx_slab : 1;
448 uint8_t has_callback: 1;
449};
450
451struct isotp_recv_ctx {
452 int filter_id;
453 const struct device *can_dev;
454 struct net_buf *buf;
455 struct net_buf *act_frag;
456 /* buffer currently processed in isotp_recv */
457 struct net_buf *recv_buf;
458 sys_snode_t alloc_node;
459 uint32_t length;
460 int error_nr;
461 struct k_work work;
462 struct k_timer timer;
463 struct k_fifo fifo;
464 struct isotp_msg_id rx_addr;
465 struct isotp_msg_id tx_addr;
466 struct isotp_fc_opts opts;
468 uint8_t bs;
469 uint8_t wft;
470 uint8_t sn_expected : 4;
471};
472
479#ifdef __cplusplus
480}
481#endif
482
483#endif /* ZEPHYR_INCLUDE_CANBUS_ISOTP_H_ */
Controller Area Network (CAN) 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:223
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
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:412
ISO-TP frame control options struct.
Definition isotp.h:210
uint8_t stmin
Minimum separation time.
Definition isotp.h:212
uint8_t bs
Block size.
Definition isotp.h:211
ISO-TP message id struct.
Definition isotp.h:169
uint8_t flags
Flags.
Definition isotp.h:194
uint32_t ext_id
Definition isotp.h:178
uint8_t ext_addr
ISO-TP extended address (if used)
Definition isotp.h:181
uint32_t std_id
Definition isotp.h:177
uint8_t dl
ISO-TP frame data length (TX_DL for TX address or RX_DL for RX address).
Definition isotp.h:192
Definition kernel.h:2468
Kernel timeout type.
Definition sys_clock.h:65
A structure used to submit work.
Definition kernel.h:3957
Network buffer representation.
Definition net_buf.h:1006