Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_ManagedNetConnectionImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FNet_ManagedNetConnectionImpl.cpp
20  * @brief               This is the implementation for the _ManagedNetConnectionImpl class.
21  */
22
23 #include <FNetManagedNetConnection.h>
24 #include <FBaseSysLog.h>
25 #include "FNet_ManagedNetConnectionImpl.h"
26 #include "FNet_NetConnectionManagerImpl.h"
27 #include "FNet_NetUtility.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Runtime;
31
32 namespace Tizen { namespace Net {
33
34 _ManagedNetConnectionImpl::_ManagedNetConnectionImpl(const ManagedNetConnection* pConnection)
35         : __isRefAdded(false)
36         , __pConnection(pConnection)
37         , __pListener(null)
38 {
39 }
40
41 _ManagedNetConnectionImpl::~_ManagedNetConnectionImpl(void)
42 {
43         result r = E_SUCCESS;
44         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
45
46         if (__pListener != null)
47         {
48                 if (pManager != null)
49                 {
50                         r = pManager->RemoveManagedNetConnectionEventListener(*__pListener);
51                         __pListener = null;
52                 }
53         }
54
55         if (__isRefAdded)
56         {
57                 if (pManager != null)
58                 {
59                         pManager->ReleaseManagedNetConnection();
60                         __isRefAdded = false;
61                 }
62         }
63
64         __pConnection = null;
65 }
66
67 result
68 _ManagedNetConnectionImpl::SetManagedNetConnectionEventListener(IManagedNetConnectionEventListener* pListener)
69 {
70         result r = E_SUCCESS;
71         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
72
73         if (pManager != null)
74         {
75                 if (__pListener != null)
76                 {
77                         r = pManager->RemoveManagedNetConnectionEventListener(*__pListener);
78                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
79
80                         __pListener = null;
81                 }
82
83                 if (pListener != null)
84                 {
85                         r = pManager->AddManagedNetConnectionEventListener(*pListener, __pConnection);
86                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
87
88                         __pListener = pListener;
89                 }
90         }
91
92         return r;
93 }
94
95 result
96 _ManagedNetConnectionImpl::Start(void)
97 {
98         result r = E_SUCCESS;
99         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
100
101         if (!__isRefAdded)
102         {
103                 if (pManager != null)
104                 {
105                         r = pManager->AddRefManagedNetConnection();
106                         if (r == E_SUCCESS)
107                         {
108                                 __isRefAdded = true;
109                         }
110                 }
111                 else
112                 {
113                         SysLogException(NID_NET, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
114                 }
115         }
116
117         return r;
118 }
119
120 ManagedNetConnection*
121 _ManagedNetConnectionImpl::CreateInstanceN(void)
122 {
123         ManagedNetConnection* pConnection = null;
124
125         pConnection = new (std::nothrow) ManagedNetConnection();
126         SysTryReturn(NID_NET, pConnection != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
127
128         return pConnection;
129 }
130
131 NetConnectionState
132 _ManagedNetConnectionImpl::GetConnectionState(void) const
133 {
134         ClearLastResult();
135
136         NetConnectionState state = NET_CONNECTION_STATE_NONE;
137         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
138
139         if (pManager != null)
140         {
141                 state = pManager->GetManagedNetConnectionState();
142         }
143
144         return state;
145 }
146
147 NetConnectionState
148 _ManagedNetConnectionImpl::QueryConnectionState(String& devName) const
149 {
150         ClearLastResult();
151
152         NetConnectionState state = NET_CONNECTION_STATE_NONE;
153         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
154
155         if (pManager != null)
156         {
157                 state = pManager->QueryManagedNetConnectionState(devName);
158         }
159
160         return state;
161 }
162
163 NetAccountId
164 _ManagedNetConnectionImpl::GetNetAccountId(void) const
165 {
166         ClearLastResult();
167
168         NetAccountId netAccountId = INVALID_HANDLE;
169         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
170
171         if (pManager != null)
172         {
173                 netAccountId = pManager->GetManagedNetAccountId();
174         }
175
176         return netAccountId;
177 }
178
179 const NetConnectionInfo*
180 _ManagedNetConnectionImpl::GetNetConnectionInfo(void) const
181 {
182         ClearLastResult();
183
184         const NetConnectionInfo* pInfo = null;
185         _NetConnectionManagerImpl* pManager = _NetConnectionManagerImpl::GetInstance();
186
187         if (pManager != null)
188         {
189                 pInfo = pManager->GetManagedNetConnectionInfo();
190         }
191
192         return pInfo;
193 }
194
195 _ManagedNetConnectionImpl*
196 _ManagedNetConnectionImpl::GetInstance(ManagedNetConnection& managedNetConnection)
197 {
198         return managedNetConnection.__pManagedNetConnectionImpl;
199 }
200
201 const _ManagedNetConnectionImpl*
202 _ManagedNetConnectionImpl::GetInstance(const ManagedNetConnection& managedNetConnection)
203 {
204         return managedNetConnection.__pManagedNetConnectionImpl;
205 }
206
207 }  } // Tizen::Net
208