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 "common_utils.h"
17 #include <map>
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "ipc_types.h"
22 #include "iservice_registry.h"
23 #include "parameters.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Location {
28 static std::shared_ptr<std::map<int, sptr<IRemoteObject>>> g_proxyMap =
29 std::make_shared<std::map<int, sptr<IRemoteObject>>>();
30 std::mutex g_proxyMutex;
31
CheckSystemCalling(pid_t uid)32 bool CommonUtils::CheckSystemCalling(pid_t uid)
33 {
34 return true;
35 }
36
CheckLocationPermission()37 bool CommonUtils::CheckLocationPermission()
38 {
39 return CheckPermission(ACCESS_LOCATION);
40 }
41
CheckPermission(const std::string & permission)42 bool CommonUtils::CheckPermission(const std::string &permission)
43 {
44 auto callerToken = IPCSkeleton::GetCallingTokenID();
45 auto tokenFirstCaller = IPCSkeleton::GetFirstTokenID();
46 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
47 int result = Security::AccessToken::PERMISSION_DENIED;
48 if (tokenFirstCaller == 0) {
49 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
50 result = Security::AccessToken::AccessTokenKit::VerifyNativeToken(callerToken, permission);
51 } else if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
52 result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
53 } else {
54 LBSLOGE(COMMON_UTILS, "invalid callerToken");
55 }
56 } else {
57 result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, tokenFirstCaller, permission);
58 }
59 if (result == Security::AccessToken::PERMISSION_GRANTED) {
60 return true;
61 } else {
62 LBSLOGD(COMMON_UTILS, "has no permission.permission name=%{public}s", permission.c_str());
63 return false;
64 }
65 }
66
CheckBackgroundPermission()67 bool CommonUtils::CheckBackgroundPermission()
68 {
69 return CheckPermission(ACCESS_BACKGROUND_LOCATION);
70 }
71
CheckSecureSettings()72 bool CommonUtils::CheckSecureSettings()
73 {
74 return CheckPermission(MANAGE_SECURE_SETTINGS);
75 }
76
AbilityConvertToId(const std::string ability)77 int CommonUtils::AbilityConvertToId(const std::string ability)
78 {
79 if (GNSS_ABILITY.compare(ability) == 0) {
80 return LOCATION_GNSS_SA_ID;
81 }
82 if (NETWORK_ABILITY.compare(ability) == 0) {
83 return LOCATION_NETWORK_LOCATING_SA_ID;
84 }
85 if (PASSIVE_ABILITY.compare(ability) == 0) {
86 return LOCATION_NOPOWER_LOCATING_SA_ID;
87 }
88 if (GEO_ABILITY.compare(ability) == 0) {
89 return LOCATION_GEO_CONVERT_SA_ID;
90 }
91 return -1;
92 }
93
GetCapabilityToString(std::string ability,uint32_t capability)94 std::u16string CommonUtils::GetCapabilityToString(std::string ability, uint32_t capability)
95 {
96 std::string value = "{\"Capabilities\":{\"" + ability + "\":" + std::to_string(capability) + "}}";
97 return Str8ToStr16(value);
98 }
99
GetCapability(std::string ability)100 std::u16string CommonUtils::GetCapability(std::string ability)
101 {
102 uint32_t capability = 0x102;
103 return GetCapabilityToString(ability, capability);
104 }
105
GetLabel(std::string name)106 OHOS::HiviewDFX::HiLogLabel CommonUtils::GetLabel(std::string name)
107 {
108 if (GNSS_ABILITY.compare(name) == 0) {
109 return GNSS;
110 }
111 if (NETWORK_ABILITY.compare(name) == 0) {
112 return NETWORK;
113 }
114 if (PASSIVE_ABILITY.compare(name) == 0) {
115 return PASSIVE;
116 }
117 if (GEO_ABILITY.compare(name) == 0) {
118 return GEO_CONVERT;
119 }
120 OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, LOCATOR_LOG_ID, "unknown" };
121 return label;
122 }
123
GetRemoteObject(int abilityId)124 sptr<IRemoteObject> CommonUtils::GetRemoteObject(int abilityId)
125 {
126 return GetRemoteObject(abilityId, InitDeviceId());
127 }
128
GetRemoteObject(int abilityId,std::string deviceId)129 sptr<IRemoteObject> CommonUtils::GetRemoteObject(int abilityId, std::string deviceId)
130 {
131 std::lock_guard<std::mutex> lock(g_proxyMutex);
132 auto objectGnss = g_proxyMap->find(abilityId);
133 if (objectGnss == g_proxyMap->end()) {
134 auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135 if (manager == nullptr) {
136 LBSLOGE(COMMON_UTILS, "GetSystemAbilityManager is null.");
137 return nullptr;
138 }
139 sptr<IRemoteObject> object = manager->GetSystemAbility(abilityId, deviceId);
140 if (object == nullptr) {
141 LBSLOGE(COMMON_UTILS, "GetSystemAbility is null.");
142 return nullptr;
143 }
144 g_proxyMap->insert(std::make_pair(abilityId, object));
145 return object;
146 } else {
147 sptr<IRemoteObject> remoteObject = objectGnss->second;
148 return remoteObject;
149 }
150 }
151
InitDeviceId()152 std::string CommonUtils::InitDeviceId()
153 {
154 std::string deviceId;
155 return deviceId;
156 }
157
Wait(int time)158 void CountDownLatch::Wait(int time)
159 {
160 LBSLOGD(LOCATOR_STANDARD, "enter wait, time = %{public}d", time);
161 std::unique_lock<std::mutex> lock(mutex_);
162 if (count_ == 0) {
163 LBSLOGE(LOCATOR_STANDARD, "count_ = 0");
164 return;
165 }
166 condition_.wait_for(lock, std::chrono::seconds(time / SEC_TO_MILLI_SEC), [&]() {return count_ == 0;});
167 }
168
CountDown()169 void CountDownLatch::CountDown()
170 {
171 LBSLOGD(LOCATOR_STANDARD, "enter CountDown");
172 std::unique_lock<std::mutex> lock(mutex_);
173 int old_c = count_.load();
174 while (old_c > 0) {
175 if (count_.compare_exchange_strong(old_c, old_c - 1)) {
176 if (old_c == 1) {
177 LBSLOGD(LOCATOR_STANDARD, "notify_all");
178 condition_.notify_all();
179 }
180 break;
181 }
182 old_c = count_.load();
183 }
184 }
185
GetCount()186 int CountDownLatch::GetCount()
187 {
188 std::unique_lock<std::mutex> lock(mutex_);
189 return count_;
190 }
191
SetCount(int count)192 void CountDownLatch::SetCount(int count)
193 {
194 std::unique_lock<std::mutex> lock(mutex_);
195 count_ = count;
196 }
197
Str16ToStr8(std::u16string str)198 std::string CommonUtils::Str16ToStr8(std::u16string str)
199 {
200 if (str == DEFAULT_USTRING) {
201 return DEFAULT_STRING;
202 }
203 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(DEFAULT_STRING);
204 std::string result = convert.to_bytes(str);
205 return result == DEFAULT_STRING ? "" : result;
206 }
207
DoubleEqual(double a,double b)208 bool CommonUtils::DoubleEqual(double a, double b)
209 {
210 if (fabs(a - b) < 1e-6) {
211 return true;
212 } else {
213 return false;
214 }
215 }
216 } // namespace Location
217 } // namespace OHOS
218