2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FNet_UsbSystemNetConnection.cpp
19 * @brief This is the implementation file for _UsbSystemNetConnection class.
22 * This file contains the implementation of _UsbSystemNetConnection class.
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"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
41 namespace Tizen { namespace Net {
44 TetheringStateChangedCallback(tethering_client_h client, bool opened, void* pUserData)
46 SysLog(NID_NET, "TetheringStateChangedCallback() has been called with opened:%d", opened);
48 _UsbSystemNetConnection* pConnection = static_cast<_UsbSystemNetConnection*>(pUserData);
52 // USB tethering is ON
53 SysLog(NID_NET, "USB tethering is ON.");
54 pConnection->HandleStartEvent();
55 pConnection->UpdateConnectionInfo(true);
59 // USB tethering is OFF
60 SysLog(NID_NET, "USB tethering is OFF.");
61 pConnection->HandleStopEvent(E_NETWORK_FAILED);
62 pConnection->UpdateConnectionInfo(false);
68 _UsbSystemNetConnection::_UsbSystemNetConnection(void)
69 : __pTetheringHandle(null)
73 _UsbSystemNetConnection::~_UsbSystemNetConnection(void)
78 _UsbSystemNetConnection::Construct(void)
81 unique_ptr<void, _TetheringDeleter> pTetheringHandle;
82 int ret = TETHERING_ERROR_NONE;
83 tethering_h tetheringHandle = null;
84 bool usbStatus = false;
86 SysAssertf(__pTetheringHandle == null,
87 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
89 r = _SystemNetConnection::Initialize(L"USB");
90 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
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);
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);
101 __pTetheringHandle = move(pTetheringHandle);
103 usbStatus = tethering_is_enabled(tetheringHandle, TETHERING_TYPE_USB);
106 // USB tethering is ON
107 SysLog(NID_NET, "USB tethering is ON.");
108 UpdateConnectionInfo(true);
112 // USB tethering is OFF
113 SysLog(NID_NET, "USB tethering is OFF. Current status of usb is %d", usbStatus);
114 UpdateConnectionInfo(false);
120 _SystemNetConnection::Deinitialize();
126 _UsbSystemNetConnection::UpdateConnectionInfo(bool isStarted)
128 int ret = TETHERING_ERROR_NONE;
129 tethering_h tetheringHandle = __pTetheringHandle.get();
131 SysLog(NID_NET, "UpdateConnectionInfo() has been called with status:%d", isStarted);
133 SysAssertf(__pTetheringHandle != null,
134 "Not yet created ! tetheringHandle should be created before use.");
136 MutexGuard locked(*_pLock);
140 char* pIpAddr = null;
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))
147 ipAddr = String(pIpAddr);
148 _pConnectionInfo->SetLocalAddress(ipAddr);
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))
156 ipAddr = String(pIpAddr);
157 _pConnectionInfo->SetSubnetMaskAddress(ipAddr);
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))
165 ipAddr = String(pIpAddr);
166 _pConnectionInfo->SetDefaultGatewayAddress(ipAddr);
170 char* pDevName = null;
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))
177 devName = String(pDevName);
178 _pConnectionInfo->SetDeviceName(devName);
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);
190 _bearerType = NET_BEARER_NONE;
191 _connectionState = NET_CONNECTION_STATE_STOPPED;
192 _pConnectionInfo->Clear();