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"
26 static void AsyncReadyCallback(GObject *source_object,
27 GAsyncResult *res, gpointer user_data)
29 MainLoop *M = (MainLoop *)user_data;
30 GLOGD("Succeeded to response async callback");
36 this->m_IfaceType = IFACE_UNKNOWN;
37 this->m_TimePeriod = TIME_PERIOD_UNKNOWN;
38 this->m_Interval.from = 0;
39 this->m_Interval.to = 0;
46 Statistics::Statistics()
51 Statistics::~Statistics()
56 error_e Statistics::SetRule(const char *app_id, const char *subscriber_id,
57 iface_type_e iface_type, time_t from, time_t to, time_period_e time_period)
59 if (app_id == NULL || strlen(app_id) == 0)
60 this->m_Rule.m_AppID[0] = '\0';
62 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
64 if (subscriber_id == NULL || strlen(subscriber_id) == 0)
65 this->m_Rule.m_SubscriberID[0] = '\0';
67 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
76 this->m_Rule.m_IfaceType = iface_type;
79 return ERROR_INVALID_PARAMETER;
82 if (from < 0 || to < 0)
83 return ERROR_INVALID_PARAMETER;
85 this->m_Rule.m_Interval.from = from;
86 this->m_Rule.m_Interval.to = to;
89 case TIME_PERIOD_UNKNOWN:
90 case TIME_PERIOD_HOUR:
92 case TIME_PERIOD_WEEK:
93 case TIME_PERIOD_MONTH:
94 this->m_Rule.m_TimePeriod = time_period;
97 return ERROR_INVALID_PARAMETER;
103 time_t Statistics::MakeTime(int year, int mon, int day, int hour, int min)
105 struct tm curr = { 0, };
106 curr.tm_year = year - 1900;
107 curr.tm_mon = mon - 1;
111 return mktime(&curr);
114 void Statistics::MakeRuleParams(GVariant **params, int mode)
116 GVariantBuilder *builder;
118 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
120 g_variant_builder_add(builder, "{sv}",
121 STATISTICS_RULE_INTERVAL_FROM,
122 g_variant_new_uint64(this->m_Rule.m_Interval.from));
124 g_variant_builder_add(builder, "{sv}",
125 STATISTICS_RULE_INTERVAL_TO,
126 g_variant_new_uint64(this->m_Rule.m_Interval.to));
128 g_variant_builder_add(builder, "{sv}",
129 STATISTICS_RULE_IFTYPE,
130 g_variant_new_uint16(this->m_Rule.m_IfaceType));
132 g_variant_builder_add(builder, "{sv}",
133 STATISTICS_RULE_TIME_PERIOD,
134 g_variant_new_int32(this->m_Rule.m_TimePeriod));
138 g_variant_builder_add(builder, "{sv}",
139 STATISTICS_RULE_APP_ID,
140 g_variant_new_string(this->m_Rule.m_AppID));
142 g_variant_builder_add(builder, "{sv}",
143 RESET_RULE_SUBSCRIBER_ID,
144 g_variant_new_string(this->m_Rule.m_SubscriberID));
146 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
148 case 1: /* get app */
149 *params = g_variant_new("(s@a{sv})", this->m_Rule.m_AppID, g_variant_builder_end(builder));
151 case 2: /* get total */
152 *params = g_variant_new("(s@a{sv})", "", g_variant_builder_end(builder));
154 case 3: /* get all */
156 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
160 g_variant_builder_unref(builder);
163 error_e Statistics::InitStatistics(void)
165 GVariant *message = NULL;
166 error_e error = ERROR_NONE;
168 message = InvokeMethod(STC_MGR_SERVICE,
169 STC_MGR_STATISTICS_PATH,
170 STC_MGR_STATISTICS_INTERFACE,
171 STC_MGR_METHOD_STATISTICS_INIT,
175 if (message == NULL) {
176 GLOGD("Failed to invoke dbus method");
180 GLOGD("Succeeded to init statistics");
185 error_e Statistics::GetStatistics(void)
188 GVariant *params = NULL;
189 error_e error = ERROR_NONE;
191 MakeRuleParams(¶ms, 1);
193 error = InvokeMethodNonblock(STC_MGR_SERVICE,
194 STC_MGR_STATISTICS_PATH,
195 STC_MGR_STATISTICS_INTERFACE,
196 STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID,
202 if (error != ERROR_NONE) {
203 GLOGD("Failed to invoke dbus method nonblock");
208 GLOGD("Succeeded to get statistics per app ID");
213 error_e Statistics::GetTotalStatistics(void)
216 GVariant *params = NULL;
217 error_e error = ERROR_NONE;
219 MakeRuleParams(¶ms, 2);
221 error = InvokeMethodNonblock(STC_MGR_SERVICE,
222 STC_MGR_STATISTICS_PATH,
223 STC_MGR_STATISTICS_INTERFACE,
224 STC_MGR_METHOD_STATISTICS_GET_TOTAL,
230 if (error != ERROR_NONE) {
231 GLOGD("Failed to invoke dbus method nonblock");
236 GLOGD("Succeeded to get total statistics");
241 error_e Statistics::GetAllStatistics(void)
244 GVariant *params = NULL;
245 error_e error = ERROR_NONE;
247 MakeRuleParams(¶ms, 3);
249 error = InvokeMethodNonblock(STC_MGR_SERVICE,
250 STC_MGR_STATISTICS_PATH,
251 STC_MGR_STATISTICS_INTERFACE,
252 STC_MGR_METHOD_STATISTICS_GET_ALL,
258 if (error != ERROR_NONE) {
259 GLOGD("Failed to invoke dbus method nonblock");
264 GLOGD("Succeeded to get all statistics");
269 error_e Statistics::ResetStatistics(void)
271 GVariant *message = NULL;
272 GVariant *params = NULL;
273 error_e error = ERROR_NONE;
276 MakeRuleParams(¶ms, 0);
278 message = InvokeMethod(STC_MGR_SERVICE,
279 STC_MGR_STATISTICS_PATH,
280 STC_MGR_STATISTICS_INTERFACE,
281 STC_MGR_METHOD_STATISTICS_RESET,
285 if (message == NULL) {
286 GLOGD("Failed to invoke dbus method");
290 g_variant_get(message, "(i)", &result);
291 GLOGD("Succeeded to reset statistics", result);
292 g_variant_unref(message);