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