1 /*
2 * Copyright (C) 2022 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 #include "fusion_controller.h"
17 #include "system_ability_definition.h"
18 #include "common_utils.h"
19 #include "constant_definition.h"
20 #include "location_log.h"
21 #include "hook_utils.h"
22 #ifdef FEATURE_NETWORK_SUPPORT
23 #include "network_ability_proxy.h"
24 #endif
25
26 namespace OHOS {
27 namespace Location {
28 const uint32_t FUSION_DEFAULT_FLAG = 0;
29 const uint32_t FUSION_BASE_FLAG = 1;
30 const uint32_t REPORT_FUSED_LOCATION_FLAG = FUSION_BASE_FLAG;
31
32 #ifdef FEATURE_NETWORK_SUPPORT
33 const uint32_t QUICK_FIX_FLAG = FUSION_BASE_FLAG << 1;
34 #endif
35 const long NANOS_PER_MILLI = 1000000L;
36 const long MAX_GNSS_LOCATION_COMPARISON_MS = 30 * MILLI_PER_SEC;
37 const long MAX_INDOOR_LOCATION_COMPARISON_MS = 5 * MILLI_PER_SEC;
38 const double MAX_INDOOR_LOCATION_SPEED = 3.0;
39
GetInstance()40 FusionController* FusionController::GetInstance()
41 {
42 static FusionController data;
43 return &data;
44 }
45
ActiveFusionStrategies(int type)46 void FusionController::ActiveFusionStrategies(int type)
47 {
48 if (needReset_) {
49 fusedFlag_ = FUSION_DEFAULT_FLAG;
50 needReset_ = false;
51 }
52 switch (type) {
53 case SCENE_NAVIGATION:
54 case SCENE_TRAJECTORY_TRACKING:
55 break;
56 case PRIORITY_FAST_FIRST_FIX:
57 fusedFlag_ = fusedFlag_ | REPORT_FUSED_LOCATION_FLAG;
58 break;
59 default:
60 break;
61 }
62 }
63
chooseBestLocation(const std::unique_ptr<Location> & location,const std::unique_ptr<Location> & lastFuseLocation)64 std::unique_ptr<Location> FusionController::chooseBestLocation(const std::unique_ptr<Location>& location,
65 const std::unique_ptr<Location>& lastFuseLocation)
66 {
67 if (location == nullptr) {
68 return nullptr;
69 }
70 if (lastFuseLocation == nullptr) {
71 return std::make_unique<Location>(*location);
72 }
73 LocationFusionInfo fusionInfo;
74 fusionInfo.location = *location;
75 fusionInfo.lastFuseLocation = *lastFuseLocation;
76 fusionInfo.resultLocation = fusionInfo.location;
77 if (fusionInfo.location.GetLocationSourceType() == LocationSourceType::NETWORK_TYPE) {
78 if (CheckIfLastGnssLocationValid(location, std::make_unique<Location>(fusionInfo.lastFuseLocation))) {
79 fusionInfo.resultLocation = fusionInfo.lastFuseLocation;
80 }
81 }
82 HookUtils::ExecuteHook(
83 LocationProcessStage::FUSION_REPORT_PROCESS, (void *)&fusionInfo, nullptr);
84 return std::make_unique<Location>(fusionInfo.resultLocation);
85 }
86
CheckIfLastGnssLocationValid(const std::unique_ptr<Location> & location,const std::unique_ptr<Location> & lastFuseLocation)87 bool FusionController::CheckIfLastGnssLocationValid(const std::unique_ptr<Location>& location,
88 const std::unique_ptr<Location>& lastFuseLocation)
89 {
90 if ((lastFuseLocation->GetLocationSourceType() == LocationSourceType::GNSS_TYPE ||
91 lastFuseLocation->GetLocationSourceType() == LocationSourceType::RTK_TYPE) &&
92 ((location->GetTimeSinceBoot() / NANOS_PER_MILLI -
93 lastFuseLocation->GetTimeSinceBoot() / NANOS_PER_MILLI) < MAX_GNSS_LOCATION_COMPARISON_MS)) {
94 return true;
95 } else {
96 return false;
97 }
98 }
99
GetFuseLocation(const std::unique_ptr<Location> & location,const sptr<Location> & lastFuseLocation)100 std::unique_ptr<Location> FusionController::GetFuseLocation(const std::unique_ptr<Location>& location,
101 const sptr<Location>& lastFuseLocation)
102 {
103 LBSLOGD(FUSION_CONTROLLER, " GetFuseLocation enter");
104 auto bestLocation = chooseBestLocation(location, std::make_unique<Location>(*lastFuseLocation));
105 return std::make_unique<Location>(*bestLocation);
106 }
107 } // namespace Location
108 } // namespace OHOS