Imported Upstream version 1.0.0
[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 #ifdef TCP_ADAPTER
38 #include "catcpadapter.h"
39 #endif
40
41 CAGlobals_t caglobals = { 0 };
42
43 #define TAG "CA_CONN_MGR"
44
45 static bool g_isInitialized = false;
46
47 #ifdef __WITH_DTLS__
48 // CAAdapterNetDTLS will register the callback.
49 // Taking callback all the way through adapters not the right approach, hence calling here.
50 extern void CADTLSSetCredentialsCallback(CAGetDTLSPskCredentialsHandler credCallback);
51 #endif
52
53 #ifdef __WITH_X509__
54 // CAAdapterNetDTLS will register the callback.
55 // Taking callback all the way through adapters not the right approach, hence calling here.
56 extern void CADTLSSetX509CredentialsCallback(CAGetDTLSX509CredentialsHandler credCallback);
57 extern void CADTLSSetCrlCallback(CAGetDTLSCrlHandler crlCallback);
58 #endif
59
60 CAResult_t CAInitialize()
61 {
62     OIC_LOG(DEBUG, TAG, "CAInitialize");
63
64     if (!g_isInitialized)
65     {
66         CAResult_t res = CAInitializeMessageHandler();
67         if (res != CA_STATUS_OK)
68         {
69             OIC_LOG(ERROR, TAG, "CAInitialize has failed");
70             return res;
71         }
72         g_isInitialized = true;
73     }
74     return CA_STATUS_OK;
75 }
76
77 void CATerminate()
78 {
79     OIC_LOG(DEBUG, TAG, "CATerminate");
80
81     if (g_isInitialized)
82     {
83         CATerminateMessageHandler();
84         CATerminateNetworkType();
85
86         g_isInitialized = false;
87     }
88 }
89
90 CAResult_t CAStartListeningServer()
91 {
92     OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
93
94     if(!g_isInitialized)
95     {
96         return CA_STATUS_NOT_INITIALIZED;
97     }
98
99     return CAStartListeningServerAdapters();
100 }
101
102 CAResult_t CAStopListeningServer()
103 {
104     OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
105
106     if(!g_isInitialized)
107     {
108         return CA_STATUS_NOT_INITIALIZED;
109     }
110
111     return CAStopListeningServerAdapters();
112 }
113
114 CAResult_t CAStartDiscoveryServer()
115 {
116     OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
117
118     if(!g_isInitialized)
119     {
120         return CA_STATUS_NOT_INITIALIZED;
121     }
122
123     return CAStartDiscoveryServerAdapters();
124 }
125
126 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
127                        CAErrorCallback ErrorHandler)
128 {
129     OIC_LOG(DEBUG, TAG, "CARegisterHandler");
130
131     if(!g_isInitialized)
132     {
133         OIC_LOG(DEBUG, TAG, "CA is not initialized");
134         return;
135     }
136
137     CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
138 }
139
140 #ifdef __WITH_DTLS__
141 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentialsHandler)
142 {
143     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
144
145     if(!g_isInitialized)
146     {
147         return CA_STATUS_NOT_INITIALIZED;
148     }
149
150     CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
151     return CA_STATUS_OK;
152 }
153 #endif //__WITH_DTLS__
154
155 #ifdef __WITH_X509__
156 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
157 {
158     OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
159
160     if(!g_isInitialized)
161     {
162         return CA_STATUS_NOT_INITIALIZED;
163     }
164
165     CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
166     return CA_STATUS_OK;
167 }
168
169 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
170 {
171     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
172
173     if(!g_isInitialized)
174     {
175         return CA_STATUS_NOT_INITIALIZED;
176     }
177
178     CADTLSSetCrlCallback(GetDTLSCrlHandler);
179     return CA_STATUS_OK;
180 }
181 #endif //__WITH_X509__
182
183 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
184                             CATransportAdapter_t adapter,
185                             const char *addr,
186                             uint16_t port,
187                             CAEndpoint_t **object)
188 {
189     if (!object)
190     {
191         OIC_LOG(ERROR, TAG, "Invalid Parameter");
192         return CA_STATUS_INVALID_PARAM;
193     }
194
195     CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
196     if (!endpoint)
197     {
198         return CA_STATUS_FAILED;
199     }
200     *object = endpoint;
201     return CA_STATUS_OK;
202 }
203
204 void CADestroyEndpoint(CAEndpoint_t *rep)
205 {
206     OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
207
208     CAFreeEndpoint(rep);
209 }
210
211 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
212 {
213     OIC_LOG(DEBUG, TAG, "CAGenerateToken");
214
215     return CAGenerateTokenInternal(token, tokenLength);
216 }
217
218 void CADestroyToken(CAToken_t token)
219 {
220     OIC_LOG(DEBUG, TAG, "CADestroyToken");
221
222     CADestroyTokenInternal(token);
223
224     OIC_LOG(DEBUG, TAG, "OUT");
225 }
226
227 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
228 {
229     OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
230
231     if(!g_isInitialized)
232     {
233         return CA_STATUS_NOT_INITIALIZED;
234     }
235
236     return CAGetNetworkInformationInternal(info, size);
237 }
238
239 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
240 {
241     OIC_LOG(DEBUG, TAG, "CASendGetRequest");
242
243     if(!g_isInitialized)
244     {
245         return CA_STATUS_NOT_INITIALIZED;
246     }
247
248     return CADetachRequestMessage(object, requestInfo);
249 }
250
251 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
252 {
253     OIC_LOG(DEBUG, TAG, "CASendResponse");
254
255     if(!g_isInitialized)
256     {
257         return CA_STATUS_NOT_INITIALIZED;
258     }
259
260     return CADetachResponseMessage(object, responseInfo);
261 }
262
263 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
264 {
265     OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
266
267     if(!g_isInitialized)
268     {
269         return CA_STATUS_NOT_INITIALIZED;
270     }
271
272     CAResult_t res = CA_STATUS_OK;
273
274     if (interestedNetwork & CA_ADAPTER_IP)
275     {
276         res = CAAddNetworkType(CA_ADAPTER_IP);
277         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
278     }
279     else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
280     {
281         res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
282         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
283     }
284     else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
285     {
286         res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
287         OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
288     }
289
290 #ifdef RA_ADAPTER
291     else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
292     {
293         res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
294         OIC_LOG_V(DEBUG, TAG,
295                   "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
296     }
297 #endif
298
299 #ifdef TCP_ADAPTER
300     else if (interestedNetwork & CA_ADAPTER_TCP)
301     {
302         res = CAAddNetworkType(CA_ADAPTER_TCP);
303         OIC_LOG_V(DEBUG, TAG,
304                   "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
305     }
306 #endif
307
308     else
309     {
310         res = CA_NOT_SUPPORTED;
311     }
312     return res;
313 }
314
315 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
316 {
317     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
318
319     if(!g_isInitialized)
320     {
321         return CA_STATUS_NOT_INITIALIZED;
322     }
323
324     CAResult_t res = CA_STATUS_OK;
325
326     if (nonInterestedNetwork & CA_ADAPTER_IP)
327     {
328         res = CARemoveNetworkType(CA_ADAPTER_IP);
329         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
330     }
331     else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
332     {
333         res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
334         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
335     }
336     else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
337     {
338         res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
339         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
340     }
341 #ifdef RA_ADAPTER
342     else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
343     {
344         res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
345         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
346                   res);
347     }
348 #endif
349
350
351 #ifdef TCP_ADAPTER
352     else if (nonInterestedNetwork & CA_ADAPTER_TCP)
353     {
354         res = CARemoveNetworkType(CA_ADAPTER_TCP);
355         OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
356                   res);
357     }
358 #endif
359
360     else
361     {
362         res = CA_STATUS_FAILED;
363     }
364     return res;
365 }
366
367 CAResult_t CAHandleRequestResponse()
368 {
369     if (!g_isInitialized)
370     {
371         OIC_LOG(ERROR, TAG, "not initialized");
372         return CA_STATUS_NOT_INITIALIZED;
373     }
374
375     CAHandleRequestResponseCallbacks();
376
377     return CA_STATUS_OK;
378 }
379
380 #ifdef __WITH_DTLS__
381
382 CAResult_t CASelectCipherSuite(const uint16_t cipher)
383 {
384     OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
385
386     return CADtlsSelectCipherSuite(cipher);
387 }
388
389 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
390 {
391     OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
392
393     return CADtlsEnableAnonECDHCipherSuite(enable);
394 }
395
396 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
397                     const uint8_t* label, const size_t labelLen,
398                     const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
399                     const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
400                     uint8_t* ownerPSK, const size_t ownerPSKSize)
401 {
402     OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
403
404     CAResult_t res = CA_STATUS_OK;
405
406     //newOwnerLabel and prevOwnerLabe can be NULL
407     if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
408     {
409         return CA_STATUS_INVALID_PARAM;
410     }
411
412     res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
413                                   rsrcServerDeviceID, rsrcServerDeviceIDLen,
414                                   provServerDeviceID, provServerDeviceIDLen,
415                                   ownerPSK, ownerPSKSize);
416     if (CA_STATUS_OK != res)
417     {
418         OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
419     }
420
421     OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
422
423     return res;
424 }
425
426 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
427 {
428     OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
429     CAResult_t res = CA_STATUS_OK;
430
431     if (!endpoint)
432     {
433         return CA_STATUS_INVALID_PARAM;
434     }
435
436     res = CADtlsInitiateHandshake(endpoint);
437     if (CA_STATUS_OK != res)
438     {
439         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
440     }
441
442     OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
443
444     return res;
445 }
446
447 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
448 {
449     OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
450     CAResult_t res = CA_STATUS_OK;
451
452     if (!endpoint)
453     {
454         return CA_STATUS_INVALID_PARAM;
455     }
456
457     res = CADtlsClose(endpoint);
458     if (CA_STATUS_OK != res)
459     {
460         OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
461     }
462
463     OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
464
465     return res;
466 }
467
468 #endif /* __WITH_DTLS__ */