aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/qmimodem/voicecall.c
blob: d16613f3202e5e7999e79a2a813ed52a26497c4e (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2011-2012  Intel Corporation. All rights reserved.
 *  Copyright (C) 2017 Alexander Couzens <lynxis@fe80.eu>
 *
 *  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 <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/voicecall.h>
#include <ofono/call-list.h>

#include "../src/common.h"

#include "qmi.h"
#include "qmimodem.h"
#include "voice.h"
#include "voice_generated.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif


/* qmi protocol */


/* end of qmi */

struct voicecall_data {
	struct qmi_service *voice;
	uint16_t major;
	uint16_t minor;
	GSList *call_list;
	struct voicecall_static *vs;
	struct ofono_phone_number dialed;
};

static void all_call_status_ind(struct qmi_result *result, void *user_data)
{
	struct ofono_voicecall *vc = user_data;
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	GSList *calls = NULL;
	int i;
	int size = 0;
	struct qmi_voice_all_call_status_ind status_ind;
	GSList *n, *o;
	struct ofono_call *nc, *oc;


	if (qmi_voice_ind_call_status(result, &status_ind) != NONE) {
		DBG("Parsing of all call status indication failed");
		return;
	}

	if (!status_ind.remote_party_number_set || !status_ind.call_information_set) {
		DBG("Some required fields are not set");
		return;
	}

	size = status_ind.call_information->size;
	if (!size) {
		DBG("No call informations received!");
		return;
	}

	/* expect we have valid fields for every call */
	if (size != status_ind.remote_party_number_size)  {
		DBG("Not all fields have the same size");
		return;
	}

	for (i = 0; i < size; i++) {
		struct qmi_voice_call_information_instance call_info;
		struct ofono_call *call;
		const struct qmi_voice_remote_party_number_instance *remote_party = status_ind.remote_party_number[i];
		int number_size;

		call_info = status_ind.call_information->instance[i];
		call = g_new0(struct ofono_call, 1);
		call->id = call_info.id;
		call->direction = qmi_to_ofono_direction(call_info.direction);

		if (qmi_to_ofono_status(call_info.state, &call->status)) {
			DBG("Ignore call id %d, because can not convert QMI state 0x%x to ofono.",
			    call_info.id, call_info.state);
			continue;
		}
		DBG("Call %d in state %s(%d)",
		    call_info.id,
		    qmi_voice_call_state_name(call_info.state),
		    call_info.state);

		call->type = 0; /* always voice */
		number_size = remote_party->number_size;
		if (number_size > OFONO_MAX_PHONE_NUMBER_LENGTH)
			OFONO_MAX_PHONE_NUMBER_LENGTH;
		strncpy(call->phone_number.number, remote_party->number,
				number_size);
		/* FIXME: set phone_number_type */

		if (strlen(call->phone_number.number) > 0)
			call->clip_validity = 0;
		else
			call->clip_validity = 2;

		calls = g_slist_insert_sorted(calls, call, ofono_call_compare);
	}

	ofono_call_list_notify(vc, &vd->call_list, calls);
}

static void create_voice_cb(struct qmi_service *service, void *user_data)
{
	struct ofono_voicecall *vc = user_data;
	struct voicecall_data *data = ofono_voicecall_get_data(vc);

	DBG("");

	if (!service) {
		ofono_error("Failed to request Voice service");
		ofono_voicecall_remove(vc);
		return;
	}

	if (!qmi_service_get_version(service, &data->major, &data->minor)) {
		ofono_error("Failed to get Voice service version");
		ofono_voicecall_remove(vc);
		return;
	}

	data->voice = qmi_service_ref(service);

	/* FIXME: we should call indication_register to ensure we get notified on call events.
	 * We rely at the moment on the default value of notifications
	 */
	qmi_service_register(data->voice, QMI_VOICE_IND_ALL_STATUS,
			     all_call_status_ind, vc, NULL);

	ofono_voicecall_register(vc);
}

static int qmi_voicecall_probe(struct ofono_voicecall *vc,
					unsigned int vendor, void *user_data)
{
	struct qmi_device *device = user_data;
	struct voicecall_data *data;

	DBG("");

	data = g_new0(struct voicecall_data, 1);

	ofono_voicecall_set_data(vc, data);

	qmi_service_create(device, QMI_SERVICE_VOICE,
					create_voice_cb, vc, NULL);

	return 0;
}

static void qmi_voicecall_remove(struct ofono_voicecall *vc)
{
	struct voicecall_data *data = ofono_voicecall_get_data(vc);

	DBG("");

	ofono_voicecall_set_data(vc, NULL);

	qmi_service_unregister_all(data->voice);

	qmi_service_unref(data->voice);

	g_slist_free_full(data->call_list, g_free);

	g_free(data);
}


static struct ofono_call *create_call(struct ofono_voicecall *vc,
				      enum call_direction direction,
				      enum call_status status,
				      const char *num,
				      int num_type,
				      int clip)
{
	return NULL;
}

static void dial_cb(struct qmi_result *result, void *user_data)
{
	struct cb_data *cbd = user_data;
	struct ofono_voicecall *vc = cbd->user;
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	ofono_voicecall_cb_t cb = cbd->cb;
	uint16_t error;
	struct qmi_voice_dial_call_result dial_result;
	struct ofono_call *call;

	if (qmi_result_set_error(result, &error)) {
		DBG("QMI Error %d", error);
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	if (NONE != qmi_voice_dial_call_parse(result, &dial_result)) {
		DBG("Received invalid Result");
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	if (!dial_result.call_id_set) {
		DBG("Didn't receive a call id");
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	DBG("New call QMI id %d", dial_result.call_id);
	ofono_call_list_dial_callback(vc,
				      &vd->call_list,
				      &vd->dialed,
				      dial_result.call_id);


	/* FIXME: create a timeout on this call_id */
	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void dial(struct ofono_voicecall *vc, const struct ofono_phone_number *ph,
		enum ofono_clir_option clir, ofono_voicecall_cb_t cb,
		void *data)
{
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	struct cb_data *cbd = cb_data_new(cb, data);
	struct qmi_voice_dial_call_arg arg;

	cbd->user = vc;
	arg.calling_number_set = true;
	arg.calling_number = ph->number;
	memcpy(&vd->dialed, ph, sizeof(*ph));

	arg.call_type_set = true;
	arg.call_type = QMI_CALL_TYPE_VOICE_FORCE;

	if (!qmi_voice_dial_call(
				&arg,
				vd->voice,
				dial_cb,
				cbd,
				g_free))
		return;

	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void answer_cb(struct qmi_result *result, void *user_data)
{
	struct cb_data *cbd = user_data;
	struct ofono_voicecall *vc = cbd->user;
	ofono_voicecall_cb_t cb = cbd->cb;
	uint16_t error;
	struct qmi_voice_answer_call_result answer_result;
	struct ofono_call *call;

	if (qmi_result_set_error(result, &error)) {
		DBG("QMI Error %d", error);
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	/* TODO: what happens when calling it with no active call or wrong caller id? */
	if (NONE != qmi_voice_answer_call_parse(result, &answer_result)) {
		DBG("Received invalid Result");
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void answer(struct ofono_voicecall *vc, ofono_voicecall_cb_t cb, void *data)
{
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	struct cb_data *cbd = cb_data_new(cb, data);
	struct qmi_voice_answer_call_arg arg;
	struct ofono_call *call;
	GSList *list;

	DBG("");
	cbd->user = vc;

	list = g_slist_find_custom(vd->call_list,
				   GINT_TO_POINTER(CALL_STATUS_INCOMING),
				   ofono_call_compare_by_status);

	if (list == NULL) {
		DBG("Can not find a call to answer");
		goto err;
	}

	call = list->data;

	arg.call_id_set = true;
	arg.call_id = call->id;

	if (!qmi_voice_answer_call(
				&arg,
				vd->voice,
				answer_cb,
				cbd,
				g_free))
		return;
err:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void manage_cb(struct qmi_result *result, void *user_data)
{
	struct cb_data *cbd = user_data;
	struct ofono_voicecall *vc = cbd->user;
	ofono_voicecall_cb_t cb = cbd->cb;
	uint16_t error;
	struct qmi_voice_manage_call_result end_result;
	struct ofono_call *call;

	if (qmi_result_set_error(result, &error)) {
		DBG("QMI Error %d", error);
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	if (NONE != qmi_voice_manage_call_parse(result, &end_result)) {
		DBG("Received invalid Result");
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void release_specific(struct ofono_voicecall *vc, int id,
		ofono_voicecall_cb_t cb, void *data)
{
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	struct cb_data *cbd = cb_data_new(cb, data);
	struct qmi_voice_manage_call_arg arg;
	int i;
	GSList *l;
	struct ofono_call *call;

	DBG("");
	cbd->user = vc;

	l = g_slist_find_custom(vd->call_list, GINT_TO_POINTER(id),
							ofono_call_compare_by_id);
	if (l == NULL) {
		ofono_error("invalid call id %d", id);
		goto error;
	}

	call = l->data;

	arg.call_id_set = true;
	arg.call_id = id;
	switch (call->status) {
		case CALL_STATUS_ACTIVE:
		case CALL_STATUS_DIALING:
		case CALL_STATUS_ALERTING:
			arg.ss_call_type = 9; // release specified call
			break;
		case CALL_STATUS_HELD:
		case CALL_STATUS_INCOMING:
		case CALL_STATUS_WAITING:
			arg.ss_call_type = 1; // release held or waiting
			break;
		case CALL_STATUS_DISCONNECTED:
			goto success;
	}

	if (!qmi_voice_manage_call(&arg,
				vd->voice,
				manage_cb,
				cbd,
				g_free))
		return;

error:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
	return;

success:
	CALLBACK_WITH_SUCCESS(cb, data);
	g_free(cbd);
	return;
}

static void hangup_active(struct ofono_voicecall *vc,
		ofono_voicecall_cb_t cb, void *data)
{
	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
	struct qmi_voice_manage_call_arg arg;
	struct ofono_call *call;
	GSList *list = NULL;
	enum call_status active[] = {
		CALL_STATUS_ACTIVE,
		CALL_STATUS_DIALING,
		CALL_STATUS_ALERTING,
		CALL_STATUS_INCOMING,
	};
	int i;

	DBG("");
	for (i = 0; i < ARRAY_SIZE(active); i++) {
		list = g_slist_find_custom(vd->call_list,
					   GINT_TO_POINTER(active[i]),
					   ofono_call_compare_by_status);

		if (list)
			break;
	}

	if (list == NULL) {
		DBG("Can not find a call to hang up");
		CALLBACK_WITH_FAILURE(cb, data);
		return;
	}

	call = list->data;
	release_specific(vc, call->id, cb, data);
}

static struct ofono_voicecall_driver driver = {
	.name		= "qmimodem",
	.probe		= qmi_voicecall_probe,
	.remove		= qmi_voicecall_remove,
	.dial		= dial,
	.answer		= answer,
	.hangup_active  = hangup_active,
	.release_specific  = release_specific,
};

void qmi_voicecall_init(void)
{
	ofono_voicecall_driver_register(&driver);
}

void qmi_voicecall_exit(void)
{
	ofono_voicecall_driver_unregister(&driver);
}