diff --git a/frameworks/location_common/common/source/common_utils.cpp b/frameworks/location_common/common/source/common_utils.cpp index b5e68b55..cb48c4d5 100644 --- a/frameworks/location_common/common/source/common_utils.cpp +++ b/frameworks/location_common/common/source/common_utils.cpp @@ -41,6 +41,7 @@ static std::random_device g_randomDevice; static std::mt19937 g_gen(g_randomDevice()); static std::uniform_int_distribution<> g_dis(0, 15); // random between 0 and 15 static std::uniform_int_distribution<> g_dis2(8, 11); // random between 8 and 11 +const int64_t SEC_TO_NANO = 1000 * 1000 * 1000; int CommonUtils::AbilityConvertToId(const std::string ability) { @@ -420,5 +421,17 @@ bool CommonUtils::CheckAppForUser(int32_t uid) } return true; } + +int64_t CommonUtils::GetSinceBootTime() +{ + int result; + struct timespec ts; + result = clock_gettime(CLOCK_BOOTTIME, &ts); + if (result == 0) { + return ts.tv_sec * SEC_TO_NANO + ts.tv_nsec; + } else { + return 0; + } +} } // namespace Location } // namespace OHOS diff --git a/interfaces/inner_api/include/common_utils.h b/interfaces/inner_api/include/common_utils.h index 55ab7594..e8e244a0 100644 --- a/interfaces/inner_api/include/common_utils.h +++ b/interfaces/inner_api/include/common_utils.h @@ -177,6 +177,7 @@ public: static bool GetEdmPolicy(std::string& name); static std::string GenerateUuid(); static bool CheckAppForUser(int32_t uid); + static int64_t GetSinceBootTime(); }; class CountDownLatch { diff --git a/services/location_gnss/gnss/source/gnss_ability.cpp b/services/location_gnss/gnss/source/gnss_ability.cpp index 63a8376f..6fb7191d 100644 --- a/services/location_gnss/gnss/source/gnss_ability.cpp +++ b/services/location_gnss/gnss/source/gnss_ability.cpp @@ -1430,6 +1430,9 @@ int32_t GnssAbility::ReportMockedLocation(const std::shared_ptr locati LBSLOGE(GNSS, "location mock is enabled, do not report gnss location!"); return ERR_OK; } + location->SetTimeSinceBoot(CommonUtils::GetSinceBootTime()); + location->SetTimeStamp(CommonUtils::GetCurrentTimeStamp() * MICRO_PER_MILLI); + location->SetLocationSourceType(LocationSourceType::GNSS_TYPE); ReportLocationInfo(GNSS_ABILITY, location); #ifdef FEATURE_PASSIVE_SUPPORT ReportLocationInfo(PASSIVE_ABILITY, location); diff --git a/services/location_locator/locator/source/report_manager.cpp b/services/location_locator/locator/source/report_manager.cpp index a1719d55..5fe18242 100644 --- a/services/location_locator/locator/source/report_manager.cpp +++ b/services/location_locator/locator/source/report_manager.cpp @@ -77,12 +77,12 @@ bool ReportManager::OnReportLocation(const std::unique_ptr& location, for (auto iter = requestList.begin(); iter != requestList.end(); iter++) { auto request = *iter; WriteNetWorkReportEvent(abilityName, request, location); - if (abilityName == NETWORK_ABILITY && (request->GetUuid() == location->GetUuid())) { - ProcessRequestForReport(request, deadRequests, location, abilityName); - break; - } else if (abilityName == NETWORK_ABILITY && (request->GetUuid() != location->GetUuid())) { - continue; - } else { + if (abilityName == NETWORK_ABILITY) { + if (request->GetUuid() == location->GetUuid() || location->GetIsFromMock()) { + ProcessRequestForReport(request, deadRequests, location, abilityName); + break; + } + } else if (abilityName == GNSS_ABILITY || abilityName == PASSIVE_ABILITY) { ProcessRequestForReport(request, deadRequests, location, abilityName); } } @@ -276,7 +276,8 @@ bool ReportManager::ResultCheck(const std::unique_ptr& location, LBSLOGE(REPORT_MANAGER, "accuracy check fail, do not report location"); return false; } - if (CommonUtils::DoubleEqual(request->GetLastLocation()->GetLatitude(), MIN_LATITUDE - 1)) { + if (CommonUtils::DoubleEqual(request->GetLastLocation()->GetLatitude(), MIN_LATITUDE - 1) || + request->GetLastLocation()->GetIsFromMock() != location->GetIsFromMock()) { LBSLOGD(REPORT_MANAGER, "no valid cache location, no need to check"); return true; } diff --git a/services/location_network/network/source/network_ability.cpp b/services/location_network/network/source/network_ability.cpp index ddb1678d..333eb689 100644 --- a/services/location_network/network/source/network_ability.cpp +++ b/services/location_network/network/source/network_ability.cpp @@ -407,6 +407,9 @@ int32_t NetworkAbility::ReportMockedLocation(const std::shared_ptr loc LBSLOGE(NETWORK, "location mock is enabled, do not report network location!"); return ERR_OK; } + location->SetTimeSinceBoot(CommonUtils::GetSinceBootTime()); + location->SetTimeStamp(CommonUtils::GetCurrentTimeStamp() * MICRO_PER_MILLI); + location->SetLocationSourceType(LocationSourceType::NETWORK_TYPE); ReportLocationInfo(NETWORK_ABILITY, location); #ifdef FEATURE_PASSIVE_SUPPORT ReportLocationInfo(PASSIVE_ABILITY, location); diff --git a/services/location_network/network/source/network_callback_host.cpp b/services/location_network/network/source/network_callback_host.cpp index e41d1013..bf15592f 100644 --- a/services/location_network/network/source/network_callback_host.cpp +++ b/services/location_network/network/source/network_callback_host.cpp @@ -63,9 +63,20 @@ int NetworkCallbackHost::OnRemoteRequest( void NetworkCallbackHost::OnLocationReport(const std::unique_ptr& location) { LBSLOGD(NETWORK, "NetworkCallbackHost::OnLocationReport"); + auto networkAbility = NetworkAbility::GetInstance(); + if (networkAbility == nullptr) { + LBSLOGE(NETWORK, "ReportLocation: network ability is nullptr."); + return; + } + if (networkAbility->IsMockEnabled()) { + LBSLOGE(NETWORK, "location mock is enabled, do not report network location!"); + return; + } std::shared_ptr locationNew = std::make_shared(*location); NetworkAbility::GetInstance()->ReportLocationInfo(NETWORK_ABILITY, locationNew); - NetworkAbility::GetInstance()->ReportLocationInfo(PASSIVE_ABILITY, locationNew); + if (locationNew->GetLocationSourceType() == LocationSourceType::NETWORK_TYPE) { + NetworkAbility::GetInstance()->ReportLocationInfo(PASSIVE_ABILITY, locationNew); + } WriteLocationInnerEvent(NETWORK_CALLBACK_LOCATION, {"speed", std::to_string(location->GetSpeed()), "accuracy", std::to_string(location->GetAccuracy()), "locationTimestamp", std::to_string(location->GetTimeStamp() / MILLI_PER_SEC),