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 LOCATION_COMMON_UTILS_H
16 #define LOCATION_COMMON_UTILS_H
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
21 #include <unistd.h>
22 #include <string>
23 
24 #include "constant_definition.h"
25 #include "iremote_object.h"
26 #include "securec.h"
27 #include "string_ex.h"
28 #include "location_log.h"
29 #include "location.h"
30 #include "app_identity.h"
31 
32 namespace OHOS {
33 namespace Location {
34 const std::string GNSS_ABILITY = "gps";
35 const std::string NETWORK_ABILITY = "network";
36 const std::string PASSIVE_ABILITY = "passive";
37 const std::string FUSED_ABILITY = "fused";
38 const std::string GEO_ABILITY = "geo";
39 const std::string DEFAULT_ABILITY = "default";
40 
41 const std::string LOCATION_DIR = "/data/service/el1/public/location/";
42 const std::string SWITCH_CONFIG_NAME = "location_switch";
43 const std::string PRIVACY_CONFIG_NAME = "location_privacy";
44 constexpr const char* NLP_SERVICE_NAME = "const.location.nlp_service_name";
45 constexpr const char* NLP_ABILITY_NAME = "const.location.nlp_ability_name";
46 constexpr const char* GEOCODE_SERVICE_NAME = "const.location.geocode_service_name";
47 constexpr const char* GEOCODE_ABILITY_NAME = "const.location.geocode_ability_name";
48 constexpr const char* SUPL_MODE_NAME = "const.location.supl_mode";
49 constexpr const char* EDM_POLICY_NAME = "persist.edm.location_policy";
50 constexpr const char* AGNSS_SERVER_ADDR = "const.location.agnss_server_addr";
51 constexpr const char* AGNSS_SERVER_PORT = "const.location.agnss_server_port";
52 constexpr const char* SETTINGS_BUNDLE_NAME = "const.location.settings_bundle_name";
53 
54 constexpr const char* BUILD_INFO = "ro.build.characteristics";
55 const int SA_NUM = 3;
56 const int DEFAULT_UID = 10001;
57 const int SYSTEM_UID = 1000;
58 static constexpr int MIN_INT_RANDOM = 10000;
59 static constexpr int MAX_INT_RANDOM = 99999;
60 
61 const int EX_HAS_REPLY_HEADER = -128;
62 const int REPLY_CODE_NO_EXCEPTION = 0;
63 const int REPLY_CODE_EXCEPTION = -1;
64 const int REPLY_CODE_MSG_UNPROCESSED = -2;
65 const int REPLY_CODE_SWITCH_OFF_EXCEPTION = -3;
66 const int REPLY_CODE_SECURITY_EXCEPTION = -4;
67 const int REPLY_CODE_UNSUPPORT = -5;
68 
69 const int INPUT_ARRAY_LEN_MAX = 100;
70 const int MAX_BUFF_SIZE = 100;
71 
72 const int EVENT_REGITERED_MAX_TRY_TIME = 30;
73 const int EVENT_REGITERED_MAX_TRY_INTERVAL = 5000;
74 
75 const std::string DFT_EXCEPTION_EVENT = "GnssException";
76 const int DFT_IPC_CALLING_ERROR = 201;
77 const int DFT_DAILY_LOCATION_REQUEST_COUNT = 220;
78 const int DFT_DAILY_DISTRIBUTE_SESSION_COUNT = 221;
79 const int MILLI_PER_SEC = 1000;
80 const int MICRO_PER_MILLI = 1000;
81 const int CONNECT_TIME_OUT = 10;
82 const int DISCONNECT_TIME_OUT = 10;
83 
84 const char DEFAULT_STRING[] = "error";
85 const std::wstring DEFAULT_WSTRING = L"error";
86 const std::u16string DEFAULT_USTRING = u"error";
87 
88 const double PI = 3.1415926;
89 const double DEGREE_PI = 180.0;
90 const double DIS_FROMLL_PARAMETER = 2;
91 const double EARTH_RADIUS = 6378137.0; // earth semimajor axis (WGS84) (m)
92 
93 static constexpr double MIN_LATITUDE = -90.0;
94 static constexpr double MAX_LATITUDE = 90.0;
95 static constexpr double MIN_LONGITUDE = -180.0;
96 static constexpr double MAX_LONGITUDE = 180.0;
97 static constexpr double DEGREE_DOUBLE_PI = 360.0;
98 static constexpr long LONG_TIME_INTERVAL = 24 * 60 * 60;
99 static constexpr int32_t LOCATION_LOADSA_TIMEOUT_MS = 5000;
100 
101 static constexpr int LOCATOR_UID = 1021;
102 static constexpr int MAXIMUM_INTERATION = 100;
103 static constexpr int MAXIMUM_CACHE_LOCATIONS = 1000;
104 static constexpr int MAXIMUM_LOCATING_REQUIRED_DATAS = 200;
105 static constexpr int MAC_LEN = 6;
106 static constexpr int DEFAULT_CODE = 0;
107 static constexpr int MAX_TRANSITION_SIZE = 3;
108 static constexpr int MAX_NOTIFICATION_REQUEST_LIST_SIZE = 3;
109 
110 #define CHK_PARCEL_RETURN_VALUE(ret) \
111 { \
112     if ((ret) != true) { \
113         return false; \
114     } \
115 }
116 
117 #define CHK_ERRORCODE_RETURN_VALUE(ret) \
118 {                                       \
119     if ((ret) != ERRCODE_SUCCESS) {     \
120         return (ret);                   \
121     }                                   \
122 }
123 
124 enum class ServiceRunningState {
125     STATE_NOT_START,
126     STATE_RUNNING
127 };
128 
129 enum {
130     DISABLED = 0,
131     ENABLED = 1,
132     DEFAULT_SWITCH_STATE = 2
133 };
134 
135 enum {
136     SUCCESS = 0,
137     COMMON_ERROR = 98,
138     PARAM_IS_EMPTY = 99,
139     NOT_SUPPORTED = 100,
140     INPUT_PARAMS_ERROR = 101,
141     REVERSE_GEOCODE_ERROR,
142     GEOCODE_ERROR,
143     LOCATOR_ERROR,
144     LOCATION_SWITCH_ERROR,
145     LAST_KNOWN_LOCATION_ERROR,
146     LOCATION_REQUEST_TIMEOUT_ERROR,
147     QUERY_COUNTRY_CODE_ERROR,
148 };
149 
150 enum {
151     PERMISSION_REVOKED_OPER = 0,
152     PERMISSION_GRANTED_OPER = 1
153 };
154 
155 class CommonUtils {
156 public:
157     static void WriteInterfaceToken(const std::u16string &descriptor, MessageParcel &data);
158     static int AbilityConvertToId(const std::string ability);
159     static std::u16string GetCapabilityToString(std::string ability, uint32_t capability);
160     static std::u16string GetCapability(std::string ability);
161     static OHOS::HiviewDFX::HiLogLabel GetLabel(std::string name);
162     static sptr<IRemoteObject> GetRemoteObject(int abilityId);
163     static sptr<IRemoteObject> GetRemoteObject(int abilityId, std::string deviceId);
164     static std::string InitDeviceId();
165     static bool GetCurrentUserId(int &userId);
166     static std::string Str16ToStr8(std::u16string str);
167     static bool DoubleEqual(double a, double b);
168     static double CalDistance(const double lat1, const double lon1, const double lat2, const double lon2);
169     static double DoubleRandom(double min, double max);
170     static int IntRandom(int min, int max);
171     static bool GetBundleNameByUid(int32_t uid, std::string& bundleName);
172     static bool CheckAppInstalled(const std::string& bundleName);
173     static int64_t GetCurrentTime();
174     static int64_t GetCurrentTimeStamp();
175     static int64_t GetCurrentTimeStampMs();
176     static std::vector<std::string> Split(std::string str, std::string pattern);
177     static errno_t GetMacArray(const std::string& strMac, uint8_t mac[MAC_LEN]);
178     static unsigned char ConvertStringToDigit(std::string str);
179     static bool GetStringParameter(const std::string& type, std::string& value);
180     static bool GetEdmPolicy(std::string& name);
181     static std::string GenerateUuid();
182     static bool CheckAppForUser(int32_t uid);
183     static int64_t GetSinceBootTime();
184     static bool GetAllUserId(std::vector<int>& activeIds);
185     static bool IsAppBelongCurrentAccount(AppIdentity &identity);
186 };
187 
188 class CountDownLatch {
189 public:
CountDownLatch()190     CountDownLatch()
191     {
192         count_ = 0;
193     }
194     ~CountDownLatch() = default;
195     void Wait(int time);
196     void CountDown();
197     int GetCount();
198     void SetCount(int count);
199 private:
200     std::mutex mutex_;
201     std::condition_variable condition_;
202     std::atomic<int> count_;
203 };
204 } // namespace Location
205 } // namespace OHOS
206 #endif // COMMON_UTILS_H
207