| 1 | #ifndef _DIAG_L3_H_ |
| 2 | #define _DIAG_L3_H_ |
| 3 | |
| 4 | /* |
| 5 | * freediag - Vehicle Diagnostic Utility |
| 6 | * |
| 7 | * |
| 8 | * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected]) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | * |
| 24 | ************************************************************************* |
| 25 | * |
| 26 | * L3 header. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #if defined(__cplusplus) |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | struct diag_l2_conn; |
| 38 | struct diag_msg; |
| 39 | |
| 40 | /** Layer 3 connection info |
| 41 | */ |
| 42 | struct diag_l3_conn { |
| 43 | struct diag_l2_conn *d_l3l2_conn; |
| 44 | int d_l3l2_flags; /* Flags from L2 */ |
| 45 | uint32_t d_l3l1_flags; /* Flags from L1 */ |
| 46 | |
| 47 | const struct diag_l3_proto *d_l3_proto; |
| 48 | void *l3_int; // internal (private) data for each connection instance |
| 49 | |
| 50 | /* Callback into next layer (which passed us the handle) */ |
| 51 | void (*callback)(void *handle, struct diag_msg *msg); |
| 52 | void *handle; |
| 53 | |
| 54 | /* Received messages */ |
| 55 | struct diag_msg *msg; |
| 56 | |
| 57 | /* time (in ms since an arbitrary reference) of last tx/rx , for managing periodic timers */ |
| 58 | unsigned long timer; |
| 59 | |
| 60 | /* Linked list held by main L3 code */ |
| 61 | struct diag_l3_conn *next; |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | /** L3 Protocol descriptor. |
| 66 | * |
| 67 | * Do not call member functions directly. |
| 68 | * All functions must be implemented, except |
| 69 | * _ioctl |
| 70 | * _timer |
| 71 | */ |
| 72 | |
| 73 | struct diag_l3_proto { |
| 74 | const char *proto_name; |
| 75 | |
| 76 | //start, stop : initiate L3 comms, filling the given diag_l3_conn. Must return 0 if ok, <0 on error |
| 77 | int (*diag_l3_proto_start)(struct diag_l3_conn *); |
| 78 | int (*diag_l3_proto_stop)(struct diag_l3_conn *); |
| 79 | |
| 80 | //proto_send: ret 0 if ok |
| 81 | int (*diag_l3_proto_send)(struct diag_l3_conn *, struct diag_msg *); |
| 82 | |
| 83 | //proto_recv: ret 0 if ok |
| 84 | int (*diag_l3_proto_recv)(struct diag_l3_conn *, unsigned int, |
| 85 | void (*rcv_call_back)(void *handle,struct diag_msg *), void *); |
| 86 | |
| 87 | //proto_ioctl (optional). ret 0 if ok; if not defined the ioctl is passed to L2. |
| 88 | int (*diag_l3_proto_ioctl)(struct diag_l3_conn *, int cmd, void *data); |
| 89 | |
| 90 | //proto_request : send request and return a new message with the reply, and error in *errval (0 if ok) |
| 91 | struct diag_msg *(*diag_l3_proto_request)(struct diag_l3_conn *, |
| 92 | struct diag_msg *txmsg, int *errval); |
| 93 | |
| 94 | /* Decode msg to printable text in *buf */ |
| 95 | void (*diag_l3_proto_decode)(struct diag_l3_conn *, |
| 96 | struct diag_msg *msg, |
| 97 | char *buf, |
| 98 | const size_t bufsize); |
| 99 | |
| 100 | /* Timer (optional) |
| 101 | * If defined, this is called from diag_l3_timer() |
| 102 | * by the periodic callback (in diag_os); the ms argument |
| 103 | * is the difference (in ms) between [now] and [diag_l3_conn->timer]. |
| 104 | * ret 0 if ok |
| 105 | */ |
| 106 | int (*diag_l3_proto_timer)(struct diag_l3_conn *, unsigned long ms); |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | /** Initialize L3 layer |
| 111 | * Must be called once before using any L3 function |
| 112 | */ |
| 113 | void diag_l3_init(void); |
| 114 | |
| 115 | /** De-initialize L3 layer |
| 116 | * opposite of diag_l3_init(); call before unloading / exiting |
| 117 | */ |
| 118 | void diag_l3_end(void); |
| 119 | |
| 120 | /** Start L3 connection |
| 121 | * |
| 122 | * must free() everything if it fails; |
| 123 | * make sure to diag_l3_stop afterwards to free() the diag_l3_conn ! |
| 124 | * This adds the new l3 connection to the diag_l3_list linked-list |
| 125 | */ |
| 126 | struct diag_l3_conn *diag_l3_start(const char *protocol, struct diag_l2_conn *d_l2_conn); |
| 127 | |
| 128 | /** Stop L3 connection |
| 129 | * |
| 130 | * must free() everything diag_l3_start alloc'd |
| 131 | * and remove from diag_l3_list |
| 132 | */ |
| 133 | int diag_l3_stop(struct diag_l3_conn *d_l3_conn); |
| 134 | |
| 135 | /** Send message |
| 136 | * |
| 137 | * @return 0 if ok |
| 138 | */ |
| 139 | int diag_l3_send(struct diag_l3_conn *d_l3_conn, struct diag_msg *msg); |
| 140 | |
| 141 | /** Receive message |
| 142 | * |
| 143 | * @return 0 if ok |
| 144 | */ |
| 145 | int diag_l3_recv(struct diag_l3_conn *d_l3_conn, unsigned int timeout, |
| 146 | void (* rcv_call_back)(void *handle,struct diag_msg *), void *handle); |
| 147 | |
| 148 | /** Format given message as text |
| 149 | * |
| 150 | */ |
| 151 | void diag_l3_decode(struct diag_l3_conn *d_l3_conn, struct diag_msg *msg, |
| 152 | char *buf, const size_t bufsize); |
| 153 | |
| 154 | /** Send a request and return a new msg with the response. |
| 155 | * |
| 156 | * Caller must free that msg |
| 157 | */ |
| 158 | struct diag_msg *diag_l3_request(struct diag_l3_conn *dl3c, struct diag_msg *txmsg, |
| 159 | int *errval); |
| 160 | |
| 161 | /** Send ioctl to the specified |
| 162 | * L3 |
| 163 | * |
| 164 | * ret 0 if ok |
| 165 | */ |
| 166 | int diag_l3_ioctl(struct diag_l3_conn *connection, unsigned int cmd, void *data); |
| 167 | |
| 168 | /** Periodic timer dispatcher |
| 169 | * |
| 170 | * (call only from diag_os_*.c) * |
| 171 | * This calls the diag_l3_proto_timer function of every |
| 172 | * diag_l3_conn in the diag_l3_list linked-list. |
| 173 | */ |
| 174 | void diag_l3_timer(void); |
| 175 | |
| 176 | |
| 177 | /* Base implementations: |
| 178 | * these are defined in diag_l3.c and perform no operation. |
| 179 | */ |
| 180 | int diag_l3_base_start(struct diag_l3_conn *); |
| 181 | int diag_l3_base_stop(struct diag_l3_conn *); |
| 182 | int diag_l3_base_send(struct diag_l3_conn *, struct diag_msg *); |
| 183 | int diag_l3_base_recv(struct diag_l3_conn *, unsigned int, |
| 184 | void (* rcv_call_back)(void *handle,struct diag_msg *), void *); |
| 185 | //XXX diag_l3_base_ioctl : there's no code for this one ? |
| 186 | //int diag_l3_base_ioctl(struct diag_l3_conn *, int cmd, void *data); |
| 187 | struct diag_msg *diag_l3_base_request(struct diag_l3_conn *dl3c, |
| 188 | struct diag_msg *txmsg, int *errval); |
| 189 | |
| 190 | |
| 191 | |
| 192 | // diag_l3_debug : contains debugging message flags (see diag.h) |
| 193 | extern int diag_l3_debug; |
| 194 | extern struct diag_l3_conn *global_l3_conn; |
| 195 | |
| 196 | /* List of supported L3 protocols; last element is NULL */ |
| 197 | extern const struct diag_l3_proto *diag_l3_protocols[]; |
| 198 | |
| 199 | #if defined(__cplusplus) |
| 200 | } |
| 201 | #endif |
| 202 | #endif |
| 203 | |