drop -O0 for debug builds
[project/netifd.git] / proto / 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="$1"; shift
30 cmd="$1"; shift
31 interface="$1"; shift
32 data="$1"; shift
33 ifname="$1"; shift
34
35 case "$cmd" in
36         dump)
37                 add_protocol() {
38                         no_device=0
39                         available=0
40
41                         add_default_handler "$1_init_config"
42
43                         json_init
44                         json_add_string "name" "$1"
45                         eval "$1_init"
46                         json_add_boolean no-device "$no_device"
47                         json_add_boolean available "$available"
48                         json_add_array "config"
49                         eval "$1_init_config"
50                         json_close_array
51                         json_dump
52                 }
53         ;;
54         *)
55                 add_protocol() {
56                         [[ "$proto" == "$1" ]] || return 0
57
58                         case "$cmd" in
59                                 setup) eval "$1_setup \"\$interface\" \"\$data\" \"\$ifname\"" ;;
60                                 teardown) eval "$1_teardown \"\$interface\" \"\$data\" \"\$ifname\"" ;;
61                                 *) return 1 ;;
62                         esac
63                 }
64         ;;
65 esac