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 CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentialsHandler)
152 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
156 return CA_STATUS_NOT_INITIALIZED;
159 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
162 #endif //__WITH_DTLS__
165 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
167 OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
171 return CA_STATUS_NOT_INITIALIZED;
174 CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
178 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
180 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
184 return CA_STATUS_NOT_INITIALIZED;
187 CADTLSSetCrlCallback(GetDTLSCrlHandler);
190 #endif //__WITH_X509__
192 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
193 CATransportAdapter_t adapter,
196 CAEndpoint_t **object)
200 OIC_LOG(ERROR, TAG, "Invalid Parameter");
201 return CA_STATUS_INVALID_PARAM;
204 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
207 return CA_STATUS_FAILED;
213 void CADestroyEndpoint(CAEndpoint_t *rep)
215 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
220 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
222 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
224 return CAGenerateTokenInternal(token, tokenLength);
227 void CADestroyToken(CAToken_t token)
229 OIC_LOG(DEBUG, TAG, "CADestroyToken");
231 CADestroyTokenInternal(token);
233 OIC_LOG(DEBUG, TAG, "OUT");
236 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
238 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
242 return CA_STATUS_NOT_INITIALIZED;
245 return CAGetNetworkInformationInternal(info, size);
248 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
250 OIC_LOG(DEBUG, TAG, "CASendGetRequest");
254 return CA_STATUS_NOT_INITIALIZED;
257 return CADetachRequestMessage(object, requestInfo);
260 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
262 OIC_LOG(DEBUG, TAG, "CASendResponse");
266 return CA_STATUS_NOT_INITIALIZED;
269 return CADetachResponseMessage(object, responseInfo);
272 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
274 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
278 return CA_STATUS_NOT_INITIALIZED;
281 CAResult_t res = CA_STATUS_OK;
283 if (interestedNetwork & CA_ADAPTER_IP)
285 res = CAAddNetworkType(CA_ADAPTER_IP);
286 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
288 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
290 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
291 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
293 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
295 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
296 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
300 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
302 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
303 OIC_LOG_V(DEBUG, TAG,
304 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
309 else if (interestedNetwork & CA_ADAPTER_TCP)
311 res = CAAddNetworkType(CA_ADAPTER_TCP);
312 OIC_LOG_V(DEBUG, TAG,
313 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
319 res = CA_NOT_SUPPORTED;
324 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
326 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
330 return CA_STATUS_NOT_INITIALIZED;
333 CAResult_t res = CA_STATUS_OK;
335 if (nonInterestedNetwork & CA_ADAPTER_IP)
337 res = CARemoveNetworkType(CA_ADAPTER_IP);
338 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
340 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
342 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
343 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
345 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
347 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
348 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
351 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
353 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
354 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
361 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
363 res = CARemoveNetworkType(CA_ADAPTER_TCP);
364 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
371 res = CA_STATUS_FAILED;
376 CAResult_t CAHandleRequestResponse()
378 if (!g_isInitialized)
380 OIC_LOG(ERROR, TAG, "not initialized");
381 return CA_STATUS_NOT_INITIALIZED;
384 CAHandleRequestResponseCallbacks();
391 CAResult_t CASelectCipherSuite(const uint16_t cipher)
393 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
395 return CADtlsSelectCipherSuite(cipher);
398 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
400 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
402 return CADtlsEnableAnonECDHCipherSuite(enable);
405 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
406 const uint8_t* label, const size_t labelLen,
407 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
408 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
409 uint8_t* ownerPSK, const size_t ownerPSKSize)
411 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
413 CAResult_t res = CA_STATUS_OK;
415 //newOwnerLabel and prevOwnerLabe can be NULL
416 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
418 return CA_STATUS_INVALID_PARAM;
421 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
422 rsrcServerDeviceID, rsrcServerDeviceIDLen,
423 provServerDeviceID, provServerDeviceIDLen,
424 ownerPSK, ownerPSKSize);
425 if (CA_STATUS_OK != res)
427 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
430 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
435 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
437 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
438 CAResult_t res = CA_STATUS_OK;
442 return CA_STATUS_INVALID_PARAM;
445 res = CADtlsInitiateHandshake(endpoint);
446 if (CA_STATUS_OK != res)
448 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
451 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
456 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
458 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
459 CAResult_t res = CA_STATUS_OK;
463 return CA_STATUS_INVALID_PARAM;
466 res = CADtlsClose(endpoint);
467 if (CA_STATUS_OK != res)
469 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
472 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
477 #endif /* __WITH_DTLS__ */