4A: delay service startup as a workaround to SPEC-1762/SPEC-1763 (temp workaround)
[AGL/meta-agl-devel.git] / meta-audio-4a-framework / recipes-multimedia / agl-service-audio-4a / files / 4a_wait_bt.sh
1 #!/bin/bash
2
3 ###############################################################
4 # This is a workaround for SPEC-1762/SPEC-1763
5 #
6 # THIS SCRIPT MUST BE REMOVED ONCE THE ABOVE ISSUES ARE SOLVED
7 #
8 # Source recipe is:
9 #
10 # meta-audio-4a-framework/
11 #       recipes-multimedia/
12 #               agl-service-audio-4a/
13 #                       agl-service-audio-4a_git.bb
14 #
15 # Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
16 ###############################################################
17
18 # time from script startup
19 LIMIT=20
20 ts0=0
21 function ts() { echo $(( $(date +%s) - ts0 )); }
22 ts0=$(ts)
23 function havetime() { [[ $(ts) -le ${1:-$LIMIT} ]] || return 1; }
24
25 function waitloop() {
26         # ensure bt modules are loaded (delay: 5s)
27         while havetime 4; do
28                 [[ -d /sys/module/bluetooth ]] && {
29                         echo "bluetooth kernel module detected"
30                         break
31                 }
32                 echo "waiting for bluetooth kernel module to be up"
33                 sleep 0.2
34         done
35         havetime 4 || { echo "TIMEOUT REACHED"; return 1; }
36
37         # check that we have at least one controller
38         HCIDEV=
39         while havetime 8; do
40                 echo "detecting hci devices..."
41                 for x in $(ls /sys/class/bluetooth/hci* 2>/dev/null); do
42                         [[ -z "$HCIDEV" ]] && HCIDEV=$(basename $x)
43                 done
44                 [[ -n "$HCIDEV" ]] && {
45                         echo "found HCI controller: $HCIDEV"
46                         break
47                 }
48                 sleep 0.2
49         done
50         havetime 8 || { echo "TIMEOUT REACHED"; return 1; }
51
52         # wait for controller to be up and running
53         while havetime 15; do
54                 state=$(hciconfig $HCIDEV | grep -A 2 ^hci0 | tail -1)
55                 [[ $state =~ UP ]] && [[ $state =~ RUNNING ]] && {
56                         echo "HCI controller $HCIDEV state: $state"
57                         break
58                 }
59                 echo "HCI controller $HCIDEV state: $state ... waiting for UP RUNNING"
60                 sleep 0.2
61         done
62         havetime 15 || { echo "TIMEOUT REACHED"; return 1; }
63         echo "HCI device up and running after $(ts) seconds"
64
65         # wait for bluetooth-service to return something
66         while havetime; do
67                 res=$(afb-client-demo -d unix:/run/user/$UID/apis/ws/Bluetooth-Manager power true)
68                 [[ "$res" =~ \"response\":(.*)}$ ]] && res=${BASH_REMATCH[1]}
69                 [[ "$res" == '{"power":"on"}' ]] && {
70                         echo "Bluetooth-Manager/power: $res"
71                         break
72                 }
73                 echo "Bluetooth-Manager/power: not ready yet ($res)"
74                 sleep 0.2
75         done
76         echo "Bluetooth-Manager ready after $(ts) seconds"
77
78         echo "4A now starting..."
79 }
80
81 waitloop 2>&1 | sed 's/^/4AWAITBT /' >&2
82 exec "$@"
83