1 /*
2 * Copyright (c) 2021 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 "distributed_hardware_stub.h"
17
18 #include <cinttypes>
19
20 #include "nlohmann/json.hpp"
21
22 #include "anonymous_string.h"
23 #include "constants.h"
24 #include "publisher_listener_proxy.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
31 MessageOption &option)
32 {
33 if (data.ReadInterfaceToken() != GetDescriptor()) {
34 DHLOGE("IPC Token valid fail!");
35 return ERR_INVALID_DATA;
36 }
37 switch (code) {
38 case (uint32_t)IDistributedHardware::Message::REG_PUBLISHER_LISTNER: {
39 return RegisterPublisherListenerInner(data, reply);
40 }
41 case (uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER: {
42 return UnregisterPublisherListenerInner(data, reply);
43 }
44 case (uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE: {
45 return PublishMessageInner(data, reply);
46 }
47 default:
48 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49 }
50 return DH_FWK_SUCCESS;
51 }
52
RegisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)53 int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
54 {
55 uint32_t topicInt = data.ReadUint32();
56 if (!ValidTopic(topicInt)) {
57 DHLOGE("Topic invalid: %" PRIu32, topicInt);
58 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
59 return ERR_DH_FWK_PARA_INVALID;
60 }
61
62 DHTopic topic = (DHTopic)topicInt;
63 sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
64 if (listener == nullptr) {
65 DHLOGE("Register publisher listener is null");
66 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
67 return ERR_DH_FWK_PARA_INVALID;
68 }
69 DHLOGI("Register listener, topic: %" PRIu32, (uint32_t)topic);
70 RegisterPublisherListener(topic, listener);
71 reply.WriteInt32(DH_FWK_SUCCESS);
72 return DH_FWK_SUCCESS;
73 }
74
UnregisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)75 int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
76 {
77 uint32_t topicInt = data.ReadUint32();
78 if (!ValidTopic(topicInt)) {
79 DHLOGE("Topic invalid: %" PRIu32, topicInt);
80 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
81 return ERR_DH_FWK_PARA_INVALID;
82 }
83
84 DHTopic topic = (DHTopic)topicInt;
85 sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
86 if (listener == nullptr) {
87 DHLOGE("Unregister publisher listener is null");
88 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
89 return ERR_DH_FWK_PARA_INVALID;
90 }
91 DHLOGI("Unregister listener, topic: %" PRIu32, (uint32_t)topic);
92 UnregisterPublisherListener(topic, listener);
93 reply.WriteInt32(DH_FWK_SUCCESS);
94 return DH_FWK_SUCCESS;
95 }
96
PublishMessageInner(MessageParcel & data,MessageParcel & reply)97 int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data, MessageParcel &reply)
98 {
99 uint32_t topicInt = data.ReadUint32();
100 if (!ValidTopic(topicInt)) {
101 DHLOGE("Topic invalid: %" PRIu32, topicInt);
102 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
103 return ERR_DH_FWK_PARA_INVALID;
104 }
105
106 DHTopic topic = (DHTopic)topicInt;
107 std::string message = data.ReadString();
108 DHLOGI("Publish message, topic: %" PRIu32, (uint32_t)topic);
109 PublishMessage(topic, message);
110 reply.WriteInt32(DH_FWK_SUCCESS);
111 return DH_FWK_SUCCESS;
112 }
113
ValidTopic(uint32_t topic)114 bool DistributedHardwareStub::ValidTopic(uint32_t topic)
115 {
116 if (topic <= (uint32_t)DHTopic::TOPIC_MIN || topic >= (uint32_t)DHTopic::TOPIC_MAX) {
117 return false;
118 }
119 return true;
120 }
121 } // namespace DistributedHardware
122 } // namespace OHOS
123