104edc7b6b55fad89c0f1504716daba288541244
[project/netifd.git] / dummy / netifd-proto.sh
1 . /usr/share/libubox/jshn.sh
2
3 proto_config_add_generic() {
4         json_add_array ""
5         json_add_string "" "$1"
6         json_add_int "" "$2"
7         json_close_array
8 }
9
10 proto_config_add_int() {
11         proto_config_add_generic "$1" 5
12 }
13
14 proto_config_add_string() {
15         proto_config_add_generic "$1" 3
16 }
17
18 proto_config_add_boolean() {
19         proto_config_add_generic "$1" 7
20 }
21
22 add_default_handler() {
23         case "$(type $1 2>/dev/null)" in
24                 *function*) return;;
25                 *) eval "$1() { return; }"
26         esac
27 }
28
29 _proto_do_teardown() {
30         json_load "$data"
31         eval "$1_teardown \"$interface\" \"$ifname\""
32 }
33
34 _proto_do_setup() {
35         json_load "$data"
36         eval "$1_setup \"$interface\" \"$ifname\""
37 }
38
39 proto="$1"; shift
40 cmd="$1"; shift
41 interface="$1"; shift
42 data="$1"; shift
43 ifname="$1"; shift
44
45 case "$cmd" in
46         dump)
47                 add_protocol() {
48                         no_device=0
49                         available=0
50
51                         add_default_handler "$1_init_config"
52
53                         json_init
54                         json_add_string "name" "$1"
55                         eval "$1_init"
56                         json_add_boolean no-device "$no_device"
57                         json_add_boolean available "$available"
58                         json_add_array "config"
59                         eval "$1_init_config"
60                         json_close_array
61                         json_dump
62                 }
63         ;;
64         *)
65                 add_protocol() {
66                         [[ "$proto" == "$1" ]] || return 0
67
68                         case "$cmd" in
69                                 setup) _proto_do_setup "$1";;
70                                 teardown) _proto_do_teardown "$1" ;;
71                                 *) return 1 ;;
72                         esac
73                 }
74         ;;
75 esac