mirror of
https://github.com/openharmony/device_manager.git
synced 2026-07-18 16:24:27 -04:00
新device manager工程代码上库
Signed-off-by: cao_liu_chao <caochao12@huawei.com>
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_CONSTANTS_H
|
||||
#define OHOS_DM_CONSTANTS_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
const std::string DM_PKG_NAME = "ohos.distributedhardware.devicemanager";
|
||||
const std::string DM_SESSION_NAME = "ohos.distributedhardware.devicemanager.resident";
|
||||
|
||||
const std::string DISCOVER_STATUS_KEY = "persist.distributed_hardware.device_manager.discover_status";
|
||||
const std::string DISCOVER_STATUS_ON = "1";
|
||||
const std::string DISCOVER_STATUS_OFF = "0";
|
||||
const std::string AUTH_LOAD_JSON_KEY = "devicemanager_auth_components";
|
||||
const std::string ADAPTER_LOAD_JSON_KEY = "devicemanager_adapter_components";
|
||||
const std::string AUTH_JSON_TYPE_KEY = "AUTHENTICATE";
|
||||
const std::string CPYPTO_JSON_TYPE_KEY = "CPYPTO";
|
||||
const std::string PROFILE_JSON_TYPE_KEY = "PROFILE";
|
||||
const std::string DECISION_JSON_TYPE_KEY = "DECISION";
|
||||
|
||||
const int32_t DM_NAPI_SUBSCRIBE_CAPABILITY_DDMP = 0;
|
||||
const int32_t DM_NAPI_SUBSCRIBE_CAPABILITY_OSD = 1;
|
||||
const int32_t LOG_MAX_LEN = 512;
|
||||
const int32_t MIN_PIN_TOKEN = 10000000;
|
||||
const int32_t MAX_PIN_TOKEN = 90000000;
|
||||
const int32_t MIN_PIN_CODE = 100000;
|
||||
const int32_t MAX_PIN_CODE = 999999;
|
||||
const int32_t DISCOVER_STATUS_LEN = 20;
|
||||
const int32_t COMMON_CALLBACK_MAX_SIZE = 200;
|
||||
// const int32_t TOKEN_LEN = 9;
|
||||
|
||||
enum {
|
||||
DM_OK = 0,
|
||||
DM_FAILED = 1000,
|
||||
DM_TIME_OUT,
|
||||
DM_NOT_INIT,
|
||||
DM_INT_MULTIPLE,
|
||||
DM_INIT_FAILED,
|
||||
DM_UNINIT_FAILED,
|
||||
DM_POINT_NULL,
|
||||
DM_INPUT_PARA_EMPTY,
|
||||
DM_NOT_SYSTEM_APP,
|
||||
DM_INVALID_VALUE,
|
||||
DM_FLATTEN_OBJECT,
|
||||
DM_MALLOC_ERROR,
|
||||
DM_COPY_FAILED,
|
||||
DM_WRITE_FAILED,
|
||||
DM_DISCOVERY_FAILED,
|
||||
DM_MAKE_SHARED_FAIL,
|
||||
DM_SERVICE_NOT_READY,
|
||||
DM_DEVICE_ALREADY_TRUSTED,
|
||||
DM_IPC_FAILED = 2000,
|
||||
DM_IPC_TRANSACTION_FAILED,
|
||||
DM_IPC_FLATTEN_OBJECT,
|
||||
DM_IPC_COPY_FAILED,
|
||||
DM_IPC_SEND_REQUEST_FAILED,
|
||||
DM_IPC_NOT_REGISTER_FUNC,
|
||||
DM_IPC_RESPOND_ERROR,
|
||||
DM_DISCOVERY_REPEATED,
|
||||
DM_AUTH_NOT_SUPPORT,
|
||||
DM_AUTH_BUSINESS_BUSY,
|
||||
DM_AUTH_INPUT_FAILED,
|
||||
DM_AUTH_STATE_FAILED,
|
||||
DM_AUTH_DEVICE_AUTHED,
|
||||
DM_AUTH_OPEN_SESSION_FAILED,
|
||||
DM_AUTH_PEER_REJECT,
|
||||
DM_AUTH_NOT_AUTH,
|
||||
DM_SOFTBUS_FAILED = 3000,
|
||||
DM_SOFTBUS_CREATE_SESSION_SERVER_FAILED,
|
||||
DM_HICHAIN_FAILED = 4000,
|
||||
DM_HICHAIN_GROUP_CREATE_FAILED,
|
||||
DM_HICHAIN_MEMBER_ADD_FAILED,
|
||||
DM_HICHAIN_CREATE_CHANNEL_FAILED,
|
||||
};
|
||||
const std::string TARGET_PKG_NAME_KEY = "targetPkgName";
|
||||
const std::string HOST_PKG_NAME_KEY = "hostPackageName";
|
||||
const std::string TAG_REQUESTER = "REQUESTER";
|
||||
const std::string TAG_TOKEN = "TOKEN";
|
||||
const std::string TAG_HOST = "HOST";
|
||||
const std::string TAG_TARGET = "TARGET";
|
||||
const std::string TAG_VISIBILITY = "VISIBILITY";
|
||||
const std::string TAG_GROUPIDS = "GROUPIDLIST";
|
||||
const std::string TAG_REPLY = "REPLY";
|
||||
const std::string TAG_NET_ID = "NETID";
|
||||
const std::string TAG_GROUP_ID = "groupId";
|
||||
const std::string TAG_GROUP_NAME = "GROUPNAME";
|
||||
const std::string TAG_REQUEST_ID = "REQUESTID";
|
||||
const std::string TAG_DEVICE_ID = "DEVICEID";
|
||||
const std::string TAG_LOCAL_DEVICE_ID = "LOCALDEVICEID";
|
||||
const std::string TAG_DEVICE_TYPE = "DEVICETYPE";
|
||||
const std::string TAG_APP_NAME = "APPNAME";
|
||||
const std::string TAG_APP_DESCRIPTION = "APPDESC";
|
||||
const std::string TAG_APP_ICON = "APPICON";
|
||||
const std::string TAG_APP_THUMBNAIL = "APPTHUM";
|
||||
const std::string TAG_INDEX = "INDEX";
|
||||
const std::string TAG_SLICE_NUM = "SLICE";
|
||||
const std::string TAG_THUMBNAIL_SIZE = "THUMSIZE";
|
||||
const std::string TAG_AUTH_TYPE = "AUTHTYPE";
|
||||
const std::string TAG_CRYPTO_SUPPORT = "CRYPTOSUPPORT";
|
||||
const std::string TAG_CRYPTO_NAME = "CRYPTONAME";
|
||||
const std::string TAG_CRYPTO_VERSION = "CRYPTOVERSION";
|
||||
const std::string TAG_VER = "ITF_VER";
|
||||
const std::string TAG_TYPE = "MSG_TYPE";
|
||||
const std::string DM_ITF_VER_1_0 = "1.0";
|
||||
const std::string DM_ITF_VER = "1.1";
|
||||
const std::string TAG = "DM_MSG_CODEC";
|
||||
const std::string APP_NAME_KEY = "appName";
|
||||
const std::string APP_DESCRIPTION_KEY = "appDescription";
|
||||
const std::string APP_ICON_KEY = "appIcon";
|
||||
const std::string APP_THUMBNAIL_KEY = "appThumbnailKey";
|
||||
const std::string APP_THUMBNAIL_LEN = "appThumbnailLen";
|
||||
const std::string APP_THUMBNAIL = "appThumbnail";
|
||||
const std::string CANCEL_DISPLAY_KEY = "cancelPinCodeDisplay";
|
||||
const int32_t MSG_MAX_SIZE = 45 * 1024;
|
||||
const int32_t AUTH_REPLY_ACCEPT = 0;
|
||||
const int32_t ENCRYPT_TAG_LEN = 32;
|
||||
|
||||
// pin
|
||||
const int32_t DISPLAY_OWNER_SYSTEM = 0;
|
||||
const int32_t DISPLAY_OWNER_OTHER = 1;
|
||||
const int32_t BUSINESS_FA_MIRGRATION = 0;
|
||||
const int32_t BUSINESS_RESOURCE_ACCESS = 1;
|
||||
|
||||
// json
|
||||
const std::string AUTH_TYPE = "authType";
|
||||
const std::string TOKEN = "token";
|
||||
const std::string PIN_TOKEN = "pinToken";
|
||||
const std::string PIN_CODE_KEY = "pinCode";
|
||||
const int32_t AUTH_TYPE_PIN = 1;
|
||||
const int32_t AUTH_TYPE_SCAN = 2;
|
||||
const int32_t AUTH_TYPE_TOUCH = 3;
|
||||
const int32_t DEFAULT_PIN_CODE = 0;
|
||||
const int32_t DEFAULT_PIN_TOKEN = 0;
|
||||
|
||||
// Softbus
|
||||
const int32_t SOFTBUS_CHECK_INTERVAL = 100000; // 100ms
|
||||
const uint32_t SOFTBUS_SUBSCRIBE_ID_PREFIX_LEN = 16;
|
||||
const int32_t SOFTBUS_SUBSCRIBE_ID_MASK = 0x0000FFFF;
|
||||
const int32_t SOFTBUS_DISCOVER_DEVICE_INFO_MAX_SIZE = 20;
|
||||
const int32_t AUTH_SESSION_SIDE_SERVER = 0;
|
||||
const int32_t AUTH_SESSION_SIDE_CLIENT = 1;
|
||||
const static char *DM_CAPABILITY_OSD = "osdCapability";
|
||||
|
||||
// HiChain
|
||||
const int32_t DEVICE_UUID_LENGTH = 65;
|
||||
const int32_t GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP = 1;
|
||||
const int32_t GROUP_TYPE_PEER_TO_PEER_GROUP = 256;
|
||||
const int32_t GROUP_VISIBILITY_PUBLIC = -1;
|
||||
const int64_t MIN_REQUEST_ID = 1000000000;
|
||||
const int64_t MAX_REQUEST_ID = 9999999999;
|
||||
const int32_t FIELD_EXPIRE_TIME_VALUE = 7;
|
||||
const int32_t GROUP_VISIBILITY_IS_PUBLIC = -1;
|
||||
const int32_t GROUP_VISIBILITY_IS_PRIVATE = 0;
|
||||
const std::string DEVICE_ID = "DEVICE_ID";
|
||||
const std::string WIFI_IP = "WIFI_IP";
|
||||
const std::string WIFI_PORT = "WIFI_PORT";
|
||||
const std::string BR_MAC = "BR_MAC";
|
||||
const std::string BLE_MAC = "BLE_MAC";
|
||||
const std::string ETH_IP = "ETH_IP";
|
||||
const std::string ETH_PORT = "ETH_PORT";
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DEVICE_MANAGER_IPC_GET_DM_FA_PARAM_RSP_H
|
||||
#define OHOS_DEVICE_MANAGER_IPC_GET_DM_FA_PARAM_RSP_H
|
||||
|
||||
#include "dm_device_info.h"
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcGetDmFaParamRsp : public IpcRsp {
|
||||
DECLARE_IPC_MODEL(IpcGetDmFaParamRsp);
|
||||
|
||||
public:
|
||||
const DmAuthParam GetDmAuthParam() const
|
||||
{
|
||||
return dmFaParam_;
|
||||
}
|
||||
|
||||
void SetDmAuthParam(const DmAuthParam &dmFaParam)
|
||||
{
|
||||
dmFaParam_ = dmFaParam;
|
||||
}
|
||||
|
||||
private:
|
||||
DmAuthParam dmFaParam_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DEVICE_MANAGER_IPC_GET_DM_FA_PARAM_RSP_H
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_REQ_H
|
||||
#define OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_REQ_H
|
||||
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcGetInfoByNetWorkReq : public IpcReq {
|
||||
DECLARE_IPC_MODEL(IpcGetInfoByNetWorkReq);
|
||||
|
||||
public:
|
||||
const std::string GetNetWorkId() const
|
||||
{
|
||||
return netWorkId_;
|
||||
}
|
||||
|
||||
void SetNetWorkId(const std::string &netWorkId)
|
||||
{
|
||||
netWorkId_ = netWorkId;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string netWorkId_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_REQ_H
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_RSP_H
|
||||
#define OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_RSP_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "ipc_rsp.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcGetInfoByNetWorkRsp : public IpcRsp {
|
||||
DECLARE_IPC_MODEL(IpcGetInfoByNetWorkRsp);
|
||||
|
||||
public:
|
||||
const std::string GetUdid() const
|
||||
{
|
||||
return udid_;
|
||||
}
|
||||
|
||||
void SetUdid(const std::string &udid)
|
||||
{
|
||||
udid_ = udid;
|
||||
}
|
||||
const std::string GetUuid() const
|
||||
{
|
||||
return uuid_;
|
||||
}
|
||||
|
||||
void SetUuid(const std::string &uuid)
|
||||
{
|
||||
uuid_ = uuid;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string udid_;
|
||||
std::string uuid_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DEVICE_MANAGER_IPC_GET_INFO_BY_NETWORK_RSP_H
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_IPC_GET_LOCAL_DEVICE_INFO_RSP_H
|
||||
#define OHOS_DM_IPC_GET_LOCAL_DEVICE_INFO_RSP_H
|
||||
|
||||
#include "dm_device_info.h"
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcGetLocalDeviceInfoRsp : public IpcRsp {
|
||||
DECLARE_IPC_MODEL(IpcGetLocalDeviceInfoRsp);
|
||||
|
||||
public:
|
||||
const DmDeviceInfo &GetLocalDeviceInfo() const
|
||||
{
|
||||
return localDeviceInfo_;
|
||||
}
|
||||
|
||||
void SetLocalDeviceInfo(const DmDeviceInfo &localDeviceInfo)
|
||||
{
|
||||
localDeviceInfo_ = localDeviceInfo;
|
||||
}
|
||||
|
||||
private:
|
||||
DmDeviceInfo localDeviceInfo_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_IPC_GET_LOCAL_DEVICE_INFO_RSP_H
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_IPC_NOTIFY_CHECK_AUTH_RESULT_REQ_H
|
||||
#define OHOS_DM_IPC_NOTIFY_CHECK_AUTH_RESULT_REQ_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcNotifyVerifyAuthResultReq : public IpcReq {
|
||||
DECLARE_IPC_MODEL(IpcNotifyVerifyAuthResultReq);
|
||||
|
||||
public:
|
||||
std::string GetDeviceId() const
|
||||
{
|
||||
return deviceId_;
|
||||
}
|
||||
|
||||
void SetDeviceId(const std::string &deviceId)
|
||||
{
|
||||
deviceId_ = deviceId;
|
||||
}
|
||||
|
||||
int32_t GetResult() const
|
||||
{
|
||||
return result_;
|
||||
}
|
||||
|
||||
void SetResult(int32_t result)
|
||||
{
|
||||
result_ = result;
|
||||
}
|
||||
|
||||
int32_t GetFlag() const
|
||||
{
|
||||
return flag_;
|
||||
}
|
||||
|
||||
void SetFlag(int32_t flag)
|
||||
{
|
||||
flag_ = flag;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string deviceId_;
|
||||
int32_t result_{0};
|
||||
int32_t flag_{0};
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_IPC_NOTIFY_CHECK_AUTH_RESULT_REQ_H
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_IPC_UNAUTHENTICATE_DEVICE_REQ_H
|
||||
#define OHOS_DM_IPC_UNAUTHENTICATE_DEVICE_REQ_H
|
||||
|
||||
#include "dm_device_info.h"
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcUnAuthenticateDeviceReq : public IpcReq {
|
||||
DECLARE_IPC_MODEL(IpcUnAuthenticateDeviceReq);
|
||||
|
||||
public:
|
||||
const DmDeviceInfo &GetDeviceInfo() const
|
||||
{
|
||||
return deviceInfo_;
|
||||
}
|
||||
|
||||
void SetDeviceInfo(const DmDeviceInfo &deviceInfo)
|
||||
{
|
||||
deviceInfo_ = deviceInfo;
|
||||
}
|
||||
|
||||
private:
|
||||
DmDeviceInfo deviceInfo_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_IPC_UNAUTHENTICATE_DEVICE_REQ_H
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DEVICE_MANAGER_IPC_VERIFY_AUTHENTICATE_REQ_H
|
||||
#define OHOS_DEVICE_MANAGER_IPC_VERIFY_AUTHENTICATE_REQ_H
|
||||
|
||||
#include "ipc_req.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IpcVerifyAuthenticateReq : public IpcReq {
|
||||
DECLARE_IPC_MODEL(IpcVerifyAuthenticateReq);
|
||||
|
||||
public:
|
||||
const std::string &GetAuthPara() const
|
||||
{
|
||||
return authPara_;
|
||||
}
|
||||
|
||||
void SetAuthPara(const std::string &authPara)
|
||||
{
|
||||
authPara_ = authPara;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string authPara_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DEVICE_MANAGER_IPC_VERIFY_AUTHENTICATE_REQ_H
|
||||
@@ -0,0 +1,67 @@
|
||||
# Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
if (defined(ohos_lite)) {
|
||||
import("//build/lite/config/component/lite_component.gni")
|
||||
} else {
|
||||
import("//build/ohos.gni")
|
||||
}
|
||||
|
||||
import("//foundation/distributedhardware/devicemanager/devicemanager.gni")
|
||||
|
||||
if (defined(ohos_lite)) {
|
||||
} else {
|
||||
ohos_shared_library("devicemanagerext_pin_auth") {
|
||||
include_dirs = [
|
||||
"include",
|
||||
"${common_path}/include",
|
||||
"${services_path}/include/adapter",
|
||||
"${ext_path}/pin_auth/include/ability",
|
||||
"${services_path}/include/authentication",
|
||||
"${utils_path}/include",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"src/ability/standard/dm_ability_manager.cpp",
|
||||
"src/pin_auth.cpp",
|
||||
"src/pin_auth_ui.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"${utils_path}:devicemanagerutils",
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager",
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/want:want",
|
||||
"//foundation/aafwk/standard/services/abilitymgr:abilityms",
|
||||
"//utils/native/base:utils",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"appexecfwk_standard:appexecfwk_base",
|
||||
"appexecfwk_standard:appexecfwk_core",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
"safwk:system_ability_fwk",
|
||||
"startup_l2:syspara",
|
||||
]
|
||||
|
||||
defines = [
|
||||
"HI_LOG_ENABLE",
|
||||
"DH_LOG_TAG=\"devicemanagerext\"",
|
||||
"LOG_DOMAIN=0xD004100",
|
||||
]
|
||||
|
||||
subsystem_name = "distributedhardware"
|
||||
|
||||
part_name = "device_manager_base"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_ABILITY_MANAGER_H
|
||||
#define OHOS_DM_ABILITY_MANAGER_H
|
||||
|
||||
#include <semaphore.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
enum AbilityRole { ABILITY_ROLE_PASSIVE = 0, ABILITY_ROLE_INITIATIVE = 1, ABILITY_ROLE_UNKNOWN = 2 };
|
||||
|
||||
enum AbilityStatus { ABILITY_STATUS_FAILED = 0, ABILITY_STATUS_SUCCESS = 1, ABILITY_STATUS_START = 2 };
|
||||
|
||||
enum FaAction {
|
||||
USER_OPERATION_TYPE_ALLOW_AUTH = 0,
|
||||
USER_OPERATION_TYPE_CANCEL_AUTH = 1,
|
||||
USER_OPERATION_TYPE_AUTH_CONFIRM_TIMEOUT = 2,
|
||||
USER_OPERATION_TYPE_CANCEL_PINCODE_DISPLAY = 3,
|
||||
USER_OPERATION_TYPE_CANCEL_PINCODE_INPUT = 4
|
||||
};
|
||||
|
||||
class DmAbilityManager {
|
||||
public:
|
||||
AbilityRole GetAbilityRole();
|
||||
AbilityStatus StartAbility(AbilityRole role);
|
||||
void StartAbilityDone();
|
||||
|
||||
private:
|
||||
void waitForTimeout(uint32_t timeout_s);
|
||||
|
||||
private:
|
||||
sem_t mSem_;
|
||||
AbilityStatus mStatus_;
|
||||
AbilityRole mAbilityStatus_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_ABILITY_MANAGER_H
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_PIN_AUTH_H
|
||||
#define OHOS_DM_PIN_AUTH_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "authentication.h"
|
||||
#include "dm_ability_manager.h"
|
||||
#include "pin_auth_ui.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class PinAuth : public IAuthentication {
|
||||
public:
|
||||
PinAuth();
|
||||
~PinAuth();
|
||||
int32_t ShowAuthInfo() override;
|
||||
int32_t StartAuth(std::shared_ptr<DmAbilityManager> dmAbilityManager) override;
|
||||
int32_t VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam) override;
|
||||
|
||||
private:
|
||||
int32_t times_ = 0;
|
||||
std::shared_ptr<PinAuthUi> pinAuthUi_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_PIN_AUTH_H
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_PIN_AUTH_UI_H
|
||||
#define OHOS_DM_PIN_AUTH_UI_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "dm_ability_manager.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class PinAuthUi {
|
||||
public:
|
||||
PinAuthUi();
|
||||
int32_t ShowPinDialog();
|
||||
int32_t InputPinDialog(std::shared_ptr<DmAbilityManager> dmAbilityManager);
|
||||
|
||||
private:
|
||||
int32_t StartFaUiService(std::shared_ptr<DmAbilityManager> dmAbilityManager);
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_PIN_AUTH_UI_H
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_ability_manager.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
AbilityRole DmAbilityManager::GetAbilityRole()
|
||||
{
|
||||
return mAbilityStatus_;
|
||||
}
|
||||
|
||||
AbilityStatus DmAbilityManager::StartAbility(AbilityRole role)
|
||||
{
|
||||
// not support for L1 yet, do nothing. just save status and role
|
||||
mAbilityStatus_ = role;
|
||||
mStatus_ = AbilityStatus::ABILITY_STATUS_SUCCESS;
|
||||
return mStatus_;
|
||||
}
|
||||
|
||||
void DmAbilityManager::waitForTimeout(uint32_t timeout_s)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_sec += timeout_s;
|
||||
sem_timedwait(&mSem_, &ts);
|
||||
}
|
||||
|
||||
void DmAbilityManager::StartAbilityDone()
|
||||
{
|
||||
mStatus_ = AbilityStatus::ABILITY_STATUS_SUCCESS;
|
||||
sem_post(&mSem_);
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_ability_manager.h"
|
||||
|
||||
#include "ability_manager_client.h"
|
||||
#include "ability_manager_service.h"
|
||||
#include "ability_record.h"
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "parameter.h"
|
||||
#include "semaphore.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
namespace {
|
||||
const int32_t ABILITY_START_TIMEOUT = 3; // 3 second
|
||||
const std::string bundleUiName = "com.ohos.devicemanagerui";
|
||||
const std::string abilityUiName = "com.ohos.devicemanagerui.MainAbility";
|
||||
} // namespace
|
||||
|
||||
AbilityRole DmAbilityManager::GetAbilityRole()
|
||||
{
|
||||
return mAbilityStatus_;
|
||||
}
|
||||
|
||||
AbilityStatus DmAbilityManager::StartAbility(AbilityRole role)
|
||||
{
|
||||
mAbilityStatus_ = role;
|
||||
char localDeviceId[DEVICE_UUID_LENGTH] = {0};
|
||||
GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
|
||||
std::string deviceId = localDeviceId;
|
||||
mStatus_ = ABILITY_STATUS_START;
|
||||
AAFwk::Want want;
|
||||
AppExecFwk::ElementName element(deviceId, bundleUiName, abilityUiName);
|
||||
want.SetElement(element);
|
||||
AAFwk::AbilityManagerClient::GetInstance()->Connect();
|
||||
ErrCode result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
|
||||
if (result != OHOS::ERR_OK) {
|
||||
LOGE("Start Ability faild");
|
||||
mStatus_ = ABILITY_STATUS_FAILED;
|
||||
return mStatus_;
|
||||
}
|
||||
waitForTimeout(ABILITY_START_TIMEOUT);
|
||||
return mStatus_;
|
||||
}
|
||||
|
||||
void DmAbilityManager::waitForTimeout(uint32_t timeout_s)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_sec += timeout_s;
|
||||
sem_timedwait(&mSem_, &ts);
|
||||
}
|
||||
|
||||
void DmAbilityManager::StartAbilityDone()
|
||||
{
|
||||
mStatus_ = ABILITY_STATUS_SUCCESS;
|
||||
sem_post(&mSem_);
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "pin_auth.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
PinAuth::PinAuth()
|
||||
{
|
||||
LOGI("PinAuth constructor");
|
||||
}
|
||||
|
||||
PinAuth::~PinAuth()
|
||||
{
|
||||
}
|
||||
|
||||
int32_t PinAuth::ShowAuthInfo()
|
||||
{
|
||||
return pinAuthUi_->ShowPinDialog();
|
||||
}
|
||||
|
||||
int32_t PinAuth::StartAuth(std::shared_ptr<DmAbilityManager> dmAbilityManager)
|
||||
{
|
||||
times_ = 0;
|
||||
return pinAuthUi_->InputPinDialog(dmAbilityManager);
|
||||
}
|
||||
|
||||
int32_t PinAuth::VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam)
|
||||
{
|
||||
times_ += 1;
|
||||
nlohmann::json jsonObject = nlohmann::json::parse(authParam, nullptr, false);
|
||||
if (jsonObject.is_discarded()) {
|
||||
LOGE("DecodeRequestAuth jsonStr error");
|
||||
return DM_FAILED;
|
||||
}
|
||||
if (!jsonObject.contains(PIN_CODE_KEY) && !jsonObject.contains(PIN_TOKEN)) {
|
||||
LOGE("err json string, first time");
|
||||
return DM_FAILED;
|
||||
}
|
||||
int32_t inputPinCode = jsonObject[PIN_CODE_KEY];
|
||||
int32_t inputPinToken = jsonObject[PIN_TOKEN];
|
||||
if (code == inputPinCode && stoi(pinToken) == inputPinToken) {
|
||||
return DM_OK;
|
||||
} else if (code != inputPinCode && times_ < 3) {
|
||||
return DM_AUTH_INPUT_FAILED;
|
||||
} else {
|
||||
return DM_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" IAuthentication *CreatePinAuthObject(void)
|
||||
{
|
||||
return new PinAuth;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "pin_auth_ui.h"
|
||||
|
||||
#include "dm_ability_manager.h"
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
|
||||
PinAuthUi::PinAuthUi()
|
||||
{
|
||||
LOGI("AuthUi constructor");
|
||||
}
|
||||
|
||||
int32_t PinAuthUi::ShowPinDialog()
|
||||
{
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t PinAuthUi::InputPinDialog(std::shared_ptr<DmAbilityManager> dmAbilityManager)
|
||||
{
|
||||
if (dmAbilityManager == nullptr) {
|
||||
LOGE("PinAuthUi::dmAbilityManager is null");
|
||||
return DM_FAILED;
|
||||
}
|
||||
return StartFaUiService(dmAbilityManager);
|
||||
}
|
||||
|
||||
int32_t PinAuthUi::StartFaUiService(std::shared_ptr<DmAbilityManager> dmAbilityManager)
|
||||
{
|
||||
AbilityStatus status = dmAbilityManager->StartAbility(AbilityRole::ABILITY_ROLE_INITIATIVE);
|
||||
if (status != AbilityStatus::ABILITY_STATUS_SUCCESS) {
|
||||
LOGE("PinAuthUi::StartFaService timeout");
|
||||
return DM_FAILED;
|
||||
}
|
||||
return DM_OK;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,75 @@
|
||||
# Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
if (defined(ohos_lite)) {
|
||||
import("//build/lite/config/component/lite_component.gni")
|
||||
} else {
|
||||
import("//build/ohos.gni")
|
||||
}
|
||||
|
||||
import("//foundation/distributedhardware/devicemanager/devicemanager.gni")
|
||||
|
||||
if (defined(ohos_lite)) {
|
||||
} else {
|
||||
ohos_shared_library("devicemanagerext_profile") {
|
||||
include_dirs = [
|
||||
"include",
|
||||
"${common_path}/include",
|
||||
"${common_path}/include/ipc",
|
||||
"${common_path}/include/ipc/model",
|
||||
"${ext_path}/profile/include",
|
||||
"${services_path}/include",
|
||||
"${services_path}/include/adapter",
|
||||
"${services_path}/include/dependency/softbus",
|
||||
"${services_path}/include/ipc/standard",
|
||||
"${services_path}/include/devicestate",
|
||||
"${utils_path}/include",
|
||||
"${utils_path}/include/ipc/standard",
|
||||
"${innerkits_path}/native_cpp/include",
|
||||
"${innerkits_path}/native_cpp/include/ipc",
|
||||
"${innerkits_path}/native_cpp/include/ipc/standard",
|
||||
"//foundation/communication/dsoftbus/interfaces/kits/bus_center",
|
||||
"//foundation/communication/dsoftbus/interfaces/kits/common",
|
||||
"//foundation/communication/dsoftbus/interfaces/kits/discovery",
|
||||
"//foundation/communication/dsoftbus/interfaces/kits/transport",
|
||||
"//foundation/communication/dsoftbus/interfaces/inner_kits/transport",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"src/device_profile_adapter.cpp",
|
||||
"src/profile_connector.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"${services_path}:devicemanagerservice",
|
||||
"${utils_path}:devicemanagerutils",
|
||||
"//utils/native/base:utils",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"device_profile_core:distributed_device_profile_client",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
"ipc:ipc_core",
|
||||
]
|
||||
|
||||
defines = [
|
||||
"HI_LOG_ENABLE",
|
||||
"DH_LOG_TAG=\"devicemanagerext\"",
|
||||
"LOG_DOMAIN=0xD004100",
|
||||
]
|
||||
|
||||
subsystem_name = "distributedhardware"
|
||||
|
||||
part_name = "device_manager_base"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_ADAPTER_DEVICE_PROFILE_H
|
||||
#define OHOS_DM_ADAPTER_DEVICE_PROFILE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "profile_adapter.h"
|
||||
#include "profile_connector.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class DeviceProfileAdapter : public IProfileAdapter, public IProfileConnectorCallback {
|
||||
|
||||
public:
|
||||
DeviceProfileAdapter();
|
||||
~DeviceProfileAdapter();
|
||||
int32_t RegisterProfileListener(const std::string &pkgName, const std::string &deviceId,
|
||||
std::shared_ptr<DmDeviceStateManager> callback);
|
||||
int32_t UnRegisterProfileListener(const std::string &pkgName);
|
||||
int32_t OnProfileClientDeviceReady(const std::string &pkgName, const std::string &deviceId);
|
||||
void OnProfileChanged(const std::string &pkgName, const std::string &deviceId);
|
||||
void OnProfileComplete(const std::string &pkgName, const std::string &deviceId);
|
||||
|
||||
private:
|
||||
std::shared_ptr<DmDeviceStateManager> deviceProfileAdapterCallback_;
|
||||
static std::shared_ptr<ProfileConnector> profileConnector_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_ADAPTER_DEVICE_PROFILE_H
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_PROFILE_CONNECTOR_H
|
||||
#define OHOS_DM_PROFILE_CONNECTOR_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "distributed_device_profile_client.h"
|
||||
#include "profile_connector_callback.h"
|
||||
#include "single_instance.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
using namespace OHOS::DeviceProfile;
|
||||
|
||||
class ProfileEventCallback : public IProfileEventCallback {
|
||||
public:
|
||||
void OnSyncCompleted(const SyncResult &syncResults);
|
||||
int32_t RegisterProfileCallback(const std::string &pkgName, std::shared_ptr<IProfileConnectorCallback> callback);
|
||||
int32_t UnRegisterProfileCallback(const std::string &pkgName);
|
||||
|
||||
public:
|
||||
static std::map<std::string, std::shared_ptr<IProfileConnectorCallback>> profileConnectorCallback_;
|
||||
};
|
||||
|
||||
class ProfileConnector : public IProfileEventCallback {
|
||||
public:
|
||||
int32_t RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId,
|
||||
std::shared_ptr<IProfileConnectorCallback> callback);
|
||||
int32_t UnRegisterProfileCallback(const std::string &pkgName);
|
||||
int32_t SubscribeProfileEvents(const std::list<std::string> &serviceIds, const std::string &deviceId);
|
||||
int32_t UnSubscribeProfileEvents();
|
||||
|
||||
private:
|
||||
static std::shared_ptr<ProfileEventCallback> profileEventCallback_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_PROFILE_CONNECTOR_H
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_PROFILE_CONNECTOR_CALLBACK_H
|
||||
#define OHOS_DM_PROFILE_CONNECTOR_CALLBACK_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class IProfileConnectorCallback {
|
||||
public:
|
||||
virtual void OnProfileChanged(const std::string &pkgName, const std::string &deviceId) = 0;
|
||||
virtual void OnProfileComplete(const std::string &pkgName, const std::string &deviceId) = 0;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_PROFILE_CONNECTOR_CALLBACK_H
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "device_profile_adapter.h"
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "dm_device_state_manager.h"
|
||||
#include "dm_log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
std::shared_ptr<ProfileConnector> DeviceProfileAdapter::profileConnector_ = std::make_shared<ProfileConnector>();
|
||||
|
||||
DeviceProfileAdapter::DeviceProfileAdapter()
|
||||
{
|
||||
}
|
||||
|
||||
DeviceProfileAdapter::~DeviceProfileAdapter()
|
||||
{
|
||||
}
|
||||
|
||||
int32_t DeviceProfileAdapter::RegisterProfileListener(const std::string &pkgName, const std::string &deviceId,
|
||||
std::shared_ptr<DmDeviceStateManager> callback)
|
||||
{
|
||||
LOGI("DeviceProfileAdapter::RegisterProfileListener");
|
||||
deviceProfileAdapterCallback_ = callback;
|
||||
profileConnector_->RegisterProfileCallback(pkgName, deviceId, std::shared_ptr<DeviceProfileAdapter>(this));
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t DeviceProfileAdapter::UnRegisterProfileListener(const std::string &pkgName)
|
||||
{
|
||||
LOGI("DeviceProfileAdapter::RegisterProfileListener");
|
||||
deviceProfileAdapterCallback_ = nullptr;
|
||||
profileConnector_->UnRegisterProfileCallback(pkgName);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t DeviceProfileAdapter::OnProfileClientDeviceReady(const std::string &pkgName, const std::string &deviceId)
|
||||
{
|
||||
LOGI("DeviceProfileAdapter::OnProfileClientDeviceReady");
|
||||
if (nullptr != deviceProfileAdapterCallback_) {
|
||||
deviceProfileAdapterCallback_->OnProfileReady(pkgName, deviceId);
|
||||
} else {
|
||||
LOGI("deviceProfileAdapterCallback_ is nullptr");
|
||||
}
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
void DeviceProfileAdapter::OnProfileChanged(const std::string &pkgName, const std::string &deviceId)
|
||||
{
|
||||
OnProfileClientDeviceReady(pkgName, deviceId);
|
||||
}
|
||||
|
||||
void DeviceProfileAdapter::OnProfileComplete(const std::string &pkgName, const std::string &deviceId)
|
||||
{
|
||||
OnProfileClientDeviceReady(pkgName, deviceId);
|
||||
}
|
||||
|
||||
extern "C" IProfileAdapter *CreateDeviceProfileObject(void)
|
||||
{
|
||||
return new DeviceProfileAdapter;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "profile_connector.h"
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
|
||||
std::map<std::string, std::shared_ptr<IProfileConnectorCallback>> ProfileEventCallback::profileConnectorCallback_ = {};
|
||||
std::shared_ptr<ProfileEventCallback> ProfileConnector::profileEventCallback_ =
|
||||
std::make_shared<ProfileEventCallback>();
|
||||
|
||||
int32_t ProfileConnector::RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId,
|
||||
std::shared_ptr<IProfileConnectorCallback> callback)
|
||||
{
|
||||
LOGI("ProfileConnector::RegisterProfileCallback");
|
||||
profileEventCallback_->RegisterProfileCallback(pkgName, callback);
|
||||
SubscribeProfileEvents({"system", "device", "fakeStorage", "fakeSystem"}, deviceId);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t ProfileConnector::UnRegisterProfileCallback(const std::string &pkgName)
|
||||
{
|
||||
LOGI("ProfileConnector::UnRegisterProfileCallback");
|
||||
profileEventCallback_->UnRegisterProfileCallback(pkgName);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t ProfileConnector::SubscribeProfileEvents(const std::list<std::string> &serviceIds, const std::string &deviceId)
|
||||
{
|
||||
ExtraInfo extraInfo;
|
||||
extraInfo["deviceId"] = deviceId;
|
||||
extraInfo["serviceIds"] = serviceIds;
|
||||
|
||||
std::list<SubscribeInfo> subscribeInfos;
|
||||
|
||||
SubscribeInfo eventSync;
|
||||
eventSync.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
|
||||
subscribeInfos.emplace_back(eventSync);
|
||||
|
||||
std::list<ProfileEvent> failedEvents;
|
||||
u_int32_t errCode;
|
||||
errCode = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(subscribeInfos,
|
||||
profileEventCallback_, failedEvents);
|
||||
LOGI("ProfileConnector::SubscribeProfileEvents result=%d", errCode);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t ProfileConnector::UnSubscribeProfileEvents()
|
||||
{
|
||||
std::list<ProfileEvent> profileEvents;
|
||||
profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED);
|
||||
profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED);
|
||||
std::list<ProfileEvent> failedEvents;
|
||||
u_int32_t errCode;
|
||||
errCode = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(
|
||||
profileEvents, profileEventCallback_, failedEvents);
|
||||
LOGI("ProfileConnector::UnSubscribeProfileEvents result=%d", errCode);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t ProfileEventCallback::RegisterProfileCallback(const std::string &pkgName,
|
||||
std::shared_ptr<IProfileConnectorCallback> callback)
|
||||
{
|
||||
LOGI("ProfileEventCallback::RegisterProfileCallback");
|
||||
profileConnectorCallback_.emplace(pkgName, callback);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t ProfileEventCallback::UnRegisterProfileCallback(const std::string &pkgName)
|
||||
{
|
||||
LOGI("ProfileEventCallback::UnRegisterProfileCallback");
|
||||
profileConnectorCallback_.erase(pkgName);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
void ProfileEventCallback::OnSyncCompleted(const SyncResult &syncResults)
|
||||
{
|
||||
std::string deviceId;
|
||||
u_int32_t SyncStatus;
|
||||
for (auto &iterResult : syncResults) {
|
||||
deviceId = iterResult.first;
|
||||
SyncStatus = iterResult.second;
|
||||
}
|
||||
LOGI("ProfileEventCallback::OnSyncCompleted, deviceId = %s", deviceId.c_str());
|
||||
for (auto &iter : profileConnectorCallback_) {
|
||||
iter.second->OnProfileComplete(iter.first, deviceId);
|
||||
}
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_ANONYMOUS_H
|
||||
#define OHOS_DM_ANONYMOUS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
std::string GetAnonyString(const std::string &value);
|
||||
std::string GetAnonyInt32(const int32_t value);
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // OHOS_DM_ANONYMOUS_H
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_LOG_H
|
||||
#define OHOS_DM_LOG_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
|
||||
typedef enum {
|
||||
DM_LOG_DEBUG,
|
||||
DM_LOG_INFO,
|
||||
DM_LOG_WARN,
|
||||
DM_LOG_ERROR,
|
||||
} DMLogLevel;
|
||||
|
||||
void DMLog(DMLogLevel logLevel, const char *fmt, ...);
|
||||
|
||||
#define LOGD(fmt, ...) \
|
||||
DMLog(DM_LOG_DEBUG, (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__)
|
||||
|
||||
#define LOGI(fmt, ...) \
|
||||
DMLog(DM_LOG_INFO, (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__)
|
||||
|
||||
#define LOGW(fmt, ...) \
|
||||
DMLog(DM_LOG_WARN, (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__)
|
||||
|
||||
#define LOGE(fmt, ...) \
|
||||
DMLog(DM_LOG_ERROR, (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__)
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_DM_LOG_H
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DM_RANDOM_H
|
||||
#define OHOS_DM_RANDOM_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
int32_t GenRandInt(int32_t randMin, int32_t randMax);
|
||||
int64_t GenRandLongLong(int64_t randMin, int64_t randMax);
|
||||
int32_t GetRandomData(uint8_t *randStr, uint32_t len);
|
||||
bool MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly);
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // OHOS_DM_RANDOM_H
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_anonymous.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
std::string GetAnonyString(const std::string &value)
|
||||
{
|
||||
const int32_t INT32_SHORT_ID_LENGTH = 20;
|
||||
const int32_t INT32_PLAINTEXT_LENGTH = 4;
|
||||
const int32_t INT32_MIN_ID_LENGTH = 3;
|
||||
|
||||
std::string tmpStr("******");
|
||||
int32_t strLen = value.length();
|
||||
if (strLen < INT32_MIN_ID_LENGTH) {
|
||||
return tmpStr;
|
||||
}
|
||||
|
||||
std::string res;
|
||||
if (strLen <= INT32_SHORT_ID_LENGTH) {
|
||||
res += value[0];
|
||||
res += tmpStr;
|
||||
res += value[strLen - 1];
|
||||
} else {
|
||||
res.append(value, 0, INT32_PLAINTEXT_LENGTH);
|
||||
res += tmpStr;
|
||||
res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string GetAnonyInt32(const int32_t value)
|
||||
{
|
||||
std::string tempString = std::to_string(value);
|
||||
int32_t length = tempString.length();
|
||||
if (length == 0x01) {
|
||||
tempString[0] = '*';
|
||||
return tempString;
|
||||
}
|
||||
|
||||
for (int32_t i = 1; i < length - 1; i++) {
|
||||
tempString[i] = '*';
|
||||
}
|
||||
return tempString;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_log.h"
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "securec.h"
|
||||
|
||||
#ifdef HI_LOG_ENABLE
|
||||
#include "hilog/log.h"
|
||||
#else
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
static void DMLogOut(DMLogLevel logLevel, const char *logBuf)
|
||||
{
|
||||
#ifdef HI_LOG_ENABLE
|
||||
LogLevel hiLogLevel = LOG_INFO;
|
||||
switch (logLevel) {
|
||||
case DM_LOG_DEBUG:
|
||||
hiLogLevel = LOG_DEBUG;
|
||||
break;
|
||||
case DM_LOG_INFO:
|
||||
hiLogLevel = LOG_INFO;
|
||||
break;
|
||||
case DM_LOG_WARN:
|
||||
hiLogLevel = LOG_WARN;
|
||||
break;
|
||||
case DM_LOG_ERROR:
|
||||
hiLogLevel = LOG_ERROR;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
(void)HiLogPrint(LOG_CORE, hiLogLevel, LOG_DOMAIN, "", "%{public}s", logBuf);
|
||||
#else
|
||||
switch (logLevel) {
|
||||
case DM_LOG_DEBUG:
|
||||
printf("[D]%s\n", logBuf);
|
||||
break;
|
||||
case DM_LOG_INFO:
|
||||
printf("[I]%s\n", logBuf);
|
||||
break;
|
||||
case DM_LOG_WARN:
|
||||
printf("[W]%s\n", logBuf);
|
||||
break;
|
||||
case DM_LOG_ERROR:
|
||||
printf("[E]%s\n", logBuf);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void DMLog(DMLogLevel logLevel, const char *fmt, ...)
|
||||
{
|
||||
char logBuf[LOG_MAX_LEN] = {0};
|
||||
va_list arg;
|
||||
int32_t ret = 0;
|
||||
|
||||
(void)memset_s(&arg, sizeof(va_list), 0, sizeof(va_list));
|
||||
va_start(arg, fmt);
|
||||
ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg);
|
||||
va_end(arg);
|
||||
if (ret < 0) {
|
||||
DMLogOut(logLevel, "DM log length error.");
|
||||
return;
|
||||
}
|
||||
DMLogOut(logLevel, logBuf);
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_random.h"
|
||||
|
||||
#include <random>
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/md.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
int32_t GenRandInt(int32_t randMin, int32_t randMax)
|
||||
{
|
||||
std::random_device randDevice;
|
||||
std::mt19937 genRand(randDevice());
|
||||
std::uniform_int_distribution<int> disRand(randMin, randMax);
|
||||
return disRand(genRand);
|
||||
}
|
||||
|
||||
int64_t GenRandLongLong(int64_t randMin, int64_t randMax)
|
||||
{
|
||||
std::random_device randDevice;
|
||||
std::mt19937 genRand(randDevice());
|
||||
std::uniform_int_distribution<long long> disRand(randMin, randMax);
|
||||
return disRand(genRand);
|
||||
}
|
||||
|
||||
int32_t GetRandomData(uint8_t *randStr, uint32_t len)
|
||||
{
|
||||
mbedtls_entropy_context *entropy = nullptr;
|
||||
mbedtls_ctr_drbg_context *ctrDrbg = nullptr;
|
||||
int32_t ret = DM_FAILED;
|
||||
do {
|
||||
if (randStr == nullptr || len == 0) {
|
||||
break;
|
||||
}
|
||||
entropy = (mbedtls_entropy_context *)malloc(sizeof(mbedtls_entropy_context));
|
||||
if (entropy == nullptr) {
|
||||
break;
|
||||
}
|
||||
ctrDrbg = (mbedtls_ctr_drbg_context *)malloc(sizeof(mbedtls_ctr_drbg_context));
|
||||
if (ctrDrbg == nullptr) {
|
||||
break;
|
||||
}
|
||||
mbedtls_ctr_drbg_init(ctrDrbg);
|
||||
mbedtls_entropy_init(entropy);
|
||||
ret = mbedtls_ctr_drbg_seed(ctrDrbg, mbedtls_entropy_func, entropy, nullptr, 0);
|
||||
if (ret != 0) {
|
||||
break;
|
||||
}
|
||||
ret = mbedtls_ctr_drbg_random(ctrDrbg, randStr, len);
|
||||
if (ret != 0) {
|
||||
break;
|
||||
}
|
||||
ret = DM_OK;
|
||||
} while (0);
|
||||
if (entropy != nullptr) {
|
||||
free(entropy);
|
||||
}
|
||||
if (ctrDrbg != nullptr) {
|
||||
free(ctrDrbg);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly)
|
||||
{
|
||||
const int32_t MIN_OUT_LENGTH = 2;
|
||||
if (szOut == nullptr || szOutLen <= MIN_OUT_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
szOut[--szOutLen] = 0;
|
||||
GetRandomData((uint8_t *)szOut, szOutLen);
|
||||
const int32_t NUMBER_COUNT = 10;
|
||||
const int32_t ALPHA_COUNT = 26;
|
||||
const int32_t ALPHA_BYTE_COUNT = 2;
|
||||
int32_t M = numberOnly ? NUMBER_COUNT : (NUMBER_COUNT + ALPHA_BYTE_COUNT * ALPHA_COUNT);
|
||||
for (int32_t i = 0; i < szOutLen; i++) {
|
||||
// 0~9,A~Z,a~z
|
||||
uint32_t idx = ((uint32_t)szOut[i] % M);
|
||||
char base;
|
||||
if (idx < NUMBER_COUNT) {
|
||||
base = '0';
|
||||
} else if (idx >= NUMBER_COUNT && idx < (NUMBER_COUNT + ALPHA_COUNT)) {
|
||||
base = 'A';
|
||||
idx -= NUMBER_COUNT;
|
||||
} else {
|
||||
base = 'a';
|
||||
idx -= (NUMBER_COUNT + ALPHA_COUNT);
|
||||
}
|
||||
szOut[i] = base + idx;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
Reference in New Issue
Block a user