-
Notifications
You must be signed in to change notification settings - Fork 11
/
smart-lock
executable file
·1053 lines (904 loc) · 31.8 KB
/
smart-lock
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 -eufo pipefail
IFS=$'\n\t'
declare -r _SMART_LOCK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
declare -r command="$0"
declare -i arg_idx=0
declare -ra args=( "$@" )
## If the user has podman installed and in the path, prefer it over docker.
if [ -z "`type podman 2> /dev/null`" ]; then
DOCKER_COMMAND=docker
else
DOCKER_COMMAND=podman
fi
function end-of-input? {
[[ ${arg_idx} == ${#args[@]} ]]
}
function peek {
if end-of-input?; then
echo '#END#'
return
fi
# When peeking at the completion word, return a special token.
if [[ $((${arg_idx}+1)) == "${COMP_CWORD:-}" ]]; then
echo '#COMPLETION#'
return
fi
echo "${args+${args[${arg_idx}]:-}}"
}
function consume {
if end-of-input?; then
return
fi
arg_idx=$((arg_idx+1))
true
}
function command-prefix {
echo "${command} ${args+${args[@]:0:${arg_idx}}}"
}
function expected {
if [[ $((${arg_idx}+1)) == "${COMP_CWORD:-}" ]]; then
# Completion.
for x in "$@"; do
echo "${x}"
done | grep "^${args[$((${arg_idx}))]}"
exit
fi
if [[ "$(peek)" == 'help' ]]; then
echo "${_help_message}"
exit
fi
{
local -ra before=("${command}" "${args+${args[@]:0:${arg_idx}}}")
local -ra after=("${args+${args[@]:${arg_idx}}}")
echo "${before+${before[@]}} . ${after+${after[@]}}"
echo "Expected:"
for x in "$@"; do
echo " ${x}"
done
} >&2
exit 1
}
function expected-keywords {
expected "$@" help
}
function match-keyword {
if end-of-input? || [[ "$(peek)" != "$1" ]]; then
expected-keywords "$1"
fi
consume
}
function expected-file {
if [[ $((${arg_idx}+1)) == "${COMP_CWORD:-}" ]]; then
# Completion.
compgen -f "${args[$((${arg_idx}))]}"
exit
fi
{
echo "${command} ${args+${args[@]:0:${arg_idx}}} . ${args+${args[@]:${arg_idx}}}"
echo "Expected:"
echo " PATH"
} >&2
exit 1
}
function expected-end-of-input {
local -r token="$(peek)"
case "${token}" in
'#COMPLETION#')
# Offer help if completing at the end.
echo help
exit
;;
'#END#')
# Great.
return
;;
'help')
if [ -z "${COMP_CWORD:-}" ]; then
echo "${_help_message}"
fi
exit
;;
*)
{
echo "${command} ${args+${args[@]:0:${arg_idx}}} . ${args+${args[@]:${arg_idx}}}"
echo "Expected:"
echo " (end of arguments)"
} >&2
exit 1
esac
}
declare _help_message=
function set-help {
_help_message="$1"
}
declare _no_config=yes
declare config_path
function config {
match-keyword 'config'
unset _no_config
config_path="$(peek)"
case "${config_path}" in
'#COMPLETION#')
expected-file
;;
'#END#')
expected-file
;;
esac
consume
}
function load-config {
if [[ "${_no_config:-}" == yes ]]; then
if [ -r 'smart-lock-conf.sh' ]; then
echo "Loading config from smart-lock-conf.sh"
source smart-lock-conf.sh
else
echo "No config"
fi
return
fi
echo "Loading config from ${config_path}"
source "${config_path}"
}
declare pi_name
function to-pi {
pi_name="$(peek)"
case "${pi_name}" in
'#COMPLETION#')
load-config >/dev/null 2>&1
expected "${PI_NAMES[@]}"
;;
'#END#')
expected 'NAME'
;;
esac
consume
}
declare -r PI_LOCAL_IMAGE="pi-opendds-cross"
declare -r PI_GITHUB_IMAGE="ghcr.io/opendds/opendds-smart-lock:master-pi"
#Either pi-build-toolchain or pi-get-github-image must be run first to
#build the image for the toolchain or pull the existing image from GitHub.
function pi-build-toolchain {
#Build a Docker image containing a cross-compiler and cross-compiled dependencies
#ex: ./smart-lock pi build-toolchain
match-keyword 'build-toolchain'
expected-end-of-input
#add --no-cache if needed: "docker build --no-cache ..."
${DOCKER_COMMAND} build "${_SMART_LOCK_DIR}/dockerfiles/pi-opendds-cross" -f "${_SMART_LOCK_DIR}/dockerfiles/pi-opendds-cross/Dockerfile" -t ${PI_LOCAL_IMAGE}
}
function pi-get-github-image {
#Use the container image from GitHub Container Registry for the toolchain
#ex: ./smart-lock pi get-github-image
match-keyword 'get-github-image'
expected-end-of-input
${DOCKER_COMMAND} pull ${PI_GITHUB_IMAGE}
}
function pi-clean-toolchain {
#Remove the Docker image containing a cross-compiler and cross-compiled dependencies
#ex: ./smart-lock pi clean-toolchain
match-keyword 'clean-toolchain'
expected-end-of-input
local -r local_image_exists=$((`docker images ${PI_LOCAL_IMAGE} | wc -l`-1))
if [ $local_image_exists -eq 1 ]
then
${DOCKER_COMMAND} rmi ${PI_LOCAL_IMAGE}
fi
local -r github_image_exists=$((`docker images ${PI_GITHUB_IMAGE} | wc -l`-1))
if [ $github_image_exists -eq 1 ]
then
${DOCKER_COMMAND} rmi ${PI_GITHUB_IMAGE}
fi
}
function current-pi-image {
local -r local_image_exists=$((`${DOCKER_COMMAND} images ${PI_LOCAL_IMAGE} | wc -l`-1))
local -r github_image_exists=$((`${DOCKER_COMMAND} images ${PI_GITHUB_IMAGE} | wc -l`-1))
if [ $local_image_exists -eq 1 ]
then
echo ${PI_LOCAL_IMAGE}
elif [ $github_image_exists -eq 1 ]
then
echo ${PI_GITHUB_IMAGE}
else
echo "Image for pi not found!"
fi
}
function pi-install-dependencies {
# Copies all dependencies to target pi from docker image
# ex: ./smart-lock pi install-dependencies pi_1
match-keyword 'install-dependencies'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
# Copy the dependencies out of the docker image.
rm -rf "${_SMART_LOCK_DIR}/libs"
mkdir -p "${_SMART_LOCK_DIR}/libs"
local -r toolchain_image=`current-pi-image`
${DOCKER_COMMAND} run -d --name piopendds ${toolchain_image}
${DOCKER_COMMAND} cp piopendds:/home/pi/pi-opendds.tar.gz libs
${DOCKER_COMMAND} cp piopendds:/home/pi/pi-openssl.tar.gz libs
${DOCKER_COMMAND} cp piopendds:/home/pi/pi-xerces.tar.gz libs
${DOCKER_COMMAND} cp piopendds:/home/pi/pigpio.tar.gz libs
${DOCKER_COMMAND} stop piopendds
${DOCKER_COMMAND} rm piopendds
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "${_SMART_LOCK_DIR}/libs/pi-opendds.tar.gz" "${_SMART_LOCK_DIR}/libs/pi-openssl.tar.gz" "${_SMART_LOCK_DIR}/libs/pi-xerces.tar.gz" "${_SMART_LOCK_DIR}/libs/pigpio.tar.gz" "pi@${!ip_var}:"
# Unzip them.
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" 'tar xzf pi-opendds.tar.gz; tar xzf pi-openssl.tar.gz; tar xzf pi-xerces.tar.gz; tar xzf pigpio.tar.gz'
}
function pi-compile {
#Cross-compile the SmartLock Demo application
#ex: ./smart-lock pi compile
match-keyword 'compile'
expected-end-of-input
COMPILE="${_SMART_LOCK_DIR}/dockerfiles/pi-opendds-cross/pi-compile"
local -r toolchain_image=`current-pi-image`
MSYS_NO_PATHCONV=1 ${DOCKER_COMMAND} run --rm -it -v "${_SMART_LOCK_DIR}/src:/home/pi/smartlock" -w "/home/pi/smartlock" "${toolchain_image}" bash -c "$(tr '\n' ';' < ${COMPILE} | sed 's/\r//g')"
}
declare dpm_password
function dpm-password {
dpm_password="$(peek)"
case "${dpm_password}" in
'#END#')
expected 'password'
;;
esac
consume
}
function pi-install {
#Copy the compiled SmartLock Demo to target pi
#ex: ./smart-lock pi install pi_1
#To be used after build-toolchain, install-dependencies, and compile
match-keyword 'install'
to-pi
dpm-password
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
local -r lock_id_var="${pi_name}_lock_id"
cp "${_SMART_LOCK_DIR}/src/rtps.ini.template" "${_SMART_LOCK_DIR}/src/rtps.ini"
if [ -n "${RTPS_RELAY_IP_PI:-}" ]; then
let "RTPS_RELAY_PORT_PI_SEDP = RTPS_RELAY_PORT_PI + 1"
let "RTPS_RELAY_PORT_PI_DATA = RTPS_RELAY_PORT_PI + 2"
sed -e "s/#SPDP_RTPS_RELAY_ADDRESS_LINE/SpdpRtpsRelayAddress=${RTPS_RELAY_IP_PI}:${RTPS_RELAY_PORT_PI}/g" -e "s/#SEDP_RTPS_RELAY_ADDRESS_LINE/SedpRtpsRelayAddress=${RTPS_RELAY_IP_PI}:${RTPS_RELAY_PORT_PI_SEDP}/g" -e "s/#DATA_RTPS_RELAY_ADDRESS_LINE/DataRtpsRelayAddress=${RTPS_RELAY_IP_PI}:${RTPS_RELAY_PORT_PI_DATA}/g" -i'' "${_SMART_LOCK_DIR}/src/rtps.ini"
fi
rm -f "${_SMART_LOCK_DIR}/src/smartlock_env"
if [ ${ENABLE_SECURITY:-0} == 1 ]; then
echo "SMARTLOCK_SECURE=1" > "${_SMART_LOCK_DIR}/src/smartlock_env"
else
echo "SMARTLOCK_SECURE=0" > "${_SMART_LOCK_DIR}/src/smartlock_env"
fi
rsync -avz -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' "${_SMART_LOCK_DIR}/src/" "pi@${!ip_var}:~/smartlock"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" "sudo cp /home/pi/smartlock/systemd/smartlock.service /etc/systemd/system/smartlock.service; sudo chmod 0644 /etc/systemd/system/smartlock.service; sudo systemctl daemon-reload; sudo systemctl enable smartlock; echo '${dpm_password}' > /home/pi/dpm_password; echo '${!lock_id_var:-}' > /home/pi/smartlock.id"
}
function pi-start {
# Start the SmartLock Demo on target pi
# ex: ./smart-lock pi start pi_1
match-keyword 'start'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" 'sudo systemctl start smartlock'
}
function pi-stop {
#Stop the SmartLock Demo on target pi
# ex: ./smart-lock pi stop pi_1
match-keyword 'stop'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" 'sudo systemctl stop smartlock'
}
function pi-status {
#Show the status of the SmartLock on target pi
#ex: ./smart-lock pi status pi_1
match-keyword 'status'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" 'sudo systemctl status smartlock'
}
function pi-restart {
#Restart the SmartLock the Demo on the target pi
#ex: ./smart-lock pi restart pi_1
match-keyword 'restart'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" 'sudo systemctl restart smartlock'
}
function pi-logs {
# Show the logs for target pi
#ex: ./smart-lock pi logs pi_1
#Can be augmented with "follow"
match-keyword 'logs'
to-pi
declare follow=
local -r token="$(peek)"
case "${token}" in
follow)
match-keyword 'follow'
follow='-f'
;;
'#COMPLETION#')
expected-keywords 'follow'
;;
'#END#')
# Okay.
;;
esac
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" "sudo journalctl ${follow} -u smartlock"
}
function pi-ssh {
#ssh into target pi
#ex: ./smart-lock pi ssh pi_1
match-keyword 'ssh'
to-pi
expected-end-of-input
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}"
}
function pi-copy-public-key {
#Copy a public key to target pi
#ex: ./smart-lock copy-public-key ~/your/path/here
match-keyword 'copy-public-key'
to-pi
local -r path="$(peek)"
case "${path}" in
'#COMPLETION#')
expected-file
;;
'#END#')
expected-file
;;
esac
consume
expected-end-of-input
local -r key=$(cat "${path}")
load-config
local -r ip_var="${pi_name}_ip"
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "pi@${!ip_var}" "if ! [ -e /home/pi/.ssh/authorized_keys ] || ! grep -q '${key}' /home/pi/.ssh/authorized_keys; then echo '${key}' >> /home/pi/.ssh/authorized_keys; chmod 400 /home/pi/.ssh/authorized_keys; fi"
}
function pi {
#Maps commands to a pi version
#ex: ./smart-lock pi help
match-keyword 'pi'
local -r h=$(cat <<EOF
$(command-prefix) - Development tool for Raspberry Pis in the SmartLock Demo
Usage: $(command-prefix) COMMAND [OPTION ...]
Commands:
build-toolchain Build a Docker image containing a cross-compiler
and cross-compiled dependencies
get-github-image Use an existing Docker image from GitHub Container
Registry instead of building a local image
clean-toolchain Remove the Docker image containing a
cross-compiler and cross-compiled dependencies
compile Cross-compile the SmartLock Demo application
install-dependencies NAME Copy all dependencies to NAME
install NAME DPM_PASSWORD Copy the compiled SmartLock Demo application to
NAME. The DPM_PASSWORD is the DDS Permissions
Manager password for the SmartLock application.
start NAME Start the SmartLock Demo application on NAME
stop NAME Stop the SmartLock Demo application on NAME
status NAME Show the status of the SmartLock Demo
application on NAME
restart NAME Restart the SmartLock the Demo application on
NAME
ssh NAME ssh into NAME
logs NAME [follow] Show the logs for NAME
copy-public-key PATH Copy a public key to NAME
help Print this help
NOTE: You will need to export <NAME>_ip=xxx.xxx.xxx.xxx before running install,
start, stop, restart, ssh, or logs
EOF
)
set-help "${h}"
local -r token="$(peek)"
case "${token}" in
build-toolchain)
pi-build-toolchain
;;
get-github-image)
pi-get-github-image
;;
clean-toolchain)
pi-clean-toolchain
;;
compile)
pi-compile
;;
install-dependencies)
pi-install-dependencies
;;
install)
pi-install
;;
start)
pi-start
;;
stop)
pi-stop
;;
status)
pi-status
;;
restart)
pi-restart
;;
ssh)
pi-ssh
;;
logs)
pi-logs
;;
copy-public-key)
pi-copy-public-key
;;
*)
expected-keywords build-toolchain clean-toolchain compile install-dependencies install start stop status restart ssh logs copy-public-key
;;
esac
}
declare -r ANDROID_LOCAL_IMAGE="android-opendds-cross"
declare -r ANDROID_GITHUB_IMAGE_ARM64="ghcr.io/opendds/opendds-smart-lock:master-android-arm64"
declare -r ANDROID_GITHUB_IMAGE_ARM="ghcr.io/opendds/opendds-smart-lock:master-android-arm"
declare -r ANDROID_GITHUB_IMAGE_X86="ghcr.io/opendds/opendds-smart-lock:master-android-x86"
function android-build-toolchain-arm64 {
local commit='master'
match-keyword 'build-toolchain-arm64'
if ! end-of-input?; then
commit="$(peek)"
consume
fi
expected-end-of-input
#add --no-cache if needed: "docker build --no-cache ..."
${DOCKER_COMMAND} build --build-arg "OPENDDS_BRANCH=${commit}" "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross" -f "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross/Dockerfile" -t ${ANDROID_LOCAL_IMAGE}
}
function android-get-github-image-arm64 {
match-keyword 'get-github-image-arm64'
expected-end-of-input
${DOCKER_COMMAND} pull ${ANDROID_GITHUB_IMAGE_ARM64}
}
function android-build-toolchain-arm {
local commit='master'
match-keyword 'build-toolchain-arm'
if ! end-of-input?; then
commit="$(peek)"
consume
fi
expected-end-of-input
#add --no-cache if needed: "docker build --no-cache ..."
${DOCKER_COMMAND} build --build-arg ABI=armeabi-v7a --build-arg ABI_PREFIX=armv7a-linux-androideabi --build-arg RUNTIME_ROOT=arm-linux-androideabi --build-arg PLATFORM=android-arm --build-arg "OPENDDS_BRANCH=${commit}" "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross" -f "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross/Dockerfile" -t ${ANDROID_LOCAL_IMAGE}
}
function android-get-github-image-arm {
match-keyword 'get-github-image-arm'
expected-end-of-input
${DOCKER_COMMAND} pull ${ANDROID_GITHUB_IMAGE_ARM}
}
function android-build-toolchain-x86 {
local commit='master'
match-keyword 'build-toolchain-x86'
if ! end-of-input?; then
commit="$(peek)"
consume
fi
expected-end-of-input
#add --no-cache if needed: "docker build --no-cache ..."
${DOCKER_COMMAND} build --build-arg ABI=x86 --build-arg ABI_PREFIX=i686-linux-android --build-arg PLATFORM=android-x86 --build-arg "OPENDDS_BRANCH=${commit}" "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross" -f "${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross/Dockerfile" -t ${ANDROID_LOCAL_IMAGE}
}
function android-get-github-image-x86 {
match-keyword 'get-github-image-x86'
expected-end-of-input
${DOCKER_COMMAND} pull ${ANDROID_GITHUB_IMAGE_X86}
}
function android-clean-toolchain {
#Remove the Docker image containing a cross-compiler and cross-compiled dependencies
#ex: ./smart-lock android clean-toolchain
match-keyword 'clean-toolchain'
expected-end-of-input
local -r local_image_exists=$((`docker images ${ANDROID_LOCAL_IMAGE} | wc -l`-1))
if [ $local_image_exists -eq 1 ]
then
${DOCKER_COMMAND} rmi ${ANDROID_LOCAL_IMAGE}
fi
local -r github_image_exists_arm64=$((`docker images ${ANDROID_GITHUB_IMAGE_ARM64} | wc -l`-1))
if [ $github_image_exists_arm64 -eq 1 ]
then
${DOCKER_COMMAND} rmi ${ANDROID_GITHUB_IMAGE_ARM64}
fi
local -r github_image_exists_arm=$((`docker images ${ANDROID_GITHUB_IMAGE_ARM} | wc -l`-1))
if [ $github_image_exists_arm -eq 1 ]
then
${DOCKER_COMMAND} rmi ${ANDROID_GITHUB_IMAGE_ARM}
fi
local -r github_image_exists_x86=$((`docker images ${ANDROID_GITHUB_IMAGE_X86} | wc -l`-1))
if [ $github_image_exists_x86 -eq 1 ]
then
${DOCKER_COMMAND} rmi ${ANDROID_GITHUB_IMAGE_X86}
fi
}
function current-android-image {
local -r local_image_exists=$((`${DOCKER_COMMAND} images ${ANDROID_LOCAL_IMAGE} | wc -l`-1))
local -r github_image_exists_arm64=$((`${DOCKER_COMMAND} images ${ANDROID_GITHUB_IMAGE_ARM64} | wc -l`-1))
local -r github_image_exists_arm=$((`${DOCKER_COMMAND} images ${ANDROID_GITHUB_IMAGE_ARM} | wc -l`-1))
local -r github_image_exists_x86=$((`${DOCKER_COMMAND} images ${ANDROID_GITHUB_IMAGE_X86} | wc -l`-1))
if [ $local_image_exists -eq 1 ]
then
echo ${ANDROID_LOCAL_IMAGE}
elif [ $github_image_exists_arm64 -eq 1 ]
then
echo ${ANDROID_GITHUB_IMAGE_ARM64}
elif [ $github_image_exists_arm -eq 1 ]
then
echo ${ANDROID_GITHUB_IMAGE_ARM}
elif [ $github_image_exists_x86 -eq 1 ]
then
echo ${ANDROID_GITHUB_IMAGE_X86}
else
echo "Image for android not found!"
fi
}
function android-compile {
# Cross-compile the SmartLock Demo for android
#ex: ./smart-lock android compile
match-keyword 'compile'
expected-end-of-input
load-config
local -r locks=$(
echo -n '<string-array name="locks">';
for pi_name in "${PI_NAMES[@]}"; do
local lock_id_var="${pi_name}_lock_id";
if [ -n "${!lock_id_var:-}" ]; then
echo -n "<item>${!lock_id_var}<\/item>";
else
echo "WARNING: ${pi_name} has no lock id and will not be accessible" >&2;
fi
done
echo -n '<\/string-array>';
)
if [ -n "${RTPS_RELAY_IP_ANDROID:-}" ]; then
let "RTPS_RELAY_PORT_ANDROID_SEDP = RTPS_RELAY_PORT_ANDROID + 1"
let "RTPS_RELAY_PORT_ANDROID_DATA = RTPS_RELAY_PORT_ANDROID + 2"
sed -e "s/SpdpRtpsRelayAddress=.*/SpdpRtpsRelayAddress=${RTPS_RELAY_IP_ANDROID}:${RTPS_RELAY_PORT_ANDROID}/g" -e "s/SedpRtpsRelayAddress=.*/SedpRtpsRelayAddress=${RTPS_RELAY_IP_ANDROID}:${RTPS_RELAY_PORT_ANDROID_SEDP}/g" -e "s/DataRtpsRelayAddress=.*/DataRtpsRelayAddress=${RTPS_RELAY_IP_ANDROID}:${RTPS_RELAY_PORT_ANDROID_DATA}/g" -i'' "${_SMART_LOCK_DIR}/flutter/smartlock/assets/opendds_config.ini"
fi
## Within this set of shell commands, we copy SmartLock.idl into another
## directory. This allows the TypeSupport.idl file to find the original
## idl file.
COMPILE="${_SMART_LOCK_DIR}/dockerfiles/android-opendds-cross/android-compile"
local -r toolchain_image=`current-android-image`
MSYS_NO_PATHCONV=1 ${DOCKER_COMMAND} run --rm -ti -v "${_SMART_LOCK_DIR}:/home/droid/smartlock" -w '/home/droid' ${toolchain_image} bash -c "$(tr '\n' ';' < ${COMPILE} | sed 's/\r//g')"
## Determine which platform to target
local -r abi=`${DOCKER_COMMAND} run --rm -ti ${toolchain_image} bash -c 'echo -n $ANDROID_ABI'`
local target=""
case "$abi" in
arm64-v8a)
target=android-arm64
;;
armeabi-v7a)
target=android-arm
;;
x86)
target=android-x86
;;
x86_64)
target=android-x64
;;
*)
echo "WARNING: '$abi' not recognized."
;;
esac
(
if [ "$abi" = "x86" ]; then
echo "INFORMATION: A release apk cannot be built for x86"
else
cd "${_SMART_LOCK_DIR}/flutter/smartlock"
sed -i "s% abiFilter% //abiFilter%g; s%//abiFilter \"$abi\"%abiFilter \"$abi\"%" ../smartlock_idl_plugin/android/build.gradle
flutter build apk --split-per-abi --release --target-platform $target
fi
)
}
function android-clean {
#Remove the compiled application
#ex: ./smart-lock android clean
match-keyword 'clean'
expected-end-of-input
load-config
local -r toolchain_image=`current-android-image`
MSYS_NO_PATHCONV=1 ${DOCKER_COMMAND} run --rm -ti -v "${_SMART_LOCK_DIR}:/home/droid/smartlock" -w '/home/droid' ${toolchain_image} bash -c '
DDS_ROOT=/home/droid/droid-opendds;
source $DDS_ROOT/build/target/setenv.sh;
echo $HOME
cd $HOME/smartlock/flutter/Idl;
make clean
rm $HOME/smartlock/flutter/smartlock_idl_plugin/android/src/main/jniLibs/${ANDROID_ABI}/*.so;'
(
cd "${_SMART_LOCK_DIR}/flutter/smartlock"
flutter clean
)
}
function android-install {
#Install the SmartLock Demo application
#ex: ./smart-lock android install
match-keyword 'install'
expected-end-of-input
(
load-config
cd "${_SMART_LOCK_DIR}/flutter/smartlock"
local -r toolchain_image=`current-android-image`
local -r abi=`${DOCKER_COMMAND} run --rm -ti ${toolchain_image} bash -c 'echo -n $ANDROID_ABI'`
"${ANDROID_SDK_ROOT}/platform-tools/adb" -d install build/app/outputs/flutter-apk/app-$abi-release.apk
)
}
function android-ssh {
#SSH into the Android
#ex: ./smart-lock android ssh
match-keyword 'ssh'
expected-end-of-input
load-config
"${ANDROID_SDK_ROOT}/platform-tools/adb" shell
}
function android-start {
#Start the SmartLock Demo application
#ex: ./smart-lock android start
match-keyword 'start'
expected-end-of-input
(
load-config
cd "${_SMART_LOCK_DIR}/flutter/smartlock"
local -r toolchain_image=`current-android-image`
local -r abi=`${DOCKER_COMMAND} run --rm -ti ${toolchain_image} bash -c 'echo -n $ANDROID_ABI'`
flutter run --use-application-binary build/app/outputs/flutter-apk/app-$abi-release.apk
)
}
function android-stop {
#Stop the SmartLock Demo application
#ex: //smart-lock android stop
match-keyword 'stop'
expected-end-of-input
load-config
"${ANDROID_SDK_ROOT}/platform-tools/adb" shell am force-stop org.opendds.smartlock
}
function android-logs {
# Show logs from the SmartLock Demo
#ex: //smart-lock android logs
local dump_flags='-d'
match-keyword 'logs'
local -r token="$(peek)"
case "${token}" in
follow)
match-keyword 'follow'
dump_flags=''
;;
'#COMPLETION#')
expected-keywords 'follow'
;;
'#END#')
# Okay.
;;
esac
expected-end-of-input
load-config
local -r pid=$("${ANDROID_SDK_ROOT}/platform-tools/adb" shell 'ps -A -o PID,ARGS | grep smartlock | grep -v grep' | awk '{print $1}')
if [[ -z ${pid} ]]; then
echo "Smartlock Demo application is not running" >&2
return 1
fi
"${ANDROID_SDK_ROOT}/platform-tools/adb" logcat --pid="${pid}" "${dump_flags}"
}
function android {
#Maps commands to a android version
#ex: ./smart-lock android help
match-keyword 'android'
local -r h=$(cat <<EOF
$(command-prefix) - Development tool for Android in the SmartLock Demo
Usage: $(command-prefix) COMMAND [OPTION ...]
Commands:
build-toolchain-arm64 [commit] Build a Docker image containing arm64-v8a cross-compiler and cross-compiled dependencies
get-github-image-arm64 Use an existing Docker image from GitHub Container Registry for arm64 instead of building a local image
build-toolchain-arm [commit] Build a Docker image containing arm cross-compiler and cross-compiled dependencies
get-github-image-arm Use an existing Docker image from GitHub Container Registry for arm instead of building a local image
build-toolchain-x86 [commit] Build a Docker image containing x86 cross-compiler and cross-compiled dependencies
get-github-image-x86 Use an existing Docker image from GitHub Container Registry for x86 instead of building a local image
clean-toolchain Remove the Docker image containing a cross-compiler and cross-compiled dependencies
compile Cross-compile the SmartLock Demo application
clean Remove the compiled application
install Install the SmartLock Demo application
ssh SSH into the Android
start Start the SmartLock Demo application
stop Stop the SmartLock Demo application
logs [follow] Show logs from the SmartLock Demo application
help Print this help
EOF
)
set-help "${h}"
local -r token="$(peek)"
case "${token}" in
build-toolchain-arm64)
android-build-toolchain-arm64
;;
get-github-image-arm64)
android-get-github-image-arm64
;;
build-toolchain-arm)
android-build-toolchain-arm
;;
get-github-image-arm)
android-get-github-image-arm
;;
build-toolchain-x86)
android-build-toolchain-x86
;;
get-github-image-x86)
android-get-github-image-x86
;;
clean-toolchain)
android-clean-toolchain
;;
compile)
android-compile
;;
clean)
android-clean
;;
install)
android-install
;;
start)
android-start
;;
stop)
android-stop
;;
ssh)
android-ssh
;;
logs)
android-logs
;;
*)
expected-keywords build-toolchain-arm64 build-toolchain-arm build-toolchain-x86 clean-toolchain compile clean install start stop ssh logs
;;
esac
}
function ios-build-toolchain-arm64 {
match-keyword 'build-toolchain-arm64'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh --toolchain
}
function ios-build-toolchain-sim {
match-keyword 'build-toolchain-sim'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh --toolchain --simulator
}
function ios-compile-arm64 {
match-keyword 'compile-arm64'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh
cd $_SMART_LOCK_DIR/flutter/smartlock
flutter build ios
}
function ios-compile-sim {
match-keyword 'compile-sim'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh --simulator
cd $_SMART_LOCK_DIR/flutter/smartlock
flutter build ios --simulator
}
function ios-clean-toolchain {
match-keyword 'clean-toolchain'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh --toolchain --clean
}
function ios-clean {
match-keyword 'clean'
expected-end-of-input
cd $_SMART_LOCK_DIR/ios
./build.sh --clean
}
function ios {
#Maps commands to a ios version
#ex: ./smart-lock ios help
match-keyword 'ios'
local -r h=$(cat <<EOF
$(command-prefix) - Development tool for iOS in the SmartLock Demo
Usage: $(command-prefix) COMMAND [OPTION ...]
Commands:
build-toolchain-arm64 Build the arm64 cross-compiled dependencies
build-toolchain-sim Build the simulator cross-compiled dependencies
clean-toolchain Remove the arm64 & sim cross-compiled dependencies
compile-arm64 Cross-compile the arm64 SmartLock Demo application
compile-sim Cross-compile the arm64 SmartLock Demo application
clean Remove the compiled application
help Print this help
EOF
)
set-help "${h}"
local -r token="$(peek)"
case "${token}" in
build-toolchain-arm64)
ios-build-toolchain-arm64
;;
build-toolchain-sim)
ios-build-toolchain-sim
;;
clean-toolchain)
ios-clean-toolchain
;;
compile-arm64)
ios-compile-arm64
;;
compile-sim)
ios-compile-sim
;;
clean)
ios-clean
;;
*)
expected-keywords build-toolchain-arm64 build-toolchain-sim clean-toolchain compile-arm64 compile-sim clean
;;
esac
}
function top-level-command {
#sets up the command tree
# first branches are pi / android / ios / help / config
local -r h=$(cat <<EOF
$(command-prefix) - Development tool for the SmartLock Demo
Usage: $(command-prefix) [OPTION...] COMMAND ...