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 0xD002500
28
29 namespace OHOS {
30 namespace HDI {
31 namespace Display {
HdifdParcelable()32 HdifdParcelable::HdifdParcelable()
33 : init_(false), hdiFd_(-1)
34 {
35 }
36
HdifdParcelable(int32_t fd)37 HdifdParcelable::HdifdParcelable(int32_t fd)
38 : init_(true), hdiFd_(fd)
39 {
40 }
41
~HdifdParcelable()42 HdifdParcelable::~HdifdParcelable()
43 {
44 if ((init_ != false) && (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 (init_ == true) {
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 == false) {
64 return ret;
65 }
66 init_ = 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 return false;
85 }
86 return parcel.WriteObject<IPCFileDescriptor>(descriptor);
87 }
88
ReadFileDescriptor(Parcel & parcel)89 int HdifdParcelable::ReadFileDescriptor(Parcel& parcel)
90 {
91 sptr<IPCFileDescriptor> descriptor = parcel.ReadObject<IPCFileDescriptor>();
92 if (descriptor == nullptr) {
93 return -1;
94 }
95 int fd = descriptor->GetFd();
96 if (fd < 0) {
97 return -1;
98 }
99 return dup(fd);
100 }
101
Marshalling(Parcel & parcel) const102 bool HdifdParcelable::Marshalling(Parcel& parcel) const
103 {
104 bool validFlag = (hdiFd_ >= 0);
105 if (!parcel.WriteBool(validFlag)) {
106 HDF_LOGE("%{public}s: parcel.WriteBool failed", __func__);
107 return false;
108 }
109 if (validFlag && !WriteFileDescriptor(hdiFd_, parcel)) {
110 HDF_LOGE("%{public}s: parcel.WriteFileDescriptor fd failed", __func__);
111 return false;
112 }
113 return true;
114 }
115
Unmarshalling(Parcel & parcel)116 sptr<HdifdParcelable> HdifdParcelable::Unmarshalling(Parcel& parcel)
117 {
118 bool validFlag = false;
119 if (!parcel.ReadBool(validFlag)) {
120 HDF_LOGE("%{public}s: ReadBool validFlag failed", __func__);
121 return nullptr;
122 }
123 int32_t fd = -1;
124 if (validFlag) {
125 fd = ReadFileDescriptor(parcel);
126 if (fd < 0) {
127 HDF_LOGE("%{public}s: ReadFileDescriptor fd failed", __func__);
128 return nullptr;
129 }
130 }
131 sptr<HdifdParcelable> newParcelable = new HdifdParcelable(fd);
132 newParcelable->init_ = true;
133 return newParcelable;
134 }
135
GetFd()136 int32_t HdifdParcelable::GetFd()
137 {
138 return hdiFd_;
139 }
140
Move()141 int32_t HdifdParcelable::Move()
142 {
143 init_ = false;
144 return hdiFd_;
145 }
146
Dump() const147 std::string HdifdParcelable::Dump() const
148 {
149 std::stringstream os;
150 os << "fd: {" << hdiFd_ << "}\n";
151 return os.str();
152 }
153 } // Display
154 } // HDI
155 } // OHOS
156