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);
53 CAResult_t CAInitialize()
55 OIC_LOG(DEBUG, TAG, "CAInitialize");
59 CAResult_t res = CAInitializeMessageHandler();
60 if (res != CA_STATUS_OK)
62 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
65 g_isInitialized = true;
72 OIC_LOG(DEBUG, TAG, "CATerminate");
76 CATerminateMessageHandler();
77 CATerminateNetworkType();
79 g_isInitialized = false;
83 CAResult_t CAStartListeningServer()
85 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
89 return CA_STATUS_NOT_INITIALIZED;
92 return CAStartListeningServerAdapters();
95 CAResult_t CAStartDiscoveryServer()
97 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
101 return CA_STATUS_NOT_INITIALIZED;
104 return CAStartDiscoveryServerAdapters();
107 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
108 CAErrorCallback ErrorHandler)
110 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
114 OIC_LOG(DEBUG, TAG, "CA is not initialized");
118 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
122 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
124 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
128 return CA_STATUS_NOT_INITIALIZED;
131 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
134 #endif //__WITH_DTLS__
136 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
137 CATransportAdapter_t adapter,
140 CAEndpoint_t **object)
144 OIC_LOG(ERROR, TAG, "Invalid Parameter");
145 return CA_STATUS_INVALID_PARAM;
148 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
151 return CA_STATUS_FAILED;
157 void CADestroyEndpoint(CAEndpoint_t *rep)
159 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
164 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
166 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
168 return CAGenerateTokenInternal(token, tokenLength);
171 void CADestroyToken(CAToken_t token)
173 OIC_LOG(DEBUG, TAG, "CADestroyToken");
175 CADestroyTokenInternal(token);
177 OIC_LOG(DEBUG, TAG, "OUT");
180 CAResult_t CAGetNetworkInformation(CAEndpoint_t **info, uint32_t *size)
182 OIC_LOG(DEBUG, TAG, "CAGetNetworkInformation");
186 return CA_STATUS_NOT_INITIALIZED;
189 return CAGetNetworkInformationInternal(info, size);
192 CAResult_t CASendRequest(const CAEndpoint_t *object,const CARequestInfo_t *requestInfo)
194 OIC_LOG(DEBUG, TAG, "CASendGetRequest");
198 return CA_STATUS_NOT_INITIALIZED;
201 return CADetachRequestMessage(object, requestInfo);
204 CAResult_t CASendNotification(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
206 OIC_LOG(DEBUG, TAG, "CASendNotification");
210 return CA_STATUS_NOT_INITIALIZED;
213 return CADetachResponseMessage(object, responseInfo);
216 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
218 OIC_LOG(DEBUG, TAG, "CASendResponse");
222 return CA_STATUS_NOT_INITIALIZED;
225 return CADetachResponseMessage(object, responseInfo);
228 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
230 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
234 return CA_STATUS_NOT_INITIALIZED;
237 CAResult_t res = CA_STATUS_OK;
239 if (interestedNetwork & CA_ADAPTER_IP)
241 res = CAAddNetworkType(CA_ADAPTER_IP);
242 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns result: %d", res);
244 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
246 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
247 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
249 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
251 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
252 OIC_LOG_V(DEBUG, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
256 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
258 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
259 OIC_LOG_V(DEBUG, TAG,
260 "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d", res);
265 else if (interestedNetwork & CA_ADAPTER_TCP)
267 res = CAAddNetworkType(CA_ADAPTER_TCP);
268 OIC_LOG_V(DEBUG, TAG,
269 "CAAddNetworkType(CA_ADAPTER_TCP) function returns result : %d", res);
275 res = CA_NOT_SUPPORTED;
280 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
282 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
286 return CA_STATUS_NOT_INITIALIZED;
289 CAResult_t res = CA_STATUS_OK;
291 if (nonInterestedNetwork & CA_ADAPTER_IP)
293 res = CARemoveNetworkType(CA_ADAPTER_IP);
294 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns result : %d", res);
296 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
298 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
299 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns result : %d", res);
301 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
303 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
304 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns result : %d", res);
307 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
309 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
310 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns result : %d",
317 else if (nonInterestedNetwork & CA_ADAPTER_TCP)
319 res = CARemoveNetworkType(CA_ADAPTER_TCP);
320 OIC_LOG_V(DEBUG, TAG, "CARemoveNetworkType(CA_ADAPTER_TCP) function returns result : %d",
327 res = CA_STATUS_FAILED;
332 CAResult_t CAHandleRequestResponse()
334 if (!g_isInitialized)
336 OIC_LOG(ERROR, TAG, "not initialized");
337 return CA_STATUS_NOT_INITIALIZED;
340 CAHandleRequestResponseCallbacks();
347 CAResult_t CASelectCipherSuite(const uint16_t cipher)
349 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
351 return CADtlsSelectCipherSuite(cipher);
354 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
356 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
358 return CADtlsEnableAnonECDHCipherSuite(enable);
361 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
362 const uint8_t* label, const size_t labelLen,
363 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
364 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
365 uint8_t* ownerPSK, const size_t ownerPSKSize)
367 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
369 CAResult_t res = CA_STATUS_OK;
371 //newOwnerLabel and prevOwnerLabe can be NULL
372 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
374 return CA_STATUS_INVALID_PARAM;
377 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
378 rsrcServerDeviceID, rsrcServerDeviceIDLen,
379 provServerDeviceID, provServerDeviceIDLen,
380 ownerPSK, ownerPSKSize);
381 if (CA_STATUS_OK != res)
383 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
386 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
391 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
393 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
394 CAResult_t res = CA_STATUS_OK;
398 return CA_STATUS_INVALID_PARAM;
401 res = CADtlsInitiateHandshake(endpoint);
402 if (CA_STATUS_OK != res)
404 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
407 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
412 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
414 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
415 CAResult_t res = CA_STATUS_OK;
419 return CA_STATUS_INVALID_PARAM;
422 res = CADtlsClose(endpoint);
423 if (CA_STATUS_OK != res)
425 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
428 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
433 #endif /* __WITH_DTLS__ */