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 "constants.h"
17 #include "distributed_hardware_log.h"
18 #include "publisher_listener_proxy.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
PublisherListenerProxy(const sptr<IRemoteObject> & object)22 PublisherListenerProxy::PublisherListenerProxy(const sptr<IRemoteObject> &object)
23 : IRemoteProxy<IPublisherListener>(object)
24 {
25 }
26
~PublisherListenerProxy()27 PublisherListenerProxy::~PublisherListenerProxy()
28 {
29 }
30
OnMessage(const DHTopic topic,const std::string & message)31 void PublisherListenerProxy::OnMessage(const DHTopic topic, const std::string& message)
32 {
33 sptr<IRemoteObject> remote = Remote();
34 if (remote == nullptr) {
35 DHLOGE("Get Remote IRemoteObject failed");
36 return;
37 }
38 if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) {
39 DHLOGE("Topic is invalid!");
40 return;
41 }
42 if (message.size() == 0 || message.size() > MAX_MESSAGE_LEN) {
43 DHLOGE("Message is invalid");
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<int32_t>(IPublisherListener::Message::ON_MESSAGE), data, reply, option);
66 if (ret != 0) {
67 DHLOGE("PublisherListenerProxy send requeset failed, ret: %d", ret);
68 return;
69 }
70 }
71 } // namespace DistributedHardware
72 } // namespace OHOS