summaryrefslogtreecommitdiff
path: root/lib/icl-payload.c
blob: 4e50ada5a3b03ccd813223e7301488069ccbe320 (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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdlib.h>
#include <glib.h>

#include "iotcon.h"
#include "ic-utils.h"
#include "icl.h"
#include "icl-representation.h"
#include "icl-list.h"
#include "icl-value.h"
#include "icl-resource-types.h"
#include "icl-response.h"
#include "icl-payload.h"

static GVariant* _icl_state_value_to_gvariant(GHashTable *hash);
static iotcon_list_h _icl_state_list_from_gvariant(GVariant *var);

static GVariant* _icl_state_list_to_gvariant(iotcon_list_h list)
{
	GList *node;
	GVariant *var = NULL;
	GVariantBuilder builder;
	iotcon_state_h state;
	struct icl_value_s *state_value = NULL;

	RETV_IF(NULL == list, NULL);

	g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);

	switch (list->type) {
	case IOTCON_TYPE_INT:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			g_variant_builder_add(&builder, "i", ((icl_basic_s*)state_value)->val.i);
		}
		break;
	case IOTCON_TYPE_BOOL:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			g_variant_builder_add(&builder, "b", ((icl_basic_s*)state_value)->val.b);
		}
		break;
	case IOTCON_TYPE_DOUBLE:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			g_variant_builder_add(&builder, "d", ((icl_basic_s*)state_value)->val.d);
		}
		break;
	case IOTCON_TYPE_STR:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			g_variant_builder_add(&builder, "s", ((icl_basic_s*)state_value)->val.s);
		}
		break;
	case IOTCON_TYPE_LIST:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			var = _icl_state_list_to_gvariant(((icl_val_list_s*)state_value)->list);
			g_variant_builder_add(&builder, "v", var);
		}
		break;
	case IOTCON_TYPE_STATE:
		for (node = list->list; node; node = node->next) {
			state_value = node->data;
			state = ((icl_val_state_s*)state_value)->state;
			var = _icl_state_value_to_gvariant(state->hash_table);
			g_variant_builder_add(&builder, "v", var);
		}
		break;
	default:
		ERR("Invalid type(%d)", list->type);
		return NULL;
	}

	return g_variant_builder_end(&builder);

}


static GVariantBuilder* _icl_state_value_to_gvariant_builder(GHashTable *hash)
{
	gpointer key, value;
	GHashTableIter iter;
	GVariant *var = NULL;
	iotcon_state_h state;
	GVariantBuilder *builder;
//	GVariantBuilder *state_builder;
	struct icl_value_s *state_value = NULL;

	if (NULL == hash)
		return NULL;

	builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));

	g_hash_table_iter_init(&iter, hash);
	while (g_hash_table_iter_next(&iter, &key, &value)) {
		state_value = value;
		switch (state_value->type) {
		case IOTCON_TYPE_INT:
			var = g_variant_new_int32(((icl_basic_s*)state_value)->val.i);
			break;
		case IOTCON_TYPE_BOOL:
			var = g_variant_new_boolean(((icl_basic_s*)state_value)->val.b);
			break;
		case IOTCON_TYPE_DOUBLE:
			var = g_variant_new_double(((icl_basic_s*)state_value)->val.d);
			break;
		case IOTCON_TYPE_STR:
			var = g_variant_new_string(((icl_basic_s*)state_value)->val.s);
			break;
		case IOTCON_TYPE_NULL:
			var = g_variant_new_string(IC_STR_NULL);
			break;
		case IOTCON_TYPE_LIST:
			var = _icl_state_list_to_gvariant(((icl_val_list_s*)state_value)->list);
			break;
		case IOTCON_TYPE_STATE:
			state = ((icl_val_state_s*)state_value)->state;
			var = _icl_state_value_to_gvariant(state->hash_table);
//			var = g_variant_new("a{sv}", state_builder);
			break;
		case IOTCON_TYPE_NONE:
		default:
			ERR("Invalid Type(%d)", state_value->type);
			g_variant_builder_unref(builder);
			return NULL;
		}

		if (var)
			g_variant_builder_add(builder, "{sv}", key, var);
	}

	return builder;
}


static GVariant* _icl_state_value_to_gvariant(GHashTable *hash)
{
	GVariantBuilder* builder;

	builder = _icl_state_value_to_gvariant_builder(hash);

	return g_variant_builder_end(builder);
}


