blob: f8a0a143e15891588d303ca89ed8d3031db4fed5 [file] [log] [blame]
Ronghua Wu231c3d12015-03-11 15:10:32 -07001/*
2**
3** Copyright 2015, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "IResourceManagerService"
20#include <utils/Log.h>
21
Pawin Vongmasa255735a2017-07-19 11:24:56 -070022#include <media/IResourceManagerService.h>
Ronghua Wu231c3d12015-03-11 15:10:32 -070023
24#include <binder/Parcel.h>
25
26#include <stdint.h>
27#include <sys/types.h>
28
29namespace android {
30
31enum {
32 CONFIG = IBinder::FIRST_CALL_TRANSACTION,
33 ADD_RESOURCE,
34 REMOVE_RESOURCE,
Chong Zhangfb092d32019-08-12 09:45:44 -070035 REMOVE_CLIENT,
Ronghua Wu231c3d12015-03-11 15:10:32 -070036 RECLAIM_RESOURCE,
37};
38
39template <typename T>
40static void writeToParcel(Parcel *data, const Vector<T> &items) {
41 size_t size = items.size();
Ronghua Wu231c3d12015-03-11 15:10:32 -070042 // truncates size, but should be okay for this usecase
43 data->writeUint32(static_cast<uint32_t>(size));
44 for (size_t i = 0; i < size; i++) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070045 items[i].writeToParcel(data);
46 }
47}
48
49template <typename T>
50static void readFromParcel(const Parcel &data, Vector<T> *items) {
51 size_t size = (size_t)data.readUint32();
Ronghua Wud66ef452015-05-27 09:46:44 -070052 for (size_t i = 0; i < size && data.dataAvail() > 0; i++) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070053 T item;
54 item.readFromParcel(data);
55 items->add(item);
56 }
57}
58
59class BpResourceManagerService : public BpInterface<IResourceManagerService>
60{
61public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070062 explicit BpResourceManagerService(const sp<IBinder> &impl)
Ronghua Wu231c3d12015-03-11 15:10:32 -070063 : BpInterface<IResourceManagerService>(impl)
64 {
65 }
66
67 virtual void config(const Vector<MediaResourcePolicy> &policies) {
68 Parcel data, reply;
69 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
70 writeToParcel(&data, policies);
71 remote()->transact(CONFIG, data, &reply);
72 }
73
74 virtual void addResource(
75 int pid,
Chong Zhangee33d642019-08-08 14:26:43 -070076 int uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -070077 int64_t clientId,
78 const sp<IResourceManagerClient> client,
79 const Vector<MediaResource> &resources) {
80 Parcel data, reply;
81 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
82 data.writeInt32(pid);
Chong Zhangee33d642019-08-08 14:26:43 -070083 data.writeInt32(uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -070084 data.writeInt64(clientId);
85 data.writeStrongBinder(IInterface::asBinder(client));
86 writeToParcel(&data, resources);
87
88 remote()->transact(ADD_RESOURCE, data, &reply);
89 }
90
Chong Zhangfb092d32019-08-12 09:45:44 -070091 virtual void removeResource(int pid, int64_t clientId, const Vector<MediaResource> &resources) {
92 Parcel data, reply;
93 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
94 data.writeInt32(pid);
95 data.writeInt64(clientId);
96 writeToParcel(&data, resources);
97
98 remote()->transact(REMOVE_RESOURCE, data, &reply);
99 }
100
101 virtual void removeClient(int pid, int64_t clientId) {
Ronghua Wu231c3d12015-03-11 15:10:32 -0700102 Parcel data, reply;
103 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
Ronghua Wu37c89242015-07-15 12:23:48 -0700104 data.writeInt32(pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700105 data.writeInt64(clientId);
106
Chong Zhangfb092d32019-08-12 09:45:44 -0700107 remote()->transact(REMOVE_CLIENT, data, &reply);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700108 }
109
110 virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources) {
111 Parcel data, reply;
112 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
113 data.writeInt32(callingPid);
114 writeToParcel(&data, resources);
115
116 bool ret = false;
117 status_t status = remote()->transact(RECLAIM_RESOURCE, data, &reply);
118 if (status == NO_ERROR) {
119 ret = (bool)reply.readInt32();
120 }
121 return ret;
122 }
123};
124
125IMPLEMENT_META_INTERFACE(ResourceManagerService, "android.media.IResourceManagerService");
126
127// ----------------------------------------------------------------------
128
129
130status_t BnResourceManagerService::onTransact(
131 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags)
132{
133 switch (code) {
134 case CONFIG: {
135 CHECK_INTERFACE(IResourceManagerService, data, reply);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700136 Vector<MediaResourcePolicy> policies;
137 readFromParcel(data, &policies);
138 config(policies);
139 return NO_ERROR;
140 } break;
141
142 case ADD_RESOURCE: {
143 CHECK_INTERFACE(IResourceManagerService, data, reply);
144 int pid = data.readInt32();
Chong Zhangee33d642019-08-08 14:26:43 -0700145 int uid = data.readInt32();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700146 int64_t clientId = data.readInt64();
147 sp<IResourceManagerClient> client(
148 interface_cast<IResourceManagerClient>(data.readStrongBinder()));
Wei Jia2afac0c2016-01-07 12:13:07 -0800149 if (client == NULL) {
150 return NO_ERROR;
151 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700152 Vector<MediaResource> resources;
153 readFromParcel(data, &resources);
Chong Zhangee33d642019-08-08 14:26:43 -0700154 addResource(pid, uid, clientId, client, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700155 return NO_ERROR;
156 } break;
157
158 case REMOVE_RESOURCE: {
159 CHECK_INTERFACE(IResourceManagerService, data, reply);
Ronghua Wu37c89242015-07-15 12:23:48 -0700160 int pid = data.readInt32();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700161 int64_t clientId = data.readInt64();
Chong Zhangfb092d32019-08-12 09:45:44 -0700162 Vector<MediaResource> resources;
163 readFromParcel(data, &resources);
164 removeResource(pid, clientId, resources);
165 return NO_ERROR;
166 } break;
167
168 case REMOVE_CLIENT: {
169 CHECK_INTERFACE(IResourceManagerService, data, reply);
170 int pid = data.readInt32();
171 int64_t clientId = data.readInt64();
172 removeClient(pid, clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700173 return NO_ERROR;
174 } break;
175
176 case RECLAIM_RESOURCE: {
177 CHECK_INTERFACE(IResourceManagerService, data, reply);
178 int callingPid = data.readInt32();
179 Vector<MediaResource> resources;
180 readFromParcel(data, &resources);
181 bool ret = reclaimResource(callingPid, resources);
182 reply->writeInt32(ret);
183 return NO_ERROR;
184 } break;
185
186 default:
187 return BBinder::onTransact(code, data, reply, flags);
188 }
189}
190
191// ----------------------------------------------------------------------------
192
193}; // namespace android