merge with master
[framework/osp/net.git] / src / FNet_NetStatisticsImpl.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_NetStatisticsImpl.cpp
19  * @brief               This is the implementation for the FNet_NetStatisticsImpl class.
20  */
21
22 #include <net_connection.h>
23 #include <FNetNetTypes.h>
24 #include <FNetNetStatistics.h>
25 #include <FBaseSysLog.h>
26 #include "FNet_NetTypes.h"
27 #include "FNet_NetStatisticsImpl.h"
28 #include "FNet_NetIpcProxy.h"
29
30 using namespace Tizen::Base;
31
32 namespace Tizen { namespace Net
33 {
34
35 _NetStatisticsImpl::_NetStatisticsImpl(void)
36         : __isConstructed(false)
37 {
38 }
39
40 _NetStatisticsImpl::~_NetStatisticsImpl(void)
41 {
42 }
43
44 result
45 _NetStatisticsImpl::Construct()
46 {
47         result r = E_SUCCESS;
48
49         SysAssertf(!__isConstructed,
50                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
51
52         __isConstructed = true;
53
54         return r;
55 }
56
57 long long
58 _NetStatisticsImpl::GetNetStatisticsInfo(NetBearerType operationMode, NetStatisticsInfoType netStatType) const
59 {
60         int err = CONNECTION_ERROR_NONE;
61         long long returnVal = INVALID_HANDLE;
62
63         ClearLastResult();
64
65         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
66
67         SysTryReturn(NID_NET, (operationMode == NET_BEARER_WIFI || operationMode == NET_BEARER_PS), INVALID_HANDLE, E_INVALID_ARG,
68                         "[%s] Invalid argument is used. operationMode=%d", GetErrorMessage(E_INVALID_ARG), operationMode);
69
70         //get net statistics info based on type
71         switch (netStatType)
72         {
73         //supported cases
74         case NET_STAT_RCVD_SIZE:
75                 if (operationMode == NET_BEARER_PS)
76                 {
77                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &returnVal);
78                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_RCVD_SIZE, Value returned: %d", returnVal);
79                 }
80                 else if (operationMode == NET_BEARER_WIFI)
81                 {
82                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &returnVal);
83                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_RCVD_SIZE, Value returned: %d", returnVal);
84                 }
85                 else
86                 {
87                         // should not reach here.
88                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
89                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
90                 }
91
92                 break;
93
94         case NET_STAT_SENT_SIZE:
95                 if (operationMode == NET_BEARER_PS)
96                 {
97                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &returnVal);
98                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_SENT_SIZE, Value returned: %d", returnVal);
99                 }
100                 else if (operationMode == NET_BEARER_WIFI)
101                 {
102                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &returnVal);
103                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type :NET_STAT_SENT_SIZE, Value returned: %d", returnVal);
104                 }
105                 else
106                 {
107                         // should not reach here.
108                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
109                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
110                 }
111
112                 break;
113
114         case NET_STAT_TOTAL_RCVD_SIZE:
115                 if (operationMode == NET_BEARER_PS)
116                 {
117                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &returnVal);
118                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_TOTAL_RCVD_SIZE, Value returned: %d", returnVal);
119                 }
120                 else if (operationMode == NET_BEARER_WIFI)
121                 {
122                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &returnVal);
123                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_TOTAL_RCVD_SIZE, Value returned: %d", returnVal);
124                 }
125                 else
126                 {
127                         // should not reach here.
128                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
129                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
130                 }
131
132                 break;
133
134         case NET_STAT_TOTAL_SENT_SIZE:
135                 if (operationMode == NET_BEARER_PS)
136                 {
137                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &returnVal);
138                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_TOTAL_SENT_SIZE, Value returned: %d", returnVal);
139                 }
140                 else if (operationMode == NET_BEARER_WIFI)
141                 {
142                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &returnVal);
143                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_TOTAL_SENT_SIZE, Value returned: %d", returnVal);
144                 }
145                 else
146                 {
147                         // should not reach here.
148                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
149                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
150                 }
151
152                 break;
153
154         default:
155                 SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
156
157                 break;
158         }
159
160         // make sure u catch error
161         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo err code: %d", err);
162         SysTryReturn(NID_NET, err == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
163                         "[%s] A system error has been occurred. Operation failed .", GetErrorMessage(E_SYSTEM));
164
165         return returnVal;
166 }
167
168 result
169 _NetStatisticsImpl::Reset(NetBearerType operationMode, NetStatisticsInfoType netStatType)
170 {
171         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
172
173         result r = E_SUCCESS;
174
175         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
176         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
177                         "A system error has been occurred. Failed to get an IPC proxy.");
178
179         r = pProxy->ResetNetStatistics(operationMode, netStatType);
180         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
181
182         return r;
183 }
184
185 result
186 _NetStatisticsImpl::ResetAll(NetBearerType operationMode)
187 {
188         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
189
190         result r = E_SUCCESS;
191
192         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
193         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
194                         "A system error has been occurred. Failed to get an IPC proxy.");
195
196         r = pProxy->ResetAllNetStatistics(operationMode);
197         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
198
199         return r;
200 }
201
202 _NetStatisticsImpl*
203 _NetStatisticsImpl::GetInstance(NetStatistics& netStatistics)
204 {
205         return netStatistics.__pNetStatisticsImpl;      
206 }
207
208 const _NetStatisticsImpl*
209 _NetStatisticsImpl::GetInstance(const NetStatistics& netStatistics)
210 {
211         return netStatistics.__pNetStatisticsImpl;
212 }
213
214 } } // Tizen::Net