aboutsummaryrefslogtreecommitdiffstats
path: root/src/wgt-json.c
blob: 91660320cf534c8c25198a8036d092a73d2ce5cd (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 Copyright (C) 2015-2019 IoT.bzh

 author: José Bollo <jose.bollo@iot.bzh>

 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.
*/

#define _GNU_SOURCE

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>

#include <json-c/json.h>

#include "utils-json.h"
#include "wgt-info.h"
#include "wgt-json.h"
#include "wgt-strings.h"
#include "verbose.h"

/*
 * This describes an action to be performed for a parameter of name.
 * The action takes a not null json object, the param matched and a given closure.
 */
struct paramaction
{
	const char *name;	/* the name of the parameter trigerring the action or null (mark end) */
	int (*action)(struct json_object *obj, const struct wgt_desc_param *param, void *closure); /* the action to perform or null (no action) */
	void *closure;		/* the closure to pass to the action */
};


/*
 * Apply the first matching 'actions' to each of the given 'params' and for the given json 'obj' (that shouldn't be null)
 * Returns 0 in case of success or -1 on error.
 */
static int apply_params(struct json_object *obj, const struct wgt_desc_param *params, const struct paramaction *actions)
{
	int rc;
	const struct paramaction *a;

	if (!obj) {
		/* obj can't be null */
		errno = EINVAL;
		rc = -1;
	} else {
		/* iterate over the params */
		rc = 0;
		while(params) {
			/* search the first match */
			for (a = actions ; a->name && strcmp(a->name, params->name) ; a++);
			/* invoke the action (or not if null) */
			if (a->action && a->action(obj, params, a->closure) < 0)
				rc = -1;
			/* next */
			params = params->next;
		}
	}
	return rc;
}

/*
 * Get the array at the 'mkey' in 'obj'. 'create' it if needed and requested.
 * Returns 0 in case of success or a negative number otherwise.
 */
static int get_array(struct json_object **result, struct json_object *obj, const char *mkey, int create)
{
	/* enter the multiple key */
	int rc = j_enter_m(&obj, &mkey, create);
	if (rc < 0)
		/* parent keys doesn't exist */
		*result = NULL;
	else if (!json_object_object_get_ex(obj, mkey, result)) {
		/* parent keys exist but not the final key */
		if (!create) {
			*result = NULL;
			rc = -ENOENT;
		} else {
			/* try to create the array */
			if (!(*result = j_add_new_array(obj, mkey)))
				rc = -ENOMEM;
		}
	}
	return rc;
}

/*
 * Returns the target name (#target) of the feature 'feat'.
 * Returns 'defval' if not found or NULL is duplicated
 */
static const char *get_target_name(const struct wgt_desc_feature *feat, const char *defval)
{
	const struct wgt_desc_param *param = feat->params;

	/* search the parameter of name '#target' */
	while (param && strcmp(param->name, string_sharp_target))
		param = param->next;
	if (param) {
		/* found, get itsd value */
		defval = param->value;

		/* check it is defined only one time */
		/* TODO: validate it in an other place? */
		param = param->next;
		while (param) {
			if (!strcmp(param->name, string_sharp_target)) {
				defval = NULL;
				break;
			}
			param = param->next;
		}
	}
	return defval;
}

/*
 * Search in 'array' the first object having a field of name 'key' and of string value 'val'
 * Returns NULL if not found
 */
static struct json_object *get_array_item_by_key(struct json_object *array, const char *key, const char *val)
{
	struct json_object *result, *k;
	int i, n;

	n = json_object_array_length(array);
	for (i = 0 ; i < n ; i++) {
		result = json_object_array_get_idx(array, i);
		if (result && json_object_object_get_ex(result, key, &k)
			&& json_object_get_type(k) == json_type_string
			&& !strcmp(json_object_get_string(k), val))
			return result;
	}
	return NULL;
}

/*
 * Get or create from the array 'targets' the structure for the target of 'name'
 * Should create if 'create' is not not otherwise just get.
 * Returns zero on success or a errno negative code
 */
static int get_target(struct json_object **result, struct json_object *targets, const char *name, int create)
{
	int rc;
	struct json_object *t;

	/* search in targets a structure of target name */
	t = get_array_item_by_key(targets, string_sharp_target, name);
	if (t) {
		/* found */
		if (!create)
			rc = 0;
		else {
			ERROR("duplicated target name: %s", name);
			t = NULL;
			rc = -EEXIST;
		}
	} else {
		/* not found */
		if (!create) {
			ERROR("target name not found: %s", name);
			rc = -ENOENT;
		} else {
			/* create a new value */
			t = j_add_new_object(targets, NULL);
			if (t && j_add_string(t, string_sharp_target, name))
				rc = 0;
			else {
				json_object_put(t);
				t = NULL;
				rc = -ENOMEM;
			}
		}
	}
	*result = t;
	return rc;
}

/* create the target value for the feature */
static int create_target(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	const char *id;
	struct json_object *target;

	/* search within the feature the parameter naming the target */
	id = get_target_name(feat, NULL);
	if (id == NULL) {
		ERROR("target of feature %s is %s", feat->name, get_target_name(feat, feat->name) ? "repeated" : "missing");
		return -EINVAL;
	}

	/* create the target */
	return get_target(&target, targets, id, 1);
}

/*
 * Adds icon data to the target
 */
static int add_icon(struct json_object *target, const struct wgt_desc_icon *icon)
{
	int rc;
	struct json_object *object, *array;

	rc = get_array(&array, target, string_icon, 1);
	if (rc >= 0) {
		object = json_object_new_object();
		if(!object
		|| !j_add_string(object, string_src, icon->src)
		|| (icon->width > 0 && !j_add_integer(object, string_width, icon->width))
		|| (icon->height > 0 && !j_add_integer(object, string_height, icon->height))
		|| !j_add(array, NULL, object)) {
			json_object_put(object);
			rc = -ENOMEM;
		}
	}
	return rc;
}

/*
 * Creates the main target
 */
static int create_main_target(struct json_object *targets, const struct wgt_desc *desc)
{
	int rc;
	struct wgt_desc_icon *icon;
	struct json_object *target;

	/* create the target 'main' */
	rc = get_target(&target, targets, string_main, 1);

	/* add icons if any */
	icon = desc->icons;
	while (rc >= 0 && icon) {
		rc = add_icon(target, icon);
		icon = icon->next;
	}

	/* add content */
	if (rc >= 0)
		rc = j_add_many_strings_m(target,
			"content.src", desc->content_src,
			"content.type", desc->content_type,
			"content.encoding", desc->content_encoding,
			NULL) ? 0 : -errno;

	/* add other info */
	if (rc >= 0)
		if((desc->width && !j_add_integer(target, string_width, desc->width))
		|| (desc->height && !j_add_integer(target, string_height, desc->height))
		|| (desc->viewmodes && !j_add_string(target, string_viewmodes,  desc->viewmodes))
		|| (desc->defaultlocale && !j_add_string(target, string_defaultlocale,  desc->defaultlocale)))
			rc = -ENOMEM;

	return rc;
}

/***********************************************************************************************************/

/*
 * translate a param to an object { "name": name, "value": value }
 */
static struct json_object *object_of_param(const struct wgt_desc_param *param)
{
	struct json_object *value;

	value = json_object_new_object();
	if (value
	 && j_add_string(value, string_name, param->name)
	 && j_add_string(value, string_value, param->value))
		return value;

	json_object_put(value);
	return NULL;
}

/*
 * Add the field of mkey 'param'.name with 'param'.value to the object 'obj'
 */
static int add_param_simple(struct json_object *obj, const struct wgt_desc_param *param, void *closure)
{
	return j_add_string_m(obj, param->name, param->value);
}

/* add a param object to an array of param objects */
static int add_param_array(struct json_object *obj, const struct wgt_desc_param *param, void *closure)
{
	const char *array_name = closure;
	struct json_object *array, *value;

	/* the array is either pointed by 'closure=array_name' or the given 'obj' */
	if (!array_name)
		array = obj;
	else if (!json_object_object_get_ex(obj, array_name, &array)) {
		array = j_add_new_array(obj, array_name);
		if (!array)
			return -ENOMEM;
	}

	/* append the param object */
	value = object_of_param(param);
	if (value && j_add(array, NULL, value))
		return 0;

	json_object_put(value);
	return -ENOMEM;
}

/* add a param object to an object of param objects, indexed by the param name */
static int add_param_object(struct json_object *obj, const struct wgt_desc_param *param, void *closure)
{
	const char *object_name = closure;
	struct json_object *object, *value;

	/* the object is either pointed by 'object_name=closure' or the given 'obj' */
	if (!object_name)
		object = obj;
	else if (!json_object_object_get_ex(obj, object_name, &object)) {
		object = j_add_new_object(obj, object_name);
		if (!object)
			return -ENOMEM;
	}

	/* append the param object */
	value = object_of_param(param);
	if (value && j_add(object, param->name, value))
		return 0;

	json_object_put(value);
	return -ENOMEM;
}

/* Retrieve within 'targets' the target of the feature 'feat' and applies 'actions' to it */
static int add_targeted_params(struct json_object *targets, const struct wgt_desc_feature *feat, struct paramaction actions[])
{
	int rc;
	const char *id;
	struct json_object *obj;

	id = get_target_name(feat, string_main);
	rc = get_target(&obj, targets, id, 0);
	return rc < 0 ? rc : apply_params(obj, feat->params, actions);
}

/* Treats the feature "provided_unit" */
static int add_provided_unit(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* skip #target */
		{ .name = NULL, .action = add_param_simple, .closure = NULL }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "provided_api" */
static int add_provided_api(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* skip #target */
		{ .name = NULL, .action = add_param_array, .closure = (void*)string_provided_api }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "required_api" */
static int add_required_api(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* skip #target */
		{ .name = NULL, .action = add_param_array, .closure = (void*)string_required_api }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "provided_binding" */
static int add_provided_binding(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* TODO: should be an error! */
		{ .name = NULL, .action = add_param_array, .closure = (void*)string_provided_binding }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "required_binding" */
static int add_required_binding(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* skip #target */
		{ .name = NULL, .action = add_param_array, .closure = (void*)string_required_binding }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "required_permission" */
static int add_required_permission(struct json_object *targets, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = string_sharp_target, .action = NULL, .closure = NULL }, /* skip #target */
		{ .name = NULL, .action = add_param_object, .closure = (void*)string_required_permission }
	};
	return add_targeted_params(targets, feat, actions);
}

/* Treats the feature "defined_permission" */
static int add_defined_permission(struct json_object *defperm, const struct wgt_desc_feature *feat)
{
	static struct paramaction actions[] = {
		{ .name = NULL, .action = add_param_array, .closure = NULL }
	};
	return apply_params(defperm, feat->params, actions);
}

/***********************************************************************************************************/

/*
 * Create the json object from 'desc'
 */
static struct json_object *to_json(const struct wgt_desc *desc)
{
	size_t prefixlen;
	const struct wgt_desc_feature *feat;
	const char *featname;
	struct json_object *result, *targets, *permissions;
	int rc, rc2;

	/* create the application structure */
	if(!(result = json_object_new_object())
	|| !(targets = j_add_new_array(result, string_targets))
	|| !(permissions = j_add_new_array(result, string_defined_permission))
	)
		goto error;

	/* first pass: declarations */
	rc = create_main_target(targets, desc);
	prefixlen = strlen(string_AGL_widget_prefix);
	for (feat = desc->features ; feat ; feat = feat->next) {
		featname = feat->name;
		if (!memcmp(featname, string_AGL_widget_prefix, prefixlen)) {
			if (!feat->required) {
				ERROR("feature %s can't be optional", featname);
				if (!rc)
					rc = -EINVAL;
			}
			featname += prefixlen;
			if (!strcmp(featname, string_provided_unit)) {
				rc2 = create_target(targets, feat);
				if (rc2 < 0 && !rc)
					rc = rc2;
			}
		}
	}

	/* second pass: definitions */
	for (feat = desc->features ; feat ; feat = feat->next) {
		featname = feat->name;
		if (!memcmp(featname, string_AGL_widget_prefix, prefixlen)) {
			featname += prefixlen;
			if (!strcmp(featname, string_defined_permission)) {
				rc2 = add_defined_permission(permissions, feat);
			}
			else if (!strcmp(featname, string_provided_unit)) {
				rc2 = add_provided_unit(targets, feat);
			}
			else if (!strcmp(featname, string_provided_api)) {
				rc2 = add_provided_api(targets, feat);
			}
			else if (!strcmp(featname, string_provided_binding)) {
				rc2 = add_provided_binding(targets, feat);
			}
			else if (!strcmp(featname, string_required_api)) {
				rc2 = add_required_api(targets, feat);
			}
			else if (!strcmp(featname, string_required_binding)) {
				rc2 = add_required_binding(targets, feat);
			}
			else if (!strcmp(featname, string_required_permission)) {
				rc2 = add_required_permission(targets, feat);
			} else {
				/* gently ignore other features */
				rc2 = 0;
			}
			if (rc2 < 0 && !rc)
				rc = rc2;
		}
	}

	/* fills the main */
	rc2 = j_add_many_strings_m(result,
		string_id, desc->id,
		string_idaver, desc->idaver,
		string_version, desc->version,
		"ver", desc->ver,
		"author.content", desc->author,
		"author.href", desc->author_href,
		"author.email", desc->author_email,
		"license.content", desc->license,
		"license.href", desc->license_href,
		"defaultlocale", desc->defaultlocale,
		"name.content", desc->name,
		"name.short", desc->name_short,
		"description", desc->description,
		NULL) ? 0 : -errno;
	if (rc2 < 0 && !rc)
		rc = rc2;

	/* returns the result if there is no error*/
	if (!rc)
		return result;

error:
	json_object_put(result);
	return NULL;
}

/* get the json_object of the wgt_info 'info' or NULL if error */
struct json_object *wgt_info_to_json(struct wgt_info *info)
{
	return to_json(wgt_info_desc(info));
}

/* get the json_object of the 'wgt' or NULL if error */
struct json_object *wgt_to_json(struct wgt *wgt)
{
	struct json_object *result;
	struct wgt_info *info;

	info = wgt_info_create(wgt, 1, 1, 1);
	if (info == NULL)
		result = NULL;
	else {
		result = wgt_info_to_json(info);
		wgt_info_unref(info);
	}
	return result;
}

/* get the json_object of the widget of 'path' relative to 'dfd' or NULL if error */
struct json_object *wgt_path_at_to_json(int dfd, const char *path)
{
	struct json_object *result;
	struct wgt_info *info;

	info = wgt_info_createat(dfd, path, 1, 1, 1);
	if (info == NULL)
		result = NULL;
	else {
		result = wgt_info_to_json(info);
		wgt_info_unref(info);
	}
	return result;
}

/* get the json_object of the widget of 'path' or NULL if error */
struct json_object *wgt_path_to_json(const char *path)
{
	return wgt_path_at_to_json(AT_FDCWD, path);
}