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     if (location->GetLocationSourceType() == LocationSourceType::INDOOR_TYPE) {
74         if (CheckIfLastGnssLocationValid(location, std::make_unique<Location>(*lastFuseLocation)) &&
75             lastFuseLocation->GetSpeed() >= MAX_INDOOR_LOCATION_SPEED) {
76             return std::make_unique<Location>(*lastFuseLocation);
77         }
78         return std::make_unique<Location>(*location);
79     } else if (location->GetLocationSourceType() == LocationSourceType::GNSS_TYPE ||
80                 location->GetLocationSourceType() == LocationSourceType::RTK_TYPE) {
81         if (location->GetSpeed() >= MAX_INDOOR_LOCATION_SPEED) {
82             return std::make_unique<Location>(*location);
83         }
84         if (CheckIfLastIndoorLocationValid(location, std::make_unique<Location>(*lastFuseLocation))) {
85             return std::make_unique<Location>(*lastFuseLocation);
86         }
87     } else if (location->GetLocationSourceType() == LocationSourceType::NETWORK_TYPE) {
88         if (CheckIfLastIndoorLocationValid(location, std::make_unique<Location>(*lastFuseLocation))) {
89             return std::make_unique<Location>(*lastFuseLocation);
90         } else if (CheckIfLastGnssLocationValid(location, std::make_unique<Location>(*lastFuseLocation))) {
91             return std::make_unique<Location>(*lastFuseLocation);
92         }
93     }
94     return std::make_unique<Location>(*location);
95 }
96 
CheckIfLastIndoorLocationValid(const std::unique_ptr<Location> & location,const std::unique_ptr<Location> & lastFuseLocation)97 bool FusionController::CheckIfLastIndoorLocationValid(const std::unique_ptr<Location>& location,
98     const std::unique_ptr<Location>& lastFuseLocation)
99 {
100     if (lastFuseLocation->GetLocationSourceType() == LocationSourceType::INDOOR_TYPE &&
101         ((location->GetTimeSinceBoot() / NANOS_PER_MILLI -
102         lastFuseLocation->GetTimeSinceBoot() / NANOS_PER_MILLI) < MAX_INDOOR_LOCATION_COMPARISON_MS)) {
103         return true;
104     } else {
105         return false;
106     }
107 }
108 
CheckIfLastGnssLocationValid(const std::unique_ptr<Location> & location,const std::unique_ptr<Location> & lastFuseLocation)109 bool FusionController::CheckIfLastGnssLocationValid(const std::unique_ptr<Location>& location,
110     const std::unique_ptr<Location>& lastFuseLocation)
111 {
112     if (lastFuseLocation->GetLocationSourceType() == LocationSourceType::GNSS_TYPE &&
113         ((location->GetTimeSinceBoot() / NANOS_PER_MILLI -
114         lastFuseLocation->GetTimeSinceBoot() / NANOS_PER_MILLI) < MAX_GNSS_LOCATION_COMPARISON_MS)) {
115         return true;
116     } else {
117         return false;
118     }
119 }
120 
GetFuseLocation(const std::unique_ptr<Location> & location,const sptr<Location> & lastFuseLocation)121 std::unique_ptr<Location> FusionController::GetFuseLocation(const std::unique_ptr<Location>& location,
122     const sptr<Location>& lastFuseLocation)
123 {
124     LBSLOGD(FUSION_CONTROLLER, " GetFuseLocation enter");
125     auto bestLocation = chooseBestLocation(location, std::make_unique<Location>(*lastFuseLocation));
126     return std::make_unique<Location>(*bestLocation);
127 }
128 } // namespace Location
129 } // namespace OHOS