1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <binder/IServiceManager.h>
18 #include <binder/Parcel.h>
19 
20 #include "../includes/common.h"
21 
22 using namespace android;
23 
24 typedef struct ThreadParams {
25   sp<IBinder> service;
26 } ThreadParams;
27 
thread_getParameter(void * p)28 static void *thread_getParameter(void *p) {
29   ThreadParams *params = (ThreadParams *)p;
30   int err;
31   time_t currentTime = start_timer();
32   while (timer_active(currentTime)) {
33     Parcel data, reply;
34     data.writeInterfaceToken(params->service->getInterfaceDescriptor());
35     int key = (('m') << 24 | ('t') << 16 | ('r') << 8 | ('X'));
36     data.writeInt32(key);
37     err = params->service->transact(/*GET_PARAMETER_ID*/ 31, data, &reply, 0);
38     if (err == EPIPE) {
39       break;
40     }
41     usleep(5000);
42   }
43   return nullptr;
44 }
45 
main()46 int main() {
47   status_t err;
48   sp<IServiceManager> sm = defaultServiceManager();
49   String16 name(String16("media.player"));
50   sp<IBinder> service = sm->checkService(name);
51   sp<IBinder> binder = nullptr;
52   if (not service) {
53     return EXIT_FAILURE;
54   }
55 
56   String16 interface_name = service->getInterfaceDescriptor();
57   Parcel data, reply;
58   data.writeInterfaceToken(interface_name);
59   data.writeStrongBinder(new BBinder());
60   for (int i = 0; i < 1024; ++i)
61     data.writeInt32(1);
62   if (service) {
63     err = service->transact(/*CREATE_ID*/ 1, data, &reply, 0);
64     binder = reply.readStrongBinder();
65   }
66 
67   if (not binder) {
68     return EXIT_FAILURE;
69   }
70 
71   pthread_t t1, t2;
72 
73   ThreadParams *params = new ThreadParams();
74   params->service = binder;
75   pthread_create(&t1, nullptr, thread_getParameter, params);
76   pthread_create(&t2, nullptr, thread_getParameter, params);
77 
78   time_t currentTime = start_timer();
79   while (timer_active(currentTime)) {
80     if (not binder) {
81       break;
82     }
83     Parcel data, reply;
84     data.writeInterfaceToken(binder->getInterfaceDescriptor());
85     data.writeStrongBinder(binder);
86     err = binder->transact(/*SET_DATA_SOURCE_URL_ID*/ 2, data, &reply, 0);
87     if (err == EPIPE) {
88       break;
89     }
90     usleep(500000);
91   }
92 
93   pthread_join(t1, nullptr);
94   pthread_join(t2, nullptr);
95   delete params;
96   return EXIT_SUCCESS;
97 }
98