telephony-ofono.c revision 9946e8636c3361dc769c5f8a463b74158018d3dd
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;
599946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentzstatic GSList *watches = NULL;
60c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentzstatic GSList *pending = NULL;
61a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
62a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_BUS_NAME "org.ofono"
63a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_PATH "/"
64d89690657f2634ceb9d4b13257ccc7d60bc0cb5dForrest Zhao#define OFONO_MANAGER_INTERFACE "org.ofono.Manager"
65a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_NETWORKREG_INTERFACE "org.ofono.NetworkRegistration"
66a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VCMANAGER_INTERFACE "org.ofono.VoiceCallManager"
67a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VC_INTERFACE "org.ofono.VoiceCall"
68a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
69a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/* HAL battery namespace key values */
70a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_cur = -1;    /* "battery.charge_level.current" */
71a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_last = -1;   /* "battery.charge_level.last_full" */
72a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_design = -1; /* "battery.charge_level.design" */
73a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
74a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct {
75a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint8_t status;
76a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t signals_bar;
77a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *operator_name;
78a34c155f55270b917795d003be24488f53d9b711Forrest Zhao} net = {
79a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.status = NETWORK_REG_STATUS_NOSERV,
80a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.signals_bar = 0,
81a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.operator_name = NULL,
82a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
83a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
84a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic const char *chld_str = "0,1,1x,2,2x,3,4";
85a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *subscriber_number = NULL;
86a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
87a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean events_enabled = FALSE;
88a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
89a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct indicator ofono_indicators[] =
90a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
91884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "battchg",	"0-5",	5,	TRUE },
92884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "signal",	"0-5",	5,	TRUE },
93884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "service",	"0,1",	1,	TRUE },
94884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "call",	"0,1",	0,	TRUE },
95884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callsetup",	"0-3",	0,	TRUE },
96884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callheld",	"0-2",	0,	FALSE },
97884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "roam",	"0,1",	0,	TRUE },
98a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	{ NULL }
99a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
100a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
101a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc(const char *path)
102a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
103a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
104a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
105a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
106a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
107a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
108a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(vc->obj_path, path))
109a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
110a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
111a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
112a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
113a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
114a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
115a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc_with_status(int status)
116a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
117a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
118a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
119a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
120a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
121a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
122a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (vc->status == status)
123a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
124a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
125a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
126a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
127a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
128a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1291097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentzstatic int number_type(const char *number)
1301097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz{
1311097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (number == NULL)
1321097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		return NUMBER_TYPE_TELEPHONY;
1331097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1341097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (number[0] == '+' || strncmp(number, "00", 2) == 0)
1351097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		return NUMBER_TYPE_INTERNATIONAL;
1361097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1371097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	return NUMBER_TYPE_TELEPHONY;
1381097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz}
1391097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
140a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_connected(void *telephony_device)
141a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1421097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	struct voice_call *coming;
1431097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1448e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: device %p connected", telephony_device);
1451097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz
1461097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	coming = find_vc_with_status(CALL_STATUS_ALERTING);
1471097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	if (coming) {
1481097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		if (find_vc_with_status(CALL_STATUS_ACTIVE))
1491097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz			telephony_call_waiting_ind(coming->number,
1501097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz						number_type(coming->number));
1511097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz		else
1521097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz			telephony_incoming_call_ind(coming->number,
1531097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz						number_type(coming->number));
1541097cfd46e906ddfd8a827ce683c1eddf2d98499Luiz Augusto von Dentz	}
155a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
156a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
157a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_disconnected(void *telephony_device)
158a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1598e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: device %p disconnected", telephony_device);
160a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = FALSE;
161a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
162a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
163a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_event_reporting_req(void *telephony_device, int ind)
164a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
165a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = ind == 1 ? TRUE : FALSE;
166a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
167a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_event_reporting_rsp(telephony_device, CME_ERROR_NONE);
168a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
169a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
170a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_response_and_hold_req(void *telephony_device, int rh)
171a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
172b0d302d48976920cec7548ec5cc603f3896bbb8cJohan Hedberg	telephony_response_and_hold_rsp(telephony_device,
173b0d302d48976920cec7548ec5cc603f3896bbb8cJohan Hedberg						CME_ERROR_NOT_SUPPORTED);
174a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
175a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
176a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_last_dialed_number_req(void *telephony_device)
177a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1788e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: last dialed number request");
179a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
180a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (last_dialed_number)
181a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_req(telephony_device, last_dialed_number);
182a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
183a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_last_dialed_number_rsp(telephony_device,
184a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CME_ERROR_NOT_ALLOWED);
185a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
186a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
187a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int send_method_call(const char *dest, const char *path,
188a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                const char *interface, const char *method,
189a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                DBusPendingCallNotifyFunction cb,
190a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                void *user_data, int type, ...)
191a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
192a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *msg;
193a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusPendingCall *call;
194a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list args;
195a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
196a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	msg = dbus_message_new_method_call(dest, path, interface, method);
197a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!msg) {
198a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unable to allocate new D-Bus %s message", method);
199a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -ENOMEM;
200a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
201a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
202a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(args, type);
203a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
204a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_message_append_args_valist(msg, type, args)) {
205a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
206a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		va_end(args);
207a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
208a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
209a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
210a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(args);
211a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
212a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!cb) {
213a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_dbus_send_message(connection, msg);
214a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return 0;
215a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
216a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
217a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
218a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Sending %s failed", method);
219a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
220a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
221a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
222a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
223a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_pending_call_set_notify(call, cb, user_data, NULL);
224c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	pending = g_slist_prepend(pending, call);
225a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(msg);
226a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
227a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return 0;
228a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
229a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
230a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_terminate_call_req(void *telephony_device)
231a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
232a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	struct voice_call *vc;
233a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
234a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
235e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	if ((vc = find_vc_with_status(CALL_STATUS_ACTIVE))) {
236e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_DIALING))) {
237e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_ALERTING))) {
238e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_INCOMING))) {
239e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	}
240a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
241a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
242a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("in telephony_terminate_call_req, no active call");
243a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_terminate_call_rsp(telephony_device,
244a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
245a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
246a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
247a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
248a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
249a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					OFONO_VC_INTERFACE,
250a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					"Hangup", NULL,
251a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					NULL, DBUS_TYPE_INVALID);
252a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
253a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
254a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
255a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
256a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
257a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
258a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
259a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
260a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
261a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
262a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_answer_call_req(void *telephony_device)
263a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
26416ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	struct voice_call *vc;
265a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
266a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
26716ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	vc = find_vc_with_status(CALL_STATUS_INCOMING);
26816ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	if (!vc)
26916ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz		vc = find_vc_with_status(CALL_STATUS_ALERTING);
27016ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz
27116ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz	if (!vc)
27216ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz		vc = find_vc_with_status(CALL_STATUS_WAITING);
27316ec168ac7fdd18cb2cbc7b3be7df349fd84c41cLuiz Augusto von Dentz
274a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
275a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
276a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
277a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
278a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
279a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
280a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
281a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VC_INTERFACE,
282a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"Answer", NULL,
283a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			NULL, DBUS_TYPE_INVALID);
284a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
285a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
286a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
287a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
288a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
289a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
290a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
291a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
292a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
293a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
294a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_dial_number_req(void *telephony_device, const char *number)
295a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
29683003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *clir;
297a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
298a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
2998e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: dial request to %s", number);
300a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
301c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	if (!modem_obj_path) {
302c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		telephony_dial_number_rsp(telephony_device,
303c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao					CME_ERROR_AG_FAILURE);
304c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		return;
305c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	}
306c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
307a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!strncmp(number, "*31#", 4)) {
308a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
30983003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "enabled";
310a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (!strncmp(number, "#31#", 4)) {
311a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
31283003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir =  "disabled";
31383003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	} else
31483003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "default";
315a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
316a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
317a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
318a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                        "Dial", NULL, NULL,
319a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &number,
320a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &clir,
321a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
322a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
323a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
324a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device,
325a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
326a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
327a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device, CME_ERROR_NONE);
328a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
329a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
330a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_transmit_dtmf_req(void *telephony_device, char tone)
331a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
332a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *tone_string;
333a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
334a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
3358e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: transmit dtmf: %c", tone);
336a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
337c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	if (!modem_obj_path) {
338c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device,
339c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao					CME_ERROR_AG_FAILURE);
340c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao		return;
341c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao	}
342c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
343a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	tone_string = g_strdup_printf("%c", tone);
344a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
345a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
346a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"SendTones", NULL, NULL,
347a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &tone_string,
348a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
349a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(tone_string);
350a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
351a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
352a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device,
353a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
354a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
355a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device, CME_ERROR_NONE);
356a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
357a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
358a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_subscriber_number_req(void *telephony_device)
359a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
3608e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: subscriber number request");
361a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
362a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (subscriber_number)
363a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_subscriber_number_ind(subscriber_number,
364a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						NUMBER_TYPE_TELEPHONY,
365a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						SUBSCRIBER_SERVICE_VOICE);
366a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_subscriber_number_rsp(telephony_device, CME_ERROR_NONE);
367a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
368a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
369a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_list_current_calls_req(void *telephony_device)
370a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
371a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
372a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int i;
373a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
3748e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: list current calls request");
375a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
376a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls, i = 1; l != NULL; l = l->next, i++) {
377a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
378a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int direction;
379a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
380a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		direction = vc->originating ?
381a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CALL_DIR_OUTGOING : CALL_DIR_INCOMING;
382a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
383a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_list_current_call_ind(i, direction, vc->status,
384a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CALL_MODE_VOICE, CALL_MULTIPARTY_NO,
385a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					vc->number, NUMBER_TYPE_TELEPHONY);
386a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
387a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_list_current_calls_rsp(telephony_device, CME_ERROR_NONE);
388a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
389a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
390a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_operator_selection_req(void *telephony_device)
391a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
3928e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: operator selection request");
393a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
394a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_ind(OPERATOR_MODE_AUTO,
395a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				net.operator_name ? net.operator_name : "");
396a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_rsp(telephony_device, CME_ERROR_NONE);
397a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
398a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
399a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_call_hold_req(void *telephony_device, const char *cmd)
400a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4018e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got call hold request %s", cmd);
402a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_call_hold_rsp(telephony_device, CME_ERROR_NONE);
403a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
404a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
405a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_nr_and_ec_req(void *telephony_device, gboolean enable)
406a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4078e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got %s NR and EC request",
408a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			enable ? "enable" : "disable");
409a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
410a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_nr_and_ec_rsp(telephony_device, CME_ERROR_NONE);
411a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
412a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
413a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_key_press_req(void *telephony_device, const char *keys)
414a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
4158e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got key press request for %s", keys);
416a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_key_press_rsp(telephony_device, CME_ERROR_NONE);
417a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
418a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
4190218ab79ea881c593111b7165cd57337c711d2ecPeter Zotovvoid telephony_voice_dial_req(void *telephony_device, gboolean enable)
4200218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov{
4218e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: got %s voice dial request",
4220218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov			enable ? "enable" : "disable");
4230218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov
4240218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov	telephony_voice_dial_rsp(telephony_device, CME_ERROR_NOT_SUPPORTED);
4250218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov}
4260218ab79ea881c593111b7165cd57337c711d2ecPeter Zotov
427a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean iter_get_basic_args(DBusMessageIter *iter,
428a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					int first_arg_type, ...)
429a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
430a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int type;
431a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list ap;
432a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
433a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(ap, first_arg_type);
434a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
435a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (type = first_arg_type; type != DBUS_TYPE_INVALID;
436a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		type = va_arg(ap, int)) {
437a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		void *value = va_arg(ap, void *);
438a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int real_type = dbus_message_iter_get_arg_type(iter);
439a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
440a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (real_type != type) {
441a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("iter_get_basic_args: expected %c but got %c",
442a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				(char) type, (char) real_type);
443a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
444a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
445a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
446a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(iter, value);
447a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(iter);
448a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
449a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
450a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(ap);
451a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
452a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return type == DBUS_TYPE_INVALID ? TRUE : FALSE;
453a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
454a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
4559fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_free(struct voice_call *vc)
4569fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
4579fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", vc->obj_path);
4589fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4599fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_dbus_remove_watch(connection, vc->watch);
4609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc->obj_path);
4619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc->number);
4629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_free(vc);
4639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
4649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vc_property_changed(DBusConnection *conn,
4669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					DBusMessage *msg, void *data)
4679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
4689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc = data;
4699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *obj_path = dbus_message_get_path(msg);
4709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, sub;
4719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *property, *state;
4729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("path %s", obj_path);
4749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
4769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
4789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in vc PropertyChanged signal");
4799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
4809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
4819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &property);
4839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("property %s", property);
4849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
4859fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_next(&iter);
4869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
4879fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_str_equal(property, "State")) {
4889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&sub, &state);
4899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("State %s", state);
4909fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		if (g_str_equal(state, "disconnected")) {
4919fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_ACTIVE)
4929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_update_indicator(ofono_indicators,
4939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						"call", EV_CALL_INACTIVE);
4949fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else
4959fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_update_indicator(ofono_indicators,
4969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_INACTIVE);
4979fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_INCOMING)
4989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_calling_stopped_ind();
4999fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			calls = g_slist_remove(calls, vc);
5009fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			call_free(vc);
5019fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "active")) {
5029fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							"call", EV_CALL_ACTIVE);
5049fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							"callsetup",
5069fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz							EV_CALLSETUP_INACTIVE);
5079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (vc->status == CALL_STATUS_INCOMING)
5089fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				telephony_calling_stopped_ind();
5099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_ACTIVE;
5109fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "alerting")) {
5119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_ALERTING);
5139fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_ALERTING;
5149fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->originating = TRUE;
5159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(state, "incoming")) {
5169fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			/* state change from waiting to incoming */
5179fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_update_indicator(ofono_indicators,
5189fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					"callsetup", EV_CALLSETUP_INCOMING);
5199fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			telephony_incoming_call_ind(vc->number,
5209fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						NUMBER_TYPE_TELEPHONY);
5219fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->status = CALL_STATUS_INCOMING;
5229fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->originating = FALSE;
5239fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		}
5249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
5259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5269fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return TRUE;
5279fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
5289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic struct voice_call *call_new(const char *path, DBusMessageIter *properties)
5309fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
5319fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
5329fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
5349fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5359fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = g_new0(struct voice_call, 1);
5369fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc->obj_path = g_strdup(path);
5379fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc->watch = g_dbus_add_signal_watch(connection, NULL, path,
5389fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					OFONO_VC_INTERFACE, "PropertyChanged",
5399fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					handle_vc_property_changed, vc, NULL);
5409fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5419fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(properties)
5429fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						== DBUS_TYPE_DICT_ENTRY) {
5439fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBusMessageIter entry, value;
5449fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		const char *property, *cli, *state;
5459fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5469fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(properties, &entry);
5479fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&entry, &property);
5489fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5499fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
5509fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
5519fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5529fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		if (g_str_equal(property, "LineIdentification")) {
5539fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			dbus_message_iter_get_basic(&value, &cli);
5549fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			DBG("cli %s", cli);
5559fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			vc->number = g_strdup(cli);
5569fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		} else if (g_str_equal(property, "State")) {
5579fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			dbus_message_iter_get_basic(&value, &state);
5589fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			DBG("state %s", state);
5599fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			if (g_str_equal(state, "incoming"))
5609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_INCOMING;
5619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "dialing"))
5629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_DIALING;
5639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "alerting"))
5649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_ALERTING;
5659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz			else if (g_str_equal(state, "waiting"))
5669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				vc->status = CALL_STATUS_WAITING;
5679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		}
5689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(properties);
5709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
5719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
5729fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	switch (vc->status) {
5739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_INCOMING:
5749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_INCOMING");
5759fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = FALSE;
5769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_INCOMING);
5789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_incoming_call_ind(vc->number, NUMBER_TYPE_TELEPHONY);
5799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
5809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_DIALING:
5819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_DIALING");
5829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = TRUE;
5839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		g_free(last_dialed_number);
5849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		last_dialed_number = g_strdup(vc->number);
5859fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_OUTGOING);
5879fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
5889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_ALERTING:
5899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_ALERTING");
5909fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = TRUE;
5919fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		g_free(last_dialed_number);
5929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		last_dialed_number = g_strdup(vc->number);
5939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
5949fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_ALERTING);
5959fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
5969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	case CALL_STATUS_WAITING:
5979fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBG("CALL_STATUS_WAITING");
5989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		vc->originating = FALSE;
5999fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_update_indicator(ofono_indicators, "callsetup",
6009fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					EV_CALLSETUP_INCOMING);
6019fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		telephony_call_waiting_ind(vc->number, NUMBER_TYPE_TELEPHONY);
6029fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		break;
6039fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6049fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return vc;
6069fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
608c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentzstatic void remove_pending(DBusPendingCall *call)
609c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz{
610c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	pending = g_slist_remove(pending, call);
611c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	dbus_pending_call_unref(call);
612c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz}
613c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz
6149fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_added(const char *path, DBusMessageIter *properties)
6159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
6169fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
6179fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6189fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
6199fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6209fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = find_vc(path);
6219fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (vc)
6229fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return;
6239fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = call_new(path, properties);
6259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	calls = g_slist_prepend(calls, vc);
6269fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6279fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void get_calls_reply(DBusPendingCall *call, void *user_data)
6299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz{
6309fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusError err;
6319fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessage *reply;
6329fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, entry;
6339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6349fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("");
6359fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	reply = dbus_pending_call_steal_reply(call);
6369fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6379fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_error_init(&err);
6389fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_set_error_from_message(&err, reply)) {
6399fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("ofono replied with an error: %s, %s",
6409fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				err.name, err.message);
6419fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_error_free(&err);
6429fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
6439fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6449fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6459fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(reply, &iter);
6469fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6479fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
6489fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature");
6499fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
6509fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6519fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6529fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &entry);
6539fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6549fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(&entry)
6559fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						== DBUS_TYPE_STRUCT) {
6569fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		const char *path;
6579fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		DBusMessageIter value, properties;
6589fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6599fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
6609fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&value, &path);
6619fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6629fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&value);
6639fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_recurse(&value, &properties);
6649fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6659fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		call_added(path, &properties);
6669fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6679fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
6689fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
6699fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
6709fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzdone:
6719fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_unref(reply);
672c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	remove_pending(call);
6739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz}
6749fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
675e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void handle_network_property(const char *property, DBusMessageIter *variant)
676a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
67783003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *status, *operator;
678a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	unsigned int signals_bar;
679a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
680a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (g_str_equal(property, "Status")) {
681e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &status);
6828e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("Status is %s", status);
683a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(status, "registered")) {
684a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_HOME;
685a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
686a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
687a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
688a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
689a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(status, "roaming")) {
690a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_ROAM;
691a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
692a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_ACTIVE);
693a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
694a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
695a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else {
696a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_NOSERV;
697a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
698a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
699a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
700a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_NONE);
701a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
702e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	} else if (g_str_equal(property, "Name")) {
703e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &operator);
7048e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("Operator is %s", operator);
705a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_free(net.operator_name);
706a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.operator_name = g_strdup(operator);
707a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (g_str_equal(property, "SignalStrength")) {
708e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(variant, &signals_bar);
7098e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("SignalStrength is %d", signals_bar);
710a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.signals_bar = signals_bar;
711a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "signal",
712a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						(signals_bar + 20) / 21);
713a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
714a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
715a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
716e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic int parse_network_properties(DBusMessageIter *properties)
717a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
718a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t features = AG_FEATURE_EC_ANDOR_NR |
719a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_REJECT_A_CALL |
720a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_ENHANCED_CALL_STATUS |
721a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
722a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
723e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(properties)
724e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						== DBUS_TYPE_DICT_ENTRY) {
725e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		const char *key;
726e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		DBusMessageIter value, entry;
727e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
728e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_recurse(properties, &entry);
729e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_get_basic(&entry, &key);
730e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
731e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_next(&entry);
732e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_recurse(&entry, &value);
733e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
734e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		handle_network_property(key, &value);
735e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
736e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		dbus_message_iter_next(properties);
737e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
738e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
739e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	telephony_ready_ind(features, ofono_indicators, BTRH_NOT_SUPPORTED,
740e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz								chld_str);
741e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
742e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return 0;
743e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
744e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
745e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void get_properties_reply(DBusPendingCall *call, void *user_data)
746e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
747e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusError err;
748e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessage *reply;
749e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
750e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	int ret = 0;
751e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
752e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("");
753a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
754a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
755a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
756a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
757a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
758a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
759a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
760a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
761a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
762a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
763a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
764a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
765a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
766e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
767a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
768a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
769a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
770e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
771a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
772e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	ret = parse_network_properties(&properties);
7739fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (ret < 0) {
774e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unable to parse %s.GetProperty reply",
775e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_NETWORKREG_INTERFACE);
7769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		goto done;
7779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	}
7789fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz
7799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
7809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				OFONO_VCMANAGER_INTERFACE, "GetCalls",
7819fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				get_calls_reply, NULL, DBUS_TYPE_INVALID);
7829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (ret < 0)
7839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unable to send %s.GetCalls",
7849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						OFONO_VCMANAGER_INTERFACE);
785a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
786a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
787a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
788c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	remove_pending(call);
789a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
790a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
791e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void modem_added(const char *path)
792a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
793e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	int ret;
794c5321032b046c39477ab99a05e933b13d0b67a04Forrest Zhao
795e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("%s", path);
796e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
797e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path != NULL)
798e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return;
799e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
800e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_obj_path = g_strdup(path);
801e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
802e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	ret = send_method_call(OFONO_BUS_NAME, path,
803e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				OFONO_NETWORKREG_INTERFACE, "GetProperties",
804e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				get_properties_reply, NULL, DBUS_TYPE_INVALID);
805e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (ret < 0)
806e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unable to send %s.GetProperties",
807e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						OFONO_NETWORKREG_INTERFACE);
808a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
809a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
810e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void get_modems_reply(DBusPendingCall *call, void *user_data)
811a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
812a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
813a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
814e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, entry;
815e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
816a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8178e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("list_modem_reply is called\n");
818a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
819a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
820a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
821a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
822a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
823a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
824a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
825a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
826a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
827a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
828a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
829a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
830a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
831e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
832a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
833a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
834a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
835e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &entry);
836a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
837e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_OBJECT_PATH) {
838e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature");
839a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
840a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
841a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
842e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&entry, &path);
843a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
844e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_added(path);
845a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
846a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
847a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
848c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	remove_pending(call);
849a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
850a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
851e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_network_property_changed(DBusConnection *conn,
8528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
853a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
854e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, variant;
855a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *property;
856a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
857a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
858a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
859a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
860a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in networkregistration"
861a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" PropertyChanged signal");
8628309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
863a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
864a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &property);
8658e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("in handle_registration_property_changed(),"
866a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" the property is %s", property);
867a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
868a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
869e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &variant);
870a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
871e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	handle_network_property(property, &variant);
8728309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8738309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
874a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
875a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vcmanager_call_added(DBusConnection *conn,
8779fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
878a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
8799fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
8809fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *path = dbus_message_get_path(msg);
881a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8829fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	/* Ignore call if modem path doesn't math */
8839fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
8849fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
885a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8869fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
887a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8889fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter)
8899fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz						!= DBUS_TYPE_OBJECT_PATH) {
8909fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
8919fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
8929fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_member(msg));
8939fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return TRUE;
894a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
895a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
8969fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &path);
8979fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_next(&iter);
8989fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
899a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9009fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_added(path, &properties);
901a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9029fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	return TRUE;
903a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
904a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9059fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic void call_removed(const char *path)
906a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
9079fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	struct voice_call *vc;
908a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9099fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	DBG("%s", path);
910a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9119fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	vc = find_vc(path);
9129fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (vc == NULL)
9139fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		return;
9148309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9159fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	calls = g_slist_remove(calls, vc);
9169fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_free(vc);
9178309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz}
9188309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9199fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentzstatic gboolean handle_vcmanager_call_removed(DBusConnection *conn,
9208309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
9218309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz{
9229fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	const char *path = dbus_message_get_path(msg);
9238309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9249fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	/* Ignore call if modem path doesn't math */
9259fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
9268309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
9278309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9289fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	if (!dbus_message_get_args(msg, NULL,
9299fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				DBUS_TYPE_OBJECT_PATH, &path,
9309fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz				DBUS_TYPE_INVALID)) {
9319fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
9329fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
9339fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz					dbus_message_get_member(msg));
9348309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
9358309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
9368309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9379fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	call_removed(path);
9388309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9398309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
940a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
941a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
942e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_manager_modem_added(DBusConnection *conn,
943e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
944e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
945e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBusMessageIter iter, properties;
946e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
947e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
948e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path != NULL)
949e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
950e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
951e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
952e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
953e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter)
954e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						!= DBUS_TYPE_OBJECT_PATH) {
955e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
956e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
957e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_member(msg));
958e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
959e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
960e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
961e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &path);
962e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &properties);
963e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
964e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_added(path);
965e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
966e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return TRUE;
967e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
968e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
969e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic void modem_removed(const char *path)
970e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
971e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (g_strcmp0(modem_obj_path, path) != 0)
972e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return;
973e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
974e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("%s", path);
975e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
9769fa0976e28224d25c62d4a68fd02a6e5134447e6Luiz Augusto von Dentz	g_slist_foreach(calls, (GFunc) call_free, NULL);
977e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_slist_free(calls);
978e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	calls = NULL;
979e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
980e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_free(net.operator_name);
981e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	net.operator_name = NULL;
982e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
983e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	g_free(modem_obj_path);
984e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_obj_path = NULL;
985e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
986e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
987e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentzstatic gboolean handle_manager_modem_removed(DBusConnection *conn,
988e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz						DBusMessage *msg, void *data)
989e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz{
990e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	const char *path;
991e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
992e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (!dbus_message_get_args(msg, NULL,
993e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				DBUS_TYPE_OBJECT_PATH, &path,
994e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				DBUS_TYPE_INVALID)) {
995e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		error("Unexpected signature in %s.%s signal",
996e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_interface(msg),
997e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz					dbus_message_get_member(msg));
998e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		return TRUE;
999e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	}
1000e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1001e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	modem_removed(path);
1002e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1003e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	return TRUE;
1004e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz}
1005e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz
1006a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_battery_level_reply(DBusPendingCall *call, void *user_data)
1007a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1008a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
1009a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
1010a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t level;
1011a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int *value = user_data;
1012a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1013a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
1014a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1015a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
1016a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
1017a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("hald replied with an error: %s, %s",
1018a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
1019a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
1020a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
1021a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1022a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1023463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	dbus_error_init(&err);
1024463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	if (dbus_message_get_args(reply, &err,
1025a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INT32, &level,
1026463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz				DBUS_TYPE_INVALID) == FALSE) {
1027463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		error("Unable to parse GetPropertyInteger reply: %s, %s",
1028463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz							err.name, err.message);
1029463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		dbus_error_free(&err);
1030463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz		goto done;
1031463763c080cd72ecd727a218dd68f7c0ba95bc9eLuiz Augusto von Dentz	}
1032a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1033a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	*value = (int) level;
1034a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1035a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (value == &battchg_last)
10368e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.last_full"
1037a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1038a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else if (value == &battchg_design)
10398e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.design"
1040a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1041a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
10428e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan		DBG("telephony-ofono: battery.charge_level.current"
1043a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
1044a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1045a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if ((battchg_design > 0 || battchg_last > 0) && battchg_cur >= 0) {
10469ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		int new, max;
1047a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1048a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (battchg_last > 0)
1049a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_last;
1050a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else
1051a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_design;
1052a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1053a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		new = battchg_cur * 5 / max;
1054a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10559ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		telephony_update_indicator(ofono_indicators, "battchg", new);
1056a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1057a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
1058a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
1059c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	remove_pending(call);
1060a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1061a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1062a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_get_integer(const char *path, const char *key, void *user_data)
1063a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1064a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	send_method_call("org.freedesktop.Hal", path,
1065a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"org.freedesktop.Hal.Device",
1066a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"GetPropertyInteger",
1067a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_battery_level_reply, user_data,
1068a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &key,
1069a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
1070a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1071a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10728309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_hal_property_modified(DBusConnection *conn,
10738309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
1074a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1075a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *path;
1076a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, array;
1077a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t num_changes;
1078a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1079a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	path = dbus_message_get_path(msg);
1080a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1081a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
1082a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1083a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32) {
1084a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
10858309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
1086a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1087a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1088a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &num_changes);
1089a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
1090a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1091a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
1092a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
10938309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
1094a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
1095a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1096a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &array);
1097a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1098a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
1099a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		DBusMessageIter prop;
1100a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		const char *name;
1101a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_bool_t added, removed;
1102a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1103a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&array, &prop);
1104a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1105a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (!iter_get_basic_args(&prop,
1106a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_STRING, &name,
1107a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &added,
1108a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &removed,
1109a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_INVALID)) {
1110a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("Invalid hal PropertyModified parameters");
1111a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
1112a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
1113a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1114a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(name, "battery.charge_level.last_full"))
1115a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_last);
1116a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.current"))
1117a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_cur);
1118a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.design"))
1119a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_design);
1120a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1121a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&array);
1122a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
11238309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11248309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
1125a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1126a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
11279946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentzstatic void add_watch(const char *sender, const char *path,
11289946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz				const char *interface, const char *member,
11299946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz				GDBusSignalFunction function)
11309946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz{
11319946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	guint watch;
11329946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz
11339946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	watch = g_dbus_add_signal_watch(connection, sender, path, interface,
11349946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz					member, function, NULL, NULL);
11359946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz
11369946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	watches = g_slist_prepend(watches, GUINT_TO_POINTER(watch));
11379946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz}
11389946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz
11398309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic void hal_find_device_reply(DBusPendingCall *call, void *user_data)
1140a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
11418309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessage *reply;
11428309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusError err;
11438309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessageIter iter, sub;
11448309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	int type;
11458309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	const char *path;
11468309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11478e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("begin of hal_find_device_reply()");
11488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	reply = dbus_pending_call_steal_reply(call);
11498309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11508309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_error_init(&err);
11518309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_set_error_from_message(&err, reply)) {
11538309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("hald replied with an error: %s, %s",
11548309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				err.name, err.message);
11558309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		dbus_error_free(&err);
11568309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11578309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11588309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11598309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_init(reply, &iter);
11608309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11618309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
11628309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("Unexpected signature in hal_find_device_reply()");
11638309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11648309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11658309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11668309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
11678309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	type = dbus_message_iter_get_arg_type(&sub);
11698309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11708309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (type != DBUS_TYPE_OBJECT_PATH && type != DBUS_TYPE_STRING) {
11718309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("No hal device with battery capability found");
11728309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
11738309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
11748309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11758309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_get_basic(&sub, &path);
11768309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11778e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony-ofono: found battery device at %s", path);
11788309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11799946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(NULL, path, "org.freedesktop.Hal.Device",
11809946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"PropertyModified", handle_hal_property_modified);
11818309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
11828309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.last_full", &battchg_last);
11838309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.current", &battchg_cur);
11848309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.design", &battchg_design);
11858309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzdone:
11868309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_unref(reply);
1187c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	remove_pending(call);
1188a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1189a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1190a34c155f55270b917795d003be24488f53d9b711Forrest Zhaoint telephony_init(void)
1191a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1192a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *battery_cap = "battery";
1193a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
1194a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1195a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1196a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
11979946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(OFONO_BUS_NAME, NULL, OFONO_NETWORKREG_INTERFACE,
11989946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"PropertyChanged", handle_network_property_changed);
11999946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(OFONO_BUS_NAME, NULL, OFONO_MANAGER_INTERFACE,
12009946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"ModemAdded", handle_manager_modem_added);
12019946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(OFONO_BUS_NAME, NULL, OFONO_MANAGER_INTERFACE,
12029946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"ModemRemoved", handle_manager_modem_removed);
12039946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(OFONO_BUS_NAME, NULL, OFONO_VCMANAGER_INTERFACE,
12049946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"CallAdded", handle_vcmanager_call_added);
12059946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	add_watch(OFONO_BUS_NAME, NULL, OFONO_VCMANAGER_INTERFACE,
12069946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz			"CallRemoved", handle_vcmanager_call_removed);
1207a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1208a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, OFONO_PATH,
1209e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				OFONO_MANAGER_INTERFACE, "GetModems",
1210e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz				get_modems_reply, NULL, DBUS_TYPE_INVALID);
1211a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1212a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1213a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1214a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call("org.freedesktop.Hal",
1215a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"/org/freedesktop/Hal/Manager",
1216a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"org.freedesktop.Hal.Manager",
1217a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"FindDeviceByCapability",
1218a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				hal_find_device_reply, NULL,
1219a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_STRING, &battery_cap,
1220a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INVALID);
1221a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1222a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1223a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
12248e58667ef0a4cda88ac64137728da28d8fdf3f0fGustavo F. Padovan	DBG("telephony_init() successfully");
1225a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1226a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return ret;
1227a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1228a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
12299946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentzstatic void remove_watch(gpointer data)
12309946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz{
12319946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	g_dbus_remove_watch(connection, GPOINTER_TO_UINT(data));
12329946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz}
12339946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz
1234a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_exit(void)
1235a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1236e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	DBG("");
1237a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1238a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(last_dialed_number);
1239e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	last_dialed_number = NULL;
1240a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1241e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz	if (modem_obj_path)
1242e2f9bfa4b8ce406d93f38f248ffd2e503bb4a7a6Luiz Augusto von Dentz		modem_removed(modem_obj_path);
1243a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
12449946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	g_slist_foreach(watches, (GFunc) remove_watch, NULL);
12459946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	g_slist_free(watches);
12469946e8636c3361dc769c5f8a463b74158018d3ddLuiz Augusto von Dentz	watches = NULL;
1247a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1248c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	g_slist_foreach(pending, (GFunc) dbus_pending_call_cancel, NULL);
1249c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	g_slist_foreach(pending, (GFunc) dbus_pending_call_unref, NULL);
1250c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	g_slist_free(pending);
1251c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz	pending = NULL;
1252c49dc8c7c815191b5825896f827cac2e2ff395ffLuiz Augusto von Dentz
1253a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_connection_unref(connection);
1254a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = NULL;
1255401be3748717fae613041592778a3a7160fb763cJohan Hedberg
1256401be3748717fae613041592778a3a7160fb763cJohan Hedberg	telephony_deinit();
1257a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1258