move immediate protocol state transitions to a wrapper function
[project/netifd.git] / device.h
1 #ifndef __LL_H
2 #define __LL_H
3
4 #include <libubox/avl.h>
5
6 struct device;
7 struct device_hotplug_ops;
8
9 typedef int (*device_state_cb)(struct device *, bool up);
10
11 struct device_type {
12         const char *name;
13
14         void (*dump_status)(struct device *, struct blob_buf *buf);
15         int (*check_state)(struct device *);
16         void (*free)(struct device *);
17 };
18
19 /* 
20  * link layer device. typically represents a linux network device.
21  * can be used to support VLANs as well
22  */
23 struct device {
24         const struct device_type *type;
25
26         struct avl_node avl;
27         struct list_head users;
28
29         char ifname[IFNAMSIZ + 1];
30         int ifindex;
31
32         bool present;
33         int active;
34
35         /* set interface up or down */
36         device_state_cb set_state;
37
38         const struct device_hotplug_ops *hotplug_ops;
39 };
40
41 /* events broadcasted to all users of a device */
42 enum device_event {
43         DEV_EVENT_ADD,
44         DEV_EVENT_REMOVE,
45
46         DEV_EVENT_SETUP,
47         DEV_EVENT_TEARDOWN,
48         DEV_EVENT_UP,
49         DEV_EVENT_DOWN,
50
51         DEV_EVENT_LINK_UP,
52         DEV_EVENT_LINK_DOWN,
53 };
54
55 /*
56  * device dependency with callbacks
57  */
58 struct device_user {
59         struct list_head list;
60
61         struct device *dev;
62         void (*cb)(struct device_user *, enum device_event);
63 };
64
65 struct device_hotplug_ops {
66         int (*add)(struct device *main, struct device *member);
67         int (*del)(struct device *main, struct device *member);
68 };
69
70 void init_virtual_device(struct device *dev, const struct device_type *type, const char *name);
71 int init_device(struct device *iface, const struct device_type *type, const char *ifname);
72 void cleanup_device(struct device *iface);
73 struct device *get_device(const char *name, bool create);
74 void add_device_user(struct device_user *dep, struct device *iface);
75 void remove_device_user(struct device_user *dep);
76
77 void set_device_present(struct device *dev, bool state);
78 int claim_device(struct device *dev);
79 void release_device(struct device *dev);
80 int check_device_state(struct device *dev);
81
82 struct device *get_vlan_device_chain(const char *ifname, bool create);
83
84 #endif