1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "device_manager_impl_mini.h"
17
18 #include "device_manager_ipc_interface_code.h"
19 #include "dm_error_type.h"
20 #include "dm_log.h"
21 #include "ipc_get_local_display_device_name_req.h"
22 #include "ipc_get_local_display_device_name_rsp.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace {
27 constexpr int32_t SERVICE_INIT_MAX_NUM = 20;
28 const int32_t USLEEP_TIME_US_100000 = 100000; // 100ms
29 }
GetInstance()30 DeviceManagerImplMini &DeviceManagerImplMini::GetInstance()
31 {
32 static DeviceManagerImplMini instance;
33 return instance;
34 }
35
InitDeviceManager(const std::string & pkgName)36 int32_t DeviceManagerImplMini::InitDeviceManager(const std::string &pkgName)
37 {
38 if (pkgName.empty()) {
39 LOGE("InitDeviceManager error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
40 return ERR_DM_INPUT_PARA_INVALID;
41 }
42 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
43 int32_t ret = DM_OK;
44 int32_t retryNum = 0;
45 while (retryNum < SERVICE_INIT_MAX_NUM) {
46 ret = ipcClientProxy_->Init(pkgName);
47 if (ret == DM_OK) {
48 break;
49 }
50 usleep(USLEEP_TIME_US_100000);
51 retryNum++;
52 if (retryNum == SERVICE_INIT_MAX_NUM) {
53 LOGE("InitDeviceManager error, wait for device manager service starting timeout.");
54 return ERR_DM_NOT_INIT;
55 }
56 }
57 if (ret != DM_OK) {
58 LOGE("InitDeviceManager error, proxy init failed ret: %{public}d", ret);
59 return ERR_DM_INIT_FAILED;
60 }
61 LOGI("Success");
62 return DM_OK;
63 }
64
UnInitDeviceManager(const std::string & pkgName)65 int32_t DeviceManagerImplMini::UnInitDeviceManager(const std::string &pkgName)
66 {
67 if (pkgName.empty()) {
68 LOGE("UnInitDeviceManager Invalid parameter, pkgName is empty.");
69 return ERR_DM_INPUT_PARA_INVALID;
70 }
71 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
72 int32_t ret = ipcClientProxy_->UnInit(pkgName);
73 if (ret != DM_OK) {
74 LOGE("UnInitDeviceManager error, proxy unInit failed ret: %{public}d", ret);
75 return ERR_DM_FAILED;
76 }
77 LOGI("Success");
78 return DM_OK;
79 }
80
OnDmServiceDied()81 int32_t DeviceManagerImplMini::OnDmServiceDied()
82 {
83 LOGI("Start");
84 int32_t ret = ipcClientProxy_->OnDmServiceDied();
85 if (ret != DM_OK) {
86 LOGE("OnDmServiceDied failed, ret: %{public}d", ret);
87 return ERR_DM_FAILED;
88 }
89 return DM_OK;
90 }
91
GetLocalDisplayDeviceName(const std::string & pkgName,int32_t maxNameLength,std::string & displayName)92 int32_t DeviceManagerImplMini::GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength,
93 std::string &displayName)
94 {
95 LOGI("In pkgName:%{public}s,", pkgName.c_str());
96 std::shared_ptr<IpcGetLocalDisplayDeviceNameReq> req = std::make_shared<IpcGetLocalDisplayDeviceNameReq>();
97 std::shared_ptr<IpcGetLocalDisplayDeviceNameRsp> rsp = std::make_shared<IpcGetLocalDisplayDeviceNameRsp>();
98 req->SetPkgName(pkgName);
99 req->SetMaxNameLength(maxNameLength);
100 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DISPLAY_DEVICE_NAME, req, rsp);
101 if (ret != DM_OK) {
102 LOGE("error: Send Request failed ret: %{public}d", ret);
103 return ERR_DM_IPC_SEND_REQUEST_FAILED;
104 }
105 ret = rsp->GetErrCode();
106 if (ret != DM_OK) {
107 LOGE("error: Failed with ret %{public}d", ret);
108 return ret;
109 }
110 displayName = rsp->GetDisplayName();
111 LOGI("Completed");
112 return DM_OK;
113 }
114
115 } // namespace DistributedHardware
116 } // namespace OHOS
117