1 /*
2  * Copyright (c) 2019 Alexander Wachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_CANBUS_ISOTP_ISOTP_INTERNAL_H_
8 #define ZEPHYR_SUBSYS_CANBUS_ISOTP_ISOTP_INTERNAL_H_
9 
10 #include <zephyr/canbus/isotp.h>
11 #include <zephyr/sys/slist.h>
12 
13 /* Protocol control information*/
14 #define ISOTP_PCI_SF 0x00 /* Single frame*/
15 #define ISOTP_PCI_FF 0x01 /* First frame */
16 #define ISOTP_PCI_CF 0x02 /* Consecutive frame */
17 #define ISOTP_PCI_FC 0x03 /* Flow control frame */
18 
19 #define ISOTP_PCI_TYPE_BYTE        0
20 #define ISOTP_PCI_TYPE_POS         4
21 #define ISOTP_PCI_TYPE_MASK        0xF0
22 #define ISOTP_PCI_TYPE_SF   (ISOTP_PCI_SF << ISOTP_PCI_TYPE_POS)
23 #define ISOTP_PCI_TYPE_FF   (ISOTP_PCI_FF << ISOTP_PCI_TYPE_POS)
24 #define ISOTP_PCI_TYPE_CF   (ISOTP_PCI_CF << ISOTP_PCI_TYPE_POS)
25 #define ISOTP_PCI_TYPE_FC   (ISOTP_PCI_FC << ISOTP_PCI_TYPE_POS)
26 
27 #define ISOTP_PCI_SF_DL_MASK       0x0F
28 
29 #define ISOTP_PCI_FF_DL_UPPER_BYTE 0
30 #define ISOTP_PCI_FF_DL_UPPER_MASK 0x0F
31 #define ISOTP_PCI_FF_DL_LOWER_BYTE 1
32 
33 #define ISOTP_PCI_FS_BYTE          0
34 #define ISOTP_PCI_FS_MASK          0x0F
35 #define ISOTP_PCI_BS_BYTE          1
36 #define ISOTP_PCI_ST_MIN_BYTE      2
37 
38 #define ISOTP_PCI_FS_CTS           0x0
39 #define ISOTP_PCI_FS_WAIT          0x1
40 #define ISOTP_PCI_FS_OVFLW         0x2
41 
42 #define ISOTP_PCI_SN_MASK          0x0F
43 
44 #define ISOTP_FF_DL_MIN            8
45 #define ISOTP_PADDED_FRAME_DL_MIN  8
46 #define ISOTP_PAD_BYTE             0xCC
47 
48 #define ISOTP_STMIN_MAX            0xFA
49 #define ISOTP_STMIN_MS_MAX         0x7F
50 #define ISOTP_STMIN_US_BEGIN       0xF1
51 #define ISOTP_STMIN_US_END         0xF9
52 
53 #define ISOTP_WFT_FIRST            0xFF
54 
55 #define ISOTP_4BIT_SF_MAX_CAN_DL   8
56 
57 #define ISOTP_BS_TIMEOUT_MS (CONFIG_ISOTP_BS_TIMEOUT)
58 #define ISOTP_A_TIMEOUT_MS  (CONFIG_ISOTP_A_TIMEOUT)
59 #define ISOTP_CR_TIMEOUT_MS (CONFIG_ISOTP_CR_TIMEOUT)
60 
61 /* Just before the sender would time out*/
62 #define ISOTP_ALLOC_TIMEOUT_MS (CONFIG_ISOTP_A_TIMEOUT - 100)
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 enum isotp_rx_state {
69 	ISOTP_RX_STATE_WAIT_FF_SF,
70 	ISOTP_RX_STATE_PROCESS_SF,
71 	ISOTP_RX_STATE_PROCESS_FF,
72 	ISOTP_RX_STATE_TRY_ALLOC,
73 	ISOTP_RX_STATE_SEND_FC,
74 	ISOTP_RX_STATE_WAIT_CF,
75 	ISOTP_RX_STATE_SEND_WAIT,
76 	ISOTP_RX_STATE_ERR,
77 	ISOTP_RX_STATE_RECYCLE,
78 	ISOTP_RX_STATE_UNBOUND
79 };
80 
81 enum isotp_tx_state {
82 	ISOTP_TX_STATE_RESET,
83 	ISOTP_TX_SEND_SF,
84 	ISOTP_TX_SEND_FF,
85 	ISOTP_TX_WAIT_FC,
86 	ISOTP_TX_SEND_CF,
87 	ISOTP_TX_WAIT_ST,
88 	ISOTP_TX_WAIT_BACKLOG,
89 	ISOTP_TX_WAIT_FIN,
90 	ISOTP_TX_ERR
91 };
92 
93 struct isotp_global_ctx {
94 	sys_slist_t alloc_list;
95 	sys_slist_t ff_sf_alloc_list;
96 };
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif /* ZEPHYR_SUBSYS_CANBUS_ISOTP_ISOTP_INTERNAL_H_ */
103