summaryrefslogtreecommitdiffstats
path: root/src/history.c
blob: 3eb9a3da8454477e14aa9d65553b99c9e1788dc6 (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
/*
 *
 *  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 <string.h>
#include <stdio.h>

#include <glib.h>

#include "ofono.h"

static GSList *history_drivers = NULL;

struct history_call_foreach_data {
	const struct ofono_call *call;
	union {
		struct {
			time_t start;
			time_t end;
		};

		time_t when;
	};
};

struct history_sms_foreach_data {
	const struct ofono_uuid *uuid;
	const char *address;
	const char *text;
	union {
		struct {
			const struct tm *remote;
			const struct tm *local;
		};
		struct {
			time_t when;
			enum ofono_history_sms_status status;
		};
	};
};

static struct ofono_history_context *history_context_create(
					struct ofono_modem *modem,
					struct ofono_history_driver *driver)
{
	struct ofono_history_context *context;

	if (driver->probe == NULL)
		return NULL;

	context = g_try_new0(struct ofono_history_context, 1);

	if (context == NULL)
		return NULL;

	context->driver = driver;
	context->modem = modem;

	if (driver->probe(context) < 0) {
		g_free(context);
		return NULL;
	}

	return context;
}

static void context_remove(struct ofono_atom *atom)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);

	if (context->driver->remove)
		context->driver->remove(context);

	g_free(context);
}

void __ofono_history_probe_drivers(struct ofono_modem *modem)
{
	struct ofono_history_driver *driver;
	struct ofono_history_context *context;
	GSList *l;

	for (l = history_drivers; l; l = l->next) {
		driver = l->data;

		context = history_context_create(modem, driver);
		if (context == NULL)
			continue;

		__ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_HISTORY,
						context_remove, context);
	}
}

static void history_call_ended(struct ofono_atom *atom, void *data)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);
	struct history_call_foreach_data *hfd = data;

	if (context->driver->call_ended == NULL)
		return;

	context->driver->call_ended(context, hfd->call, hfd->start, hfd->end);
}

void __ofono_history_call_ended(struct ofono_modem *modem,
				const struct ofono_call *call,
				time_t start, time_t end)
{
	struct history_call_foreach_data hfd;

	hfd.call = call;
	hfd.start = start;
	hfd.end = end;

	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_HISTORY,
					history_call_ended, &hfd);
}

static void history_call_missed(struct ofono_atom *atom, void *data)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);
	struct history_call_foreach_data *hfd = data;

	if (context->driver->call_missed == NULL)
		return;

	context->driver->call_missed(context, hfd->call, hfd->when);
}

void __ofono_history_call_missed(struct ofono_modem *modem,
				const struct ofono_call *call, time_t when)
{
	struct history_call_foreach_data hfd;

	hfd.call = call;
	hfd.when = when;

	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_HISTORY,
					history_call_missed, &hfd);
}

static void history_sms_received(struct ofono_atom *atom, void *data)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);
	struct history_sms_foreach_data *hfd = data;

	if (context->driver->sms_received == NULL)
		return;

	context->driver->sms_received(context, hfd->uuid, hfd->address,
					hfd->remote, hfd->local, hfd->text);
}

void __ofono_history_sms_received(struct ofono_modem *modem,
					const struct ofono_uuid *uuid,
					const char *from,
					const struct tm *remote,
					const struct tm *local,
					const char *text)
{
	struct history_sms_foreach_data hfd;

	hfd.uuid = uuid;
	hfd.address = from;
	hfd.remote = remote;
	hfd.local = local;
	hfd.text = text;

	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_HISTORY,
					history_sms_received, &hfd);
}

static void history_sms_send_pending(struct ofono_atom *atom, void *data)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);
	struct history_sms_foreach_data *hfd = data;

	if (context->driver->sms_send_pending == NULL)
		return;

	context->driver->sms_send_pending(context, hfd->uuid, hfd->address,
						hfd->when, hfd->text);
}

void __ofono_history_sms_send_pending(struct ofono_modem *modem,
					const struct ofono_uuid *uuid,
					const char *to,
					time_t when, const char *text)
{
	struct history_sms_foreach_data hfd;

	hfd.uuid = uuid;
	hfd.address = to;
	hfd.text = text;
	hfd.when = when;
	hfd.status = OFONO_HISTORY_SMS_STATUS_PENDING;

	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_HISTORY,
					history_sms_send_pending, &hfd);
}

static void history_sms_send_status(struct ofono_atom *atom, void *data)
{
	struct ofono_history_context *context = __ofono_atom_get_data(atom);
	struct history_sms_foreach_data *hfd = data;

	if (context->driver->sms_send_status == NULL)
		return;

	context->driver->sms_send_status(context, hfd->uuid,
						hfd->when, hfd->status);
}

void __ofono_history_sms_send_status(struct ofono_modem *modem,
					const struct ofono_uuid *uuid,
					time_t when,
					enum ofono_history_sms_status status)
{
	struct history_sms_foreach_data hfd;

	hfd.uuid = uuid;
	hfd.address = NULL;
	hfd.text = NULL;
	hfd.when = when;
	hfd.status = status;

	__ofono_modem_foreach_atom(modem, OFONO_ATOM_TYPE_HISTORY,
					history_sms_send_status, &hfd);
}

int ofono_history_driver_register(const struct ofono_history_driver *driver)
{
	DBG("driver: %p name: %s", driver, driver->name);

	history_drivers = g_slist_prepend(history_drivers, (void *) driver);

	return 0;
}

void ofono_history_driver_unregister(const struct ofono_history_driver *driver)
{
	DBG("driver: %p name: %s", driver, driver->name);

	history_drivers = g_slist_remove(history_drivers, driver);
}