1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdio.h>
5 
6 #include "sd-bus.h"
7 
8 #include "hashmap.h"
9 
10 enum bus_match_node_type {
11         BUS_MATCH_ROOT,
12         BUS_MATCH_VALUE,
13         BUS_MATCH_LEAF,
14 
15         /* The following are all different kinds of compare nodes */
16         BUS_MATCH_SENDER,
17         BUS_MATCH_MESSAGE_TYPE,
18         BUS_MATCH_DESTINATION,
19         BUS_MATCH_INTERFACE,
20         BUS_MATCH_MEMBER,
21         BUS_MATCH_PATH,
22         BUS_MATCH_PATH_NAMESPACE,
23         BUS_MATCH_ARG,
24         BUS_MATCH_ARG_LAST = BUS_MATCH_ARG + 63,
25         BUS_MATCH_ARG_PATH,
26         BUS_MATCH_ARG_PATH_LAST = BUS_MATCH_ARG_PATH + 63,
27         BUS_MATCH_ARG_NAMESPACE,
28         BUS_MATCH_ARG_NAMESPACE_LAST = BUS_MATCH_ARG_NAMESPACE + 63,
29         BUS_MATCH_ARG_HAS,
30         BUS_MATCH_ARG_HAS_LAST = BUS_MATCH_ARG_HAS + 63,
31         _BUS_MATCH_NODE_TYPE_MAX,
32         _BUS_MATCH_NODE_TYPE_INVALID = -EINVAL,
33 };
34 
35 struct bus_match_node {
36         enum bus_match_node_type type;
37         struct bus_match_node *parent, *next, *prev, *child;
38 
39         union {
40                 struct {
41                         char *str;
42                         uint8_t u8;
43                 } value;
44                 struct {
45                         struct match_callback *callback;
46                 } leaf;
47                 struct {
48                         /* If this is set, then the child is NULL */
49                         Hashmap *children;
50                 } compare;
51         };
52 };
53 
54 struct bus_match_component {
55         enum bus_match_node_type type;
56         uint8_t value_u8;
57         char *value_str;
58 };
59 
60 enum bus_match_scope {
61         BUS_MATCH_GENERIC,
62         BUS_MATCH_LOCAL,
63         BUS_MATCH_DRIVER,
64 };
65 
66 int bus_match_run(sd_bus *bus, struct bus_match_node *root, sd_bus_message *m);
67 
68 int bus_match_add(struct bus_match_node *root, struct bus_match_component *components, unsigned n_components, struct match_callback *callback);
69 int bus_match_remove(struct bus_match_node *root, struct match_callback *callback);
70 
71 void bus_match_free(struct bus_match_node *node);
72 
73 void bus_match_dump(FILE *out, struct bus_match_node *node, unsigned level);
74 
75 const char* bus_match_node_type_to_string(enum bus_match_node_type t, char buf[], size_t l);
76 enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n);
77 
78 int bus_match_parse(const char *match, struct bus_match_component **ret_components, unsigned *ret_n_components);
79 void bus_match_parse_free(struct bus_match_component *components, unsigned n_components);
80 char *bus_match_to_string(struct bus_match_component *components, unsigned n_components);
81 
82 enum bus_match_scope bus_match_get_scope(const struct bus_match_component *components, unsigned n_components);
83