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 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
36 #include "ca_adapter_net_ssl.h"
37 #endif // __WITH_DTLS__ or __WITH_TLS__
40 #include "catcpadapter.h"
43 CAGlobals_t caglobals = { .clientFlags = 0,
46 #define TAG "OIC_CA_CONN_MGR"
48 static bool g_isInitialized = false;
50 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
51 // Taking callback all the way through adapters not the right approach, hence calling here.
52 extern void CAsetPkixInfoCallback(CAgetPkixInfoHandler infCallback);
53 extern void CAsetPskCredentialsCallback(CAgetPskCredentialsHandler credCallback);
54 extern void CAsetCredentialTypesCallback(CAgetCredentialTypesHandler credCallback);
55 #endif // __WITH_DTLS__ or __WITH_TLS__
58 CAResult_t CAInitialize(CATransportAdapter_t transportType)
60 OIC_LOG_V(DEBUG, TAG, "IoTivity version is v%s", IOTIVITY_VERSION);
61 OIC_LOG_V(DEBUG, TAG, "CAInitialize type : %d", transportType);
65 CAResult_t res = CAInitializeMessageHandler(transportType);
66 if (res != CA_STATUS_OK)
68 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
69 CATerminateMessageHandler();
72 g_isInitialized = true;
80 OIC_LOG(DEBUG, TAG, "CATerminate");
84 CATerminateMessageHandler();
85 CATerminateNetworkType();
87 g_isInitialized = false;
91 CAResult_t CAStartListeningServer()
93 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
97 return CA_STATUS_NOT_INITIALIZED;
100 return CAStartListeningServerAdapters();
103 CAResult_t CAStopListeningServer()
105 OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
107 if (!g_isInitialized)
109 return CA_STATUS_NOT_INITIALIZED;
112 return CAStopListeningServerAdapters();
115 CAResult_t CAStartDiscoveryServer()
117 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
119 if (!g_isInitialized)
121 return CA_STATUS_NOT_INITIALIZED;
124 return CAStartDiscoveryServerAdapters();
127 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
128 CAErrorCallback ErrorHandler)
130 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
132 if (!g_isInitialized)
134 OIC_LOG(DEBUG, TAG, "CA is not initialized");
138 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
141 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
142 #ifdef MULTIPLE_OWNER
143 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer)
145 OIC_LOG(DEBUG, TAG, "IN CAGetSecurePeerInfo");
147 if (!g_isInitialized)
149 OIC_LOG(DEBUG, TAG, "CA is not initialized");
153 OIC_LOG(DEBUG, TAG, "OUT CAGetSecurePeerInfo");
154 return GetCASecureEndpointData(peer);
156 #endif //MULTIPLE_OWNER
158 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback)
160 OIC_LOG(DEBUG, TAG, "CAregisterSslHandshakeCallback");
164 return CA_STATUS_NOT_INITIALIZED;
167 CAsetSslHandshakeCallback(tlsHandshakeCallback);
171 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentialsHandler)
173 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
175 if (!g_isInitialized)
177 return CA_STATUS_NOT_INITIALIZED;
179 CAsetPskCredentialsCallback(getTlsCredentialsHandler);
180 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
184 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler)
186 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
188 if (!g_isInitialized)
190 return CA_STATUS_NOT_INITIALIZED;
192 CAsetPkixInfoCallback(getPkixInfoHandler);
193 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
197 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler)
199 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
201 if (!g_isInitialized)
203 return CA_STATUS_NOT_INITIALIZED;
205 CAsetCredentialTypesCallback(getCredTypesHandler);
206 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
209 #endif // __WITH_DTLS__ or __WITH_TLS__
211 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
212 CATransportAdapter_t adapter,
215 CAEndpoint_t **object)
219 OIC_LOG(ERROR, TAG, "Invalid Parameter");
220 return CA_STATUS_INVALID_PARAM;
223 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
226 return CA_STATUS_FAILED;
232 void CADestroyEndpoint(CAEndpoint_t *rep)
234 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
239 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
241 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
243 return CAGenerateTokenInternal(token, tokenLength);
246 void CADestroyToken(CAToken_t token)
248 OIC_LOG(DEBUG, TAG, "CADestroyToken");
250 CADestroyTokenInternal(token);
252 OIC_LOG(DEBUG, TAG, "OUT");
255 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
257 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
259 if (!g_isInitialized)
261 return CA_STATUS_NOT_INITIALIZED;
264 return CAGetNetworkInformationInternal(info, size);
267 static CAResult_t CASendMessageMultiAdapter(const CAEndpoint_t *object, const void *sendMsg,
268 CADataType_t dataType)
270 OIC_LOG(DEBUG, TAG, "CASendMessageMultipleAdapter");
272 CATransportAdapter_t connTypes[] = {
275 ,CA_ADAPTER_GATT_BTLE
278 ,CA_ADAPTER_RFCOMM_BTEDR
284 ,CA_ADAPTER_REMOTE_ACCESS
291 CAEndpoint_t *cloneEp = CACloneEndpoint(object);
294 OIC_LOG(ERROR, TAG, "Failed to clone CAEndpoint");
295 return CA_MEMORY_ALLOC_FAILED;
298 CAResult_t ret = CA_STATUS_OK;
299 size_t numConnTypes = sizeof(connTypes) / sizeof(connTypes[0]);
301 for (size_t i = 0; i < numConnTypes && ret == CA_STATUS_OK; i++)
303 cloneEp->adapter = connTypes[i];
304 ret = CADetachSendMessage(cloneEp, sendMsg, dataType);
306 CAFreeEndpoint(cloneEp);
310 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
312 OIC_LOG(DEBUG, TAG, "CASendRequest");
314 if (!g_isInitialized)
316 return CA_STATUS_NOT_INITIALIZED;
319 if (requestInfo && requestInfo->isMulticast &&
320 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
322 return CASendMessageMultiAdapter(object, requestInfo, CA_REQUEST_DATA);
326 return CADetachSendMessage(object, requestInfo, CA_REQUEST_DATA);
330 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
332 OIC_LOG(DEBUG, TAG, "CASendResponse");
334 if (!g_isInitialized)
336 return CA_STATUS_NOT_INITIALIZED;
339 if (!responseInfo || !object)
341 return CA_STATUS_INVALID_PARAM;
344 if (responseInfo->isMulticast &&
345 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
347 return CASendMessageMultiAdapter(object, responseInfo, responseInfo->info.dataType);
351 return CADetachSendMessage(object, responseInfo, responseInfo->info.dataType);
355 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
357 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
359 if (!g_isInitialized)
361 return CA_STATUS_NOT_INITIALIZED;
364 CAResult_t res = CA_STATUS_OK;
366 if (interestedNetwork & CA_ADAPTER_IP)
368 res = CAAddNetworkType(CA_ADAPTER_IP);
369 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
371 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
373 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
374 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
376 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
378 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
379 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
383 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
385 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
386 OIC_LOG_V(DEBUG, TAG,
387 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
392 else if (interestedNetwork & CA_ADAPTER_TCP)
394 res = CAAddNetworkType(CA_ADAPTER_TCP);
395 OIC_LOG_V(DEBUG, TAG,
396 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
399 else if (interestedNetwork & CA_ADAPTER_NFC)
401 res = CAAddNetworkType(CA_ADAPTER_NFC);
402 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_ADAPTER_NFC) function returns result : %d", res);
406 res = CA_NOT_SUPPORTED;
411 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
413 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
415 if (!g_isInitialized)
417 return CA_STATUS_NOT_INITIALIZED;
420 CAResult_t res = CA_STATUS_OK;
422 if (nonInterestedNetwork & CA_ADAPTER_IP)
424 res = CARemoveNetworkType(CA_ADAPTER_IP);
425 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
427 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
429 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
430 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
432 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
434 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
435 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
438 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
440 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
441 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
448 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
450 res = CARemoveNetworkType(CA_ADAPTER_TCP);
451 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
458 res = CA_STATUS_FAILED;
463 CAResult_t CAHandleRequestResponse()
465 if (!g_isInitialized)
467 OIC_LOG(ERROR, TAG, "not initialized");
468 return CA_STATUS_NOT_INITIALIZED;
471 CAHandleRequestResponseCallbacks();
476 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter)
478 (void)(adapter); // prevent unused-parameter warning when building release variant
479 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
480 OIC_LOG_V(DEBUG, TAG, "cipher : %d , CATransportAdapter : %d", cipher, adapter);
481 CAResult_t res = CA_STATUS_FAILED;
482 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
483 res = CAsetTlsCipherSuite(cipher);
484 if (CA_STATUS_OK != res)
486 OIC_LOG_V(ERROR, TAG, "Failed to CAsetTlsCipherSuite : %d", res);
489 (void)(cipher); // prevent unused-parameter warning
490 OIC_LOG(ERROR, TAG, "Method not supported");
492 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
496 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
498 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
499 CAResult_t res = CA_STATUS_FAILED;
500 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
501 // TLS_ECDH_ANON_WITH_AES_128_CBC_SHA256 0xFF00 replaces 0xC018
502 res = CAsetTlsCipherSuite(enable ? 0xFF00 : 0x00);
503 if (CA_STATUS_OK != res)
505 OIC_LOG_V(ERROR, TAG, "Failed to CAsetTlsCipherSuite : %d", res);
508 (void)(enable); // prevent unused-parameter compiler warning
509 OIC_LOG(ERROR, TAG, "Method not supported");
511 OIC_LOG_V(ERROR, TAG, "Out %s", __func__);
515 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
516 const uint8_t* label, const size_t labelLen,
517 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
518 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
519 uint8_t* ownerPSK, const size_t ownerPskSize)
521 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
522 CAResult_t res = CA_STATUS_FAILED;
523 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
524 //newOwnerLabel and prevOwnerLabe can be NULL
525 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPskSize)
527 return CA_STATUS_INVALID_PARAM;
530 res = CAsslGenerateOwnerPsk(endpoint, label, labelLen,
531 rsrcServerDeviceID, rsrcServerDeviceIDLen,
532 provServerDeviceID, provServerDeviceIDLen,
533 ownerPSK, ownerPskSize);
534 if (CA_STATUS_OK != res)
536 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
539 (void)(endpoint); // prevent unused-parameter compiler warnings
542 (void)(rsrcServerDeviceID);
543 (void)(rsrcServerDeviceIDLen);
544 (void)(provServerDeviceID);
545 (void)(provServerDeviceIDLen);
547 (void)(ownerPskSize);
548 OIC_LOG(ERROR, TAG, "Method not supported");
550 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
554 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
556 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
557 CAResult_t res = CA_STATUS_FAILED;
558 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
561 return CA_STATUS_INVALID_PARAM;
564 res = CAinitiateSslHandshake(endpoint);
565 if (CA_STATUS_OK != res)
567 OIC_LOG_V(ERROR, TAG, "Failed to CAinitiateSslHandshake : %d", res);
570 (void)(endpoint); // prevent unused-parameter compiler warning
571 OIC_LOG(ERROR, TAG, "Method not supported");
573 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
577 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint)
579 OIC_LOG_V(DEBUG, TAG, "IN : CAcloseSslSession");
580 CAResult_t res = CA_STATUS_FAILED;
581 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
584 return CA_STATUS_INVALID_PARAM;
587 res = CAcloseSslConnection(endpoint);
588 if (CA_STATUS_OK != res)
590 OIC_LOG_V(ERROR, TAG, "Failed to CAsslClose : %d", res);
593 (void)(endpoint); // prevent unused-parameter compiler warning
594 OIC_LOG(ERROR, TAG, "Method not supported");
596 OIC_LOG_V(DEBUG, TAG, "OUT : CAcloseSslSession");
601 void CARegisterKeepAliveHandler(CAKeepAliveConnectionCallback ConnHandler)
603 CATCPSetKeepAliveCallbacks(ConnHandler);