ubus: add config support
[project/udebug.git] / server.h
1 #ifndef __UDEBUG_SERVER_H
2 #define __UDEBUG_SERVER_H
3
4 #include <libubox/list.h>
5 #include <libubox/uloop.h>
6 #include <libubox/avl.h>
7 #include "priv.h"
8
9 extern int debug_level;
10
11 #define D(level, format, ...) \
12 do { \
13 if (debug_level >= level) \
14 fprintf(stderr, "DEBUG: %s(%d) " format "\n", \
15 __func__, __LINE__, ##__VA_ARGS__); \
16 } while (0)
17
18 #define DC(level, cl, format, ...) \
19 D(level, "[%s(%d)] " format, cl->proc_name, cl->pid, ##__VA_ARGS__)
20
21 struct client {
22 struct list_head list;
23 struct list_head bufs;
24 struct uloop_fd fd;
25 int notify_id;
26
27 char proc_name[64];
28 int pid;
29 int uid;
30
31 int rx_fd;
32 size_t rx_ofs;
33 struct {
34 struct udebug_client_msg msg;
35 union {
36 struct blob_attr data;
37 uint8_t buf[4096];
38 };
39 } __attribute__((packed,aligned(4))) rx_buf;
40 };
41
42 struct client_ring {
43 struct list_head list;
44 struct avl_node node;
45 struct client *cl;
46
47 int fd;
48 uint32_t id;
49 uint32_t ring_size, data_size;
50 const char *name;
51 struct blob_attr *flags;
52 };
53
54 extern struct avl_tree rings;
55
56 void client_alloc(int fd);
57 struct client_ring *client_ring_alloc(struct client *cl);
58 struct client_ring *client_ring_get_by_id(struct client *cl, uint32_t id);
59 void client_ring_free(struct client_ring *r);
60
61 static inline uint32_t ring_id(struct client_ring *r)
62 {
63 return (uint32_t)(uintptr_t)r->node.key;
64 }
65
66 static inline struct client_ring *ring_get_by_id(uint32_t id)
67 {
68 struct client_ring *r;
69 void *key = (void *)(uintptr_t)id;
70
71 return avl_find_element(&rings, key, r, node);
72 }
73
74 void udebug_ubus_init(void);
75 void udebug_ubus_ring_notify(struct client_ring *r, bool add);
76 void udebug_ubus_free(void);
77
78 #endif