diff --git a/bundle.json b/bundle.json index 8287c1b6..6aafbee6 100644 --- a/bundle.json +++ b/bundle.json @@ -76,7 +76,8 @@ "image_framework", "movement", "device_standby", - "time_service" + "time_service", + "netmanager_base" ] }, "build": { diff --git a/config.gni b/config.gni index 0e2d5d78..3da07a09 100644 --- a/config.gni +++ b/config.gni @@ -59,6 +59,7 @@ declare_args() { time_service_enable = true movement_client_enable = true location_device_standby_enable = true + net_manager_enable = true if (defined(global_parts_info) && !defined(global_parts_info.global_i18n)) { i18n_enable = false @@ -152,4 +153,9 @@ declare_args() { !defined(global_parts_info.time_time_service)) { time_service_enable = false } + + if (defined(global_parts_info) && + !defined(global_parts_info.communication_netmanager_base)) { + net_manager_enable = false + } } diff --git a/services/etc/init/locationsa.cfg b/services/etc/init/locationsa.cfg index 4c797537..be646775 100644 --- a/services/etc/init/locationsa.cfg +++ b/services/etc/init/locationsa.cfg @@ -32,7 +32,8 @@ "ohos.permission.ACCESS_PROTOCOL_DFX_STATE", "ohos.permission.MANAGE_SETTINGS", "ohos.permission.ACTIVITY_MOTION", - "ohos.permission.INTERNET" + "ohos.permission.INTERNET", + "ohos.permission.GET_NETWORK_INFO" ], "permission_acls" : ["ohos.permission.GET_SENSITIVE_PERMISSIONS"], "critical" : [0, 5, 10], diff --git a/services/location_gnss/gnss/BUILD.gn b/services/location_gnss/gnss/BUILD.gn index b947f3c1..940fe20d 100644 --- a/services/location_gnss/gnss/BUILD.gn +++ b/services/location_gnss/gnss/BUILD.gn @@ -23,6 +23,12 @@ if (location_feature_with_gnss) { "$LOCATION_GNSS_ROOT/source/gnss_ability_skeleton.cpp", "$LOCATION_GNSS_ROOT/source/gnss_common_event_subscriber.cpp", "$LOCATION_GNSS_ROOT/source/gnss_event_callback.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/elapsed_real_time_check.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/gps_time_manager.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/net_conn_observer.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/ntp_time_check.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/ntp_time_helper.cpp", + "$LOCATION_GNSS_ROOT/source/ntp/time_manager.cpp", "$LOCATION_GNSS_ROOT/source/string_utils.cpp", "$LOCATION_LOCATOR_ROOT/source/location_config_manager.cpp", "$LOCATION_LOCATOR_ROOT/source/subability_common.cpp", @@ -36,6 +42,7 @@ if (location_feature_with_gnss) { include_dirs = [ "$LOCATION_ROOT_DIR/interfaces/inner_api/include", "$LOCATION_GNSS_ROOT/include", + "$LOCATION_GNSS_ROOT/include/ntp", "$LOCATION_LOCATOR_ROOT/include", ] @@ -143,6 +150,11 @@ if (location_feature_with_gnss) { defines += [ "TIME_SERVICE_ENABLE" ] } + if (net_manager_enable) { + external_deps += [ "netmanager_base:net_conn_manager_if" ] + defines += [ "NET_MANAGER_ENABLE" ] + } + # Used to control the export of dynamic library symbols. version_script = "liblbsservice_gnss_version_script.txt" @@ -156,6 +168,7 @@ if (location_feature_with_gnss) { include_dirs = [ "$LOCATION_ROOT_DIR/interfaces/inner_api/include", "$LOCATION_GNSS_ROOT/include", + "$LOCATION_GNSS_ROOT/include/ntp", "$LOCATION_LOCATOR_ROOT/include", ] @@ -251,6 +264,11 @@ if (location_feature_with_gnss) { defines += [ "TIME_SERVICE_ENABLE" ] } + if (net_manager_enable) { + external_deps += [ "netmanager_base:net_conn_manager_if" ] + defines += [ "NET_MANAGER_ENABLE" ] + } + part_name = "location" subsystem_name = "location" } diff --git a/services/location_gnss/gnss/include/gnss_ability.h b/services/location_gnss/gnss/include/gnss_ability.h index 7ed162f9..008b569b 100644 --- a/services/location_gnss/gnss/include/gnss_ability.h +++ b/services/location_gnss/gnss/include/gnss_ability.h @@ -45,6 +45,10 @@ #include "geofence_event_callback.h" #include "ipc_skeleton.h" +#ifdef TIME_SERVICE_ENABLE +#include "time_manager.h" +#endif + namespace OHOS { namespace Location { #ifdef __aarch64__ @@ -213,6 +217,8 @@ public: LocationErrCode SendNetworkLocation(const std::unique_ptr& location) override; LocationErrCode InjectLocation(); LocationErrCode InjectTime(); + LocationErrCode UpdateNtpTime(int64_t ntpTime, int64_t elapsedTime); + void MonitorNetwork(); private: bool Init(); @@ -253,6 +259,9 @@ private: sptr gnssInterface_; sptr gnssCallback_; Location nlpLocation_; +#ifdef TIME_SERVICE_ENABLE + TimeManager ntpTime_; +#endif #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE sptr agnssCallback_; sptr agnssInterface_; diff --git a/services/location_gnss/gnss/include/ntp/elapsed_real_time_check.h b/services/location_gnss/gnss/include/ntp/elapsed_real_time_check.h new file mode 100644 index 00000000..f7591b98 --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/elapsed_real_time_check.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ELAPSED_REAL_TIME_CHECK_H +#define ELAPSED_REAL_TIME_CHECK_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include + +namespace OHOS { +namespace Location { +class ElapsedRealTimeCheck { +public: + static ElapsedRealTimeCheck* GetInstance(); + void CheckRealTime(int64_t time); + bool CanTrustElapsedRealTime(); + +private: + ElapsedRealTimeCheck(); + ~ElapsedRealTimeCheck(); + + int64_t timeBegin_; + int64_t timeBeginElapsed_; + int64_t timeCheck_; + int64_t timeCheckElapsed_; + bool canTrustElapsedRealTime_ = true; // default is true, usually we trust this time. +}; +} // namespace Location +} // namespace OHOS + +#endif // TIME_SERVICE_ENABLE +#endif // FEATURE_GNSS_SUPPORT +#endif // ELAPSED_REAL_TIME_CHECK_H diff --git a/services/location_gnss/gnss/include/ntp/gps_time_manager.h b/services/location_gnss/gnss/include/ntp/gps_time_manager.h new file mode 100644 index 00000000..ebcc0f8b --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/gps_time_manager.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GPS_TIME_MANAGER_H +#define GPS_TIME_MANAGER_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include "time_manager.h" + +namespace OHOS { +namespace Location { +class GpsTimeManager { +public: + GpsTimeManager(); + ~GpsTimeManager(); + int64_t GetGpsTime(); + void SetGpsTime(int64_t gpsTime, int64_t bootTimeMs); + +private: + bool CheckValid(int64_t gpsTime, int64_t lastSystemTime); + TimeManager timeManager_; + bool validFlag_ {false}; + int64_t lastSystemTime_; +}; +} // namespace Location +} // namespace OHOS + +#endif // TIME_SERVICE_ENABLE +#endif // FEATURE_GNSS_SUPPORT +#endif // GPS_TIME_MANAGER_H diff --git a/services/location_gnss/gnss/include/ntp/net_conn_observer.h b/services/location_gnss/gnss/include/ntp/net_conn_observer.h new file mode 100644 index 00000000..81bd53b8 --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/net_conn_observer.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H +#define NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef NET_MANAGER_ENABLE + +#include "net_all_capabilities.h" +#include "net_conn_callback_stub.h" + +namespace OHOS { +namespace Location { +class NetConnObserver : public NetManagerStandard::NetConnCallbackStub { +public: + int32_t NetAvailable(sptr &netHandle) override; + + int32_t NetCapabilitiesChange(sptr &netHandle, + const sptr &netAllCap) override; + + int32_t NetConnectionPropertiesChange(sptr &netHandle, + const sptr &info) override; + + int32_t NetLost(sptr &netHandle) override; + + int32_t NetUnavailable() override; + + int32_t NetBlockStatusChange(sptr &netHandle, bool blocked) override; +}; +} // namespace Location +} // namespace OHOS + +#endif +#endif // NET_MANAGER_ENABLE +#endif // NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H diff --git a/services/location_gnss/gnss/include/ntp/ntp_time_check.h b/services/location_gnss/gnss/include/ntp/ntp_time_check.h new file mode 100644 index 00000000..85ac8ea0 --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/ntp_time_check.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GPS_NTP_TIME_CHECK_H +#define GPS_NTP_TIME_CHECK_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include "gps_time_manager.h" + +namespace OHOS { +namespace Location { +const int64_t DEFAULT_UNCERTAINTY = 30; +class NtpTimeCheck { +public: + static NtpTimeCheck* GetInstance(); + bool CheckNtpTime(int64_t ntpMsTime, int64_t msTimeSynsBoot); + int GetUncertainty(); + void SetGpsTime(int64_t gpsMsTime, int64_t bootTimeMs); + +private: + NtpTimeCheck(); + ~NtpTimeCheck(); + bool CompareTime(int64_t currentNtpTime, int64_t compareTime); + GpsTimeManager gpsTimeManager_; + int difference_ = DEFAULT_UNCERTAINTY; +}; +} // namespace Location +} // namespace OHOS + +#endif // TIME_SERVICE_ENABLE +#endif // FEATURE_GNSS_SUPPORT +#endif // GPS_NTP_TIME_CHECK_H diff --git a/services/location_gnss/gnss/include/ntp/ntp_time_helper.h b/services/location_gnss/gnss/include/ntp/ntp_time_helper.h new file mode 100644 index 00000000..96d27494 --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/ntp_time_helper.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NTP_TIME_HELPER_H +#define NTP_TIME_HELPER_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include +#include "time_manager.h" + +namespace OHOS { +namespace Location { + +struct NtpTrustedTime { + int64_t ntpTime = 0; + int64_t ageMillis = 0; + int64_t elapsedRealTime = 0; +}; + +class NtpTimeHelper { +public: + static NtpTimeHelper* GetInstance(); + void RetrieveAndInjectNtpTime(); + +private: + NtpTimeHelper() = default; + ~NtpTimeHelper() = default; + + NtpTrustedTime ntpResult_; +}; +} // namespace Location +} // namespace OHOS + +#endif // TIME_SERVICE_ENABLE +#endif // FEATURE_GNSS_SUPPORT +#endif // NTP_TIME_HELPER_H diff --git a/services/location_gnss/gnss/include/ntp/time_manager.h b/services/location_gnss/gnss/include/ntp/time_manager.h new file mode 100644 index 00000000..21db8450 --- /dev/null +++ b/services/location_gnss/gnss/include/ntp/time_manager.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TIME_MANAGER_H +#define TIME_MANAGER_H +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include + +namespace OHOS { +namespace Location { +const int64_t EXPIRT_TIME = 14 * 24 * 60 * 60 * 1000L; // 14 day, two week + +class TimeManager { +public: + TimeManager() = default; + explicit TimeManager(int64_t expireTime) : expireTime_(expireTime) {} + ~TimeManager() = default; + int64_t GetCurrentTime(); + void SetCurrentTime(int64_t msTime, int64_t msTimeSynsBoot); + int64_t GetTimestamp(); + +private: + int64_t timestamp_ = 0; + int64_t timeSynsBoot_ = 0; + int64_t expireTime_ = EXPIRT_TIME; +}; +} // namespace Location +} // namespace OHOS + +#endif // TIME_SERVICE_ENABLE +#endif // FEATURE_GNSS_SUPPORT +#endif // TIME_MANAGER_H diff --git a/services/location_gnss/gnss/source/gnss_ability.cpp b/services/location_gnss/gnss/source/gnss_ability.cpp index 84c851b6..86f059d8 100644 --- a/services/location_gnss/gnss/source/gnss_ability.cpp +++ b/services/location_gnss/gnss/source/gnss_ability.cpp @@ -52,6 +52,13 @@ #ifdef TIME_SERVICE_ENABLE #include "time_service_client.h" +#include "ntp_time_check.h" +#endif + +#ifdef NET_MANAGER_ENABLE +#include "net_conn_observer.h" +#include "net_conn_client.h" +#include "net_specifier.h" #endif namespace OHOS { @@ -70,10 +77,8 @@ constexpr const char *GEOFENCE_SERVICE_NAME = "geofence_interface_service"; constexpr const char *UNLOAD_GNSS_TASK = "gnss_sa_unload"; const uint32_t RETRY_INTERVAL_OF_UNLOAD_SA = 4 * 60 * EVENT_INTERVAL_UNITE; constexpr int32_t FENCE_MAX_ID = 1000000; -#ifdef TIME_SERVICE_ENABLE -constexpr int DEFAULT_UNCERTAINTY = 30; -#endif constexpr int NLP_FIX_VALID_TIME = 2; +const int64_t INVALID_TIME = 0; } const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility( @@ -106,6 +111,7 @@ GnssAbility::GnssAbility() : SystemAbility(LOCATION_GNSS_SA_ID, true) if (agnssNiManager != nullptr) { agnssNiManager->SubscribeSaStatusChangeListerner(); } + MonitorNetwork(); LBSLOGI(GNSS, "ability constructed."); } @@ -496,26 +502,68 @@ LocationErrCode GnssAbility::SetPositionMode() return ERRCODE_SUCCESS; } +void GnssAbility::MonitorNetwork() +{ +#ifdef NET_MANAGER_ENABLE + NetManagerStandard::NetSpecifier netSpecifier; + NetManagerStandard::NetAllCapabilities netAllCapabilities; + netAllCapabilities.netCaps_.insert(NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET); + netSpecifier.netCapabilities_ = netAllCapabilities; + sptr specifier = + new (std::nothrow) NetManagerStandard::NetSpecifier(netSpecifier); + if (specifier == nullptr) { + LBSLOGE(GNSS, "new operator error.specifier is nullptr"); + return; + } + sptr observer = new (std::nothrow) NetConnObserver(); + if (observer == nullptr) { + LBSLOGE(GNSS, "new operator error.observer is nullptr"); + return; + } + int ret = NetManagerStandard::NetConnClient::GetInstance().RegisterNetConnCallback(specifier, observer, 0); + LBSLOGI(GNSS, "RegisterNetConnCallback retcode= %{public}d", ret); +#endif + return; +} + LocationErrCode GnssAbility::InjectTime() { #ifdef TIME_SERVICE_ENABLE - if (gnssInterface_ == nullptr) { - LBSLOGE(GNSS, "gnssInterface_ is nullptr"); + LBSLOGD(GNSS, "InjectTime"); + int64_t currentTime = ntpTime_.GetCurrentTime(); + if (currentTime == INVALID_TIME) { return ERRCODE_SERVICE_UNAVAILABLE; } - - GnssRefInfo refInfo; - refInfo.type = GnssRefInfoType::GNSS_REF_INFO_TIME; - auto wallTime = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs(); auto elapsedTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs(); - if (wallTime < 0 || elapsedTime < 0) { - LBSLOGE(GNSS, "get time failed"); + if (elapsedTime < 0) { + LBSLOGE(GNSS, "get boot time failed"); return ERRCODE_SERVICE_UNAVAILABLE; } - refInfo.time.time = wallTime; - refInfo.time.elapsedRealtime = elapsedTime; - refInfo.time.uncertaintyOfTime = DEFAULT_UNCERTAINTY; - gnssInterface_->SetGnssReferenceInfo(refInfo); + auto ntpTimeCheck = NtpTimeCheck::GetInstance(); + if (ntpTimeCheck != nullptr && ntpTimeCheck->CheckNtpTime(currentTime, elapsedTime)) { + GnssRefInfo refInfo; + refInfo.type = GnssRefInfoType::GNSS_REF_INFO_TIME; + refInfo.time.time = currentTime; + refInfo.time.elapsedRealtime = elapsedTime; + refInfo.time.uncertaintyOfTime = ntpTimeCheck->GetUncertainty(); + if (gnssInterface_ != nullptr) { + LBSLOGI(GNSS, "inject ntp time: %{public}s unert %{public}d", + std::to_string(currentTime).c_str(), ntpTimeCheck->GetUncertainty()); + gnssInterface_->SetGnssReferenceInfo(refInfo); + } + } +#endif + return ERRCODE_SUCCESS; +} + +LocationErrCode GnssAbility::UpdateNtpTime(int64_t ntpTime, int64_t elapsedTime) +{ +#ifdef TIME_SERVICE_ENABLE + if (ntpTime <= 0 || elapsedTime <= 0) { + LBSLOGE(GNSS, "failed to UpdateNtpTime"); + return ERRCODE_SERVICE_UNAVAILABLE; + } + ntpTime_.SetCurrentTime(ntpTime, elapsedTime); #endif return ERRCODE_SUCCESS; } @@ -548,7 +596,9 @@ LocationErrCode GnssAbility::InjectLocation() GnssRefInfo refInfo; refInfo.type = GnssRefInfoType::GNSS_REF_INFO_LOCATION; refInfo.gnssLocation.fieldValidity = - GnssLocationValidity::GNSS_LOCATION_LAT_VALID | GnssLocationValidity::GNSS_LOCATION_LONG_VALID; + GnssLocationValidity::GNSS_LOCATION_LAT_VALID | + GnssLocationValidity::GNSS_LOCATION_LONG_VALID | + GnssLocationValidity::GNSS_LOCATION_HORIZONTAL_ACCURACY_VALID; refInfo.gnssLocation.latitude = nlpLocation_.GetLatitude(); refInfo.gnssLocation.longitude = nlpLocation_.GetLongitude(); refInfo.gnssLocation.altitude = nlpLocation_.GetAltitude(); diff --git a/services/location_gnss/gnss/source/gnss_event_callback.cpp b/services/location_gnss/gnss/source/gnss_event_callback.cpp index 2cd92ca7..fe0f7ba2 100644 --- a/services/location_gnss/gnss/source/gnss_event_callback.cpp +++ b/services/location_gnss/gnss/source/gnss_event_callback.cpp @@ -25,6 +25,10 @@ #include "common_hisysevent.h" #include "agnss_ni_manager.h" +#ifdef TIME_SERVICE_ENABLE +#include "ntp_time_check.h" +#include "time_service_client.h" +#endif namespace OHOS { namespace Location { using namespace OHOS::HiviewDFX; @@ -37,6 +41,17 @@ bool g_hasLocation = false; bool g_svIncrease = false; std::unique_ptr g_svInfo = nullptr; +static void SetGpsTime(int64_t gpsTime) +{ +#ifdef TIME_SERVICE_ENABLE + auto ntpTimeCheck = NtpTimeCheck::GetInstance(); + if (ntpTimeCheck != nullptr) { + auto elapsedRealTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs(); + ntpTimeCheck->SetGpsTime(gpsTime, elapsedRealTime); + } +#endif +} + int32_t GnssEventCallback::ReportLocation(const LocationInfo& location) { auto gnssAbility = GnssAbility::GetInstance(); @@ -80,6 +95,7 @@ int32_t GnssEventCallback::ReportLocation(const LocationInfo& location) #ifdef FEATURE_PASSIVE_SUPPORT gnssAbility->ReportLocationInfo(PASSIVE_ABILITY, locationNew); #endif + SetGpsTime(locationNew->GetTimeStamp()); IPCSkeleton::SetCallingIdentity(identity); return ERR_OK; } diff --git a/services/location_gnss/gnss/source/ntp/elapsed_real_time_check.cpp b/services/location_gnss/gnss/source/ntp/elapsed_real_time_check.cpp new file mode 100644 index 00000000..b28d38fc --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/elapsed_real_time_check.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE +#include "elapsed_real_time_check.h" + +#include +#include + +#include "common_utils.h" +#include "location_log.h" +#include "time_service_client.h" + +namespace OHOS { +namespace Location { +const int64_t MAX_MISS_MS = 24 * 1000; // we only allow 24s misstake + +ElapsedRealTimeCheck *ElapsedRealTimeCheck::GetInstance() +{ + static ElapsedRealTimeCheck data; + return &data; +} + +ElapsedRealTimeCheck::ElapsedRealTimeCheck() {} + +ElapsedRealTimeCheck::~ElapsedRealTimeCheck() {} + +void ElapsedRealTimeCheck::CheckRealTime(int64_t time) +{ + if (timeBegin_ == 0) { + timeBegin_ = time; + timeBeginElapsed_ = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs(); + } else if (timeCheck_ == 0) { + timeCheck_ = time; + timeCheckElapsed_ = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs(); + } else { + LBSLOGD(GNSS, "checkRealTime"); + } + + if ((timeBegin_ != 0) && (timeCheck_ != 0)) { + int64_t missTime = (timeCheck_ - timeBegin_) - (timeCheckElapsed_ - timeBeginElapsed_); + LBSLOGD(GNSS, + "checkRealTime timeBegin_:%{public}s, timeCheck_:%{public}s, timeBeginElapsed_:%{public}s, " + "timeCheckElapsed_:%{public}s", + std::to_string(timeBegin_).c_str(), std::to_string(timeCheck_).c_str(), + std::to_string(timeBeginElapsed_).c_str(), std::to_string(timeCheckElapsed_).c_str()); + LBSLOGI(GNSS, "checkRealTime missTime:%{public}s", std::to_string(missTime).c_str()); + // we found missTime usually should not grow, results should be simillar on any moment. + // so if very high missTime should be problem. + if (std::abs(missTime) >= MAX_MISS_MS) { + LBSLOGI(GNSS, "checkRealTime false"); + canTrustElapsedRealTime_ = false; + } else { + // sometimes just once system communication block, this could recover automaticly, + // elapsedRealtime still be ok. + canTrustElapsedRealTime_ = true; + } + timeCheck_ = 0; + timeCheckElapsed_ = 0; + } +} + +bool ElapsedRealTimeCheck::CanTrustElapsedRealTime() +{ + return canTrustElapsedRealTime_; +} + +} // namespace Location +} // namespace OHOS +#endif // TIME_SERVICE_ENABLE +#endif diff --git a/services/location_gnss/gnss/source/ntp/gps_time_manager.cpp b/services/location_gnss/gnss/source/ntp/gps_time_manager.cpp new file mode 100644 index 00000000..0349e47a --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/gps_time_manager.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE + +#include "gps_time_manager.h" + +#include "elapsed_real_time_check.h" +#include "location_log.h" +#include "time_service_client.h" + +namespace OHOS { +namespace Location { +const int64_t INVALID_TIME = 0L; +const int64_t MAX_MISSTAKE_TIME = 10 * 1000L; + +GpsTimeManager::GpsTimeManager() +{ + timeManager_ = TimeManager(EXPIRT_TIME); +} + +GpsTimeManager::~GpsTimeManager() {} + +bool GpsTimeManager::CheckValid(int64_t gpsTime, int64_t lastSystemTime) +{ + int64_t bootTimeDiff = gpsTime - timeManager_.GetTimestamp(); + int64_t systemTimeDiff = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs() - lastSystemTime; + if (abs(bootTimeDiff - systemTimeDiff) > MAX_MISSTAKE_TIME) { + return false; + } + return true; +} + +int64_t GpsTimeManager::GetGpsTime() +{ + int64_t tmpTime = timeManager_.GetCurrentTime(); + if ((!validFlag_) && (tmpTime > 0)) { + if (!CheckValid(tmpTime, lastSystemTime_)) { + return INVALID_TIME; + } + } + return tmpTime; +} + +void GpsTimeManager::SetGpsTime(int64_t gpsMsTime, int64_t bootTimeMs) +{ + validFlag_ = true; + lastSystemTime_ = MiscServices::TimeServiceClient::GetInstance()->GetWallTimeMs(); + timeManager_.SetCurrentTime(gpsMsTime, bootTimeMs); + auto elapsedRealTimeCheck = ElapsedRealTimeCheck::GetInstance(); + if (elapsedRealTimeCheck != nullptr) { + elapsedRealTimeCheck->CheckRealTime(gpsMsTime); + } +} + +} // namespace Location +} // namespace OHOS +#endif // TIME_SERVICE_ENABLE +#endif diff --git a/services/location_gnss/gnss/source/ntp/net_conn_observer.cpp b/services/location_gnss/gnss/source/ntp/net_conn_observer.cpp new file mode 100644 index 00000000..1f005ff9 --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/net_conn_observer.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef NET_MANAGER_ENABLE +#include "net_conn_observer.h" + +#include "location_log.h" +#include "ntp_time_helper.h" + +using namespace OHOS::NetManagerStandard; + +namespace OHOS { +namespace Location { +int32_t NetConnObserver::NetAvailable(sptr &netHandle) +{ + return ERR_OK; +} + +int32_t NetConnObserver::NetCapabilitiesChange(sptr &netHandle, + const sptr &netAllCap) +{ + LBSLOGI(GNSS, "Observe network capabilities change"); + if (netAllCap->netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) { +#ifdef TIME_SERVICE_ENABLE + auto npTimeHelper = NtpTimeHelper::GetInstance(); + if (npTimeHelper != nullptr) { + npTimeHelper->RetrieveAndInjectNtpTime(); + } +#endif + } + return ERR_OK; +} + +int32_t NetConnObserver::NetConnectionPropertiesChange(sptr &netHandle, + const sptr &info) +{ + return ERR_OK; +} + +int32_t NetConnObserver::NetLost(sptr &netHandle) +{ + return ERR_OK; +} + +int32_t NetConnObserver::NetUnavailable() +{ + return ERR_OK; +} + +int32_t NetConnObserver::NetBlockStatusChange(sptr &netHandle, bool blocked) +{ + return ERR_OK; +} +} // namespace Location +} // namespace OHOS + +#endif +#endif \ No newline at end of file diff --git a/services/location_gnss/gnss/source/ntp/ntp_time_check.cpp b/services/location_gnss/gnss/source/ntp/ntp_time_check.cpp new file mode 100644 index 00000000..fcea116f --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/ntp_time_check.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE +#include "ntp_time_check.h" + +#include +#include + +#include "common_utils.h" +#include "location_log.h" +#include "time_service_client.h" + +namespace OHOS { +namespace Location { +const int64_t INVALID_TIME = 0; +const int64_t MISSTAKE_TIME = 50 * 1000; // 50s + +NtpTimeCheck::NtpTimeCheck() {} + +NtpTimeCheck::~NtpTimeCheck() {} + +NtpTimeCheck *NtpTimeCheck::GetInstance() +{ + static NtpTimeCheck data; + return &data; +} + +bool NtpTimeCheck::CheckNtpTime(int64_t ntpMsTime, int64_t msTimeSynsBoot) +{ + int64_t currentNtpTime = + ntpMsTime + MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs() - msTimeSynsBoot; + if (gpsTimeManager_.GetGpsTime() != INVALID_TIME) { + return CompareTime(currentNtpTime, gpsTimeManager_.GetGpsTime()); + } + + LBSLOGI(GNSS, "checkNtpTime return false"); + return false; +} + +bool NtpTimeCheck::CompareTime(int64_t currentNtpTime, int64_t compareTime) +{ + LBSLOGI(GNSS, "compareTime currentNtpTime:%{public}s, compareTime:%{public}s", + std::to_string(currentNtpTime).c_str(), std::to_string(compareTime).c_str()); + int64_t misstake = std::abs(currentNtpTime - compareTime); + if (misstake > MISSTAKE_TIME) { + LBSLOGI(GNSS, "find error ntp time:%{public}s", std::to_string(misstake).c_str()); + return false; + } else { + difference_ = misstake; + return true; + } +} + +void NtpTimeCheck::SetGpsTime(int64_t gpsMsTime, int64_t bootTimeMs) +{ + if (gpsMsTime <= 0 || bootTimeMs <= 0) { + LBSLOGE(GNSS, "set gps time failed :%{public}s, %{public}s", std::to_string(gpsMsTime).c_str(), + std::to_string(bootTimeMs).c_str()); + return; + } + gpsTimeManager_.SetGpsTime(gpsMsTime, bootTimeMs); +} + +int NtpTimeCheck::GetUncertainty() +{ + return (difference_ > DEFAULT_UNCERTAINTY) ? difference_ : DEFAULT_UNCERTAINTY; +} + +} // namespace Location +} // namespace OHOS +#endif // TIME_SERVICE_ENABLE +#endif diff --git a/services/location_gnss/gnss/source/ntp/ntp_time_helper.cpp b/services/location_gnss/gnss/source/ntp/ntp_time_helper.cpp new file mode 100644 index 00000000..1f401746 --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/ntp_time_helper.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE +#include "ntp_time_helper.h" + +#include +#include "gnss_ability.h" +#include "location_log.h" +#include "ntp_time_check.h" +#include "time_service_client.h" + +namespace OHOS { +namespace Location { +static const int64_t NTP_INTERVAL = 24 * 60 * 60 * 1000; + +NtpTimeHelper *NtpTimeHelper::GetInstance() +{ + static NtpTimeHelper data; + return &data; +} + +void NtpTimeHelper::RetrieveAndInjectNtpTime() +{ + if (ntpResult_.elapsedRealTime > 0) { + ntpResult_.ageMillis = + MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs() - ntpResult_.elapsedRealTime; + LBSLOGD(GNSS, "ntp age millis %{public}s", std::to_string(ntpResult_.ageMillis).c_str()); + } + + auto gnssAbility = GnssAbility::GetInstance(); + if (ntpResult_.ntpTime == 0 || ntpResult_.ageMillis > NTP_INTERVAL) { + auto ret = MiscServices::TimeServiceClient::GetInstance()->GetNtpTimeMs(ntpResult_.ntpTime); + if (ret != ERR_OK) { + ntpResult_.ntpTime = 0; + LBSLOGE(GNSS, "failed to get ntp time %{public}d", ret); + return; + } + LBSLOGI(GNSS, "get ntp time %{public}s", std::to_string(ntpResult_.ntpTime).c_str()); + ntpResult_.elapsedRealTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs(); + ntpResult_.ageMillis = 0; + if (gnssAbility != nullptr) { + gnssAbility->UpdateNtpTime(ntpResult_.ntpTime, ntpResult_.elapsedRealTime); + } + } + if (gnssAbility != nullptr) { + gnssAbility->InjectTime(); + } +} + +} // namespace Location +} // namespace OHOS +#endif // TIME_SERVICE_ENABLE +#endif diff --git a/services/location_gnss/gnss/source/ntp/time_manager.cpp b/services/location_gnss/gnss/source/ntp/time_manager.cpp new file mode 100644 index 00000000..3d625c6c --- /dev/null +++ b/services/location_gnss/gnss/source/ntp/time_manager.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef FEATURE_GNSS_SUPPORT +#ifdef TIME_SERVICE_ENABLE +#include "time_manager.h" + +#include +#include "common_utils.h" +#include "elapsed_real_time_check.h" +#include "location_log.h" +#include "time_service_client.h" + +namespace OHOS { +namespace Location { +const int64_t INVALID_TIME = 0; +const int64_t GPS_UTC_REFERENCE_TIME = 946656000; // 946656000:UTC time of 2000-01-01 00:00:00 + +int64_t TimeManager::GetCurrentTime() +{ + LBSLOGI(GNSS, "beginning timestamp_ is %{public}s", std::to_string(timestamp_).c_str()); + if (timestamp_ < GPS_UTC_REFERENCE_TIME) { + return INVALID_TIME; + } + if (!ElapsedRealTimeCheck::GetInstance()->CanTrustElapsedRealTime()) { + LBSLOGI(GNSS, "getCurrentTime ElapsedRealTime INVALID_TIME"); + return INVALID_TIME; + } + + int64_t timeTillNow = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs() - timeSynsBoot_; + if (timeTillNow >= expireTime_) { + LBSLOGI(GNSS, "getCurrentTime INVALID_TIME"); + return INVALID_TIME; + } else { + LBSLOGI(GNSS, "getCurrentTime:%{public}s", std::to_string(timestamp_ + timeTillNow).c_str()); + return timestamp_ + timeTillNow; + } +} + +void TimeManager::SetCurrentTime(int64_t msTime, int64_t msTimeSynsBoot) +{ + timestamp_ = msTime; + timeSynsBoot_ = msTimeSynsBoot; + LBSLOGI(GNSS, "setCurrentTime timestamp_:%{public}s, mTimeReference:%{public}s", + std::to_string(timestamp_).c_str(), std::to_string(timeSynsBoot_).c_str()); +} + +int64_t TimeManager::GetTimestamp() +{ + return timestamp_; +} + +} // namespace Location +} // namespace OHOS +#endif // TIME_SERVICE_ENABLE +#endif diff --git a/test/location_gnss/BUILD.gn b/test/location_gnss/BUILD.gn index d110a399..81674dce 100644 --- a/test/location_gnss/BUILD.gn +++ b/test/location_gnss/BUILD.gn @@ -26,6 +26,7 @@ if (location_feature_with_gnss) { "$GNSS_UNIT_TEST_DIR/source/gnss_ability_test.cpp", "$GNSS_UNIT_TEST_DIR/source/gnss_event_callback_test.cpp", "$GNSS_UNIT_TEST_DIR/source/gnss_interface_test.cpp", + "$GNSS_UNIT_TEST_DIR/source/ntp_time_test.cpp", "$LOCATION_ROOT_DIR/test/mock/src/mock_service_registry.cpp", ] diff --git a/test/location_gnss/include/ntp_time_test.h b/test/location_gnss/include/ntp_time_test.h new file mode 100644 index 00000000..2cb9b3a7 --- /dev/null +++ b/test/location_gnss/include/ntp_time_test.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NTP_TIME_TEST_H +#define NTP_TIME_TEST_H + +#include + +namespace OHOS { +namespace Location { +class NtpTimeTest : public testing::Test { +public: + void SetUp(); + void TearDown(); +}; +} // namespace Location +} // namespace OHOS +#endif // NTP_TIME_TEST_H \ No newline at end of file diff --git a/test/location_gnss/source/ntp_time_test.cpp b/test/location_gnss/source/ntp_time_test.cpp new file mode 100644 index 00000000..6ede55ac --- /dev/null +++ b/test/location_gnss/source/ntp_time_test.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ntp_time_test.h" + +#include "location_log.h" +#include "ntp_time_check.h" + +using namespace testing::ext; +namespace OHOS { +namespace Location { +void NtpTimeTest::SetUp() +{ +} + +void NtpTimeTest::TearDown() +{ +} + +HWTEST_F(NtpTimeTest, NtpTimeCheckSetGpsTimeTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) + << "NtpTimeTest, NtpTimeCheckSetGpsTimeTest001, TestSize.Level1"; + LBSLOGI(GNSS_TEST, "[NtpTimeTest] NtpTimeCheckSetGpsTimeTest001 begin"); + auto ntpTimeCheck = NtpTimeCheck::GetInstance(); + EXPECT_NE(nullptr, ntpTimeCheck); + int64_t gpsMsTime = -1; + int64_t bootTimeMs = -1; + ntpTimeCheck->SetGpsTime(gpsMsTime, bootTimeMs); + LBSLOGI(GNSS_TEST, "[NtpTimeTest] NtpTimeCheckSetGpsTimeTest001 end"); +} + +HWTEST_F(NtpTimeTest, NtpTimeCheckCheckNtpTimeTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) + << "NtpTimeTest, NtpTimeCheckCheckNtpTimeTest001, TestSize.Level1"; + LBSLOGI(GNSS_TEST, "[NtpTimeTest] NtpTimeCheckCheckNtpTimeTest001 begin"); + auto ntpTimeCheck = NtpTimeCheck::GetInstance(); + EXPECT_NE(nullptr, ntpTimeCheck); + int64_t gpsMsTime = 1720522730000; + int64_t bootTimeMs = 1; + ntpTimeCheck->SetGpsTime(gpsMsTime, bootTimeMs); + int64_t ntpMsTime = 1720522730000; + int64_t msTimeSynsBoot = 1; + ntpTimeCheck->CheckNtpTime(ntpMsTime, msTimeSynsBoot); + LBSLOGI(GNSS_TEST, "[NtpTimeTest] NtpTimeCheckCheckNtpTimeTest001 end"); +} + +} +} \ No newline at end of file