summaryrefslogtreecommitdiff
path: root/src/ic-ioty-ocprocess.c
blob: 2e1fe46489d66dc7d340fc515248aafa0572c18b (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
/*
 * Copyright (c) 2015 - 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 <errno.h>
#include <sys/prctl.h>
#include <ocstack.h>
#include <octypes.h>

#include "iotcon.h"
#include "ic.h"
#include "ic-utils.h"
#include "ic-ioty.h"
#include "ic-ioty-parse.h"
#include "ic-remote-resource.h"
#include "ic-device.h"
#include "ic-presence.h"
#include "ic-request.h"
#include "ic-resource.h"
#include "ic-response.h"
#include "ic-options.h"
#include "ic-representation.h"
#include "ic-types.h"
#include "ic-attributes.h"
#include "ic-lite-resource.h"
#include "ic-ioty.h"
#include "ic-ioty-ocprocess.h"

#define ICL_IOTY_TIME_INTERVAL_MIN 0
#define ICL_IOTY_TIME_INTERVAL_DEFAULT 100

static int icl_ioty_alive = 1;
static int icl_ioty_polling_interval;

void icl_ioty_ocprocess_stop()
{
	icl_ioty_alive = 0;
}

void icl_ioty_ocprocess_start()
{
	icl_ioty_alive = 1;
}


API int iotcon_polling_get_interval(int *interval)
{
	FN_CALL;
	RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);
	RETV_IF(NULL == interval, IOTCON_ERROR_INVALID_PARAMETER);

	*interval = icl_ioty_polling_interval;

	return IOTCON_ERROR_NONE;
}


API int iotcon_polling_set_interval(int interval)
{
	FN_CALL;
	RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);
	RETV_IF(interval <= ICL_IOTY_TIME_INTERVAL_MIN, IOTCON_ERROR_INVALID_PARAMETER);

	icl_ioty_polling_interval = interval;

	ic_utils_cond_signal(IC_UTILS_COND_POLLING);

	return IOTCON_ERROR_NONE;
}


API int iotcon_polling_invoke(void)
{
	FN_CALL;
	RETV_IF(false == ic_utils_check_ocf_feature(), IOTCON_ERROR_NOT_SUPPORTED);

	ic_utils_cond_signal(IC_UTILS_COND_POLLING);

	return IOTCON_ERROR_NONE;
}


void* icl_ioty_ocprocess_thread(void *data)
{
	FN_CALL;
	int ret;

	icl_ioty_polling_interval = ICL_IOTY_TIME_INTERVAL_DEFAULT;

	/* For setting this thread name */
	ret = prctl(PR_SET_NAME, "iotcon_ocprocess_thread");
	if (0 != ret)
		ERR("prctl(PR_SET_NAME) Fail(%d)", ret);

	ic_utils_mutex_lock(IC_UTILS_MUTEX_POLLING);
	while (icl_ioty_alive) {
		ic_utils_mutex_lock(IC_UTILS_MUTEX_IOTY);
		ret = OCProcess();
		ic_utils_mutex_unlock(IC_UTILS_MUTEX_IOTY);
		if (OC_STACK_OK != ret) {
			ERR("OCProcess() Fail(%d)", ret);
			break;
		}

		ic_utils_cond_timedwait(IC_UTILS_COND_POLLING, IC_UTILS_MUTEX_POLLING,
				icl_ioty_polling_interval);
	}
	ic_utils_mutex_unlock(IC_UTILS_MUTEX_POLLING);

	return NULL;
}

static gboolean _icl_ioty_ocprocess_find_idle_cb(gpointer p)
{
	int ret;
	icl_cb_s *cb_data;
	icl_find_cb_s *find_cb_data = p;
	iotcon_found_resource_cb cb;
	iotcon_remote_resource_h resource;
	GList *resource_list;

	RETV_IF(NULL == find_cb_data, G_SOURCE_REMOVE);
	cb_data = find_cb_data->cb_data;

	resource_list = find_cb_data->resource_list;

	if (cb_data && cb_data->cb) {
		for (; resource_list; resource_list = resource_list->next) {
			resource = resource_list->data;
			resource->is_found = true;
			cb = (iotcon_found_resource_cb)cb_data->cb;
			INFO("Call the callback");
			if (IOTCON_FUNC_STOP == cb(resource, IOTCON_ERROR_NONE, cb_data->user_data)) {
				/* Stop */
				INFO("Stop the callback");
				cb_data->cb = NULL;

				ret = icl_ioty_mutex_lock();
				if (IOTCON_ERROR_NONE != ret) {
					ERR("icl_ioty_mutex_lock() Fail(%d)", ret);
					return G_SOURCE_REMOVE;
				}

				OCCancel(cb_data->handle, OC_LOW_QOS, NULL, 0);
				icl_ioty_mutex_unlock();

				break;
			}
			resource->is_found = false;
		}
	}
	icl_destroy_find_cb_data(find_cb_data);
	return G_SOURCE_REMOVE;
}