static GVariant* _icl_representation_empty_gvariant(void)
{
	GVariant *value;
	GVariantBuilder types, repr, children;

	g_variant_builder_init(&types, G_VARIANT_TYPE("as"));
	g_variant_builder_init(&repr, G_VARIANT_TYPE("a{sv}"));
	g_variant_builder_init(&children, G_VARIANT_TYPE("av"));

	value = g_variant_new("(siasa{sv}av)", IC_STR_NULL, 0, &types, &repr, &children);

	return value;
}


GVariant* icl_representation_to_gvariant(iotcon_representation_h repr)
{
	GList *node;
	int ifaces = 0;
	const char *uri_path;
	GVariant *value, *child;
	iotcon_state_h state;
	GVariantBuilder *repr_gvar = NULL;
	GVariantBuilder children, resource_types;

	if (NULL == repr)
		return _icl_representation_empty_gvariant();

	/* uri path */
	uri_path = ic_utils_dbus_encode_str(repr->uri_path);

	/* Resource Types & Interfaces */
	g_variant_builder_init(&resource_types, G_VARIANT_TYPE("as"));

	if (ICL_VISIBILITY_PROP & repr->visibility) {
		if (repr->res_types) {
			for (node = repr->res_types->type_list; node; node = node->next)
				g_variant_builder_add(&resource_types, "s", node->data);
		}

		ifaces = repr->interfaces;
	}

	/* Representation */
	state = repr->state;
	if (state && (ICL_VISIBILITY_REPR & repr->visibility))
		repr_gvar = _icl_state_value_to_gvariant_builder(state->hash_table);

	/* Children */
	g_variant_builder_init(&children, G_VARIANT_TYPE("av"));

	for (node = repr->children; node; node = node->next) {
		/* generate recursively */
		child = icl_representation_to_gvariant(node->data);
		g_variant_builder_add(&children, "v", child);
	}

	value = g_variant_new("(siasa{sv}av)", uri_path, ifaces, &resource_types,
			repr_gvar, &children);

	return value;
}


void icl_state_from_gvariant(iotcon_state_h state, GVariantIter *iter)
{
	char *key;
	GVariant *var;
	const char *str_value;
	iotcon_list_h list_value;
	iotcon_value_h value = NULL;
	iotcon_state_h state_value = NULL;

	while (g_variant_iter_loop(iter, "{sv}", &key, &var)) {

		if (g_variant_is_of_type(var, G_VARIANT_TYPE_BOOLEAN)) {
			value = icl_value_create_bool(g_variant_get_boolean(var));

		} else if (g_variant_is_of_type(var, G_VARIANT_TYPE_INT32)) {
			value = icl_value_create_int(g_variant_get_int32(var));

		} else if (g_variant_is_of_type(var, G_VARIANT_TYPE_DOUBLE)) {
			value = icl_value_create_double(g_variant_get_double(var));

		} else if (g_variant_is_of_type(var, G_VARIANT_TYPE_STRING)) {
			str_value = g_variant_get_string(var, NULL);
			if (IC_STR_EQUAL == strcmp(IC_STR_NULL, str_value))
				value = icl_value_create_null();
			else
				value = icl_value_create_str(str_value);

		} else if (g_variant_is_of_type(var, G_VARIANT_TYPE_ARRAY)) {
			list_value = _icl_state_list_from_gvariant(var);
			value = icl_value_create_list(list_value);
		} else if (g_variant_is_of_type(var, G_VARIANT_TYPE("a{sv}"))) {
			GVariantIter *state_iter;
			g_variant_get(var, "(&a{sv})", &state_iter);
			icl_state_from_gvariant(state_value, state_iter);
			value = icl_value_create_state(state_value);
		}

		g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
	}

	return;
}


