| 1 | /* |
| 2 | * freediag - Vehicle Diagnostic Utility |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | ************************************************************************* |
| 22 | * |
| 23 | * Diag |
| 24 | * |
| 25 | * L2 driver for "raw" interface (just sends and receives data without |
| 26 | * modifying it) |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include "diag.h" |
| 34 | #include "diag_l1.h" |
| 35 | #include "diag_l2.h" |
| 36 | #include "diag_err.h" |
| 37 | #include "diag_os.h" |
| 38 | #include "diag_tty.h" |
| 39 | |
| 40 | #include "diag_l2_raw.h" /* prototypes for this file */ |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | int dl2p_raw_startcomms( struct diag_l2_conn *d_l2_conn, |
| 46 | UNUSED(flag_type flags), |
| 47 | unsigned int bitrate, |
| 48 | target_type target, |
| 49 | source_type source) { |
| 50 | int rv; |
| 51 | struct diag_serial_settings set; |
| 52 | |
| 53 | set.speed = bitrate; |
| 54 | set.databits = diag_databits_8; |
| 55 | set.stopbits = diag_stopbits_1; |
| 56 | set.parflag = diag_par_n; |
| 57 | |
| 58 | /* Set the speed as shown */ |
| 59 | rv=diag_l2_ioctl(d_l2_conn, DIAG_IOCTL_SETSPEED, &set); |
| 60 | |
| 61 | if (rv) { |
| 62 | return diag_iseterr(DIAG_ERR_GENERAL); |
| 63 | } |
| 64 | |
| 65 | //set tgt and src address in d_l2_conn |
| 66 | d_l2_conn->diag_l2_destaddr=target; |
| 67 | d_l2_conn->diag_l2_srcaddr=source; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | */ |
| 74 | |
| 75 | int dl2p_raw_stopcomms(UNUSED(struct diag_l2_conn *pX)) { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Just send the data, with no processing etc |
| 81 | * ret 0 if ok |
| 82 | */ |
| 83 | int dl2p_raw_send(struct diag_l2_conn *d_l2_conn, struct diag_msg *msg) { |
| 84 | int rv; |
| 85 | |
| 86 | DIAG_DBGM(diag_l2_debug, DIAG_DEBUG_WRITE, DIAG_DBGLEVEL_V, |
| 87 | FLFMT "diag_l2_send %p, msg %p len %u called\n", |
| 88 | FL, (void *)d_l2_conn, (void *)msg, msg->len); |
| 89 | |
| 90 | rv = diag_l1_send (d_l2_conn->diag_link->l2_dl0d, |
| 91 | msg->data, msg->len, d_l2_conn->diag_l2_p4min); |
| 92 | |
| 93 | return rv? diag_ifwderr(rv):0; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | */ |
| 98 | int dl2p_raw_recv(struct diag_l2_conn *d_l2_conn, unsigned int timeout, |
| 99 | void (*callback)(void *handle, struct diag_msg *msg), void *handle) { |
| 100 | uint8_t rxbuf[MAXRBUF]; |
| 101 | struct diag_msg msg = {0}; //local message structure that will disappear when we return |
| 102 | int rv; |
| 103 | |
| 104 | /* |
| 105 | * Read data from fd |
| 106 | */ |
| 107 | rv = diag_l1_recv (d_l2_conn->diag_link->l2_dl0d, |
| 108 | rxbuf, sizeof(rxbuf), timeout); |
| 109 | |
| 110 | if (rv <= 0) { /* Failure, or 0 bytes (which cant happen) */ |
| 111 | return rv; |
| 112 | } |
| 113 | |
| 114 | msg.len = (uint8_t) rv; |
| 115 | msg.data = rxbuf; |
| 116 | /* This is raw, unframed data; we don't set .fmt */ |
| 117 | msg.next = NULL; |
| 118 | msg.idata=NULL; |
| 119 | msg.rxtime = diag_os_getms(); |
| 120 | |
| 121 | DIAG_DBGM(diag_l2_debug, DIAG_DEBUG_READ, DIAG_DBGLEVEL_V, |
| 122 | FLFMT "l2_proto_raw_recv: handle=%p\n", FL, handle); |
| 123 | |
| 124 | /* |
| 125 | * Call user callback routine |
| 126 | */ |
| 127 | if (callback) { |
| 128 | callback(handle, &msg); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | */ |
| 136 | struct diag_msg *dl2p_raw_request(struct diag_l2_conn *d_l2_conn, struct diag_msg *msg, |
| 137 | int *errval) { |
| 138 | int rv; |
| 139 | struct diag_msg *rmsg = NULL; |
| 140 | uint8_t rxbuf[MAXRBUF]; |
| 141 | |
| 142 | rv = diag_l2_send(d_l2_conn, msg); |
| 143 | if (rv < 0) { |
| 144 | *errval = rv; |
| 145 | return diag_pseterr(DIAG_ERR_GENERAL); |
| 146 | } |
| 147 | |
| 148 | /* And wait for response */ |
| 149 | rv = diag_l1_recv (d_l2_conn->diag_link->l2_dl0d, |
| 150 | rxbuf, sizeof(rxbuf), 1000); |
| 151 | |
| 152 | if (rv <= 0) { |
| 153 | *errval = rv; |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Ok, alloc a message |
| 159 | */ |
| 160 | rmsg = diag_allocmsg((size_t)rv); |
| 161 | if (rmsg == NULL) { |
| 162 | return diag_pseterr(DIAG_ERR_NOMEM); |
| 163 | } |
| 164 | memcpy(&rmsg->data, rxbuf, (size_t)rv); /* Data */ |
| 165 | rmsg->fmt = 0; |
| 166 | rmsg->rxtime = diag_os_getms(); |
| 167 | |
| 168 | return rmsg; |
| 169 | } |
| 170 | |
| 171 | const struct diag_l2_proto diag_l2_proto_raw = { |
| 172 | DIAG_L2_PROT_RAW, |
| 173 | "RAW", |
| 174 | 0, |
| 175 | dl2p_raw_startcomms, |
| 176 | dl2p_raw_stopcomms, |
| 177 | dl2p_raw_send, |
| 178 | dl2p_raw_recv, |
| 179 | dl2p_raw_request, |
| 180 | NULL |
| 181 | }; |
| 182 | |