1/*
2 * Copyright (C) 2018 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 "Connection.h"
18
19namespace android {
20namespace hardware {
21namespace media {
22namespace bufferpool {
23namespace V1_0 {
24namespace implementation {
25
26// Methods from ::android::hardware::media::bufferpool::V1_0::IConnection follow.
27Return<void> Connection::fetch(uint64_t transactionId, uint32_t bufferId, fetch_cb _hidl_cb) {
28    ResultStatus status = ResultStatus::CRITICAL_ERROR;
29    if (mInitialized && mAccessor) {
30        const native_handle_t *handle = NULL;
31        status = mAccessor->fetch(
32                mConnectionId, transactionId, bufferId, &handle);
33        if (status == ResultStatus::OK) {
34            _hidl_cb(status, Buffer{bufferId, handle});
35            return Void();
36        }
37    }
38    _hidl_cb(status, Buffer{0, nullptr});
39    return Void();
40}
41
42Connection::Connection() : mInitialized(false), mConnectionId(-1LL) {}
43
44Connection::~Connection() {
45    if (mInitialized && mAccessor) {
46        mAccessor->close(mConnectionId);
47    }
48}
49
50void Connection::initialize(
51        const sp<Accessor>& accessor, ConnectionId connectionId) {
52    if (!mInitialized) {
53        mAccessor = accessor;
54        mConnectionId = connectionId;
55        mInitialized = true;
56    }
57}
58
59ResultStatus Connection::allocate(
60        const std::vector<uint8_t> &params, BufferId *bufferId,
61        const native_handle_t **handle) {
62    if (mInitialized && mAccessor) {
63        return mAccessor->allocate(mConnectionId, params, bufferId, handle);
64    }
65    return ResultStatus::CRITICAL_ERROR;
66}
67
68void Connection::cleanUp(bool clearCache) {
69    if (mInitialized && mAccessor) {
70        mAccessor->cleanUp(clearCache);
71    }
72}
73
74// Methods from ::android::hidl::base::V1_0::IBase follow.
75
76//IConnection* HIDL_FETCH_IConnection(const char* /* name */) {
77//    return new Connection();
78//}
79
80}  // namespace implementation
81}  // namespace V1_0
82}  // namespace bufferpool
83}  // namespace media
84}  // namespace hardware
85}  // namespace android
86