1 //
2 // Copyright 2018 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 #define LOG_TAG "VtsHidlHandleDriver"
17
18 #include "hidl_handle_driver/VtsHidlHandleDriver.h"
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <vector>
26
27 #include <android-base/logging.h>
28
29 using android::hardware::hidl_handle;
30
31 using namespace std;
32
33 namespace android {
34 namespace vts {
35
VtsHidlHandleDriver()36 VtsHidlHandleDriver::VtsHidlHandleDriver() {}
37
~VtsHidlHandleDriver()38 VtsHidlHandleDriver::~VtsHidlHandleDriver() {
39 // clears objects in the map.
40 hidl_handle_map_.clear();
41 }
42
CreateFileHandle(string filepath,int flag,int mode,vector<int> data)43 HandleId VtsHidlHandleDriver::CreateFileHandle(string filepath, int flag,
44 int mode, vector<int> data) {
45 int num_fds = 1;
46 int num_ints = data.size();
47 native_handle_t* native_handle = native_handle_create(num_fds, num_ints);
48 if (native_handle == nullptr) {
49 LOG(ERROR) << "native_handle create failure.";
50 return -1;
51 }
52
53 for (int i = 0; i < num_fds + num_ints; i++) {
54 if (i < num_fds) {
55 int curr_fd = open(filepath.c_str(), flag, mode);
56 if (curr_fd == -1) {
57 LOG(ERROR) << "Failed to create file descriptor for file with path "
58 << filepath;
59 // Need to close already opened files because creating handle fails.
60 for (int j = 0; j < i; j++) {
61 close(native_handle->data[j]);
62 }
63 native_handle_delete(native_handle);
64 return -1;
65 }
66 native_handle->data[i] = curr_fd;
67 } else {
68 native_handle->data[i] = data[i - num_fds];
69 }
70 }
71
72 // This class owns native_handle object, responsible for deleting it
73 // in the destructor.
74 unique_ptr<hidl_handle> hidl_handle_ptr(new hidl_handle());
75 hidl_handle_ptr->setTo(native_handle, true);
76 // Insert the handle object into the map.
77 map_mutex_.lock();
78 size_t new_handle_id = hidl_handle_map_.size();
79 hidl_handle_map_.emplace(new_handle_id, move(hidl_handle_ptr));
80 map_mutex_.unlock();
81 return new_handle_id;
82 }
83
UnregisterHidlHandle(HandleId handle_id)84 bool VtsHidlHandleDriver::UnregisterHidlHandle(HandleId handle_id) {
85 // true flag to indicate we want smart pointer to release ownership.
86 hidl_handle* handle_obj = FindHandle(handle_id, true);
87 if (handle_obj == nullptr) return false; // unable to find handle object.
88 delete handle_obj; // This closes open file descriptors in the handle object.
89 return true;
90 }
91
ReadFile(HandleId handle_id,void * read_data,size_t num_bytes)92 ssize_t VtsHidlHandleDriver::ReadFile(HandleId handle_id, void* read_data,
93 size_t num_bytes) {
94 hidl_handle* handle_obj = FindHandle(handle_id);
95 if (handle_obj == nullptr) return -1;
96
97 const native_handle_t* native_handle = handle_obj->getNativeHandle();
98 // Check if a file descriptor exists.
99 if (native_handle->numFds == 0) {
100 LOG(ERROR) << "Read from file failure: handle object with id " << handle_id
101 << " has no file descriptor.";
102 return -1;
103 }
104 int fd = native_handle->data[0];
105 ssize_t read_result = read(fd, read_data, num_bytes);
106 if (read_result == -1) {
107 LOG(ERROR) << "Read from file failure: read from file with descriptor "
108 << fd << " failure: " << strerror(errno);
109 }
110 return read_result;
111 }
112
WriteFile(HandleId handle_id,const void * write_data,size_t num_bytes)113 ssize_t VtsHidlHandleDriver::WriteFile(HandleId handle_id,
114 const void* write_data,
115 size_t num_bytes) {
116 hidl_handle* handle_obj = FindHandle(handle_id);
117 if (handle_obj == nullptr) return -1;
118
119 const native_handle_t* native_handle = handle_obj->getNativeHandle();
120 // Check if a file descriptor exists.
121 if (native_handle->numFds == 0) {
122 LOG(ERROR) << "Write to file failure: handle object with id " << handle_id
123 << " has no file descriptor.";
124 return -1;
125 }
126 int fd = native_handle->data[0];
127 ssize_t write_result = write(fd, write_data, num_bytes);
128 if (write_result == -1) {
129 LOG(ERROR) << "Write to file failure: write to file with descriptor " << fd
130 << " failure: " << strerror(errno);
131 }
132 return write_result;
133 }
134
RegisterHidlHandle(size_t hidl_handle_address)135 HandleId VtsHidlHandleDriver::RegisterHidlHandle(size_t hidl_handle_address) {
136 unique_ptr<hidl_handle> hidl_handle_ptr(
137 reinterpret_cast<hidl_handle*>(hidl_handle_address));
138
139 map_mutex_.lock();
140 size_t new_handle_id = hidl_handle_map_.size();
141 hidl_handle_map_.emplace(new_handle_id, move(hidl_handle_ptr));
142 map_mutex_.unlock();
143 return new_handle_id;
144 }
145
GetHidlHandleAddress(HandleId handle_id,size_t * result)146 bool VtsHidlHandleDriver::GetHidlHandleAddress(HandleId handle_id,
147 size_t* result) {
148 hidl_handle* handle = FindHandle(handle_id);
149 if (handle == nullptr) return false; // unable to find handle object.
150 *result = reinterpret_cast<size_t>(handle);
151 return true;
152 }
153
FindHandle(HandleId handle_id,bool release)154 hidl_handle* VtsHidlHandleDriver::FindHandle(HandleId handle_id, bool release) {
155 hidl_handle* handle;
156 map_mutex_.lock();
157 auto iterator = hidl_handle_map_.find(handle_id);
158 if (iterator == hidl_handle_map_.end()) {
159 LOG(ERROR) << "Unable to find hidl_handle associated with handle_id "
160 << handle_id;
161 map_mutex_.unlock();
162 return nullptr;
163 }
164 // During unregistering a handle, unique_ptr releases ownership.
165 if (release)
166 handle = (iterator->second).release();
167 else
168 handle = (iterator->second).get();
169 map_mutex_.unlock();
170 return handle;
171 }
172
173 } // namespace vts
174 } // namespace android
175