PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
ctl.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/file.h>
4
5#include <stdint.h>
6
7/**
8 * @brief Helpers to implement control file operations.
9 * @defgroup kernel_fs_ctl Control Files
10 * @ingroup kernel_fs
11 *
12 * A control file is a special file that takes in commands as text input and performs actions based on those commands.
13 *
14 * ## Command Format
15 *
16 * Commands should be formatted as follows:
17 *
18 * ```
19 * command1 arg1 arg2 arg3 ... && command2 arg1 arg2 ... && ...
20 * ```
21 *
22 * @{
23 */
24
25/**
26 * @brief Maximum size of the buffer used for argument parsing.
27 */
28#define CTL_MAX_BUFFER 0x1000
29
30/**
31 * @brief Helper macro to define a standard ctl write function.
32 *
33 * This macro defines a write function that dispatches commands to a given array of ctl_t structures.
34 *
35 * @param name The name of the ctl write function.
36 * @param ... The ctl array to dispatch commands to.
37 */
38#define CTL_STANDARD_WRITE_DEFINE(name, ...) \
39 static ctl_t name##ctls[] = __VA_ARGS__; \
40 static size_t name(file_t* file, const void* buffer, size_t count, size_t* offset) \
41 { \
42 UNUSED(offset); \
43 return ctl_dispatch(name##ctls, file, buffer, count); \
44 }
45
46/**
47 * @brief Helper macro to define a standard ctl file operations structure.
48 *
49 * This macro defines a file operations structure with all standard ctl operations implemented.
50 *
51 * @param name The name of the ctl file operations structure.
52 * @param ... The ctl array to dispatch commands to.
53 */
54#define CTL_STANDARD_OPS_DEFINE(name, ...) \
55 CTL_STANDARD_WRITE_DEFINE(name##write, __VA_ARGS__) \
56 static file_ops_t name = (file_ops_t){ \
57 .write = name##write, \
58 };
59
60/**
61 * @brief Type definition for a ctl function.
62 */
63typedef uint64_t (*ctl_func_t)(file_t* file, uint64_t, const char**);
64
65/**
66 * @brief Structure defining a ctl command.
67 * @struct ctl_t
68 */
69typedef struct
70{
71 const char* name; ///< The name of the command.
72 ctl_func_t func; ///< The function to call for the command.
73 uint64_t argcMin; ///< The minimum number of arguments accepted by func.
74 uint64_t argcMax; ///< The maximum number of arguments accepted by func.
75} ctl_t;
76
77/**
78 * @brief Dispatch a ctl command.
79 *
80 * @param ctls The array of ctl commands to dispatch to, terminated by an entry with a `NULL` name.
81 * @param file The file the ctl command was sent to.
82 * @param buffer The buffer containing the command and its arguments.
83 * @param count The number of bytes in the buffer.
84 * @return On success, the number of bytes processed (count). On failure, `ERR` and `errno` is set.
85 */
86uint64_t ctl_dispatch(ctl_t ctls[], file_t* file, const void* buffer, size_t count);
87
88/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
uint64_t ctl_dispatch(ctl_t ctls[], file_t *file, const void *buffer, size_t count)
Dispatch a ctl command.
uint64_t(* ctl_func_t)(file_t *file, uint64_t, const char **)
Type definition for a ctl function.
Definition ctl.h:63
static atomic_long count
Definition main.c:11
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Structure defining a ctl command.
Definition ctl.h:70
uint64_t argcMin
The minimum number of arguments accepted by func.
Definition ctl.h:73
ctl_func_t func
The function to call for the command.
Definition ctl.h:72
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition ctl.h:74
const char * name
The name of the command.
Definition ctl.h:71
File structure.
Definition file.h:39