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