2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
18 #include <json/json.h>
19 #include <ServerUtil.h>
20 #include "BatteryStatisticsTypes.h"
21 #include "HeartDbReader.h"
23 #define HEART_CPU_TABLE "cpu"
24 #define HEART_APP_ID "appid"
25 #define HEART_TIME "time"
26 #define HEART_DATA "data"
27 #define HEART_INDEX "idx"
29 #define HEART_DB ".resourced-heart-default.db"
33 HeartDbReader::HeartDbReader(uid_t uid) :
39 __initializeDBusConnection();
42 HeartDbReader::~HeartDbReader()
49 __releaseDBusConnection();
53 bool HeartDbReader::dbOpen()
62 ret = sqlite3_open_v2(util::get_user_path(__uid, TZ_USER_DB, HEART_DB).c_str(), &db, SQLITE_OPEN_READONLY, NULL);
63 IF_FAIL_RETURN_TAG(ret == SQLITE_OK, false, _E, "Failed to open HEART db: %s", sqlite3_errmsg(db));
71 void HeartDbReader::dbClose()
73 if (__dbRefCount > 1) {
78 sqlite3_close(__heartDb);
83 bool HeartDbReader::readCpuLog(int lastHeartAccessTime, std::vector<Json::Value>* cpuUsageLog)
85 IF_FAIL_RETURN(__heartDb, false);
87 char* sql = sqlite3_mprintf(
88 "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA ", " HEART_INDEX " FROM " HEART_CPU_TABLE \
89 " WHERE " HEART_TIME " > %d AND " HEART_TIME " <= %d", lastHeartAccessTime, CURRENT_TIME);
92 int ret = sqlite3_exec(__heartDb, sql, __cpuUsageLogCb, cpuUsageLog, &err);
94 if (ret != SQLITE_OK) {
95 _E("Failed to read heart DB: Error(%s)", err);
103 int HeartDbReader::__cpuUsageLogCb(void *userData, int dim, char **value, char **column)
105 IF_FAIL_RETURN(userData, 0);
107 std::vector<Json::Value> *records = static_cast<std::vector<Json::Value>*>(userData);
108 std::map<std::string, std::string> row;
110 bool columnNull = false;
112 for (int i = 0; i < dim; ++i) {
115 _W(RED("Null columns exist"));
119 row[column[i]] = value[i];
125 auto convToJson = [](std::map<std::string, std::string>& in)->Json::Value {
132 std::stringstream buf(in[HEART_DATA]);
133 buf >> utime >> stime >> pid >> type;
135 jval[BATTERY_APP_ID] = in[HEART_APP_ID];
136 jval[BATTERY_TIMESTAMP] = atoi(in[HEART_TIME].c_str());
137 jval[BATTERY_INDEX] = atoi(in[HEART_INDEX].c_str());
139 jval[BATTERY_UTIME] = utime;
140 jval[BATTERY_STIME] = stime;
141 jval[BATTERY_TYPE] = type;
142 jval[BATTERY_PID] = pid;
147 records->push_back(convToJson(row));
152 bool HeartDbReader::__initializeDBusConnection()
154 #if !GLIB_CHECK_VERSION(2, 36, 0)
157 GError *error = NULL;
159 __connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
161 _E("dbus connection get failed: %s", error->message);
169 void HeartDbReader::__releaseDBusConnection()
172 g_object_unref(__connection);
177 bool HeartDbReader::requestSync()
179 IF_FAIL_RETURN_TAG(__connection, false, _E, "Dbus connection is not initialized");
181 GError *error = NULL;
182 GVariant* ret = NULL;
183 ret = g_dbus_connection_call_sync(__connection,
184 "org.tizen.resourced", "/Org/Tizen/ResourceD/Logging", "org.tizen.resourced.logging",
185 "SyncCpuData", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &error);
188 _E("Failed to dbus call: %s", error->message);
189 g_clear_error(&error);
194 g_variant_get(ret, "(i)", &result);
195 g_variant_unref(ret);
196 IF_FAIL_RETURN_TAG(result != -1, false, _E, "Failed to sync heart cpu data");