1 /*
2 * Copyright (c) 2022-2024 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_server_listener.h"
17
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "ipc_cmd_register.h"
21 #include "ipc_def.h"
22 #include "ipc_server_listenermgr.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
CommonSvcToIdentity(CommonSvcId * svcId,SvcIdentity * identity)26 void IpcServerListener::CommonSvcToIdentity(CommonSvcId *svcId, SvcIdentity *identity)
27 {
28 identity->handle = svcId->handle;
29 identity->token = svcId->token;
30 identity->cookie = svcId->cookie;
31 }
32
GetIdentityByPkgName(std::string & name,SvcIdentity * svc)33 int32_t IpcServerListener::GetIdentityByPkgName(std::string &name, SvcIdentity *svc)
34 {
35 CommonSvcId svcId;
36 if (IpcServerListenermgr::GetInstance().GetListenerByPkgName(name, &svcId) != DM_OK) {
37 LOGE("get identity failed.");
38 return ERR_DM_FAILED;
39 }
40 CommonSvcToIdentity(&svcId, svc);
41 return DM_OK;
42 }
43
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)44 int32_t IpcServerListener::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
45 {
46 if (req == nullptr || rsp == nullptr) {
47 LOGE("SendRequest req or rsp is nullptr.");
48 return ERR_DM_POINT_NULL;
49 }
50 std::string pkgName = req->GetPkgName();
51 SvcIdentity svc;
52 if (GetIdentityByPkgName(pkgName, &svc) != DM_OK) {
53 LOGE("OnDeviceFound callback get listener failed.");
54 return ERR_DM_FAILED;
55 }
56
57 IpcIo io;
58 uint8_t data[MAX_DM_IPC_LEN] = {0};
59 if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, io, data, MAX_DM_IPC_LEN) != DM_OK) {
60 LOGE("SetRequest failed cmdCode:%{public}d", cmdCode);
61 return ERR_DM_FAILED;
62 }
63
64 MessageOption option;
65 MessageOptionInit(&option);
66 option.flags = TF_OP_ASYNC;
67 if (::SendRequest(svc, cmdCode, &io, nullptr, option, nullptr) != DM_OK) {
68 LOGI("SendRequest failed cmdCode:%{public}d", cmdCode);
69 }
70 return DM_OK;
71 }
72
GetAllProcessInfo()73 std::vector<ProcessInfo> IpcServerListener::GetAllProcessInfo()
74 {
75 std::vector<ProcessInfo> processInfoVec;
76 const std::map<std::string, CommonSvcId> &listenerMap = IpcServerListenermgr::GetInstance().GetAllListeners();
77 for (const auto &kv : listenerMap) {
78 ProcessInfo processInfo;
79 processInfo.pkgName = kv.first;
80 processInfoVec.push_back(processInfo);
81 }
82 return processInfoVec;
83 }
84
GetSystemSA()85 std::set<std::string> IpcServerListener::GetSystemSA()
86 {
87 return {};
88 }
89 } // namespace DistributedHardware
90 } // namespace OHOS
91