Resource Encapsulation
[platform/core/iot/iotcon.git] / daemon / icd-ioty-ocprocess.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <unistd.h> /* for usleep() */
19 #include <glib.h>
20
21 #include <ocstack.h>
22 #include <octypes.h>
23 #include <ocpayload.h>
24
25 #include "iotcon.h"
26 #include "ic-utils.h"
27 #include "icd.h"
28 #include "icd-payload.h"
29 #include "icd-dbus.h"
30 #include "icd-ioty.h"
31 #include "icd-ioty-type.h"
32 #include "icd-ioty-ocprocess.h"
33
34 static int icd_ioty_alive;
35
36 typedef int (*_ocprocess_cb)(void *user_data);
37
38 struct icd_ioty_worker
39 {
40         void *ctx;
41         _ocprocess_cb cb;
42 };
43
44
45 struct icd_req_context {
46         unsigned int signum;
47         char *bus_name;
48         int types;
49         int observer_id;
50         int observe_action;
51         OCRequestHandle request_h;
52         OCResourceHandle resource_h;
53         GVariant *payload;
54         GVariantBuilder *options;
55         GVariantBuilder *query;
56 };
57
58
59 struct icd_find_context {
60         unsigned int signum;
61         char *bus_name;
62         int conn_type;
63         GVariant **payload;
64 };
65
66
67 struct icd_crud_context {
68         int res;
69         int crud_type;
70         GVariant *payload;
71         GVariantBuilder *options;
72         GDBusMethodInvocation *invocation;
73 };
74
75
76 struct icd_info_context {
77         unsigned int signum;
78         int info_type;
79         char *bus_name;
80         GVariant *payload;
81 };
82
83
84 struct icd_tizen_info_context {
85         OCRequestHandle request_h;
86         OCResourceHandle resource_h;
87         GDBusMethodInvocation *invocation;
88 };
89
90
91 struct icd_observe_context {
92         unsigned int signum;
93         int res;
94         int seqnum;
95         char *bus_name;
96         GVariant *payload;
97         GVariantBuilder *options;
98 };
99
100
101 struct icd_presence_context {
102         unsigned int signum;
103         char *bus_name;
104         int result;
105         unsigned int nonce;
106         OCDevAddr *dev_addr;
107 };
108
109
110 void icd_ioty_ocprocess_stop()
111 {
112         icd_ioty_alive = 0;
113 }
114
115
116 static void* _ocprocess_worker_thread(void *data)
117 {
118         int ret;
119         struct icd_ioty_worker *worker = data;
120
121         if (NULL == data) {
122                 ERR("worker is NULL");
123                 return NULL;
124         }
125
126         ret = worker->cb(worker->ctx);
127         if (IOTCON_ERROR_NONE != ret)
128                 ERR("cb() Fail(%d)", ret);
129
130         /* worker was allocated from _ocprocess_worker_start() */
131         free(worker);
132
133         /* GCC warning happen if use g_thread_exit() */
134         return NULL;
135 }
136
137
138 static int _ocprocess_worker_start(_ocprocess_cb cb, void *ctx)
139 {
140         GError *error;
141         GThread *thread;
142         struct icd_ioty_worker *worker;
143
144         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
145
146         worker = calloc(1, sizeof(struct icd_ioty_worker));
147         if (NULL == worker) {
148                 ERR("calloc() Fail(%d)", errno);
149                 return IOTCON_ERROR_OUT_OF_MEMORY;
150         }
151
152         worker->cb = cb;
153         worker->ctx = ctx;
154
155         /* TODO : consider thread pool mechanism */
156         thread = g_thread_try_new("worker_thread", _ocprocess_worker_thread, worker, &error);
157         if (NULL == thread) {
158                 ERR("g_thread_try_new() Fail(%s)", error->message);
159                 g_error_free(error);
160                 free(worker);
161                 return IOTCON_ERROR_SYSTEM;
162         }
163
164         /* DO NOT join thread. It was already detached by calling g_thread_unref() */
165         g_thread_unref(thread);
166
167         /* DO NOT FREE worker. It MUST be freed in the _ocprocess_worker_thread() */
168
169         return IOTCON_ERROR_NONE;
170 }
171
172
173 static int _ocprocess_response_signal(const char *dest, const char *signal,
174                 unsigned int signum, GVariant *value)
175 {
176         int ret;
177         char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
178
179         ret = snprintf(sig_name, sizeof(sig_name), "%s_%u", signal, signum);
180         if (ret <= 0 || sizeof(sig_name) <= ret) {
181                 ERR("snprintf() Fail(%d)", ret);
182                 return IOTCON_ERROR_IO_ERROR;
183         }
184
185         ret = icd_dbus_emit_signal(dest, sig_name, value);
186         if (IOTCON_ERROR_NONE != ret) {
187                 ERR("icd_dbus_emit_signal() Fail(%d)", ret);
188                 return ret;
189         }
190
191         return IOTCON_ERROR_NONE;
192 }
193
194
195 static inline GVariantBuilder* _ocprocess_parse_header_options(
196                 OCHeaderOption *oic_option, int option_size)
197 {
198         int i;
199         GVariantBuilder *options;
200
201         options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
202         for (i = 0; i < option_size; i++) {
203                 g_variant_builder_add(options, "(qs)", oic_option[i].optionID,
204                                 oic_option[i].optionData);
205         }
206
207         return options;
208 }
209
210
211 static int _worker_req_handler(void *context)
212 {
213         int ret;
214         GVariant *value;
215         struct icd_req_context *ctx = context;
216         GVariantBuilder payload_builder;
217
218         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
219
220         g_variant_builder_init(&payload_builder, G_VARIANT_TYPE("av"));
221         if (ctx->payload)
222                 g_variant_builder_add(&payload_builder, "v", ctx->payload);
223
224         value = g_variant_new("(ia(qs)a(ss)iiavxx)",
225                         ctx->types,
226                         ctx->options,
227                         ctx->query,
228                         ctx->observe_action,
229                         ctx->observer_id,
230                         &payload_builder,
231                         ICD_POINTER_TO_INT64(ctx->request_h),
232                         ICD_POINTER_TO_INT64(ctx->resource_h));
233
234         ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_REQUEST_HANDLER,
235                         ctx->signum, value);
236         if (IOTCON_ERROR_NONE != ret)
237                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
238
239         free(ctx->bus_name);
240         g_variant_builder_unref(ctx->options);
241         g_variant_builder_unref(ctx->query);
242         free(ctx);
243
244         return ret;
245 }
246
247
248 OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
249                 OCEntityHandlerRequest *request, void *user_data)
250 {
251         FN_CALL;
252         int ret;
253         unsigned int signal_number;
254         char *query_str, *query_key, *query_value;
255         char *token, *save_ptr1, *save_ptr2;
256         char *bus_name = NULL;
257         struct icd_req_context *req_ctx;
258
259         RETV_IF(NULL == request, OC_EH_ERROR);
260
261         req_ctx = calloc(1, sizeof(struct icd_req_context));
262         if (NULL == req_ctx) {
263                 ERR("calloc() Fail(%d)", errno);
264                 return OC_EH_ERROR;
265         }
266
267         /* handle */
268         req_ctx->request_h = request->requestHandle;
269         req_ctx->resource_h = request->resource;
270
271         ret = icd_dbus_client_list_get_info(req_ctx->resource_h, &signal_number, &bus_name);
272         if (IOTCON_ERROR_NONE != ret) {
273                 ERR("icd_dbus_client_list_get_info() Fail(%d)", ret);
274                 free(req_ctx);
275                 return OC_EH_ERROR;
276         }
277
278         /* signal number & bus_name */
279         req_ctx->signum = signal_number;
280         req_ctx->bus_name = bus_name;
281
282         /* request type */
283         if (OC_REQUEST_FLAG & flag) {
284                 switch (request->method) {
285                 case OC_REST_GET:
286                         req_ctx->types = IOTCON_REQUEST_GET;
287                         req_ctx->payload = NULL;
288
289                         if (OC_OBSERVE_FLAG & flag) {
290                                 req_ctx->types |= IOTCON_REQUEST_OBSERVE;
291                                 /* observation info*/
292                                 req_ctx->observer_id = request->obsInfo.obsId;
293                                 req_ctx->observe_action = request->obsInfo.action;
294                         }
295                         break;
296                 case OC_REST_PUT:
297                         req_ctx->types = IOTCON_REQUEST_PUT;
298                         req_ctx->payload = icd_payload_to_gvariant(request->payload);
299                         break;
300                 case OC_REST_POST:
301                         req_ctx->types = IOTCON_REQUEST_POST;
302                         req_ctx->payload = icd_payload_to_gvariant(request->payload);
303                         break;
304                 case OC_REST_DELETE:
305                         req_ctx->types = IOTCON_REQUEST_DELETE;
306                         req_ctx->payload = NULL;
307                         break;
308                 default:
309                         free(req_ctx->bus_name);
310                         free(req_ctx);
311                         return OC_EH_ERROR;
312                 }
313         }
314
315         /* header options */
316         req_ctx->options = _ocprocess_parse_header_options(
317                         request->rcvdVendorSpecificHeaderOptions,
318                         request->numRcvdVendorSpecificHeaderOptions);
319
320         /* query */
321         req_ctx->query = g_variant_builder_new(G_VARIANT_TYPE("a(ss)"));
322         query_str = request->query;
323         while ((token = strtok_r(query_str, "&;", &save_ptr1))) {
324                 while ((query_key = strtok_r(token, "=", &save_ptr2))) {
325                         token = NULL;
326                         query_value = strtok_r(token, "=", &save_ptr2);
327                         if (NULL == query_value)
328                                 break;
329
330                         g_variant_builder_add(req_ctx->query, "(ss)", query_key, query_value);
331                 }
332                 query_str = NULL;
333         }
334
335         ret = _ocprocess_worker_start(_worker_req_handler, req_ctx);
336         if (IOTCON_ERROR_NONE != ret) {
337                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
338                 free(req_ctx->bus_name);
339                 if (req_ctx->payload)
340                         g_variant_unref(req_ctx->payload);
341                 g_variant_builder_unref(req_ctx->options);
342                 g_variant_builder_unref(req_ctx->query);
343                 free(req_ctx);
344                 return OC_EH_ERROR;
345         }
346
347         /* DO NOT FREE req_ctx. It MUST be freed in the _worker_req_handler func */
348
349         return OC_EH_OK;
350 }
351
352
353 gpointer icd_ioty_ocprocess_thread(gpointer data)
354 {
355         FN_CALL;
356         OCStackResult result;
357
358         icd_ioty_alive = 1;
359         while (icd_ioty_alive) {
360                 icd_ioty_csdk_lock();
361                 result = OCProcess();
362                 icd_ioty_csdk_unlock();
363                 if (OC_STACK_OK != result) {
364                         ERR("OCProcess() Fail(%d)", result);
365                         break;
366                 }
367
368                 /* TODO : SHOULD revise time or usleep */
369                 usleep(10);
370         }
371
372         return NULL;
373 }
374
375
376 static int _worker_find_cb(void *context)
377 {
378         GVariant *value;
379         int i, ret;
380         struct icd_find_context *ctx = context;
381
382         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
383
384         for (i = 0; ctx->payload[i]; i++) {
385                 value = g_variant_new("(vi)", ctx->payload[i], ctx->conn_type);
386                 /* TODO : If one device has multi resources, it comes as bulk data.
387                  * To reduce the number of emit_signal, let's send signal only one time for one device.
388                  * for ex, client list. */
389                 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_FOUND_RESOURCE,
390                                 ctx->signum, value);
391                 if (IOTCON_ERROR_NONE != ret) {
392                         ERR("_ocprocess_response_signal() Fail(%d)", ret);
393                         g_variant_unref(value);
394                         return ret;
395                 }
396         }
397
398         /* ctx was allocated from icd_ioty_ocprocess_find_cb() */
399         free(ctx->bus_name);
400         free(ctx);
401
402         return ret;
403 }
404
405
406 OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
407                 OCClientResponse *resp)
408 {
409         int ret;
410         struct icd_find_context *find_ctx;
411         icd_sig_ctx_s *sig_context = ctx;
412
413         RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
414         RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
415         if (NULL == resp->payload)
416                 /* normal case : payload COULD be NULL */
417                 return OC_STACK_KEEP_TRANSACTION;
418         RETVM_IF(PAYLOAD_TYPE_DISCOVERY != resp->payload->type,
419                         OC_STACK_KEEP_TRANSACTION, "Invalid payload type(%d)", resp->payload->type);
420
421         find_ctx = calloc(1, sizeof(struct icd_find_context));
422         if (NULL == find_ctx) {
423                 ERR("calloc() Fail(%d)", errno);
424                 return OC_STACK_KEEP_TRANSACTION;
425         }
426
427         find_ctx->signum = sig_context->signum;
428         find_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
429         find_ctx->payload = icd_payload_res_to_gvariant(resp->payload, &resp->devAddr);
430         find_ctx->conn_type = icd_ioty_transport_flag_to_conn_type(resp->devAddr.adapter,
431                         resp->devAddr.flags);
432
433         ret = _ocprocess_worker_start(_worker_find_cb, find_ctx);
434         if (IOTCON_ERROR_NONE != ret) {
435                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
436                 free(find_ctx->bus_name);
437                 ic_utils_gvariant_array_free(find_ctx->payload);
438                 free(find_ctx);
439                 return OC_STACK_KEEP_TRANSACTION;
440         }
441
442         /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
443         /* DO NOT FREE find_ctx. It MUST be freed in the _worker_find_cb func */
444
445         return OC_STACK_KEEP_TRANSACTION;
446 }
447
448
449 static int _worker_crud_cb(void *context)
450 {
451         GVariant *value;
452         struct icd_crud_context *ctx = context;
453
454         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
455
456         if (ICD_CRUD_DELETE == ctx->crud_type)
457                 value = g_variant_new("(a(qs)i)", ctx->options, ctx->res);
458         else
459                 value = g_variant_new("(a(qs)vi)", ctx->options, ctx->payload, ctx->res);
460         icd_ioty_complete(ctx->crud_type, ctx->invocation, value);
461
462         /* ctx was allocated from icd_ioty_ocprocess_xxx_cb() */
463         g_variant_builder_unref(ctx->options);
464         free(ctx);
465
466         return IOTCON_ERROR_NONE;
467 }
468
469
470 static int _worker_info_cb(void *context)
471 {
472         int ret;
473         const char *sig_name = NULL;
474         struct icd_info_context *ctx = context;
475
476         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
477
478         if (ICD_DEVICE_INFO == ctx->info_type)
479                 sig_name = IC_DBUS_SIGNAL_DEVICE;
480         else if (ICD_PLATFORM_INFO == ctx->info_type)
481                 sig_name = IC_DBUS_SIGNAL_PLATFORM;
482
483         ret = _ocprocess_response_signal(ctx->bus_name, sig_name, ctx->signum, ctx->payload);
484         if (IOTCON_ERROR_NONE != ret)
485                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
486
487         /* ctx was allocated from icd_ioty_ocprocess_info_cb() */
488         free(ctx->bus_name);
489         free(ctx);
490
491         return ret;
492 }
493
494
495 static int _ocprocess_worker(_ocprocess_cb cb, int type, OCPayload *payload, int res,
496                 GVariantBuilder *options, void *ctx)
497 {
498         int ret;
499         struct icd_crud_context *crud_ctx;
500
501         crud_ctx = calloc(1, sizeof(struct icd_crud_context));
502         if (NULL == crud_ctx) {
503                 ERR("calloc() Fail(%d)", errno);
504                 return IOTCON_ERROR_OUT_OF_MEMORY;
505         }
506
507         crud_ctx->crud_type = type;
508         crud_ctx->payload = icd_payload_to_gvariant(payload);
509         crud_ctx->res = res;
510         crud_ctx->options = options;
511         crud_ctx->invocation = ctx;
512
513         ret = _ocprocess_worker_start(cb, crud_ctx);
514         if (IOTCON_ERROR_NONE != ret) {
515                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
516                 if (crud_ctx->payload)
517                         g_variant_unref(crud_ctx->payload);
518                 g_variant_builder_unref(crud_ctx->options);
519                 free(crud_ctx);
520         }
521
522         /* DO NOT FREE crud_ctx. It MUST be freed in the _worker_crud_cb func */
523
524         return ret;
525 }
526
527
528 static int _ocprocess_parse_oic_result(OCStackResult result)
529 {
530         int res;
531
532         switch (result) {
533         case OC_STACK_OK:
534                 res = IOTCON_RESPONSE_RESULT_OK;
535                 break;
536         case OC_STACK_RESOURCE_CREATED:
537                 res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
538                 break;
539         case OC_STACK_RESOURCE_DELETED:
540                 res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
541                 break;
542         case OC_STACK_UNAUTHORIZED_REQ:
543                 res = IOTCON_RESPONSE_RESULT_FORBIDDEN;
544                 break;
545         default:
546                 WARN("response error(%d)", result);
547                 res = IOTCON_RESPONSE_RESULT_ERROR;
548                 break;
549         }
550
551         return res;
552 }
553
554
555 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
556                 OCClientResponse *resp)
557 {
558         FN_CALL;
559         int ret, res;
560         GVariantBuilder *options;
561
562         RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
563
564         if (NULL == resp->payload) {
565                 ERR("payload is empty");
566                 icd_ioty_complete_error(ICD_CRUD_GET, ctx, IOTCON_ERROR_IOTIVITY);
567                 return OC_STACK_DELETE_TRANSACTION;
568         }
569
570         res = _ocprocess_parse_oic_result(resp->result);
571
572         options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
573                         resp->numRcvdVendorSpecificHeaderOptions);
574
575         ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_GET, resp->payload, res,
576                         options, ctx);
577         if (IOTCON_ERROR_NONE != ret) {
578                 ERR("_ocprocess_worker() Fail(%d)", ret);
579                 icd_ioty_complete_error(ICD_CRUD_GET, ctx, ret);
580                 return OC_STACK_DELETE_TRANSACTION;
581         }
582
583         return OC_STACK_DELETE_TRANSACTION;
584 }
585
586
587 OCStackApplicationResult icd_ioty_ocprocess_put_cb(void *ctx, OCDoHandle handle,
588                 OCClientResponse *resp)
589 {
590         FN_CALL;
591         int ret, res;
592         GVariantBuilder *options;
593
594         RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
595
596         if (NULL == resp->payload) {
597                 ERR("payload is empty");
598                 icd_ioty_complete_error(ICD_CRUD_PUT, ctx, IOTCON_ERROR_IOTIVITY);
599                 return OC_STACK_DELETE_TRANSACTION;
600         }
601
602         res = _ocprocess_parse_oic_result(resp->result);
603
604         options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
605                         resp->numRcvdVendorSpecificHeaderOptions);
606
607         ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_PUT, resp->payload, res,
608                         options, ctx);
609         if (IOTCON_ERROR_NONE != ret) {
610                 ERR("_ocprocess_worker() Fail(%d)", ret);
611                 icd_ioty_complete_error(ICD_CRUD_PUT, ctx, ret);
612                 return OC_STACK_DELETE_TRANSACTION;
613         }
614
615         return OC_STACK_DELETE_TRANSACTION;
616 }
617
618
619 OCStackApplicationResult icd_ioty_ocprocess_post_cb(void *ctx, OCDoHandle handle,
620                 OCClientResponse *resp)
621 {
622         FN_CALL;
623         int ret, res;
624         GVariantBuilder *options;
625
626         RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
627
628         if (NULL == resp->payload) {
629                 ERR("payload is empty");
630                 icd_ioty_complete_error(ICD_CRUD_POST, ctx, IOTCON_ERROR_IOTIVITY);
631                 return OC_STACK_DELETE_TRANSACTION;
632         }
633
634         res = _ocprocess_parse_oic_result(resp->result);
635
636         options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
637                         resp->numRcvdVendorSpecificHeaderOptions);
638
639         ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_POST, resp->payload, res,
640                         options, ctx);
641         if (IOTCON_ERROR_NONE != ret) {
642                 ERR("_ocprocess_worker() Fail(%d)", ret);
643                 icd_ioty_complete_error(ICD_CRUD_POST, ctx, ret);
644                 return OC_STACK_DELETE_TRANSACTION;
645         }
646
647         return OC_STACK_DELETE_TRANSACTION;
648 }
649
650
651 OCStackApplicationResult icd_ioty_ocprocess_delete_cb(void *ctx, OCDoHandle handle,
652                 OCClientResponse *resp)
653 {
654         FN_CALL;
655         int ret, res;
656         GVariantBuilder *options;
657
658         RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
659
660         if (NULL == resp->payload) {
661                 ERR("payload is empty");
662                 icd_ioty_complete_error(ICD_CRUD_DELETE, ctx, IOTCON_ERROR_IOTIVITY);
663                 return OC_STACK_DELETE_TRANSACTION;
664         }
665
666         res = _ocprocess_parse_oic_result(resp->result);
667
668         options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
669                         resp->numRcvdVendorSpecificHeaderOptions);
670
671         ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_DELETE, NULL, res, options, ctx);
672         if (IOTCON_ERROR_NONE != ret) {
673                 ERR("_ocprocess_worker() Fail(%d)", ret);
674                 icd_ioty_complete_error(ICD_CRUD_DELETE, ctx, ret);
675                 return OC_STACK_DELETE_TRANSACTION;
676         }
677
678         return OC_STACK_DELETE_TRANSACTION;
679 }
680
681
682 static int _worker_observe_cb(void *context)
683 {
684         int ret;
685         GVariant *value;
686         struct icd_observe_context *ctx = context;
687
688         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
689
690         value = g_variant_new("(a(qs)vii)", ctx->options, ctx->payload, ctx->res,
691                         ctx->seqnum);
692
693         ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_OBSERVE, ctx->signum,
694                         value);
695         if (IOTCON_ERROR_NONE != ret)
696                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
697
698         /* ctx was allocated from icd_ioty_ocprocess_observe_cb() */
699         free(ctx->bus_name);
700         g_variant_builder_unref(ctx->options);
701         free(ctx);
702
703         return ret;
704 }
705
706
707 static void _observe_cb_response_error(const char *dest, unsigned int signum, int ret_val)
708 {
709         int ret;
710         GVariant *value;
711         GVariant *payload;
712         GVariantBuilder options;
713
714         g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
715         payload = icd_payload_representation_empty_gvariant();
716
717         value = g_variant_new("(a(qs)vii)", &options, payload, ret_val, 0);
718
719         ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_OBSERVE, signum, value);
720         if (IOTCON_ERROR_NONE != ret)
721                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
722 }
723
724
725 OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle handle,
726                 OCClientResponse *resp)
727 {
728         int ret, res;
729         GVariantBuilder *options;
730         struct icd_observe_context *observe_ctx;
731         icd_sig_ctx_s *sig_context = ctx;
732
733         RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
734
735         if (NULL == resp->payload) {
736                 ERR("payload is empty");
737                 _observe_cb_response_error(sig_context->bus_name, sig_context->signum,
738                                 IOTCON_ERROR_IOTIVITY);
739                 return OC_STACK_KEEP_TRANSACTION;
740         }
741
742         observe_ctx = calloc(1, sizeof(struct icd_observe_context));
743         if (NULL == observe_ctx) {
744                 ERR("calloc() Fail(%d)", errno);
745                 _observe_cb_response_error(sig_context->bus_name, sig_context->signum,
746                                 IOTCON_ERROR_OUT_OF_MEMORY);
747                 return OC_STACK_KEEP_TRANSACTION;
748         }
749
750         res = _ocprocess_parse_oic_result(resp->result);
751
752         options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
753                         resp->numRcvdVendorSpecificHeaderOptions);
754
755         observe_ctx->payload = icd_payload_to_gvariant(resp->payload);
756         observe_ctx->signum = sig_context->signum;
757         observe_ctx->res = res;
758         observe_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
759         observe_ctx->options = options;
760
761         ret = _ocprocess_worker_start(_worker_observe_cb, observe_ctx);
762         if (IOTCON_ERROR_NONE != ret) {
763                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
764                 _observe_cb_response_error(sig_context->bus_name, sig_context->signum, ret);
765                 free(observe_ctx->bus_name);
766                 if (observe_ctx->payload)
767                         g_variant_unref(observe_ctx->payload);
768                 g_variant_builder_unref(observe_ctx->options);
769                 free(observe_ctx);
770                 return OC_STACK_KEEP_TRANSACTION;
771         }
772
773         /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
774         /* DO NOT FREE observe_ctx. It MUST be freed in the _worker_observe_cb func */
775
776         return OC_STACK_KEEP_TRANSACTION;
777 }
778
779
780 static int _worker_presence_cb(void *context)
781 {
782         FN_CALL;
783         int ret;
784         GVariant *value;
785         char addr[PATH_MAX] = {0};
786         struct icd_presence_context *ctx = context;
787
788         RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
789
790         snprintf(addr, sizeof(addr), "%s:%d", ctx->dev_addr->addr, ctx->dev_addr->port);
791
792         value = g_variant_new("(ius)", ctx->result, ctx->nonce, addr);
793
794         ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_PRESENCE, ctx->signum,
795                         value);
796         if (IOTCON_ERROR_NONE != ret)
797                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
798
799         /* ctx was allocated from icd_ioty_ocprocess_presence_cb() */
800         free(ctx->bus_name);
801         free(ctx->dev_addr);
802         free(ctx);
803
804         return ret;
805 }
806
807
808 static void _presence_cb_response_error(const char *dest, unsigned int signum,
809                 int ret_val)
810 {
811         FN_CALL;
812         int ret;
813         GVariant *value;
814
815         value = g_variant_new("(ius)", ret_val, 0, IC_STR_NULL);
816
817         ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_PRESENCE, signum, value);
818         if (IOTCON_ERROR_NONE != ret)
819                 ERR("_ocprocess_response_signal() Fail(%d)", ret);
820 }
821
822
823 OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle handle,
824                 OCClientResponse *resp)
825 {
826         FN_CALL;
827         int ret;
828         OCDevAddr *dev_addr;
829         icd_sig_ctx_s *sig_context = ctx;
830         struct icd_presence_context *presence_ctx;
831
832         RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
833         RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
834
835         presence_ctx = calloc(1, sizeof(struct icd_presence_context));
836         if (NULL == presence_ctx) {
837                 ERR("calloc() Fail(%d)", errno);
838                 _presence_cb_response_error(sig_context->bus_name, sig_context->signum,
839                                 IOTCON_ERROR_OUT_OF_MEMORY);
840                 return OC_STACK_KEEP_TRANSACTION;
841         }
842
843         dev_addr = calloc(1, sizeof(OCDevAddr));
844         if (NULL == dev_addr) {
845                 ERR("calloc() Fail(%d)", errno);
846                 _presence_cb_response_error(sig_context->bus_name, sig_context->signum,
847                                 IOTCON_ERROR_OUT_OF_MEMORY);
848                 free(presence_ctx);
849                 return OC_STACK_KEEP_TRANSACTION;
850         }
851         memcpy(dev_addr, &resp->devAddr, sizeof(OCDevAddr));
852
853         switch (resp->result) {
854         case OC_STACK_OK:
855                 presence_ctx->result = IOTCON_PRESENCE_OK;
856                 break;
857         case OC_STACK_PRESENCE_STOPPED:
858                 presence_ctx->result = IOTCON_PRESENCE_STOPPED;
859                 break;
860         case OC_STACK_PRESENCE_TIMEOUT:
861                 presence_ctx->result = IOTCON_PRESENCE_TIMEOUT;
862                 break;
863         case OC_STACK_ERROR:
864         default:
865                 DBG("Presence error(%d)", resp->result);
866                 presence_ctx->result = IOTCON_ERROR_IOTIVITY;
867         }
868
869         presence_ctx->signum = sig_context->signum;
870         presence_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
871         presence_ctx->nonce = resp->sequenceNumber;
872         presence_ctx->dev_addr = dev_addr;
873
874         ret = _ocprocess_worker_start(_worker_presence_cb, presence_ctx);
875         if (IOTCON_ERROR_NONE != ret) {
876                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
877                 _presence_cb_response_error(sig_context->bus_name, sig_context->signum, ret);
878                 free(presence_ctx->bus_name);
879                 free(presence_ctx->dev_addr);
880                 free(presence_ctx);
881                 return OC_STACK_KEEP_TRANSACTION;
882         }
883
884         /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
885         /* DO NOT FREE presence_ctx. It MUST be freed in the _worker_presence_cb func */
886
887         return OC_STACK_KEEP_TRANSACTION;
888 }
889
890
891 OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle,
892                 OCClientResponse *resp)
893 {
894         int ret;
895         int info_type;
896         struct icd_info_context *info_ctx;
897         icd_sig_ctx_s *sig_context = ctx;
898
899         RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
900         RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
901         RETV_IF(NULL == resp->payload, OC_STACK_KEEP_TRANSACTION);
902
903         if (PAYLOAD_TYPE_DEVICE == resp->payload->type)
904                 info_type = ICD_DEVICE_INFO;
905         else if (PAYLOAD_TYPE_PLATFORM == resp->payload->type)
906                 info_type = ICD_PLATFORM_INFO;
907         else
908                 return OC_STACK_KEEP_TRANSACTION;
909
910         info_ctx = calloc(1, sizeof(struct icd_info_context));
911         if (NULL == info_ctx) {
912                 ERR("calloc() Fail(%d)", errno);
913                 return OC_STACK_KEEP_TRANSACTION;
914         }
915
916         info_ctx->info_type = info_type;
917         info_ctx->payload = icd_payload_to_gvariant(resp->payload);
918         info_ctx->signum = sig_context->signum;
919         info_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
920
921         ret = _ocprocess_worker_start(_worker_info_cb, info_ctx);
922         if (IOTCON_ERROR_NONE != ret) {
923                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
924                 free(info_ctx->bus_name);
925                 if (info_ctx->payload)
926                         g_variant_unref(info_ctx->payload);
927                 free(info_ctx);
928                 return OC_STACK_KEEP_TRANSACTION;
929         }
930
931         /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
932         /* DO NOT FREE info_ctx. It MUST be freed in the _worker_info_cb func */
933
934         return OC_STACK_KEEP_TRANSACTION;
935 }
936
937
938 static int _worker_tizen_info_handler(void *context)
939 {
940         int ret;
941         char *device_name;
942         char *tizen_device_id;
943         OCStackResult result;
944         OCRepPayload *payload;
945         OCEntityHandlerResponse response = {0};
946         struct icd_tizen_info_context *ctx = context;
947
948         response.requestHandle = ctx->request_h;
949         response.resourceHandle = ctx->resource_h;
950         response.ehResult = OC_EH_OK;
951
952         /* Get Tizen Info */
953         ret = icd_ioty_tizen_info_get_property(&device_name, &tizen_device_id);
954         if (IOTCON_ERROR_NONE != ret) {
955                 ERR("icd_ioty_tizen_info_get_property() Fail(%d)", ret);
956                 response.ehResult = OC_EH_ERROR;
957         }
958
959         /* payload */
960         payload = OCRepPayloadCreate();
961         OCRepPayloadSetUri(payload, ICD_IOTY_TIZEN_INFO_URI);
962         OCRepPayloadAddResourceType(payload, ICD_IOTY_TIZEN_INFO_TYPE);
963         OCRepPayloadAddInterface(payload, IC_INTERFACE_DEFAULT);
964
965         OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_DEVICE_NAME,
966                         ic_utils_dbus_encode_str(device_name));
967         OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_TIZEN_DEVICE_ID,
968                         ic_utils_dbus_encode_str(tizen_device_id));
969         response.payload = (OCPayload*)payload;
970
971         icd_ioty_csdk_lock();
972         result = OCDoResponse(&response);
973         icd_ioty_csdk_unlock();
974
975         if (OC_STACK_OK != result) {
976                 ERR("OCDoResponse() Fail(%d)", result);
977                 free(ctx);
978                 return IOTCON_ERROR_IOTIVITY;
979         }
980
981         free(ctx);
982         return IOTCON_ERROR_NONE;
983 }
984
985
986 OCEntityHandlerResult icd_ioty_ocprocess_tizen_info_handler(OCEntityHandlerFlag flag,
987                 OCEntityHandlerRequest *request, void *user_data)
988 {
989         int ret;
990         struct icd_tizen_info_context *tizen_info_ctx;
991
992         if ((0 == (OC_REQUEST_FLAG & flag)) || (OC_REST_GET != request->method)) {
993                 ERR("Prohibited Action");
994                 return OC_EH_FORBIDDEN;
995         }
996
997         tizen_info_ctx = calloc(1, sizeof(struct icd_tizen_info_context));
998         if (NULL == tizen_info_ctx) {
999                 ERR("calloc() Fail(%d)", errno);
1000                 return OC_EH_ERROR;
1001         }
1002
1003         tizen_info_ctx->request_h = request->requestHandle;
1004         tizen_info_ctx->resource_h = request->resource;
1005
1006         ret = _ocprocess_worker_start(_worker_tizen_info_handler, tizen_info_ctx);
1007         if (IOTCON_ERROR_NONE != ret) {
1008                 ERR("_ocprocess_worker_start() Fail(%d)", ret);
1009                 free(tizen_info_ctx);
1010                 return OC_EH_ERROR;
1011         }
1012
1013         return OC_EH_OK;
1014 }
1015
1016
1017 OCStackApplicationResult icd_ioty_ocprocess_get_tizen_info_cb(void *ctx,
1018                 OCDoHandle handle, OCClientResponse *resp)
1019 {
1020         int res;
1021         char *device_name;
1022         char *tizen_device_id;
1023         GVariant *tizen_info;
1024         OCRepPayload *payload;
1025         OCRepPayloadValue *val;
1026
1027         RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
1028
1029         if (NULL == resp->payload) {
1030                 ERR("payload is empty");
1031                 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1032                 return OC_STACK_DELETE_TRANSACTION;
1033         }
1034
1035         payload = (OCRepPayload*)resp->payload;
1036         val = payload->values;
1037         if (NULL == val) {
1038                 ERR("Invalid payload");
1039                 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1040                 return OC_STACK_DELETE_TRANSACTION;
1041         }
1042         device_name = val->str;
1043
1044         val = val->next;
1045         if (NULL == val) {
1046                 ERR("Invalid Payload");
1047                 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1048                 return OC_STACK_DELETE_TRANSACTION;
1049         }
1050
1051         tizen_device_id = val->str;
1052
1053         if (OC_STACK_OK == resp->result)
1054                 res = IOTCON_RESPONSE_RESULT_OK;
1055         else
1056                 res = IOTCON_RESPONSE_RESULT_ERROR;
1057
1058         tizen_info = g_variant_new("(ssi)", device_name, tizen_device_id, res);
1059
1060         icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
1061
1062         return OC_STACK_DELETE_TRANSACTION;
1063 }