From 39bb2b517ee6ab3a5b587a743c1fdcb883db43d2 Mon Sep 17 00:00:00 2001 From: "weidong.liu@thundersoft.com" Date: Thu, 24 Feb 2022 19:41:12 +0800 Subject: [PATCH] compile test for ds --- bundle.json | 3 +- common/include/ipc/ipc_def.h | 2 + .../ipc/model/ipc_register_decision_req.h | 44 ++++ .../native_cpp/include/device_manager.h | 2 + .../native_cpp/include/device_manager_impl.h | 2 + .../native_cpp/src/device_manager_impl.cpp | 52 ++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 49 ++++ .../kits/js/include/native_devicemanager_js.h | 24 +- .../kits/js/src/native_devicemanager_js.cpp | 229 ++++++++++++------ .../include/config/json_config.h | 10 +- .../include/device_manager_service.h | 2 + .../deviceinfo/dm_device_info_manager.h | 3 +- .../devicestate/dm_device_state_manager.h | 8 + .../src/device_manager_service.cpp | 12 + .../src/device_manager_service_listener.cpp | 7 +- .../src/deviceinfo/dm_device_info_manager.cpp | 4 +- .../devicestate/dm_device_state_manager.cpp | 109 ++++++++- .../src/ipc/standard/ipc_cmd_parser.cpp | 24 ++ 18 files changed, 498 insertions(+), 88 deletions(-) create mode 100644 common/include/ipc/model/ipc_register_decision_req.h diff --git a/bundle.json b/bundle.json index 06952ef47..c736f2842 100644 --- a/bundle.json +++ b/bundle.json @@ -47,7 +47,8 @@ "//foundation/distributedhardware/devicemanager/interfaces/kits:devicemanager_native_js", "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", "//foundation/distributedhardware/devicemanager/sa_profile:dm_sa_profile", - "//foundation/distributedhardware/devicemanager/ext:ext_modules" + "//foundation/distributedhardware/devicemanager/ext:ext_modules", + "//foundation/distributedhardware/devicemanager/ext/decision:devicemanagerext_decision" ], "inner_kits": [ { diff --git a/common/include/ipc/ipc_def.h b/common/include/ipc/ipc_def.h index ec3a93d3e..b3231043b 100644 --- a/common/include/ipc/ipc_def.h +++ b/common/include/ipc/ipc_def.h @@ -54,6 +54,8 @@ enum IpcCmdID { SERVER_GET_DMFA_INFO, SERVER_USER_AUTH_OPERATION, SERVER_DEVICE_FA_NOTIFY, + REGISTER_DECISION_FILTER, + UNREGISTER_DECISION_FILTER, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_register_decision_req.h b/common/include/ipc/model/ipc_register_decision_req.h new file mode 100644 index 000000000..8aed79ee9 --- /dev/null +++ b/common/include/ipc/model/ipc_register_decision_req.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_IPC_REGISTER_DECISION_REQ_H +#define OHOS_DM_IPC_REGISTER_DECISION_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRegisterDecisionReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegisterDecisionReq); + +public: + const std::string &GetExtra() const + { + return extra_; + } + + void SetExtra(const std::string &extra) + { + extra_ = extra; + } + +private: + std::string extra_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_REGISTER_DECISION_REQ_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 9e76a1361..ee60c2aed 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -54,6 +54,8 @@ public: virtual int32_t SetUserOperation(const std::string &pkgName, int32_t action) = 0; virtual int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid) = 0; virtual int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid) = 0; + virtual int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) = 0; + virtual int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 1e38d7dbd..ee8581d22 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -55,6 +55,8 @@ public: std::string &udid) override; virtual int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid) override; + virtual int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) override; + virtual int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) override; private: DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 452c1704a..f1c6c3a2a 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -32,6 +32,7 @@ #include "ipc_stop_discovery_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" +#include "ipc_register_decision_req.h" #include "securec.h" namespace OHOS { @@ -143,6 +144,9 @@ int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, } DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback); + if (extra != "") { + RegisterDecisionFilter(pkgName, extra); + } LOGI("RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); return DM_OK; } @@ -156,6 +160,8 @@ int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName } DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName); + std::string extra = ""; + UnRegisterDecisionFilter(pkgName, extra); LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str()); return DM_OK; } @@ -439,5 +445,51 @@ int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const uuid = rsp->GetUuid(); return DM_OK; } + +int32_t DeviceManagerImpl::RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("RegisterDecisionFilter failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + + if (ipcClientProxy_->SendRequest(REGISTER_DECISION_FILTER, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("RegisterDecisionFilter Failed with ret %d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("UnRegisterDecisionFilter failed, pkgName is empty"); + return DM_INVALID_VALUE; + } + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + + if (ipcClientProxy_->SendRequest(UNREGISTER_DECISION_FILTER, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnRegisterDecisionFilter Failed with ret %d", ret); + return ret; + } + return DM_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 index be759e55e..451c83d2b 100644 --- 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 @@ -34,6 +34,7 @@ #include "ipc_stop_discovery_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" +#include "ipc_register_decision_req.h" #include "securec.h" namespace OHOS { @@ -349,6 +350,54 @@ ON_IPC_READ_RESPONSE(SERVER_USER_AUTH_OPERATION, MessageParcel &reply, std::shar return DM_OK; } +ON_IPC_SET_REQUEST(REGISTER_DECISION_FILTER, 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)) { + LOGE("write pkgName failed"); + return DM_IPC_TRANSACTION_FAILED; + } + if (!data.WriteString(extra)) { + LOGE("write extra failed"); + return DM_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_DECISION_FILTER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_DECISION_FILTER, 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)) { + LOGE("write pkgName failed"); + return DM_IPC_TRANSACTION_FAILED; + } + if (!data.WriteString(extra)) { + LOGE("write extra failed"); + return DM_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_DECISION_FILTER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + ON_IPC_CMD(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &data, MessageParcel &reply) { std::string pkgName = data.ReadString(); diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index 71cb995b4..f3a0f1a53 100644 --- a/interfaces/kits/js/include/native_devicemanager_js.h +++ b/interfaces/kits/js/include/native_devicemanager_js.h @@ -47,6 +47,20 @@ struct DeviceInfoAsyncCallbackInfo { std::string bundleName; size_t bundleNameLen = 0; OHOS::DistributedHardware::DmDeviceInfo deviceInfo; + std::string extra; + // OHOS::DistributedHardware::DmFilterOptions filter; + napi_ref callback = nullptr; + napi_value thisVar = nullptr; + napi_deferred deferred = nullptr; + int32_t status = -1; +}; + +struct DeviceInfoListAsyncCallbackInfo { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + + std::string bundleName; + size_t bundleNameLen = 0; std::vector devList; std::string extra; // OHOS::DistributedHardware::DmFilterOptions filter; @@ -54,7 +68,6 @@ struct DeviceInfoAsyncCallbackInfo { napi_value thisVar = nullptr; napi_deferred deferred = nullptr; int32_t status = -1; - int32_t isList = 0; }; struct AuthAsyncCallbackInfo { @@ -183,6 +196,7 @@ public: static void HandleCreateDmCallBack(const napi_env &env, AsyncCallbackInfo *asCallbackInfo); static DeviceManagerNapi *GetDeviceManagerNapi(std::string &buldleName); static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType); + static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra); static void ReleaseDmCallback(std::string &bundleName, std::string &eventType); static void DeviceInfoToJsArray(const napi_env &env, const std::vector &vecDevInfo, @@ -229,12 +243,14 @@ private: static napi_value JsOnFrench(napi_env env, int32_t num, napi_value thisVar, napi_value argv[]); static void CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); static void CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + static void CallAsyncWorkSync(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); + static void CallAsyncWork(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetTrustedDeviceListStatusSync(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetTrustedDeviceListStatus(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static napi_value CallDeviceList(napi_env env, napi_callback_info info, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo); static void CallGetLocalDeviceInfoSync(napi_env env, napi_status &status, DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo); static void CallGetLocalDeviceInfo(napi_env env, napi_status &status, diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index bb96c37a7..6a6e585e1 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -860,6 +860,25 @@ void DeviceManagerNapi::CreateDmCallback(napi_env env, std::string &bundleName, } } +void DeviceManagerNapi::CreateDmCallback(napi_env env, std::string &bundleName, + std::string &eventType, std::string &extra) +{ + LOGE("CreateDmCallback for bundleName %s eventType %s extra=%s", + bundleName.c_str(), eventType.c_str(), extra.c_str()); + if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { + auto iter = g_deviceStateCallbackMap.find(bundleName); + if (iter == g_deviceStateCallbackMap.end()) { + auto callback = std::make_shared(env, bundleName); + int32_t ret = DeviceManager::GetInstance().RegisterDevStateCallback(bundleName, extra, callback); + if (ret != 0) { + LOGE("RegisterDevStateCallback failed for bunderName %s", bundleName.c_str()); + return; + } + g_deviceStateCallbackMap[bundleName] = callback; + } + } +} + void DeviceManagerNapi::ReleaseDmCallback(std::string &bundleName, std::string &eventType) { if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { @@ -953,27 +972,28 @@ napi_value DeviceManagerNapi::SetUserOperationSync(napi_env env, napi_callback_i } void DeviceManagerNapi::CallGetTrustedDeviceListStatusSync(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { - for (unsigned int i = 0; i < deviceInfoAsyncCallbackInfo->devList.size(); i++) { + for (unsigned int i = 0; i < deviceInfoListAsyncCallbackInfo->devList.size(); i++) { LOGI("DeviceManager::GetTrustedDeviceList deviceId:%s deviceName:%s deviceTypeId:%d ", - deviceInfoAsyncCallbackInfo->devList[i].deviceId, deviceInfoAsyncCallbackInfo->devList[i].deviceName, - deviceInfoAsyncCallbackInfo->devList[i].deviceTypeId); + deviceInfoListAsyncCallbackInfo->devList[i].deviceId, + deviceInfoListAsyncCallbackInfo->devList[i].deviceName, + deviceInfoListAsyncCallbackInfo->devList[i].deviceTypeId); } napi_value array[DM_NAPI_ARGS_TWO] = {0}; - if (deviceInfoAsyncCallbackInfo->status == 0) { - if (deviceInfoAsyncCallbackInfo->devList.size() > 0) { + if (deviceInfoListAsyncCallbackInfo->status == 0) { + if (deviceInfoListAsyncCallbackInfo->devList.size() > 0) { bool isArray = false; napi_create_array(env, &array[1]); napi_is_array(env, array[1], &isArray); if (isArray == false) { LOGE("napi_create_array fail"); } - for (unsigned int i = 0; i != deviceInfoAsyncCallbackInfo->devList.size(); ++i) { - DeviceInfoToJsArray(env, deviceInfoAsyncCallbackInfo->devList, i, array[1]); + for (unsigned int i = 0; i != deviceInfoListAsyncCallbackInfo->devList.size(); ++i) { + DeviceInfoToJsArray(env, deviceInfoListAsyncCallbackInfo->devList, i, array[1]); } - napi_resolve_deferred(env, deviceInfoAsyncCallbackInfo->deferred, array[1]); + napi_resolve_deferred(env, deviceInfoListAsyncCallbackInfo->deferred, array[1]); LOGE("devList is OK"); } else { LOGE("devList is null"); @@ -981,7 +1001,7 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatusSync(napi_env env, napi_st } else { napi_create_object(env, &array[0]); SetValueInt32(env, "code", status, array[0]); - napi_reject_deferred(env, deviceInfoAsyncCallbackInfo->deferred, array[0]); + napi_reject_deferred(env, deviceInfoListAsyncCallbackInfo->deferred, array[0]); } } @@ -1025,19 +1045,20 @@ void DeviceManagerNapi::OnDmfaCall(const std::string ¶mJson) } void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status &status, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { - for (unsigned int i = 0; i < deviceInfoAsyncCallbackInfo->devList.size(); i++) { + for (unsigned int i = 0; i < deviceInfoListAsyncCallbackInfo->devList.size(); i++) { LOGI("DeviceManager::GetTrustedDeviceList deviceId:%s deviceName:%s deviceTypeId:%d ", - deviceInfoAsyncCallbackInfo->devList[i].deviceId, deviceInfoAsyncCallbackInfo->devList[i].deviceName, - deviceInfoAsyncCallbackInfo->devList[i].deviceTypeId); + deviceInfoListAsyncCallbackInfo->devList[i].deviceId, + deviceInfoListAsyncCallbackInfo->devList[i].deviceName, + deviceInfoListAsyncCallbackInfo->devList[i].deviceTypeId); } napi_value callResult = nullptr; napi_value handler = nullptr; napi_value array[DM_NAPI_ARGS_TWO] = {0}; - if (deviceInfoAsyncCallbackInfo->status == 0) { - if (deviceInfoAsyncCallbackInfo->devList.size() > 0) { + if (deviceInfoListAsyncCallbackInfo->status == 0) { + if (deviceInfoListAsyncCallbackInfo->devList.size() > 0) { bool isArray = false; napi_create_array(env, &array[1]); napi_is_array(env, array[1], &isArray); @@ -1045,8 +1066,8 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status LOGE("napi_create_array fail"); } - for (size_t i = 0; i != deviceInfoAsyncCallbackInfo->devList.size(); ++i) { - DeviceInfoToJsArray(env, deviceInfoAsyncCallbackInfo->devList, i, array[1]); + for (size_t i = 0; i != deviceInfoListAsyncCallbackInfo->devList.size(); ++i) { + DeviceInfoToJsArray(env, deviceInfoListAsyncCallbackInfo->devList, i, array[1]); } LOGE("devList is OK"); } else { @@ -1057,10 +1078,10 @@ void DeviceManagerNapi::CallGetTrustedDeviceListStatus(napi_env env, napi_status SetValueInt32(env, "code", status, array[0]); } - napi_get_reference_value(env, deviceInfoAsyncCallbackInfo->callback, &handler); + napi_get_reference_value(env, deviceInfoListAsyncCallbackInfo->callback, &handler); if (handler != nullptr) { napi_call_function(env, nullptr, handler, DM_NAPI_ARGS_TWO, &array[0], &callResult); - napi_delete_reference(env, deviceInfoAsyncCallbackInfo->callback); + napi_delete_reference(env, deviceInfoListAsyncCallbackInfo->callback); } else { LOGE("handler is nullptr"); } @@ -1121,14 +1142,8 @@ void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackI (void)env; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; int32_t ret = 0; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->extra, - deviceInfoAsyncCallbackInfo->devList); - } else { - ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->deviceInfo); - } + ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, + deviceInfoAsyncCallbackInfo->deviceInfo); if (ret != 0) { LOGE("CallAsyncWorkSync for bunderName %s failed, ret %d", deviceInfoAsyncCallbackInfo->bundleName.c_str(), ret); @@ -1141,11 +1156,7 @@ void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, DeviceInfoAsyncCallbackI [](napi_env env, napi_status status, void *data) { (void)status; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - CallGetTrustedDeviceListStatusSync(env, status, deviceInfoAsyncCallbackInfo); - } else { - CallGetLocalDeviceInfoSync(env, status, deviceInfoAsyncCallbackInfo); - } + CallGetLocalDeviceInfoSync(env, status, deviceInfoAsyncCallbackInfo); napi_delete_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); delete deviceInfoAsyncCallbackInfo; }, @@ -1162,17 +1173,11 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo [](napi_env env, void *data) { DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; int32_t ret = 0; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->extra, - deviceInfoAsyncCallbackInfo->devList); - } else { - ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, - deviceInfoAsyncCallbackInfo->deviceInfo); - } + ret = DeviceManager::GetInstance().GetLocalDeviceInfo(deviceInfoAsyncCallbackInfo->bundleName, + deviceInfoAsyncCallbackInfo->deviceInfo); if (ret != 0) { - LOGE("CallAsyncWork for bunderName %s failed, ret %d", deviceInfoAsyncCallbackInfo->bundleName.c_str(), - ret); + LOGE("CallAsyncWork for bunderName %s failed, ret %d", + deviceInfoAsyncCallbackInfo->bundleName.c_str(), ret); deviceInfoAsyncCallbackInfo->status = -1; return; } @@ -1182,11 +1187,7 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo [](napi_env env, napi_status status, void *data) { (void)status; DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo = (DeviceInfoAsyncCallbackInfo *)data; - if (deviceInfoAsyncCallbackInfo->isList == 1) { - CallGetTrustedDeviceListStatus(env, status, deviceInfoAsyncCallbackInfo); - } else { - CallGetLocalDeviceInfo(env, status, deviceInfoAsyncCallbackInfo); - } + CallGetLocalDeviceInfo(env, status, deviceInfoAsyncCallbackInfo); napi_delete_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); delete deviceInfoAsyncCallbackInfo; }, @@ -1194,19 +1195,93 @@ void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoAsyncCallbackInfo napi_queue_async_work(env, deviceInfoAsyncCallbackInfo->asyncWork); } +void DeviceManagerNapi::CallAsyncWorkSync(napi_env env, + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) +{ + napi_value resourceName; + napi_create_string_latin1(env, "GetTrustListInfo", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + (void)env; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + int32_t ret = 0; + ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList); + if (ret != 0) { + LOGE("CallAsyncWorkSync for bunderName %s failed, ret %d", + deviceInfoListAsyncCallbackInfo->bundleName.c_str(), ret); + deviceInfoListAsyncCallbackInfo->status = -1; + return; + } + deviceInfoListAsyncCallbackInfo->status = 0; + LOGE("CallAsyncWorkSync status %d", deviceInfoListAsyncCallbackInfo->status); + }, + [](napi_env env, napi_status status, void *data) { + (void)status; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + CallGetTrustedDeviceListStatusSync(env, status, deviceInfoListAsyncCallbackInfo); + napi_delete_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); + delete deviceInfoListAsyncCallbackInfo; + }, + (void *)deviceInfoListAsyncCallbackInfo, &deviceInfoListAsyncCallbackInfo->asyncWork); + napi_queue_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); +} + +void DeviceManagerNapi::CallAsyncWork(napi_env env, DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) +{ + napi_value resourceName; + napi_create_string_latin1(env, "GetTrustListInfo", NAPI_AUTO_LENGTH, &resourceName); + // for test + std::string extraString = + "{\"FilterOptions\": {\"targetPkgName\":\"targetPkgName\",\"sortType\":0,\"filter\":\"filter\"}}"; + deviceInfoListAsyncCallbackInfo->extra = extraString; + DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList); + + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + int32_t ret = 0; + ret = DeviceManager::GetInstance().GetTrustedDeviceList(deviceInfoListAsyncCallbackInfo->bundleName, + deviceInfoListAsyncCallbackInfo->extra, + deviceInfoListAsyncCallbackInfo->devList); + if (ret != 0) { + LOGE("CallAsyncWork for bunderName %s failed, ret %d", + deviceInfoListAsyncCallbackInfo->bundleName.c_str(), ret); + deviceInfoListAsyncCallbackInfo->status = -1; + return; + } + deviceInfoListAsyncCallbackInfo->status = 0; + LOGE("CallAsyncWork status %d", deviceInfoListAsyncCallbackInfo->status); + }, + [](napi_env env, napi_status status, void *data) { + (void)status; + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo = (DeviceInfoListAsyncCallbackInfo *)data; + CallGetTrustedDeviceListStatus(env, status, deviceInfoListAsyncCallbackInfo); + napi_delete_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); + delete deviceInfoListAsyncCallbackInfo; + }, + (void *)deviceInfoListAsyncCallbackInfo, &deviceInfoListAsyncCallbackInfo->asyncWork); + napi_queue_async_work(env, deviceInfoListAsyncCallbackInfo->asyncWork); +} + napi_value DeviceManagerNapi::CallDeviceList(napi_env env, napi_callback_info info, - DeviceInfoAsyncCallbackInfo *deviceInfoAsyncCallbackInfo) + DeviceInfoListAsyncCallbackInfo *deviceInfoListAsyncCallbackInfo) { napi_value result = nullptr; std::string extra = ""; - deviceInfoAsyncCallbackInfo->extra = extra; + deviceInfoListAsyncCallbackInfo->extra = extra; GET_PARAMS(env, info, DM_NAPI_ARGS_ONE); napi_valuetype eventHandleType = napi_undefined; napi_typeof(env, argv[0], &eventHandleType); if (eventHandleType == napi_function) { LOGE("CallDeviceList for argc %d Type = %d", argc, (int)eventHandleType); - napi_create_reference(env, argv[0], 1, &deviceInfoAsyncCallbackInfo->callback); - CallAsyncWork(env, deviceInfoAsyncCallbackInfo); + napi_create_reference(env, argv[0], 1, &deviceInfoListAsyncCallbackInfo->callback); + CallAsyncWork(env, deviceInfoListAsyncCallbackInfo); napi_get_undefined(env, &result); return result; } else { @@ -1214,11 +1289,11 @@ napi_value DeviceManagerNapi::CallDeviceList(napi_env env, napi_callback_info in napi_deferred deferred; napi_value promise = 0; napi_create_promise(env, &deferred, &promise); - deviceInfoAsyncCallbackInfo->deferred = deferred; + deviceInfoListAsyncCallbackInfo->deferred = deferred; char extraString[20]; JsObjectToString(env, argv[0], "extra", extraString, sizeof(extraString)); - deviceInfoAsyncCallbackInfo->extra = extraString; - CallAsyncWorkSync(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->extra = extraString; + CallAsyncWorkSync(env, deviceInfoListAsyncCallbackInfo); return promise; } } @@ -1269,23 +1344,22 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i DeviceManagerNapi *deviceManagerWrapper = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&deviceManagerWrapper)); - auto *deviceInfoAsyncCallbackInfo = new DeviceInfoAsyncCallbackInfo(); - deviceInfoAsyncCallbackInfo->env = env; - deviceInfoAsyncCallbackInfo->devList = devList; - deviceInfoAsyncCallbackInfo->isList = 1; - deviceInfoAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; + auto *deviceInfoListAsyncCallbackInfo = new DeviceInfoListAsyncCallbackInfo(); + deviceInfoListAsyncCallbackInfo->env = env; + deviceInfoListAsyncCallbackInfo->devList = devList; + deviceInfoListAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; LOGE("GetTrustedDeviceList for argc %d", argc); if (argc == 0) { std::string extra = ""; - deviceInfoAsyncCallbackInfo->extra = extra; + deviceInfoListAsyncCallbackInfo->extra = extra; napi_deferred deferred; napi_value promise = 0; napi_create_promise(env, &deferred, &promise); - deviceInfoAsyncCallbackInfo->deferred = deferred; - CallAsyncWorkSync(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->deferred = deferred; + CallAsyncWorkSync(env, deviceInfoListAsyncCallbackInfo); return promise; } else if (argc == 1) { - return CallDeviceList(env, info, deviceInfoAsyncCallbackInfo); + return CallDeviceList(env, info, deviceInfoListAsyncCallbackInfo); } else if (argc == DM_NAPI_ARGS_TWO) { GET_PARAMS(env, info, DM_NAPI_ARGS_TWO); napi_valuetype valueType; @@ -1299,9 +1373,9 @@ napi_value DeviceManagerNapi::GetTrustedDeviceList(napi_env env, napi_callback_i NAPI_ASSERT(env, eventHandleType == napi_function, "Wrong argument type. Object expected."); char extra[20]; JsObjectToString(env, argv[0], "extra", extra, sizeof(extra)); - deviceInfoAsyncCallbackInfo->extra = extra; - napi_create_reference(env, argv[1], 1, &deviceInfoAsyncCallbackInfo->callback); - CallAsyncWork(env, deviceInfoAsyncCallbackInfo); + deviceInfoListAsyncCallbackInfo->extra = extra; + napi_create_reference(env, argv[1], 1, &deviceInfoListAsyncCallbackInfo->callback); + CallAsyncWork(env, deviceInfoListAsyncCallbackInfo); napi_get_undefined(env, &result); return result; } @@ -1347,7 +1421,6 @@ napi_value DeviceManagerNapi::GetLocalDeviceInfo(napi_env env, napi_callback_inf auto *deviceInfoAsyncCallbackInfo = new DeviceInfoAsyncCallbackInfo(); deviceInfoAsyncCallbackInfo->env = env; deviceInfoAsyncCallbackInfo->deviceInfo = deviceInfo; - deviceInfoAsyncCallbackInfo->isList = 0; deviceInfoAsyncCallbackInfo->bundleName = deviceManagerWrapper->bundleName_; LOGE("GetLocalDeviceInfo for argc %d", argc); if (argc == 0) { @@ -1557,7 +1630,25 @@ napi_value DeviceManagerNapi::JsOnFrench(napi_env env, int32_t num, napi_value t LOGI("JsOn for bunderName %s, eventType %s ", deviceManagerWrapper->bundleName_.c_str(), eventType.c_str()); deviceManagerWrapper->On(eventType, argv[num + 1]); - CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + if (eventType == DM_NAPI_EVENT_DEVICE_STATE_CHANGE) { + if (num == 1) { + size_t extraLen = 0; + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &extraLen); + NAPI_ASSERT(env, extraLen < DM_NAPI_BUF_LENGTH, "extraLen >= MAXLEN"); + char extra[DM_NAPI_BUF_LENGTH] = {0}; + napi_get_value_string_utf8(env, argv[1], extra, extraLen + 1, &extraLen); + std::string extraString = extra; + LOGI("extra = %s", extraString.c_str()); + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType, extraString); + } else { + // for test + std::string extraString = + "{\"FilterOptions\": {\"targetPkgName\":\"targetPkgName\",\"sortType\":0,\"filter\":\"filter\"}}"; + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType, extraString); + } + } else { + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + } napi_value result = nullptr; napi_get_undefined(env, &result); diff --git a/services/devicemanagerservice/include/config/json_config.h b/services/devicemanagerservice/include/config/json_config.h index a1c495cc0..c23894cf9 100644 --- a/services/devicemanagerservice/include/config/json_config.h +++ b/services/devicemanagerservice/include/config/json_config.h @@ -38,6 +38,14 @@ const std::string adapterJsonConfigString = "funcName": "CreateDeviceProfileObject", "soName": "libdevicemanagerext_profile.z.so", "soPath": "/system/lib/" + }, + { + "name": "device_decision", + "type": "DECISION", + "version": "1.0", + "funcName": "CreateDeviceDecisionObject", + "soName": "libdevicemanagerext_decision.z.so", + "soPath": "/system/lib/" } ] })"; @@ -76,4 +84,4 @@ const std::string authJsonConfigString = })"; } // namespace DistributedHardware } // namespace OHOS -#endif // OHOS_OHOS_DM_JSON_CONFIG_H \ No newline at end of file +#endif // OHOS_OHOS_DM_JSON_CONFIG_H diff --git a/services/devicemanagerservice/include/device_manager_service.h b/services/devicemanagerservice/include/device_manager_service.h index aedfc875d..361ccf1ff 100644 --- a/services/devicemanagerservice/include/device_manager_service.h +++ b/services/devicemanagerservice/include/device_manager_service.h @@ -49,6 +49,8 @@ public: int32_t VerifyAuthentication(const std::string &authParam); int32_t GetFaParam(std::string &pkgName, DmAuthParam &authParam); int32_t SetUserOperation(std::string &pkgName, int32_t action); + int32_t RegisterDecisionFilter(const std::string &pkgName, const std::string &extra); + int32_t UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra); private: DeviceManagerService() = default; bool intFlag_ = false; diff --git a/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h b/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h index 9dfe5b159..9a9fbfc51 100644 --- a/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h +++ b/services/devicemanagerservice/include/deviceinfo/dm_device_info_manager.h @@ -29,11 +29,12 @@ class DmDeviceInfoManager { public: DmDeviceInfoManager(std::shared_ptr &softbusConnectorPtr); int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, - std::vector &deviceList); + std::vector &deviceList); int32_t GetLocalDeviceInfo(DmDeviceInfo &info); private: std::shared_ptr softbusConnector_; + std::string decisionSoName_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index 7e7ccb82f..dd4ac7661 100644 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -31,6 +31,10 @@ public: std::shared_ptr listener, std::shared_ptr hiChainConnector); ~DmDeviceStateManager(); + int32_t RegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info); + int32_t UnRegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info); + void PostDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info); + void PostDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); @@ -40,6 +44,8 @@ public: void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); void StartOffLineTimer(const DmDeviceInfo &deviceInfo); void DeleteTimeOutGroup(std::string deviceId); + void SaveDecisionFilterExt(const std::string &pkgName, const std::string &extra); + void RemoveDecisionFilterExt(const std::string &pkgName, const std::string &extra); private: std::shared_ptr softbusConnector_; @@ -47,9 +53,11 @@ private: std::shared_ptr listener_; std::map deviceStateMap_; std::map remoteDeviceInfos_; + std::map decisionInfos_; std::map> timerMap_; std::shared_ptr hiChainConnector_; std::string profileSoName_; + std::string decisionSoName_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index a5f33b609..0009b67b1 100644 --- a/services/devicemanagerservice/src/device_manager_service.cpp +++ b/services/devicemanagerservice/src/device_manager_service.cpp @@ -299,5 +299,17 @@ int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t act authMgr_->OnUserOperation(action); return DM_OK; } + +int32_t DeviceManagerService::RegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->SaveDecisionFilterExt(pkgName, extra); + return DM_OK; +} + +int32_t DeviceManagerService::UnRegisterDecisionFilter(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->RemoveDecisionFilterExt(pkgName, extra); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/device_manager_service_listener.cpp b/services/devicemanagerservice/src/device_manager_service_listener.cpp index c67191a3e..f9b196c73 100644 --- a/services/devicemanagerservice/src/device_manager_service_listener.cpp +++ b/services/devicemanagerservice/src/device_manager_service_listener.cpp @@ -33,9 +33,14 @@ void DeviceManagerServiceListener::OnDeviceStateChange(const std::string &pkgNam std::shared_ptr pReq = std::make_shared(); std::shared_ptr pRsp = std::make_shared(); + pReq->SetPkgName(pkgName); pReq->SetDeviceState(state); pReq->SetDeviceInfo(info); - ipcServerListener_.SendAll(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + if (pkgName == DM_PKG_NAME) { + ipcServerListener_.SendAll(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } else { + ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp); + } } void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, diff --git a/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp b/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp index dcf240abf..5138eb900 100644 --- a/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp +++ b/services/devicemanagerservice/src/deviceinfo/dm_device_info_manager.cpp @@ -24,6 +24,7 @@ DmDeviceInfoManager::DmDeviceInfoManager(std::shared_ptr &soft : softbusConnector_(softbusConnectorPtr) { LOGI("DmDeviceInfoManager constructor"); + decisionSoName_ = "libdevicemanagerext_decision.z.so"; } int32_t DmDeviceInfoManager::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, @@ -36,9 +37,8 @@ int32_t DmDeviceInfoManager::GetTrustedDeviceList(const std::string &pkgName, co } if (!extra.empty() && !deviceList.empty()) { - std::string soName; DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); - std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(soName); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); if (decisionAdapter != nullptr) { decisionAdapter->FilterDeviceList(deviceList, extra); } else { diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index 98ba3c7f2..b7c775820 100644 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -40,6 +40,7 @@ DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr sof { LOGI("DmDeviceStateManager constructor"); profileSoName_ = "libdevicemanagerext_profile.z.so"; + decisionSoName_ = "libdevicemanagerext_decision.z.so"; } DmDeviceStateManager::~DmDeviceStateManager() @@ -48,10 +49,8 @@ DmDeviceStateManager::~DmDeviceStateManager() softbusConnector_->UnRegisterSoftbusStateCallback("DM_PKG_NAME"); } -void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) +int32_t DmDeviceStateManager::RegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info) { - LOGI("DmDeviceStateManager::OnDeviceOnline in"); - RegisterOffLineTimer(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); if (profileAdapter == nullptr) { @@ -73,15 +72,11 @@ void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDe LOGI("RegisterProfileListener out"); } } - DmDeviceState state = DEVICE_STATE_ONLINE; - deviceStateMap_[info.deviceId] = DEVICE_STATE_ONLINE; - listener_->OnDeviceStateChange(pkgName, state, info); - LOGI("DmDeviceStateManager::OnDeviceOnline out"); + return DM_OK; } -void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) +int32_t DmDeviceStateManager::UnRegisterProfileListener(const std::string &pkgName, const DmDeviceInfo &info) { - StartOffLineTimer(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); if (profileAdapter == nullptr) { @@ -95,9 +90,87 @@ void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmD remoteDeviceInfos_.erase(std::string(info.deviceId)); } } + return DM_OK; +} + +void DmDeviceStateManager::PostDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::PostDeviceOnline in"); + DmDeviceState state = DEVICE_STATE_ONLINE; + deviceStateMap_[info.deviceId] = DEVICE_STATE_ONLINE; + listener_->OnDeviceStateChange(pkgName, state, info); + LOGI("DmDeviceStateManager::PostDeviceOnline out"); +} + +void DmDeviceStateManager::PostDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::PostDeviceOffline in"); DmDeviceState state = DEVICE_STATE_OFFLINE; deviceStateMap_[info.deviceId] = DEVICE_STATE_OFFLINE; listener_->OnDeviceStateChange(pkgName, state, info); + UnRegisterProfileListener(pkgName, info); + LOGI("DmDeviceStateManager::PostDeviceOffline out"); +} + +void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceOnline in"); + RegisterProfileListener(pkgName, info); + + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("OnDeviceOnline decision adapter is null"); + PostDeviceOnline(pkgName, info); + } else if (decisionInfos_.size() == 0) { + PostDeviceOnline(pkgName, info); + } else { + std::string extra; + std::vector infoList; + LOGI("OnDeviceOnline decision decisionInfos_ size: %d", decisionInfos_.size()); + for (auto iter : decisionInfos_) { + std::string listenerPkgName = iter.first; + std::string extra = iter.second; + infoList.clear(); + infoList.push_back(info); + decisionAdapter->FilterDeviceList(infoList, extra); + if (infoList.size() == 1) { + PostDeviceOnline(listenerPkgName, info); + } + } + } + + LOGI("DmDeviceStateManager::OnDeviceOnline out"); +} + +void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) +{ + LOGI("DmDeviceStateManager::OnDeviceOffline in"); + UnRegisterProfileListener(pkgName, info); + + DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); + std::shared_ptr decisionAdapter = adapterMgrPtr.GetDecisionAdapter(decisionSoName_); + if (decisionAdapter == nullptr) { + LOGE("OnDeviceOnline decision adapter is null"); + PostDeviceOffline(pkgName, info); + } else if (decisionInfos_.size() == 0) { + PostDeviceOffline(pkgName, info); + } else { + std::string extra; + std::vector infoList; + LOGI("OnDeviceOnline decision decisionInfos_ size: %d", decisionInfos_.size()); + for (auto iter : decisionInfos_) { + std::string listenerPkgName = iter.first; + std::string extra = iter.second; + infoList.clear(); + infoList.push_back(info); + decisionAdapter->FilterDeviceList(infoList, extra); + if (infoList.size() == 1) { + PostDeviceOffline(listenerPkgName, info); + } + } + } + LOGI("DmDeviceStateManager::OnDeviceOffline out"); } void DmDeviceStateManager::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info) @@ -130,6 +203,24 @@ int32_t DmDeviceStateManager::RegisterSoftbusStateCallback() return DM_OK; } +void DmDeviceStateManager::SaveDecisionFilterExt(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::RegisterDecisionFilter pkgName=%s, extra=%s", pkgName.c_str(), extra.c_str()); + if (pkgName != "") { + decisionInfos_[pkgName] = extra; + } +} + +void DmDeviceStateManager::RemoveDecisionFilterExt(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::UnRegisterDecisionFilter pkgName=%s, extra=%s", pkgName.c_str(), extra.c_str()); + auto iter = decisionInfos_.find(pkgName); + if (iter == decisionInfos_.end()) { + } else { + decisionInfos_.erase(pkgName); + } +} + void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) { std::string deviceId; diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 2a61266b5..bb28af743 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp @@ -431,5 +431,29 @@ ON_IPC_CMD(SERVER_USER_AUTH_OPERATION, MessageParcel &data, MessageParcel &reply } return result; } + +ON_IPC_CMD(REGISTER_DECISION_FILTER, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().RegisterDecisionFilter(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} + +ON_IPC_CMD(UNREGISTER_DECISION_FILTER, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().UnRegisterDecisionFilter(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee