mirror of
https://gitee.com/openharmony/distributedhardware_device_manager
synced 2024-11-23 08:19:49 +00:00
增加DM查询和监听设备功能
Signed-off-by: 李巍 <liwei787@huawei.com>
This commit is contained in:
parent
0f3ebd2189
commit
ead1afa184
@ -36,6 +36,7 @@
|
||||
"cJSON",
|
||||
"c_utils",
|
||||
"common_event_service",
|
||||
"data_share",
|
||||
"device_auth",
|
||||
"device_info_manager",
|
||||
"dsoftbus",
|
||||
|
@ -26,6 +26,7 @@
|
||||
"ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
|
||||
"ohos.permission.ENABLE_DISTRIBUTED_HARDWARE",
|
||||
"ohos.permission.MANAGE_LOCAL_ACCOUNTS",
|
||||
"ohos.permission.MANAGE_SECURE_SETTINGS",
|
||||
"ohos.permission.ACCESS_BLUETOOTH",
|
||||
"ohos.permission.MANAGE_BLUETOOTH",
|
||||
"ohos.permission.MANAGE_SETTINGS",
|
||||
|
@ -27,6 +27,7 @@ namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
std::shared_ptr<ISoftbusSessionCallback> SoftbusSession::sessionCallback_ = nullptr;
|
||||
constexpr const char* DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION";
|
||||
constexpr int32_t DATA_LEN = 65535;
|
||||
|
||||
static void OnShutdown(int32_t socket, ShutdownReason reason)
|
||||
{
|
||||
@ -179,7 +180,7 @@ void SoftbusSession::OnSessionClosed(int sessionId)
|
||||
|
||||
void SoftbusSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
|
||||
{
|
||||
if (sessionId < 0 || data == nullptr || dataLen <= 0) {
|
||||
if (sessionId < 0 || data == nullptr || dataLen <= 0 || dataLen > DATA_LEN) {
|
||||
LOGI("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId,
|
||||
dataLen);
|
||||
return;
|
||||
|
@ -119,6 +119,7 @@ if (defined(ohos_lite)) {
|
||||
include_dirs = [
|
||||
"include",
|
||||
"include/advertise",
|
||||
"include/devicenamemgr",
|
||||
"include/discovery",
|
||||
"include/ipc",
|
||||
"include/ipc/standard",
|
||||
@ -163,6 +164,9 @@ if (defined(ohos_lite)) {
|
||||
"src/advertise/advertise_manager.cpp",
|
||||
"src/device_manager_service.cpp",
|
||||
"src/device_manager_service_listener.cpp",
|
||||
"src/devicenamemgr/account_boot_listener.cpp",
|
||||
"src/devicenamemgr/local_device_name_mgr.cpp",
|
||||
"src/devicenamemgr/settings_data_event_monitor.cpp",
|
||||
"src/discovery/discovery_filter.cpp",
|
||||
"src/discovery/discovery_manager.cpp",
|
||||
"src/ipc/standard/ipc_cmd_parser.cpp",
|
||||
@ -204,6 +208,8 @@ if (defined(ohos_lite)) {
|
||||
"c_utils:utils",
|
||||
"common_event_service:cesfwk_core",
|
||||
"common_event_service:cesfwk_innerkits",
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
"dsoftbus:softbus_client",
|
||||
@ -217,6 +223,7 @@ if (defined(ohos_lite)) {
|
||||
"ipc:ipc_single",
|
||||
"openssl:libcrypto_shared",
|
||||
"os_account:libaccountkits",
|
||||
"os_account:os_account_innerkits",
|
||||
"safwk:system_ability_fwk",
|
||||
"samgr:samgr_proxy",
|
||||
]
|
||||
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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_ACCOUNT_BOOT_LISTENER_H
|
||||
#define OHOS_ACCOUNT_BOOT_LISTENER_H
|
||||
#include <atomic>
|
||||
|
||||
#include "local_device_name_mgr.h"
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class AccountBootListener {
|
||||
public:
|
||||
AccountBootListener();
|
||||
~AccountBootListener();
|
||||
void RegisterAccountBootCb();
|
||||
void DoAccountBootProc();
|
||||
private:
|
||||
/**
|
||||
* @brief flag for is registered callback for account boot event
|
||||
* true: has register the callback
|
||||
* false: NOT register the callback
|
||||
*/
|
||||
std::atomic<bool> isRegAccountBootCb_;
|
||||
std::shared_ptr<LocalDeviceNameMgr> localDeviceMgr_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif // OHOS_ACCOUNT_BOOT_LISTENER_H
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_DEVICE_NAME_MGR_H
|
||||
#define OHOS_DEVICE_NAME_MGR_H
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "datashare_helper.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class LocalDeviceNameMgr : public std::enable_shared_from_this<LocalDeviceNameMgr> {
|
||||
public:
|
||||
LocalDeviceNameMgr();
|
||||
virtual ~LocalDeviceNameMgr();
|
||||
int32_t QueryLocalDeviceName();
|
||||
void RegisterDeviceNameChangeCb();
|
||||
int32_t QueryLocalDisplayName();
|
||||
void RegisterDisplayNameChangeCb();
|
||||
|
||||
private:
|
||||
std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper();
|
||||
int32_t GetDeviceNameFromDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::shared_ptr<Uri> uri, const char *key, std::string &deviceName);
|
||||
int32_t GetDefaultDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName);
|
||||
int32_t GetUserDefinedDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName);
|
||||
int32_t GetDisplayDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName);
|
||||
int32_t GetActiveOsAccountIds();
|
||||
private:
|
||||
std::mutex devNameMtx_;
|
||||
std::string localDeviceName_;
|
||||
std::string localDisplayName_;
|
||||
};
|
||||
} // DistributedHardware
|
||||
} // OHOS
|
||||
|
||||
#endif // OHOS_DEVICE_NAME_MGR_H
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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_SETTINGS_DATA_EVENT_MONITOR_H
|
||||
#define OHOS_SETTINGS_DATA_EVENT_MONITOR_H
|
||||
|
||||
#include "data_ability_observer_stub.h"
|
||||
#include <memory>
|
||||
|
||||
#include "local_device_name_mgr.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class LocalDeviceNameMgr;
|
||||
enum class SettingsDataMonitorType : int32_t {
|
||||
USER_DEFINED_DEVICE_NAME_MONITOR = 0,
|
||||
DISPLAY_DEVICE_NAME_MONITOR = 1
|
||||
};
|
||||
|
||||
class SettingsDataEventMonitor : public AAFwk::DataAbilityObserverStub {
|
||||
public:
|
||||
SettingsDataEventMonitor(std::shared_ptr<LocalDeviceNameMgr> localDeviceNameMgr,
|
||||
SettingsDataMonitorType monitorType);
|
||||
void OnChange() override;
|
||||
private:
|
||||
std::weak_ptr<LocalDeviceNameMgr> localDeviceNameMgrWPtr_;
|
||||
SettingsDataMonitorType monitorType_;
|
||||
};
|
||||
} // DistributedHardware
|
||||
} // OHOS
|
||||
#endif // OHOS_SETTINGS_DATA_EVENT_MONITOR_H
|
@ -27,9 +27,11 @@
|
||||
#include "ipc_req.h"
|
||||
#include "ipc_rsp.h"
|
||||
#include "iremote_stub.h"
|
||||
#include "dm_single_instance.h"
|
||||
#include "system_ability.h"
|
||||
|
||||
#include "account_boot_listener.h"
|
||||
#include "dm_single_instance.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
|
||||
@ -160,6 +162,7 @@ private:
|
||||
mutable std::mutex listenerLock_;
|
||||
std::map<std::string, sptr<AppDeathRecipient>> appRecipient_;
|
||||
std::map<std::string, sptr<IpcRemoteBroker>> dmListener_;
|
||||
std::shared_ptr<AccountBootListener> accountBootListener_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
98
services/service/src/devicenamemgr/account_boot_listener.cpp
Normal file
98
services/service/src/devicenamemgr/account_boot_listener.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "account_boot_listener.h"
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
#include "parameter.h"
|
||||
|
||||
#include "dm_log.h"
|
||||
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
|
||||
#include "ffrt.h"
|
||||
#endif
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
namespace {
|
||||
const char * const BOOTEVENT_ACCOUNT_READY = "bootevent.account.ready";
|
||||
const char * const ACCOUNT_BOOT_EVENT = "account_boot_event";
|
||||
}
|
||||
|
||||
static void AccountBootCb(const char *key, const char *value, void *context)
|
||||
{
|
||||
if (strcmp(key, BOOTEVENT_ACCOUNT_READY) != 0 || strcmp(value, "true") != 0) {
|
||||
return;
|
||||
}
|
||||
AccountBootListener *accountBootListener = static_cast<AccountBootListener *>(context);
|
||||
|
||||
if (accountBootListener == nullptr) {
|
||||
LOGE("accountBootListener is null");
|
||||
return;
|
||||
}
|
||||
LOGI("Trigger");
|
||||
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
|
||||
ffrt::submit([=]() { accountBootListener->DoAccountBootProc(); });
|
||||
#else
|
||||
std::thread dealThread([=]() {
|
||||
accountBootListener->DoAccountBootProc();
|
||||
});
|
||||
int32_t ret = pthread_setname_np(dealThread.native_handle(), ACCOUNT_BOOT_EVENT);
|
||||
if (ret != DM_OK) {
|
||||
LOGE("dealThread setname failed.");
|
||||
}
|
||||
dealThread.detach();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
AccountBootListener::AccountBootListener() : isRegAccountBootCb_(false),
|
||||
localDeviceMgr_(std::make_shared<LocalDeviceNameMgr>())
|
||||
{
|
||||
LOGI("Ctor AccountBootListener");
|
||||
}
|
||||
|
||||
AccountBootListener::~AccountBootListener()
|
||||
{
|
||||
LOGI("Dtor AccountBootListener");
|
||||
}
|
||||
|
||||
void AccountBootListener::RegisterAccountBootCb()
|
||||
{
|
||||
if (isRegAccountBootCb_) {
|
||||
return;
|
||||
}
|
||||
LOGI("start");
|
||||
int32_t ret = WatchParameter(BOOTEVENT_ACCOUNT_READY, AccountBootCb, this);
|
||||
if (ret != 0) {
|
||||
LOGE("watch account boot event fail");
|
||||
}
|
||||
isRegAccountBootCb_ = true;
|
||||
}
|
||||
|
||||
void AccountBootListener::DoAccountBootProc()
|
||||
{
|
||||
LOGI("start");
|
||||
if (localDeviceMgr_ == nullptr) {
|
||||
LOGE("localDeviceMgr_ is null");
|
||||
return;
|
||||
}
|
||||
localDeviceMgr_->RegisterDeviceNameChangeCb();
|
||||
localDeviceMgr_->RegisterDisplayNameChangeCb();
|
||||
localDeviceMgr_->QueryLocalDeviceName();
|
||||
localDeviceMgr_->QueryLocalDisplayName();
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
259
services/service/src/devicenamemgr/local_device_name_mgr.cpp
Normal file
259
services/service/src/devicenamemgr/local_device_name_mgr.cpp
Normal file
@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "local_device_name_mgr.h"
|
||||
|
||||
#include "data_ability_observer_stub.h"
|
||||
#include "datashare_helper.h"
|
||||
#include "datashare_predicates.h"
|
||||
#include "datashare_result_set.h"
|
||||
#include "ohos_account_kits.h"
|
||||
#include "os_account_manager.h"
|
||||
#include "iservice_registry.h"
|
||||
#include "system_ability_definition.h"
|
||||
#include "uri.h"
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "settings_data_event_monitor.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
namespace {
|
||||
const std::string SETTINGS_DATA_BASE_URI =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
|
||||
const std::string SETTINGS_DATA_SECURE_URI =
|
||||
"datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
|
||||
constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
|
||||
constexpr const char *SETTINGS_DATA_FIELD_KEYWORD = "KEYWORD";
|
||||
constexpr const char *SETTINGS_DATA_FIELD_VALUE = "VALUE";
|
||||
constexpr const char *PREDICATES_STRING = "settings.general.device_name";
|
||||
constexpr const char *USER_DEFINED_STRING = "settings.general.user_defined_device_name";
|
||||
constexpr const char *DISPLAY_DEVICE_NAME_STRING = "settings.general.display_device_name";
|
||||
}
|
||||
LocalDeviceNameMgr::LocalDeviceNameMgr() : localDeviceName_(""), localDisplayName_("")
|
||||
{
|
||||
LOGI("Ctor LocalDeviceNameMgr");
|
||||
}
|
||||
|
||||
LocalDeviceNameMgr::~LocalDeviceNameMgr()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(devNameMtx_);
|
||||
localDeviceName_ = "";
|
||||
localDisplayName_ = "";
|
||||
LOGI("Dtor LocalDeviceNameMgr");
|
||||
}
|
||||
|
||||
std::shared_ptr<DataShare::DataShareHelper> LocalDeviceNameMgr::GetDataShareHelper()
|
||||
{
|
||||
sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
|
||||
if (saManager == nullptr) {
|
||||
LOGE("saManager NULL");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
|
||||
if (remoteObject == nullptr) {
|
||||
LOGE("remoteObject NULL");
|
||||
return nullptr;
|
||||
}
|
||||
return DataShare::DataShareHelper::Creator(remoteObject, SETTINGS_DATA_BASE_URI, SETTINGS_DATA_EXT_URI);
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::GetDeviceNameFromDataShareHelper(
|
||||
std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, std::shared_ptr<Uri> uri,
|
||||
const char *key, std::string &deviceName)
|
||||
{
|
||||
int32_t numRows = 0;
|
||||
std::string val;
|
||||
std::vector<std::string> columns;
|
||||
columns.emplace_back(SETTINGS_DATA_FIELD_VALUE);
|
||||
DataShare::DataSharePredicates predicates;
|
||||
predicates.EqualTo(SETTINGS_DATA_FIELD_KEYWORD, key);
|
||||
|
||||
auto resultSet = dataShareHelper->Query(*uri, predicates, columns);
|
||||
if (resultSet == nullptr) {
|
||||
LOGE("query fail.");
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
resultSet->GetRowCount(numRows);
|
||||
if (numRows <= 0) {
|
||||
LOGE("row zero.");
|
||||
resultSet->Close();
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
|
||||
int columnIndex;
|
||||
resultSet->GoToFirstRow();
|
||||
resultSet->GetColumnIndex(SETTINGS_DATA_FIELD_VALUE, columnIndex);
|
||||
if (resultSet->GetString(columnIndex, val) != DM_OK) {
|
||||
LOGE("GetString val fail");
|
||||
resultSet->Close();
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
deviceName = val;
|
||||
LOGI("deviceName=%{public}s.", deviceName.c_str());
|
||||
resultSet->Close();
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::GetDefaultDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName)
|
||||
{
|
||||
std::shared_ptr<Uri> uri = std::make_shared<Uri>(SETTINGS_DATA_BASE_URI + "&key=" + PREDICATES_STRING);
|
||||
LOGI("get default deviceName");
|
||||
return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, PREDICATES_STRING, deviceName);
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::GetUserDefinedDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName)
|
||||
{
|
||||
int32_t osAccountId = GetActiveOsAccountIds();
|
||||
if (osAccountId == ERR_DM_FAILED) {
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
std::string accountIdStr = std::to_string(osAccountId);
|
||||
std::shared_ptr<Uri> uri = std::make_shared<Uri>(SETTINGS_DATA_SECURE_URI + accountIdStr + "?Proxy=true&key=" +
|
||||
USER_DEFINED_STRING);
|
||||
LOGI("get user defined deviceName, accountId=%{public}s", accountIdStr.c_str());
|
||||
return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, USER_DEFINED_STRING, deviceName);
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::GetDisplayDeviceName(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
|
||||
std::string &deviceName)
|
||||
{
|
||||
int32_t osAccountId = GetActiveOsAccountIds();
|
||||
if (osAccountId == ERR_DM_FAILED) {
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
std::string accountIdStr = std::to_string(osAccountId);
|
||||
std::shared_ptr<Uri> uri = std::make_shared<Uri>(SETTINGS_DATA_SECURE_URI + accountIdStr + "?Proxy=true&key=" +
|
||||
DISPLAY_DEVICE_NAME_STRING);
|
||||
LOGI("get user defined deviceName, accountId=%{public}s", accountIdStr.c_str());
|
||||
return GetDeviceNameFromDataShareHelper(dataShareHelper, uri, DISPLAY_DEVICE_NAME_STRING, deviceName);
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::GetActiveOsAccountIds()
|
||||
{
|
||||
std::vector<int32_t> accountId;
|
||||
int32_t ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountId);
|
||||
if (ret != DM_OK || accountId.empty()) {
|
||||
LOGE("QueryActiveOsAccountIds failed");
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
LOGI("account id=%{public}d", accountId[0]);
|
||||
return accountId[0];
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::QueryLocalDeviceName()
|
||||
{
|
||||
LOGI("start");
|
||||
auto dataShareHelper = GetDataShareHelper();
|
||||
if (dataShareHelper == nullptr) {
|
||||
LOGE("dataShareHelper is null");
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
std::string localDeviceName = "";
|
||||
int32_t ret = GetUserDefinedDeviceName(dataShareHelper, localDeviceName);
|
||||
if (ret == DM_OK && !localDeviceName.empty()) {
|
||||
std::lock_guard<std::mutex> lock(devNameMtx_);
|
||||
localDeviceName_ = localDeviceName;
|
||||
dataShareHelper->Release();
|
||||
LOGI("get user defined deviceName=%{public}s", localDeviceName.c_str());
|
||||
return DM_OK;
|
||||
}
|
||||
ret = GetDefaultDeviceName(dataShareHelper, localDeviceName);
|
||||
LOGI("get default deviceName=%{public}s", localDeviceName.c_str());
|
||||
dataShareHelper->Release();
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
void LocalDeviceNameMgr::RegisterDeviceNameChangeCb()
|
||||
{
|
||||
LOGI("start");
|
||||
auto dataShareHelper = GetDataShareHelper();
|
||||
if (dataShareHelper == nullptr) {
|
||||
LOGE("dataShareHelper is null");
|
||||
return;
|
||||
}
|
||||
|
||||
auto uri = std::make_shared<Uri>(SETTINGS_DATA_BASE_URI + "&key=" + PREDICATES_STRING);
|
||||
sptr<SettingsDataEventMonitor> settingDataObserver =
|
||||
sptr<SettingsDataEventMonitor>(new SettingsDataEventMonitor(shared_from_this(),
|
||||
SettingsDataMonitorType::USER_DEFINED_DEVICE_NAME_MONITOR));
|
||||
dataShareHelper->RegisterObserver(*uri, settingDataObserver);
|
||||
|
||||
int32_t osAccountId = GetActiveOsAccountIds();
|
||||
if (osAccountId == ERR_DM_FAILED) {
|
||||
LOGE("Get OsAccountId error");
|
||||
return;
|
||||
}
|
||||
std::string accountIdStr = std::to_string(osAccountId);
|
||||
uri = std::make_shared<Uri>(SETTINGS_DATA_SECURE_URI + accountIdStr +
|
||||
"?Proxy=true&key=" + USER_DEFINED_STRING);
|
||||
|
||||
dataShareHelper->RegisterObserver(*uri, settingDataObserver);
|
||||
dataShareHelper->Release();
|
||||
LOGI("register device name change cb success");
|
||||
}
|
||||
|
||||
int32_t LocalDeviceNameMgr::QueryLocalDisplayName()
|
||||
{
|
||||
LOGI("start");
|
||||
auto dataShareHelper = GetDataShareHelper();
|
||||
if (dataShareHelper == nullptr) {
|
||||
LOGE("dataShareHelper is null");
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
std::string localDisplayName = "";
|
||||
int32_t ret = GetDisplayDeviceName(dataShareHelper, localDisplayName);
|
||||
if (ret != DM_OK || localDisplayName.empty()) {
|
||||
LOGE("get display device name failed");
|
||||
return ERR_DM_FAILED;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(devNameMtx_);
|
||||
localDisplayName_ = localDisplayName;
|
||||
dataShareHelper->Release();
|
||||
LOGI("get display deviceName=%{public}s", localDisplayName.c_str());
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
void LocalDeviceNameMgr::RegisterDisplayNameChangeCb()
|
||||
{
|
||||
LOGI("start");
|
||||
auto dataShareHelper = GetDataShareHelper();
|
||||
if (dataShareHelper == nullptr) {
|
||||
LOGE("dataShareHelper is null");
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t osAccountId = GetActiveOsAccountIds();
|
||||
if (osAccountId == ERR_DM_FAILED) {
|
||||
LOGE("Get OsAccountId error");
|
||||
return;
|
||||
}
|
||||
std::string accountIdStr = std::to_string(osAccountId);
|
||||
auto uri = std::make_shared<Uri>(SETTINGS_DATA_SECURE_URI + accountIdStr +
|
||||
"?Proxy=true&key=" + DISPLAY_DEVICE_NAME_STRING);
|
||||
sptr<SettingsDataEventMonitor> settingDataObserver =
|
||||
sptr<SettingsDataEventMonitor>(new SettingsDataEventMonitor(shared_from_this(),
|
||||
SettingsDataMonitorType::DISPLAY_DEVICE_NAME_MONITOR));
|
||||
dataShareHelper->RegisterObserver(*uri, settingDataObserver);
|
||||
dataShareHelper->Release();
|
||||
LOGI("register display device name change cb success");
|
||||
}
|
||||
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "settings_data_event_monitor.h"
|
||||
|
||||
#include "dm_log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
SettingsDataEventMonitor::SettingsDataEventMonitor(std::shared_ptr<LocalDeviceNameMgr> localDeviceNameMgr,
|
||||
SettingsDataMonitorType monitorType)
|
||||
: localDeviceNameMgrWPtr_(localDeviceNameMgr), monitorType_(monitorType)
|
||||
{
|
||||
LOGI("Ctor SettingsDataEventMonitor, monitorType: %{public}d", (int32_t)monitorType);
|
||||
}
|
||||
|
||||
void SettingsDataEventMonitor::OnChange()
|
||||
{
|
||||
if (localDeviceNameMgrWPtr_.expired()) {
|
||||
LOGE("localDeviceNameMgrWPtr_ expired");
|
||||
return;
|
||||
}
|
||||
LOGI("Settings OnChange type: %{public}d", (int32_t)monitorType_);
|
||||
std::shared_ptr<LocalDeviceNameMgr> localDevNameSPtr = localDeviceNameMgrWPtr_.lock();
|
||||
switch (monitorType_) {
|
||||
case SettingsDataMonitorType::USER_DEFINED_DEVICE_NAME_MONITOR:
|
||||
localDevNameSPtr->QueryLocalDeviceName();
|
||||
break;
|
||||
case SettingsDataMonitorType::DISPLAY_DEVICE_NAME_MONITOR:
|
||||
localDevNameSPtr->QueryLocalDisplayName();
|
||||
break;
|
||||
default:
|
||||
LOGE("unknwon monitor type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // DistributedHardware
|
||||
} // OHOS
|
@ -15,10 +15,6 @@
|
||||
|
||||
#include "ipc_server_stub.h"
|
||||
|
||||
#include "device_manager_ipc_interface_code.h"
|
||||
#include "device_manager_service.h"
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "if_system_ability_manager.h"
|
||||
#include "ipc_cmd_register.h"
|
||||
#include "ipc_skeleton.h"
|
||||
@ -31,9 +27,13 @@
|
||||
#include "mem_mgr_client.h"
|
||||
#include "mem_mgr_proxy.h"
|
||||
#endif // SUPPORT_MEMMGR
|
||||
|
||||
#include "string_ex.h"
|
||||
#include "system_ability_definition.h"
|
||||
|
||||
#include "device_manager_ipc_interface_code.h"
|
||||
#include "device_manager_service.h"
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "multiple_user_connector.h"
|
||||
|
||||
namespace OHOS {
|
||||
@ -47,6 +47,7 @@ IpcServerStub::IpcServerStub() : SystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGE
|
||||
{
|
||||
registerToService_ = false;
|
||||
state_ = ServiceRunningState::STATE_NOT_START;
|
||||
accountBootListener_ = std::make_shared<AccountBootListener>();
|
||||
}
|
||||
|
||||
void IpcServerStub::OnStart()
|
||||
@ -85,6 +86,7 @@ void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::strin
|
||||
return;
|
||||
}
|
||||
state_ = ServiceRunningState::STATE_RUNNING;
|
||||
accountBootListener_->RegisterAccountBootCb();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,8 @@ ohos_fuzztest("IpcServerStubFuzzTest") {
|
||||
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"ipc:ipc_core",
|
||||
"ipc:ipc_single",
|
||||
|
@ -93,6 +93,8 @@ ohos_unittest("UTTest_pin_auth") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -174,6 +176,8 @@ ohos_unittest("UTTest_ipc_cmd_parser_service") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -672,6 +676,8 @@ ohos_unittest("UTTest_ipc_server_client_proxy") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -692,6 +698,8 @@ ohos_unittest("UTTest_ipc_server_listener") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"googletest:gmock",
|
||||
]
|
||||
@ -708,6 +716,8 @@ ohos_unittest("UTTest_ipc_server_stub") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -1150,6 +1160,8 @@ ohos_unittest("UTTest_discovery_filter") {
|
||||
deps = [ ":device_manager_test_common" ]
|
||||
|
||||
external_deps = [
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -1368,6 +1380,8 @@ ohos_unittest("UTTest_dm_publish_manager") {
|
||||
|
||||
external_deps = [
|
||||
"cJSON:cjson",
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
@ -1472,6 +1486,8 @@ ohos_static_library("device_manager_test_common") {
|
||||
"c_utils:utils",
|
||||
"common_event_service:cesfwk_core",
|
||||
"common_event_service:cesfwk_innerkits",
|
||||
"data_share:datashare_common",
|
||||
"data_share:datashare_consumer",
|
||||
"device_auth:deviceauth_sdk",
|
||||
"device_info_manager:distributed_device_profile_common",
|
||||
"device_info_manager:distributed_device_profile_sdk",
|
||||
|
Loading…
Reference in New Issue
Block a user