1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "caadapterutils.h"
25 #include "oic_malloc.h"
27 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
29 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
32 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
33 OICMalloc(sizeof(CALocalConnectivity_t));
36 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
39 memset(info, 0, sizeof(CALocalConnectivity_t));
42 if (address && strlen(address))
46 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
47 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
49 else if (CA_LE == type)
51 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
52 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
54 else if (CA_WIFI == type || CA_ETHERNET == type)
56 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
57 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
64 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
66 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
68 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
69 OICMalloc(sizeof(CALocalConnectivity_t));
72 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
75 memset(info, 0, sizeof(CALocalConnectivity_t));
77 info->type = connectivity->type;
78 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
80 strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
82 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
84 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
86 strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
88 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
90 else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
91 && strlen(connectivity->addressInfo.IP.ipAddress))
93 strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
95 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
96 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
99 info->isSecured = connectivity->isSecured;
103 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
107 OICFree(localEndpoint);
111 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
113 const char *resourceUri)
115 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
116 OICMalloc(sizeof(CARemoteEndpoint_t));
119 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
122 memset(info, 0, sizeof(CARemoteEndpoint_t));
124 info->connectivityType = type;
125 if (address && strlen(address))
129 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
130 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
132 else if (CA_LE == info->connectivityType)
134 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
135 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
137 else if (CA_WIFI == type || CA_ETHERNET == type)
139 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
140 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
144 if (resourceUri && strlen(resourceUri))
146 info->resourceUri = OICStrdup(resourceUri);
152 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
154 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
156 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
157 OICMalloc(sizeof(CARemoteEndpoint_t));
160 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
163 memset(info, 0, sizeof(CARemoteEndpoint_t));
165 info->connectivityType = remoteEndpoint->connectivityType;
166 if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
168 strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
169 CA_MACADDR_SIZE - 1);
170 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
172 else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
174 strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
175 CA_MACADDR_SIZE - 1);
176 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
178 else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
179 && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
181 strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
183 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
184 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
187 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
189 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
192 info->isSecured = remoteEndpoint->isSecured;
196 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
200 if (remoteEndpoint->resourceUri)
202 OICFree(remoteEndpoint->resourceUri);
205 OICFree(remoteEndpoint);