Fix enum (IOTCON_OBSERVE -> IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER)
[platform/core/iot/iotcon.git] / common / ic-utils.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 <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <glib.h>
20
21 #include "iotcon-constant.h"
22 #include "ic-common.h"
23 #include "ic-log.h"
24 #include "ic-utils.h"
25
26 char* ic_utils_strdup(const char *src)
27 {
28         char *dest = NULL;
29
30         RETV_IF(NULL == src, NULL);
31
32         errno = 0;
33         dest = strdup(src);
34         if (NULL == dest) {
35                 ERR("strdup() Fail(%d)", errno);
36                 return NULL;
37         }
38
39         return dest;
40 }
41
42
43 const char* ic_utils_dbus_encode_str(const char *src)
44 {
45         return (src) ? src : IC_STR_NULL;
46 }
47
48
49 char* ic_utils_dbus_decode_str(char *src)
50 {
51         RETV_IF(NULL == src, NULL);
52
53         if (IC_STR_EQUAL == strcmp(IC_STR_NULL, src))
54                 return NULL;
55         else
56                 return src;
57 }
58
59
60 int ic_utils_convert_interface_flag(iotcon_interface_e src, char **dest)
61 {
62         switch (src) {
63         case IOTCON_INTERFACE_DEFAULT:
64                 *dest = IC_INTERFACE_DEFAULT;
65                 break;
66         case IOTCON_INTERFACE_LINK:
67                 *dest = IC_INTERFACE_LINK;
68                 break;
69         case IOTCON_INTERFACE_BATCH:
70                 *dest = IC_INTERFACE_BATCH;
71                 break;
72         case IOTCON_INTERFACE_GROUP:
73                 *dest = IC_INTERFACE_GROUP;
74                 break;
75         case IOTCON_INTERFACE_NONE:
76         default:
77                 ERR("Invalid interface(%d)", src);
78                 return IOTCON_ERROR_INVALID_PARAMETER;
79         }
80         return IOTCON_ERROR_NONE;
81 }
82
83
84 int ic_utils_convert_interface_string(const char *src, iotcon_interface_e *dest)
85 {
86         RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
87         RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
88
89         if (IC_STR_EQUAL == strcmp(IC_INTERFACE_DEFAULT, src)) {
90                 *dest = IOTCON_INTERFACE_DEFAULT;
91         } else if (IC_STR_EQUAL == strcmp(IC_INTERFACE_LINK, src)) {
92                 *dest = IOTCON_INTERFACE_LINK;
93         } else if (IC_STR_EQUAL == strcmp(IC_INTERFACE_BATCH, src)) {
94                 *dest = IOTCON_INTERFACE_BATCH;
95         } else if (IC_STR_EQUAL == strcmp(IC_INTERFACE_GROUP, src)) {
96                 *dest = IOTCON_INTERFACE_GROUP;
97         } else {
98                 ERR("Invalid Interface");
99                 return IOTCON_ERROR_INVALID_PARAMETER;
100         }
101
102         return IOTCON_ERROR_NONE;
103 }
104
105
106 void ic_utils_gvariant_array_free(GVariant **value)
107 {
108         int i;
109
110         for (i = 0; value[i]; i++)
111                 g_variant_unref(value[i]);
112
113         free(value);
114 }
115
116