static gboolean _icl_ioty_ocprocess_fail_idle_cb(gpointer p)
{
	FN_CALL;
	icl_cb_s *cb_data = p;

	RETV_IF(NULL == cb_data, G_SOURCE_REMOVE);

	if (cb_data->cb) {
		switch (cb_data->op) {
		case ICL_FIND_RESOURCE:
			((iotcon_found_resource_cb)cb_data->cb)(NULL, cb_data->result, cb_data->user_data);
			break;
		case ICL_GET_DEVICE_INFO:
			((iotcon_device_info_cb)cb_data->cb)(NULL, cb_data->result, cb_data->user_data);
			break;
		case ICL_GET_PLATFORM_INFO:
			((iotcon_platform_info_cb)cb_data->cb)(NULL, cb_data->result, cb_data->user_data);
			break;
		default:
			ERR("Invalid operation(%d)", cb_data->op);
			return G_SOURCE_REMOVE;
		}
	}
	return G_SOURCE_REMOVE;
}


OCStackApplicationResult icl_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
		OCClientResponse *resp)
{
	FN_CALL;
	int ret;
	GList *resource_list;
	icl_find_cb_s *find_cb_data = NULL;
	icl_cb_s *cb_data = ctx;

	RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
	RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
	if (NULL == resp->payload) /* normal case : payload COULD be NULL */
		return OC_STACK_KEEP_TRANSACTION;
	RETVM_IF(PAYLOAD_TYPE_DISCOVERY != resp->payload->type,
			OC_STACK_KEEP_TRANSACTION, "Invalid payload type(%d)", resp->payload->type);

	ret = ic_ioty_parse_oic_discovery_payload(&(resp->devAddr), cb_data->connectivity_type,
			cb_data->connectivity_extra, (OCDiscoveryPayload*)resp->payload, &resource_list);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("_icl_ioty_ocprocess_parse_find_payload() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = icl_create_find_cb_data(cb_data, resource_list, &find_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_find_cb_data() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	g_idle_add(_icl_ioty_ocprocess_find_idle_cb, find_cb_data);

	/* DO NOT FREE ctx(cb_data). It MUST be freed in the ocstack */

	return OC_STACK_KEEP_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_device_info_idle_cb(gpointer p)
{
	int ret;
	icl_cb_s *cb_data;
	icl_device_cb_s *device_cb_data = p;
	iotcon_device_info_cb cb;

	RETV_IF(NULL == device_cb_data, G_SOURCE_REMOVE);
	cb_data = device_cb_data->cb_data;

	if (cb_data && cb_data->cb) {
		cb = (iotcon_device_info_cb)cb_data->cb;
		if (IOTCON_FUNC_STOP == cb(device_cb_data->device_info, IOTCON_ERROR_NONE,
					cb_data->user_data)) { /* Stop */
			cb_data->cb = NULL;

			ret = icl_ioty_mutex_lock();
			if (IOTCON_ERROR_NONE != ret) {
				ERR("icl_ioty_mutex_lock() Fail(%d)", ret);
				return G_SOURCE_REMOVE;
			}

			OCCancel(cb_data->handle, OC_LOW_QOS, NULL, 0);
			icl_ioty_mutex_unlock();
		}
	}

	icl_destroy_device_cb_data(device_cb_data);
	return G_SOURCE_REMOVE;
}

OCStackApplicationResult icl_ioty_ocprocess_device_info_cb(void *ctx,
		OCDoHandle handle, OCClientResponse* resp)
{
	FN_CALL;
	int ret;
	icl_cb_s *cb_data = ctx;
	icl_device_cb_s *device_cb_data = NULL;
	iotcon_device_info_h info;

	RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
	RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
	RETVM_IF(OC_STACK_UNAUTHORIZED_REQ == resp->result, OC_STACK_KEEP_TRANSACTION,
			"No Authorization");
	RETV_IF(NULL == resp->payload, OC_STACK_KEEP_TRANSACTION);

	if (PAYLOAD_TYPE_REPRESENTATION != resp->payload->type) {
		ERR("Invalid payload type (%d)", resp->payload->type);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = ic_ioty_parse_oic_device_payload((OCRepPayload *)resp->payload,
			&info);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_device_payload() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = icl_create_device_cb_data(cb_data, info, &device_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_device_cb_data() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	g_idle_add(_icl_ioty_ocprocess_device_info_idle_cb, device_cb_data);

	/* DO NOT FREE ctx(cb_data). It MUST be freed in the ocstack */

	return OC_STACK_KEEP_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_platform_info_idle_cb(gpointer p)
{
	int ret;
	icl_cb_s *cb_data;
	icl_platform_cb_s *platform_cb_data = p;
	iotcon_platform_info_cb cb;

	RETV_IF(NULL == platform_cb_data, G_SOURCE_REMOVE);
	cb_data = platform_cb_data->cb_data;

	if (cb_data && cb_data->cb) {
		cb = (iotcon_platform_info_cb)cb_data->cb;
		if (IOTCON_FUNC_STOP == cb(platform_cb_data->platform_info, IOTCON_ERROR_NONE,
					cb_data->user_data)) { /* Stop */
			cb_data->cb = NULL;

			ret = icl_ioty_mutex_lock();
			if (IOTCON_ERROR_NONE != ret) {
				ERR("icl_ioty_mutex_lock() Fail(%d)", ret);
				return G_SOURCE_REMOVE;
			}

			OCCancel(cb_data->handle, OC_LOW_QOS, NULL, 0);
			icl_ioty_mutex_unlock();
		}
	}

	icl_destroy_platform_cb_data(platform_cb_data);
	return G_SOURCE_REMOVE;
}

OCStackApplicationResult icl_ioty_ocprocess_platform_info_cb(void *ctx,
		OCDoHandle handle, OCClientResponse* resp)
{
	FN_CALL;
	int ret;
	icl_cb_s *cb_data = ctx;
	icl_platform_cb_s *platform_cb_data = NULL;
	iotcon_platform_info_h info;

	RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
	RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
	RETVM_IF(OC_STACK_UNAUTHORIZED_REQ == resp->result, OC_STACK_KEEP_TRANSACTION,
			"No Authorization");
	RETV_IF(NULL == resp->payload, OC_STACK_KEEP_TRANSACTION);

	if (PAYLOAD_TYPE_REPRESENTATION != resp->payload->type) {
		ERR("Invalid payload type (%d)", resp->payload->type);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = ic_ioty_parse_oic_platform_payload((OCRepPayload *)resp->payload,
			&info);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_platform_payload() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = icl_create_platform_cb_data(cb_data, info, &platform_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_platform_cb_data() Fail(%d)", ret);
		cb_data->result = ret;
		g_idle_add(_icl_ioty_ocprocess_fail_idle_cb, cb_data);
		return OC_STACK_KEEP_TRANSACTION;
	}

	g_idle_add(_icl_ioty_ocprocess_platform_info_idle_cb, platform_cb_data);

	/* DO NOT FREE ctx(cb_data). It MUST be freed in the ocstack */

	return OC_STACK_KEEP_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_presence_idle_cb(gpointer p)
{
	icl_presence_cb_s *cb_data = p;
	iotcon_presence_h presence;

	RETV_IF(NULL == cb_data, G_SOURCE_REMOVE);

	presence = cb_data->presence;
	if (NULL == presence) {
		ERR("presence is NULL");
		icl_destroy_presence_cb_data(cb_data);
		return G_SOURCE_REMOVE;
	}

	if (presence->cb)
		presence->cb(presence, IOTCON_ERROR_NONE, cb_data->response, presence->user_data);

	icl_destroy_presence_cb_data(cb_data);
	return G_SOURCE_REMOVE;
}

OCStackApplicationResult icl_ioty_ocprocess_presence_cb(void *ctx,
		OCDoHandle handle, OCClientResponse *resp)
{
	FN_CALL;
	int ret;
	icl_presence_cb_s *presence_cb_data;
	iotcon_presence_h presence = (iotcon_presence_h)ctx;
	iotcon_presence_response_h presence_response = NULL;

	RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
	RETV_IF(NULL == presence, OC_STACK_KEEP_TRANSACTION);

	DBG("presence->handle:[%p] vs handle:[%p]", presence->handle, handle);

	ret = ic_ioty_parse_oic_presence_payload(&resp->devAddr,
			(OCPresencePayload*)resp->payload, resp->result, &presence_response);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_presence_payload() Fail(%d)", ret);
		return OC_STACK_KEEP_TRANSACTION;
	}

	ret = icl_create_presence_cb_data(presence, presence_response, &presence_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_presence_cb_data() Fail(%d)", ret);
		icl_destroy_presence_response(presence_response);
		return OC_STACK_KEEP_TRANSACTION;
	}

	g_idle_add(_icl_ioty_ocprocess_presence_idle_cb, presence_cb_data);

	return OC_STACK_KEEP_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_observe_idle_cb(gpointer p)
{
	icl_observe_cb_s *cb_data = p;

	RETV_IF(NULL == cb_data, G_SOURCE_REMOVE);

	if (cb_data->cb)
		cb_data->cb(cb_data->resource, IOTCON_ERROR_NONE, cb_data->sequence_number,
				cb_data->response, cb_data->user_data);

	icl_destroy_observe_cb_data(cb_data);

	return G_SOURCE_REMOVE;
}


OCStackApplicationResult icl_ioty_ocprocess_observe_cb(void *ctx,
		OCDoHandle handle, OCClientResponse* resp)
{
	FN_CALL;
	int ret, response_result;
	iotcon_options_h options;
	iotcon_response_h response;
	iotcon_representation_h repr;
	icl_observe_cb_s *observe_cb_data;
	icl_observe_container_s *cb_container = ctx;

	RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
	RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);

	if (OC_STACK_UNAUTHORIZED_REQ == resp->result) {
		ERR("No Authorization");
		return OC_STACK_KEEP_TRANSACTION;
	}

	if (NULL == resp->payload) {
		ERR("payload is empty(%d)", resp->result);
		return OC_STACK_KEEP_TRANSACTION;
	}

	/* representation */
	ret = ic_ioty_parse_oic_rep_payload((OCRepPayload*)resp->payload, true, &repr);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_rep_payload() Fail(%d)", ret);
		return OC_STACK_KEEP_TRANSACTION;
	}

	if (NULL == repr->uri_path && resp->resourceUri)
		repr->uri_path = ic_utils_strdup(resp->resourceUri);

	/* response result */
	response_result = ic_ioty_parse_oic_response_result(resp->result);

	/* header options */
	ret = ic_ioty_parse_oic_header_option(resp->rcvdVendorSpecificHeaderOptions,
			resp->numRcvdVendorSpecificHeaderOptions, &options);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_header_option() Fail(%d)", ret);
		return OC_STACK_KEEP_TRANSACTION;
	}

	/* response */
	response = calloc(1, sizeof(struct icl_resource_response));
	if (NULL == response) {
		ERR("calloc() Fail(%d)", errno);
		if (options)
			iotcon_options_destroy(options);
		iotcon_representation_destroy(repr);
		return OC_STACK_KEEP_TRANSACTION;
	}
	response->header_options = options;
	response->result = (iotcon_response_result_e)response_result;
	response->repr = repr;

	ret = icl_create_observe_cb_data(cb_container, resp->sequenceNumber, response,
			&observe_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_observe_cb_data() Fail(%d)", ret);
		iotcon_response_destroy(response);
		return OC_STACK_KEEP_TRANSACTION;
	}

	g_idle_add(_icl_ioty_ocprocess_observe_idle_cb, observe_cb_data);

	/* DO NOT FREE ctx(cb_container). It MUST be freed in the ocstack */
	/* DO NOT FREE cb_data. It MUST be freed in the idle */

	return OC_STACK_KEEP_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_crud_idle_cb(gpointer p)
{
	icl_response_cb_s *cb_data = p;

	RETV_IF(NULL == cb_data, G_SOURCE_REMOVE);

	if (cb_data->cb)
		cb_data->cb(cb_data->resource, IOTCON_ERROR_NONE, cb_data->req_type,
				cb_data->response, cb_data->user_data);

	icl_destroy_response_cb_data(cb_data);

	return G_SOURCE_REMOVE;
}

OCStackApplicationResult icl_ioty_ocprocess_crud_cb(void *ctx,
		OCDoHandle handle, OCClientResponse *resp)
{
	FN_CALL;
	int ret;
	GSource *idle_src;
	iotcon_options_h options;
	iotcon_response_h response;
	iotcon_representation_h repr;
	icl_response_cb_s *response_cb_data;
	iotcon_response_result_e response_result;
	icl_response_container_s *cb_container = ctx;

	RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
	RETV_IF(NULL == resp, OC_STACK_DELETE_TRANSACTION);

	/* remove timeout */
	if (cb_container->timeout) {
		g_source_remove(cb_container->timeout);
		cb_container->timeout = 0;
	}

	if (OC_STACK_UNAUTHORIZED_REQ == resp->result) {
		ERR("No Authorization");
		if (cb_container->cb) {
			cb_container->cb(cb_container->resource, IOTCON_ERROR_IOTIVITY,
					cb_container->req_type, NULL, cb_container->user_data);
		}
		return OC_STACK_DELETE_TRANSACTION;
	}

	if (NULL == resp->payload) {
		ERR("payload is empty(%d)", resp->result);
		if (cb_container->cb) {
			cb_container->cb(cb_container->resource, IOTCON_ERROR_IOTIVITY,
					cb_container->req_type, NULL, cb_container->user_data);
		}
		return OC_STACK_DELETE_TRANSACTION;
	}

	/* representation */
	ret = ic_ioty_parse_oic_rep_payload((OCRepPayload*)resp->payload, true, &repr);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_rep_payload() Fail(%d)", ret);
		return OC_STACK_DELETE_TRANSACTION;
	}

	if (NULL == repr->uri_path && resp->resourceUri)
		repr->uri_path = ic_utils_strdup(resp->resourceUri);

	/* response result */
	response_result = ic_ioty_parse_oic_response_result(resp->result);

	/* header options */
	ret = ic_ioty_parse_oic_header_option(resp->rcvdVendorSpecificHeaderOptions,
			resp->numRcvdVendorSpecificHeaderOptions, &options);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_header_option() Fail(%d)", ret);
		return OC_STACK_DELETE_TRANSACTION;
	}

	/* response */
	response = calloc(1, sizeof(struct icl_resource_response));
	if (NULL == response) {
		ERR("calloc() Fail(%d)", errno);
		if (options)
			iotcon_options_destroy(options);
		iotcon_representation_destroy(repr);
		return OC_STACK_DELETE_TRANSACTION;
	}
	response->header_options = options;
	response->result = response_result;
	response->repr = repr;

	ret = icl_create_response_cb_data(cb_container, response, &response_cb_data);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_create_observe_cb_data() Fail(%d)", ret);
		iotcon_response_destroy(response);
		return OC_STACK_DELETE_TRANSACTION;
	}

	idle_src = g_idle_source_new();
	g_source_set_priority(idle_src, G_PRIORITY_DEFAULT);
	g_source_set_callback(idle_src, _icl_ioty_ocprocess_crud_idle_cb, response_cb_data,
			NULL);
	g_source_attach(idle_src, cb_container->thread_context);
	g_source_unref(idle_src);

	/* DO NOT FREE ctx(cb_container). It MUST be freed in the ocstack */
	/* DO NOT FREE cb_data. It MUST be freed in the idle */

	return OC_STACK_DELETE_TRANSACTION;
}

static gboolean _icl_ioty_ocprocess_request_idle_cb(gpointer p)
{
	iotcon_resource_h resource;
	iotcon_request_h request;
	icl_request_container_s *req_container = p;

	RETV_IF(NULL == req_container, IOTCON_ERROR_INVALID_PARAMETER);

	resource = req_container->resource;
	request = req_container->request;

	resource->cb(resource, request, resource->user_data);
	icl_destroy_request_container(req_container);

	return G_SOURCE_REMOVE;
}

OCEntityHandlerResult icl_ioty_ocprocess_request_cb(OCEntityHandlerFlag flag,
		OCEntityHandlerRequest *request, void *user_data)
{
	FN_CALL;
	int ret, conn_type, observe_id = 0;
	char *host_address;
	iotcon_observe_type_e obs_type;
	iotcon_request_type_e req_type;
	iotcon_options_h options;
	iotcon_query_h query;
	iotcon_representation_h repr;
	iotcon_resource_h resource = user_data;
	char *token, *save_ptr1, *save_ptr2;
	char *query_str, *query_key, *query_value;
	iotcon_request_h req;

	RETV_IF(NULL == resource, OC_EH_ERROR);
	RETV_IF(NULL == request, OC_EH_ERROR);

	/* request type */
	if (OC_REQUEST_FLAG & flag) {
		req_type = ic_ioty_parse_oic_method(request->method);
	} else {
		ERR("Invalid flag(%d)", flag);
		return OC_EH_ERROR;
	}

	/* observe type */
	if (OC_OBSERVE_FLAG & flag) {
		observe_id = request->obsInfo.obsId;
		obs_type = ic_ioty_parse_oic_action(request->obsInfo.action);
	} else {
		obs_type = IOTCON_OBSERVE_NO_TYPE;
	}

	/* header options */
	ret = ic_ioty_parse_oic_header_option(request->rcvdVendorSpecificHeaderOptions,
			request->numRcvdVendorSpecificHeaderOptions, &options);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("ic_ioty_parse_oic_header_option() Fail(%d)", ret);
		return OC_EH_ERROR;
	}

	/* query */
	if (request->query && *request->query) {
		iotcon_query_create(&query);
		query_str = request->query;
		while ((token = strtok_r(query_str, "&;", &save_ptr1))) {
			while ((query_key = strtok_r(token, "=", &save_ptr2))) {
				token = NULL;
				query_value = strtok_r(token, "=", &save_ptr2);
				if (NULL == query_value)
					break;
				iotcon_query_add(query, query_key, query_value);
			}
			query_str = NULL;
		}
	} else {
		query = NULL;
	}

	/* representation */
	if (request->payload) {
		ic_ioty_parse_oic_rep_payload((OCRepPayload*)request->payload, true, &repr);
		if (repr && NULL == repr->uri_path)
			repr->uri_path = ic_utils_strdup(resource->uri_path);
	} else {
		repr = NULL;
	}


	/* for iotcon_resource_notify */
	if (IOTCON_OBSERVE_REGISTER == obs_type) {
		if (NULL == resource->observers)
			iotcon_observers_create(&resource->observers);
		iotcon_observers_add(resource->observers, observe_id);
	} else if (IOTCON_OBSERVE_DEREGISTER == obs_type) {
		iotcon_observers_remove(resource->observers, observe_id);
	}

	ic_ioty_parse_oic_dev_address(&request->devAddr, &host_address, &conn_type);

	req = calloc(1, sizeof(struct icl_resource_request));
	if (NULL == req) {
		ERR("calloc() Fail(%d)", errno);
		free(host_address);
		if (options)
			iotcon_options_destroy(options);
		if (query)
			iotcon_query_destroy(query);
		if (repr)
			iotcon_representation_destroy(repr);
		return OC_EH_ERROR;
	}

	req->connectivity_type = conn_type;
	req->host_address = host_address;
	req->type = req_type;
	req->header_options = options;
	req->query = query;
	req->observation_info.action = obs_type;
	req->observation_info.observe_id = observe_id;
	req->repr = repr;
	req->oic_request_h = request->requestHandle;
	req->oic_resource_h = request->resource;

	icl_request_container_s *req_container = calloc(1, sizeof(icl_request_container_s));
	req_container->request = req;
	req_container->resource = resource;

	g_idle_add(_icl_ioty_ocprocess_request_idle_cb, req_container);

	return OC_EH_SLOW;
}

static int _icl_ioty_ocprocess_lite_resource_get_repr(
		iotcon_lite_resource_h resource, iotcon_representation_h *representation)
{
	int ret;
	iotcon_representation_h repr;
	iotcon_resource_interfaces_h ifaces;

	RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
	RETV_IF(NULL == representation, IOTCON_ERROR_INVALID_PARAMETER);

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

	ret = iotcon_representation_set_uri_path(repr, resource->uri_path);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
		iotcon_representation_destroy(repr);
		return ret;
	}

	ret = iotcon_representation_set_attributes(repr, resource->attributes);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
		iotcon_representation_destroy(repr);
		return ret;
	}

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

	ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_resource_interfaces_add() Fail(%d)", ret);
		iotcon_resource_interfaces_destroy(ifaces);
		iotcon_representation_destroy(repr);
		return ret;
	}

	ret = iotcon_representation_set_resource_interfaces(repr, ifaces);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("iotcon_representation_set_resource_interfaces() Fail(%d)", ret);
		iotcon_representation_destroy(repr);
		return ret;
	}
	iotcon_resource_interfaces_destroy(ifaces);

	*representation = repr;
	return IOTCON_ERROR_NONE;
}

