1 /*
2 * Copyright (c) 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 "media_monitor_stub.h"
17 #include "media_monitor_ipc_interface_code.h"
18 #include "log.h"
19 #include "monitor_error.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MediaMonitorStub"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 namespace MediaMonitor {
28
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t MediaMonitorStub::OnRemoteRequest(
30 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32 FALSE_RETURN_V_MSG_E(data.ReadInterfaceToken() == GetDescriptor(), -1, "ReadInterfaceToken failed");
33 if (code <= static_cast<uint32_t>(MediaMonitorInterfaceCode ::MEDIA_MONITOR_CODE_MAX)) {
34 (this->*handlers[code])(data, reply);
35 return MediaMonitorErr::SUCCESS;
36 }
37 MEDIA_LOG_E("default case, need check MediaMonitorStub");
38 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
39 }
40
HandleWriteLogMsg(MessageParcel & data,MessageParcel & reply)41 void MediaMonitorStub::HandleWriteLogMsg(MessageParcel &data, MessageParcel &reply)
42 {
43 std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>();
44 eventBean->ReadFromParcel(data);
45 WriteLogMsg(eventBean);
46 }
47
HandleGetAudioRouteMsg(MessageParcel & data,MessageParcel & reply)48 void MediaMonitorStub::HandleGetAudioRouteMsg(MessageParcel &data, MessageParcel &reply)
49 {
50 MEDIA_LOG_I("HandleGetAudioRouteMsg in");
51 std::map<PerferredType, std::shared_ptr<MonitorDeviceInfo>> perferredDevices;
52
53 int32_t ret = GetAudioRouteMsg(perferredDevices);
54
55 int32_t mapSize = static_cast<int32_t>(perferredDevices.size());
56 reply.WriteInt32(mapSize);
57 for (auto &it : perferredDevices) {
58 reply.WriteInt32(static_cast<int32_t>(it.first));
59 reply.WriteInt32(it.second->deviceType_);
60 reply.WriteString(it.second->deviceName_);
61 reply.WriteString(it.second->address_);
62 reply.WriteInt32(it.second->deviceCategory_);
63 reply.WriteInt32(it.second->usageOrSourceType_);
64 }
65 reply.WriteInt32(ret);
66 MEDIA_LOG_I("MediaMonitorStub::HandleGetAudioRouteMsg out");
67 }
68
HandleSetMediaParams(MessageParcel & data,MessageParcel & reply)69 void MediaMonitorStub::HandleSetMediaParams(MessageParcel &data, MessageParcel &reply)
70 {
71 std::string dumpType;
72 std::string dumpEnable;
73 if (data.ReadString(dumpType) == false) {
74 MEDIA_LOG_E("MediaMonitorStub::HandleSetMediaParams read type failed");
75 reply.WriteInt32(ERR_INVALID_PARAM);
76 return;
77 }
78 if (data.ReadString(dumpEnable) == false) {
79 MEDIA_LOG_E("MediaMonitorStub::HandleSetMediaParams read type failed");
80 reply.WriteInt32(ERR_INVALID_PARAM);
81 return;
82 }
83 int32_t ret = SetMediaParameters(dumpType, dumpEnable);
84 reply.WriteInt32(ret);
85 }
86
HandleGetInputBuffer(MessageParcel & data,MessageParcel & reply)87 void MediaMonitorStub::HandleGetInputBuffer(MessageParcel &data, MessageParcel &reply)
88 {
89 int32_t size;
90 std::shared_ptr<DumpBuffer> buffer = nullptr;
91 if (data.ReadInt32(size) == false) {
92 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer get size failed");
93 reply.WriteInt32(ERR_INVALID_PARAM);
94 return;
95 }
96 int32_t ret = GetInputBuffer(buffer, size);
97 if (buffer == nullptr) {
98 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer failed");
99 reply.WriteInt32(ret);
100 return;
101 }
102 if (dumpBufferWrap_ == nullptr) {
103 reply.WriteInt32(ERR_OPERATION_FAILED);
104 return;
105 }
106 void *replyPtr = reinterpret_cast<void *>(&reply);
107 if (dumpBufferWrap_->WriteToParcel(buffer.get(), replyPtr) == false) {
108 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer write data failed");
109 reply.WriteInt32(ERR_OPERATION_FAILED);
110 return;
111 }
112 reply.WriteInt32(ret);
113 }
114
HandleInputBufferFilled(MessageParcel & data,MessageParcel & reply)115 void MediaMonitorStub::HandleInputBufferFilled(MessageParcel &data, MessageParcel &reply)
116 {
117 std::string fileName;
118 uint64_t bufferId = 0;
119 int32_t size = 0;
120 if (data.ReadString(fileName) == false) {
121 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read name failed");
122 reply.WriteInt32(ERR_INVALID_PARAM);
123 return;
124 }
125 if (data.ReadUint64(bufferId) == false) {
126 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read id failed");
127 reply.WriteInt32(ERR_INVALID_PARAM);
128 return;
129 }
130
131 if (data.ReadInt32(size) == false) {
132 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read size failed");
133 reply.WriteInt32(ERR_INVALID_PARAM);
134 return;
135 }
136 int32_t ret = InputBufferFilled(fileName, bufferId, size);
137 reply.WriteInt32(ret);
138 }
139
HandleErasePreferredDeviceByType(MessageParcel & data,MessageParcel & reply)140 void MediaMonitorStub::HandleErasePreferredDeviceByType(MessageParcel &data, MessageParcel &reply)
141 {
142 int32_t preferredType;
143 if (data.ReadInt32(preferredType) == false) {
144 MEDIA_LOG_E("MediaMonitorStub::HandleErasePreferredDeviceByType read type failed");
145 reply.WriteInt32(ERR_INVALID_PARAM);
146 return;
147 }
148 int32_t ret = ErasePreferredDeviceByType(static_cast<PerferredType>(preferredType));
149 reply.WriteInt32(ret);
150 }
151 } // namespace MediaMonitor
152 } // namespace Media
153 } // namespace OHOS