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" |
| 28 | #include "diagchar_hdlc.h" |
| 29 | |
| 30 | /* transmit a msgb containing a DIAG message over the given fd */ |
| 31 | int diag_transmit_msgb(struct diag_instance *di, struct msgb *msg) |
| 32 | { |
| 33 | int out_len, rc; |
| 34 | uint8_t packet[DIAG_MAX_HDLC_BUF_SIZE]; |
| 35 | struct diag_send_desc_type send; |
| 36 | struct diag_hdlc_dest_type enc = { NULL, NULL, 0 }; |
| 37 | |
| 38 | send.state = DIAG_STATE_START; |
| 39 | send.pkt = msgb_data(msg); |
| 40 | send.last = msgb_data(msg) + msgb_length(msg) - 1; |
| 41 | send.terminate = 1; |
| 42 | |
| 43 | enc.dest = packet; |
| 44 | enc.dest_last = packet + sizeof(packet) - 1; |
| 45 | |
| 46 | diag_hdlc_encode(&send, &enc); |
| 47 | |
| 48 | out_len = (enc.dest - (void *)packet); |
| 49 | |
| 50 | rc = write(di->fd, packet, out_len); |
| 51 | if (rc != out_len) { |
Harald Welte | 98f6fad | 2016-12-24 01:27:49 +0100 | [diff] [blame] | 52 | fprintf(stderr, "Short write on packet.\n"); |
Harald Welte | bd44858 | 2016-12-23 23:41:14 +0100 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | msgb_free(msg); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /* transmit a message from a buffer (nto msgb) as DIAG over the given fd */ |
| 62 | int diag_transmit_buf(struct diag_instance *di, const uint8_t *data, size_t data_len) |
| 63 | { |
| 64 | struct msgb *msg = msgb_alloc(DIAG_MAX_REQ_SIZE, "DIAG Tx"); |
| 65 | |
| 66 | memcpy(msg->tail, data, data_len); |
| 67 | msgb_put(msg, data_len); |
| 68 | |
| 69 | return diag_transmit_msgb(di, msg); |
| 70 | } |
| 71 | |
| 72 | int diag_read(struct diag_instance *di) |
| 73 | { |
| 74 | uint8_t buf[DIAG_MAX_HDLC_BUF_SIZE]; |
| 75 | struct diag_hdlc_decode_type hdlc_decode; |
| 76 | struct msgb *msg; |
| 77 | int rc; |
| 78 | |
| 79 | /* read raw data into buffer */ |
| 80 | rc = read(di->fd, buf, sizeof(buf)); |
| 81 | if (rc <= 0 ) { |
| 82 | fprintf(stderr, "Short read!\n"); |
| 83 | return -EIO; |
| 84 | } |
| 85 | |
| 86 | if (!di->rx.msg) { |
| 87 | di->rx.msg = msgb_alloc(DIAG_MAX_REQ_SIZE, "DIAG Rx"); |
| 88 | di->rx.msg->l2h = di->rx.msg->tail; |
| 89 | } |
| 90 | msg = di->rx.msg; |
| 91 | |
| 92 | hdlc_decode.dest_ptr = msg->tail; |
| 93 | hdlc_decode.dest_size = msgb_tailroom(msg); |
| 94 | hdlc_decode.src_ptr = buf; |
| 95 | hdlc_decode.src_size = rc; |
| 96 | hdlc_decode.src_idx = 0; |
| 97 | hdlc_decode.dest_idx = 0; |
| 98 | |
| 99 | rc = diag_hdlc_decode(&hdlc_decode); |
| 100 | |
| 101 | if (msgb_length(msg) + hdlc_decode.dest_idx > DIAG_MAX_REQ_SIZE) { |
| 102 | fprintf(stderr, "Dropping packet. pkt_size: %d, max: %d\n", |
| 103 | msgb_length(msg) + hdlc_decode.dest_idx, |
| 104 | DIAG_MAX_REQ_SIZE); |
| 105 | return -EIO; |
| 106 | } |
| 107 | |
| 108 | msgb_put(msg, hdlc_decode.dest_idx); |
| 109 | |
| 110 | if (rc == HDLC_COMPLETE) { |
| 111 | di->rx.msg = NULL; |
| 112 | rc = crc_check(msgb_data(msg), msgb_length(msg)); |
| 113 | if (rc) { |
| 114 | fprintf(stderr, "Bad CRC, dropping packet\n"); |
| 115 | msgb_free(msg); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | msgb_get(msg, HDLC_FOOTER_LEN); |
| 119 | |
| 120 | if (msgb_length(msg) < 1) { |
| 121 | fprintf(stderr, "Message too short, len: %u\n", msgb_length(msg)); |
| 122 | msgb_free(msg); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | |
| 126 | if (di->rx.rcvmsg) |
| 127 | (di->rx.rcvmsg)(di, msg); |
| 128 | else |
| 129 | msgb_free(msg); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | }; |