diff --git a/gnss/common/utils/default/include/v2_1/GnssTemplate.h b/gnss/common/utils/default/include/v2_1/GnssTemplate.h index d16a67bffe..1fe6c3eb87 100644 --- a/gnss/common/utils/default/include/v2_1/GnssTemplate.h +++ b/gnss/common/utils/default/include/v2_1/GnssTemplate.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include #include @@ -158,14 +159,16 @@ GnssTemplate::~GnssTemplate() { template std::unique_ptr GnssTemplate::getLocationFromHW() { char inputBuffer[INPUT_BUFFER_SIZE]; - mHardwareModeOn = false; if (mGnssFd == -1) { mGnssFd = open(GNSS_PATH, O_RDWR | O_NONBLOCK); } + if (mGnssFd == -1) { + ALOGW("Failed to open /dev/gnss0 errno: %d", errno); return nullptr; } - // Send control message to device + // Indicates it is a hardwareMode, don't report the default location. + mHardwareModeOn = true; int bytes_write = write(mGnssFd, CMD_GET_LOCATION, strlen(CMD_GET_LOCATION)); if (bytes_write <= 0) { return nullptr; @@ -179,8 +182,7 @@ std::unique_ptr GnssTemplate::getLocationFromHW() { int bytes_read = -1; std::string inputStr = ""; int epoll_ret = epoll_wait(epoll_fd, events, 1, mMinIntervalMs); - // Indicates it is a hardwareMode, don't need to wait outside. - mHardwareModeOn = true; + if (epoll_ret == -1) { return nullptr; } @@ -206,10 +208,12 @@ Return GnssTemplate::start() { while (mIsActive == true) { auto svStatus = filterBlacklistedSatellitesV2_1(Utils::getMockSvInfoListV2_1()); this->reportSvStatus(svStatus); - auto currentLocation = getLocationFromHW(); - if (currentLocation != nullptr) { - this->reportLocation(*currentLocation); + if (mHardwareModeOn) { + if (currentLocation != nullptr) { + // Only report location if the return from hardware is valid + this->reportLocation(*currentLocation); + } } else { if (sGnssCallback_2_1 != nullptr || sGnssCallback_2_0 != nullptr) { const auto location = Utils::getMockLocationV2_0(); @@ -218,13 +222,8 @@ Return GnssTemplate::start() { const auto location = Utils::getMockLocationV1_0(); this->reportLocation(location); } - - // Only need do the sleep in the static location mode, which mocks the "wait - // for" hardware behavior. - if (!mHardwareModeOn) { - std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs)); - } } + std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs)); } }); return true;