Upgrade ofono to 1.2
[profile/ivi/ofono.git] / gatchat / test-qcdm.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27
28 #include <glib.h>
29
30 #include "gattty.h"
31 #include "gathdlc.h"
32
33 static gboolean option_debug = FALSE;
34 static gchar *option_device = NULL;
35
36 static GMainLoop *event_loop;
37
38 struct version_info {
39         char comp_date[11];
40         char comp_time[8];
41         char rel_date[11];
42         char rel_time[8];
43         char model[8];
44         guint8 scm;
45         guint8 mob_cai_rev;
46         guint8 mob_model;
47         guint16 mob_firmware_rev;
48         guint8 slot_cycle_index;
49         guint8 msm_ver;
50         guint8 unknown;
51 } __attribute__ ((packed));
52
53 static void parse_qcdm(const unsigned char *buf, gsize len)
54 {
55         struct version_info *verinfo;
56         char str[12];
57         guint8 cmd = buf[0];
58
59         switch (cmd) {
60         case 0x00:
61                 g_print("==> Version information\n");
62                 verinfo = (struct version_info *) (buf + 1);
63                 snprintf(str, 12, "%s", verinfo->comp_date);
64                 g_print("Compiled Date: %s\n", str);
65                 snprintf(str, 9, "%s", verinfo->comp_time);
66                 g_print("Compiled Time: %s\n", str);
67                 snprintf(str, 12, "%s", verinfo->rel_date);
68                 g_print("Release Date: %s\n", str);
69                 snprintf(str, 9, "%s", verinfo->rel_time);
70                 g_print("Release Time: %s\n", str);
71                 snprintf(str, 9, "%s", verinfo->model);
72                 g_print("Model: %s\n", str);
73                 g_print("MSM version: %d\n", verinfo->msm_ver);
74                 break;
75         case 0x13:
76                 g_print("==> Invalid command response\n");
77                 break;
78         case 0x4b:
79                 g_print("==> Subsystem response\n");
80                 break;
81         case 0x51:
82                 g_print("==> Features response\n");
83                 break;
84         default:
85                 g_print("==> Unknown command 0x%02x\n", cmd);
86                 break;
87         }
88 }
89
90 static void hdlc_debug(const char *str, void *data)
91 {
92         g_print("%s: %s\n", (const char *) data, str);
93 }
94
95 static void hdlc_receive(const unsigned char *buf, gsize len, void *data)
96 {
97         parse_qcdm(buf, len);
98 }
99
100 static void send_command(GAtHDLC *hdlc, guint8 cmd)
101 {
102         unsigned char cmdbuf[1];
103
104         cmdbuf[0] = cmd;
105
106         g_at_hdlc_send(hdlc, cmdbuf, sizeof(cmdbuf));
107 }
108
109 static void send_subsys_command(GAtHDLC *hdlc, guint8 id, guint16 cmd)
110 {
111         unsigned char cmdbuf[4];
112
113         cmdbuf[0] = 0x4b;
114         cmdbuf[1] = id;
115         cmdbuf[2] = cmd & 0xff;
116         cmdbuf[3] = cmd >> 8;
117
118         g_at_hdlc_send(hdlc, cmdbuf, sizeof(cmdbuf));
119 }
120
121 static GOptionEntry options[] = {
122         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
123                                                 "Enable debugging" },
124         { "device", 'n', 0, G_OPTION_ARG_STRING, &option_device,
125                                                 "Specify device" },
126         { NULL },
127 };
128
129 int main(int argc, char **argv)
130 {
131         GOptionContext *context;
132         GError *err = NULL;
133         GIOChannel *channel;
134         GAtHDLC *hdlc;
135
136         context = g_option_context_new(NULL);
137         g_option_context_add_main_entries(context, options, NULL);
138
139         if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
140                 if (err != NULL) {
141                         g_printerr("%s\n", err->message);
142                         g_error_free(err);
143                         return 1;
144                 }
145
146                 g_printerr("An unknown error occurred\n");
147                 return 1;
148         }
149
150         g_option_context_free(context);
151
152         if (option_device == NULL)
153                 option_device = g_strdup("/dev/ttyUSB1");
154
155         g_print("Device: %s\n", option_device);
156
157         channel = g_at_tty_open_qcdm(option_device);
158         if (channel == NULL) {
159                 g_printerr("Failed to open QCDM device\n");
160                 return 1;
161         }
162
163         event_loop = g_main_loop_new(NULL, FALSE);
164
165         hdlc = g_at_hdlc_new(channel);
166
167         g_io_channel_unref(channel);
168
169         if (hdlc == NULL)
170                 return 1;
171
172         if (option_debug == TRUE)
173                 g_at_hdlc_set_debug(hdlc, hdlc_debug, "HDLC");
174
175         g_at_hdlc_set_xmit_accm(hdlc, 0);
176         g_at_hdlc_set_recv_accm(hdlc, 0);
177
178         g_at_hdlc_set_receive(hdlc, hdlc_receive, NULL);
179
180         send_command(hdlc, 0x00);       /* Version info */
181         send_command(hdlc, 0x51);       /* Features query */
182
183         send_subsys_command(hdlc, 250, 7);      /* Novatel modem status */
184
185         g_main_loop_run(event_loop);
186
187         g_at_hdlc_unref(hdlc);
188
189         g_main_loop_unref(event_loop);
190
191         g_free(option_device);
192
193         return 0;
194 }