blob: 2658e89bc8f8f293f06c6657b21710247c1439b1 [file] [log] [blame]
Jeremy Kerr9326d772016-03-17 17:15:02 +08001/**
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 Kerr1a0e03b2016-03-08 17:57:11 +080016
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080017#include <poll.h>
18#include <stdint.h>
Jeremy Kerr021b91f2016-04-28 11:51:52 +080019#include <stdbool.h>
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080020
Jeremy Kerr329a35f2016-03-10 15:36:01 +080021struct console;
Jeremy Kerrd47963e2016-03-16 17:29:55 +080022struct config;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080023
24/* handler API */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080025enum {
26 HANDLER_OK = 0,
27 HANDLER_EXIT,
28};
29
30struct handler {
31 const char *name;
32 int (*init)(struct handler *handler,
Jeremy Kerrd47963e2016-03-16 17:29:55 +080033 struct console *console,
34 struct config *config);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080035 int (*data_in)(struct handler *handler,
36 uint8_t *buf, size_t len);
37 void (*fini)(struct handler *handler);
Jeremy Kerr021b91f2016-04-28 11:51:52 +080038 bool active;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080039};
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
50int console_data_out(struct console *console, const uint8_t *data, size_t len);
51
Jeremy Kerr329a35f2016-03-10 15:36:01 +080052/* poller API */
53struct poller;
54
55enum poller_ret {
56 POLLER_OK = 0,
57 POLLER_REMOVE,
58 POLLER_EXIT,
59};
60
61typedef enum poller_ret (*poller_fn_t)(struct handler *handler,
62 int revents, void *data);
63
64struct poller *console_register_poller(struct console *console,
65 struct handler *handler, poller_fn_t poller_fn,
66 int fd, int events, void *data);
67
68void console_unregister_poller(struct console *console, struct poller *poller);
69
Jeremy Kerrd66195c2016-03-16 17:24:51 +080070/* config API */
71struct config;
72const char *config_get_value(struct config *config, const char *name);
73struct config *config_init(const char *filename);
74void config_fini(struct config *config);
75
Jeremy Kerr2bd05182016-03-10 16:59:43 +080076/* socket paths */
Jeremy Kerr0cff6522016-03-18 09:57:01 +080077extern const char *console_socket_path;
78extern const size_t console_socket_path_len;
79extern const char *console_socket_path_readable;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080080
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080081/* utils */
82int 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 Kerr658cbda2016-03-09 18:10:00 +080092#define BUILD_ASSERT(c) \
93 do { \
94 char __c[(c)?1:-1] __attribute__((unused)); \
95 } while (0)