15c735df2Sopenharmony_ci/*
25c735df2Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
35c735df2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45c735df2Sopenharmony_ci * you may not use this file except in compliance with the License.
55c735df2Sopenharmony_ci * You may obtain a copy of the License at
65c735df2Sopenharmony_ci *
75c735df2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85c735df2Sopenharmony_ci *
95c735df2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105c735df2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115c735df2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125c735df2Sopenharmony_ci * See the License for the specific language governing permissions and
135c735df2Sopenharmony_ci * limitations under the License.
145c735df2Sopenharmony_ci */
155c735df2Sopenharmony_ci#ifndef LOCATION_COMMON_UTILS_H
165c735df2Sopenharmony_ci#define LOCATION_COMMON_UTILS_H
175c735df2Sopenharmony_ci
185c735df2Sopenharmony_ci#include <atomic>
195c735df2Sopenharmony_ci#include <condition_variable>
205c735df2Sopenharmony_ci#include <mutex>
215c735df2Sopenharmony_ci#include <unistd.h>
225c735df2Sopenharmony_ci#include <string>
235c735df2Sopenharmony_ci
245c735df2Sopenharmony_ci#include "constant_definition.h"
255c735df2Sopenharmony_ci#include "iremote_object.h"
265c735df2Sopenharmony_ci#include "securec.h"
275c735df2Sopenharmony_ci#include "string_ex.h"
285c735df2Sopenharmony_ci#include "location_log.h"
295c735df2Sopenharmony_ci#include "location.h"
305c735df2Sopenharmony_ci#include "app_identity.h"
315c735df2Sopenharmony_ci
325c735df2Sopenharmony_cinamespace OHOS {
335c735df2Sopenharmony_cinamespace Location {
345c735df2Sopenharmony_ciconst std::string GNSS_ABILITY = "gps";
355c735df2Sopenharmony_ciconst std::string NETWORK_ABILITY = "network";
365c735df2Sopenharmony_ciconst std::string PASSIVE_ABILITY = "passive";
375c735df2Sopenharmony_ciconst std::string FUSED_ABILITY = "fused";
385c735df2Sopenharmony_ciconst std::string GEO_ABILITY = "geo";
395c735df2Sopenharmony_ciconst std::string DEFAULT_ABILITY = "default";
405c735df2Sopenharmony_ci
415c735df2Sopenharmony_ciconst std::string LOCATION_DIR = "/data/service/el1/public/location/";
425c735df2Sopenharmony_ciconst std::string SWITCH_CONFIG_NAME = "location_switch";
435c735df2Sopenharmony_ciconst std::string PRIVACY_CONFIG_NAME = "location_privacy";
445c735df2Sopenharmony_ciconstexpr const char* NLP_SERVICE_NAME = "const.location.nlp_service_name";
455c735df2Sopenharmony_ciconstexpr const char* NLP_ABILITY_NAME = "const.location.nlp_ability_name";
465c735df2Sopenharmony_ciconstexpr const char* GEOCODE_SERVICE_NAME = "const.location.geocode_service_name";
475c735df2Sopenharmony_ciconstexpr const char* GEOCODE_ABILITY_NAME = "const.location.geocode_ability_name";
485c735df2Sopenharmony_ciconstexpr const char* SUPL_MODE_NAME = "const.location.supl_mode";
495c735df2Sopenharmony_ciconstexpr const char* EDM_POLICY_NAME = "persist.edm.location_policy";
505c735df2Sopenharmony_ciconstexpr const char* AGNSS_SERVER_ADDR = "const.location.agnss_server_addr";
515c735df2Sopenharmony_ciconstexpr const char* AGNSS_SERVER_PORT = "const.location.agnss_server_port";
525c735df2Sopenharmony_ciconstexpr const char* SETTINGS_BUNDLE_NAME = "const.location.settings_bundle_name";
535c735df2Sopenharmony_ci
545c735df2Sopenharmony_ciconstexpr const char* BUILD_INFO = "ro.build.characteristics";
555c735df2Sopenharmony_ciconst int SA_NUM = 3;
565c735df2Sopenharmony_ciconst int DEFAULT_UID = 10001;
575c735df2Sopenharmony_ciconst int SYSTEM_UID = 1000;
585c735df2Sopenharmony_cistatic constexpr int MIN_INT_RANDOM = 10000;
595c735df2Sopenharmony_cistatic constexpr int MAX_INT_RANDOM = 99999;
605c735df2Sopenharmony_ci
615c735df2Sopenharmony_ciconst int EX_HAS_REPLY_HEADER = -128;
625c735df2Sopenharmony_ciconst int REPLY_CODE_NO_EXCEPTION = 0;
635c735df2Sopenharmony_ciconst int REPLY_CODE_EXCEPTION = -1;
645c735df2Sopenharmony_ciconst int REPLY_CODE_MSG_UNPROCESSED = -2;
655c735df2Sopenharmony_ciconst int REPLY_CODE_SWITCH_OFF_EXCEPTION = -3;
665c735df2Sopenharmony_ciconst int REPLY_CODE_SECURITY_EXCEPTION = -4;
675c735df2Sopenharmony_ciconst int REPLY_CODE_UNSUPPORT = -5;
685c735df2Sopenharmony_ci
695c735df2Sopenharmony_ciconst int INPUT_ARRAY_LEN_MAX = 100;
705c735df2Sopenharmony_ciconst int MAX_BUFF_SIZE = 100;
715c735df2Sopenharmony_ci
725c735df2Sopenharmony_ciconst int EVENT_REGITERED_MAX_TRY_TIME = 30;
735c735df2Sopenharmony_ciconst int EVENT_REGITERED_MAX_TRY_INTERVAL = 5000;
745c735df2Sopenharmony_ci
755c735df2Sopenharmony_ciconst std::string DFT_EXCEPTION_EVENT = "GnssException";
765c735df2Sopenharmony_ciconst int DFT_IPC_CALLING_ERROR = 201;
775c735df2Sopenharmony_ciconst int DFT_DAILY_LOCATION_REQUEST_COUNT = 220;
785c735df2Sopenharmony_ciconst int DFT_DAILY_DISTRIBUTE_SESSION_COUNT = 221;
795c735df2Sopenharmony_ciconst int MILLI_PER_SEC = 1000;
805c735df2Sopenharmony_ciconst int MICRO_PER_MILLI = 1000;
815c735df2Sopenharmony_ciconst int CONNECT_TIME_OUT = 10;
825c735df2Sopenharmony_ciconst int DISCONNECT_TIME_OUT = 10;
835c735df2Sopenharmony_ci
845c735df2Sopenharmony_ciconst char DEFAULT_STRING[] = "error";
855c735df2Sopenharmony_ciconst std::wstring DEFAULT_WSTRING = L"error";
865c735df2Sopenharmony_ciconst std::u16string DEFAULT_USTRING = u"error";
875c735df2Sopenharmony_ci
885c735df2Sopenharmony_ciconst double PI = 3.1415926;
895c735df2Sopenharmony_ciconst double DEGREE_PI = 180.0;
905c735df2Sopenharmony_ciconst double DIS_FROMLL_PARAMETER = 2;
915c735df2Sopenharmony_ciconst double EARTH_RADIUS = 6378137.0; // earth semimajor axis (WGS84) (m)
925c735df2Sopenharmony_ci
935c735df2Sopenharmony_cistatic constexpr double MIN_LATITUDE = -90.0;
945c735df2Sopenharmony_cistatic constexpr double MAX_LATITUDE = 90.0;
955c735df2Sopenharmony_cistatic constexpr double MIN_LONGITUDE = -180.0;
965c735df2Sopenharmony_cistatic constexpr double MAX_LONGITUDE = 180.0;
975c735df2Sopenharmony_cistatic constexpr double DEGREE_DOUBLE_PI = 360.0;
985c735df2Sopenharmony_cistatic constexpr long LONG_TIME_INTERVAL = 24 * 60 * 60;
995c735df2Sopenharmony_cistatic constexpr int32_t LOCATION_LOADSA_TIMEOUT_MS = 5000;
1005c735df2Sopenharmony_ci
1015c735df2Sopenharmony_cistatic constexpr int LOCATOR_UID = 1021;
1025c735df2Sopenharmony_cistatic constexpr int MAXIMUM_INTERATION = 100;
1035c735df2Sopenharmony_cistatic constexpr int MAXIMUM_CACHE_LOCATIONS = 1000;
1045c735df2Sopenharmony_cistatic constexpr int MAXIMUM_LOCATING_REQUIRED_DATAS = 200;
1055c735df2Sopenharmony_cistatic constexpr int MAC_LEN = 6;
1065c735df2Sopenharmony_cistatic constexpr int DEFAULT_CODE = 0;
1075c735df2Sopenharmony_cistatic constexpr int MAX_TRANSITION_SIZE = 3;
1085c735df2Sopenharmony_cistatic constexpr int MAX_NOTIFICATION_REQUEST_LIST_SIZE = 3;
1095c735df2Sopenharmony_ci
1105c735df2Sopenharmony_ci#define CHK_PARCEL_RETURN_VALUE(ret) \
1115c735df2Sopenharmony_ci{ \
1125c735df2Sopenharmony_ci    if ((ret) != true) { \
1135c735df2Sopenharmony_ci        return false; \
1145c735df2Sopenharmony_ci    } \
1155c735df2Sopenharmony_ci}
1165c735df2Sopenharmony_ci
1175c735df2Sopenharmony_ci#define CHK_ERRORCODE_RETURN_VALUE(ret) \
1185c735df2Sopenharmony_ci{                                       \
1195c735df2Sopenharmony_ci    if ((ret) != ERRCODE_SUCCESS) {     \
1205c735df2Sopenharmony_ci        return (ret);                   \
1215c735df2Sopenharmony_ci    }                                   \
1225c735df2Sopenharmony_ci}
1235c735df2Sopenharmony_ci
1245c735df2Sopenharmony_cienum class ServiceRunningState {
1255c735df2Sopenharmony_ci    STATE_NOT_START,
1265c735df2Sopenharmony_ci    STATE_RUNNING
1275c735df2Sopenharmony_ci};
1285c735df2Sopenharmony_ci
1295c735df2Sopenharmony_cienum {
1305c735df2Sopenharmony_ci    DISABLED = 0,
1315c735df2Sopenharmony_ci    ENABLED = 1,
1325c735df2Sopenharmony_ci    DEFAULT_SWITCH_STATE = 2
1335c735df2Sopenharmony_ci};
1345c735df2Sopenharmony_ci
1355c735df2Sopenharmony_cienum {
1365c735df2Sopenharmony_ci    SUCCESS = 0,
1375c735df2Sopenharmony_ci    COMMON_ERROR = 98,
1385c735df2Sopenharmony_ci    PARAM_IS_EMPTY = 99,
1395c735df2Sopenharmony_ci    NOT_SUPPORTED = 100,
1405c735df2Sopenharmony_ci    INPUT_PARAMS_ERROR = 101,
1415c735df2Sopenharmony_ci    REVERSE_GEOCODE_ERROR,
1425c735df2Sopenharmony_ci    GEOCODE_ERROR,
1435c735df2Sopenharmony_ci    LOCATOR_ERROR,
1445c735df2Sopenharmony_ci    LOCATION_SWITCH_ERROR,
1455c735df2Sopenharmony_ci    LAST_KNOWN_LOCATION_ERROR,
1465c735df2Sopenharmony_ci    LOCATION_REQUEST_TIMEOUT_ERROR,
1475c735df2Sopenharmony_ci    QUERY_COUNTRY_CODE_ERROR,
1485c735df2Sopenharmony_ci};
1495c735df2Sopenharmony_ci
1505c735df2Sopenharmony_cienum {
1515c735df2Sopenharmony_ci    PERMISSION_REVOKED_OPER = 0,
1525c735df2Sopenharmony_ci    PERMISSION_GRANTED_OPER = 1
1535c735df2Sopenharmony_ci};
1545c735df2Sopenharmony_ci
1555c735df2Sopenharmony_ciclass CommonUtils {
1565c735df2Sopenharmony_cipublic:
1575c735df2Sopenharmony_ci    static void WriteInterfaceToken(const std::u16string &descriptor, MessageParcel &data);
1585c735df2Sopenharmony_ci    static int AbilityConvertToId(const std::string ability);
1595c735df2Sopenharmony_ci    static std::u16string GetCapabilityToString(std::string ability, uint32_t capability);
1605c735df2Sopenharmony_ci    static std::u16string GetCapability(std::string ability);
1615c735df2Sopenharmony_ci    static OHOS::HiviewDFX::HiLogLabel GetLabel(std::string name);
1625c735df2Sopenharmony_ci    static sptr<IRemoteObject> GetRemoteObject(int abilityId);
1635c735df2Sopenharmony_ci    static sptr<IRemoteObject> GetRemoteObject(int abilityId, std::string deviceId);
1645c735df2Sopenharmony_ci    static std::string InitDeviceId();
1655c735df2Sopenharmony_ci    static bool GetCurrentUserId(int &userId);
1665c735df2Sopenharmony_ci    static std::string Str16ToStr8(std::u16string str);
1675c735df2Sopenharmony_ci    static bool DoubleEqual(double a, double b);
1685c735df2Sopenharmony_ci    static double CalDistance(const double lat1, const double lon1, const double lat2, const double lon2);
1695c735df2Sopenharmony_ci    static double DoubleRandom(double min, double max);
1705c735df2Sopenharmony_ci    static int IntRandom(int min, int max);
1715c735df2Sopenharmony_ci    static bool GetBundleNameByUid(int32_t uid, std::string& bundleName);
1725c735df2Sopenharmony_ci    static bool CheckAppInstalled(const std::string& bundleName);
1735c735df2Sopenharmony_ci    static int64_t GetCurrentTime();
1745c735df2Sopenharmony_ci    static int64_t GetCurrentTimeStamp();
1755c735df2Sopenharmony_ci    static int64_t GetCurrentTimeStampMs();
1765c735df2Sopenharmony_ci    static std::vector<std::string> Split(std::string str, std::string pattern);
1775c735df2Sopenharmony_ci    static errno_t GetMacArray(const std::string& strMac, uint8_t mac[MAC_LEN]);
1785c735df2Sopenharmony_ci    static unsigned char ConvertStringToDigit(std::string str);
1795c735df2Sopenharmony_ci    static bool GetStringParameter(const std::string& type, std::string& value);
1805c735df2Sopenharmony_ci    static bool GetEdmPolicy(std::string& name);
1815c735df2Sopenharmony_ci    static std::string GenerateUuid();
1825c735df2Sopenharmony_ci    static bool CheckAppForUser(int32_t uid);
1835c735df2Sopenharmony_ci    static int64_t GetSinceBootTime();
1845c735df2Sopenharmony_ci    static bool GetAllUserId(std::vector<int>& activeIds);
1855c735df2Sopenharmony_ci    static bool IsAppBelongCurrentAccount(AppIdentity &identity);
1865c735df2Sopenharmony_ci};
1875c735df2Sopenharmony_ci
1885c735df2Sopenharmony_ciclass CountDownLatch {
1895c735df2Sopenharmony_cipublic:
1905c735df2Sopenharmony_ci    CountDownLatch()
1915c735df2Sopenharmony_ci    {
1925c735df2Sopenharmony_ci        count_ = 0;
1935c735df2Sopenharmony_ci    }
1945c735df2Sopenharmony_ci    ~CountDownLatch() = default;
1955c735df2Sopenharmony_ci    void Wait(int time);
1965c735df2Sopenharmony_ci    void CountDown();
1975c735df2Sopenharmony_ci    int GetCount();
1985c735df2Sopenharmony_ci    void SetCount(int count);
1995c735df2Sopenharmony_ciprivate:
2005c735df2Sopenharmony_ci    std::mutex mutex_;
2015c735df2Sopenharmony_ci    std::condition_variable condition_;
2025c735df2Sopenharmony_ci    std::atomic<int> count_;
2035c735df2Sopenharmony_ci};
2045c735df2Sopenharmony_ci} // namespace Location
2055c735df2Sopenharmony_ci} // namespace OHOS
2065c735df2Sopenharmony_ci#endif // COMMON_UTILS_H
207