Merge "Add the exception handling when using manual cert mode" into tizen_2.1
[platform/framework/native/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         int ret = CONNECTION_ERROR_NONE;
49         connection_h connectionHandle = null;
50
51         SysAssertf(!__isConstructed,
52                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
53
54         ret = connection_create(&connectionHandle);
55         SysTryReturnResult(NID_NET, ret == CONNECTION_ERROR_NONE, E_SYSTEM,
56                         "A system error has been occurred. The return value from connection_create() is %d", GetErrorMessage(E_SYSTEM), ret);
57
58         __pConnectionHandle.reset(connectionHandle);
59         __isConstructed = true;
60
61         return r;
62 }
63
64 long long
65 _NetStatisticsImpl::GetNetStatisticsInfo(NetBearerType operationMode, NetStatisticsInfoType netStatType) const
66 {
67         int err = CONNECTION_ERROR_NONE;
68         long long returnVal = INVALID_HANDLE;
69
70         ClearLastResult();
71
72         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
73
74         SysTryReturn(NID_NET, (operationMode == NET_BEARER_WIFI || operationMode == NET_BEARER_PS), INVALID_HANDLE, E_INVALID_ARG,
75                         "[%s] Invalid argument is used. operationMode=%d", GetErrorMessage(E_INVALID_ARG), operationMode);
76
77         //get net statistics info based on type
78         switch (netStatType)
79         {
80         //supported cases
81         case NET_STAT_RCVD_SIZE:
82                 if (operationMode == NET_BEARER_PS)
83                 {
84                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &returnVal);
85                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_RCVD_SIZE, Value returned: %d", returnVal);
86                 }
87                 else if (operationMode == NET_BEARER_WIFI)
88                 {
89                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA, &returnVal);
90                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_RCVD_SIZE, Value returned: %d", returnVal);
91                 }
92                 else
93                 {
94                         // should not reach here.
95                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
96                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
97                 }
98
99                 break;
100
101         case NET_STAT_SENT_SIZE:
102                 if (operationMode == NET_BEARER_PS)
103                 {
104                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &returnVal);
105                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_SENT_SIZE, Value returned: %d", returnVal);
106                 }
107                 else if (operationMode == NET_BEARER_WIFI)
108                 {
109                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA, &returnVal);
110                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type :NET_STAT_SENT_SIZE, Value returned: %d", returnVal);
111                 }
112                 else
113                 {
114                         // should not reach here.
115                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
116                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
117                 }
118
119                 break;
120
121         case NET_STAT_TOTAL_RCVD_SIZE:
122                 if (operationMode == NET_BEARER_PS)
123                 {
124                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &returnVal);
125                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_TOTAL_RCVD_SIZE, Value returned: %d", returnVal);
126                 }
127                 else if (operationMode == NET_BEARER_WIFI)
128                 {
129                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA, &returnVal);
130                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_TOTAL_RCVD_SIZE, Value returned: %d", returnVal);
131                 }
132                 else
133                 {
134                         // should not reach here.
135                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
136                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
137                 }
138
139                 break;
140
141         case NET_STAT_TOTAL_SENT_SIZE:
142                 if (operationMode == NET_BEARER_PS)
143                 {
144                         err = connection_get_statistics(CONNECTION_TYPE_CELLULAR, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &returnVal);
145                         SysLog(NID_NET, "Bearer type: NET_BEARER_PS, Statistics type: NET_STAT_TOTAL_SENT_SIZE, Value returned: %d", returnVal);
146                 }
147                 else if (operationMode == NET_BEARER_WIFI)
148                 {
149                         err = connection_get_statistics(CONNECTION_TYPE_WIFI, CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA, &returnVal);
150                         SysLog(NID_NET, "Bearer type: NET_BEARER_WIFI, Statistics type: NET_STAT_TOTAL_SENT_SIZE, Value returned: %d", returnVal);
151                 }
152                 else
153                 {
154                         // should not reach here.
155                         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo invalid mode: %d", operationMode);
156                         SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
157                 }
158
159                 break;
160
161         default:
162                 SysTryReturn(NID_NET, false, INVALID_HANDLE, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
163
164                 break;
165         }
166
167         // make sure u catch error
168         SysLog(NID_NET, "_NetStatisticsImpl::GetNetStatisticsInfo err code: %d", err);
169         SysTryReturn(NID_NET, err == CONNECTION_ERROR_NONE, INVALID_HANDLE, E_SYSTEM,
170                         "[%s] A system error has been occurred. Operation failed .", GetErrorMessage(E_SYSTEM));
171
172         return returnVal;
173 }
174
175 result
176 _NetStatisticsImpl::Reset(NetBearerType operationMode, NetStatisticsInfoType netStatType)
177 {
178         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
179
180         result r = E_SUCCESS;
181
182         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
183         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
184                         "A system error has been occurred. Failed to get an IPC proxy.");
185
186         r = pProxy->ResetNetStatistics(operationMode, netStatType);
187         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
188
189         return r;
190 }
191
192 result
193 _NetStatisticsImpl::ResetAll(NetBearerType operationMode)
194 {
195         SysAssertf(__isConstructed, "Not yet constructed. Construct() should be called before use.");
196
197         result r = E_SUCCESS;
198
199         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
200         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
201                         "A system error has been occurred. Failed to get an IPC proxy.");
202
203         r = pProxy->ResetAllNetStatistics(operationMode);
204         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
205
206         return r;
207 }
208
209 _NetStatisticsImpl*
210 _NetStatisticsImpl::GetInstance(NetStatistics& netStatistics)
211 {
212         return netStatistics.__pNetStatisticsImpl;      
213 }
214
215 const _NetStatisticsImpl*
216 _NetStatisticsImpl::GetInstance(const NetStatistics& netStatistics)
217 {
218         return netStatistics.__pNetStatisticsImpl;
219 }
220
221 } } // Tizen::Net