• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMMON_UTILS_H
16 #define COMMON_UTILS_H
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
21 #include <unistd.h>
22 #include <string>
23 
24 #include "iremote_object.h"
25 #include "string_ex.h"
26 
27 #include "location_log.h"
28 
29 namespace OHOS {
30 namespace Location {
31 const std::string GNSS_ABILITY = "gps";
32 const std::string NETWORK_ABILITY = "network";
33 const std::string PASSIVE_ABILITY = "passive";
34 const std::string FUSED_ABILITY = "fused";
35 const std::string GEO_ABILITY = "geo";
36 const std::string DEFAULT_ABILITY = "default";
37 
38 const std::string ACCESS_LOCATION = "ohos.permission.LOCATION";
39 const std::string ACCESS_APPROXIMATELY_LOCATION = "ohos.permission.APPROXIMATELY_LOCATION";
40 const std::string ACCESS_BACKGROUND_LOCATION = "ohos.permission.LOCATION_IN_BACKGROUND";
41 const std::string MANAGE_SECURE_SETTINGS = "ohos.permission.MANAGE_SECURE_SETTINGS";
42 const std::string LOCATION_DIR = "/data/service/el1/public/location/";
43 const std::string SWITCH_CONFIG_NAME = "location_switch";
44 const std::string PRIVACY_CONFIG_NAME = "location_privacy";
45 
46 const std::string BUILD_INFO = "ro.build.characteristics";
47 const int SA_NUM = 3;
48 const int DEFAULT_UID = 10001;
49 const int SYSTEM_UID = 1000;
50 const int ROOT_UID = 0;
51 static constexpr int MIN_INT_RANDOM = 10000;
52 static constexpr int MAX_INT_RANDOM = 99999;
53 
54 const int EX_HAS_REPLY_HEADER = -128;
55 const int REPLY_CODE_NO_EXCEPTION = 0;
56 const int REPLY_CODE_EXCEPTION = -1;
57 const int REPLY_CODE_MSG_UNPROCESSED = -2;
58 const int REPLY_CODE_SWITCH_OFF_EXCEPTION = -3;
59 const int REPLY_CODE_SECURITY_EXCEPTION = -4;
60 const int REPLY_CODE_UNSUPPORT = -5;
61 
62 const int INPUT_ARRAY_LEN_MAX = 100;
63 
64 const int EVENT_REGITERED_MAX_TRY_TIME = 30;
65 const int EVENT_REGITERED_MAX_TRY_INTERVAL = 5000;
66 
67 const std::string DFT_EXCEPTION_EVENT = "GnssException";
68 const int DFT_IPC_CALLING_ERROR = 201;
69 const int DFT_DAILY_LOCATION_REQUEST_COUNT = 220;
70 const int DFT_DAILY_DISTRIBUTE_SESSION_COUNT = 221;
71 const int SEC_TO_MILLI_SEC = 1000;
72 const int CONNECT_TIME_OUT = 10;
73 
74 const char DEFAULT_STRING[] = "error";
75 const std::wstring DEFAULT_WSTRING = L"error";
76 const std::u16string DEFAULT_USTRING = u"error";
77 
78 const double PI = 3.1415926;
79 const double DEGREE_PI = 180.0;
80 const double DIS_FROMLL_PARAMETER = 2;
81 const double EARTH_RADIUS = 6378137.0; // earth semimajor axis (WGS84) (m)
82 
83 static constexpr double MIN_LATITUDE = -90.0;
84 static constexpr double MAX_LATITUDE = 90.0;
85 static constexpr double MIN_LONGITUDE = -180.0;
86 static constexpr double MAX_LONGITUDE = 180.0;
87 static constexpr double DEGREE_DOUBLE_PI = 360.0;
88 static constexpr long LONG_TIME_INTERVAL = 24 * 60 * 60;
89 
90 static constexpr int PERMISSION_ACCURATE = 2;
91 static constexpr int PERMISSION_APPROXIMATELY = 1;
92 static constexpr int PERMISSION_INVALID = 0;
93 
94 static constexpr int LOCATOR_UID = 1021;
95 
96 #define CHK_PARCEL_RETURN_VALUE(ret) \
97 { \
98     if ((ret) != true) { \
99         return false; \
100     } \
101 }
102 
103 enum class ServiceRunningState {
104     STATE_NOT_START,
105     STATE_RUNNING
106 };
107 
108 enum {
109     ENABLED = 1,
110     DISABLED = 0
111 };
112 
113 enum {
114     SUCCESS = 0,
115     COMMON_ERROR = 98,
116     PARAM_IS_EMPTY = 99,
117     NOT_SUPPORTED = 100,
118     INPUT_PARAMS_ERROR = 101,
119     REVERSE_GEOCODE_ERROR,
120     GEOCODE_ERROR,
121     LOCATOR_ERROR,
122     LOCATION_SWITCH_ERROR,
123     LAST_KNOWN_LOCATION_ERROR,
124     LOCATION_REQUEST_TIMEOUT_ERROR,
125     QUERY_COUNTRY_CODE_ERROR,
126 };
127 
128 enum {
129     PERMISSION_REVOKED_OPER = 0,
130     PERMISSION_GRANTED_OPER = 1
131 };
132 
133 class CommonUtils {
134 public:
135     static void WriteInterfaceToken(const std::u16string &descriptor, MessageParcel &data);
136     static int AbilityConvertToId(const std::string ability);
137     static std::u16string GetCapabilityToString(std::string ability, uint32_t capability);
138     static std::u16string GetCapability(std::string ability);
139     static OHOS::HiviewDFX::HiLogLabel GetLabel(std::string name);
140     static sptr<IRemoteObject> GetRemoteObject(int abilityId);
141     static sptr<IRemoteObject> GetRemoteObject(int abilityId, std::string deviceId);
142     static std::string InitDeviceId();
143     static bool CheckLocationPermission(uint32_t tokenId, uint32_t firstTokenId);
144     static bool CheckApproximatelyPermission(uint32_t tokenId, uint32_t firstTokenId);
145     static bool CheckBackgroundPermission(uint32_t tokenId, uint32_t firstTokenId);
146     static bool CheckPermission(const std::string &permission, uint32_t tokenId, uint32_t firstTokenId);
147     static bool CheckSecureSettings(uint32_t tokenId, uint32_t firstTokenId);
148     static bool CheckCallingPermission(pid_t callingUid, pid_t callingPid, MessageParcel &reply);
149     static bool GetCurrentUserId(int &userId);
150     static std::string Str16ToStr8(std::u16string str);
151     static bool DoubleEqual(double a, double b);
152     static double CalDistance(const double lat1, const double lon1, const double lat2, const double lon2);
153     static double DoubleRandom(double min, double max);
154     static int IntRandom(int min, int max);
155     static int GetPermissionLevel(uint32_t tokenId, uint32_t firstTokenId);
156     static bool CheckSystemPermission(pid_t uid, uint32_t callerTokenId);
157     static bool GetBundleNameByUid(int32_t uid, std::string& bundleName);
158     static bool CheckAppInstalled(const std::string& bundleName);
159 };
160 
161 class CountDownLatch {
162 public:
CountDownLatch()163     CountDownLatch()
164     {
165         count_ = 0;
166     }
167     ~CountDownLatch() = default;
168     void Wait(int time);
169     void CountDown();
170     int GetCount();
171     void SetCount(int count);
172 private:
173     std::mutex mutex_;
174     std::condition_variable condition_;
175     std::atomic<int> count_;
176 };
177 } // namespace Location
178 } // namespace OHOS
179 #endif // COMMON_UTILS_H
180