xref: /aosp_15_r20/external/ot-br-posix/script/_otbr (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1#!/bin/bash
2#
3#  Copyright (c) 2017, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30OTBR_TOP_BUILDDIR="${BUILD_DIR}/otbr"
31readonly OTBR_TOP_BUILDDIR
32
33OTBR_OPTIONS="${OTBR_OPTIONS-}"
34readonly OTBR_OPTIONS
35
36REFERENCE_DEVICE="${REFERENCE_DEVICE:-0}"
37readonly REFERENCE_DEVICE
38
39otbr_uninstall()
40{
41    if have systemctl; then
42        sudo systemctl stop otbr-web || true
43        sudo systemctl stop otbr-agent || true
44        sudo systemctl disable otbr-web || true
45        sudo systemctl disable otbr-agent || true
46        ! sudo systemctl is-enabled otbr-web
47        ! sudo systemctl is-enabled otbr-agent
48    fi
49    sudo killall otbr-web otbr-agent || true
50
51    (
52        if cd "${OTBR_TOP_BUILDDIR}"; then
53            # shellcheck disable=SC2024
54            sudo xargs rm <install_manifests.txt || true
55        fi
56    )
57    if have systemctl; then
58        sudo systemctl daemon-reload
59    fi
60}
61
62otbr_install()
63{
64    local otbr_options=()
65
66    if [[ ${OTBR_OPTIONS} ]]; then
67        read -r -a otbr_options <<<"${OTBR_OPTIONS}"
68    fi
69
70    otbr_options=(
71        "-DBUILD_TESTING=OFF"
72        "-DCMAKE_INSTALL_PREFIX=/usr"
73        "-DOTBR_DBUS=ON"
74        "-DOTBR_DNSSD_DISCOVERY_PROXY=ON"
75        "-DOTBR_SRP_ADVERTISING_PROXY=ON"
76        "-DOTBR_INFRA_IF_NAME=${INFRA_IF_NAME}"
77        "-DOTBR_MDNS=${OTBR_MDNS:=mDNSResponder}"
78        # Force re-evaluation of version strings
79        "-DOTBR_VERSION="
80        "-DOT_PACKAGE_VERSION="
81        "${otbr_options[@]}"
82    )
83
84    if with WEB_GUI; then
85        otbr_options+=("-DOTBR_WEB=ON")
86    fi
87
88    if with BORDER_ROUTING; then
89        otbr_options+=(
90            "-DOTBR_BORDER_ROUTING=ON"
91        )
92    fi
93
94    if with REST_API; then
95        otbr_options+=("-DOTBR_REST=ON")
96    fi
97
98    if with BACKBONE_ROUTER; then
99        otbr_options+=(
100            "-DOTBR_BACKBONE_ROUTER=ON"
101        )
102        if [[ ${REFERENCE_DEVICE} == "1" ]]; then
103            otbr_options+=(
104                "-DOTBR_DUA_ROUTING=ON"
105            )
106        fi
107    fi
108
109    if [[ ${REFERENCE_DEVICE} == "1" ]]; then
110        otbr_options+=(
111            "-DOTBR_NO_AUTO_ATTACH=1"
112            "-DOT_REFERENCE_DEVICE=ON"
113            "-DOT_DHCP6_CLIENT=ON"
114            "-DOT_DHCP6_SERVER=ON"
115        )
116    fi
117
118    if with NAT64 && [[ ${NAT64_SERVICE-} == "openthread" ]]; then
119        otbr_options+=(
120            "-DOTBR_NAT64=ON"
121            "-DOT_POSIX_NAT64_CIDR=${NAT64_DYNAMIC_POOL:-192.168.255.0/24}"
122        )
123    fi
124
125    if with NAT64; then
126        otbr_options+=(
127            "-DOTBR_DNS_UPSTREAM_QUERY=ON"
128        )
129    fi
130
131    if with FIREWALL; then
132        otbr_options+=(
133            "-DOT_FIREWALL=ON"
134        )
135    else
136        otbr_options+=(
137            "-DOT_FIREWALL=OFF"
138        )
139    fi
140
141    (./script/cmake-build "${otbr_options[@]}" \
142        && cd "${OTBR_TOP_BUILDDIR}" \
143        && ninja \
144        && sudo ninja install)
145
146    if have systemctl; then
147        sudo systemctl reload dbus
148        sudo systemctl daemon-reload
149        without WEB_GUI || sudo systemctl enable otbr-web || true
150        sudo systemctl enable otbr-agent || true
151        sudo systemctl is-enabled otbr-agent || die 'Failed to enable otbr-agent!'
152        without WEB_GUI || sudo systemctl is-enabled otbr-web || die 'Failed to enable otbr-web!'
153
154        if [[ ${REFERENCE_DEVICE} == "1" ]]; then
155            sudo systemctl enable testharness-discovery || true
156            sudo systemctl is-enabled testharness-discovery || die 'Failed to enable otbr-agent!'
157        fi
158    else
159        echo >&2 ' *** WARNING: systemctl not found. otbr cannot start on boot.'
160    fi
161}
162
163otbr_update()
164{
165    otbr_install
166}
167