aboutsummaryrefslogtreecommitdiffstats
path: root/src/diag_io.c
blob: bcae7937dd129dc599c940710df8a499c9e53d27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * (C) 2013-2016 by Harald Welte <laforge@gnumonks.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "protocol/protocol.h"
#include "diag_io.h"
#include "diag_cmd.h"
#include "diagchar_hdlc.h"

struct msgb *msgb_alloc_diag(void)
{
	return msgb_alloc_headroom(DIAG_MAX_REQ_SIZE+16, 16, "DIAG Tx");
}

/* transmit a msgb containing a DIAG message over the given fd */
int diag_transmit_msgb(struct diag_instance *di, struct msgb *msg)
{
	int out_len, rc;
	uint8_t packet[DIAG_MAX_HDLC_BUF_SIZE];
	struct diag_send_desc_type send;
	struct diag_hdlc_dest_type enc = { NULL, NULL, 0 };

	if (di->flags & DIAG_INST_F_HEXDUMP)
		printf("Tx: %s\n", msgb_hexdump(msg));

	send.state = DIAG_STATE_START;
	send.pkt = msgb_data(msg);
	send.last = msgb_data(msg) + msgb_length(msg) - 1;
	send.terminate = 1;

	enc.dest = packet;
	enc.dest_last = packet + sizeof(packet) - 1;

	diag_hdlc_encode(&send, &enc);

	out_len = (enc.dest - (void *)packet);

	rc = write(di->fd, packet, out_len);
	if (rc != out_len) {
		fprintf(stderr, "Short write on packet.\n");
		return -1;
	}

	msgb_free(msg);

	return 0;
}

/* transmit a message from a buffer (nto msgb) as DIAG over the given fd */
int diag_transmit_buf(struct diag_instance *di, const uint8_t *data, size_t data_len)
{
	struct msgb *msg = msgb_alloc_diag();

	memcpy(msg->tail, data, data_len);
	msgb_put(msg, data_len);

	return diag_transmit_msgb(di, msg);
}

struct msgb *diag_read_msg(struct diag_instance *di)
{
	uint8_t buf[DIAG_MAX_HDLC_BUF_SIZE];
	struct diag_hdlc_decode_type hdlc_decode;
	struct msgb *msg;
	int rc;

	/* read raw data into buffer */
	rc = read(di->fd, buf, sizeof(buf));
	if (rc <= 0) {
		fprintf(stderr, "Short read!\n");
		exit(1);
	}

	if (!di->rx.msg) {
		di->rx.msg = msgb_alloc(DIAG_MAX_REQ_SIZE, "DIAG Rx");
		di->rx.msg->l2h = di->rx.msg->tail;
	}
	msg = di->rx.msg;

	memset(&hdlc_decode, 0, sizeof(hdlc_decode));
	hdlc_decode.dest_ptr = msg->tail;
	hdlc_decode.dest_size = msgb_tailroom(msg);
	hdlc_decode.src_ptr = buf;
	hdlc_decode.src_size = rc;
	hdlc_decode.src_idx = 0;
	hdlc_decode.dest_idx = 0;

	rc = diag_hdlc_decode(&hdlc_decode);

	if (msgb_length(msg) + hdlc_decode.dest_idx > DIAG_MAX_REQ_SIZE) {
		fprintf(stderr, "Dropping packet. pkt_size: %d, max: %d\n",
			msgb_length(msg) + hdlc_decode.dest_idx,
			DIAG_MAX_REQ_SIZE);
		return NULL;
	}

	msgb_put(msg, hdlc_decode.dest_idx);

	if (rc == HDLC_COMPLETE) {
		di->rx.msg = NULL;
		rc = crc_check(msgb_data(msg), msgb_length(msg));
		if (rc) {
			fprintf(stderr, "Bad CRC, dropping packet\n");
			//msgb_free(msg);
			//return NULL;
		}
		msgb_get(msg, HDLC_FOOTER_LEN);

		if (msgb_length(msg) < 1) {
			fprintf(stderr, "Message too short, len: %u\n", msgb_length(msg));
			msgb_free(msg);
			return NULL;
		}

		if (di->flags & DIAG_INST_F_HEXDUMP)
			printf("Rx: %s\n", msgb_hexdump(msg));

		return msg;
	}

	return NULL;
};

/* transmit a message, wait for response, return response */
struct msgb *diag_transceive_msg(struct diag_instance *di, struct msgb *tx)
{
	struct msgb *rx;
	int rc;

	/* transmit the tx message */
	diag_transmit_msgb(di, tx);

	/* blocking loop and process incoming messages until there is
	 * one for which we don't have a parser registered, let's assume
	 * that this is our response */
	while (1) {
		rx = diag_read_msg(di);
		if (rx) {
			rc = diag_process_msg(di, rx);
			printf("rc = %d\n", rc);
			if (rc == 0)
				return rx;
		}
	}
	return NULL;
}

struct msgb *diag_subsys_transceive_msg(struct diag_instance *di, struct msgb *tx,
					uint8_t subsys, uint16_t code)
{
	struct msgb *rx;
	struct diagpkt_subsys_hdr *dsh;

	diag_push_subsys_hdr(tx, subsys, code);
	rx = diag_transceive_msg(di, tx);
	if (!rx)
		return NULL;
	dsh = (struct diagpkt_subsys_hdr *) rx->l2h;
	if (msgb_l2len(rx) < sizeof(*dsh) ||
	    dsh->subsys_id != subsys ||
	    osmo_load16le(&dsh->subsys_cmd_code) != code) {
		msgb_free(rx);
		return NULL;
	}
	rx->l3h = rx->l2h + sizeof(*dsh);
	return rx;
}

/* transmit a message, wait for response, then ignore response */
void diag_transceive_msg_ign(struct diag_instance *di, struct msgb *tx)
{
	struct msgb *rx;

	rx = diag_transceive_msg(di, tx);
	msgb_free(rx);
}

/* transmit a message from a buffer, wait for response, return it */
struct msgb *diag_transceive_buf(struct diag_instance *di, const uint8_t *data, size_t data_len)
{
	struct msgb *msg = msgb_alloc_diag();

	memcpy(msg->tail, data, data_len);
	msgb_put(msg, data_len);

	return diag_transceive_msg(di, msg);
}

/* transmit a message from a buffer, wait for response, ignore it */
void diag_transceive_buf_ign(struct diag_instance *di, const uint8_t *data, size_t data_len)
{
	struct msgb *rx = diag_transceive_buf(di, data, data_len);
	msgb_free(rx);
}