blob: 00f9b88aadd39add957ffc1a2a765c6eb1ae9b7f [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,
35 RECLAIM_RESOURCE,
36};
37
38template <typename T>
39static void writeToParcel(Parcel *data, const Vector<T> &items) {
40 size_t size = items.size();
Ronghua Wu231c3d12015-03-11 15:10:32 -070041 // truncates size, but should be okay for this usecase
42 data->writeUint32(static_cast<uint32_t>(size));
43 for (size_t i = 0; i < size; i++) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070044 items[i].writeToParcel(data);
45 }
46}
47
48template <typename T>
49static void readFromParcel(const Parcel &data, Vector<T> *items) {
50 size_t size = (size_t)data.readUint32();
Ronghua Wud66ef452015-05-27 09:46:44 -070051 for (size_t i = 0; i < size && data.dataAvail() > 0; i++) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070052 T item;
53 item.readFromParcel(data);
54 items->add(item);
55 }
56}
57
58class BpResourceManagerService : public BpInterface<IResourceManagerService>
59{
60public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070061 explicit BpResourceManagerService(const sp<IBinder> &impl)
Ronghua Wu231c3d12015-03-11 15:10:32 -070062 : BpInterface<IResourceManagerService>(impl)
63 {
64 }
65
66 virtual void config(const Vector<MediaResourcePolicy> &policies) {
67 Parcel data, reply;
68 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
69 writeToParcel(&data, policies);
70 remote()->transact(CONFIG, data, &reply);
71 }
72
73 virtual void addResource(
74 int pid,
Chong Zhangee33d642019-08-08 14:26:43 -070075 int uid,
Ronghua Wu231c3d12015-03-11 15:10:32 -070076 int64_t clientId,
77 const sp<IResourceManagerClient> client,
78 const Vector<MediaResource> &resources) {
79 Parcel data, reply;
80 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
81 data.writeInt32(pid);
Chong Zhangee33d642019-08-08 14:26:43 -070082 data.writeInt32(uid);
Ronghua Wu231c3d12015-03-11 15:10:32 -070083 data.writeInt64(clientId);
84 data.writeStrongBinder(IInterface::asBinder(client));
85 writeToParcel(&data, resources);
86
87 remote()->transact(ADD_RESOURCE, data, &reply);
88 }
89
Ronghua Wu37c89242015-07-15 12:23:48 -070090 virtual void removeResource(int pid, int64_t clientId) {
Ronghua Wu231c3d12015-03-11 15:10:32 -070091 Parcel data, reply;
92 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
Ronghua Wu37c89242015-07-15 12:23:48 -070093 data.writeInt32(pid);
Ronghua Wu231c3d12015-03-11 15:10:32 -070094 data.writeInt64(clientId);
95
96 remote()->transact(REMOVE_RESOURCE, data, &reply);
97 }
98
99 virtual bool reclaimResource(int callingPid, const Vector<MediaResource> &resources) {
100 Parcel data, reply;
101 data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor());
102 data.writeInt32(callingPid);
103 writeToParcel(&data, resources);
104
105 bool ret = false;
106 status_t status = remote()->transact(RECLAIM_RESOURCE, data, &reply);
107 if (status == NO_ERROR) {
108 ret = (bool)reply.readInt32();
109 }
110 return ret;
111 }
112};
113
114IMPLEMENT_META_INTERFACE(ResourceManagerService, "android.media.IResourceManagerService");
115
116// ----------------------------------------------------------------------
117
118
119status_t BnResourceManagerService::onTransact(
120 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags)
121{
122 switch (code) {
123 case CONFIG: {
124 CHECK_INTERFACE(IResourceManagerService, data, reply);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700125 Vector<MediaResourcePolicy> policies;
126 readFromParcel(data, &policies);
127 config(policies);
128 return NO_ERROR;
129 } break;
130
131 case ADD_RESOURCE: {
132 CHECK_INTERFACE(IResourceManagerService, data, reply);
133 int pid = data.readInt32();
Chong Zhangee33d642019-08-08 14:26:43 -0700134 int uid = data.readInt32();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700135 int64_t clientId = data.readInt64();
136 sp<IResourceManagerClient> client(
137 interface_cast<IResourceManagerClient>(data.readStrongBinder()));
Wei Jia2afac0c2016-01-07 12:13:07 -0800138 if (client == NULL) {
139 return NO_ERROR;
140 }
Ronghua Wu231c3d12015-03-11 15:10:32 -0700141 Vector<MediaResource> resources;
142 readFromParcel(data, &resources);
Chong Zhangee33d642019-08-08 14:26:43 -0700143 addResource(pid, uid, clientId, client, resources);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700144 return NO_ERROR;
145 } break;
146
147 case REMOVE_RESOURCE: {
148 CHECK_INTERFACE(IResourceManagerService, data, reply);
Ronghua Wu37c89242015-07-15 12:23:48 -0700149 int pid = data.readInt32();
Ronghua Wu231c3d12015-03-11 15:10:32 -0700150 int64_t clientId = data.readInt64();
Ronghua Wu37c89242015-07-15 12:23:48 -0700151 removeResource(pid, clientId);
Ronghua Wu231c3d12015-03-11 15:10:32 -0700152 return NO_ERROR;
153 } break;
154
155 case RECLAIM_RESOURCE: {
156 CHECK_INTERFACE(IResourceManagerService, data, reply);
157 int callingPid = data.readInt32();
158 Vector<MediaResource> resources;
159 readFromParcel(data, &resources);
160 bool ret = reclaimResource(callingPid, resources);
161 reply->writeInt32(ret);
162 return NO_ERROR;
163 } break;
164
165 default:
166 return BBinder::onTransact(code, data, reply, flags);
167 }
168}
169
170// ----------------------------------------------------------------------------
171
172}; // namespace android