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 ******************************************************************/
27 #include "cainterface.h"
28 #include "caremotehandler.h"
29 #include "camessagehandler.h"
30 #include "caprotocolmessage.h"
31 #include "canetworkconfigurator.h"
32 #include "cainterfacecontroller.h"
35 #include "caadapternetdtls.h"
39 #include "catcpadapter.h"
42 CAGlobals_t caglobals = { .clientFlags = 0,
45 #define TAG "OIC_CA_CONN_MGR"
47 static bool g_isInitialized = false;
50 // CAAdapterNetDTLS will register the callback.
51 // Taking callback all the way through adapters not the right approach, hence calling here.
52 extern void CADTLSSetCredentialsCallback(CAGetDTLSPskCredentialsHandler credCallback);
56 // CAAdapterNetDTLS will register the callback.
57 // Taking callback all the way through adapters not the right approach, hence calling here.
58 extern void CADTLSSetX509CredentialsCallback(CAGetDTLSX509CredentialsHandler credCallback);
59 extern void CADTLSSetCrlCallback(CAGetDTLSCrlHandler crlCallback);
62 CAResult_t CAInitialize()
64 OIC_LOG_V(DEBUG, TAG, "IoTivity version is v%s", IOTIVITY_VERSION);
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");
103 if (!g_isInitialized)
105 return CA_STATUS_NOT_INITIALIZED;
108 return CAStartListeningServerAdapters();
111 CAResult_t CAStopListeningServer()
113 OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
115 if (!g_isInitialized)
117 return CA_STATUS_NOT_INITIALIZED;
120 return CAStopListeningServerAdapters();
123 CAResult_t CAStartDiscoveryServer()
125 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
127 if (!g_isInitialized)
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");
140 if (!g_isInitialized)
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");
154 if (!g_isInitialized)
156 return CA_STATUS_NOT_INITIALIZED;
159 CADTLSSetHandshakeCallback(dtlsHandshakeCallback);
164 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSPskCredentialsHandler GetDTLSCredentialsHandler)
166 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
168 if (!g_isInitialized)
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");
183 if (!g_isInitialized)
185 return CA_STATUS_NOT_INITIALIZED;
188 CADTLSSetX509CredentialsCallback(GetDTLSX509CredentialsHandler);
192 CAResult_t CARegisterDTLSCrlHandler(CAGetDTLSCrlHandler GetDTLSCrlHandler)
194 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCrlHandler");
196 if (!g_isInitialized)
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");
254 if (!g_isInitialized)
256 return CA_STATUS_NOT_INITIALIZED;
259 return CAGetNetworkInformationInternal(info, size);
262 static CAResult_t CASendMessageMultiAdapter(const CAEndpoint_t *object, const void *sendMsg,
263 CADataType_t dataType)
265 OIC_LOG(DEBUG, TAG, "CASendMessageMultipleAdapter");
267 CATransportAdapter_t connTypes[] = {
270 ,CA_ADAPTER_GATT_BTLE
273 ,CA_ADAPTER_RFCOMM_BTEDR
279 ,CA_ADAPTER_REMOTE_ACCESS
286 CAEndpoint_t *cloneEp = CACloneEndpoint(object);
289 OIC_LOG(ERROR, TAG, "Failed to clone CAEndpoint");
290 return CA_MEMORY_ALLOC_FAILED;
293 CAResult_t ret = CA_STATUS_OK;
294 size_t numConnTypes = sizeof(connTypes) / sizeof(connTypes[0]);
296 for (size_t i = 0; i < numConnTypes && ret == CA_STATUS_OK; i++)
298 cloneEp->adapter = connTypes[i];
299 ret = CADetachSendMessage(cloneEp, sendMsg, dataType);
301 CAFreeEndpoint(cloneEp);
305 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
307 OIC_LOG(DEBUG, TAG, "CASendRequest");
309 if (!g_isInitialized)
311 return CA_STATUS_NOT_INITIALIZED;
314 if (requestInfo && requestInfo->isMulticast &&
315 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
317 return CASendMessageMultiAdapter(object, requestInfo, CA_REQUEST_DATA);
321 return CADetachSendMessage(object, requestInfo, CA_REQUEST_DATA);
325 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
327 OIC_LOG(DEBUG, TAG, "CASendResponse");
329 if (!g_isInitialized)
331 return CA_STATUS_NOT_INITIALIZED;
334 if (!responseInfo || !object)
336 return CA_STATUS_INVALID_PARAM;
339 if (responseInfo->isMulticast &&
340 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
342 return CASendMessageMultiAdapter(object, responseInfo, responseInfo->info.dataType);
346 return CADetachSendMessage(object, responseInfo, responseInfo->info.dataType);
350 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
352 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
354 if (!g_isInitialized)
356 return CA_STATUS_NOT_INITIALIZED;
359 CAResult_t res = CA_STATUS_OK;
361 if (interestedNetwork & CA_ADAPTER_IP)
363 res = CAAddNetworkType(CA_ADAPTER_IP);
364 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
366 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
368 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
369 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
371 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
373 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
374 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
378 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
380 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
381 OIC_LOG_V(DEBUG, TAG,
382 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
387 else if (interestedNetwork & CA_ADAPTER_TCP)
389 res = CAAddNetworkType(CA_ADAPTER_TCP);
390 OIC_LOG_V(DEBUG, TAG,
391 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
394 else if (interestedNetwork & CA_ADAPTER_NFC)
396 res = CAAddNetworkType(CA_ADAPTER_NFC);
397 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_ADAPTER_NFC) function returns result : %d", res);
401 res = CA_NOT_SUPPORTED;
406 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
408 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
410 if (!g_isInitialized)
412 return CA_STATUS_NOT_INITIALIZED;
415 CAResult_t res = CA_STATUS_OK;
417 if (nonInterestedNetwork & CA_ADAPTER_IP)
419 res = CARemoveNetworkType(CA_ADAPTER_IP);
420 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
422 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
424 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
425 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
427 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
429 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
430 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
433 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
435 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
436 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
443 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
445 res = CARemoveNetworkType(CA_ADAPTER_TCP);
446 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
453 res = CA_STATUS_FAILED;
458 CAResult_t CAHandleRequestResponse()
460 if (!g_isInitialized)
462 OIC_LOG(ERROR, TAG, "not initialized");
463 return CA_STATUS_NOT_INITIALIZED;
466 CAHandleRequestResponseCallbacks();
472 CAResult_t CASelectCipherSuite(const uint16_t cipher)
474 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
476 return CADtlsSelectCipherSuite(cipher);
479 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
481 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
483 return CADtlsEnableAnonECDHCipherSuite(enable);
486 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
487 const uint8_t* label, const size_t labelLen,
488 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
489 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
490 uint8_t* ownerPSK, const size_t ownerPSKSize)
492 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
494 CAResult_t res = CA_STATUS_OK;
496 //newOwnerLabel and prevOwnerLabe can be NULL
497 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
499 return CA_STATUS_INVALID_PARAM;
502 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
503 rsrcServerDeviceID, rsrcServerDeviceIDLen,
504 provServerDeviceID, provServerDeviceIDLen,
505 ownerPSK, ownerPSKSize);
506 if (CA_STATUS_OK != res)
508 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
511 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
516 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
518 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
519 CAResult_t res = CA_STATUS_OK;
523 return CA_STATUS_INVALID_PARAM;
526 res = CADtlsInitiateHandshake(endpoint);
527 if (CA_STATUS_OK != res)
529 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
532 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
537 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
539 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
540 CAResult_t res = CA_STATUS_OK;
544 return CA_STATUS_INVALID_PARAM;
547 res = CADtlsClose(endpoint);
548 if (CA_STATUS_OK != res)
550 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
553 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
558 #endif /* __WITH_DTLS__ */
561 void CARegisterKeepAliveHandler(CAKeepAliveConnectionCallback ConnHandler)
563 CATCPSetKeepAliveCallbacks(ConnHandler);