[Systeminfo] Implementation for remaining time to charge/discharge battery 02/121302/4
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Mon, 3 Apr 2017 10:18:51 +0000 (12:18 +0200)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Mon, 3 Apr 2017 10:18:51 +0000 (12:18 +0200)
[Feature] Added attributes timeToDischarge & timeToFullCharge
          to interface SystemInfoBattery

[Verification] TCT systeminfo tests passed 100 %

Change-Id: Iac34bee547d004a9858a3aed7e8fcc736d2e6086
Signed-off-by: Szymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
src/systeminfo/systeminfo_api.js
src/systeminfo/systeminfo_properties_manager.cc

index ce4755b..919b353 100644 (file)
@@ -413,6 +413,16 @@ function SystemInfoBattery(data) {
             value : data.isCharging,
             writable : false,
             enumerable : true
+        },
+        timeToDischarge : {
+            value : data.timeToDischarge,
+            writable : false,
+            enumerable : true
+        },
+        timeToFullCharge : {
+            value : data.timeToFullCharge,
+            writable : false,
+            enumerable : true
         }
     });
 }
index 8a5c6e4..955719b 100644 (file)
@@ -32,6 +32,7 @@
 #include "common/scope_exit.h"
 #include "systeminfo/systeminfo-utils.h"
 #include "common/filesystem/filesystem_provider.h"
+#include "common/GDBus/connection.h"
 
 namespace extension {
 namespace systeminfo {
@@ -46,6 +47,11 @@ const double kDisplayInchToMillimeter = 2.54;
 //Battery
 const double kRemainingBatteryChargeMax = 100.0;
 const int kVconfErrorNone = 0;
+const char *kBatteryTarget = "org.tizen.resourced";
+const char *kBatteryObject = "/Org/Tizen/ResourceD/Logging";
+const char *kBatteryInterface = "org.tizen.resourced.logging";
+const char *kBatteryGetBatteryChargingTime = "GetBatteryChargingTime";
+const char *kBatteryGetBatteryRemainingTime = "GetBatteryRemainingTime";
 //Display
 const double kDisplayBrightnessDivideValue = 100;
 //Device Orientation
@@ -209,7 +215,62 @@ PlatformResult SysteminfoPropertiesManager::ReportBattery(picojson::object* out)
               ErrorCode::UNKNOWN_ERR, (log_msg + std::to_string(ret)),
               ("vconf_get_int error: %d (%s)", ret, get_error_message(ret)));
   }
-  out->insert(std::make_pair("isCharging", picojson::value(0 != value)));
+  bool isCharging = (0 != value);
+  out->insert(std::make_pair("isCharging", picojson::value(isCharging)));
+
+  if (nullptr == common::dbus::Connection::getInstance().getDBus()) {
+    return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "dbus wasn't initialized");
+  }
+
+  GError* error = nullptr;
+  GVariant *variant = nullptr;
+  if (isCharging) {
+    variant = g_dbus_connection_call_sync(common::dbus::Connection::getInstance().getDBus(),
+                                          kBatteryTarget,
+                                          kBatteryObject,
+                                          kBatteryInterface,
+                                          kBatteryGetBatteryChargingTime,
+                                          g_variant_new("()"),
+                                          NULL,
+                                           G_DBUS_CALL_FLAGS_NONE,
+                                          -1,
+                                          NULL,
+                                          &error);
+  } else {
+    variant = g_dbus_connection_call_sync(common::dbus::Connection::getInstance().getDBus(),
+                                          kBatteryTarget,
+                                          kBatteryObject,
+                                          kBatteryInterface,
+                                          kBatteryGetBatteryRemainingTime,
+                                          g_variant_new("(i)", 0),   // 0 - POWER_NORMAL_MODE
+                                          NULL,                      // 1 - POWER_SAVING_MODE
+                                          G_DBUS_CALL_FLAGS_NONE,    // 2 - ULTRA_SAVING_MODE
+                                          -1,                        // for now, only 0 is supported
+                                          NULL,
+                                          &error);
+  }
+
+  if (!variant || error) {
+    std::string message = error ? error->message : "";
+    g_error_free(error);
+    return LogAndCreateResult(
+        ErrorCode::UNKNOWN_ERR,
+        "DBus returned error",
+        ("Failed to call %s method - %s",
+        isCharging ? "GetBatteryChargingTime" : "GetBatteryRemainingTime", message.c_str()));
+  } else {
+    int time = 0;
+    g_variant_get(variant, "(i)", &time);
+    g_variant_unref(variant);
+    if (isCharging) {
+      out->insert(std::make_pair("timeToFullCharge", picojson::value(static_cast<double>(time))));
+      out->insert(std::make_pair("timeToDischarge", picojson::value()));
+    } else {
+      out->insert(std::make_pair("timeToFullCharge", picojson::value()));
+      out->insert(std::make_pair("timeToDischarge", picojson::value(static_cast<double>(time))));
+    }
+  }
+
   return PlatformResult(ErrorCode::NO_ERROR);
 }