Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Vongmasa | 255735a | 2017-07-19 11:24:56 -0700 | [diff] [blame] | 22 | #include <media/IResourceManagerService.h> |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 23 | |
| 24 | #include <binder/Parcel.h> |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | enum { |
| 32 | CONFIG = IBinder::FIRST_CALL_TRANSACTION, |
| 33 | ADD_RESOURCE, |
| 34 | REMOVE_RESOURCE, |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 35 | REMOVE_CLIENT, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 36 | RECLAIM_RESOURCE, |
| 37 | }; |
| 38 | |
| 39 | template <typename T> |
| 40 | static void writeToParcel(Parcel *data, const Vector<T> &items) { |
| 41 | size_t size = items.size(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 42 | // 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 Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 45 | items[i].writeToParcel(data); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | template <typename T> |
| 50 | static void readFromParcel(const Parcel &data, Vector<T> *items) { |
| 51 | size_t size = (size_t)data.readUint32(); |
Ronghua Wu | d66ef45 | 2015-05-27 09:46:44 -0700 | [diff] [blame] | 52 | for (size_t i = 0; i < size && data.dataAvail() > 0; i++) { |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 53 | T item; |
| 54 | item.readFromParcel(data); |
| 55 | items->add(item); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | class BpResourceManagerService : public BpInterface<IResourceManagerService> |
| 60 | { |
| 61 | public: |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 62 | explicit BpResourceManagerService(const sp<IBinder> &impl) |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 63 | : 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 Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 76 | int uid, |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 77 | 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 Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 83 | data.writeInt32(uid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 84 | data.writeInt64(clientId); |
| 85 | data.writeStrongBinder(IInterface::asBinder(client)); |
| 86 | writeToParcel(&data, resources); |
| 87 | |
| 88 | remote()->transact(ADD_RESOURCE, data, &reply); |
| 89 | } |
| 90 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 91 | 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 Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 102 | Parcel data, reply; |
| 103 | data.writeInterfaceToken(IResourceManagerService::getInterfaceDescriptor()); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 104 | data.writeInt32(pid); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 105 | data.writeInt64(clientId); |
| 106 | |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 107 | remote()->transact(REMOVE_CLIENT, data, &reply); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 108 | } |
| 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 | |
| 125 | IMPLEMENT_META_INTERFACE(ResourceManagerService, "android.media.IResourceManagerService"); |
| 126 | |
| 127 | // ---------------------------------------------------------------------- |
| 128 | |
| 129 | |
| 130 | status_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 Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 136 | 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 Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 145 | int uid = data.readInt32(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 146 | int64_t clientId = data.readInt64(); |
| 147 | sp<IResourceManagerClient> client( |
| 148 | interface_cast<IResourceManagerClient>(data.readStrongBinder())); |
Wei Jia | 2afac0c | 2016-01-07 12:13:07 -0800 | [diff] [blame] | 149 | if (client == NULL) { |
| 150 | return NO_ERROR; |
| 151 | } |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 152 | Vector<MediaResource> resources; |
| 153 | readFromParcel(data, &resources); |
Chong Zhang | ee33d64 | 2019-08-08 14:26:43 -0700 | [diff] [blame] | 154 | addResource(pid, uid, clientId, client, resources); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 155 | return NO_ERROR; |
| 156 | } break; |
| 157 | |
| 158 | case REMOVE_RESOURCE: { |
| 159 | CHECK_INTERFACE(IResourceManagerService, data, reply); |
Ronghua Wu | 37c8924 | 2015-07-15 12:23:48 -0700 | [diff] [blame] | 160 | int pid = data.readInt32(); |
Ronghua Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 161 | int64_t clientId = data.readInt64(); |
Chong Zhang | fb092d3 | 2019-08-12 09:45:44 -0700 | [diff] [blame] | 162 | 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 Wu | 231c3d1 | 2015-03-11 15:10:32 -0700 | [diff] [blame] | 173 | 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 |