2020092403

Signed-off-by: guduhanyan <xuyanjun27@163.com>
This commit is contained in:
guduhanyan
2021-09-24 04:14:53 +08:00
parent 3b46654122
commit a54b57a380
12 changed files with 72 additions and 61 deletions
@@ -27,7 +27,7 @@ namespace OHOS {
namespace MiscServices {
constexpr int64_t ERROR_OPREATION_FAILED = -1;
class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient{
class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient {
public:
explicit TimeSaDeathRecipient();
~TimeSaDeathRecipient() = default;
@@ -126,7 +126,11 @@ static napi_value JSSystemTimeSetTimeZone(napi_env env,napi_callback_info info)
if (i == 0 && valueType == napi_string) {
char timeZoneChars[MAX_TIME_ZONE_ID];
size_t timeZoneCharsSize;
if (napi_ok != napi_get_value_string_utf8(env,argv[i],timeZoneChars,MAX_TIME_ZONE_ID-1,&timeZoneCharsSize)){
if (napi_ok != napi_get_value_string_utf8(env,
argv[i],
timeZoneChars,
MAX_TIME_ZONE_ID-1,
&timeZoneCharsSize)) {
delete asyncContext;
NAPI_ASSERT(env, false, "input para invalid");
}
@@ -189,7 +193,7 @@ static napi_value JSSystemTimeSetTimeZone(napi_env env,napi_callback_info info)
},
(void*)asyncContext,
&asyncContext->work);
napi_queue_async_work(env,asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
return result;
}
@@ -345,10 +345,10 @@ napi_value CreateTimer(napi_env env, napi_callback_info info)
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
std::shared_ptr<ITimerInfoInstance> iTimerInfoInstance = std::make_shared<ITimerInfoInstance>();
napi_ref callback = nullptr;
if (ParseParametersByCreateTimer(env, argv, argc, iTimerInfoInstance, callback) == nullptr){
if (ParseParametersByCreateTimer(env, argv, argc, iTimerInfoInstance, callback) == nullptr) {
return JSParaError(env, callback);
}
AsyncCallbackInfoCreate *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCreate{
AsyncCallbackInfoCreate *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCreate {
.env = env,
.asyncWork = nullptr,
.iTimerInfoInstance = iTimerInfoInstance
+41 -36
View File
@@ -19,16 +19,17 @@
#include <sys/time.h>
#include <cerrno>
#include <mutex>
#include <sys/ioctl.h>
#include <linux/rtc.h>
#include <dirent.h>
#include <cstring>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fstream>
#include <sstream>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>
#include "pthread.h"
#include "time_service.h"
#include "time_zone_info.h"
@@ -71,7 +72,7 @@ TimeService::TimeService(int32_t systemAbilityId, bool runOnCreate)
}
TimeService::TimeService()
:state_(ServiceRunningState::STATE_NOT_START),rtc_id(get_wall_clock_rtc_id())
:state_(ServiceRunningState::STATE_NOT_START), rtc_id(get_wall_clock_rtc_id())
{
}
@@ -79,9 +80,9 @@ TimeService::~TimeService() {};
sptr<TimeService> TimeService::GetInstance()
{
if (instance_ == nullptr){
if (instance_ == nullptr) {
std::lock_guard<std::mutex> autoLock(instanceLock_);
if (instance_ == nullptr){
if (instance_ == nullptr) {
instance_ = new TimeService;
}
}
@@ -173,13 +174,13 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T
bool isRealtime = false;
bool isWakeup = false;
paras.flag = 0;
if ((type & TIMER_TYPE_REALTIME_MASK) > 0 ){
if ((type & TIMER_TYPE_REALTIME_MASK) > 0 ) {
isRealtime = true;
}
if ((type & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0 ){
if ((type & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0) {
isWakeup = true;
}
if ((type & TIMER_TYPE_EXACT_MASK) > 0){
if ((type & TIMER_TYPE_EXACT_MASK) > 0) {
paras.windowLength = 0;
}else{
paras.windowLength = -1;
@@ -230,7 +231,11 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval,
}
}
return timerManagerHandler_->CreateTimer(paras.timerType,
paras.windowLength,paras.interval,paras.flag,callbackFunc,0);
paras.windowLength,
paras.interval,
paras.flag,
callbackFunc,
0);
}
bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes)
@@ -295,28 +300,28 @@ int32_t TimeService::SetTime(const int64_t time)
return E_TIME_NO_PERMISSION;
}
TIME_HILOGI(TIME_MODULE_SERVICE, "Setting time of day to milliseconds: %{public}" PRId64 "", time);
if (time < 0 || time / 1000LL >= LONG_MAX) {
if (time < 0 || time/1000LL >= LONG_MAX) {
TIME_HILOGE(TIME_MODULE_SERVICE, "input param error");
return E_TIME_PARAMETERS_INVALID;
}
struct timeval tv{};
tv.tv_sec = (time_t) (time / MILLI_TO_BASE);
tv.tv_usec = (suseconds_t) ((time % MILLI_TO_BASE) * MILLI_TO_MICR);
tv.tv_sec = (time_t) (time/MILLI_TO_BASE);
tv.tv_usec = (suseconds_t)((time % MILLI_TO_BASE) * MILLI_TO_MICR);
int result = settimeofday(&tv, NULL);
if (result < 0) {
if (result < 0){
TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.",result);
return E_TIME_DEAL_FAILED;
}
auto ret = set_rtc_time(tv.tv_sec);
if (ret < 0) {
if (ret < 0){
TIME_HILOGE(TIME_MODULE_SERVICE, "set rtc fail: %{public}d.", ret);
return E_TIME_SET_RTC_FAILED;
}
int64_t currentTime = 0;
GetWallTimeMs(currentTime);
if (timeServiceNotify_ != nullptr) {
if (timeServiceNotify_ != nullptr){
timeServiceNotify_->PublishTimeChanageEvents(currentTime);
}
@@ -324,12 +329,12 @@ int32_t TimeService::SetTime(const int64_t time)
}
int TimeService::set_rtc_time(time_t sec) {
struct rtc_time rtc {};
struct rtc_time rtc{};
struct tm tm {};
struct tm *gmtime_res = nullptr;
int fd = 0;
int res;
if (rtc_id < 0) {
if (rtc_id < 0){
TIME_HILOGE(TIME_MODULE_SERVICE, "invalid rtc id: %{public}s:", strerror(ENODEV));
return -1;
}
@@ -354,8 +359,8 @@ int TimeService::set_rtc_time(time_t sec) {
rtc.tm_wday = tm.tm_wday;
rtc.tm_yday = tm.tm_yday;
rtc.tm_isdst = tm.tm_isdst;
res = ioctl(fd, RTC_SET_TIME, &rtc);
if (res < 0) {
res = ioctl(fd, RTC_SET_TIME,&rtc);
if (res < 0){
TIME_HILOGE(TIME_MODULE_SERVICE, "ioctl RTC_SET_TIME failed: %{public}s", strerror(errno));
}
}else{
@@ -374,9 +379,9 @@ bool TimeService::check_rtc(std::string rtc_path, uint64_t rtc_id_t)
uint32_t hctosys;
std::fstream file(hctosys_path.data(), std::ios_base::in);
if (file.is_open()) {
if (file.is_open()){
file >> hctosys;
} else {
} else{
TIME_HILOGE(TIME_MODULE_SERVICE, "failed to open %{public}s", hctosys_path.data());
return false;
}
@@ -395,16 +400,16 @@ int TimeService::get_wall_clock_rtc_id()
struct dirent *dirent;
std::string s = "rtc";
while (errno = 0,
dirent = readdir(dir.get())) {
while (errno = 0,
dirent = readdir(dir.get())){
std::string name(dirent->d_name);
unsigned long rtc_id_t = 0 ;
auto index = name.find(s);
if (index == std::string::npos) {
unsigned long rtc_id_t = 0;
auto index = name.find(s);
if (index == std::string::npos){
continue;
} else {
auto rtc_id_str = name.substr(index+s.length());
auto rtc_id_str = name.substr(index + s.length());
rtc_id_t = std::stoul(rtc_id_str);
}
@@ -414,7 +419,7 @@ int TimeService::get_wall_clock_rtc_id()
}
}
if (errno == 0) {
if (errno == 0){
TIME_HILOGE(TIME_MODULE_SERVICE, "no wall clock rtc found");
}else{
TIME_HILOGE(TIME_MODULE_SERVICE, "failed to check rtc: %{public}s", strerror(errno));
@@ -456,7 +461,7 @@ int32_t TimeService::GetWallTimeMs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_REALTIME, &tv)) {
if (GetTimeByClockid(CLOCK_REALTIME, &tv)){
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
return ERR_OK;
}
@@ -468,7 +473,7 @@ int32_t TimeService::GetWallTimeNs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_REALTIME, &tv)) {
if (GetTimeByClockid(CLOCK_REALTIME, &tv)){
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
return ERR_OK;
}
@@ -480,7 +485,7 @@ int32_t TimeService::GetBootTimeMs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) {
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)){
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
return ERR_OK;
}
@@ -492,7 +497,7 @@ int32_t TimeService::GetBootTimeNs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) {
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)){
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
return ERR_OK;
}
@@ -504,7 +509,7 @@ int32_t TimeService::GetMonotonicTimeMs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) {
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)){
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
return ERR_OK;
}
@@ -516,7 +521,7 @@ int32_t TimeService::GetMonotonicTimeNs(int64_t &times)
{
struct timespec tv{};
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) {
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)){
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
return ERR_OK;
}
@@ -59,7 +59,8 @@ int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Mes
}
pid_t p = IPCSkeleton::GetCallingPid();
pid_t p1 = IPCSkeleton::GetCallingUid();
TIME_HILOGI(TIME_MODULE_SERVICE,"CallingPid = %{public}d,CallingUid = %{public}d,code = %{public}u",p,p1,code);
TIME_HILOGI(TIME_MODULE_SERVICE,"CallingPid = % {public}d,CallingUid = % {public}d,code = % {public}u",
p, p1, code);
auto itFunc = memberFuncMap_.find(code);
if (itFunc != memberFuncMap_.end()) {
auto memberFunc = itFunc->second;
+3 -3
View File
@@ -119,8 +119,8 @@ bool TimeZoneInfo::SetOffsetToKernel(float offsetHour)
struct timezone tz{};
tz.tz_minuteswest = static_cast<int>(offsetHour * HOURS_TO_MINUTES);
tz.tz_dsttime = 0;
TIME_HILOGD(TIME_MODULE_SERVICE,"settimeofday,Offset hours:%{public}f,Offset minutes:%{public}d",
offsetHour,tz.tz_minuteswest);
TIME_HILOGD(TIME_MODULE_SERVICE, "settimeofday, Offset hours % {public}f, Offset minutes % {public}d",
offsetHour, tz.tz_minuteswest);
int result = settimeofday(NULL, &tz);
if (result < 0) {
TIME_HILOGE(TIME_MODULE_SERVICE,"settimeofday fail:%{public}d.",result);
@@ -165,7 +165,7 @@ bool TimeZoneInfo::SaveTimezoneToFile(std::string timezoneId)
bool TimeZoneInfo::GetOffsetById(const std::string timezoneId, float &offset)
{
auto itEntry = timezoneInfoMap_.find(timezoneId);
if (itEntry != timezoneInfoMap_.end()){
if (itEntry != timezoneInfoMap_.end()) {
auto zoneInfo = itEntry->second;
offset = zoneInfo.utcOffsetHours;
curTimezoneId_ = timezoneId;
@@ -19,13 +19,13 @@
std::atomic<int> g_data1(0);
void TimeOutCallback1()
void TimeOutCallback1(void)
{
g_data1 += 1;
}
std::atomic<int> g_data2(0);
void TimeOutCallback2()
void TimeOutCallback2(void)
{
g_data2 += 1;
}
@@ -42,8 +42,8 @@ public:
virtual void SetWantAgent(std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> wantAgent) override;
void SetCallbackInfo(const std::function<void()> &callBack);
private:
std::function<void()> callBack_ = nullptr;
private:
std::function<void()> callBack_ = nullptr;
};
TimerInfoTest::TimerInfoTest()
+3 -3
View File
@@ -57,7 +57,7 @@ bool Batch::Add (const std::shared_ptr<TimerInfo> &alarm)
auto it = std::upper_bound(alarms_.begin(),
alarms_.end(),
alarm,
[](const std::shared_ptr<TimerInfo> &first, const std::shared_ptr<TimerInfo> &second){
[](const std::shared_ptr<TimerInfo> &first, const std::shared_ptr<TimerInfo> &second) {
return first->whenElapsed < second->whenElapsed;
});
alarms_.insert (it, alarm); // 根据Alarm.when_elapsed从小到大排列
@@ -117,8 +117,8 @@ bool Batch::Remove (std::function<bool (const TimerInfo &)> predicate)
bool Batch::HasPackage (const std::string &package_name)
{
return std::find_if(alarms_.begin(),
alarms_.end(),
[package_name](const std::shared_ptr<TimerInfo> &alarm){
alarms_.end(),
[package_name](const std::shared_ptr<TimerInfo> &alarm) {
return alarm->Matches(package_name);
}) != alarms_.end();
}
+3 -3
View File
@@ -95,7 +95,7 @@ TimerHandler::TimerHandler(const TimerFds &fds, int epollfd)
TimerHandler::~TimerHandler()
{
for (auto fd : fds_){
for (auto fd : fds_) {
epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr);
close(fd);
}
@@ -126,12 +126,12 @@ uint32_t TimerHandler::WaitForAlarm()
}
uint32_t result = 0;
for(int i = 0;i < nevents;i++){
for(int i = 0;i < nevents;i++) {
uint32_t alarm_idx = events[i].data.u32;
uint64_t unused;
ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused));
if (err < 0) {
if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED){
if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) {
result |= ALARM_TIME_CHANGE_MASK;
} else {
return err;
+5 -4
View File
@@ -343,7 +343,7 @@ void TimerManager::TimerLooper()
if (lastTimeChangeClockTime == system_clock::time_point::min()
|| nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND))
|| nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))){
|| nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))) {
TIME_HILOGI(TIME_MODULE_SERVICE, "Time changed notification from kernel; rebatching");
ReBatchAllTimers();
lastTimeChangeClockTime_ = nowRtc;
@@ -398,7 +398,7 @@ bool TimerManager::TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &
auto alarm = batch->Get(i);
alarm->count = 1;
triggerList.push_back(alarm);
if (alarm->repeatInterval > milliseconds::zero()){
if (alarm->repeatInterval > milliseconds::zero()) {
alarm->count += duration_cast<milliseconds>(nowElapsed-
alarm->expectedWhenElapsed) / alarm->repeatInterval;
auto delta = alarm->count * alarm->repeatInterval;
@@ -414,7 +414,7 @@ bool TimerManager::TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &
}
std::sort(triggerList.begin(),
triggerList.end(),
[](const std::shared_ptr<TimerInfo> &l, const std::shared_ptr<TimerInfo> &r){
[](const std::shared_ptr<TimerInfo> &l, const std::shared_ptr<TimerInfo> &r) {
return l->whenElapsed < r->whenElapsed;
});
@@ -516,7 +516,8 @@ bool AddBatchLocked(std::vector<std::shared_ptr<Batch>> &list, const std::shared
return it == list.begin();
}
steady_clock::time_point MaxTriggerTime(steady_clock::time_point now,steady_clock::time_point triggerAtTime,
steady_clock::time_point MaxTriggerTime(steady_clock::time_point now,
steady_clock::time_point triggerAtTime,
milliseconds interval)
{
milliseconds futurity = (interval == milliseconds::zero()) ?
+2 -2
View File
@@ -50,8 +50,8 @@ bool TimePermission::CheckCallingPermission(int32_t uid, std::string permName)
return true;
}
auto userId = uid / UID_TO_USERID;
TIME_HILOGI(TIME_MODULE_COMMON,"VerifyPermission bundleName:%{public}s,permission:%{public}s",
bundleName.c_str(),permName.c_str());
TIME_HILOGI(TIME_MODULE_COMMON,"VerifyPermission bundleName: % {public}s,permission: % {public}s",
bundleName.c_str(), permName.c_str());
return MockPermission::VerifyPermission(bundleName, permName, userId);
}