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_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     std::string pkgName = req->GetPkgName();
47     SvcIdentity svc;
48     if (GetIdentityByPkgName(pkgName, &svc) != DM_OK) {
49         LOGE("OnDeviceFound callback get listener failed.");
50         return ERR_DM_FAILED;
51     }
52 
53     IpcIo io;
54     uint8_t data[MAX_DM_IPC_LEN] = {0};
55     if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, io, data, MAX_DM_IPC_LEN) != DM_OK) {
56         LOGE("SetRequest failed cmdCode:%{public}d", cmdCode);
57         return ERR_DM_FAILED;
58     }
59 
60     MessageOption option;
61     MessageOptionInit(&option);
62     option.flags = TF_OP_ASYNC;
63     if (::SendRequest(svc, cmdCode, &io, nullptr, option, nullptr) != DM_OK) {
64         LOGI("SendRequest failed cmdCode:%{public}d", cmdCode);
65     }
66     return DM_OK;
67 }
68 
SendAll(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)69 int32_t IpcServerListener::SendAll(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
70 {
71     const std::map<std::string, CommonSvcId> &listenerMap = IpcServerListenermgr::GetInstance().GetAllListeners();
72     for (const auto &kv : listenerMap) {
73         SvcIdentity svc;
74         IpcIo io;
75         uint8_t data[MAX_DM_IPC_LEN] = {0};
76         std::string pkgName = kv.first;
77 
78         req->SetPkgName(pkgName);
79         if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, io, data, MAX_DM_IPC_LEN) != DM_OK) {
80             LOGE("SetRequest failed cmdCode:%{public}d", cmdCode);
81             continue;
82         }
83         CommonSvcId svcId = kv.second;
84         CommonSvcToIdentity(&svcId, &svc);
85         MessageOption option;
86         MessageOptionInit(&option);
87         option.flags = TF_OP_ASYNC;
88         if (::SendRequest(svc, cmdCode, &io, nullptr, option, nullptr) != DM_OK) {
89             LOGI("SendRequest failed cmdCode:%{public}d", cmdCode);
90         }
91     }
92     return DM_OK;
93 }
94 
GetAllPkgName()95 std::vector<std::string> IpcServerListener::GetAllPkgName()
96 {
97     std::vector<std::string> pkgNameList;
98     const std::map<std::string, CommonSvcId> &listenerMap = IpcServerListenermgr::GetInstance().GetAllListeners();
99     for (const auto &kv : listenerMap) {
100         pkgNameList.push_back(kv.first);
101     }
102     return pkgNameList;
103 }
104 } // namespace DistributedHardware
105 } // namespace OHOS
106