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