1 /* ****************************************************************
3 * Copyright 2016 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 ******************************************************************/
24 * This file contains the implementation of classes and its members related
25 * to CAManager. Network changes status will be passed from CA to application.
29 #include "CAManager.h"
30 #include "cautilinterface.h"
31 #include "casecurityinterface.h"
37 CAManager::AdapterChangedCallback g_adapterHandler = nullptr;
38 CAManager::ConnectionChangedCallback g_connectionHandler = nullptr;
41 OCStackResult convertCAResultToOCResult(CAResult_t caResult)
47 case CA_STATUS_INVALID_PARAM:
48 return OC_STACK_INVALID_PARAM;
49 case CA_STATUS_FAILED:
50 return OC_STACK_ERROR;
51 case CA_NOT_SUPPORTED:
52 return OC_STACK_NOTIMPL;
54 return OC_STACK_ERROR;
58 void DefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
62 g_adapterHandler((OCTransportAdapter) adapter, enabled);
66 void DefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
68 if (g_connectionHandler)
70 std::ostringstream ss;
72 if (info->flags & CA_IPV6)
74 ss << '[' << info->addr << ']';
82 ss << ':' << info->port;
85 OCTransportAdapter adapter = (OCTransportAdapter)info->adapter;
86 OCTransportFlags flags = (OCTransportFlags)info->flags;
87 OCConnectivityType connType = (OCConnectivityType)
88 ((adapter << CT_ADAPTER_SHIFT) | (flags & CT_MASK_FLAGS));
90 g_connectionHandler(ss.str(), connType, isConnected);
94 OCStackResult CAManager::setNetworkMonitorHandler(AdapterChangedCallback adapterHandler,
95 ConnectionChangedCallback connectionHandler)
97 g_adapterHandler = adapterHandler;
98 g_connectionHandler = connectionHandler;
100 CAResult_t ret = CARegisterNetworkMonitorHandler(DefaultAdapterStateChangedHandler,
101 DefaultConnectionStateChangedHandler);
103 return convertCAResultToOCResult(ret);
106 OCStackResult CAManager::setPortNumberToAssign(OCTransportAdapter adapter,
107 OCTransportFlags flag, uint16_t port)
109 CAResult_t ret = CASetPortNumberToAssign((CATransportAdapter_t) adapter,
110 (CATransportFlags_t) flag, port);
112 return convertCAResultToOCResult(ret);
115 uint16_t CAManager::getAssignedPortNumber(OCTransportAdapter adapter, OCTransportFlags flag)
117 return CAGetAssignedPortNumber((CATransportAdapter_t) adapter, (CATransportFlags_t) flag);
119 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
120 OCStackResult CAManager::setCipherSuite(const uint16_t cipher, OCTransportAdapter adapter)
122 CAResult_t ret = CASelectCipherSuite(cipher, (CATransportAdapter_t) adapter);
123 return convertCAResultToOCResult(ret);
125 #endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)