static gboolean _icl_ioty_ocprocess_lite_resource_response_idle_cb(gpointer p)
{
	int ret;

	ret = icl_ioty_response_send(p);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_ioty_response_send() Fail(%d)", ret);
		return G_SOURCE_REMOVE;
	}
	iotcon_response_destroy(p);
	return G_SOURCE_REMOVE;
}

static gboolean _icl_ioty_ocprocess_lite_resource_notify_idle_cb(gpointer p)
{
	int ret;

	ret = icl_ioty_lite_resource_notify(p);
	if (IOTCON_ERROR_NONE != ret) {
		ERR("icl_ioty_lite_resource_notify() Fail(%d)", ret);
		return G_SOURCE_REMOVE;
	}
	return G_SOURCE_REMOVE;
}

OCEntityHandlerResult icl_ioty_ocprocess_lite_request_cb(OCEntityHandlerFlag flag,
		OCEntityHandlerRequest *request, void *user_data)
{
	FN_CALL;
	iotcon_request_type_e req_type;
	iotcon_lite_resource_h resource = user_data;
	iotcon_representation_h repr = NULL;
	iotcon_response_h res = NULL;

	RETV_IF(NULL == resource, OC_EH_ERROR);
	RETV_IF(NULL == request, OC_EH_ERROR);

	/* request type */
	if (OC_REQUEST_FLAG & flag)
		req_type = ic_ioty_parse_oic_method(request->method);
	else {
		ERR("Invalid flag(%d)", flag);
		return OC_EH_ERROR;
	}

	/* representation */
	if (request->payload)
		ic_ioty_parse_oic_rep_payload((OCRepPayload*)request->payload, true, &repr);

	res = calloc(1, sizeof(struct icl_resource_response));
	if (NULL == res) {
		ERR("calloc() Fail(%d)", errno);
		return OC_EH_ERROR;
	}
	res->oic_request_h = request->requestHandle;
	res->oic_resource_h = request->resource;

	switch (req_type) {
	case IOTCON_REQUEST_GET:
		res->result = IOTCON_RESPONSE_OK;
		_icl_ioty_ocprocess_lite_resource_get_repr(resource, &(res->repr));
		g_idle_add(_icl_ioty_ocprocess_lite_resource_response_idle_cb, res);
		break;
	case IOTCON_REQUEST_POST:
		if (NULL == repr) {
			ERR("repr is NULL!!");
			break;
		}
		if (resource->cb) {
			if (false == resource->cb(resource, repr->attributes, resource->cb_data)) {
				res->result = IOTCON_RESPONSE_ERROR;
				g_idle_add(_icl_ioty_ocprocess_lite_resource_response_idle_cb, res);
				break;
			}
		}
		iotcon_attributes_destroy(resource->attributes);
		resource->attributes = repr->attributes;
		repr->attributes = NULL;
		_icl_ioty_ocprocess_lite_resource_get_repr(resource, &(res->repr));
		res->result = IOTCON_RESPONSE_OK;
		g_idle_add(_icl_ioty_ocprocess_lite_resource_response_idle_cb, res);
		g_idle_add(_icl_ioty_ocprocess_lite_resource_notify_idle_cb, resource);
		break;
	case IOTCON_REQUEST_PUT:
	case IOTCON_REQUEST_DELETE:
	default:
		WARN("Not supported request (only GET / POST / OBSERVE)");
		res->result = IOTCON_RESPONSE_FORBIDDEN;
		g_idle_add(_icl_ioty_ocprocess_lite_resource_response_idle_cb, res);
		break;
	}

	return OC_EH_SLOW;
}