diff options
| author | Humberto Borba <humberos@gmail.com> | 2016-07-06 18:58:40 -0300 |
|---|---|---|
| committer | Humberto Borba <humberos@gmail.com> | 2016-08-04 11:47:11 -0300 |
| commit | 04996642e2ffa21058ccdf91c8f47e0566a47c06 (patch) | |
| tree | 618b67cf6032a5e25eb8b7e30d0bbcc93a161c2c | |
| parent | abed5961b9b78445b43e418f4581a0b0c816d651 (diff) | |
timekeep: Store the adjusted value into ats_2 file
Store the adjusted value into /data/time/ats_2 file
TWRP will read this value from this path and fix the time
This patch requires some changes in ramdisk, selinux and twrp.
TWRP:
TARGET_RECOVERY_QCOM_RTC_FIX := true
reference: https://gerrit.omnirom.org/#/c/18410/
RAMDISK:
on boot
...
mkdir /data/time/ 0700 system system
reference: https://gerrit.omnirom.org/#/c/18411/
SELINUX:
file.te
type timekeep_data_file, file_type, data_file_type;
file_contexts
/data/time(/.*)? u:object_r:timekeep_data_file:s0
system_app.te
allow system_app timekeep_data_file:dir { create_dir_perms search };
allow system_app timekeep_data_file:file create_file_perms;
timekeep.te
allow timekeep self:capability {
sys_time
dac_override
dac_read_search
};
allow timekeep timekeep_data_file:file create_file_perms;
allow timekeep timekeep_data_file:dir { create_dir_perms search };
reference: https://gerrit.omnirom.org/#/c/18409/
Signed-off-by: Humberto Borba <humberos@gmail.com>
Change-Id: I52ff27c377b8a4663762e0c3f6c21d353217dced
| -rw-r--r-- | src/com/sony/timekeep/TimeKeep.java | 34 | ||||
| -rw-r--r-- | timekeep.c | 22 |
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); + } + } + } + } } @@ -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); |
