From 65516eb8b991ca73e2bf7c4ebe5f999474f77482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Sat, 11 Sep 2021 17:44:43 +0800 Subject: [PATCH 1/2] L1 & L2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- common/include/ipc/ipc_def.h | 56 +++ .../ipc/model/ipc_authenticate_device_req.h | 65 +++ .../ipc/model/ipc_check_authenticate_req.h | 40 ++ .../model/ipc_get_authenticationparam_rsp.h | 41 ++ .../ipc/model/ipc_get_trustdevice_req.h | 40 ++ .../ipc/model/ipc_get_trustdevice_rsp.h | 44 ++ .../ipc/model/ipc_notify_auth_result_req.h | 75 ++++ .../model/ipc_notify_check_auth_result_req.h | 64 +++ .../ipc/model/ipc_notify_device_found_req.h | 53 +++ .../ipc/model/ipc_notify_device_state_req.h | 53 +++ .../model/ipc_notify_discover_result_req.h | 53 +++ .../ipc/model/ipc_notify_dmfa_result_req.h | 55 +++ .../ipc/model/ipc_register_listener_req.h | 62 +++ common/include/ipc/model/ipc_req.h | 42 ++ common/include/ipc/model/ipc_rsp.h | 42 ++ .../ipc/model/ipc_set_useroperation_req.h | 41 ++ .../ipc/model/ipc_start_discovery_req.h | 42 ++ .../ipc/model/ipc_stop_discovery_req.h | 42 ++ .../native_cpp/include/ipc/ipc_client.h | 37 ++ .../native_cpp/include/ipc/ipc_client_proxy.h | 41 ++ .../include/ipc/lite/ipc_client_manager.h | 42 ++ .../ipc/lite/ipc_client_server_proxy.h | 45 ++ .../include/ipc/lite/ipc_client_stub.h | 43 ++ .../include/ipc/standard/ipc_client_manager.h | 58 +++ .../ipc/standard/ipc_client_server_proxy.h | 39 ++ .../include/ipc/standard/ipc_client_stub.h | 40 ++ .../include/ipc/standard/ipc_remote_broker.h | 38 ++ .../include/notify/device_manager_notify.h | 75 ++++ .../native_cpp/src/ipc/ipc_client_proxy.cpp | 48 +++ .../src/ipc/lite/ipc_client_manager.cpp | 92 +++++ .../src/ipc/lite/ipc_client_server_proxy.cpp | 134 ++++++ .../src/ipc/lite/ipc_client_stub.cpp | 57 +++ .../src/ipc/lite/ipc_cmd_parser.cpp | 287 +++++++++++++ .../src/ipc/standard/ipc_client_manager.cpp | 152 +++++++ .../ipc/standard/ipc_client_server_proxy.cpp | 47 +++ .../src/ipc/standard/ipc_client_stub.cpp | 45 ++ .../src/ipc/standard/ipc_cmd_parser.cpp | 390 ++++++++++++++++++ .../src/notify/device_manager_notify.cpp | 295 +++++++++++++ .../include/ipc/ipc_server_adapter.h | 53 +++ .../include/ipc/ipc_server_listener_adapter.h | 43 ++ .../include/ipc/lite/ipc_server_listener.h | 41 ++ .../include/ipc/lite/ipc_server_listenermgr.h | 50 +++ .../include/ipc/lite/ipc_server_stub.h | 26 ++ .../ipc/standard/ipc_server_client_proxy.h | 35 ++ .../ipc/standard/ipc_server_listener.h | 36 ++ .../include/ipc/standard/ipc_server_stub.h | 76 ++++ .../src/ipc/ipc_server_adapter.cpp | 189 +++++++++ .../src/ipc/ipc_server_listener_adapter.cpp | 129 ++++++ .../src/ipc/lite/ipc_cmd_parser.cpp | 215 ++++++++++ .../src/ipc/lite/ipc_server_listener.cpp | 90 ++++ .../src/ipc/lite/ipc_server_listenermgr.cpp | 65 +++ .../src/ipc/lite/ipc_server_main.cpp | 52 +++ .../src/ipc/lite/ipc_server_stub.cpp | 238 +++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 364 ++++++++++++++++ .../ipc/standard/ipc_server_client_proxy.cpp | 48 +++ .../src/ipc/standard/ipc_server_listener.cpp | 48 +++ .../src/ipc/standard/ipc_server_stub.cpp | 197 +++++++++ utils/include/anonymous_string.h | 26 ++ utils/include/cipher/encrypt_utils.h | 46 +++ utils/include/ipc/lite/ipc_cmd_register.h | 113 +++++ utils/include/ipc/standard/ipc_cmd_register.h | 93 +++++ utils/include/log/dm_log.h | 31 ++ utils/src/anonymous_string.cpp | 68 +++ utils/src/cipher/encrypt_utils.cpp | 148 +++++++ utils/src/ipc/lite/ipc_cmd_register.cpp | 68 +++ utils/src/ipc/standard/ipc_cmd_register.cpp | 55 +++ utils/src/log/dm_log.cpp | 89 ++++ 67 files changed, 5647 insertions(+) create mode 100644 common/include/ipc/ipc_def.h create mode 100644 common/include/ipc/model/ipc_authenticate_device_req.h create mode 100644 common/include/ipc/model/ipc_check_authenticate_req.h create mode 100644 common/include/ipc/model/ipc_get_authenticationparam_rsp.h create mode 100644 common/include/ipc/model/ipc_get_trustdevice_req.h create mode 100644 common/include/ipc/model/ipc_get_trustdevice_rsp.h create mode 100644 common/include/ipc/model/ipc_notify_auth_result_req.h create mode 100644 common/include/ipc/model/ipc_notify_check_auth_result_req.h create mode 100644 common/include/ipc/model/ipc_notify_device_found_req.h create mode 100644 common/include/ipc/model/ipc_notify_device_state_req.h create mode 100644 common/include/ipc/model/ipc_notify_discover_result_req.h create mode 100644 common/include/ipc/model/ipc_notify_dmfa_result_req.h create mode 100644 common/include/ipc/model/ipc_register_listener_req.h create mode 100644 common/include/ipc/model/ipc_req.h create mode 100644 common/include/ipc/model/ipc_rsp.h create mode 100644 common/include/ipc/model/ipc_set_useroperation_req.h create mode 100644 common/include/ipc/model/ipc_start_discovery_req.h create mode 100644 common/include/ipc/model/ipc_stop_discovery_req.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/ipc_client.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/ipc_client_proxy.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_server_proxy.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_stub.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_manager.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_server_proxy.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_stub.h create mode 100644 interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_remote_broker.h create mode 100644 interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/ipc_client_proxy.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_server_proxy.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_stub.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp create mode 100644 interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp create mode 100644 services/devicemanagerservice/include/ipc/ipc_server_adapter.h create mode 100644 services/devicemanagerservice/include/ipc/ipc_server_listener_adapter.h create mode 100644 services/devicemanagerservice/include/ipc/lite/ipc_server_listener.h create mode 100644 services/devicemanagerservice/include/ipc/lite/ipc_server_listenermgr.h create mode 100644 services/devicemanagerservice/include/ipc/lite/ipc_server_stub.h create mode 100644 services/devicemanagerservice/include/ipc/standard/ipc_server_client_proxy.h create mode 100644 services/devicemanagerservice/include/ipc/standard/ipc_server_listener.h create mode 100644 services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h create mode 100644 services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp create mode 100644 services/devicemanagerservice/src/ipc/ipc_server_listener_adapter.cpp create mode 100644 services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp create mode 100644 services/devicemanagerservice/src/ipc/lite/ipc_server_listener.cpp create mode 100644 services/devicemanagerservice/src/ipc/lite/ipc_server_listenermgr.cpp create mode 100644 services/devicemanagerservice/src/ipc/lite/ipc_server_main.cpp create mode 100644 services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp create mode 100644 services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp create mode 100644 services/devicemanagerservice/src/ipc/standard/ipc_server_client_proxy.cpp create mode 100644 services/devicemanagerservice/src/ipc/standard/ipc_server_listener.cpp create mode 100644 services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp create mode 100644 utils/include/anonymous_string.h create mode 100644 utils/include/cipher/encrypt_utils.h create mode 100644 utils/include/ipc/lite/ipc_cmd_register.h create mode 100644 utils/include/ipc/standard/ipc_cmd_register.h create mode 100644 utils/include/log/dm_log.h create mode 100644 utils/src/anonymous_string.cpp create mode 100644 utils/src/cipher/encrypt_utils.cpp create mode 100644 utils/src/ipc/lite/ipc_cmd_register.cpp create mode 100644 utils/src/ipc/standard/ipc_cmd_register.cpp create mode 100644 utils/src/log/dm_log.cpp diff --git a/common/include/ipc/ipc_def.h b/common/include/ipc/ipc_def.h new file mode 100644 index 000000000..e35af6f2c --- /dev/null +++ b/common/include/ipc/ipc_def.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 DEVICE_MANAGER_IPC_DEF_H +#define DEVICE_MANAGER_IPC_DEF_H + +namespace OHOS { +namespace DistributedHardware { +#define DEVICE_MANAGER_SERVICE_NAME "dev_mgr_svc" +#define MAX_DM_IPC_LEN 4096 + +#define DECLARE_IPC_MODEL(className) \ +public: \ + className() = default; \ + virtual ~className() = default; \ +public: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete \ + +#define DECLARE_IPC_INTERFACE(className) \ + DECLARE_IPC_MODEL(className) + +enum IpcCmdID { + REGISTER_DEVICE_MANAGER_LISTENER = 0, + UNREGISTER_DEVICE_MANAGER_LISTENER, + GET_TRUST_DEVICE_LIST, + START_DEVICE_DISCOVER, + STOP_DEVICE_DISCOVER, + AUTHENTICATE_DEVICE, + CHECK_AUTHENTICATION, + SERVER_DEVICE_STATE_NOTIFY, + SERVER_DEVICE_FOUND, + SERVER_DISCOVER_FINISH, + SERVER_AUTH_RESULT, + SERVER_CHECK_AUTH_RESULT, + SERVER_GET_AUTHENTCATION_INFO, + SERVER_USER_AUTHORIZATION_OPERATION, + SERVER_DEVICEMANAGER_FA_NOTIFY, +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // DEVICE_MANAGER_IPC_DEF_H diff --git a/common/include/ipc/model/ipc_authenticate_device_req.h b/common/include/ipc/model/ipc_authenticate_device_req.h new file mode 100644 index 000000000..f4f066b3f --- /dev/null +++ b/common/include/ipc/model/ipc_authenticate_device_req.h @@ -0,0 +1,65 @@ +/* + * 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_AUTHENTICATE_DEVICE_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_AUTHENTICATE_DEVICE_REQ_H + +#include "ipc_req.h" + +#include "dm_device_info.h" +#include "dm_app_image_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcAuthenticateDeviceReq : public IpcReq { +DECLARE_IPC_MODEL(IpcAuthenticateDeviceReq); +public: + const DmDeviceInfo& GetDeviceInfo() const + { + return deviceInfo_; + } + + void SetDeviceInfo(const DmDeviceInfo &deviceInfo) + { + deviceInfo_ = deviceInfo; + } + + const DmAppImageInfo& GetAppImageInfo() const + { + return appImageInfo_; + } + + void SetAppImageInfo(const DmAppImageInfo &appImageInfo) + { + appImageInfo_ = appImageInfo; + } + + const std::string& GetExtra() const + { + return extra_; + } + + void SetExtra(std::string &extra) + { + extra_ = extra; + } +private: + DmDeviceInfo deviceInfo_; + DmAppImageInfo appImageInfo_; + std::string extra_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_AUTHENTICATE_DEVICE_REQ_H diff --git a/common/include/ipc/model/ipc_check_authenticate_req.h b/common/include/ipc/model/ipc_check_authenticate_req.h new file mode 100644 index 000000000..ea136b421 --- /dev/null +++ b/common/include/ipc/model/ipc_check_authenticate_req.h @@ -0,0 +1,40 @@ +/* + * 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_CHECK_AUTHENTICATE_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_CHECK_AUTHENTICATE_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcCheckAuthenticateReq : public IpcReq { +DECLARE_IPC_MODEL(IpcCheckAuthenticateReq); +public: + const std::string& GetAuthPara() const + { + return authPara_; + } + + void SetAuthPara(std::string &authPara) + { + authPara_ = authPara; + } +private: + std::string authPara_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CHECK_AUTHENTICATE_REQ_H diff --git a/common/include/ipc/model/ipc_get_authenticationparam_rsp.h b/common/include/ipc/model/ipc_get_authenticationparam_rsp.h new file mode 100644 index 000000000..b12c39f68 --- /dev/null +++ b/common/include/ipc/model/ipc_get_authenticationparam_rsp.h @@ -0,0 +1,41 @@ +/* + * 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_AUTH_PARAM_RSP_H +#define OHOS_DEVICE_MANAGER_IPC_GET_AUTH_PARAM_RSP_H + +#include "ipc_rsp.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetAuthParamRsp : public IpcRsp { +DECLARE_IPC_MODEL(IpcGetAuthParamRsp); +public: + const DmAuthParam& GetAuthParam() const + { + return authParam_; + } + + void SetAuthParam(DmAuthParam &authParam) + { + authParam_ = authParam; + } +private: + DmAuthParam authParam_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_GET_AUTH_PARAM_RSP_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_get_trustdevice_req.h b/common/include/ipc/model/ipc_get_trustdevice_req.h new file mode 100644 index 000000000..695d87186 --- /dev/null +++ b/common/include/ipc/model/ipc_get_trustdevice_req.h @@ -0,0 +1,40 @@ +/* + * 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_TRUST_DEVICE_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_GET_TRUST_DEVICE_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetTrustdeviceReq : public IpcReq { +DECLARE_IPC_MODEL(IpcGetTrustdeviceReq); +public: + const std::string& GetExtra() const + { + return extra_; + } + + void SetExtra(std::string &extra) + { + extra_ = extra; + } +private: + std::string extra_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_GET_TRUST_DEVICE_REQ_H diff --git a/common/include/ipc/model/ipc_get_trustdevice_rsp.h b/common/include/ipc/model/ipc_get_trustdevice_rsp.h new file mode 100644 index 000000000..6df09e883 --- /dev/null +++ b/common/include/ipc/model/ipc_get_trustdevice_rsp.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_DEVICE_MANAGER_IPC_GET_TRUSTDEVICE_RSP_H +#define OHOS_DEVICE_MANAGER_IPC_GET_TRUSTDEVICE_RSP_H + +#include + +#include "dm_device_info.h" + +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetTrustdeviceRsp : public IpcRsp { +DECLARE_IPC_MODEL(IpcGetTrustdeviceRsp); +public: + std::vector GetDeviceVec() const + { + return deviceVec_; + } + + void SetDeviceVec(std::vector& deviceVec) + { + deviceVec_ = deviceVec; + } +private: + std::vector deviceVec_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_GET_TRUSTDEVICE_RSP_H diff --git a/common/include/ipc/model/ipc_notify_auth_result_req.h b/common/include/ipc/model/ipc_notify_auth_result_req.h new file mode 100644 index 000000000..5fb8e53f7 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_auth_result_req.h @@ -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. + */ + +#ifndef OHOS_DEVICE_MANAGER_IPC_NOTIFY_AUTH_RESULT_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_AUTH_RESULT_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyAuthResultReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyAuthResultReq); +public: + std::string GetDeviceId() const + { + return deviceId_; + } + + void SetDeviceId(std::string& deviceId) + { + deviceId_ = deviceId; + } + + int32_t GetPinToken() const + { + return pinToken_; + } + + void SetPinToken(int32_t pinToken) + { + pinToken_ = pinToken; + } + + int32_t GetStatus() const + { + return status_; + } + + void SetStatus(int32_t status) + { + status_ = status; + } + + int32_t GetReason() const + { + return reason_; + } + + void SetReason(int32_t reason) + { + reason_ = reason; + } +private: + std::string deviceId_; + int32_t pinToken_; + int32_t status_; + int32_t reason_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_AUTH_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_notify_check_auth_result_req.h b/common/include/ipc/model/ipc_notify_check_auth_result_req.h new file mode 100644 index 000000000..f98db1e21 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_check_auth_result_req.h @@ -0,0 +1,64 @@ +/* + * 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_NOTIFY_CHECK_AUTH_RESULT_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_CHECK_AUTH_RESULT_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyCheckAuthResultReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyCheckAuthResultReq); +public: + std::string GetDeviceId() const + { + return deviceId_; + } + + void SetDeviceId(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_; + int32_t flag_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_CHECK_AUTH_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_notify_device_found_req.h b/common/include/ipc/model/ipc_notify_device_found_req.h new file mode 100644 index 000000000..7ffb99d88 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_device_found_req.h @@ -0,0 +1,53 @@ +/* + * 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_NOTIFY_DEVICE_FOUND_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_DEVICE_FOUND_REQ_H + +#include "dm_device_info.h" + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyDeviceFoundReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyDeviceFoundReq); +public: + const DmDeviceInfo& GetDeviceInfo() const + { + return dmDeviceInfo_; + } + + void SetDeviceInfo(DmDeviceInfo& dmDeviceInfo) + { + dmDeviceInfo_ = dmDeviceInfo; + } + + uint16_t GetSubscribeId() const + { + return subscribeId_; + } + + void SetSubscribeId(uint16_t subscribeId) + { + subscribeId_ = subscribeId; + } +private: + uint16_t subscribeId_; + DmDeviceInfo dmDeviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_DEVICE_FOUND_REQ_H diff --git a/common/include/ipc/model/ipc_notify_device_state_req.h b/common/include/ipc/model/ipc_notify_device_state_req.h new file mode 100644 index 000000000..2ae13ea04 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_device_state_req.h @@ -0,0 +1,53 @@ +/* + * 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_NOTIFY_DEVICE_STATE_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_DEVICE_STATE_REQ_H + +#include "dm_device_info.h" + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyDeviceStateReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyDeviceStateReq); +public: + int32_t GetDeviceState() const + { + return deviceState_; + } + + void SetDeviceState(int32_t deviceState) + { + deviceState_ = deviceState; + } + + const DmDeviceInfo& GetDeviceInfo() const + { + return dmDeviceInfo_; + } + + void SetDeviceInfo(DmDeviceInfo& dmDeviceInfo) + { + dmDeviceInfo_ = dmDeviceInfo; + } +private: + int32_t deviceState_; + DmDeviceInfo dmDeviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_DEVICE_STATE_REQ_H diff --git a/common/include/ipc/model/ipc_notify_discover_result_req.h b/common/include/ipc/model/ipc_notify_discover_result_req.h new file mode 100644 index 000000000..00ea2e5d8 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_discover_result_req.h @@ -0,0 +1,53 @@ +/* + * 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_NOTIFY_DISCOVER_RESULT_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_DISCOVER_RESULT_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyDiscoverResultReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyDiscoverResultReq); +public: + uint16_t GetSubscribeId() const + { + return subscribeId_; + } + + void SetSubscribeId(uint16_t subscribeId) + { + subscribeId_ = subscribeId; + } + + int32_t GetResult() const + { + return result_; + } + + void SetResult(int32_t result) + { + result_ = result; + } +private: + uint16_t subscribeId_; + int32_t result_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_DISCOVER_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_notify_dmfa_result_req.h b/common/include/ipc/model/ipc_notify_dmfa_result_req.h new file mode 100644 index 000000000..adbcf86cd --- /dev/null +++ b/common/include/ipc/model/ipc_notify_dmfa_result_req.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_DEVICE_MANAGER_IPC_NOTIFY_DMFA_RESULT_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_NOTIFY_DMFA_RESULT_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyDMFAResultReq : public IpcReq { +DECLARE_IPC_MODEL(IpcNotifyDMFAResultReq); +public: + std::string GetPkgName() const + { + return PackageName_; + } + + void SetPkgName(std::string& pkgName) + { + PackageName_ = pkgName; + } + + std::string GetJsonParam() const + { + return JsonParam_; + } + + void SetJsonParam(std::string& JsonParam) + { + JsonParam_ = JsonParam; + } + + +private: + std::string PackageName_; + std::string JsonParam_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_NOTIFY_AUTH_RESULT_REQ_H diff --git a/common/include/ipc/model/ipc_register_listener_req.h b/common/include/ipc/model/ipc_register_listener_req.h new file mode 100644 index 000000000..67690a5b8 --- /dev/null +++ b/common/include/ipc/model/ipc_register_listener_req.h @@ -0,0 +1,62 @@ +/* + * 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_REGISTER_LISTENER_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_REGISTER_LISTENER_REQ_H + +#include "ipc_req.h" + +#ifdef LITE_DEVICE +#include "liteipc_adapter.h" +#else +#include "iremote_stub.h" +#endif + +namespace OHOS { +namespace DistributedHardware { +class IpcRegisterListenerReq : public IpcReq { +DECLARE_IPC_MODEL(IpcRegisterListenerReq); +public: +#ifdef LITE_DEVICE + SvcIdentity GetSvcIdentity() const + { + return svcIdentity_; + } + + void SetSvcIdentity(SvcIdentity svcIdentity) + { + svcIdentity_ = svcIdentity; + } +#else + sptr GetListener() const + { + return listener_; + } + + void SetListener(sptr listener) + { + listener_ = listener; + } +#endif +private: +#ifdef LITE_DEVICE + SvcIdentity svcIdentity_; +#else + sptr listener_; +#endif +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_REGISTER_LISTENER_REQ_H diff --git a/common/include/ipc/model/ipc_req.h b/common/include/ipc/model/ipc_req.h new file mode 100644 index 000000000..ac6e99d80 --- /dev/null +++ b/common/include/ipc/model/ipc_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_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_REQ_H + +#include + +#include "ipc_def.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcReq { +DECLARE_IPC_MODEL(IpcReq); +public: + const std::string& GetPkgName() const + { + return pkgName_; + } + + void SetPkgName(std::string &pkgName) + { + pkgName_ = pkgName; + } +private: + std::string pkgName_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_REQ_H diff --git a/common/include/ipc/model/ipc_rsp.h b/common/include/ipc/model/ipc_rsp.h new file mode 100644 index 000000000..14394280b --- /dev/null +++ b/common/include/ipc/model/ipc_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_RSP_H +#define OHOS_DEVICE_MANAGER_IPC_RSP_H + +#include + +#include "ipc_def.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRsp { +DECLARE_IPC_MODEL(IpcRsp); +public: + int32_t GetErrCode() const + { + return errCode_; + } + + void SetErrCode(int32_t errCode) + { + errCode_ = errCode; + } +private: + int32_t errCode_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_RSP_H diff --git a/common/include/ipc/model/ipc_set_useroperation_req.h b/common/include/ipc/model/ipc_set_useroperation_req.h new file mode 100644 index 000000000..7c7416955 --- /dev/null +++ b/common/include/ipc/model/ipc_set_useroperation_req.h @@ -0,0 +1,41 @@ +/* + * 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_USER_OPERATION_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_GET_USER_OPERATION_REQ_H +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetOperationReq : public IpcReq { +DECLARE_IPC_MODEL(IpcGetOperationReq); +public: + int32_t GetOperation() const + { + return action_; + } + + void SetOperation(int32_t action) + { + action_ = action; + } + +private: + int32_t action_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_GET_USER_OPERATION_REQ_H diff --git a/common/include/ipc/model/ipc_start_discovery_req.h b/common/include/ipc/model/ipc_start_discovery_req.h new file mode 100644 index 000000000..341641e61 --- /dev/null +++ b/common/include/ipc/model/ipc_start_discovery_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_START_DISCOVERY_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_START_DISCOVERY_REQ_H + +#include "ipc_req.h" + +#include "dm_subscribe_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcStartDiscoveryReq : public IpcReq { +DECLARE_IPC_MODEL(IpcStartDiscoveryReq); +public: + const DmSubscribeInfo& GetSubscribeInfo() const + { + return subscribeInfo_; + } + + void SetSubscribeInfo(DmSubscribeInfo &subscribeInfo) + { + subscribeInfo_ = subscribeInfo; + } +private: + DmSubscribeInfo subscribeInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_START_DISCOVERY_REQ_H diff --git a/common/include/ipc/model/ipc_stop_discovery_req.h b/common/include/ipc/model/ipc_stop_discovery_req.h new file mode 100644 index 000000000..3497ec6f3 --- /dev/null +++ b/common/include/ipc/model/ipc_stop_discovery_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_STOP_DISCOVERY_REQ_H +#define OHOS_DEVICE_MANAGER_IPC_STOP_DISCOVERY_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcStopDiscoveryReq : public IpcReq { +DECLARE_IPC_MODEL(IpcStopDiscoveryReq); +public: + uint16_t GetSubscribeId() const + { + return subscribeId_; + } + + void SetSubscribeId(uint16_t subscribeId) + { + subscribeId_ = subscribeId; + } +private: + uint16_t subscribeId_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_STOP_DISCOVERY_REQ_H diff --git a/interfaces/inner_kits/native_cpp/include/ipc/ipc_client.h b/interfaces/inner_kits/native_cpp/include/ipc/ipc_client.h new file mode 100644 index 000000000..818746ff7 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/ipc_client.h @@ -0,0 +1,37 @@ +/* + * 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_CLIENT_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_H + +#include +#include +#include + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClient { +DECLARE_IPC_INTERFACE(IpcClient); +public: + virtual int32_t Init(std::string &pkgName) = 0; + virtual int32_t UnInit(std::string &pkgName) = 0; + virtual int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/ipc/ipc_client_proxy.h b/interfaces/inner_kits/native_cpp/include/ipc/ipc_client_proxy.h new file mode 100644 index 000000000..faa9e1a4b --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/ipc_client_proxy.h @@ -0,0 +1,41 @@ +/* + * 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_CLIENT_PROXY_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_PROXY_H + +#include +#include + +#include "ipc_client.h" +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientProxy : public IpcClient { +DECLARE_IPC_INTERFACE(IpcClientProxy); +public: + IpcClientProxy(std::shared_ptr ipcClientManager) : ipcClientManager_(ipcClientManager) {}; +public: + virtual int32_t Init(std::string &pkgName); + virtual int32_t UnInit(std::string &pkgName); + virtual int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); +private: + std::shared_ptr ipcClientManager_ {nullptr}; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_PROXY_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_manager.h new file mode 100644 index 000000000..997b5d39b --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_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_DEVICE_MANAGER_IPC_CLIENT_MANAGER_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_MANAGER_H + +#include +#include +#include + +#include "ipc_client.h" +#include "ipc_client_server_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientManager : public IpcClient { +DECLARE_IPC_INTERFACE(IpcClientManager); +public: + virtual int32_t Init(std::string &pkgName) override; + virtual int32_t UnInit(std::string &pkgName) override; + virtual int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; +private: + bool IsInit(std::string &pkgName); +private: + IpcClientServerProxy serverProxy_; + std::set packageInitSet_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_server_proxy.h b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_server_proxy.h new file mode 100644 index 000000000..d7497c55c --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_server_proxy.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_DEVICE_MANAGER_IPC_CLIENT_SERVER_PROXY_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_SERVER_PROXY_H + +#include +#include +#include + +#include "iproxy_client.h" +#include "liteipc_adapter.h" +#include "samgr_lite.h" + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientServerProxy { +public: + int32_t Init(); + int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); +private: + IClientProxy *GetServerProxy(void); + int32_t RegisterServerDeathCb(void); +private: + std::mutex lock_; + IClientProxy *serviceProxy_ {nullptr}; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_SERVER_PROXY_H diff --git a/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_stub.h b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_stub.h new file mode 100644 index 000000000..672e4cb89 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/lite/ipc_client_stub.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_CLIENT_STUB_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_STUB_H + +#include +#include + +#include "liteipc_adapter.h" + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientStub { +DECLARE_SINGLE_INSTANCE(IpcClientStub); +public: + int32_t Init(); + SvcIdentity GetSvcIdentity() const + { + return clientIdentity_; + } +private: + std::mutex lock_; + bool bInit {false}; + SvcIdentity clientIdentity_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_STUB_H diff --git a/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_manager.h b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_manager.h new file mode 100644 index 000000000..422f29728 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_manager.h @@ -0,0 +1,58 @@ +/* + * 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_CLIENT_MANAGER_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_MANAGER_H + +#include +#include +#include +#include + +#include "iremote_object.h" + +#include "ipc_client.h" +#include "ipc_client_stub.h" +#include "ipc_def.h" +#include "ipc_remote_broker.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr &remote) override; + DmDeathRecipient() = default; + ~DmDeathRecipient() = default; +}; + +class IpcClientManager : public IpcClient { +friend class DmDeathRecipient; +DECLARE_IPC_INTERFACE(IpcClientManager); +public: + virtual int32_t Init(std::string &pkgName) override; + virtual int32_t UnInit(std::string &pkgName) override; + virtual int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; +private: + bool IsInit(std::string &pkgName); + int32_t ClientInit(); +private: + std::mutex lock_; + std::map> dmListener_; + sptr dmInterface_ {nullptr}; + sptr dmRecipient_ {nullptr}; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_MANAGER_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_server_proxy.h b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_server_proxy.h new file mode 100644 index 000000000..3fd0586cd --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_server_proxy.h @@ -0,0 +1,39 @@ +/* + * 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_CLIENT_SERVER_PROXY_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_SERVER_PROXY_H + +#include +#include + +#include "iremote_proxy.h" +#include "ipc_remote_broker.h" +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientServerProxy : public IRemoteProxy { +public: + explicit IpcClientServerProxy(const sptr &impl) : IRemoteProxy(impl) {}; + ~IpcClientServerProxy() {}; + int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_SERVER_PROXY_H diff --git a/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_stub.h b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_stub.h new file mode 100644 index 000000000..61633f6e8 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_client_stub.h @@ -0,0 +1,40 @@ +/* + * 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_CLIENT_STUB_H +#define OHOS_DEVICE_MANAGER_IPC_CLIENT_STUB_H + +#include +#include + +#include "iremote_stub.h" +#include "iremote_broker.h" + +#include "ipc_remote_broker.h" +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcClientStub : public IRemoteStub { +public: + IpcClientStub() {}; + ~IpcClientStub() {}; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CLIENT_STUB_H diff --git a/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_remote_broker.h b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_remote_broker.h new file mode 100644 index 000000000..b2b8618c7 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/ipc/standard/ipc_remote_broker.h @@ -0,0 +1,38 @@ +/* + * 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_REMOTE_BROKER_H +#define OHOS_DEVICE_MANAGER_IPC_REMOTE_BROKER_H + +#include +#include + +#include "iremote_broker.h" + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRemoteBroker : public OHOS::IRemoteBroker { +public: + virtual ~IpcRemoteBroker() {} + virtual int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) = 0; +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.devicemanager"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_REMOTE_BROKER_H diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h new file mode 100644 index 000000000..14dc0dcf6 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -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. + */ + +#ifndef OHOS_DEVICE_MANAGER_NOTIFY_H +#define OHOS_DEVICE_MANAGER_NOTIFY_H + +#include +#include +#include +#include + +#include "dm_device_info.h" +#include "dm_subscribe_info.h" +#include "device_manager_callback.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerNotify { +DECLARE_SINGLE_INSTANCE(DeviceManagerNotify); +public: + void RegisterDeathRecipientCallback(std::string &pkgName, std::shared_ptr dmInitCallback); + void UnRegisterDeathRecipientCallback(std::string &pkgName); + void RegisterDeviceStateCallback(std::string &pkgName, std::shared_ptr callback); + void UnRegisterDeviceStateCallback(std::string &pkgName); + void RegisterDiscoverCallback(std::string &pkgName, uint16_t subscribeId, + std::shared_ptr callback); + void UnRegisterDiscoverCallback(std::string &pkgName, uint16_t subscribeId); + void RegisterAuthenticateCallback(std::string &pkgName, std::string &deviceId, + std::shared_ptr callback); + void UnRegisterAuthenticateCallback(std::string &pkgName, std::string &deviceId); + void UnRegisterPackageCallback(std::string &pkgName); + void RegisterCheckAuthenticationCallback(std::string &pkgName, std::string &authPara, + std::shared_ptr callback); + void UnRegisterCheckAuthenticationCallback(std::string &pkgName); + void RegisterDeviceManagerFaCallback(std::string &packageName, + std::shared_ptr callback); + void UnRegisterDeviceManagerFaCallback(std::string &pkgName); + +public: + void OnRemoteDied(); + void OnDeviceOnline(std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceOffline(std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceChanged(std::string &pkgName, const DmDeviceInfo &deviceInfo); + void OnDeviceFound(std::string &pkgName, uint16_t subscribeId, const DmDeviceInfo &deviceInfo); + void OnDiscoverFailed(std::string &pkgName, uint16_t subscribeId, int32_t failedReason); + void OnDiscoverySuccess(std::string &pkgName, uint16_t subscribeId); + void OnAuthResult(std::string &pkgName, std::string &deviceId, int32_t pinToken, uint32_t status, + uint32_t reason); + void OnCheckAuthResult(std::string &pkgName, std::string &deviceId, int32_t resultCode, int32_t flag); + void OnFaCall(std::string &pkgName,std::string ¶mJson); +private: + std::mutex lock_; + std::map> deviceStateCallback_; + std::map>> deviceDiscoverCallbacks_; + std::map>> authenticateCallback_; + std::map>> checkauthcallback_; + std::map> dmInitCallback_; + std::map> dmFaCallback_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_NOTIFY_H diff --git a/interfaces/inner_kits/native_cpp/src/ipc/ipc_client_proxy.cpp b/interfaces/inner_kits/native_cpp/src/ipc/ipc_client_proxy.cpp new file mode 100644 index 000000000..77cf6559d --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/ipc_client_proxy.cpp @@ -0,0 +1,48 @@ +/* + * 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 "ipc_client_proxy.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcClientProxy::Init(std::string &pkgName) +{ + if (ipcClientManager_ == nullptr) { + return DEVICEMANAGER_NULLPTR; + } + return ipcClientManager_->Init(pkgName); +} + +int32_t IpcClientProxy::UnInit(std::string &pkgName) +{ + if (ipcClientManager_ == nullptr) { + return DEVICEMANAGER_NULLPTR; + } + return ipcClientManager_->UnInit(pkgName); +} + +int32_t IpcClientProxy::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + if (req == nullptr || rsp == nullptr || ipcClientManager_ == nullptr) { + DMLOG(DM_LOG_INFO, "req,rsp or ipc client is null"); + return DEVICEMANAGER_NULLPTR; + } + return ipcClientManager_->SendRequest(cmdCode, req, rsp); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp new file mode 100644 index 000000000..26551b435 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_manager.cpp @@ -0,0 +1,92 @@ +/* + * 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 "ipc_client_manager.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +#include "ipc_client_stub.h" +#include "ipc_register_listener_req.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcClientManager::Init(std::string &pkgName) +{ + if (IsInit(pkgName)) { + DMLOG(DM_LOG_INFO, "already init"); + return DEVICEMANAGER_OK; + } + if (serverProxy_.Init() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "server proxy init failed."); + return DEVICEMANAGER_INIT_FAILED; + } + if (IpcClientStub::GetInstance().Init() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "ipcclientstub init failed."); + return DEVICEMANAGER_INIT_FAILED; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetSvcIdentity(IpcClientStub::GetInstance().GetSvcIdentity()); + int32_t ret = serverProxy_.SendCmd(REGISTER_DEVICE_MANAGER_LISTENER, req, rsp); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "InitDeviceManager: RegisterDeviceManagerListener Failed with ret %d", ret); + return ret; + } + ret = rsp->GetErrCode(); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "DeviceManager::InitDeviceManager completed, pkgName: %s, ret=%d", pkgName.c_str(), ret); + return ret; + } + packageInitSet_.emplace(pkgName); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientManager::UnInit(std::string &pkgName) +{ + DMLOG(DM_LOG_INFO, "UnInitDeviceManager in, pkgName %s", pkgName.c_str()); + if (!IsInit(pkgName)) { + return DEVICEMANAGER_FAILED; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + int32_t ret = serverProxy_.SendCmd(UNREGISTER_DEVICE_MANAGER_LISTENER, req, rsp); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "UnRegisterDeviceManagerListener Failed with ret %d", ret); + return ret; + } + packageInitSet_.erase(pkgName); + DMLOG(DM_LOG_INFO, "UnInitDeviceManager SUCCESS"); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + std::string pkgName = req->GetPkgName(); + if (!IsInit(pkgName)) { + return DEVICEMANAGER_SERVICE_NOT_READY; + } + return serverProxy_.SendCmd(cmdCode, req, rsp); +} + +bool IpcClientManager::IsInit(std::string &pkgName) +{ + return (packageInitSet_.count(pkgName) > 0); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp new file mode 100644 index 000000000..88a0464dc --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp @@ -0,0 +1,134 @@ +/* + * 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 "ipc_client_server_proxy.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" +#include "device_manager_notify.h" + +#include "ipc_def.h" +#include "ipc_cmd_register.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const int32_t INVALID_CB_ID = 0xFF; +} +static uint32_t g_deathCbId = INVALID_CB_ID; +static SvcIdentity g_svcIdentity; +static std::shared_ptr pCurRsp; + +void __attribute__((weak)) HOS_SystemInit(void) +{ + SAMGR_Bootstrap(); + return; +} + +int32_t DMDeathCallback(const IpcContext *ctx, void *ipcMsg, IpcIo *data, void *arg) +{ + (void)ctx; + (void)ipcMsg; + (void)data; + (void)arg; + DMLOG(DM_LOG_INFO, "ATTENTION SERVICE (%s) DEAD !!!\n", DEVICE_MANAGER_SERVICE_NAME); + UnregisterDeathCallback(g_svcIdentity, g_deathCbId); + g_deathCbId = INVALID_CB_ID; + g_svcIdentity.handle = 0; + g_svcIdentity.token = 0; + g_svcIdentity.cookie = 0; + DeviceManagerNotify::GetInstance().OnRemoteDied(); + return DEVICEMANAGER_OK; +} + +static int32_t SendCmdResultCb(IOwner owner, int code, IpcIo *reply) +{ + int32_t cmdCode = *(int32_t *)owner; + DMLOG(DM_LOG_INFO, "SendCmdResultCb code:%d", cmdCode); + (void)IpcCmdRegister::GetInstance().ReadResponse(cmdCode, *reply, pCurRsp); + return DEVICEMANAGER_OK; +} + +IClientProxy *IpcClientServerProxy::GetServerProxy(void) +{ + IClientProxy *clientProxy = nullptr; + IUnknown *iUnknown = nullptr; + + DMLOG(DM_LOG_INFO, "start get client proxy"); + iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(DEVICE_MANAGER_SERVICE_NAME); + if (iUnknown == nullptr) { + return nullptr; + } + if (iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&clientProxy) != DEVICEMANAGER_OK || + clientProxy == nullptr) { + DMLOG(DM_LOG_ERROR, "QueryInterface failed"); + } + return clientProxy; +} + +int IpcClientServerProxy::RegisterServerDeathCb(void) +{ + g_svcIdentity = SAMGR_GetRemoteIdentity(DEVICE_MANAGER_SERVICE_NAME, nullptr); + g_deathCbId = INVALID_CB_ID; + if (RegisterDeathCallback(nullptr, g_svcIdentity, DMDeathCallback, nullptr, &g_deathCbId) != EC_SUCCESS) { + DMLOG(DM_LOG_ERROR, "reg death callback failed"); + return DEVICEMANAGER_FAILED; + } + return DEVICEMANAGER_OK; +} + +int32_t IpcClientServerProxy::SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + DMLOG(DM_LOG_INFO, "SendCmd:%d", cmdCode); + uint8_t data[MAX_DM_IPC_LEN] = {0}; + IpcIo request; + + if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, request, data, MAX_DM_IPC_LEN) != DEVICEMANAGER_OK) { + return DEVICEMANAGER_FAILED; + } + { + std::lock_guard autoLock(lock_); + pCurRsp = rsp; + if (serviceProxy_ != nullptr && + serviceProxy_->Invoke(serviceProxy_, cmdCode, &request, &cmdCode, SendCmdResultCb) != 0) { + DMLOG(DM_LOG_ERROR, "serviceProxy_ invoke failed."); + return DEVICEMANAGER_FAILED; + } + } + DMLOG(DM_LOG_INFO, "SendCmd:%d end", cmdCode); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientServerProxy::Init(void) +{ + if (serviceProxy_ != nullptr) { + DMLOG(DM_LOG_INFO, "ServerProxy already Init"); + return DEVICEMANAGER_OK; + } + HOS_SystemInit(); + serviceProxy_ = GetServerProxy(); + if (serviceProxy_ == nullptr) { + DMLOG(DM_LOG_ERROR, "get ipc client proxy failed"); + return DEVICEMANAGER_FAILED; + } + if (RegisterServerDeathCb() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "register server death cb failed"); + return DEVICEMANAGER_FAILED; + } + DMLOG(DM_LOG_INFO, "ServerProxyInit ok"); + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp new file mode 100644 index 000000000..9ae99e19a --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_stub.cpp @@ -0,0 +1,57 @@ +/* + * 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 "ipc_client_stub.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "device_manager_notify.h" + +#include "ipc_def.h" +#include "ipc_cmd_register.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcClientStub); + +static int32_t ClientIpcInterfaceMsgHandle(const IpcContext *ctx, void *ipcMsg, IpcIo *io, void *arg) +{ + (void)arg; + if (ipcMsg == nullptr || io == nullptr || ctx == nullptr) { + DMLOG(DM_LOG_ERROR, "invalid param"); + return DEVICEMANAGER_INVALID_PARAM; + } + + uint32_t code = 0; + GetCode(ipcMsg, &code); + DMLOG(DM_LOG_INFO, "receive ipc transact code(%u)", code); + return IpcCmdRegister::GetInstance().OnIpcCmd(code, *io, *ctx, ipcMsg); +} + +int32_t IpcClientStub::Init() +{ + std::lock_guard autoLock(lock_); + if (bInit) { + return DEVICEMANAGER_OK; + } + if (RegisterIpcCallback(ClientIpcInterfaceMsgHandle, 0, IPC_WAIT_FOREVER, &clientIdentity_, nullptr) != 0) { + DMLOG(DM_LOG_ERROR, "register ipc cb failed"); + return DEVICEMANAGER_FAILED; + } + bInit = true; + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp new file mode 100644 index 000000000..79dc594be --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_cmd_parser.cpp @@ -0,0 +1,287 @@ +/* + * 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 "ipc_cmd_register.h" +#include "ipc_def.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "device_manager_notify.h" + +#include "dm_device_info.h" +#include "dm_subscribe_info.h" + +#include "ipc_register_listener_req.h" +#include "ipc_start_discovery_req.h" +#include "ipc_stop_discovery_req.h" +#include "ipc_get_trustdevice_req.h" +#include "ipc_get_trustdevice_rsp.h" +#include "ipc_authenticate_device_req.h" +#include "ipc_check_authenticate_req.h" + +namespace OHOS { +namespace DistributedHardware { +ON_IPC_SET_REQUEST(REGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + SvcIdentity svcIdentity = pReq->GetSvcIdentity(); + + IpcIoInit(&request, buffer, buffLen, 1); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushSvc(&request, &svcIdentity); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::string pkgName = pBaseReq->GetPkgName(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(GET_TRUST_DEVICE_LIST, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushString(&request, extra.c_str()); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(GET_TRUST_DEVICE_LIST, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + int32_t deviceNum = IpcIoPopInt32(&reply); + uint32_t deviceTotalSize = deviceNum * (int32_t)sizeof(DmDeviceInfo); + + if (deviceTotalSize > 0) { + std::vector deviceInfoVec; + DmDeviceInfo *pDmDeviceinfo = (DmDeviceInfo *)IpcIoPopFlatObj(&reply, &deviceTotalSize); + if (pDmDeviceinfo == nullptr) { + DMLOG(DM_LOG_ERROR, "GetTrustedDeviceList read node info failed!"); + pRsp->SetErrCode(DEVICEMANAGER_IPC_TRANSACTION_FAILED); + return DEVICEMANAGER_IPC_TRANSACTION_FAILED; + } + for (int32_t i = 0; i < deviceNum; ++i) { + pDmDeviceinfo = pDmDeviceinfo + i; + deviceInfoVec.emplace_back(*pDmDeviceinfo); + } + pRsp->SetDeviceVec(deviceInfoVec); + } + pRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(START_DEVICE_DISCOVER, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + const DmSubscribeInfo dmSubscribeInfo = pReq->GetSubscribeInfo(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushFlatObj(&request, &dmSubscribeInfo, sizeof(DmSubscribeInfo)); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(START_DEVICE_DISCOVER, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(STOP_DEVICE_DISCOVER, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushUint16(&request, subscribeId); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(STOP_DEVICE_DISCOVER, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(AUTHENTICATE_DEVICE, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushString(&request, extra.c_str()); + IpcIoPushFlatObj(&request, &deviceInfo, sizeof(DmDeviceInfo)); + // L1 暂时没有考虑appimage校验(8k限制) + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(AUTHENTICATE_DEVICE, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(CHECK_AUTHENTICATION, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string authPara = pReq->GetAuthPara(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, authPara.c_str()); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(CHECK_AUTHENTICATION, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&reply, &len); + DmDeviceState deviceState = static_cast(IpcIoPopInt32(&reply)); + uint32_t size; + const DmDeviceInfo *deviceInfo = (const DmDeviceInfo*)IpcIoPopFlatObj(&reply, &size); + if (pkgName == "" || len == 0 || deviceInfo == NULL) { + DMLOG(DM_LOG_ERROR, "OnDeviceOnline, get para failed"); + FreeBuffer(&ctx, ipcMsg); + return; + } + switch (deviceState) { + case DEVICE_STATE_ONLINE: + DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, *deviceInfo); + break; + case DEVICE_STATE_OFFLINE: + DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, *deviceInfo); + break; + case DEVICE_INFO_CHANGED: + DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, *deviceInfo); + break; + default: + DMLOG(DM_LOG_ERROR, "unknown device state:%d", deviceState); + break; + } + FreeBuffer(&ctx, ipcMsg); +} + +ON_IPC_CMD(SERVER_DEVICE_FOUND, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&reply, &len); + uint16_t subscribeId = IpcIoPopUint16(&reply); + uint32_t size; + const DmDeviceInfo *deviceInfo = (const DmDeviceInfo*)IpcIoPopFlatObj(&reply, &size); + if (pkgName == "" || len == 0 || deviceInfo == NULL) { + DMLOG(DM_LOG_ERROR, "OnDeviceChanged, get para failed"); + FreeBuffer(&ctx, ipcMsg); + return; + } + DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, *deviceInfo); + FreeBuffer(&ctx, ipcMsg); +} + +ON_IPC_CMD(SERVER_DISCOVER_FINISH, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&reply, &len); + uint16_t subscribeId = IpcIoPopUint16(&reply); + int32_t failedReason = IpcIoPopInt32(&reply); + + if (pkgName == "" || len == 0) { + DMLOG(DM_LOG_ERROR, "OnDiscoverySuccess, get para failed"); + FreeBuffer(&ctx, ipcMsg); + return; + } + if (failedReason == DEVICEMANAGER_OK) { + DeviceManagerNotify::GetInstance().OnDiscoverySuccess(pkgName, subscribeId); + } else { + DeviceManagerNotify::GetInstance().OnDiscoverFailed(pkgName, subscribeId, failedReason); + } + FreeBuffer(&ctx, ipcMsg); +} + +ON_IPC_CMD(SERVER_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&reply, &len); + size_t devIdLen = 0; + std::string deviceId = (const char *)IpcIoPopString(&reply, &devIdLen); + int32_t pinToken = IpcIoPopInt32(&reply); + int32_t status = IpcIoPopInt32(&reply); + int32_t reason = IpcIoPopInt32(&reply); + + if (pkgName == "" || len == 0 || deviceId == "" || devIdLen == 0) { + DMLOG(DM_LOG_ERROR, "OnAuthResult, get para failed"); + FreeBuffer(&ctx, ipcMsg); + return; + } + DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, pinToken, status, reason); + FreeBuffer(&ctx, ipcMsg); +} + +ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&reply, &len); + size_t devIdLen = 0; + std::string deviceId = (const char *)IpcIoPopString(&reply, &devIdLen); + int32_t resultCode = IpcIoPopInt32(&reply); + int32_t flag = IpcIoPopInt32(&reply); + + if (pkgName == "" || len == 0 || deviceId == "" || devIdLen == 0) { + DMLOG(DM_LOG_ERROR, "OnAuthResult, get para failed"); + FreeBuffer(&ctx, ipcMsg); + return; + } + DeviceManagerNotify::GetInstance().OnCheckAuthResult(pkgName, deviceId, resultCode, flag); + FreeBuffer(&ctx, ipcMsg); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp new file mode 100644 index 000000000..26d9fb927 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_manager.cpp @@ -0,0 +1,152 @@ +/* + * 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 "ipc_client_manager.h" + +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "device_manager_notify.h" + +#include "ipc_register_listener_req.h" + +namespace OHOS { +namespace DistributedHardware { +void DmDeathRecipient::OnRemoteDied(const wptr &remote) +{ + (void)remote; + DMLOG(DM_LOG_WARN, "DmDeathRecipient : OnRemoteDied"); + DeviceManagerNotify::GetInstance().OnRemoteDied(); +} + +int32_t IpcClientManager::ClientInit() +{ + DMLOG(DM_LOG_INFO, "InitDeviceManagerService start"); + if (dmInterface_ != nullptr) { + DMLOG(DM_LOG_INFO, "DeviceManagerService Already Init"); + return DEVICEMANAGER_OK; + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + DMLOG(DM_LOG_ERROR, "Get SystemAbilityManager Failed"); + return DEVICEMANAGER_SERVICE_NOT_READY; + } + + auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + if (object == nullptr) { + DMLOG(DM_LOG_ERROR, "Get DeviceManager SystemAbility Failed"); + return DEVICEMANAGER_SERVICE_NOT_READY; + } + + if (dmRecipient_ == nullptr) { + dmRecipient_ = sptr(new DmDeathRecipient()); + } + if (!object->AddDeathRecipient(dmRecipient_)) { + DMLOG(DM_LOG_ERROR, "InitDeviceManagerService: AddDeathRecipient Failed"); + } + dmInterface_ = iface_cast(object); + DMLOG(DM_LOG_INFO, "DeviceManager::InitDeviceManagerService completed"); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientManager::Init(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + int32_t ret = ClientInit(); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "InitDeviceManager Failed with ret %d", ret); + return ret; + } + if (dmListener_.count(pkgName) > 0) { + DMLOG(DM_LOG_INFO, "dmListener_ Already Init"); + return DEVICEMANAGER_OK; + } + + sptr listener = sptr(new IpcClientStub()); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetListener(listener); + ret = dmInterface_->SendCmd(REGISTER_DEVICE_MANAGER_LISTENER, req, rsp); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "InitDeviceManager: RegisterDeviceManagerListener Failed with ret %d", ret); + return ret; + } + ret = rsp->GetErrCode(); + if (ret != DEVICEMANAGER_OK) { + return ret; + } + dmListener_[pkgName] = listener; + DMLOG(DM_LOG_INFO, "completed, pkgName: %s", pkgName.c_str()); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientManager::UnInit(std::string &pkgName) +{ + DMLOG(DM_LOG_INFO, "in, pkgName %s", pkgName.c_str()); + if (dmInterface_ == nullptr) { + DMLOG(DM_LOG_ERROR, "DeviceManager not Init"); + return DEVICEMANAGER_SERVICE_NOT_READY; + } + + std::lock_guard autoLock(lock_); + if (dmListener_.count(pkgName) > 0) { + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + int32_t ret = dmInterface_->SendCmd(UNREGISTER_DEVICE_MANAGER_LISTENER, req, rsp); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "UnRegisterDeviceManagerListener Failed with ret %d", ret); + return ret; + } + dmListener_.erase(pkgName); + } + if (dmListener_.empty()) { + dmInterface_ = nullptr; + dmRecipient_ = nullptr; + } + DMLOG(DM_LOG_INFO, "completed, pkgName: %s", pkgName.c_str()); + return DEVICEMANAGER_OK; +} + +int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + std::string pkgName = req->GetPkgName(); + if (!IsInit(pkgName)) { + return DEVICEMANAGER_SERVICE_NOT_READY; + } + return dmInterface_->SendCmd(cmdCode, req, rsp); +} + +bool IpcClientManager::IsInit(std::string &pkgName) +{ + if (dmInterface_ == nullptr) { + DMLOG(DM_LOG_ERROR, "DeviceManager not Init"); + return false; + } + std::lock_guard autoLock(lock_); + if (dmListener_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "dmListener_ not Init for %s", pkgName.c_str()); + return false; + } + return true; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_server_proxy.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_server_proxy.cpp new file mode 100644 index 000000000..75d0d1923 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_server_proxy.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 "ipc_client_server_proxy.h" + +#include "ipc_cmd_register.h" +#include "ipc_types.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcClientServerProxy::SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DMLOG(DM_LOG_ERROR, "remote service null"); + return DEVICEMANAGER_NULLPTR; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data) != DEVICEMANAGER_OK) { + return DEVICEMANAGER_FAILED; + } + if (remote->SendRequest(cmdCode, data, reply, option) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "SendRequest fail, cmd:%d", cmdCode); + return DEVICEMANAGER_IPC_FAILED; + } + return IpcCmdRegister::GetInstance().ReadResponse(cmdCode, reply, rsp); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_stub.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_stub.cpp new file mode 100644 index 000000000..cbe30dd5b --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_stub.cpp @@ -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. + */ + +#include "ipc_client_stub.h" + +#include "ipc_skeleton.h" +#include "ipc_types.h" + +#include "ipc_cmd_register.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DMLOG(DM_LOG_INFO, "code = %d, flags= %d.", code, option.GetFlags()); + if (IpcCmdRegister::GetInstance().OnIpcCmd(code, data, reply) == DEVICEMANAGER_OK) { + return DEVICEMANAGER_OK; + } + DMLOG(DM_LOG_WARN, "unsupport code: %d", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +int32_t IpcClientStub::SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + DMLOG(DM_LOG_ERROR, "error"); + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp new file mode 100644 index 000000000..88217e1bd --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -0,0 +1,390 @@ +/* + * 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 "ipc_cmd_register.h" + +#include "securec.h" +#include "constants.h" +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "device_manager_notify.h" + +#include "ipc_def.h" +#include "ipc_register_listener_req.h" +#include "ipc_start_discovery_req.h" +#include "ipc_stop_discovery_req.h" +#include "ipc_get_trustdevice_req.h" +#include "ipc_get_trustdevice_rsp.h" +#include "ipc_authenticate_device_req.h" +#include "ipc_check_authenticate_req.h" +#include "ipc_get_authenticationparam_rsp.h" +#include "ipc_set_useroperation_req.h" + +namespace OHOS { +namespace DistributedHardware { +ON_IPC_SET_REQUEST(REGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + sptr listener = pReq->GetListener(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteRemoteObject(listener)) { + DMLOG(DM_LOG_ERROR, "write listener failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::string pkgName = pBaseReq->GetPkgName(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write papam failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(GET_TRUST_DEVICE_LIST, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkg failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteString(extra)) { + DMLOG(DM_LOG_ERROR, "write extra failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(GET_TRUST_DEVICE_LIST, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + int32_t deviceNum = reply.ReadInt32(); + int32_t deviceTotalSize = deviceNum * (int32_t)sizeof(DmDeviceInfo); + if (deviceTotalSize > 0) { + std::vector deviceInfoVec; + DmDeviceInfo *pDmDeviceinfo = (DmDeviceInfo *)reply.ReadRawData(deviceTotalSize); + if (pDmDeviceinfo == nullptr) { + DMLOG(DM_LOG_ERROR, "GetTrustedDeviceList read node info failed!"); + pRsp->SetErrCode(DEVICEMANAGER_IPC_TRANSACTION_FAILED); + return DEVICEMANAGER_IPC_TRANSACTION_FAILED; + } + for (int32_t i = 0; i < deviceTotalSize; ++i) { + pDmDeviceinfo = pDmDeviceinfo + i; + deviceInfoVec.emplace_back(*pDmDeviceinfo); + } + pRsp->SetDeviceVec(deviceInfoVec); + } + pRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(START_DEVICE_DISCOVER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + const DmSubscribeInfo dmSubscribeInfo = pReq->GetSubscribeInfo(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteRawData(&dmSubscribeInfo, sizeof(DmSubscribeInfo))) { + DMLOG(DM_LOG_ERROR, "write subscribe info failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(START_DEVICE_DISCOVER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(STOP_DEVICE_DISCOVER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt16(subscribeId)) { + DMLOG(DM_LOG_ERROR, "write subscribeId failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(STOP_DEVICE_DISCOVER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(AUTHENTICATE_DEVICE, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + DmAppImageInfo imageInfo = pReq->GetAppImageInfo(); + + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteString(extra)) { + DMLOG(DM_LOG_ERROR, "write extra failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) { + DMLOG(DM_LOG_ERROR, "write deviceInfo failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + int32_t appIconLen = imageInfo.GetAppIconLen(); + int32_t appThumbnailLen = imageInfo.GetAppThumbnailLen(); + if (!data.WriteInt32(appIconLen)) { + DMLOG(DM_LOG_ERROR, "write imageinfo appicon len failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(appThumbnailLen)) { + DMLOG(DM_LOG_ERROR, "write imageinfo appThumbnailLen failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (appIconLen > 0 && !data.WriteRawData(imageInfo.GetAppIcon(), appIconLen)) { + DMLOG(DM_LOG_ERROR, "write imageinfo appIcon failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (appThumbnailLen > 0 && !data.WriteRawData(imageInfo.GetAppThumbnail(), appThumbnailLen)) { + DMLOG(DM_LOG_ERROR, "write imageinfo appThumbnail failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(AUTHENTICATE_DEVICE, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(CHECK_AUTHENTICATION, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string authPara = pReq->GetAuthPara(); + if (!data.WriteString(authPara)) { + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(CHECK_AUTHENTICATION, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + DmDeviceState deviceState = static_cast(data.ReadInt32()); + DmDeviceInfo dmDeviceInfo; + size_t deviceSize = sizeof(DmDeviceInfo); + void *deviceInfo = (void *)data.ReadRawData(deviceSize); + if (deviceInfo != nullptr && memcpy_s(&dmDeviceInfo, deviceSize, deviceInfo, deviceSize) != 0) { + reply.WriteInt32(DEVICEMANAGER_COPY_FAILED); + return DEVICEMANAGER_OK; + } + switch (deviceState) { + case DEVICE_STATE_ONLINE: + DeviceManagerNotify::GetInstance().OnDeviceOnline(pkgName, dmDeviceInfo); + break; + case DEVICE_STATE_OFFLINE: + DeviceManagerNotify::GetInstance().OnDeviceOffline(pkgName, dmDeviceInfo); + break; + case DEVICE_INFO_CHANGED: + DeviceManagerNotify::GetInstance().OnDeviceChanged(pkgName, dmDeviceInfo); + break; + default: + DMLOG(DM_LOG_ERROR, "unknown device state:%d", deviceState); + break; + } + reply.WriteInt32(DEVICEMANAGER_OK); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_DEVICE_FOUND, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + uint16_t subscribeId = data.ReadInt16(); + DmDeviceInfo dmDeviceInfo; + size_t deviceSize = sizeof(DmDeviceInfo); + void *deviceInfo = (void *)data.ReadRawData(deviceSize); + if (deviceInfo != nullptr && memcpy_s(&dmDeviceInfo, deviceSize, deviceInfo, deviceSize) != 0) { + reply.WriteInt32(DEVICEMANAGER_COPY_FAILED); + return DEVICEMANAGER_OK; + } + DeviceManagerNotify::GetInstance().OnDeviceFound(pkgName, subscribeId, dmDeviceInfo); + reply.WriteInt32(DEVICEMANAGER_OK); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_DISCOVER_FINISH, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + uint16_t subscribeId = data.ReadInt16(); + int32_t failedReason = data.ReadInt32(); + + if (failedReason == DEVICEMANAGER_OK) { + DeviceManagerNotify::GetInstance().OnDiscoverySuccess(pkgName, subscribeId); + } else { + DeviceManagerNotify::GetInstance().OnDiscoverFailed(pkgName, subscribeId, failedReason); + } + reply.WriteInt32(DEVICEMANAGER_OK); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_AUTH_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + std::string deviceId = data.ReadString(); + int32_t pinToken = data.ReadInt32(); + int32_t status = data.ReadInt32(); + int32_t reason = data.ReadInt32(); + + DeviceManagerNotify::GetInstance().OnAuthResult(pkgName, deviceId, pinToken, status, reason); + reply.WriteInt32(DEVICEMANAGER_OK); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_CHECK_AUTH_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + std::string deviceId = data.ReadString(); + int32_t resultCode = data.ReadInt32(); + int32_t flag = data.ReadInt32(); + + DeviceManagerNotify::GetInstance().OnCheckAuthResult(pkgName, deviceId, resultCode, flag); + reply.WriteInt32(DEVICEMANAGER_OK); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_GET_AUTHENTCATION_INFO, std::shared_ptr pBaseReq, MessageParcel& data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string packagename = pReq->GetPkgName(); + if (!data.WriteString(packagename)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_GET_AUTHENTCATION_INFO, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + DmAuthParam authParam; + authParam.direction = reply.ReadInt32(); + authParam.authType = reply.ReadInt32(); + if (authParam.direction == AUTH_SESSION_SIDE_CLIENT) { + authParam.pinToken = reply.ReadInt32(); + pRsp->SetAuthParam(authParam); + return DEVICEMANAGER_OK; + } + + authParam.packageName = reply.ReadString(); + authParam.appName = reply.ReadString(); + authParam.appDescription = reply.ReadString(); + authParam.business = reply.ReadInt32(); + authParam.pincode = reply.ReadInt32(); + + int32_t appIconLen = reply.ReadInt32(); + uint8_t *appIconBuffer = nullptr; + int32_t appThumbnailLen = reply.ReadInt32(); + uint8_t *appThumbBuffer = nullptr; + if (appIconLen > 0) { + appIconBuffer = (uint8_t *)reply.ReadRawData(appIconLen); + } + if (appThumbnailLen > 0) { + appThumbBuffer = (uint8_t *)reply.ReadRawData(appThumbnailLen); + } + authParam.imageinfo.Reset(appIconBuffer, appIconLen, appThumbBuffer, appThumbnailLen); + pRsp->SetAuthParam(authParam); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_USER_AUTHORIZATION_OPERATION, std::shared_ptr pBaseReq, MessageParcel& data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + int32_t action = pReq->GetOperation(); + + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(action)) { + DMLOG(DM_LOG_ERROR, "write extra failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_USER_AUTHORIZATION_OPERATION, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_DEVICEMANAGER_FA_NOTIFY, MessageParcel &data, MessageParcel &reply) +{ + DMLOG(DM_LOG_INFO, "OnFaCallBack"); + std::string packagename = data.ReadString(); + std::string paramJson = data.ReadString(); + DMLOG(DM_LOG_INFO, "OnFaCallBack Packagename is %s", packagename.c_str()); + DMLOG(DM_LOG_INFO, "OnFaCallBack Json is %s", paramJson.c_str()); + DeviceManagerNotify::GetInstance().OnFaCall(packagename, paramJson); + + if (!reply.WriteInt32(DEVICEMANAGER_OK)) { + DMLOG(DM_LOG_ERROR, "write return failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp new file mode 100644 index 000000000..bf8961b57 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -0,0 +1,295 @@ +/* + * 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_manager_notify.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" +#include "nlohmann/json.hpp" +#include "constants.h" +#include "device_manager.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify); + +void DeviceManagerNotify::RegisterDeathRecipientCallback(std::string &pkgName, + std::shared_ptr dmInitCallback) +{ + std::lock_guard autoLock(lock_); + dmInitCallback_[pkgName] = dmInitCallback; +} + +void DeviceManagerNotify::UnRegisterDeathRecipientCallback(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + dmInitCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDeviceStateCallback(std::string &pkgName, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + deviceStateCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterDeviceStateCallback(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + deviceStateCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDiscoverCallback(std::string &pkgName, uint16_t subscribeId, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + if (deviceDiscoverCallbacks_.count(pkgName) == 0) { + deviceDiscoverCallbacks_[pkgName] = std::map>(); + } + deviceDiscoverCallbacks_[pkgName][subscribeId] = callback; +} + +void DeviceManagerNotify::UnRegisterDiscoverCallback(std::string &pkgName, uint16_t subscribeId) +{ + std::lock_guard autoLock(lock_); + if (deviceDiscoverCallbacks_.count(pkgName) > 0) { + deviceDiscoverCallbacks_[pkgName].erase(subscribeId); + if (deviceDiscoverCallbacks_[pkgName].empty()) { + deviceDiscoverCallbacks_.erase(pkgName); + } + } +} + +void DeviceManagerNotify::RegisterAuthenticateCallback(std::string &pkgName, std::string &deviceId, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + if (authenticateCallback_.count(pkgName) == 0) { + authenticateCallback_[pkgName] = std::map>(); + } + authenticateCallback_[pkgName][deviceId] = callback; +} + +void DeviceManagerNotify::UnRegisterAuthenticateCallback(std::string &pkgName, std::string &deviceId) +{ + std::lock_guard autoLock(lock_); + if (authenticateCallback_.count(pkgName) > 0) { + authenticateCallback_[pkgName].erase(deviceId); + if (authenticateCallback_[pkgName].empty()) { + authenticateCallback_.erase(pkgName); + } + } +} + +void DeviceManagerNotify::UnRegisterPackageCallback(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + deviceStateCallback_.erase(pkgName); + deviceDiscoverCallbacks_.erase(pkgName); + authenticateCallback_.erase(pkgName); + dmInitCallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterCheckAuthenticationCallback(std::string &pkgName, std::string &authPara, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + if (checkauthcallback_.count(pkgName) == 0) { + checkauthcallback_[pkgName] = std::map>(); + } + checkauthcallback_[pkgName][authPara] = callback; +} + +void DeviceManagerNotify::UnRegisterCheckAuthenticationCallback(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + deviceStateCallback_.erase(pkgName); + deviceDiscoverCallbacks_.erase(pkgName); + authenticateCallback_.erase(pkgName); + dmInitCallback_.erase(pkgName); + checkauthcallback_.erase(pkgName); +} + +void DeviceManagerNotify::RegisterDeviceManagerFaCallback(std::string &packageName, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + dmFaCallback_[packageName] = callback; +} + +void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + dmFaCallback_.erase(pkgName); +} + + +void DeviceManagerNotify::OnRemoteDied() +{ + DMLOG(DM_LOG_WARN, "DeviceManager : OnRemoteDied"); + for (auto iter : dmInitCallback_) { + iter.second->OnRemoteDied(); + } +} + +void DeviceManagerNotify::OnDeviceOnline(std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDeviceOnline pkgName:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (deviceStateCallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDeviceOnlinecallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceOnline(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceOffline(std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDeviceOffline pkgName:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (deviceStateCallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDeviceOfflinecallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceOffline(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceChanged(std::string &pkgName, const DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDeviceChanged pkgName:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (deviceStateCallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDeviceChangedcallback not register"); + return; + } + deviceStateCallback_[pkgName]->OnDeviceChanged(deviceInfo); +} + +void DeviceManagerNotify::OnDeviceFound(std::string &pkgName, uint16_t subscribeId, + const DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDeviceFound pkgName:%s, subscribeId:%d.", pkgName.c_str(), + (int32_t)subscribeId); + std::lock_guard autoLock(lock_); + if (deviceDiscoverCallbacks_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDeviceFound: no register discoverCallback for this package"); + return; + } + std::map>& discoverCallMap = deviceDiscoverCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDeviceFound: no register discoverCallback for subscribeId %d", + subscribeId); + return; + } + iter->second->OnDeviceFound(subscribeId, deviceInfo); +} + +void DeviceManagerNotify::OnDiscoverFailed(std::string &pkgName, uint16_t subscribeId, int32_t failedReason) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDiscoverFailed pkgName:%s, subscribeId %d, reason %d", + pkgName.c_str(), subscribeId, failedReason); + std::lock_guard autoLock(lock_); + if (deviceDiscoverCallbacks_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDiscoverFailed: no register discoverCallback for this package"); + return; + } + std::map>& discoverCallMap = deviceDiscoverCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDiscoverFailed: no register discoverCallback for subscribeId %d", + subscribeId); + return; + } + iter->second->OnDiscoverFailed(subscribeId, failedReason); +} + +void DeviceManagerNotify::OnDiscoverySuccess(std::string &pkgName, uint16_t subscribeId) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnDiscoverySuccess pkgName:%s, subscribeId:%d.", pkgName.c_str(), + subscribeId); + std::lock_guard autoLock(lock_); + if (deviceDiscoverCallbacks_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDiscoverySuccess: no register discoverCallback for this package"); + return; + } + std::map>& discoverCallMap = deviceDiscoverCallbacks_[pkgName]; + auto iter = discoverCallMap.find(subscribeId); + if (iter == discoverCallMap.end()) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnDiscoverySuccess: no register discoverCallback for subscribeId %d", + subscribeId); + return; + } + iter->second->OnDiscoverySuccess(subscribeId); +} + +void DeviceManagerNotify::OnAuthResult(std::string &pkgName, std::string &deviceId, int32_t pinToken, + uint32_t status, uint32_t reason) +{ + DMLOG(DM_LOG_INFO, "DeviceManagerNotify::OnAuthResult pkgName:%s, status:%d, reason:%d", + pkgName.c_str(), status, reason); + std::lock_guard autoLock(lock_); + if (authenticateCallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnAuthResult: no register authCallback for this package"); + return; + } + std::map>& authCallMap = authenticateCallback_[pkgName]; + auto iter = authCallMap.find(deviceId); + if (iter == authCallMap.end()) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnAuthResult: no register authCallback for deviceID "); + return; + } + iter->second->OnAuthResult(deviceId, pinToken, status, reason); + authenticateCallback_[pkgName].erase(deviceId); + if (authenticateCallback_[pkgName].empty()) { + authenticateCallback_.erase(pkgName); + } +} + +void DeviceManagerNotify::OnCheckAuthResult(std::string &pkgName, std::string &deviceId, int32_t resultCode, + int32_t flag) +{ + DMLOG(DM_LOG_INFO, "DeviceManagerNotify::OnCheckAuthResult pkgName:%s, resultCode:%d, flag:%d", + pkgName.c_str(), resultCode, flag); + std::lock_guard autoLock(lock_); + if (checkauthcallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnCheckAuthResult: no register authCallback for this package"); + return; + } + std::map>& CheckAuthCallmap = checkauthcallback_[pkgName]; + auto iter = CheckAuthCallmap.find(deviceId); + if (iter == CheckAuthCallmap.end()) { + DMLOG(DM_LOG_ERROR, "DeviceManager OnCheckAuthResult: no register CheckAuthCallmap for deviceID"); + return; + } + iter->second->OnCheckAuthResult(deviceId, resultCode, flag); + checkauthcallback_[pkgName].erase(deviceId); + if (checkauthcallback_[pkgName].empty()) { + checkauthcallback_.erase(pkgName); + } +} + +void DeviceManagerNotify::OnFaCall(std::string &pkgName, std::string ¶mJson) +{ + DMLOG(DM_LOG_INFO, "DeviceManager OnFaCallback pkgName:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (dmFaCallback_.count(pkgName) == 0) { + DMLOG(DM_LOG_ERROR, "DeviceManager DmFaCallback not register"); + return; + } + dmFaCallback_[pkgName]->OnCall(paramJson); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/include/ipc/ipc_server_adapter.h b/services/devicemanagerservice/include/ipc/ipc_server_adapter.h new file mode 100644 index 000000000..b371e03a4 --- /dev/null +++ b/services/devicemanagerservice/include/ipc/ipc_server_adapter.h @@ -0,0 +1,53 @@ +/* + * 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_SERVER_ADAPTER_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_ADAPTER_H + +#include + +#include "dm_app_image_info.h" +#include "dm_device_info.h" +#include "dm_subscribe_info.h" + +#include "hichain_connector.h" + +#include "single_instance.h" +#include "softbus_adapter.h" +#include "ability_manager_interface.h" +#include "want.h" +#include "ipc_server_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerAdapter { +DECLARE_SINGLE_INSTANCE(IpcServerAdapter); +public: + int32_t ModuleInit(); + int32_t GetTrustedDeviceList(std::string &pkgName, std::string &extra, DmDeviceInfo **info, int32_t *infoNum); + int32_t StartDeviceDiscovery(std::string &pkgName, DmSubscribeInfo &dmSubscribeInfo); + int32_t StopDiscovery(std::string &pkgName, uint16_t subscribeId); + int32_t AuthenticateDevice(std::string &pkgName, const DmDeviceInfo &deviceInfo, + const DmAppImageInfo &imageInfo, std::string &extra); + int32_t CheckAuthentication(std::string &authPara); + int32_t GetAuthenticationParam(std::string &pkgName, DmAuthParam &authParam); + int32_t SetUserOperation(std::string &pkgName, int32_t action); + static int32_t GenRandInt(int32_t minPinToken, int32_t maxPinToken); +private: + int32_t CheckParamValid(nlohmann::json &extraJson, const DmAppImageInfo &imageInfo); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_ADAPTER_H diff --git a/services/devicemanagerservice/include/ipc/ipc_server_listener_adapter.h b/services/devicemanagerservice/include/ipc/ipc_server_listener_adapter.h new file mode 100644 index 000000000..e4b3e357d --- /dev/null +++ b/services/devicemanagerservice/include/ipc/ipc_server_listener_adapter.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_SERVER_LISTENER_ADAPTER_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_ADAPTER_H + +#include "discovery_service.h" + +#include "ipc_server_listener.h" + +#include "single_instance.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerListenerAdapter { +DECLARE_SINGLE_INSTANCE(IpcServerListenerAdapter); +public: + void OnDeviceStateChange(DmDeviceState state, DmDeviceInfo &deviceInfo); + void OnDeviceFound(std::string &pkgName, uint16_t originId, DmDeviceInfo &deviceInfo); + void OnDiscoverFailed(std::string &pkgName, uint16_t originId, DiscoveryFailReason failReason); + void OnDiscoverySuccess(std::string &pkgName, uint16_t originId); + void OnAuthResult(std::string &pkgName, std::string &deviceId, int32_t pinToken, uint32_t status, uint32_t reason); + void OnCheckAuthResult(std::string &pkgName, std::string &deviceId, int32_t resultCode, int32_t flag); + void OnFaCall(std::string &pkgName, std::string ¶mJson); +private: + IpcServerListener ipcServerListener_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_ADAPTER_H diff --git a/services/devicemanagerservice/include/ipc/lite/ipc_server_listener.h b/services/devicemanagerservice/include/ipc/lite/ipc_server_listener.h new file mode 100644 index 000000000..9503191aa --- /dev/null +++ b/services/devicemanagerservice/include/ipc/lite/ipc_server_listener.h @@ -0,0 +1,41 @@ +/* + * 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_SERVER_LISTENER_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_H + +#include + +#include "ipc_req.h" +#include "ipc_rsp.h" + +#include "ipc_server_listenermgr.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerListener { +public: + IpcServerListener() = default; + virtual ~IpcServerListener() = default; +public: + int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); + int32_t SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); +private: + void CommonSvcToIdentity(CommonSvcId *svcId, SvcIdentity *identity); + int32_t GetIdentityByPkgName(std::string &name, SvcIdentity *svc); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_H diff --git a/services/devicemanagerservice/include/ipc/lite/ipc_server_listenermgr.h b/services/devicemanagerservice/include/ipc/lite/ipc_server_listenermgr.h new file mode 100644 index 000000000..3e39f8cce --- /dev/null +++ b/services/devicemanagerservice/include/ipc/lite/ipc_server_listenermgr.h @@ -0,0 +1,50 @@ +/* + * 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_SERVER_LISTENER_MGR_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_MGR_H + +#include +#include +#include +#include + +#include "liteipc_adapter.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +typedef struct CommonSvcId { + unsigned int handle; + unsigned int token; + unsigned int cookie; + IpcContext *ipcCtx; + unsigned int cbId; +}CommonSvcId; + +class IpcServerListenermgr { +DECLARE_SINGLE_INSTANCE(IpcServerListenermgr); +public: + int32_t RegisterListener(std::string &pkgName, const CommonSvcId *svcId); + int32_t GetListenerByPkgName(std::string &pkgName, CommonSvcId *svcId); + int32_t UnregisterListener(std::string &pkgName); + const std::map &GetAllListeners(); +private: + std::map dmListenerMap_; + std::mutex lock_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_MGR_H diff --git a/services/devicemanagerservice/include/ipc/lite/ipc_server_stub.h b/services/devicemanagerservice/include/ipc/lite/ipc_server_stub.h new file mode 100644 index 000000000..9baa73d4a --- /dev/null +++ b/services/devicemanagerservice/include/ipc/lite/ipc_server_stub.h @@ -0,0 +1,26 @@ +/* + * 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_SERVER_STUB_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H + +#include + +#include "liteipc_adapter.h" + +int32_t IpcServerStubInit(void); +int32_t RegisterDeviceManagerListener(IpcIo *req, IpcIo *reply); +int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply); +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H diff --git a/services/devicemanagerservice/include/ipc/standard/ipc_server_client_proxy.h b/services/devicemanagerservice/include/ipc/standard/ipc_server_client_proxy.h new file mode 100644 index 000000000..15b5e44a4 --- /dev/null +++ b/services/devicemanagerservice/include/ipc/standard/ipc_server_client_proxy.h @@ -0,0 +1,35 @@ +/* + * 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_SERVER_CLIENT_PROXY_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_CLIENT_PROXY_H + +#include "ipc_remote_broker.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerClientProxy : public IRemoteProxy { +public: + explicit IpcServerClientProxy(const sptr &impl) + : IRemoteProxy(impl) {}; + ~IpcServerClientProxy() {}; + int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_CLIENT_PROXY_H diff --git a/services/devicemanagerservice/include/ipc/standard/ipc_server_listener.h b/services/devicemanagerservice/include/ipc/standard/ipc_server_listener.h new file mode 100644 index 000000000..3737b7cde --- /dev/null +++ b/services/devicemanagerservice/include/ipc/standard/ipc_server_listener.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_DEVICE_MANAGER_IPC_SERVER_LISTENER_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_H + +#include + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerListener { +public: + IpcServerListener() = default; + virtual ~IpcServerListener() = default; +public: + int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); + int32_t SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_LISTENER_H diff --git a/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h b/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h new file mode 100644 index 000000000..4e653eb97 --- /dev/null +++ b/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h @@ -0,0 +1,76 @@ +/* + * 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_SERVER_STUB_H +#define OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H + +#include +#include +#include +#include +#include + +#include "nlohmann/json.hpp" + +#include "system_ability.h" +#include "thread_pool.h" +#include "iremote_stub.h" +#include "ipc_remote_broker.h" + +#include "single_instance.h" +#include "hichain_connector.h" + +namespace OHOS { +namespace DistributedHardware { +enum class ServiceRunningState { + STATE_NOT_START, + STATE_RUNNING +}; + +class AppDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr &remote) override; + AppDeathRecipient() = default; + ~AppDeathRecipient() = default; +}; + +class IpcServerStub : public SystemAbility, public IRemoteStub { +DECLARE_SYSTEM_ABILITY(IpcServerStub); +DECLARE_SINGLE_INSTANCE_BASE(IpcServerStub); +public: + void OnStart() override; + void OnStop() override; + int32_t OnRemoteRequest(uint32_t code, + MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + int32_t SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; + int32_t RegisterDeviceManagerListener(std::string &pkgName, sptr listener); + int32_t UnRegisterDeviceManagerListener(std::string &pkgName); + ServiceRunningState QueryServiceState() const; + const std::map> &GetDmListener(); + const sptr GetDmListener(std::string pkgName) const; +private: + IpcServerStub(); + ~IpcServerStub() = default; + bool Init(); +private: + bool registerToService_; + ServiceRunningState state_; + std::mutex listenerLock_; + std::map> appRecipient_; + std::map> dmListener_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H diff --git a/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp new file mode 100644 index 000000000..095dcebda --- /dev/null +++ b/services/devicemanagerservice/src/ipc/ipc_server_adapter.cpp @@ -0,0 +1,189 @@ +/* + * 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 + +#include "securec.h" + +#include "anonymous_string.h" +#include "auth_manager.h" +#include "constants.h" +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "dm_ability_manager.h" +#include "encrypt_utils.h" +#include "ipc_server_adapter.h" +#include "ipc_server_listener.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcServerAdapter); + +int32_t IpcServerAdapter::CheckParamValid(nlohmann::json &extraJson, const DmAppImageInfo &imageInfo) +{ + if (!extraJson.contains(APP_NAME_KEY) || + !extraJson.contains(APP_DESCRIPTION_KEY) || + !extraJson.contains(AUTH_TYPE)) { + DMLOG(DM_LOG_ERROR, "Invalid para"); + return DEVICEMANAGER_INVALID_VALUE; + } + + std::string appName = extraJson[APP_NAME_KEY]; + std::string appDescription = extraJson[APP_DESCRIPTION_KEY]; + + if (appName.empty() || appDescription.empty()) { + DMLOG(DM_LOG_ERROR, "Invalid app image info"); + return DEVICEMANAGER_INVALID_VALUE; + } + if (extraJson[AUTH_TYPE] != AUTH_TYPE_PIN) { + DMLOG(DM_LOG_ERROR, "invalid auth type, only support pin auth"); + return DEVICEMANAGER_INVALID_VALUE; + } + return DEVICEMANAGER_OK; +} + +int32_t IpcServerAdapter::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); +} + +int32_t IpcServerAdapter::ModuleInit() +{ + if (SoftbusAdapter::Init() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "softbus adapter init failed"); + return DEVICEMANAGER_INIT_FAILED; + } + if (HichainConnector::GetInstance().Init() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "hichain connector init failed"); + return DEVICEMANAGER_INIT_FAILED; + } + return DEVICEMANAGER_OK; +} + +int32_t IpcServerAdapter::GetTrustedDeviceList(std::string &pkgName, std::string &extra, + DmDeviceInfo **info, int32_t *infoNum) +{ + DMLOG(DM_LOG_INFO, "In, pkgName: %s", pkgName.c_str()); + NodeBasicInfo *nodeInfo = nullptr; + int32_t ret = SoftbusAdapter::GetTrustDevices(pkgName, &nodeInfo, infoNum); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "DM_GetSoftbusTrustDevices failed with ret %d", ret); + return ret; + } + *info = nullptr; + if (*infoNum > 0) { + *info = (DmDeviceInfo *)malloc(sizeof(DmDeviceInfo) * (*infoNum)); + } + for (int32_t i = 0; i < *infoNum; ++i) { + NodeBasicInfo *nodeBasicInfo = nodeInfo + i; + DmDeviceInfo *deviceInfo = *info + i; + + (void)memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId, + std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))); + (void)memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName, + std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))); + deviceInfo->deviceTypeId = (DMDeviceType)nodeBasicInfo->deviceTypeId; + } + FreeNodeInfo(nodeInfo); + DMLOG(DM_LOG_INFO, "success, pkgName:%s, deviceCount %d", pkgName.c_str(), *infoNum); + return DEVICEMANAGER_OK; +} + +int32_t IpcServerAdapter::StartDeviceDiscovery(std::string &pkgName, DmSubscribeInfo &dmSubscribeInfo) +{ + DMLOG(DM_LOG_INFO, "In, pkgName: %s, subscribeId %d", pkgName.c_str(), + (int32_t)dmSubscribeInfo.subscribeId); + + DMLOG(DM_LOG_INFO, "capability: %s", dmSubscribeInfo.capability); + SubscribeInfo subscribeInfo; + + subscribeInfo.subscribeId = dmSubscribeInfo.subscribeId; + subscribeInfo.mode = (DiscoverMode)dmSubscribeInfo.mode; + subscribeInfo.medium = (ExchanageMedium)dmSubscribeInfo.medium; + subscribeInfo.freq = (ExchangeFreq)dmSubscribeInfo.freq; + subscribeInfo.isSameAccount = dmSubscribeInfo.isSameAccount; + subscribeInfo.isWakeRemote = dmSubscribeInfo.isWakeRemote; + subscribeInfo.capability = dmSubscribeInfo.capability; + subscribeInfo.capabilityData = nullptr; + subscribeInfo.dataLen = 0; + return SoftbusAdapter::StartDiscovery(pkgName, &subscribeInfo); +} + +int32_t IpcServerAdapter::StopDiscovery(std::string &pkgName, uint16_t subscribeId) +{ + DMLOG(DM_LOG_INFO, "In, pkgName: %s, subscribeId %d", pkgName.c_str(), (int32_t)subscribeId); + return SoftbusAdapter::StopDiscovery(pkgName, subscribeId); +} + +int32_t IpcServerAdapter::AuthenticateDevice(std::string &pkgName, const DmDeviceInfo &deviceInfo, + const DmAppImageInfo &imageInfo, std::string &extra) +{ + if (pkgName.empty() || extra.empty()) { + DMLOG(DM_LOG_ERROR, "invalid para"); + return DEVICEMANAGER_INVALID_VALUE; + } + nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false); + if (jsonObject.is_discarded()) { + DMLOG(DM_LOG_ERROR, "AuthenticateDevice extra jsonStr error"); + return DEVICEMANAGER_INVALID_VALUE; + } + int32_t ret = CheckParamValid(jsonObject, imageInfo); + if (ret != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "AuthenticateDevice para invalid, ret %d", ret); + return ret; + } + DMLOG(DM_LOG_INFO, "AuthenticateDevice In, pkgName: %s, deviceId %s", pkgName.c_str(), + GetAnonyString(deviceInfo.deviceId).c_str()); + + AuthManager::GetInstance().AuthDeviceGroup(pkgName, deviceInfo, imageInfo, extra); + return DEVICEMANAGER_OK; +} + +int32_t IpcServerAdapter::CheckAuthentication(std::string &authPara) +{ + if (authPara.empty()) { + DMLOG(DM_LOG_INFO, " DeviceManagerIpcAdapter::CheckAuthentication check authPara failed"); + return DEVICEMANAGER_INVALID_VALUE; + } + DMLOG(DM_LOG_INFO, " DeviceManagerIpcAdapter::CheckAuthentication"); + return AuthManager::GetInstance().CheckAuthentication(authPara); +} + +int32_t IpcServerAdapter::GetAuthenticationParam(std::string &pkgName, DmAuthParam &authParam) +{ + if (pkgName.empty()) { + DMLOG(DM_LOG_ERROR, "invalid para"); + return DEVICEMANAGER_INVALID_VALUE; + } + + DmAbilityManager::GetInstance().StartAbilityDone(); + AuthManager::GetInstance().GetAuthenticationParam(authParam); + return DEVICEMANAGER_OK; +} + +int32_t IpcServerAdapter::SetUserOperation(std::string &pkgName, int32_t action) +{ + if (pkgName.empty()) { + DMLOG(DM_LOG_ERROR, "invalid para"); + return DEVICEMANAGER_INVALID_VALUE; + } + + AuthManager::GetInstance().OnUserOperate(action); + return SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/ipc_server_listener_adapter.cpp b/services/devicemanagerservice/src/ipc/ipc_server_listener_adapter.cpp new file mode 100644 index 000000000..fe29635d2 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/ipc_server_listener_adapter.cpp @@ -0,0 +1,129 @@ +/* + * 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 "ipc_server_listener_adapter.h" + +#include + +#include "securec.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "anonymous_string.h" + +#include "ipc_notify_auth_result_req.h" +#include "ipc_notify_check_auth_result_req.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_discover_result_req.h" +#include "ipc_notify_dmfa_result_req.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcServerListenerAdapter); + +void IpcServerListenerAdapter::OnDeviceStateChange(DmDeviceState state, DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "OnDeviceStateChange"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetDeviceState(state); + pReq->SetDeviceInfo(deviceInfo); + ipcServerListener_.SendAll(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnDeviceFound(std::string &pkgName, uint16_t originId, + DmDeviceInfo &deviceInfo) +{ + DMLOG(DM_LOG_INFO, "call OnDeviceFound for %s, originId %d, deviceId %s", + pkgName.c_str(), originId, GetAnonyString(std::string(deviceInfo.deviceId)).c_str()); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetSubscribeId(originId); + pReq->SetDeviceInfo(deviceInfo); + ipcServerListener_.SendRequest(SERVER_DEVICE_FOUND, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnDiscoverFailed(std::string &pkgName, uint16_t originId, + DiscoveryFailReason failReason) +{ + DMLOG(DM_LOG_INFO, "OnDiscoverFailed"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetSubscribeId(originId); + pReq->SetResult(failReason); + ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnDiscoverySuccess(std::string &pkgName, uint16_t originId) +{ + DMLOG(DM_LOG_INFO, "OnDiscoverySuccess"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetSubscribeId(originId); + pReq->SetResult(DEVICEMANAGER_OK); + ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnAuthResult(std::string &pkgName, std::string &deviceId, int32_t pinToken, + uint32_t status, uint32_t reason) +{ + DMLOG(DM_LOG_INFO, "%s, package: %s, deviceId: %s", __FUNCTION__, pkgName.c_str(), + GetAnonyString(deviceId).c_str()); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetDeviceId(deviceId); + pReq->SetPinToken(pinToken); + pReq->SetStatus(status); + pReq->SetReason(reason); + ipcServerListener_.SendRequest(SERVER_AUTH_RESULT, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnCheckAuthResult(std::string &pkgName, std::string &authParam, int32_t resultCode, + int32_t flag) +{ + DMLOG(DM_LOG_INFO, "OnCheckResult, authParam: %s, errorCode: %d", + GetAnonyString(authParam).c_str(), resultCode); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetDeviceId(authParam); + pReq->SetResult(resultCode); + pReq->SetFlag(flag); + ipcServerListener_.SendRequest(SERVER_CHECK_AUTH_RESULT, pReq, pRsp); +} + +void IpcServerListenerAdapter::OnFaCall(std::string &pkgName, std::string ¶mJson) +{ + DMLOG(DM_LOG_INFO, "OnFaCallSuccess"); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetJsonParam(paramJson); + ipcServerListener_.SendRequest(SERVER_DEVICEMANAGER_FA_NOTIFY, pReq, pRsp); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp new file mode 100644 index 000000000..b4e185050 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/lite/ipc_cmd_parser.cpp @@ -0,0 +1,215 @@ +/* + * 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 "ipc_cmd_register.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +#include "ipc_def.h" +#include "ipc_notify_auth_result_req.h" +#include "ipc_notify_check_auth_result_req.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_discover_result_req.h" +#include "ipc_server_adapter.h" +#include "ipc_server_stub.h" + +namespace OHOS { +namespace DistributedHardware { +ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + int32_t deviceState = pReq->GetDeviceState(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushInt32(&request, deviceState); + IpcIoPushFlatObj(&request, &deviceInfo, sizeof(DmDeviceInfo)); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushUint16(&request, subscribeId); + IpcIoPushFlatObj(&request, &deviceInfo, sizeof(DmDeviceInfo)); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + int32_t result = pReq->GetResult(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushUint16(&request, subscribeId); + IpcIoPushInt32(&request, result); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string deviceId = pReq->GetDeviceId(); + int32_t pinToken = pReq->GetPinToken(); + int32_t status = pReq->GetStatus(); + int32_t reason = pReq->GetReason(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushString(&request, deviceId.c_str()); + IpcIoPushInt32(&request, pinToken); + IpcIoPushInt32(&request, status); + IpcIoPushInt32(&request, reason); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string deviceId = pReq->GetDeviceId(); + int32_t result = pReq->GetResult(); + int32_t flag = pReq->GetFlag(); + + IpcIoInit(&request, buffer, buffLen, 0); + IpcIoPushString(&request, pkgName.c_str()); + IpcIoPushString(&request, deviceId.c_str()); + IpcIoPushInt32(&request, result); + IpcIoPushInt32(&request, flag); + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(IpcIoPopInt32(&reply)); + return DEVICEMANAGER_OK; +} + +ON_IPC_SERVER_CMD(GET_TRUST_DEVICE_LIST, IpcIo &req, IpcIo &reply) +{ + DMLOG(DM_LOG_INFO, "enter GetTrustedDeviceList."); + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&req, &len); + std::string extra = (const char *)IpcIoPopString(&req, &len); + DmDeviceInfo *info = nullptr; + int32_t infoNum = 0; + int32_t ret = IpcServerAdapter::GetInstance().GetTrustedDeviceList(pkgName, extra, &info, &infoNum); + IpcIoPushInt32(&reply, infoNum); + if (infoNum > 0) { + IpcIoPushFlatObj(&reply, info, sizeof(DmDeviceInfo) * infoNum); + free(info); + } + IpcIoPushInt32(&reply, ret); +} + +ON_IPC_SERVER_CMD(REGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply) +{ + RegisterDeviceManagerListener(&req, &reply); +} + +ON_IPC_SERVER_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, IpcIo &req, IpcIo &reply) +{ + UnRegisterDeviceManagerListener(&req, &reply); +} + +ON_IPC_SERVER_CMD(START_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply) +{ + DMLOG(DM_LOG_INFO, "StartDeviceDiscovery service listener."); + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&req, &len); + uint32_t size = 0; + DmSubscribeInfo *pDmSubscribeInfo = (DmSubscribeInfo*)IpcIoPopFlatObj(&req, &size); + + int32_t ret = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *pDmSubscribeInfo); + IpcIoPushInt32(&reply, ret); +} + +ON_IPC_SERVER_CMD(STOP_DEVICE_DISCOVER, IpcIo &req, IpcIo &reply) +{ + DMLOG(DM_LOG_INFO, "StopDeviceDiscovery service listener."); + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&req, &len); + uint16_t subscribeId = IpcIoPopUint16(&req); + int32_t ret = IpcServerAdapter::GetInstance().StopDiscovery(pkgName, subscribeId); + IpcIoPushInt32(&reply, ret); +} + +ON_IPC_SERVER_CMD(AUTHENTICATE_DEVICE, IpcIo &req, IpcIo &reply) +{ + DMLOG(DM_LOG_INFO, "AuthenticateDevice service listener."); + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(&req, &len); + size_t extraLen = 0; + std::string extra = (const char *)IpcIoPopString(&req, &extraLen); + uint32_t size; + DmDeviceInfo *deviceInfo = (DmDeviceInfo*)IpcIoPopFlatObj(&req, &size); + DmAppImageInfo imageInfo(nullptr, 0, nullptr, 0); + int32_t ret = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra); + IpcIoPushInt32(&reply, ret); +} + +ON_IPC_SERVER_CMD(CHECK_AUTHENTICATION, IpcIo &req, IpcIo &reply) +{ + DMLOG(DM_LOG_INFO, "CheckAuthentication service listener."); + size_t authParaLen = 0; + std::string authPara = (const char *)IpcIoPopString(&req, &authParaLen); + int32_t ret = IpcServerAdapter::GetInstance().CheckAuthentication(authPara); + IpcIoPushInt32(&reply, ret); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_listener.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_listener.cpp new file mode 100644 index 000000000..ad2141de8 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_listener.cpp @@ -0,0 +1,90 @@ +/* + * 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 "ipc_server_listener.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" + +#include "ipc_cmd_register.h" +#include "ipc_def.h" +#include "ipc_server_listenermgr.h" +#include "ipc_server_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +void IpcServerListener::CommonSvcToIdentity(CommonSvcId *svcId, SvcIdentity *identity) +{ + identity->handle = svcId->handle; + identity->token = svcId->token; + identity->cookie = svcId->cookie; +#ifdef __LINUX__ + identity->ipcContext = svcId->ipcCtx; +#endif +} + +int32_t IpcServerListener::GetIdentityByPkgName(std::string &name, SvcIdentity *svc) +{ + CommonSvcId svcId; + if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(name, &svcId) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "get identity failed."); + return DEVICEMANAGER_FAILED; + } + CommonSvcToIdentity(&svcId, svc); + return DEVICEMANAGER_OK; +} + +int32_t IpcServerListener::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + std::string pkgName = req->GetPkgName(); + SvcIdentity svc; + if (GetIdentityByPkgName(pkgName, &svc) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "ondevice found callback get listener failed."); + return DEVICEMANAGER_FAILED; + } + + IpcIo io; + uint8_t data[MAX_DM_IPC_LEN] = {0}; + if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, io, data, MAX_DM_IPC_LEN) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_DEBUG, "SetRequest failed cmdCode:%d", cmdCode); + return DEVICEMANAGER_FAILED; + } + if (::SendRequest(nullptr, svc, cmdCode, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_DEBUG, "SendRequest failed cmdCode:%d", cmdCode); + } + return DEVICEMANAGER_OK; +} + +int32_t IpcServerListener::SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + const std::map &listenerMap = IpcServerListenermgr::GetInstance().GetAllListeners(); + for (auto &kv : listenerMap) { + SvcIdentity svc; + IpcIo io; + uint8_t data[MAX_DM_IPC_LEN] = {0}; + if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, io, data, MAX_DM_IPC_LEN) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_DEBUG, "SetRequest failed cmdCode:%d", cmdCode); + continue; + } + CommonSvcId svcId = kv.second; + CommonSvcToIdentity(&svcId, &svc); + if (::SendRequest(nullptr, svc, cmdCode, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_DEBUG, "SendRequest failed cmdCode:%d", cmdCode); + } + } + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_listenermgr.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_listenermgr.cpp new file mode 100644 index 000000000..59aec4fda --- /dev/null +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_listenermgr.cpp @@ -0,0 +1,65 @@ +/* + * 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 "ipc_server_listenermgr.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcServerListenermgr); + +int32_t IpcServerListenermgr::RegisterListener(std::string &pkgName, const CommonSvcId *svcId) +{ + if (pkgName == "" || svcId == nullptr) { + DMLOG(DM_LOG_ERROR, "invalid param"); + return DEVICEMANAGER_FAILED; + } + DMLOG(DM_LOG_INFO, "new listener register:%s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + dmListenerMap_[pkgName] = *svcId; + return DEVICEMANAGER_OK; +} + +int32_t IpcServerListenermgr::GetListenerByPkgName(std::string &pkgName, CommonSvcId *svcId) +{ + if (pkgName == "" || svcId == nullptr) { + DMLOG(DM_LOG_ERROR, "invalid param"); + return DEVICEMANAGER_FAILED; + } + std::lock_guard autoLock(lock_); + std::map::iterator iter = dmListenerMap_.find(pkgName); + if (iter == dmListenerMap_.end()) { + DMLOG(DM_LOG_ERROR, "listener not found for pkg:%s", pkgName.c_str()); + return DEVICEMANAGER_FAILED; + } + *svcId = iter->second; + return DEVICEMANAGER_OK; +} + +int32_t IpcServerListenermgr::UnregisterListener(std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + dmListenerMap_.erase(pkgName); + return DEVICEMANAGER_OK; +} + +const std::map &IpcServerListenermgr::GetAllListeners() +{ + return dmListenerMap_; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_main.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_main.cpp new file mode 100644 index 000000000..c31c4a7ac --- /dev/null +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_main.cpp @@ -0,0 +1,52 @@ +/* + * 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 +#include + +#include "device_manager_log.h" +#include "device_manager_errno.h" + +#include "ipc_server_stub.h" +#include "ipc_server_adapter.h" + +using namespace OHOS::DistributedHardware; + +static void InitAll() +{ + const int32_t DM_SERVICE_INIT_DELAY = 5; + + sleep(DM_SERVICE_INIT_DELAY); + if (IpcServerStubInit() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "IpcServerStubInit failed"); + return; + } + if (IpcServerAdapter::GetInstance().ModuleInit() != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "module init failed"); + return; + } + DMLOG(DM_LOG_INFO, "DM ipc server Init success"); +} + +int32_t main(int32_t argc, char *argv[]) +{ + (void)argc; + (void)argv; + InitAll(); + while (1) { + pause(); + } + return 0; +} \ No newline at end of file diff --git a/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp new file mode 100644 index 000000000..a14342da7 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/lite/ipc_server_stub.cpp @@ -0,0 +1,238 @@ +/* + * 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 "ipc_server_stub.h" + +#include "securec.h" + +#include "liteipc_adapter.h" +#include "ohos_init.h" +#include "samgr_lite.h" +#include "iproxy_server.h" + +#include "device_manager_log.h" +#include "device_manager_errno.h" +#include "dm_subscribe_info.h" + +#include "ipc_cmd_register.h" +#include "ipc_def.h" +#include "ipc_server_listenermgr.h" +#include "ipc_server_adapter.h" + +namespace { + const int32_t WAIT_FOR_SERVER = 2; + const int32_t STACK_SIZE = 0x800; + const int32_t QUEUE_SIZE = 20; +} + +using namespace OHOS::DistributedHardware; + +struct DefaultFeatureApi { + INHERIT_SERVER_IPROXY; +}; + +struct DeviceManagerSamgrService { + INHERIT_SERVICE; + INHERIT_IUNKNOWNENTRY(DefaultFeatureApi); + Identity identity; +}; + +static int32_t DeathCb(const IpcContext *context, void *ipcMsg, IpcIo *data, void *arg) +{ + (void)context; + (void)ipcMsg; + (void)data; + if (arg == NULL) { + DMLOG(DM_LOG_ERROR, "package name is NULL."); + return DEVICEMANAGER_INVALID_PARAM; + } + CommonSvcId svcId = {0}; + std::string pkgName = (const char *)arg; + if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "not found client by package name."); + free(arg); + arg = NULL; + return DEVICEMANAGER_FAILED; + } + IpcServerListenermgr::GetInstance().UnregisterListener(pkgName); + free(arg); + arg = NULL; +#ifdef __LINUX__ + BinderRelease(svcId.ipcCtx, svcId.handle); +#endif + SvcIdentity sid = {0}; + sid.handle = svcId.handle; + sid.token = svcId.token; + sid.cookie = svcId.cookie; + UnregisterDeathCallback(sid, svcId.cbId); + + return DEVICEMANAGER_OK; +} + +int32_t RegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) +{ + DMLOG(DM_LOG_INFO, "register service listener."); + size_t len = 0; + int32_t ret = DEVICEMANAGER_OK; + uint8_t *name = IpcIoPopString(req, &len); + SvcIdentity *svc = IpcIoPopSvc(req); + if (name == NULL || svc == NULL || len == 0) { + DMLOG(DM_LOG_ERROR, "get para failed"); + IpcIoPushInt32(reply, ret); + return DEVICEMANAGER_OK; + } + + CommonSvcId svcId = {0}; + svcId.handle = svc->handle; + svcId.token = svc->token; + svcId.cookie = svc->cookie; + + SvcIdentity sid = *svc; +#ifdef __LINUX__ + svcId.ipcCtx = svc->ipcContext; + BinderAcquire(svcId.ipcCtx, svcId.handle); + free(svc); + svc = NULL; +#endif + + char *pkgName = (char *)malloc(len + 1); + if (pkgName == NULL) { + DMLOG(DM_LOG_ERROR, "malloc failed!"); + IpcIoPushInt32(reply, DEVICEMANAGER_FAILED); + return DEVICEMANAGER_FAILED; + } + if (strcpy_s(pkgName, len + 1, (const char *)name) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "strcpy_s failed!"); + free(pkgName); + IpcIoPushInt32(reply, DEVICEMANAGER_FAILED); + return DEVICEMANAGER_FAILED; + } + uint32_t cbId = 0; + RegisterDeathCallback(NULL, sid, DeathCb, pkgName, &cbId); + svcId.cbId = cbId; + std::string strPkgName = (const char *)name; + ret = IpcServerListenermgr::GetInstance().RegisterListener(strPkgName, &svcId); + IpcIoPushInt32(reply, ret); + return DEVICEMANAGER_OK; +} + +int32_t UnRegisterDeviceManagerListener(IpcIo *req, IpcIo *reply) +{ + DMLOG(DM_LOG_INFO, "unregister service listener."); + size_t len = 0; + std::string pkgName = (const char *)IpcIoPopString(req, &len); + if (pkgName == "" || len == 0) { + DMLOG(DM_LOG_ERROR, "get para failed"); + return DEVICEMANAGER_FAILED; + } + + CommonSvcId svcId = {0}; + if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(pkgName, &svcId) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "not found listener by package name."); + return DEVICEMANAGER_FAILED; + } + int32_t ret = IpcServerListenermgr::GetInstance().UnregisterListener(pkgName); + if (ret == DEVICEMANAGER_OK) { +#ifdef __LINUX__ + BinderRelease(svcId.ipcCtx, svcId.handle); +#endif + SvcIdentity sid = {0}; + sid.handle = svcId.handle; + sid.token = svcId.token; + sid.cookie = svcId.cookie; + ret = UnregisterDeathCallback(sid, svcId.cbId); + } + IpcIoPushInt32(reply, ret); + return DEVICEMANAGER_OK; +} + +static const char *GetName(Service *service) +{ + (void)service; + return DEVICE_MANAGER_SERVICE_NAME; +} + +static BOOL Initialize(Service *service, Identity identity) +{ + if (service == NULL) { + DMLOG(DM_LOG_WARN, "invalid param"); + return FALSE; + } + + DeviceManagerSamgrService *mgrService = (DeviceManagerSamgrService *)service; + mgrService->identity = identity; + return TRUE; +} + +static BOOL MessageHandle(Service *service, Request *request) +{ + if ((service == NULL) || (request == NULL)) { + DMLOG(DM_LOG_WARN, "invalid param"); + return FALSE; + } + return TRUE; +} + +static TaskConfig GetTaskConfig(Service *service) +{ + (void)service; + TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SHARED_TASK}; + return config; +} + +static int32_t OnRemoteRequest(IServerProxy *iProxy, int funcId, void *origin, + IpcIo *req, IpcIo *reply) +{ + DMLOG(DM_LOG_INFO, "Receive funcId:%d", funcId); + (void)origin; + return IpcCmdRegister::GetInstance().OnIpcServerCmd(funcId, *req, *reply); +} + +static void HOS_SystemInit(void) +{ + SAMGR_Bootstrap(); + return; +} + +int32_t IpcServerStubInit(void) +{ + HOS_SystemInit(); + return DEVICEMANAGER_OK; +} + +static void DevMgrSvcInit(void) +{ + sleep(WAIT_FOR_SERVER); + static DeviceManagerSamgrService service = { + .GetName = GetName, + .Initialize = Initialize, + .MessageHandle = MessageHandle, + .GetTaskConfig = GetTaskConfig, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = OnRemoteRequest, + IPROXY_END, + }; + + if (!SAMGR_GetInstance()->RegisterService((Service *)&service)) { + DMLOG(DM_LOG_ERROR, "%s, RegisterService failed", DEVICE_MANAGER_SERVICE_NAME); + return; + } + if (!SAMGR_GetInstance()->RegisterDefaultFeatureApi(DEVICE_MANAGER_SERVICE_NAME, GET_IUNKNOWN(service))) { + DMLOG(DM_LOG_ERROR, "%s, RegisterDefaultFeatureApi failed", DEVICE_MANAGER_SERVICE_NAME); + return; + } + DMLOG(DM_LOG_INFO, "%s, init success", DEVICE_MANAGER_SERVICE_NAME); +} +SYSEX_SERVICE_INIT(DevMgrSvcInit); diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp new file mode 100644 index 000000000..a2378158c --- /dev/null +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -0,0 +1,364 @@ +/* + * 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 "constants.h" +#include "ipc_cmd_register.h" +#include "ipc_def.h" +#include "ipc_notify_auth_result_req.h" +#include "ipc_notify_check_auth_result_req.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_discover_result_req.h" +#include "ipc_server_adapter.h" +#include "ipc_server_stub.h" +#include "ipc_notify_dmfa_result_req.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +namespace OHOS { +namespace DistributedHardware { +ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + int32_t deviceState = pReq->GetDeviceState(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(deviceState)) { + DMLOG(DM_LOG_ERROR, "write state failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) { + DMLOG(DM_LOG_ERROR, "write deviceInfo failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + DmDeviceInfo deviceInfo = pReq->GetDeviceInfo(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt16(subscribeId)) { + DMLOG(DM_LOG_ERROR, "write subscribeId failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) { + DMLOG(DM_LOG_ERROR, "write deviceInfo failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + uint16_t subscribeId = pReq->GetSubscribeId(); + int32_t result = pReq->GetResult(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt16(subscribeId)) { + DMLOG(DM_LOG_ERROR, "write subscribeId failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string deviceId = pReq->GetDeviceId(); + int32_t pinToken = pReq->GetPinToken(); + int32_t status = pReq->GetStatus(); + int32_t reason = pReq->GetReason(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteString(deviceId)) { + DMLOG(DM_LOG_ERROR, "write deviceId failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(pinToken)) { + DMLOG(DM_LOG_ERROR, "write pinToken failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(status)) { + DMLOG(DM_LOG_ERROR, "write status failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(reason)) { + DMLOG(DM_LOG_ERROR, "write reason failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_SET_REQUEST(SERVER_CHECK_AUTH_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string deviceId = pReq->GetDeviceId(); + int32_t result = pReq->GetResult(); + int32_t flag = pReq->GetFlag(); + if (!data.WriteString(pkgName)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteString(deviceId)) { + DMLOG(DM_LOG_ERROR, "write deviceId failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteInt32(flag)) { + DMLOG(DM_LOG_ERROR, "write flag failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_CHECK_AUTH_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + std::string extra = data.ReadString(); + DMLOG(DM_LOG_INFO, "pkgName:%s, extra:%s", pkgName.c_str(), extra.c_str()); + DmDeviceInfo *info = nullptr; + int32_t infoNum = 0; + int32_t result = IpcServerAdapter::GetInstance().GetTrustedDeviceList(pkgName, extra, &info, &infoNum); + reply.WriteInt32(infoNum); + if (infoNum > 0 && info != nullptr) { + if (!reply.WriteRawData(info, sizeof(DmDeviceInfo) * infoNum)) { + DMLOG(DM_LOG_ERROR, "write subscribeInfo failed"); + } + free(info); + } + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + sptr listener = data.ReadRemoteObject(); + int32_t result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + int32_t result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + DmSubscribeInfo *subscribeInfo = (DmSubscribeInfo *)data.ReadRawData(sizeof(DmSubscribeInfo)); + DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeInfo->subscribeId); + int32_t result = IpcServerAdapter::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(STOP_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + uint16_t subscribeId = data.ReadInt32(); + DMLOG(DM_LOG_INFO, "pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeId); + int32_t result = IpcServerAdapter::GetInstance().StopDiscovery(pkgName, subscribeId); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + std::string extra = data.ReadString(); + DmDeviceInfo *deviceInfo = (DmDeviceInfo *)data.ReadRawData(sizeof(DmDeviceInfo)); + int32_t appIconLen = data.ReadInt32(); + int32_t appThumbnailLen = data.ReadInt32(); + uint8_t *appIcon = appIconLen > 0? (uint8_t *)data.ReadRawData(appIconLen) : nullptr; + uint8_t *appThumbnail = appThumbnailLen > 0? (uint8_t *)data.ReadRawData(appThumbnailLen) : nullptr; + DmAppImageInfo imageInfo(appIcon, appIconLen, appThumbnail, appThumbnailLen); + int32_t result = IpcServerAdapter::GetInstance().AuthenticateDevice(pkgName, *deviceInfo, imageInfo, extra); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(CHECK_AUTHENTICATION, MessageParcel &data, MessageParcel &reply) +{ + std::string authPara = data.ReadString(); + int32_t result = IpcServerAdapter::GetInstance().CheckAuthentication(authPara); + if (!reply.WriteInt32(result)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_GET_AUTHENTCATION_INFO, MessageParcel &data, MessageParcel &reply) +{ + std::string packName = data.ReadString(); + DmAuthParam authParam; + int32_t ret = DEVICEMANAGER_OK; + DMLOG(DM_LOG_ERROR, "DeviceManagerStub:: GET_AUTHENTCATION_INFO:pkgName:%s", packName.c_str()); + IpcServerAdapter::GetInstance().GetAuthenticationParam(packName, authParam); + if (authParam.direction == AUTH_SESSION_SIDE_CLIENT) { + if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) || + !reply.WriteInt32(authParam.pinToken)) { + DMLOG(DM_LOG_ERROR,"DeviceManagerStub::wirte client fail"); + ret = DEVICEMANAGER_WRITE_FAILED; + } + return ret; + } + + int32_t appIconLen = authParam.imageinfo.GetAppIconLen(); + int32_t appThumbnailLen = authParam.imageinfo.GetAppThumbnailLen(); + if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) || + !reply.WriteString(authParam.packageName) || !reply.WriteString(authParam.appName) || + !reply.WriteString(authParam.appDescription) || !reply.WriteInt32(authParam.business) || + !reply.WriteInt32(authParam.pincode) || !reply.WriteInt32(appIconLen) || + !reply.WriteInt32(appThumbnailLen)) { + DMLOG(DM_LOG_ERROR, "write reply failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + + if (appIconLen > 0 && authParam.imageinfo.GetAppIcon() != nullptr) { + if (!reply.WriteRawData(authParam.imageinfo.GetAppIcon(), appIconLen)){ + DMLOG(DM_LOG_ERROR, "write appIcon failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + } + + if (appThumbnailLen > 0 && authParam.imageinfo.GetAppThumbnail() != nullptr) { + if (!reply.WriteRawData(authParam.imageinfo.GetAppThumbnail(), appThumbnailLen)) { + DMLOG(DM_LOG_ERROR, "write appThumbnail failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + } + + return DEVICEMANAGER_OK; +} + +ON_IPC_CMD(SERVER_USER_AUTHORIZATION_OPERATION, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + int32_t action = data.ReadInt32(); + int result = IpcServerAdapter::GetInstance().SetUserOperation(packageName, action); + + if (!reply.WriteInt32(action)) { + DMLOG(DM_LOG_ERROR, "write result failed"); + return DEVICEMANAGER_WRITE_FAILED; + } + return result; + +} +ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY, std::shared_ptr pBaseReq, MessageParcel& data) +{ + DMLOG(DM_LOG_INFO, "OnFaCallBack"); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string packagname = pReq->GetPkgName(); + std::string paramJson = pReq->GetJsonParam(); + if (!data.WriteString(packagname)) { + DMLOG(DM_LOG_ERROR, "write pkgName failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + if (!data.WriteString(paramJson)) + { + DMLOG(DM_LOG_ERROR, "write paramJson failed"); + return DEVICEMANAGER_FLATTEN_OBJECT; + } + return DEVICEMANAGER_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_DEVICEMANAGER_FA_NOTIFY, MessageParcel& reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_server_client_proxy.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_server_client_proxy.cpp new file mode 100644 index 000000000..6dc1a24c1 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/standard/ipc_server_client_proxy.cpp @@ -0,0 +1,48 @@ +/* + * 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 "ipc_server_client_proxy.h" + +#include "ipc_types.h" + +#include "ipc_cmd_register.h" +#include "ipc_def.h" +#include "device_manager_log.h" +#include "device_manager_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcServerClientProxy::SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DMLOG(DM_LOG_ERROR, "remote service null"); + return DEVICEMANAGER_NULLPTR; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data) != DEVICEMANAGER_OK) { + return DEVICEMANAGER_FAILED; + } + if (remote->SendRequest(cmdCode, data, reply, option) != DEVICEMANAGER_OK) { + DMLOG(DM_LOG_ERROR, "SendRequest fail, cmd:%d", cmdCode); + return DEVICEMANAGER_IPC_FAILED; + } + return IpcCmdRegister::GetInstance().ReadResponse(cmdCode, reply, rsp); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_server_listener.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_server_listener.cpp new file mode 100644 index 000000000..e08eaeab5 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/standard/ipc_server_listener.cpp @@ -0,0 +1,48 @@ +/* + * 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 "ipc_server_listener.h" +#include "ipc_server_stub.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcServerListener::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + std::string pkgName = req->GetPkgName(); + sptr listener = IpcServerStub::GetInstance().GetDmListener(pkgName); + if (listener == nullptr) { + DMLOG(DM_LOG_INFO, "cannot get listener for package:%s.", pkgName.c_str()); + return DEVICEMANAGER_NULLPTR; + } + return listener->SendCmd(cmdCode, req, rsp); +} + +int32_t IpcServerListener::SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + std::map> listeners = IpcServerStub::GetInstance().GetDmListener(); + for (auto iter : listeners) { + auto pkgName = iter.first; + auto remote = iter.second; + req->SetPkgName(pkgName); + sptr listener = iface_cast(remote); + listener->SendCmd(cmdCode, req, rsp); + } + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp new file mode 100644 index 000000000..c259394f5 --- /dev/null +++ b/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp @@ -0,0 +1,197 @@ +/* + * 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 "ipc_server_stub.h" + +#include +#include + +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +#include "ipc_server_adapter.h" +#include "ipc_cmd_register.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcServerStub); + +const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&IpcServerStub::GetInstance()); + +IpcServerStub::IpcServerStub() : SystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, true) +{ + registerToService_ = false; + state_ = ServiceRunningState::STATE_NOT_START; +} + +void IpcServerStub::OnStart() +{ + DMLOG(DM_LOG_INFO, "IpcServerStub::OnStart start"); + if (state_ == ServiceRunningState::STATE_RUNNING) { + DMLOG(DM_LOG_DEBUG, "IpcServerStub has already started."); + return; + } + if (!Init()) { + DMLOG(DM_LOG_ERROR, "failed to init IpcServerStub"); + return; + } + state_ = ServiceRunningState::STATE_RUNNING; +} + +bool IpcServerStub::Init() +{ + DMLOG(DM_LOG_INFO, "IpcServerStub::Init ready to init."); + if (!registerToService_) { + bool ret = Publish(this); + if (!ret) { + DMLOG(DM_LOG_ERROR, "IpcServerStub::Init Publish failed!"); + return false; + } + registerToService_ = true; + } + + std::thread {[] { + IpcServerAdapter::GetInstance().ModuleInit(); + }}.detach(); + return true; +} + +void IpcServerStub::OnStop() +{ + DMLOG(DM_LOG_INFO, "IpcServerStub::OnStop ready to stop service."); + state_ = ServiceRunningState::STATE_NOT_START; + registerToService_ = false; +} + +int32_t IpcServerStub::OnRemoteRequest(uint32_t code, + MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + DMLOG(DM_LOG_INFO, "code = %d, flags= %d.", code, option.GetFlags()); + int32_t ret = DEVICEMANAGER_OK; + ret = IpcCmdRegister::GetInstance().OnIpcCmd(code, data, reply); + if (ret == DEVICEMANAGER_IPC_NOT_REGISTER_FUNC) { + DMLOG(DM_LOG_WARN, "unsupport code: %d", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return ret; +} + +int32_t IpcServerStub::SendCmd(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + return DEVICEMANAGER_OK; +} + +ServiceRunningState IpcServerStub::QueryServiceState() const +{ + return state_; +} + +int32_t IpcServerStub::RegisterDeviceManagerListener(std::string &pkgName, sptr listener) +{ + if (pkgName.empty() || listener == nullptr) { + DMLOG(DM_LOG_ERROR, "Error: parameter invalid"); + return DEVICEMANAGER_NULLPTR; + } + + DMLOG(DM_LOG_INFO, "In, pkgName: %s", pkgName.c_str()); + std::lock_guard autoLock(listenerLock_); + auto iter = dmListener_.find(pkgName); + if (iter != dmListener_.end()) { + DMLOG(DM_LOG_INFO, "RegisterDeviceManagerListener: listener already exists"); + return DEVICEMANAGER_OK; + } + + sptr appRecipient = sptr(new AppDeathRecipient()); + if (!listener->AddDeathRecipient(appRecipient)) { + DMLOG(DM_LOG_ERROR, "RegisterDeviceManagerListener: AddDeathRecipient Failed"); + } + dmListener_[pkgName] = listener; + appRecipient_[pkgName] = appRecipient; + return DEVICEMANAGER_OK; +} + +int32_t IpcServerStub::UnRegisterDeviceManagerListener(std::string &pkgName) +{ + if (pkgName.empty()) { + DMLOG(DM_LOG_ERROR, "Error: parameter invalid"); + return DEVICEMANAGER_NULLPTR; + } + + DMLOG(DM_LOG_INFO, "In, pkgName: %s", pkgName.c_str()); + std::lock_guard autoLock(listenerLock_); + auto listenerIter = dmListener_.find(pkgName); + if (listenerIter == dmListener_.end()) { + DMLOG(DM_LOG_INFO, "UnRegisterDeviceManagerListener: listener not exists"); + return DEVICEMANAGER_OK; + } + + auto recipientIter = appRecipient_.find(pkgName); + if (recipientIter == appRecipient_.end()) { + DMLOG(DM_LOG_INFO, "UnRegisterDeviceManagerListener: appRecipient not exists"); + dmListener_.erase(pkgName); + return DEVICEMANAGER_OK; + } + + auto listener = listenerIter->second; + auto appRecipient = recipientIter->second; + listener->RemoveDeathRecipient(appRecipient); + appRecipient_.erase(pkgName); + dmListener_.erase(pkgName); + return DEVICEMANAGER_OK; +} + +const std::map> &IpcServerStub::GetDmListener() +{ + return dmListener_; +} + +const sptr IpcServerStub::GetDmListener(std::string pkgName) const +{ + auto iter = dmListener_.find(pkgName); + if (iter == dmListener_.end()) { + return nullptr; + } + auto remote = iter->second; + sptr dmListener = iface_cast(remote); + return dmListener; +} + +void AppDeathRecipient::OnRemoteDied(const wptr &remote) +{ + DMLOG(DM_LOG_WARN, "AppDeathRecipient: OnRemoteDied"); + std::map> listeners = IpcServerStub::GetInstance().GetDmListener(); + std::string pkgName; + for (auto iter : listeners) { + if (iter.second == remote.promote()) { + pkgName = iter.first; + break; + } + } + if (pkgName.empty()) { + DMLOG(DM_LOG_ERROR, "AppDeathRecipient: OnRemoteDied, no pkgName matched"); + return; + } + DMLOG(DM_LOG_INFO, "AppDeathRecipient: OnRemoteDied for %s", pkgName.c_str()); + IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/utils/include/anonymous_string.h b/utils/include/anonymous_string.h new file mode 100644 index 000000000..4144e517c --- /dev/null +++ b/utils/include/anonymous_string.h @@ -0,0 +1,26 @@ +/* + * 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 ANONYMOUS_STRING_H +#define ANONYMOUS_STRING_H +#include + +namespace OHOS { +namespace DistributedHardware { +std::string GetAnonyString(const std::string &value); +std::string GetAnonyInt32(const int32_t value); +} +} +#endif \ No newline at end of file diff --git a/utils/include/cipher/encrypt_utils.h b/utils/include/cipher/encrypt_utils.h new file mode 100644 index 000000000..db19b2364 --- /dev/null +++ b/utils/include/cipher/encrypt_utils.h @@ -0,0 +1,46 @@ +/* + * 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_ENCRYPT_UTILS_H +#define OHOS_DEVICE_MANAGER_ENCRYPT_UTILS_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class EncryptUtils { +public: + static int32_t MbedtlsBase64Encode(uint8_t *dst, size_t dlen, size_t *olen, + const uint8_t *src, size_t slen); + static int32_t MbedtlsBase64Decode(uint8_t *dst, size_t dlen, size_t *olen, + const uint8_t *src, size_t slen); + static int32_t GenRandInt(int32_t randMin, int32_t randMax); + static int64_t GenRandLongLong(int64_t randMin, int64_t randMax); + static int32_t GetRandomData(uint8_t *randStr, uint32_t len); + static int32_t MbedtlsEncrypt(const uint8_t *plainText, int32_t plainTextLen, uint8_t *cipherText, + int32_t cipherTextLen, int32_t *outLen); + static int32_t MbedtlsDecrypt(const uint8_t *cipherText, int32_t cipherTextLen, uint8_t *plainText, + int32_t plainTextLen, int32_t *outLen); + /** + * szOut最后一位为结束符,比如 szOutLen=4 可能返回 "abc" + * @param szOutLen 至少2 + * @param numberOnly 是否只生成数据 如果为true 则期望返回随机数字 如果为否,则期望返回随机字符 + */ + static bool MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_ENCRYPT_UTILS_H \ No newline at end of file diff --git a/utils/include/ipc/lite/ipc_cmd_register.h b/utils/include/ipc/lite/ipc_cmd_register.h new file mode 100644 index 000000000..6b52515aa --- /dev/null +++ b/utils/include/ipc/lite/ipc_cmd_register.h @@ -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. + */ + +#ifndef OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H +#define OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H + +#include +#include + +#include "liteipc_adapter.h" +#include "single_instance.h" + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +#define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB, paraC, paraD) \ + static int32_t IpcSetRequest##cmdCode(paraA, paraB, paraC, paraD); \ + struct IpcRegisterSetRequestFunc##cmdCode { \ + IpcRegisterSetRequestFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterSetRequestFunc(cmdCode, IpcSetRequest##cmdCode); \ + } \ + }; \ + IpcRegisterSetRequestFunc##cmdCode g_IpcRegisterSetRequestFunc##cmdCode; \ + static int32_t IpcSetRequest##cmdCode(paraA, paraB, paraC, paraD) \ + +#define ON_IPC_READ_RESPONSE(cmdCode, paraA, paraB) \ + static int32_t IpcReadResponse##cmdCode(paraA, paraB); \ + struct IpcRegisterReadResponseFunc##cmdCode { \ + IpcRegisterReadResponseFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterReadResponseFunc(cmdCode, IpcReadResponse##cmdCode); \ + } \ + }; \ + IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode; \ + static int32_t IpcReadResponse##cmdCode(paraA, paraB) \ + +#define ON_IPC_CMD(cmdCode, paraA, paraB, paraC) \ + static void IpcCmdProcess##cmdCode(paraA, paraB, paraC); \ + struct IpcRegisterCmdProcessFunc##cmdCode { \ + IpcRegisterCmdProcessFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterCmdProcessFunc(cmdCode, IpcCmdProcess##cmdCode); \ + } \ + }; \ + IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode; \ + static void IpcCmdProcess##cmdCode(paraA, paraB, paraC) \ + +#define ON_IPC_SERVER_CMD(cmdCode, paraA, paraB) \ + static void IpcServerCmdProcess##cmdCode(paraA, paraB); \ + class IpcRegisterServerCmdProcessFunc##cmdCode { \ + public: \ + IpcRegisterServerCmdProcessFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterServerCmdProcessFunc(cmdCode, IpcServerCmdProcess##cmdCode); \ + } \ + }; \ + IpcRegisterServerCmdProcessFunc##cmdCode g_IpcRegisterServerCmdProcessFunc##cmdCode; \ + static void IpcServerCmdProcess##cmdCode(paraA, paraB) + +using SetIpcRequestFunc = int32_t (*)(std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t bufferLen); +using ReadResponseFunc = int32_t (*)(IpcIo &reply, std::shared_ptr pBaseRsp); +using OnIpcCmdFunc = void (*)(IpcIo &reply, const IpcContext &ctx, void *ipcMsg); +using OnIpcServerCmdFunc = void (*)(IpcIo &req, IpcIo &reply); + +class IpcCmdRegister { +DECLARE_SINGLE_INSTANCE(IpcCmdRegister); +public: + void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc) + { + setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc); + }; + void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc) + { + readResponseFuncMap_.emplace(cmdCode, readResponseFunc); + }; + void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc) + { + onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc); + }; + void RegisterServerCmdProcessFunc(int32_t cmdCode, OnIpcServerCmdFunc onIpcServerCmdFunc) + { + onIpcServerCmdFuncMap_.emplace(cmdCode, onIpcServerCmdFunc); + }; + int32_t SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen); + int32_t ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr pBaseRsp); + int32_t OnIpcCmd(int32_t cmdCode, IpcIo &reply, const IpcContext &ctx, void *ipcMsg); + int32_t OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply); +private: + std::unordered_map setIpcRequestFuncMap_; + std::unordered_map readResponseFuncMap_; + std::unordered_map onIpcCmdFuncMap_; + std::unordered_map onIpcServerCmdFuncMap_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H \ No newline at end of file diff --git a/utils/include/ipc/standard/ipc_cmd_register.h b/utils/include/ipc/standard/ipc_cmd_register.h new file mode 100644 index 000000000..dc852f57e --- /dev/null +++ b/utils/include/ipc/standard/ipc_cmd_register.h @@ -0,0 +1,93 @@ +/* + * 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_CMD_PARSER_H +#define OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H + +#include +#include + +#include "iremote_broker.h" +#include "single_instance.h" + +#include "ipc_types.h" +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +#define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB) \ + static int32_t IpcSetRequest##cmdCode(paraA, paraB); \ + struct IpcRegisterSetRequestFunc##cmdCode { \ + IpcRegisterSetRequestFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterSetRequestFunc(cmdCode, IpcSetRequest##cmdCode); \ + } \ + }; \ + IpcRegisterSetRequestFunc##cmdCode g_IpcRegisterSetRequestFunc##cmdCode; \ + static int32_t IpcSetRequest##cmdCode(paraA, paraB) \ + +#define ON_IPC_READ_RESPONSE(cmdCode, paraA, paraB) \ + static int32_t IpcReadResponse##cmdCode(paraA, paraB); \ + struct IpcRegisterReadResponseFunc##cmdCode { \ + IpcRegisterReadResponseFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterReadResponseFunc(cmdCode, IpcReadResponse##cmdCode); \ + } \ + }; \ + IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode; \ + static int32_t IpcReadResponse##cmdCode(paraA, paraB) \ + +#define ON_IPC_CMD(cmdCode, paraA, paraB) \ + static int32_t IpcCmdProcess##cmdCode(paraA, paraB); \ + struct IpcRegisterCmdProcessFunc##cmdCode { \ + IpcRegisterCmdProcessFunc##cmdCode() \ + { \ + IpcCmdRegister::GetInstance().RegisterCmdProcessFunc(cmdCode, IpcCmdProcess##cmdCode); \ + } \ + }; \ + IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode; \ + static int32_t IpcCmdProcess##cmdCode(paraA, paraB) + +using SetIpcRequestFunc = int32_t (*)(std::shared_ptr pBaseReq, MessageParcel &data); +using ReadResponseFunc = int32_t (*)(MessageParcel &reply, std::shared_ptr pBaseRsp); +using OnIpcCmdFunc = int32_t (*)(MessageParcel &data, MessageParcel &reply); + +class IpcCmdRegister { +DECLARE_SINGLE_INSTANCE(IpcCmdRegister); +public: + void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc) + { + setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc); + }; + void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc) + { + readResponseFuncMap_.emplace(cmdCode, readResponseFunc); + }; + void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc) + { + onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc); + }; + int32_t SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, MessageParcel &data); + int32_t ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr pBaseRsp); + int32_t OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply); +private: + std::unordered_map setIpcRequestFuncMap_; + std::unordered_map readResponseFuncMap_; + std::unordered_map onIpcCmdFuncMap_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H \ No newline at end of file diff --git a/utils/include/log/dm_log.h b/utils/include/log/dm_log.h new file mode 100644 index 000000000..1db27040a --- /dev/null +++ b/utils/include/log/dm_log.h @@ -0,0 +1,31 @@ +/* + * 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, ...); +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_LOG_H diff --git a/utils/src/anonymous_string.cpp b/utils/src/anonymous_string.cpp new file mode 100644 index 000000000..c3fdd4f3e --- /dev/null +++ b/utils/src/anonymous_string.cpp @@ -0,0 +1,68 @@ +/* + * 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 "anonymous_string.h" + +#include "securec.h" + +namespace OHOS { +namespace DistributedHardware { +std::string GetAnonyString(const std::string &value) +{ + const int INT32_SHORT_ID_LENGTH = 20; + const int INT32_PLAINTEXT_LENGTH = 4; + const int INT32_MIN_ID_LENGTH = 3; + std::string res; + std::string tmpStr("******"); + int32_t strLen = value.length(); + if (strLen < INT32_MIN_ID_LENGTH) { + return tmpStr; + } + + 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) +{ + const int INT32_STRING_LENGTH = 40; + char tempBuffer[INT32_STRING_LENGTH] = ""; + int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value); + if (secRet <= 0) { + std::string nullString(""); + return nullString; + } + int32_t length = strlen(tempBuffer); + for (int32_t i = 1; i < length - 1; i++) { + tempBuffer[i] = '*'; + } + if (length == 0x01) { + tempBuffer[0] = '*'; + } + + std::string tempSting(tempBuffer); + return tempSting; +} +} +} \ No newline at end of file diff --git a/utils/src/cipher/encrypt_utils.cpp b/utils/src/cipher/encrypt_utils.cpp new file mode 100644 index 000000000..11797a01e --- /dev/null +++ b/utils/src/cipher/encrypt_utils.cpp @@ -0,0 +1,148 @@ +/* + * 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 "encrypt_utils.h" + +#include "mbedtls/base64.h" +#include "mbedtls/gcm.h" +#include "mbedtls/md.h" + +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/entropy.h" + +#include +#include +#include +#include "device_manager_errno.h" +#include "device_manager_log.h" +#include "securec.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t EncryptUtils::MbedtlsBase64Encode(uint8_t *dst, size_t dlen, size_t *olen, + const uint8_t *src, size_t slen) +{ + return mbedtls_base64_encode(dst, dlen, olen, src, slen); +} + +int32_t EncryptUtils::MbedtlsBase64Decode(uint8_t *dst, size_t dlen, size_t *olen, + const uint8_t *src, size_t slen) +{ + return mbedtls_base64_decode(dst, dlen, olen, src, slen); +} + +int32_t EncryptUtils::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 EncryptUtils::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 EncryptUtils::GetRandomData(uint8_t *randStr, uint32_t len) +{ + mbedtls_entropy_context *entropy = nullptr; + mbedtls_ctr_drbg_context *ctrDrbg = nullptr; + int32_t ret = DEVICEMANAGER_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 = DEVICEMANAGER_OK; + } while (0); + if (entropy != nullptr) { + free(entropy); + } + if (ctrDrbg != nullptr) { + free(ctrDrbg); + } + return ret; +} + +int32_t EncryptUtils::MbedtlsEncrypt(const uint8_t *plainText, int plainTextLen, uint8_t *cipherText, + int cipherTextLen, int *outLen) +{ + if (memcpy_s(cipherText, cipherTextLen, plainText, plainTextLen) != DEVICEMANAGER_OK) { + return DEVICEMANAGER_COPY_FAILED; + } + *outLen = plainTextLen; + return DEVICEMANAGER_OK; +} + +int32_t EncryptUtils::MbedtlsDecrypt(const uint8_t *cipherText, int32_t cipherTextLen, uint8_t *plainText, + int32_t plainTextLen, int32_t *outLen) +{ + (void)outLen; + if (memcpy_s(plainText, plainTextLen, cipherText, cipherTextLen) != DEVICEMANAGER_OK) { + return DEVICEMANAGER_COPY_FAILED; + } + return DEVICEMANAGER_OK; +} + +bool EncryptUtils::MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly) +{ + if (szOut == nullptr || szOutLen <= 2) { + return false; + } + szOut[--szOutLen] = 0; + GetRandomData((uint8_t*)szOut, szOutLen); + const int NUMBER_COUNT = 10; + const int ALPHA_COUNT = 26; + int M = numberOnly ? NUMBER_COUNT : (NUMBER_COUNT + 2 * ALPHA_COUNT); + for (int i = 0; i < szOutLen; i++) { + // 0~9,A~Z,a~z + unsigned int idx = ((unsigned int)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 diff --git a/utils/src/ipc/lite/ipc_cmd_register.cpp b/utils/src/ipc/lite/ipc_cmd_register.cpp new file mode 100644 index 000000000..8faa2a22b --- /dev/null +++ b/utils/src/ipc/lite/ipc_cmd_register.cpp @@ -0,0 +1,68 @@ +/* + * 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 "ipc_cmd_register.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister); + +int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, IpcIo &request, + uint8_t *buffer, size_t buffLen) +{ + auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode); + if (setRequestMapIter == setIpcRequestFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register SetRequestFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + return (setRequestMapIter->second)(pBaseReq, request, buffer, buffLen); +} + +int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr pBaseRsp) +{ + auto readResponseMapIter = readResponseFuncMap_.find(cmdCode); + if (readResponseMapIter == readResponseFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register ReadResponseFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + return (readResponseMapIter->second)(reply, pBaseRsp); +} + +int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, IpcIo &reply, const IpcContext &ctx, void *ipcMsg) +{ + auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode); + if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register OnIpcCmdFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + (onIpcCmdMapIter->second)(reply, ctx, ipcMsg); + return DEVICEMANAGER_OK; +} + +int32_t IpcCmdRegister::OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply) +{ + auto onIpcServerCmdMapIter = onIpcServerCmdFuncMap_.find(cmdCode); + if (onIpcServerCmdMapIter == onIpcServerCmdFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register OnIpcCmdFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + (onIpcServerCmdMapIter->second)(req, reply); + return DEVICEMANAGER_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/utils/src/ipc/standard/ipc_cmd_register.cpp b/utils/src/ipc/standard/ipc_cmd_register.cpp new file mode 100644 index 000000000..e958dfd04 --- /dev/null +++ b/utils/src/ipc/standard/ipc_cmd_register.cpp @@ -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. + */ + +#include "ipc_cmd_register.h" + +#include "device_manager_errno.h" +#include "device_manager_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister); + +int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, MessageParcel &data) +{ + auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode); + if (setRequestMapIter == setIpcRequestFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register SetRequestFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + return (setRequestMapIter->second)(pBaseReq, data); +} + +int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + auto readResponseMapIter = readResponseFuncMap_.find(cmdCode); + if (readResponseMapIter == readResponseFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register ReadResponseFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + return (readResponseMapIter->second)(reply, pBaseRsp); +} + +int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply) +{ + auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode); + if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) { + DMLOG(DM_LOG_ERROR, "cmdCode:%d not register OnIpcCmdFunc", cmdCode); + return DEVICEMANAGER_IPC_NOT_REGISTER_FUNC; + } + return (onIpcCmdMapIter->second)(data, reply); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/utils/src/log/dm_log.cpp b/utils/src/log/dm_log.cpp new file mode 100644 index 000000000..538594b15 --- /dev/null +++ b/utils/src/log/dm_log.cpp @@ -0,0 +1,89 @@ +/* + * 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 "securec.h" + +#include "constants.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 -- Gitee From 053007bf8dc7c278fed357177018b8ed876c9bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=83=B3=E4=B8=8D=E5=87=BA=E5=88=AB=E5=90=8D?= Date: Sat, 11 Sep 2021 19:07:13 +0800 Subject: [PATCH 2/2] L1 & L2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 想不出别名 --- .../ipc/model/ipc_notify_dmfa_result_req.h | 2 -- .../include/notify/device_manager_notify.h | 5 ++--- .../src/ipc/lite/ipc_client_server_proxy.cpp | 1 + .../src/ipc/standard/ipc_cmd_parser.cpp | 14 +++++++------- utils/src/cipher/encrypt_utils.cpp | 16 +++++++++------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/common/include/ipc/model/ipc_notify_dmfa_result_req.h b/common/include/ipc/model/ipc_notify_dmfa_result_req.h index adbcf86cd..b76d26ba7 100644 --- a/common/include/ipc/model/ipc_notify_dmfa_result_req.h +++ b/common/include/ipc/model/ipc_notify_dmfa_result_req.h @@ -44,8 +44,6 @@ public: { JsonParam_ = JsonParam; } - - private: std::string PackageName_; std::string JsonParam_; diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 14dc0dcf6..d49118a88 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -46,9 +46,8 @@ public: std::shared_ptr callback); void UnRegisterCheckAuthenticationCallback(std::string &pkgName); void RegisterDeviceManagerFaCallback(std::string &packageName, - std::shared_ptr callback); + std::shared_ptr callback); void UnRegisterDeviceManagerFaCallback(std::string &pkgName); - public: void OnRemoteDied(); void OnDeviceOnline(std::string &pkgName, const DmDeviceInfo &deviceInfo); @@ -60,7 +59,7 @@ public: void OnAuthResult(std::string &pkgName, std::string &deviceId, int32_t pinToken, uint32_t status, uint32_t reason); void OnCheckAuthResult(std::string &pkgName, std::string &deviceId, int32_t resultCode, int32_t flag); - void OnFaCall(std::string &pkgName,std::string ¶mJson); + void OnFaCall(std::string &pkgName, std::string ¶mJson); private: std::mutex lock_; std::map> deviceStateCallback_; diff --git a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp index 88a0464dc..922844c70 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/lite/ipc_client_server_proxy.cpp @@ -55,6 +55,7 @@ int32_t DMDeathCallback(const IpcContext *ctx, void *ipcMsg, IpcIo *data, void * static int32_t SendCmdResultCb(IOwner owner, int code, IpcIo *reply) { + (void)code; int32_t cmdCode = *(int32_t *)owner; DMLOG(DM_LOG_INFO, "SendCmdResultCb code:%d", cmdCode); (void)IpcCmdRegister::GetInstance().ReadResponse(cmdCode, *reply, pCurRsp); diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index a2378158c..15935bb99 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -285,13 +285,14 @@ ON_IPC_CMD(SERVER_GET_AUTHENTCATION_INFO, MessageParcel &data, MessageParcel &re std::string packName = data.ReadString(); DmAuthParam authParam; int32_t ret = DEVICEMANAGER_OK; - DMLOG(DM_LOG_ERROR, "DeviceManagerStub:: GET_AUTHENTCATION_INFO:pkgName:%s", packName.c_str()); + DMLOG(DM_LOG_ERROR, "GET_AUTHENTCATION_INFO:pkgName:%s", packName.c_str()); IpcServerAdapter::GetInstance().GetAuthenticationParam(packName, authParam); if (authParam.direction == AUTH_SESSION_SIDE_CLIENT) { - if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) || + if (!reply.WriteInt32(authParam.direction) || + !reply.WriteInt32(authParam.authType) || !reply.WriteInt32(authParam.pinToken)) { - DMLOG(DM_LOG_ERROR,"DeviceManagerStub::wirte client fail"); - ret = DEVICEMANAGER_WRITE_FAILED; + DMLOG(DM_LOG_ERROR, "wirte client fail"); + ret = DEVICEMANAGER_WRITE_FAILED; } return ret; } @@ -320,7 +321,6 @@ ON_IPC_CMD(SERVER_GET_AUTHENTCATION_INFO, MessageParcel &data, MessageParcel &re return DEVICEMANAGER_WRITE_FAILED; } } - return DEVICEMANAGER_OK; } @@ -329,14 +329,14 @@ ON_IPC_CMD(SERVER_USER_AUTHORIZATION_OPERATION, MessageParcel &data, MessageParc std::string packageName = data.ReadString(); int32_t action = data.ReadInt32(); int result = IpcServerAdapter::GetInstance().SetUserOperation(packageName, action); - + if (!reply.WriteInt32(action)) { DMLOG(DM_LOG_ERROR, "write result failed"); return DEVICEMANAGER_WRITE_FAILED; } return result; - } + ON_IPC_SET_REQUEST(SERVER_DEVICEMANAGER_FA_NOTIFY, std::shared_ptr pBaseReq, MessageParcel& data) { DMLOG(DM_LOG_INFO, "OnFaCallBack"); diff --git a/utils/src/cipher/encrypt_utils.cpp b/utils/src/cipher/encrypt_utils.cpp index 11797a01e..c97fc6934 100644 --- a/utils/src/cipher/encrypt_utils.cpp +++ b/utils/src/cipher/encrypt_utils.cpp @@ -98,7 +98,7 @@ int32_t EncryptUtils::GetRandomData(uint8_t *randStr, uint32_t len) } int32_t EncryptUtils::MbedtlsEncrypt(const uint8_t *plainText, int plainTextLen, uint8_t *cipherText, - int cipherTextLen, int *outLen) + int32_t cipherTextLen, int32_t *outLen) { if (memcpy_s(cipherText, cipherTextLen, plainText, plainTextLen) != DEVICEMANAGER_OK) { return DEVICEMANAGER_COPY_FAILED; @@ -119,17 +119,19 @@ int32_t EncryptUtils::MbedtlsDecrypt(const uint8_t *cipherText, int32_t cipherTe bool EncryptUtils::MbedtlsGenRandomStr(char *szOut, int32_t szOutLen, bool numberOnly) { - if (szOut == nullptr || szOutLen <= 2) { + 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 int NUMBER_COUNT = 10; - const int ALPHA_COUNT = 26; - int M = numberOnly ? NUMBER_COUNT : (NUMBER_COUNT + 2 * ALPHA_COUNT); - for (int i = 0; i < szOutLen; i++) { + 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 - unsigned int idx = ((unsigned int)szOut[i] % M); + uint32_t idx = ((uint32_t)szOut[i] % M); char base; if (idx < NUMBER_COUNT) { base = '0'; -- Gitee