diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h new file mode 100644 index 00000000..cd7403f4 --- /dev/null +++ b/common/include/dm_constants.h @@ -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 +#include + +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 diff --git a/common/include/ipc/model/ipc_get_dmfaparam_rsp.h b/common/include/ipc/model/ipc_get_dmfaparam_rsp.h new file mode 100644 index 00000000..ed4b8958 --- /dev/null +++ b/common/include/ipc/model/ipc_get_dmfaparam_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_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 \ No newline at end of file diff --git a/common/include/ipc/model/ipc_get_info_by_network_req.h b/common/include/ipc/model/ipc_get_info_by_network_req.h new file mode 100644 index 00000000..786b7d58 --- /dev/null +++ b/common/include/ipc/model/ipc_get_info_by_network_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_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 \ No newline at end of file diff --git a/common/include/ipc/model/ipc_get_info_by_network_rsp.h b/common/include/ipc/model/ipc_get_info_by_network_rsp.h new file mode 100644 index 00000000..18dd12b4 --- /dev/null +++ b/common/include/ipc/model/ipc_get_info_by_network_rsp.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 + +#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 \ No newline at end of file diff --git a/common/include/ipc/model/ipc_get_local_device_info_rsp.h b/common/include/ipc/model/ipc_get_local_device_info_rsp.h new file mode 100644 index 00000000..7494da0f --- /dev/null +++ b/common/include/ipc/model/ipc_get_local_device_info_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 \ No newline at end of file diff --git a/common/include/ipc/model/ipc_notify_verify_auth_result_req.h b/common/include/ipc/model/ipc_notify_verify_auth_result_req.h new file mode 100644 index 00000000..df01942f --- /dev/null +++ b/common/include/ipc/model/ipc_notify_verify_auth_result_req.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 + +#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 diff --git a/common/include/ipc/model/ipc_unauthenticate_device_req.h b/common/include/ipc/model/ipc_unauthenticate_device_req.h new file mode 100644 index 00000000..78244377 --- /dev/null +++ b/common/include/ipc/model/ipc_unauthenticate_device_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 diff --git a/common/include/ipc/model/ipc_verify_authenticate_req.h b/common/include/ipc/model/ipc_verify_authenticate_req.h new file mode 100644 index 00000000..3c2df554 --- /dev/null +++ b/common/include/ipc/model/ipc_verify_authenticate_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 diff --git a/ext/pin_auth/BUILD.gn b/ext/pin_auth/BUILD.gn new file mode 100644 index 00000000..cc8cfcef --- /dev/null +++ b/ext/pin_auth/BUILD.gn @@ -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" + } +} \ No newline at end of file diff --git a/ext/pin_auth/include/ability/dm_ability_manager.h b/ext/pin_auth/include/ability/dm_ability_manager.h new file mode 100644 index 00000000..225fefc6 --- /dev/null +++ b/ext/pin_auth/include/ability/dm_ability_manager.h @@ -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 + +#include +#include +#include + +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 diff --git a/ext/pin_auth/include/pin_auth.h b/ext/pin_auth/include/pin_auth.h new file mode 100644 index 00000000..2129c94a --- /dev/null +++ b/ext/pin_auth/include/pin_auth.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 +#include + +#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) override; + int32_t VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam) override; + +private: + int32_t times_ = 0; + std::shared_ptr pinAuthUi_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_PIN_AUTH_H diff --git a/ext/pin_auth/include/pin_auth_ui.h b/ext/pin_auth/include/pin_auth_ui.h new file mode 100644 index 00000000..f8910cba --- /dev/null +++ b/ext/pin_auth/include/pin_auth_ui.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 + +#include "dm_ability_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class PinAuthUi { +public: + PinAuthUi(); + int32_t ShowPinDialog(); + int32_t InputPinDialog(std::shared_ptr dmAbilityManager); + +private: + int32_t StartFaUiService(std::shared_ptr dmAbilityManager); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_PIN_AUTH_UI_H diff --git a/ext/pin_auth/src/ability/lite/dm_ability_manager.cpp b/ext/pin_auth/src/ability/lite/dm_ability_manager.cpp new file mode 100644 index 00000000..45112330 --- /dev/null +++ b/ext/pin_auth/src/ability/lite/dm_ability_manager.cpp @@ -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 diff --git a/ext/pin_auth/src/ability/standard/dm_ability_manager.cpp b/ext/pin_auth/src/ability/standard/dm_ability_manager.cpp new file mode 100644 index 00000000..e2b026f1 --- /dev/null +++ b/ext/pin_auth/src/ability/standard/dm_ability_manager.cpp @@ -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 diff --git a/ext/pin_auth/src/pin_auth.cpp b/ext/pin_auth/src/pin_auth.cpp new file mode 100644 index 00000000..781a375f --- /dev/null +++ b/ext/pin_auth/src/pin_auth.cpp @@ -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 + +#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) +{ + 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 diff --git a/ext/pin_auth/src/pin_auth_ui.cpp b/ext/pin_auth/src/pin_auth_ui.cpp new file mode 100644 index 00000000..15dd767a --- /dev/null +++ b/ext/pin_auth/src/pin_auth_ui.cpp @@ -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) +{ + if (dmAbilityManager == nullptr) { + LOGE("PinAuthUi::dmAbilityManager is null"); + return DM_FAILED; + } + return StartFaUiService(dmAbilityManager); +} + +int32_t PinAuthUi::StartFaUiService(std::shared_ptr 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 diff --git a/ext/profile/BUILD.gn b/ext/profile/BUILD.gn new file mode 100644 index 00000000..4ac238c3 --- /dev/null +++ b/ext/profile/BUILD.gn @@ -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" + } +} \ No newline at end of file diff --git a/ext/profile/include/device_profile_adapter.h b/ext/profile/include/device_profile_adapter.h new file mode 100644 index 00000000..d27b71fd --- /dev/null +++ b/ext/profile/include/device_profile_adapter.h @@ -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 +#include + +#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 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 deviceProfileAdapterCallback_; + static std::shared_ptr profileConnector_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_ADAPTER_DEVICE_PROFILE_H diff --git a/ext/profile/include/profile_connector.h b/ext/profile/include/profile_connector.h new file mode 100644 index 00000000..d83f9074 --- /dev/null +++ b/ext/profile/include/profile_connector.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 +#include +#include +#include +#include + +#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 callback); + int32_t UnRegisterProfileCallback(const std::string &pkgName); + +public: + static std::map> profileConnectorCallback_; +}; + +class ProfileConnector : public IProfileEventCallback { +public: + int32_t RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId, + std::shared_ptr callback); + int32_t UnRegisterProfileCallback(const std::string &pkgName); + int32_t SubscribeProfileEvents(const std::list &serviceIds, const std::string &deviceId); + int32_t UnSubscribeProfileEvents(); + +private: + static std::shared_ptr profileEventCallback_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_PROFILE_CONNECTOR_H \ No newline at end of file diff --git a/ext/profile/include/profile_connector_callback.h b/ext/profile/include/profile_connector_callback.h new file mode 100644 index 00000000..70738fbf --- /dev/null +++ b/ext/profile/include/profile_connector_callback.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 \ No newline at end of file diff --git a/ext/profile/src/device_profile_adapter.cpp b/ext/profile/src/device_profile_adapter.cpp new file mode 100644 index 00000000..61d82e49 --- /dev/null +++ b/ext/profile/src/device_profile_adapter.cpp @@ -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 DeviceProfileAdapter::profileConnector_ = std::make_shared(); + +DeviceProfileAdapter::DeviceProfileAdapter() +{ +} + +DeviceProfileAdapter::~DeviceProfileAdapter() +{ +} + +int32_t DeviceProfileAdapter::RegisterProfileListener(const std::string &pkgName, const std::string &deviceId, + std::shared_ptr callback) +{ + LOGI("DeviceProfileAdapter::RegisterProfileListener"); + deviceProfileAdapterCallback_ = callback; + profileConnector_->RegisterProfileCallback(pkgName, deviceId, std::shared_ptr(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 diff --git a/ext/profile/src/profile_connector.cpp b/ext/profile/src/profile_connector.cpp new file mode 100644 index 00000000..a1a27106 --- /dev/null +++ b/ext/profile/src/profile_connector.cpp @@ -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> ProfileEventCallback::profileConnectorCallback_ = {}; +std::shared_ptr ProfileConnector::profileEventCallback_ = + std::make_shared(); + +int32_t ProfileConnector::RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId, + std::shared_ptr 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 &serviceIds, const std::string &deviceId) +{ + ExtraInfo extraInfo; + extraInfo["deviceId"] = deviceId; + extraInfo["serviceIds"] = serviceIds; + + std::list subscribeInfos; + + SubscribeInfo eventSync; + eventSync.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED; + subscribeInfos.emplace_back(eventSync); + + std::list 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 profileEvents; + profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED); + profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED); + std::list 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 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 \ No newline at end of file diff --git a/utils/include/dm_anonymous.h b/utils/include/dm_anonymous.h new file mode 100644 index 00000000..d8059e0b --- /dev/null +++ b/utils/include/dm_anonymous.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_ANONYMOUS_H +#define OHOS_DM_ANONYMOUS_H + +#include + +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 \ No newline at end of file diff --git a/utils/include/dm_log.h b/utils/include/dm_log.h new file mode 100644 index 00000000..cd041592 --- /dev/null +++ b/utils/include/dm_log.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 diff --git a/utils/include/dm_random.h b/utils/include/dm_random.h new file mode 100644 index 00000000..dcbe1ef1 --- /dev/null +++ b/utils/include/dm_random.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 + +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 diff --git a/utils/src/dm_anonymous.cpp b/utils/src/dm_anonymous.cpp new file mode 100644 index 00000000..8d9a7be3 --- /dev/null +++ b/utils/src/dm_anonymous.cpp @@ -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 \ No newline at end of file diff --git a/utils/src/dm_log.cpp b/utils/src/dm_log.cpp new file mode 100644 index 00000000..2284eabd --- /dev/null +++ b/utils/src/dm_log.cpp @@ -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 +#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 diff --git a/utils/src/dm_random.cpp b/utils/src/dm_random.cpp new file mode 100644 index 00000000..129b0792 --- /dev/null +++ b/utils/src/dm_random.cpp @@ -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 + +#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 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 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