Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 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 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 16 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 17 | #include <poll.h> |
| 18 | #include <stdint.h> |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 19 | #include <stdbool.h> |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 20 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 21 | struct console; |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 22 | struct config; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 23 | |
| 24 | /* handler API */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 25 | enum { |
| 26 | HANDLER_OK = 0, |
| 27 | HANDLER_EXIT, |
| 28 | }; |
| 29 | |
| 30 | struct handler { |
| 31 | const char *name; |
| 32 | int (*init)(struct handler *handler, |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 33 | struct console *console, |
| 34 | struct config *config); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 35 | int (*data_in)(struct handler *handler, |
| 36 | uint8_t *buf, size_t len); |
| 37 | void (*fini)(struct handler *handler); |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 38 | bool active; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define __handler_name(n) __handler_ ## n |
| 42 | #define _handler_name(n) __handler_name(n) |
| 43 | |
| 44 | #define console_register_handler(h) \ |
| 45 | static const \ |
| 46 | __attribute__((section("handlers"))) \ |
| 47 | __attribute__((used)) \ |
| 48 | struct handler * _handler_name(__COUNTER__) = h; |
| 49 | |
| 50 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 51 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 52 | /* poller API */ |
| 53 | struct poller; |
| 54 | |
| 55 | enum poller_ret { |
| 56 | POLLER_OK = 0, |
| 57 | POLLER_REMOVE, |
| 58 | POLLER_EXIT, |
| 59 | }; |
| 60 | |
| 61 | typedef enum poller_ret (*poller_fn_t)(struct handler *handler, |
| 62 | int revents, void *data); |
| 63 | |
| 64 | struct poller *console_register_poller(struct console *console, |
| 65 | struct handler *handler, poller_fn_t poller_fn, |
| 66 | int fd, int events, void *data); |
| 67 | |
| 68 | void console_unregister_poller(struct console *console, struct poller *poller); |
| 69 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 70 | /* config API */ |
| 71 | struct config; |
| 72 | const char *config_get_value(struct config *config, const char *name); |
| 73 | struct config *config_init(const char *filename); |
| 74 | void config_fini(struct config *config); |
| 75 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 76 | /* socket paths */ |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 77 | extern const char *console_socket_path; |
| 78 | extern const size_t console_socket_path_len; |
| 79 | extern const char *console_socket_path_readable; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 80 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 81 | /* utils */ |
| 82 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 83 | |
| 84 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 85 | |
| 86 | #define offsetof(type, member) \ |
| 87 | ((unsigned long)&((type *)NULL)->member) |
| 88 | |
| 89 | #define container_of(ptr, type, member) \ |
| 90 | ((type *)((void *)((ptr) - offsetof(type, member)))) |
| 91 | |
Jeremy Kerr | 658cbda | 2016-03-09 18:10:00 +0800 | [diff] [blame] | 92 | #define BUILD_ASSERT(c) \ |
| 93 | do { \ |
| 94 | char __c[(c)?1:-1] __attribute__((unused)); \ |
| 95 | } while (0) |