aboutsummaryrefslogtreecommitdiffstats
path: root/binding/telephony-binding.c
blob: 10c00fc63d659d036018e8030a8c83701962f161 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright (C) 2017-2019 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <glib.h>
#include <json-c/json.h>
#include <string.h>

#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>

#include "ofono_manager.h"
#include "ofono_voicecallmanager.h"
#include "ofono_voicecall.h"

#define OFONO_MODEM_INTERFACE	"org.ofono.Modem"

static OrgOfonoVoiceCallManager *vcm;
static OrgOfonoVoiceCall *incoming_call, *voice_call;
static afb_event_t call_state_changed_event;
static afb_event_t dialing_call_event;
static afb_event_t incoming_call_event;
static afb_event_t terminated_call_event;
static afb_event_t online_event;
static afb_event_t battery_event;
static afb_event_t signal_event;

static void dial(afb_req_t request)
{
	struct json_object *query, *val;
	const char *number;

	query = afb_req_json(request);
	json_object_object_get_ex(query, "value", &val);
	if (json_object_is_type(val, json_type_string)) {
		number = json_object_get_string(val);
		if (voice_call) {
			AFB_ERROR("dial: cannot dial with active call");
			afb_req_fail(request, "active call", NULL);
		} else {
			AFB_DEBUG("dial: %s...\n", number);
			if (ofono_voicecallmanager_dial(vcm, (gchar *)number, "")) {
				afb_req_success(request, NULL, NULL);
			} else {
				AFB_ERROR("dial: failed to dial number\n");
				afb_req_fail(request, "failed dial", NULL);
			}
		}
	} else {
		AFB_ERROR("dial: no phone number parameter\n");
		afb_req_fail(request, "no number", NULL);
	}
}

static void last_dial(afb_req_t request)
{
	if (voice_call) {
		AFB_ERROR("dial: cannot dial with active call");
		afb_req_fail(request, "active call", NULL);
	} else {
		if (ofono_voicecallmanager_last_dial(vcm)) {
			afb_req_success(request, NULL, NULL);
		} else {
			AFB_ERROR("dial: failed to dial number\n");
			afb_req_fail(request, "failed dial", NULL);
		}
	}
}

static void send_tones(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");

	if (!voice_call) {
		AFB_ERROR("send_tones: cannot send tone without active call");
		afb_req_fail(request, "there is no active call", NULL);
	} else {
		if (ofono_voicecallmanager_send_tones(vcm, value)) {
			afb_req_success(request, NULL, NULL);
		} else {
			AFB_ERROR("send_tones: failed to send tone\n");
			afb_req_fail(request, "failed send tone", NULL);
		}
	}
}

static void hangup(afb_req_t request)
{
	if (voice_call) {
		AFB_DEBUG("Hangup voice call\n");
		ofono_voicecall_hangup(voice_call);
		afb_req_success(request, NULL, NULL);
	} else if (incoming_call) {
		AFB_DEBUG("Reject incoming call\n");
		ofono_voicecall_hangup(incoming_call);
		afb_req_success(request, NULL, NULL);
	} else {
		AFB_ERROR("Hangup: no active call");
		afb_req_fail(request, "failed hangup", NULL);
	}
}

static void answer(afb_req_t request)
{
	if (incoming_call) {
		AFB_DEBUG("Answer voice call\n");
		voice_call = incoming_call;
		ofono_voicecall_answer(voice_call);
	} else {
		AFB_ERROR("Answer: no incoming call");
	}
}

static void get_battery_level(afb_req_t request)
{
	const gchar *device;

	device = ofono_manager_get_default_modem_path();
	if (!device) {
		afb_req_fail(request, "failed", "No path find");
		return;
	}

	json_object *result = ofono_get_property(device, OFONO_HANDSFREE_INTERFACE, "BatteryChargeLevel", NULL);
	if (!result)
		afb_req_fail(request, "failed", "Can not find Battery object");
	else
		afb_req_success(request, result, "OK");
	return;
}

static void get_network_registration(afb_req_t request)
{
	const gchar *device;

	device = ofono_manager_get_default_modem_path();
	if (!device) {
		afb_req_fail(request, "failed", "No path find");
		return;
	}

	json_object *result = ofono_get_property(device, OFONO_NetworkRegistration_INTERFACE, NULL, NULL);
	if (!result)
		afb_req_fail(request, "failed", "Can not find Network Registration object");
	else
		afb_req_success(request, result, "OK");
	return;
}

