Bug 717150: Support adjust system clock and set timezone APIs in hal layer r=cjones.
authorVincent Chang <vchang@mozilla.com>
Fri, 17 Feb 2012 02:44:00 -0800
changeset 90727 5c24d8549c37a7e2496dbfbdcf26bd5228710646
parent 90726 44b5705eeb2102a4d8535b0f6cc438f21d620618
child 90728 90cb54bb0101f57ae31254b64f6b12cea6eaf632
push id136
push userlsblakk@mozilla.com
push dateFri, 01 Jun 2012 02:39:32 +0000
treeherdermozilla-release@7ebf7352c959 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs717150
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 717150: Support adjust system clock and set timezone APIs in hal layer r=cjones.
hal/Hal.cpp
hal/Hal.h
hal/Makefile.in
hal/fallback/FallbackTime.cpp
hal/gonk/GonkHal.cpp
hal/sandbox/PHal.ipdl
hal/sandbox/SandboxHal.cpp
--- a/hal/Hal.cpp
+++ b/hal/Hal.cpp
@@ -318,16 +318,31 @@ bool SetLight(LightType light, const hal
 }
 
 bool GetLight(LightType light, hal::LightConfiguration* aConfig)
 {
   AssertMainThread();
   RETURN_PROXY_IF_SANDBOXED(GetLight(light, aConfig));
 }
 
