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"
37 CAGlobals_t caglobals = { 0 };
39 #define TAG "CA_CONN_MGR"
41 static bool g_isInitialized = false;
44 // CAAdapterNetDTLS will register the callback.
45 // Taking callback all the way through adapters not the right approach, hence calling here.
46 extern void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
49 CAResult_t CAInitialize()
51 OIC_LOG(DEBUG, TAG, "CAInitialize");
55 CAResult_t res = CAInitializeMessageHandler();
56 if (res != CA_STATUS_OK)
58 OIC_LOG(ERROR, TAG, "CAInitialize has failed");
61 g_isInitialized = true;
68 OIC_LOG(DEBUG, TAG, "CATerminate");
72 CATerminateMessageHandler();
73 CATerminateNetworkType();
75 g_isInitialized = false;
79 CAResult_t CAStartListeningServer()
81 OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
85 return CA_STATUS_NOT_INITIALIZED;
88 return CAStartListeningServerAdapters();
91 CAResult_t CAStartDiscoveryServer()
93 OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
97 return CA_STATUS_NOT_INITIALIZED;
100 return CAStartDiscoveryServerAdapters();
103 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
104 CAErrorCallback ErrorHandler)
106 OIC_LOG(DEBUG, TAG, "CARegisterHandler");
110 OIC_LOG(DEBUG, TAG, "CA is not initialized");
114 CASetInterfaceCallbacks(ReqHandler, RespHandler, ErrorHandler);
118 CAResult_t CARegisterDTLSCredentialsHandler(CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
120 OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
124 return CA_STATUS_NOT_INITIALIZED;
127 CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
130 #endif //__WITH_DTLS__
132 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
133 CATransportAdapter_t adapter,
136 CAEndpoint_t **object)
140 OIC_LOG(ERROR, TAG, "Invalid Parameter");
141 return CA_STATUS_INVALID_PARAM;
144 CAEndpoint_t *endpoint = CACreateEndpointObject(flags, adapter, addr, port);
147 return CA_STATUS_FAILED;
153 void CADestroyEndpoint(CAEndpoint_t *rep)
155 OIC_LOG(DEBUG, TAG, "CADestroyEndpoint");
160 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
162 OIC_LOG(DEBUG, TAG, "CAGenerateToken");
166 return CA_STATUS_NOT_INITIALIZED;
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);
217 CAResult_t CASendResponse(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
219 OIC_LOG(DEBUG, TAG, "CASendResponse");
223 return CA_STATUS_NOT_INITIALIZED;
226 return CADetachResponseMessage(object, responseInfo);
230 CAResult_t CASelectNetwork(CATransportAdapter_t interestedNetwork)
232 OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
236 return CA_STATUS_NOT_INITIALIZED;
239 CAResult_t res = CA_STATUS_OK;
241 if (interestedNetwork & CA_ADAPTER_IP)
243 res = CAAddNetworkType(CA_ADAPTER_IP);
244 OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_IP_ADAPTER) function returns error : %d", res);
246 else if (interestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
248 res = CAAddNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
249 OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_RFCOMM_ADAPTER) function returns error : %d", res);
251 else if (interestedNetwork & CA_ADAPTER_GATT_BTLE)
253 res = CAAddNetworkType(CA_ADAPTER_GATT_BTLE);
254 OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_GATT_ADAPTER) function returns error : %d", res);
258 else if (interestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
260 res = CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS);
261 OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns error : %d",
267 res = CA_NOT_SUPPORTED;
272 CAResult_t CAUnSelectNetwork(CATransportAdapter_t nonInterestedNetwork)
274 OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
278 return CA_STATUS_NOT_INITIALIZED;
281 CAResult_t res = CA_STATUS_OK;
283 if (nonInterestedNetwork & CA_ADAPTER_IP)
285 res = CARemoveNetworkType(CA_ADAPTER_IP);
286 OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_IP_ADAPTER) function returns error : %d", res);
288 else if (nonInterestedNetwork & CA_ADAPTER_RFCOMM_BTEDR)
290 res = CARemoveNetworkType(CA_ADAPTER_RFCOMM_BTEDR);
291 OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_RFCOMM_ADAPTER) function returns error : %d", res);
293 else if (nonInterestedNetwork & CA_ADAPTER_GATT_BTLE)
295 res = CARemoveNetworkType(CA_ADAPTER_GATT_BTLE);
296 OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_GATT_ADAPTER) function returns error : %d", res);
299 else if (nonInterestedNetwork & CA_ADAPTER_REMOTE_ACCESS)
301 res = CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS);
302 OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_ADAPTER_REMOTE_ACCESS) function returns error : %d",
308 res = CA_STATUS_FAILED;
313 CAResult_t CAHandleRequestResponse()
315 if (!g_isInitialized)
317 OIC_LOG(ERROR, TAG, "not initialized");
318 return CA_STATUS_NOT_INITIALIZED;
321 CAHandleRequestResponseCallbacks();
328 CAResult_t CASelectCipherSuite(const uint16_t cipher)
330 OIC_LOG_V(DEBUG, TAG, "CASelectCipherSuite");
332 return CADtlsSelectCipherSuite(cipher);
335 CAResult_t CAEnableAnonECDHCipherSuite(const bool enable)
337 OIC_LOG_V(DEBUG, TAG, "CAEnableAnonECDHCipherSuite");
339 return CADtlsEnableAnonECDHCipherSuite(enable);
342 CAResult_t CAGenerateOwnerPSK(const CAEndpoint_t* endpoint,
343 const uint8_t* label, const size_t labelLen,
344 const uint8_t* rsrcServerDeviceID, const size_t rsrcServerDeviceIDLen,
345 const uint8_t* provServerDeviceID, const size_t provServerDeviceIDLen,
346 uint8_t* ownerPSK, const size_t ownerPSKSize)
348 OIC_LOG_V(DEBUG, TAG, "IN : CAGenerateOwnerPSK");
350 CAResult_t res = CA_STATUS_OK;
352 //newOwnerLabel and prevOwnerLabe can be NULL
353 if (!endpoint || !label || 0 == labelLen || !ownerPSK || 0 == ownerPSKSize)
355 return CA_STATUS_INVALID_PARAM;
358 res = CADtlsGenerateOwnerPSK(endpoint, label, labelLen,
359 rsrcServerDeviceID, rsrcServerDeviceIDLen,
360 provServerDeviceID, provServerDeviceIDLen,
361 ownerPSK, ownerPSKSize);
362 if (CA_STATUS_OK != res)
364 OIC_LOG_V(ERROR, TAG, "Failed to CAGenerateOwnerPSK : %d", res);
367 OIC_LOG_V(DEBUG, TAG, "OUT : CAGenerateOwnerPSK");
372 CAResult_t CAInitiateHandshake(const CAEndpoint_t *endpoint)
374 OIC_LOG_V(DEBUG, TAG, "IN : CAInitiateHandshake");
375 CAResult_t res = CA_STATUS_OK;
379 return CA_STATUS_INVALID_PARAM;
382 res = CADtlsInitiateHandshake(endpoint);
383 if (CA_STATUS_OK != res)
385 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsInitiateHandshake : %d", res);
388 OIC_LOG_V(DEBUG, TAG, "OUT : CAInitiateHandshake");
393 CAResult_t CACloseDtlsSession(const CAEndpoint_t *endpoint)
395 OIC_LOG_V(DEBUG, TAG, "IN : CACloseDtlsSession");
396 CAResult_t res = CA_STATUS_OK;
400 return CA_STATUS_INVALID_PARAM;
403 res = CADtlsClose(endpoint);
404 if (CA_STATUS_OK != res)
406 OIC_LOG_V(ERROR, TAG, "Failed to CADtlsClose : %d", res);
409 OIC_LOG_V(DEBUG, TAG, "OUT : CACloseDtlsSession");
414 #endif /* __WITH_DTLS__ */