Initialize Tizen 2.3
[framework/osp/net.git] / src / FNet_WifiSystemNetConnection.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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  * @file                        FNet_WifiSystemNetConnection.cpp
19  * @brief               This is the implementation file for _WifiSystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _WifiSystemNetConnection class.
23  */
24
25 #include <FNetNetConnectionInfo.h>
26 #include <FBaseRtMutexGuard.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNet_NetTypes.h"
30 #include "FNet_WifiSystemNetConnection.h"
31 #include "FNet_NetConnectionInfoImpl.h"
32 #include "FNet_NetConnectionEvent.h"
33 #include "FNet_NetConnectionEventArg.h"
34
35 using namespace std;
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40
41 namespace Tizen { namespace Net {
42
43 _WifiSystemNetConnection::_WifiSystemNetConnection(void)
44         : __pIpcProxy(null)
45 {
46 }
47
48 _WifiSystemNetConnection::~_WifiSystemNetConnection(void)
49 {
50 }
51
52 result
53 _WifiSystemNetConnection::Construct(void)
54 {
55         result r = E_SUCCESS;
56         _NetConnectionInfoImpl* pConnectionInfoImpl = null;
57
58         r = _SystemNetConnection::Initialize(L"WIFI");
59         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
60
61         __proxyAddress.Clear();
62
63         __pIpcProxy = _NetIpcProxy::GetInstance();
64         SysTryCatch(NID_NET, __pIpcProxy != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to initialize IPC client.");
65
66         r = __pIpcProxy->AddWifiConnectionListener(this, _connectionState, __connectionInfo);
67         SysTryCatch(NID_NET, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
68
69         _bearerType = __connectionInfo.GetBearerType();
70         pConnectionInfoImpl = _NetConnectionInfoImpl::GetInstance(__connectionInfo);
71         if (pConnectionInfoImpl != null)
72         {
73                 __proxyAddress = pConnectionInfoImpl->GetProxyAddress();
74         }
75
76         SysLog(NID_NET, "Wi-Fi connection state[%d] proxy[%ls]", _connectionState, __proxyAddress.GetPointer());
77
78         return r;
79
80 CATCH:
81         __pIpcProxy = null;
82         _SystemNetConnection::Deinitialize();
83
84         return r;
85 }
86
87 result
88 _WifiSystemNetConnection::Start(_NetConnectionEvent& event)
89 {
90         SysLog(NID_NET, "Starting WIFI connection.");
91
92         result r = E_SUCCESS;
93
94         MutexGuard locked(*_pLock);
95
96         if (_connectionState == NET_CONNECTION_STATE_STARTED)
97         {
98                 // Already Started
99                 SysLog(NID_NET, "WIFI connection is already started.");
100
101                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTED)
102                 {
103                         unique_ptr<_NetConnectionEventArg> pEventArg(new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS));
104                         SysTryReturnResult(NID_NET, pEventArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
105
106                         event.SetConnectionState(NET_CONNECTION_STATE_STARTED);
107                         r = event.FireAsync(*pEventArg);
108                         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
109
110                         _refCount++;
111                         pEventArg.release();
112                 }
113         }
114         else if ((_connectionState == NET_CONNECTION_STATE_STARTING) || (_connectionState == NET_CONNECTION_STATE_STOPPING))
115         {
116                 // Waiting Response
117                 SysLog(NID_NET, "WIFI connection is waiting response.");
118
119                 if (event.GetConnectionState() != NET_CONNECTION_STATE_STARTING)
120                 {
121                         event.SetConnectionState(NET_CONNECTION_STATE_STARTING);
122                         _refCount++;
123                 }
124         }
125         else
126         {
127                 // None or Stopped
128                 SysLog(NID_NET, "WIFI connection is not active.");
129
130                 SysTryReturnResult(NID_NET, !event.IsManagedSystemConnection(), E_INVALID_CONNECTION, "Wi-Fi activation is not allowed for a managed network connection.");
131
132                 r = __pIpcProxy->StartWifiConnection();
133                 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
134
135                 _connectionState = NET_CONNECTION_STATE_STARTING;
136                 event.SetConnectionState(NET_CONNECTION_STATE_STARTING);
137                 _refCount++;
138         }
139
140         locked.Unlock();
141
142         SysLog(NID_NET, "Exit with result[%s].", GetErrorMessage(r));
143
144         return r;
145 }
146
147 void
148 _WifiSystemNetConnection::GetConnectionInfo(NetConnectionInfo& netConnectionInfo)
149 {
150         MutexGuard locked(*_pLock);
151
152         netConnectionInfo = __connectionInfo;
153 }
154
155 String
156 _WifiSystemNetConnection::GetProxyAddress(void) const
157 {
158         SysLog(NID_NET, "GetProxyAddress() has done with proxy[%ls]", __proxyAddress.GetPointer());
159
160         return __proxyAddress;
161 }
162
163 void
164 _WifiSystemNetConnection::ProcessEvent(result error, _NetConnectionEventType type, NetConnectionState state, const NetConnectionInfo& connectionInfo)
165 {
166         _NetConnectionEvent* pEvent = null;
167         _NetConnectionEventArg* pEventArg = null;
168         _NetConnectionInfoImpl* pConnectionInfoImpl = null;
169
170         SysLog(NID_NET, "Notify wifi connection changed event. type[%d] state[%d]", type, state);
171
172         MutexGuard locked(*_pLock);
173
174         _connectionState = state;
175         _bearerType = connectionInfo.GetBearerType();
176         __connectionInfo = connectionInfo;
177
178         pConnectionInfoImpl = _NetConnectionInfoImpl::GetInstance(__connectionInfo);
179         if (pConnectionInfoImpl != null)
180         {
181                 __proxyAddress = pConnectionInfoImpl->GetProxyAddress();
182         }
183
184         unique_ptr<IEnumerator> pEnum(_pEventList->GetEnumeratorN());
185         if (pEnum != null)
186         {
187                 while (pEnum->MoveNext() == E_SUCCESS)
188                 {
189                         pEvent = dynamic_cast<_NetConnectionEvent*>(pEnum->GetCurrent());
190                         if (pEvent != null)
191                         {
192                                 if (type == _NET_CONNECTION_EVENT_TYPE_STARTED)
193                                 {
194                                         if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
195                                         {
196                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STARTED);
197                                                 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, E_SUCCESS);
198                                         }
199                                 }
200                                 else if (type == _NET_CONNECTION_EVENT_TYPE_STOPPED)
201                                 {
202                                         if (pEvent->GetConnectionState() == NET_CONNECTION_STATE_STARTING)
203                                         {
204                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
205                                                 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STARTED, error);
206                                         }
207                                         else if ((pEvent->GetConnectionState() != NET_CONNECTION_STATE_NONE) &&
208                                                 (pEvent->GetConnectionState() != NET_CONNECTION_STATE_STOPPED))
209                                         {
210                                                 pEvent->SetConnectionState(NET_CONNECTION_STATE_STOPPED);
211                                                 pEventArg = new (std::nothrow) _NetConnectionEventArg(_NET_CONNECTION_EVENT_TYPE_STOPPED, error);
212                                         }
213                                 }
214                                 else
215                                 {
216                                         pEventArg = null;
217                                 }
218
219                                 if (pEventArg != null)
220                                 {
221                                         pEvent->FireAsync(*pEventArg);
222                                 }
223                         }
224                 }
225         }
226 }
227
228 void
229 _WifiSystemNetConnection::ProcessProxyChangedEvent(const String& proxy)
230 {
231         if (_connectionState == NET_CONNECTION_STATE_STARTED)
232         {
233                 __proxyAddress = proxy;
234         }
235 }
236
237 } } // Tizen::Net