summaryrefslogtreecommitdiff
path: root/src/libgsmd/libgsmd_passthrough.c
blob: d40f652a266d611cac4c5d3a6d6b0ec0af8125e7 (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
/* libgsmd passthrough
 *
 * (C) 2006-2007 by OpenMoko, Inc.
 * Written by Harald Welte <laforge@openmoko.org>
 * All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */ 

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

#include <sys/types.h>
#include <sys/socket.h>

#include <gsmd/usock.h>
#include <libgsmd/libgsmd.h>

#include "lgsm_internals.h"

#define PT_BUF_SIZE	1024
static char passthrough_buf[sizeof(struct gsmd_msg_hdr)+PT_BUF_SIZE];
static char passthrough_rbuf[sizeof(struct gsmd_msg_hdr)+PT_BUF_SIZE];

int lgsm_passthrough_send(struct lgsm_handle *lh, const char *tx)
{
	struct gsmd_msg_hdr *gmh = (struct gsmd_msg_hdr *)passthrough_buf;
	char *tx_buf = (char *)gmh + sizeof(*gmh);
	int len = strlen(tx);

	if (len > PT_BUF_SIZE)
		return -EINVAL;

	gmh->version = GSMD_PROTO_VERSION;
	gmh->msg_type = GSMD_MSG_PASSTHROUGH;
	gmh->msg_subtype = GSMD_PASSTHROUGH_REQ;
	gmh->len = len+1;
	strcpy(tx_buf, tx);

	if (lgsm_send(lh, gmh) < len+sizeof(*gmh))
		return -EIO;

	return gmh->id;
}

int lgsm_passthrough(struct lgsm_handle *lh, const char *tx,
		char *rx, unsigned int *rx_len)
{
	struct gsmd_msg_hdr *rgmh = (struct gsmd_msg_hdr *)passthrough_rbuf;
	char *rx_buf = (char *)rgmh + sizeof(*rgmh);
	int rc;

	rc = lgsm_passthrough_send(lh, tx);
	if (rc < 0)
		return rc;

	/* since we synchronously want to wait for a response, we need to
	 * _internally_ loop over incoming packets and call the callbacks for
	 * intermediate messages (if applicable) */
	rc = lgsm_blocking_wait_packet(lh, rc, rgmh, sizeof(passthrough_rbuf));
	if (rc <= 0)
		return rc;

	if (rc < sizeof(*rgmh))
		return -EINVAL;

	if (rc < sizeof(*rgmh) + rgmh->len)
		return -EINVAL;

	rx[--*rx_len] = 0;
	if (rgmh->len < *rx_len)
		*rx_len = rgmh->len;
	memcpy(rx, rx_buf, *rx_len);

	return *rx_len;
}
personal git repositories of Harald Welte. Your mileage may vary