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.
19 #include <unistd.h> /* for usleep() */
24 #include <ocpayload.h>
29 #include "icd-payload.h"
32 #include "icd-ioty-type.h"
33 #include "icd-ioty-ocprocess.h"
35 static int icd_ioty_alive;
37 typedef int (*_ocprocess_cb)(void *user_data);
39 struct icd_ioty_worker
46 struct icd_req_context {
47 int64_t signal_number;
52 OCRequestHandle request_h;
53 OCResourceHandle resource_h;
55 GVariantBuilder *options;
56 GVariantBuilder *query;
61 struct icd_find_context {
62 int64_t signal_number;
69 struct icd_crud_context {
73 GVariantBuilder *options;
74 GDBusMethodInvocation *invocation;
78 struct icd_info_context {
79 int64_t signal_number;
86 struct icd_tizen_info_context {
87 OCRequestHandle request_h;
88 OCResourceHandle resource_h;
89 GDBusMethodInvocation *invocation;
93 struct icd_observe_context {
94 int64_t signal_number;
99 GVariantBuilder *options;
103 struct icd_presence_context {
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 int64_t signal_number, GVariant *value)
180 char signal_name[IC_DBUS_SIGNAL_LENGTH] = {0};
182 ret = snprintf(signal_name, sizeof(signal_name), "%s_%llx", 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 int64_t 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 int64_t 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 GVariant *value, *value2;
839 int ret = IOTCON_ERROR_NONE;
840 struct icd_presence_context *ctx = context;
842 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
844 ret = icd_ioty_get_host_address(ctx->dev_addr, &host_address, &conn_type);
845 if (IOTCON_ERROR_NONE != ret) {
846 ERR("icd_ioty_get_host_address() Fail(%d)", ret);
847 free(ctx->resource_type);
853 value = g_variant_new("(iusiis)", ctx->result, ctx->nonce, host_address, conn_type,
854 ctx->trigger, ic_utils_dbus_encode_str(ctx->resource_type));
855 value2 = g_variant_ref(value);
859 ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
860 ICD_POINTER_TO_INT64(ctx->handle), value);
861 if (IOTCON_ERROR_NONE != ret)
862 ERR("_ocprocess_response_signal() Fail(%d)", ret);
864 handle = icd_ioty_presence_table_get_handle(ICD_MULTICAST_ADDRESS);
865 if (handle && (handle != ctx->handle)) {
866 ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
867 ICD_POINTER_TO_INT64(handle), value2);
868 if (IOTCON_ERROR_NONE != ret)
869 ERR("_ocprocess_response_signal() Fail(%d)", ret);
871 g_variant_unref(value2);
874 /* ctx was allocated from icd_ioty_ocprocess_presence_cb() */
875 free(ctx->resource_type);
883 static void _presence_cb_response_error(OCDoHandle handle, int ret_val)
888 GVariant *value, *value2;
890 value = g_variant_new("(iusiis)", ret_val, 0, IC_STR_NULL, IOTCON_CONNECTIVITY_ALL,
891 IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED, IC_STR_NULL);
892 value2 = g_variant_ref(value);
894 ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
895 ICD_POINTER_TO_INT64(handle), value);
896 if (IOTCON_ERROR_NONE != ret)
897 ERR("_ocprocess_response_signal() Fail(%d)", ret);
899 handle2 = icd_ioty_presence_table_get_handle(ICD_MULTICAST_ADDRESS);
900 if (handle2 && (handle2 != handle)) {
901 ret = _ocprocess_response_signal(NULL, IC_DBUS_SIGNAL_PRESENCE,
902 ICD_POINTER_TO_INT64(handle), value2);
903 if (IOTCON_ERROR_NONE != ret)
904 ERR("_ocprocess_response_signal() Fail(%d)", ret);
906 g_variant_unref(value2);
911 static int _presence_trigger_to_ioty_trigger(OCPresenceTrigger src,
912 iotcon_presence_trigger_e *dest)
914 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
917 case OC_PRESENCE_TRIGGER_CREATE:
918 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_CREATED;
920 case OC_PRESENCE_TRIGGER_CHANGE:
921 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_UPDATED;
923 case OC_PRESENCE_TRIGGER_DELETE:
924 *dest = IOTCON_PRESENCE_TRIGGER_RESOURCE_DESTROYED;
927 ERR("Invalid trigger(%d)", src);
928 return IOTCON_ERROR_INVALID_PARAMETER;
931 return IOTCON_ERROR_NONE;
935 OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle handle,
936 OCClientResponse *resp)
941 OCPresencePayload *payload;
942 struct icd_presence_context *presence_ctx;
944 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
946 presence_ctx = calloc(1, sizeof(struct icd_presence_context));
947 if (NULL == presence_ctx) {
948 ERR("calloc() Fail(%d)", errno);
949 _presence_cb_response_error(handle, IOTCON_ERROR_OUT_OF_MEMORY);
950 return OC_STACK_KEEP_TRANSACTION;
953 dev_addr = calloc(1, sizeof(OCDevAddr));
954 if (NULL == dev_addr) {
955 ERR("calloc() Fail(%d)", errno);
956 _presence_cb_response_error(handle, IOTCON_ERROR_OUT_OF_MEMORY);
958 return OC_STACK_KEEP_TRANSACTION;
960 memcpy(dev_addr, &resp->devAddr, sizeof(OCDevAddr));
962 payload = (OCPresencePayload*)resp->payload;
964 switch (resp->result) {
966 presence_ctx->result = IOTCON_PRESENCE_OK;
967 ret = _presence_trigger_to_ioty_trigger(payload->trigger, &presence_ctx->trigger);
968 if (IOTCON_ERROR_NONE != ret) {
969 ERR("_presence_trigger_to_ioty_trigger() Fail(%d)", ret);
970 _presence_cb_response_error(handle, ret);
972 return OC_STACK_KEEP_TRANSACTION;
975 case OC_STACK_PRESENCE_STOPPED:
976 presence_ctx->result = IOTCON_PRESENCE_STOPPED;
978 case OC_STACK_PRESENCE_TIMEOUT:
979 presence_ctx->result = IOTCON_PRESENCE_TIMEOUT;
983 DBG("Presence error(%d)", resp->result);
984 presence_ctx->result = IOTCON_ERROR_IOTIVITY;
987 presence_ctx->handle = handle;
988 presence_ctx->nonce = resp->sequenceNumber;
989 presence_ctx->dev_addr = dev_addr;
991 if (payload->resourceType)
992 presence_ctx->resource_type = strdup(payload->resourceType);
994 ret = _ocprocess_worker_start(_worker_presence_cb, presence_ctx);
995 if (IOTCON_ERROR_NONE != ret) {
996 ERR("_ocprocess_worker_start() Fail(%d)", ret);
997 _presence_cb_response_error(handle, ret);
998 free(presence_ctx->resource_type);
999 free(presence_ctx->dev_addr);
1001 return OC_STACK_KEEP_TRANSACTION;
1004 /* DO NOT FREE presence_ctx. It MUST be freed in the _worker_presence_cb func */
1006 return OC_STACK_KEEP_TRANSACTION;
1010 OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle,
1011 OCClientResponse *resp)
1015 struct icd_info_context *info_ctx;
1016 icd_sig_ctx_s *sig_context = ctx;
1018 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
1019 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
1020 RETV_IF(NULL == resp->payload, OC_STACK_KEEP_TRANSACTION);
1022 if (PAYLOAD_TYPE_DEVICE == resp->payload->type)
1023 info_type = ICD_DEVICE_INFO;
1024 else if (PAYLOAD_TYPE_PLATFORM == resp->payload->type)
1025 info_type = ICD_PLATFORM_INFO;
1027 return OC_STACK_KEEP_TRANSACTION;
1029 info_ctx = calloc(1, sizeof(struct icd_info_context));
1030 if (NULL == info_ctx) {
1031 ERR("calloc() Fail(%d)", errno);
1032 return OC_STACK_KEEP_TRANSACTION;
1035 info_ctx->info_type = info_type;
1036 info_ctx->payload = icd_payload_to_gvariant(resp->payload);
1037 info_ctx->signal_number = sig_context->signal_number;
1038 info_ctx->bus_name = ic_utils_strdup(sig_context->bus_name);
1040 ret = _ocprocess_worker_start(_worker_info_cb, info_ctx);
1041 if (IOTCON_ERROR_NONE != ret) {
1042 ERR("_ocprocess_worker_start() Fail(%d)", ret);
1043 free(info_ctx->bus_name);
1044 if (info_ctx->payload)
1045 g_variant_unref(info_ctx->payload);
1047 return OC_STACK_KEEP_TRANSACTION;
1050 /* DO NOT FREE sig_context. It MUST be freed in the ocstack */
1051 /* DO NOT FREE info_ctx. It MUST be freed in the _worker_info_cb func */
1053 return OC_STACK_KEEP_TRANSACTION;
1057 static int _worker_tizen_info_handler(void *context)
1061 char *tizen_device_id;
1062 OCStackResult result;
1063 OCRepPayload *payload;
1064 OCEntityHandlerResponse response = {0};
1065 struct icd_tizen_info_context *ctx = context;
1067 response.requestHandle = ctx->request_h;
1068 response.resourceHandle = ctx->resource_h;
1069 response.ehResult = OC_EH_OK;
1071 /* Get Tizen Info */
1072 ret = icd_ioty_tizen_info_get_property(&device_name, &tizen_device_id);
1073 if (IOTCON_ERROR_NONE != ret) {
1074 ERR("icd_ioty_tizen_info_get_property() Fail(%d)", ret);
1075 response.ehResult = OC_EH_ERROR;
1079 payload = OCRepPayloadCreate();
1080 OCRepPayloadSetUri(payload, ICD_IOTY_TIZEN_INFO_URI);
1081 OCRepPayloadAddResourceType(payload, ICD_IOTY_TIZEN_INFO_TYPE);
1082 OCRepPayloadAddInterface(payload, IC_INTERFACE_DEFAULT);
1084 OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_DEVICE_NAME,
1085 ic_utils_dbus_encode_str(device_name));
1086 OCRepPayloadSetPropString(payload, ICD_IOTY_TIZEN_INFO_TIZEN_DEVICE_ID,
1087 ic_utils_dbus_encode_str(tizen_device_id));
1088 response.payload = (OCPayload*)payload;
1090 icd_ioty_csdk_lock();
1091 result = OCDoResponse(&response);
1092 icd_ioty_csdk_unlock();
1094 if (OC_STACK_OK != result) {
1095 ERR("OCDoResponse() Fail(%d)", result);
1097 return IOTCON_ERROR_IOTIVITY;
1101 return IOTCON_ERROR_NONE;
1105 OCEntityHandlerResult icd_ioty_ocprocess_tizen_info_handler(OCEntityHandlerFlag flag,
1106 OCEntityHandlerRequest *request, void *user_data)
1109 struct icd_tizen_info_context *tizen_info_ctx;
1111 if ((0 == (OC_REQUEST_FLAG & flag)) || (OC_REST_GET != request->method)) {
1112 ERR("Prohibited Action");
1113 return OC_EH_FORBIDDEN;
1116 tizen_info_ctx = calloc(1, sizeof(struct icd_tizen_info_context));
1117 if (NULL == tizen_info_ctx) {
1118 ERR("calloc() Fail(%d)", errno);
1122 tizen_info_ctx->request_h = request->requestHandle;
1123 tizen_info_ctx->resource_h = request->resource;
1125 ret = _ocprocess_worker_start(_worker_tizen_info_handler, tizen_info_ctx);
1126 if (IOTCON_ERROR_NONE != ret) {
1127 ERR("_ocprocess_worker_start() Fail(%d)", ret);
1128 free(tizen_info_ctx);
1136 OCStackApplicationResult icd_ioty_ocprocess_get_tizen_info_cb(void *ctx,
1137 OCDoHandle handle, OCClientResponse *resp)
1141 char *tizen_device_id;
1142 GVariant *tizen_info;
1143 OCRepPayload *payload;
1144 OCRepPayloadValue *val;
1146 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
1148 if (NULL == resp->payload) {
1149 ERR("payload is empty");
1150 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1151 return OC_STACK_DELETE_TRANSACTION;
1154 payload = (OCRepPayload*)resp->payload;
1155 val = payload->values;
1157 ERR("Invalid payload");
1158 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1159 return OC_STACK_DELETE_TRANSACTION;
1161 device_name = val->str;
1165 ERR("Invalid Payload");
1166 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1167 return OC_STACK_DELETE_TRANSACTION;
1170 tizen_device_id = val->str;
1172 if (OC_STACK_OK == resp->result)
1173 res = IOTCON_RESPONSE_RESULT_OK;
1175 res = IOTCON_RESPONSE_RESULT_ERROR;
1177 tizen_info = g_variant_new("(ssi)", device_name, tizen_device_id, res);
1179 icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
1181 return OC_STACK_DELETE_TRANSACTION;