• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2007  Nokia Corporation
6  *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
7  *  Copyright (C) 2007-2008  Fabien Chevalier <fabchevalier@free.fr>
8  *
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 
34 #include <gdbus.h>
35 
36 #include "error.h"
37 
create_errno_message(DBusMessage * msg,int err)38 DBusMessage *create_errno_message(DBusMessage *msg, int err)
39 {
40 	return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
41 							strerror(err));
42 }
43 
44 /**
45   org.bluez.Error.ConnectionAttemptFailed:
46 
47   An unexpected error (other than DeviceUnreachable) error has occured while
48   attempting a connection to a device
49 */
error_connection_attempt_failed(DBusConnection * conn,DBusMessage * msg,int err)50 DBusHandlerResult error_connection_attempt_failed(DBusConnection *conn, DBusMessage *msg, int err)
51 {
52 	return error_common_reply(conn, msg,
53 			ERROR_INTERFACE ".ConnectionAttemptFailed",
54 			err > 0 ? strerror(err) : "Connection attempt failed");
55 }
56 
57 /**
58   org.bluez.Error.NotSupported:
59 
60   The remote device does not support the expected feature.
61   Examples of use: trying to connect to audio device while audio is not
62   declared in device sdp record.
63 */
error_not_supported(DBusConnection * conn,DBusMessage * msg)64 DBusHandlerResult error_not_supported(DBusConnection *conn, DBusMessage *msg)
65 {
66 	return error_common_reply(conn, msg, ERROR_INTERFACE ".NotSupported",
67 							"Not supported");
68 }
69 
70 /**
71   org.bluez.Error.Canceled:
72 
73   The operation was canceled.
74   Examples of use : autorization process canceled, connection canceled
75 */
error_canceled(DBusConnection * conn,DBusMessage * msg,const char * str)76 DBusHandlerResult error_canceled(DBusConnection *conn, DBusMessage *msg,
77 					const char *str)
78 {
79 	return error_common_reply(conn, msg, ERROR_INTERFACE ".Canceled", str);
80 }
81 
82 /**
83   org.bluez.Error.Failed:
84 
85   This is a the most generic error.
86   desc filed is MANDATORY
87 */
error_failed(DBusConnection * conn,DBusMessage * msg,const char * desc)88 DBusHandlerResult error_failed(DBusConnection *conn, DBusMessage *msg,
89 				const char * desc)
90 {
91 	return error_common_reply(conn, msg, ERROR_INTERFACE ".Failed", desc);
92 }
93 
94 /**
95   org.bluez.Error.Failed:
96 
97   This is a the most generic error, instantiated form a UNIX errno number.
98 */
error_failed_errno(DBusConnection * conn,DBusMessage * msg,int err)99 DBusHandlerResult error_failed_errno(DBusConnection *conn, DBusMessage *msg,
100 					int err)
101 {
102 	const char *desc = strerror(err);
103 
104 	return error_failed(conn, msg, desc);
105 }
106 
107 /* Helper function - internal use only */
error_common_reply(DBusConnection * conn,DBusMessage * msg,const char * name,const char * descr)108 DBusHandlerResult error_common_reply(DBusConnection *conn, DBusMessage *msg,
109 					const char *name, const char *descr)
110 {
111 	DBusMessage *derr;
112 	dbus_bool_t ret;
113 
114 	if (!conn || !msg)
115 		return DBUS_HANDLER_RESULT_HANDLED;
116 
117 	derr = dbus_message_new_error(msg, name, descr);
118 	if (!derr)
119 		return DBUS_HANDLER_RESULT_NEED_MEMORY;
120 
121 	ret = dbus_connection_send(conn, derr, NULL);
122 
123 	dbus_message_unref(derr);
124 
125 	return DBUS_HANDLER_RESULT_HANDLED;
126 }
127