PatchworkOS  966e257
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 ctl (control) file operations.
9 * @defgroup kernel_fs_ctl Ctl
10 * @ingroup kernel_fs
11 *
12 * A ctl 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 Helper macro to define a standard ctl write function.
27 *
28 * This macro defines a write function that dispatches commands to a given array of ctl_t structures.
29 *
30 * @param name The name of the ctl write function.
31 * @param ... The ctl array to dispatch commands to.
32 */
33#define CTL_STANDARD_WRITE_DEFINE(name, ...) \
34 static ctl_t name##ctls[] = __VA_ARGS__; \
35 static uint64_t name(file_t* file, const void* buffer, uint64_t count, uint64_t* offset) \
36 { \
37 (void)offset; \
38 return ctl_dispatch(name##ctls, file, buffer, count); \
39 }
40
41/**
42 * @brief Helper macro to define a standard ctl file operations structure.
43 *
44 * This macro defines a file operations structure with all standard ctl operations implemented.
45 *
46 * @param name The name of the ctl file operations structure.
47 * @param ... The ctl array to dispatch commands to.
48 */
49#define CTL_STANDARD_OPS_DEFINE(name, ...) \
50 CTL_STANDARD_WRITE_DEFINE(name##write, __VA_ARGS__) \
51 static file_ops_t name = (file_ops_t){ \
52 .write = name##write, \
53 };
54
55/**
56 * @brief Type definition for a ctl function.
57 */
58typedef uint64_t (*ctl_func_t)(file_t* file, uint64_t, const char**);
59
60/**
61 * @brief Structure defining a ctl command.
62 * @struct ctl_t
63 */
64typedef struct
65{
66 const char* name; ///< The name of the command.
67 ctl_func_t func; ///< The function to call for the command.
68 uint64_t argcMin; ///< The minimum number of arguments accepted by func.
69 uint64_t argcMax; ///< The maximum number of arguments accepted by func.
70} ctl_t;
71
72/**
73 * @brief Dispatch a ctl command.
74 *
75 * @param ctls The array of ctl commands to dispatch to, terminated by an entry with a `NULL` name.
76 * @param file The file the ctl command was sent to.
77 * @param buffer The buffer containing the command and its arguments.
78 * @param count The number of bytes in the buffer.
79 * @return On success, the number of bytes processed (count). On failure, `ERR` and `errno` is set.
80 */
82
83/** @} */
uint64_t ctl_dispatch(ctl_t ctls[], file_t *file, const void *buffer, uint64_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:58
static dentry_t * file
Definition log_file.c:22
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Structure defining a ctl command.
Definition ctl.h:65
uint64_t argcMin
The minimum number of arguments accepted by func.
Definition ctl.h:68
ctl_func_t func
The function to call for the command.
Definition ctl.h:67
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition ctl.h:69
const char * name
The name of the command.
Definition ctl.h:66
File structure.
Definition file.h:39