+
+void 
+AdjustSystemClock(int32_t aDeltaMilliseconds)
+{
+  AssertMainThread();
+  PROXY_IF_SANDBOXED(AdjustSystemClock(aDeltaMilliseconds));
+}
+
+void 
+SetTimezone(const nsCString& aTimezoneSpec)
+{
+  AssertMainThread();
+  PROXY_IF_SANDBOXED(SetTimezone(aTimezoneSpec));
+}
+
 void
 EnableSensorNotifications(SensorType aSensor) {
   AssertMainThread();
   PROXY_IF_SANDBOXED(EnableSensorNotifications(aSensor));
 }
 
 void
 DisableSensorNotifications(SensorType aSensor) {
--- a/hal/Hal.h
+++ b/hal/Hal.h
@@ -213,16 +213,29 @@ void GetCurrentNetworkInformation(hal::N
 
 /**
  * Notify of a change in the network state.
  * @param aNetworkInfo The new network information.
  */
 void NotifyNetworkChange(const hal::NetworkInformation& aNetworkInfo);
 
 /**
+ * Adjusting system clock.
+ * @param aDeltaMilliseconds The difference compared with current system clock.
+ */
+void AdjustSystemClock(int32_t aDeltaMilliseconds);
+
+/**
+ * Set timezone
+ * @param aTimezoneSpec The definition can be found in 
+ * http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
+ */
+void SetTimezone(const nsCString& aTimezoneSpec);
+
+/**
  * Reboot the device.
  */
 void Reboot();
 
 /**
  * Power off the device.
  */
 void PowerOff();
--- a/hal/Makefile.in
+++ b/hal/Makefile.in
@@ -100,17 +100,17 @@ CPPSRCS += \
 else
 CPPSRCS += \
   FallbackHal.cpp \
   FallbackSensor.cpp \
   $(NULL)
 endif
 
 ifneq (gonk,$(MOZ_WIDGET_TOOLKIT)) #{
-CPPSRCS += FallbackLights.cpp
+CPPSRCS += FallbackLights.cpp FallbackTime.cpp
 endif #}
 
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
 
 CFLAGS          += $(MOZ_DBUS_GLIB_CFLAGS)
 CXXFLAGS        += $(MOZ_DBUS_GLIB_CFLAGS) -DHAVE_PTHREADS
new file mode 100644
--- /dev/null
+++ b/hal/fallback/FallbackTime.cpp
@@ -0,0 +1,22 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * vim: sw=2 ts=8 et :
+ */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "Hal.h"
+
+namespace mozilla {
+namespace hal_impl {
+
+void 
+AdjustSystemClock(int32_t aDeltaMilliseconds)
+{}
+
+void
+SetTimezone(const nsCString& aTimezoneSpec)
+{}
+
+} // namespace hal_impl
+} // namespace mozilla
--- a/hal/gonk/GonkHal.cpp
+++ b/hal/gonk/GonkHal.cpp
@@ -21,17 +21,26 @@
 #include "nsIObserverService.h"
 #include "hardware/lights.h"
 #include "hardware/hardware.h"
 #include "hardware_legacy/vibrator.h"
 #include <stdio.h>
 #include <math.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <cutils/properties.h>
 #include "mozilla/dom/network/Constants.h"
+#include <android/log.h>
+
+#define LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
+#define NsecPerMsec  1000000
+#define NsecPerSec   1000000000
+
 
 using mozilla::hal::WindowIdentifier;
 
 namespace mozilla {
 namespace hal_impl {
 
 namespace {
 
@@ -505,16 +514,62 @@ GetLight(hal::LightType light, hal::Ligh
   aConfig->flash() = hal::FlashMode(state.flashMode);
   aConfig->flashOnMS() = state.flashOnMS;
   aConfig->flashOffMS() = state.flashOffMS;
   aConfig->mode() = hal::LightMode(state.brightnessMode);
 
   return true;
 }
 
+/**
+ * clock_settime() is not exposed through bionic. 
+ * we define the new function to set system time.
+ * The result is the same as using clock_settime() system call.     
+ */
+static int
+sys_clock_settime(clockid_t clk_id, const struct timespec *tp)
+{
+  return syscall(__NR_clock_settime, clk_id, tp);
+}
+
+void 
+AdjustSystemClock(int32_t aDeltaMilliseconds)
+{
+  struct timespec now;
+  
+  // Preventing context switch before setting system clock 
+  sched_yield();
+  clock_gettime(CLOCK_REALTIME, &now);
+  now.tv_sec += aDeltaMilliseconds/1000;
+  now.tv_nsec += (aDeltaMilliseconds%1000)*NsecPerMsec;
+  if (now.tv_nsec >= NsecPerSec)
+  {
+    now.tv_sec += 1;
+    now.tv_nsec -= NsecPerSec;
+  }
+
+  if (now.tv_nsec < 0)
+  {
+    now.tv_nsec += NsecPerSec;
+    now.tv_sec -= 1;  
+  }
+  // we need to have root privilege. 
+  sys_clock_settime(CLOCK_REALTIME, &now);   
+}
+
+void 
+SetTimezone(const nsCString& aTimezoneSpec)
+{ 
+  property_set("persist.sys.timezone", aTimezoneSpec.get());
+  // this function is automatically called by the other time conversion 
+  // functions that depend on the timezone. To be safe, we call it manually.  
+  tzset();
+}
+
+
 void
 EnableNetworkNotifications()
 {}
 
 void
 DisableNetworkNotifications()
 {}
 
--- a/hal/sandbox/PHal.ipdl
+++ b/hal/sandbox/PHal.ipdl
@@ -104,16 +104,19 @@ parent:
     sync GetCurrentNetworkInformation()
       returns (NetworkInformation aNetworkInfo);
 
     sync GetScreenEnabled() returns (bool enabled);
     SetScreenEnabled(bool enabled);
 
     sync GetScreenBrightness() returns (double brightness);
     SetScreenBrightness(double brightness);
+    
+    AdjustSystemClock(int32 aDeltaMilliseconds);
+    SetTimezone(nsCString aTimezoneSpec);
 
     sync SetLight(LightType light, LightConfiguration aConfig)
       returns (bool status);
     sync GetLight(LightType light)
       returns (LightConfiguration aConfig, bool status);
 
     Reboot();
     PowerOff();
--- a/hal/sandbox/SandboxHal.cpp
+++ b/hal/sandbox/SandboxHal.cpp
@@ -130,16 +130,28 @@ SetLight(hal::LightType light, const hal
 bool
 GetLight(hal::LightType light, hal::LightConfiguration* aConfig)
 {
   bool status;
   Hal()->SendGetLight(light, aConfig, &status);
   return status;
 }
 
+void 
+AdjustSystemClock(int32_t aDeltaMilliseconds)
+{
+  Hal()->SendAdjustSystemClock(aDeltaMilliseconds);
+}
+
+void
+SetTimezone(const nsCString& aTimezoneSpec)
+{
+  Hal()->SendSetTimezone(nsCString(aTimezoneSpec));
+} 
+
 void
 Reboot()
 {
   Hal()->SendReboot();
 }
 
 void
 PowerOff()
@@ -284,16 +296,30 @@ public:
   NS_OVERRIDE virtual bool
   RecvGetLight(const LightType& aLight, LightConfiguration* aConfig, bool* status)
   {
     *status = hal::GetLight(aLight, aConfig);
     return true;
   }
 
   NS_OVERRIDE virtual bool
+  RecvAdjustSystemClock(const int32_t &aDeltaMilliseconds)
+  {
+    hal::AdjustSystemClock(aDeltaMilliseconds);
+    return true;
+  }
+
+  NS_OVERRIDE virtual bool 
+  RecvSetTimezone(const nsCString& aTimezoneSpec)
+  {
+    hal::SetTimezone(aTimezoneSpec);
+    return true;  
+  }
+
+  NS_OVERRIDE virtual bool
   RecvReboot()
   {
     hal::Reboot();
     return true;
   }
 
   NS_OVERRIDE virtual bool
   RecvPowerOff()