本文主要是介绍Android: sensor 时间戳从sensor子系统到AP android层的变化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
因为_offset_ns的存在,两个时间体系的转换没有那么简单,不知offset是不是个常量,还是每次开机都会变化?开机也有可能变化?
结论:如果便于分析问题,可以直接更改该函数,是AP和SEE的 event时间戳一致,该方法已验证
create_sensor_hal_event(pb_event.timestamp());
明确pb_event.timestamp得到是qtime tick值,qtimer_ticks_to_elapsedRealtimeNano(ssc_timestamp) 把ssc_timestamp(qtimer tick)转换为对应的AP从开机到ssc_timestamp的时长 elapsedRealtime, 不是UTC时间。
sensor_timeutil构造函数
看明白sensors_timeutil构造函数,其中android::elapsedRealtimeNano 调用了clock_gettime(CLOCK_BOOTTIME, &ts)查找函数clock_gettime的说明,就大概明白了sensor_timeuitl的大概功能了。
sensors-see/sensors-log/sensors_timeutil.h
sensors_timeutil() :_qtimer_freq(qtimer_get_freq()),_last_realtime_ns(android::elapsedRealtimeNano()),_offset_ns(_last_realtime_ns - int64_t(qtimer_get_time_ns()))
{
}
void har::handle_sns_client_event(const sns_client_event_msg_sns_client_event& pb_event)
{
if (pb_event.msg_id() == SNS_HAR_MSGID_SNS_DATA) {
sns_std_sensor_event pb_stream_event;
pb_stream_event.ParseFromString(pb_event.payload());
sensors_event_t hal_event = create_sensor_hal_event(pb_event.timestamp());
hal_event.data[0] = pb_stream_event.data(0);
hal_event.data[1] = pb_stream_event.data(1);
submit_sensors_hal_event(hal_event);
sns_loge("har-sensor-hal: time=%lld data %f " , (long long)(hal_event.timestamp), hal_event.data[0]);
}
}
typedef struct _sns_client_event_msg_sns_client_event {
uint32_t msg_id;
uint64_t timestamp;
pb_callback_t payload;
/* @@protoc_insertion_point(struct:sns_client_event_msg_sns_client_event) */
} sns_client_event_msg_sns_client_event;
sensors_event_t ssc_sensor::create_sensor_hal_event(uint64_t ssc_timestamp)
{
sensors_event_t hal_event;
//Fix for static analysis error - uninitialized variable
memset(&hal_event, 0x00, sizeof(sensors_event_t));
hal_event.version = sizeof(sensors_event_t);
hal_event.sensor = get_sensor_info().handle;
hal_event.type = get_sensor_info().type;
hal_event.timestamp = sensors_timeutil::get_instance().
qtimer_ticks_to_elapsedRealtimeNano(ssc_timestamp);
sns_logv("ssc_ts = %llu tk, hal_ts = %lld ns, %lf",
(unsigned long long)ssc_timestamp,
(long long)hal_event.timestamp, hal_event.timestamp / 1000000000.0 );
return hal_event;
}
1 纳秒 = 1000皮秒
1,000 纳秒 = 1微秒 μs
1,000,000 纳秒 = 1毫秒 ms
1,000,000,000 纳秒 = 1秒 s
有关linux time的参考
https://zhuanlan.zhihu.com/p/138378298
https://zhuanlan.zhihu.com/p/20313015
https://blog.csdn.net/occupy8/article/details/48010811
https://www.cnblogs.com/arnoldlu/p/6681915.html
不同系统间的同步如AP和DSP
A表示UTC的开始时刻1970年..., 图中UTC time表示A、C间的 utc时间
B表示DSP开始计时(有tick)的时刻, DSP time表示B、C间的时长,可以通过dsp简单的函数sns_get_system_time()得到。
假设C时刻对应的UTC time发送到DSP后,DSP怎样得到和UTC时间?
把这个问题转换为数据问题,上图中已知的UTC time 用 utc表示,BC表示对应的 要得到的D时刻对应的UTC Time用 d_utc表示
utc = AB + BC (1)
d_utc = utc +CD (2)
1、2 两式左右两边分别相加得到
utc + d_utc = AB +BC + utc +CD 左右两边分别减去utc得到
d_utc = AB + BC +CD
d_utc = (utc - dsp) + (BC+CD)
其中 utc -dsp 是个固定的值,可以记为offset, BC+CD是变化通过DSP系统相关的函数如sns_get_system_time()获得当前时间
最终 d_utc = (utc - dsp) + (BC+CD)变成了 d_utc = offset + sns_get_system_time(d), 得到d时刻对应的 utc时间。
UTC时间是否和时区有关
UNIX时间戳的定义与时区无关。时间戳是自1970年1月1日午夜(UTC时间)的绝对时间点以来经过的秒数(或毫秒)。 无论您的时区如何,时间戳都代表着无处不在的时刻。
怎样把ap startup offset time改成UTC时间
增加utc时间currentUTCNano() 类似elapsedRealtimeNano
diff --git a/libutils/SystemClock.cpp b/libutils/SystemClock.cpp
index 73ec1be96..d5eb3a288 100644
--- a/libutils/SystemClock.cpp
+++ b/libutils/SystemClock.cpp
@@ -71,4 +71,20 @@ int64_t elapsedRealtimeNano()
#endif
}
+/*
+ * native public static long currentUTCNano();
+ */
+int64_t currentUTCNano()
+{
+ struct timespec ts;
+ int err = clock_gettime(CLOCK_REALTIME, &ts);
+ if (CC_UNLIKELY(err)) {
+ // This should never happen, but just in case ...
+ ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno));
+ return 0;
+ }
+
+ return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
+}
+
}; // namespace android
diff --git a/libutils/include/utils/SystemClock.h b/libutils/include/utils/SystemClock.h
index 01db34078..8656c0805 100644
--- a/libutils/include/utils/SystemClock.h
+++ b/libutils/include/utils/SystemClock.h
@@ -25,7 +25,7 @@ namespace android {
int64_t uptimeMillis();
int64_t elapsedRealtime();
int64_t elapsedRealtimeNano();
-
+int64_t currentUTCNano();
sensors-log/sensors_timeutil.h
--- a/common-8x09/proprietary/commonsys-intf/sensors-see/sensors-log/sensors_timeutil.h
+++ b/common-8x09/proprietary/commonsys-intf/sensors-see/sensors-log/sensors_timeutil.h
@@ -139,6 +139,12 @@ public:
{
return qtimer_ns_to_elapsedRealtimeNano(qtimer_ticks_to_ns(qtimer_ticks));
}
+
+ int64_t qtimer_ticks_to_utcNano(uint64_t qtimer_ticks)
+ {
+ return (_last_utctime_ns - _last_realtime_ns) + qtimer_ticks_to_elapsedRealtimeNano(qtimer_ticks);
+ }
+
/**
* @brief get offset between sensor time system and Android time system (in ns)
* @return int64_t
@@ -195,6 +201,7 @@ private:
} while ((ns_end - ns > QTIMER_GAP_THRESHOLD_NS) &&
(iter < QTIMER_GAP_MAX_ITERATION));
_last_realtime_ns = android_ts;
+ _last_utctime_ns = android::currentUTCNano();
sns_logd("updating qtimer-realtime offset, offset_diff = %" PRId64 \
" time_diff = %" PRId64, _offset_ns - old_offset_ns,
android_ts - last_android_ts);
@@ -206,6 +213,7 @@ private:
sensors_timeutil() :
_qtimer_freq(qtimer_get_freq()),
_last_realtime_ns(android::elapsedRealtimeNano()),
+ _last_utctime_ns(android::currentUTCNano()),
_offset_ns(_last_realtime_ns - int64_t(qtimer_get_time_ns()))
{
}
@@ -230,6 +238,13 @@ private:
#else
std::atomic<std::int64_t> _last_realtime_ns;
#endif
+
+ /* the last realtime timestamp converted or passed */
+#ifndef SNS_LE_QCS605
+ std::atomic<int64_t> _last_utctime_ns;
+#else
+ std::atomic<std::int64_t> _last_utctime_ns;
+#endif
framework/ssc_sensor.cpp
diff --git a/common-8x09/proprietary/sensors-see/sensors-hal/framework/ssc_sensor.cpp b/common-8x09/proprietary/sensors-see/sensors-ha
l/framework/ssc_sensor.cpp
index d7bf7bba..42b11782 100644
--- a/common-8x09/proprietary/sensors-see/sensors-hal/framework/ssc_sensor.cpp
+++ b/common-8x09/proprietary/sensors-see/sensors-hal/framework/ssc_sensor.cpp
@@ -535,7 +535,7 @@ sns_client_request_msg ssc_sensor::create_sensor_config_request()
pb_req_msg.set_msg_id(SNS_STD_SENSOR_MSGID_SNS_STD_SENSOR_CONFIG);
pb_req_msg.mutable_request()->set_payload(pb_stream_cfg_encoded);
} else {
- sns_logd("sending SNS_STD_SENSOR_MSGID_SNS_STD_ON_CHANGE_CONFIG");
+ sns_loge("sending SNS_STD_SENSOR_MSGID_SNS_STD_ON_CHANGE_CONFIG");
pb_req_msg.set_msg_id(SNS_STD_SENSOR_MSGID_SNS_STD_ON_CHANGE_CONFIG);
pb_req_msg.mutable_request()->clear_payload();
}
@@ -615,8 +615,9 @@ sensors_event_t ssc_sensor::create_sensor_hal_event(uint64_t ssc_timestamp)
hal_event.version = sizeof(sensors_event_t);
hal_event.sensor = get_sensor_info().handle;
hal_event.type = get_sensor_info().type;
+ //hal_event.timestamp = ssc_timestamp;
hal_event.timestamp = sensors_timeutil::get_instance().
- qtimer_ticks_to_elapsedRealtimeNano(ssc_timestamp);
+ qtimer_ticks_to_utcNano(ssc_timestamp) / 1000000;
sns_logv("ssc_ts = %llu tk, hal_ts = %lld ns, %lf",
(unsigned long long)ssc_timestamp,
这篇关于Android: sensor 时间戳从sensor子系统到AP android层的变化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!