• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "ipc_client_manager.h"
17 
18 #include "device_manager_ipc_interface_code.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "ipc_client_stub.h"
22 #include "ipc_register_listener_req.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
Init(const std::string & pkgName)26 int32_t IpcClientManager::Init(const std::string &pkgName)
27 {
28     if (IsInit(pkgName)) {
29         LOGI("already init");
30         return DM_OK;
31     }
32     if (serverProxy_.Init() != DM_OK) {
33         LOGE("server proxy init failed.");
34         return ERR_DM_INIT_FAILED;
35     }
36     if (IpcClientStub::GetInstance().Init() != DM_OK) {
37         LOGE("ipcclientstub init failed.");
38         return ERR_DM_INIT_FAILED;
39     }
40 
41     std::shared_ptr<IpcRegisterListenerReq> req = std::make_shared<IpcRegisterListenerReq>();
42     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
43     req->SetPkgName(pkgName);
44     req->SetSvcIdentity(IpcClientStub::GetInstance().GetSvcIdentity());
45     int32_t ret = serverProxy_.SendCmd(REGISTER_DEVICE_MANAGER_LISTENER, req, rsp);
46     if (ret != DM_OK) {
47         LOGE("InitDeviceManager: RegisterDeviceManagerListener Failed with ret %{public}d", ret);
48         return ret;
49     }
50     ret = rsp->GetErrCode();
51     if (ret != DM_OK) {
52         LOGE("DeviceManager::InitDeviceManager completed, pkgName: %{public}s, ret = %{public}d", pkgName.c_str(),
53             ret);
54         return ret;
55     }
56     packageInitSet_.emplace(pkgName);
57     return DM_OK;
58 }
59 
UnInit(const std::string & pkgName)60 int32_t IpcClientManager::UnInit(const std::string &pkgName)
61 {
62     LOGI("UnInitDeviceManager in, pkgName %{public}s", pkgName.c_str());
63     if (!IsInit(pkgName)) {
64         return ERR_DM_FAILED;
65     }
66     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
67     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
68     req->SetPkgName(pkgName);
69     int32_t ret = serverProxy_.SendCmd(UNREGISTER_DEVICE_MANAGER_LISTENER, req, rsp);
70     if (ret != DM_OK) {
71         LOGE("UnRegisterDeviceManagerListener Failed with ret %{public}d", ret);
72         return ret;
73     }
74     ret = rsp->GetErrCode();
75     if (ret != DM_OK) {
76         LOGE("DeviceManager::UnInitDeviceManager completed, pkgName: %{public}s, ret = %{public}d", pkgName.c_str(),
77             ret);
78         return ret;
79     }
80     packageInitSet_.erase(pkgName);
81     LOGI("UnInitDeviceManager SUCCESS");
82     return DM_OK;
83 }
84 
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)85 int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
86 {
87     CHECK_NULL_RETURN(req, ERR_DM_POINT_NULL);
88     CHECK_NULL_RETURN(rsp, ERR_DM_POINT_NULL);
89     std::string pkgName = req->GetPkgName();
90     if (!IsInit(pkgName)) {
91         LOGE("PkgName: %s is not init.", pkgName.c_str());
92         return ERR_DM_INIT_FAILED;
93     }
94     return serverProxy_.SendCmd(cmdCode, req, rsp);
95 }
96 
IsInit(const std::string & pkgName)97 bool IpcClientManager::IsInit(const std::string &pkgName)
98 {
99     for (auto &iter : packageInitSet_) {
100         size_t len = iter.size();
101         if (len > pkgName.size()) {
102             continue;
103         }
104         std::string tmp = pkgName.substr(0, len);
105         if (tmp == iter) {
106             return true;
107         }
108     }
109     return false;
110 }
111 
OnDmServiceDied()112 int32_t IpcClientManager::OnDmServiceDied()
113 {
114     return DM_OK;
115 }
116 } // namespace DistributedHardware
117 } // namespace OHOS
118