2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <unistd.h> /* for usleep() */
23 #include <ocpayload.h>
28 #include "icd-payload.h"
31 #include "icd-ioty-type.h"
32 #include "icd-ioty-ocprocess.h"
34 static int icd_ioty_alive;
36 typedef int (*_ocprocess_cb)(void *user_data);
38 struct icd_ioty_worker
45 struct icd_req_context {
46 unsigned int signal_number;
51 OCRequestHandle request_h;
52 OCResourceHandle resource_h;
54 GVariantBuilder *options;
55 GVariantBuilder *query;
60 struct icd_find_context {
61 unsigned int signal_number;
68 struct icd_crud_context {
72 GVariantBuilder *options;
73 GDBusMethodInvocation *invocation;
77 struct icd_info_context {
78 unsigned int signal_number;
85 struct icd_tizen_info_context {
86 OCRequestHandle request_h;
87 OCResourceHandle resource_h;
88 GDBusMethodInvocation *invocation;
92 struct icd_observe_context {
93 unsigned int signal_number;
98 GVariantBuilder *options;
102 struct icd_presence_context {
103 unsigned int signal_number;
108 iotcon_presence_trigger_e trigger;
113 void icd_ioty_ocprocess_stop()
119 static void* _ocprocess_worker_thread(void *data)
122 struct icd_ioty_worker *worker = data;
125 ERR("worker is NULL");
129 ret = worker->cb(worker->ctx);
130 if (IOTCON_ERROR_NONE != ret)
131 ERR("cb() Fail(%d)", ret);
133 /* worker was allocated from _ocprocess_worker_start() */
136 /* GCC warning happen if use g_thread_exit() */
141 static int _ocprocess_worker_start(_ocprocess_cb cb, void *ctx)
145 struct icd_ioty_worker *worker;
147 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
149 worker = calloc(1, sizeof(struct icd_ioty_worker));
150 if (NULL == worker) {
151 ERR("calloc() Fail(%d)", errno);
152 return IOTCON_ERROR_OUT_OF_MEMORY;
158 /* TODO : consider thread pool mechanism */
159 thread = g_thread_try_new("worker_thread", _ocprocess_worker_thread, worker, &error);
160 if (NULL == thread) {
161 ERR("g_thread_try_new() Fail(%s)", error->message);
164 return IOTCON_ERROR_SYSTEM;
167 /* DO NOT join thread. It was already detached by calling g_thread_unref() */
168 g_thread_unref(thread);
170 /* DO NOT FREE worker. It MUST be freed in the _ocprocess_worker_thread() */
172 return IOTCON_ERROR_NONE;
176 static int _ocprocess_response_signal(const char *dest, const char *signal_prefix,
177 unsigned int signal_number, GVariant *value)
180 char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
182 ret = snprintf(signal_name, sizeof(signal_name), "%s_%u", signal_prefix, signal_number);
183 if (ret <= 0 || sizeof(signal_name) <= ret) {
184 ERR("snprintf() Fail(%d)", ret);
185 return IOTCON_ERROR_IO_ERROR;
188 ret = icd_dbus_emit_signal(dest, signal_name, value);
189 if (IOTCON_ERROR_NONE != ret) {
190 ERR("icd_dbus_emit_signal() Fail(%d)", ret);
194 return IOTCON_ERROR_NONE;
198 static inline GVariantBuilder* _ocprocess_parse_header_options(
199 OCHeaderOption *oic_option, int option_size)
202 GVariantBuilder *options;
204 options = g_variant_builder_new(G_VARIANT_TYPE("a(qs)"));
205 for (i = 0; i < option_size; i++) {
206 g_variant_builder_add(options, "(qs)", oic_option[i].optionID,
207 oic_option[i].optionData);
213 static int _ioty_oic_action_to_ioty_action(int oic_action)
217 switch (oic_action) {
218 case OC_OBSERVE_REGISTER:
219 action = IOTCON_OBSERVE_REGISTER;
221 case OC_OBSERVE_DEREGISTER:
222 action = IOTCON_OBSERVE_DEREGISTER;
224 case OC_OBSERVE_NO_OPTION:
226 ERR("Invalid action (%d)", oic_action);
227 action = IOTCON_OBSERVE_TYPE_NONE;
233 static void _icd_req_context_free(struct icd_req_context *ctx)
237 g_variant_unref(ctx->payload);
238 g_variant_builder_unref(ctx->options);
239 g_variant_builder_unref(ctx->query);
245 static int _worker_req_handler(void *context)
250 GVariantBuilder payload_builder;
251 struct icd_req_context *ctx = context;
253 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
255 g_variant_builder_init(&payload_builder, G_VARIANT_TYPE("av"));
257 g_variant_builder_add(&payload_builder, "v", ctx->payload);
261 ret = icd_ioty_get_host_address(ctx->dev_addr, &host_address, &conn_type);
262 if (IOTCON_ERROR_NONE != ret) {
263 ERR("icd_ioty_get_host_address() Fail(%d)", ret);
264 g_variant_builder_clear(&payload_builder);
265 _icd_req_context_free(ctx);
269 value = g_variant_new("(siia(qs)a(ss)iiavxx)",
278 ICD_POINTER_TO_INT64(ctx->request_h),
279 ICD_POINTER_TO_INT64(ctx->resource_h));
283 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_REQUEST_HANDLER,
284 ctx->signal_number, value);
285 if (IOTCON_ERROR_NONE != ret)
286 ERR("_ocprocess_response_signal() Fail(%d)", ret);
288 _icd_req_context_free(ctx);
294 OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
295 OCEntityHandlerRequest *request, void *user_data)
299 unsigned int signal_number;
300 char *query_str, *query_key, *query_value;
301 char *token, *save_ptr1, *save_ptr2;
302 char *bus_name = NULL;
303 struct icd_req_context *req_ctx;
306 RETV_IF(NULL == request, OC_EH_ERROR);
308 req_ctx = calloc(1, sizeof(struct icd_req_context));
309 if (NULL == req_ctx) {
310 ERR("calloc() Fail(%d)", errno);
315 req_ctx->request_h = request->requestHandle;
316 req_ctx->resource_h = request->resource;
318 ret = icd_dbus_client_list_get_info(req_ctx->resource_h, &signal_number, &bus_name);
319 if (IOTCON_ERROR_NONE != ret) {
320 ERR("icd_dbus_client_list_get_info() Fail(%d)", ret);
325 /* signal number & bus_name */
326 req_ctx->signal_number = signal_number;
327 req_ctx->bus_name = bus_name;
329 dev_addr = calloc(1, sizeof(OCDevAddr));
330 if (NULL == dev_addr) {
331 ERR("calloc() Fail(%d)", errno);
335 memcpy(dev_addr, &request->devAddr, sizeof(OCDevAddr));
336 req_ctx->dev_addr = dev_addr;
339 if (OC_REQUEST_FLAG & flag) {
340 switch (request->method) {
342 req_ctx->request_type = IOTCON_REQUEST_GET;
343 req_ctx->payload = NULL;
346 req_ctx->request_type = IOTCON_REQUEST_PUT;
347 req_ctx->payload = icd_payload_to_gvariant(request->payload);
350 req_ctx->request_type = IOTCON_REQUEST_POST;
351 req_ctx->payload = icd_payload_to_gvariant(request->payload);
354 req_ctx->request_type = IOTCON_REQUEST_DELETE;
355 req_ctx->payload = NULL;
358 free(req_ctx->bus_name);
364 if (OC_OBSERVE_FLAG & flag) {
365 /* observation info*/
366 req_ctx->observe_id = request->obsInfo.obsId;
367 req_ctx->observe_type = _ioty_oic_action_to_ioty_action(request->obsInfo.action);
369 req_ctx->observe_type = IOTCON_OBSERVE_TYPE_NONE;
373 req_ctx->options = _ocprocess_parse_header_options(
374 request->rcvdVendorSpecificHeaderOptions,
375 request->numRcvdVendorSpecificHeaderOptions);
378 req_ctx->query = g_variant_builder_new(G_VARIANT_TYPE("a(ss)"));
379 query_str = request->query;
380 while ((token = strtok_r(query_str, "&;", &save_ptr1))) {
381 while ((query_key = strtok_r(token, "=", &save_ptr2))) {
383 query_value = strtok_r(token, "=", &save_ptr2);
384 if (NULL == query_value)
387 g_variant_builder_add(req_ctx->query, "(ss)", query_key, query_value);
392 ret = _ocprocess_worker_start(_worker_req_handler, req_ctx);
393 if (IOTCON_ERROR_NONE != ret) {
394 ERR("_ocprocess_worker_start() Fail(%d)", ret);
395 _icd_req_context_free(req_ctx);
399 /* DO NOT FREE req_ctx. It MUST be freed in the _worker_req_handler func */
405 gpointer icd_ioty_ocprocess_thread(gpointer data)
408 OCStackResult result;
411 while (icd_ioty_alive) {
412 icd_ioty_csdk_lock();
413 result = OCProcess();
414 icd_ioty_csdk_unlock();
415 if (OC_STACK_OK != result) {
416 ERR("OCProcess() Fail(%d)", result);
420 /* TODO : SHOULD revise time or usleep */
428 static int _worker_find_cb(void *context)
432 struct icd_find_context *ctx = context;
434 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
436 for (i = 0; ctx->payload[i]; i++) {
437 value = g_variant_new("(vi)", ctx->payload[i], ctx->conn_type);
438 /* TODO : If one device has multi resources, it comes as bulk data.
439 * To reduce the number of emit_signal, let's send signal only one time for one device.
440 * for ex, client list. */
441 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_FOUND_RESOURCE,
442 ctx->signal_number, value);
443 if (IOTCON_ERROR_NONE != ret) {
444 ERR("_ocprocess_response_signal() Fail(%d)", ret);
445 g_variant_unref(value);
450 /* ctx was allocated from icd_ioty_ocprocess_find_cb() */
458 OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
459 OCClientResponse *resp)
462 struct icd_find_context *find_ctx;
463 icd_sig_ctx_s *sig_context = ctx;
465 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
466 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
467 if (NULL == resp->payload)
468 /* normal case : payload COULD be NULL */
469 return OC_STACK_KEEP_TRANSACTION;
470 RETVM_IF(PAYLOAD_TYPE_DISCOVERY != resp->payload->type,
471 OC_STACK_KEEP_TRANSACTION, "Invalid payload type(%d)", resp->payload->type);
473 find_ctx = calloc(1, sizeof(struct icd_find_context));
474 if (NULL == find_ctx) {
475 ERR("calloc() Fail(%d)", errno);
476 return OC_STACK_KEEP_TRANSACTION;
479 find_ctx->signal_number = sig_context->signal_number;
480 find_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
481 find_ctx->payload = icd_payload_res_to_gvariant(resp->payload, &resp->devAddr);
482 find_ctx->conn_type = icd_ioty_transport_flag_to_conn_type(resp->devAddr.adapter,
483 resp->devAddr.flags);
485 ret = _ocprocess_worker_start(_worker_find_cb, find_ctx);
486 if (IOTCON_ERROR_NONE != ret) {
487 ERR("_ocprocess_worker_start() Fail(%d)", ret);
488 free(find_ctx->bus_name);
489 ic_utils_gvariant_array_free(find_ctx->payload);
491 return OC_STACK_KEEP_TRANSACTION;
494 /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
495 /* DO NOT FREE find_ctx. It MUST be freed in the _worker_find_cb func */
497 return OC_STACK_KEEP_TRANSACTION;
501 static int _worker_crud_cb(void *context)
504 struct icd_crud_context *ctx = context;
506 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
508 if (ICD_CRUD_DELETE == ctx->crud_type)
509 value = g_variant_new("(a(qs)i)", ctx->options, ctx->res);
511 value = g_variant_new("(a(qs)vi)", ctx->options, ctx->payload, ctx->res);
512 icd_ioty_complete(ctx->crud_type, ctx->invocation, value);
514 /* ctx was allocated from icd_ioty_ocprocess_xxx_cb() */
515 g_variant_builder_unref(ctx->options);
518 return IOTCON_ERROR_NONE;
522 static int _worker_info_cb(void *context)
525 const char *signal_prefix = NULL;
526 struct icd_info_context *ctx = context;
528 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
530 if (ICD_DEVICE_INFO == ctx->info_type)
531 signal_prefix = IC_DBUS_SIGNAL_DEVICE;
532 else if (ICD_PLATFORM_INFO == ctx->info_type)
533 signal_prefix = IC_DBUS_SIGNAL_PLATFORM;
535 ret = _ocprocess_response_signal(ctx->bus_name, signal_prefix, ctx->signal_number,
537 if (IOTCON_ERROR_NONE != ret)
538 ERR("_ocprocess_response_signal() Fail(%d)", ret);
540 /* ctx was allocated from icd_ioty_ocprocess_info_cb() */
548 static int _ocprocess_worker(_ocprocess_cb cb, int type, OCPayload *payload, int res,
549 GVariantBuilder *options, void *ctx)
552 struct icd_crud_context *crud_ctx;
554 crud_ctx = calloc(1, sizeof(struct icd_crud_context));
555 if (NULL == crud_ctx) {
556 ERR("calloc() Fail(%d)", errno);
557 return IOTCON_ERROR_OUT_OF_MEMORY;
560 crud_ctx->crud_type = type;
561 crud_ctx->payload = icd_payload_to_gvariant(payload);
563 crud_ctx->options = options;
564 crud_ctx->invocation = ctx;
566 ret = _ocprocess_worker_start(cb, crud_ctx);
567 if (IOTCON_ERROR_NONE != ret) {
568 ERR("_ocprocess_worker_start() Fail(%d)", ret);
569 if (crud_ctx->payload)
570 g_variant_unref(crud_ctx->payload);
571 g_variant_builder_unref(crud_ctx->options);
575 /* DO NOT FREE crud_ctx. It MUST be freed in the _worker_crud_cb func */
580 static int _ocprocess_parse_oic_result(OCStackResult result)
586 res = IOTCON_RESPONSE_RESULT_OK;
588 case OC_STACK_RESOURCE_CREATED:
589 res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
591 case OC_STACK_RESOURCE_DELETED:
592 res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
594 case OC_STACK_UNAUTHORIZED_REQ:
595 res = IOTCON_RESPONSE_RESULT_FORBIDDEN;
598 WARN("response error(%d)", result);
599 res = IOTCON_RESPONSE_RESULT_ERROR;
607 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
608 OCClientResponse *resp)
612 GVariantBuilder *options;
614 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
616 if (NULL == resp->payload) {
617 ERR("payload is empty");
618 icd_ioty_complete_error(ICD_CRUD_GET, ctx, IOTCON_ERROR_IOTIVITY);
619 return OC_STACK_DELETE_TRANSACTION;
622 res = _ocprocess_parse_oic_result(resp->result);
624 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
625 resp->numRcvdVendorSpecificHeaderOptions);
627 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_GET, resp->payload, res,
629 if (IOTCON_ERROR_NONE != ret) {
630 ERR("_ocprocess_worker() Fail(%d)", ret);
631 icd_ioty_complete_error(ICD_CRUD_GET, ctx, ret);
632 return OC_STACK_DELETE_TRANSACTION;
635 return OC_STACK_DELETE_TRANSACTION;
639 OCStackApplicationResult icd_ioty_ocprocess_put_cb(void *ctx, OCDoHandle handle,
640 OCClientResponse *resp)
644 GVariantBuilder *options;
646 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
648 if (NULL == resp->payload) {
649 ERR("payload is empty");
650 icd_ioty_complete_error(ICD_CRUD_PUT, ctx, IOTCON_ERROR_IOTIVITY);
651 return OC_STACK_DELETE_TRANSACTION;
654 res = _ocprocess_parse_oic_result(resp->result);
656 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
657 resp->numRcvdVendorSpecificHeaderOptions);
659 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_PUT, resp->payload, res,
661 if (IOTCON_ERROR_NONE != ret) {
662 ERR("_ocprocess_worker() Fail(%d)", ret);
663 icd_ioty_complete_error(ICD_CRUD_PUT, ctx, ret);
664 return OC_STACK_DELETE_TRANSACTION;
667 return OC_STACK_DELETE_TRANSACTION;
671 OCStackApplicationResult icd_ioty_ocprocess_post_cb(void *ctx, OCDoHandle handle,
672 OCClientResponse *resp)
676 GVariantBuilder *options;
678 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
680 if (NULL == resp->payload) {
681 ERR("payload is empty");
682 icd_ioty_complete_error(ICD_CRUD_POST, ctx, IOTCON_ERROR_IOTIVITY);
683 return OC_STACK_DELETE_TRANSACTION;
686 res = _ocprocess_parse_oic_result(resp->result);
688 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
689 resp->numRcvdVendorSpecificHeaderOptions);
691 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_POST, resp->payload, res,
693 if (IOTCON_ERROR_NONE != ret) {
694 ERR("_ocprocess_worker() Fail(%d)", ret);
695 icd_ioty_complete_error(ICD_CRUD_POST, ctx, ret);
696 return OC_STACK_DELETE_TRANSACTION;
699 return OC_STACK_DELETE_TRANSACTION;
703 OCStackApplicationResult icd_ioty_ocprocess_delete_cb(void *ctx, OCDoHandle handle,
704 OCClientResponse *resp)
708 GVariantBuilder *options;
710 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
712 if (NULL == resp->payload) {
713 ERR("payload is empty");
714 icd_ioty_complete_error(ICD_CRUD_DELETE, ctx, IOTCON_ERROR_IOTIVITY);
715 return OC_STACK_DELETE_TRANSACTION;
718 res = _ocprocess_parse_oic_result(resp->result);
720 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
721 resp->numRcvdVendorSpecificHeaderOptions);
723 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_DELETE, NULL, res, options, ctx);
724 if (IOTCON_ERROR_NONE != ret) {
725 ERR("_ocprocess_worker() Fail(%d)", ret);
726 icd_ioty_complete_error(ICD_CRUD_DELETE, ctx, ret);
727 return OC_STACK_DELETE_TRANSACTION;
730 return OC_STACK_DELETE_TRANSACTION;
734 static int _worker_observe_cb(void *context)
738 struct icd_observe_context *ctx = context;
740 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
742 value = g_variant_new("(a(qs)vii)", ctx->options, ctx->payload, ctx->res,
745 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_OBSERVE,
746 ctx->signal_number, value);
747 if (IOTCON_ERROR_NONE != ret)
748 ERR("_ocprocess_response_signal() Fail(%d)", ret);
750 /* ctx was allocated from icd_ioty_ocprocess_observe_cb() */
752 g_variant_builder_unref(ctx->options);
759 static void _observe_cb_response_error(const char *dest,
760 unsigned int signal_number, int ret_val)
765 GVariantBuilder options;
767 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
768 payload = icd_payload_representation_empty_gvariant();
770 value = g_variant_new("(a(qs)vii)", &options, payload, ret_val, 0);
772 ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_OBSERVE, signal_number, value);
773 if (IOTCON_ERROR_NONE != ret)
774 ERR("_ocprocess_response_signal() Fail(%d)", ret);
778 OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx,
779 OCDoHandle handle, OCClientResponse *resp)
782 GVariantBuilder *options;
783 struct icd_observe_context *observe_ctx;
784 icd_sig_ctx_s *sig_context = ctx;
786 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
788 if (NULL == resp->payload) {
789 ERR("payload is empty");
790 _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number,
791 IOTCON_ERROR_IOTIVITY);
792 return OC_STACK_KEEP_TRANSACTION;
795 observe_ctx = calloc(1, sizeof(struct icd_observe_context));
796 if (NULL == observe_ctx) {
797 ERR("calloc() Fail(%d)", errno);
798 _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number,
799 IOTCON_ERROR_OUT_OF_MEMORY);
800 return OC_STACK_KEEP_TRANSACTION;
803 res = _ocprocess_parse_oic_result(resp->result);
805 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
806 resp->numRcvdVendorSpecificHeaderOptions);
808 observe_ctx->payload = icd_payload_to_gvariant(resp->payload);
809 observe_ctx->signal_number = sig_context->signal_number;
810 observe_ctx->res = res;
811 observe_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
812 observe_ctx->options = options;
814 ret = _ocprocess_worker_start(_worker_observe_cb, observe_ctx);
815 if (IOTCON_ERROR_NONE != ret) {
816 ERR("_ocprocess_worker_start() Fail(%d)", ret);
817 _observe_cb_response_error(sig_context->bus_name, sig_context->signal_number, ret);
818 free(observe_ctx->bus_name);
819 if (observe_ctx->payload)
820 g_variant_unref(observe_ctx->payload);
821 g_variant_builder_unref(observe_ctx->options);
823 return OC_STACK_KEEP_TRANSACTION;
826 /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
827 /* DO NOT FREE observe_ctx. It MUST be freed in the _worker_observe_cb func */
829 return OC_STACK_KEEP_TRANSACTION;
833 static int _worker_presence_cb(void *context)
838 struct icd_presence_context *ctx = context;
840 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
842 ret = icd_ioty_get_host_address(ctx->dev_addr, &host_address, &conn_type);
843 if (IOTCON_ERROR_NONE != ret) {
844 ERR("icd_ioty_get_host_address() Fail(%d)", ret);
845 free(ctx->resource_type);
852 value = g_variant_new("(iusiis)", ctx->result, ctx->nonce, host_address, conn_type,
853 ctx->trigger, ic_utils_dbus_encode_str(ctx->resource_type));
857 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_PRESENCE,
858 ctx->signal_number, value);
859 if (IOTCON_ERROR_NONE != ret)
860 ERR("_ocprocess_response_signal() Fail(%d)", ret);
862 /* ctx was allocated from icd_ioty_ocprocess_presence_cb() */
863 free(ctx->resource_type);
872 static void _presence_cb_response_error(const char *dest, unsigned int signal_number,
879 value = g_variant_new("(iusiis)", ret_val, 0, IC_STR_NULL, IOTCON_CONNECTIVITY_ALL,
880 IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED, IC_STR_NULL);
882 ret = _ocprocess_response_signal(dest, IC_DBUS_SIGNAL_PRESENCE, signal_number, value);
883 if (IOTCON_ERROR_NONE != ret)
884 ERR("_ocprocess_response_signal() Fail(%d)", ret);
888 static int _presence_trigger_to_ioty_trigger(OCPresenceTrigger src,
889 iotcon_presence_trigger_e *dest)
891 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
894 case OC_PRESENCE_TRIGGER_CREATE:
895 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED;
897 case OC_PRESENCE_TRIGGER_CHANGE:
898 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_UPDATED;
900 case OC_PRESENCE_TRIGGER_DELETE:
901 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_DESTROYED;
904 ERR("Invalid trigger(%d)", src);
905 return IOTCON_ERROR_INVALID_PARAMETER;
908 return IOTCON_ERROR_NONE;
912 OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle handle,
913 OCClientResponse *resp)
918 OCPresencePayload *payload;
919 icd_sig_ctx_s *sig_context = ctx;
920 struct icd_presence_context *presence_ctx;
922 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
923 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
925 presence_ctx = calloc(1, sizeof(struct icd_presence_context));
926 if (NULL == presence_ctx) {
927 ERR("calloc() Fail(%d)", errno);
928 _presence_cb_response_error(sig_context->bus_name, sig_context->signal_number,
929 IOTCON_ERROR_OUT_OF_MEMORY);
930 return OC_STACK_KEEP_TRANSACTION;
933 dev_addr = calloc(1, sizeof(OCDevAddr));
934 if (NULL == dev_addr) {
935 ERR("calloc() Fail(%d)", errno);
936 _presence_cb_response_error(sig_context->bus_name, sig_context->signal_number,
937 IOTCON_ERROR_OUT_OF_MEMORY);
939 return OC_STACK_KEEP_TRANSACTION;
941 memcpy(dev_addr, &resp->devAddr, sizeof(OCDevAddr));
943 payload = (OCPresencePayload*)resp->payload;
945 switch (resp->result) {
947 presence_ctx->result = IOTCON_PRESENCE_OK;
948 ret = _presence_trigger_to_ioty_trigger(payload->trigger, &presence_ctx->trigger);
949 if (IOTCON_ERROR_NONE != ret) {
950 ERR("_presence_trigger_to_ioty_trigger() Fail(%d)", ret);
951 _presence_cb_response_error(sig_context->bus_name, sig_context->signal_number,
954 return OC_STACK_KEEP_TRANSACTION;
957 case OC_STACK_PRESENCE_STOPPED:
958 presence_ctx->result = IOTCON_PRESENCE_STOPPED;
960 case OC_STACK_PRESENCE_TIMEOUT:
961 presence_ctx->result = IOTCON_PRESENCE_TIMEOUT;
965 DBG("Presence error(%d)", resp->result);
966 presence_ctx->result = IOTCON_ERROR_IOTIVITY;
969 presence_ctx->signal_number = sig_context->signal_number;
970 presence_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
971 presence_ctx->nonce = resp->sequenceNumber;
972 presence_ctx->dev_addr = dev_addr;
974 if (payload->resourceType)
975 presence_ctx->resource_type = strdup(payload->resourceType);
977 ret = _ocprocess_worker_start(_worker_presence_cb, presence_ctx);
978 if (IOTCON_ERROR_NONE != ret) {
979 ERR("_ocprocess_worker_start() Fail(%d)", ret);
980 _presence_cb_response_error(sig_context->bus_name, sig_context->signal_number, ret);
981 free(presence_ctx->resource_type);
982 free(presence_ctx->bus_name);
983 free(presence_ctx->dev_addr);
985 return OC_STACK_KEEP_TRANSACTION;
988 /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
989 /* DO NOT FREE presence_ctx. It MUST be freed in the _worker_presence_cb func */
991 return OC_STACK_KEEP_TRANSACTION;
995 OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle,
996 OCClientResponse *resp)
1000 struct icd_info_context *info_ctx;
1001 icd_sig_ctx_s *sig_context = ctx;
1003 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
1004 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
1005 RETV_IF(NULL == resp->payload, OC_STACK_KEEP_TRANSACTION);
1007 if (PAYLOAD_TYPE_DEVICE == resp->payload->type)
1008 info_type = ICD_DEVICE_INFO;
1009 else if (PAYLOAD_TYPE_PLATFORM == resp->payload->type)
1010 info_type = ICD_PLATFORM_INFO;
1012 return OC_STACK_KEEP_TRANSACTION;
1014 info_ctx = calloc(1, sizeof(struct icd_info_context));
1015 if (NULL == info_ctx) {
1016 ERR("calloc() Fail(%d)", errno);
1017 return OC_STACK_KEEP_TRANSACTION;
1020 info_ctx->info_type = info_type;
1021 info_ctx->payload = icd_payload_to_gvariant(resp->payload);
1022 info_ctx->signal_number = sig_context->signal_number;
1023 info_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
1025 ret = _ocprocess_worker_start(_worker_info_cb, info_ctx);
1026 if (IOTCON_ERROR_NONE != ret) {
1027 ERR("_ocprocess_worker_start() Fail(%d)", ret);
1028 free(info_ctx->bus_name);
1029 if (info_ctx->payload)
1030 g_variant_unref(info_ctx->payload);
1032 return OC_STACK_KEEP_TRANSACTION;
1035 /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
1036 /* DO NOT FREE info_ctx. It MUST be freed in the _worker_info_cb func */
1038 return OC_STACK_KEEP_TRANSACTION;
1042 static int _worker_tizen_info_handler(void *context)
1046 char *tizen_device_id;
1047 OCStackResult result;
1048 OCRepPayload *payload;
1049 OCEntityHandlerResponse response = {0};
1050 struct icd_tizen_info_context *ctx = context;
1052 response.requestHandle = ctx->request_h;
1053 response.resourceHandle = ctx->resource_h;
1054 response.ehResult = OC_EH_OK;
1056 /* Get Tizen Info */
1057 ret = icd_ioty_tizen_info_get_property(&device_name, &tizen_device_id);
1058 if (IOTCON_ERROR_NONE != ret) {
1059 ERR("icd_ioty_tizen_info_get_property() Fail(%d)", ret);
1060 response.ehResult = OC_EH_ERROR;
1064 payload = OCRepPayloadCreate();
1065 OCRepPayloadSetUri(payload, ICD_IOTY_TIZEN_INFO_URI);
1066 OCRepPayloadAddResourceType(payload, ICD_IOTY_TIZEN_INFO_TYPE);
1067 OCRepPayloadAddInterface(payload, IC_INTERFACE_DEFAULT);
1069 OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_DEVICE_NAME,
1070 ic_utils_dbus_encode_str(device_name));
1071 OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_TIZEN_DEVICE_ID,
1072 ic_utils_dbus_encode_str(tizen_device_id));
1073 response.payload = (OCPayload*)payload;
1075 icd_ioty_csdk_lock();
1076 result = OCDoResponse(&response);
1077 icd_ioty_csdk_unlock();
1079 if (OC_STACK_OK != result) {
1080 ERR("OCDoResponse() Fail(%d)", result);
1082 return IOTCON_ERROR_IOTIVITY;
1086 return IOTCON_ERROR_NONE;
1090 OCEntityHandlerResult icd_ioty_ocprocess_tizen_info_handler(OCEntityHandlerFlag flag,
1091 OCEntityHandlerRequest *request, void *user_data)
1094 struct icd_tizen_info_context *tizen_info_ctx;
1096 if ((0 == (OC_REQUEST_FLAG & flag)) || (OC_REST_GET != request->method)) {
1097 ERR("Prohibited Action");
1098 return OC_EH_FORBIDDEN;
1101 tizen_info_ctx = calloc(1, sizeof(struct icd_tizen_info_context));
1102 if (NULL == tizen_info_ctx) {
1103 ERR("calloc() Fail(%d)", errno);
1107 tizen_info_ctx->request_h = request->requestHandle;
1108 tizen_info_ctx->resource_h = request->resource;
1110 ret = _ocprocess_worker_start(_worker_tizen_info_handler, tizen_info_ctx);
1111 if (IOTCON_ERROR_NONE != ret) {
1112 ERR("_ocprocess_worker_start() Fail(%d)", ret);
1113 free(tizen_info_ctx);
1121 OCStackApplicationResult icd_ioty_ocprocess_get_tizen_info_cb(void *ctx,
1122 OCDoHandle handle, OCClientResponse *resp)
1126 char *tizen_device_id;
1127 GVariant *tizen_info;
1128 OCRepPayload *payload;
1129 OCRepPayloadValue *val;
1131 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
1133 if (NULL == resp->payload) {
1134 ERR("payload is empty");
1135 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1136 return OC_STACK_DELETE_TRANSACTION;
1139 payload = (OCRepPayload*)resp->payload;
1140 val = payload->values;
1142 ERR("Invalid payload");
1143 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1144 return OC_STACK_DELETE_TRANSACTION;
1146 device_name = val->str;
1150 ERR("Invalid Payload");
1151 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1152 return OC_STACK_DELETE_TRANSACTION;
1155 tizen_device_id = val->str;
1157 if (OC_STACK_OK == resp->result)
1158 res = IOTCON_RESPONSE_RESULT_OK;
1160 res = IOTCON_RESPONSE_RESULT_ERROR;
1162 tizen_info = g_variant_new("(ssi)", device_name, tizen_device_id, res);
1164 icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
1166 return OC_STACK_DELETE_TRANSACTION;