HidlBinderSupport.cpp revision 72db40f4a612d04187ae2fc1f9a3766933264bdf
17f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong/*
27f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Copyright (C) 2016 The Android Open Source Project
37f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong *
47f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License");
57f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * you may not use this file except in compliance with the License.
67f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * You may obtain a copy of the License at
77f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong *
87f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong *      http://www.apache.org/licenses/LICENSE-2.0
97f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong *
107f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * Unless required by applicable law or agreed to in writing, software
117f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS,
127f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * See the License for the specific language governing permissions and
147f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong * limitations under the License.
157f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong */
167f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
177f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#define LOG_TAG "HidlSupport"
187f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
197f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong#include <hidl/HidlBinderSupport.h>
207f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
21777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong// C includes
22777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong#include <unistd.h>
23777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong
24777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong// C++ includes
25777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong#include <fstream>
26777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong#include <sstream>
27777bef976bf218a2290b86d52c45dcff84a2f698Yifan Hong
287f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongnamespace android {
297f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongnamespace hardware {
307f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
313079100878a2595be644d866e67c12a9de620fd5Martijn Coenenconst size_t hidl_memory::kOffsetOfHandle = offsetof(hidl_memory, mHandle);
323079100878a2595be644d866e67c12a9de620fd5Martijn Coenenconst size_t hidl_memory::kOffsetOfName = offsetof(hidl_memory, mName);
336d4acb12519597610322097f86850d9e404d251aHridya Valsarajustatic_assert(hidl_memory::kOffsetOfHandle == 0, "wrong offset");
346d4acb12519597610322097f86850d9e404d251aHridya Valsarajustatic_assert(hidl_memory::kOffsetOfName == 24, "wrong offset");
353079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
363079100878a2595be644d866e67c12a9de620fd5Martijn Coenenstatus_t readEmbeddedFromParcel(hidl_memory * /* memory */,
373079100878a2595be644d866e67c12a9de620fd5Martijn Coenen        const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
381f535a27054e7553e968cac664981073f0074009Steven Moreland    const native_handle_t *handle;
391f535a27054e7553e968cac664981073f0074009Steven Moreland    ::android::status_t _hidl_err = parcel.readNullableEmbeddedNativeHandle(
403079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parentHandle,
411f535a27054e7553e968cac664981073f0074009Steven Moreland            parentOffset + hidl_memory::kOffsetOfHandle,
421f535a27054e7553e968cac664981073f0074009Steven Moreland            &handle);
433079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
441f535a27054e7553e968cac664981073f0074009Steven Moreland    if (_hidl_err == ::android::OK) {
451f535a27054e7553e968cac664981073f0074009Steven Moreland        _hidl_err = readEmbeddedFromParcel(
461f535a27054e7553e968cac664981073f0074009Steven Moreland                (hidl_string*) nullptr,
471f535a27054e7553e968cac664981073f0074009Steven Moreland                parcel,
481f535a27054e7553e968cac664981073f0074009Steven Moreland                parentHandle,
491f535a27054e7553e968cac664981073f0074009Steven Moreland                parentOffset + hidl_memory::kOffsetOfName);
503079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    }
513079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
523079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    return _hidl_err;
533079100878a2595be644d866e67c12a9de620fd5Martijn Coenen}
543079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
553079100878a2595be644d866e67c12a9de620fd5Martijn Coenenstatus_t writeEmbeddedToParcel(const hidl_memory &memory,
563079100878a2595be644d866e67c12a9de620fd5Martijn Coenen        Parcel *parcel, size_t parentHandle, size_t parentOffset) {
573079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    status_t _hidl_err = parcel->writeEmbeddedNativeHandle(
583079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            memory.handle(),
593079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parentHandle,
603079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parentOffset + hidl_memory::kOffsetOfHandle);
613079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
623079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    if (_hidl_err == ::android::OK) {
633079100878a2595be644d866e67c12a9de620fd5Martijn Coenen        _hidl_err = writeEmbeddedToParcel(
643079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            memory.name(),
653079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parcel,
663079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parentHandle,
673079100878a2595be644d866e67c12a9de620fd5Martijn Coenen            parentOffset + hidl_memory::kOffsetOfName);
683079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    }
693079100878a2595be644d866e67c12a9de620fd5Martijn Coenen
703079100878a2595be644d866e67c12a9de620fd5Martijn Coenen    return _hidl_err;
713079100878a2595be644d866e67c12a9de620fd5Martijn Coenen}
727f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong// static
737f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongconst size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
746d4acb12519597610322097f86850d9e404d251aHridya Valsarajustatic_assert(hidl_string::kOffsetOfBuffer == 0, "wrong offset");
757f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
767f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongstatus_t readEmbeddedFromParcel(hidl_string * /* string */,
777f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
781f535a27054e7553e968cac664981073f0074009Steven Moreland    const void *out;
791f535a27054e7553e968cac664981073f0074009Steven Moreland    return parcel.readEmbeddedBuffer(
807f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            nullptr /* buffer_handle */,
817f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            parentHandle,
821f535a27054e7553e968cac664981073f0074009Steven Moreland            parentOffset + hidl_string::kOffsetOfBuffer,
831f535a27054e7553e968cac664981073f0074009Steven Moreland            &out);
847f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
857f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
867f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongstatus_t writeEmbeddedToParcel(const hidl_string &string,
877f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        Parcel *parcel, size_t parentHandle, size_t parentOffset) {
887f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    return parcel->writeEmbeddedBuffer(
897f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            string.c_str(),
907f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            string.size() + 1,
917f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            nullptr /* handle */,
927f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            parentHandle,
937f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            parentOffset + hidl_string::kOffsetOfBuffer);
947f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
957f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
967f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongandroid::status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel) {
977f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    return parcel.writeUint32(static_cast<uint32_t>(version.get_major()) << 16 | version.get_minor());
987f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
997f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1007f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Honghidl_version* readFromParcel(const android::hardware::Parcel& parcel) {
1017f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    uint32_t version;
1027f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    android::status_t status = parcel.readUint32(&version);
1037f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (status != OK) {
1047f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return nullptr;
1057f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    } else {
1067f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return new hidl_version(version >> 16, version & 0xFFFF);
1077f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1087f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
1097f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1107f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongstatus_t readFromParcel(Status *s, const Parcel& parcel) {
1117f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    int32_t exception;
1127f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    status_t status = parcel.readInt32(&exception);
1137f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (status != OK) {
1147f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        s->setFromStatusT(status);
1157f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return status;
1167f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1177f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1187f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    // Skip over fat response headers.  Not used (or propagated) in native code.
1197f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (exception == Status::EX_HAS_REPLY_HEADER) {
1207f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        // Note that the header size includes the 4 byte size field.
1217f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        const int32_t header_start = parcel.dataPosition();
1227f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        int32_t header_size;
1237f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        status = parcel.readInt32(&header_size);
1247f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        if (status != OK) {
1257f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            s->setFromStatusT(status);
1267f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong            return status;
1277f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        }
1287f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        parcel.setDataPosition(header_start + header_size);
1297f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        // And fat response headers are currently only used when there are no
1307f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        // exceptions, so act like there was no error.
1317f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        exception = Status::EX_NONE;
1327f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1337f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1347f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (exception == Status::EX_NONE) {
1357f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        *s = Status::ok();
1367f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return status;
1377f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1387f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1397f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    // The remote threw an exception.  Get the message back.
1407f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    String16 message;
1417f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    status = parcel.readString16(&message);
1427f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (status != OK) {
1437f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        s->setFromStatusT(status);
1447f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return status;
1457f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1467f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
14772db40f4a612d04187ae2fc1f9a3766933264bdfSteven Moreland    s->setException(exception, String8(message));
1487f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1497f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    return status;
1507f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
1517f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1527f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hongstatus_t writeToParcel(const Status &s, Parcel* parcel) {
1537f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    // Something really bad has happened, and we're not going to even
1547f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    // try returning rich error data.
1557f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (s.exceptionCode() == Status::EX_TRANSACTION_FAILED) {
1567f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return s.transactionError();
1577f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1587f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1597f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    status_t status = parcel->writeInt32(s.exceptionCode());
1607f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (status != OK) { return status; }
1617f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    if (s.exceptionCode() == Status::EX_NONE) {
1627f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        // We have no more information to write.
1637f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong        return status;
1647f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    }
1657f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    status = parcel->writeString16(String16(s.exceptionMessage()));
1667f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong    return status;
1677f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}
1687f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong
1696cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Morelandvoid configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
1706cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland    ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/);
1716cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland}
1726cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland
1736cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Morelandvoid joinBinderRpcThreadpool() {
1746cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland    IPCThreadState::self()->joinThreadPool();
1756cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland}
1766cf8fa2d989bf57aed9a62fd9f54261176a9f860Steven Moreland
1777f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}  // namespace hardware
1787f97f44562b057c3c780c0a05c101b677f9b0f96Yifan Hong}  // namespace android
179