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