telephony-ofono.c revision 9fa0976e28224d25c62d4a68fd02a6e5134447e6
1a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/*
2a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
3a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  BlueZ - Bluetooth protocol stack for Linux
4a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
5d3b613c392352f59fd6b18becf80e6504ad2423cMarcel Holtmann *  Copyright (C) 2009-2010  Intel Corporation
65592142cb9383df0556b27ac59e96547b380310bJohan Hedberg *  Copyright (C) 2006-2009  Nokia Corporation
79184e2eeb7b97371c6b83b747c8984e2340d2b47Marcel Holtmann *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
8a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
9a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
10a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  This program is free software; you can redistribute it and/or modify
11a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  it under the terms of the GNU General Public License as published by
12a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  the Free Software Foundation; either version 2 of the License, or
13a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  (at your option) any later version.
14a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
15a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  This program is distributed in the hope that it will be useful,
16a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  GNU General Public License for more details.
19a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
20a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  You should have received a copy of the GNU General Public License
21a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  along with this program; if not, write to the Free Software
22a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
24a34c155f55270b917795d003be24488f53d9b711Forrest Zhao */
25a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
26a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#ifdef HAVE_CONFIG_H
27a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <config.h>
28a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#endif
29a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
30a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <stdlib.h>
31a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <stdio.h>
32a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <string.h>
33a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <stdint.h>
34a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <glib.h>
35a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <dbus/dbus.h>
36a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include <gdbus.h>
37a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
38e891f7df6225c758da0d95f7554c6cc67f72f31eGustavo F. Padovan#include "log.h"
39a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include "telephony.h"
40a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
41a34c155f55270b917795d003be24488f53d9b711Forrest Zhaoenum net_registration_status {
42a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_HOME = 0x00,
43a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_ROAM,
44a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_NOSERV
45a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
46a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
47a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostruct voice_call {
48a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *obj_path;
49a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int status;
50a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	gboolean originating;
51a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *number;
528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	guint watch;
53a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
54a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
55a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic DBusConnection *connection = NULL;
56a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *modem_obj_path = NULL;
57a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *last_dialed_number = NULL;
58a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic GSList *calls = NULL;
59a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
60a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_BUS_NAME "org.ofono"
61a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_PATH "/"
62d89690657f2634ceb9d4b13257ccc7d60bc0cb5dForrest Zhao#define OFONO_MANAGER_INTERFACE "org.ofono.Manager"
63a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_NETWORKREG_INTERFACE "org.ofono.NetworkRegistration"
64a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VCMANAGER_INTERFACE "org.ofono.VoiceCallManager"
65a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VC_INTERFACE "org.ofono.VoiceCall"
66a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
678309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic guint registration_watch = 0;
688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic guint device_watch = 0;
69e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic guint modem_added_watch = 0;
70e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic guint modem_removed_watch = 0;
719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic guint call_added_watch = 0;
729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic guint call_removed_watch = 0;
738309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
74a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/* HAL battery namespace key values */
75a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_cur = -1;    /* "battery.charge_level.current" */
76a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_last = -1;   /* "battery.charge_level.last_full" */
77a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_design = -1; /* "battery.charge_level.design" */
78a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
79a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct {
80a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint8_t status;
81a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t signals_bar;
82a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *operator_name;
83a34c155f55270b917795d003be24488f53d9b711Forrest Zhao} net = {
84a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.status = NETWORK_REG_STATUS_NOSERV,
85a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.signals_bar = 0,
86a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.operator_name = NULL,
87a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
88a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
89a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic const char *chld_str = "0,1,1x,2,2x,3,4";
90a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *subscriber_number = NULL;
91a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
92a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean events_enabled = FALSE;
93a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
94a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct indicator ofono_indicators[] =
95a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
96884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "battchg",	"0-5",	5,	TRUE },
97884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "signal",	"0-5",	5,	TRUE },
98884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "service",	"0,1",	1,	TRUE },
99884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "call",	"0,1",	0,	TRUE },
100884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callsetup",	"0-3",	0,	TRUE },
101884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callheld",	"0-2",	0,	FALSE },
102884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "roam",	"0,1",	0,	TRUE },
103a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	{ NULL }
104a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
105a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
106a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc(const char *path)
107a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
108a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
109a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
110a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
111a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
112a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
113a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(vc->obj_path, path))
114a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
115a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
116a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
117a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
118a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
119a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
120a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc_with_status(int status)
121a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
122a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
123a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
124a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
125a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
126a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
127a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (vc->status == status)
128a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
129a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
130a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
131a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
132a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
133a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1341097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentzstatic int number_type(const char *number)
1351097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz{
1361097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (number == NULL)
1371097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		return NUMBER_TYPE_TELEPHONY;
1381097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1391097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (number[0] == '+' || strncmp(number, "00", 2) == 0)
1401097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		return NUMBER_TYPE_INTERNATIONAL;
1411097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1421097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	return NUMBER_TYPE_TELEPHONY;
1431097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz}
1441097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
145a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_connected(void *telephony_device)
146a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1471097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	struct voice_call *coming;
1481097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1498e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: device %p connected", telephony_device);
1501097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1511097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	coming = find_vc_with_status(CALL_STATUS_ALERTING);
1521097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (coming) {
1531097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		if (find_vc_with_status(CALL_STATUS_ACTIVE))
1541097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz			telephony_call_waiting_ind(coming->number,
1551097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz						number_type(coming->number));
1561097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		else
1571097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz			telephony_incoming_call_ind(coming->number,
1581097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz						number_type(coming->number));
1591097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	}
160a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
161a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
162a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_disconnected(void *telephony_device)
163a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1648e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: device %p disconnected", telephony_device);
165a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = FALSE;
166a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
167a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
168a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_event_reporting_req(void *telephony_device, int ind)
169a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
170a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = ind == 1 ? TRUE : FALSE;
171a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
172a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_event_reporting_rsp(telephony_device, CME_ERROR_NONE);
173a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
174a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
175a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_response_and_hold_req(void *telephony_device, int rh)
176a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
177b0d302d48976920cec7548ec5cc603f3896bbb8cJohan Hedberg	telephony_response_and_hold_rsp(telephony_device,
178b0d302d48976920cec7548ec5cc603f3896bbb8cJohan Hedberg						CME_ERROR_NOT_SUPPORTED);
179a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
180a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
181a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_last_dialed_number_req(void *telephony_device)
182a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1838e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: last dialed number request");
184a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
185a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (last_dialed_number)
186a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_req(telephony_device, last_dialed_number);
187a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
188a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_last_dialed_number_rsp(telephony_device,
189a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CME_ERROR_NOT_ALLOWED);
190a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
191a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
192a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int send_method_call(const char *dest, const char *path,
193a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                const char *interface, const char *method,
194a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                DBusPendingCallNotifyFunction cb,
195a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                void *user_data, int type, ...)
196a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
197a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *msg;
198a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusPendingCall *call;
199a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list args;
200a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
201a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	msg = dbus_message_new_method_call(dest, path, interface, method);
202a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!msg) {
203a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unable to allocate new D-Bus %s message", method);
204a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -ENOMEM;
205a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
206a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
207a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(args, type);
208a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
209a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_message_append_args_valist(msg, type, args)) {
210a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
211a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		va_end(args);
212a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
213a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
214a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
215a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(args);
216a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
217a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!cb) {
218a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_dbus_send_message(connection, msg);
219a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return 0;
220a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
221a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
222a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
223a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Sending %s failed", method);
224a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
225a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
226a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
227a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
228a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_pending_call_set_notify(call, cb, user_data, NULL);
229a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_pending_call_unref(call);
230a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(msg);
231a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
232a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return 0;
233a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
234a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
235a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_terminate_call_req(void *telephony_device)
236a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
237a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	struct voice_call *vc;
238a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
239a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
240e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	if ((vc = find_vc_with_status(CALL_STATUS_ACTIVE))) {
241e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_DIALING))) {
242e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_ALERTING))) {
243e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_INCOMING))) {
244e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	}
245a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
246a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
247a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("in telephony_terminate_call_req, no active call");
248a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_terminate_call_rsp(telephony_device,
249a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
250a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
251a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
252a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
253a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
254a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					OFONO_VC_INTERFACE,
255a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					"Hangup", NULL,
256a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					NULL, DBUS_TYPE_INVALID);
257a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
258a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
259a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
260a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
261a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
262a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
263a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
264a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
265a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
266a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
267a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_answer_call_req(void *telephony_device)
268a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
26916ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	struct voice_call *vc;
270a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
271a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
27216ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	vc = find_vc_with_status(CALL_STATUS_INCOMING);
27316ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	if (!vc)
27416ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz		vc = find_vc_with_status(CALL_STATUS_ALERTING);
27516ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz
27616ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	if (!vc)
27716ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz		vc = find_vc_with_status(CALL_STATUS_WAITING);
27816ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz
279a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
280a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
281a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
282a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
283a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
284a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
285a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
286a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VC_INTERFACE,
287a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"Answer", NULL,
288a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			NULL, DBUS_TYPE_INVALID);
289a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
290a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
291a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
292a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
293a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
294a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
295a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
296a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
297a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
298a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
299a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_dial_number_req(void *telephony_device, const char *number)
300a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
30183003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *clir;
302a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
303a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
3048e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: dial request to %s", number);
305a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
306c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	if (!modem_obj_path) {
307c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		telephony_dial_number_rsp(telephony_device,
308c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao					CME_ERROR_AG_FAILURE);
309c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		return;
310c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	}
311c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
312a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!strncmp(number, "*31#", 4)) {
313a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
31483003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "enabled";
315a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (!strncmp(number, "#31#", 4)) {
316a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
31783003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir =  "disabled";
31883003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	} else
31983003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "default";
320a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
321a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
322a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
323a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                        "Dial", NULL, NULL,
324a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &number,
325a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &clir,
326a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
327a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
328a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
329a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device,
330a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
331a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
332a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device, CME_ERROR_NONE);
333a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
334a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
335a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_transmit_dtmf_req(void *telephony_device, char tone)
336a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
337a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *tone_string;
338a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
339a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
3408e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: transmit dtmf: %c", tone);
341a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
342c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	if (!modem_obj_path) {
343c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device,
344c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao					CME_ERROR_AG_FAILURE);
345c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		return;
346c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	}
347c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
348a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	tone_string = g_strdup_printf("%c", tone);
349a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
350a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
351a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"SendTones", NULL, NULL,
352a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &tone_string,
353a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
354a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(tone_string);
355a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
356a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
357a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device,
358a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
359a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
360a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device, CME_ERROR_NONE);
361a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
362a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
363a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_subscriber_number_req(void *telephony_device)
364a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
3658e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: subscriber number request");
366a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
367a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (subscriber_number)
368a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_subscriber_number_ind(subscriber_number,
369a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						NUMBER_TYPE_TELEPHONY,
370a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						SUBSCRIBER_SERVICE_VOICE);
371a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_subscriber_number_rsp(telephony_device, CME_ERROR_NONE);
372a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
373a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
374a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_list_current_calls_req(void *telephony_device)
375a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
376a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
377a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int i;
378a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
3798e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: list current calls request");
380a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
381a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls, i = 1; l != NULL; l = l->next, i++) {
382a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
383a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int direction;
384a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
385a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		direction = vc->originating ?
386a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CALL_DIR_OUTGOING : CALL_DIR_INCOMING;
387a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
388a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_list_current_call_ind(i, direction, vc->status,
389a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CALL_MODE_VOICE, CALL_MULTIPARTY_NO,
390a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					vc->number, NUMBER_TYPE_TELEPHONY);
391a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
392a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_list_current_calls_rsp(telephony_device, CME_ERROR_NONE);
393a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
394a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
395a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_operator_selection_req(void *telephony_device)
396a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
3978e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: operator selection request");
398a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
399a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_ind(OPERATOR_MODE_AUTO,
400a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				net.operator_name ? net.operator_name : "");
401a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_rsp(telephony_device, CME_ERROR_NONE);
402a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
403a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
404a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_call_hold_req(void *telephony_device, const char *cmd)
405a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4068e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got call hold request %s", cmd);
407a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_call_hold_rsp(telephony_device, CME_ERROR_NONE);
408a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
409a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
410a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_nr_and_ec_req(void *telephony_device, gboolean enable)
411a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4128e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got %s NR and EC request",
413a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			enable ? "enable" : "disable");
414a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
415a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_nr_and_ec_rsp(telephony_device, CME_ERROR_NONE);
416a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
417a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
418a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_key_press_req(void *telephony_device, const char *keys)
419a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4208e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got key press request for %s", keys);
421a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_key_press_rsp(telephony_device, CME_ERROR_NONE);
422a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
423a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
4240218ab79ea881c593111b7165cd57337c711d2ecPeter Zotovvoid telephony_voice_dial_req(void *telephony_device, gboolean enable)
4250218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov{
4268e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got %s voice dial request",
4270218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov			enable ? "enable" : "disable");
4280218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov
4290218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov	telephony_voice_dial_rsp(telephony_device, CME_ERROR_NOT_SUPPORTED);
4300218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov}
4310218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov
432a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean iter_get_basic_args(DBusMessageIter *iter,
433a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					int first_arg_type, ...)
434a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
435a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int type;
436a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list ap;
437a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
438a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(ap, first_arg_type);
439a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
440a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (type = first_arg_type; type != DBUS_TYPE_INVALID;
441a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		type = va_arg(ap, int)) {
442a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		void *value = va_arg(ap, void *);
443a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int real_type = dbus_message_iter_get_arg_type(iter);
444a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
445a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (real_type != type) {
446a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("iter_get_basic_args: expected %c but got %c",
447a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				(char) type, (char) real_type);
448a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
449a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
450a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
451a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(iter, value);
452a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(iter);
453a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
454a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
455a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(ap);
456a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
457a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return type == DBUS_TYPE_INVALID ? TRUE : FALSE;
458a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
459a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
4609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_free(struct voice_call *vc)
4619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
4629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", vc->obj_path);
4639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, vc->watch);
4659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc->obj_path);
4669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc->number);
4679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc);
4689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
4699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vc_property_changed(DBusConnection *conn,
4719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					DBusMessage *msg, void *data)
4729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
4739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc = data;
4749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *obj_path = dbus_message_get_path(msg);
4759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, sub;
4769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *property, *state;
4779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("path %s", obj_path);
4799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
4819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
4839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in vc PropertyChanged signal");
4849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
4859fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
4869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4879fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &property);
4889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("property %s", property);
4899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4909fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_next(&iter);
4919fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
4929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_str_equal(property, "State")) {
4939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&sub, &state);
4949fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("State %s", state);
4959fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		if (g_str_equal(state, "disconnected")) {
4969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_ACTIVE)
4979fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_update_indicator(ofono_indicators,
4989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						"call", EV_CALL_INACTIVE);
4999fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else
5009fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_update_indicator(ofono_indicators,
5019fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_INACTIVE);
5029fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_INCOMING)
5039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_calling_stopped_ind();
5049fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			calls = g_slist_remove(calls, vc);
5059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			call_free(vc);
5069fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "active")) {
5079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5089fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							"call", EV_CALL_ACTIVE);
5099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5109fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							"callsetup",
5119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							EV_CALLSETUP_INACTIVE);
5129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_INCOMING)
5139fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_calling_stopped_ind();
5149fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_ACTIVE;
5159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "alerting")) {
5169fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5179fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_ALERTING);
5189fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_ALERTING;
5199fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->originating = TRUE;
5209fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "incoming")) {
5219fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			/* state change from waiting to incoming */
5229fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5239fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_INCOMING);
5249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_incoming_call_ind(vc->number,
5259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						NUMBER_TYPE_TELEPHONY);
5269fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_INCOMING;
5279fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->originating = FALSE;
5289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		}
5299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
5309fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5319fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return TRUE;
5329fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
5339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5349fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic struct voice_call *call_new(const char *path, DBusMessageIter *properties)
5359fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
5369fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
5379fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5389fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
5399fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5409fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = g_new0(struct voice_call, 1);
5419fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc->obj_path = g_strdup(path);
5429fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc->watch = g_dbus_add_signal_watch(connection, NULL, path,
5439fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					OFONO_VC_INTERFACE, "PropertyChanged",
5449fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					handle_vc_property_changed, vc, NULL);
5459fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5469fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(properties)
5479fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						== DBUS_TYPE_DICT_ENTRY) {
5489fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBusMessageIter entry, value;
5499fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		const char *property, *cli, *state;
5509fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5519fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(properties, &entry);
5529fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&entry, &property);
5539fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5549fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
5559fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
5569fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5579fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		if (g_str_equal(property, "LineIdentification")) {
5589fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			dbus_message_iter_get_basic(&value, &cli);
5599fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			DBG("cli %s", cli);
5609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->number = g_strdup(cli);
5619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(property, "State")) {
5629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			dbus_message_iter_get_basic(&value, &state);
5639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			DBG("state %s", state);
5649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (g_str_equal(state, "incoming"))
5659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_INCOMING;
5669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "dialing"))
5679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_DIALING;
5689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "alerting"))
5699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_ALERTING;
5709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "waiting"))
5719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_WAITING;
5729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		}
5739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(properties);
5759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
5769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	switch (vc->status) {
5789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_INCOMING:
5799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_INCOMING");
5809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = FALSE;
5819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_INCOMING);
5839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_incoming_call_ind(vc->number, NUMBER_TYPE_TELEPHONY);
5849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
5859fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_DIALING:
5869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_DIALING");
5879fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = TRUE;
5889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		g_free(last_dialed_number);
5899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		last_dialed_number = g_strdup(vc->number);
5909fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5919fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_OUTGOING);
5929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
5939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_ALERTING:
5949fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_ALERTING");
5959fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = TRUE;
5969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		g_free(last_dialed_number);
5979fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		last_dialed_number = g_strdup(vc->number);
5989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5999fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_ALERTING);
6009fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
6019fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_WAITING:
6029fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_WAITING");
6039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = FALSE;
6049fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
6059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_INCOMING);
6069fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_call_waiting_ind(vc->number, NUMBER_TYPE_TELEPHONY);
6079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
6089fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6109fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return vc;
6119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6139fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_added(const char *path, DBusMessageIter *properties)
6149fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
6159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
6169fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6179fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
6189fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6199fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = find_vc(path);
6209fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (vc)
6219fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return;
6229fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6239fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = call_new(path, properties);
6249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	calls = g_slist_prepend(calls, vc);
6259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6269fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6279fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void get_calls_reply(DBusPendingCall *call, void *user_data)
6289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
6299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusError err;
6309fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessage *reply;
6319fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, entry;
6329fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("");
6349fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	reply = dbus_pending_call_steal_reply(call);
6359fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6369fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_error_init(&err);
6379fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_set_error_from_message(&err, reply)) {
6389fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("ofono replied with an error: %s, %s",
6399fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				err.name, err.message);
6409fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_error_free(&err);
6419fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
6429fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6439fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6449fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(reply, &iter);
6459fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6469fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
6479fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature");
6489fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
6499fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6509fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6519fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &entry);
6529fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6539fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(&entry)
6549fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						== DBUS_TYPE_STRUCT) {
6559fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		const char *path;
6569fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBusMessageIter value, properties;
6579fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6589fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
6599fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&value, &path);
6609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&value);
6629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&value, &properties);
6639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		call_added(path, &properties);
6659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
6679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzdone:
6709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_unref(reply);
6719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
673e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void handle_network_property(const char *property, DBusMessageIter *variant)
674a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
67583003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *status, *operator;
676a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	unsigned int signals_bar;
677a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
678a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (g_str_equal(property, "Status")) {
679e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &status);
6808e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("Status is %s", status);
681a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(status, "registered")) {
682a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_HOME;
683a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
684a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
685a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
686a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
687a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(status, "roaming")) {
688a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_ROAM;
689a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
690a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_ACTIVE);
691a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
692a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
693a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else {
694a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_NOSERV;
695a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
696a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
697a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
698a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_NONE);
699a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
700e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	} else if (g_str_equal(property, "Name")) {
701e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &operator);
7028e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("Operator is %s", operator);
703a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_free(net.operator_name);
704a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.operator_name = g_strdup(operator);
705a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (g_str_equal(property, "SignalStrength")) {
706e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &signals_bar);
7078e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("SignalStrength is %d", signals_bar);
708a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.signals_bar = signals_bar;
709a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "signal",
710a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						(signals_bar + 20) / 21);
711a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
712a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
713a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
714e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic int parse_network_properties(DBusMessageIter *properties)
715a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
716a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t features = AG_FEATURE_EC_ANDOR_NR |
717a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_REJECT_A_CALL |
718a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_ENHANCED_CALL_STATUS |
719a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
720a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
721e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(properties)
722e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						== DBUS_TYPE_DICT_ENTRY) {
723e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		const char *key;
724e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		DBusMessageIter value, entry;
725e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
726e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_recurse(properties, &entry);
727e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&entry, &key);
728e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
729e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
730e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
731e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
732e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		handle_network_property(key, &value);
733e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
734e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_next(properties);
735e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
736e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
737e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	telephony_ready_ind(features, ofono_indicators, BTRH_NOT_SUPPORTED,
738e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz								chld_str);
739e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
740e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return 0;
741e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
742e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
743e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void get_properties_reply(DBusPendingCall *call, void *user_data)
744e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
745e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusError err;
746e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessage *reply;
747e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
748e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	int ret = 0;
749e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
750e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("");
751a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
752a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
753a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
754a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
755a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
756a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
757a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
758a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
759a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
760a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
761a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
762a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
763a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
764e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
765a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
766a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
767a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
768e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
769a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
770e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	ret = parse_network_properties(&properties);
7719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (ret < 0) {
772e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unable to parse %s.GetProperty reply",
773e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_NETWORKREG_INTERFACE);
7749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
7759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
7769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
7779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
7789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				OFONO_VCMANAGER_INTERFACE, "GetCalls",
7799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				get_calls_reply, NULL, DBUS_TYPE_INVALID);
7809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (ret < 0)
7819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unable to send %s.GetCalls",
7829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_VCMANAGER_INTERFACE);
783a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
784a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
785a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
786a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
787a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
788e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void modem_added(const char *path)
789a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
790e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	int ret;
791c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
792e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("%s", path);
793e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
794e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path != NULL)
795e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return;
796e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
797e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_obj_path = g_strdup(path);
798e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
799e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	ret = send_method_call(OFONO_BUS_NAME, path,
800e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				OFONO_NETWORKREG_INTERFACE, "GetProperties",
801e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				get_properties_reply, NULL, DBUS_TYPE_INVALID);
802e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (ret < 0)
803e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unable to send %s.GetProperties",
804e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_NETWORKREG_INTERFACE);
805a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
806a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
807e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void get_modems_reply(DBusPendingCall *call, void *user_data)
808a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
809a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
810a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
811e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, entry;
812e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
813a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8148e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("list_modem_reply is called\n");
815a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
816a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
817a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
818a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
819a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
820a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
821a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
822a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
823a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
824a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
825a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
826a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
827a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
828e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
829a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
830a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
831a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
832e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &entry);
833a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
834e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_OBJECT_PATH) {
835e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
836a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
837a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
838a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
839e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&entry, &path);
840a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
841e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_added(path);
842a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
843a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
844a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
845a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
846a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
847e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_network_property_changed(DBusConnection *conn,
8488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
849a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
850e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, variant;
851a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *property;
852a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
853a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
854a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
855a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
856a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in networkregistration"
857a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" PropertyChanged signal");
8588309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
859a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
860a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &property);
8618e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("in handle_registration_property_changed(),"
862a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" the property is %s", property);
863a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
864a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
865e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &variant);
866a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
867e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	handle_network_property(property, &variant);
8688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8698309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
870a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
871a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vcmanager_call_added(DBusConnection *conn,
8739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
874a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
8759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
8769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *path = dbus_message_get_path(msg);
877a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	/* Ignore call if modem path doesn't math */
8799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
8809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
881a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
883a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter)
8859fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						!= DBUS_TYPE_OBJECT_PATH) {
8869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
8879fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
8889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_member(msg));
8899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
890a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
891a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &path);
8939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_next(&iter);
8949fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
895a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_added(path, &properties);
897a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return TRUE;
899a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
900a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9019fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_removed(const char *path)
902a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
9039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
904a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
906a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = find_vc(path);
9089fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (vc == NULL)
9099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return;
9108309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	calls = g_slist_remove(calls, vc);
9129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_free(vc);
9138309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz}
9148309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vcmanager_call_removed(DBusConnection *conn,
9168309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
9178309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz{
9189fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *path = dbus_message_get_path(msg);
9198309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9209fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	/* Ignore call if modem path doesn't math */
9219fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
9228309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
9238309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (!dbus_message_get_args(msg, NULL,
9259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				DBUS_TYPE_OBJECT_PATH, &path,
9269fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				DBUS_TYPE_INVALID)) {
9279fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
9289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
9299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_member(msg));
9308309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
9318309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
9328309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_removed(path);
9348309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9358309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
936a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
937a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
938e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_manager_modem_added(DBusConnection *conn,
939e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
940e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
941e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
942e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
943e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
944e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path != NULL)
945e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
946e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
947e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
948e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
949e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter)
950e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						!= DBUS_TYPE_OBJECT_PATH) {
951e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
952e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
953e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_member(msg));
954e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
955e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
956e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
957e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &path);
958e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
959e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
960e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_added(path);
961e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
962e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return TRUE;
963e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
964e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
965e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void modem_removed(const char *path)
966e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
967e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
968e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return;
969e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
970e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("%s", path);
971e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
9729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_slist_foreach(calls, (GFunc) call_free, NULL);
973e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_slist_free(calls);
974e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	calls = NULL;
975e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
976e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_free(net.operator_name);
977e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	net.operator_name = NULL;
978e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
979e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_free(modem_obj_path);
980e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_obj_path = NULL;
981e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
982e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
983e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_manager_modem_removed(DBusConnection *conn,
984e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
985e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
986e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
987e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
988e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (!dbus_message_get_args(msg, NULL,
989e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				DBUS_TYPE_OBJECT_PATH, &path,
990e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				DBUS_TYPE_INVALID)) {
991e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
992e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
993e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_member(msg));
994e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
995e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
996e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
997e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_removed(path);
998e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
999e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return TRUE;
1000e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
1001e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1002a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_battery_level_reply(DBusPendingCall *call, void *user_data)
1003a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1004a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
1005a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
1006a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t level;
1007a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int *value = user_data;
1008a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1009a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
1010a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1011a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
1012a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
1013a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("hald replied with an error: %s, %s",
1014a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
1015a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
1016a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
1017a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1018a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1019463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	dbus_error_init(&err);
1020463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	if (dbus_message_get_args(reply, &err,
1021a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INT32, &level,
1022463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz				DBUS_TYPE_INVALID) == FALSE) {
1023463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		error("Unable to parse GetPropertyInteger reply: %s, %s",
1024463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz							err.name, err.message);
1025463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		dbus_error_free(&err);
1026463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		goto done;
1027463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	}
1028a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1029a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	*value = (int) level;
1030a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1031a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (value == &battchg_last)
10328e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.last_full"
1033a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1034a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else if (value == &battchg_design)
10358e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.design"
1036a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1037a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
10388e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.current"
1039a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1040a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1041a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if ((battchg_design > 0 || battchg_last > 0) && battchg_cur >= 0) {
10429ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		int new, max;
1043a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1044a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (battchg_last > 0)
1045a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_last;
1046a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else
1047a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_design;
1048a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1049a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		new = battchg_cur * 5 / max;
1050a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10519ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		telephony_update_indicator(ofono_indicators, "battchg", new);
1052a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1053a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
1054a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
1055a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1056a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1057a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_get_integer(const char *path, const char *key, void *user_data)
1058a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1059a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	send_method_call("org.freedesktop.Hal", path,
1060a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"org.freedesktop.Hal.Device",
1061a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"GetPropertyInteger",
1062a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_battery_level_reply, user_data,
1063a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &key,
1064a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
1065a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1066a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10678309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_hal_property_modified(DBusConnection *conn,
10688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
1069a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1070a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *path;
1071a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, array;
1072a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t num_changes;
1073a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1074a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	path = dbus_message_get_path(msg);
1075a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1076a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
1077a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1078a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32) {
1079a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
10808309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
1081a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1082a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1083a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &num_changes);
1084a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
1085a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1086a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
1087a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
10888309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
1089a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1090a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1091a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &array);
1092a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1093a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
1094a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		DBusMessageIter prop;
1095a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		const char *name;
1096a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_bool_t added, removed;
1097a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1098a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&array, &prop);
1099a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1100a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (!iter_get_basic_args(&prop,
1101a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_STRING, &name,
1102a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &added,
1103a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &removed,
1104a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_INVALID)) {
1105a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("Invalid hal PropertyModified parameters");
1106a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
1107a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
1108a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1109a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(name, "battery.charge_level.last_full"))
1110a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_last);
1111a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.current"))
1112a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_cur);
1113a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.design"))
1114a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_design);
1115a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1116a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&array);
1117a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
11188309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11198309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
1120a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1121a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
11228309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic void hal_find_device_reply(DBusPendingCall *call, void *user_data)
1123a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
11248309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessage *reply;
11258309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusError err;
11268309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessageIter iter, sub;
11278309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	int type;
11288309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	const char *path;
11298309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11308e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("begin of hal_find_device_reply()");
11318309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	reply = dbus_pending_call_steal_reply(call);
11328309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11338309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_error_init(&err);
11348309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11358309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_set_error_from_message(&err, reply)) {
11368309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("hald replied with an error: %s, %s",
11378309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				err.name, err.message);
11388309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		dbus_error_free(&err);
11398309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11408309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11418309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11428309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_init(reply, &iter);
11438309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11448309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
11458309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("Unexpected signature in hal_find_device_reply()");
11468309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11478309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11498309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
11508309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11518309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	type = dbus_message_iter_get_arg_type(&sub);
11528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11538309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (type != DBUS_TYPE_OBJECT_PATH && type != DBUS_TYPE_STRING) {
11548309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("No hal device with battery capability found");
11558309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11568309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11578309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11588309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_get_basic(&sub, &path);
11598309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11608e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: found battery device at %s", path);
11618309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11628309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	device_watch = g_dbus_add_signal_watch(connection, NULL, path,
11638309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"org.freedesktop.Hal.Device",
11648309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"PropertyModified",
11658309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					handle_hal_property_modified,
11668309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					NULL, NULL);
11678309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.last_full", &battchg_last);
11698309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.current", &battchg_cur);
11708309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.design", &battchg_design);
11718309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzdone:
11728309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_unref(reply);
1173a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1174a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1175a34c155f55270b917795d003be24488f53d9b711Forrest Zhaoint telephony_init(void)
1176a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1177a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *battery_cap = "battery";
1178a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
1179a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1180a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1181a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1182a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1183e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	registration_watch = g_dbus_add_signal_watch(connection,
1184e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_BUS_NAME, NULL,
1185e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_NETWORKREG_INTERFACE,
1186e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						"PropertyChanged",
1187e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						handle_network_property_changed,
1188e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						NULL, NULL);
1189e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1190e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_added_watch = g_dbus_add_signal_watch(connection,
1191e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_BUS_NAME, NULL,
1192e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_MANAGER_INTERFACE,
1193e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						"ModemAdded",
1194e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						handle_manager_modem_added,
1195e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						NULL, NULL);
1196e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1197e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_removed_watch = g_dbus_add_signal_watch(connection,
1198e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_BUS_NAME, NULL,
1199e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_MANAGER_INTERFACE,
1200e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						"ModemRemoved",
1201e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						handle_manager_modem_removed,
1202e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						NULL, NULL);
12039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_added_watch = g_dbus_add_signal_watch(connection,
12049fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_BUS_NAME, NULL,
12059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_VCMANAGER_INTERFACE,
12069fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						"CallAdded",
12079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						handle_vcmanager_call_added,
12089fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						NULL, NULL);
12099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
12109fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_removed_watch = g_dbus_add_signal_watch(connection,
12119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_BUS_NAME, NULL,
12129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_VCMANAGER_INTERFACE,
12139fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						"CallRemoved",
12149fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						handle_vcmanager_call_removed,
12159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						NULL, NULL);
1216a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1217a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, OFONO_PATH,
1218e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				OFONO_MANAGER_INTERFACE, "GetModems",
1219e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				get_modems_reply, NULL, DBUS_TYPE_INVALID);
1220a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1221a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1222a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1223a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call("org.freedesktop.Hal",
1224a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"/org/freedesktop/Hal/Manager",
1225a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"org.freedesktop.Hal.Manager",
1226a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"FindDeviceByCapability",
1227a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				hal_find_device_reply, NULL,
1228a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_STRING, &battery_cap,
1229a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INVALID);
1230a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1231a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1232a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
12338e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony_init() successfully");
1234a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1235a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return ret;
1236a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1237a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1238a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_exit(void)
1239a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1240e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("");
1241a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1242a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(last_dialed_number);
1243e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	last_dialed_number = NULL;
1244a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1245e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path)
1246e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		modem_removed(modem_obj_path);
1247a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
12488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, registration_watch);
1249e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, modem_added_watch);
1250e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, modem_removed_watch);
12519fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, call_added_watch);
12529fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, call_removed_watch);
12538309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, device_watch);
1254a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1255a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_connection_unref(connection);
1256a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = NULL;
1257401be3748717fae613041592778a3a7160fb763cJohan Hedberg
1258401be3748717fae613041592778a3a7160fb763cJohan Hedberg	telephony_deinit();
1259a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1260