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_proxy.h"
17 
18 #include "parcel.h"
19 
20 #include "dscreen_constants.h"
21 #include "dscreen_errcode.h"
22 #include "dscreen_log.h"
23 #include "dscreen_util.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
OnNotifyRegResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & resultData)27 int32_t DScreenSourceCallbackProxy::OnNotifyRegResult(const std::string &devId, const std::string &dhId,
28     const std::string &reqId, int32_t status, const std::string &resultData)
29 {
30     if (!CheckParams(devId, dhId, reqId, resultData)) {
31         DHLOGE("OnNotifyRegResult error: invalid parameter.");
32         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
33     }
34     sptr<IRemoteObject> remote = Remote();
35     if (remote == nullptr) {
36         DHLOGE("DScreenSourceCallbackProxy remote service null");
37         return DSCREEN_BAD_VALUE;
38     }
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option;
42     if (!data.WriteInterfaceToken(GetDescriptor())) {
43         DHLOGE("WriteInterfaceToken failed");
44         return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
45     }
46 
47     if (!data.WriteString(devId) || !data.WriteString(dhId) ||
48         !data.WriteString(reqId) || !data.WriteInt32(status) ||
49         !data.WriteString(resultData)) {
50         DHLOGE("Write param failed.");
51         return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
52     }
53 
54     remote->SendRequest(NOTIFY_REG_RESULT, data, reply, option);
55     return reply.ReadInt32();
56 }
57 
OnNotifyUnregResult(const std::string & devId,const std::string & dhId,const std::string & reqId,int32_t status,const std::string & resultData)58 int32_t DScreenSourceCallbackProxy::OnNotifyUnregResult(const std::string &devId, const std::string &dhId,
59     const std::string &reqId, int32_t status, const std::string &resultData)
60 {
61     if (!CheckParams(devId, dhId, reqId, resultData)) {
62         DHLOGE("OnNotifyUnregResult error: invalid parameter.");
63         return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
64     }
65     sptr<IRemoteObject> remote = Remote();
66     if (remote == nullptr) {
67         DHLOGE("DScreenSourceCallbackProxy remote service null");
68         return DSCREEN_BAD_VALUE;
69     }
70     MessageParcel data;
71     MessageParcel reply;
72     MessageOption option;
73     if (!data.WriteInterfaceToken(GetDescriptor())) {
74         DHLOGE("WriteInterfaceToken failed.");
75         return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
76     }
77 
78     if (!data.WriteString(devId) || !data.WriteString(dhId) ||
79         !data.WriteString(reqId) || !data.WriteInt32(status) ||
80         !data.WriteString(resultData)) {
81         DHLOGE("Write param failed.");
82         return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
83     }
84 
85     remote->SendRequest(NOTIFY_UNREG_RESULT, data, reply, option);
86     return reply.ReadInt32();
87 }
88 
CheckParams(const std::string & devId,const std::string & dhId,const std::string & reqId,const std::string & resultData) const89 bool DScreenSourceCallbackProxy::CheckParams(const std::string &devId, const std::string &dhId,
90     const std::string &reqId, const std::string &resultData) const
91 {
92     if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
93         DHLOGE("DScreenSourceCallbackProxy CheckParams devId or dhId is invalid.");
94         return false;
95     }
96     if (reqId.empty() || reqId.size() > DID_MAX_SIZE || resultData.empty() || resultData.size() > PARAM_MAX_SIZE) {
97         DHLOGE("DScreenSourceCallbackProxy CheckParams reqId or resultData is invalid.");
98         return false;
99     }
100     return true;
101 }
102 } // namespace DistributedHardware
103 } // namespace OHOS