static iotcon_list_h _icl_state_list_from_gvariant(GVariant *var)
{
	int ret;
	GVariantIter iter;
	const GVariantType *type;
	iotcon_list_h list = NULL;

	type = g_variant_get_type(var);

	g_variant_iter_init(&iter, var);

	if (g_variant_type_equal(G_VARIANT_TYPE("ab"), type)) {
		bool b;
		ret = iotcon_list_create(IOTCON_TYPE_BOOL, &list);
		if (IOTCON_ERROR_NONE != ret) {
			ERR("iotcon_list_create() Fail(%d)", ret);
			return NULL;
		}

		while (g_variant_iter_loop(&iter, "b", &b))
			iotcon_list_add_bool(list, b, -1);
	} else if (g_variant_type_equal(G_VARIANT_TYPE("ai"), type)) {
		int i;
		ret = iotcon_list_create(IOTCON_TYPE_INT, &list);
		if (IOTCON_ERROR_NONE != ret) {
			ERR("iotcon_list_create() Fail(%d)", ret);
			return NULL;
		}

		while (g_variant_iter_loop(&iter, "i", &i))
			iotcon_list_add_int(list, i, -1);
	} else if (g_variant_type_equal(G_VARIANT_TYPE("ad"), type)) {
		double d;
		ret = iotcon_list_create(IOTCON_TYPE_DOUBLE, &list);
		if (IOTCON_ERROR_NONE != ret) {
			ERR("iotcon_list_create() Fail(%d)", ret);
			return NULL;
		}

		while (g_variant_iter_loop(&iter, "d", &d))
			iotcon_list_add_double(list, d, -1);
	} else if (g_variant_type_equal(G_VARIANT_TYPE("as"), type)) {
		char *s;
		ret = iotcon_list_create(IOTCON_TYPE_STR, &list);
		if (IOTCON_ERROR_NONE != ret) {
			ERR("iotcon_list_create() Fail(%d)", ret);
			return NULL;
		}

		while (g_variant_iter_loop(&iter, "s", &s))
			iotcon_list_add_str(list, s, -1);
	} else if (g_variant_type_equal(G_VARIANT_TYPE("v"), type)) {
		GVariant *value;
		iotcon_list_h list_value;
		iotcon_state_h state_value = NULL;

		while (g_variant_iter_loop(&iter, "v", &value)) {
			if (g_variant_is_of_type(value, G_VARIANT_TYPE_ARRAY)) {
				list_value = _icl_state_list_from_gvariant(value);
				iotcon_list_add_list(list, list_value, -1);
			} else if (g_variant_is_of_type(value, G_VARIANT_TYPE("a{sv}"))) {
				GVariantIter *state_iter;
				g_variant_get(value, "(&a{sv})", &state_iter);
				icl_state_from_gvariant(state_value, state_iter);
				iotcon_list_add_state(list, state_value, -1);
			}
		}
	}

	return list;
}


iotcon_representation_h icl_representation_from_gvariant(GVariant *var)
{
	int ret;
	GVariant *child;
	iotcon_representation_h repr;
	iotcon_state_h state;
	char *uri_path, *resource_type;
	GVariantIter *children, *repr_gvar, *resource_types;

	ret = iotcon_representation_create(&repr);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_representation_create() Fail(%d)", ret);
		return NULL;
	}

	ret = iotcon_state_create(&state);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_state_create() Fail(%d)", ret);
		iotcon_representation_destroy(repr);
		return NULL;
	}

	DBG("repr : %s", g_variant_print(var, FALSE));

	g_variant_get(var, "(&siasa{sv}av)", &uri_path, &repr->interfaces,
			&resource_types, &repr_gvar, &children);

	/* uri path */
	if (IC_STR_EQUAL != strcmp(IC_STR_NULL, uri_path))
		repr->uri_path = strdup(uri_path);

	/* resource types */
	if (g_variant_iter_n_children(resource_types)) {
		ret = iotcon_resource_types_create(&repr->res_types);
		if (IOTCON_ERROR_NONE != ret) {
			ERR("iotcon_resource_types_create() Fail(%d)", ret);
			g_variant_iter_free(resource_types);
			g_variant_iter_free(children);
			iotcon_state_destroy(state);
			iotcon_representation_destroy(repr);
			return NULL;
		}

		while (g_variant_iter_loop(resource_types, "s", &resource_type))
			iotcon_resource_types_add(repr->res_types, resource_type);
	}
	g_variant_iter_free(resource_types);

	/* attribute */
	icl_state_from_gvariant(state, repr_gvar);

	ret = iotcon_representation_set_state(repr, state);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_representation_set_state() Fail(%d)", ret);
		iotcon_state_destroy(state);
		iotcon_representation_destroy(repr);
		return NULL;
	}
	iotcon_state_destroy(state);

	/* children */
	while (g_variant_iter_loop(children, "v", &child)) {
		repr->children = g_list_append(repr->children,
				icl_representation_from_gvariant(child));
	}
	g_variant_iter_free(children);

	return repr;
}