From 249188a35ef4680f3d0cccc41efa664b9d91aff8 Mon Sep 17 00:00:00 2001 From: "weidong.liu@thundersoft.com" Date: Mon, 28 Feb 2022 18:18:47 +0800 Subject: [PATCH] add NFC/TOUCH/DS --- common/include/dm_constants.h | 3 + common/include/ipc/ipc_def.h | 2 + .../ipc_register_dev_state_callback_req.h | 44 ++++ ext/pin_auth/include/pin_auth.h | 7 +- ext/pin_auth/src/pin_auth.cpp | 50 +++- ext/pin_auth/src/pin_auth_ui.cpp | 6 +- .../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 | 232 ++++++++++++------ .../include/authentication/authentication.h | 6 +- .../include/authentication/dm_auth_manager.h | 7 +- .../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 + .../authentication/auth_message_processor.cpp | 7 +- .../src/authentication/dm_auth_manager.cpp | 101 ++++---- .../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 | 111 ++++++++- .../src/discovery/dm_discovery_manager.cpp | 14 +- .../src/ipc/standard/ipc_cmd_parser.cpp | 24 ++ 26 files changed, 605 insertions(+), 184 deletions(-) create mode 100644 common/include/ipc/model/ipc_register_dev_state_callback_req.h diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 184e2e1fc..62c002170 100644 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -140,6 +140,9 @@ const std::string AUTH_TYPE = "authType"; const std::string TOKEN = "token"; const std::string PIN_TOKEN = "pinToken"; const std::string PIN_CODE_KEY = "pinCode"; +const std::string NFC_CODE_KEY = "nfcCode"; +const std::string QR_CODE_KEY = "qrCode"; +const std::string TAG_AUTH_TOKEN = "authToken"; const int32_t AUTH_TYPE_PIN = 1; const int32_t AUTH_TYPE_SCAN = 2; const int32_t AUTH_TYPE_TOUCH = 3; diff --git a/common/include/ipc/ipc_def.h b/common/include/ipc/ipc_def.h index ec3a93d3e..cc2aa67d6 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_DEV_STATE_CALLBACK, + UNREGISTER_DEV_STATE_CALLBACK, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_register_dev_state_callback_req.h b/common/include/ipc/model/ipc_register_dev_state_callback_req.h new file mode 100644 index 000000000..4ab4bc253 --- /dev/null +++ b/common/include/ipc/model/ipc_register_dev_state_callback_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_DEV_STATE_CALLBACK_REQ_H +#define OHOS_DM_IPC_REGISTER_DEV_STATE_CALLBACK_REQ_H + +#include + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRegisterDevStateCallbackReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegisterDevStateCallbackReq); + +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_DEV_STATE_CALLBACK_REQ_H diff --git a/ext/pin_auth/include/pin_auth.h b/ext/pin_auth/include/pin_auth.h index dca2e7e7f..ac02b1017 100644 --- a/ext/pin_auth/include/pin_auth.h +++ b/ext/pin_auth/include/pin_auth.h @@ -30,10 +30,9 @@ class PinAuth : public IAuthentication { public: PinAuth(); ~PinAuth(); - int32_t ShowAuthInfo(int32_t code, std::shared_ptr authManager) override; - int32_t StartAuth(int32_t code, std::shared_ptr authManager) override; - int32_t VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam) override; - + int32_t ShowAuthInfo(std::string &authToken, std::shared_ptr authManager) override; + int32_t StartAuth(std::string &authToken, std::shared_ptr authManager) override; + int32_t VerifyAuthentication(std::string &authToken, const std::string &authParam) override; private: int32_t times_ = 0; std::shared_ptr pinAuthUi_; diff --git a/ext/pin_auth/src/pin_auth.cpp b/ext/pin_auth/src/pin_auth.cpp index 0bbadd15f..15c52772c 100644 --- a/ext/pin_auth/src/pin_auth.cpp +++ b/ext/pin_auth/src/pin_auth.cpp @@ -33,31 +33,59 @@ PinAuth::~PinAuth() { } -int32_t PinAuth::ShowAuthInfo(int32_t code, std::shared_ptr authManager) +int32_t PinAuth::ShowAuthInfo(std::string &authToken, std::shared_ptr authManager) { - return pinAuthUi_->ShowPinDialog(code, authManager); + nlohmann::json jsonObject = nlohmann::json::parse(authToken, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return DM_FAILED; + } + if (!jsonObject.contains(PIN_CODE_KEY)) { + LOGE("err json string, first time"); + return DM_FAILED; + } + return pinAuthUi_->ShowPinDialog(jsonObject[PIN_CODE_KEY], authManager); } -int32_t PinAuth::StartAuth(int32_t code, std::shared_ptr authManager) +int32_t PinAuth::StartAuth(std::string &authToken, std::shared_ptr authManager) { - return pinAuthUi_->InputPinDialog(code, authManager); + nlohmann::json jsonObject = nlohmann::json::parse(authToken, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return DM_FAILED; + } + if (!jsonObject.contains(PIN_CODE_KEY)) { + LOGE("err json string, first time"); + return DM_FAILED; + } + return pinAuthUi_->InputPinDialog(jsonObject[PIN_CODE_KEY], authManager); } -int32_t PinAuth::VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam) +int32_t PinAuth::VerifyAuthentication(std::string &authToken, const std::string &authParam) { times_ += 1; - nlohmann::json jsonObject = nlohmann::json::parse(authParam, nullptr, false); - if (jsonObject.is_discarded()) { + nlohmann::json authParamJson = nlohmann::json::parse(authParam, nullptr, false); + if (authParamJson.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return DM_FAILED; + } + nlohmann::json authTokenJson = nlohmann::json::parse(authToken, nullptr, false); + if (authParamJson.is_discarded()) { LOGE("DecodeRequestAuth jsonStr error"); return DM_FAILED; } - if (!jsonObject.contains(PIN_CODE_KEY) && !jsonObject.contains(PIN_TOKEN)) { + if (!authParamJson.contains(PIN_CODE_KEY) && !authParamJson.contains(PIN_TOKEN)) { + if (authParam == "0") { + return DM_OK; + } LOGE("err json string, first time"); return DM_FAILED; } - int32_t inputPinCode = jsonObject[PIN_CODE_KEY]; - int32_t inputPinToken = jsonObject[PIN_TOKEN]; - if (code == inputPinCode && stoi(pinToken) == inputPinToken) { + int32_t code = authTokenJson[PIN_CODE_KEY]; + int32_t pinToken = authTokenJson[PIN_TOKEN]; + int32_t inputPinCode = authParamJson[PIN_CODE_KEY]; + int32_t inputPinToken = authParamJson[PIN_TOKEN]; + if (code == inputPinCode && pinToken == inputPinToken) { return DM_OK; } else if (code != inputPinCode && times_ < MAX_VERIFY_TIMES) { return DM_AUTH_INPUT_FAILED; diff --git a/ext/pin_auth/src/pin_auth_ui.cpp b/ext/pin_auth/src/pin_auth_ui.cpp index 9fc3459cd..eb976a678 100644 --- a/ext/pin_auth/src/pin_auth_ui.cpp +++ b/ext/pin_auth/src/pin_auth_ui.cpp @@ -43,7 +43,7 @@ int32_t PinAuthUi::ShowPinDialog(int32_t code, std::shared_ptr au ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authManager](int32_t id, const std::string& event, const std::string& params) { if (strcmp(params.c_str(), "0") == 0) { - authManager->ClosePage(id); + authManager->SetPageId(id); } if (strcmp(params.c_str(), "1") == 0) { LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); @@ -69,12 +69,12 @@ int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr a ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authManager](int32_t id, const std::string& event, const std::string& params) { if (strcmp(params.c_str(), "2") == 0) { - authManager->ClosePage(id); + authManager->SetPageId(id); } if (strcmp(params.c_str(), "0") == 0 || strcmp(params.c_str(), "1") == 0) { Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); - authManager->VerifyPinAuthAuthentication(params.c_str()); + authManager->VerifyAuthentication(params.c_str()); } }); LOGI("ShowConfigDialog end"); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 9e76a1361..4b814a7a7 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 RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) = 0; + virtual int32_t UnRegisterDevStateCallback(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..805d6640f 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 RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) override; + virtual int32_t UnRegisterDevStateCallback(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..e861dbca4 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_dev_state_callback_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.empty()) { + RegisterDevStateCallback(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 = ""; + UnRegisterDevStateCallback(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::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("RegisterDevStateCallback 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_DEV_STATE_CALLBACK, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("RegisterDevStateCallback Failed with ret %d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + if (pkgName.empty()) { + LOGE("UnRegisterDevStateCallback 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_DEV_STATE_CALLBACK, req, rsp) != DM_OK) { + return DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnRegisterDevStateCallback 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..b2c4ccefc 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_dev_state_callback_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_DEV_STATE_CALLBACK, 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_DEV_STATE_CALLBACK, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_DEV_STATE_CALLBACK, 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_DEV_STATE_CALLBACK, 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..4190e4378 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,89 @@ 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); + 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 +1285,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 +1340,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 +1369,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 +1417,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) { @@ -1491,8 +1560,8 @@ napi_value DeviceManagerNapi::AuthenticateDevice(napi_env env, napi_callback_inf JsToDmDeviceInfo(env, argv[0], deviceInfo); std::string extraString; JsToDmExtra(env, argv[PARAM_INDEX_ONE], extraString, authAsyncCallbackInfo_.authType); - int32_t ret = DeviceManager::GetInstance().AuthenticateDevice(deviceManagerWrapper->bundleName_, 1, deviceInfo, - extraString, authCallback); + int32_t ret = DeviceManager::GetInstance().AuthenticateDevice(deviceManagerWrapper->bundleName_, + authAsyncCallbackInfo_.authType, deviceInfo, extraString, authCallback); if (ret != 0) { LOGE("AuthenticateDevice for bundleName %s failed, ret %d", deviceManagerWrapper->bundleName_.c_str(), ret); } @@ -1557,7 +1626,22 @@ 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 { + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + } + } else { + CreateDmCallback(env, deviceManagerWrapper->bundleName_, eventType); + } napi_value result = nullptr; napi_get_undefined(env, &result); @@ -1570,7 +1654,7 @@ napi_value DeviceManagerNapi::JsOn(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr)); if (argc == DM_NAPI_ARGS_THREE) { - LOGI("JsOff in argc == 3"); + LOGI("JsOn in argc == 3"); GET_PARAMS(env, info, DM_NAPI_ARGS_THREE); NAPI_ASSERT(env, argc >= DM_NAPI_ARGS_THREE, "Wrong number of arguments, required 2"); @@ -1580,7 +1664,7 @@ napi_value DeviceManagerNapi::JsOn(napi_env env, napi_callback_info info) napi_valuetype valueType; napi_typeof(env, argv[1], &valueType); - NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 2"); + NAPI_ASSERT(env, (valueType == napi_string || valueType == napi_object), "type mismatch for parameter 2"); napi_valuetype eventHandleType = napi_undefined; napi_typeof(env, argv[DM_NAPI_ARGS_TWO], &eventHandleType); @@ -1643,7 +1727,7 @@ napi_value DeviceManagerNapi::JsOff(napi_env env, napi_callback_info info) napi_valuetype valueType; napi_typeof(env, argv[1], &valueType); - NAPI_ASSERT(env, valueType == napi_object, "type mismatch for parameter 2"); + NAPI_ASSERT(env, (valueType == napi_string || valueType == napi_object), "type mismatch for parameter 2"); if (argc > requireArgc) { napi_valuetype eventHandleType = napi_undefined; diff --git a/services/devicemanagerservice/include/authentication/authentication.h b/services/devicemanagerservice/include/authentication/authentication.h index 67ca0c3ee..7c59f31c4 100644 --- a/services/devicemanagerservice/include/authentication/authentication.h +++ b/services/devicemanagerservice/include/authentication/authentication.h @@ -24,9 +24,9 @@ class DmAuthManager; class IAuthentication { public: virtual ~IAuthentication() = default; - virtual int32_t ShowAuthInfo(int32_t code, std::shared_ptr authManager) = 0; - virtual int32_t StartAuth(int32_t code, std::shared_ptr authManager) = 0; - virtual int32_t VerifyAuthentication(std::string pinToken, int32_t code, const std::string &authParam) = 0; + virtual int32_t ShowAuthInfo(std::string &authToken, std::shared_ptr authManager) = 0; + virtual int32_t StartAuth(std::string &authToken, std::shared_ptr authManager) = 0; + virtual int32_t VerifyAuthentication(std::string &authToken, const std::string &authParam) = 0; }; using CreateIAuthAdapterFuncPtr = IAuthentication *(*)(void); diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index 6a7ae6b85..204ef0fe7 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -86,7 +86,6 @@ typedef struct DmAuthRequestContext { std::string appThumbnail; std::string token; int32_t reason; - int32_t aceId; std::vector syncGroupList; } DmAuthRequestContext; @@ -110,10 +109,11 @@ typedef struct DmAuthResponseContext { std::string appIcon; std::string appThumbnail; std::string token; + std::string authToken; + int32_t pageId; int64_t requestId; int32_t code; int32_t state; - int32_t aceId; std::vector syncGroupList; } DmAuthResponseContext; @@ -131,7 +131,6 @@ public: const std::string &extra); int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId); int32_t VerifyAuthentication(const std::string &authParam); - void VerifyPinAuthAuthentication(const std::string &action); void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); void OnSessionClosed(int32_t sessionId); void OnDataReceived(int32_t sessionId, std::string message); @@ -164,7 +163,7 @@ public: int32_t GetAuthenticationParam(DmAuthParam &authParam); int32_t OnUserOperation(int32_t action); void UserSwitchEventCallback(int32_t userId); - void ClosePage(const int32_t &id); + int32_t SetPageId(int32_t pageId); private: std::shared_ptr softbusConnector_; 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..d5e636928 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 RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); + int32_t UnRegisterDevStateCallback(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..b551e1e9d 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 RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); + void UnRegisterDevStateCallback(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/authentication/auth_message_processor.cpp b/services/devicemanagerservice/src/authentication/auth_message_processor.cpp index 9ba556f4a..6bbf020e9 100644 --- a/services/devicemanagerservice/src/authentication/auth_message_processor.cpp +++ b/services/devicemanagerservice/src/authentication/auth_message_processor.cpp @@ -128,9 +128,6 @@ void AuthMessageProcessor::CreateResponseAuthMessage(nlohmann::json &json) json[TAG_REPLY] = authResponseContext_->reply; json[TAG_DEVICE_ID] = authResponseContext_->deviceId; json[TAG_TOKEN] = authResponseContext_->token; - // json[TAG_GROUPIDS] = authResponseContext_->deviceId; //? - LOGI("AuthMessageProcessor::ParseAuthResponseMessage %d,%d", authResponseContext_->reply, - authResponseContext_->code); LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s", authResponseContext_->deviceId.c_str()); if (authResponseContext_->reply == 0) { std::string groupId = authResponseContext_->groupId; @@ -141,11 +138,11 @@ void AuthMessageProcessor::CreateResponseAuthMessage(nlohmann::json &json) return; } groupId = jsonObject[TAG_GROUP_ID]; - json[PIN_CODE_KEY] = authResponseContext_->code; json[TAG_NET_ID] = authResponseContext_->networkId; json[TAG_REQUEST_ID] = authResponseContext_->requestId; json[TAG_GROUP_ID] = groupId; json[TAG_GROUP_NAME] = authResponseContext_->groupName; + json[TAG_AUTH_TOKEN] = authResponseContext_->authToken; LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s,%s", groupId.c_str(), authResponseContext_->groupName.c_str()); } @@ -220,11 +217,11 @@ void AuthMessageProcessor::ParseAuthResponseMessage(nlohmann::json &json) authResponseContext_->deviceId = json[TAG_DEVICE_ID]; authResponseContext_->token = json[TAG_TOKEN]; if (authResponseContext_->reply == 0) { - authResponseContext_->code = json[PIN_CODE_KEY]; authResponseContext_->networkId = json[TAG_NET_ID]; authResponseContext_->requestId = json[TAG_REQUEST_ID]; authResponseContext_->groupId = json[TAG_GROUP_ID]; authResponseContext_->groupName = json[TAG_GROUP_NAME]; + authResponseContext_->authToken = json[TAG_AUTH_TOKEN]; LOGI("AuthMessageProcessor::ParseAuthResponseMessage %s,%s", authResponseContext_->groupId.c_str(), authResponseContext_->groupName.c_str()); } diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index e759e8c4b..ed9c60b13 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -80,7 +80,7 @@ DmAuthManager::~DmAuthManager() int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { - LOGE("DmAuthManager::AuthenticateDevice start"); + LOGE("DmAuthManager::AuthenticateDevice start auth type %d", authType); std::shared_ptr authentication = authenticationMap_[authType]; if (authentication == nullptr) { LOGE("DmAuthManager::AuthenticateDevice authType %d not support.", authType); @@ -176,14 +176,14 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) { LOGI("DmAuthManager::VerifyAuthentication"); std::shared_ptr ptr; - if (authenticationMap_.find(1) == authenticationMap_.end() && timerMap_[INPUT_TIMEOUT_TASK] == nullptr) { + if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end() + && timerMap_[INPUT_TIMEOUT_TASK] == nullptr) { LOGE("DmAuthManager::authenticationMap_ is null"); return DM_FAILED; } timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); - - ptr = authenticationMap_[1]; - int32_t ret = ptr->VerifyAuthentication(authRequestContext_->token, authResponseContext_->code, authParam); + ptr = authenticationMap_[authResponseContext_->authType]; + int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam); switch (ret) { case DM_OK: authRequestState_->TransitionTo(std::make_shared()); @@ -193,7 +193,6 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) DM_AUTH_INPUT_FAILED, ""); break; default: - CancelDisplay(); authRequestContext_->reason = DM_AUTH_INPUT_FAILED; authResponseContext_->state = authRequestState_->GetStateType(); authRequestState_->TransitionTo(std::make_shared()); @@ -269,8 +268,7 @@ void DmAuthManager::OnDataReceived(int32_t sessionId, std::string message) } switch (authResponseContext_->msgType) { case MSG_TYPE_NEGOTIATE: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT - && timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] != nullptr) { + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT) { timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); authResponseState_->TransitionTo(std::make_shared()); } else { @@ -278,8 +276,7 @@ void DmAuthManager::OnDataReceived(int32_t sessionId, std::string message) } break; case MSG_TYPE_REQ_AUTH: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE - && timerMap_[WAIT_REQUEST_TIMEOUT_TASK] != nullptr) { + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE) { timerMap_[WAIT_REQUEST_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); authResponseState_->TransitionTo(std::make_shared()); } else { @@ -306,6 +303,7 @@ void DmAuthManager::OnDataReceived(int32_t sessionId, std::string message) authResponseState_->TransitionTo(std::make_shared()); } else if (authRequestState_ != nullptr && authRequestState_->GetStateType() != AuthState::AUTH_REQUEST_FINISH) { + authResponseContext_->state = authRequestState_->GetStateType(); authRequestState_->TransitionTo(std::make_shared()); } break; @@ -328,7 +326,13 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); return; } - authResponseContext_->code = GeneratePincode(); + nlohmann::json jsonObj; + jsonObj[PIN_CODE_KEY] = GeneratePincode(); + jsonObj[PIN_TOKEN] = authResponseContext_->token; + jsonObj[QR_CODE_KEY] = GenerateGroupName(); + jsonObj[NFC_CODE_KEY] = GenerateGroupName(); + authResponseContext_->authToken = jsonObj.dump(); + LOGI("DmAuthManager::AddMember start %s", authResponseContext_->authToken.c_str()); authResponseContext_->groupId = groupId; authMessageProcessor_->SetResponseContext(authResponseContext_); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_AUTH); @@ -339,7 +343,7 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) { LOGE("DmAuthManager OnMemberJoin start"); - if (authRequestState_ != nullptr && timerMap_[ADD_TIMEOUT_TASK] != nullptr) { + if (authRequestState_ != nullptr) { timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); if (status != DM_OK || authResponseContext_->requestId != requestId) { if (authRequestState_ != nullptr) { @@ -446,9 +450,6 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) void DmAuthManager::SendAuthRequest(const int32_t &sessionId) { LOGE("DmAuthManager::EstablishAuthChannel session id"); - if (timerMap_[NEGOTIATE_TIMEOUT_TASK] == nullptr) { - return; - } timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); if (authResponseContext_->cryptoSupport == true) { isCryptoSupport_ = true; @@ -484,9 +485,6 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action) void DmAuthManager::StartRespAuthProcess() { LOGI("DmAuthManager::StartRespAuthProcess StartRespAuthProcess", authResponseContext_->sessionId); - if (timerMap_[CONFIRM_TIMEOUT_TASK] == nullptr) { - return; - } timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) { std::shared_ptr inputStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); @@ -513,10 +511,16 @@ int32_t DmAuthManager::CreateGroup() int32_t DmAuthManager::AddMember(const std::string &deviceId) { LOGI("DmAuthManager::AddMember start"); + nlohmann::json jsonObj = nlohmann::json::parse(authResponseContext_->authToken, nullptr, false); + if (jsonObj.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return DM_FAILED; + } + LOGI("DmAuthManager::AddMember start %s", authResponseContext_->authToken.c_str()); nlohmann::json jsonObject; jsonObject[TAG_GROUP_ID] = authResponseContext_->groupId; jsonObject[TAG_GROUP_NAME] = authResponseContext_->groupName; - jsonObject[PIN_CODE_KEY] = authResponseContext_->code; + jsonObject[PIN_CODE_KEY] = jsonObj[PIN_CODE_KEY]; jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId; jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId; std::string connectInfo = jsonObject.dump(); @@ -528,7 +532,7 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId) return DM_FAILED; } LOGI("DmAuthManager::authRequestContext CancelDisplay start"); - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->aceId); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->pageId); return DM_OK; } @@ -543,9 +547,6 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId) int32_t DmAuthManager::JoinNetwork() { LOGE("DmAuthManager JoinNetwork start"); - if (timerMap_[AUTHENTICATE_TIMEOUT_TASK] == nullptr) { - return DM_FAILED; - } timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; @@ -558,7 +559,13 @@ void DmAuthManager::AuthenticateFinish() LOGI("DmAuthManager::AuthenticateFinish start"); if (authResponseState_ != nullptr) { if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_FINISH) { - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->aceId); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->pageId); + } + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_SHOW + && authResponseContext_->authType != 1) { + authMessageProcessor_->SetResponseContext(authResponseContext_); + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_AUTH_TERMINATE); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); } if (!timerMap_.empty()) { for (auto &iter : timerMap_) { @@ -580,7 +587,7 @@ void DmAuthManager::AuthenticateFinish() } if (authResponseContext_->state == AuthState::AUTH_REQUEST_INPUT) { - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->aceId); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(authResponseContext_->pageId); } listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId, @@ -661,7 +668,12 @@ int32_t DmAuthManager::SetAuthResponseState(std::shared_ptr a int32_t DmAuthManager::GetPinCode() { - return authResponseContext_->code; + nlohmann::json jsonObj = nlohmann::json::parse(authResponseContext_->authToken, nullptr, false); + if (jsonObj.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return DM_FAILED; + } + return jsonObj[PIN_CODE_KEY]; } void DmAuthManager::ShowConfigDialog() @@ -692,25 +704,25 @@ void DmAuthManager::ShowAuthInfoDialog() { LOGI("DmAuthManager::ShowAuthInfoDialog start"); std::shared_ptr ptr; - if (authenticationMap_.find(1) == authenticationMap_.end()) { + if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { LOGE("DmAuthManager::authenticationMap_ is null"); return; } - ptr = authenticationMap_[1]; - LOGI("ShowAuthInfoDialog code:%d", authResponseContext_->code); - ptr->ShowAuthInfo(authResponseContext_->code, shared_from_this()); + ptr = authenticationMap_[authResponseContext_->authType]; + LOGI("ShowAuthInfoDialog authToken:%s", authResponseContext_->authToken.c_str()); + ptr->ShowAuthInfo(authResponseContext_->authToken, shared_from_this()); } void DmAuthManager::ShowStartAuthDialog() { LOGI("DmAuthManager::ShowStartAuthDialog start"); std::shared_ptr ptr; - if (authenticationMap_.find(1) == authenticationMap_.end()) { + if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { LOGE("DmAuthManager::authenticationMap_ is null"); return; } - ptr = authenticationMap_[1]; - ptr->StartAuth(authResponseContext_->code, shared_from_this()); + ptr = authenticationMap_[authResponseContext_->authType]; + ptr->StartAuth(authResponseContext_->authToken, shared_from_this()); } int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) @@ -795,27 +807,10 @@ void DmAuthManager::UserSwitchEventCallback (int32_t userId) } } -void DmAuthManager::VerifyPinAuthAuthentication(const std::string &action) -{ - LOGI("DmAuthManager::VerifyPinAuthAuthentication"); - if (timerMap_[INPUT_TIMEOUT_TASK] == nullptr) { - return; - } - timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); - if (action == "0") { - authRequestState_->TransitionTo(std::make_shared()); - } - if (action == "1") { - authRequestContext_->reason = DM_AUTH_INPUT_FAILED; - authResponseContext_->state = authRequestState_->GetStateType(); - authRequestState_->TransitionTo(std::make_shared()); - } - LOGI("DmAuthManager::VerifyAuthentication complete"); -} - -void DmAuthManager::ClosePage(const int32_t &id) +int32_t DmAuthManager::SetPageId(int32_t pageId) { - authResponseContext_->aceId = id; + authResponseContext_->pageId = pageId; + return DM_OK; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index a5f33b609..cda681629 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::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->RegisterDevStateCallback(pkgName, extra); + return DM_OK; +} + +int32_t DeviceManagerService::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + deviceStateMgr_->UnRegisterDevStateCallback(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..fd363ae9b 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,31 +72,103 @@ 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) { LOGE("OnDeviceOffline profile adapter is null"); } else { - std::string uuid; - SoftbusConnector::GetUuidByNetworkId(info.deviceId, uuid); auto iter = remoteDeviceInfos_.find(std::string(info.deviceId)); if (iter == remoteDeviceInfos_.end()) { } else { 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 +201,24 @@ int32_t DmDeviceStateManager::RegisterSoftbusStateCallback() return DM_OK; } +void DmDeviceStateManager::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::RegisterDevStateCallback pkgName=%s, extra=%s", pkgName.c_str(), extra.c_str()); + if (pkgName != "") { + decisionInfos_[pkgName] = extra; + } +} + +void DmDeviceStateManager::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) +{ + LOGI("DmDeviceStateManager::UnRegisterDevStateCallback 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/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index f48689431..4a489935a 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -73,14 +73,14 @@ int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, con int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId) { - if (discoveryQueue_.empty()) { - LOGE("discovery is not start"); - return DM_FAILED; + if (!discoveryQueue_.empty()) { + discoveryQueue_.pop(); + } + if (!discoveryContextMap_.empty()) { + discoveryContextMap_.erase(pkgName); + softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName); + discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT); } - discoveryQueue_.pop(); - discoveryContextMap_.erase(pkgName); - softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName); - discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT); return softbusConnector_->StopDiscovery(subscribeId); } diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_cmd_parser.cpp index 2a61266b5..1fa1c23e0 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_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().RegisterDevStateCallback(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} + +ON_IPC_CMD(UNREGISTER_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + std::string extra = data.ReadString(); + int result = DeviceManagerService::GetInstance().UnRegisterDevStateCallback(packageName, extra); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return DM_WRITE_FAILED; + } + return result; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee