blob: 9d65fad2286a38a235e17568205b12c03d84ed35 [file] [log] [blame]
Mathias Agopian589ce852010-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#ifndef ANDROID_GUI_SENSOR_CHANNEL_H
18#define ANDROID_GUI_SENSOR_CHANNEL_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <android/log.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070024#include <utils/Errors.h>
25#include <utils/RefBase.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070026
27namespace android {
28// ----------------------------------------------------------------------------
29class Parcel;
30
Mathias Agopian5cae0d02011-10-20 18:42:02 -070031class BitTube : public RefBase
Mathias Agopian589ce852010-07-13 22:21:56 -070032{
33public:
34
Mathias Agopian90ed3e82013-09-09 23:36:25 -070035 // creates a BitTube with a default (4KB) send buffer
36 BitTube();
37
38 // creates a BitTube with a a specified send and receive buffer size
39 explicit BitTube(size_t bufsize);
40
41 explicit BitTube(const Parcel& data);
Mathias Agopian5cae0d02011-10-20 18:42:02 -070042 virtual ~BitTube();
Mathias Agopian589ce852010-07-13 22:21:56 -070043
Mathias Agopian90ed3e82013-09-09 23:36:25 -070044 // check state after construction
Mathias Agopian5cae0d02011-10-20 18:42:02 -070045 status_t initCheck() const;
Mathias Agopian90ed3e82013-09-09 23:36:25 -070046
47 // get receive file-descriptor
Mathias Agopian589ce852010-07-13 22:21:56 -070048 int getFd() const;
Mathias Agopian589ce852010-07-13 22:21:56 -070049
Aravind Akella56ae4262014-07-10 16:01:10 -070050 // get the send file-descriptor.
51 int getSendFd() const;
52
Mathias Agopian90ed3e82013-09-09 23:36:25 -070053 // send objects (sized blobs). All objects are guaranteed to be written or the call fails.
Mathias Agopian7b5be952012-04-02 17:02:19 -070054 template <typename T>
55 static ssize_t sendObjects(const sp<BitTube>& tube,
56 T const* events, size_t count) {
57 return sendObjects(tube, events, count, sizeof(T));
58 }
59
Mathias Agopian90ed3e82013-09-09 23:36:25 -070060 // receive objects (sized blobs). If the receiving buffer isn't large enough,
61 // excess messages are silently discarded.
Mathias Agopian7b5be952012-04-02 17:02:19 -070062 template <typename T>
63 static ssize_t recvObjects(const sp<BitTube>& tube,
64 T* events, size_t count) {
65 return recvObjects(tube, events, count, sizeof(T));
66 }
67
Mathias Agopian90ed3e82013-09-09 23:36:25 -070068 // parcels this BitTube
69 status_t writeToParcel(Parcel* reply) const;
70
Mathias Agopian589ce852010-07-13 22:21:56 -070071private:
Mathias Agopian90ed3e82013-09-09 23:36:25 -070072 void init(size_t rcvbuf, size_t sndbuf);
73
74 // send a message. The write is guaranteed to send the whole message or fail.
75 ssize_t write(void const* vaddr, size_t size);
76
77 // receive a message. the passed buffer must be at least as large as the
78 // write call used to send the message, excess data is silently discarded.
79 ssize_t read(void* vaddr, size_t size);
80
Mathias Agopian589ce852010-07-13 22:21:56 -070081 int mSendFd;
82 mutable int mReceiveFd;
Mathias Agopian7b5be952012-04-02 17:02:19 -070083
84 static ssize_t sendObjects(const sp<BitTube>& tube,
85 void const* events, size_t count, size_t objSize);
86
87 static ssize_t recvObjects(const sp<BitTube>& tube,
88 void* events, size_t count, size_t objSize);
Mathias Agopian589ce852010-07-13 22:21:56 -070089};
90
91// ----------------------------------------------------------------------------
92}; // namespace android
93
94#endif // ANDROID_GUI_SENSOR_CHANNEL_H