From 98624afe7d6376db4bee80f26ea6fbc2c4909ea9 Mon Sep 17 00:00:00 2001 From: MyungJoo Ham Date: Tue, 20 Sep 2016 04:03:13 +0000 Subject: [PATCH 01/16] [4.0] Remove ExcludeArch and dead copmiler definitions If TV-profile should not include this package, it should be removed from the ks file (meta-tv.git). As we are trying to merge build projects, trying not to build for a specific profile will become meaningless soon. Thus, the ExcludeArch statement in the specfile is useless and confuses the system. So, it is removed. CMakeLists.txt states as if there are code with compiler definitions of "_MOBILE_" and "_WEARABLE". However, grep on the source code has revealed that there are no such code. Therefore, these two definitions are deadcode. We have not yet completely removed profile build dependencies. There will be another patch following this soon from me. Change-Id: I59025327e0ca933fd5599388b13939973a81081a Signed-off-by: MyungJoo Ham --- CMakeLists.txt | 9 --------- packaging/context-provider.spec | 4 ---- 2 files changed, 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 303cca7..2c21cb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,15 +15,6 @@ ADD_DEFINITIONS(-DLOG_TAG="CONTEXT") ADD_DEFINITIONS(-D_LIBDIR_="${LIBDIR}") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section") -# Profiles -IF("${PROFILE}" STREQUAL "mobile") - ADD_DEFINITIONS("-D_MOBILE_") -ENDIF("${PROFILE}" STREQUAL "mobile") - -IF("${PROFILE}" STREQUAL "wearable") - ADD_DEFINITIONS("-D_WEARABLE_") -ENDIF("${PROFILE}" STREQUAL "wearable") - INSTALL( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${target_dir}/internal diff --git a/packaging/context-provider.spec b/packaging/context-provider.spec index 2a2a1d6..765f485 100644 --- a/packaging/context-provider.spec +++ b/packaging/context-provider.spec @@ -8,10 +8,6 @@ Source0: %{name}-%{version}.tar.gz %define BUILD_PROFILE %{?profile}%{!?profile:%{?tizen_profile_name}} -%if "%{?BUILD_PROFILE}" == "tv" -ExcludeArch: %{arm} aarch64 %ix86 x86_64 -%endif - BuildRequires: cmake BuildRequires: pkgconfig(gmodule-2.0) -- 2.7.4 From 7e5c677c0b5f13f09fdc7cba00cb983ed6a0b7cf Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Wed, 2 Nov 2016 18:26:00 +0100 Subject: [PATCH 02/16] [my-place] Table creations change to synchronous. Change-Id: I672539d4e1c2eb5c936552c6842cdbdde148ca07 Signed-off-by: Marcin Masternak --- src/my-place/facade/UserPlaces.cpp | 2 +- src/my-place/place/PlacesDetector.cpp | 2 +- src/my-place/visit-detector/LocationLogger.cpp | 2 +- src/my-place/visit-detector/VisitDetector.cpp | 4 ++-- src/my-place/visit-detector/WifiLogger.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/my-place/facade/UserPlaces.cpp b/src/my-place/facade/UserPlaces.cpp index 01b0683..4fc6024 100755 --- a/src/my-place/facade/UserPlaces.cpp +++ b/src/my-place/facade/UserPlaces.cpp @@ -233,7 +233,7 @@ std::vector> ctx::UserPlaces::__placesFromJsons(std: void ctx::UserPlaces::__dbCreateMyPlaceSettingsTable() { DatabaseManager dbManager; - bool ret = dbManager.createTable(0, MYPLACE_SETTINGS_TABLE, __MYPLACE_SETTINGS_TABLE_COLUMNS); + bool ret = dbManager.createTableSync(MYPLACE_SETTINGS_TABLE, __MYPLACE_SETTINGS_TABLE_COLUMNS); _D("db: Myplace setting Table Creation Result: %s", ret ? "SUCCESS" : "FAIL"); } diff --git a/src/my-place/place/PlacesDetector.cpp b/src/my-place/place/PlacesDetector.cpp index 03fd1d5..31d0a3f 100644 --- a/src/my-place/place/PlacesDetector.cpp +++ b/src/my-place/place/PlacesDetector.cpp @@ -348,7 +348,7 @@ void ctx::PlacesDetector::__dbDeleteOlderWifiAPsThan(time_t threshold) void ctx::PlacesDetector::__dbCreateTable() { DatabaseManager dbManager; - bool ret = dbManager.createTable(0, PLACE_TABLE, __PLACE_TABLE_COLUMNS); + bool ret = dbManager.createTableSync(PLACE_TABLE, __PLACE_TABLE_COLUMNS); _D("db: place Table Creation Result: %s", ret ? "SUCCESS" : "FAIL"); } diff --git a/src/my-place/visit-detector/LocationLogger.cpp b/src/my-place/visit-detector/LocationLogger.cpp index 372ee74..6369694 100644 --- a/src/my-place/visit-detector/LocationLogger.cpp +++ b/src/my-place/visit-detector/LocationLogger.cpp @@ -176,7 +176,7 @@ void ctx::LocationLogger::__log(location_accessibility_state_e state) int ctx::LocationLogger::__dbCreateTable() { DatabaseManager dbManager; - bool ret = dbManager.createTable(0, LOCATION_TABLE, __LOCATION_CREATE_TABLE_COLUMNS, NULL, NULL); + bool ret = dbManager.createTableSync(LOCATION_TABLE, __LOCATION_CREATE_TABLE_COLUMNS); _D("%s -> Table Creation Request", ret ? "SUCCESS" : "FAIL"); return 0; } diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index 98460c2..ba431f6 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -387,10 +387,10 @@ std::shared_ptr ctx::VisitDetector::__getVisits() void ctx::VisitDetector::__dbCreateTables() { DatabaseManager dbManager; - bool ret = dbManager.createTable(0, VISIT_TABLE, __VISIT_TABLE_COLUMNS); + bool ret = dbManager.createTableSync(VISIT_TABLE, __VISIT_TABLE_COLUMNS); _D("db: Visit Table Creation Result: %s", ret ? "SUCCESS" : "FAIL"); - ret = dbManager.createTable(0, WIFI_APS_MAP_TABLE, __WIFI_APS_MAP_TABLE_COLUMNS); + ret = dbManager.createTableSync(WIFI_APS_MAP_TABLE, __WIFI_APS_MAP_TABLE_COLUMNS); _D("db: Wifi AP Map Table Creation Result: %s", ret ? "SUCCESS" : "FAIL"); } diff --git a/src/my-place/visit-detector/WifiLogger.cpp b/src/my-place/visit-detector/WifiLogger.cpp index 874f494..b46ecd9 100644 --- a/src/my-place/visit-detector/WifiLogger.cpp +++ b/src/my-place/visit-detector/WifiLogger.cpp @@ -37,7 +37,7 @@ int ctx::WifiLogger::__dbCreateTable() { DatabaseManager dbManager; - bool ret = dbManager.createTable(0, WIFI_TABLE, __WIFI_CREATE_TABLE_COLUMNS, NULL, NULL); + bool ret = dbManager.createTableSync(WIFI_TABLE, __WIFI_CREATE_TABLE_COLUMNS); _D("Table Creation Request: %s", ret ? "SUCCESS" : "FAIL"); return ret; } -- 2.7.4 From f0939ffa55dd1c3ecb9b267c8686fca4d02f9d4e Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Wed, 2 Nov 2016 18:27:45 +0100 Subject: [PATCH 03/16] [my-place] Place Recognition engine support on. Change-Id: Ibb2d8dc50bc3f258bcab8e2861e90062acda8b65 Signed-off-by: Marcin Masternak --- src/my-place/PlaceRecognitionProvider.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/my-place/PlaceRecognitionProvider.cpp b/src/my-place/PlaceRecognitionProvider.cpp index 1d17e84..fd30973 100644 --- a/src/my-place/PlaceRecognitionProvider.cpp +++ b/src/my-place/PlaceRecognitionProvider.cpp @@ -27,14 +27,14 @@ int ctx::PlaceRecognitionProvider::subscribe(ctx::Json option, ctx::Json* reques { /* NOTE: This function needs to return ERR_NONE. Otherwise, context-service will automatically delete this object. */ - return ERR_NOT_SUPPORTED; + return ERR_NONE; } int ctx::PlaceRecognitionProvider::unsubscribe(ctx::Json option) { /* NOTE: As the above subscribe() returns ERR_NONE, in parallel, this function also needs to return ERR_NONE. */ - return ERR_NOT_SUPPORTED; + return ERR_NONE; } int ctx::PlaceRecognitionProvider::read(ctx::Json option, ctx::Json* requestResult) -- 2.7.4 From cbe8fae7ec18fe11031bc7f4c89d3ed6af5fc3e1 Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Wed, 23 Nov 2016 18:43:04 +0100 Subject: [PATCH 04/16] [my-place] Change debug flag from TIZEN_ENGINEER_MODE to DEBUG_MODE. Change-Id: I9744d8a8698807e9e42ee9a3203a05bbbc586cff Signed-off-by: Marcin Masternak --- src/my-place/place/PlacesDetector.cpp | 20 +++++++++--------- src/my-place/utils/UserPlacesTypes.cpp | 6 +++--- src/my-place/utils/UserPlacesTypes.h | 18 ++++++++--------- src/my-place/visit-detector/LocationLogger.cpp | 28 +++++++++++++------------- src/my-place/visit-detector/VisitDetector.cpp | 10 ++++----- src/my-place/visit-detector/WifiLogger.cpp | 6 +++--- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/my-place/place/PlacesDetector.cpp b/src/my-place/place/PlacesDetector.cpp index 31d0a3f..7a226c7 100644 --- a/src/my-place/place/PlacesDetector.cpp +++ b/src/my-place/place/PlacesDetector.cpp @@ -21,9 +21,9 @@ #include "PlacesDetector.h" #include "PlaceCateger.h" #include -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #include -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #include #include #include @@ -33,9 +33,9 @@ #define __DELETE_PLACES_QUERY "DELETE FROM " PLACE_TABLE -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #define __USER_PLACES_FILE "/tmp/user_places.txt" // TODO: Only for debug purposes -> Remove in final solution -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #define __GET_VISITS_QUERY "SELECT "\ VISIT_COLUMN_END_TIME ", "\ @@ -165,9 +165,9 @@ void ctx::PlacesDetector::__reduceOutliers(ctx::Visits &visits) std::vector> ctx::PlacesDetector::__processVisits(ctx::Visits &visits, bool testMode) { -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE std::vector placesVisits; // TODO: remove from final solution. -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ std::vector> newDetectedPlaces; @@ -189,7 +189,7 @@ std::vector> ctx::PlacesDetector::__processVisits(ct if (!testMode) __dbInsertPlace(*place); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE { // TODO: Only for debug -> remove in final solution Visits placeVisits; for (graph::Node i : *component) { @@ -197,10 +197,10 @@ std::vector> ctx::PlacesDetector::__processVisits(ct } placesVisits.push_back(placeVisits); } -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ } -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE { // Print to file TODO: Only for debug -> remove in final solution std::ofstream out(__USER_PLACES_FILE); for (size_t i = 0; i < newDetectedPlaces.size(); i++) { @@ -213,7 +213,7 @@ std::vector> ctx::PlacesDetector::__processVisits(ct out.close(); Gmap::writeMap(newDetectedPlaces); } -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ return newDetectedPlaces; } diff --git a/src/my-place/utils/UserPlacesTypes.cpp b/src/my-place/utils/UserPlacesTypes.cpp index c5ed30c..06074c4 100644 --- a/src/my-place/utils/UserPlacesTypes.cpp +++ b/src/my-place/utils/UserPlacesTypes.cpp @@ -159,20 +159,20 @@ std::ostream& ctx::operator<<(std::ostream &output, const ctx::MacSet &macSet) void ctx::LocationEvent::log() { std::string time_str = DebugUtils::humanReadableDateTime(timestamp, "%T", 9); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s, method=%d", coordinates.latitude, coordinates.longitude, coordinates.accuracy, time_str.c_str(), method); -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s", coordinates.latitude, coordinates.longitude, coordinates.accuracy, time_str.c_str()); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ } void ctx::Visit::setLocation(Location location_) diff --git a/src/my-place/utils/UserPlacesTypes.h b/src/my-place/utils/UserPlacesTypes.h index 9365beb..cce02a1 100644 --- a/src/my-place/utils/UserPlacesTypes.h +++ b/src/my-place/utils/UserPlacesTypes.h @@ -32,10 +32,10 @@ #define VISIT_COLUMN_END_TIME "end_time" #define VISIT_COLUMN_WIFI_APS "wifi_aps" #define VISIT_COLUMN_CATEGORY "category" -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #define VISIT_COLUMN_START_TIME_HUMAN "start_time_human" // only for debug: human readable time data: #define VISIT_COLUMN_END_TIME_HUMAN "end_time_human" // only for debug: human readable time data: -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #define VISIT_COLUMN_LOCATION_VALID "location_valid" #define VISIT_COLUMN_LOCATION_LATITUDE "location_latitude" #define VISIT_COLUMN_LOCATION_LONGITUDE "location_longitude" @@ -70,10 +70,10 @@ #define LOCATION_COLUMN_LONGITUDE "longitude" #define LOCATION_COLUMN_ACCURACY "accuracy" #define LOCATION_COLUMN_TIMESTAMP "timestamp" -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #define LOCATION_COLUMN_TIMESTAMP_HUMAN "time_human" // only for debug: human readable time data: #define LOCATION_COLUMN_METHOD "method" -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #define MYPLACE_SETTINGS_TABLE "Myplace_Settings" #define MYPLACE_SETTINGS_COLUMN_KEY "key" @@ -195,13 +195,13 @@ namespace ctx { typedef std::map Categs; // scores of categories -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE enum LocationSource { LOCATION_METHOD_REQUEST = 0, LOCATION_METHOD_GET_LOCATION = 1, LOCATION_METHOD_GET_LAST_LOCATION = 2 }; -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ /* * location + timestamp + method @@ -210,17 +210,17 @@ namespace ctx { Location coordinates; time_t timestamp; -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE LocationSource method; LocationEvent(double latitude_, double longitude_, double accuracy_, time_t timestamp_, LocationSource method_) : coordinates(latitude_, longitude_, accuracy_), timestamp(timestamp_), method(method_) {} -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ LocationEvent(double latitude_, double longitude_, double accuracy_, time_t timestamp_) : coordinates(latitude_, longitude_, accuracy_), timestamp(timestamp_) {} -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ void log(); diff --git a/src/my-place/visit-detector/LocationLogger.cpp b/src/my-place/visit-detector/LocationLogger.cpp index 6369694..16bec65 100644 --- a/src/my-place/visit-detector/LocationLogger.cpp +++ b/src/my-place/visit-detector/LocationLogger.cpp @@ -23,7 +23,7 @@ #include #include "LocationLogger.h" -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #define __LOCATION_CREATE_TABLE_COLUMNS \ LOCATION_COLUMN_LATITUDE " REAL NOT NULL, "\ LOCATION_COLUMN_LONGITUDE " REAL NOT NULL, "\ @@ -31,13 +31,13 @@ LOCATION_COLUMN_TIMESTAMP " timestamp NOT NULL, "\ LOCATION_COLUMN_TIMESTAMP_HUMAN " TEXT, "\ LOCATION_COLUMN_METHOD " INTEGER " -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ #define __LOCATION_CREATE_TABLE_COLUMNS \ LOCATION_COLUMN_LATITUDE " REAL NOT NULL, "\ LOCATION_COLUMN_LONGITUDE " REAL NOT NULL, "\ LOCATION_COLUMN_ACCURACY " REAL, "\ LOCATION_COLUMN_TIMESTAMP " timestamp NOT NULL " -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #define __LOCATION_ERROR_LOG(error) { \ if (error != LOCATIONS_ERROR_NONE) { \ @@ -111,11 +111,11 @@ void ctx::LocationLogger::__positionUpdatedCb(double latitude, double longitude, _D(""); ctx::LocationLogger* locationLogger = (ctx::LocationLogger *)userData; double horizontal = locationLogger->__locationManagerGetHorizontalAccuracy(); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_REQUEST); -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ locationLogger->__broadcast(location); locationLogger->__onActiveRequestSucceeded(); } @@ -188,11 +188,11 @@ int ctx::LocationLogger::__dbInsertLog(LocationEvent locationEvent) data.set(NULL, LOCATION_COLUMN_LONGITUDE, locationEvent.coordinates.longitude); data.set(NULL, LOCATION_COLUMN_ACCURACY, locationEvent.coordinates.accuracy); data.set(NULL, LOCATION_COLUMN_TIMESTAMP, static_cast(locationEvent.timestamp)); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE std::string timeHuman = DebugUtils::humanReadableDateTime(locationEvent.timestamp, "%F %T", 80); data.set(NULL, LOCATION_COLUMN_TIMESTAMP_HUMAN, timeHuman); data.set(NULL, LOCATION_COLUMN_METHOD, static_cast(locationEvent.method)); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ DatabaseManager dbManager; int64_t rowId; @@ -351,11 +351,11 @@ bool ctx::LocationLogger::__locationManagerGetLocation() __activeAttempts++; __allAttempts++; if (ret == LOCATIONS_ERROR_NONE) { -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_GET_LOCATION); -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ __broadcast(location); __onActiveLocationSucceeded(); return true; @@ -384,11 +384,11 @@ void ctx::LocationLogger::__locationManagerGetLastLocation() __LOCATION_ERROR_LOG(ret); __allAttempts++; if (ret == LOCATIONS_ERROR_NONE) { -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_GET_LAST_LOCATION); -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ __broadcast(location); } } diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index ba431f6..4f81baf 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -33,7 +33,7 @@ typedef void (*visit_categer_t)(ctx::Visit &visit); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE #define __VISIT_TABLE_COLUMNS \ VISIT_COLUMN_WIFI_APS " TEXT, "\ VISIT_COLUMN_START_TIME " timestamp, "\ @@ -47,7 +47,7 @@ typedef void (*visit_categer_t)(ctx::Visit &visit); VISIT_COLUMN_CATEG_HOME " REAL, "\ VISIT_COLUMN_CATEG_WORK " REAL, "\ VISIT_COLUMN_CATEG_OTHER " REAL" -#else /* TIZEN_ENGINEER_MODE */ +#else /* DEBUG_MODE */ #define __VISIT_TABLE_COLUMNS \ VISIT_COLUMN_WIFI_APS " TEXT, "\ VISIT_COLUMN_START_TIME " timestamp, "\ @@ -59,7 +59,7 @@ typedef void (*visit_categer_t)(ctx::Visit &visit); VISIT_COLUMN_CATEG_HOME " REAL, "\ VISIT_COLUMN_CATEG_WORK " REAL, "\ VISIT_COLUMN_CATEG_OTHER " REAL" -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ #define __WIFI_APS_MAP_TABLE_COLUMNS \ WIFI_APS_MAP_COLUMN_MAC " TEXT NOT NULL UNIQUE, "\ @@ -427,7 +427,7 @@ int ctx::VisitDetector::__dbInsertVisit(Visit visit) data.set(NULL, VISIT_COLUMN_START_TIME, static_cast(visit.interval.start)); data.set(NULL, VISIT_COLUMN_END_TIME, static_cast(visit.interval.end)); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE std::string startTimeHuman = DebugUtils::humanReadableDateTime(visit.interval.start, "%F %T", 80); std::string endTimeHuman = DebugUtils::humanReadableDateTime(visit.interval.end, "%F %T", 80); data.set(NULL, VISIT_COLUMN_START_TIME_HUMAN, startTimeHuman.c_str()); @@ -436,7 +436,7 @@ int ctx::VisitDetector::__dbInsertVisit(Visit visit) visit.interval.start, visit.interval.end, startTimeHuman.c_str(), endTimeHuman.c_str()); #else _D("db: visit table insert interval: (%d, %d)", visit.interval.start, visit.interval.end); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ __putVisitCategsToJson(visit.categs, data); diff --git a/src/my-place/visit-detector/WifiLogger.cpp b/src/my-place/visit-detector/WifiLogger.cpp index b46ecd9..df4dfa8 100644 --- a/src/my-place/visit-detector/WifiLogger.cpp +++ b/src/my-place/visit-detector/WifiLogger.cpp @@ -67,7 +67,7 @@ int ctx::WifiLogger::__dbInsertLogs() return 0; } -ctx::WifiLogger::WifiLogger(IWifiListener * listener, PlaceRecogMode energyMode) : +ctx::WifiLogger::WifiLogger(IWifiListener *listener, PlaceRecogMode energyMode) : __timerOn(false), __timerId(-1), __intervalMinutes(WIFI_LOGGER_INTERVAL_MINUTES_HIGH_ACCURACY), @@ -220,7 +220,7 @@ void ctx::WifiLogger::__wifiScanFinishedCb(wifi_error_e errorCode, void *userDat ctx::WifiLogger* wifiLogger = (ctx::WifiLogger *)userData; time_t now = time(nullptr); -#ifdef TIZEN_ENGINEER_MODE +#ifdef DEBUG_MODE double seconds = 0; if (wifiLogger->__lastScanTime > 0) { seconds = difftime(now, wifiLogger->__lastScanTime); @@ -232,7 +232,7 @@ void ctx::WifiLogger::__wifiScanFinishedCb(wifi_error_e errorCode, void *userDat wifiLogger->__lastScansPool.size(), timeStr.c_str(), seconds); -#endif /* TIZEN_ENGINEER_MODE */ +#endif /* DEBUG_MODE */ wifiLogger->__lastScanTime = now; int ret = wifiLogger->__wifiForeachFoundApsRequest(userData); -- 2.7.4 From 711cb287e2684e0c10b428ce2f09a0882ae25d97 Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Wed, 23 Nov 2016 18:40:10 +0100 Subject: [PATCH 05/16] [my-place] Change db queries async -> sync. Change-Id: Id7d450f23b050121529da41fef89574359c19ee9 Signed-off-by: Marcin Masternak --- src/my-place/visit-detector/VisitDetector.cpp | 3 ++- src/my-place/visit-detector/WifiLogger.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index 4f81baf..7a6786d 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -464,8 +464,9 @@ int ctx::VisitDetector::__dbInsertWifiAPsMap(Visit visit) __wifiAPsMap.clear(); query << "; \ END TRANSACTION;"; + std::vector records; DatabaseManager dbManager; - bool ret = dbManager.execute(0, query.str().c_str(), NULL); + bool ret = dbManager.executeSync(query.str().c_str(), &records); _D("DB Wifi APs map insert request: %s", ret ? "SUCCESS" : "FAIL"); return ret; } diff --git a/src/my-place/visit-detector/WifiLogger.cpp b/src/my-place/visit-detector/WifiLogger.cpp index df4dfa8..4a41c19 100644 --- a/src/my-place/visit-detector/WifiLogger.cpp +++ b/src/my-place/visit-detector/WifiLogger.cpp @@ -58,8 +58,9 @@ int ctx::WifiLogger::__dbInsertLogs() __logs.clear(); query << "; \ END TRANSACTION;"; + std::vector records; DatabaseManager dbManager; - bool ret = dbManager.execute(0, query.str().c_str(), NULL); + bool ret = dbManager.executeSync(query.str().c_str(), &records); _D("DB insert request: %s", ret ? "SUCCESS" : "FAIL"); return ret; } -- 2.7.4 From acfe9155f5767a28353e8c79045810dd7fbccffd Mon Sep 17 00:00:00 2001 From: Marcin Masternak Date: Tue, 29 Nov 2016 16:27:34 +0100 Subject: [PATCH 06/16] [my-place] DEBUG_MODE simplification. Change-Id: If9c1be9ff546b6fc15a5390dee704ff29d9eebd0 Signed-off-by: Marcin Masternak --- src/my-place/facade/UserPlaces.cpp | 1 - src/my-place/place/PlacesDetector.cpp | 9 ++++----- src/my-place/utils/Gmap.h | 2 +- src/my-place/utils/UserPlacesTypes.cpp | 9 --------- src/my-place/utils/UserPlacesTypes.h | 24 ----------------------- src/my-place/visit-detector/LocationLogger.cpp | 27 -------------------------- src/my-place/visit-detector/LocationLogger.h | 2 +- src/my-place/visit-detector/VisitDetector.cpp | 18 ----------------- 8 files changed, 6 insertions(+), 86 deletions(-) diff --git a/src/my-place/facade/UserPlaces.cpp b/src/my-place/facade/UserPlaces.cpp index 4fc6024..067d595 100755 --- a/src/my-place/facade/UserPlaces.cpp +++ b/src/my-place/facade/UserPlaces.cpp @@ -154,7 +154,6 @@ std::map ctx::UserPlaces::__dbGetWifiAPsMap() std::map wifiAPsMap; DatabaseManager dbManager; bool ret = dbManager.executeSync(__GET_WIFI_APS_MAP_QUERY, &records); - // TODO: add return statements if db fail _D("load Wifi APs map (size = %d), result: %s", records.size(), ret ? "SUCCESS" : "FAIL"); std::string mac = ""; std::string networkName = ""; diff --git a/src/my-place/place/PlacesDetector.cpp b/src/my-place/place/PlacesDetector.cpp index 7a226c7..96e9a41 100644 --- a/src/my-place/place/PlacesDetector.cpp +++ b/src/my-place/place/PlacesDetector.cpp @@ -34,7 +34,7 @@ #define __DELETE_PLACES_QUERY "DELETE FROM " PLACE_TABLE #ifdef DEBUG_MODE -#define __USER_PLACES_FILE "/tmp/user_places.txt" // TODO: Only for debug purposes -> Remove in final solution +#define __USER_PLACES_FILE "/tmp/user_places.txt" #endif /* DEBUG_MODE */ #define __GET_VISITS_QUERY "SELECT "\ @@ -166,7 +166,7 @@ void ctx::PlacesDetector::__reduceOutliers(ctx::Visits &visits) std::vector> ctx::PlacesDetector::__processVisits(ctx::Visits &visits, bool testMode) { #ifdef DEBUG_MODE - std::vector placesVisits; // TODO: remove from final solution. + std::vector placesVisits; #endif /* DEBUG_MODE */ std::vector> newDetectedPlaces; @@ -190,7 +190,7 @@ std::vector> ctx::PlacesDetector::__processVisits(ct __dbInsertPlace(*place); #ifdef DEBUG_MODE - { // TODO: Only for debug -> remove in final solution + { Visits placeVisits; for (graph::Node i : *component) { placeVisits.push_back(visits[i]); @@ -201,7 +201,7 @@ std::vector> ctx::PlacesDetector::__processVisits(ct } #ifdef DEBUG_MODE - { // Print to file TODO: Only for debug -> remove in final solution + { std::ofstream out(__USER_PLACES_FILE); for (size_t i = 0; i < newDetectedPlaces.size(); i++) { DebugUtils::printPlace2Stream(*newDetectedPlaces[i], out); @@ -327,7 +327,6 @@ void ctx::PlacesDetector::__dbDeleteOlderVisitsThan(time_t threshold) std::stringstream query; query << "DELETE FROM " << VISIT_TABLE; query << " WHERE " << VISIT_COLUMN_END_TIME << " < " << threshold; - // query << " AND 0"; // XXX: Always false condition. Uncomment it for not deleting any visit during development. std::vector records; DatabaseManager dbManager; bool ret = dbManager.executeSync(query.str().c_str(), &records); diff --git a/src/my-place/utils/Gmap.h b/src/my-place/utils/Gmap.h index c6a5482..9e5c930 100644 --- a/src/my-place/utils/Gmap.h +++ b/src/my-place/utils/Gmap.h @@ -25,7 +25,7 @@ namespace ctx { /* * Class for generating a HTML page with GoogleMaps with all user places. - * This class is for test/demo purposes only. TODO: Remove this class from final solution. + * This class is for test/demo purposes only. */ class Gmap { diff --git a/src/my-place/utils/UserPlacesTypes.cpp b/src/my-place/utils/UserPlacesTypes.cpp index 06074c4..1b8857f 100644 --- a/src/my-place/utils/UserPlacesTypes.cpp +++ b/src/my-place/utils/UserPlacesTypes.cpp @@ -159,20 +159,11 @@ std::ostream& ctx::operator<<(std::ostream &output, const ctx::MacSet &macSet) void ctx::LocationEvent::log() { std::string time_str = DebugUtils::humanReadableDateTime(timestamp, "%T", 9); -#ifdef DEBUG_MODE - _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s, method=%d", - coordinates.latitude, - coordinates.longitude, - coordinates.accuracy, - time_str.c_str(), - method); -#else /* DEBUG_MODE */ _D("location lat=%.8f, lon=%.8f, acc=%.2f[m], time=%s", coordinates.latitude, coordinates.longitude, coordinates.accuracy, time_str.c_str()); -#endif /* DEBUG_MODE */ } void ctx::Visit::setLocation(Location location_) diff --git a/src/my-place/utils/UserPlacesTypes.h b/src/my-place/utils/UserPlacesTypes.h index cce02a1..7955080 100644 --- a/src/my-place/utils/UserPlacesTypes.h +++ b/src/my-place/utils/UserPlacesTypes.h @@ -32,10 +32,6 @@ #define VISIT_COLUMN_END_TIME "end_time" #define VISIT_COLUMN_WIFI_APS "wifi_aps" #define VISIT_COLUMN_CATEGORY "category" -#ifdef DEBUG_MODE -#define VISIT_COLUMN_START_TIME_HUMAN "start_time_human" // only for debug: human readable time data: -#define VISIT_COLUMN_END_TIME_HUMAN "end_time_human" // only for debug: human readable time data: -#endif /* DEBUG_MODE */ #define VISIT_COLUMN_LOCATION_VALID "location_valid" #define VISIT_COLUMN_LOCATION_LATITUDE "location_latitude" #define VISIT_COLUMN_LOCATION_LONGITUDE "location_longitude" @@ -70,10 +66,6 @@ #define LOCATION_COLUMN_LONGITUDE "longitude" #define LOCATION_COLUMN_ACCURACY "accuracy" #define LOCATION_COLUMN_TIMESTAMP "timestamp" -#ifdef DEBUG_MODE -#define LOCATION_COLUMN_TIMESTAMP_HUMAN "time_human" // only for debug: human readable time data: -#define LOCATION_COLUMN_METHOD "method" -#endif /* DEBUG_MODE */ #define MYPLACE_SETTINGS_TABLE "Myplace_Settings" #define MYPLACE_SETTINGS_COLUMN_KEY "key" @@ -195,14 +187,6 @@ namespace ctx { typedef std::map Categs; // scores of categories -#ifdef DEBUG_MODE - enum LocationSource { - LOCATION_METHOD_REQUEST = 0, - LOCATION_METHOD_GET_LOCATION = 1, - LOCATION_METHOD_GET_LAST_LOCATION = 2 - }; -#endif /* DEBUG_MODE */ - /* * location + timestamp + method */ @@ -210,17 +194,9 @@ namespace ctx { Location coordinates; time_t timestamp; -#ifdef DEBUG_MODE - LocationSource method; - - LocationEvent(double latitude_, double longitude_, double accuracy_, time_t timestamp_, LocationSource method_) : - coordinates(latitude_, longitude_, accuracy_), - timestamp(timestamp_), method(method_) {} -#else /* DEBUG_MODE */ LocationEvent(double latitude_, double longitude_, double accuracy_, time_t timestamp_) : coordinates(latitude_, longitude_, accuracy_), timestamp(timestamp_) {} -#endif /* DEBUG_MODE */ void log(); diff --git a/src/my-place/visit-detector/LocationLogger.cpp b/src/my-place/visit-detector/LocationLogger.cpp index 16bec65..cf4ce95 100644 --- a/src/my-place/visit-detector/LocationLogger.cpp +++ b/src/my-place/visit-detector/LocationLogger.cpp @@ -23,21 +23,11 @@ #include #include "LocationLogger.h" -#ifdef DEBUG_MODE -#define __LOCATION_CREATE_TABLE_COLUMNS \ - LOCATION_COLUMN_LATITUDE " REAL NOT NULL, "\ - LOCATION_COLUMN_LONGITUDE " REAL NOT NULL, "\ - LOCATION_COLUMN_ACCURACY " REAL, "\ - LOCATION_COLUMN_TIMESTAMP " timestamp NOT NULL, "\ - LOCATION_COLUMN_TIMESTAMP_HUMAN " TEXT, "\ - LOCATION_COLUMN_METHOD " INTEGER " -#else /* DEBUG_MODE */ #define __LOCATION_CREATE_TABLE_COLUMNS \ LOCATION_COLUMN_LATITUDE " REAL NOT NULL, "\ LOCATION_COLUMN_LONGITUDE " REAL NOT NULL, "\ LOCATION_COLUMN_ACCURACY " REAL, "\ LOCATION_COLUMN_TIMESTAMP " timestamp NOT NULL " -#endif /* DEBUG_MODE */ #define __LOCATION_ERROR_LOG(error) { \ if (error != LOCATIONS_ERROR_NONE) { \ @@ -111,11 +101,7 @@ void ctx::LocationLogger::__positionUpdatedCb(double latitude, double longitude, _D(""); ctx::LocationLogger* locationLogger = (ctx::LocationLogger *)userData; double horizontal = locationLogger->__locationManagerGetHorizontalAccuracy(); -#ifdef DEBUG_MODE - ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_REQUEST); -#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* DEBUG_MODE */ locationLogger->__broadcast(location); locationLogger->__onActiveRequestSucceeded(); } @@ -188,11 +174,6 @@ int ctx::LocationLogger::__dbInsertLog(LocationEvent locationEvent) data.set(NULL, LOCATION_COLUMN_LONGITUDE, locationEvent.coordinates.longitude); data.set(NULL, LOCATION_COLUMN_ACCURACY, locationEvent.coordinates.accuracy); data.set(NULL, LOCATION_COLUMN_TIMESTAMP, static_cast(locationEvent.timestamp)); -#ifdef DEBUG_MODE - std::string timeHuman = DebugUtils::humanReadableDateTime(locationEvent.timestamp, "%F %T", 80); - data.set(NULL, LOCATION_COLUMN_TIMESTAMP_HUMAN, timeHuman); - data.set(NULL, LOCATION_COLUMN_METHOD, static_cast(locationEvent.method)); -#endif /* DEBUG_MODE */ DatabaseManager dbManager; int64_t rowId; @@ -351,11 +332,7 @@ bool ctx::LocationLogger::__locationManagerGetLocation() __activeAttempts++; __allAttempts++; if (ret == LOCATIONS_ERROR_NONE) { -#ifdef DEBUG_MODE - ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_GET_LOCATION); -#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* DEBUG_MODE */ __broadcast(location); __onActiveLocationSucceeded(); return true; @@ -384,11 +361,7 @@ void ctx::LocationLogger::__locationManagerGetLastLocation() __LOCATION_ERROR_LOG(ret); __allAttempts++; if (ret == LOCATIONS_ERROR_NONE) { -#ifdef DEBUG_MODE - ctx::LocationEvent location(latitude, longitude, horizontal, timestamp, LOCATION_METHOD_GET_LAST_LOCATION); -#else /* DEBUG_MODE */ ctx::LocationEvent location(latitude, longitude, horizontal, timestamp); -#endif /* DEBUG_MODE */ __broadcast(location); } } diff --git a/src/my-place/visit-detector/LocationLogger.h b/src/my-place/visit-detector/LocationLogger.h index b901dee..c0c3dc8 100644 --- a/src/my-place/visit-detector/LocationLogger.h +++ b/src/my-place/visit-detector/LocationLogger.h @@ -23,7 +23,7 @@ #include "LocationListenerIface.h" /* Database usage flag */ -#define LOCATION_LOGGER_DATABASE false // TODO: false in final solution +#define LOCATION_LOGGER_DATABASE false /* Locations measure method */ #define LOCATION_LOGGER_METHOD LOCATIONS_METHOD_HYBRID diff --git a/src/my-place/visit-detector/VisitDetector.cpp b/src/my-place/visit-detector/VisitDetector.cpp index 7a6786d..5fbc5cb 100644 --- a/src/my-place/visit-detector/VisitDetector.cpp +++ b/src/my-place/visit-detector/VisitDetector.cpp @@ -33,13 +33,10 @@ typedef void (*visit_categer_t)(ctx::Visit &visit); -#ifdef DEBUG_MODE #define __VISIT_TABLE_COLUMNS \ VISIT_COLUMN_WIFI_APS " TEXT, "\ VISIT_COLUMN_START_TIME " timestamp, "\ VISIT_COLUMN_END_TIME " timestamp, "\ - VISIT_COLUMN_START_TIME_HUMAN " TEXT, "\ - VISIT_COLUMN_END_TIME_HUMAN " TEXT, "\ VISIT_COLUMN_LOCATION_VALID " INTEGER, "\ VISIT_COLUMN_LOCATION_LATITUDE " REAL, "\ VISIT_COLUMN_LOCATION_LONGITUDE " REAL, "\ @@ -47,19 +44,6 @@ typedef void (*visit_categer_t)(ctx::Visit &visit); VISIT_COLUMN_CATEG_HOME " REAL, "\ VISIT_COLUMN_CATEG_WORK " REAL, "\ VISIT_COLUMN_CATEG_OTHER " REAL" -#else /* DEBUG_MODE */ -#define __VISIT_TABLE_COLUMNS \ - VISIT_COLUMN_WIFI_APS " TEXT, "\ - VISIT_COLUMN_START_TIME " timestamp, "\ - VISIT_COLUMN_END_TIME " timestamp, "\ - VISIT_COLUMN_LOCATION_VALID " INTEGER, "\ - VISIT_COLUMN_LOCATION_LATITUDE " REAL, "\ - VISIT_COLUMN_LOCATION_LONGITUDE " REAL, "\ - VISIT_COLUMN_LOCATION_ACCURACY " REAL, "\ - VISIT_COLUMN_CATEG_HOME " REAL, "\ - VISIT_COLUMN_CATEG_WORK " REAL, "\ - VISIT_COLUMN_CATEG_OTHER " REAL" -#endif /* DEBUG_MODE */ #define __WIFI_APS_MAP_TABLE_COLUMNS \ WIFI_APS_MAP_COLUMN_MAC " TEXT NOT NULL UNIQUE, "\ @@ -430,8 +414,6 @@ int ctx::VisitDetector::__dbInsertVisit(Visit visit) #ifdef DEBUG_MODE std::string startTimeHuman = DebugUtils::humanReadableDateTime(visit.interval.start, "%F %T", 80); std::string endTimeHuman = DebugUtils::humanReadableDateTime(visit.interval.end, "%F %T", 80); - data.set(NULL, VISIT_COLUMN_START_TIME_HUMAN, startTimeHuman.c_str()); - data.set(NULL, VISIT_COLUMN_END_TIME_HUMAN, endTimeHuman.c_str()); _D("db: visit table insert interval: (%d, %d): (%s, %s)", visit.interval.start, visit.interval.end, startTimeHuman.c_str(), endTimeHuman.c_str()); #else -- 2.7.4 From d870eba93204766d38d90d1661ddda62ce388a0c Mon Sep 17 00:00:00 2001 From: Kunal Date: Wed, 30 Nov 2016 17:51:38 +0900 Subject: [PATCH 07/16] Resolved a bug which gives negative battery values on process being killed and re-launched When an app is killed and re-launched,the CPU times are reset, the difference is negative, so checked for non-negative differences Change-Id: Ib1611fcb5ea05295990bd3f127a24abf7a2df5c4 Signed-off-by: Kunal --- src/battery-stats/BatteryUsageAnalyzer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/battery-stats/BatteryUsageAnalyzer.cpp b/src/battery-stats/BatteryUsageAnalyzer.cpp index 73f1b2c..66dd430 100644 --- a/src/battery-stats/BatteryUsageAnalyzer.cpp +++ b/src/battery-stats/BatteryUsageAnalyzer.cpp @@ -81,6 +81,13 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std prevUtime = (lastAppCpuUsage->second).utime; prevStime = (lastAppCpuUsage->second).stime; + // If the app is killed and launched again + if (prevStime > stime || prevUtime > utime) { + prevUtime = 0; + prevStime = 0; + prevTimestamp = lastTime; + } + Json row; row.set(NULL, BATTERY_APP_ID, appId); row.set(NULL, BATTERY_START_TIME, prevTimestamp); -- 2.7.4 From d504138ba887844970a6c088f861b7062582f511 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Wed, 30 Nov 2016 15:37:54 +0900 Subject: [PATCH 08/16] Modify recnet battery usage to return data after booting, if device never fully charged. Change-Id: I01f9e9f62271094a6e10f4fec00eeba71fda1fb4 Signed-off-by: Somin Kim --- src/battery-stats/BatteryMonitor.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 4352019..9a11562 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -27,6 +27,7 @@ using namespace ctx; +static int __bootingTime = 0; static int __lastResetTime = 0; static int __lastPercent = -1; static int __lastPercentTime = 0; @@ -64,6 +65,8 @@ void BatteryMonitor::__initialize() bool BatteryMonitor::__loadLastInfo() { + __bootingTime = CURRENT_TIME; + device_battery_is_charging(&__isCharging); std::vector records; @@ -355,8 +358,13 @@ bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage) return __dbMgr.executeSync(query.c_str(), NULL); } +// Used for Recent Battery Usage int BatteryMonitor::getLastResetTime() { - IF_FAIL_RETURN_TAG(__lastResetTime != 0, CURRENT_TIME, _D, "Last reset time doesn't exist"); + if (__lastResetTime == 0) { + _I("Device has not fully charged until now. Start time of recent battery usage will be booting time"); + return __bootingTime; + } + return __lastResetTime; } -- 2.7.4 From 9fd8b2dae584ec8811a871e982b7371d25353c6c Mon Sep 17 00:00:00 2001 From: Kunal Date: Wed, 30 Nov 2016 17:49:13 +0900 Subject: [PATCH 09/16] Added code to query HeartDB every 10 minutes Change-Id: I62086cb8a5b1f92d5434956e9e5213477cb2910d Signed-off-by: Kunal --- src/battery-stats/BatteryMonitor.cpp | 38 +++++++++++++++++++++++++++++++----- src/battery-stats/BatteryMonitor.h | 1 + 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 9a11562..7098c9f 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -34,7 +34,9 @@ static int __lastPercentTime = 0; static int __lastHeartTimestamp = 0; static bool __isCharging = 0; static std::vector __batteryTimeInfoVec; +static bool __timerRunning = false; +#define DB_QUERY_DELAY 600000 BatteryMonitor::BatteryMonitor() { @@ -133,7 +135,12 @@ void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void battTimeInfo.set(NULL, BATTERY_END_TIME, currentTime); __batteryTimeInfoVec.push_back(battTimeInfo); - instance->processBatteryUsage(); + bool dataRemaining = instance->processBatteryUsage(); + if (dataRemaining && !__timerRunning) { + __timerRunning = true; + _D("Start timer to request HEART data"); + g_timeout_add(DB_QUERY_DELAY, __timeoutCb, instance); + } } __lastPercentTime = currentTime; @@ -163,6 +170,18 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void } } +gboolean BatteryMonitor::__timeoutCb(gpointer data) { + BatteryMonitor* instance = static_cast(data); + + bool dataRemaining = instance->processBatteryUsage(); + if (!dataRemaining) { + _D("Stop timer, no more data to process"); + __timerRunning = false; + } + + return dataRemaining ? TRUE : FALSE; +} + int BatteryMonitor::start() { __loadLastInfo(); @@ -173,6 +192,9 @@ int BatteryMonitor::start() error = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, __chargerChangeCb, NULL); IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to set battery charging change cb"); + __timerRunning = true; + _D("Start timer to request HEAERT data"); + g_timeout_add(DB_QUERY_DELAY, __timeoutCb, this); return ERR_NONE; } @@ -219,9 +241,10 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage) return true; } +//Return false if all is processed, true otherwise bool BatteryMonitor::processBatteryUsage() { - IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, true, _D, "All per-app battery usages are already calculated"); + IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, false, _D, "All per-app battery usages are already calculated"); int totalStartTime; int totalEndTime; @@ -231,12 +254,12 @@ bool BatteryMonitor::processBatteryUsage() // Read cpu table from heart db for time span of stacked in __batteryTimeInfoVec HeartDbReader heartReader; bool ret = heartReader.open(); - IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to open heart db"); + IF_FAIL_RETURN_TAG(ret, true, _E, "Failed to open heart db"); std::vector cpuUsageLog; ret = heartReader.readCpuUsageLog(totalStartTime, totalEndTime, &cpuUsageLog); heartReader.close(); - IF_FAIL_RETURN_TAG(ret, false, _E, "Cannot read from heart cpu table"); + IF_FAIL_RETURN_TAG(ret, true, _E, "Cannot read from heart cpu table"); IF_FAIL_RETURN_TAG(cpuUsageLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime); _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime); @@ -287,7 +310,12 @@ bool BatteryMonitor::processBatteryUsage() _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i); __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i); - return true; + if (__batteryTimeInfoVec.size() == 0) { + return false; + } + else { + return true; + } } bool BatteryMonitor::__insertBatteryUsageLog(std::vector& usage) diff --git a/src/battery-stats/BatteryMonitor.h b/src/battery-stats/BatteryMonitor.h index 7ec34f8..2ac3db8 100644 --- a/src/battery-stats/BatteryMonitor.h +++ b/src/battery-stats/BatteryMonitor.h @@ -40,6 +40,7 @@ namespace ctx { bool __loadLastInfo(); bool __updateLastInfo(); + static int __timeoutCb(void* data); static void __batteryChangeCb(device_callback_e type, void* value, void* userData); static void __chargerChangeCb(device_callback_e type, void* value, void* userData); -- 2.7.4 From d78cdf243e637382ab63cccd88a30328b0c21647 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Wed, 30 Nov 2016 18:57:24 +0900 Subject: [PATCH 10/16] Version 0.9.8 Change-Id: If35b39342b368903970ccb37b262035093d48188 Signed-off-by: Somin Kim --- packaging/context-provider.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/context-provider.spec b/packaging/context-provider.spec index 765f485..6443513 100644 --- a/packaging/context-provider.spec +++ b/packaging/context-provider.spec @@ -1,6 +1,6 @@ Name: context-provider Summary: Context Provider -Version: 0.9.7 +Version: 0.9.8 Release: 1 Group: Service/Context License: Apache-2.0 -- 2.7.4 From 88fc42229b16afd32a122393bba81dc2e54823ec Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 2 Dec 2016 19:07:28 +0900 Subject: [PATCH 11/16] Fix svace issues (possible memory leaks) Change-Id: Ic1fc5047424c0fb97f60c35bd262795d51c9315e Signed-off-by: Mu-Woong Lee --- src/my-place/visit-detector/WifiLogger.cpp | 32 ++++++++++++++++++++---------- src/my-place/visit-detector/WifiLogger.h | 4 ++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/my-place/visit-detector/WifiLogger.cpp b/src/my-place/visit-detector/WifiLogger.cpp index 4a41c19..785aecc 100644 --- a/src/my-place/visit-detector/WifiLogger.cpp +++ b/src/my-place/visit-detector/WifiLogger.cpp @@ -147,13 +147,13 @@ bool ctx::WifiLogger::__wifiFoundApCb(wifi_ap_h ap, void *userData) { ctx::WifiLogger* wifiLogger = (ctx::WifiLogger *)userData; - char *bssid = NULL; - int ret = wifiLogger->__wifiApGetBssidRequest(ap, &bssid); + std::string bssid; + int ret = wifiLogger->__wifiApGetBssidRequest(ap, bssid); if (ret != WIFI_ERROR_NONE) return false; - char *essid = NULL; - ret = wifiLogger->__wifiApGetEssidRequest(ap, &essid); + std::string essid; + ret = wifiLogger->__wifiApGetEssidRequest(ap, essid); if (ret != WIFI_ERROR_NONE) return false; @@ -165,13 +165,13 @@ bool ctx::WifiLogger::__wifiFoundApCb(wifi_ap_h ap, void *userData) return false; } - MacEvent log(wifiLogger->__lastScanTime, mac, std::string(essid)); + MacEvent log(wifiLogger->__lastScanTime, mac, essid); if (wifiLogger->__listener) { wifiLogger->__listener->onWifiScan(log); if (WIFI_LOGGER_LOW_POWER_MODE && (wifiLogger->__connectedToWifiAp || wifiLogger->__duringVisit) ) { // Add to last scans AP's set - wifiLogger->__lastScansPool.insert(std::pair(std::string(bssid), std::string(essid))); + wifiLogger->__lastScansPool.insert(std::pair(bssid, essid)); } } if (WIFI_LOGGER_DATABASE) @@ -291,17 +291,29 @@ void ctx::WifiLogger::__wifiSetConnectionStateChangedCbRequest() __WIFI_ERROR_LOG(ret); } -int ctx::WifiLogger::__wifiApGetEssidRequest(wifi_ap_h ap, char **essid) +int ctx::WifiLogger::__wifiApGetEssidRequest(wifi_ap_h ap, std::string &essid) { - int ret = __wifiWrapper.getEssidFromAP(ap, essid); + char *_essid = NULL; + int ret = __wifiWrapper.getEssidFromAP(ap, &_essid); __WIFI_ERROR_LOG(ret); + + if (ret == WIFI_ERROR_NONE && _essid) + essid = _essid; + + free(_essid); return ret; } -int ctx::WifiLogger::__wifiApGetBssidRequest(wifi_ap_h ap, char **bssid) +int ctx::WifiLogger::__wifiApGetBssidRequest(wifi_ap_h ap, std::string &bssid) { - int ret = __wifiWrapper.getBssidFromAP(ap, bssid); + char *_bssid = NULL; + int ret = __wifiWrapper.getBssidFromAP(ap, &_bssid); __WIFI_ERROR_LOG(ret); + + if (ret == WIFI_ERROR_NONE && _bssid) + bssid = _bssid; + + free(_bssid); return ret; } diff --git a/src/my-place/visit-detector/WifiLogger.h b/src/my-place/visit-detector/WifiLogger.h index 7c627a1..cc052fc 100644 --- a/src/my-place/visit-detector/WifiLogger.h +++ b/src/my-place/visit-detector/WifiLogger.h @@ -89,8 +89,8 @@ namespace ctx { void __wifiScanRequest(); int __wifiForeachFoundApsRequest(void *userData); wifi_connection_state_e __wifiGetConnectionStateRequest(); - int __wifiApGetEssidRequest(wifi_ap_h ap, char **essid); - int __wifiApGetBssidRequest(wifi_ap_h ap, char **bssid); + int __wifiApGetEssidRequest(wifi_ap_h ap, std::string &essid); + int __wifiApGetBssidRequest(wifi_ap_h ap, std::string &bssid); /* SYSTEM CAPI CALLBACKS */ static void __wifiDeviceStateChangedCb(wifi_device_state_e state, void *userData); -- 2.7.4 From 16c87c180f9a103d5c624ef63347ae3bf7728e84 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Mon, 5 Dec 2016 13:42:13 +0900 Subject: [PATCH 12/16] Modified RecentBetteryUsage to deliver UsedTime Change-Id: I5a426ddbcf82351619a59e4edc6802f2f12ff5dd Signed-off-by: Somin Kim --- src/battery-stats/BatteryUsage.cpp | 4 ++-- src/battery-stats/Querier.cpp | 36 +++++++++++++++++++++++++++----- src/battery-stats/Querier.h | 6 ++++-- src/battery-stats/RecentBatteryUsage.cpp | 4 ++-- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/battery-stats/BatteryUsage.cpp b/src/battery-stats/BatteryUsage.cpp index 631308d..c443f62 100644 --- a/src/battery-stats/BatteryUsage.cpp +++ b/src/battery-stats/BatteryUsage.cpp @@ -55,9 +55,9 @@ int BatteryUsageProvider::read(Json option, Json *requestResult) int error; if (hasLimit) { - error = querier->queryRangeLimit((startTime > timeSpanPoint)? startTime : timeSpanPoint, endTime, resultSize); + error = querier->query((startTime > timeSpanPoint)? startTime : timeSpanPoint, endTime, resultSize); } else { - error = querier->queryRange((startTime > timeSpanPoint)? startTime : timeSpanPoint, endTime); + error = querier->query((startTime > timeSpanPoint)? startTime : timeSpanPoint, endTime); } IF_FAIL_RETURN_TAG(error == ERR_NONE, ERR_OPERATION_FAILED, _E, "Failed to get app battery usage"); diff --git a/src/battery-stats/Querier.cpp b/src/battery-stats/Querier.cpp index f0f043a..1185165 100644 --- a/src/battery-stats/Querier.cpp +++ b/src/battery-stats/Querier.cpp @@ -19,11 +19,17 @@ #include "BatteryStatisticsTypes.h" #include "Querier.h" -#define BATTERY_USAGE_DEFAULT_QUERY \ +#define BATTERY_USAGE_QUERY \ "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(SUM(" BATTERY_AMOUNT ")/10000.0, 2) AS " KEY_TOTAL_AMOUNT \ " FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d" \ " GROUP BY " KEY_APP_ID " ORDER BY " KEY_TOTAL_AMOUNT " DESC" +#define RECENT_BATTERY_USAGE_QUERY \ + "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(SUM(" BATTERY_AMOUNT ")/10000.0, 2) AS " KEY_TOTAL_AMOUNT \ + ", %d AS " KEY_USED_TIME \ + " FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d" \ + " GROUP BY " KEY_APP_ID " ORDER BY " KEY_TOTAL_AMOUNT " DESC" + using namespace ctx; BatteryStatsQuerier::BatteryStatsQuerier(ContextProvider *provider, Json option) : @@ -41,9 +47,29 @@ int BatteryStatsQuerier::query(const char *sql) return __dbMgr.execute(0, sql, this) ? ERR_NONE : ERR_OPERATION_FAILED; } -int BatteryStatsQuerier::queryRange(int startTime, int endTime) +int BatteryStatsQuerier::query(int startTime, int endTime) +{ + char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY, startTime, endTime); + + int error = query(sql); + sqlite3_free(sql); + + return error; +} + +int BatteryStatsQuerier::query(int startTime, int endTime, int resultSize) +{ + char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY " LIMIT %d", startTime, endTime, resultSize); + + int error = query(sql); + sqlite3_free(sql); + + return error; +} + +int BatteryStatsQuerier::queryRecent(int startTime, int endTime) { - char *sql = sqlite3_mprintf(BATTERY_USAGE_DEFAULT_QUERY, startTime, endTime); + char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY, endTime - startTime, startTime, endTime); int error = query(sql); sqlite3_free(sql); @@ -51,9 +77,9 @@ int BatteryStatsQuerier::queryRange(int startTime, int endTime) return error; } -int BatteryStatsQuerier::queryRangeLimit(int startTime, int endTime, int resultSize) +int BatteryStatsQuerier::queryRecent(int startTime, int endTime, int resultSize) { - char *sql = sqlite3_mprintf(BATTERY_USAGE_DEFAULT_QUERY " LIMIT %d", startTime, endTime, resultSize); + char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY " LIMIT %d", endTime - startTime, startTime, endTime, resultSize); int error = query(sql); sqlite3_free(sql); diff --git a/src/battery-stats/Querier.h b/src/battery-stats/Querier.h index 2385a2f..6d320ae 100644 --- a/src/battery-stats/Querier.h +++ b/src/battery-stats/Querier.h @@ -28,8 +28,10 @@ namespace ctx { ~BatteryStatsQuerier(); // For now, Default result size(10) isn't applied, unlike other history type. - int queryRange(int startTime, int endTime); - int queryRangeLimit(int startTime, int endTime, int resultSize); + int query(int startTime, int endTime); + int query(int startTime, int endTime, int resultSize); + int queryRecent(int startTime, int endTime); + int queryRecent(int startTime, int endTime, int resultSize); protected: int query(const char *sql); diff --git a/src/battery-stats/RecentBatteryUsage.cpp b/src/battery-stats/RecentBatteryUsage.cpp index 0663650..511b55b 100644 --- a/src/battery-stats/RecentBatteryUsage.cpp +++ b/src/battery-stats/RecentBatteryUsage.cpp @@ -38,9 +38,9 @@ int RecentBatteryUsageProvider::read(Json option, Json *requestResult) int error; if (hasLimit) { - error = querier->queryRangeLimit(BatteryMonitor::getLastResetTime(), CURRENT_TIME, resultSize); + error = querier->queryRecent(BatteryMonitor::getLastResetTime(), CURRENT_TIME, resultSize); } else { - error = querier->queryRange(BatteryMonitor::getLastResetTime(), CURRENT_TIME); + error = querier->queryRecent(BatteryMonitor::getLastResetTime(), CURRENT_TIME); } IF_FAIL_RETURN_TAG(error == ERR_NONE, ERR_OPERATION_FAILED, _E, "Failed to get recent app battery usage"); -- 2.7.4 From 1e0cd966adf69a1e6e59a1a47fbf91b51831c662 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Mon, 5 Dec 2016 14:03:08 +0900 Subject: [PATCH 13/16] Version 0.9.9 Change-Id: I83341de2ac6091d1b87fcf7e0a4ab1fb9c045f02 Signed-off-by: Somin Kim --- packaging/context-provider.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/context-provider.spec b/packaging/context-provider.spec index 6443513..acccdbb 100644 --- a/packaging/context-provider.spec +++ b/packaging/context-provider.spec @@ -1,6 +1,6 @@ Name: context-provider Summary: Context Provider -Version: 0.9.8 +Version: 0.9.9 Release: 1 Group: Service/Context License: Apache-2.0 -- 2.7.4 From 9f42eed73a9907361c8a9bc179cda6978763b108 Mon Sep 17 00:00:00 2001 From: Kunal Date: Fri, 9 Dec 2016 17:14:00 +0900 Subject: [PATCH 14/16] Fixed to compare pid, instead of using cpu time difference, when an app is re-launced. Change-Id: Ib2c72bd5634d2555ccdf517985c6711e4818325c Signed-off-by: Kunal --- src/battery-stats/BatteryMonitor.cpp | 11 ++++++++--- src/battery-stats/BatteryStatisticsTypes.h | 4 +++- src/battery-stats/BatteryUsageAnalyzer.cpp | 7 ++++++- src/battery-stats/HeartDbReader.cpp | 1 + 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 7098c9f..6e1687e 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -224,18 +224,21 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage) int timestamp; int stime; int utime; + int pid; while (k < lastCpuUsageLog.size()) { lastCpuUsageLog[k].get(NULL, BATTERY_APP_ID, &appId); lastCpuUsageLog[k].get(NULL, BATTERY_TIMESTAMP, ×tamp); lastCpuUsageLog[k].get(NULL, BATTERY_UTIME, &utime); lastCpuUsageLog[k].get(NULL, BATTERY_STIME, &stime); + lastCpuUsageLog[k].get(NULL, BATTERY_PID, &pid); k++; LastAppCpuUsageInfo lastAppCpuUsage; lastAppCpuUsage.timestamp = timestamp; lastAppCpuUsage.utime = utime; lastAppCpuUsage.stime = stime; + lastAppCpuUsage.pid = pid; (*lastCpuUsage)[appId] = lastAppCpuUsage; } return true; @@ -361,10 +364,10 @@ bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage) _D("Delete all rows from app last times table"); __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL); - std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME "," BATTERY_TIMESTAMP ") VALUES"); + std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME ", " BATTERY_TIMESTAMP ", " BATTERY_PID ") VALUES"); std::string appId; - int stime, utime; + int stime, utime, pid; int timestamp; for (auto it = usage.begin(); it != usage.end(); it++) { @@ -372,11 +375,13 @@ bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage) timestamp = (it->second).timestamp; utime = (it->second).utime; stime = (it->second).stime; + pid = (it->second).pid; query += " ('" + appId + "', " + std::to_string(utime) + ", " + std::to_string(stime) - + ", " + std::to_string(timestamp) + ")"; + + ", " + std::to_string(timestamp) + + ", " + std::to_string(pid) + ")"; query += ", "; } diff --git a/src/battery-stats/BatteryStatisticsTypes.h b/src/battery-stats/BatteryStatisticsTypes.h index d22faa7..31622bc 100644 --- a/src/battery-stats/BatteryStatisticsTypes.h +++ b/src/battery-stats/BatteryStatisticsTypes.h @@ -44,7 +44,7 @@ #define BATTERY_LAST_CPU_USAGE_TABLE "Temp_LastCpuUsagePerApp" #define BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS \ "AppId TEXT PRIMARY KEY, Utime INTEGER NOT NULL DEFAULT 0, " \ - "Stime INTEGER NOT NULL DEFAULT 0, Timestamp INTEGER NOT NULL DEFAULT 0" + "Stime INTEGER NOT NULL DEFAULT 0, Timestamp INTEGER NOT NULL DEFAULT 0, Pid INTEGER NOT NULL DEFAULT 0" #define BATTERY_APP_ID "AppId" #define BATTERY_INDEX "idx" @@ -61,6 +61,7 @@ #define BATTERY_LAST_PERCENT_TIME "LastPercentTime" #define BATTERY_LAST_HEART_TIMESTAMP "LastHeartTimestamp" #define BATTERY_ROW_ID "RowId" +#define BATTERY_PID "Pid" #define CURRENT_TIME (int)(time(0)) @@ -70,6 +71,7 @@ namespace ctx { int timestamp; int utime; int stime; + int pid; }; typedef std::map CpuUsageMap; } diff --git a/src/battery-stats/BatteryUsageAnalyzer.cpp b/src/battery-stats/BatteryUsageAnalyzer.cpp index 66dd430..9a01f02 100644 --- a/src/battery-stats/BatteryUsageAnalyzer.cpp +++ b/src/battery-stats/BatteryUsageAnalyzer.cpp @@ -48,6 +48,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std int stime; int utime; int idx; + int pid; int totalUtime = 0; int totalStime = 0; @@ -57,6 +58,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std cpuUsageLog[i].get(NULL, BATTERY_UTIME, &utime); cpuUsageLog[i].get(NULL, BATTERY_STIME, &stime); cpuUsageLog[i].get(NULL, BATTERY_INDEX, &idx); + cpuUsageLog[i].get(NULL, BATTERY_PID, &pid); if (timestamp < lastTime) { continue; @@ -75,14 +77,16 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std int prevUtime = 0; int prevStime = 0; int prevTimestamp = 0; + int prevPid = 0; if (lastAppCpuUsage != lastCpuUsage.end()) { prevTimestamp = (lastAppCpuUsage->second).timestamp; prevUtime = (lastAppCpuUsage->second).utime; prevStime = (lastAppCpuUsage->second).stime; + prevPid = (lastAppCpuUsage->second).pid; // If the app is killed and launched again - if (prevStime > stime || prevUtime > utime) { + if (pid != prevPid) { prevUtime = 0; prevStime = 0; prevTimestamp = lastTime; @@ -118,6 +122,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std lastAppUsage.timestamp = timestamp; lastAppUsage.utime = utime; lastAppUsage.stime = stime; + lastAppUsage.pid = pid; lastCpuUsage[appId] = lastAppUsage; } __normalizeBatteryUsage(usage, totalUtime, totalStime); diff --git a/src/battery-stats/HeartDbReader.cpp b/src/battery-stats/HeartDbReader.cpp index 456d0d5..b19a5a0 100644 --- a/src/battery-stats/HeartDbReader.cpp +++ b/src/battery-stats/HeartDbReader.cpp @@ -132,4 +132,5 @@ void HeartDbReader::__convertCpuUsageLog(Json& row, Json* newRow) newRow->set(NULL, BATTERY_STIME, stime); newRow->set(NULL, BATTERY_TYPE, type); newRow->set(NULL, BATTERY_INDEX, index); + newRow->set(NULL, BATTERY_PID, pid); } -- 2.7.4 From 32978308c71bd6ef5d679b175daa235c8c3e1730 Mon Sep 17 00:00:00 2001 From: Kunal Date: Mon, 12 Dec 2016 20:58:26 +0900 Subject: [PATCH 15/16] Writing last battery infos periodically rather than just at end To ensure no loss of info in case of sudden shutdown Change-Id: Ifc2d3f18acab5d99c3ffe53578b17dcfbb91cb1b Signed-off-by: Kunal --- src/battery-stats/BatteryMonitor.cpp | 96 +++++++++++++++++++----------- src/battery-stats/BatteryMonitor.h | 4 +- src/battery-stats/BatteryStatisticsTypes.h | 7 ++- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 6e1687e..2a71946 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -31,12 +31,16 @@ static int __bootingTime = 0; static int __lastResetTime = 0; static int __lastPercent = -1; static int __lastPercentTime = 0; -static int __lastHeartTimestamp = 0; +static int __lastBatteryTimeInfoDeleteTime = 0; static bool __isCharging = 0; -static std::vector __batteryTimeInfoVec; static bool __timerRunning = false; +static std::vector __batteryTimeInfoVec; -#define DB_QUERY_DELAY 600000 +#define HEART_DB_QUERY_DELAY 600000 +#define BATTERY_TIME_INFO_DELETE_DELAY 10 * 3600 + +#define UPDATE_BATTERY_LOGS_QUERY \ + "UPDATE " BATTERY_TEMP_TIME_INFO " SET " BATTERY_TO_BE_PROCESSED " = %d WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d" BatteryMonitor::BatteryMonitor() { @@ -81,41 +85,24 @@ bool BatteryMonitor::__loadLastInfo() records[0].get(NULL, BATTERY_LAST_RESET_TIME, &__lastResetTime); records[0].get(NULL, BATTERY_LAST_PERCENT, &__lastPercent); records[0].get(NULL, BATTERY_LAST_PERCENT_TIME, &__lastPercentTime); - records[0].get(NULL, BATTERY_LAST_HEART_TIMESTAMP, &__lastHeartTimestamp); + records[0].get(NULL, BATTERY_TIME_INFO_LAST_DELETE_TIME, &__lastBatteryTimeInfoDeleteTime); } records.clear(); __batteryTimeInfoVec.clear(); - ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec); - - ret = __dbMgr.executeSync("DELETE FROM " BATTERY_TEMP_TIME_INFO, &records); - + ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO " WHERE " BATTERY_TO_BE_PROCESSED " = 1", &__batteryTimeInfoVec); return true; } bool BatteryMonitor::__updateLastInfo() { - if (__batteryTimeInfoVec.size() > 0) { - for (unsigned int i = 0; i < __batteryTimeInfoVec.size(); i++) { - int64_t rowId; - __dbMgr.insertSync(BATTERY_TEMP_TIME_INFO, __batteryTimeInfoVec.at(i), &rowId); - } - } - - IF_FAIL_RETURN_TAG(__lastResetTime != 0 || __lastPercentTime != 0, false, _W, "Last info doesn't exist"); - - char *query = sqlite3_mprintf( - "INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \ - BATTERY_ROW_ID ", " BATTERY_LAST_RESET_TIME ", " \ - BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME ", " BATTERY_LAST_HEART_TIMESTAMP ") VALUES (%s, %d, %d, %d, %d)", - DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime, __lastHeartTimestamp); - - std::vector records; - bool ret = __dbMgr.executeSync(query, &records); + char *query = sqlite3_mprintf("INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \ + BATTERY_ROW_ID ", " BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME \ + ", " BATTERY_TIME_INFO_LAST_DELETE_TIME ", " BATTERY_LAST_RESET_TIME ") VALUES (%s, %d, %d, %d, %d)", + DEFAULT_ROW_ID_STR, __lastPercent, __lastPercentTime, __lastBatteryTimeInfoDeleteTime, __lastResetTime); + bool ret = __dbMgr.executeSync(query, NULL); sqlite3_free(query); - IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to update battery stat last info"); - - return true; + return ret; } void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void* userData) @@ -133,18 +120,25 @@ void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void battTimeInfo.set(NULL, BATTERY_AMOUNT, percent); battTimeInfo.set(NULL, BATTERY_START_TIME, __lastPercentTime); battTimeInfo.set(NULL, BATTERY_END_TIME, currentTime); + battTimeInfo.set(NULL, BATTERY_TO_BE_PROCESSED, 1); __batteryTimeInfoVec.push_back(battTimeInfo); + int64_t rowId; + (instance->__dbMgr).insertSync(BATTERY_TEMP_TIME_INFO, battTimeInfo, &rowId); + bool dataRemaining = instance->processBatteryUsage(); if (dataRemaining && !__timerRunning) { __timerRunning = true; _D("Start timer to request HEART data"); - g_timeout_add(DB_QUERY_DELAY, __timeoutCb, instance); + g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, instance); } } __lastPercentTime = currentTime; __lastPercent = percent; + + bool ret = instance->__updateLastInfo(); + IF_FAIL_VOID_TAG(ret, _E, "Failed to update last percent and last percent times"); } void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void* userData) @@ -153,6 +147,7 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void __isCharging = intptr_t(value); IF_FAIL_VOID(!__isCharging); + BatteryMonitor* instance = static_cast(userData); int percent; if (__lastPercent < 0) { @@ -167,6 +162,9 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void __lastResetTime = CURRENT_TIME; __lastPercentTime = __lastResetTime; _D("Charger is disconnected after fully charged. ResetTime: %d", __lastResetTime); + + bool ret = instance->__updateLastInfo(); + IF_FAIL_VOID_TAG(ret, _E, "Failed to update last reset time and last percent time"); } } @@ -193,15 +191,13 @@ int BatteryMonitor::start() IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to set battery charging change cb"); __timerRunning = true; - _D("Start timer to request HEAERT data"); - g_timeout_add(DB_QUERY_DELAY, __timeoutCb, this); + _D("Start timer to request HEART data"); + g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, this); return ERR_NONE; } int BatteryMonitor::stop() { - __updateLastInfo(); - int error = device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, __batteryChangeCb); IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to remove callback for battery capacity"); @@ -267,7 +263,8 @@ bool BatteryMonitor::processBatteryUsage() _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime); // Get the last timestamp of HEART cpu data and maximum app times tables in cache - cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartTimestamp); + int lastHeartTimestamp; + cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &lastHeartTimestamp); CpuUsageMap lastCpuUsage; ret = __getLastCpuUsageTable(&lastCpuUsage); @@ -280,7 +277,7 @@ bool BatteryMonitor::processBatteryUsage() row.get(NULL, BATTERY_START_TIME, &startTime); row.get(NULL, BATTERY_END_TIME, &endTime); - if (endTime > __lastHeartTimestamp) { + if (endTime > lastHeartTimestamp) { _W("[%d] Heart cpu data is not prepared yet (%d ~ %d)", i, startTime, endTime); break; } @@ -301,17 +298,25 @@ bool BatteryMonitor::processBatteryUsage() } } - // Insert log of last times of apps + // Insert log of last times of apps, and update battery infos ToBeProcessed field if (i != 0) { ret = __insertLastCpuUsageLog(lastCpuUsage); if (!ret) { _E("Failed to insert last Cpu Usage of apps"); } + + int processedStartTime = totalStartTime; + int processedEndTime; + __batteryTimeInfoVec[i-1].get(NULL, BATTERY_END_TIME, &processedEndTime); + char *sql = sqlite3_mprintf(UPDATE_BATTERY_LOGS_QUERY, 0, processedStartTime, processedEndTime); + __dbMgr.executeSync(sql, NULL); + sqlite3_free(sql); } // Remove completed time info _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i); __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i); + __deleteBatteryTimeInfo(); if (__batteryTimeInfoVec.size() == 0) { return false; @@ -401,3 +406,22 @@ int BatteryMonitor::getLastResetTime() return __lastResetTime; } + +void BatteryMonitor::__deleteBatteryTimeInfo() +{ + int batteryTimeInfoDeleteTime = CURRENT_TIME - BATTERY_TIME_INFO_DELETE_DELAY; + if (__lastBatteryTimeInfoDeleteTime > batteryTimeInfoDeleteTime) { + _D("Old battery time info already deleted"); + return; + } + + std::string query("DELETE FROM " BATTERY_TEMP_TIME_INFO " WHERE StartTime <= "); + query += std::to_string(batteryTimeInfoDeleteTime); + + __dbMgr.executeSync(query.c_str(), NULL); + __lastBatteryTimeInfoDeleteTime = CURRENT_TIME; + + bool ret = __updateLastInfo(); + IF_FAIL_VOID_TAG(ret, _E, "Failed to update last battery time info deletion time"); + _D("Old battery time info deleted"); +} diff --git a/src/battery-stats/BatteryMonitor.h b/src/battery-stats/BatteryMonitor.h index 2ac3db8..c97a2bc 100644 --- a/src/battery-stats/BatteryMonitor.h +++ b/src/battery-stats/BatteryMonitor.h @@ -40,7 +40,6 @@ namespace ctx { bool __loadLastInfo(); bool __updateLastInfo(); - static int __timeoutCb(void* data); static void __batteryChangeCb(device_callback_e type, void* value, void* userData); static void __chargerChangeCb(device_callback_e type, void* value, void* userData); @@ -48,6 +47,9 @@ namespace ctx { bool __insertLastCpuUsageLog(CpuUsageMap& usage); bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage); + void __deleteBatteryTimeInfo(); + static int __timeoutCb(void* data); + DatabaseManager __dbMgr; BatteryUsageAnalyzer __analyzer; diff --git a/src/battery-stats/BatteryStatisticsTypes.h b/src/battery-stats/BatteryStatisticsTypes.h index 31622bc..d9bead6 100644 --- a/src/battery-stats/BatteryStatisticsTypes.h +++ b/src/battery-stats/BatteryStatisticsTypes.h @@ -34,12 +34,12 @@ "LastResetTime UNSIGNED INT NOT NULL DEFAULT 0, " \ "LastPercent UNSIGNED INT NOT NULL DEFAULT 0, " \ "LastPercentTime UNSIGNED INT NOT NULL DEFAULT 0, " \ - "LastHeartTimestamp UNSIGNED INT NOT NULL DEFAULT 0" + "LastBatteryTimeInfoDeleteTime UNSIGNED INT NOT NULL DEFAULT 0" #define BATTERY_TEMP_TIME_INFO "Temp_BatteryTimeInfo" #define BATTERY_TEMP_TIME_INFO_COLUMNS \ "Amount INTEGER NOT NULL DEFAULT 0, StartTime INTEGER NOT NULL DEFAULT 0, " \ - "EndTime INTEGER NOT NULL DEFAULT 0" + "EndTime INTEGER NOT NULL DEFAULT 0, ToBeProcessed BOOLEAN DEFAULT 1" #define BATTERY_LAST_CPU_USAGE_TABLE "Temp_LastCpuUsagePerApp" #define BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS \ @@ -59,8 +59,9 @@ #define BATTERY_LAST_RESET_TIME "LastResetTime" #define BATTERY_LAST_PERCENT "LastPercent" #define BATTERY_LAST_PERCENT_TIME "LastPercentTime" -#define BATTERY_LAST_HEART_TIMESTAMP "LastHeartTimestamp" +#define BATTERY_TIME_INFO_LAST_DELETE_TIME "LastBatteryTimeInfoDeleteTime" #define BATTERY_ROW_ID "RowId" +#define BATTERY_TO_BE_PROCESSED "ToBeProcessed" #define BATTERY_PID "Pid" #define CURRENT_TIME (int)(time(0)) -- 2.7.4 From 5236411d92b567c2dc9ec8793180a3f4336f5123 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Fri, 16 Dec 2016 20:24:08 +0900 Subject: [PATCH 16/16] Changed battery usage normalization logic & Version 0.9.10 Change-Id: Ic27d93caf1322276d93da199cf19d5fc9cc9aae3 Signed-off-by: Somin Kim --- packaging/context-provider.spec | 2 +- src/battery-stats/Querier.cpp | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packaging/context-provider.spec b/packaging/context-provider.spec index acccdbb..cc39fd3 100644 --- a/packaging/context-provider.spec +++ b/packaging/context-provider.spec @@ -1,6 +1,6 @@ Name: context-provider Summary: Context Provider -Version: 0.9.9 +Version: 0.9.10 Release: 1 Group: Service/Context License: Apache-2.0 diff --git a/src/battery-stats/Querier.cpp b/src/battery-stats/Querier.cpp index 1185165..15c8f95 100644 --- a/src/battery-stats/Querier.cpp +++ b/src/battery-stats/Querier.cpp @@ -20,12 +20,16 @@ #include "Querier.h" #define BATTERY_USAGE_QUERY \ - "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(SUM(" BATTERY_AMOUNT ")/10000.0, 2) AS " KEY_TOTAL_AMOUNT \ + "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(100.0 * SUM(" BATTERY_AMOUNT ") / " \ + "(SELECT SUM(" BATTERY_AMOUNT ") FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d)" \ + ", 2) AS " KEY_TOTAL_AMOUNT \ " FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d" \ " GROUP BY " KEY_APP_ID " ORDER BY " KEY_TOTAL_AMOUNT " DESC" #define RECENT_BATTERY_USAGE_QUERY \ - "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(SUM(" BATTERY_AMOUNT ")/10000.0, 2) AS " KEY_TOTAL_AMOUNT \ + "SELECT " BATTERY_APP_ID " AS " KEY_APP_ID ", ROUND(100.0 * SUM(" BATTERY_AMOUNT ") / " \ + "(SELECT SUM(" BATTERY_AMOUNT ") FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d)" \ + ", 2) AS " KEY_TOTAL_AMOUNT \ ", %d AS " KEY_USED_TIME \ " FROM " BATTERY_USAGE_TABLE " WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d" \ " GROUP BY " KEY_APP_ID " ORDER BY " KEY_TOTAL_AMOUNT " DESC" @@ -49,7 +53,7 @@ int BatteryStatsQuerier::query(const char *sql) int BatteryStatsQuerier::query(int startTime, int endTime) { - char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY, startTime, endTime); + char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY, startTime, endTime, startTime, endTime); int error = query(sql); sqlite3_free(sql); @@ -59,7 +63,7 @@ int BatteryStatsQuerier::query(int startTime, int endTime) int BatteryStatsQuerier::query(int startTime, int endTime, int resultSize) { - char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY " LIMIT %d", startTime, endTime, resultSize); + char *sql = sqlite3_mprintf(BATTERY_USAGE_QUERY " LIMIT %d", startTime, endTime, startTime, endTime, resultSize); int error = query(sql); sqlite3_free(sql); @@ -69,7 +73,7 @@ int BatteryStatsQuerier::query(int startTime, int endTime, int resultSize) int BatteryStatsQuerier::queryRecent(int startTime, int endTime) { - char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY, endTime - startTime, startTime, endTime); + char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY, startTime, endTime, endTime - startTime, startTime, endTime); int error = query(sql); sqlite3_free(sql); @@ -79,7 +83,7 @@ int BatteryStatsQuerier::queryRecent(int startTime, int endTime) int BatteryStatsQuerier::queryRecent(int startTime, int endTime, int resultSize) { - char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY " LIMIT %d", endTime - startTime, startTime, endTime, resultSize); + char *sql = sqlite3_mprintf(RECENT_BATTERY_USAGE_QUERY " LIMIT %d", startTime, endTime, endTime - startTime, startTime, endTime, resultSize); int error = query(sql); sqlite3_free(sql); -- 2.7.4