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 {
51 OCRequestHandle request_h;
52 OCResourceHandle resource_h;
54 GVariantBuilder *options;
55 GVariantBuilder *query;
59 struct icd_find_context {
67 struct icd_crud_context {
71 GVariantBuilder *options;
72 GDBusMethodInvocation *invocation;
76 struct icd_info_context {
84 struct icd_tizen_info_context {
85 OCRequestHandle request_h;
86 OCResourceHandle resource_h;
87 GDBusMethodInvocation *invocation;
91 struct icd_observe_context {
97 GVariantBuilder *options;
101 struct icd_presence_context {
110 void icd_ioty_ocprocess_stop()
116 static void* _ocprocess_worker_thread(void *data)
119 struct icd_ioty_worker *worker = data;
122 ERR("worker is NULL");
126 ret = worker->cb(worker->ctx);
127 if (IOTCON_ERROR_NONE != ret)
128 ERR("cb() Fail(%d)", ret);
130 /* worker was allocated from _ocprocess_worker_start() */
133 /* GCC warning happen if use g_thread_exit() */
138 static int _ocprocess_worker_start(_ocprocess_cb cb, void *ctx)
142 struct icd_ioty_worker *worker;
144 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
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;
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);
161 return IOTCON_ERROR_SYSTEM;
164 /* DO NOT join thread. It was already detached by calling g_thread_unref() */
165 g_thread_unref(thread);
167 /* DO NOT FREE worker. It MUST be freed in the _ocprocess_worker_thread() */
169 return IOTCON_ERROR_NONE;
173 static int _ocprocess_response_signal(const char *dest, const char *signal,
174 unsigned int signum, GVariant *value)
177 char sig_name[IC_DBUS_SIGNAL_LENGTH] = {0};
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;
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);
191 return IOTCON_ERROR_NONE;
195 static inline GVariantBuilder* _ocprocess_parse_header_options(
196 OCHeaderOption *oic_option, int option_size)
199 GVariantBuilder *options;
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);
211 static int _worker_req_handler(void *context)
215 struct icd_req_context *ctx = context;
216 GVariantBuilder payload_builder;
218 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
220 g_variant_builder_init(&payload_builder, G_VARIANT_TYPE("av"));
222 g_variant_builder_add(&payload_builder, "v", ctx->payload);
224 value = g_variant_new("(ia(qs)a(ss)iiavxx)",
231 ICD_POINTER_TO_INT64(ctx->request_h),
232 ICD_POINTER_TO_INT64(ctx->resource_h));
234 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_REQUEST_HANDLER,
236 if (IOTCON_ERROR_NONE != ret)
237 ERR("_ocprocess_response_signal() Fail(%d)", ret);
240 g_variant_builder_unref(ctx->options);
241 g_variant_builder_unref(ctx->query);
248 OCEntityHandlerResult icd_ioty_ocprocess_req_handler(OCEntityHandlerFlag flag,
249 OCEntityHandlerRequest *request, void *user_data)
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;
259 RETV_IF(NULL == request, OC_EH_ERROR);
261 req_ctx = calloc(1, sizeof(struct icd_req_context));
262 if (NULL == req_ctx) {
263 ERR("calloc() Fail(%d)", errno);
268 req_ctx->request_h = request->requestHandle;
269 req_ctx->resource_h = request->resource;
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);
278 /* signal number & bus_name */
279 req_ctx->signum = signal_number;
280 req_ctx->bus_name = bus_name;
283 if (OC_REQUEST_FLAG & flag) {
284 switch (request->method) {
286 req_ctx->types = IOTCON_REQUEST_GET;
287 req_ctx->payload = NULL;
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;
297 req_ctx->types = IOTCON_REQUEST_PUT;
298 req_ctx->payload = icd_payload_to_gvariant(request->payload);
301 req_ctx->types = IOTCON_REQUEST_POST;
302 req_ctx->payload = icd_payload_to_gvariant(request->payload);
305 req_ctx->types = IOTCON_REQUEST_DELETE;
306 req_ctx->payload = NULL;
309 free(req_ctx->bus_name);
316 req_ctx->options = _ocprocess_parse_header_options(
317 request->rcvdVendorSpecificHeaderOptions,
318 request->numRcvdVendorSpecificHeaderOptions);
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))) {
326 query_value = strtok_r(token, "=", &save_ptr2);
327 if (NULL == query_value)
330 g_variant_builder_add(req_ctx->query, "(ss)", query_key, query_value);
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);
347 /* DO NOT FREE req_ctx. It MUST be freed in the _worker_req_handler func */
353 gpointer icd_ioty_ocprocess_thread(gpointer data)
356 OCStackResult result;
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);
368 /* TODO : SHOULD revise time or usleep */
376 static int _worker_find_cb(void *context)
380 struct icd_find_context *ctx = context;
382 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
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,
391 if (IOTCON_ERROR_NONE != ret) {
392 ERR("_ocprocess_response_signal() Fail(%d)", ret);
393 g_variant_unref(value);
398 /* ctx was allocated from icd_ioty_ocprocess_find_cb() */
406 OCStackApplicationResult icd_ioty_ocprocess_find_cb(void *ctx, OCDoHandle handle,
407 OCClientResponse *resp)
410 struct icd_find_context *find_ctx;
411 icd_sig_ctx_s *sig_context = ctx;
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);
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;
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);
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);
439 return OC_STACK_KEEP_TRANSACTION;
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 */
445 return OC_STACK_KEEP_TRANSACTION;
449 static int _worker_crud_cb(void *context)
452 struct icd_crud_context *ctx = context;
454 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
456 if (ICD_CRUD_DELETE == ctx->crud_type)
457 value = g_variant_new("(a(qs)i)", ctx->options, ctx->res);
459 value = g_variant_new("(a(qs)vi)", ctx->options, ctx->payload, ctx->res);
460 icd_ioty_complete(ctx->crud_type, ctx->invocation, value);
462 /* ctx was allocated from icd_ioty_ocprocess_xxx_cb() */
463 g_variant_builder_unref(ctx->options);
466 return IOTCON_ERROR_NONE;
470 static int _worker_info_cb(void *context)
473 const char *sig_name = NULL;
474 struct icd_info_context *ctx = context;
476 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
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;
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);
487 /* ctx was allocated from icd_ioty_ocprocess_info_cb() */
495 static int _ocprocess_worker(_ocprocess_cb cb, int type, OCPayload *payload, int res,
496 GVariantBuilder *options, void *ctx)
499 struct icd_crud_context *crud_ctx;
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;
507 crud_ctx->crud_type = type;
508 crud_ctx->payload = icd_payload_to_gvariant(payload);
510 crud_ctx->options = options;
511 crud_ctx->invocation = ctx;
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);
522 /* DO NOT FREE crud_ctx. It MUST be freed in the _worker_crud_cb func */
528 static int _ocprocess_parse_oic_result(OCStackResult result)
534 res = IOTCON_RESPONSE_RESULT_OK;
536 case OC_STACK_RESOURCE_CREATED:
537 res = IOTCON_RESPONSE_RESULT_RESOURCE_CREATED;
539 case OC_STACK_RESOURCE_DELETED:
540 res = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
542 case OC_STACK_UNAUTHORIZED_REQ:
543 res = IOTCON_RESPONSE_RESULT_FORBIDDEN;
546 WARN("response error(%d)", result);
547 res = IOTCON_RESPONSE_RESULT_ERROR;
555 OCStackApplicationResult icd_ioty_ocprocess_get_cb(void *ctx, OCDoHandle handle,
556 OCClientResponse *resp)
560 GVariantBuilder *options;
562 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
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;
570 res = _ocprocess_parse_oic_result(resp->result);
572 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
573 resp->numRcvdVendorSpecificHeaderOptions);
575 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_GET, resp->payload, res,
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;
583 return OC_STACK_DELETE_TRANSACTION;
587 OCStackApplicationResult icd_ioty_ocprocess_put_cb(void *ctx, OCDoHandle handle,
588 OCClientResponse *resp)
592 GVariantBuilder *options;
594 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
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;
602 res = _ocprocess_parse_oic_result(resp->result);
604 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
605 resp->numRcvdVendorSpecificHeaderOptions);
607 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_PUT, resp->payload, res,
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;
615 return OC_STACK_DELETE_TRANSACTION;
619 OCStackApplicationResult icd_ioty_ocprocess_post_cb(void *ctx, OCDoHandle handle,
620 OCClientResponse *resp)
624 GVariantBuilder *options;
626 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
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;
634 res = _ocprocess_parse_oic_result(resp->result);
636 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
637 resp->numRcvdVendorSpecificHeaderOptions);
639 ret = _ocprocess_worker(_worker_crud_cb, ICD_CRUD_POST, resp->payload, res,
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;
647 return OC_STACK_DELETE_TRANSACTION;
651 OCStackApplicationResult icd_ioty_ocprocess_delete_cb(void *ctx, OCDoHandle handle,
652 OCClientResponse *resp)
656 GVariantBuilder *options;
658 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
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;
666 res = _ocprocess_parse_oic_result(resp->result);
668 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
669 resp->numRcvdVendorSpecificHeaderOptions);
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;
678 return OC_STACK_DELETE_TRANSACTION;
682 static int _worker_observe_cb(void *context)
686 struct icd_observe_context *ctx = context;
688 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
690 value = g_variant_new("(a(qs)vii)", ctx->options, ctx->payload, ctx->res,
693 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_OBSERVE, ctx->signum,
695 if (IOTCON_ERROR_NONE != ret)
696 ERR("_ocprocess_response_signal() Fail(%d)", ret);
698 /* ctx was allocated from icd_ioty_ocprocess_observe_cb() */
700 g_variant_builder_unref(ctx->options);
707 static void _observe_cb_response_error(const char *dest, unsigned int signum, int ret_val)
712 GVariantBuilder options;
714 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
715 payload = icd_payload_representation_empty_gvariant();
717 value = g_variant_new("(a(qs)vii)", &options, payload, ret_val, 0);
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);
725 OCStackApplicationResult icd_ioty_ocprocess_observe_cb(void *ctx, OCDoHandle handle,
726 OCClientResponse *resp)
729 GVariantBuilder *options;
730 struct icd_observe_context *observe_ctx;
731 icd_sig_ctx_s *sig_context = ctx;
733 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
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;
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;
750 res = _ocprocess_parse_oic_result(resp->result);
752 options = _ocprocess_parse_header_options(resp->rcvdVendorSpecificHeaderOptions,
753 resp->numRcvdVendorSpecificHeaderOptions);
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;
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);
770 return OC_STACK_KEEP_TRANSACTION;
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 */
776 return OC_STACK_KEEP_TRANSACTION;
780 static int _worker_presence_cb(void *context)
785 char addr[PATH_MAX] = {0};
786 struct icd_presence_context *ctx = context;
788 RETV_IF(NULL == ctx, IOTCON_ERROR_INVALID_PARAMETER);
790 snprintf(addr, sizeof(addr), "%s:%d", ctx->dev_addr->addr, ctx->dev_addr->port);
792 value = g_variant_new("(ius)", ctx->result, ctx->nonce, addr);
794 ret = _ocprocess_response_signal(ctx->bus_name, IC_DBUS_SIGNAL_PRESENCE, ctx->signum,
796 if (IOTCON_ERROR_NONE != ret)
797 ERR("_ocprocess_response_signal() Fail(%d)", ret);
799 /* ctx was allocated from icd_ioty_ocprocess_presence_cb() */
808 static void _presence_cb_response_error(const char *dest, unsigned int signum,
815 value = g_variant_new("(ius)", ret_val, 0, IC_STR_NULL);
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);
823 OCStackApplicationResult icd_ioty_ocprocess_presence_cb(void *ctx, OCDoHandle handle,
824 OCClientResponse *resp)
829 icd_sig_ctx_s *sig_context = ctx;
830 struct icd_presence_context *presence_ctx;
832 RETV_IF(NULL == ctx, OC_STACK_KEEP_TRANSACTION);
833 RETV_IF(NULL == resp, OC_STACK_KEEP_TRANSACTION);
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;
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);
849 return OC_STACK_KEEP_TRANSACTION;
851 memcpy(dev_addr, &resp->devAddr, sizeof(OCDevAddr));
853 switch (resp->result) {
855 presence_ctx->result = IOTCON_PRESENCE_OK;
857 case OC_STACK_PRESENCE_STOPPED:
858 presence_ctx->result = IOTCON_PRESENCE_STOPPED;
860 case OC_STACK_PRESENCE_TIMEOUT:
861 presence_ctx->result = IOTCON_PRESENCE_TIMEOUT;
865 DBG("Presence error(%d)", resp->result);
866 presence_ctx->result = IOTCON_ERROR_IOTIVITY;
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;
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);
881 return OC_STACK_KEEP_TRANSACTION;
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 */
887 return OC_STACK_KEEP_TRANSACTION;
891 OCStackApplicationResult icd_ioty_ocprocess_info_cb(void *ctx, OCDoHandle handle,
892 OCClientResponse *resp)
896 struct icd_info_context *info_ctx;
897 icd_sig_ctx_s *sig_context = ctx;
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);
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;
908 return OC_STACK_KEEP_TRANSACTION;
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;
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);
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);
928 return OC_STACK_KEEP_TRANSACTION;
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 */
934 return OC_STACK_KEEP_TRANSACTION;
938 static int _worker_tizen_info_handler(void *context)
942 char *tizen_device_id;
943 OCStackResult result;
944 OCRepPayload *payload;
945 OCEntityHandlerResponse response = {0};
946 struct icd_tizen_info_context *ctx = context;
948 response.requestHandle = ctx->request_h;
949 response.resourceHandle = ctx->resource_h;
950 response.ehResult = OC_EH_OK;
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;
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);
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;
971 icd_ioty_csdk_lock();
972 result = OCDoResponse(&response);
973 icd_ioty_csdk_unlock();
975 if (OC_STACK_OK != result) {
976 ERR("OCDoResponse() Fail(%d)", result);
978 return IOTCON_ERROR_IOTIVITY;
982 return IOTCON_ERROR_NONE;
986 OCEntityHandlerResult icd_ioty_ocprocess_tizen_info_handler(OCEntityHandlerFlag flag,
987 OCEntityHandlerRequest *request, void *user_data)
990 struct icd_tizen_info_context *tizen_info_ctx;
992 if ((0 == (OC_REQUEST_FLAG & flag)) || (OC_REST_GET != request->method)) {
993 ERR("Prohibited Action");
994 return OC_EH_FORBIDDEN;
997 tizen_info_ctx = calloc(1, sizeof(struct icd_tizen_info_context));
998 if (NULL == tizen_info_ctx) {
999 ERR("calloc() Fail(%d)", errno);
1003 tizen_info_ctx->request_h = request->requestHandle;
1004 tizen_info_ctx->resource_h = request->resource;
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);
1017 OCStackApplicationResult icd_ioty_ocprocess_get_tizen_info_cb(void *ctx,
1018 OCDoHandle handle, OCClientResponse *resp)
1022 char *tizen_device_id;
1023 GVariant *tizen_info;
1024 OCRepPayload *payload;
1025 OCRepPayloadValue *val;
1027 RETV_IF(NULL == ctx, OC_STACK_DELETE_TRANSACTION);
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;
1035 payload = (OCRepPayload*)resp->payload;
1036 val = payload->values;
1038 ERR("Invalid payload");
1039 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1040 return OC_STACK_DELETE_TRANSACTION;
1042 device_name = val->str;
1046 ERR("Invalid Payload");
1047 icd_ioty_complete_error(ICD_TIZEN_INFO, ctx, IOTCON_ERROR_IOTIVITY);
1048 return OC_STACK_DELETE_TRANSACTION;
1051 tizen_device_id = val->str;
1053 if (OC_STACK_OK == resp->result)
1054 res = IOTCON_RESPONSE_RESULT_OK;
1056 res = IOTCON_RESPONSE_RESULT_ERROR;
1058 tizen_info = g_variant_new("(ssi)", device_name, tizen_device_id, res);
1060 icd_ioty_complete(ICD_TIZEN_INFO, ctx, tizen_info);
1062 return OC_STACK_DELETE_TRANSACTION;