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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* NEARDAL (Neard Abstraction Library)
*
* Copyright 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2
* as published by the Free Software Foundation.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <glib-object.h>
#include "dbus/dbus.h"
#include "neard-manager-proxy.h"
#include "neard-adapter-proxy.h"
#include "neardal.h"
#include "neardal_prv.h"
#define g_marshal_value_peek_string(v) (char *) g_value_get_string(v)
#define g_marshal_value_peek_boxed(v) g_value_get_boxed(v)
void
neardal_marshal_VOID__STRING_BOXED(GClosure *closure, GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data)
{
typedef void (*GMarshalFunc_VOID__STRING_BOXED)(gpointer data1, gpointer arg_1,
gpointer arg_2, gpointer data2);
register GMarshalFunc_VOID__STRING_BOXED callback;
register gpointer data1, data2;
register GCClosure *cc = (GCClosure *)closure;
GValue *value = NULL;
g_return_if_fail(n_param_values == 3);
(void) return_value; /* remove warning */
(void) invocation_hint; /* remove warning */
if (G_CCLOSURE_SWAP_DATA(closure)) {
data1 = closure->data;
data2 = g_value_peek_pointer(param_values + 0);
} else {
data1 = g_value_peek_pointer(param_values + 0);
data2 = closure->data;
}
callback = (GMarshalFunc_VOID__STRING_BOXED) (marshal_data ?
marshal_data : cc->callback);
if (G_IS_VALUE(g_marshal_value_peek_boxed(param_values + 2)))
value = g_marshal_value_peek_boxed(param_values + 2);
callback(data1, g_marshal_value_peek_string(param_values + 1), value,
data2);
}
/******************************************************************************
* neardal_tools_prv__g_ptr_array_copy: duplicate a 'GPtrArray' array
*****************************************************************************/
static void neardal_tools_g_ptr_array_add(gpointer data, gpointer array)
{
char *tmp;
tmp = g_strdup(data);
g_ptr_array_add(array, tmp);
}
void neardal_tools_prv_g_ptr_array_copy(GPtrArray **target, GPtrArray *source)
{
if (*target == NULL)
*target = g_ptr_array_sized_new(source->len);
g_ptr_array_foreach(source, neardal_tools_g_ptr_array_add, *target);
}
/******************************************************************************
* neardal_tools_prv_g_ptr_array_free: free a 'GPtrArray' array
*****************************************************************************/
static void neardal_tools_g_ptr_array_free_node(gpointer data, gpointer array)
{
(void) array; /* remove warning */
g_free(data);
}
void neardal_tools_prv_g_ptr_array_free(GPtrArray *array)
{
g_ptr_array_foreach(array, neardal_tools_g_ptr_array_free_node, NULL);
g_ptr_array_free(array, TRUE);
}
/******************************************************************************
* neardal_tools_prv_create_proxy: create dbus proxy to Neard daemon
*****************************************************************************/
errorCode_t neardal_tools_prv_create_proxy(DBusGConnection *conn,
DBusGProxy **oProxy, const char *path,
const char *iface)
{
DBusGProxy *proxy = NULL;
GError *gerror = NULL;
g_assert(conn != NULL);
g_assert(oProxy != NULL);
g_assert(path != NULL);
g_assert(iface != NULL);
NEARDAL_TRACEIN();
NEARDAL_TRACEF("Trying connection to (path:'%s', interface:'%s')...\n",
path, iface);
proxy = dbus_g_proxy_new_for_name_owner(conn, NEARD_DBUS_SERVICE, path,
iface, &gerror);
if (proxy == NULL) {
NEARDAL_TRACE_ERR(
"Unable to connect to (path:'%s', interface:'%s') (err:'%s')\n",
path, iface, gerror->message);
g_error_free(gerror);
return NEARDAL_ERROR_DBUS_CANNOT_CREATE_PROXY;
}
NEARDAL_TRACEF("Connection to (path:'%s', interface:'%s') OK!\n", path,
iface);
/*TODO g_signal_connect (G_OBJECT (proxy), "destroy",
* G_CALLBACK(destroy_signal), proxy); */
*oProxy = proxy;
return NEARDAL_SUCCESS;
}
/******************************************************************************
* neardal_tools_prv_free_gerror: freeing gerror in neardal context
*****************************************************************************/
void neardal_tools_prv_free_gerror(neardal_t neardalObj)
{
g_assert(neardalObj != NULL);
if (neardalObj->gerror != NULL)
g_error_free(neardalObj->gerror);
neardalObj->gerror = NULL;
}
/******************************************************************************
* neardal_tools_prv_cmp_path: Compare dbus path.
* return true (<>0) if path is same, 0 otherwise
*****************************************************************************/
int neardal_tools_prv_cmp_path(const char *neardalPath, const char *reqPath)
{
const char *shortest;
const char *longest;
int len, lenNfcPath, lenReqPath;
int ret = FALSE;
g_assert(neardalPath != NULL);
g_assert(reqPath != NULL);
lenNfcPath = strlen(neardalPath);
lenReqPath = strlen(reqPath);
if (lenNfcPath > lenReqPath) {
shortest = reqPath;
longest = neardalPath;
len = lenReqPath;
} else {
shortest = neardalPath;
longest = reqPath;
len = lenNfcPath;
}
if (!strncmp(shortest, longest, len)) {
if (longest[len] == '/' || longest[len] == '\0')
ret = TRUE;
else
ret = FALSE;
}
return ret;
}
/******************************************************************************
* neardal_tools_prv_hashtable_get: Parse a hashtable and get value of GType
* 'type' with a specific key
*****************************************************************************/
errorCode_t neardal_tools_prv_hashtable_get(GHashTable *hashTable,
gconstpointer key, GType gtype,
void *value)
{
gpointer valueGp = NULL;
errorCode_t errCode = NEARDAL_ERROR_GENERAL_ERROR;
gboolean found;
NEARDAL_TRACEF("key'%s'=", key);
found = g_hash_table_lookup_extended(hashTable, key, NULL, &valueGp);
if (found == FALSE) {
NEARDAL_TRACE_ERR("key '%s' NOT FOUND!!!\n", key);
return errCode;
}
if (valueGp == NULL)
goto error;
if (G_VALUE_HOLDS(valueGp, gtype) != TRUE) {
NEARDAL_TRACE_ERR(
"Unknown GType (waiting 0x%X='%s' instead of 0x%X='%s')",
gtype, g_type_name(gtype),
G_VALUE_TYPE(valueGp),
g_type_name(G_VALUE_TYPE(valueGp)));
goto error;
}
if (value == NULL)
goto error;
switch (gtype) {
case G_TYPE_BOOLEAN:
*((gboolean *)value) = g_value_get_boolean(valueGp);
NEARDAL_TRACE("(boolean) %d", *(gboolean *)value);
errCode = NEARDAL_SUCCESS;
break;
case G_TYPE_STRING:
*((const gchar * *)value) = g_value_get_string(valueGp);
NEARDAL_TRACE("(string) %s", *(const gchar * *)value);
errCode = NEARDAL_SUCCESS;
break;
default:
if (gtype == G_TYPE_STRV) {
void *tmp;
tmp = g_value_get_boxed(valueGp);
*((gchar ***)value) = tmp;
NEARDAL_TRACE("(array of strings, first string =) %s",
((gchar **) tmp)[0]);
errCode = NEARDAL_SUCCESS;
break;
}
if (gtype == DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH) {
GPtrArray *tmp = NULL;
tmp = g_value_get_boxed(valueGp);
errCode = NEARDAL_ERROR_GENERAL_ERROR;
if (tmp == NULL)
break;
NEARDAL_TRACE("(array of DBusGObjectPath: ");
if (tmp->len > 0)
NEARDAL_TRACE(
"%d elements, first path=%s)",
tmp->len,
g_ptr_array_index(tmp, 0));
else
NEARDAL_TRACE(" (Empty!)");
*((GPtrArray **)value) = tmp;
errCode = NEARDAL_SUCCESS;
break;
}
NEARDAL_TRACE_ERR(
"Unknown GType (waiting 0x%X='%s' instead of 0x%X='%s')",
gtype, g_type_name(gtype),
G_VALUE_TYPE(valueGp),
g_type_name(G_VALUE_TYPE(valueGp)));
break;
}
NEARDAL_TRACE("\n");
return errCode;
error:
NEARDAL_TRACE_ERR("key '%s' NOT FOUND!!!\n", key);
return errCode;
}
/******************************************************************************
* neardal_tools_create_dict: Create a GHashTable for dict_entries.
*****************************************************************************/
GHashTable *neardal_tools_create_dict(void)
{
return g_hash_table_new(g_str_hash, g_str_equal);
}
/******************************************************************************
* neardal_tools_add_dict_entry: add an entry in a dictionnary
*****************************************************************************/
errorCode_t neardal_tools_add_dict_entry(GHashTable *hash, gchar *key,
gchar *value)
{
errorCode_t err = NEARDAL_ERROR_NO_MEMORY;
GValue *gvalue;
g_assert(hash != NULL);
gvalue = g_try_malloc0(sizeof(GValue));
if (gvalue == NULL)
goto error;
g_value_init (gvalue, G_TYPE_STRING);
g_value_set_string (gvalue, value);
g_hash_table_insert (hash, (char *) key, gvalue);
err = NEARDAL_SUCCESS;
error:
return err;
}
|