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 ******************************************************************/
28 #include "cainterface.h"
29 #include "caremotehandler.h"
30 #include "camessagehandler.h"
31 #include "caprotocolmessage.h"
32 #include "canetworkconfigurator.h"
33 #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 extern void CAsetSetupPkContextCallback(CAsetupPkContextHandler setupPkCtxCallback);
56 #endif // __WITH_DTLS__ or __WITH_TLS__
59 CAResult_t CAInitialize(CATransportAdapter_t transportType)
61 OIC_LOG_V(DEBUG, TAG, "IoTivity version is v%s", IOTIVITY_VERSION);
62 OIC_LOG_V(DEBUG, TAG, "CAInitialize type : %d", transportType);
66 if (0 != OCSeedRandom())
68 OIC_LOG(ERROR, TAG, "Seed Random Failed");
71 CAResult_t res = CAInitializeMessageHandler(transportType);
72 if (res != CA_STATUS_OK)
74 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
75 CATerminateMessageHandler();
78 g_isInitialized = true;
86 OIC_LOG(DEBUG, TAG, "CATerminate");
90 CATerminateMessageHandler();
91 CATerminateNetworkType();
93 g_isInitialized = false;
97 CAResult_t CAStartListeningServer()
99 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
101 if (!g_isInitialized)
103 return CA_STATUS_NOT_INITIALIZED;
106 return CAStartListeningServerAdapters();
109 CAResult_t CAStopListeningServer()
111 OIC_LOG(DEBUG, TAG, "CAStopListeningServer");
113 if (!g_isInitialized)
115 return CA_STATUS_NOT_INITIALIZED;
118 return CAStopListeningServerAdapters();
121 CAResult_t CAStartDiscoveryServer()
123 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
125 if (!g_isInitialized)
127 return CA_STATUS_NOT_INITIALIZED;
130 return CAStartDiscoveryServerAdapters();
133 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
134 CAErrorCallback ErrorHandler)
136 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
138 if (!g_isInitialized)
140 OIC_LOG(DEBUG, TAG, "CA is not initialized");
144 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
147 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
148 #ifdef MULTIPLE_OWNER
149 const CASecureEndpoint_t *CAGetSecureEndpointData(const CAEndpoint_t *peer)
151 OIC_LOG(DEBUG, TAG, "IN CAGetSecurePeerInfo");
153 if (!g_isInitialized)
155 OIC_LOG(DEBUG, TAG, "CA is not initialized");
159 OIC_LOG(DEBUG, TAG, "OUT CAGetSecurePeerInfo");
160 return GetCASecureEndpointData(peer);
162 #endif //MULTIPLE_OWNER
164 CAResult_t CAregisterSslHandshakeCallback(CAErrorCallback tlsHandshakeCallback)
166 OIC_LOG(DEBUG, TAG, "CAregisterSslHandshakeCallback");
169 return CA_STATUS_NOT_INITIALIZED;
172 CAsetSslHandshakeCallback(tlsHandshakeCallback);
176 CAResult_t CAregisterPskCredentialsHandler(CAgetPskCredentialsHandler getTlsCredentialsHandler)
178 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
180 if (!g_isInitialized)
182 return CA_STATUS_NOT_INITIALIZED;
184 CAsetPskCredentialsCallback(getTlsCredentialsHandler);
185 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
189 CAResult_t CAregisterPkixInfoHandler(CAgetPkixInfoHandler getPkixInfoHandler)
191 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
193 if (!g_isInitialized)
195 return CA_STATUS_NOT_INITIALIZED;
197 CAsetPkixInfoCallback(getPkixInfoHandler);
198 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
202 CAResult_t CAregisterGetCredentialTypesHandler(CAgetCredentialTypesHandler getCredTypesHandler)
204 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
206 if (!g_isInitialized)
208 return CA_STATUS_NOT_INITIALIZED;
210 CAsetCredentialTypesCallback(getCredTypesHandler);
211 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
215 CAResult_t CAregisterSetupPkContextHandler(CAsetupPkContextHandler setupPkContextCallback)
217 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
219 if (!g_isInitialized)
221 return CA_STATUS_NOT_INITIALIZED;
223 CAsetSetupPkContextCallback(setupPkContextCallback);
224 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
227 #endif // __WITH_DTLS__ or __WITH_TLS__
229 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
230 CATransportAdapter_t adapter,
233 CAEndpoint_t **object)
237 OIC_LOG(ERROR, TAG, "Invalid Parameter");
238 return CA_STATUS_INVALID_PARAM;
241 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
244 return CA_STATUS_FAILED;
250 void CADestroyEndpoint(CAEndpoint_t *rep)
252 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
257 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
259 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
261 return CAGenerateTokenInternal(token, tokenLength);
264 void CADestroyToken(CAToken_t token)
266 OIC_LOG(DEBUG, TAG, "CADestroyToken");
268 CADestroyTokenInternal(token);
270 OIC_LOG(DEBUG, TAG, "OUT");
273 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
275 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
277 if (!g_isInitialized)
279 return CA_STATUS_NOT_INITIALIZED;
282 return CAGetNetworkInformationInternal(info, size);
285 static CAResult_t CASendMessageMultiAdapter(const CAEndpoint_t *object, const void *sendMsg,
286 CADataType_t dataType)
288 OIC_LOG(DEBUG, TAG, "CASendMessageMultipleAdapter");
290 CATransportAdapter_t connTypes[] = {
293 ,CA_ADAPTER_GATT_BTLE
296 ,CA_ADAPTER_RFCOMM_BTEDR
302 ,CA_ADAPTER_REMOTE_ACCESS
309 CAEndpoint_t *cloneEp = CACloneEndpoint(object);
312 OIC_LOG(ERROR, TAG, "Failed to clone CAEndpoint");
313 return CA_MEMORY_ALLOC_FAILED;
316 CAResult_t ret = CA_STATUS_OK;
317 size_t numConnTypes = sizeof(connTypes) / sizeof(connTypes[0]);
319 for (size_t i = 0; i < numConnTypes && ret == CA_STATUS_OK; i++)
321 cloneEp->adapter = connTypes[i];
322 ret = CADetachSendMessage(cloneEp, sendMsg, dataType);
324 CAFreeEndpoint(cloneEp);
328 CAResult_t CASendRequest(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
330 OIC_LOG(DEBUG, TAG, "CASendRequest");
332 if (!g_isInitialized)
334 return CA_STATUS_NOT_INITIALIZED;
337 if (requestInfo && requestInfo->isMulticast &&
338 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
340 return CASendMessageMultiAdapter(object, requestInfo, CA_REQUEST_DATA);
342 else if (requestInfo && requestInfo->info.event == CA_REQ_DISCONNECT &&
343 (object->adapter == CA_ADAPTER_TCP || object->adapter == CA_ALL_ADAPTERS))
345 return CADetachSendNetworkReqMessage(object, requestInfo->info.event, CA_NETWORK_COMMAND);
349 return CADetachSendMessage(object, requestInfo, CA_REQUEST_DATA);
353 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
355 OIC_LOG(DEBUG, TAG, "CASendResponse");
357 if (!g_isInitialized)
359 return CA_STATUS_NOT_INITIALIZED;
362 if (!responseInfo || !object)
364 return CA_STATUS_INVALID_PARAM;
367 if (responseInfo->isMulticast &&
368 (object->adapter == CA_DEFAULT_ADAPTER || object->adapter == CA_ALL_ADAPTERS))
370 return CASendMessageMultiAdapter(object, responseInfo, responseInfo->info.dataType);
374 return CADetachSendMessage(object, responseInfo, responseInfo->info.dataType);
378 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
380 if (!g_isInitialized)
382 return CA_STATUS_NOT_INITIALIZED;
385 CAResult_t res = CA_STATUS_OK;
387 if (interestedNetwork & CA_ADAPTER_IP)
389 res = CAAddNetworkType(CA_ADAPTER_IP);
390 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
392 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
394 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
395 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
397 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
399 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
400 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
404 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
406 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
407 OIC_LOG_V(DEBUG, TAG,
408 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
413 else if (interestedNetwork & CA_ADAPTER_TCP)
415 res = CAAddNetworkType(CA_ADAPTER_TCP);
416 OIC_LOG_V(DEBUG, TAG,
417 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
420 else if (interestedNetwork & CA_ADAPTER_NFC)
422 res = CAAddNetworkType(CA_ADAPTER_NFC);
423 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_ADAPTER_NFC) function returns result : %d", res);
427 res = CA_NOT_SUPPORTED;
432 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
434 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
436 if (!g_isInitialized)
438 return CA_STATUS_NOT_INITIALIZED;
441 CAResult_t res = CA_STATUS_OK;
443 if (nonInterestedNetwork & CA_ADAPTER_IP)
445 res = CARemoveNetworkType(CA_ADAPTER_IP);
446 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
448 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
450 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
451 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
453 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
455 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
456 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
459 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
461 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
462 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
469 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
471 res = CARemoveNetworkType(CA_ADAPTER_TCP);
472 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
479 res = CA_STATUS_FAILED;
484 CAResult_t CAHandleRequestResponse()
486 if (!g_isInitialized)
488 OIC_LOG(ERROR, TAG, "not initialized");
489 return CA_STATUS_NOT_INITIALIZED;
492 CAHandleRequestResponseCallbacks();
497 CAResult_t CASelectCipherSuite(const uint16_t cipher, CATransportAdapter_t adapter)
499 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
500 OIC_LOG_V(DEBUG, TAG, "cipher : %d , CATransportAdapter : %d", cipher, adapter);
501 CAResult_t res = CA_STATUS_FAILED;
502 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
503 res = CAsetTlsCipherSuite(cipher);
504 if (CA_STATUS_OK != res)
506 OIC_LOG_V(ERROR, TAG, "Failed to CAsetTlsCipherSuite : %d", res);
509 OIC_LOG(ERROR, TAG, "Method not supported");
511 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
515 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
517 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
518 CAResult_t res = CA_STATUS_FAILED;
519 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
520 // TLS_ECDH_ANON_WITH_AES_128_CBC_SHA256 0xFF00 replaces 0xC018
521 res = CAsetTlsCipherSuite(enable ? 0xFF00 : 0x00);
522 if (CA_STATUS_OK != res)
524 OIC_LOG_V(ERROR, TAG, "Failed to CAsetTlsCipherSuite : %d", res);
527 OIC_LOG(ERROR, TAG, "Method not supported");
529 OIC_LOG_V(ERROR, TAG, "Out %s", __func__);
533 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
534 const uint8_t* label, const size_t labelLen,
535 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
536 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
537 uint8_t* ownerPSK, const size_t ownerPskSize)
539 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
540 CAResult_t res = CA_STATUS_FAILED;
541 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
542 //newOwnerLabel and prevOwnerLabe can be NULL
543 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPskSize)
545 return CA_STATUS_INVALID_PARAM;
548 res = CAsslGenerateOwnerPsk(endpoint, label, labelLen,
549 rsrcServerDeviceID, rsrcServerDeviceIDLen,
550 provServerDeviceID, provServerDeviceIDLen,
551 ownerPSK, ownerPskSize);
552 if (CA_STATUS_OK != res)
554 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
557 OIC_LOG(ERROR, TAG, "Method not supported");
559 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
563 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
565 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
566 CAResult_t res = CA_STATUS_FAILED;
567 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
570 return CA_STATUS_INVALID_PARAM;
573 res = CAinitiateSslHandshake(endpoint);
574 if (CA_STATUS_OK != res)
576 OIC_LOG_V(ERROR, TAG, "Failed to CAinitiateSslHandshake : %d", res);
579 OIC_LOG(ERROR, TAG, "Method not supported");
581 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
585 CAResult_t CAcloseSslSession(const CAEndpoint_t *endpoint)
587 OIC_LOG_V(DEBUG, TAG, "IN : CAcloseSslSession");
588 CAResult_t res = CA_STATUS_FAILED;
589 #if defined (__WITH_DTLS__) || defined(__WITH_TLS__)
592 return CA_STATUS_INVALID_PARAM;
595 res = CAcloseSslConnection(endpoint);
596 if (CA_STATUS_OK != res)
598 OIC_LOG_V(ERROR, TAG, "Failed to CAsslClose : %d", res);
601 OIC_LOG(ERROR, TAG, "Method not supported");
603 OIC_LOG_V(DEBUG, TAG, "OUT : CAcloseSslSession");
608 void CARegisterKeepAliveHandler(CAKeepAliveConnectionCallback ConnHandler)
610 CATCPSetKeepAliveCallbacks(ConnHandler);