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"
41 CAGlobals_t caglobals = { 0 };
43 #define TAG "CA_CONN_MGR"
45 static bool g_isInitialized = false;
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);
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);
60 CAResult_t CAInitialize()
62 OIC_LOG(DEBUG, TAG, "CAInitialize");
66 CAResult_t res = CAInitializeMessageHandler();
67 if (res != CA_STATUS_OK)
69 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
72 g_isInitialized = true;
79 OIC_LOG(DEBUG, TAG, "CATerminate");
83 CATerminateMessageHandler();
84 CATerminateNetworkType();
86 g_isInitialized = false;
90 CAResult_t CAStartListeningServer()
92 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
96 return CA_STATUS_NOT_INITIALIZED;
99 return CAStartListeningServerAdapters();
102 CAResult_t CAStartDiscoveryServer()
104 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
108 return CA_STATUS_NOT_INITIALIZED;
111 return CAStartDiscoveryServerAdapters();
114 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
115 CAErrorCallback ErrorHandler)
117 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
121 OIC_LOG(DEBUG, TAG, "CA is not initialized");
125 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
129 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
131 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
135 return CA_STATUS_NOT_INITIALIZED;
138 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
141 #endif //__WITH_DTLS__
144 CAResult_t CARegisterDTLSX509CredentialsHandler(CAGetDTLSX509CredentialsHandler GetDTLSX509CredentialsHandler)
146 OIC_LOG(DEBUG, TAG, "CARegisterDTLSX509CredentialsHandler");
150 return CA_STATUS_NOT_INITIALIZED;
153 CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
157 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
159 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
163 return CA_STATUS_NOT_INITIALIZED;
166 CADTLSSetCrlCallback(GetDTLSCrlHandler);
169 #endif //__WITH_X509__
171 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
172 CATransportAdapter_t adapter,
175 CAEndpoint_t **object)
179 OIC_LOG(ERROR, TAG, "Invalid Parameter");
180 return CA_STATUS_INVALID_PARAM;
183 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
186 return CA_STATUS_FAILED;
192 void CADestroyEndpoint(CAEndpoint_t *rep)
194 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
199 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
201 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
203 return CAGenerateTokenInternal(token, tokenLength);
206 void CADestroyToken(CAToken_t token)
208 OIC_LOG(DEBUG, TAG, "CADestroyToken");
210 CADestroyTokenInternal(token);
212 OIC_LOG(DEBUG, TAG, "OUT");
215 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
217 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
221 return CA_STATUS_NOT_INITIALIZED;
224 return CAGetNetworkInformationInternal(info, size);
227 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
229 OIC_LOG(DEBUG, TAG, "CASendGetRequest");
233 return CA_STATUS_NOT_INITIALIZED;
236 return CADetachRequestMessage(object, requestInfo);
239 CAResult_t CASendNotification(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
241 OIC_LOG(DEBUG, TAG, "CASendNotification");
245 return CA_STATUS_NOT_INITIALIZED;
248 return CADetachResponseMessage(object, responseInfo);
251 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
253 OIC_LOG(DEBUG, TAG, "CASendResponse");
257 return CA_STATUS_NOT_INITIALIZED;
260 return CADetachResponseMessage(object, responseInfo);
263 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
265 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
269 return CA_STATUS_NOT_INITIALIZED;
272 CAResult_t res = CA_STATUS_OK;
274 if (interestedNetwork & CA_ADAPTER_IP)
276 res = CAAddNetworkType(CA_ADAPTER_IP);
277 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
279 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
281 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
282 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
284 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
286 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
287 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
291 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
293 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
294 OIC_LOG_V(DEBUG, TAG,
295 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
300 else if (interestedNetwork & CA_ADAPTER_TCP)
302 res = CAAddNetworkType(CA_ADAPTER_TCP);
303 OIC_LOG_V(DEBUG, TAG,
304 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
310 res = CA_NOT_SUPPORTED;
315 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
317 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
321 return CA_STATUS_NOT_INITIALIZED;
324 CAResult_t res = CA_STATUS_OK;
326 if (nonInterestedNetwork & CA_ADAPTER_IP)
328 res = CARemoveNetworkType(CA_ADAPTER_IP);
329 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
331 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
333 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
334 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
336 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
338 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
339 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
342 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
344 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
345 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
352 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
354 res = CARemoveNetworkType(CA_ADAPTER_TCP);
355 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
362 res = CA_STATUS_FAILED;
367 CAResult_t CAHandleRequestResponse()
369 if (!g_isInitialized)
371 OIC_LOG(ERROR, TAG, "not initialized");
372 return CA_STATUS_NOT_INITIALIZED;
375 CAHandleRequestResponseCallbacks();
382 CAResult_t CASelectCipherSuite(const uint16_t cipher)
384 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
386 return CADtlsSelectCipherSuite(cipher);
389 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
391 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
393 return CADtlsEnableAnonECDHCipherSuite(enable);
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)
402 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
404 CAResult_t res = CA_STATUS_OK;
406 //newOwnerLabel and prevOwnerLabe can be NULL
407 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
409 return CA_STATUS_INVALID_PARAM;
412 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
413 rsrcServerDeviceID, rsrcServerDeviceIDLen,
414 provServerDeviceID, provServerDeviceIDLen,
415 ownerPSK, ownerPSKSize);
416 if (CA_STATUS_OK != res)
418 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
421 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
426 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
428 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
429 CAResult_t res = CA_STATUS_OK;
433 return CA_STATUS_INVALID_PARAM;
436 res = CADtlsInitiateHandshake(endpoint);
437 if (CA_STATUS_OK != res)
439 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
442 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
447 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
449 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
450 CAResult_t res = CA_STATUS_OK;
454 return CA_STATUS_INVALID_PARAM;
457 res = CADtlsClose(endpoint);
458 if (CA_STATUS_OK != res)
460 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
463 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
468 #endif /* __WITH_DTLS__ */