Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caconnectivitymanager.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 <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25
26 #include "cainterface.h"
27 #include "caremotehandler.h"
28 #include "camessagehandler.h"
29 #include "caprotocolmessage.h"
30 #include "canetworkconfigurator.h"
31 #include "cainterfacecontroller.h"
32 #include "logger.h"
33 #ifdef __WITH_DTLS__
34 #include "caadapternetdtls.h"
35 #endif
36
37 CAGlobals_t caglobals;
38
39 #define TAG "CA"
40
41 static bool g_isInitialized = false;
42
43 #ifdef __WITH_DTLS__
44 // CAAdapterNetDTLS will register the callback.
45 // Taking callback all the way through adapters not the right approach, hence calling here.
46 extern void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
47 #endif
48
49 CAResult_t CAInitialize()
50 {
51     OIC_LOG(DEBUG, TAG, "CAInitialize");
52
53     if (!g_isInitialized)
54     {
55         CAResult_t res = CAInitializeMessageHandler();
56         if (res != CA_STATUS_OK)
57         {
58             OIC_LOG(ERROR, TAG, "CAInitialize has failed");
59             return res;
60         }
61         g_isInitialized = true;
62     }
63     return CA_STATUS_OK;
64 }
65
66 void CATerminate()
67 {
68     OIC_LOG(DEBUG, TAG, "CATerminate");
69
70     if (g_isInitialized)
71     {
72         CATerminateMessageHandler();
73         CATerminateNetworkType();
74
75         g_isInitialized = false;
76     }
77 }
78
79 CAResult_t CAStartListeningServer()
80 {
81     OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
82
83     if(!g_isInitialized)
84     {
85         return CA_STATUS_NOT_INITIALIZED;
86     }
87
88     return CAStartListeningServerAdapters();
89 }
90
91 CAResult_t CAStartDiscoveryServer()
92 {
93     OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
94
95     if(!g_isInitialized)
96     {
97         return CA_STATUS_NOT_INITIALIZED;
98     }
99
100     return CAStartDiscoveryServerAdapters();
101 }
102
103 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
104                        CAErrorCallback ErrorHandler)
105 {
106     OIC_LOG(DEBUG, TAG, "CARegisterHandler");
107
108     if(!g_isInitialized)
109     {
110         OIC_LOG(DEBUG, TAG, "CA is not initialized");
111         return;
112     }
113
114     CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
115 }
116
117 #ifdef __WITH_DTLS__
118 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
119 {
120     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
121
122     if(!g_isInitialized)
123     {
124         return CA_STATUS_NOT_INITIALIZED;
125     }
126
127     CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
128     return CA_STATUS_OK;
129 }
130 #endif //__WITH_DTLS__
131
132 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
133                             CATransportAdapter_t adapter,
134                             const char *addr,
135                             uint16_t port,
136                             CAEndpoint_t **object)
137 {
138     if (!object)
139     {
140         OIC_LOG(ERROR, TAG, "Invalid Parameter");
141         return CA_STATUS_INVALID_PARAM;
142     }
143
144     CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
145     if (!endpoint)
146     {
147         return CA_STATUS_FAILED;
148     }
149     *object = endpoint;
150     return CA_STATUS_OK;
151 }
152
153 void CADestroyEndpoint(CAEndpoint_t *rep)
154 {
155     OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
156
157     CAFreeEndpoint(rep);
158 }
159
160 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
161 {
162     OIC_LOG(DEBUG, TAG, "CAGenerateToken");
163
164     if(!g_isInitialized)
165     {
166         return CA_STATUS_NOT_INITIALIZED;
167     }
168     return CAGenerateTokenInternal(token, tokenLength);
169 }
170
171 void CADestroyToken(CAToken_t token)
172 {
173     OIC_LOG(DEBUG, TAG, "CADestroyToken");
174
175     CADestroyTokenInternal(token);
176
177     OIC_LOG(DEBUG, TAG, "OUT");
178 }
179
180 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
181 {
182     OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
183
184     if(!g_isInitialized)
185     {
186         return CA_STATUS_NOT_INITIALIZED;
187     }
188
189     return CAGetNetworkInformationInternal(info, size);
190 }
191
192 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
193 {
194     OIC_LOG(DEBUG, TAG, "CASendGetRequest");
195
196     if(!g_isInitialized)
197     {
198         return CA_STATUS_NOT_INITIALIZED;
199     }
200
201     return CADetachRequestMessage(object, requestInfo);
202 }
203
204 CAResult_t CASendNotification(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
205 {
206     OIC_LOG(DEBUG, TAG, "CASendNotification");
207
208     if(!g_isInitialized)
209     {
210         return CA_STATUS_NOT_INITIALIZED;
211     }
212
213     return CADetachResponseMessage(object, responseInfo);
214
215 }
216
217 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
218 {
219     OIC_LOG(DEBUG, TAG, "CASendResponse");
220
221     if(!g_isInitialized)
222     {
223         return CA_STATUS_NOT_INITIALIZED;
224     }
225
226     return CADetachResponseMessage(object, responseInfo);
227
228 }
229
230 CAResult_t CASelectNetwork(const uint32_t interestedNetwork)
231 {
232     OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
233
234     if(!g_isInitialized)
235     {
236         return CA_STATUS_NOT_INITIALIZED;
237     }
238
239     CAResult_t res = CA_STATUS_OK;
240
241     if (interestedNetwork & CA_ADAPTER_IP)
242     {
243         res = CAAddNetworkType(CA_ADAPTER_IP);
244         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns error : %d", res);
245     }
246     else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
247     {
248         res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
249         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns error : %d", res);
250     }
251     else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
252     {
253         res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
254         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns error : %d", res);
255     }
256     else
257     {
258         res = CA_NOT_SUPPORTED;
259     }
260
261     return res;
262 }
263
264 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork)
265 {
266     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
267
268     if(!g_isInitialized)
269     {
270         return CA_STATUS_NOT_INITIALIZED;
271     }
272
273     CAResult_t res = CA_STATUS_OK;
274
275     if (nonInterestedNetwork & CA_ADAPTER_IP)
276     {
277         res = CARemoveNetworkType(CA_ADAPTER_IP);
278         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns error : %d", res);
279     }
280     else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
281     {
282         res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
283         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns error : %d", res);
284     }
285     else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
286     {
287         res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
288         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns error : %d", res);
289     }
290     else
291     {
292         res = CA_STATUS_FAILED;
293     }
294
295     return res;
296 }
297
298 CAResult_t CAHandleRequestResponse()
299 {
300     if (!g_isInitialized)
301     {
302         OIC_LOG(ERROR, TAG, "not initialized");
303         return CA_STATUS_NOT_INITIALIZED;
304     }
305
306     CAHandleRequestResponseCallbacks();
307
308     return CA_STATUS_OK;
309 }
310
311 #ifdef __WITH_DTLS__
312
313 CAResult_t CASelectCipherSuite(const uint16_t cipher)
314 {
315     OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
316
317     return CADtlsSelectCipherSuite(cipher);
318 }
319
320 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
321 {
322     OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
323
324     return CADtlsEnableAnonECDHCipherSuite(enable);
325 }
326
327 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
328                     const uint8_t* label, const size_t labelLen,
329                     const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
330                     const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
331                     uint8_t* ownerPSK, const size_t ownerPSKSize)
332 {
333     OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
334
335     CAResult_t res = CA_STATUS_OK;
336
337     //newOwnerLabel and prevOwnerLabe can be NULL
338     if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
339     {
340         return CA_STATUS_INVALID_PARAM;
341     }
342
343     res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
344                                   rsrcServerDeviceID, rsrcServerDeviceIDLen,
345                                   provServerDeviceID, provServerDeviceIDLen,
346                                   ownerPSK, ownerPSKSize);
347     if (CA_STATUS_OK != res)
348     {
349         OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
350     }
351
352     OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
353
354     return res;
355 }
356
357 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
358 {
359     OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
360     CAResult_t res = CA_STATUS_OK;
361
362     if (!endpoint)
363     {
364         return CA_STATUS_INVALID_PARAM;
365     }
366
367     res = CADtlsInitiateHandshake(endpoint);
368     if (CA_STATUS_OK != res)
369     {
370         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
371     }
372
373     OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
374
375     return res;
376 }
377
378 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
379 {
380     OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
381     CAResult_t res = CA_STATUS_OK;
382
383     if (!endpoint)
384     {
385         return CA_STATUS_INVALID_PARAM;
386     }
387
388     res = CADtlsClose(endpoint);
389     if (CA_STATUS_OK != res)
390     {
391         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
392     }
393
394     OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
395
396     return res;
397 }
398
399 #endif /* __WITH_DTLS__ */