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 "dscreen_sink_stub.h"
17
18 #include "dscreen_constants.h"
19 #include "dscreen_errcode.h"
20 #include "dscreen_log.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
DScreenSinkStub()24 DScreenSinkStub::DScreenSinkStub()
25 {
26 memberFuncMap_[INIT_SINK] = &DScreenSinkStub::InitSinkInner;
27 memberFuncMap_[RELEASE_SINK] = &DScreenSinkStub::ReleaseSinkInner;
28 memberFuncMap_[SUBSCRIBE_DISTRIBUTED_HARDWARE] = &DScreenSinkStub::SubscribeDistributedHardwareInner;
29 memberFuncMap_[UNSUBSCRIBE_DISTRIBUTED_HARDWARE] = &DScreenSinkStub::UnsubscribeDistributedHardwareInner;
30 memberFuncMap_[DSCREEN_NOTIFY] = &DScreenSinkStub::DScreenNotifyInner;
31 }
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t DScreenSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34 MessageOption &option)
35 {
36 std::u16string desc = DScreenSinkStub::GetDescriptor();
37 std::u16string remoteDesc = data.ReadInterfaceToken();
38 if (desc != remoteDesc) {
39 DHLOGE("DScreenSinkStub::OnRemoteRequest remoteDesc is invalid!");
40 return ERR_INVALID_DATA;
41 }
42
43 const auto &iter = memberFuncMap_.find(code);
44 if (iter == memberFuncMap_.end()) {
45 DHLOGE("invalid request code.");
46 return ERR_DH_SCREEN_SA_INVALID_IPC_CALL;
47 }
48 DScreenSinkFunc &func = iter->second;
49 return (this->*func)(data, reply, option);
50 }
51
InitSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)52 int32_t DScreenSinkStub::InitSinkInner(MessageParcel &data, MessageParcel &reply,
53 MessageOption &option)
54 {
55 std::string param = data.ReadString();
56 if (param.empty() || param.size() > PARAM_MAX_SIZE) {
57 DHLOGE("InitSinkInner error: invalid parameter.");
58 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
59 }
60 int32_t ret = InitSink(param);
61 reply.WriteInt32(ret);
62 return DH_SUCCESS;
63 }
64
ReleaseSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int32_t DScreenSinkStub::ReleaseSinkInner(MessageParcel &data, MessageParcel &reply,
66 MessageOption &option)
67 {
68 int32_t ret = ReleaseSink();
69 reply.WriteInt32(ret);
70 return DH_SUCCESS;
71 }
72
SubscribeDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)73 int32_t DScreenSinkStub::SubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
74 MessageOption &option)
75 {
76 std::string dhId = data.ReadString();
77 std::string param = data.ReadString();
78 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || param.empty() || param.size() > PARAM_MAX_SIZE) {
79 DHLOGE("SubscribeDistributedHardwareInner error: invalid parameter.");
80 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
81 }
82 int32_t ret = SubscribeLocalHardware(dhId, param);
83 reply.WriteInt32(ret);
84 return DH_SUCCESS;
85 }
86
UnsubscribeDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)87 int32_t DScreenSinkStub::UnsubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
88 MessageOption &option)
89 {
90 std::string dhId = data.ReadString();
91 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
92 DHLOGE("UnsubscribeDistributedHardwareInner error: invalid parameter.");
93 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
94 }
95 int32_t ret = UnsubscribeLocalHardware(dhId);
96 reply.WriteInt32(ret);
97 return DH_SUCCESS;
98 }
99
DScreenNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)100 int32_t DScreenSinkStub::DScreenNotifyInner(MessageParcel &data, MessageParcel &reply,
101 MessageOption &option)
102 {
103 std::string devId = data.ReadString();
104 int32_t eventCode = data.ReadInt32();
105 std::string eventContent = data.ReadString();
106 if (devId.empty() || devId.size() > DID_MAX_SIZE || eventContent.empty() ||
107 eventContent.size() > PARAM_MAX_SIZE) {
108 DHLOGE("DScreenNotifyInner error: invalid parameter.");
109 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
110 }
111 DScreenNotify(devId, eventCode, eventContent);
112 return DH_SUCCESS;
113 }
114 } // namespace DistributedHardware
115 } // namespace OHOS