blob: 42dbb9e70803f51dd18467625c0ec8c29ee79ccc [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2006 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 __FDEVENT_H
18#define __FDEVENT_H
19
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070020#include <stddef.h>
Josh Gao1a901182019-01-31 15:51:52 -080021#include <stdint.h>
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020022
Josh Gao1a901182019-01-31 15:51:52 -080023#include <chrono>
Josh Gao4c936392017-05-03 14:10:39 -070024#include <functional>
Josh Gao1a901182019-01-31 15:51:52 -080025#include <optional>
Josh Gaoc162c712019-01-25 15:30:25 -080026#include <variant>
Josh Gao4c936392017-05-03 14:10:39 -070027
Josh Gao3b37fa22018-05-14 13:42:49 -070028#include "adb_unique_fd.h"
29
Josh Gao1a901182019-01-31 15:51:52 -080030// Events that may be observed
31#define FDE_READ 0x0001
32#define FDE_WRITE 0x0002
33#define FDE_ERROR 0x0004
34#define FDE_TIMEOUT 0x0008
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gaoc162c712019-01-25 15:30:25 -080037typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070039struct fdevent {
Josh Gaoded557f2018-06-18 14:55:27 -070040 uint64_t id;
41
Josh Gao3b37fa22018-05-14 13:42:49 -070042 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -070043 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070044
Josh Gao71f775a2018-05-14 11:14:33 -070045 uint16_t state = 0;
46 uint16_t events = 0;
Josh Gao1a901182019-01-31 15:51:52 -080047 std::optional<std::chrono::milliseconds> timeout;
48 std::chrono::steady_clock::time_point last_active;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070049
Josh Gaoc162c712019-01-25 15:30:25 -080050 std::variant<fd_func, fd_func2> func;
Josh Gao71f775a2018-05-14 11:14:33 -070051 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070052};
53
Josh Gaoc162c712019-01-25 15:30:25 -080054// Allocate and initialize a new fdevent object
55// TODO: Switch these to unique_fd.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056fdevent *fdevent_create(int fd, fd_func func, void *arg);
Josh Gaoc162c712019-01-25 15:30:25 -080057fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Josh Gao2f6c2fa2018-09-20 17:38:55 -070059// Deallocate an fdevent object that was created by fdevent_create.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060void fdevent_destroy(fdevent *fde);
61
Josh Gao2f6c2fa2018-09-20 17:38:55 -070062// fdevent_destroy, except releasing the file descriptor previously owned by the fdevent.
63unique_fd fdevent_release(fdevent* fde);
64
Josh Gaoc162c712019-01-25 15:30:25 -080065// Change which events should cause notifications
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066void fdevent_set(fdevent *fde, unsigned events);
67void fdevent_add(fdevent *fde, unsigned events);
68void fdevent_del(fdevent *fde, unsigned events);
69
Josh Gao1a901182019-01-31 15:51:52 -080070// Set a timeout on an fdevent.
71// If no events are triggered by the timeout, an FDE_TIMEOUT will be generated.
72// Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will
73// trigger repeatedly every |timeout| ms.
74void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020075
Josh Gaoc162c712019-01-25 15:30:25 -080076// Loop forever, handling events.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077void fdevent_loop();
78
Yabin Cuib5e11412017-03-10 16:01:01 -080079void check_main_thread();
80
Josh Gao4c936392017-05-03 14:10:39 -070081// Queue an operation to run on the main thread.
82void fdevent_run_on_main_thread(std::function<void()> fn);
83
Josh Gao022d4472016-02-10 14:49:00 -080084// The following functions are used only for tests.
85void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070086size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070087void fdevent_reset();
Yabin Cuib5e11412017-03-10 16:01:01 -080088void set_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090#endif