static void subscribe(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");
	if(value) {
		if (!strcasecmp(value, "callStateChanged")) {
			afb_req_subscribe(request, call_state_changed_event);
		} else if (!strcasecmp(value, "dialingCall")) {
			afb_req_subscribe(request, dialing_call_event);
		} else if (!strcasecmp(value, "incomingCall")) {
			afb_req_subscribe(request, incoming_call_event);
		} else if (!strcasecmp(value, "terminatedCall")) {
			afb_req_subscribe(request, terminated_call_event);
		} else if (!strcasecmp(value, "battery")) {
			afb_req_subscribe(request, battery_event);
		} else if (!strcasecmp(value, "signal")) {
			afb_req_subscribe(request, signal_event);
		} else if (!strcasecmp(value, "online")) {
			json_object *jresp = json_object_new_object();

			afb_req_subscribe(request, online_event);

			json_object_object_add(jresp, "connected",
				json_object_new_boolean(ofono_manager_get_default_modem_online()));
			afb_event_push(online_event, jresp);
		} else {
			afb_req_fail(request, "failed", "Invalid event");
			return;
		}
	}

	afb_req_success(request, NULL, NULL);
}

static void unsubscribe(afb_req_t request)
{
	const char *value = afb_req_value(request, "value");
	if(value) {
		if (!strcasecmp(value, "callStateChanged")) {
			afb_req_unsubscribe(request, call_state_changed_event);
		} else if (!strcasecmp(value, "dialingCall")) {
			afb_req_unsubscribe(request, dialing_call_event);
		} else if (!strcasecmp(value, "incomingCall")) {
			afb_req_unsubscribe(request, incoming_call_event);
		} else if (!strcasecmp(value, "terminatedCall")) {
			afb_req_unsubscribe(request, terminated_call_event);
		} else {
			afb_req_fail(request, "failed", "Invalid event");
			return;
		}
	}

	afb_req_success(request, NULL, NULL);
}

static void call_state_changed_cb(OrgOfonoVoiceCall *vc, gchar *state)
{
	struct json_object *call_state;
	call_state = json_object_new_object();
	json_object_object_add(call_state, "state", json_object_new_string(state));
	afb_event_push(call_state_changed_event, call_state);
}

static void incoming_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar *clip)
{
	struct json_object *call_info;

	call_info = json_object_new_object();
	json_object_object_add(call_info, "clip", json_object_new_string(clip));
	afb_event_push(incoming_call_event, call_info);
	incoming_call = ofono_voicecall_new(op, call_state_changed_cb);
}

static void dialing_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar *colp)
{
	struct json_object *call_info;

	call_info = json_object_new_object();
	json_object_object_add(call_info, "colp", json_object_new_string(colp));
	afb_event_push(dialing_call_event, call_info);
	voice_call = ofono_voicecall_new(op, call_state_changed_cb);
}

static void terminated_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op)
{
	if (incoming_call) {
		ofono_voicecall_free(incoming_call);
		incoming_call = NULL;
	} else if (voice_call) {
		ofono_voicecall_free(voice_call);
	}
	voice_call = NULL;
	afb_event_push(terminated_call_event, NULL);
}

static void *main_loop_thread(void *unused)
{
	GMainLoop *loop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(loop);
	return NULL;
}

static int ofono_init_default_modem(void)
{
	int ret = 0;
	const gchar *modem_path = ofono_manager_get_default_modem_path();

	if (modem_path) {
		vcm = ofono_voicecallmanager_init(modem_path,
				incoming_call_cb,
				dialing_call_cb,
				terminated_call_cb);
		if (!vcm) {
			AFB_ERROR("failed to initialize voice call manager\n");
			ret = -1;
		}
	} else {
		AFB_ERROR("default modem not set\n");
		ret = -1;
	}

	return ret;
}

static void ofono_modem_signal_callback(
	GDBusConnection *connection,
	const gchar *sender_name,
	const gchar *object_path,
	const gchar *interface_name,
	const gchar *signal_name,
	GVariant *parameters,
	gpointer user_data)
{
	GVariant *var = NULL;
	const gchar *key = NULL;
	gchar *address, *tmp;
	gboolean state;

	/* Only support hands-free profile for now */
	if (strncasecmp(object_path, "/hfp/org/bluez", 14))
		return;

	if (g_strcmp0(signal_name, "PropertyChanged"))
		return;

	g_variant_get(parameters, "(sv)", &key, &var);

	if (g_strcmp0(key, "Online"))
		return;

	state = g_variant_get_boolean(var);

	address = g_strdup(strstr(object_path, "dev_") + 4);
	tmp = address;

	for (; *tmp; tmp++) {
		if (*tmp == '_') *tmp = ':';
	}

	if (state) {
		ofono_manager_set_default_modem((const char *) address);

		if (ofono_manager_get_default_modem_valid()) {
			json_object *jresp = json_object_new_object();

			ofono_init_default_modem();

			json_object_object_add(jresp, "connected", json_object_new_boolean(TRUE));
			afb_event_push(online_event, jresp);
		}
	} else if (!g_strcmp0(ofono_manager_get_default_modem_address(), address)) {
		json_object *jresp = json_object_new_object();

		AFB_NOTICE("Removing modem: (%s)", address);
		ofono_manager_invalidate_default_modem();

		json_object_object_add(jresp, "connected", json_object_new_boolean(FALSE));
		afb_event_push(online_event, jresp);
	}

	g_free(address);
}

