modify "is_observable->properties", & remove tizen_info
[platform/core/iot/iotcon.git] / daemon / icd-ioty-type.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 <errno.h>
19 #include <limits.h>
20
21 #include <octypes.h>
22
23 #include "iotcon-types.h"
24 #include "ic-utils.h"
25 #include "icd.h"
26 #include "icd-ioty-type.h"
27
28 OCConnectivityType icd_ioty_conn_type_to_oic_conn_type(int conn_type)
29 {
30         OCConnectivityType oic_conn_type = CT_DEFAULT;
31
32         switch (conn_type) {
33         case IOTCON_CONNECTIVITY_IPV4:
34                 oic_conn_type = CT_IP_USE_V4;
35                 break;
36         case IOTCON_CONNECTIVITY_IPV6:
37                 oic_conn_type = CT_IP_USE_V6;
38                 break;
39         case IOTCON_CONNECTIVITY_BT_EDR:
40                 oic_conn_type = CT_ADAPTER_RFCOMM_BTEDR;
41                 break;
42         case IOTCON_CONNECTIVITY_BT_LE:
43                 oic_conn_type = CT_ADAPTER_GATT_BTLE;
44                 break;
45         case IOTCON_CONNECTIVITY_ALL:
46         default:
47                 oic_conn_type = CT_DEFAULT;
48         }
49
50         return oic_conn_type;
51 }
52
53
54 int icd_ioty_transport_flag_to_conn_type(OCTransportAdapter adapter,
55                 OCTransportFlags flag)
56 {
57         int conn_type = IOTCON_CONNECTIVITY_ALL;
58
59         /* Need to consider to allow various connectivity types */
60         switch (adapter) {
61         case OC_ADAPTER_IP:
62                 if (OC_IP_USE_V4 & flag)
63                         conn_type = IOTCON_CONNECTIVITY_IPV4;
64                 else if (OC_IP_USE_V6 & flag)
65                         conn_type = IOTCON_CONNECTIVITY_IPV6;
66                 break;
67         case OC_ADAPTER_RFCOMM_BTEDR:
68                 conn_type = IOTCON_CONNECTIVITY_BT_EDR;
69                 break;
70         case OC_ADAPTER_GATT_BTLE:
71                 conn_type = IOTCON_CONNECTIVITY_BT_LE;
72                 break;
73         default:
74                 ERR("Invalid Adpater");
75         }
76
77         return conn_type;
78 }
79
80
81 static void _icd_ioty_conn_type_to_oic_transport_type(int conn_type,
82                 OCTransportAdapter *adapter, OCTransportFlags *flag)
83 {
84         switch (conn_type) {
85         case IOTCON_CONNECTIVITY_IPV4:
86                 *adapter = OC_ADAPTER_IP;
87                 *flag = OC_IP_USE_V4;
88                 break;
89         case IOTCON_CONNECTIVITY_IPV6:
90                 *adapter = OC_ADAPTER_IP;
91                 *flag = OC_IP_USE_V6;
92                 break;
93         case IOTCON_CONNECTIVITY_BT_EDR:
94                 *adapter = OC_ADAPTER_RFCOMM_BTEDR;
95                 *flag = OC_DEFAULT_FLAGS;
96                 break;
97         case IOTCON_CONNECTIVITY_BT_LE:
98                 *adapter = OC_ADAPTER_GATT_BTLE;
99                 *flag = OC_DEFAULT_FLAGS;
100                 break;
101         case IOTCON_CONNECTIVITY_ALL:
102         default:
103                 *adapter = OC_DEFAULT_ADAPTER;
104                 *flag = OC_DEFAULT_FLAGS;
105         }
106 }
107
108
109 int icd_ioty_get_dev_addr(const char *host_address, int conn_type, OCDevAddr *dev_addr)
110 {
111         char host[PATH_MAX] = {0};
112         char *dev_host, *ptr = NULL;
113
114         snprintf(host, sizeof(host), "%s", host_address);
115
116         switch (conn_type) {
117         case IOTCON_CONNECTIVITY_IPV4:
118                 dev_host = strtok_r(host, ":", &ptr);
119                 snprintf(dev_addr->addr, sizeof(dev_addr->addr), "%s", dev_host);
120                 dev_addr->port = atoi(strtok_r(NULL, ":", &ptr));
121                 break;
122         case IOTCON_CONNECTIVITY_IPV6:
123                 dev_host = strtok_r(host, "]", &ptr);
124                 snprintf(dev_addr->addr, sizeof(dev_addr->addr), "%s", dev_host);
125                 dev_addr->port = atoi(strtok_r(NULL, "]", &ptr));
126                 break;
127         case IOTCON_CONNECTIVITY_BT_EDR:
128                 snprintf(dev_addr->addr, sizeof(dev_addr->addr), "%s", host);
129                 break;
130         case IOTCON_CONNECTIVITY_BT_LE:
131         default:
132                 ERR("Invalid Connectivity Type(%d)", conn_type);
133                 return IOTCON_ERROR_INVALID_PARAMETER;
134         }
135
136         _icd_ioty_conn_type_to_oic_transport_type(conn_type, &(dev_addr->adapter),
137                         &(dev_addr->flags));
138
139         return IOTCON_ERROR_NONE;
140 }
141
142
143 int icd_ioty_get_host_address(OCDevAddr *dev_addr, char **host_address, int *conn_type)
144 {
145         int connectivity_type;
146         char host_addr[PATH_MAX] = {0};
147
148         connectivity_type = icd_ioty_transport_flag_to_conn_type(dev_addr->adapter,
149                         dev_addr->flags);
150
151         switch (connectivity_type) {
152         case IOTCON_CONNECTIVITY_IPV6:
153                 snprintf(host_addr, sizeof(host_addr), "[%s]:%d", dev_addr->addr,
154                                 dev_addr->port);
155                 break;
156         case IOTCON_CONNECTIVITY_IPV4:
157                 snprintf(host_addr, sizeof(host_addr), "%s:%d", dev_addr->addr,
158                                 dev_addr->port);
159                 break;
160         case IOTCON_CONNECTIVITY_BT_EDR:
161                 snprintf(host_addr, sizeof(host_addr), "%s", dev_addr->addr);
162                 break;
163         case IOTCON_CONNECTIVITY_BT_LE:
164         default:
165                 ERR("Invalid Connectivity Type(%d)", connectivity_type);
166                 return IOTCON_ERROR_INVALID_PARAMETER;
167         }
168
169         *host_address = strdup(host_addr);
170
171         *conn_type = connectivity_type;
172
173         return IOTCON_ERROR_NONE;
174 }
175
176
177 int icd_ioty_oic_properties_to_properties(int oic_properties)
178 {
179         int prop = IOTCON_RESOURCE_NO_PROPERTY;
180
181         if (OC_DISCOVERABLE & oic_properties)
182                 prop |= IOTCON_RESOURCE_DISCOVERABLE;
183
184         if (OC_OBSERVABLE & oic_properties)
185                 prop |= IOTCON_RESOURCE_OBSERVABLE;
186
187         if (OC_ACTIVE & oic_properties)
188                 prop |= IOTCON_RESOURCE_ACTIVE;
189
190         if (OC_SLOW & oic_properties)
191                 prop |= IOTCON_RESOURCE_SLOW;
192
193         if (OC_SECURE & oic_properties)
194                 prop |= IOTCON_RESOURCE_SECURE;
195
196         if (OC_EXPLICIT_DISCOVERABLE & oic_properties)
197                 prop |= IOTCON_RESOURCE_EXPLICIT_DISCOVERABLE;
198
199         return prop;
200 }