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_server_listener.h"
17
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "ipc_server_stub.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)24 int32_t IpcServerListener::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
25 {
26 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT || rsp == nullptr) {
27 LOGE("IpcServerListener::SendRequest cmdCode param invalid!");
28 return ERR_DM_INPUT_PARA_INVALID;
29 }
30 std::string pkgName = req->GetPkgName();
31 if (pkgName.empty()) {
32 LOGE("Invalid parameter, pkgName is empty.");
33 return ERR_DM_INPUT_PARA_INVALID;
34 }
35 sptr<IpcRemoteBroker> listener = IpcServerStub::GetInstance().GetDmListener(pkgName);
36 if (listener == nullptr) {
37 LOGI("cannot get listener for package:%s.", pkgName.c_str());
38 return ERR_DM_POINT_NULL;
39 }
40 return listener->SendCmd(cmdCode, req, rsp);
41 }
42
SendAll(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)43 int32_t IpcServerListener::SendAll(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
44 {
45 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
46 LOGE("IpcServerListener::SendRequest cmdCode param invalid!");
47 return ERR_DM_INPUT_PARA_INVALID;
48 }
49 std::map<std::string, sptr<IRemoteObject>> listeners = IpcServerStub::GetInstance().GetDmListener();
50 for (auto iter : listeners) {
51 auto pkgName = iter.first;
52 auto remote = iter.second;
53 req->SetPkgName(pkgName);
54 sptr<IpcRemoteBroker> listener = iface_cast<IpcRemoteBroker>(remote);
55 if (listener == nullptr) {
56 LOGE("cannot get listener for package:%s.", pkgName.c_str());
57 return ERR_DM_FAILED;
58 }
59 listener->SendCmd(cmdCode, req, rsp);
60 }
61 return DM_OK;
62 }
63 } // namespace DistributedHardware
64 } // namespace OHOS
65