blob: 15bf92e90cc1b12759b5eac948755192c9f7ed60 [file] [log] [blame]
developer8fb759f2022-02-21 16:39:38 +08001#ifndef __WM_H
2#define __WM_H
3
4#include <stdbool.h>
5#include <netlink/netlink.h>
6#include <netlink/genl/genl.h>
7#include <netlink/genl/family.h>
8#include <netlink/genl/ctrl.h>
9#include <endian.h>
10
11enum command_identify_by {
12 CIB_NONE,
13 CIB_PHY,
14 CIB_NETDEV,
15 CIB_WDEV,
16};
17
18/* libnl 1.x compatibility code */
19#if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30)
20# define nl_sock nl_handle
21#endif
22
23struct nl80211_state {
24 struct nl_sock *nl_sock;
25 int nl80211_id;
26};
27
28enum id_input {
29 II_NONE,
30 II_NETDEV,
31 II_PHY_NAME,
32 II_PHY_IDX,
33 II_WDEV,
34};
35
36struct cmd {
37 const char *name;
38 const char *args;
39 const char *help;
40 const enum mtk_nl80211_vendor_commands cmd;
41 int nl_msg_flags;
42 int hidden;
43 const enum command_identify_by idby;
44 /*
45 * The handler should return a negative error code,
46 * zero on success, 1 if the arguments were wrong.
47 * Return 2 iff you provide the error message yourself.
48 */
49 int (*handler)(struct nl_msg *msg, int argc,
50 char **argv, void *ctx);
51 const struct cmd *(*selector)(int argc, char **argv);
52 const struct cmd *parent;
53};
54#ifndef ARRAY_SIZE
55#define ARRAY_SIZE(ar) (sizeof(ar)/sizeof(ar[0]))
56#endif
57#ifndef DIV_ROUND_UP
58#define DIV_ROUND_UP(x, y) (((x) + (y - 1)) / (y))
59#endif
60#define __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel)\
61 static struct cmd \
62 __cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden = {\
63 .name = (_name), \
64 .args = (_args), \
65 .cmd = (_nlcmd), \
66 .nl_msg_flags = (_flags), \
67 .hidden = (_hidden), \
68 .idby = (_idby), \
69 .handler = (_handler), \
70 .help = (_help), \
71 .parent = _section, \
72 .selector = (_sel), \
73 }; \
74 static struct cmd *__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden ## _p \
75 __attribute__((used,section("__cmd"))) = \
76 &__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden
77#define __ACMD(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel, _alias)\
78 __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel);\
79 static const struct cmd *_alias = &__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden
80#define COMMAND(section, name, args, cmd, flags, idby, handler, help) \
81 __COMMAND(&(__section ## _ ## section), name, #name, args, cmd, flags, 0, idby, handler, help, NULL)
82#define COMMAND_ALIAS(section, name, args, cmd, flags, idby, handler, help, selector, alias)\
83 __ACMD(&(__section ## _ ## section), name, #name, args, cmd, flags, 0, idby, handler, help, selector, alias)
84#define HIDDEN(section, name, args, cmd, flags, idby, handler) \
85 __COMMAND(&(__section ## _ ## section), name, #name, args, cmd, flags, 1, idby, handler, NULL, NULL)
86
87#define TOPLEVEL(_name, _args, _nlcmd, _flags, _idby, _handler, _help) \
88 struct cmd __section ## _ ## _name = { \
89 .name = (#_name), \
90 .args = (_args), \
91 .cmd = (_nlcmd), \
92 .nl_msg_flags = (_flags), \
93 .idby = (_idby), \
94 .handler = (_handler), \
95 .help = (_help), \
96 }; \
97 static struct cmd *__section ## _ ## _name ## _p \
98 __attribute__((used,section("__cmd"))) = &__section ## _ ## _name
99
100#define SECTION(_name) \
101 struct cmd __section ## _ ## _name = { \
102 .name = (#_name), \
103 .hidden = 1, \
104 }; \
105 static struct cmd *__section ## _ ## _name ## _p \
106 __attribute__((used,section("__cmd"))) = &__section ## _ ## _name
107
108#define DECLARE_SECTION(_name) \
109 extern struct cmd __section ## _ ## _name;
110
111void register_handler(int (*handler)(struct nl_msg *, void *), void *data);
112#endif