summaryrefslogtreecommitdiffstats
path: root/drivers/atmodem/gnss.c
blob: 5d868dd338492406f527d65d3fce6e566d98a3a9 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
 *  Copyright (C) 2011  ST-Ericsson AB.
 *
 *  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

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/gnss.h>

#include "gatchat.h"
#include "gatresult.h"

#include "atmodem.h"
#include "vendor.h"

struct gnss_data {
	GAtChat *chat;
	unsigned int vendor;
};

static const char *none_prefix[] = { NULL };
static const char *cpos_prefix[] = { "+CPOS:", NULL };
static const char *cposr_prefix[] = { "+CPOSR:", NULL };

static void gnss_pr_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_gnss_cb_t cb = cbd->cb;
	struct ofono_error error;

	DBG("");

	decode_at_error(&error, g_at_result_final_response(result));

	cb(&error, cbd->data);
}

static void at_gnss_position_reporting(struct ofono_gnss *gnss,
					ofono_bool_t enable,
					ofono_gnss_cb_t cb,
					void *data)
{
	struct gnss_data *ad = ofono_gnss_get_data(gnss);
	struct cb_data *cbd = cb_data_new(cb, data);

	DBG("");

	if (enable) {
		g_at_chat_send(ad->chat, "AT+CPOSR=1",
				cposr_prefix, gnss_pr_cb, cbd, g_free);

		if (ad->vendor == OFONO_VENDOR_STE)
			g_at_chat_send(ad->chat, "AT*EPOSADRR=1",
					NULL, NULL, NULL, NULL);
	} else {
		g_at_chat_send(ad->chat, "AT+CPOSR=0",
				cposr_prefix, gnss_pr_cb, cbd, g_free);

		if (ad->vendor == OFONO_VENDOR_STE)
			g_at_chat_send(ad->chat, "AT*EPOSADRR=0",
					NULL, NULL, NULL, NULL);
	}
}

static void gnss_se_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct cb_data *cbd = user_data;
	ofono_gnss_cb_t cb = cbd->cb;
	struct ofono_error error;

	DBG("");

	decode_at_error(&error, g_at_result_final_response(result));

	cb(&error, cbd->data);
}

static void at_gnss_send_element(struct ofono_gnss *gnss,
				const char *xml,
				ofono_gnss_cb_t cb, void *data)
{
	struct gnss_data *ad = ofono_gnss_get_data(gnss);
	struct cb_data *cbd = cb_data_new(cb, data);
	char *buf = g_try_new(char, strlen(xml) + 10);
	int len;

	DBG("");

	if (buf == NULL)
		goto error;

	len = sprintf(buf, "AT+CPOS\r");
	len += sprintf(buf + len, "%s", xml);

	if (g_at_chat_send_and_expect_short_prompt(ad->chat, buf, cpos_prefix,
							gnss_se_cb, cbd,
							g_free) > 0) {
		g_free(buf);
		return;
	}

error:
	g_free(buf);
	g_free(cbd);

	CALLBACK_WITH_FAILURE(cb, data);
}

static gboolean gnss_parse_report(GAtResult *result, const char *prefix,
					const char **xml)
{
	GAtResultIter iter;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, prefix))
		return FALSE;

	if (!g_at_result_iter_next_unquoted_string(&iter, xml))
		return FALSE;

	return TRUE;
}

static void gnss_report(GAtResult *result, gpointer user_data)
{
	const char *xml;

	DBG("");

	xml = NULL;

	if (!gnss_parse_report(result, "+CPOSR:", &xml)) {
		ofono_error("Unable to parse CPOSR notification");
		return;
	}

	if (xml == NULL) {
		ofono_error("Unable to parse CPOSR notification");
		return;
	}

	DBG("%s", xml);
}

static void at_gnss_reset_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_gnss *gnss = user_data;

	DBG("");

	ofono_gnss_notify_posr_reset(gnss);
}

static void at_gnss_not_supported(struct ofono_gnss *gnss)
{
	ofono_error("gnss not supported by this modem.");

	ofono_gnss_remove(gnss);
}

static void at_gnss_cposr_support_cb(gboolean ok, GAtResult *result,
					gpointer user_data)
{
	struct ofono_gnss *gnss = user_data;
	struct gnss_data *ad = ofono_gnss_get_data(gnss);

	DBG("");

	if (!ok) {
		at_gnss_not_supported(gnss);
		return;
	}

	g_at_chat_register(ad->chat, "+CPOSR:", gnss_report,
				FALSE, gnss, NULL);

	if (ad->vendor == OFONO_VENDOR_STE)
		g_at_chat_register(ad->chat, "*EPOSADRR:", at_gnss_reset_notify,
					FALSE, gnss, NULL);

	ofono_gnss_register(gnss);
}

static void at_gnss_cpos_support_cb(gboolean ok, GAtResult *result,
					gpointer user_data)
{
	struct ofono_gnss *gnss = user_data;
	struct gnss_data *ad = ofono_gnss_get_data(gnss);

	DBG("");

	if (!ok) {
		at_gnss_not_supported(gnss);
		return;
	}

	g_at_chat_send(ad->chat, "AT+CPOSR=?",
			none_prefix, at_gnss_cposr_support_cb, gnss, NULL);
}

static int at_gnss_probe(struct ofono_gnss *gnss, unsigned int vendor,
				void *user)
{
	GAtChat *chat = user;
	struct gnss_data *gd;

	DBG("");

	gd = g_try_new0(struct gnss_data, 1);
	if (gd == NULL)
		return -ENOMEM;

	gd->chat = g_at_chat_clone(chat);
	gd->vendor = vendor;

	ofono_gnss_set_data(gnss, gd);

	g_at_chat_send(gd->chat, "AT+CPOS=?",
			none_prefix, at_gnss_cpos_support_cb, gnss, NULL);

	return 0;
}

static void at_gnss_remove(struct ofono_gnss *gnss)
{
	struct gnss_data *gd = ofono_gnss_get_data(gnss);

	DBG("");

	ofono_gnss_set_data(gnss, NULL);

	g_at_chat_unref(gd->chat);
	g_free(gd);
}

static struct ofono_gnss_driver driver = {
	.name			= "atmodem",
	.probe			= at_gnss_probe,
	.remove			= at_gnss_remove,
	.send_element		= at_gnss_send_element,
	.set_position_reporting	= at_gnss_position_reporting,
};

void at_gnss_init(void)
{
	ofono_gnss_driver_register(&driver);
}

void at_gnss_exit(void)
{
	ofono_gnss_driver_unregister(&driver);
}