static void ofono_handsfree_signal_callback(
	GDBusConnection *connection,
	const gchar *sender_name,
	const gchar *object_path,
	const gchar *interface_name,
	const gchar *signal_name,
	GVariant *parameters,
	gpointer user_data)
{
	GVariant *var = NULL;
	const gchar *key = NULL;

	if (strncasecmp(object_path, "/hfp/org/bluez", 14))
		return;

	if (g_strcmp0(signal_name, "PropertyChanged"))
		return;

	g_variant_get(parameters, "(sv)", &key, &var);
	if (g_strcmp0(key, "BatteryChargeLevel"))
		return;

	g_variant_get(parameters, "(sv)", &key, &var);

	json_object *jresp = json_object_new_object();
	json_object_object_add(jresp, "battery level", json_object_new_int((int)g_variant_get_byte(var)));
	afb_event_push(battery_event, jresp);
}

static void ofono_networkregistration_signal_callback(
	GDBusConnection *connection,
	const gchar *sender_name,
	const gchar *object_path,
	const gchar *interface_name,
	const gchar *signal_name,
	GVariant *parameters,
	gpointer user_data)
{
	GVariant *var = NULL;
	const gchar *key = NULL;

	if (strncasecmp(object_path, "/hfp/org/bluez", 14))
		return;

	if (g_strcmp0(signal_name, "PropertyChanged"))
		return;

	g_variant_get(parameters, "(sv)", &key, &var);
	if (g_strcmp0(key, "Strength"))
		return;
	json_object *jresp = json_object_new_object();
	json_object_object_add(jresp, "signal level", json_object_new_int((int)g_variant_get_byte(var)));
	afb_event_push(signal_event, jresp);
}

static void ofono_event_init(void)
{
	GError *error = NULL;
	GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);

	g_dbus_connection_signal_subscribe(conn,
			NULL,	/* sender */
			OFONO_MODEM_INTERFACE,
			NULL,	/* member */
			NULL,	/* object path */
			NULL,	/* arg0 */
			G_DBUS_SIGNAL_FLAGS_NONE,
			ofono_modem_signal_callback,
			NULL,   /* user ptr */
			NULL);

	g_dbus_connection_signal_subscribe(conn,
			NULL,	/* sender */
			OFONO_HANDSFREE_INTERFACE,
			NULL,	/* member */
			NULL,	/* object path */
			NULL,	/* arg0 */
			G_DBUS_SIGNAL_FLAGS_NONE,
			ofono_handsfree_signal_callback,
			NULL,   /* user ptr */
			NULL);

	g_dbus_connection_signal_subscribe(conn,
			NULL,	/* sender */
			OFONO_NetworkRegistration_INTERFACE,
			NULL,	/* member */
			NULL,	/* object path */
			NULL,	/* arg0 */
			G_DBUS_SIGNAL_FLAGS_NONE,
			ofono_networkregistration_signal_callback,
			NULL,   /* user ptr */
			NULL);
}


static void ofono_hfp_init(void)
{
	ofono_manager_set_default_modem(NULL);

	if (ofono_manager_get_default_modem_valid()) {
		ofono_init_default_modem();
	}
}

static int ofono_init(afb_api_t api)
{
	pthread_t tid;
	int ret = 0;

	call_state_changed_event = afb_daemon_make_event("callStateChanged");
	dialing_call_event = afb_daemon_make_event("dialingCall");
	incoming_call_event = afb_daemon_make_event("incomingCall");
	terminated_call_event = afb_daemon_make_event("terminatedCall");
	online_event = afb_daemon_make_event("online");
	battery_event = afb_daemon_make_event("battery");
	signal_event = afb_daemon_make_event("signal");

	/* Start the main loop thread */
	pthread_create(&tid, NULL, main_loop_thread, NULL);

	ret = ofono_manager_init();
	if (ret == 0) {
		ofono_event_init();
		ofono_hfp_init();
	} else {
		AFB_ERROR("failed to initialize ofono manager");
	}

	return ret;
}

static const afb_verb_t verbs[]= {
	{
		.verb		= "dial",
		.callback	= dial,
	},
	{
		.verb		= "last_dial",
		.callback	= last_dial,
	},
	{
		.verb		= "send_tones",
		.callback	= send_tones,
	},
	{
		.verb		= "hangup",
		.callback	= hangup,
	},
	{
		.verb		= "answer",
		.callback	= answer,
	},
	{
		.verb		= "get_battery_level",
		.callback	= get_battery_level,
	},
	{
		.verb		= "get_network_registration",
		.callback	= get_network_registration,
	},
	{
		.verb		= "subscribe",
		.callback	= subscribe,
	},
	{
		.verb		= "unsubscribe",
		.callback	= unsubscribe,
	},
	{ }
};

static int init(afb_api_t api)
{
	AFB_NOTICE("Initializing telephony service");

	return ofono_init(api);
}

const afb_binding_t afbBindingV3 = {
	.api = "telephony",
	.verbs = verbs,
	.init = init,
};