Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNet_UsbSystemNetConnection.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_UsbSystemNetConnection.cpp
19  * @brief               This is the implementation file for _UsbSystemNetConnection class.
20  * @version     3.0
21  *
22  * This file contains the implementation of _UsbSystemNetConnection class.
23  */
24
25 #include <tethering.h>
26 #include <FNetNetConnectionInfo.h>
27 #include <FBaseRtMutexGuard.h>
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
30 #include "FNet_NetTypes.h"
31 #include "FNet_NetConnectionInfoImpl.h"
32 #include "FNet_UsbSystemNetConnection.h"
33 #include "FNet_NetConnectionEvent.h"
34 #include "FNet_NetConnectionEventArg.h"
35
36 using namespace std;
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 void
44 TetheringStateChangedCallback(tethering_client_h client, bool opened, void* pUserData)
45 {
46         SysLog(NID_NET, "TetheringStateChangedCallback() has been called with opened:%d", opened);
47
48         _UsbSystemNetConnection* pConnection = static_cast<_UsbSystemNetConnection*>(pUserData);
49
50         if (opened)
51         {
52                 // USB tethering is ON
53                 SysLog(NID_NET, "USB tethering is ON.");
54                 pConnection->HandleStartEvent();
55                 pConnection->UpdateConnectionInfo(true);
56         }
57         else
58         {
59                 // USB tethering is OFF
60                 SysLog(NID_NET, "USB tethering is OFF.");
61                 pConnection->HandleStopEvent(E_NETWORK_FAILED);
62                 pConnection->UpdateConnectionInfo(false);
63         }
64
65         return;
66 }
67
68 _UsbSystemNetConnection::_UsbSystemNetConnection(void)
69         : __pTetheringHandle(null)
70 {
71 }
72
73 _UsbSystemNetConnection::~_UsbSystemNetConnection(void)
74 {
75 }
76
77 result
78 _UsbSystemNetConnection::Construct(void)
79 {
80         result r = E_SUCCESS;
81         unique_ptr<void, _TetheringDeleter> pTetheringHandle;
82         int ret = TETHERING_ERROR_NONE;
83         tethering_h tetheringHandle = null;
84         bool usbStatus = false;
85
86         SysAssertf(__pTetheringHandle == null,
87                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
88
89         r = _SystemNetConnection::Initialize(L"USB");
90         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
91
92         ret = tethering_create(&tetheringHandle);
93         SysTryCatch(NID_NET, ret == TETHERING_ERROR_NONE && tetheringHandle != null, r = E_SYSTEM, E_SYSTEM,
94                         "[%s] A system error has been occurred. The return value from tethering_create() is %d", GetErrorMessage(E_SYSTEM), ret);
95         pTetheringHandle.reset(tetheringHandle);
96
97         ret = tethering_set_connection_state_changed_cb(tetheringHandle, TETHERING_TYPE_USB, TetheringStateChangedCallback, this);
98         SysTryCatch(NID_NET, ret == TETHERING_ERROR_NONE, r = E_SYSTEM, E_SYSTEM,
99                         "[%s] A system error has been occurred. The return value from tethering_set_connection_state_changed_cb() is %d", GetErrorMessage(E_SYSTEM), ret);
100
101         __pTetheringHandle = move(pTetheringHandle);
102
103         usbStatus = tethering_is_enabled(tetheringHandle, TETHERING_TYPE_USB);
104         if (usbStatus)
105         {
106                 // USB tethering is ON
107                 SysLog(NID_NET, "USB tethering is ON.");
108                 UpdateConnectionInfo(true);
109         }
110         else
111         {
112                 // USB tethering is OFF
113                 SysLog(NID_NET, "USB tethering is OFF. Current status of usb is %d", usbStatus);
114                 UpdateConnectionInfo(false);
115         }
116
117         return r;
118
119 CATCH:
120         _SystemNetConnection::Deinitialize();
121
122         return r;
123 }
124
125 void
126 _UsbSystemNetConnection::UpdateConnectionInfo(bool isStarted)
127 {
128         int ret = TETHERING_ERROR_NONE;
129         tethering_h tetheringHandle = __pTetheringHandle.get();
130
131         SysLog(NID_NET, "UpdateConnectionInfo() has been called with status:%d", isStarted);
132
133         SysAssertf(__pTetheringHandle != null,
134                         "Not yet created ! tetheringHandle should be created before use.");
135
136         MutexGuard locked(*_pLock);
137
138         if (isStarted)
139         {
140                 char* pIpAddr = null;
141                 String ipAddr;
142
143                 ret = tethering_get_ip_address(tetheringHandle, TETHERING_TYPE_USB, TETHERING_ADDRESS_FAMILY_IPV4, &pIpAddr);
144                 SysSecureLog(NID_NET, "tethering_get_ip_address() ret[%d] addr[%s]", ret, pIpAddr);
145                 if ((ret == TETHERING_ERROR_NONE) && (pIpAddr != null))
146                 {
147                         ipAddr = String(pIpAddr);
148                         _pConnectionInfo->SetLocalAddress(ipAddr);
149                         free(pIpAddr);
150                 }
151
152                 ret = tethering_get_subnet_mask(tetheringHandle, TETHERING_TYPE_USB, TETHERING_ADDRESS_FAMILY_IPV4, &pIpAddr);
153                 SysSecureLog(NID_NET, "tethering_get_subnet_mask() ret[%d] addr[%s]", ret, pIpAddr);
154                 if ((ret == TETHERING_ERROR_NONE) && (pIpAddr != null))
155                 {
156                         ipAddr = String(pIpAddr);
157                         _pConnectionInfo->SetSubnetMaskAddress(ipAddr);
158                         free(pIpAddr);
159                 }
160
161                 ret = tethering_get_gateway_address(tetheringHandle, TETHERING_TYPE_USB, TETHERING_ADDRESS_FAMILY_IPV4, &pIpAddr);
162                 SysSecureLog(NID_NET, "tethering_get_gateway_address() ret[%d] addr[%s]", ret, pIpAddr);
163                 if ((ret == TETHERING_ERROR_NONE) && (pIpAddr != null))
164                 {
165                         ipAddr = String(pIpAddr);
166                         _pConnectionInfo->SetDefaultGatewayAddress(ipAddr);
167                         free(pIpAddr);
168                 }
169
170                 char* pDevName = null;
171                 String devName;
172
173                 ret = tethering_get_network_interface_name(tetheringHandle, TETHERING_TYPE_USB, &pDevName);
174                 SysSecureLog(NID_NET, "tethering_get_network_interface_name() ret[%d] devName[%s]", ret, pDevName);
175                 if ((ret == TETHERING_ERROR_NONE) && (pDevName != null))
176                 {
177                         devName = String(pDevName);
178                         _pConnectionInfo->SetDeviceName(devName);
179                         free(pDevName);
180                 }
181
182                 _bearerType = NET_BEARER_USB;
183                 _connectionState = NET_CONNECTION_STATE_STARTED;
184                 _pConnectionInfo->SetBearerType(NET_BEARER_USB);
185                 _pConnectionInfo->SetProtocolType(NET_PROTO_TYPE_IPV4);
186                 _pConnectionInfo->SetLocalAddressScheme(NET_ADDRESS_SCHEME_STATIC);
187         }
188         else
189         {
190                 _bearerType = NET_BEARER_NONE;
191                 _connectionState = NET_CONNECTION_STATE_STOPPED;
192                 _pConnectionInfo->Clear();
193         }
194
195         locked.Unlock();
196 }
197
198 } } // Tizen::Net