Merge branch 'master' into connectivity-abstraction
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapterutils.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "caadapterutils.h"
22
23 #include <string.h>
24
25 #include "oic_malloc.h"
26
27 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
28
29 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type, const char *address,
30         const char *interfaceName)
31 {
32     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
33                                   OICMalloc(sizeof(CALocalConnectivity_t));
34     if (NULL == info)
35     {
36         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
37         return NULL;
38     }
39     memset(info, 0, sizeof(CALocalConnectivity_t));
40
41     info->type = type;
42     if (address && strlen(address))
43     {
44         if (CA_EDR == type)
45         {
46             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
47             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
48         }
49         else if (CA_LE == type)
50         {
51             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
52             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
53         }
54         else if (CA_WIFI == type || CA_ETHERNET == type)
55         {
56             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
57             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
58         }
59     }
60
61     return info;
62 }
63
64 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
65 {
66     VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
67
68     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
69                                   OICMalloc(sizeof(CALocalConnectivity_t));
70     if (NULL == info)
71     {
72         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
73         return NULL;
74     }
75     memset(info, 0, sizeof(CALocalConnectivity_t));
76
77     info->type = connectivity->type;
78     if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
79     {
80         strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
81                 CA_MACADDR_SIZE - 1);
82         info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
83     }
84     else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
85     {
86         strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
87                 CA_MACADDR_SIZE - 1);
88         info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
89     }
90     else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
91              && strlen(connectivity->addressInfo.IP.ipAddress))
92     {
93         strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
94                 CA_IPADDR_SIZE - 1);
95         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
96     }
97
98     return info;
99 }
100
101 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
102 {
103     if (localEndpoint)
104     {
105         OICFree(localEndpoint);
106     }
107 }
108
109 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type, const char *address,
110         const char *resourceUri)
111 {
112     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
113                                OICMalloc(sizeof(CARemoteEndpoint_t));
114     if (NULL == info)
115     {
116         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
117         return NULL;
118     }
119     memset(info, 0, sizeof(CARemoteEndpoint_t));
120
121     info->connectivityType = type;
122     if (address && strlen(address))
123     {
124         if (CA_EDR == type)
125         {
126             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
127             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
128         }
129         else if (CA_LE == info->connectivityType)
130         {
131             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
132             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
133         }
134         else if (CA_WIFI == type || CA_ETHERNET == type)
135         {
136             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
137             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
138         }
139     }
140
141     if (resourceUri && strlen(resourceUri))
142     {
143         info->resourceUri = OICStrdup(resourceUri);
144     }
145
146     return info;
147 }
148
149 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
150 {
151     VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
152
153     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
154                                OICMalloc(sizeof(CARemoteEndpoint_t));
155     if (NULL == info)
156     {
157         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
158         return NULL;
159     }
160     memset(info, 0, sizeof(CARemoteEndpoint_t));
161
162     info->connectivityType = remoteEndpoint->connectivityType;
163     if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
164     {
165         strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
166                 CA_MACADDR_SIZE - 1);
167         info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
168     }
169     else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
170     {
171         strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
172                 CA_MACADDR_SIZE - 1);
173         info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
174     }
175     else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
176              && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
177     {
178         strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
179                 CA_IPADDR_SIZE - 1);
180         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
181         info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
182     }
183
184     if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
185     {
186         info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
187     }
188
189     return info;
190 }
191
192 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
193 {
194     if (remoteEndpoint)
195     {
196         if (remoteEndpoint->resourceUri)
197         {
198             OICFree(remoteEndpoint->resourceUri);
199         }
200
201         OICFree(remoteEndpoint);
202     }
203 }
204