981a9feed3ef95ced6bbe72c508d2d2bb2289ba1
[platform/core/iot/iotcon.git] / daemon / icd-ioty.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 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h> /* for uint8_t etc */
19 #include <errno.h>
20 #include <glib.h>
21 #include <tizen_type.h>
22 #include <system_info.h>
23 #include <system_settings.h>
24
25 #include <octypes.h>
26 #include <ocstack.h>
27
28 #include "iotcon.h"
29 #include "ic-dbus.h"
30 #include "ic-utils.h"
31 #include "icd.h"
32 #include "icd-payload.h"
33 #include "icd-dbus.h"
34 #include "icd-ioty.h"
35 #include "icd-ioty-type.h"
36 #include "icd-ioty-ocprocess.h"
37
38 #define ICD_MULTICAST_ADDRESS "224.0.1.187:5683"
39 #define ICD_UUID_LENGTH 37
40
41 static const char *ICD_SYSTEM_INFO_TIZEN_ID = "http://tizen.org/system/tizenid";
42 static const char *ICD_SYSTEM_INFO_PLATFORM_NAME = "http://tizen.org/system/platform.name";
43 static const char *ICD_SYSTEM_INFO_PLATFORM_VERSION = "http://tizen.org/feature/platform.version";
44 static const char *ICD_SYSTEM_INFO_MANUF_NAME = "http://tizen.org/system/manufacturer";
45 static const char *ICD_SYSTEM_INFO_MODEL_NAME = "http://tizen.org/system/model_name";
46 static const char *ICD_SYSTEM_INFO_BUILD_STRING = "http://tizen.org/system/build.string";
47
48 typedef struct {
49         char *device_name;
50         char *tizen_device_id;
51 } icd_tizen_info_s;
52
53 static icd_tizen_info_s icd_tizen_info = {0};
54
55 static GMutex icd_csdk_mutex;
56
57 void icd_ioty_csdk_lock()
58 {
59         g_mutex_lock(&icd_csdk_mutex);
60 }
61
62
63 void icd_ioty_csdk_unlock()
64 {
65         g_mutex_unlock(&icd_csdk_mutex);
66 }
67
68
69 GThread* icd_ioty_init(const char *addr, unsigned short port)
70 {
71         FN_CALL;
72         GError *error;
73         GThread *thread;
74
75         OCStackResult result = OCInit(addr, port, OC_CLIENT_SERVER);
76         if (OC_STACK_OK != result) {
77                 ERR("OCInit() Fail(%d)", result);
78                 return NULL;
79         }
80
81         DBG("OCInit() Success");
82
83         thread = g_thread_try_new("packet_receive_thread", icd_ioty_ocprocess_thread,
84                         NULL, &error);
85         if (NULL == thread) {
86                 ERR("g_thread_try_new() Fail(%s)", error->message);
87                 g_error_free(error);
88                 return NULL;
89         }
90
91         return thread;
92 }
93
94
95 void icd_ioty_deinit(GThread *thread)
96 {
97         OCStackResult result;
98
99         icd_ioty_ocprocess_stop();
100         g_thread_join(thread);
101
102         result = OCStop();
103         if (OC_STACK_OK != result)
104                 ERR("OCStop() Fail(%d)", result);
105 }
106
107 static int _ioty_properties_to_oic_properties(int properties)
108 {
109         int prop = OC_RES_PROP_NONE;
110
111         if (IOTCON_DISCOVERABLE & properties)
112                 prop |= OC_DISCOVERABLE;
113
114         if (IOTCON_OBSERVABLE & properties)
115                 prop |= OC_OBSERVABLE;
116
117         if (IOTCON_ACTIVE & properties)
118                 prop |= OC_ACTIVE;
119
120         if (IOTCON_SLOW & properties)
121                 prop |= OC_SLOW;
122
123         if (IOTCON_SECURE & properties)
124                 prop |= OC_SECURE;
125
126         if (IOTCON_EXPLICIT_DISCOVERABLE & properties)
127                 prop |= OC_EXPLICIT_DISCOVERABLE;
128
129         /* TODO: Secure option is not supported yet. */
130         properties = (properties & OC_SECURE)? (properties ^ OC_SECURE):properties;
131
132         return prop;
133 }
134
135 OCResourceHandle icd_ioty_register_resource(const char *uri_path,
136                 const char* const* res_types, int ifaces, int properties)
137 {
138         FN_CALL;
139         int i;
140         OCStackResult ret;
141         OCResourceHandle handle;
142         const char *res_iface = NULL;
143
144         if (IOTCON_INTERFACE_DEFAULT & ifaces) {
145                 res_iface = IC_INTERFACE_DEFAULT;
146                 ifaces ^= IOTCON_INTERFACE_DEFAULT;
147         } else if (IOTCON_INTERFACE_LINK & ifaces) {
148                 res_iface = IC_INTERFACE_LINK;
149                 ifaces ^= IOTCON_INTERFACE_LINK;
150         } else if (IOTCON_INTERFACE_BATCH & ifaces) {
151                 res_iface = IC_INTERFACE_BATCH;
152                 ifaces ^= IOTCON_INTERFACE_BATCH;
153         } else if (IOTCON_INTERFACE_GROUP & ifaces) {
154                 res_iface = IC_INTERFACE_GROUP;
155                 ifaces ^= IOTCON_INTERFACE_GROUP;
156         } else {
157                 ERR("Invalid interface type(%d)", ifaces);
158                 return NULL;
159         }
160
161         properties = _ioty_properties_to_oic_properties(properties);
162
163         icd_ioty_csdk_lock();
164         ret = OCCreateResource(&handle, res_types[0], res_iface, uri_path,
165                         icd_ioty_ocprocess_req_handler, NULL, properties);
166         icd_ioty_csdk_unlock();
167         if (OC_STACK_OK != ret) {
168                 ERR("OCCreateResource() Fail(%d)", ret);
169                 return NULL;
170         }
171
172         for (i = 1; res_types[i]; i++)
173                 icd_ioty_bind_type(handle, res_types[i]);
174
175         if (IOTCON_INTERFACE_DEFAULT & ifaces)
176                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_DEFAULT);
177         if (IOTCON_INTERFACE_LINK & ifaces)
178                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_LINK);
179         if (IOTCON_INTERFACE_BATCH & ifaces)
180                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_BATCH);
181         if (IOTCON_INTERFACE_GROUP & ifaces)
182                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_GROUP);
183
184         return handle;
185 }
186
187
188 int icd_ioty_unregister_resource(OCResourceHandle handle)
189 {
190         OCStackResult ret;
191
192         icd_ioty_csdk_lock();
193         ret = OCDeleteResource(handle);
194         icd_ioty_csdk_unlock();
195
196         if (OC_STACK_OK != ret) {
197                 ERR("OCDeleteResource() Fail(%d)", ret);
198                 return icd_ioty_convert_error(ret);
199         }
200
201         return IOTCON_ERROR_NONE;
202 }
203
204
205 int icd_ioty_bind_interface(OCResourceHandle handle, iotcon_interface_e iface)
206 {
207         int ret;
208         OCStackResult result;
209         char *resource_interface;
210
211         ret = ic_utils_convert_interface_flag(iface, &resource_interface);
212         if (IOTCON_ERROR_NONE != ret) {
213                 ERR("ic_utils_convert_interface_flag(%d) Fail(%d)", iface, ret);
214                 return ret;
215         }
216
217         icd_ioty_csdk_lock();
218         result = OCBindResourceInterfaceToResource(handle, resource_interface);
219         icd_ioty_csdk_unlock();
220
221         if (OC_STACK_OK != result) {
222                 ERR("OCBindResourceInterfaceToResource() Fail(%d)", result);
223                 return icd_ioty_convert_error(result);
224         }
225
226         return IOTCON_ERROR_NONE;
227 }
228
229
230 int icd_ioty_bind_type(OCResourceHandle handle, const char *resource_type)
231 {
232         OCStackResult ret;
233
234         icd_ioty_csdk_lock();
235         ret = OCBindResourceTypeToResource(handle, resource_type);
236         icd_ioty_csdk_unlock();
237
238         if (OC_STACK_OK != ret) {
239                 ERR("OCBindResourceTypeToResource() Fail(%d)", ret);
240                 return icd_ioty_convert_error(ret);
241         }
242
243         return IOTCON_ERROR_NONE;
244 }
245
246
247 int icd_ioty_bind_resource(OCResourceHandle parent, OCResourceHandle child)
248 {
249         OCStackResult ret;
250
251         icd_ioty_csdk_lock();
252         ret = OCBindResource(parent, child);
253         icd_ioty_csdk_unlock();
254
255         if (OC_STACK_OK != ret) {
256                 ERR("OCBindResource() Fail(%d)", ret);
257                 return icd_ioty_convert_error(ret);
258         }
259
260         return IOTCON_ERROR_NONE;
261 }
262
263
264 int icd_ioty_unbind_resource(OCResourceHandle parent, OCResourceHandle child)
265 {
266         OCStackResult ret;
267
268         icd_ioty_csdk_lock();
269         ret = OCUnBindResource(parent, child);
270         icd_ioty_csdk_unlock();
271
272         if (OC_STACK_OK != ret) {
273                 ERR("OCUnBindResource() Fail(%d)", ret);
274                 return icd_ioty_convert_error(ret);
275         }
276
277         return IOTCON_ERROR_NONE;
278 }
279
280 int icd_ioty_notify(OCResourceHandle handle, GVariant *msg, GVariant *observers)
281 {
282         int i, obs_length, msg_length;
283         GVariant *repr_gvar;
284         GVariantIter obs_iter, msg_iter;
285         OCStackResult ret;
286         OCRepPayload *payload;
287
288         g_variant_iter_init(&obs_iter, observers);
289         obs_length = g_variant_iter_n_children(&obs_iter);
290
291         /* Variable-length Array */
292         OCObservationId obs_ids[obs_length];
293
294         for (i = 0; i < obs_length; i++)
295                 g_variant_iter_loop(&obs_iter, "i", &obs_ids[i]);
296
297         g_variant_iter_init(&msg_iter, msg);
298         msg_length = g_variant_iter_n_children(&msg_iter);
299         if (msg_length) {
300                 g_variant_iter_loop(&msg_iter, "v", &repr_gvar);
301                 /* TODO : How to use error_code. */
302                 payload = icd_payload_representation_from_gvariant(repr_gvar);
303         }
304
305         icd_ioty_csdk_lock();
306         /* TODO : QoS is come from lib. */
307         if (msg_length) {
308                 ret = OCNotifyListOfObservers(handle, obs_ids, obs_length, payload, OC_LOW_QOS);
309         } else {
310                 ret = OCNotifyAllObservers(handle, OC_LOW_QOS);
311         }
312         icd_ioty_csdk_unlock();
313
314         if (OC_STACK_NO_OBSERVERS == ret) {
315                 WARN("No Observers. Stop Notifying");
316                 return IOTCON_ERROR_NONE;
317         } else if (OC_STACK_OK != ret) {
318                 ERR("OCNotifyListOfObservers() Fail(%d)", ret);
319                 return icd_ioty_convert_error(ret);
320         }
321
322         return IOTCON_ERROR_NONE;
323 }
324
325
326 static int _ioty_get_header_options(GVariantIter *src, int src_size,
327                 OCHeaderOption dest[], int dest_size)
328 {
329         int i = 0;
330         char *option_data;
331         unsigned short option_id;
332
333         RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
334
335         if (dest_size < src_size) {
336                 ERR("Exceed Size(%d)", src_size);
337                 return IOTCON_ERROR_INVALID_PARAMETER;
338         }
339
340         while (g_variant_iter_loop(src, "(q&s)", &option_id, &option_data)) {
341                 dest[i].protocolID = OC_COAP_ID;
342                 dest[i].optionID = option_id;
343                 dest[i].optionLength = strlen(option_data) + 1;
344                 memcpy(dest[i].optionData, option_data, dest[i].optionLength);
345                 i++;
346         }
347
348         return IOTCON_ERROR_NONE;
349 }
350
351
352 int icd_ioty_send_response(GVariant *resp)
353 {
354         GVariant *repr_gvar;
355         GVariantIter *options;
356         OCStackResult ret;
357         OCEntityHandlerResponse response = {0};
358         int result, options_size;
359         int64_t request_handle, resource_handle;
360
361         g_variant_get(resp, "(a(qs)ivxx)",
362                         &options,
363                         &result,
364                         &repr_gvar,
365                         &request_handle,
366                         &resource_handle);
367
368         response.requestHandle = ICD_INT64_TO_POINTER(request_handle);
369         response.resourceHandle = ICD_INT64_TO_POINTER(resource_handle);
370         response.ehResult = (OCEntityHandlerResult)result;
371
372         options_size = g_variant_iter_n_children(options);
373         response.numSendVendorSpecificHeaderOptions = options_size;
374
375         if (0 != options_size) {
376                 int ret= _ioty_get_header_options(options,
377                                 response.numSendVendorSpecificHeaderOptions,
378                                 response.sendVendorSpecificHeaderOptions,
379                                 sizeof(response.sendVendorSpecificHeaderOptions)
380                                 / sizeof(response.sendVendorSpecificHeaderOptions[0]));
381
382                 if (IOTCON_ERROR_NONE != ret)
383                         ERR("_ioty_get_header_options() Fail(%d)", ret);
384         }
385         g_variant_iter_free(options);
386
387         response.payload = (OCPayload*)icd_payload_representation_from_gvariant(repr_gvar);
388
389         /* related to block transfer */
390         response.persistentBufferFlag = 0;
391
392         icd_ioty_csdk_lock();
393         ret = OCDoResponse(&response);
394         icd_ioty_csdk_unlock();
395
396         if (OC_STACK_OK != ret) {
397                 ERR("OCDoResponse() Fail(%d)", ret);
398                 return icd_ioty_convert_error(ret);
399         }
400
401         return IOTCON_ERROR_NONE;
402 }
403
404
405 static void _ioty_free_signal_context(void *data)
406 {
407         icd_sig_ctx_s *context = data;
408         free(context->bus_name);
409         free(context);
410 }
411
412
413 int icd_ioty_find_resource(const char *host_address, int conn_type,
414                 const char *resource_type, unsigned int signal_number, const char *bus_name)
415 {
416         int len;
417         OCStackResult result;
418         icd_sig_ctx_s *context;
419         char uri[PATH_MAX] = {0};
420         OCCallbackData cbdata = {0};
421         OCConnectivityType oic_conn_type;
422
423         if (IC_STR_EQUAL == strcmp(IC_STR_NULL, host_address)) {
424                 len = snprintf(uri, sizeof(uri), "%s", OC_RSRVD_WELL_KNOWN_URI);
425         } else {
426                 len = snprintf(uri, sizeof(uri), ICD_IOTY_COAP"%s%s", host_address,
427                                 OC_RSRVD_WELL_KNOWN_URI);
428         }
429         if (len <= 0 || sizeof(uri) <= len) {
430                 ERR("snprintf() Fail(%d)", len);
431                 return IOTCON_ERROR_IO_ERROR;
432         }
433
434         if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
435                 snprintf(uri + len, sizeof(uri) - len, "?rt=%s", resource_type);
436
437         context = calloc(1, sizeof(icd_sig_ctx_s));
438         if (NULL == context) {
439                 ERR("calloc() Fail(%d)", errno);
440                 return IOTCON_ERROR_OUT_OF_MEMORY;
441         }
442
443         context->bus_name = ic_utils_strdup(bus_name);
444         context->signal_number = signal_number;
445
446         cbdata.context = context;
447         cbdata.cb = icd_ioty_ocprocess_find_cb;
448         cbdata.cd = _ioty_free_signal_context;
449
450         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
451
452         icd_ioty_csdk_lock();
453         /* TODO : QoS is come from lib. */
454         result = OCDoResource(NULL, OC_REST_DISCOVER, uri, NULL, NULL, oic_conn_type,
455                         OC_LOW_QOS, &cbdata, NULL, 0);
456         icd_ioty_csdk_unlock();
457
458         if (OC_STACK_OK != result) {
459                 ERR("OCDoResource() Fail(%d)", result);
460                 free(context->bus_name);
461                 free(context);
462                 return icd_ioty_convert_error(result);
463         }
464
465         return IOTCON_ERROR_NONE;
466 }
467
468
469 /*
470  * returned string SHOULD be released by you
471  */
472 static char* _icd_ioty_resource_generate_uri(char *uri_path, GVariant *query)
473 {
474         int len;
475         bool loop_first = true;
476         char *key, *value;
477         GVariantIter query_iter;
478         char uri_buf[PATH_MAX] = {0};
479
480         len = snprintf(uri_buf, sizeof(uri_buf), "%s", uri_path);
481
482         /* remove suffix '/' */
483         if ('/' == uri_buf[strlen(uri_buf) - 1]) {
484                 uri_buf[strlen(uri_buf) - 1] = '\0';
485                 len--;
486         }
487
488         g_variant_iter_init(&query_iter, query);
489
490         while (g_variant_iter_loop(&query_iter, "(&s&s)", &key, &value)) {
491                 int query_len;
492
493                 DBG("query exist. key(%s), value(%s)", key, value);
494
495                 if (true == loop_first) {
496                         query_len = snprintf(uri_buf + len, sizeof(uri_buf) - len, "?%s=%s", key, value);
497                         loop_first = false;
498                 } else {
499                         query_len = snprintf(uri_buf + len, sizeof(uri_buf) - len, "&%s=%s", key, value);
500                 }
501
502                 len += query_len;
503         }
504
505         return strdup(uri_buf);
506 }
507
508
509 void icd_ioty_complete(int type, GDBusMethodInvocation *invocation, GVariant *value)
510 {
511         switch(type) {
512         case ICD_CRUD_GET:
513                 ic_dbus_complete_get(icd_dbus_get_object(), invocation, value);
514                 break;
515         case ICD_CRUD_PUT:
516                 ic_dbus_complete_put(icd_dbus_get_object(), invocation, value);
517                 break;
518         case ICD_CRUD_POST:
519                 ic_dbus_complete_post(icd_dbus_get_object(), invocation, value);
520                 break;
521         case ICD_CRUD_DELETE:
522                 ic_dbus_complete_delete(icd_dbus_get_object(), invocation, value);
523                 break;
524         case ICD_TIZEN_INFO:
525                 ic_dbus_complete_get_tizen_info(icd_dbus_get_object(), invocation, value);
526                 break;
527         default:
528                 INFO("Invalid type(%d)", type);
529         }
530 }
531
532
533 void icd_ioty_complete_error(int type, GDBusMethodInvocation *invocation, int ret_val)
534 {
535         GVariant *value;
536         GVariant *payload;
537         GVariantBuilder options;
538
539         switch (type) {
540         case ICD_CRUD_GET:
541                 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
542                 payload = icd_payload_representation_empty_gvariant();
543                 value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
544                 ic_dbus_complete_get(icd_dbus_get_object(), invocation, value);
545                 break;
546         case ICD_CRUD_PUT:
547                 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
548                 payload = icd_payload_representation_empty_gvariant();
549                 value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
550                 ic_dbus_complete_put(icd_dbus_get_object(), invocation, value);
551                 break;
552         case ICD_CRUD_POST:
553                 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
554                 payload = icd_payload_representation_empty_gvariant();
555                 value = g_variant_new("(a(qs)vi)", &options, payload, ret_val);
556                 ic_dbus_complete_post(icd_dbus_get_object(), invocation, value);
557                 break;
558         case ICD_CRUD_DELETE:
559                 g_variant_builder_init(&options, G_VARIANT_TYPE("a(qs)"));
560                 value = g_variant_new("(a(qs)i)", &options, ret_val);
561                 ic_dbus_complete_delete(icd_dbus_get_object(), invocation, value);
562                 break;
563         case ICD_TIZEN_INFO:
564                 value = g_variant_new("(ssi)", IC_STR_NULL, IC_STR_NULL, ret_val);
565                 ic_dbus_complete_get_tizen_info(icd_dbus_get_object(), invocation, value);
566                 break;
567         }
568
569 }
570
571
572 static gboolean _icd_ioty_crud(int type,
573                 icDbus *object,
574                 GDBusMethodInvocation *invocation,
575                 GVariant *resource,
576                 GVariant *query,
577                 GVariant *repr)
578 {
579         bool is_secure;
580         OCMethod rest_type;
581         OCStackResult result;
582         GVariantIter *options;
583         OCCallbackData cbdata = {0};
584         int ret, conn_type, options_size;
585         char *uri_path, *uri, *host;
586         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
587         OCHeaderOption *oic_options_ptr = NULL;
588         OCPayload *payload = NULL;
589         OCConnectivityType oic_conn_type;
590         OCDevAddr dev_addr = {0};
591
592         switch (type) {
593         case ICD_CRUD_GET:
594                 cbdata.cb = icd_ioty_ocprocess_get_cb;
595                 rest_type = OC_REST_GET;
596                 break;
597         case ICD_CRUD_PUT:
598                 cbdata.cb = icd_ioty_ocprocess_put_cb;
599                 rest_type = OC_REST_PUT;
600                 break;
601         case ICD_CRUD_POST:
602                 cbdata.cb = icd_ioty_ocprocess_post_cb;
603                 rest_type = OC_REST_POST;
604                 break;
605         case ICD_CRUD_DELETE:
606                 cbdata.cb = icd_ioty_ocprocess_delete_cb;
607                 rest_type = OC_REST_DELETE;
608                 break;
609         default:
610                 ERR("Invalid CRUD Type(%d)", type);
611                 return FALSE;
612         }
613
614         g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure, &options,
615                         &conn_type);
616
617         switch (type) {
618         case ICD_CRUD_GET:
619         case ICD_CRUD_PUT:
620         case ICD_CRUD_POST:
621                 uri = _icd_ioty_resource_generate_uri(uri_path, query);
622                 if (NULL == uri) {
623                         ERR("_icd_ioty_resource_generate_uri() Fail");
624                         g_variant_iter_free(options);
625                         icd_ioty_complete_error(type, invocation, IOTCON_ERROR_INVALID_PARAMETER);
626                         return TRUE;
627                 }
628                 break;
629         case ICD_CRUD_DELETE:
630                 uri = strdup(uri_path);
631                 break;
632         }
633
634         cbdata.context = invocation;
635
636         options_size = g_variant_iter_n_children(options);
637         if (0 != options_size) {
638                 int ret = _ioty_get_header_options(options, options_size, oic_options,
639                                 sizeof(oic_options) / sizeof(oic_options[0]));
640                 if (IOTCON_ERROR_NONE != ret) {
641                         ERR("_ioty_get_header_options() Fail(%d)", ret);
642                         free(uri);
643                         g_variant_iter_free(options);
644                         icd_ioty_complete_error(type, invocation, ret);
645                         return TRUE;
646                 }
647                 oic_options_ptr = oic_options;
648         }
649         g_variant_iter_free(options);
650
651         if (repr)
652                 payload = (OCPayload*)icd_payload_representation_from_gvariant(repr);
653
654         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
655
656         ret = icd_ioty_get_dev_addr(host, conn_type, &dev_addr);
657         if (IOTCON_ERROR_NONE != ret) {
658                 ERR("icd_ioty_get_dev_addr() Fail(%d)", ret);
659                 icd_ioty_complete_error(type, invocation, IOTCON_ERROR_IOTIVITY);
660                 free(uri);
661                 return TRUE;
662         }
663
664         icd_ioty_csdk_lock();
665         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
666         result = OCDoResource(NULL, rest_type, uri, &dev_addr, payload, oic_conn_type,
667                         OC_LOW_QOS, &cbdata, oic_options_ptr, options_size);
668         icd_ioty_csdk_unlock();
669
670         free(uri);
671
672         if (OC_STACK_OK != result) {
673                 ERR("OCDoResource() Fail(%d)", result);
674                 icd_ioty_complete_error(type, invocation, icd_ioty_convert_error(result));
675                 return TRUE;
676         }
677
678         return TRUE;
679 }
680
681 gboolean icd_ioty_get(icDbus *object, GDBusMethodInvocation *invocation,
682                 GVariant *resource, GVariant *query)
683 {
684         return _icd_ioty_crud(ICD_CRUD_GET, object, invocation, resource, query, NULL);
685 }
686
687
688 gboolean icd_ioty_put(icDbus *object, GDBusMethodInvocation *invocation,
689                 GVariant *resource, GVariant *repr, GVariant *query)
690 {
691         return _icd_ioty_crud(ICD_CRUD_PUT, object, invocation, resource, query, repr);
692 }
693
694
695 gboolean icd_ioty_post(icDbus *object, GDBusMethodInvocation *invocation,
696                 GVariant *resource, GVariant *repr, GVariant *query)
697 {
698         return _icd_ioty_crud(ICD_CRUD_POST, object, invocation, resource, query, repr);
699 }
700
701
702 gboolean icd_ioty_delete(icDbus *object, GDBusMethodInvocation *invocation,
703                 GVariant *resource)
704 {
705         return _icd_ioty_crud(ICD_CRUD_DELETE, object, invocation, resource, NULL, NULL);
706 }
707
708
709 OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_type,
710                 GVariant *query, unsigned int signal_number, const char *bus_name)
711 {
712         bool is_secure;
713         OCMethod method;
714         OCDoHandle handle;
715         OCStackResult result;
716         GVariantIter *options;
717         icd_sig_ctx_s *context;
718         OCCallbackData cbdata = {0};
719         int ret, conn_type, options_size;
720         char *uri_path, *host, *uri;
721         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
722         OCHeaderOption *oic_options_ptr = NULL;
723         OCConnectivityType oic_conn_type;
724         OCDevAddr dev_addr = {0};
725
726         g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure, &options,
727                         &conn_type);
728
729         uri = _icd_ioty_resource_generate_uri(uri_path, query);
730         if (NULL == uri) {
731                 ERR("_icd_ioty_resource_generate_uri() Fail");
732                 g_variant_iter_free(options);
733                 return NULL;
734         }
735
736         if (IOTCON_OBSERVE == observe_type)
737                 method = OC_REST_OBSERVE;
738         else if (IOTCON_OBSERVE_ALL == observe_type)
739                 method = OC_REST_OBSERVE_ALL;
740         else
741                 method = OC_REST_OBSERVE_ALL;
742
743         context = calloc(1, sizeof(icd_sig_ctx_s));
744         if (NULL == context) {
745                 ERR("calloc() Fail(%d)", errno);
746                 return NULL;
747         }
748         context->bus_name = ic_utils_strdup(bus_name);
749         context->signal_number = signal_number;
750
751         cbdata.context = context;
752         cbdata.cb = icd_ioty_ocprocess_observe_cb;
753         cbdata.cd = _ioty_free_signal_context;
754
755         options_size = g_variant_iter_n_children(options);
756         if (0 != options_size) {
757                 int ret = _ioty_get_header_options(options, options_size, oic_options,
758                                 sizeof(oic_options) / sizeof(oic_options[0]));
759                 if (IOTCON_ERROR_NONE != ret) {
760                         ERR("_ioty_get_header_options() Fail(%d)", ret);
761                         free(context->bus_name);
762                         free(context);
763                         free(uri);
764                         g_variant_iter_free(options);
765                         return NULL;
766                 }
767                 oic_options_ptr = oic_options;
768         }
769         g_variant_iter_free(options);
770
771         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
772
773         ret = icd_ioty_get_dev_addr(host, conn_type, &dev_addr);
774         if (IOTCON_ERROR_NONE != ret) {
775                 ERR("icd_ioty_get_dev_addr() Fail(%d)", ret);
776                 free(context->bus_name);
777                 free(context);
778                 free(uri);
779                 return NULL;
780         }
781
782         icd_ioty_csdk_lock();
783         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
784         result = OCDoResource(&handle, method, uri, &dev_addr, NULL, oic_conn_type,
785                         OC_LOW_QOS, &cbdata, oic_options_ptr, options_size);
786         icd_ioty_csdk_unlock();
787         free(uri);
788         if (OC_STACK_OK != result) {
789                 ERR("OCDoResource() Fail(%d)", result);
790                 free(context->bus_name);
791                 free(context);
792                 return NULL;
793         }
794
795         return handle;
796 }
797
798
799 int icd_ioty_observer_stop(OCDoHandle handle, GVariant *options)
800 {
801         int options_size;
802         OCStackResult ret;
803         GVariantIter options_iter;
804         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
805         OCHeaderOption *oic_options_ptr = NULL;
806
807         g_variant_iter_init(&options_iter, options);
808
809         options_size = g_variant_iter_n_children(&options_iter);
810         if (0 != options_size) {
811                 int ret = _ioty_get_header_options(&options_iter, options_size, oic_options,
812                                 sizeof(oic_options) / sizeof(oic_options[0]));
813                 if (IOTCON_ERROR_NONE != ret) {
814                         ERR("_ioty_get_header_options() Fail(%d)", ret);
815                         return ret;
816                 }
817                 oic_options_ptr = oic_options;
818         }
819
820         icd_ioty_csdk_lock();
821         ret = OCCancel(handle, OC_HIGH_QOS, oic_options_ptr, options_size);
822         icd_ioty_csdk_unlock();
823         if (OC_STACK_OK != ret) {
824                 ERR("OCCancel() Fail(%d)", ret);
825                 return icd_ioty_convert_error(ret);
826         }
827
828         return IOTCON_ERROR_NONE;
829 }
830
831 int icd_ioty_get_info(int type, const char *host_address, int conn_type,
832                 unsigned int signal_number, const char *bus_name)
833 {
834         OCStackResult result;
835         icd_sig_ctx_s *context;
836         OCCallbackData cbdata = {0};
837         char uri[PATH_MAX] = {0};
838         char *uri_path = NULL;
839         OCConnectivityType oic_conn_type;
840
841         if (ICD_DEVICE_INFO == type)
842                 uri_path = OC_RSRVD_DEVICE_URI;
843         else if (ICD_PLATFORM_INFO == type)
844                 uri_path = OC_RSRVD_PLATFORM_URI;
845         else
846                 return IOTCON_ERROR_INVALID_PARAMETER;
847
848         if (IC_STR_EQUAL == strcmp(IC_STR_NULL, host_address))
849                 snprintf(uri, sizeof(uri), "%s", uri_path);
850         else
851                 snprintf(uri, sizeof(uri), "%s%s", host_address, uri_path);
852
853         context = calloc(1, sizeof(icd_sig_ctx_s));
854         if (NULL == context) {
855                 ERR("calloc() Fail(%d)", errno);
856                 return IOTCON_ERROR_OUT_OF_MEMORY;
857         }
858         context->bus_name = ic_utils_strdup(bus_name);
859         context->signal_number = signal_number;
860
861         cbdata.context = context;
862         cbdata.cb = icd_ioty_ocprocess_info_cb;
863         cbdata.cd = _ioty_free_signal_context;
864
865         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
866
867         icd_ioty_csdk_lock();
868         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
869         result = OCDoResource(NULL, OC_REST_DISCOVER, uri, NULL, NULL, oic_conn_type,
870                         OC_LOW_QOS, &cbdata, NULL, 0);
871         icd_ioty_csdk_unlock();
872
873         if (OC_STACK_OK != result) {
874                 ERR("OCDoResource() Fail(%d)", result);
875                 free(context->bus_name);
876                 free(context);
877                 return icd_ioty_convert_error(result);
878         }
879
880         return IOTCON_ERROR_NONE;
881 }
882
883 static int _icd_ioty_get_tizen_id(char **tizen_device_id)
884 {
885         int ret;
886         char *tizen_id = NULL;
887
888         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_TIZEN_ID, &tizen_id);
889         if (SYSTEM_INFO_ERROR_NONE != ret) {
890                 ERR("system_info_get_platform_string() Fail(%d)", ret);
891                 return IOTCON_ERROR_SYSTEM;
892         }
893         *tizen_device_id = tizen_id;
894
895         return IOTCON_ERROR_NONE;
896 }
897
898 static int _ioty_set_device_info()
899 {
900         int ret;
901         char *device_name = NULL;
902         OCDeviceInfo device_info = {0};
903
904         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEVICE_NAME, &device_name);
905         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
906                 ERR("system_settings_get_value_string() Fail(%d)", ret);
907                 return IOTCON_ERROR_SYSTEM;
908         }
909
910         device_info.deviceName = device_name;
911
912         icd_ioty_csdk_lock();
913         ret = OCSetDeviceInfo(device_info);
914         icd_ioty_csdk_unlock();
915
916         if (OC_STACK_OK != ret) {
917                 ERR("OCSetDeviceInfo() Fail(%d)", ret);
918                 free(device_name);
919                 return icd_ioty_convert_error(ret);
920         }
921
922         free(icd_tizen_info.device_name);
923         icd_tizen_info.device_name = device_name;
924
925         return IOTCON_ERROR_NONE;
926 }
927
928 static void _icd_ioty_on_device_name_changed_cb(system_settings_key_e key,
929                 void *user_data)
930 {
931         FN_CALL;
932         int ret;
933
934         ret = _ioty_set_device_info();
935         if (IOTCON_ERROR_NONE != ret) {
936                 ERR("_ioty_set_device_info() Fail(%d)", ret);
937                 return;
938         }
939 }
940
941 int icd_ioty_set_device_info()
942 {
943         int ret;
944
945         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_DEVICE_NAME,
946                         _icd_ioty_on_device_name_changed_cb, NULL);
947         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
948                 ERR("system_settings_set_changed_cb() Fail(%d)", ret);
949                 return IOTCON_ERROR_SYSTEM;
950         }
951
952         ret = _ioty_set_device_info();
953         if (IOTCON_ERROR_NONE != ret) {
954                 ERR("_ioty_set_device_info() Fail(%d)", ret);
955                 return ret;
956         }
957
958         return IOTCON_ERROR_NONE;
959 }
960
961 static void _ioty_free_platform_info(OCPlatformInfo platform_info)
962 {
963         free(platform_info.manufacturerName);
964         free(platform_info.manufacturerUrl);
965         free(platform_info.modelNumber);
966         free(platform_info.dateOfManufacture);
967         free(platform_info.platformVersion);
968         free(platform_info.operatingSystemVersion);
969         free(platform_info.hardwareVersion);
970         free(platform_info.firmwareVersion);
971         free(platform_info.supportUrl);
972         free(platform_info.systemTime);
973 }
974
975 int icd_ioty_set_platform_info()
976 {
977         int ret;
978         OCPlatformInfo platform_info = {0};
979
980         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_PLATFORM_NAME,
981                         &platform_info.platformID);
982         if (SYSTEM_INFO_ERROR_NONE != ret) {
983                 ERR("system_info_get_platform_string() Fail(%d)", ret);
984                 _ioty_free_platform_info(platform_info);
985                 return IOTCON_ERROR_SYSTEM;
986         }
987
988         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_MANUF_NAME,
989                         &platform_info.manufacturerName);
990         if (SYSTEM_INFO_ERROR_NONE != ret) {
991                 ERR("system_info_get_platform_string() Fail(%d)", ret);
992                 _ioty_free_platform_info(platform_info);
993                 return IOTCON_ERROR_SYSTEM;
994         }
995
996         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_MODEL_NAME,
997                         &platform_info.modelNumber);
998         if (SYSTEM_INFO_ERROR_NONE != ret) {
999                 ERR("system_info_get_platform_string() Fail(%d)", ret);
1000                 _ioty_free_platform_info(platform_info);
1001                 return IOTCON_ERROR_SYSTEM;
1002         }
1003
1004         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_PLATFORM_VERSION,
1005                         &platform_info.platformVersion);
1006         if (SYSTEM_INFO_ERROR_NONE != ret) {
1007                 ERR("system_info_get_platform_string() Fail(%d)", ret);
1008                 _ioty_free_platform_info(platform_info);
1009                 return IOTCON_ERROR_SYSTEM;
1010         }
1011
1012         ret = system_info_get_platform_string(ICD_SYSTEM_INFO_BUILD_STRING,
1013                         &platform_info.firmwareVersion);
1014         if (SYSTEM_INFO_ERROR_NONE != ret) {
1015                 ERR("system_info_get_platform_string() Fail(%d)", ret);
1016                 _ioty_free_platform_info(platform_info);
1017                 return IOTCON_ERROR_SYSTEM;
1018         }
1019
1020         /* platform_info.manufacturerUrl */
1021         /* platform_info.dateOfManufacture */
1022         /* platform_info.operatingSystemVersion */
1023         /* platform_info.hardwareVersion */
1024         /* platform_info.supportUrl */
1025         /* platform_info.systemTime */
1026
1027         icd_ioty_csdk_lock();
1028         ret = OCSetPlatformInfo(platform_info);
1029         icd_ioty_csdk_unlock();
1030
1031         if (OC_STACK_OK != ret) {
1032                 ERR("OCSetPlatformInfo() Fail(%d)", ret);
1033                 _ioty_free_platform_info(platform_info);
1034                 return icd_ioty_convert_error(ret);
1035         }
1036         _ioty_free_platform_info(platform_info);
1037
1038         return IOTCON_ERROR_NONE;
1039 }
1040
1041 int icd_ioty_set_tizen_info()
1042 {
1043         int result;
1044         OCStackResult ret;
1045         OCResourceHandle handle;
1046         char *tizen_device_id = NULL;
1047
1048         result = _icd_ioty_get_tizen_id(&tizen_device_id);
1049         if (IOTCON_ERROR_NONE != result) {
1050                 ERR("_icd_ioty_get_tizen_id() Fail(%d)", result);
1051                 return result;
1052         }
1053
1054         icd_tizen_info.tizen_device_id = tizen_device_id;
1055         DBG("tizen_device_id : %s", icd_tizen_info.tizen_device_id);
1056
1057         icd_ioty_csdk_lock();
1058         ret = OCCreateResource(&handle,
1059                         ICD_IOTY_TIZEN_INFO_TYPE,
1060                         IC_INTERFACE_DEFAULT,
1061                         ICD_IOTY_TIZEN_INFO_URI,
1062                         icd_ioty_ocprocess_tizen_info_handler,
1063                         NULL,
1064                         OC_EXPLICIT_DISCOVERABLE);
1065         icd_ioty_csdk_unlock();
1066         if (OC_STACK_OK != ret) {
1067                 ERR("OCCreateResource() Fail(%d)", ret);
1068                 return icd_ioty_convert_error(ret);
1069         }
1070
1071         return IOTCON_ERROR_NONE;
1072 }
1073
1074
1075 gboolean icd_ioty_get_tizen_info(icDbus *object, GDBusMethodInvocation *invocation,
1076                 const gchar *host_address, int conn_type)
1077 {
1078         int ret;
1079         OCStackResult result;
1080         OCDevAddr dev_addr = {0};
1081         OCCallbackData cbdata = {0};
1082         OCConnectivityType oic_conn_type;
1083
1084         cbdata.cb = icd_ioty_ocprocess_get_tizen_info_cb;
1085         cbdata.context = invocation;
1086
1087         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
1088
1089         ret = icd_ioty_get_dev_addr(host_address, conn_type, &dev_addr);
1090         if (IOTCON_ERROR_NONE != ret) {
1091                 ERR("icd_ioty_get_dev_addr() Fail(%d)", ret);
1092                 icd_ioty_complete_error(ICD_TIZEN_INFO, invocation, IOTCON_ERROR_IOTIVITY);
1093                 return TRUE;
1094         }
1095
1096         icd_ioty_csdk_lock();
1097         result = OCDoResource(NULL, OC_REST_GET, ICD_IOTY_TIZEN_INFO_URI, &dev_addr, NULL,
1098                         oic_conn_type, OC_LOW_QOS, &cbdata, NULL, 0);
1099         icd_ioty_csdk_unlock();
1100
1101         if (OC_STACK_OK != result) {
1102                 ERR("OCDoResource() Fail(%d)", result);
1103                 icd_ioty_complete_error(ICD_TIZEN_INFO, invocation, icd_ioty_convert_error(result));
1104                 return TRUE;
1105         }
1106
1107         return TRUE;
1108 }
1109
1110
1111 int icd_ioty_tizen_info_get_property(char **device_name, char **tizen_device_id)
1112 {
1113         RETV_IF(NULL == device_name, IOTCON_ERROR_INVALID_PARAMETER);
1114         RETV_IF(NULL == tizen_device_id, IOTCON_ERROR_INVALID_PARAMETER);
1115
1116         *device_name = icd_tizen_info.device_name;
1117         *tizen_device_id = icd_tizen_info.tizen_device_id;
1118
1119         return IOTCON_ERROR_NONE;
1120 }
1121
1122
1123 OCDoHandle icd_ioty_subscribe_presence(const char *host_address, int conn_type,
1124                 const char *resource_type, unsigned int signal_number, const char *bus_name)
1125 {
1126         int len;
1127         OCDoHandle handle;
1128         OCStackResult result;
1129         icd_sig_ctx_s *context;
1130         char uri[PATH_MAX] = {0};
1131         OCCallbackData cbdata = {0};
1132         OCConnectivityType oic_conn_type;
1133
1134         if (IC_STR_EQUAL == strcmp(IC_STR_NULL, host_address) || '\0' == host_address[0]) {
1135                 len = snprintf(uri, sizeof(uri), "%s%s", ICD_MULTICAST_ADDRESS,
1136                                 OC_RSRVD_PRESENCE_URI);
1137         } else {
1138                 len = snprintf(uri, sizeof(uri), "%s%s", host_address, OC_RSRVD_PRESENCE_URI);
1139         }
1140         if (len <= 0 || sizeof(uri) <= len) {
1141                 ERR("snprintf() Fail(%d)", len);
1142                 return NULL;
1143         }
1144
1145         if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
1146                 snprintf(uri + len, sizeof(uri) - len, "?rt=%s", resource_type);
1147
1148         context = calloc(1, sizeof(icd_sig_ctx_s));
1149         if (NULL == context) {
1150                 ERR("calloc() Fail(%d)", errno);
1151                 return NULL;
1152         }
1153         context->bus_name = ic_utils_strdup(bus_name);
1154         context->signal_number = signal_number;
1155
1156         cbdata.context = context;
1157         cbdata.cb = icd_ioty_ocprocess_presence_cb;
1158         cbdata.cd = _ioty_free_signal_context;
1159
1160         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
1161
1162         icd_ioty_csdk_lock();
1163         result = OCDoResource(&handle, OC_REST_PRESENCE, uri, NULL, NULL, oic_conn_type,
1164                         OC_LOW_QOS, &cbdata, NULL, 0);
1165         icd_ioty_csdk_unlock();
1166
1167         if (OC_STACK_OK != result) {
1168                 ERR("OCDoResource() Fail(%d)", result);
1169                 free(context->bus_name);
1170                 free(context);
1171                 return NULL;
1172         }
1173         return handle;
1174 }
1175
1176
1177 int icd_ioty_unsubscribe_presence(OCDoHandle handle)
1178 {
1179         OCStackResult ret;
1180
1181         icd_ioty_csdk_lock();
1182         ret = OCCancel(handle, OC_LOW_QOS, NULL, 0);
1183         icd_ioty_csdk_unlock();
1184         if (OC_STACK_OK != ret) {
1185                 ERR("OCCancel() Fail(%d)", ret);
1186                 return icd_ioty_convert_error(ret);
1187         }
1188
1189         return IOTCON_ERROR_NONE;
1190 }
1191
1192
1193 int icd_ioty_start_presence(unsigned int time_to_live)
1194 {
1195         OCStackResult ret;
1196
1197         icd_ioty_csdk_lock();
1198         ret = OCStartPresence(time_to_live);
1199         icd_ioty_csdk_unlock();
1200         if (OC_STACK_OK != ret) {
1201                 ERR("OCStartPresence() Fail(%d)", ret);
1202                 return icd_ioty_convert_error(ret);
1203         }
1204
1205         return IOTCON_ERROR_NONE;
1206 }
1207
1208
1209 int icd_ioty_stop_presence()
1210 {
1211         OCStackResult ret;
1212
1213         icd_ioty_csdk_lock();
1214         ret = OCStopPresence();
1215         icd_ioty_csdk_unlock();
1216         if (OC_STACK_OK != ret) {
1217                 ERR("OCStopPresence() Fail(%d)", ret);
1218                 return icd_ioty_convert_error(ret);
1219         }
1220
1221         return IOTCON_ERROR_NONE;
1222 }