1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef FEATURE_GNSS_SUPPORT
17 #ifdef TIME_SERVICE_ENABLE
18
19 #include "gps_time_manager.h"
20
21 #include "elapsed_real_time_check.h"
22 #include "location_log.h"
23 #include "time_service_client.h"
24
25 namespace OHOS {
26 namespace Location {
27 const int64_t INVALID_TIME = 0L;
28 const int64_t MAX_MISSTAKE_TIME = 10 * 1000L;
29
GpsTimeManager()30 GpsTimeManager::GpsTimeManager()
31 {
32 timeManager_ = TimeManager(EXPIRT_TIME);
33 }
34
~GpsTimeManager()35 GpsTimeManager::~GpsTimeManager() {}
36
CheckValid(int64_t gpsTime,int64_t lastSystemTime)37 bool GpsTimeManager::CheckValid(int64_t gpsTime, int64_t lastSystemTime)
38 {
39 int64_t bootTimeDiff = gpsTime - timeManager_.GetTimestamp();
40 int64_t systemTimeDiff = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs() - lastSystemTime;
41 if (abs(bootTimeDiff - systemTimeDiff) > MAX_MISSTAKE_TIME) {
42 return false;
43 }
44 return true;
45 }
46
GetGpsTime()47 int64_t GpsTimeManager::GetGpsTime()
48 {
49 int64_t tmpTime = timeManager_.GetCurrentTime();
50 if ((!validFlag_) && (tmpTime > 0)) {
51 if (!CheckValid(tmpTime, lastSystemTime_)) {
52 return INVALID_TIME;
53 }
54 }
55 return tmpTime;
56 }
57
SetGpsTime(int64_t gpsMsTime,int64_t bootTimeMs)58 void GpsTimeManager::SetGpsTime(int64_t gpsMsTime, int64_t bootTimeMs)
59 {
60 validFlag_ = true;
61 lastSystemTime_ = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs();
62 timeManager_.SetCurrentTime(gpsMsTime, bootTimeMs);
63 auto elapsedRealTimeCheck = ElapsedRealTimeCheck::GetInstance();
64 if (elapsedRealTimeCheck != nullptr) {
65 elapsedRealTimeCheck->CheckRealTime(gpsMsTime);
66 }
67 }
68
69 } // namespace Location
70 } // namespace OHOS
71 #endif // TIME_SERVICE_ENABLE
72 #endif
73