blob: 35896786ccfb764630a7a755d41cfcd52aa0c8c5 [file] [log] [blame]
Harald Welteccea8dd2016-12-24 10:27:55 +01001/*
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 Weltebd448582016-12-23 23:41:14 +010019
20#include <stdint.h>
21#include <stdio.h>
22#include <errno.h>
23#include <string.h>
24#include <unistd.h>
25
Harald Welte84ec50f2016-12-24 10:16:00 +010026#include "protocol/protocol.h"
Harald Weltebd448582016-12-23 23:41:14 +010027#include "diag_io.h"
Harald Weltedc2cafc2017-01-01 11:15:35 +010028#include "diag_cmd.h"
Harald Weltebd448582016-12-23 23:41:14 +010029#include "diagchar_hdlc.h"
30
Harald Weltedc2cafc2017-01-01 11:15:35 +010031struct msgb *msgb_alloc_diag(void)
32{
Harald Welte58185e62017-01-01 12:05:33 +010033 return msgb_alloc_headroom(DIAG_MAX_REQ_SIZE+16, 16, "DIAG Tx");
Harald Weltedc2cafc2017-01-01 11:15:35 +010034}
35
Harald Weltebd448582016-12-23 23:41:14 +010036/* transmit a msgb containing a DIAG message over the given fd */
37int 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 Welteefb3eca2017-01-01 11:41:02 +010044 if (di->flags & DIAG_INST_F_HEXDUMP)
45 printf("Tx: %s\n", msgb_hexdump(msg));
46
Harald Weltebd448582016-12-23 23:41:14 +010047 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 Welte98f6fad2016-12-24 01:27:49 +010061 fprintf(stderr, "Short write on packet.\n");
Harald Weltebd448582016-12-23 23:41:14 +010062 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 */
71int diag_transmit_buf(struct diag_instance *di, const uint8_t *data, size_t data_len)
72{
Harald Weltedc2cafc2017-01-01 11:15:35 +010073 struct msgb *msg = msgb_alloc_diag();
Harald Weltebd448582016-12-23 23:41:14 +010074
75 memcpy(msg->tail, data, data_len);
76 msgb_put(msg, data_len);
77
78 return diag_transmit_msgb(di, msg);
79}
80
Harald Weltedc2cafc2017-01-01 11:15:35 +010081struct msgb *diag_read_msg(struct diag_instance *di)
Harald Weltebd448582016-12-23 23:41:14 +010082{
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 Weltedc2cafc2017-01-01 11:15:35 +010090 if (rc <= 0) {
Harald Weltebd448582016-12-23 23:41:14 +010091 fprintf(stderr, "Short read!\n");
Harald Weltedc2cafc2017-01-01 11:15:35 +010092 exit(1);
Harald Weltebd448582016-12-23 23:41:14 +010093 }
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 Weltedc2cafc2017-01-01 11:15:35 +0100114 return NULL;
Harald Weltebd448582016-12-23 23:41:14 +0100115 }
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 Weltedc2cafc2017-01-01 11:15:35 +0100124 //msgb_free(msg);
125 //return NULL;
Harald Weltebd448582016-12-23 23:41:14 +0100126 }
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 Weltedc2cafc2017-01-01 11:15:35 +0100132 return NULL;
Harald Weltebd448582016-12-23 23:41:14 +0100133 }
Harald Welteefb3eca2017-01-01 11:41:02 +0100134
135 if (di->flags & DIAG_INST_F_HEXDUMP)
136 printf("Rx: %s\n", msgb_hexdump(msg));
137
Harald Weltedc2cafc2017-01-01 11:15:35 +0100138 return msg;
Harald Weltebd448582016-12-23 23:41:14 +0100139 }
140
Harald Weltedc2cafc2017-01-01 11:15:35 +0100141 return NULL;
Harald Weltebd448582016-12-23 23:41:14 +0100142};
Harald Weltedc2cafc2017-01-01 11:15:35 +0100143
144/* transmit a message, wait for response, return response */
145struct 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 Weltedc2cafc2017-01-01 11:15:35 +0100152
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 Weltedc2cafc2017-01-01 11:15:35 +0100158 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 */
169void 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 */
178struct 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 */
189void 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}