1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
26 #include "cainterface.h"
27 #include "caremotehandler.h"
28 #include "camessagehandler.h"
29 #include "caprotocolmessage.h"
30 #include "canetworkconfigurator.h"
31 #include "cainterfacecontroller.h"
34 #include "caadapternetdtls.h"
38 #include "catcpadapter.h"
44 CAGlobals_t caglobals = { 0 };
46 #define TAG "CA_CONN_MGR"
48 static bool g_isInitialized = false;
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);
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);
63 CAResult_t CAInitialize()
65 OIC_LOG(DEBUG, TAG, "CAInitialize");
69 if (0 != OCSeedRandom())
71 OIC_LOG(ERROR, TAG, "Seed Random Failed");
74 CAResult_t res = CAInitializeMessageHandler();
75 if (res != CA_STATUS_OK)
77 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
80 g_isInitialized = true;
88 OIC_LOG(DEBUG, TAG, "CATerminate");
92 CATerminateMessageHandler();
93 CATerminateNetworkType();
95 g_isInitialized = false;
99 CAResult_t CAStartListeningServer()
101 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
105 return CA_STATUS_NOT_INITIALIZED;
108 return CAStartListeningServerAdapters();
111 CAResult_t CAStopListeningServer()
113 OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
117 return CA_STATUS_NOT_INITIALIZED;
120 return CAStopListeningServerAdapters();
123 CAResult_t CAStartDiscoveryServer()
125 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
129 return CA_STATUS_NOT_INITIALIZED;
132 return CAStartDiscoveryServerAdapters();
135 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
136 CAErrorCallback ErrorHandler)
138 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
142 OIC_LOG(DEBUG, TAG, "CA is not initialized");
146 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
150 CAResult_t CARegisterDTLSHandshakeCallback(CAErrorCallback dtlsHandshakeCallback)
152 OIC_LOG(DEBUG, TAG, "CARegisterDTLSHandshakeCallback");
156 return CA_STATUS_NOT_INITIALIZED;
159 CADTLSSetHandshakeCallback(dtlsHandshakeCallback);
164 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentialsHandler)
166 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
170 return CA_STATUS_NOT_INITIALIZED;
173 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
176 #endif //__WITH_DTLS__
179 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
181 OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
185 return CA_STATUS_NOT_INITIALIZED;
188 CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
192 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
194 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
198 return CA_STATUS_NOT_INITIALIZED;
201 CADTLSSetCrlCallback(GetDTLSCrlHandler);
204 #endif //__WITH_X509__
206 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
207 CATransportAdapter_t adapter,
210 CAEndpoint_t **object)
214 OIC_LOG(ERROR, TAG, "Invalid Parameter");
215 return CA_STATUS_INVALID_PARAM;
218 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
221 return CA_STATUS_FAILED;
227 void CADestroyEndpoint(CAEndpoint_t *rep)
229 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
234 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
236 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
238 return CAGenerateTokenInternal(token, tokenLength);
241 void CADestroyToken(CAToken_t token)
243 OIC_LOG(DEBUG, TAG, "CADestroyToken");
245 CADestroyTokenInternal(token);
247 OIC_LOG(DEBUG, TAG, "OUT");
250 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
252 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
256 return CA_STATUS_NOT_INITIALIZED;
259 return CAGetNetworkInformationInternal(info, size);
262 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
264 OIC_LOG(DEBUG, TAG, "CASendRequest");
268 return CA_STATUS_NOT_INITIALIZED;
271 return CADetachSendMessage(object, requestInfo, CA_REQUEST_DATA);
274 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
276 OIC_LOG(DEBUG, TAG, "CASendResponse");
280 return CA_STATUS_NOT_INITIALIZED;
283 return CADetachSendMessage(object, responseInfo, CA_RESPONSE_DATA);
286 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
288 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
292 return CA_STATUS_NOT_INITIALIZED;
295 CAResult_t res = CA_STATUS_OK;
297 if (interestedNetwork & CA_ADAPTER_IP)
299 res = CAAddNetworkType(CA_ADAPTER_IP);
300 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
302 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
304 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
305 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
307 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
309 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
310 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
314 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
316 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
317 OIC_LOG_V(DEBUG, TAG,
318 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
323 else if (interestedNetwork & CA_ADAPTER_TCP)
325 res = CAAddNetworkType(CA_ADAPTER_TCP);
326 OIC_LOG_V(DEBUG, TAG,
327 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
333 res = CA_NOT_SUPPORTED;
338 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
340 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
344 return CA_STATUS_NOT_INITIALIZED;
347 CAResult_t res = CA_STATUS_OK;
349 if (nonInterestedNetwork & CA_ADAPTER_IP)
351 res = CARemoveNetworkType(CA_ADAPTER_IP);
352 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
354 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
356 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
357 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
359 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
361 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
362 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
365 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
367 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
368 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
375 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
377 res = CARemoveNetworkType(CA_ADAPTER_TCP);
378 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
385 res = CA_STATUS_FAILED;
390 CAResult_t CAHandleRequestResponse()
392 if (!g_isInitialized)
394 OIC_LOG(ERROR, TAG, "not initialized");
395 return CA_STATUS_NOT_INITIALIZED;
398 CAHandleRequestResponseCallbacks();
404 CAResult_t CASelectCipherSuite(const uint16_t cipher)
406 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
408 return CADtlsSelectCipherSuite(cipher);
411 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
413 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
415 return CADtlsEnableAnonECDHCipherSuite(enable);
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)
424 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
426 CAResult_t res = CA_STATUS_OK;
428 //newOwnerLabel and prevOwnerLabe can be NULL
429 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
431 return CA_STATUS_INVALID_PARAM;
434 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
435 rsrcServerDeviceID, rsrcServerDeviceIDLen,
436 provServerDeviceID, provServerDeviceIDLen,
437 ownerPSK, ownerPSKSize);
438 if (CA_STATUS_OK != res)
440 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
443 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
448 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
450 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
451 CAResult_t res = CA_STATUS_OK;
455 return CA_STATUS_INVALID_PARAM;
458 res = CADtlsInitiateHandshake(endpoint);
459 if (CA_STATUS_OK != res)
461 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
464 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
469 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
471 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
472 CAResult_t res = CA_STATUS_OK;
476 return CA_STATUS_INVALID_PARAM;
479 res = CADtlsClose(endpoint);
480 if (CA_STATUS_OK != res)
482 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
485 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
490 #endif /* __WITH_DTLS__ */