summaryrefslogtreecommitdiff
path: root/hfpd/dbus-marshall.h
blob: faa7cb5e4228dd2056b90de41135949ab3f28ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* -*- C++ -*- */
/*
 * Software Bluetooth Hands-Free Implementation
 *
 * Copyright (C) 2008 Sam Revitch <samr7@cs.washington.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#if !defined(__HFPD_DBUS_MARSHALL_H__)
#define __HFPD_DBUS_MARSHALL_H__

/*
 * Marshalling in this dbus binding is not automatic at all, but
 * can be configured through functions set in DbusMethod and
 * DbusProperty.
 *
 * At present, only property marshalling is supported.
 *
 * The sanctioned way of describing interfaces is by initializing arrays
 * of DbusMethod and DbusProperty structures, and assigning names,
 * D-Bus type signatures, and function pointers.  To help with adapting
 * the function pointer signature to more useful one, there are some
 * templatized methods defined below, some of which support argument
 * marshalling from D-Bus.
 */

/*
 * To support marshalling/demarshalling of user-defined types,
 * a DbusTypeTraits specialization must be defined for the type.
 *
 * The specialization must follow the form:
 * template <> struct DbusTypeTraits<UserType> {
 *	typedef UserType type_t;
 *	static const char *Sig(void);
 *	static bool Marshall(DBusMessageIter &iter, const T &arg);
 *	static bool Demarshall(DBusMessageIter &iter, T &arg);
 *	static void DemarshallDelete(T &arg);
 * }
 *
 * The Signature method must return a static-storage string of the
 * D-Bus type signature, e.g. "ss(ias)a{is}".  See the D-Bus
 * specification for more information on type signatures.
 *
 * The Marshall method must use its iter argument to append the
 * D-Bus message representation of the type to a DBusMessage.  It
 * returns true on success.  A return value of false is interpreted
 * as resource exhaustion, i.e. malloc() failure.
 *
 * The Demarshall method must use its iter argument to unpack or
 * instantiate the user-defined type from its D-Bus message
 * representation.  It returns true on success.  A return value of
 * false is interpreted as resource exhaustion, i.e. malloc() failure.
 *
 * The DemarshallDestroy method may optionally release memory allocated
 * by the Demarshall method.  This makes it possible to use simple
 * pointer types, i.e. Object*, to represent a demarshalled parameter
 * instead of defining a smart pointer container type with a destructor.
 */

struct DbusValue {
	union {
		void		*ptr;
		void		(DbusValue::*membfunc)(void);
#if defined(DBUS_HAVE_INT64)
		dbus_int64_t	i64;
#endif /* defined(DBUS_HAVE_INT64) */
	}			m_value;
	bool			m_simple;
};

/* DbusTypeTraitsBasic supports DBus basic types */
template <typename T, int DBT>
struct DbusTypeTraitsBasic {
	typedef T type_t;
	static const char *Signature(void)
		{ static const char sig[2] = { DBT, 0 }; return sig; }
	static bool Marshall(DBusMessageIter &iter, const T &arg)
		{ return dbus_message_iter_append_basic(&iter, DBT, &arg); }
	static bool Demarshall(DBusMessageIter &iter, T &arg) {
		assert(dbus_message_iter_get_arg_type(&iter) == DBT);
		dbus_message_iter_get_basic(&iter, &arg);
		return true;
	}
	static void DemarshallDestroy(T &arg) {}
};

/* DbusTypeTraits for most basic types, defined below */
template <typename T> struct DbusTypeTraits { };
template <> struct DbusTypeTraits<unsigned char>
	: public DbusTypeTraitsBasic<unsigned char, DBUS_TYPE_BYTE> {};
template <> struct DbusTypeTraits<dbus_int16_t>
	: public DbusTypeTraitsBasic<dbus_int16_t, DBUS_TYPE_INT16> {};
template <> struct DbusTypeTraits<dbus_uint16_t>
	: public DbusTypeTraitsBasic<dbus_uint16_t, DBUS_TYPE_UINT16> {};
template <> struct DbusTypeTraits<dbus_int32_t>
	: public DbusTypeTraitsBasic<dbus_int32_t, DBUS_TYPE_INT32> {};
template <> struct DbusTypeTraits<dbus_uint32_t>
	: public DbusTypeTraitsBasic<dbus_uint32_t, DBUS_TYPE_UINT32> {};
#if defined(DBUS_HAVE_INT64)
template <> struct DbusTypeTraits<dbus_int64_t>
	: public DbusTypeTraitsBasic<dbus_int64_t, DBUS_TYPE_INT64> {};
template <> struct DbusTypeTraits<dbus_uint64_t>
	: public DbusTypeTraitsBasic<dbus_uint64_t, DBUS_TYPE_UINT64> {};
