blob: 147e1c2bb4d565b76e546d879b963496de8ca8c3 [file] [log] [blame]
Mathias Agopianb957b9d2010-07-13 22:21:56 -07001/*
2 * Copyright (C) 2010 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 <stdint.h>
18#include <sys/types.h>
19
20#include <unistd.h>
21#include <fcntl.h>
22
23#include <utils/Errors.h>
24
25#include <binder/Parcel.h>
26
27#include <gui/SensorChannel.h>
28
29namespace android {
30// ----------------------------------------------------------------------------
31
32SensorChannel::SensorChannel()
33 : mSendFd(-1), mReceiveFd(-1)
34{
35 int fds[2];
36 if (pipe(fds) == 0) {
37 mReceiveFd = fds[0];
38 mSendFd = fds[1];
39 fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
40 fcntl(mSendFd, F_SETFL, O_NONBLOCK);
41 }
42}
43
44SensorChannel::SensorChannel(const Parcel& data)
45 : mSendFd(-1), mReceiveFd(-1)
46{
47 mReceiveFd = dup(data.readFileDescriptor());
48 fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
49}
50
51SensorChannel::~SensorChannel()
52{
53 if (mSendFd >= 0)
54 close(mSendFd);
55
56 if (mReceiveFd >= 0)
57 close(mReceiveFd);
58}
59
60int SensorChannel::getFd() const
61{
62 return mReceiveFd;
63}
64
65ssize_t SensorChannel::write(void const* vaddr, size_t size)
66{
67 ssize_t len = ::write(mSendFd, vaddr, size);
68 if (len < 0)
69 return -errno;
70 return len;
71}
72
73ssize_t SensorChannel::read(void* vaddr, size_t size)
74{
75 ssize_t len = ::read(mReceiveFd, vaddr, size);
76 if (len < 0)
77 return -errno;
78 return len;
79}
80
81status_t SensorChannel::writeToParcel(Parcel* reply) const
82{
83 if (mReceiveFd < 0)
84 return -EINVAL;
85
86 status_t result = reply->writeDupFileDescriptor(mReceiveFd);
87 close(mReceiveFd);
88 mReceiveFd = -1;
89 return result;
90}
91
92// ----------------------------------------------------------------------------
93}; // namespace android