Merge branch 'remote-access2'
[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(CAGetDTLSCredentialsHandler 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 CAStartDiscoveryServer()
103 {
104     OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
105
106     if(!g_isInitialized)
107     {
108         return CA_STATUS_NOT_INITIALIZED;
109     }
110
111     return CAStartDiscoveryServerAdapters();
112 }
113
114 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
115                        CAErrorCallback ErrorHandler)
116 {
117     OIC_LOG(DEBUG, TAG, "CARegisterHandler");
118
119     if(!g_isInitialized)
120     {
121         OIC_LOG(DEBUG, TAG, "CA is not initialized");
122         return;
123     }
124
125     CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
126 }
127
128 #ifdef __WITH_DTLS__
129 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
130 {
131     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
132
133     if(!g_isInitialized)
134     {
135         return CA_STATUS_NOT_INITIALIZED;
136     }
137
138     CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
139     return CA_STATUS_OK;
140 }
141 #endif //__WITH_DTLS__
142
143 #ifdef __WITH_X509__
144 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
145 {
146     OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
147
148     if(!g_isInitialized)
149     {
150         return CA_STATUS_NOT_INITIALIZED;
151     }
152
153     CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
154     return CA_STATUS_OK;
155 }
156
157 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
158 {
159     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
160
161     if(!g_isInitialized)
162     {
163         return CA_STATUS_NOT_INITIALIZED;
164     }
165
166     CADTLSSetCrlCallback(GetDTLSCrlHandler);
167     return CA_STATUS_OK;
168 }
169 #endif //__WITH_X509__
170
171 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
172                             CATransportAdapter_t adapter,
173                             const char *addr,
174                             uint16_t port,
175                             CAEndpoint_t **object)
176 {
177     if (!object)
178     {
179         OIC_LOG(ERROR, TAG, "Invalid Parameter");
180         return CA_STATUS_INVALID_PARAM;
181     }
182
183     CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
184     if (!endpoint)
185     {
186         return CA_STATUS_FAILED;
187     }
188     *object = endpoint;
189     return CA_STATUS_OK;
190 }
191
192 void CADestroyEndpoint(CAEndpoint_t *rep)
193 {
194     OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
195
196     CAFreeEndpoint(rep);
197 }
198
199 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
200 {
201     OIC_LOG(DEBUG, TAG, "CAGenerateToken");
202
203     return CAGenerateTokenInternal(token, tokenLength);
204 }
205
206 void CADestroyToken(CAToken_t token)
207 {
208     OIC_LOG(DEBUG, TAG, "CADestroyToken");
209
210     CADestroyTokenInternal(token);
211
212     OIC_LOG(DEBUG, TAG, "OUT");
213 }
214
215 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
216 {
217     OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
218
219     if(!g_isInitialized)
220     {
221         return CA_STATUS_NOT_INITIALIZED;
222     }
223
224     return CAGetNetworkInformationInternal(info, size);
225 }
226
227 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
228 {
229     OIC_LOG(DEBUG, TAG, "CASendGetRequest");
230
231     if(!g_isInitialized)
232     {
233         return CA_STATUS_NOT_INITIALIZED;
234     }
235
236     return CADetachRequestMessage(object, requestInfo);
237 }
238
239 CAResult_t CASendNotification(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
240 {
241     OIC_LOG(DEBUG, TAG, "CASendNotification");
242
243     if(!g_isInitialized)
244     {
245         return CA_STATUS_NOT_INITIALIZED;
246     }
247
248     return CADetachResponseMessage(object, responseInfo);
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__ */