Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/net.git] / src / FNetNetStatistics.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                        FNetNetStatistics.cpp
19  * @brief               This is the implementation for the FNetNetStatistics class.
20  */
21
22 #include <unique_ptr.h>
23 #include <FNetNetTypes.h>
24 #include <FNetNetStatistics.h>
25 #include <FNet_NetTypes.h>
26 #include <FBaseSysLog.h>
27 #include <FSec_AccessController.h>
28 #include <FSys_SystemInfoImpl.h>
29 #include "FNet_NetStatisticsImpl.h"
30
31 using namespace std;
32 using namespace Tizen::Base;
33 using namespace Tizen::Security;
34 using namespace Tizen::System;
35
36 namespace Tizen { namespace Net
37 {
38
39 NetStatistics::NetStatistics(void)
40         : __pNetStatisticsImpl(null)
41 {
42 }
43
44 result
45 NetStatistics::Construct(void)
46 {
47         result r = E_SUCCESS;
48
49         SysAssertf(__pNetStatisticsImpl == null,
50                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
51
52         unique_ptr<_NetStatisticsImpl> pNetStatisticsImpl(new (std::nothrow) _NetStatisticsImpl());
53         SysTryReturnResult(NID_NET, pNetStatisticsImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
54
55         r = pNetStatisticsImpl->Construct();
56         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
57
58         __pNetStatisticsImpl = pNetStatisticsImpl.release();
59
60         return r;
61 }
62
63 NetStatistics::~NetStatistics(void)
64 {
65         delete __pNetStatisticsImpl;
66         __pNetStatisticsImpl = null;
67 }
68
69 long long
70 NetStatistics::GetNetStatisticsInfo(NetBearerType operationMode, NetStatisticsInfoType netStatType) const
71 {
72         result r = E_SUCCESS;
73         long long ret = INVALID_HANDLE;
74
75         ClearLastResult();
76
77         r = _AccessController::CheckUserPrivilege(_PRV_NETWORK_STATISTICS_READ, _PRV_NETWORK_STATISTICS);
78         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
79         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r,
80                         "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
81
82 #ifndef _OSP_EMUL_
83         if (operationMode == NET_BEARER_WIFI)
84         {
85                 static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
86
87                 bool isWifiSupported = false;
88
89             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
90             SysTryReturn(NID_NET, r == E_SUCCESS && isWifiSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
91                         "[%s] Wi-Fi is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
92         }
93         else if (operationMode == NET_BEARER_PS)
94         {
95                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
96
97                 bool isTelephonySupported = false;
98
99             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
100             SysTryReturn(NID_NET, r == E_SUCCESS && isTelephonySupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
101                         "[%s] Telephony is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
102         }
103 #endif // !_OSP_EMUL_
104
105         SysAssertf(__pNetStatisticsImpl != null, "Not yet constructed. Construct() should be called before use.");
106
107         ret = __pNetStatisticsImpl->GetNetStatisticsInfo(operationMode, netStatType);
108         r = GetLastResult();
109         SysTryReturn(NID_NET, r == E_SUCCESS, ret, r, "[%s] Propagating.", GetErrorMessage(r));
110
111         return ret;
112 }
113
114 result
115 NetStatistics::Reset(NetBearerType operationMode, NetStatisticsInfoType netStatType)
116 {
117         result r = E_SUCCESS;
118
119         SysAssertf(__pNetStatisticsImpl != null, "Not yet constructed. Construct() should be called before use.");
120
121 #ifndef _OSP_EMUL_
122         if (operationMode == NET_BEARER_WIFI)
123         {
124                 static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
125
126                 bool isWifiSupported = false;
127
128             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
129             SysTryReturnResult(NID_NET, r == E_SUCCESS && isWifiSupported, E_UNSUPPORTED_OPERATION, "Wi-Fi is not supported.");
130         }
131         else if (operationMode == NET_BEARER_PS)
132         {
133                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
134
135                 bool isTelephonySupported = false;
136
137             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
138             SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
139         }
140 #endif // !_OSP_EMUL_
141
142         r = __pNetStatisticsImpl->Reset(operationMode, netStatType);
143         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
144
145         return r;
146 }
147
148 result
149 NetStatistics::ResetAll(NetBearerType operationMode)
150 {
151         result r = E_SUCCESS;
152
153         SysAssertf(__pNetStatisticsImpl != null, "Not yet constructed. Construct() should be called before use.");
154
155 #ifndef _OSP_EMUL_
156         if (operationMode == NET_BEARER_WIFI)
157         {
158                 static const wchar_t _WIFI[] = L"http://tizen.org/feature/network.wifi";
159
160                 bool isWifiSupported = false;
161
162             r = _SystemInfoImpl::GetSysInfo(_WIFI, isWifiSupported);
163             SysTryReturnResult(NID_NET, r == E_SUCCESS && isWifiSupported, E_UNSUPPORTED_OPERATION, "Wi-Fi is not supported.");
164         }
165         else if (operationMode == NET_BEARER_PS)
166         {
167                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
168
169                 bool isTelephonySupported = false;
170
171             r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
172             SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
173         }
174 #endif // !_OSP_EMUL_
175
176         r = __pNetStatisticsImpl->ResetAll(operationMode);
177         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
178
179         return r;
180 }
181
182 } } // Tizen::Net