| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 20 | #include <stddef.h> | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 21 | #include <stdint.h>  /* for int64_t */ | 
|  | 22 |  | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 23 | #include <functional> | 
|  | 24 |  | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 25 | #include "adb_unique_fd.h" | 
|  | 26 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | /* events that may be observed */ | 
|  | 28 | #define FDE_READ              0x0001 | 
|  | 29 | #define FDE_WRITE             0x0002 | 
|  | 30 | #define FDE_ERROR             0x0004 | 
|  | 31 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); | 
|  | 33 |  | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 34 | struct fdevent { | 
| Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 35 | uint64_t id; | 
|  | 36 |  | 
| Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 37 | unique_fd fd; | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 38 | int force_eof = 0; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 39 |  | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 40 | uint16_t state = 0; | 
|  | 41 | uint16_t events = 0; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 42 |  | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 43 | fd_func func = nullptr; | 
|  | 44 | void* arg = nullptr; | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 45 | }; | 
|  | 46 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | /* Allocate and initialize a new fdevent object | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 48 | * Note: use FD_TIMER as 'fd' to create a fd-less object | 
|  | 49 | * (used to implement timers). | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | */ | 
|  | 51 | fdevent *fdevent_create(int fd, fd_func func, void *arg); | 
|  | 52 |  | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 53 | // Deallocate an fdevent object that was created by fdevent_create. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | void fdevent_destroy(fdevent *fde); | 
|  | 55 |  | 
| Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 56 | // fdevent_destroy, except releasing the file descriptor previously owned by the fdevent. | 
|  | 57 | unique_fd fdevent_release(fdevent* fde); | 
|  | 58 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | /* Change which events should cause notifications | 
|  | 60 | */ | 
|  | 61 | void fdevent_set(fdevent *fde, unsigned events); | 
|  | 62 | void fdevent_add(fdevent *fde, unsigned events); | 
|  | 63 | void fdevent_del(fdevent *fde, unsigned events); | 
|  | 64 |  | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 65 | void fdevent_set_timeout(fdevent *fde, int64_t  timeout_ms); | 
|  | 66 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | /* loop forever, handling events. | 
|  | 68 | */ | 
|  | 69 | void fdevent_loop(); | 
|  | 70 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 71 | void check_main_thread(); | 
|  | 72 |  | 
| Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 73 | // Queue an operation to run on the main thread. | 
|  | 74 | void fdevent_run_on_main_thread(std::function<void()> fn); | 
|  | 75 |  | 
| Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 76 | // The following functions are used only for tests. | 
|  | 77 | void fdevent_terminate_loop(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 78 | size_t fdevent_installed_count(); | 
| Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 79 | void fdevent_reset(); | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 80 | void set_main_thread(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | #endif |