1 /*
2  * Copyright (c) 2019 Alexander Wachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_NET_CAN_ISOTP_INTERNAL_H_
8 #define ZEPHYR_SUBSYS_NET_CAN_ISOTP_INTERNAL_H_
9 
10 
11 #include <canbus/isotp.h>
12 #include <sys/slist.h>
13 
14 /*
15  * Abbreviations
16  * BS      Block Size
17  * CAN_DL  CAN LL data size
18  * CF      Consecutive Frame
19  * CTS     Continue to send
20  * DLC     Data length code
21  * FC      Flow Control
22  * FF      First Frame
23  * SF      Single Frame
24  * FS      Flow Status
25  * AE      Adders Extension
26  * SN      Sequence Number
27  * ST      Separation time
28  * PCI     Process Control Information
29  */
30 
31 /* This is for future use when we have CAN-FD */
32 #ifdef ISOTP_USE_CAN_FD
33 /* #define ISOTP_CAN_DL CONFIG_ISOTP_TX_DL* */
34 #define ISOTP_CAN_DL 8
35 #else
36 #define ISOTP_CAN_DL 8
37 #endif/*ISOTP_USE_CAN_FD*/
38 
39 /* Protocol control information*/
40 #define ISOTP_PCI_SF 0x00 /* Single frame*/
41 #define ISOTP_PCI_FF 0x01 /* First frame */
42 #define ISOTP_PCI_CF 0x02 /* Consecutive frame */
43 #define ISOTP_PCI_FC 0x03 /* Flow control frame */
44 
45 #define ISOTP_PCI_TYPE_BYTE        0
46 #define ISOTP_PCI_TYPE_POS         4
47 #define ISOTP_PCI_TYPE_MASK        0xF0
48 #define ISOTP_PCI_TYPE_SF   (ISOTP_PCI_SF << ISOTP_PCI_TYPE_POS)
49 #define ISOTP_PCI_TYPE_FF   (ISOTP_PCI_FF << ISOTP_PCI_TYPE_POS)
50 #define ISOTP_PCI_TYPE_CF   (ISOTP_PCI_CF << ISOTP_PCI_TYPE_POS)
51 #define ISOTP_PCI_TYPE_FC   (ISOTP_PCI_FC << ISOTP_PCI_TYPE_POS)
52 
53 #define ISOTP_PCI_SF_DL_MASK       0x0F
54 
55 #define ISOTP_PCI_FF_DL_UPPER_BYTE 0
56 #define ISOTP_PCI_FF_DL_UPPER_MASK 0x0F
57 #define ISOTP_PCI_FF_DL_LOWER_BYTE 1
58 
59 #define ISOTP_PCI_FS_BYTE          0
60 #define ISOTP_PCI_FS_MASK          0x0F
61 #define ISOTP_PCI_BS_BYTE          1
62 #define ISOTP_PCI_ST_MIN_BYTE      2
63 
64 #define ISOTP_PCI_FS_CTS           0x0
65 #define ISOTP_PCI_FS_WAIT          0x1
66 #define ISOTP_PCI_FS_OVFLW         0x2
67 
68 #define ISOTP_PCI_SN_MASK          0x0F
69 
70 #define ISOTP_FF_DL_MIN            (ISOTP_CAN_DL)
71 
72 #define ISOTP_STMIN_MAX            0xFA
73 #define ISOTP_STMIN_MS_MAX         0x7F
74 #define ISOTP_STMIN_US_BEGIN       0xF1
75 #define ISOTP_STMIN_US_END         0xF9
76 
77 #define ISOTP_WFT_FIRST            0xFF
78 
79 #define ISOTP_BS (CONFIG_ISOTP_BS_TIMEOUT)
80 #define ISOTP_A  (CONFIG_ISOTP_A_TIMEOUT)
81 #define ISOTP_CR (CONFIG_ISOTP_CR_TIMEOUT)
82 
83 /* Just before the sender would time out*/
84 #define ISOTP_ALLOC_TIMEOUT (CONFIG_ISOTP_A_TIMEOUT - 100)
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
90 enum isotp_rx_state {
91 	ISOTP_RX_STATE_WAIT_FF_SF,
92 	ISOTP_RX_STATE_PROCESS_SF,
93 	ISOTP_RX_STATE_PROCESS_FF,
94 	ISOTP_RX_STATE_TRY_ALLOC,
95 	ISOTP_RX_STATE_SEND_FC,
96 	ISOTP_RX_STATE_WAIT_CF,
97 	ISOTP_RX_STATE_SEND_WAIT,
98 	ISOTP_RX_STATE_ERR,
99 	ISOTP_RX_STATE_RECYCLE,
100 	ISOTP_RX_STATE_UNBOUND
101 };
102 
103 enum isotp_tx_state {
104 	ISOTP_TX_STATE_RESET,
105 	ISOTP_TX_SEND_SF,
106 	ISOTP_TX_SEND_FF,
107 	ISOTP_TX_WAIT_FC,
108 	ISOTP_TX_SEND_CF,
109 	ISOTP_TX_WAIT_ST,
110 	ISOTP_TX_WAIT_BACKLOG,
111 	ISOTP_TX_WAIT_FIN,
112 	ISOTP_TX_ERR
113 };
114 
115 struct isotp_global_ctx {
116 	sys_slist_t alloc_list;
117 	sys_slist_t ff_sf_alloc_list;
118 };
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* ZEPHYR_SUBSYS_NET_CAN_ISOTP_INTERNAL_H_ */
125