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