• 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 %d", ret);
48         return ret;
49     }
50     ret = rsp->GetErrCode();
51     if (ret != DM_OK) {
52         LOGE("DeviceManager::InitDeviceManager completed, pkgName: %s, ret = %d", pkgName.c_str(), ret);
53         return ret;
54     }
55     packageInitSet_.emplace(pkgName);
56     return DM_OK;
57 }
58 
UnInit(const std::string & pkgName)59 int32_t IpcClientManager::UnInit(const std::string &pkgName)
60 {
61     LOGI("UnInitDeviceManager in, pkgName %s", pkgName.c_str());
62     if (!IsInit(pkgName)) {
63         return ERR_DM_FAILED;
64     }
65     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
66     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
67     req->SetPkgName(pkgName);
68     int32_t ret = serverProxy_.SendCmd(UNREGISTER_DEVICE_MANAGER_LISTENER, req, rsp);
69     if (ret != DM_OK) {
70         LOGE("UnRegisterDeviceManagerListener Failed with ret %d", ret);
71         return ret;
72     }
73     packageInitSet_.erase(pkgName);
74     LOGI("UnInitDeviceManager SUCCESS");
75     return DM_OK;
76 }
77 
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)78 int32_t IpcClientManager::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
79 {
80     std::string pkgName = req->GetPkgName();
81     if (!IsInit(pkgName)) {
82         return ERR_DM_INIT_FAILED;
83     }
84     return serverProxy_.SendCmd(cmdCode, req, rsp);
85 }
86 
IsInit(const std::string & pkgName)87 bool IpcClientManager::IsInit(const std::string &pkgName)
88 {
89     return (packageInitSet_.count(pkgName) > 0);
90 }
91 
OnDmServiceDied()92 int32_t IpcClientManager::OnDmServiceDied()
93 {
94     return DM_OK;
95 }
96 } // namespace DistributedHardware
97 } // namespace OHOS
98