forked from esmil/lem-dbus
/
add.c
285 lines (231 loc) · 6.32 KB
/
add.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* This file is part of lem-dbus.
* Copyright 2011 Emil Renner Berthing
*
* lem-dbus 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 3 of
* the License, or (at your option) any later version.
*
* lem-dbus 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 lem-dbus. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALLINONE
#include <lem.h>
#include <dbus/dbus.h>
#define EXPORT
#endif
enum add_return {
ADD_OK = 0,
ADD_ERROR
};
typedef enum add_return
(*add_function)(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args);
static add_function get_addfunc(DBusSignatureIter *type);
static enum add_return
add_error(lua_State *L, int index, int expected)
{
lua_pushfstring(L, "(%s expected, got %s)",
lua_typename(L, expected),
lua_typename(L, lua_type(L, index)));
return ADD_ERROR;
}
static enum add_return
add_not_implemented(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
(void)index;
(void)args;
lua_pushfstring(L, "(adding type '%s' not implemented yet)",
dbus_signature_iter_get_signature(type));
return ADD_ERROR;
}
static enum add_return
add_byte(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
unsigned char n;
(void)type;
if (!lua_isnumber(L, index))
return add_error(L, index, LUA_TNUMBER);
n = (unsigned char)lua_tonumber(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_BYTE, &n);
return ADD_OK;
}
static enum add_return
add_boolean(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
dbus_bool_t b;
(void)type;
if (!lua_isboolean(L, index))
return add_error(L, index, LUA_TBOOLEAN);
b = lua_toboolean(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_BOOLEAN, &b);
return ADD_OK;
}
static enum add_return
add_int16(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
dbus_int16_t n;
(void)type;
if (!lua_isnumber(L, index))
return add_error(L, index, LUA_TNUMBER);
n = (dbus_int16_t)lua_tonumber(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_INT16, &n);
return ADD_OK;
}
static enum add_return
add_uint16(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
dbus_uint16_t n;
(void)type;
if (!lua_isnumber(L, index))
return add_error(L, index, LUA_TNUMBER);
n = (dbus_uint16_t)lua_tonumber(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_UINT16, &n);
return ADD_OK;
}
static enum add_return
add_int32(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
dbus_int32_t n;
(void)type;
if (!lua_isnumber(L, index))
return add_error(L, index, LUA_TNUMBER);
n = (dbus_int32_t)lua_tonumber(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_INT32, &n);
return ADD_OK;
}
static enum add_return
add_uint32(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
dbus_uint32_t n;
(void)type;
if (!lua_isnumber(L, index))
return add_error(L, index, LUA_TNUMBER);
n = (dbus_uint32_t)lua_tonumber(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_UINT32, &n);
return ADD_OK;
}
static enum add_return
add_string(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
const char *s;
(void)type;
if (!lua_isstring(L, index))
return add_error(L, index, LUA_TSTRING);
s = lua_tostring(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_STRING, &s);
return ADD_OK;
}
static enum add_return
add_object_path(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
const char *s;
(void)type;
if (!lua_isstring(L, index))
return add_error(L, index, LUA_TSTRING);
s = lua_tostring(L, index);
dbus_message_iter_append_basic(args, DBUS_TYPE_OBJECT_PATH, &s);
return ADD_OK;
}
static enum add_return
add_array(lua_State *L, int index,
DBusSignatureIter *type, DBusMessageIter *args)
{
DBusSignatureIter array_type;
DBusMessageIter array_args;
char *signature;
add_function af;
int i;
if (!lua_istable(L, index))
return add_error(L, index, LUA_TTABLE);
dbus_signature_iter_recurse(type, &array_type);
signature = dbus_signature_iter_get_signature(&array_type);
dbus_message_iter_open_container(args, DBUS_TYPE_ARRAY,
signature, &array_args);
af = get_addfunc(&array_type);
i = 1;
while (1) {
lua_rawgeti(L, index, i);
if (lua_isnil(L, -1))
break;
if (af(L, -1, &array_type, &array_args) != ADD_OK) {
lua_insert(L, -2);
lua_pop(L, 1);
return ADD_ERROR;
}
lua_pop(L, 1);
i++;
}
lua_pop(L, 1);
dbus_free(signature);
dbus_message_iter_close_container(args, &array_args);
return ADD_OK;
}
static add_function
get_addfunc(DBusSignatureIter *type)
{
switch (dbus_signature_iter_get_current_type(type)) {
case DBUS_TYPE_BOOLEAN:
return add_boolean;
case DBUS_TYPE_BYTE:
return add_byte;
case DBUS_TYPE_INT16:
return add_int16;
case DBUS_TYPE_UINT16:
return add_uint16;
case DBUS_TYPE_INT32:
return add_int32;
case DBUS_TYPE_UINT32:
return add_uint32;
case DBUS_TYPE_STRING:
return add_string;
case DBUS_TYPE_OBJECT_PATH:
return add_object_path;
case DBUS_TYPE_ARRAY:
return add_array;
}
return add_not_implemented;
}
EXPORT unsigned int
lem_dbus_add_arguments(lua_State *L, int start,
const char *signature, DBusMessage *msg)
{
DBusMessageIter args;
DBusSignatureIter type;
int i = start;
int end = lua_gettop(L);
dbus_message_iter_init_append(msg, &args);
dbus_signature_iter_init(&type, signature);
do {
if (i > end) {
lua_pushfstring(L, "type error adding value #%d "
"of '%s' (too few arguments)",
i - start + 1, signature);
return -1;
}
if ((get_addfunc(&type))(L, i, &type, &args)) {
lua_pushfstring(L, "type error adding value #%d of '%s' ",
i - start + 1, signature);
lua_insert(L, -2);
lua_concat(L, 2);
return -1;
}
i++;
} while (dbus_signature_iter_next(&type));
return 0;
}