1 /*
2 * Copyright (c) 2022 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 "dm_constants.h"
19 #include "dm_log.h"
20 #include "ipc_client_stub.h"
21 #include "ipc_register_listener_req.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
Init(const std::string & pkgName)25 int32_t IpcClientManager::Init(const std::string &pkgName)
26 {
27 if (IsInit(pkgName)) {
28 LOGI("already init");
29 return DM_OK;
30 }
31 if (serverProxy_.Init() != DM_OK) {
32 LOGE("server proxy init failed.");
33 return ERR_DM_INIT_FAILED;
34 }
35 if (IpcClientStub::GetInstance().Init() != DM_OK) {
36 LOGE("ipcclientstub init failed.");
37 return ERR_DM_INIT_FAILED;
38 }
39
40 std::shared_ptr<IpcRegisterListenerReq> req = std::make_shared<IpcRegisterListenerReq>();
41 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
42 req->SetPkgName(pkgName);
43 req->SetSvcIdentity(IpcClientStub::GetInstance().GetSvcIdentity());
44 int32_t ret = serverProxy_.SendCmd(REGISTER_DEVICE_MANAGER_LISTENER, req, rsp);
45 if (ret != DM_OK) {
46 LOGE("InitDeviceManager: RegisterDeviceManagerListener Failed with ret %d", ret);
47 return ret;
48 }
49 ret = rsp->GetErrCode();
50 if (ret != DM_OK) {
51 LOGE("DeviceManager::InitDeviceManager completed, pkgName: %s, ret = %d", pkgName.c_str(), ret);
52 return ret;
53 }
54 packageInitSet_.emplace(pkgName);
55 return DM_OK;
56 }
57
UnInit(const std::string & pkgName)58 int32_t IpcClientManager::UnInit(const std::string &pkgName)
59 {
60 LOGI("UnInitDeviceManager in, pkgName %s", pkgName.c_str());
61 if (!IsInit(pkgName)) {
62 return ERR_DM_FAILED;
63 }
64 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
65 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
66 req->SetPkgName(pkgName);
67 int32_t ret = serverProxy_.SendCmd(UNREGISTER_DEVICE_MANAGER_LISTENER, req, rsp);
68 if (ret != DM_OK) {
69 LOGE("UnRegisterDeviceManagerListener Failed with ret %d", ret);
70 return ret;
71 }
72 packageInitSet_.erase(pkgName);
73 LOGI("UnInitDeviceManager SUCCESS");
74 return DM_OK;
75 }
76
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)77 int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
78 {
79 std::string pkgName = req->GetPkgName();
80 if (!IsInit(pkgName)) {
81 return ERR_DM_INIT_FAILED;
82 }
83 return serverProxy_.SendCmd(cmdCode, req, rsp);
84 }
85
IsInit(const std::string & pkgName)86 bool IpcClientManager::IsInit(const std::string &pkgName)
87 {
88 return (packageInitSet_.count(pkgName) > 0);
89 }
90
OnDmServiceDied()91 int32_t IpcClientManager::OnDmServiceDied()
92 {
93 return DM_OK;
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS
97