telephony-ofono.c revision 9184e2eeb7b97371c6b83b747c8984e2340d2b47
1a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/*
2a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
3a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  BlueZ - Bluetooth protocol stack for Linux
4a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *
5a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  Copyright (C) 2009       Intel Corporation
6a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  Copyright (C) 2006-2007  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
38a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include "logging.h"
39a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#include "telephony.h"
40a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
41a34c155f55270b917795d003be24488f53d9b711Forrest Zhaoenum net_registration_status {
42a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_HOME = 0x00,
43a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_ROAM,
44a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	NETWORK_REG_STATUS_NOSERV
45a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
46a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
47a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostruct voice_call {
48a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *obj_path;
49a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int status;
50a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	gboolean originating;
51a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *number;
528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	guint watch;
53a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
54a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
55a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic DBusConnection *connection = NULL;
56a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *modem_obj_path = NULL;
57a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *last_dialed_number = NULL;
58a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic GSList *calls = NULL;
59a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
60a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_BUS_NAME "org.ofono"
61a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_PATH "/"
62d89690657f2634ceb9d4b13257ccc7d60bc0cb5dForrest Zhao#define OFONO_MANAGER_INTERFACE "org.ofono.Manager"
63a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_NETWORKREG_INTERFACE "org.ofono.NetworkRegistration"
64a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VCMANAGER_INTERFACE "org.ofono.VoiceCallManager"
65a34c155f55270b917795d003be24488f53d9b711Forrest Zhao#define OFONO_VC_INTERFACE "org.ofono.VoiceCall"
66a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
678309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic guint registration_watch = 0;
688309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic guint voice_watch = 0;
698309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic guint device_watch = 0;
708309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
71a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/* HAL battery namespace key values */
72a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_cur = -1;    /* "battery.charge_level.current" */
73a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_last = -1;   /* "battery.charge_level.last_full" */
74a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int battchg_design = -1; /* "battery.charge_level.design" */
75a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
76a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct {
77a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint8_t status;
78a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t signals_bar;
79a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *operator_name;
80a34c155f55270b917795d003be24488f53d9b711Forrest Zhao} net = {
81a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.status = NETWORK_REG_STATUS_NOSERV,
82a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.signals_bar = 0,
83a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	.operator_name = NULL,
84a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
85a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
86a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic const char *chld_str = "0,1,1x,2,2x,3,4";
87a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic char *subscriber_number = NULL;
88a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
89a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean events_enabled = FALSE;
90a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
91a34c155f55270b917795d003be24488f53d9b711Forrest Zhao/* Response and hold state
92a34c155f55270b917795d003be24488f53d9b711Forrest Zhao * -1 = none
93a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  0 = incoming call is put on hold in the AG
94a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  1 = held incoming call is accepted in the AG
95a34c155f55270b917795d003be24488f53d9b711Forrest Zhao *  2 = held incoming call is rejected in the AG
96a34c155f55270b917795d003be24488f53d9b711Forrest Zhao */
97a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int response_and_hold = -1;
98a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
99a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct indicator ofono_indicators[] =
100a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
101884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "battchg",	"0-5",	5,	TRUE },
102884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "signal",	"0-5",	5,	TRUE },
103884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "service",	"0,1",	1,	TRUE },
104884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "call",	"0,1",	0,	TRUE },
105884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callsetup",	"0-3",	0,	TRUE },
106884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "callheld",	"0-2",	0,	FALSE },
107884debf8418651b63b1a5a8918092ba4d230783eJohan Hedberg	{ "roam",	"0,1",	0,	TRUE },
108a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	{ NULL }
109a34c155f55270b917795d003be24488f53d9b711Forrest Zhao};
110a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
111a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc(const char *path)
112a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
113a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
114a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
115a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
116a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
117a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
118a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(vc->obj_path, path))
119a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
120a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
121a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
122a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
123a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
124a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
125a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic struct voice_call *find_vc_with_status(int status)
126a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
127a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
128a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
129a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls; l != NULL; l = l->next) {
130a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
131a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
132a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (vc->status == status)
133a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			return vc;
134a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
135a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
136a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return NULL;
137a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
138a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
139a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_connected(void *telephony_device)
140a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
141a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: device %p connected", telephony_device);
142a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
143a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
144a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_device_disconnected(void *telephony_device)
145a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
146a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: device %p disconnected", telephony_device);
147a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = FALSE;
148a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
149a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
150a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_event_reporting_req(void *telephony_device, int ind)
151a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
152a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	events_enabled = ind == 1 ? TRUE : FALSE;
153a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
154a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_event_reporting_rsp(telephony_device, CME_ERROR_NONE);
155a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
156a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
157a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_response_and_hold_req(void *telephony_device, int rh)
158a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
159a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	response_and_hold = rh;
160a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
161a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_response_and_hold_ind(response_and_hold);
162a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
163a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_response_and_hold_rsp(telephony_device, CME_ERROR_NONE);
164a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
165a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
166a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_last_dialed_number_req(void *telephony_device)
167a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
168a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: last dialed number request");
169a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
170a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (last_dialed_number)
171a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_req(telephony_device, last_dialed_number);
172a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
173a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_last_dialed_number_rsp(telephony_device,
174a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CME_ERROR_NOT_ALLOWED);
175a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
176a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
177a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int send_method_call(const char *dest, const char *path,
178a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                const char *interface, const char *method,
179a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                DBusPendingCallNotifyFunction cb,
180a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                                void *user_data, int type, ...)
181a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
182a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *msg;
183a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusPendingCall *call;
184a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list args;
185a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
186a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	msg = dbus_message_new_method_call(dest, path, interface, method);
187a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!msg) {
188a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unable to allocate new D-Bus %s message", method);
189a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -ENOMEM;
190a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
191a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
192a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(args, type);
193a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
194a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_message_append_args_valist(msg, type, args)) {
195a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
196a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		va_end(args);
197a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
198a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
199a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
200a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(args);
201a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
202a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!cb) {
203a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_dbus_send_message(connection, msg);
204a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return 0;
205a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
206a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
207a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!dbus_connection_send_with_reply(connection, msg, &call, -1)) {
208a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Sending %s failed", method);
209a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_unref(msg);
210a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return -EIO;
211a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
212a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
213a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_pending_call_set_notify(call, cb, user_data, NULL);
214a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_pending_call_unref(call);
215a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(msg);
216a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
217a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return 0;
218a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
219a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
220a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_terminate_call_req(void *telephony_device)
221a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
222a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	struct voice_call *vc;
223a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
224a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
225e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	if ((vc = find_vc_with_status(CALL_STATUS_ACTIVE))) {
226e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_DIALING))) {
227e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_ALERTING))) {
228e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	} else if ((vc = find_vc_with_status(CALL_STATUS_INCOMING))) {
229e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmann	}
230a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
231a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
232a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("in telephony_terminate_call_req, no active call");
233a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_terminate_call_rsp(telephony_device,
234a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
235a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
236a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
237a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
238a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
239a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					OFONO_VC_INTERFACE,
240a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					"Hangup", NULL,
241a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					NULL, DBUS_TYPE_INVALID);
242a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
243a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
244a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
245a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
246a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
247a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
248a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
249a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
250a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
251a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
252a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_answer_call_req(void *telephony_device)
253a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
254a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	struct voice_call *vc = find_vc_with_status(CALL_STATUS_INCOMING);
255a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
256a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
257a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
258a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
259a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_NOT_ALLOWED);
260a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
261a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
262a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
263a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, vc->obj_path,
264a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VC_INTERFACE,
265a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"Answer", NULL,
266a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			NULL, DBUS_TYPE_INVALID);
267a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
268a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0) {
269a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_answer_call_rsp(telephony_device,
270a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CME_ERROR_AG_FAILURE);
271a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
272a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
273a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
274a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_answer_call_rsp(telephony_device, CME_ERROR_NONE);
275a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
276a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
277a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_dial_number_req(void *telephony_device, const char *number)
278a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
27983003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *clir;
280a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
281a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
282a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: dial request to %s", number);
283a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
284a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!strncmp(number, "*31#", 4)) {
285a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
28683003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "enabled";
287a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (!strncmp(number, "#31#", 4)) {
288a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		number += 4;
28983003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir =  "disabled";
29083003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	} else
29183003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		clir = "default";
292a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
293a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
294a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
295a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                        "Dial", NULL, NULL,
296a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &number,
297a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &clir,
298a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
299a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
300a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
301a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device,
302a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
303a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
304a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_dial_number_rsp(telephony_device, CME_ERROR_NONE);
305a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
306a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
307a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_transmit_dtmf_req(void *telephony_device, char tone)
308a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
309a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *tone_string;
310a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
311a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
312a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: transmit dtmf: %c", tone);
313a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
314a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	tone_string = g_strdup_printf("%c", tone);
315a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, modem_obj_path,
316a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_VCMANAGER_INTERFACE,
317a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"SendTones", NULL, NULL,
318a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &tone_string,
319a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
320a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(tone_string);
321a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
322a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
323a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device,
324a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			CME_ERROR_AG_FAILURE);
325a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
326a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_transmit_dtmf_rsp(telephony_device, CME_ERROR_NONE);
327a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
328a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
329a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_subscriber_number_req(void *telephony_device)
330a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
331a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: subscriber number request");
332a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
333a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (subscriber_number)
334a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_subscriber_number_ind(subscriber_number,
335a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						NUMBER_TYPE_TELEPHONY,
336a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						SUBSCRIBER_SERVICE_VOICE);
337a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_subscriber_number_rsp(telephony_device, CME_ERROR_NONE);
338a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
339a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
340a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_list_current_calls_req(void *telephony_device)
341a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
342a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	GSList *l;
343a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int i;
344a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
345a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: list current calls request");
346a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
347a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (l = calls, i = 1; l != NULL; l = l->next, i++) {
348a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		struct voice_call *vc = l->data;
349a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int direction;
350a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
351a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		direction = vc->originating ?
352a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				CALL_DIR_OUTGOING : CALL_DIR_INCOMING;
353a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
354a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_list_current_call_ind(i, direction, vc->status,
355a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					CALL_MODE_VOICE, CALL_MULTIPARTY_NO,
356a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					vc->number, NUMBER_TYPE_TELEPHONY);
357a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
358a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_list_current_calls_rsp(telephony_device, CME_ERROR_NONE);
359a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
360a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
361a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_operator_selection_req(void *telephony_device)
362a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
363a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: operator selection request");
364a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
365a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_ind(OPERATOR_MODE_AUTO,
366a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				net.operator_name ? net.operator_name : "");
367a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_operator_selection_rsp(telephony_device, CME_ERROR_NONE);
368a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
369a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
370a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_call_hold_req(void *telephony_device, const char *cmd)
371a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
372a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: got call hold request %s", cmd);
373a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_call_hold_rsp(telephony_device, CME_ERROR_NONE);
374a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
375a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
376a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_nr_and_ec_req(void *telephony_device, gboolean enable)
377a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
378a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: got %s NR and EC request",
379a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			enable ? "enable" : "disable");
380a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
381a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_nr_and_ec_rsp(telephony_device, CME_ERROR_NONE);
382a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
383a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
384a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_key_press_req(void *telephony_device, const char *keys)
385a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
386a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony-ofono: got key press request for %s", keys);
387a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_key_press_rsp(telephony_device, CME_ERROR_NONE);
388a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
389a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
390a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic gboolean iter_get_basic_args(DBusMessageIter *iter,
391a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					int first_arg_type, ...)
392a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
393a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int type;
394a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_list ap;
395a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
396a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_start(ap, first_arg_type);
397a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
398a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	for (type = first_arg_type; type != DBUS_TYPE_INVALID;
399a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		type = va_arg(ap, int)) {
400a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		void *value = va_arg(ap, void *);
401a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		int real_type = dbus_message_iter_get_arg_type(iter);
402a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
403a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (real_type != type) {
404a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("iter_get_basic_args: expected %c but got %c",
405a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				(char) type, (char) real_type);
406a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
407a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
408a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
409a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(iter, value);
410a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(iter);
411a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
412a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
413a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	va_end(ap);
414a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
415a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return type == DBUS_TYPE_INVALID ? TRUE : FALSE;
416a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
417a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
418e925c22367a6b5c501c6635691d3e3399f492e78Marcel Holtmannstatic void handle_registration_property(const char *property, DBusMessageIter sub)
419a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
42083003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	const char *status, *operator;
421a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	unsigned int signals_bar;
422a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
423a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (g_str_equal(property, "Status")) {
424a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&sub, &status);
425a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("Status is %s", status);
426a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(status, "registered")) {
427a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_HOME;
428a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
429a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
430a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
431a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
432a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(status, "roaming")) {
433a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_ROAM;
434a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
435a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_ACTIVE);
436a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
437a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_PRESENT);
438a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else {
439a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			net.status = NETWORK_REG_STATUS_NOSERV;
440a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
441a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"roam", EV_ROAM_INACTIVE);
442a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
443a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"service", EV_SERVICE_NONE);
444a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
445a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (g_str_equal(property, "Operator")) {
446a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&sub, &operator);
447a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("Operator is %s", operator);
448a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_free(net.operator_name);
449a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.operator_name = g_strdup(operator);
450a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	} else if (g_str_equal(property, "SignalStrength")) {
451a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&sub, &signals_bar);
452a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("SignalStrength is %d", signals_bar);
453a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		net.signals_bar = signals_bar;
454a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "signal",
455a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						(signals_bar + 20) / 21);
456a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
457a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
458a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
459a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void get_registration_reply(DBusPendingCall *call, void *user_data)
460a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
461a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
462a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
463a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, iter_entry;
464a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	uint32_t features = AG_FEATURE_EC_ANDOR_NR |
465a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_REJECT_A_CALL |
466a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_ENHANCED_CALL_STATUS |
467a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
468a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
469a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
470a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
471a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
472a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
473a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
474a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
475a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
476a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
477a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
478a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
479a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
480a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
481a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	/* ARRAY -> ENTRY -> VARIANT*/
482a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
483a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in GetProperties return");
484a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
485a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
486a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
487a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &iter_entry);
488a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
489a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter_entry)
490a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					!= DBUS_TYPE_DICT_ENTRY) {
491a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in GetProperties return");
492a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
493a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
494a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
495a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&iter_entry)
496a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					!= DBUS_TYPE_INVALID) {
497a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		DBusMessageIter iter_property, sub;
498a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		char *property;
499a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
500a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&iter_entry, &iter_property);
501a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (dbus_message_iter_get_arg_type(&iter_property)
502a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					!= DBUS_TYPE_STRING) {
503a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("Unexpected signature in GetProperties return");
504a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			goto done;
505a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
506a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
507a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&iter_property, &property);
508a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
509a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&iter_property);
510a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&iter_property, &sub);
511a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
512a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		handle_registration_property(property, sub);
513a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
514a34c155f55270b917795d003be24488f53d9b711Forrest Zhao                dbus_message_iter_next(&iter_entry);
515a34c155f55270b917795d003be24488f53d9b711Forrest Zhao        }
516a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
517a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	telephony_ready_ind(features, ofono_indicators,
518a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				response_and_hold, chld_str);
519a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
520a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
521a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
522a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
523a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
524a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic int get_registration_and_signal_status()
525a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
526a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return send_method_call(OFONO_BUS_NAME, modem_obj_path,
527a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			OFONO_NETWORKREG_INTERFACE,
528a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"GetProperties", get_registration_reply,
529a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			NULL, DBUS_TYPE_INVALID);
530a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
531a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
532a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void list_modem_reply(DBusPendingCall *call, void *user_data)
533a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
534a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
535a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
536a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, iter_entry, iter_property, iter_arrary, sub;
537a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	char *property, *modem_obj_path_local;
538a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
539a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
540a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("list_modem_reply is called\n");
541a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
542a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
543a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
544a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
545a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
546a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
547a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
548a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
549a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
550a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
551a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
552a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
553a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
554a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in ListModems return");
555a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
556a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
557a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
558a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &iter_entry);
559a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
560a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter_entry)
561a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					!= DBUS_TYPE_DICT_ENTRY) {
562a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in ListModems return 2, %c",
563a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				dbus_message_iter_get_arg_type(&iter_entry));
564a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
565a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
566a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
567a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter_entry, &iter_property);
568a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
569a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter_property, &property);
570a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
571a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter_property);
572a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter_property, &iter_arrary);
573a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter_arrary, &sub);
574a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
575a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
576a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&sub, &modem_obj_path_local);
577a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		modem_obj_path = g_strdup(modem_obj_path_local);
578a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("modem_obj_path is %p, %s\n", modem_obj_path,
579a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						modem_obj_path);
580a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&sub);
581a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
582a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
583a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = get_registration_and_signal_status();
584a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
585a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("get_registration_and_signal_status() failed(%d)", ret);
586a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
587a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
588a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
589a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
5908309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_registration_property_changed(DBusConnection *conn,
5918309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
592a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
593a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, sub;
594a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *property;
595a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
596a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
597a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
598a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
599a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in networkregistration"
600a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" PropertyChanged signal");
6018309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
602a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
603a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &property);
6048309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	debug("in handle_registration_property_changed(),"
605a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" the property is %s", property);
606a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
607a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
608a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &sub);
609a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
610a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	handle_registration_property(property, sub);
6118309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
6128309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
613a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
614a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
615a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void vc_getproperties_reply(DBusPendingCall *call, void *user_data)
616a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
617a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
618a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
619a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, iter_entry;
620a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *path = user_data;
621a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	struct voice_call *vc;
622a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
623a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("in vc_getproperties_reply");
624a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
625a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
626a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
627a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
628a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("ofono replied with an error: %s, %s",
629a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
630a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
631a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
63283003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	}
633a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
634a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	vc = find_vc(path);
635a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc) {
636a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("in vc_getproperties_reply, vc is NULL");
637a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
638a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
639a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
640a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(reply, &iter);
641a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
642a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
643a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in vc_getproperties_reply()");
644a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
64583003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	}
646a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
647a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &iter_entry);
648a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
649a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter_entry)
65083003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi			!= DBUS_TYPE_DICT_ENTRY) {
651a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in vc_getproperties_reply()");
652a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
65383003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi	}
654a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
655a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&iter_entry)
65683003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi			!= DBUS_TYPE_INVALID) {
657a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		DBusMessageIter iter_property, sub;
658a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		char *property, *cli, *state;
659a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
660a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&iter_entry, &iter_property);
661a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (dbus_message_iter_get_arg_type(&iter_property)
66283003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi				!= DBUS_TYPE_STRING) {
663a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("Unexpected signature in"
664a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" vc_getproperties_reply()");
665a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			goto done;
66683003660808c30390f21c569a5ba7dbdd8344a41Claudio Takahasi		}
667a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
668a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&iter_property, &property);
669a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
670a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&iter_property);
671a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&iter_property, &sub);
672a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(property, "LineIdentification")) {
673a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			dbus_message_iter_get_basic(&sub, &cli);
674a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			debug("in vc_getproperties_reply(), cli is %s", cli);
675a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			vc->number = g_strdup(cli);
676a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(property, "State")) {
677a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			dbus_message_iter_get_basic(&sub, &state);
678a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			debug("in vc_getproperties_reply(),"
679a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" state is %s", state);
680a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			if (g_str_equal(state, "incoming"))
681a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				vc->status = CALL_STATUS_INCOMING;
682a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			else if (g_str_equal(state, "dialing"))
683a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				vc->status = CALL_STATUS_DIALING;
684a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			else if (g_str_equal(state, "alerting"))
685a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				vc->status = CALL_STATUS_ALERTING;
686a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			else if (g_str_equal(state, "waiting"))
687a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				vc->status = CALL_STATUS_WAITING;
688a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
689a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
690a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&iter_entry);
691a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
692a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
693a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	switch (vc->status) {
694a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	case CALL_STATUS_INCOMING:
695a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		printf("in CALL_STATUS_INCOMING: case\n");
696a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		vc->originating = FALSE;
697a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "callsetup",
698a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					EV_CALLSETUP_INCOMING);
699a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_incoming_call_ind(vc->number, NUMBER_TYPE_TELEPHONY);
700a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		break;
701a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	case CALL_STATUS_DIALING:
702a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		printf("in CALL_STATUS_DIALING: case\n");
703a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		vc->originating = TRUE;
704a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_free(last_dialed_number);
705a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		last_dialed_number = g_strdup(vc->number);
706a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "callsetup",
707a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					EV_CALLSETUP_OUTGOING);
708a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		break;
709a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	case CALL_STATUS_ALERTING:
710a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		printf("in CALL_STATUS_ALERTING: case\n");
711a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		vc->originating = TRUE;
712a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		g_free(last_dialed_number);
713a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		last_dialed_number = g_strdup(vc->number);
714a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "callsetup",
715a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					EV_CALLSETUP_ALERTING);
716a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		break;
717a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	case CALL_STATUS_WAITING:
718a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("in CALL_STATUS_WAITING: case");
719a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		vc->originating = FALSE;
720a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_update_indicator(ofono_indicators, "callsetup",
721a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					EV_CALLSETUP_INCOMING);
722a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		telephony_call_waiting_ind(vc->number, NUMBER_TYPE_TELEPHONY);
723a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		break;
724a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
725a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
726a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
727a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
728a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
729a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void vc_free(struct voice_call *vc)
730a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
731a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (!vc)
732a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return;
733a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
7348309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, vc->watch);
735a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(vc->obj_path);
736a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(vc->number);
737a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(vc);
738a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
739a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
7408309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_vc_property_changed(DBusConnection *conn,
7418309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					DBusMessage *msg, void *data)
742a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
7438309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	struct voice_call *vc = data;
7448309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	const char *obj_path = dbus_message_get_path(msg);
745a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, sub;
746a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *property, *state;
747a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
748a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("in handle_vc_property_changed, obj_path is %s", obj_path);
749a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
750a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
751a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
752a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
753a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in vc PropertyChanged signal");
7548309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
755a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
756a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
757a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &property);
758a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("in handle_vc_property_changed(), the property is %s", property);
759a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
760a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
761a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &sub);
762a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (g_str_equal(property, "State")) {
763a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_get_basic(&sub, &state);
764a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("in handle_vc_property_changed(), State is %s", state);
765a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(state, "disconnected")) {
766a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			printf("in disconnected case\n");
767a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			if (vc->status == CALL_STATUS_ACTIVE)
768a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				telephony_update_indicator(ofono_indicators,
769a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						"call", EV_CALL_INACTIVE);
770a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			else
771a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				telephony_update_indicator(ofono_indicators,
772a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					"callsetup", EV_CALLSETUP_INACTIVE);
773a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			if (vc->status == CALL_STATUS_INCOMING)
774a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				telephony_calling_stopped_ind();
775a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			calls = g_slist_remove(calls, vc);
776a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			vc_free(vc);
777a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(state, "active")) {
778a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
779a34c155f55270b917795d003be24488f53d9b711Forrest Zhao							"call", EV_CALL_ACTIVE);
780a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
781a34c155f55270b917795d003be24488f53d9b711Forrest Zhao							"callsetup",
782a34c155f55270b917795d003be24488f53d9b711Forrest Zhao							EV_CALLSETUP_INACTIVE);
783a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			if (vc->status == CALL_STATUS_INCOMING) {
784a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				telephony_calling_stopped_ind();
785a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			}
786a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			vc->status = CALL_STATUS_ACTIVE;
787a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			debug("vc status is CALL_STATUS_ACTIVE");
788a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(state, "alerting")) {
789a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
790a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					 "callsetup", EV_CALLSETUP_ALERTING);
791a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			vc->status = CALL_STATUS_ALERTING;
792a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			debug("vc status is CALL_STATUS_ALERTING");
793a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		} else if (g_str_equal(state, "incoming")) {
794a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			/* state change from waiting to incoming */
795a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_update_indicator(ofono_indicators,
796a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					"callsetup", EV_CALLSETUP_INCOMING);
797a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			telephony_incoming_call_ind(vc->number,
798a34c155f55270b917795d003be24488f53d9b711Forrest Zhao						NUMBER_TYPE_TELEPHONY);
799a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			vc->status = CALL_STATUS_INCOMING;
800a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			debug("vc status is CALL_STATUS_INCOMING");
801a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
802a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
8038309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8048309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
8058309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz}
8068309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8078309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_vcmanager_property_changed(DBusConnection *conn,
8088309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
8098309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz{
8108309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessageIter iter, sub, array;
8118309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	const char *property, *vc_obj_path = NULL;
8128309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	struct voice_call *vc, *vc_new = NULL;
8138309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8148309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	debug("in handle_vcmanager_property_changed");
8158309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8168309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_init(msg, &iter);
8178309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8188309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
8198309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("Unexpected signature in vcmanager"
8208309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					" PropertyChanged signal");
8218309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
8228309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
8238309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8248309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_get_basic(&iter, &property);
8258309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	debug("in handle_vcmanager_property_changed(),"
8268309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				" the property is %s", property);
8278309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8288309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_next(&iter);
8298309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
8308309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY) {
8318309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("Unexpected signature in vcmanager"
8328309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					" PropertyChanged signal");
8338309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
8348309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
8358309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_recurse(&sub, &array);
8368309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
8378309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		dbus_message_iter_get_basic(&array, &vc_obj_path);
8388309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		vc = find_vc(vc_obj_path);
8398309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		if (vc) {
8408309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz			debug("in handle_vcmanager_property_changed,"
8418309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					" found an existing vc");
8428309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		} else {
8438309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz			vc_new = g_new0(struct voice_call, 1);
8448309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz			vc_new->obj_path = g_strdup(vc_obj_path);
8458309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz			calls = g_slist_append(calls, vc_new);
8468309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz			device_watch = g_dbus_add_signal_watch(connection,
8478309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					NULL, vc_obj_path,
8488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					OFONO_VC_INTERFACE,
8498309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"PropertyChanged",
8508309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					handle_vc_property_changed,
8518309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					vc_new, NULL);
8528309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		}
8538309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		dbus_message_iter_next(&array);
8548309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
8558309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8568309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (!vc_new)
8578309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
8588309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8598309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	send_method_call(OFONO_BUS_NAME, vc_new->obj_path,
8608309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				OFONO_VC_INTERFACE,
8618309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				"GetProperties", vc_getproperties_reply,
8628309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				vc_new->obj_path, DBUS_TYPE_INVALID);
8638309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
8648309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
865a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
866a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
867a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_battery_level_reply(DBusPendingCall *call, void *user_data)
868a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
869a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessage *reply;
870a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusError err;
871a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t level;
872a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int *value = user_data;
873a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
874a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	reply = dbus_pending_call_steal_reply(call);
875a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
876a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_error_init(&err);
877a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_set_error_from_message(&err, reply)) {
878a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("hald replied with an error: %s, %s",
879a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				err.name, err.message);
880a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_error_free(&err);
881a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		goto done;
882a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
883a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
884a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_get_args(reply, NULL,
885a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INT32, &level,
886a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INVALID);
887a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
888a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	*value = (int) level;
889a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
890a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (value == &battchg_last)
891a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("telephony-ofono: battery.charge_level.last_full"
892a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
893a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else if (value == &battchg_design)
894a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("telephony-ofono: battery.charge_level.design"
895a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
896a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	else
897a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		debug("telephony-ofono: battery.charge_level.current"
898a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					" is %d", *value);
899a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
900a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if ((battchg_design > 0 || battchg_last > 0) && battchg_cur >= 0) {
9019ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		int new, max;
902a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
903a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (battchg_last > 0)
904a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_last;
905a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else
906a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			max = battchg_design;
907a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
908a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		new = battchg_cur * 5 / max;
909a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9109ff86aa7f2c11cb3198dfcde93f8db3c534ecdb6Johan Hedberg		telephony_update_indicator(ofono_indicators, "battchg", new);
911a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
912a34c155f55270b917795d003be24488f53d9b711Forrest Zhaodone:
913a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_unref(reply);
914a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
915a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
916a34c155f55270b917795d003be24488f53d9b711Forrest Zhaostatic void hal_get_integer(const char *path, const char *key, void *user_data)
917a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
918a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	send_method_call("org.freedesktop.Hal", path,
919a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"org.freedesktop.Hal.Device",
920a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			"GetPropertyInteger",
921a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_battery_level_reply, user_data,
922a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_STRING, &key,
923a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			DBUS_TYPE_INVALID);
924a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
925a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9268309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic gboolean handle_hal_property_modified(DBusConnection *conn,
9278309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz						DBusMessage *msg, void *data)
928a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
929a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *path;
930a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	DBusMessageIter iter, array;
931a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_int32_t num_changes;
932a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
933a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	path = dbus_message_get_path(msg);
934a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
935a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_init(msg, &iter);
936a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
937a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32) {
938a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
9398309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
940a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
941a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
942a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_get_basic(&iter, &num_changes);
943a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_next(&iter);
944a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
945a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
946a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		error("Unexpected signature in hal PropertyModified signal");
9478309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		return TRUE;
948a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
949a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
950a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_message_iter_recurse(&iter, &array);
951a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
952a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	while (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_INVALID) {
953a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		DBusMessageIter prop;
954a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		const char *name;
955a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_bool_t added, removed;
956a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
957a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_recurse(&array, &prop);
958a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
959a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (!iter_get_basic_args(&prop,
960a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_STRING, &name,
961a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &added,
962a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_BOOLEAN, &removed,
963a34c155f55270b917795d003be24488f53d9b711Forrest Zhao					DBUS_TYPE_INVALID)) {
964a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			error("Invalid hal PropertyModified parameters");
965a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			break;
966a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		}
967a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
968a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		if (g_str_equal(name, "battery.charge_level.last_full"))
969a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_last);
970a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.current"))
971a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_cur);
972a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		else if (g_str_equal(name, "battery.charge_level.design"))
973a34c155f55270b917795d003be24488f53d9b711Forrest Zhao			hal_get_integer(path, name, &battchg_design);
974a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
975a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		dbus_message_iter_next(&array);
976a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	}
9778309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9788309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	return TRUE;
979a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
980a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
9818309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzstatic void hal_find_device_reply(DBusPendingCall *call, void *user_data)
982a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
9838309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessage *reply;
9848309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusError err;
9858309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	DBusMessageIter iter, sub;
9868309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	int type;
9878309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	const char *path;
9888309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9898309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	debug("begin of hal_find_device_reply()");
9908309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	reply = dbus_pending_call_steal_reply(call);
9918309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9928309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_error_init(&err);
9938309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
9948309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_set_error_from_message(&err, reply)) {
9958309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("hald replied with an error: %s, %s",
9968309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz				err.name, err.message);
9978309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		dbus_error_free(&err);
9988309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
9998309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
10008309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10018309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_init(reply, &iter);
10028309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10038309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
10048309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("Unexpected signature in hal_find_device_reply()");
10058309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
10068309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
10078309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10088309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_recurse(&iter, &sub);
10098309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10108309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	type = dbus_message_iter_get_arg_type(&sub);
10118309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10128309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	if (type != DBUS_TYPE_OBJECT_PATH && type != DBUS_TYPE_STRING) {
10138309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		error("No hal device with battery capability found");
10148309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz		goto done;
10158309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	}
10168309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10178309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_iter_get_basic(&sub, &path);
10188309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10198309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	debug("telephony-ofono: found battery device at %s", path);
10208309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10218309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	device_watch = g_dbus_add_signal_watch(connection, NULL, path,
10228309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"org.freedesktop.Hal.Device",
10238309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"PropertyModified",
10248309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					handle_hal_property_modified,
10258309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					NULL, NULL);
10268309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz
10278309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.last_full", &battchg_last);
10288309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.current", &battchg_cur);
10298309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	hal_get_integer(path, "battery.charge_level.design", &battchg_design);
10308309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentzdone:
10318309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	dbus_message_unref(reply);
1032a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1033a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1034a34c155f55270b917795d003be24488f53d9b711Forrest Zhaoint telephony_init(void)
1035a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1036a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	const char *battery_cap = "battery";
1037a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	int ret;
1038a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1039a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1040a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10418309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	registration_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
10428309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					OFONO_NETWORKREG_INTERFACE,
10438309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"PropertyChanged",
10448309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					handle_registration_property_changed,
10458309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					NULL, NULL);
1046a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10478309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	voice_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
10488309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					OFONO_VCMANAGER_INTERFACE,
10498309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					"PropertyChanged",
10508309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					handle_vcmanager_property_changed,
10518309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz					NULL, NULL);
1052a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1053a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call(OFONO_BUS_NAME, OFONO_PATH,
1054a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				OFONO_MANAGER_INTERFACE, "GetProperties",
1055a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				list_modem_reply, NULL, DBUS_TYPE_INVALID);
1056a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1057a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1058a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1059a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	ret = send_method_call("org.freedesktop.Hal",
1060a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"/org/freedesktop/Hal/Manager",
1061a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"org.freedesktop.Hal.Manager",
1062a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				"FindDeviceByCapability",
1063a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				hal_find_device_reply, NULL,
1064a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_STRING, &battery_cap,
1065a34c155f55270b917795d003be24488f53d9b711Forrest Zhao				DBUS_TYPE_INVALID);
1066a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	if (ret < 0)
1067a34c155f55270b917795d003be24488f53d9b711Forrest Zhao		return ret;
1068a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1069a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	debug("telephony_init() successfully");
1070a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1071a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	return ret;
1072a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1073a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1074a34c155f55270b917795d003be24488f53d9b711Forrest Zhaovoid telephony_exit(void)
1075a34c155f55270b917795d003be24488f53d9b711Forrest Zhao{
1076a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(net.operator_name);
1077a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1078a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(modem_obj_path);
1079a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_free(last_dialed_number);
1080a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1081a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_slist_foreach(calls, (GFunc) vc_free, NULL);
1082a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	g_slist_free(calls);
1083a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	calls = NULL;
1084a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
10858309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, registration_watch);
10868309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, voice_watch);
10878309018a3f1e381c95a019071b6e870e6363bd9bLuiz Augusto von Dentz	g_dbus_remove_watch(connection, device_watch);
1088a34c155f55270b917795d003be24488f53d9b711Forrest Zhao
1089a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	dbus_connection_unref(connection);
1090a34c155f55270b917795d003be24488f53d9b711Forrest Zhao	connection = NULL;
1091a34c155f55270b917795d003be24488f53d9b711Forrest Zhao}
1092