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 #ifndef CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_LARGEPARCELABLE_H_
18 #define CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_LARGEPARCELABLE_H_
19 
20 #include "LargeParcelableBase.h"
21 #include "SharedMemory.h"
22 
23 #include <android/binder_parcel.h>
24 #include <android/binder_parcel_utils.h>
25 #include <android/binder_status.h>
26 
27 #include <memory>
28 #include <optional>
29 
30 namespace android {
31 namespace automotive {
32 namespace car_binder_lib {
33 
34 // This class allows a stable AIDL parcelable to be marshalled to a shared memory file if its
35 // serialized parcel exceeds binder limitation.
36 template <class T>
37 class LargeParcelable : public LargeParcelableBase {
38 public:
LargeParcelable()39     LargeParcelable() {}
40 
41     // Use an existing stable AIDL parcelable to initiate this large parcelable. The input
42     // parcelable's marshal/unmarshal method would be used to read/write from a shared memory
43     // file if needed.
44     //
45     // T must be a Parcelable.
LargeParcelable(std::unique_ptr<T> parcelable)46     explicit LargeParcelable(std::unique_ptr<T> parcelable) : mParcelable(std::move(parcelable)) {}
47 
48     // Get the stable AIDL parcelable object. Caller is supposed to use 'LargeParcelable' class to
49     // parse data from 'AParcel' and then use 'getParcelable' to get the underlying parsed regular
50     // parcelable object.
getParcelable()51     inline const std::optional<const T*> getParcelable() const {
52         if (!hasDeserializedParcelable() || mParcelable == nullptr) {
53             return std::nullopt;
54         }
55         return mParcelable.get();
56     }
57 
58 protected:
59     // Serialize this parcelable to 'dest'.
60     binder_status_t serialize(AParcel* dest) const override;
61 
62     // Serialize a NULL parcelable for 'this' class to 'dest'.
63     binder_status_t serializeNullPayload(AParcel* dest) const override;
64 
65     // Read a 'Parcelable' from the given AParcel. The src might be parsed to a NULL parcelable.
66     binder_status_t deserialize(const AParcel& src) override;
67 
68 private:
69     std::unique_ptr<T> mParcelable;
70 
71     // Serialize a nullable Parcelabel 'payload' to 'dest'.
72     static binder_status_t serializeNullablePayload(const T* payload, AParcel* dest);
73 };
74 
75 template <class T>
serializeNullablePayload(const T * payload,AParcel * dest)76 binder_status_t LargeParcelable<T>::serializeNullablePayload(const T* payload, AParcel* dest) {
77     int32_t startPosition;
78     if (DBG_PAYLOAD) {
79         startPosition = AParcel_getDataPosition(dest);
80     }
81     if (payload == nullptr) {
82         // Write a null parcelable.
83         if (binder_status_t status =
84                     ::ndk::AParcel_writeNullableParcelable(dest, std::optional<T>(std::nullopt));
85             status != STATUS_OK) {
86             ALOGE("failed to write null parcelable to parcel, status: %d", status);
87             return status;
88         }
89     } else {
90         if (binder_status_t status = ::ndk::AParcel_writeParcelable(dest, *payload);
91             status != STATUS_OK) {
92             ALOGE("failed to write parcelable to parcel, status: %d", status);
93             return status;
94         }
95     }
96 
97     if (DBG_PAYLOAD) {
98         ALOGD("serialize-payload, start:%d size: %d", startPosition,
99               (AParcel_getDataPosition(dest) - startPosition));
100     }
101     return STATUS_OK;
102 }
103 
104 template <class T>
serialize(AParcel * dest)105 binder_status_t LargeParcelable<T>::serialize(AParcel* dest) const {
106     return serializeNullablePayload(mParcelable.get(), dest);
107 }
108 
109 template <class T>
serializeNullPayload(AParcel * dest)110 binder_status_t LargeParcelable<T>::serializeNullPayload(AParcel* dest) const {
111     return serializeNullablePayload(nullptr, dest);
112 }
113 
114 template <class T>
deserialize(const AParcel & src)115 binder_status_t LargeParcelable<T>::deserialize(const AParcel& src) {
116     int32_t startPosition = AParcel_getDataPosition(&src);
117     if (DBG_PAYLOAD) {
118         ALOGD("start position: %d", startPosition);
119     }
120     std::optional<T> parcelable;
121     if (binder_status_t status = ::ndk::AParcel_readNullableParcelable(&src, &parcelable);
122         status != OK) {
123         ALOGE("failed to read parcelable from parcel, status: %d", status);
124         return status;
125     }
126     int32_t size = (AParcel_getDataPosition(&src) - startPosition);
127     if (!parcelable.has_value()) {
128         if (DBG_PAYLOAD) {
129             ALOGD("deserialize-payload: null parcelable, start: %d, size: %d", startPosition, size);
130         }
131         mParcelable = nullptr;
132         return STATUS_OK;
133     }
134     mParcelable = std::make_unique<T>(std::move(parcelable.value()));
135     if (DBG_PAYLOAD) {
136         ALOGD("deserialize-payload, start: %d, size: %d", startPosition, size);
137     }
138     return STATUS_OK;
139 }
140 
141 }  // namespace car_binder_lib
142 }  // namespace automotive
143 }  // namespace android
144 
145 #endif  // CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_LARGEPARCELABLE_H_
146