#endif /* defined(DBUS_HAVE_INT64) */
/* Boolean help: bool works, except that dbus_bool_t is a 32-bit integer */
template <> struct DbusTypeTraits<bool>
	: public DbusTypeTraitsBasic<bool, DBUS_TYPE_BOOLEAN> {
	static bool Marshall(DBusMessageIter &iter, const bool &arg) {
		dbus_bool_t xarg = arg;
		return dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN,
						      &xarg);
	}
	static bool Demarshall(DBusMessageIter &iter, bool &arg) {
		dbus_bool_t xarg;
		assert(dbus_message_iter_get_arg_type(&iter) ==
		       DBUS_TYPE_BOOLEAN);
		dbus_message_iter_get_basic(&iter, &xarg);
		arg = xarg;
		return true;
	}
};
template <> struct DbusTypeTraits<const char *>
	: public DbusTypeTraitsBasic<const char *, DBUS_TYPE_STRING> {};
template <> struct DbusTypeTraits<double>
	: public DbusTypeTraitsBasic<double, DBUS_TYPE_DOUBLE> {};
template <> struct DbusTypeTraits<float>
	: public DbusTypeTraitsBasic<float, DBUS_TYPE_DOUBLE> {
	static bool Marshall(DBusMessageIter &iter, const float &arg) {
		double xarg = arg;
		return dbus_message_iter_append_basic(&iter, DBUS_TYPE_DOUBLE,
						      &xarg);
	}
	static bool Demarshall(DBusMessageIter &iter, float &arg) {
		double xarg;
		assert(dbus_message_iter_get_arg_type(&iter) ==
		       DBUS_TYPE_DOUBLE);
		dbus_message_iter_get_basic(&iter, &xarg);
		arg = xarg;
		return true;
	}
};

template <typename T>
const char *
DbusTypeSig(T &) {
	return DbusTypeTraits<T>::Signature();
}

/*
 * @brief Dispatch a D-Bus method call with a raw DBusMessage
 * argument to a member function
 */
template <typename T, bool (T::*Func)(DBusMessage *)>
bool
DbusMethodRawMember(DbusExportObject *objp, DBusMessage *msgp)
{
	return (((T*) objp)->*Func)(msgp);
}

/*
 * @brief Dispatch a D-Bus property get call with a raw
 * DBusMessage argument to a member function, and a DBusMessageIter
 * where the result should be stored.
 *
 * This interface does not expect the callee to send a reply, and supports
 * both the org.freedesktop.DBus.Properties.{Get,GetAll} interfaces.
 */
template <typename T, bool (T::*Func)(DBusMessage *, const DbusProperty *,
				      DBusMessageIter &)>
bool
DbusPropGetRawMember(DbusExportObject *objp, DBusMessage *msgp,
		     const DbusProperty *propp, DBusMessageIter &iter)
{
	return (((T*) objp)->*Func)(msgp, propp, iter);
}

template <typename T, bool (T::*Func)(DBusMessage *, const DbusProperty *,
				      DBusMessageIter &)>
bool
DbusPropSetRawMember(DbusExportObject *objp, DBusMessage *msgp,
		     const DbusProperty *propp, DBusMessageIter &iter)
{
	return (((T*) objp)->*Func)(msgp, propp, iter);
}

/*
 * @brief Dispatch a D-Bus property get call, along with a reference to
 * a value to be marshalled, to a member function.
 *
 * This interface does not expect the callee to send a reply, and supports
 * both the org.freedesktop.DBus.Properties.{Get,GetAll} interfaces.
 */
template <typename TraitsT, typename T,
	  bool (T::*Func)(DBusMessage *msgp, typename TraitsT::type_t &)>
bool
DbusPropGetMarshallMember(DbusExportObject *objp, DBusMessage *msgp,
			  const DbusProperty *propp, DBusMessageIter &mi)
{
	typename TraitsT::type_t value;

	if (!(((T*)objp)->*Func)(msgp, value))
		return false;

	return TraitsT::Marshall(mi, value);
}

template <typename TraitsT, typename T,
	  bool (T::*Func)(DBusMessage *msgp,
			  const typename TraitsT::type_t &, bool &accept)>
bool
DbusPropSetMarshallMember(DbusExportObject *objp, DBusMessage *msgp,
			  const DbusProperty *propp, DBusMessageIter &mi)
{
	typename TraitsT::type_t value;
	bool res, accept;

	if (!TraitsT::Demarshall(mi, value))
		return false;

	accept = true;
	res = (((T*)objp)->*Func)(msgp, value, accept);

	TraitsT::DemarshallDestroy(value);

	if (!res)
		return false;
	if (!accept)
		return true;

	if (!dbus_message_get_no_reply(msgp))
		return objp->SendReplyArgs(msgp, DBUS_TYPE_INVALID);

	return true;
}

#endif /* !defined(__HFPD_DBUS_MARSHALL_H__) */