1 /*
2  * Copyright (C) 2023 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 "tests/simple_parcelable_ndk.h"
18 
19 #include <android-base/stringprintf.h>
20 #include <android/binder_parcel.h>
21 #include <android/binder_parcel_utils.h>
22 
23 using android::base::StringPrintf;
24 using ndk::AParcel_readString;
25 using ndk::AParcel_writeString;
26 
27 namespace aidl {
28 namespace android {
29 namespace aidl {
30 namespace tests {
31 
SimpleParcelable(const std::string & name,int32_t number)32 SimpleParcelable::SimpleParcelable(const std::string& name, int32_t number)
33     : name_(name), number_(number) {}
34 
writeToParcel(AParcel * _Nonnull parcel) const35 binder_status_t SimpleParcelable::writeToParcel(AParcel* _Nonnull parcel) const {
36   binder_status_t status = AParcel_writeString(parcel, name_);
37   if (status != STATUS_OK) {
38     return status;
39   }
40   status = AParcel_writeInt32(parcel, number_);
41   return status;
42 }
43 
readFromParcel(const AParcel * _Nonnull parcel)44 binder_status_t SimpleParcelable::readFromParcel(const AParcel* _Nonnull parcel) {
45   binder_status_t status = AParcel_readString(parcel, &name_);
46   if (status != STATUS_OK) {
47     return status;
48   }
49   return AParcel_readInt32(parcel, &number_);
50 }
51 
toString() const52 std::string SimpleParcelable::toString() const {
53   return StringPrintf("%s(%d)", name_.c_str(), number_);
54 }
55 
56 }  // namespace tests
57 }  // namespace aidl
58 }  // namespace android
59 }  // namespace aidl
60