1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 "monitor_service_stub.h"
17 #include "monitor_server.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "ipc_skeleton.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MonitorServiceStub"};
24 }
25
26 namespace OHOS {
27 namespace Media {
MonitorServiceStub()28 MonitorServiceStub::MonitorServiceStub()
29 {
30 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
31 }
32
~MonitorServiceStub()33 MonitorServiceStub::~MonitorServiceStub()
34 {
35 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
36 }
37
GetInstance()38 sptr<MonitorServiceStub> MonitorServiceStub::GetInstance()
39 {
40 static sptr<MonitorServiceStub> monitor = nullptr;
41 if (monitor == nullptr) {
42 monitor = new(std::nothrow) MonitorServiceStub();
43 (void)monitor->Init();
44 }
45 return monitor;
46 }
47
Init()48 int32_t MonitorServiceStub::Init()
49 {
50 monitorFuncs_[MONITOR_CLICK] = &MonitorServiceStub::Click;
51 monitorFuncs_[MONITOR_ENABLE] = &MonitorServiceStub::EnableMonitor;
52 monitorFuncs_[MONITOR_DISABLE] = &MonitorServiceStub::DisableMonitor;
53 return MSERR_OK;
54 }
55
DumpInfo(int32_t fd,bool needDetail)56 int32_t MonitorServiceStub::DumpInfo(int32_t fd, bool needDetail)
57 {
58 return MonitorServer::GetInstance().Dump(fd, needDetail);
59 }
60
OnClientDie(int32_t pid)61 int32_t MonitorServiceStub::OnClientDie(int32_t pid)
62 {
63 return MonitorServer::GetInstance().OnClientDie(pid);
64 }
65
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)66 int MonitorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
67 MessageOption &option)
68 {
69 MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}d is received", code);
70
71 auto remoteDescriptor = data.ReadInterfaceToken();
72 CHECK_AND_RETURN_RET_LOG(MonitorServiceStub::GetDescriptor() == remoteDescriptor,
73 MSERR_INVALID_OPERATION, "Invalid descriptor");
74
75 auto itFunc = monitorFuncs_.find(code);
76 CHECK_AND_RETURN_RET_LOG(itFunc != monitorFuncs_.end(), IPCObjectStub::OnRemoteRequest(code, data, reply, option),
77 "MonitorServiceStub: no member func supporting, applying default process");
78
79 auto memberFunc = itFunc->second;
80 CHECK_AND_RETURN_RET_LOG(memberFunc != nullptr, IPCObjectStub::OnRemoteRequest(code, data, reply, option),
81 "MonitorServiceStub: no member func supporting, applying default process");
82
83 int32_t ret = (this->*memberFunc)(data, reply);
84 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_OK, "calling memberFunc is failed.");
85 return MSERR_OK;
86 }
87
Click()88 int32_t MonitorServiceStub::Click()
89 {
90 int32_t pid = IPCSkeleton::GetCallingPid();
91 return MonitorServer::GetInstance().Click(pid);
92 }
93
EnableMonitor()94 int32_t MonitorServiceStub::EnableMonitor()
95 {
96 int32_t pid = IPCSkeleton::GetCallingPid();
97 return MonitorServer::GetInstance().EnableMonitor(pid);
98 }
99
DisableMonitor()100 int32_t MonitorServiceStub::DisableMonitor()
101 {
102 int32_t pid = IPCSkeleton::GetCallingPid();
103 return MonitorServer::GetInstance().DisableMonitor(pid);
104 }
105
Click(MessageParcel & data,MessageParcel & reply)106 int32_t MonitorServiceStub::Click(MessageParcel &data, MessageParcel &reply)
107 {
108 (void)data;
109 reply.WriteInt32(Click());
110 return MSERR_OK;
111 }
112
EnableMonitor(MessageParcel & data,MessageParcel & reply)113 int32_t MonitorServiceStub::EnableMonitor(MessageParcel &data, MessageParcel &reply)
114 {
115 (void)data;
116 reply.WriteInt32(EnableMonitor());
117 return MSERR_OK;
118 }
119
DisableMonitor(MessageParcel & data,MessageParcel & reply)120 int32_t MonitorServiceStub::DisableMonitor(MessageParcel &data, MessageParcel &reply)
121 {
122 (void)data;
123 reply.WriteInt32(DisableMonitor());
124 return MSERR_OK;
125 }
126 } // namespace Media
127 } // namespace OHOS
128