2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
23 #include "statistics.h"
25 static GMainLoop *g_pMainLoop = NULL;
26 static guint g_nTimeoutId = 0;
28 static gboolean CallbackTimeut(gpointer data)
31 g_main_loop_quit(g_pMainLoop);
36 static void RunMainLoop(void)
38 g_pMainLoop = g_main_loop_new(NULL, false);
39 g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
40 g_main_loop_run(g_pMainLoop);
42 g_source_remove(g_nTimeoutId);
46 static void QuitMainLoop(void)
49 g_main_loop_quit(g_pMainLoop);
52 static void AsyncReadyCallback(GObject *source_object,
53 GAsyncResult *res, gpointer user_data)
55 GLOGD("Succeeded to response async callback");
61 this->m_IfaceType = IFACE_UNKNOWN;
62 this->m_TimePeriod = TIME_PERIOD_UNKNOWN;
63 this->m_Interval.from = 0;
64 this->m_Interval.to = 0;
71 Statistics::Statistics()
76 Statistics::~Statistics()
81 error_e Statistics::SetRule(const char *app_id, const char *subscriber_id,
82 iface_type_e iface_type, time_t from, time_t to, time_period_e time_period)
84 if (app_id == NULL || strlen(app_id) == 0)
85 this->m_Rule.m_AppID[0] = '\0';
87 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
89 if (subscriber_id == NULL || strlen(subscriber_id) == 0)
90 this->m_Rule.m_SubscriberID[0] = '\0';
92 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
101 this->m_Rule.m_IfaceType = iface_type;
104 return ERROR_INVALID_PARAMETER;
107 if (from < 0 || to < 0)
108 return ERROR_INVALID_PARAMETER;
110 this->m_Rule.m_Interval.from = from;
111 this->m_Rule.m_Interval.to = to;
113 switch(time_period) {
114 case TIME_PERIOD_UNKNOWN:
115 case TIME_PERIOD_HOUR:
116 case TIME_PERIOD_DAY:
117 case TIME_PERIOD_WEEK:
118 case TIME_PERIOD_MONTH:
119 this->m_Rule.m_TimePeriod = time_period;
122 return ERROR_INVALID_PARAMETER;
128 time_t Statistics::MakeTime(int year, int mon, int day, int hour, int min)
130 struct tm curr = { 0, };
131 curr.tm_year = year - 1900;
132 curr.tm_mon = mon - 1;
136 return mktime(&curr);
139 void Statistics::MakeRuleParams(GVariant **params, int mode)
141 GVariantBuilder *builder;
143 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
145 g_variant_builder_add(builder, "{sv}",
146 STATISTICS_RULE_INTERVAL_FROM,
147 g_variant_new_uint64(this->m_Rule.m_Interval.from));
149 g_variant_builder_add(builder, "{sv}",
150 STATISTICS_RULE_INTERVAL_TO,
151 g_variant_new_uint64(this->m_Rule.m_Interval.to));
153 g_variant_builder_add(builder, "{sv}",
154 STATISTICS_RULE_IFTYPE,
155 g_variant_new_uint16(this->m_Rule.m_IfaceType));
157 g_variant_builder_add(builder, "{sv}",
158 STATISTICS_RULE_TIME_PERIOD,
159 g_variant_new_int32(this->m_Rule.m_TimePeriod));
163 g_variant_builder_add(builder, "{sv}",
164 STATISTICS_RULE_APP_ID,
165 g_variant_new_string(this->m_Rule.m_AppID));
167 g_variant_builder_add(builder, "{sv}",
168 RESET_RULE_SUBSCRIBER_ID,
169 g_variant_new_string(this->m_Rule.m_SubscriberID));
171 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
173 case 1: /* get app */
174 *params = g_variant_new("(s@a{sv})", this->m_Rule.m_AppID, g_variant_builder_end(builder));
176 case 2: /* get total */
177 *params = g_variant_new("(s@a{sv})", "", g_variant_builder_end(builder));
179 case 3: /* get all */
181 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
185 g_variant_builder_unref(builder);
188 error_e Statistics::InitStatistics(void)
190 GVariant *message = NULL;
191 error_e error = ERROR_NONE;
193 message = InvokeMethod(STC_MGR_SERVICE,
194 STC_MGR_STATISTICS_PATH,
195 STC_MGR_STATISTICS_INTERFACE,
196 STC_MGR_METHOD_STATISTICS_INIT,
200 if (message == NULL) {
201 GLOGD("Failed to invoke dbus method");
205 GLOGD("Succeeded to init statistics");
210 error_e Statistics::GetStatistics(void)
212 GVariant *params = NULL;
213 error_e error = ERROR_NONE;
215 MakeRuleParams(¶ms, 1);
217 error = InvokeMethodNonblock(STC_MGR_SERVICE,
218 STC_MGR_STATISTICS_PATH,
219 STC_MGR_STATISTICS_INTERFACE,
220 STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID,
226 if (error != ERROR_NONE) {
227 GLOGD("Failed to invoke dbus method nonblock");
232 GLOGD("Succeeded to get statistics per app ID");
237 error_e Statistics::GetTotalStatistics(void)
239 GVariant *params = NULL;
240 error_e error = ERROR_NONE;
242 MakeRuleParams(¶ms, 2);
244 error = InvokeMethodNonblock(STC_MGR_SERVICE,
245 STC_MGR_STATISTICS_PATH,
246 STC_MGR_STATISTICS_INTERFACE,
247 STC_MGR_METHOD_STATISTICS_GET_TOTAL,
253 if (error != ERROR_NONE) {
254 GLOGD("Failed to invoke dbus method nonblock");
259 GLOGD("Succeeded to get total statistics");
264 error_e Statistics::GetAllStatistics(void)
266 GVariant *params = NULL;
267 error_e error = ERROR_NONE;
269 MakeRuleParams(¶ms, 3);
271 error = InvokeMethodNonblock(STC_MGR_SERVICE,
272 STC_MGR_STATISTICS_PATH,
273 STC_MGR_STATISTICS_INTERFACE,
274 STC_MGR_METHOD_STATISTICS_GET_ALL,
280 if (error != ERROR_NONE) {
281 GLOGD("Failed to invoke dbus method nonblock");
286 GLOGD("Succeeded to get all statistics");
291 error_e Statistics::ResetStatistics(void)
293 GVariant *message = NULL;
294 GVariant *params = NULL;
295 error_e error = ERROR_NONE;
298 MakeRuleParams(¶ms, 0);
300 message = InvokeMethod(STC_MGR_SERVICE,
301 STC_MGR_STATISTICS_PATH,
302 STC_MGR_STATISTICS_INTERFACE,
303 STC_MGR_METHOD_STATISTICS_RESET,
307 if (message == NULL) {
308 GLOGD("Failed to invoke dbus method");
312 g_variant_get(message, "(i)", &result);
313 GLOGD("Succeeded to reset statistics", result);
314 g_variant_unref(message);