xref: /aosp_15_r20/external/ot-br-posix/script/_initrc (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
30set -euxo pipefail
31
32cd "$(dirname "$0")/.."
33
34die()
35{
36    echo >&2 " *** ERROR:  $*"
37    exit 1
38}
39
40have()
41{
42    # This function checks if a tool is available
43    #
44
45    command -v "$1" >/dev/null 2>/dev/null
46}
47
48have_or_die()
49{
50    # This function verifies a tool is available and dies with proper
51    # information if not available.
52    #
53
54    have "$1" || die "$1 not available!"
55}
56
57with()
58{
59    # This function verifies a flag is on.
60    #
61    # NOTE environment settings takes higher priority than default files.
62    #
63
64    local value
65    value=$(printenv "$1")
66    if [[ -z $value ]]; then
67        if [[ -f examples/platforms/$PLATFORM/default ]]; then
68            # shellcheck source=examples/platforms/raspbian/default
69            value="$(. "examples/platforms/$PLATFORM/default" && eval echo "\${$1-}")"
70        fi
71    fi
72
73    [[ $value == 1 ]]
74}
75
76without()
77{
78    # This function verifies a flag is off.
79    #
80    # NOTE environment settings takes higher priority than default files.
81    #
82
83    ! with "$1"
84}
85
86HAVE_SYSTEMCTL=0
87if have systemctl; then
88    HAVE_SYSTEMCTL=1
89fi
90HAVE_SERVICE=0
91if have service; then
92    HAVE_SERVICE=1
93fi
94
95start_service()
96{
97    local service_name=$1
98    if [[ ${HAVE_SYSTEMCTL} == 1 ]]; then
99        systemctl is-active "$service_name" || sudo systemctl start "$service_name" || die "Failed to start $service_name!"
100    elif [[ ${HAVE_SERVICE} == 1 ]]; then
101        sudo service "$service_name" status || sudo service "$service_name" start || echo "Failed to start $service_name!"
102    else
103        die 'Unable to find service manager. Try script/console to start in console mode!'
104    fi
105}
106
107stop_service()
108{
109    local service_name=$1
110    if $HAVE_SYSTEMCTL; then
111        systemctl is-active "$service_name" && sudo systemctl stop "$service_name" || echo "Failed to stop $service_name!"
112    elif $HAVE_SERVICE; then
113        sudo service "$service_name" status && sudo service "$service_name" stop || echo "Failed to stop $service_name!"
114    else
115        die 'Unable to find service manager. Try script/console to stop in console mode!'
116    fi
117}
118
119# Platform information is needed to load hooks and default settings.
120
121if [[ ! ${PLATFORM+x} ]]; then
122    # BeagleBone Black debian distribution does not support "lsb_release"
123    if grep -s "BeagleBone Black" /sys/firmware/devicetree/base/model; then
124        # Note: 'model' is a binary file with no newline
125        PLATFORM=beagleboneblack
126    else
127        case "${OSTYPE}" in
128            darwin*)
129                PLATFORM=macOS
130                ;;
131            *)
132                have_or_die lsb_release
133                PLATFORM=$(lsb_release -i | cut -c17- | tr '[:upper:]' '[:lower:]')
134                ;;
135        esac
136    fi
137fi
138echo "Current platform is $PLATFORM"
139
140# The DHCPV6_PD feature requires IPv6 features in dhcpcd but RIO
141# is not supported within dhcpcd.
142with BORDER_ROUTING && with DHCPV6_PD && die "BORDER_ROUTING and DHCPV6_PD cannot coexist!"
143
144# OTBR cannot receive RS messages when NETWORK_MANAGER is enabled.
145with BORDER_ROUTING && with NETWORK_MANAGER && die "BORDER_ROUTING and NETWORK_MANAGER cannot coexist!"
146
147STAGE_DIR=$PWD/stage
148BUILD_DIR=$PWD/build
149
150[[ -d $STAGE_DIR ]] || mkdir -v -p "$STAGE_DIR"
151[[ -d $BUILD_DIR ]] || mkdir -v -p "$BUILD_DIR"
152
153export PATH=$STAGE_DIR/usr/bin:$STAGE_DIR/usr/sbin:$PATH
154
155TASKNAME=$(basename "$0")
156BEFORE_HOOK=examples/platforms/$PLATFORM/before_$TASKNAME
157AFTER_HOOK=examples/platforms/$PLATFORM/after_$TASKNAME
158if [[ ! -f $BEFORE_HOOK ]]; then
159    BEFORE_HOOK=/dev/null
160fi
161if [[ ! -f $AFTER_HOOK ]]; then
162    AFTER_HOOK=/dev/null
163fi
164