Exposing CASelectCipherSuite to OC layer
[platform/upstream/iotivity.git] / resource / src / CAManager.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  *
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.
26  */
27
28 #include "OCApi.h"
29 #include "CAManager.h"
30 #include "cautilinterface.h"
31 #include "casecurityinterface.h"
32
33 using namespace OC;
34
35 namespace
36 {
37         CAManager::AdapterChangedCallback g_adapterHandler = nullptr;
38         CAManager::ConnectionChangedCallback g_connectionHandler = nullptr;
39 }
40
41 OCStackResult convertCAResultToOCResult(CAResult_t caResult)
42 {
43     switch (caResult)
44     {
45         case CA_STATUS_OK:
46             return OC_STACK_OK;
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;
53         default:
54             return OC_STACK_ERROR;
55     }
56 }
57
58 void DefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
59 {
60     if (g_adapterHandler)
61     {
62         g_adapterHandler((OCTransportAdapter) adapter, enabled);
63     }
64 }
65
66 void DefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
67 {
68     if (g_connectionHandler)
69     {
70         std::ostringstream ss;
71
72         if (info->flags & CA_IPV6)
73         {
74             ss << '[' << info->addr << ']';
75         }
76         else
77         {
78             ss << info->addr;
79         }
80         if (info->port)
81         {
82             ss << ':' << info->port;
83         }
84
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));
89
90         g_connectionHandler(ss.str(), connType, isConnected);
91     }
92 }
93
94 OCStackResult CAManager::setNetworkMonitorHandler(AdapterChangedCallback adapterHandler,
95                                                   ConnectionChangedCallback connectionHandler)
96 {
97     g_adapterHandler = adapterHandler;
98     g_connectionHandler = connectionHandler;
99
100     CAResult_t ret = CARegisterNetworkMonitorHandler(DefaultAdapterStateChangedHandler,
101                                                      DefaultConnectionStateChangedHandler);
102
103     return convertCAResultToOCResult(ret);
104 }
105
106 OCStackResult CAManager::setPortNumberToAssign(OCTransportAdapter adapter,
107                                                OCTransportFlags flag, uint16_t port)
108 {
109     CAResult_t ret = CASetPortNumberToAssign((CATransportAdapter_t) adapter,
110                                              (CATransportFlags_t) flag, port);
111
112     return convertCAResultToOCResult(ret);
113 }
114
115 uint16_t CAManager::getAssignedPortNumber(OCTransportAdapter adapter, OCTransportFlags flag)
116 {
117     return CAGetAssignedPortNumber((CATransportAdapter_t) adapter, (CATransportFlags_t) flag);
118 }
119 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
120 OCStackResult CAManager::setCipherSuite(const uint16_t cipher, OCTransportAdapter adapter)
121 {
122     CAResult_t ret = CASelectCipherSuite(cipher, (CATransportAdapter_t) adapter);
123     return convertCAResultToOCResult(ret);
124 }
125 #endif // defined(__WITH_DTLS__) || defined(__WITH_TLS__)