3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
7 * Copyright (C) 2011 Texas Instruments, Inc.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <sys/types.h>
41 #include <dbus/dbus.h>
43 #include "lib/bluetooth.h"
45 #include "lib/sdp_lib.h"
48 #include "gdbus/gdbus.h"
50 #include "src/adapter.h"
51 #include "src/device.h"
52 #include "src/profile.h"
53 #include "src/service.h"
55 #include "src/error.h"
57 #include "src/uuid-helper.h"
58 #include "src/dbus-common.h"
64 static GSList *devices = NULL;
67 struct btd_device *dev;
68 struct avctp *session;
69 struct btd_service *target;
70 struct btd_service *remote;
71 unsigned int avctp_id;
75 static void state_changed(struct btd_device *dev, avctp_state_t old_state,
76 avctp_state_t new_state, int err,
79 struct control *control = user_data;
80 DBusConnection *conn = btd_get_dbus_connection();
81 const char *path = device_get_path(dev);
84 case AVCTP_STATE_DISCONNECTED:
85 control->session = NULL;
86 control->player = NULL;
88 g_dbus_emit_property_changed(conn, path,
89 AUDIO_CONTROL_INTERFACE, "Connected");
90 g_dbus_emit_property_changed(conn, path,
91 AUDIO_CONTROL_INTERFACE, "Player");
94 case AVCTP_STATE_CONNECTING:
98 control->session = avctp_get(dev);
101 case AVCTP_STATE_CONNECTED:
102 g_dbus_emit_property_changed(conn, path,
103 AUDIO_CONTROL_INTERFACE, "Connected");
105 case AVCTP_STATE_BROWSING_CONNECTING:
106 case AVCTP_STATE_BROWSING_CONNECTED:
112 int control_connect(struct btd_service *service)
114 struct control *control = btd_service_get_user_data(service);
116 if (control->session)
119 control->session = avctp_connect(control->dev);
120 if (!control->session)
126 int control_disconnect(struct btd_service *service)
128 struct control *control = btd_service_get_user_data(service);
130 if (!control->session)
133 avctp_disconnect(control->session);
138 static DBusMessage *key_pressed(DBusConnection *conn, DBusMessage *msg,
139 uint8_t op, void *data)
141 struct control *control = data;
144 if (!control->session)
145 return btd_error_not_connected(msg);
147 if (!control->target)
148 return btd_error_not_supported(msg);
150 err = avctp_send_passthrough(control->session, op);
152 return btd_error_failed(msg, strerror(-err));
154 return dbus_message_new_method_return(msg);
157 static DBusMessage *control_volume_up(DBusConnection *conn, DBusMessage *msg,
160 return key_pressed(conn, msg, AVC_VOLUME_UP, data);
163 static DBusMessage *control_volume_down(DBusConnection *conn, DBusMessage *msg,
166 return key_pressed(conn, msg, AVC_VOLUME_DOWN, data);
169 static DBusMessage *control_play(DBusConnection *conn, DBusMessage *msg,
172 return key_pressed(conn, msg, AVC_PLAY, data);
175 static DBusMessage *control_pause(DBusConnection *conn, DBusMessage *msg,
178 return key_pressed(conn, msg, AVC_PAUSE, data);
181 static DBusMessage *control_stop(DBusConnection *conn, DBusMessage *msg,
184 return key_pressed(conn, msg, AVC_STOP, data);
187 static DBusMessage *control_next(DBusConnection *conn, DBusMessage *msg,
190 return key_pressed(conn, msg, AVC_FORWARD, data);
193 static DBusMessage *control_previous(DBusConnection *conn, DBusMessage *msg,
196 return key_pressed(conn, msg, AVC_BACKWARD, data);
199 static DBusMessage *control_fast_forward(DBusConnection *conn, DBusMessage *msg,
202 return key_pressed(conn, msg, AVC_FAST_FORWARD, data);
205 static DBusMessage *control_rewind(DBusConnection *conn, DBusMessage *msg,
208 return key_pressed(conn, msg, AVC_REWIND, data);
211 static gboolean control_property_get_connected(
212 const GDBusPropertyTable *property,
213 DBusMessageIter *iter, void *data)
215 struct control *control = data;
216 dbus_bool_t value = (control->session != NULL);
218 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &value);
223 static gboolean control_player_exists(const GDBusPropertyTable *property,
226 struct control *control = data;
228 return control->player != NULL;
231 static gboolean control_get_player(const GDBusPropertyTable *property,
232 DBusMessageIter *iter, void *data)
234 struct control *control = data;
236 if (!control->player)
239 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
245 static const GDBusMethodTable control_methods[] = {
246 { GDBUS_DEPRECATED_METHOD("Play", NULL, NULL, control_play) },
247 { GDBUS_DEPRECATED_METHOD("Pause", NULL, NULL, control_pause) },
248 { GDBUS_DEPRECATED_METHOD("Stop", NULL, NULL, control_stop) },
249 { GDBUS_DEPRECATED_METHOD("Next", NULL, NULL, control_next) },
250 { GDBUS_DEPRECATED_METHOD("Previous", NULL, NULL, control_previous) },
251 { GDBUS_DEPRECATED_METHOD("VolumeUp", NULL, NULL, control_volume_up) },
252 { GDBUS_DEPRECATED_METHOD("VolumeDown", NULL, NULL,
253 control_volume_down) },
254 { GDBUS_DEPRECATED_METHOD("FastForward", NULL, NULL,
255 control_fast_forward) },
256 { GDBUS_DEPRECATED_METHOD("Rewind", NULL, NULL, control_rewind) },
260 static const GDBusPropertyTable control_properties[] = {
261 { "Connected", "b", control_property_get_connected },
262 { "Player", "o", control_get_player, NULL, control_player_exists },
266 static void path_unregister(void *data)
268 struct control *control = data;
270 DBG("Unregistered interface %s on path %s", AUDIO_CONTROL_INTERFACE,
271 device_get_path(control->dev));
273 if (control->session)
274 avctp_disconnect(control->session);
276 avctp_remove_state_cb(control->avctp_id);
279 btd_service_unref(control->target);
282 btd_service_unref(control->remote);
284 devices = g_slist_remove(devices, control);
288 void control_unregister(struct btd_service *service)
290 struct btd_device *dev = btd_service_get_device(service);
292 g_dbus_unregister_interface(btd_get_dbus_connection(),
293 device_get_path(dev),
294 AUDIO_CONTROL_INTERFACE);
297 static struct control *find_control(struct btd_device *dev)
301 for (l = devices; l; l = l->next) {
302 struct control *control = l->data;
304 if (control->dev == dev)
311 static struct control *control_init(struct btd_service *service)
313 struct control *control;
314 struct btd_device *dev = btd_service_get_device(service);
316 control = find_control(dev);
320 control = g_new0(struct control, 1);
322 if (!g_dbus_register_interface(btd_get_dbus_connection(),
323 device_get_path(dev),
324 AUDIO_CONTROL_INTERFACE,
325 control_methods, NULL,
326 control_properties, control,
332 DBG("Registered interface %s on path %s", AUDIO_CONTROL_INTERFACE,
333 device_get_path(dev));
336 control->avctp_id = avctp_add_state_cb(dev, state_changed, control);
337 devices = g_slist_prepend(devices, control);
342 int control_init_target(struct btd_service *service)
344 struct control *control;
346 control = control_init(service);
350 control->target = btd_service_ref(service);
352 btd_service_set_user_data(service, control);
357 int control_init_remote(struct btd_service *service)
359 struct control *control;
361 control = control_init(service);
365 control->remote = btd_service_ref(service);
367 btd_service_set_user_data(service, control);
372 int control_set_player(struct btd_service *service, const char *path)
374 struct control *control = btd_service_get_user_data(service);
376 if (!control->session)
379 if (g_strcmp0(control->player, path) == 0)
382 control->player = path;
384 g_dbus_emit_property_changed(btd_get_dbus_connection(),
385 device_get_path(control->dev),
386 AUDIO_CONTROL_INTERFACE, "Player");