mirror of
https://github.com/openharmony/miscservices_time.git
synced 2026-07-19 20:03:38 -04:00
@@ -1,4 +1,4 @@
|
||||
# Timing and Time<a name="EN-US_TOPIC_0000001115554184"></a>
|
||||
# Timing and Time<a name="EN-US_TOPIC_0000001115554184"></a>
|
||||
|
||||
- [Introduction](#section11660541593)
|
||||
- [Directory Structure](#section161941989596)
|
||||
@@ -28,6 +28,7 @@ The timing and time module provides APIs for managing the system time.
|
||||
└── services # Service implementation
|
||||
```
|
||||
|
||||
|
||||
## Usage<a name="section38521239153117"></a>
|
||||
|
||||
### Available JS APIs<a name="section11908203714422"></a>
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
"name": "time_native",
|
||||
"subsystem": "miscservices",
|
||||
"syscap": [
|
||||
"SystemCapability.MiscServices.Time"
|
||||
"SystemCapability.Miscservices.Time"
|
||||
],
|
||||
"features": [
|
||||
],
|
||||
|
||||
@@ -73,7 +73,7 @@ void TimeTickNotify::RefreshNextTriggerTime()
|
||||
struct tm *tblock = localtime(&t);
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "Time now: %{public}s", asctime(tblock));
|
||||
auto UTCTimeMicro = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Time Now Mirc: %{public}lld", UTCTimeMicro);
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "Time Now Mirc");
|
||||
auto timeMilliseconds = GetMillisecondsFromUTC(UTCTimeMicro);
|
||||
nextTriggerTime_ = MINUTE_TO_MILLISECOND - timeMilliseconds;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ ohos_unittest("TimeServiceTest") {
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/intent:intent",
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/want:want",
|
||||
"//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core",
|
||||
"//foundation/distributedschedule/dmsfwk/services/dtbschedmgr:distributedschedsvr",
|
||||
"//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk",
|
||||
"//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy",
|
||||
"//third_party/googletest:gtest_main",
|
||||
|
||||
@@ -182,6 +182,10 @@ void TimerManager::SetHandler(uint64_t id,
|
||||
|
||||
auto nowElapsed = steady_clock::now();
|
||||
auto nominalTrigger = ConvertToElapsed(milliseconds(triggerAtTime), type);
|
||||
if (nominalTrigger < nowElapsed) {
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "invalid trigger time end.");
|
||||
return;
|
||||
}
|
||||
auto minTrigger = (IsSystemUid(uid)) ? (nowElapsed + ZERO_FUTURITY) : (nowElapsed + MIN_FUTURITY);
|
||||
auto triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger;
|
||||
|
||||
@@ -302,6 +306,10 @@ void TimerManager::ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "start");
|
||||
timer->when = timer->origWhen;
|
||||
auto whenElapsed = ConvertToElapsed(timer->when, timer->type);
|
||||
if (whenElapsed < nowElapsed) {
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "invalid timer end.");
|
||||
return;
|
||||
}
|
||||
steady_clock::time_point maxElapsed;
|
||||
if (timer->windowLength == milliseconds::zero()) {
|
||||
maxElapsed = whenElapsed;
|
||||
@@ -319,10 +327,16 @@ std::chrono::steady_clock::time_point TimerManager::ConvertToElapsed(std::chrono
|
||||
{
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "start");
|
||||
if (type == RTC || type == RTC_WAKEUP) {
|
||||
auto offset = when - system_clock::now().time_since_epoch();
|
||||
auto systemTimeNow = system_clock::now().time_since_epoch();
|
||||
auto offset = when - systemTimeNow;
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "systemTimeNow : %{public}lld", systemTimeNow.count());
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "offset : %{public}lld", offset.count());
|
||||
return steady_clock::now() + offset;
|
||||
}
|
||||
auto offset = when - steady_clock::now().time_since_epoch();
|
||||
auto bootTimeNow = steady_clock::now().time_since_epoch();
|
||||
auto offset = when - bootTimeNow;
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "bootTimeNow : %{public}lld", bootTimeNow.count());
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "offset : %{public}lld", offset.count());
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "end");
|
||||
return steady_clock::now() + offset;
|
||||
}
|
||||
@@ -439,9 +453,17 @@ void TimerManager::RescheduleKernelTimerLocked()
|
||||
auto firstWakeup = FindFirstWakeupBatchLocked();
|
||||
auto firstBatch = alarmBatches_.front();
|
||||
if (firstWakeup != nullptr) {
|
||||
auto alarmPtr = firstWakeup->Get(0);
|
||||
if (alarmPtr != nullptr) {
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "wake up alarm id :%{public}" PRId64 "", alarmPtr->id);
|
||||
}
|
||||
SetLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup->GetStart().time_since_epoch());
|
||||
}
|
||||
if (firstBatch != firstWakeup) {
|
||||
auto alarmPtr = firstBatch->Get(0);
|
||||
if (alarmPtr != nullptr) {
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "nonwakeup alarm id :%{public}" PRId64 "", alarmPtr->id);
|
||||
}
|
||||
nextNonWakeup = firstBatch->GetStart();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace OHOS {
|
||||
namespace MiscServices {
|
||||
namespace {
|
||||
constexpr int32_t SYSTEM_UID = 1000;
|
||||
constexpr int32_t TEST_UID = 0;
|
||||
constexpr int32_t ROOT_UID = 0;
|
||||
constexpr int32_t MIN_SYSTEM_UID = 2100;
|
||||
constexpr int32_t MAX_SYSTEM_UID = 2899;
|
||||
}
|
||||
@@ -37,8 +37,8 @@ bool TimePermission::CheckSelfPermission(std::string permName)
|
||||
|
||||
bool TimePermission::CheckCallingPermission(int32_t uid, std::string permName)
|
||||
{
|
||||
if ((uid == SYSTEM_UID) || (uid == TEST_UID)) {
|
||||
TIME_HILOGD(TIME_MODULE_COMMON, "system uid return true");
|
||||
if ((uid == SYSTEM_UID) || (uid == ROOT_UID)) {
|
||||
TIME_HILOGD(TIME_MODULE_COMMON, "root uid return true");
|
||||
return true;
|
||||
}
|
||||
if (IsSystemUid(uid)) {
|
||||
@@ -46,6 +46,12 @@ bool TimePermission::CheckCallingPermission(int32_t uid, std::string permName)
|
||||
return true;
|
||||
}
|
||||
auto callingToken = IPCSkeleton::GetCallingTokenID();
|
||||
|
||||
auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callingToken);
|
||||
if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
|
||||
TIME_HILOGD(TIME_MODULE_COMMON, "native token.");
|
||||
return true;
|
||||
}
|
||||
auto result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callingToken, permName);
|
||||
if (result == Security::AccessToken::TypePermissionState::PERMISSION_DENIED) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user