1 /*
2 
3   $Id$
4 
5   G N O K I I
6 
7   A Linux/Unix toolset and driver for the mobile phones.
8 
9   This file is part of gnokii.
10 
11   Gnokii is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15 
16   Gnokii is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20 
21   You should have received a copy of the GNU General Public License
22   along with gnokii; if not, write to the Free Software
23   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 
25   Copyright (C) 1999-2000  Hugh Blemings & Pavel Janik ml.
26   Copyright (C) 1999-2000  Gary Reuter, Reinhold Jordan
27   Copyright (C) 1999-2006  Pawel Kot
28   Copyright (C) 2000-2002  Marcin Wiacek, Chris Kemp, Manfred Jonsson
29   Copyright (C) 2001       Marian Jancar, Bartek Klepacz
30   Copyright (C) 2001-2002  Pavel Machek, Markus Plail
31   Copyright (C) 2002       Ladis Michl, Simon Huggins
32   Copyright (C) 2002-2004  BORBELY Zoltan
33   Copyright (C) 2003       Bertrik Sikken
34   Copyright (C) 2004       Martin Goldhahn
35 
36   Mainline code for gnokii utility. Other phone handling functions.
37 
38 */
39 
40 #include "config.h"
41 #include "misc.h"
42 #include "compat.h"
43 
44 #include <stdio.h>
45 #ifndef _GNU_SOURCE
46 #  define _GNU_SOURCE 1
47 #endif
48 #include <getopt.h>
49 
50 #ifdef WIN32
51 #  include <io.h>
52 #endif
53 
54 #include "gnokii-app.h"
55 #include "gnokii.h"
56 
other_usage(FILE * f)57 void other_usage(FILE *f)
58 {
59 	fprintf(f, _("Misc options:\n"
60 		     "          --keysequence\n"
61 		     "          --enterchar\n"
62 		     "          --listnetworks\n"
63 		     "          --getnetworkinfo\n"
64 		));
65 }
66 
67 /* pmon allows fbus code to run in a passive state - it doesn't worry about
68    whether comms are established with the phone.  A debugging/development
69    tool. */
pmon(gn_data * data,struct gn_statemachine * state)70 gn_error pmon(gn_data *data, struct gn_statemachine *state)
71 {
72 	gn_error error;
73 
74 	/* Initialise the code for the GSM interface. */
75 	error = gn_gsm_initialise(state);
76 
77 	if (error != GN_ERR_NONE) {
78 		fprintf(stderr, _("GSM/FBUS init failed! (Unknown model?). Quitting.\n"));
79 		return error;
80 	}
81 
82 	while (1) {
83 		usleep(50000);
84 	}
85 
86 	return GN_ERR_NONE;
87 }
88 
presskey(gn_data * data,struct gn_statemachine * state)89 static gn_error presskey(gn_data *data, struct gn_statemachine *state)
90 {
91 	gn_error error;
92 	error = gn_sm_functions(GN_OP_PressPhoneKey, data, state);
93 	if (error == GN_ERR_NONE)
94 		error = gn_sm_functions(GN_OP_ReleasePhoneKey, data, state);
95 	if (error != GN_ERR_NONE)
96 		fprintf(stderr, _("Failed to press key: %s\n"), gn_error_print(error));
97 	return error;
98 }
99 
presskeysequence(gn_data * data,struct gn_statemachine * state)100 gn_error presskeysequence(gn_data *data, struct gn_statemachine *state)
101 {
102 	gn_error error = GN_ERR_NONE;
103 	unsigned char *syms = "0123456789#*PGR+-UDMN";
104 	gn_key_code keys[] = {GN_KEY_0, GN_KEY_1, GN_KEY_2, GN_KEY_3,
105 			      GN_KEY_4, GN_KEY_5, GN_KEY_6, GN_KEY_7,
106 			      GN_KEY_8, GN_KEY_9, GN_KEY_HASH,
107 			      GN_KEY_ASTERISK, GN_KEY_POWER, GN_KEY_GREEN,
108 			      GN_KEY_RED, GN_KEY_INCREASEVOLUME,
109 			      GN_KEY_DECREASEVOLUME, GN_KEY_UP, GN_KEY_DOWN,
110 			      GN_KEY_MENU, GN_KEY_NAMES};
111 	unsigned char ch, *pos;
112 
113 	gn_data_clear(data);
114 	console_raw();
115 
116 	while (read(0, &ch, 1) > 0) {
117 		if ((pos = strchr(syms, toupper(ch))) != NULL)
118 			data->key_code = keys[pos - syms];
119 		else
120 			continue;
121 		error = presskey(data, state);
122 	}
123 
124 	return error;
125 }
126 
enterchar(gn_data * data,struct gn_statemachine * state)127 gn_error enterchar(gn_data *data, struct gn_statemachine *state)
128 {
129 	unsigned char ch;
130 	gn_error error = GN_ERR_NONE;
131 
132 	gn_data_clear(data);
133 	console_raw();
134 
135 	while ((error = GN_ERR_NONE) && (read(0, &ch, 1) > 0)) {
136 		switch (ch) {
137 		case '\r':
138 			break;
139 		case '\n':
140 			data->key_code = GN_KEY_MENU;
141 			presskey(data, state);
142 			break;
143 #ifdef WIN32
144 		case '\033':
145 #else
146 		case '\e':
147 #endif
148 			data->key_code = GN_KEY_NAMES;
149 			presskey(data, state);
150 			break;
151 		default:
152 			data->character = ch;
153 			error = gn_sm_functions(GN_OP_EnterChar, data, state);
154 			if (error != GN_ERR_NONE)
155 				fprintf(stderr, _("Error entering char: %s\n"), gn_error_print(error));
156 			break;
157 		}
158 	}
159 
160 	return error;
161 }
162 
list_gsm_networks(void)163 void list_gsm_networks(void)
164 {
165 
166 	gn_network network;
167 	int i = 0;
168 
169 	printf(_("Network  Name\n"));
170 	printf(_("-----------------------------------------\n"));
171 	while (gn_network_get(&network, i++))
172 		printf(_("%-7s  %s (%s)\n"), network.code, network.name, gn_country_name_get(network.code));
173 }
174 
getnetworkinfo(gn_data * data,struct gn_statemachine * state)175 gn_error getnetworkinfo(gn_data *data, struct gn_statemachine *state)
176 {
177 	gn_network_info networkinfo;
178 	gn_error error;
179 	int lac, cid;
180 	char country[4] = {0, 0, 0, 0};
181 
182 	gn_data_clear(data);
183 	memset(&networkinfo, 0, sizeof(networkinfo));
184 
185 	data->network_info = &networkinfo;
186 	data->reg_notification = NULL;
187 	data->callback_data = NULL;
188 
189 	if ((error = gn_sm_functions(GN_OP_GetNetworkInfo, data, state)) != GN_ERR_NONE) {
190 		fprintf(stderr, _("Error: %s\n"), gn_error_print(error));
191 		return error;
192 	}
193 
194 	/* Ugly, ugly, ... */
195         if (networkinfo.cell_id[2] == 0 && networkinfo.cell_id[3] == 0)
196         	cid = (networkinfo.cell_id[0] << 8) + networkinfo.cell_id[1];
197 	else
198 		cid = (networkinfo.cell_id[0] << 24) + (networkinfo.cell_id[1] << 16) + (networkinfo.cell_id[2] << 8) + networkinfo.cell_id[3];
199 	lac = (networkinfo.LAC[0] << 8) + networkinfo.LAC[1];
200 	memcpy(country, networkinfo.network_code, 3);
201 
202 	fprintf(stdout, _("Network      : %s (%s)\n"),
203 			gn_network_name_get((char *)networkinfo.network_code),
204 			gn_country_name_get((char *)country));
205 	fprintf(stdout, _("Network code : %s\n"), (*networkinfo.network_code ? networkinfo.network_code : _("undefined")));
206 	fprintf(stdout, _("LAC          : %04x (%d)\n"), lac, lac);
207 	fprintf(stdout, _("Cell id      : %08x (%d)\n"), cid, cid);
208 
209 	return GN_ERR_NONE;
210 }
211 
212