summaryrefslogtreecommitdiff
path: root/gatchat/test-qcdm.c
blob: c8a317484afa7afe6ddf585b33e1762326525c44 (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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#include <glib.h>

#include "gattty.h"
#include "gatutil.h"
#include "gathdlc.h"

static gboolean option_debug = FALSE;
static gchar *option_device = NULL;

static GMainLoop *event_loop;

struct version_info {
	char comp_date[11];
	char comp_time[8];
	char rel_date[11];
	char rel_time[8];
	char model[8];
	guint8 scm;
	guint8 mob_cai_rev;
	guint8 mob_model;
	guint16 mob_firmware_rev;
	guint8 slot_cycle_index;
	guint8 msm_ver;
	guint8 unknown;
} __attribute__ ((packed));

static void parse_qcdm(const unsigned char *buf, gsize len)
{
	struct version_info *verinfo;
	char str[12];
	guint8 cmd = buf[0];

	switch (cmd) {
	case 0x00:
		g_print("==> Version information\n");
		verinfo = (struct version_info *) (buf + 1);
		snprintf(str, 12, "%s", verinfo->comp_date);
		g_print("Compiled Date: %s\n", str);
		snprintf(str, 9, "%s", verinfo->comp_time);
		g_print("Compiled Time: %s\n", str);
		snprintf(str, 12, "%s", verinfo->rel_date);
		g_print("Release Date: %s\n", str);
		snprintf(str, 9, "%s", verinfo->rel_time);
		g_print("Release Time: %s\n", str);
		snprintf(str, 9, "%s", verinfo->model);
		g_print("Model: %s\n", str);
		g_print("MSM version: %d\n", verinfo->msm_ver);
		break;
	case 0x13:
		g_print("==> Invalid command response\n");
		break;
	case 0x4b:
		g_print("==> Subsystem response\n");
		break;
	case 0x51:
		g_print("==> Features response\n");
		break;
	default:
		g_print("==> Unknown command 0x%02x\n", cmd);
		break;
	}
}

static void hdlc_debug(const char *str, void *data)
{
	g_print("%s: %s\n", (const char *) data, str);
}

static void hdlc_receive(const unsigned char *buf, gsize len, void *data)
{
	if (option_debug == TRUE)
		g_at_util_debug_dump(TRUE, buf, len, hdlc_debug, "QCDM");

	parse_qcdm(buf, len);
}

static void send_command(GAtHDLC *hdlc, guint8 cmd)
{
	unsigned char cmdbuf[1];

	cmdbuf[0] = cmd;

	if (option_debug == TRUE)
		g_at_util_debug_dump(FALSE, cmdbuf, sizeof(cmdbuf),
							hdlc_debug, "QCDM");

	g_at_hdlc_send(hdlc, cmdbuf, sizeof(cmdbuf));
}

static void send_subsys_command(GAtHDLC *hdlc, guint8 id, guint16 cmd)
{
	unsigned char cmdbuf[4];

	cmdbuf[0] = 0x4b;
	cmdbuf[1] = id;
	cmdbuf[2] = cmd & 0xff;
	cmdbuf[3] = cmd >> 8;

	if (option_debug == TRUE)
		g_at_util_debug_dump(FALSE, cmdbuf, sizeof(cmdbuf),
							hdlc_debug, "QCDM");

	g_at_hdlc_send(hdlc, cmdbuf, sizeof(cmdbuf));
}

static GOptionEntry options[] = {
	{ "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
						"Enable debugging" },
	{ "device", 'n', 0, G_OPTION_ARG_STRING, &option_device,
						"Specify device" },
	{ NULL },
};

int main(int argc, char **argv)
{
	GOptionContext *context;
	GError *err = NULL;
	GIOChannel *channel;
	GAtHDLC *hdlc;

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, options, NULL);

	if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
		if (err != NULL) {
			g_printerr("%s\n", err->message);
			g_error_free(err);
			return 1;
		}

		g_printerr("An unknown error occurred\n");
		return 1;
	}

	g_option_context_free(context);

	if (!option_device)
		option_device = g_strdup("/dev/ttyUSB1");

	g_print("Device: %s\n", option_device);

	channel = g_at_tty_open(option_device, NULL);
	if (!channel) {
		g_printerr("Failed to open QCDM device\n");
		return 1;
	}

	event_loop = g_main_loop_new(NULL, FALSE);

	hdlc = g_at_hdlc_new(channel);

	g_io_channel_unref(channel);

	if (!hdlc)
		return 1;

	if (option_debug == TRUE)
		g_at_hdlc_set_debug(hdlc, hdlc_debug, "HDLC");

	g_at_hdlc_set_receive(hdlc, hdlc_receive, NULL);

	send_command(hdlc, 0x00);	/* Version info */
	send_command(hdlc, 0x51);	/* Features query */

	send_subsys_command(hdlc, 250, 7);	/* Novatel modem status */

	g_main_loop_run(event_loop);

	g_at_hdlc_unref(hdlc);

	g_main_loop_unref(event_loop);

	g_free(option_device);

	return 0;
}