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 "constants.h"
17 #include "dh_utils_tool.h"
18 #include "distributed_hardware_log.h"
19 #include "publisher_listener_proxy.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
PublisherListenerProxy(const sptr<IRemoteObject> object)23 PublisherListenerProxy::PublisherListenerProxy(const sptr<IRemoteObject> object)
24     : IRemoteProxy<IPublisherListener>(object)
25 {
26 }
27 
~PublisherListenerProxy()28 PublisherListenerProxy::~PublisherListenerProxy()
29 {
30 }
31 
OnMessage(const DHTopic topic,const std::string & message)32 void PublisherListenerProxy::OnMessage(const DHTopic topic, const std::string& message)
33 {
34     sptr<IRemoteObject> remote = Remote();
35     if (remote == nullptr) {
36         DHLOGE("Get Remote IRemoteObject failed");
37         return;
38     }
39     if (topic < DHTopic::TOPIC_MIN || topic > DHTopic::TOPIC_MAX) {
40         DHLOGE("Topic is invalid!");
41         return;
42     }
43     if (!IsMessageLengthValid(message)) {
44         return;
45     }
46 
47     MessageParcel data;
48     MessageParcel reply;
49     MessageOption option;
50     if (!data.WriteInterfaceToken(GetDescriptor())) {
51         DHLOGE("PublisherListenerProxy write token failed");
52         return;
53     }
54 
55     if (!data.WriteUint32((uint32_t)topic)) {
56         DHLOGE("Parcel write topic failed");
57         return;
58     }
59 
60     if (!data.WriteString(message)) {
61         DHLOGE("Parcel write message failed");
62         return;
63     }
64     int32_t ret = remote->SendRequest(
65         static_cast<uint32_t>(IPublisherListener::Message::ON_MESSAGE), data, reply, option);
66     if (ret != 0) {
67         DHLOGE("PublisherListenerProxy send requeset failed, ret: %{public}d", ret);
68         return;
69     }
70 }
71 } // namespace DistributedHardware
72 } // namespace OHOS