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 "dscreen_source_callback_stub.h"
17
18 #include <string>
19
20 #include "idscreen_source_callback.h"
21 #include "ipc_types.h"
22 #include "message_parcel.h"
23
24 #include "dscreen_constants.h"
25 #include "dscreen_errcode.h"
26 #include "dscreen_log.h"
27
28 namespace OHOS { class MessageOption; }
29 namespace OHOS {
30 namespace DistributedHardware {
DScreenSourceCallbackStub()31 DScreenSourceCallbackStub::DScreenSourceCallbackStub() {}
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t DScreenSourceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
34 MessageParcel &reply, MessageOption &option)
35 {
36 std::u16string desc = DScreenSourceCallbackStub::GetDescriptor();
37 std::u16string remoteDesc = data.ReadInterfaceToken();
38 if (desc != remoteDesc) {
39 DHLOGE("DScreenSourceCallbackStub::OnRemoteRequest remoteDesc is invalid!");
40 return ERR_INVALID_DATA;
41 }
42
43 switch (code) {
44 case NOTIFY_REG_RESULT:
45 return OnNotifyRegResultInner(data, reply, option);
46 case NOTIFY_UNREG_RESULT:
47 return OnNotifyUnregResultInner(data, reply, option);
48 default:
49 DHLOGE("invalid request code.");
50 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
51 }
52 }
53
OnNotifyRegResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int32_t DScreenSourceCallbackStub::OnNotifyRegResultInner(MessageParcel &data, MessageParcel &reply,
55 MessageOption &option)
56 {
57 (void)reply;
58 (void)option;
59 std::string devId = data.ReadString();
60 std::string dhId = data.ReadString();
61 std::string reqId = data.ReadString();
62 int32_t status = data.ReadInt32();
63 std::string resultDataStr = data.ReadString();
64 if (!CheckParams(devId, dhId, reqId, resultDataStr)) {
65 DHLOGE("OnNotifyRegResultInner error: Invalid parameter.");
66 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
67 }
68 int32_t ret = OnNotifyRegResult(devId, dhId, reqId, status, resultDataStr);
69 return ret;
70 }
71
OnNotifyUnregResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)72 int32_t DScreenSourceCallbackStub::OnNotifyUnregResultInner(MessageParcel &data, MessageParcel &reply,
73 MessageOption &option)
74 {
75 (void)reply;
76 (void)option;
77 std::string devId = data.ReadString();
78 std::string dhId = data.ReadString();
79 std::string reqId = data.ReadString();
80 int32_t status = data.ReadInt32();
81 std::string resultData = data.ReadString();
82 if (!CheckParams(devId, dhId, reqId, resultData)) {
83 DHLOGE("OnNotifyUnregResultInner error: Invalid parameter.");
84 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
85 }
86 int32_t ret = OnNotifyUnregResult(devId, dhId, reqId, status, resultData);
87 return ret;
88 }
89
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const std::string & resultData) const90 bool DScreenSourceCallbackStub::CheckParams(const std::string &devId, const std::string &dhId,
91 const std::string &reqId, const std::string &resultData) const
92 {
93 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
94 DHLOGE("DScreenSourceCallbackStub CheckParams devId or dhId is invalid.");
95 return false;
96 }
97 if (reqId.empty() || reqId.size() > DID_MAX_SIZE || resultData.empty() || resultData.size() > PARAM_MAX_SIZE) {
98 DHLOGE("DScreenSourceCallbackStub CheckParams reqId or resultData is invalid.");
99 return false;
100 }
101 return true;
102 }
103 } // namespace DistributedHardware
104 } // namespace OHOS
105