aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlin Jerpelea <alin.jerpelea@sonymobile.com>2016-08-04 16:59:10 +0200
committerGitHub <noreply@github.com>2016-08-04 16:59:10 +0200
commitc44898e1bd7237df857057697a078e03e9516730 (patch)
tree618b67cf6032a5e25eb8b7e30d0bbcc93a161c2c
parentabed5961b9b78445b43e418f4581a0b0c816d651 (diff)
parent04996642e2ffa21058ccdf91c8f47e0566a47c06 (diff)
Merge pull request #8 from humberos/master
timekeep: Store the adjusted value into ats_2 file
-rw-r--r--src/com/sony/timekeep/TimeKeep.java34
-rw-r--r--timekeep.c22
2 files changed, 56 insertions, 0 deletions
diff --git a/src/com/sony/timekeep/TimeKeep.java b/src/com/sony/timekeep/TimeKeep.java
index 484ca3c..1bfe5e1 100644
--- a/src/com/sony/timekeep/TimeKeep.java
+++ b/src/com/sony/timekeep/TimeKeep.java
@@ -31,8 +31,15 @@
package com.sony.timekeep;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.IOException;
+import java.lang.Long;
import java.lang.System;
import android.content.BroadcastReceiver;
@@ -45,6 +52,7 @@ public class TimeKeep extends BroadcastReceiver {
private static final String TAG = "TimeKeep-Receiver";
private static final String TIMEADJ_PROP = "persist.sys.timeadjust";
private static final String RTC_SINCE_EPOCH = "/sys/class/rtc/rtc0/since_epoch";
+ private static final String RTC_ATS_FILE = "/data/time/ats_2";
@Override
public void onReceive(Context context, Intent intent) {
@@ -58,6 +66,7 @@ public class TimeKeep extends BroadcastReceiver {
Log.d(TAG, "Setting adjust property to " + seconds);
SystemProperties.set(TIMEADJ_PROP, Long.toString(seconds));
+ writeATS(seconds);
}
private long readEpoch() {
@@ -80,4 +89,29 @@ public class TimeKeep extends BroadcastReceiver {
return epoch;
}
+
+ private void writeATS(long seconds) {
+ BufferedOutputStream bos = null;
+ long milliseconds = seconds * 1000;
+ ByteBuffer buffer = ByteBuffer.allocate(8).order(ByteOrder.nativeOrder());
+ buffer.putLong(milliseconds);
+ try {
+ bos = new BufferedOutputStream(new FileOutputStream(RTC_ATS_FILE, false));
+ bos.write(buffer.array());
+ bos.flush();
+ } catch (FileNotFoundException ex) {
+ Log.w(TAG, "file " + RTC_ATS_FILE + " not found: " + ex);
+ } catch (IOException ex) {
+ Log.w(TAG, "IOException trying to sync " + RTC_ATS_FILE + ": " + ex);
+ } finally {
+ if (bos != null) {
+ try {
+ Log.w(TAG, "file " + RTC_ATS_FILE + ": " + milliseconds);
+ bos.close();
+ } catch (IOException ex) {
+ Log.w(TAG, "IOException while closing synced file: ", ex);
+ }
+ }
+ }
+ }
}
diff --git a/timekeep.c b/timekeep.c
index 5e48bbe..6bb9575 100644
--- a/timekeep.c
+++ b/timekeep.c
@@ -33,6 +33,7 @@
#include <string.h>
#include <fcntl.h>
+#include <sys/stat.h>
#include <sys/time.h>
#define LOG_TAG "TimeKeep"
@@ -42,6 +43,7 @@
#include <errno.h>
#define RTC_SYS_FILE "/sys/class/rtc/rtc0/since_epoch"
+#define RTC_ATS_FILE "/data/time/ats_2"
#define TIME_ADJUST_PROP "persist.sys.timeadjust"
int read_epoch(unsigned long* epoch) {
@@ -70,6 +72,24 @@ int read_epoch(unsigned long* epoch) {
return res;
}
+void restore_ats(unsigned long value) {
+ FILE *fp = NULL;
+ char mode[] = "0777";
+ int i;
+
+ i = strtol(mode, 0, 8);
+ value *= 1000;
+ fp = fopen(RTC_ATS_FILE, "wb");
+
+ if (fp != NULL) {
+ chmod(RTC_ATS_FILE, i);
+ fwrite(&value, sizeof(value), 1, fp);
+ fclose(fp);
+ } else {
+ ALOGI("Can't restore " RTC_ATS_FILE);
+ }
+}
+
int store_time() {
char prop[PROPERTY_VALUE_MAX];
unsigned long seconds = 0;
@@ -90,6 +110,7 @@ int store_time() {
} else {
seconds -= epoch_since;
snprintf(prop, PROPERTY_VALUE_MAX, "%lu", seconds);
+ restore_ats(seconds);
property_set(TIME_ADJUST_PROP, prop);
ALOGI("Time adjustment stored to property");
res = 0;
@@ -127,6 +148,7 @@ int restore_time() {
ALOGI("Failed to read from " RTC_SYS_FILE
" (%d), bailing out", res);
} else {
+ restore_ats(time_adjust);
tv.tv_sec = epoch_since + time_adjust;
tv.tv_usec = 0;
res = settimeofday(&tv, NULL);