| 1 | #ifndef _DIAG_L1_H_ |
| 2 | #define _DIAG_L1_H_ |
| 3 | |
| 4 | /* |
| 5 | * freediag - Vehicle Diagnostic Utility |
| 6 | * |
| 7 | * |
| 8 | * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected]) |
| 9 | * 2009-2015 fenugrec |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | * |
| 25 | ************************************************************************* |
| 26 | * |
| 27 | * Diagnostic Software |
| 28 | * |
| 29 | * - Layer 1 interface definitions |
| 30 | */ |
| 31 | |
| 32 | #include <stddef.h> |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | struct diag_l0_device; |
| 36 | |
| 37 | |
| 38 | #if defined(__cplusplus) |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /* |
| 43 | * Layer 1/0 device flags |
| 44 | * |
| 45 | * Each potential Layer 0 logical interface has a number of flags that show |
| 46 | * what it supports |
| 47 | * |
| 48 | * Some of these (like SLOW start) will be needed by certain ECUs , and |
| 49 | * so the flags are also used in the ECU definitions |
| 50 | * |
| 51 | * Most L1 drivers will prefer fast start, hopefully support both |
| 52 | * fast and slow start |
| 53 | * |
| 54 | * Can be read by higher layers using ..._ioctl(GET_L1_FLAGS) |
| 55 | */ |
| 56 | |
| 57 | |
| 58 | #define DIAG_L1_SLOW 0x01 /* Supports SLOW (5 baud) Start */ |
| 59 | #define DIAG_L1_FAST 0x02 /* Supports FAST Start */ |
| 60 | #define DIAG_L1_PREFSLOW 0x04 /* Prefers SLOW (5 baud) Start */ |
| 61 | #define DIAG_L1_PREFFAST 0x08 /* Prefers FAST Start */ |
| 62 | #define DIAG_L1_HALFDUPLEX 0x10 /* Physical interface is half duplex, need to remove echos */ |
| 63 | |
| 64 | /* following flags are for semi-intelligent interfaces */ |
| 65 | |
| 66 | /* |
| 67 | * L1/L0 is intelligent and does L2 stuff, this means |
| 68 | * - l1_recv() splits multiple responses and returns a complete L2 frame; |
| 69 | * presence of headers / checksum is determined by other flags below |
| 70 | * - l1_send() ignores P4 inter-byte spacing when forwarding the data to L0 |
| 71 | */ |
| 72 | #define DIAG_L1_DOESL2FRAME 0x20 |
| 73 | /* |
| 74 | * DOESSLOWINIT |
| 75 | * L1/L0 interface does the slowinit stuff, so L2 doesn't need to do complex |
| 76 | * handshake. L1 will send the keybytes on the first recv(). (All L1's |
| 77 | * read the 0x55 and do the right thing, L2 never sees that) See DIAG_L1_DOESFULLINIT |
| 78 | */ |
| 79 | #define DIAG_L1_DOESSLOWINIT 0x40 |
| 80 | /* |
| 81 | * DOESL2CKSUM |
| 82 | * L1/L0 interface adds the L2 checksum/CRC on send |
| 83 | */ |
| 84 | #define DIAG_L1_DOESL2CKSUM 0x80 |
| 85 | /* |
| 86 | * STRIPSL2CKSUM |
| 87 | * L1 strips/checks L2 checksum before sending frame upward |
| 88 | */ |
| 89 | #define DIAG_L1_STRIPSL2CKSUM 0x100 |
| 90 | /* |
| 91 | * DOESP4WAIT |
| 92 | * |
| 93 | * interface is semi-intelligent and does the interbyte delay P4 for ISO |
| 94 | * (P4 : inter-byte delay for messages from tester (us) to ECU) |
| 95 | */ |
| 96 | #define DIAG_L1_DOESP4WAIT 0x200 |
| 97 | |
| 98 | //AUTOSPEED |
| 99 | //interface takes care of setting the baudrate; we check this before |
| 100 | //calling diag_l1_setspeed |
| 101 | #define DIAG_L1_AUTOSPEED 0x400 |
| 102 | |
| 103 | //NOTTY : specifically for carsim interface. Prevents l2_ioctl |
| 104 | //from calling diag_tty_* |
| 105 | #define DIAG_L1_NOTTY 0x800 |
| 106 | |
| 107 | //BLOCKDUPLEX |
| 108 | //This tells diag_l1_send() to do half-duplex removal on the whole |
| 109 | //block instead of byte per byte ( if P4=0 ; no interbyte spacing) |
| 110 | #define DIAG_L1_BLOCKDUPLEX 0x1000 |
| 111 | |
| 112 | //NOHDRS |
| 113 | //this indicates that L1 already stripped the headers from the frame (ELM default behavior) |
| 114 | //but the l0_elm init code enables headers so this is not useful at the moment. |
| 115 | //If NOHDRS, DIAG_L1_DOESL2FRAME must also be set! (otherwise, no hope of splitting messages...) |
| 116 | #define DIAG_L1_NOHDRS 0x2000 |
| 117 | |
| 118 | //DOESFULLINIT |
| 119 | //indicates that L0 does the full init, including keybyte stuff. (like ELMs) |
| 120 | //this implies that the initbus ioctl still has to be used. |
| 121 | #define DIAG_L1_DOESFULLINIT 0x4000 |
| 122 | |
| 123 | //DATAONLY |
| 124 | //indicates that L0 adds headers + checksums before sending to ECU (like ELMs). |
| 125 | #define DIAG_L1_DATAONLY 0x8000 |
| 126 | |
| 127 | //DOESKEEPALIVE |
| 128 | //L0 handles any periodic message required by L2/L3. |
| 129 | #define DIAG_L1_DOESKEEPALIVE 0x10000 |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * Layer 0 device types |
| 134 | * |
| 135 | * Types of L1 Interface (L1protocol) supported |
| 136 | * Each "device" has up to 16 interfaces, and many sub-interfaces XXX what ? |
| 137 | * |
| 138 | * This is a bitmask of what is supported; |
| 139 | * used for struct diag_l0 (l1proto_mask) |
| 140 | */ |
| 141 | #define DIAG_L1_ISO9141 0x01 /* K line */ |
| 142 | #define DIAG_L1_ISO14230 0x02 /* K line, different inits allowed */ |
| 143 | #define DIAG_L1_J1850_VPW 0x04 /* J1850 interface, 10400 baud, VPW */ |
| 144 | #define DIAG_L1_J1850_PWM 0x08 /* J1850 interface 41600 baud, PWM */ |
| 145 | #define DIAG_L1_CAN 0x10 /* CAN bus */ |
| 146 | #define DIAG_L1_RES1 0x20 /* Reserved */ |
| 147 | #define DIAG_L1_RES2 0x40 /* Reserved */ |
| 148 | #define DIAG_L1_RAW 0x80 /* Raw data interface */ |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * L2 -> L1 interface |
| 153 | * |
| 154 | * L1 public interface |
| 155 | */ |
| 156 | |
| 157 | /* Argument for DIAG_IOCTL_INITBUS */ |
| 158 | struct diag_l1_initbus_args { |
| 159 | uint8_t type; /* Init type */ |
| 160 | uint8_t addr; /* ECU (target) address, if iso9141 or 14230 init */ |
| 161 | uint8_t testerid; /* tester address, for 14230 init */ |
| 162 | uint8_t physaddr; //1:physical addressing, 0: func. iso14230 only. |
| 163 | uint8_t kb1, kb2; /* key bytes (return value from L0) */ |
| 164 | }; |
| 165 | //initbus types: |
| 166 | #define DIAG_L1_INITBUS_NONE 0 /* Not needed */ |
| 167 | #define DIAG_L1_INITBUS_FAST 1 /* Fast init (25ms low, 25ms high) */ |
| 168 | #define DIAG_L1_INITBUS_5BAUD 2 /* 5 baud init */ |
| 169 | #define DIAG_L1_INITBUS_2SLOW 3 /* 2 second low on bus, ISO9141-1989 style ? */ |
| 170 | |
| 171 | /********** Public L1 interface **********/ |
| 172 | /** Parses through the l0dev_list linked list |
| 173 | * and calls ->init for each of them. |
| 174 | * @return 0 on success (always succeeds) |
| 175 | * |
| 176 | * must not be used to allocate memory or open handles ! |
| 177 | */ |
| 178 | int diag_l1_init(void); |
| 179 | |
| 180 | /** Opposite of diag_l1_init; does nothing for now */ |
| 181 | int diag_l1_end(void); |
| 182 | |
| 183 | |
| 184 | /** Send IOCTL to L1/L0 |
| 185 | * @param command : IOCTL #, defined in diag.h |
| 186 | * @param data optional, input/output |
| 187 | * @return 0 if OK, diag error num (<0) on error |
| 188 | */ |
| 189 | int diag_l1_ioctl(struct diag_l0_device *, unsigned cmd, void *data); |
| 190 | |
| 191 | |
| 192 | /** calls l0 ->open with the specified L1 protocol; |
| 193 | * @return 0 if ok |
| 194 | */ |
| 195 | int diag_l1_open(struct diag_l0_device *, int L1protocol); |
| 196 | |
| 197 | /** Calls diag_l0_close as required; always succeeds. */ |
| 198 | void diag_l1_close(struct diag_l0_device *); |
| 199 | |
| 200 | /** Send data. |
| 201 | * @param p4 : inter-byte spacing (ms), if applicable. |
| 202 | * @return 0 if ok |
| 203 | */ |
| 204 | int diag_l1_send(struct diag_l0_device *, const void *data, size_t len, unsigned int p4); |
| 205 | |
| 206 | /** Receive data. |
| 207 | * |
| 208 | * @return # of bytes read, DIAG_ERR_TIMEOUT or \<0 if failed. DIAG_ERR_TIMEOUT is not a hard failure |
| 209 | * since a lot of L2 code uses this to detect end of responses |
| 210 | */ |
| 211 | int diag_l1_recv(struct diag_l0_device *, void *data, size_t len, unsigned int timeout); |
| 212 | |
| 213 | /** Get L0/L1 device flags (defined in diag_l1.h) |
| 214 | * @return bitmask of L0/L1 flags |
| 215 | */ |
| 216 | uint32_t diag_l1_getflags(struct diag_l0_device *); |
| 217 | |
| 218 | /** Get L1 type (supported protocols) |
| 219 | * @return bitmask of supported L1 protocols |
| 220 | */ |
| 221 | int diag_l1_gettype(struct diag_l0_device *); |
| 222 | |
| 223 | /**********/ |
| 224 | |
| 225 | extern int diag_l1_debug; //L1 debug flags (see diag.h) |
| 226 | |
| 227 | #if defined(__cplusplus) |
| 228 | } |
| 229 | #endif |
| 230 | #endif /* _DIAG_L1_H_ */ |
| 231 | |