• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "dhardware_ipc_interface_code.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 #include "publisher_listener_proxy.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
32     MessageOption &option)
33 {
34     if (data.ReadInterfaceToken() != GetDescriptor()) {
35         DHLOGE("IPC Token valid fail!");
36         return ERR_INVALID_DATA;
37     }
38     switch (code) {
39         case static_cast<uint32_t>(DHMsgInterfaceCode::REG_PUBLISHER_LISTNER): {
40             return RegisterPublisherListenerInner(data, reply);
41         }
42         case static_cast<uint32_t>(DHMsgInterfaceCode::UNREG_PUBLISHER_LISTENER): {
43             return UnregisterPublisherListenerInner(data, reply);
44         }
45         case static_cast<uint32_t>(DHMsgInterfaceCode::PUBLISH_MESSAGE): {
46             return PublishMessageInner(data, reply);
47         }
48         case static_cast<uint32_t>(DHMsgInterfaceCode::INIT_CTL_CEN): {
49             return InitializeAVCenterInner(data, reply);
50         }
51         case static_cast<uint32_t>(DHMsgInterfaceCode::RELEASE_CTL_CEN): {
52             return ReleaseAVCenterInner(data, reply);
53         }
54         case static_cast<uint32_t>(DHMsgInterfaceCode::CREATE_CTL_CEN_CHANNEL): {
55             return CreateControlChannelInner(data, reply);
56         }
57         case static_cast<uint32_t>(DHMsgInterfaceCode::NOTIFY_AV_EVENT): {
58             return NotifyAVCenterInner(data, reply);
59         }
60         case static_cast<uint32_t>(DHMsgInterfaceCode::REGISTER_CTL_CEN_CALLBACK): {
61             return RegisterControlCenterCallbackInner(data, reply);
62         }
63         case static_cast<uint32_t>(DHMsgInterfaceCode::QUERY_LOCAL_SYS_SPEC): {
64             return QueryLocalSysSpecInner(data, reply);
65         }
66         default:
67             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68     }
69     return DH_FWK_SUCCESS;
70 }
71 
RegisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)72 int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
73 {
74     uint32_t topicInt = data.ReadUint32();
75     if (!ValidTopic(topicInt)) {
76         DHLOGE("Topic invalid: %" PRIu32, topicInt);
77         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
78         return ERR_DH_FWK_PARA_INVALID;
79     }
80 
81     DHTopic topic = (DHTopic)topicInt;
82     sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
83     if (listener == nullptr) {
84         DHLOGE("Register publisher listener is null");
85         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
86         return ERR_DH_FWK_PARA_INVALID;
87     }
88     DHLOGI("Register listener, topic: %" PRIu32, (uint32_t)topic);
89     RegisterPublisherListener(topic, listener);
90     reply.WriteInt32(DH_FWK_SUCCESS);
91     return DH_FWK_SUCCESS;
92 }
93 
UnregisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)94 int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
95 {
96     uint32_t topicInt = data.ReadUint32();
97     if (!ValidTopic(topicInt)) {
98         DHLOGE("Topic invalid: %" PRIu32, topicInt);
99         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
100         return ERR_DH_FWK_PARA_INVALID;
101     }
102 
103     DHTopic topic = (DHTopic)topicInt;
104     sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
105     if (listener == nullptr) {
106         DHLOGE("Unregister publisher listener is null");
107         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
108         return ERR_DH_FWK_PARA_INVALID;
109     }
110     DHLOGI("Unregister listener, topic: %" PRIu32, (uint32_t)topic);
111     UnregisterPublisherListener(topic, listener);
112     reply.WriteInt32(DH_FWK_SUCCESS);
113     return DH_FWK_SUCCESS;
114 }
115 
PublishMessageInner(MessageParcel & data,MessageParcel & reply)116 int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data, MessageParcel &reply)
117 {
118     uint32_t topicInt = data.ReadUint32();
119     if (!ValidTopic(topicInt)) {
120         DHLOGE("Topic invalid: %" PRIu32, topicInt);
121         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
122         return ERR_DH_FWK_PARA_INVALID;
123     }
124 
125     DHTopic topic = (DHTopic)topicInt;
126     std::string message = data.ReadString();
127     DHLOGI("Publish message, topic: %" PRIu32, (uint32_t)topic);
128     PublishMessage(topic, message);
129     reply.WriteInt32(DH_FWK_SUCCESS);
130     return DH_FWK_SUCCESS;
131 }
132 
QueryLocalSysSpecInner(MessageParcel & data,MessageParcel & reply)133 int32_t DistributedHardwareStub::QueryLocalSysSpecInner(MessageParcel &data, MessageParcel &reply)
134 {
135     uint32_t specInt = data.ReadUint32();
136     if (!ValidQueryLocalSpec(specInt)) {
137         DHLOGE("Spec invalid: %" PRIu32, specInt);
138         reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
139         return ERR_DH_FWK_PARA_INVALID;
140     }
141 
142     QueryLocalSysSpecType spec = (QueryLocalSysSpecType)specInt;
143     DHLOGI("Query Local Sys Spec: %" PRIu32, (uint32_t)spec);
144     std::string res = QueryLocalSysSpec(spec);
145     DHLOGI("Get Local spec: %s", res.c_str());
146     reply.WriteString(res);
147     return DH_FWK_SUCCESS;
148 }
149 
InitializeAVCenterInner(MessageParcel & data,MessageParcel & reply)150 int32_t DistributedHardwareStub::InitializeAVCenterInner(MessageParcel &data, MessageParcel &reply)
151 {
152     TransRole transRole = (TransRole)(data.ReadUint32());
153     int32_t engineId = 0;
154     int32_t ret = InitializeAVCenter(transRole, engineId);
155     if (!reply.WriteInt32(engineId)) {
156         DHLOGE("Write engine id failed");
157         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
158     }
159     if (!reply.WriteInt32(ret)) {
160         DHLOGE("Write ret code failed");
161         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
162     }
163     return DH_FWK_SUCCESS;
164 }
165 
ReleaseAVCenterInner(MessageParcel & data,MessageParcel & reply)166 int32_t DistributedHardwareStub::ReleaseAVCenterInner(MessageParcel &data, MessageParcel &reply)
167 {
168     int32_t engineId = data.ReadInt32();
169     int32_t ret = ReleaseAVCenter(engineId);
170     if (!reply.WriteInt32(ret)) {
171         DHLOGE("Write ret code failed");
172         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
173     }
174     return DH_FWK_SUCCESS;
175 }
176 
CreateControlChannelInner(MessageParcel & data,MessageParcel & reply)177 int32_t DistributedHardwareStub::CreateControlChannelInner(MessageParcel &data, MessageParcel &reply)
178 {
179     int32_t engineId = data.ReadInt32();
180     std::string peerDevId = data.ReadString();
181     int32_t ret = CreateControlChannel(engineId, peerDevId);
182     if (!reply.WriteInt32(ret)) {
183         DHLOGE("Write ret code failed");
184         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
185     }
186     return DH_FWK_SUCCESS;
187 }
188 
NotifyAVCenterInner(MessageParcel & data,MessageParcel & reply)189 int32_t DistributedHardwareStub::NotifyAVCenterInner(MessageParcel &data, MessageParcel &reply)
190 {
191     int32_t engineId = data.ReadInt32();
192     uint32_t type = data.ReadUint32();
193     std::string content = data.ReadString();
194     std::string peerDevId = data.ReadString();
195     int32_t ret = NotifyAVCenter(engineId, AVTransEvent{ (EventType)type, content, peerDevId });
196     if (!reply.WriteInt32(ret)) {
197         DHLOGE("Write ret code failed");
198         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
199     }
200     return DH_FWK_SUCCESS;
201 }
202 
RegisterControlCenterCallbackInner(MessageParcel & data,MessageParcel & reply)203 int32_t DistributedHardwareStub::RegisterControlCenterCallbackInner(MessageParcel &data, MessageParcel &reply)
204 {
205     int32_t engineId = data.ReadInt32();
206     sptr<IAVTransControlCenterCallback> callback = iface_cast<IAVTransControlCenterCallback>(data.ReadRemoteObject());
207     if (callback == nullptr) {
208         DHLOGE("Input av control center callback is null");
209         return ERR_DH_FWK_PARA_INVALID;
210     }
211 
212     int32_t ret = RegisterCtlCenterCallback(engineId, callback);
213     if (!reply.WriteInt32(ret)) {
214         DHLOGE("Write ret code failed");
215         return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
216     }
217     return DH_FWK_SUCCESS;
218 }
219 
ValidTopic(uint32_t topic)220 bool DistributedHardwareStub::ValidTopic(uint32_t topic)
221 {
222     if (topic <= (uint32_t)DHTopic::TOPIC_MIN || topic >= (uint32_t)DHTopic::TOPIC_MAX) {
223         return false;
224     }
225     return true;
226 }
227 
ValidQueryLocalSpec(uint32_t spec)228 bool DistributedHardwareStub::ValidQueryLocalSpec(uint32_t spec)
229 {
230     if (spec <= (uint32_t)QueryLocalSysSpecType::MIN || spec >= (uint32_t)QueryLocalSysSpecType::MAX) {
231         return false;
232     }
233     return true;
234 }
235 } // namespace DistributedHardware
236 } // namespace OHOS
237