-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwbeam
More file actions
executable file
·2113 lines (1911 loc) · 71 KB
/
wbeam
File metadata and controls
executable file
·2113 lines (1911 loc) · 71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOST_SCRIPTS_DIR="$ROOT_DIR/host/scripts"
WBEAM_CONFIG_HELPER="$HOST_SCRIPTS_DIR/wbeam_config.sh"
if [[ -f "$WBEAM_CONFIG_HELPER" ]]; then
# shellcheck source=host/scripts/wbeam_config.sh
source "$WBEAM_CONFIG_HELPER"
wbeam_load_config "$ROOT_DIR"
fi
CONTROL_PORT="${WBEAM_CONTROL_PORT:-5001}"
STREAM_PORT="${WBEAM_STREAM_PORT:-5000}"
DAEMON_NAME="${WBEAM_DAEMON_SERVICE_NAME:-wbeam-daemon}"
DAEMON_BIN="${WBEAM_DAEMON_BIN:-$ROOT_DIR/host/rust/target/release/wbeamd-server}"
if [[ ! -x "$DAEMON_BIN" && -x "/usr/local/bin/wbeamd-server" ]]; then
DAEMON_BIN="/usr/local/bin/wbeamd-server"
fi
ANDROID_DIR="$ROOT_DIR/android"
ANDROID_PACKAGE="${WBEAM_ANDROID_PACKAGE:-com.wbeam}"
ANDROID_ACTIVITY="${WBEAM_ANDROID_ACTIVITY:-.MainActivity}"
ANDROID_APK="$ANDROID_DIR/app/build/outputs/apk/debug/app-debug.apk"
ANDROID_APK_RELEASE="$ANDROID_DIR/app/build/outputs/apk/release/app-release.apk"
ANDROID_APK_RELEASE_UNSIGNED="$ANDROID_DIR/app/build/outputs/apk/release/app-release-unsigned.apk"
ANDROID_SERIAL="${WBEAM_ANDROID_SERIAL:-}"
ANDROID_PIPELINE_FILE="$ROOT_DIR/.wbeam_android_pipeline"
LOG_DIR="$ROOT_DIR/logs"
ADB_LOGCAT_PID_FILE="$LOG_DIR/adb-logcat.pid"
BUILD_VERSION_FILE="$ROOT_DIR/.wbeam_build_version"
DEVICE_PORTS_FILE="$ROOT_DIR/.wbeam_device_ports"
VERSION_BASE="${WBEAM_VERSION_BASE:-0.1.2}"
print_help() {
cat <<'EOF'
WBeam command runner
Usage:
./wbeam <group> <command>
./wbgui # interactive TUI menu (arrow keys)
Groups:
ip up | down | status
host build | run | debug | status | down | probe | tuner
daemon up | down | status (alias for host)
android build | install | launch | deploy | deploy-all | build-release | install-release | deploy-release
deps virtual check [--json] | virtual install [--dry-run] [--yes]
version new | current | doctor
watch devices | connections | streaming | service | logs | status | health | doctor | tui
debug up | watch (alias for host debug)
logs live | host | adb (start|stop|status|tail)
Examples:
./wbgui
./wbeam host build # build wbeamd-server + wbeamd-streamer (evdi)
./wbeam host run # release binary, foreground
./wbeam host debug # debug build + coloured logs + ADB sidecar
./wbeam host probe # host fingerprint JSON (os/session/display/providers/tools)
./wbeam host tuner # interactive Rust TUI autotuner wizard
./wbeam android deploy # build + install + launch in one step
./wbeam android deploy-all # build once, install + launch on all connected devices
./wbeam android build # ./gradlew assembleDebug only
./wbeam android install # adb install only (uses existing debug APK)
./wbeam android launch # adb shell am start only
./wbeam android build-release # ./gradlew assembleRelease
./wbeam android install-release
./wbeam android deploy-release
./wbeam deps virtual check --json
./wbeam deps virtual install --dry-run
./wbeam version doctor # diagnose host/APK version mismatch
./wbeam watch devices # refresh adb/device status every 1s
./wbeam watch connections # refresh host/ports/reverse map every 1s
./wbeam watch streaming # per-device stream state + fps/config
./wbeam watch service # service state + host probe + activity
./wbeam watch logs # current host/adb/service logs snapshot
./wbeam watch tui # interactive screens (1..5, r, q)
./wbeam watch connections --once
watch -n 1 -d --color 'WBEAM_WATCH_COLOR=1 ./wbeam watch connections --once'
WBEAM_WATCH_LOG=1 ./wbeam watch doctor
./wbeam debug up
./wbeam logs live
./wbeam logs adb start
EOF
}
adb_device_cmd() {
require_cmd adb
android_select_serial_if_needed || true
if [[ -n "$ANDROID_SERIAL" ]]; then
adb -s "$ANDROID_SERIAL" "$@"
else
adb "$@"
fi
}
android_connected_serials() {
if [[ -n "${WBEAM_ANDROID_SERIALS:-}" ]]; then
printf '%s\n' "${WBEAM_ANDROID_SERIALS}" \
| tr ',;[:space:]' '\n' \
| awk 'NF { print $1 }'
return 0
fi
require_cmd adb
adb start-server >/dev/null 2>&1 || true
adb devices | awk 'NR>1 && $2=="device"{print $1}'
}
android_select_serial_if_needed() {
if [[ -n "$ANDROID_SERIAL" ]]; then
return 0
fi
local serials=()
mapfile -t serials < <(android_connected_serials)
if [[ "${#serials[@]}" -eq 0 ]]; then
return 1
fi
if [[ "${#serials[@]}" -eq 1 ]]; then
ANDROID_SERIAL="${serials[0]}"
return 0
fi
ANDROID_SERIAL="${serials[0]}"
echo "[android] WARN: multiple adb devices detected (${serials[*]})." >&2
echo "[android] WARN: auto-selecting serial=$ANDROID_SERIAL; set WBEAM_ANDROID_SERIAL to override." >&2
return 0
}
android_pipeline_mode() {
local mode="${WBEAM_ANDROID_PIPELINE:-}"
if [[ -z "$mode" && -f "$ANDROID_PIPELINE_FILE" ]]; then
mode="$(tr -d '[:space:]' < "$ANDROID_PIPELINE_FILE" 2>/dev/null || true)"
fi
if [[ -z "$mode" ]]; then
mode="auto"
fi
case "$mode" in
legacy|modern)
echo "$mode"
;;
auto)
android_auto_pipeline_mode
;;
*)
android_auto_pipeline_mode
;;
esac
}
android_connected_api_level() {
local api
require_cmd adb
adb start-server >/dev/null 2>&1 || true
api="$(adb_device_cmd shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r[:space:]' || true)"
if [[ "$api" =~ ^[0-9]+$ ]]; then
echo "$api"
return 0
fi
return 1
}
android_auto_pipeline_mode() {
local api
api="$(android_connected_api_level || true)"
# modern pipeline requires minSdk=19, so API <=18 must use legacy.
if [[ "$api" =~ ^[0-9]+$ ]] && (( api <= 18 )); then
echo "legacy"
else
if [[ "$api" =~ ^[0-9]+$ ]]; then
echo "modern"
else
# When API detection is flaky (USB mode flips/race with adb), default to
# legacy for compatibility so old tablets do not get unreachable LAN hosts.
echo "[android] WARN: could not detect Android API level; defaulting pipeline=legacy for safety" >&2
echo "legacy"
fi
fi
}
android_pipeline_min_sdk() {
case "$1" in
legacy) echo "17" ;;
*) echo "19" ;;
esac
}
detect_tether_ip() {
# Determine host address reachable from the currently targeted Android device.
# On old API levels `ip route` may be missing, so keep multiple probes.
#
# Priority 1 — ask the connected device for its default gateway.
# On USB tether this is the host address. We trust device route first,
# because host-side interface state can flap during reconnect and a strict
# host-IP cross-check can incorrectly fall back to LAN.
local gw
if command -v adb >/dev/null 2>&1; then
gw="$(adb_device_cmd shell ip route 2>/dev/null \
| awk '/^default/{for(i=1;i<=NF;i++){if($i=="via"){print $(i+1); exit}}}' \
| tr -d '\r' || true)"
if [[ -z "$gw" ]]; then
# API17/legacy fallback (toolbox route).
gw="$(adb_device_cmd shell route -n 2>/dev/null \
| awk '$1=="0.0.0.0" || $1=="default"{print $2; exit}' \
| tr -d '\r' || true)"
fi
if [[ -n "$gw" && "$gw" != "127."* ]]; then
echo "$gw"; return 0
fi
fi
# Priority 2 — look for a host interface in common USB-tether private ranges.
# Keep AOSP 192.168.42/43 first.
local ip
ip="$(ip -4 addr show 2>/dev/null \
| awk '/inet 192\.168\.4[23]\./{gsub(/\/.*/, "", $2); print $2; exit}')"
if [[ -n "$ip" ]]; then echo "$ip"; return 0; fi
# Priority 3 — USB-like interfaces with any private IPv4 (vendor subnets vary).
ip="$(ip -4 addr show 2>/dev/null \
| awk '
/^[0-9]+: (usb|rndis|enx)/{iface=1; next}
iface && /inet /{
gsub(/\/.*/, "", $2);
if ($2 ~ /^10\./ || $2 ~ /^192\.168\./ || $2 ~ /^172\.(1[6-9]|2[0-9]|3[01])\./) {
print $2; exit
}
iface=0
}')"
if [[ -n "$ip" ]]; then echo "$ip"; return 0; fi
# Priority 4 — as a last resort, any USB-like interface IPv4.
ip="$(ip -4 addr show 2>/dev/null \
| awk '/^[0-9]+: (usb|rndis|enx)/{iface=1; next} iface && /inet /{gsub(/\/.*/, "", $2); print $2; exit}')"
if [[ -n "$ip" ]]; then echo "$ip"; return 0; fi
return 1
}
detect_host_ip() {
# For tethered Android (legacy path) we prefer the tether interface IP.
local tether_ip
tether_ip="$(detect_tether_ip 2>/dev/null || true)"
if [[ -n "$tether_ip" ]]; then
echo "$tether_ip"; return 0
fi
# Fallback: IP of the interface used to reach the internet (LAN).
local ip_candidate=""
ip_candidate="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++){if($i=="src"){print $(i+1); exit}}}')"
if [[ -n "$ip_candidate" ]]; then
echo "$ip_candidate"
return 0
fi
ip_candidate="$(hostname -I 2>/dev/null | awk '{print $1}')"
if [[ -n "$ip_candidate" ]]; then
echo "$ip_candidate"
return 0
fi
return 1
}
android_build_host() {
local pipeline_mode="$1"
if [[ -n "${WBEAM_ANDROID_HOST:-}" ]]; then
echo "$WBEAM_ANDROID_HOST"
return 0
fi
if [[ "$pipeline_mode" == "legacy" ]]; then
local detected
detected="$(detect_host_ip || true)"
if [[ -n "$detected" ]]; then
echo "$detected"
return 0
fi
fi
echo "127.0.0.1"
}
normalize_build_version() {
local raw="${1:-}"
if [[ -z "$raw" ]]; then
return 1
fi
if [[ "$raw" =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+\.([A-Za-z0-9._-]+)$ ]]; then
echo "${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
elif [[ "$raw" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\.[A-Za-z0-9._-]+)?$ ]]; then
echo "$raw"
else
echo "${VERSION_BASE}.${raw}"
fi
}
git_short_rev() {
local hash
hash="$(git -C "$ROOT_DIR" rev-parse --short=5 HEAD 2>/dev/null || true)"
if [[ "$hash" =~ ^[A-Fa-f0-9]{5}$ ]]; then
echo "$hash"
else
echo "dev00"
fi
}
build_version_generate() {
echo "${VERSION_BASE}.$(git_short_rev)"
}
build_version_write_file() {
local version="$1"
printf '%s\n' "$version" > "$BUILD_VERSION_FILE"
}
build_version_read_file() {
if [[ -f "$BUILD_VERSION_FILE" ]]; then
tr -d '\r[:space:]' < "$BUILD_VERSION_FILE" 2>/dev/null || true
fi
}
build_version_for_build() {
local explicit="${WBEAM_BUILD_REV:-}"
local version
if [[ -n "$explicit" ]]; then
version="$(normalize_build_version "$explicit")"
else
version="$(build_version_generate)"
fi
build_version_write_file "$version"
echo "$version"
}
build_version_for_runtime() {
local explicit="${WBEAM_BUILD_REV:-}"
if [[ -n "$explicit" ]]; then
normalize_build_version "$explicit"
return 0
fi
local from_file
from_file="$(build_version_read_file)"
if [[ -n "$from_file" ]]; then
normalize_build_version "$from_file"
return 0
fi
build_version_generate
}
version_new() {
build_version_for_build
}
version_current() {
build_version_for_runtime
}
version_log_file() {
local ts day counter_file n run_id
mkdir -p "$LOG_DIR"
ts="$(date +%Y%m%d-%H%M%S)"
day="$(date +%Y%m%d)"
counter_file="$LOG_DIR/.run.${day}.counter"
n=0
if [[ -f "$counter_file" ]]; then
n="$(tr -d '[:space:]' < "$counter_file" 2>/dev/null || echo 0)"
fi
if [[ ! "$n" =~ ^[0-9]+$ ]]; then
n=0
fi
n=$((n + 1))
printf '%s' "$n" > "$counter_file"
run_id="$(printf '%04d' "$n")"
echo "$LOG_DIR/${ts}.version.${run_id}.log"
}
watch_log_file() {
local topic="$1"
local ts day counter_file n run_id
mkdir -p "$LOG_DIR"
ts="$(date +%Y%m%d-%H%M%S)"
day="$(date +%Y%m%d)"
counter_file="$LOG_DIR/.run.${day}.counter"
n=0
if [[ -f "$counter_file" ]]; then
n="$(tr -d '[:space:]' < "$counter_file" 2>/dev/null || echo 0)"
fi
if [[ ! "$n" =~ ^[0-9]+$ ]]; then
n=0
fi
n=$((n + 1))
printf '%s' "$n" > "$counter_file"
run_id="$(printf '%04d' "$n")"
echo "$LOG_DIR/${ts}.watch-${topic}.${run_id}.log"
}
host_health_raw() {
local url
url="http://127.0.0.1:${CONTROL_PORT}/health"
curl -fsS --max-time 1 "$url" 2>/dev/null || return 1
}
json_string_field() {
local json="$1"
local field="$2"
printf '%s\n' "$json" \
| sed -n "s/.*\"${field}\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" \
| head -n 1
}
json_scalar_field() {
local json="$1"
local field="$2"
printf '%s\n' "$json" \
| sed -n "s/.*\"${field}\"[[:space:]]*:[[:space:]]*\\([^,}]*\\).*/\\1/p" \
| head -n 1 \
| tr -d '"' \
| tr -d '[:space:]'
}
version_doctor() {
local log_file now host_health="" host_build="" host_service="" file_build="" host_status=""
local serials=()
now="$(date -Iseconds)"
log_file="$(version_log_file)"
{
echo "[version-doctor] time=$now"
echo "[version-doctor] control_api=http://127.0.0.1:${CONTROL_PORT}"
if host_health="$(host_health_raw)"; then
host_build="$(json_string_field "$host_health" "build_revision")"
host_service="$(json_string_field "$host_health" "service")"
host_status="reachable"
echo "[version-doctor] host_health=$host_status service=${host_service:-unknown} build_revision=${host_build:-unknown}"
else
host_status="unreachable"
echo "[version-doctor] host_health=$host_status"
fi
file_build="$(build_version_read_file || true)"
echo "[version-doctor] build_file=${BUILD_VERSION_FILE} value=${file_build:-<empty>}"
if [[ -n "${WBEAM_BUILD_REV:-}" ]]; then
echo "[version-doctor] env_WBEAM_BUILD_REV=${WBEAM_BUILD_REV}"
else
echo "[version-doctor] env_WBEAM_BUILD_REV=<unset>"
fi
if ! command -v adb >/dev/null 2>&1; then
echo "[version-doctor] adb=missing"
exit 0
fi
mapfile -t serials < <(android_connected_serials)
if [[ "${#serials[@]}" -eq 0 ]]; then
echo "[version-doctor] adb_devices=none"
exit 0
fi
echo "[version-doctor] adb_devices=${serials[*]}"
for serial in "${serials[@]}"; do
local api model apk_version expected reason
api="$(adb -s "$serial" shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r[:space:]' || true)"
model="$(adb -s "$serial" shell getprop ro.product.model 2>/dev/null | tr -d '\r' || true)"
apk_version="$(adb -s "$serial" shell dumpsys package "$ANDROID_PACKAGE" 2>/dev/null \
| sed -n 's/^[[:space:]]*versionName=//p' | head -n 1 | tr -d '\r' || true)"
expected="$host_build"
if [[ -z "$expected" ]]; then
expected="$file_build"
fi
if [[ -z "$apk_version" ]]; then
reason="apk_not_installed"
elif [[ -z "$expected" ]]; then
reason="missing_expected_version"
elif [[ "$apk_version" == "$expected" ]]; then
reason="match"
else
reason="mismatch"
fi
echo "[version-doctor][$serial] model=${model:-unknown} api=${api:-unknown} apk=${apk_version:-<none>} expected=${expected:-<none>} result=$reason"
done
} | tee "$log_file"
echo "[version-doctor] log=$log_file"
}
android_expected_version_name() {
build_version_for_runtime
}
android_installed_version_name() {
adb_device_cmd shell dumpsys package "$ANDROID_PACKAGE" 2>/dev/null \
| sed -n 's/^[[:space:]]*versionName=//p' \
| head -n 1 \
| tr -d '\r' \
|| true
}
android_build_stamp_path() {
local apk_path="$1"
echo "${apk_path}.wbeam-config"
}
android_install_stamp_path() {
local apk_path="$1"
echo "${apk_path}.wbeam-installed"
}
android_should_skip_install() {
local apk_path="${1:-$ANDROID_APK}"
local force_install="${WBEAM_ANDROID_FORCE_INSTALL:-0}"
if [[ "$force_install" =~ ^(1|true|TRUE|yes|on)$ ]]; then
return 1
fi
local expected installed
expected="$(android_expected_version_name)"
installed="$(android_installed_version_name)"
if [[ -z "$installed" || "$installed" != "$expected" ]]; then
return 1 # version mismatch — must install
fi
# Also force reinstall when the baked-in host/pipeline changed since last
# install (covers: same git SHA but different host IP or pipeline variant).
local build_stamp install_stamp
build_stamp="$(android_build_stamp_path "$apk_path")"
install_stamp="$(android_install_stamp_path "$apk_path")"
if [[ -f "$build_stamp" && -f "$install_stamp" ]]; then
if ! diff -q "$build_stamp" "$install_stamp" >/dev/null 2>&1; then
return 1 # config changed since last install
fi
elif [[ -f "$build_stamp" && ! -f "$install_stamp" ]]; then
return 1 # never recorded as installed with this config
fi
return 0 # version + config match — safe to skip
}
android_device_has_ipv4() {
local ips line ip
# Try modern 'ip addr' first; fall back to 'ifconfig' and 'netcfg' for API<21
# Android (busybox) where 'ip' is absent or the GNU sed pattern fails.
ips="$(adb_device_cmd shell 'ip -f inet addr show 2>/dev/null | grep inet || ifconfig 2>/dev/null | grep -Eo "addr:[0-9.]+" | sed s/addr:// || netcfg 2>/dev/null | awk "{print \$3}" | grep -v "^0\.0\.0\.0"' 2>/dev/null | tr -d '\r')"
while IFS= read -r line; do
ip="${line%%/*}"
ip="${ip##addr:}"
if [[ -n "$ip" && "$ip" != "127.0.0.1" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
return 0
fi
done <<< "$ips"
return 1
}
android_try_reverse_setup() {
local stream_port="${1:-$STREAM_PORT}"
local control_port="${2:-$CONTROL_PORT}"
local legacy_stream_port=5002
# Canonical device-side ports expected by Android app.
adb_device_cmd reverse "tcp:5000" "tcp:${stream_port}" >/dev/null 2>&1 || return 1
adb_device_cmd reverse "tcp:5001" "tcp:${control_port}" >/dev/null 2>&1 || return 1
# Compatibility mapping for APKs built with per-device stream port.
if [[ "$stream_port" != "5000" ]]; then
adb_device_cmd reverse "tcp:${stream_port}" "tcp:${stream_port}" >/dev/null 2>&1 || true
fi
# Legacy compatibility: some builds are pinned to 5002 while host sessions may
# currently run on 5000 (or another selected stream_port).
if [[ "$stream_port" != "$legacy_stream_port" ]]; then
adb_device_cmd reverse "tcp:${legacy_stream_port}" "tcp:${stream_port}" >/dev/null 2>&1 || true
fi
return 0
}
android_probe_control_api_from_device() {
local host="$1"
local control_port="${2:-$CONTROL_PORT}"
local -a adb_base
android_select_serial_if_needed || true
adb_base=(adb)
if [[ -n "$ANDROID_SERIAL" ]]; then
adb_base+=(-s "$ANDROID_SERIAL")
fi
# On legacy Android (API 17), busybox wget is typically available.
# We force a short timeout to avoid hanging deploy.
if command -v timeout >/dev/null 2>&1; then
if timeout 6 "${adb_base[@]}" shell "busybox wget -q -O - http://${host}:${control_port}/status" >/dev/null 2>&1; then
return 0
fi
if timeout 6 "${adb_base[@]}" shell "wget -q -O - http://${host}:${control_port}/status" >/dev/null 2>&1; then
return 0
fi
return 1
fi
"${adb_base[@]}" shell "busybox wget -q -O - http://${host}:${control_port}/status" >/dev/null 2>&1 \
|| "${adb_base[@]}" shell "wget -q -O - http://${host}:${control_port}/status" >/dev/null 2>&1
}
android_echo_hosts() {
local api_host="$1"
local stream_host="$2"
printf '%s|%s\n' "$api_host" "$stream_host"
}
android_resolve_deploy_hosts() {
local pipeline_mode="$1"
local allow_no_transport="${WBEAM_ANDROID_ALLOW_NO_TRANSPORT:-0}"
local api_only_fallback="${WBEAM_ANDROID_API_ONLY_FALLBACK:-0}"
if [[ -n "${WBEAM_ANDROID_API_HOST:-}" || -n "${WBEAM_ANDROID_STREAM_HOST:-}" || -n "${WBEAM_ANDROID_HOST:-}" ]]; then
local explicit_api="${WBEAM_ANDROID_API_HOST:-${WBEAM_ANDROID_HOST:-127.0.0.1}}"
local explicit_stream="${WBEAM_ANDROID_STREAM_HOST:-${WBEAM_ANDROID_HOST:-127.0.0.1}}"
android_echo_hosts "$explicit_api" "$explicit_stream"
return 0
fi
require_cmd adb
adb start-server >/dev/null
adb_device_cmd wait-for-device
if android_try_reverse_setup; then
android_echo_hosts "127.0.0.1" "127.0.0.1"
return 0
fi
if [[ "$pipeline_mode" != "legacy" ]]; then
# Modern pipeline: adb reverse failed — fall back to LAN IP so the app can
# still reach the host when connected on Wi-Fi.
local host_ip_mod
host_ip_mod="$(detect_host_ip || true)"
if [[ -n "$host_ip_mod" ]]; then
echo "[android] WARN: adb reverse failed; using LAN IP ${host_ip_mod} for modern pipeline" >&2
android_echo_hosts "$host_ip_mod" "$host_ip_mod"
return 0
fi
echo "[android] WARN: adb reverse failed and no LAN IP detected; falling back to 127.0.0.1" >&2
android_echo_hosts "127.0.0.1" "127.0.0.1"
return 0
fi
local host_ip
host_ip="$(detect_host_ip || true)"
# For the legacy pipeline, adb reverse is never available (API<21), so
# 127.0.0.1 is NEVER useful — bake the host's LAN/tether IP instead.
# Try host IP unconditionally; the device-side IP check is irrelevant here
# because it's the *host* address we're embedding in the APK.
if [[ -n "$host_ip" ]]; then
if ! android_probe_control_api_from_device "$host_ip"; then
local host_iface
host_iface="$(ip -4 -o addr show scope global 2>/dev/null | awk -v target="$host_ip" '{split($4,a,"/"); if (a[1]==target) {print $2; exit}}' || true)"
echo "[android] ERROR: device cannot reach host control API at http://${host_ip}:${CONTROL_PORT}/status" >&2
echo "[android] - this causes the red 'control link' screen." >&2
if [[ -n "$host_iface" ]]; then
echo "[android] Fix firewall (run locally as root):" >&2
echo " sudo iptables -I INPUT -i ${host_iface} -s 192.168.42.0/24 -p tcp -m multiport --dports ${CONTROL_PORT},${STREAM_PORT} -j ACCEPT" >&2
fi
echo "[android] Then rerun: ./wbeam android deploy" >&2
return 1
fi
# api_only_fallback is only meaningful for modern (where stream may use
# adb reverse): for legacy both endpoints must use the direct IP.
android_echo_hosts "$host_ip" "$host_ip"
return 0
fi
if [[ "$allow_no_transport" =~ ^(1|true|TRUE|yes|on)$ ]]; then
echo "[android] WARN: legacy pipeline — no host IP detected; app will be unreachable." >&2
echo "[android] Hint: set WBEAM_ANDROID_HOST=<host-ip> explicitly (e.g. 192.168.100.201)." >&2
android_echo_hosts "127.0.0.1" "127.0.0.1"
return 0
fi
echo "[android] ERROR: no transport path to host for legacy device." >&2
echo "[android] - adb reverse is unavailable on this device (or ADB transport is unstable)." >&2
echo "[android] - device has no active IPv4 network interface for LAN fallback." >&2
echo "[android] Hint: set WBEAM_ANDROID_ALLOW_NO_TRANSPORT=1 to force loopback deploy despite missing tunnel." >&2
echo "[android] Fix: connect tablet to Wi-Fi (same LAN as host) or set WBEAM_ANDROID_HOST manually after network is up." >&2
return 1
}
android_target_desc() {
if [[ -n "$ANDROID_SERIAL" ]]; then
echo "serial=$ANDROID_SERIAL"
else
echo "default-device"
fi
}
require_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1" >&2
exit 1
fi
}
host_build() {
require_cmd cargo
local rust_dir="$ROOT_DIR/host/rust"
local build_version
build_version="$(build_version_for_build)"
echo "[host] Build version: $build_version"
echo "[host] Building Rust backend (release)…"
cd "$rust_dir" && \
WBEAM_BUILD_REV="$build_version" cargo build --release -p wbeamd-server && \
WBEAM_BUILD_REV="$build_version" cargo build --release -p wbeamd-streamer --no-default-features --features evdi
echo "[host] Build OK → $DAEMON_BIN"
}
ensure_daemon_binary() {
if [[ -x "$DAEMON_BIN" ]]; then
return 0
fi
echo "Daemon binary missing: $DAEMON_BIN" >&2
echo "Build it first:" >&2
echo " cd $ROOT_DIR/host/rust && cargo build --release -p wbeamd-server" >&2
echo " cd $ROOT_DIR/host/rust && cargo build --release -p wbeamd-streamer --no-default-features --features evdi" >&2
echo "or install package providing /usr/local/bin/wbeamd-server" >&2
exit 1
}
stop_local_processes() {
pkill -9 wbeamd-streamer 2>/dev/null || true
pkill -9 wbeamd-server 2>/dev/null || true
rm -f /tmp/wbeamd.lock
}
ensure_log_dir() {
mkdir -p "$LOG_DIR"
}
next_log_run_id() {
local day counter_file n
ensure_log_dir
day="$(date +%Y%m%d)"
counter_file="$LOG_DIR/.run.${day}.counter"
n=0
if [[ -f "$counter_file" ]]; then
n="$(tr -d '[:space:]' < "$counter_file" 2>/dev/null || echo 0)"
fi
if [[ ! "$n" =~ ^[0-9]+$ ]]; then
n=0
fi
n=$((n + 1))
printf '%s' "$n" > "$counter_file"
printf '%04d' "$n"
}
new_log_file() {
local domain ts run_id
domain="$1"
ts="$(date +%Y%m%d-%H%M%S)"
run_id="$(next_log_run_id)"
echo "$LOG_DIR/${ts}.${domain}.${run_id}.log"
}
adb_logcat_is_running() {
local pid
if [[ ! -f "$ADB_LOGCAT_PID_FILE" ]]; then
return 1
fi
pid="$(awk 'NR==1{print $1}' "$ADB_LOGCAT_PID_FILE" 2>/dev/null || true)"
[[ -n "$pid" ]] || return 1
kill -0 "$pid" 2>/dev/null
}
adb_logcat_start() {
local log_file pid serial
require_cmd adb
ensure_log_dir
serial="${ANDROID_SERIAL:-default}"
if adb_logcat_is_running; then
echo "[logs][adb] already running: $(cat "$ADB_LOGCAT_PID_FILE" 2>/dev/null)"
return 0
fi
adb start-server >/dev/null 2>&1 || true
log_file="$(new_log_file "adb")"
# Capture device logs continuously; useful for control-link failures.
(adb_device_cmd logcat -v threadtime) >>"$log_file" 2>&1 &
pid=$!
printf '%s %s %s\n' "$pid" "$log_file" "$serial" > "$ADB_LOGCAT_PID_FILE"
echo "[logs][adb] started pid=$pid serial=$serial file=$log_file"
}
adb_logcat_stop() {
local pid
if ! adb_logcat_is_running; then
rm -f "$ADB_LOGCAT_PID_FILE"
echo "[logs][adb] not running"
return 0
fi
pid="$(awk 'NR==1{print $1}' "$ADB_LOGCAT_PID_FILE" 2>/dev/null || true)"
kill "$pid" 2>/dev/null || true
rm -f "$ADB_LOGCAT_PID_FILE"
echo "[logs][adb] stopped pid=$pid"
}
adb_logcat_status() {
if adb_logcat_is_running; then
echo "[logs][adb] running: $(cat "$ADB_LOGCAT_PID_FILE" 2>/dev/null)"
else
echo "[logs][adb] not running"
return 1
fi
}
adb_logcat_tail() {
local latest
latest="$(ls -1t "$LOG_DIR"/*.adb.*.log 2>/dev/null | head -n 1 || true)"
if [[ -z "$latest" ]]; then
echo "[logs][adb] no adb logs in $LOG_DIR"
exit 1
fi
echo "[logs][adb] tailing $latest"
tail -n 200 -f "$latest"
}
adb_logcat_ensure_running() {
local auto_on="${WBEAM_ADB_LOGCAT_AUTO:-1}"
if [[ "$auto_on" =~ ^(1|true|TRUE|yes|on)$ ]]; then
adb_logcat_start
fi
}
control_api_status() {
curl -sSf "http://127.0.0.1:${CONTROL_PORT}/status"
}
print_control_unreachable_diagnostics() {
local lines ts diag_dir diag_file
lines="${WBEAM_DAEMON_LOG_LINES:-40}"
ts="$(date '+%Y-%m-%d %H:%M:%S %z')"
diag_dir="$ROOT_DIR/.logs"
diag_file="$diag_dir/wbeam-daemon-status.log"
echo "Control API not reachable on 127.0.0.1:${CONTROL_PORT}"
mkdir -p "$diag_dir"
{
echo "[$ts] control API unreachable on 127.0.0.1:${CONTROL_PORT}"
echo "user=$(id -un) uid=${UID}"
echo
} >> "$diag_file"
echo
echo "Diagnostics saved to: $diag_file"
}
ip_up() {
require_cmd adb
adb start-server >/dev/null
adb_device_cmd wait-for-device
adb_device_cmd reverse "tcp:${STREAM_PORT}" "tcp:${STREAM_PORT}"
adb_device_cmd reverse "tcp:${CONTROL_PORT}" "tcp:${CONTROL_PORT}"
ip_status
}
ip_down() {
adb_device_cmd reverse --remove "tcp:${STREAM_PORT}" >/dev/null 2>&1 || true
adb_device_cmd reverse --remove "tcp:${CONTROL_PORT}" >/dev/null 2>&1 || true
ip_status
}
ip_status() {
adb_device_cmd version >/dev/null
echo "ADB reverse mappings:"
adb_device_cmd reverse --list || true
}
# Run the release binary in the foreground (no systemd, clean output)
host_run() {
ensure_daemon_binary
stop_local_processes
if command -v adb >/dev/null 2>&1; then
adb start-server >/dev/null 2>&1 || true
adb_device_cmd reverse "tcp:${STREAM_PORT}" "tcp:${STREAM_PORT}" 2>/dev/null || true
adb_device_cmd reverse "tcp:${CONTROL_PORT}" "tcp:${CONTROL_PORT}" 2>/dev/null || true
fi
echo "[wbeam] host run — release binary, foreground"
echo "[wbeam] control=127.0.0.1:${CONTROL_PORT} stream=127.0.0.1:${STREAM_PORT}"
echo "[wbeam] Ctrl-C to stop"
echo ""
export RUST_LOG="${RUST_LOG:-info}"
export WBEAM_USE_RUST_STREAMER=1
exec "$DAEMON_BIN" \
--control-port "$CONTROL_PORT" \
--stream-port "$STREAM_PORT" \
--root "$ROOT_DIR"
}
# Debug run: full RUST_LOG=debug, coloured output, ADB logcat sidecar
host_debug() {
exec "$HOST_SCRIPTS_DIR/run_wbeamd_debug.sh" "$CONTROL_PORT" "$STREAM_PORT"
}
# Backwards-compat aliases
daemon_up() { host_run; }
daemon_down() { stop_local_processes; echo "Daemon processes stopped."; }
daemon_status() {
if control_api_status >/dev/null 2>&1; then
control_api_status
echo
else
print_control_unreachable_diagnostics
exit 1
fi
}
host_probe() {
if command -v python3 >/dev/null 2>&1; then
exec python3 "$HOST_SCRIPTS_DIR/probe_host.py" "$@"
fi
if command -v python >/dev/null 2>&1; then
exec python "$HOST_SCRIPTS_DIR/probe_host.py" "$@"
fi
echo "Missing required command: python3 (or python)" >&2
exit 1
}
host_tuner() {
require_cmd cargo
android_select_serial_if_needed || true
local rust_dir="$ROOT_DIR/host/rust"
local tuner_stream_port="${WBEAM_STREAM_PORT:-$STREAM_PORT}"
local tuner_control_port="${WBEAM_CONTROL_PORT:-$CONTROL_PORT}"
local tuner_args=("--host-url" "http://127.0.0.1:${tuner_control_port}")
if [[ -n "$ANDROID_SERIAL" ]]; then
echo "[host] tuner using serial=$ANDROID_SERIAL" >&2
if android_try_reverse_setup "$tuner_stream_port" "$tuner_control_port"; then
echo "[host] tuner adb reverse OK (control:${tuner_control_port} stream:${tuner_stream_port})" >&2
else
echo "[host] WARN: tuner could not configure adb reverse (control:${tuner_control_port} stream:${tuner_stream_port})" >&2
fi
tuner_args+=("--serial" "$ANDROID_SERIAL")
tuner_args+=("--stream-port" "$tuner_stream_port")
else
echo "[host] tuner using default session (no serial selected)" >&2
fi
tuner_args+=("$@")
cd "$rust_dir" && cargo run --release -p wbeamd-tuner -- "${tuner_args[@]}"
}
android_build() {
local pipeline_mode min_sdk build_api_host build_stream_host gradle_user_home build_version
local build_stream_port build_control_port
pipeline_mode="$(android_pipeline_mode)"
min_sdk="$(android_pipeline_min_sdk "$pipeline_mode")"
build_api_host="${WBEAM_ANDROID_API_HOST:-${WBEAM_ANDROID_HOST:-$(android_build_host "$pipeline_mode")}}"
build_stream_host="${WBEAM_ANDROID_STREAM_HOST:-${WBEAM_ANDROID_HOST:-$(android_build_host "$pipeline_mode")}}"
build_stream_port="${WBEAM_STREAM_PORT:-$STREAM_PORT}"
build_control_port="${WBEAM_CONTROL_PORT:-$CONTROL_PORT}"
build_version="$(build_version_for_build)"
gradle_user_home="$ROOT_DIR/.gradle-user"
mkdir -p "$gradle_user_home"
echo "[android] Building debug APK (version=${build_version}, pipeline=${pipeline_mode}, minSdk=${min_sdk}, api_host=${build_api_host}, stream_host=${build_stream_host}, stream_port=${build_stream_port})…"
android_gradle_build_retry_once ":app:assembleDebug" "$min_sdk" "$build_api_host" "$build_stream_host" "$build_control_port" "$build_stream_port" "$gradle_user_home" "$build_version"
echo "[android] APK: $ANDROID_APK"
# Record the baked config so android_should_skip_install can detect host changes.
printf 'version=%s|api=%s|stream=%s|stream_port=%s|pipeline=%s' "$build_version" "$build_api_host" "$build_stream_host" "$build_stream_port" "$pipeline_mode" \
> "$(android_build_stamp_path "$ANDROID_APK")" 2>/dev/null || true
}
android_install_with_signature_recover() {
local apk_path="$1"
local package_name="$2"
local out=""
local rc=1
local install_ok=0
out="$(adb_device_cmd install -r "$apk_path" 2>&1)" || rc=$?
if [[ "$rc" -eq 0 ]] && ! printf '%s' "$out" | grep -q "Failure \["; then
install_ok=1
fi
if [[ "$install_ok" -eq 1 ]]; then
[[ -n "$out" ]] && printf '%s\n' "$out"
return 0
fi
[[ -n "$out" ]] && printf '%s\n' "$out" >&2
if printf '%s' "$out" | grep -Eq "INSTALL_FAILED_UPDATE_INCOMPATIBLE|INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES"; then
echo "[android] Detected signature mismatch for ${package_name}; uninstalling existing package and retrying..."
adb_device_cmd uninstall "$package_name" >/dev/null 2>&1 || true
adb_device_cmd shell pm uninstall --user 0 "$package_name" >/dev/null 2>&1 || true
rc=1
install_ok=0
out="$(adb_device_cmd install -r "$apk_path" 2>&1)" || rc=$?
if [[ "$rc" -eq 0 ]] && ! printf '%s' "$out" | grep -q "Failure \["; then
install_ok=1
fi
if [[ "$install_ok" -eq 1 ]]; then
[[ -n "$out" ]] && printf '%s\n' "$out"
return 0
fi
[[ -n "$out" ]] && printf '%s\n' "$out" >&2
fi