1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hdifd_parcelable.h"
17 #include <unistd.h>
18 #include <sstream>
19 #include "securec.h"
20 #include "hdf_log.h"
21 #include "hilog/log.h"
22 #include "ipc_file_descriptor.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "DISP_HDIFD"
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN 0xD002515
28
29 namespace OHOS {
30 namespace HDI {
31 namespace Display {
HdifdParcelable()32 HdifdParcelable::HdifdParcelable()
33 : isOwner_(false), hdiFd_(-1)
34 {
35 }
36
HdifdParcelable(int32_t fd)37 HdifdParcelable::HdifdParcelable(int32_t fd)
38 : isOwner_(true), hdiFd_(fd)
39 {
40 }
41
~HdifdParcelable()42 HdifdParcelable::~HdifdParcelable()
43 {
44 if (isOwner_ && (hdiFd_ >= 0)) {
45 close(hdiFd_);
46 }
47 }
48
Init(int32_t fd)49 bool HdifdParcelable::Init(int32_t fd)
50 {
51 bool ret = true;
52
53 if (isOwner_) {
54 HDF_LOGI("%{public}s: fd parcelable have been initialized", __func__);
55 ret = false;
56 } else {
57 if (fd < 0) {
58 hdiFd_ = -1;
59 } else {
60 hdiFd_ = dup(fd);
61 ret = (hdiFd_ < 0) ? false : true;
62 }
63 if (!ret) {
64 return ret;
65 }
66 isOwner_ = true;
67 }
68 return ret;
69 }
70
WriteFileDescriptor(const int fd,Parcel & parcel)71 bool HdifdParcelable::WriteFileDescriptor(const int fd, Parcel& parcel)
72 {
73 if (fd < 0) {
74 return false;
75 }
76 int dupFd = dup(fd);
77 if (dupFd < 0) {
78 return false;
79 }
80
81 sptr<IPCFileDescriptor> descriptor(new (std::nothrow) IPCFileDescriptor(dupFd));
82 if (descriptor == nullptr) {
83 HDF_LOGE("%{public}s: create IPCFileDescriptor object failed", __func__);
84 close(dupFd);
85 return false;
86 }
87 bool ret = parcel.WriteObject<IPCFileDescriptor>(descriptor);
88 if (!ret) {
89 HDF_LOGE("%{public}s: WriteObject IPCFileDescriptor failed", __func__);
90 close(dupFd);
91 return false;
92 }
93 return ret;
94 }
95
ReadFileDescriptor(Parcel & parcel)96 int HdifdParcelable::ReadFileDescriptor(Parcel& parcel)
97 {
98 sptr<IPCFileDescriptor> descriptor = parcel.ReadObject<IPCFileDescriptor>();
99 if (descriptor == nullptr) {
100 return -1;
101 }
102 int fd = descriptor->GetFd();
103 if (fd < 0) {
104 return -1;
105 }
106 return dup(fd);
107 }
108
Marshalling(Parcel & parcel) const109 bool HdifdParcelable::Marshalling(Parcel& parcel) const
110 {
111 bool validFlag = (hdiFd_ >= 0);
112 if (!parcel.WriteBool(validFlag)) {
113 HDF_LOGE("%{public}s: parcel.WriteBool failed", __func__);
114 return false;
115 }
116 if (validFlag && !WriteFileDescriptor(hdiFd_, parcel)) {
117 HDF_LOGE("%{public}s: parcel.WriteFileDescriptor fd failed", __func__);
118 return false;
119 }
120 return true;
121 }
122
Unmarshalling(Parcel & parcel)123 sptr<HdifdParcelable> HdifdParcelable::Unmarshalling(Parcel& parcel)
124 {
125 bool validFlag = false;
126 if (!parcel.ReadBool(validFlag)) {
127 HDF_LOGE("%{public}s: ReadBool validFlag failed", __func__);
128 return sptr<HdifdParcelable>();
129 }
130 int32_t fd = -1;
131 if (validFlag) {
132 fd = ReadFileDescriptor(parcel);
133 if (fd < 0) {
134 HDF_LOGE("%{public}s: ReadFileDescriptor fd failed", __func__);
135 return sptr<HdifdParcelable>();
136 }
137 }
138 sptr<HdifdParcelable> newParcelable(new HdifdParcelable(fd));
139
140 if (newParcelable == nullptr) {
141 HDF_LOGE("%{public}s: new HdifdParcelable failed", __func__);
142 if (fd >= 0) {
143 close(fd);
144 }
145 return sptr<HdifdParcelable>();
146 }
147 return newParcelable;
148 }
149
GetFd()150 int32_t HdifdParcelable::GetFd()
151 {
152 return hdiFd_;
153 }
154
Move()155 int32_t HdifdParcelable::Move()
156 {
157 isOwner_ = false;
158 return hdiFd_;
159 }
160
Dump() const161 std::string HdifdParcelable::Dump() const
162 {
163 std::stringstream os;
164 os << "fd: {" << hdiFd_ << "}\n";
165 return os.str();
166 }
167 } // Display
168 } // HDI
169 } // OHOS
170