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_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_errcode.h"
25 #include "dscreen_log.h"
26
27 namespace OHOS { class MessageOption; }
28 namespace OHOS {
29 namespace DistributedHardware {
DScreenSourceCallbackStub()30 DScreenSourceCallbackStub::DScreenSourceCallbackStub()
31 {
32 memberFuncMap_[NOTIFY_REG_RESULT] = &DScreenSourceCallbackStub::OnNotifyRegResultInner;
33 memberFuncMap_[NOTIFY_UNREG_RESULT] = &DScreenSourceCallbackStub::OnNotifyUnregResultInner;
34 }
35
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t DScreenSourceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
37 MessageParcel &reply, MessageOption &option)
38 {
39 std::u16string desc = DScreenSourceCallbackStub::GetDescriptor();
40 std::u16string remoteDesc = data.ReadInterfaceToken();
41 if (desc != remoteDesc) {
42 DHLOGE("DScreenSourceCallbackStub::OnRemoteRequest remoteDesc is invalid!");
43 return ERR_INVALID_DATA;
44 }
45
46 std::map<int32_t, DScreenFunc>::iterator iter = memberFuncMap_.find(code);
47 if (iter == memberFuncMap_.end()) {
48 DHLOGE("invalid request code.");
49 return ERR_DH_SCREEN_SA_REQUEST_CODE_INVALID;
50 }
51 DScreenFunc &func = iter->second;
52 return (this->*func)(data, reply, option);
53 }
54
OnNotifyRegResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int32_t DScreenSourceCallbackStub::OnNotifyRegResultInner(MessageParcel &data, MessageParcel &reply,
56 MessageOption &option)
57 {
58 std::string devId = data.ReadString();
59 std::string dhId = data.ReadString();
60 std::string reqId = data.ReadString();
61 int32_t status = data.ReadInt32();
62 std::string resultData = data.ReadString();
63 if (!CheckParams(devId, dhId, reqId, resultData)) {
64 DHLOGE("OnNotifyRegResultInner error: Invalid parameter.");
65 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
66 }
67 int32_t ret = OnNotifyRegResult(devId, dhId, reqId, status, resultData);
68 return ret;
69 }
70
OnNotifyUnregResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)71 int32_t DScreenSourceCallbackStub::OnNotifyUnregResultInner(MessageParcel &data, MessageParcel &reply,
72 MessageOption &option)
73 {
74 std::string devId = data.ReadString();
75 std::string dhId = data.ReadString();
76 std::string reqId = data.ReadString();
77 int32_t status = data.ReadInt32();
78 std::string resultData = data.ReadString();
79 if (!CheckParams(devId, dhId, reqId, resultData)) {
80 DHLOGE("OnNotifyUnregResultInner error: Invalid parameter.");
81 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
82 }
83 int32_t ret = OnNotifyUnregResult(devId, dhId, reqId, status, resultData);
84 return ret;
85 }
86
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const std::string & resultData)87 bool DScreenSourceCallbackStub::CheckParams(const std::string &devId, const std::string &dhId,
88 const std::string &reqId, const std::string &resultData)
89 {
90 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
91 DHLOGE("DScreenSourceCallbackStub CheckParams devId or dhId is invalid.");
92 return false;
93 }
94 if (reqId.empty() || reqId.size() > DID_MAX_SIZE || resultData.empty() || resultData.size() > PARAM_MAX_SIZE) {
95 DHLOGE("DScreenSourceCallbackStub CheckParams reqId or resultData is invalid.");
96 return false;
97 }
98 return true;
99 }
100 }
101 }