Support 64-bit architectures
[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
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h> /* for uint8_t etc */
20 #include <stdbool.h>
21 #include <errno.h>
22 #include <glib.h>
23
24 #include <octypes.h>
25 #include <ocstack.h>
26
27 #include "iotcon.h"
28 #include "ic-dbus.h"
29 #include "ic-utils.h"
30 #include "icd.h"
31 #include "icd-payload.h"
32 #include "icd-dbus.h"
33 #include "icd-ioty.h"
34 #include "icd-ioty-type.h"
35 #include "icd-ioty-ocprocess.h"
36
37 static GMutex icd_csdk_mutex;
38
39 void icd_ioty_csdk_lock()
40 {
41         g_mutex_lock(&icd_csdk_mutex);
42 }
43
44
45 void icd_ioty_csdk_unlock()
46 {
47         g_mutex_unlock(&icd_csdk_mutex);
48 }
49
50
51 GThread* icd_ioty_init(const char *addr, unsigned short port)
52 {
53         FN_CALL;
54         GError *error;
55         GThread *thread;
56
57         OCStackResult result = OCInit(addr, port, OC_CLIENT_SERVER);
58         if (OC_STACK_OK != result) {
59                 ERR("OCInit() Fail(%d)", result);
60                 return NULL;
61         }
62
63         DBG("OCInit() Success");
64
65         thread = g_thread_try_new("packet_receive_thread", icd_ioty_ocprocess_thread,
66                         NULL, &error);
67         if (NULL == thread) {
68                 ERR("g_thread_try_new() Fail(%s)", error->message);
69                 g_error_free(error);
70                 return NULL;
71         }
72
73         return thread;
74 }
75
76
77 void icd_ioty_deinit(GThread *thread)
78 {
79         OCStackResult result;
80
81         icd_ioty_ocprocess_stop();
82         g_thread_join(thread);
83
84         result = OCStop();
85         if (OC_STACK_OK != result)
86                 ERR("OCStop() Fail(%d)", result);
87 }
88
89
90 OCResourceHandle icd_ioty_register_resource(const char *uri_path,
91                 const char* const* res_types, int ifaces, uint8_t properties)
92 {
93         FN_CALL;
94         int i;
95         OCStackResult ret;
96         OCResourceHandle handle;
97         const char *res_iface = NULL;
98
99         if (IOTCON_INTERFACE_DEFAULT & ifaces) {
100                 res_iface = IC_INTERFACE_DEFAULT;
101                 ifaces ^= IOTCON_INTERFACE_DEFAULT;
102         } else if (IOTCON_INTERFACE_LINK & ifaces) {
103                 res_iface = IC_INTERFACE_LINK;
104                 ifaces ^= IOTCON_INTERFACE_LINK;
105         } else if (IOTCON_INTERFACE_BATCH & ifaces) {
106                 res_iface = IC_INTERFACE_BATCH;
107                 ifaces ^= IOTCON_INTERFACE_BATCH;
108         } else if (IOTCON_INTERFACE_GROUP & ifaces) {
109                 res_iface = IC_INTERFACE_GROUP;
110                 ifaces ^= IOTCON_INTERFACE_GROUP;
111         } else {
112                 ERR("Invalid interface type(%d)", ifaces);
113                 return NULL;
114         }
115
116         /* Secure option is not supported yet. */
117         properties = (properties & OC_SECURE)? (properties ^ OC_SECURE):properties;
118
119         icd_ioty_csdk_lock();
120         ret = OCCreateResource(&handle, res_types[0], res_iface, uri_path,
121                         icd_ioty_ocprocess_req_handler, NULL, properties);
122         icd_ioty_csdk_unlock();
123         if (OC_STACK_OK != ret) {
124                 ERR("OCCreateResource() Fail(%d)", ret);
125                 return NULL;
126         }
127
128         for (i = 1; res_types[i]; i++)
129                 icd_ioty_bind_type(handle, res_types[i]);
130
131         if (IOTCON_INTERFACE_DEFAULT & ifaces)
132                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_DEFAULT);
133         if (IOTCON_INTERFACE_LINK & ifaces)
134                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_LINK);
135         if (IOTCON_INTERFACE_BATCH & ifaces)
136                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_BATCH);
137         if (IOTCON_INTERFACE_GROUP & ifaces)
138                 icd_ioty_bind_interface(handle, IOTCON_INTERFACE_GROUP);
139
140         return handle;
141 }
142
143
144 int icd_ioty_unregister_resource(OCResourceHandle handle)
145 {
146         OCStackResult ret;
147
148         icd_ioty_csdk_lock();
149         ret = OCDeleteResource(handle);
150         icd_ioty_csdk_unlock();
151         if (OC_STACK_OK != ret) {
152                 ERR("OCDeleteResource() Fail(%d)", ret);
153                 return IOTCON_ERROR_IOTIVITY;
154         }
155
156         return IOTCON_ERROR_NONE;
157 }
158
159
160 int icd_ioty_bind_interface(OCResourceHandle handle, iotcon_interface_e iface)
161 {
162         int ret;
163         OCStackResult result;
164         char *resource_interface;
165
166         ret = ic_utils_convert_interface_flag(iface, &resource_interface);
167         if (IOTCON_ERROR_NONE != ret) {
168                 ERR("ic_utils_convert_interface_flag(%d) Fail(%d)", iface, ret);
169                 return ret;
170         }
171
172         icd_ioty_csdk_lock();
173         result = OCBindResourceInterfaceToResource(handle, resource_interface);
174         icd_ioty_csdk_unlock();
175         if (OC_STACK_OK != result) {
176                 ERR("OCBindResourceInterfaceToResource() Fail(%d)", result);
177                 return IOTCON_ERROR_IOTIVITY;
178         }
179
180         return IOTCON_ERROR_NONE;
181 }
182
183
184 int icd_ioty_bind_type(OCResourceHandle handle, const char *resource_type)
185 {
186         OCStackResult ret;
187
188         icd_ioty_csdk_lock();
189         ret = OCBindResourceTypeToResource(handle, resource_type);
190         icd_ioty_csdk_unlock();
191         if (OC_STACK_OK != ret) {
192                 ERR("OCBindResourceTypeToResource() Fail(%d)", ret);
193                 return IOTCON_ERROR_IOTIVITY;
194         }
195
196         return IOTCON_ERROR_NONE;
197 }
198
199
200 int icd_ioty_bind_resource(OCResourceHandle parent, OCResourceHandle child)
201 {
202         OCStackResult ret;
203
204         icd_ioty_csdk_lock();
205         ret = OCBindResource(parent, child);
206         icd_ioty_csdk_unlock();
207         if (OC_STACK_OK != ret) {
208                 ERR("OCBindResource() Fail(%d)", ret);
209                 return IOTCON_ERROR_IOTIVITY;
210         }
211
212         return IOTCON_ERROR_NONE;
213 }
214
215
216 int icd_ioty_unbind_resource(OCResourceHandle parent, OCResourceHandle child)
217 {
218         OCStackResult ret;
219
220         icd_ioty_csdk_lock();
221         ret = OCUnBindResource(parent, child);
222         icd_ioty_csdk_unlock();
223         if (OC_STACK_OK != ret) {
224                 ERR("OCUnBindResource() Fail(%d)", ret);
225                 return IOTCON_ERROR_IOTIVITY;
226         }
227
228         return IOTCON_ERROR_NONE;
229 }
230
231
232 int icd_ioty_notify_list_of_observers(OCResourceHandle handle, GVariant *msg,
233                 GVariant *observers)
234 {
235         int i, error_code, obs_length;
236         GVariant *repr_gvar;
237         GVariantIter obs_iter, msg_iter;
238         OCStackResult ret;
239         OCRepPayload *payload;
240
241         g_variant_iter_init(&obs_iter, observers);
242         obs_length = g_variant_iter_n_children(&obs_iter);
243
244         /* Variable-length Array */
245         OCObservationId obs_ids[obs_length];
246
247         for (i = 0; i < obs_length; i++)
248                 g_variant_iter_loop(&obs_iter, "i", &obs_ids[i]);
249
250         g_variant_iter_init(&msg_iter, msg);
251         g_variant_iter_loop(&msg_iter, "(iv)", &error_code, &repr_gvar);
252         /* TODO : How to use error_code. */
253
254         payload = icd_payload_repr_from_gvariant(repr_gvar);
255
256         icd_ioty_csdk_lock();
257         /* TODO : QoS is come from lib. */
258         ret = OCNotifyListOfObservers(handle, obs_ids, obs_length, payload, OC_LOW_QOS);
259         icd_ioty_csdk_unlock();
260
261         if (OC_STACK_NO_OBSERVERS == ret) {
262                 WARN("No Observers. Stop Notifying");
263                 return IOTCON_ERROR_NONE;
264         } else if (OC_STACK_OK != ret) {
265                 ERR("OCNotifyListOfObservers() Fail(%d)", ret);
266                 return IOTCON_ERROR_IOTIVITY;
267         }
268
269         return IOTCON_ERROR_NONE;
270 }
271
272
273 int icd_ioty_notify_all(OCResourceHandle handle)
274 {
275         OCStackResult ret;
276
277         icd_ioty_csdk_lock();
278         /* TODO : QoS is come from lib. */
279         ret = OCNotifyAllObservers(handle, OC_LOW_QOS);
280         icd_ioty_csdk_unlock();
281
282         if (OC_STACK_NO_OBSERVERS == ret) {
283                 WARN("No Observers. Stop Notifying");
284                 return IOTCON_ERROR_NONE;
285         } else if (OC_STACK_OK != ret) {
286                 ERR("OCNotifyAllObservers() Fail(%d)", ret);
287                 return IOTCON_ERROR_IOTIVITY;
288         }
289
290         return IOTCON_ERROR_NONE;
291 }
292
293
294 static int _ioty_get_header_options(GVariantIter *src, int src_size,
295                 OCHeaderOption dest[], int dest_size)
296 {
297         int i = 0;
298         char *option_data;
299         unsigned short option_id;
300
301         RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
302
303         if (dest_size < src_size) {
304                 ERR("Exceed Size(%d)", src_size);
305                 return IOTCON_ERROR_INVALID_PARAMETER;
306         }
307
308         while (g_variant_iter_loop(src, "(q&s)", &option_id, &option_data)) {
309                 dest[i].protocolID = OC_COAP_ID;
310                 dest[i].optionID = option_id;
311                 dest[i].optionLength = strlen(option_data) + 1;
312                 memcpy(dest[i].optionData, option_data, dest[i].optionLength);
313                 i++;
314         }
315
316         return IOTCON_ERROR_NONE;
317 }
318
319
320 int icd_ioty_send_response(GVariant *resp)
321 {
322         char *new_uri_path;
323         GVariant *repr_gvar;
324         GVariantIter *options;
325         OCStackResult ret;
326         OCEntityHandlerResponse response = {0};
327         int result, error_code, options_size;
328         int64_t request_handle, resource_handle;
329
330         g_variant_get(resp, "(&sia(qs)ivxx)",
331                         &new_uri_path,
332                         &error_code,
333                         &options,
334                         &result,
335                         &repr_gvar,
336                         &request_handle,
337                         &resource_handle);
338
339         response.requestHandle = ICD_INT64_TO_POINTER(request_handle);
340         response.resourceHandle = ICD_INT64_TO_POINTER(resource_handle);
341         response.ehResult = (OCEntityHandlerResult)result;
342
343         if (OC_EH_RESOURCE_CREATED == response.ehResult)
344                 snprintf(response.resourceUri, sizeof(response.resourceUri), "%s", new_uri_path);
345
346         options_size = g_variant_iter_n_children(options);
347         response.numSendVendorSpecificHeaderOptions = options_size;
348
349         if (0 != options_size) {
350                 int ret= _ioty_get_header_options(options,
351                                 response.numSendVendorSpecificHeaderOptions,
352                                 response.sendVendorSpecificHeaderOptions,
353                                 sizeof(response.sendVendorSpecificHeaderOptions)
354                                 / sizeof(response.sendVendorSpecificHeaderOptions[0]));
355
356                 if (IOTCON_ERROR_NONE != ret)
357                         ERR("_ioty_get_header_options() Fail(%d)", ret);
358         }
359         g_variant_iter_free(options);
360
361         response.payload = (OCPayload*)icd_payload_repr_from_gvariant(repr_gvar);
362
363         /* related to block transfer */
364         response.persistentBufferFlag = 0;
365
366         icd_ioty_csdk_lock();
367         ret = OCDoResponse(&response);
368         icd_ioty_csdk_unlock();
369
370         if (OC_STACK_OK != ret) {
371                 ERR("OCDoResponse() Fail(%d)", ret);
372                 return IOTCON_ERROR_IOTIVITY;
373         }
374
375         return IOTCON_ERROR_NONE;
376 }
377
378
379 static void _ioty_free_signal_context(void *data)
380 {
381         icd_sig_ctx_s *context = data;
382         free(context->bus_name);
383         free(context);
384 }
385
386
387 int icd_ioty_find_resource(const char *host_address, const char *resource_type,
388                 unsigned int signum, const char *bus_name)
389 {
390         int len;
391         OCStackResult result;
392         icd_sig_ctx_s *context;
393         char uri[PATH_MAX] = {0};
394         OCCallbackData cbdata = {0};
395         OCConnectivityType oic_conn_type;
396         iotcon_connectivity_type_e conn_type = IOTCON_CONNECTIVITY_IPV4;
397
398         if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address)) {
399                 len = snprintf(uri, sizeof(uri), "%s", OC_RSRVD_WELL_KNOWN_URI);
400         } else {
401                 len = snprintf(uri, sizeof(uri), ICD_IOTY_COAP"%s%s", host_address,
402                                 OC_RSRVD_WELL_KNOWN_URI);
403         }
404         if (len <= 0 || sizeof(uri) <= len) {
405                 ERR("snprintf() Fail(%d)", len);
406                 return IOTCON_ERROR_UNKNOWN;
407         }
408
409         if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
410                 snprintf(uri + len, sizeof(uri), "?rt=%s", resource_type);
411
412         context = calloc(1, sizeof(icd_sig_ctx_s));
413         if (NULL == context) {
414                 ERR("calloc() Fail(%d)", errno);
415                 return IOTCON_ERROR_OUT_OF_MEMORY;
416         }
417
418         context->bus_name = ic_utils_strdup(bus_name);
419         context->signum = signum;
420
421         cbdata.context = context;
422         cbdata.cb = icd_ioty_ocprocess_find_cb;
423         cbdata.cd = _ioty_free_signal_context;
424
425         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
426
427         icd_ioty_csdk_lock();
428         /* TODO : QoS is come from lib. */
429         result = OCDoResource(NULL, OC_REST_DISCOVER, uri, NULL, NULL, oic_conn_type,
430                         OC_LOW_QOS, &cbdata, NULL, 0);
431         icd_ioty_csdk_unlock();
432
433         if (OC_STACK_OK != result) {
434                 ERR("OCDoResource() Fail(%d)", result);
435                 free(context->bus_name);
436                 free(context);
437                 return IOTCON_ERROR_IOTIVITY;
438         }
439
440         return IOTCON_ERROR_NONE;
441 }
442
443
444 /*
445  * returned string SHOULD be released by you
446  */
447 static char* _icd_ioty_resource_generate_uri(char *uri_path, GVariant *query)
448 {
449         int len;
450         bool loop_first = true;
451         char *key, *value;
452         GVariantIter query_iter;
453         char uri_buf[PATH_MAX] = {0};
454
455         len = snprintf(uri_buf, sizeof(uri_buf), "%s", uri_path);
456
457         /* remove suffix '/' */
458         if ('/' == uri_buf[strlen(uri_buf) - 1]) {
459                 uri_buf[strlen(uri_buf) - 1] = '\0';
460                 len--;
461         }
462
463         g_variant_iter_init(&query_iter, query);
464
465         while (g_variant_iter_loop(&query_iter, "(&s&s)", &key, &value)) {
466                 int query_len;
467
468                 DBG("query exist. key(%s), value(%s)", key, value);
469
470                 if (true == loop_first) {
471                         query_len = snprintf(uri_buf + len, sizeof(uri_buf), "?%s=%s", key, value);
472                         loop_first = false;
473                 } else {
474                         query_len = snprintf(uri_buf + len, sizeof(uri_buf), "&%s=%s", key, value);
475                 }
476
477                 len += query_len;
478         }
479
480         return strdup(uri_buf);
481 }
482
483
484 void icd_ioty_complete(int type, GDBusMethodInvocation *invocation, GVariant *value)
485 {
486         switch(type) {
487         case ICD_CRUD_GET:
488                 ic_dbus_complete_get(icd_dbus_get_object(), invocation, value);
489                 break;
490         case ICD_CRUD_PUT:
491                 ic_dbus_complete_put(icd_dbus_get_object(), invocation, value);
492                 break;
493         case ICD_CRUD_POST:
494                 ic_dbus_complete_post(icd_dbus_get_object(), invocation, value);
495                 break;
496         case ICD_CRUD_DELETE:
497                 ic_dbus_complete_delete(icd_dbus_get_object(), invocation, value);
498                 break;
499         }
500 }
501
502
503 void icd_ioty_complete_error(int type, GDBusMethodInvocation *invocation, int ret_val)
504 {
505         GVariant *value;
506
507         switch (type) {
508         case ICD_CRUD_GET:
509                 value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
510                 ic_dbus_complete_get(icd_dbus_get_object(), invocation, value);
511                 break;
512         case ICD_CRUD_PUT:
513                 value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
514                 ic_dbus_complete_put(icd_dbus_get_object(), invocation, value);
515                 break;
516         case ICD_CRUD_POST:
517                 value = g_variant_new("(a(qs)vi)", NULL, NULL, ret_val);
518                 ic_dbus_complete_post(icd_dbus_get_object(), invocation, value);
519                 break;
520         case ICD_CRUD_DELETE:
521                 value = g_variant_new("(a(qs)i)", NULL, ret_val);
522                 ic_dbus_complete_delete(icd_dbus_get_object(), invocation, value);
523                 break;
524         }
525 }
526
527
528 static gboolean _icd_ioty_crud(int type, icDbus *object, GDBusMethodInvocation *invocation,
529                 GVariant *resource, GVariant *query, GVariant *repr)
530 {
531         bool is_secure;
532         OCMethod rest_type;
533         OCStackResult result;
534         GVariantIter *options;
535         OCCallbackData cbdata = {0};
536         int conn_type, options_size;
537         char *uri_path, *host, *uri, *dev_host, *ptr;
538         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
539         OCHeaderOption *oic_options_ptr = NULL;
540         OCPayload *payload = NULL;
541         OCConnectivityType oic_conn_type;
542         OCDevAddr dev_addr = {0};
543
544         switch (type) {
545         case ICD_CRUD_GET:
546                 cbdata.cb = icd_ioty_ocprocess_get_cb;
547                 rest_type = OC_REST_GET;
548                 break;
549         case ICD_CRUD_PUT:
550                 cbdata.cb = icd_ioty_ocprocess_put_cb;
551                 rest_type = OC_REST_PUT;
552                 break;
553         case ICD_CRUD_POST:
554                 cbdata.cb = icd_ioty_ocprocess_post_cb;
555                 rest_type = OC_REST_POST;
556                 break;
557         case ICD_CRUD_DELETE:
558                 cbdata.cb = icd_ioty_ocprocess_delete_cb;
559                 rest_type = OC_REST_DELETE;
560                 break;
561         default:
562                 ERR("Invalid CRUD Type(%d)", type);
563                 return FALSE;
564         }
565
566         g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure, &options,
567                         &conn_type);
568
569         switch (type) {
570         case ICD_CRUD_GET:
571         case ICD_CRUD_PUT:
572         case ICD_CRUD_POST:
573                 uri = _icd_ioty_resource_generate_uri(uri_path, query);
574                 if (NULL == uri) {
575                         ERR("_icd_ioty_resource_generate_uri() Fail");
576                         g_variant_iter_free(options);
577                         icd_ioty_complete_error(type, invocation, IOTCON_ERROR_INVALID_PARAMETER);
578                         return TRUE;
579                 }
580                 break;
581         case ICD_CRUD_DELETE:
582                 uri = strdup(uri_path);
583                 break;
584         }
585
586         cbdata.context = invocation;
587
588         options_size = g_variant_iter_n_children(options);
589         if (0 != options_size) {
590                 int ret = _ioty_get_header_options(options, options_size, oic_options,
591                                 sizeof(oic_options) / sizeof(oic_options[0]));
592                 if (IOTCON_ERROR_NONE != ret) {
593                         ERR("_ioty_get_header_options() Fail(%d)", ret);
594                         free(uri);
595                         g_variant_iter_free(options);
596                         icd_ioty_complete_error(type, invocation, ret);
597                         return TRUE;
598                 }
599                 oic_options_ptr = oic_options;
600         }
601         g_variant_iter_free(options);
602
603         if (repr)
604                 payload = (OCPayload*)icd_payload_repr_from_gvariant(repr);
605
606         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
607
608         icd_ioty_conn_type_to_oic_transport_type(conn_type, &dev_addr.adapter,
609                         &dev_addr.flags);
610
611         switch (conn_type) {
612         case IOTCON_CONNECTIVITY_IPV4:
613                 dev_host = strtok_r(host, ":", &ptr);
614                 snprintf(dev_addr.addr, sizeof(dev_addr.addr), "%s", dev_host);
615                 dev_addr.port = atoi(strtok_r(NULL, ":", &ptr));
616                 break;
617         case IOTCON_CONNECTIVITY_IPV6:
618                 dev_host = strtok_r(host, "]", &ptr);
619                 snprintf(dev_addr.addr, sizeof(dev_addr.addr), "%s", dev_host);
620                 dev_addr.port = atoi(strtok_r(NULL, "]", &ptr));
621                 break;
622         default:
623                 ERR("Invalid Connectivitiy Type");
624                 icd_ioty_complete_error(type, invocation, IOTCON_ERROR_IOTIVITY);
625                 return TRUE;
626         }
627
628         icd_ioty_csdk_lock();
629         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
630         result = OCDoResource(NULL, rest_type, uri, &dev_addr, payload, oic_conn_type,
631                         OC_LOW_QOS, &cbdata, oic_options_ptr, options_size);
632         icd_ioty_csdk_unlock();
633
634         free(uri);
635
636         if (OC_STACK_OK != result) {
637                 ERR("OCDoResource() Fail(%d)", result);
638                 icd_ioty_complete_error(type, invocation, IOTCON_ERROR_IOTIVITY);
639                 return TRUE;
640         }
641
642         return TRUE;
643 }
644
645 gboolean icd_ioty_get(icDbus *object, GDBusMethodInvocation *invocation,
646                 GVariant *resource, GVariant *query)
647 {
648         return _icd_ioty_crud(ICD_CRUD_GET, object, invocation, resource, query, NULL);
649 }
650
651
652 gboolean icd_ioty_put(icDbus *object, GDBusMethodInvocation *invocation,
653                 GVariant *resource, GVariant *repr, GVariant *query)
654 {
655         return _icd_ioty_crud(ICD_CRUD_PUT, object, invocation, resource, query, repr);
656 }
657
658
659 gboolean icd_ioty_post(icDbus *object, GDBusMethodInvocation *invocation,
660                 GVariant *resource, GVariant *repr, GVariant *query)
661 {
662         return _icd_ioty_crud(ICD_CRUD_POST, object, invocation, resource, query, repr);
663 }
664
665
666 gboolean icd_ioty_delete(icDbus *object, GDBusMethodInvocation *invocation,
667                 GVariant *resource)
668 {
669         return _icd_ioty_crud(ICD_CRUD_DELETE, object, invocation, resource, NULL, NULL);
670 }
671
672
673 OCDoHandle icd_ioty_observer_start(GVariant *resource, int observe_type, GVariant *query,
674                 unsigned int signal_number, const char *bus_name)
675 {
676         bool is_secure;
677         OCMethod method;
678         OCDoHandle handle;
679         OCStackResult result;
680         GVariantIter *options;
681         icd_sig_ctx_s *context;
682         OCCallbackData cbdata = {0};
683         int conn_type, options_size;
684         char *uri_path, *host, *uri, *dev_host, *ptr;
685         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
686         OCHeaderOption *oic_options_ptr = NULL;
687         OCConnectivityType oic_conn_type;
688         OCDevAddr dev_addr = {0};
689
690         g_variant_get(resource, "(&s&sba(qs)i)", &uri_path, &host, &is_secure,  &options,
691                         &conn_type);
692
693         uri = _icd_ioty_resource_generate_uri(uri_path, query);
694         if (NULL == uri) {
695                 ERR("_icd_ioty_resource_generate_uri() Fail");
696                 g_variant_iter_free(options);
697                 return NULL;
698         }
699
700         if (IOTCON_OBSERVE == observe_type)
701                 method = OC_REST_OBSERVE;
702         else if (IOTCON_OBSERVE_ALL == observe_type)
703                 method = OC_REST_OBSERVE_ALL;
704         else
705                 method = OC_REST_OBSERVE_ALL;
706
707         context = calloc(1, sizeof(icd_sig_ctx_s));
708         if (NULL == context) {
709                 ERR("calloc() Fail(%d)", errno);
710                 return NULL;
711         }
712         context->bus_name = ic_utils_strdup(bus_name);
713         context->signum = signal_number;
714
715         cbdata.context = context;
716         cbdata.cb = icd_ioty_ocprocess_observe_cb;
717         cbdata.cd = _ioty_free_signal_context;
718
719         options_size = g_variant_iter_n_children(options);
720         if (0 != options_size) {
721                 int ret = _ioty_get_header_options(options, options_size, oic_options,
722                                 sizeof(oic_options) / sizeof(oic_options[0]));
723                 if (IOTCON_ERROR_NONE != ret) {
724                         ERR("_ioty_get_header_options() Fail(%d)", ret);
725                         free(context->bus_name);
726                         free(context);
727                         free(uri);
728                         g_variant_iter_free(options);
729                         return NULL;
730                 }
731                 oic_options_ptr = oic_options;
732         }
733         g_variant_iter_free(options);
734
735         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
736
737         icd_ioty_conn_type_to_oic_transport_type(conn_type, &dev_addr.adapter,
738                         &dev_addr.flags);
739
740         switch (conn_type) {
741         case IOTCON_CONNECTIVITY_IPV4:
742                 dev_host = strtok_r(host, ":", &ptr);
743                 snprintf(dev_addr.addr, sizeof(dev_addr.addr), "%s", dev_host);
744                 dev_addr.port = atoi(strtok_r(NULL, ":", &ptr));
745                 break;
746         case IOTCON_CONNECTIVITY_IPV6:
747                 dev_host = strtok_r(host, "]", &ptr);
748                 snprintf(dev_addr.addr, sizeof(dev_addr.addr), "%s", dev_host);
749                 dev_addr.port = atoi(strtok_r(NULL, "]", &ptr));
750                 break;
751         default:
752                 ERR("Invalid Connectivitiy Type");
753                 return NULL;
754         }
755
756         icd_ioty_csdk_lock();
757         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
758         result = OCDoResource(&handle, method, uri, &dev_addr, NULL, oic_conn_type,
759                         OC_LOW_QOS, &cbdata, oic_options_ptr, options_size);
760         icd_ioty_csdk_unlock();
761         free(uri);
762         if (OC_STACK_OK != result) {
763                 ERR("OCDoResource() Fail(%d)", result);
764                 free(context->bus_name);
765                 free(context);
766                 return NULL;
767         }
768
769         return handle;
770 }
771
772
773 int icd_ioty_observer_stop(OCDoHandle handle, GVariant *options)
774 {
775         int options_size;
776         OCStackResult ret;
777         GVariantIter options_iter;
778         OCHeaderOption oic_options[MAX_HEADER_OPTIONS];
779         OCHeaderOption *oic_options_ptr = NULL;
780
781         g_variant_iter_init(&options_iter, options);
782
783         options_size = g_variant_iter_n_children(&options_iter);
784         if (0 != options_size) {
785                 int ret = _ioty_get_header_options(&options_iter, options_size, oic_options,
786                                 sizeof(oic_options) / sizeof(oic_options[0]));
787                 if (IOTCON_ERROR_NONE != ret) {
788                         ERR("_ioty_get_header_options() Fail(%d)", ret);
789                         return ret;
790                 }
791                 oic_options_ptr = oic_options;
792         }
793
794         icd_ioty_csdk_lock();
795         ret = OCCancel(handle, OC_HIGH_QOS, oic_options_ptr, options_size);
796         icd_ioty_csdk_unlock();
797         if (OC_STACK_OK != ret) {
798                 ERR("OCCancel() Fail(%d)", ret);
799                 return IOTCON_ERROR_IOTIVITY;
800         }
801
802         return IOTCON_ERROR_NONE;
803 }
804
805
806 int icd_ioty_register_device_info(GVariant *value)
807 {
808         OCStackResult result;
809         OCDeviceInfo device_info = {0};
810
811         g_variant_get(value, "(&s)", &device_info.deviceName);
812
813         icd_ioty_csdk_lock();
814         result = OCSetDeviceInfo(device_info);
815         icd_ioty_csdk_unlock();
816
817         if (OC_STACK_OK != result) {
818                 ERR("OCSetDeviceInfo() Fail(%d)", result);
819                 return IOTCON_ERROR_IOTIVITY;
820         }
821
822         return IOTCON_ERROR_NONE;
823 }
824
825
826 int icd_ioty_register_platform_info(GVariant *value)
827 {
828         OCStackResult result;
829         OCPlatformInfo platform_info = {0};
830
831         g_variant_get(value, "(&s&s&s&s&s&s&s&s&s&s&s)",
832                         &platform_info.platformID,
833                         &platform_info.manufacturerName,
834                         &platform_info.manufacturerUrl,
835                         &platform_info.modelNumber,
836                         &platform_info.dateOfManufacture,
837                         &platform_info.platformVersion,
838                         &platform_info.operatingSystemVersion,
839                         &platform_info.hardwareVersion,
840                         &platform_info.firmwareVersion,
841                         &platform_info.supportUrl,
842                         &platform_info.systemTime);
843
844         icd_ioty_csdk_lock();
845         result = OCSetPlatformInfo(platform_info);
846         icd_ioty_csdk_unlock();
847
848         if (OC_STACK_OK != result) {
849                 ERR("OCSetPlatformInfo() Fail(%d)", result);
850                 return IOTCON_ERROR_IOTIVITY;
851         }
852
853         return IOTCON_ERROR_NONE;
854 }
855
856
857 int icd_ioty_get_info(int type, const char *host_address, unsigned int signal_number,
858                 const char *bus_name)
859 {
860         OCStackResult result;
861         icd_sig_ctx_s *context;
862         OCCallbackData cbdata = {0};
863         char uri[PATH_MAX] = {0};
864         char *uri_path = NULL;
865         iotcon_connectivity_type_e conn_type = IOTCON_CONNECTIVITY_IPV4;
866         OCConnectivityType oic_conn_type;
867
868         if (ICD_DEVICE_INFO == type)
869                 uri_path = OC_RSRVD_DEVICE_URI;
870         else if (ICD_PLATFORM_INFO == type)
871                 uri_path = OC_RSRVD_PLATFORM_URI;
872         else
873                 return IOTCON_ERROR_INVALID_PARAMETER;
874
875         if (IC_STR_EQUAL == strcmp(IOTCON_MULTICAST_ADDRESS, host_address))
876                 snprintf(uri, sizeof(uri), "%s", uri_path);
877         else
878                 snprintf(uri, sizeof(uri), "%s%s", host_address, uri_path);
879
880         context = calloc(1, sizeof(icd_sig_ctx_s));
881         if (NULL == context) {
882                 ERR("calloc() Fail(%d)", errno);
883                 return IOTCON_ERROR_OUT_OF_MEMORY;
884         }
885         context->bus_name = ic_utils_strdup(bus_name);
886         context->signum = signal_number;
887
888         cbdata.context = context;
889         cbdata.cb = icd_ioty_ocprocess_info_cb;
890         cbdata.cd = _ioty_free_signal_context;
891
892         oic_conn_type = icd_ioty_conn_type_to_oic_conn_type(conn_type);
893
894         icd_ioty_csdk_lock();
895         /* TODO : QoS is come from lib. And user can set QoS to client structure.  */
896         result = OCDoResource(NULL, OC_REST_DISCOVER, uri, NULL, NULL, oic_conn_type,
897                         OC_LOW_QOS, &cbdata, NULL, 0);
898         icd_ioty_csdk_unlock();
899
900         if (OC_STACK_OK != result) {
901                 ERR("OCDoResource() Fail(%d)", result);
902                 free(context->bus_name);
903                 free(context);
904                 return IOTCON_ERROR_IOTIVITY;
905         }
906
907         return IOTCON_ERROR_NONE;
908 }
909
910
911 OCDoHandle icd_ioty_subscribe_presence(const char *host_address,
912                 const char *resource_type, unsigned int signal_number, const char *bus_name)
913 {
914         int len;
915         OCDoHandle handle;
916         OCStackResult result;
917         char uri[PATH_MAX] = {0};
918         OCCallbackData cbdata = {0};
919         icd_sig_ctx_s *context;
920
921         len = snprintf(uri, sizeof(uri), "%s%s", host_address, OC_RSRVD_PRESENCE_URI);
922         if (len <= 0 || sizeof(uri) <= len) {
923                 ERR("snprintf() Fail(%d)", len);
924                 return NULL;
925         }
926
927         if (IC_STR_EQUAL != strcmp(IC_STR_NULL, resource_type))
928                 snprintf(uri + len, sizeof(uri), "?rt=%s", resource_type);
929
930         context = calloc(1, sizeof(icd_sig_ctx_s));
931         if (NULL == context) {
932                 ERR("calloc() Fail(%d)", errno);
933                 return NULL;
934         }
935         context->bus_name = ic_utils_strdup(bus_name);
936         context->signum = signal_number;
937
938         cbdata.context = context;
939         cbdata.cb = icd_ioty_ocprocess_presence_cb;
940         cbdata.cd = _ioty_free_signal_context;
941
942         /* In case of IPV4 or IPV6, connectivity type is CT_ADAPTER_IP in iotivity 0.9.2 */
943         icd_ioty_csdk_lock();
944         result = OCDoResource(&handle, OC_REST_PRESENCE, uri, NULL, NULL, CT_ADAPTER_IP,
945                         OC_LOW_QOS, &cbdata, NULL, 0);
946         icd_ioty_csdk_unlock();
947
948         if (OC_STACK_OK != result) {
949                 ERR("OCDoResource() Fail(%d)", result);
950                 free(context->bus_name);
951                 free(context);
952                 return NULL;
953         }
954         return handle;
955 }
956
957
958 int icd_ioty_unsubscribe_presence(OCDoHandle handle)
959 {
960         OCStackResult ret;
961
962         icd_ioty_csdk_lock();
963         ret = OCCancel(handle, OC_LOW_QOS, NULL, 0);
964         icd_ioty_csdk_unlock();
965         if (OC_STACK_OK != ret) {
966                 ERR("OCCancel() Fail(%d)", ret);
967                 return IOTCON_ERROR_IOTIVITY;
968         }
969
970         return IOTCON_ERROR_NONE;
971 }
972
973
974 int icd_ioty_start_presence(unsigned int time_to_live)
975 {
976         OCStackResult ret;
977
978         icd_ioty_csdk_lock();
979         ret = OCStartPresence(time_to_live);
980         icd_ioty_csdk_unlock();
981         if (OC_STACK_OK != ret) {
982                 ERR("OCStartPresence() Fail(%d)", ret);
983                 return IOTCON_ERROR_IOTIVITY;
984         }
985
986         return IOTCON_ERROR_NONE;
987 }
988
989
990 int icd_ioty_stop_presence()
991 {
992         OCStackResult ret;
993
994         icd_ioty_csdk_lock();
995         ret = OCStopPresence();
996         icd_ioty_csdk_unlock();
997         if (OC_STACK_OK != ret) {
998                 ERR("OCStopPresence() Fail(%d)", ret);
999                 return IOTCON_ERROR_IOTIVITY;
1000         }
1001
1002         return IOTCON_ERROR_NONE;
1003 }