summaryrefslogtreecommitdiff
path: root/app_control/app_control.c
blob: d2ad6a0fab55ea2a4db3753e4037fe03f41f39a9 (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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
/*
 * Copyright (c) 2011 - 2016 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 <unistd.h>
#include <string.h>
#include <errno.h>

#include <bundle.h>
#include <bundle_internal.h>
#include <aul.h>
#include <aul_svc.h>
#include <dlog.h>

#include <app_control.h>
#include <app_control_internal.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "CAPI_APPFW_APP_CONTROL"

#ifndef TIZEN_PATH_MAX
#define TIZEN_PATH_MAX 1024
#endif

#define BUNDLE_KEY_PREFIX_AUL "__AUL_"
#define BUNDLE_KEY_PREFIX_SERVICE "__APP_SVC_"

#define BUNDLE_KEY_OPERATION	"__APP_SVC_OP_TYPE__"
#define BUNDLE_KEY_URI		"__APP_SVC_URI__"
#define BUNDLE_KEY_MIME		"__APP_SVC_MIME_TYPE__"
#define BUNDLE_KEY_DATA		"__APP_SVC_DATA__"
#define BUNDLE_KEY_PACKAGE	"__APP_SVC_PKG_NAME__"
#define BUNDLE_KEY_WINDOW	"__APP_SVC_K_WIN_ID__"
#define BUNDLE_KEY_CATEGORY	"__APP_SVC_CATEGORY__"

#define LAUNCH_MODE_SIZE 8
#define LAUNCH_MODE_SINGLE "single"
#define LAUNCH_MODE_GROUP "group"

typedef enum {
	APP_CONTROL_TYPE_REQUEST,
	APP_CONTROL_TYPE_EVENT,
	APP_CONTROL_TYPE_REPLY,
} app_control_type_e;

struct app_control_s {
	int id;
	app_control_type_e type;
	bundle *data;
	int launch_pid;
};

typedef struct app_control_request_context_s {
	app_control_h app_control;
	app_control_reply_cb reply_cb;
	void *user_data;
} *app_control_request_context_h;

static int app_control_create_reply(bundle *data, struct app_control_s **app_control);

static const char *app_control_error_to_string(app_control_error_e error)
{
	switch (error) {
	case APP_CONTROL_ERROR_NONE:
		return "NONE";
	case APP_CONTROL_ERROR_INVALID_PARAMETER:
		return "INVALID_PARAMETER";
	case APP_CONTROL_ERROR_OUT_OF_MEMORY:
		return "OUT_OF_MEMORY";
	case APP_CONTROL_ERROR_APP_NOT_FOUND:
		return "APP_NOT_FOUND";
	case APP_CONTROL_ERROR_KEY_NOT_FOUND:
		return "KEY_NOT_FOUND";
	case APP_CONTROL_ERROR_KEY_REJECTED:
		return "KEY_REJECTED";
	case APP_CONTROL_ERROR_INVALID_DATA_TYPE:
		return "INVALID_DATA_TYPE";
	case APP_CONTROL_ERROR_LAUNCH_REJECTED:
		return "LAUNCH_REJECTED";
	case APP_CONTROL_ERROR_PERMISSION_DENIED:
		return "PERMISSION_DENIED";
	case APP_CONTROL_ERROR_LAUNCH_FAILED:
		return "LAUNCH_FAILED";
	case APP_CONTROL_ERROR_TIMED_OUT:
		return "TIMED_OUT";
	case APP_CONTROL_ERROR_IO_ERROR:
		return "IO ERROR";
	default:
		return "UNKNOWN";
	}
}

int app_control_error(app_control_error_e error, const char *function, const char *description)
{
	if (description)
		LOGE("[%s] %s(0x%08x) : %s", function, app_control_error_to_string(error), error, description);
	else {
		if (error == APP_CONTROL_ERROR_KEY_NOT_FOUND)
			LOGW("[%s] %s(0x%08x)", function, app_control_error_to_string(error), error);
		else
			LOGE("[%s] %s(0x%08x)", function, app_control_error_to_string(error), error);
	}

	return error;
}

static int app_control_validate_extra_data(const char *data)
{
	if (data == NULL || data[0] == '\0')
		return APP_CONTROL_ERROR_INVALID_PARAMETER;

	return APP_CONTROL_ERROR_NONE;
}

static int app_control_validate(app_control_h app_control)
{
	if (app_control == NULL || app_control->data == NULL)
		return APP_CONTROL_ERROR_INVALID_PARAMETER;

	return APP_CONTROL_ERROR_NONE;
}

static int app_control_new_id()
{
	static int sid = 0;
	return sid++;
}

int app_control_validate_internal_key(const char *key)
{
	if (strncmp(BUNDLE_KEY_PREFIX_AUL, key, strlen(BUNDLE_KEY_PREFIX_AUL)) == 0)
		return -1;

	if (strncmp(BUNDLE_KEY_PREFIX_SERVICE, key, strlen(BUNDLE_KEY_PREFIX_SERVICE)) == 0)
		return -1;

	return 0;
}

/* LCOV_EXCL_START */
static void app_control_request_result_broker(bundle *appsvc_bundle, int appsvc_request_code, aul_svc_result_val appsvc_result, void *appsvc_data)
{
	app_control_request_context_h request_context;
	app_control_h request;
	app_control_h reply = NULL;
	app_control_result_e result;
	void *user_data;
	app_control_reply_cb reply_cb;

	if (appsvc_data == NULL) {
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid app_control reply");
		return;
	}

	if (app_control_create_reply(appsvc_bundle, &reply) != 0) {
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create app_control reply");
		return;
	}

	request_context = appsvc_data;
	request = request_context->app_control;

	switch (appsvc_result) {
	case AUL_SVC_RES_OK:
		result = APP_CONTROL_RESULT_SUCCEEDED;
		break;
	case AUL_SVC_RES_NOT_OK:
		result = APP_CONTROL_RESULT_FAILED;
		break;
	case AUL_SVC_RES_CANCEL:
		result = APP_CONTROL_RESULT_CANCELED;
		break;
	default:
		result = APP_CONTROL_RESULT_CANCELED;
		break;
	}

	user_data = request_context->user_data;
	reply_cb = request_context->reply_cb;

	if (reply_cb != NULL)
		reply_cb(request, reply, result, user_data);
	else
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid callback ");

	app_control_destroy(reply);

	if (request_context->app_control != NULL)
		app_control_destroy(request_context->app_control);

	free(request_context);
}
/* LCOV_EXCL_STOP */

int app_control_create_request(bundle *data, app_control_h *app_control)
{
	struct app_control_s *app_control_request;

	if (app_control == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	app_control_request = malloc(sizeof(struct app_control_s));
	if (app_control_request == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create a app_control handle");

	app_control_request->type = APP_CONTROL_TYPE_REQUEST;

	if (data != NULL)
		app_control_request->data = bundle_dup(data);
	else
		app_control_request->data = bundle_create();

	if (app_control_request->data == NULL) {
		free(app_control_request);
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create a bundle");
	}

	app_control_request->id = app_control_new_id();
	app_control_request->launch_pid = -1;

	*app_control = app_control_request;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_create(app_control_h *app_control)
{
	return app_control_create_request(NULL, app_control);
}

int app_control_create_event(bundle *data, struct app_control_s **app_control)
{
	struct app_control_s *app_control_event;

	const char *operation;

	if (data == NULL || app_control == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	app_control_event = malloc(sizeof(struct app_control_s));
	if (app_control_event == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create a app_control handle");

	app_control_event->type = APP_CONTROL_TYPE_EVENT;
	app_control_event->data = bundle_dup(data);
	app_control_event->id = app_control_new_id();

	operation = aul_svc_get_operation(app_control_event->data);
	if (operation == NULL)
		aul_svc_set_operation(app_control_event->data, APP_CONTROL_OPERATION_DEFAULT);

	*app_control = app_control_event;

	return APP_CONTROL_ERROR_NONE;
}

/* LCOV_EXCL_START */
static int app_control_create_reply(bundle *data, struct app_control_s **app_control)
{
	struct app_control_s *app_control_reply;

	if (data == NULL || app_control == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	app_control_reply = malloc(sizeof(struct app_control_s));
	if (app_control_reply == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create a app_control handle");

	app_control_reply->type = APP_CONTROL_TYPE_REPLY;
	app_control_reply->data = bundle_dup(data);
	app_control_reply->id = app_control_new_id();

	*app_control = app_control_reply;

	return APP_CONTROL_ERROR_NONE;
}
/* LCOV_EXCL_STOP */

int app_control_destroy(app_control_h app_control)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control->type == APP_CONTROL_TYPE_REQUEST && app_control->launch_pid > 0
			&& bundle_get_val(app_control->data, AUL_SVC_K_LAUNCH_RESULT_APP_STARTED) == NULL)
		aul_remove_caller_cb(app_control->launch_pid, app_control);

	bundle_free(app_control->data);
	app_control->data = NULL;
	free(app_control);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_to_bundle(app_control_h app_control, bundle **data)
{
	if (app_control_validate(app_control) || data == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*data = app_control->data;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_operation(app_control_h app_control, const char *operation)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (operation != NULL) {
		if (aul_svc_set_operation(app_control->data, operation) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid operation");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_OPERATION);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_operation(app_control_h app_control, char **operation)
{
	const char *operation_value;

	if (app_control_validate(app_control) || operation == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	operation_value = aul_svc_get_operation(app_control->data);
	if (operation_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*operation = strdup(operation_value);
	if (*operation == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_uri(app_control_h app_control, const char *uri)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (uri != NULL) {
		if (aul_svc_set_uri(app_control->data, uri) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid URI");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_URI);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_uri(app_control_h app_control, char **uri)
{
	const char *uri_value;

	if (app_control_validate(app_control) || uri == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	uri_value = aul_svc_get_uri(app_control->data);
	if (uri_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*uri = strdup(uri_value);
	if (*uri == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_mime(app_control_h app_control, const char *mime)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (mime != NULL) {
		if (aul_svc_set_mime(app_control->data, mime) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid MIME type");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_MIME);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_mime(app_control_h app_control, char **mime)
{
	const char *mime_value;

	if (app_control_validate(app_control) || mime == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	mime_value = aul_svc_get_mime(app_control->data);
	if (mime_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*mime = strdup(mime_value);
	if (*mime == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_category(app_control_h app_control, const char *category)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (category != NULL) {
		if (aul_svc_set_category(app_control->data, category) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid Category");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_CATEGORY);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_category(app_control_h app_control, char **category)
{
	const char *category_value;

	if (app_control_validate(app_control) || category == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	category_value = aul_svc_get_category(app_control->data);
	if (category_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*category = strdup(category_value);
	if (*category == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_package(app_control_h app_control, const char *package)
{
	/* TODO: this function must be deprecated */
	return app_control_set_app_id(app_control, package);
}

int app_control_get_package(app_control_h app_control, char **package)
{
	/* TODO: this function must be deprecated */
	return app_control_get_app_id(app_control, package);
}

int app_control_set_app_id(app_control_h app_control, const char *app_id)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_id != NULL) {
		if (aul_svc_set_appid(app_control->data, app_id) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid application ID");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_PACKAGE);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_app_id(app_control_h app_control, char **app_id)
{
	const char *app_id_value;

	if (app_control_validate(app_control) || app_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	app_id_value = aul_svc_get_appid(app_control->data);
	if (app_id_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	*app_id = strdup(app_id_value);
	if (*app_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_window(app_control_h app_control, unsigned int id)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (id > 0) {
		if (aul_svc_allow_transient_app(app_control->data, id) != 0)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid id");
	} else {
		bundle_del(app_control->data, BUNDLE_KEY_WINDOW);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_window(app_control_h app_control, unsigned int *id)
{
	const char *window_id;

	if (app_control_validate(app_control) || id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	window_id = bundle_get_val(app_control->data, BUNDLE_KEY_WINDOW);
	if (window_id != NULL)
		*id = atoi(window_id);
	else
		*id = 0;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_clone(app_control_h *clone, app_control_h app_control)
{
	app_control_h app_control_clone;

	if (app_control_validate(app_control) || clone == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	app_control_clone = malloc(sizeof(struct app_control_s));
	if (app_control_clone == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "failed to create a app_control handle");

	app_control_clone->id = app_control_new_id();
	app_control_clone->type = app_control->type;
	app_control_clone->data = bundle_dup(app_control->data);

	*clone = app_control_clone;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_launch_mode(app_control_h app_control,
		app_control_launch_mode_e mode)
{
	char launch_mode[LAUNCH_MODE_SIZE] = { 0, };

	if (app_control_validate(app_control)) {
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER,
				__FUNCTION__, NULL);
	}

	switch (mode) {
	case APP_CONTROL_LAUNCH_MODE_SINGLE:
		strncpy(launch_mode, LAUNCH_MODE_SINGLE, strlen(LAUNCH_MODE_SINGLE));
		break;
	case APP_CONTROL_LAUNCH_MODE_GROUP:
		strncpy(launch_mode, LAUNCH_MODE_GROUP, strlen(LAUNCH_MODE_GROUP));
		break;
	default:
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER,
				__FUNCTION__, "invalid mode");
	}

	return aul_svc_set_launch_mode(app_control->data, launch_mode);
}

int app_control_get_launch_mode(app_control_h app_control,
		app_control_launch_mode_e *mode)
{
	const char *launch_mode;

	if (app_control_validate(app_control)) {
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER,
				__FUNCTION__, NULL);
	}

	launch_mode = aul_svc_get_launch_mode(app_control->data);
	if (launch_mode == NULL) {
		*mode = APP_CONTROL_LAUNCH_MODE_SINGLE;
	} else {
		if (!strcmp(launch_mode, LAUNCH_MODE_SINGLE)) {
			*mode = APP_CONTROL_LAUNCH_MODE_SINGLE;
		} else if (!strcmp(launch_mode, LAUNCH_MODE_GROUP)) {
			*mode = APP_CONTROL_LAUNCH_MODE_GROUP;
		} else {
			*mode = APP_CONTROL_LAUNCH_MODE_SINGLE;
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER,
					__FUNCTION__, "launch_mode is not matched");
		}
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_defapp(app_control_h app_control, const char *app_id)
{
	int ret;

	if (app_control_validate(app_control) || app_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	ret = aul_svc_set_appid(app_control->data, app_id);
	if (ret < 0)
		return app_control_error(APP_CONTROL_ERROR_IO_ERROR, __FUNCTION__, NULL);

	ret = aul_set_default_app_by_operation(app_control->data);
	if (ret < 0) {
		if (ret == AUL_R_EILLACC)
			return app_control_error(APP_CONTROL_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_IO_ERROR, __FUNCTION__, NULL);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_unset_defapp(const char *app_id)
{
	int ret;

	if (app_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	ret = aul_unset_default_app_by_operation(app_id);
	if (ret < 0) {
		if (ret == AUL_R_EILLACC)
			return app_control_error(APP_CONTROL_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_IO_ERROR, __FUNCTION__, NULL);
	}

	return APP_CONTROL_ERROR_NONE;
}

/* LCOV_EXCL_START */
static void __update_launch_pid(int launched_pid, void *data)
{
	app_control_h app_control;

	if (data == NULL)
		return;

	app_control = data;

	app_control->launch_pid = launched_pid;
}
/* LCOV_EXCL_STOP */

static void __handle_launch_result(int launched_pid, void *data)
{
	app_control_request_context_h request_context;
	app_control_h reply = NULL;
	app_control_h request;
	app_control_result_e result;
	app_control_reply_cb reply_cb;
	void *user_data;
	char callee[255] = {0, };
	char instance_id[256] = {0,};
	int ret;

	if (data == NULL)
		return;

	request_context = (app_control_request_context_h)data;

	if (app_control_create_event(request_context->app_control->data, &reply) != 0) {
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create app_control event");
		return;
	}

	ret = aul_app_get_appid_bypid(launched_pid, callee, sizeof(callee));
	if (ret < 0)
		LOGE("aul_app_get_appid_bypid failed: %d", launched_pid);

	app_control_set_app_id(reply, callee);
	LOGI("app control async result callback callee pid:%d", launched_pid);

	ret = aul_app_get_instance_id_bypid(launched_pid, instance_id,
			sizeof(instance_id));
	if (ret == AUL_R_OK) {
		app_control_set_instance_id(reply, instance_id);
		LOGI("instance id(%s)", instance_id);
	}

	result = APP_CONTROL_RESULT_APP_STARTED;
	request = request_context->app_control;
	user_data = request_context->user_data;
	reply_cb = request_context->reply_cb;

	if (reply_cb != NULL)
		reply_cb(request, reply, result, user_data);
	else
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid callback ");

	app_control_destroy(reply);
}

int app_control_send_launch_request(app_control_h app_control, app_control_reply_cb callback, void *user_data)
{
	const char *operation;
	bool implicit_default_operation = false;
	int launch_pid;
	app_control_request_context_h request_context = NULL;
	int ret;

	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	operation = aul_svc_get_operation(app_control->data);
	if (operation == NULL) {
		implicit_default_operation = true;
		operation = APP_CONTROL_OPERATION_DEFAULT;
	}

	if (!strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT))
		return app_control_error(APP_CONTROL_ERROR_LAUNCH_REJECTED, __FUNCTION__,
				"Not supported operation value");

	/* TODO: Check the privilege for call operation */

	/* operation : default */
	if (!strcmp(operation, APP_CONTROL_OPERATION_DEFAULT)) {
		const char *appid  = aul_svc_get_appid(app_control->data);
		if (appid == NULL)
			return app_control_error(APP_CONTROL_ERROR_APP_NOT_FOUND, __FUNCTION__, "package must be specified if the operation is default value");
	}

	if (callback != NULL) {
		app_control_h request_clone = NULL;

		request_context = calloc(1, sizeof(struct app_control_request_context_s));
		if (request_context == NULL)
			return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

		request_context->reply_cb = callback;

		ret = app_control_clone(&request_clone, app_control);
		if (ret != APP_CONTROL_ERROR_NONE) {
			free(request_context);
			return app_control_error(ret, __FUNCTION__, "failed to clone the app_control request handle");
		}

		request_context->app_control = request_clone;
		request_context->user_data = user_data;
	}

	if (implicit_default_operation == true)
		aul_svc_set_operation(app_control->data, APP_CONTROL_OPERATION_DEFAULT);

	launch_pid = aul_svc_run_service_for_uid(app_control->data, app_control->id, callback ? app_control_request_result_broker : NULL, request_context, getuid());
	if (implicit_default_operation == true)
		bundle_del(app_control->data, BUNDLE_KEY_OPERATION);

	if (launch_pid < 0) {
		if (request_context) {
			if (request_context->app_control)
				app_control_destroy(request_context->app_control);

			free(request_context);
		}

		if (launch_pid == AUL_SVC_RET_ENOMATCH)
			return app_control_error(APP_CONTROL_ERROR_APP_NOT_FOUND, __FUNCTION__, NULL);
		else if (launch_pid == AUL_SVC_RET_EILLACC)
			return app_control_error(APP_CONTROL_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
		else if (launch_pid == AUL_SVC_RET_EINVAL)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_LAUNCH_REJECTED, __FUNCTION__, NULL);
	}

	app_control->launch_pid = launch_pid;
	/* app_control_enable_app_started_result_event called */
	if (bundle_get_val(app_control->data, AUL_SVC_K_LAUNCH_RESULT_APP_STARTED)) {
		char callee[255] = {0,};
		if (aul_app_get_appid_bypid(launch_pid, callee, sizeof(callee)) != AUL_R_OK)
			LOGE("aul_app_get_appid_bypid failed: %d", launch_pid);

		if (request_context && request_context->app_control)
			request_context->app_control->launch_pid = launch_pid;

		aul_add_caller_cb(launch_pid, __handle_launch_result, request_context);

		/* launched without app selector */
		if ((strcmp(callee, APP_SELECTOR) != 0) &&
				(strcmp(callee, SHARE_PANEL) != 0))

			aul_invoke_caller_cb(request_context);

	} else { /* default case */
		aul_add_caller_cb(launch_pid, __update_launch_pid, app_control);
	}

	return APP_CONTROL_ERROR_NONE;
}

int app_control_send_terminate_request(app_control_h app_control)
{
	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control->type != APP_CONTROL_TYPE_REQUEST || app_control->launch_pid < 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	aul_svc_subapp_terminate_request_pid(app_control->launch_pid);

	return APP_CONTROL_ERROR_NONE;
}

/* LCOV_EXCL_START */
static bool app_control_copy_reply_data_cb(app_control_h app_control, const char *key, void *user_data)
{
	bundle *reply_data = user_data;
	char *value = NULL;
	char **value_array = NULL;
	int value_array_length = 0;
	int value_array_index = 0;

	if (reply_data == NULL) {
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
		return false;
	}

	if (aul_svc_data_is_array(app_control->data, key)) {
		app_control_get_extra_data_array(app_control, key, &value_array, &value_array_length);
		aul_svc_add_data_array(reply_data, key, (const char **)value_array, value_array_length);

		for (value_array_index = 0; value_array_index < value_array_length; value_array_index++)
			free(value_array[value_array_index]);

		free(value_array);
	} else {
		app_control_get_extra_data(app_control, key, &value);
		aul_svc_add_data(reply_data, key, value);
		free(value);
	}

	return true;
}
/* LCOV_EXCL_STOP */

int app_control_reply_to_launch_request(app_control_h reply, app_control_h request, app_control_result_e result)
{
	bundle *reply_data;
	int appsvc_result;
	int ret = 0;

	if (app_control_validate(reply) || app_control_validate(request))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (result == APP_CONTROL_RESULT_APP_STARTED)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "APP_CONTROL_RESULT_APP_STARTED is not allowed to use");

	if (aul_svc_create_result_bundle(request->data, &reply_data) != 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to create a result bundle");

	app_control_foreach_extra_data(reply, app_control_copy_reply_data_cb, reply_data);

	switch (result) {
	case APP_CONTROL_RESULT_SUCCEEDED:
		appsvc_result = AUL_SVC_RES_OK;
		break;
	case APP_CONTROL_RESULT_FAILED:
		appsvc_result = AUL_SVC_RES_NOT_OK;
		break;
	case APP_CONTROL_RESULT_CANCELED:
		appsvc_result = AUL_SVC_RES_CANCEL;
		break;
	default:
		appsvc_result = AUL_SVC_RES_CANCEL;
		break;
	}

	ret = aul_svc_send_result(reply_data, appsvc_result);
	bundle_free(reply_data);
	if (ret < 0) {
		if (ret == AUL_SVC_RET_EINVAL)
			return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_LAUNCH_REJECTED, __FUNCTION__, NULL);
	}

	return APP_CONTROL_ERROR_NONE;
}


int app_control_add_extra_data(app_control_h app_control, const char *key, const char *value)
{
	if (app_control_validate(app_control) || app_control_validate_extra_data(key) || app_control_validate_extra_data(value))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	if (aul_svc_get_data(app_control->data, key) != NULL) {
		/* overwrite any existing value */
		bundle_del(app_control->data, key);
	}

	if (aul_svc_add_data(app_control->data, key, value) != 0)
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "failed to add data to the appsvc handle");

	return APP_CONTROL_ERROR_NONE;
}


int app_control_add_extra_data_array(app_control_h app_control, const char *key, const char* value[], int length)
{
	if (app_control_validate(app_control) || app_control_validate_extra_data(key))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (value == NULL || length <= 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid array");

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	if (aul_svc_get_data_array(app_control->data, key, NULL) != NULL) {
		/* overwrite any existing value */
		bundle_del(app_control->data, key);
	}

	if (aul_svc_add_data_array(app_control->data, key, value, length) != 0)
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "failed to add array data to the appsvc handle");

	return APP_CONTROL_ERROR_NONE;
}

int app_control_remove_extra_data(app_control_h app_control, const char *key)
{
	if (app_control_validate(app_control) || app_control_validate_extra_data(key))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	if (bundle_del(app_control->data, key))
		return app_control_error(APP_CONTROL_ERROR_KEY_NOT_FOUND, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_extra_data(app_control_h app_control, const char *key, char **value)
{
	const char *data_value;

	if (app_control_validate(app_control) || app_control_validate_extra_data(key) || value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	data_value = aul_svc_get_data(app_control->data, key);
	if (data_value == NULL) {
		if (errno == ENOTSUP)
			return app_control_error(APP_CONTROL_ERROR_INVALID_DATA_TYPE, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_KEY_NOT_FOUND, __FUNCTION__, NULL);
	}

	*value = strdup(data_value);
	if (*value == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_extra_data_array(app_control_h app_control, const char *key, char ***value, int *length)
{
	const char **array_data;
	int array_data_length;
	char **array_data_clone;
	int i;

	if (app_control_validate(app_control) || app_control_validate_extra_data(key))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (value == NULL || length == 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	array_data = aul_svc_get_data_array(app_control->data, key, &array_data_length);
	if (array_data == NULL) {
		if (errno == ENOTSUP)
			return app_control_error(APP_CONTROL_ERROR_INVALID_DATA_TYPE, __FUNCTION__, NULL);
		else
			return app_control_error(APP_CONTROL_ERROR_KEY_NOT_FOUND, __FUNCTION__, NULL);
	}

	array_data_clone = calloc(array_data_length, sizeof(char *));
	if (array_data_clone == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	for (i = 0; i < array_data_length; i++) {
		if (array_data[i] != NULL) {
			array_data_clone[i] = strdup(array_data[i]);
			if (array_data_clone[i] == NULL)
				goto error_oom;
		}
	}

	*value = array_data_clone;
	*length = array_data_length;

	return APP_CONTROL_ERROR_NONE;

error_oom:
	for (i = 0; i < array_data_length; i++) {
		if (array_data_clone[i])
			free(array_data_clone[i]);
	}
	free(array_data_clone);

	return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
}

int app_control_is_extra_data_array(app_control_h app_control, const char *key, bool *array)
{
	if (app_control_validate(app_control) || app_control_validate_extra_data(key) || array == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control_validate_internal_key(key))
		return app_control_error(APP_CONTROL_ERROR_KEY_REJECTED, __FUNCTION__, "the given key is reserved as internal use");

	if (!aul_svc_data_is_array(app_control->data, key))
		*array = false;
	else
		*array = true;

	return APP_CONTROL_ERROR_NONE;
}

typedef struct {
	app_control_h app_control;
	app_control_extra_data_cb callback;
	void *user_data;
	bool foreach_break;
} foreach_context_extra_data_t;

static void app_control_cb_broker_bundle_iterator(const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
{
	foreach_context_extra_data_t *foreach_context = NULL;
	app_control_extra_data_cb extra_data_cb;

	if (key == NULL || !(type == BUNDLE_TYPE_STR || type == BUNDLE_TYPE_STR_ARRAY))
		return;

	foreach_context = (foreach_context_extra_data_t *)user_data;
	if (foreach_context->foreach_break == true)
		return;

	if (app_control_validate_internal_key(key))
		return;

	extra_data_cb = foreach_context->callback;

	if (extra_data_cb != NULL) {
		bool stop_foreach = false;

		stop_foreach = !extra_data_cb(foreach_context->app_control, key, foreach_context->user_data);

		foreach_context->foreach_break = stop_foreach;
	}
}

int app_control_foreach_extra_data(app_control_h app_control, app_control_extra_data_cb callback, void *user_data)
{
	foreach_context_extra_data_t foreach_context = {
		.app_control = app_control,
		.callback = callback,
		.user_data = user_data,
		.foreach_break = false
	};

	if (app_control_validate(app_control) || callback == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	bundle_foreach(app_control->data, app_control_cb_broker_bundle_iterator, &foreach_context);

	return APP_CONTROL_ERROR_NONE;
}

typedef struct {
	app_control_h app_control;
	app_control_app_matched_cb callback;
	void *user_data;
	bool foreach_break;
} foreach_context_launchable_app_t;

/* LCOV_EXCL_START */
int app_control_cb_broker_foreach_app_matched(const char *package, void *data)
{
	foreach_context_launchable_app_t *foreach_context;
	app_control_app_matched_cb app_matched_cb;

	if (package == NULL || data == NULL) {
		app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
		return -1;
	}

	foreach_context = (foreach_context_launchable_app_t *)data;
	if (foreach_context->foreach_break == true)
		return -1;

	app_matched_cb = foreach_context->callback;
	if (app_matched_cb != NULL) {
		bool stop_foreach = false;

		stop_foreach = !app_matched_cb(foreach_context->app_control, package, foreach_context->user_data);

		foreach_context->foreach_break = stop_foreach;
	}

	return 0;
}
/* LCOV_EXCL_STOP */

int app_control_foreach_app_matched(app_control_h app_control, app_control_app_matched_cb callback, void *user_data)
{
	foreach_context_launchable_app_t foreach_context = {
		.app_control = app_control,
		.callback = callback,
		.user_data = user_data,
		.foreach_break = false
	};

	if (app_control_validate(app_control) || callback == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	aul_svc_get_list_for_uid(app_control->data, app_control_cb_broker_foreach_app_matched, &foreach_context, getuid());

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_caller(app_control_h app_control, char **package)
{
	const char *bundle_value;
	char *package_dup;

	if (app_control_validate(app_control) || package == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control->type != APP_CONTROL_TYPE_EVENT)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid app_control handle type");

	bundle_value = bundle_get_val(app_control->data, AUL_K_CALLER_APPID);
	if (bundle_value == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to retrieve the appid of the caller");

	package_dup = strdup(bundle_value);
	if (package_dup == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	*package = package_dup;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_is_reply_requested(app_control_h app_control, bool *requested)
{
	const char *bundle_value;

	if (app_control_validate(app_control) || requested == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (app_control->type != APP_CONTROL_TYPE_EVENT)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid app_control handle type");

	bundle_value = bundle_get_val(app_control->data, AUL_K_WAIT_RESULT);
	if (bundle_value != NULL)
		*requested = true;
	else
		*requested = false;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_import_from_bundle(app_control_h app_control, bundle *data)
{
	bundle *data_dup = NULL;

	if (app_control_validate(app_control) || data == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	data_dup = bundle_dup(data);
	if (data_dup == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to duplicate the bundle");

	if (app_control->data != NULL)
		bundle_free(app_control->data);

	app_control->data = data_dup;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_export_as_bundle(app_control_h app_control, bundle **data)
{
	bundle *data_dup = NULL;

	if (app_control_validate(app_control) || data == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	data_dup = bundle_dup(app_control->data);
	if (data_dup == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to duplicate the bundle");

	*data = data_dup;

	return APP_CONTROL_ERROR_NONE;
}

int app_control_request_transient_app(app_control_h app_control, unsigned int callee_id, app_control_host_res_fn cbfunc, void *data)
{
	int ret;

	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	ret = aul_svc_request_transient_app(app_control->data, callee_id, (aul_svc_host_res_fn)cbfunc, data);
	if (ret < 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_enable_app_started_result_event(app_control_h app_control)
{
	int ret;

	if (app_control_validate(app_control))
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	ret = aul_svc_subscribe_launch_result(app_control->data, AUL_SVC_K_LAUNCH_RESULT_APP_STARTED);
	if (ret < 0)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	return APP_CONTROL_ERROR_NONE;
}

int app_control_set_instance_id(app_control_h app_control, const char *instance_id)
{
	int ret;

	if (app_control_validate(app_control) || instance_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	ret = aul_svc_set_instance_id(app_control->data, instance_id);
	if (ret < 0)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "Out of memory");

	return APP_CONTROL_ERROR_NONE;
}

int app_control_get_instance_id(app_control_h app_control, char **instance_id)
{
	const char *id;

	if (app_control_validate(app_control) || instance_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	id = aul_svc_get_instance_id(app_control->data);
	if (id == NULL)
		return app_control_error(APP_CONTROL_ERROR_INVALID_PARAMETER, __FUNCTION__, "Failed to get the instance id");

	*instance_id = strdup(id);
	if (*instance_id == NULL)
		return app_control_error(APP_CONTROL_ERROR_OUT_OF_MEMORY, __FUNCTION__, "Failed to duplicate the instance id");

	return APP_CONTROL_ERROR_NONE;
}