make it easier to use netifd-proto.sh as a library
[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
42 case "$cmd" in
43         dump)
44                 add_protocol() {
45                         no_device=0
46                         available=0
47
48                         add_default_handler "$1_init_config"
49
50                         json_init
51                         json_add_string "name" "$1"
52                         eval "$1_init"
53                         json_add_boolean no-device "$no_device"
54                         json_add_boolean available "$available"
55                         json_add_array "config"
56                         eval "$1_init_config"
57                         json_close_array
58                         json_dump
59                 }
60         ;;
61         setup|teardown)
62                 interface="$1"; shift
63                 data="$1"; shift
64                 ifname="$1"; shift
65
66                 add_protocol() {
67                         [[ "$proto" == "$1" ]] || return 0
68
69                         case "$cmd" in
70                                 setup) _proto_do_setup "$1";;
71                                 teardown) _proto_do_teardown "$1" ;;
72                                 *) return 1 ;;
73                         esac
74                 }
75         ;;
76 esac