928bafe71285d983602d1ff8266c7899cb007b04
[feed/packages.git] / net / openthread-br / files / openthread-proto.sh
1 #!/bin/sh
2 #
3 # SPDX-FileCopyrightText: 2023 Stijn Tintel <stijn@linux-ipv6.be>
4 # SPDX-License-Identifier: GPL-2.0-only
5
6 OTCTL="/usr/sbin/ot-ctl"
7 PROG="/usr/sbin/otbr-agent"
8
9 [ -x "$PROG" ] || exit 0
10
11 [ -n "$INCLUDE_ONLY" ] || {
12 . /lib/functions.sh
13 . /lib/functions/network.sh
14 . ../netifd-proto.sh
15 init_proto "$@"
16 }
17
18 proto_openthread_add_prefix() {
19 prefix="$1"
20 # shellcheck disable=SC2086
21 [ -n "$prefix" ] && $OTCTL prefix add $prefix
22 }
23
24 proto_openthread_check_service() {
25 service="$1"
26 ret=1
27 json_init
28 json_add_string name "$service"
29 ubus call service list "$(json_dump)" | jsonfilter -e '@[*].instances[*]["running"]' > /dev/null
30 ret=$?
31 json_cleanup
32
33 return "$ret"
34 }
35
36 proto_openthread_init_config() {
37 proto_config_add_array 'prefix:list(string)'
38 proto_config_add_boolean verbose
39 proto_config_add_string backbone_network
40 proto_config_add_string dataset
41 proto_config_add_string radio_url
42 proto_config_add_string foobar
43
44 available=1
45 no_device=1
46 }
47
48 proto_openthread_setup_error() {
49 interface="$1"
50 error="$2"
51 proto_notify_error "$interface" "$error"
52 # prevent netifd from trying to bring up interface over and over
53 proto_block_restart "$interface"
54 proto_setup_failed "$interface"
55 exit 1
56 }
57
58 proto_openthread_setup() {
59 interface="$1"
60 device="$2"
61
62 json_get_vars backbone_network dataset device radio_url verbose:0
63
64 [ -n "$backbone_network" ] || proto_openthread_setup_error "$interface" MISSING_BACKBONE_NETWORK
65 proto_add_host_dependency "$interface" "" "$backbone_network"
66 network_get_device backbone_ifname "$backbone_network"
67
68 [ -n "$backbone_ifname" ] || proto_openthread_setup_error "$interface" MISSING_BACKBONE_IFNAME
69 [ -n "$device" ] || proto_openthread_setup_error "$interface" MISSING_DEVICE
70 [ -n "$radio_url" ] || proto_openthread_setup_error "$interface" MISSING_RADIO_URL
71
72 # run in subshell to prevent wiping json data needed for prefixes
73 ( proto_openthread_check_service mdnsd ) || proto_openthread_setup_error "$interface" MISSING_SVC_MDNSD
74
75 opts="--auto-attach=0"
76 [ "$verbose" -eq 0 ] || append opts -v
77 append opts "-I$device"
78 append opts "-B$backbone_ifname"
79 append opts "$radio_url"
80 append opts "trel://$backbone_ifname"
81 # run in subshell to prevent wiping json data needed for prefixes
82 ( proto_run_command "$interface" "$PROG" $opts )
83
84 ubus -t30 wait_for otbr
85
86 [ -n "$dataset" ] && {
87 $OTCTL dataset set active "$dataset"
88 }
89
90 json_for_each_item proto_openthread_add_prefix prefix
91 ubus call otbr threadstart || proto_openthread_setup_error "$interface" MISSING_UBUS_OBJ
92 $OTCTL netdata register
93
94 proto_init_update "$device" 1 1
95 proto_send_update "$interface"
96 }
97
98 proto_openthread_teardown() {
99 interface="$1"
100 ubus call otbr threadstop
101 proto_kill_command "$interface"
102 }
103
104 [ -n "$INCLUDE_ONLY" ] || {
105 add_protocol openthread
106 }