Harald Welte | ccea8dd | 2016-12-24 10:27:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013-2016 by Harald Welte <laforge@gnumonks.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <errno.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
Harald Welte | 84ec50f | 2016-12-24 10:16:00 +0100 | [diff] [blame] | 26 | #include "protocol/protocol.h" |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 27 | #include "diag_io.h" |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 28 | #include "diag_cmd.h" |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 29 | #include "diagchar_hdlc.h" |
| 30 | |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 31 | struct msgb *msgb_alloc_diag(void) |
| 32 | { |
Harald Welte | 58185e6 | 2017-01-01 12:05:33 +0100 | [diff] [blame^] | 33 | return msgb_alloc_headroom(DIAG_MAX_REQ_SIZE+16, 16, "DIAG Tx"); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 36 | /* transmit a msgb containing a DIAG message over the given fd */ |
| 37 | int diag_transmit_msgb(struct diag_instance *di, struct msgb *msg) |
| 38 | { |
| 39 | int out_len, rc; |
| 40 | uint8_t packet[DIAG_MAX_HDLC_BUF_SIZE]; |
| 41 | struct diag_send_desc_type send; |
| 42 | struct diag_hdlc_dest_type enc = { NULL, NULL, 0 }; |
| 43 | |
Harald Welte | efb3eca | 2017-01-01 11:41:02 +0100 | [diff] [blame] | 44 | if (di->flags & DIAG_INST_F_HEXDUMP) |
| 45 | printf("Tx: %s\n", msgb_hexdump(msg)); |
| 46 | |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 47 | send.state = DIAG_STATE_START; |
| 48 | send.pkt = msgb_data(msg); |
| 49 | send.last = msgb_data(msg) + msgb_length(msg) - 1; |
| 50 | send.terminate = 1; |
| 51 | |
| 52 | enc.dest = packet; |
| 53 | enc.dest_last = packet + sizeof(packet) - 1; |
| 54 | |
| 55 | diag_hdlc_encode(&send, &enc); |
| 56 | |
| 57 | out_len = (enc.dest - (void *)packet); |
| 58 | |
| 59 | rc = write(di->fd, packet, out_len); |
| 60 | if (rc != out_len) { |
Harald Welte | 98f6fad | 2016-12-24 01:27:49 +0100 | [diff] [blame] | 61 | fprintf(stderr, "Short write on packet.\n"); |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | msgb_free(msg); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* transmit a message from a buffer (nto msgb) as DIAG over the given fd */ |
| 71 | int diag_transmit_buf(struct diag_instance *di, const uint8_t *data, size_t data_len) |
| 72 | { |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 73 | struct msgb *msg = msgb_alloc_diag(); |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 74 | |
| 75 | memcpy(msg->tail, data, data_len); |
| 76 | msgb_put(msg, data_len); |
| 77 | |
| 78 | return diag_transmit_msgb(di, msg); |
| 79 | } |
| 80 | |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 81 | struct msgb *diag_read_msg(struct diag_instance *di) |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 82 | { |
| 83 | uint8_t buf[DIAG_MAX_HDLC_BUF_SIZE]; |
| 84 | struct diag_hdlc_decode_type hdlc_decode; |
| 85 | struct msgb *msg; |
| 86 | int rc; |
| 87 | |
| 88 | /* read raw data into buffer */ |
| 89 | rc = read(di->fd, buf, sizeof(buf)); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 90 | if (rc <= 0) { |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 91 | fprintf(stderr, "Short read!\n"); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 92 | exit(1); |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (!di->rx.msg) { |
| 96 | di->rx.msg = msgb_alloc(DIAG_MAX_REQ_SIZE, "DIAG Rx"); |
| 97 | di->rx.msg->l2h = di->rx.msg->tail; |
| 98 | } |
| 99 | msg = di->rx.msg; |
| 100 | |
| 101 | hdlc_decode.dest_ptr = msg->tail; |
| 102 | hdlc_decode.dest_size = msgb_tailroom(msg); |
| 103 | hdlc_decode.src_ptr = buf; |
| 104 | hdlc_decode.src_size = rc; |
| 105 | hdlc_decode.src_idx = 0; |
| 106 | hdlc_decode.dest_idx = 0; |
| 107 | |
| 108 | rc = diag_hdlc_decode(&hdlc_decode); |
| 109 | |
| 110 | if (msgb_length(msg) + hdlc_decode.dest_idx > DIAG_MAX_REQ_SIZE) { |
| 111 | fprintf(stderr, "Dropping packet. pkt_size: %d, max: %d\n", |
| 112 | msgb_length(msg) + hdlc_decode.dest_idx, |
| 113 | DIAG_MAX_REQ_SIZE); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 114 | return NULL; |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | msgb_put(msg, hdlc_decode.dest_idx); |
| 118 | |
| 119 | if (rc == HDLC_COMPLETE) { |
| 120 | di->rx.msg = NULL; |
| 121 | rc = crc_check(msgb_data(msg), msgb_length(msg)); |
| 122 | if (rc) { |
| 123 | fprintf(stderr, "Bad CRC, dropping packet\n"); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 124 | //msgb_free(msg); |
| 125 | //return NULL; |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 126 | } |
| 127 | msgb_get(msg, HDLC_FOOTER_LEN); |
| 128 | |
| 129 | if (msgb_length(msg) < 1) { |
| 130 | fprintf(stderr, "Message too short, len: %u\n", msgb_length(msg)); |
| 131 | msgb_free(msg); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 132 | return NULL; |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 133 | } |
Harald Welte | efb3eca | 2017-01-01 11:41:02 +0100 | [diff] [blame] | 134 | |
| 135 | if (di->flags & DIAG_INST_F_HEXDUMP) |
| 136 | printf("Rx: %s\n", msgb_hexdump(msg)); |
| 137 | |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 138 | return msg; |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 141 | return NULL; |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 142 | }; |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 143 | |
| 144 | /* transmit a message, wait for response, return response */ |
| 145 | struct msgb *diag_transceive_msg(struct diag_instance *di, struct msgb *tx) |
| 146 | { |
| 147 | struct msgb *rx; |
| 148 | int rc; |
| 149 | |
| 150 | /* transmit the tx message */ |
| 151 | diag_transmit_msgb(di, tx); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 152 | |
| 153 | /* blocking loop and process incoming messages until there is |
| 154 | * one for which we don't have a parser registered, let's assume |
| 155 | * that this is our response */ |
| 156 | while (1) { |
| 157 | rx = diag_read_msg(di); |
Harald Welte | dc2cafc | 2017-01-01 11:15:35 +0100 | [diff] [blame] | 158 | if (rx) { |
| 159 | rc = diag_process_msg(di, rx); |
| 160 | printf("rc = %d\n", rc); |
| 161 | if (rc == 0) |
| 162 | return rx; |
| 163 | } |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /* transmit a message, wait for response, then ignore response */ |
| 169 | void diag_transceive_msg_ign(struct diag_instance *di, struct msgb *tx) |
| 170 | { |
| 171 | struct msgb *rx; |
| 172 | |
| 173 | rx = diag_transceive_msg(di, tx); |
| 174 | msgb_free(rx); |
| 175 | } |
| 176 | |
| 177 | /* transmit a message from a buffer, wait for response, return it */ |
| 178 | struct msgb *diag_transceive_buf(struct diag_instance *di, const uint8_t *data, size_t data_len) |
| 179 | { |
| 180 | struct msgb *msg = msgb_alloc_diag(); |
| 181 | |
| 182 | memcpy(msg->tail, data, data_len); |
| 183 | msgb_put(msg, data_len); |
| 184 | |
| 185 | return diag_transceive_msg(di, msg); |
| 186 | } |
| 187 | |
| 188 | /* transmit a message from a buffer, wait for response, ignore it */ |
| 189 | void diag_transceive_buf_ign(struct diag_instance *di, const uint8_t *data, size_t data_len) |
| 190 | { |
| 191 | struct msgb *rx = diag_transceive_buf(di, data, data_len); |
| 192 | msgb_free(rx); |
| 193 | } |