summaryrefslogtreecommitdiff
path: root/plugins/allowed-apns.c
blob: 199202b5c82b7eceb01acd4edfaaf6f0d6fda440 (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
283
284
285
286
287
288
289
290
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2016  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 <string.h>
#include <errno.h>
#include <glib.h>
#include <ofono.h>
#include <simutil.h>

#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/plugin.h>
#include <ofono/modem.h>
#include <ofono/sim.h>
#include <ofono/dbus.h>
#include <gdbus.h>

#define SIM_EFACL_FILEID	0x6f57

#define ALLOWED_ACCESS_POINTS_INTERFACE "org.ofono.AllowedAccessPoints"

guint modemwatch_id;
GSList *context_list;

struct allowed_apns_ctx {
	guint simwatch_id;
	guint atomwatch_id;
	struct ofono_modem *modem;
	struct ofono_sim *sim;
	struct ofono_sim_context *sim_context;
	DBusMessage *pending;
	DBusMessage *reply;
	bool registered;
};

static void context_destroy(gpointer data)
{
	struct allowed_apns_ctx *ctx = data;

	if (ctx->simwatch_id)
		ofono_sim_remove_state_watch(ctx->sim,
					ctx->simwatch_id);

	if (ctx->atomwatch_id)
		__ofono_modem_remove_atom_watch(ctx->modem,
					ctx->atomwatch_id);

	if (ctx->sim_context)
		ofono_sim_context_free(ctx->sim_context);

	g_free(ctx);
}

static void atomwatch_destroy(gpointer data)
{
	struct allowed_apns_ctx *ctx = data;

	ctx->atomwatch_id = 0;
}

static void sim_acl_read_cb(int ok, int total_length, int record,
			const unsigned char *data, int record_length,
			void *userdata)
{
	struct allowed_apns_ctx *ctx = userdata;
	DBusMessage *reply = ctx->reply;
	DBusMessageIter iter;
	DBusMessageIter array;
	struct simple_tlv_iter tlv_iter;
	char *apn;

	if (!ok) {
		reply = __ofono_error_failed(ctx->pending);
		__ofono_dbus_pending_reply(&ctx->pending, reply);
		return;
	}

	reply = dbus_message_new_method_return(ctx->pending);
	if (reply == NULL)
		return;

	dbus_message_iter_init_append(reply, &iter);

	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
					DBUS_TYPE_STRING_AS_STRING,
					&array);

	if (data[0] == 0)
		goto done;

	simple_tlv_iter_init(&tlv_iter, &data[1], total_length - 1);

	while (simple_tlv_iter_next(&tlv_iter)) {
		if (simple_tlv_iter_get_tag(&tlv_iter) != 0xDD)
			continue;

		apn = g_strndup(
			(char *) simple_tlv_iter_get_data(&tlv_iter),
			simple_tlv_iter_get_length(&tlv_iter));

		dbus_message_iter_append_basic(&array,
					DBUS_TYPE_STRING,
					&apn);

		g_free(apn);
	}

done:
	dbus_message_iter_close_container(&iter, &array);

	__ofono_dbus_pending_reply(&ctx->pending, reply);
}

static DBusMessage *get_allowed_apns(DBusConnection *conn,
			DBusMessage *msg, void *data)
{
	struct allowed_apns_ctx *ctx = data;

	if (ctx->pending)
		return __ofono_error_busy(msg);

	ctx->pending = dbus_message_ref(msg);

	ofono_sim_read(ctx->sim_context, SIM_EFACL_FILEID,
		OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
		sim_acl_read_cb, ctx);

	return NULL;
}

static const GDBusMethodTable allowed_apns_methods[] = {
	{ GDBUS_ASYNC_METHOD("GetAllowedAccessPoints",
			NULL, GDBUS_ARGS({ "apnlist", "as" }),
			get_allowed_apns) },
	{ }
};

static void sim_state_watch(enum ofono_sim_state new_state, void *data)
{
	struct allowed_apns_ctx *ctx = data;
	DBusConnection *conn = ofono_dbus_get_connection();

	if (new_state != OFONO_SIM_STATE_READY) {
		if (!ctx->registered)
			return;

		g_dbus_unregister_interface(conn,
				ofono_modem_get_path(ctx->modem),
				ALLOWED_ACCESS_POINTS_INTERFACE);

		ofono_modem_remove_interface(ctx->modem,
				ALLOWED_ACCESS_POINTS_INTERFACE);

		ctx->registered = false;
		return;
	}

	if (!g_dbus_register_interface(conn,
				ofono_modem_get_path(ctx->modem),
				ALLOWED_ACCESS_POINTS_INTERFACE,
				allowed_apns_methods, NULL, NULL,
				ctx, NULL)) {
		ofono_error("Cannot create %s Interface\n",
			ALLOWED_ACCESS_POINTS_INTERFACE);

		return;
	}

	ctx->registered = true;
	ofono_modem_add_interface(ctx->modem,
			ALLOWED_ACCESS_POINTS_INTERFACE);
}

static void sim_watch(struct ofono_atom *atom,
		enum ofono_atom_watch_condition cond,
		void *data)
{
	struct allowed_apns_ctx *ctx = data;

	if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
		if (ctx->simwatch_id) {
			sim_state_watch(OFONO_SIM_STATE_NOT_PRESENT, data);
			ofono_sim_remove_state_watch(ctx->sim, ctx->simwatch_id);
			ctx->simwatch_id = 0;
		}

		if (ctx->sim_context) {
			ofono_sim_context_free(ctx->sim_context);
			ctx->sim_context = NULL;
		}

		return;
	}

	ctx->sim = __ofono_atom_get_data(atom);

	ctx->sim_context = ofono_sim_context_create(ctx->sim);

	ctx->simwatch_id = ofono_sim_add_state_watch(ctx->sim,
						sim_state_watch,
						ctx, NULL);
}

static gint context_list_modem_compare(gconstpointer data1,
				gconstpointer data2)
{
	const struct allowed_apns_ctx *ctx = data1;
	const struct ofono_modem *modem = data2;
	return (ctx->modem == modem);
}

static void modem_watch(struct ofono_modem *modem,
		gboolean added, void *userdata)
{
	struct allowed_apns_ctx *ctx;
	GSList *l;

	if (added == FALSE) {
		l = g_slist_find_custom(context_list,
				modem, context_list_modem_compare);

		if (l) {
			ctx = l->data;
			context_destroy(ctx);
			context_list = g_slist_delete_link(context_list, l);
		}

		return;
	}

	ctx = g_try_new0(struct allowed_apns_ctx, 1);
	if (ctx == NULL)
		return;

	context_list = g_slist_prepend(context_list, ctx);

	ctx->modem = modem;

	ctx->atomwatch_id = __ofono_modem_add_atom_watch(ctx->modem,
						OFONO_ATOM_TYPE_SIM,
						sim_watch, ctx,
						atomwatch_destroy);
}

static void call_modemwatch(struct ofono_modem *modem, void *userdata)
{
	modem_watch(modem, TRUE, userdata);
}

static int allowed_apns_init(void)
{
	modemwatch_id = __ofono_modemwatch_add(modem_watch, NULL, NULL);

	__ofono_modem_foreach(call_modemwatch, NULL);

	return 0;
}

static void allowed_apns_exit(void)
{
	__ofono_modemwatch_remove(modemwatch_id);

	g_slist_free_full(context_list, context_destroy);
}

OFONO_PLUGIN_DEFINE(allowed_apns, "Plugin to read EFACL from SIM",
		VERSION, OFONO_PLUGIN_PRIORITY_DEFAULT,
		allowed_apns_init, allowed_apns_exit)