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