2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "iotcon-types.h"
26 #include "icd-ioty-type.h"
28 OCConnectivityType icd_ioty_conn_type_to_oic_conn_type(int conn_type)
30 OCConnectivityType oic_conn_type = CT_DEFAULT;
33 case IOTCON_CONNECTIVITY_IPV4:
34 oic_conn_type = CT_IP_USE_V4;
36 case IOTCON_CONNECTIVITY_IPV6:
37 oic_conn_type = CT_IP_USE_V6;
39 case IOTCON_CONNECTIVITY_BT_EDR:
40 oic_conn_type = CT_ADAPTER_RFCOMM_BTEDR;
42 case IOTCON_CONNECTIVITY_BT_LE:
43 oic_conn_type = CT_ADAPTER_GATT_BTLE;
45 case IOTCON_CONNECTIVITY_ALL:
47 oic_conn_type = CT_DEFAULT;
54 int icd_ioty_transport_flag_to_conn_type(OCTransportAdapter adapter,
55 OCTransportFlags flag)
57 int conn_type = IOTCON_CONNECTIVITY_ALL;
59 /* Need to consider to allow various connectivity types */
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;
67 case OC_ADAPTER_RFCOMM_BTEDR:
68 conn_type = IOTCON_CONNECTIVITY_BT_EDR;
70 case OC_ADAPTER_GATT_BTLE:
71 conn_type = IOTCON_CONNECTIVITY_BT_LE;
74 ERR("Invalid Adpater");
81 static void _icd_ioty_conn_type_to_oic_transport_type(int conn_type,
82 OCTransportAdapter *adapter, OCTransportFlags *flag)
85 case IOTCON_CONNECTIVITY_IPV4:
86 *adapter = OC_ADAPTER_IP;
89 case IOTCON_CONNECTIVITY_IPV6:
90 *adapter = OC_ADAPTER_IP;
93 case IOTCON_CONNECTIVITY_BT_EDR:
94 *adapter = OC_ADAPTER_RFCOMM_BTEDR;
95 *flag = OC_DEFAULT_FLAGS;
97 case IOTCON_CONNECTIVITY_BT_LE:
98 *adapter = OC_ADAPTER_GATT_BTLE;
99 *flag = OC_DEFAULT_FLAGS;
101 case IOTCON_CONNECTIVITY_ALL:
103 *adapter = OC_DEFAULT_ADAPTER;
104 *flag = OC_DEFAULT_FLAGS;
109 int icd_ioty_get_dev_addr(const char *host_address, int conn_type, OCDevAddr *dev_addr)
111 char host[PATH_MAX] = {0};
112 char *dev_host, *ptr = NULL;
114 snprintf(host, sizeof(host), "%s", host_address);
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));
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));
127 case IOTCON_CONNECTIVITY_BT_EDR:
128 snprintf(dev_addr->addr, sizeof(dev_addr->addr), "%s", host);
130 case IOTCON_CONNECTIVITY_BT_LE:
132 ERR("Invalid Connectivity Type(%d)", conn_type);
133 return IOTCON_ERROR_INVALID_PARAMETER;
136 _icd_ioty_conn_type_to_oic_transport_type(conn_type, &(dev_addr->adapter),
139 return IOTCON_ERROR_NONE;
143 int icd_ioty_get_host_address(OCDevAddr *dev_addr, char **host_address, int *conn_type)
145 int connectivity_type;
146 char host_addr[PATH_MAX] = {0};
148 connectivity_type = icd_ioty_transport_flag_to_conn_type(dev_addr->adapter,
151 switch (connectivity_type) {
152 case IOTCON_CONNECTIVITY_IPV6:
153 snprintf(host_addr, sizeof(host_addr), "[%s]:%d", dev_addr->addr,
156 case IOTCON_CONNECTIVITY_IPV4:
157 snprintf(host_addr, sizeof(host_addr), "%s:%d", dev_addr->addr,
160 case IOTCON_CONNECTIVITY_BT_EDR:
161 snprintf(host_addr, sizeof(host_addr), "%s", dev_addr->addr);
163 case IOTCON_CONNECTIVITY_BT_LE:
165 ERR("Invalid Connectivity Type(%d)", connectivity_type);
166 return IOTCON_ERROR_INVALID_PARAMETER;
169 *host_address = strdup(host_addr);
171 *conn_type = connectivity_type;
173 return IOTCON_ERROR_NONE;
177 int icd_ioty_oic_properties_to_properties(int oic_properties)
179 int prop = IOTCON_RESOURCE_NO_PROPERTY;
181 if (OC_DISCOVERABLE & oic_properties)
182 prop |= IOTCON_RESOURCE_DISCOVERABLE;
184 if (OC_OBSERVABLE & oic_properties)
185 prop |= IOTCON_RESOURCE_OBSERVABLE;
187 if (OC_ACTIVE & oic_properties)
188 prop |= IOTCON_RESOURCE_ACTIVE;
190 if (OC_SLOW & oic_properties)
191 prop |= IOTCON_RESOURCE_SLOW;
193 if (OC_SECURE & oic_properties)
194 prop |= IOTCON_RESOURCE_SECURE;
196 if (OC_EXPLICIT_DISCOVERABLE & oic_properties)
197 prop |= IOTCON_RESOURCE_EXPLICIT_DISCOVERABLE;