-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
executable file
·1738 lines (1465 loc) · 50.9 KB
/
tasks.py
File metadata and controls
executable file
·1738 lines (1465 loc) · 50.9 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
from invoke import task
import os
import sys
import pexpect
from pexpect.exceptions import TIMEOUT
import time
from io import StringIO
HOME_PATH = os.environ['HOME']
DRONE_DEFAULT_IP = "192.168.2.222"
ESC_IMAGE_DIR = "/usr/local/tealflasher/images/"
FMU_IMAGE_DIR = "/usr/local/tealflasher/images/"
"""
Invoce reminders:
- @task decorator to define an exported task
Function name uses underscores, command name translates them dashes
- Function arguments become flags. If no default value, it is assumed to be
a string.
@task
def hi(ctx, name):
print("Hi {}".format(name))
Can be:
$ invoke hi Name
$ invoke hi --name Name
$ invoke hi --name=Name
$ invoke hi -n Name
$ invoke hi -nName
- Metadata via @task
- @task(help={'name': "Name of the person to say hi to"})
def hi(ctx, name):
<triple quote>
Say hi to someone.
<triple quote>
$ inv --help hi
Usage: inv[oke] [--core-opts] hi [--options] [other tasks here...]
Docstring:
Say hi to someone
Options:
-n STRING, --name=STRING Name of person to say hi to.
- invoke --list
- Run shell commands with:
ctx.run(quoted command)
- Declar pre-tasks
@task(<name-of-invoke-task>)
- Tab completion:
Add this to .zshrc
source <(invoke --print-completion-script zsh)
"""
#@task(help={'postfix_name':'Name to append to file - usually version info'})
#def px4_update_name(ctx, postfix_name):
# """
# Copy px4 bin with a new name
#
# Args:
# postfix_name (string): version info to append to string
# example: inv px4-update-name 1.9.13-01
# """
# # Make something to use to grab the output from the run so I can parse it.
# mystdout = StringIO()
#
# # 1. check for correct directory. Should see build directory
# ctx.run('ls -d */', out_stream=mystdout)
# lines = mystdout.getvalue().splitlines()
# if not "build/" in lines:
# print("Build dir not found. Are you in the project root?")
# return
#
# mystdout.flush()
# mystdout.truncate(0)
#
# # 2. build up new name. Use param
# new_name = 'teal_fmu-v5-mk1_' + postfix_name + '.bin'
# # 3. Figure out file names
# bin_path = './build/teal_fmu-v5-mk1_default/'
# src = bin_path + 'teal_fmu-v5-mk1.bin'
# dst = bin_path + new_name
# # 4. Copy existing to new name
# print('copy bin as:\n->', dst)
# ctx.run('cp {} {}'.format(src, dst))
# # 5. Optionally copy to image dir
# image_dst = '../../reference/images/{}'.format(new_name)
# ctx.run('cp {} {}'.format(src, image_dst))
#
# # 6. check
#
# ctx.run('ls -lF {}'.format(dst), out_stream=mystdout)
# ctx.run('ls -lF {}'.format(image_dst), out_stream=mystdout)
#
# #sys.stdout = old_stdout
# lines = mystdout.getvalue().splitlines()
#
# print("bin files:")
# for line in lines:
# print("->", line)
@task
def set_gpio(ctx, gpio_pin_number, value):
child = _spawn_shell(ctx)
if child == None:
print("Error connecting to drone")
sys.exit()
gpio_string = "echo " + str(value) + " > /sys/class/gpio/gpio"+str(gpio_pin_number)+"/value"
print("Sending:", gpio_string)
child.sendline(gpio_string)
@task
def reset_esc(ctx, esc_number=0):
child = _spawn_shell(ctx)
if child == None:
print("Error connecting to drone")
sys.exit()
start_esc = 0
end_esc = 3
if esc_number < 1 or esc_number > 4:
print("\nresetting all escs")
else:
start_esc = esc_number - 1
end_esc = esc_number - 1
for esc in range(start_esc, end_esc + 1):
gpio_number = 70 + esc
# print("gpio:", gpio_number)
time.sleep(0.5)
child.sendline("echo 1 > /sys/class/gpio/gpio"+str(gpio_number)+"/value")
time.sleep(0.5)
child.sendline("echo 0 > /sys/class/gpio/gpio"+str(gpio_number)+"/value")
#
# Generic shell
#
@task
def drone_shell(ctx):
"""
Connect to a Teal drone, either via ADB or SSH
ADB is attempted first, the SSH
"""
child = _get_drone_shell(ctx)
child.interact()
def _get_drone_shell(ctx):
return _wait_for_and_connect_adb_or_ip(ctx)
def _wait_for_and_connect_adb_or_ip(ctx):
iteration_count = 0
while True:
if _adb_is_present(ctx):
return _spawn_adb_shell(ctx, "")
elif _ip_is_present(ctx):
return _spawn_ssh_shell(ctx)
iteration_count += 1
if iteration_count == 3:
print("Waiting for ADB for SSH connection")
def _adb_is_present(ctx):
try:
ctx.run('adb devices | grep "td" &> /dev/null')
except:
return False
return True
def _ip_is_present(ctx, ip_address=DRONE_DEFAULT_IP):
response = os.system("ping -c 1 -t 1 " + ip_address + "&> /dev/null")
if response == 0:
return True
return False
CONNECTION_TIMEOUT = 1
CONNECTION_ADB = 2
CONNECTION_SSH = 3
@task
def drone_upload_file(ctx, local_file, remote_file):
ip_address = DRONE_DEFAULT_IP
connection_type = _find_drone_connection(ctx)
if connection_type == CONNECTION_ADB:
print("\nDrone on ADB")
print("ADB local file \"{}\" to remote \"{}\"".format(local_file, remote_file))
ctx.run("adb push {} {}/".format(local_file, remote_file))
elif connection_type == CONNECTION_SSH:
print("\nDrone on SSH")
_scp(ctx, local_file, remote_file, ip_address)
else:
print("\nDrone connection timeout")
@task
def drone_upload_esc(ctx, image_file):
child = _spawn_shell(ctx)
if child != None:
print("cd to {}".format(ESC_IMAGE_DIR))
child.sendline("cd {}".format(ESC_IMAGE_DIR))
print("wait for prompt")
_wait_for_prompt_via_expect(ctx, child)
print("renaming old esc bin")
child.sendline('for x in foc_esc*bin; do mv "$x" _"$x"; done')
print("waiting for prompt")
_wait_for_prompt_via_expect(ctx, child)
print("sending file")
drone_upload_file(ctx, image_file, ESC_IMAGE_DIR)
def _spawn_shell(ctx):
connection_type = _find_drone_connection(ctx)
if connection_type == CONNECTION_ADB:
return _spawn_adb_shell(ctx)
elif connection_type == CONNECTION_SSH:
return _spawn_ssh_shell(ctx)
else:
return None
def _find_drone_connection(ctx, timeout_seconds = 20):
iteration_count = 0
try:
while True:
if _adb_is_present(ctx):
return CONNECTION_ADB
elif _ip_is_present(ctx):
return CONNECTION_SSH
iteration_count += 1
if iteration_count == 1:
print("Waiting for ADB for SSH connection ", end="")
if iteration_count >= timeout_seconds:
return CONNECTION_TIMEOUT
time.sleep(1)
if iteration_count > 3:
_update_progress()
except KeyboardInterrupt:
sys.exit()
def _update_progress():
progresses = ["/", "-", "\\", "|"]
sys.stdout.write("\b%s" % progresses[_update_progress.progress_index])
sys.stdout.flush()
_update_progress.progress_index += 1
if _update_progress.progress_index > 3:
_update_progress.progress_index = 0
_update_progress.progress_index = 0
#####################################################
@task
def px4_update_name(ctx, version_info, move_to_images=False):
"""
Copy px4 default bin with a new name
Args:
postfix_name (string): version info to append to string
-m to move to reference dir
Example: inv px4-update-name v1.9.13-01 -m
"""
file_source_name = "teal_fmu-v5-mk1.bin"
file_extension = ".bin"
file_base_name = "teal_fmu-v5-mk1"
if len(version_info) != 0:
file_detail_name = "_" + version_info
else:
file_detail_name = ""
dest_file_name = file_base_name + file_detail_name + file_extension
print("Dest name:", dest_file_name)
print("copying ./build/teal_fmu-v5-mk1_default/teal_fmu-v5-mk1.bin to ./build/teal_fmu-v5-mk1_default/{}".format(dest_file_name))
ctx.run("cp ./build/teal_fmu-v5-mk1_default/teal_fmu-v5-mk1_default.bin ./build/teal_fmu-v5-mk1_default/{}".format(dest_file_name))
if move_to_images:
cmd = "cp ./build/teal_fmu-v5-mk1_default/{} {}/MattMcFadden/3-Resources/px4-images/{}".format(dest_file_name, HOME_PATH, dest_file_name)
ctx.run(cmd)
#print("cp ./build/teal_fmu-v5-mk1_default/{} /home/mattmc/MattMcFadden/3-Resources/px4-images/{}".format(dest_file_name, dest_file_name))
print(cmd)
@task
def px4_release_rename(ctx, version_info, move_to_images=False):
"""
Copy px4 default build files with the version appended to the name
Args:
postfix_name (string): version info to append to string
-m to move to reference dir
Example: inv px4-update-name 1.9.13-01 -m
"""
base_name = "teal_fmu-v6x-mk2*"
#cmd_str = 'cd build/' + base_name + '* ; for file in ' + base_name + '* ; do echo mv "${file}" "${file//default/v' + version_info + '}" ; done ; ls ' + base_name
cmd_str = 'cd build/' + base_name + '* ; for file in ' + base_name + '* ; do mv "${file}" "${file//default/v' + version_info + '}" ; done ; ls ' + base_name
ctx.run(cmd_str)
if move_to_images:
print("Not implemented yet")
#cmd = "cp ./build/teal_fmu*/{} {}/MattMcFadden/3-Resources/px4-images/{}".format(dest_file_name, HOME_PATH, dest_file_name)
#ctx.run(cmd)
##print("cp ./build/teal_fmu-v5-mk1_default/{} /home/mattmc/MattMcFadden/3-Resources/px4-images/{}".format(dest_file_name, dest_file_name))
#print(cmd)
@task
def px4_release(ctx):
"""Step through process for releasing a px4 image to teal-mk1-build"""
# 1.
@task
def help(ctx):
"""
invoke --list, plus other stuff
"""
ctx.run("inv --list")
print("Notes:")
print(" source <(invoke --print-completion-script bash)")
print(" cmd help: inv -h <command>")
@task
def create_project(ctx, project):
"""
Create my standard project directory structure.
"""
ctx.run("mkdir {}".format(project))
ctx.run("mkdir {}/data".format(project))
ctx.run("mkdir {}/notes".format(project))
ctx.run("mkdir {}/code".format(project))
ctx.run("echo \# Project {} Notes, Data, and Code >> {}/README.md".format(project, project))
@task
def adb_push_logger_conf(ctx):
"""
Replace the logger.conf file. Some tests currently corrupt it.
"""
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Current listing:")
ctx.run("adb shell ls -l /data/teal/mavlink-router/")
print("Updating logger.conf...")
ctx.run("adb push ~/code/teal-mk1-build/meta-teal-core/recipes-teal-log-handler/files/logger.conf /data/teal/mavlink-router/")
print("New listing:")
ctx.run("adb shell ls -l /data/teal/mavlink-router/")
@task
def adb_download_drone(ctx):
"""
Use ADB to download all logs and images from a MK1 drone.
"""
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Downloading log files")
ctx.run("adb pull /data/teal/flight-logs/")
print("Downloading SDCard")
ctx.run("adb pull /mnt/sdcard/")
@task
def adb_download_and_wipe_drone(ctx):
"""
Use ADB to downlaod and delete all logs and images from a MK1 drone.
"""
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Downloading log files")
ctx.run("adb pull /data/teal/flight-logs/")
print("Downloading SDCard")
ctx.run("adb pull /mnt/sdcard/")
adb_wipe_drone(ctx)
print("Check Drone:")
ctx.run("adb shell ls -l /data/teal/flight-logs")
print("Check Download:")
ctx.run("tree -s")
@task
def adb_wipe_drone(ctx):
"""
Use ADB to delete all logs and images from a MK1 drone.
"""
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Deleting log files")
ctx.run("adb shell 'rm /data/teal/flight-logs/*'")
print("Deleting SDCard")
ctx.run("adb shell 'rm /mnt/sdcard/*'")
ctx.run("adb shell 'sync'")
@task
def adb_replace_fmu(ctx, image_file):
"""
Use ADB to update the FMU only. Pushes a new image and calls tealflasher.sh fmu.
"""
IMAGE_DIR = "/usr/local/tealflasher/images"
SCRIPT_DIR = "/usr/local/tealflasher"
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Removing old PX4 image")
ctx.run("adb shell rm {}/teal_fmu-v5-mk1_*\.bin".format(IMAGE_DIR))
print("Copying {} to drone".format(image_file))
ctx.run("adb push {} {}".format(image_file, IMAGE_DIR))
print("Programming FMU image")
ctx.run("adb shell {}/tealflasher.sh fmu".format(SCRIPT_DIR))
@task
def adb_upload_fmu(ctx, image_file, adb_sn=""):
"""
Using an ADB connection, rename current version of FMU image to something the script won't recognize,
upload new version.
Args:
ctx (invoke context): Invoke Context
image_file (string): new image file name
adb_sn (string): optional adb device serial number
Returns:
nothing
"""
IMAGE_DIR = "/usr/local/tealflasher/images"
SCRIPT_DIR = "/usr/local/tealflasher"
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
child = _spawn_adb_shell(ctx, adb_sn)
child.sendline("cd {}".format(IMAGE_DIR))
_wait_for_prompt_adb_shell(ctx, child)
child.sendline('for x in teal_fmu*bin; do mv "$x" _"$x"; done')
_wait_for_prompt_adb_shell(ctx, child)
ctx.run("adb push {} {}/".format(image_file, IMAGE_DIR))
child.sendline('ls -l')
child.sendline('cd ..')
child.send('./tealflasher.sh fmu')
child.interact()
@task
def mav_shell(ctx, ip_address=DRONE_DEFAULT_IP):
"""
call the python mavlink-shell.py script
"""
cmd = "/Users/mmcfadden/bin/mavlink_shell.py tcp:"+ip_address+":5760"
progresses = ["/", "-", "\\", "|"]
progress_index = 0
while not _ip_is_present(ctx, ip_address):
sys.stdout.write("\b%s" % progresses[progress_index])
sys.stdout.flush()
progress_index += 1
if progress_index > 3:
progress_index = 0
time.sleep(0.1)
print("\b", end="")
time.sleep(1)
child = pexpect.spawn(cmd)
child.interact()
@task
def mav_shell_arm(ctx, ip_address=DRONE_DEFAULT_IP):
"""
call the python mavlink-shell.py script
"""
cmd = "/Users/mmcfadden/bin/mavlink_shell.py tcp:"+ip_address+":5760"
progresses = ["/", "-", "\\", "|"]
progress_index = 0
while not _ip_is_present(ctx, ip_address):
sys.stdout.write("\b%s" % progresses[progress_index])
sys.stdout.flush()
progress_index += 1
if progress_index > 3:
progress_index = 0
time.sleep(0.1)
print("\b", end="")
time.sleep(1)
child = pexpect.spawn(cmd)
time.sleep(10)
child.sendline("commander arm")
child.interact()
@task
def ssh_upload_fmu(ctx, image_file, ip_address=DRONE_DEFAULT_IP):
"""
Via an SSH connection, rename current version of FMU image to something the script won't recognize,
upload new version.
inv ssh-upload-fmu -i="192.168.1.222" teal_fmu-v5-mk1.bin
inv ssh-upload-fum teal_fmu-v5-mk1.bin
Args:
ctx (invoke context): Invoke Context
ip_address : IP of drone
image_file (string) : new image file name
Returns:
nothing
"""
if not os.path.isfile(image_file):
print("File \"{}\" not found.".format(image_file))
sys.exit(1)
IMAGE_DIR = "/usr/local/tealflasher/images/"
SCRIPT_DIR = "/usr/local/tealflasher"
print('spawning shell')
child = _spawn_ssh_shell(ctx, ip_address)
#child = _spawn_adb_shell(ctx, adb_sn)
print("cd to dir")
child.sendline("cd {}".format(IMAGE_DIR))
print("wait for prompt")
_wait_for_prompt_via_expect(ctx, child)
print("rename existing image")
child.sendline('for x in teal_fmu*bin; do mv "$x" _"$x"; done')
print("wait for prompt")
_wait_for_prompt_via_expect(ctx, child)
print("scp new image")
_scp(ctx, image_file, IMAGE_DIR, ip_address)
#ctx.run("adb push {} {}/".format(image_file, IMAGE_DIR))
#ctx.run("scp -i ~/.ssh/mk1-ssh-dev {} root@{}:{}".format(image_file, ip_address,IMAGE_DIR))
child.sendline('ls -l')
child.sendline('cd ..')
child.send('./tealflasher.sh fmu')
child.interact()
@task
def ssh_watch_radio(ctx, ip_of_radio="192.168.168.1", ip_address=DRONE_DEFAULT_IP):
"""
Watch radio via ssh
"""
child = _spawn_ssh_shell(ctx, ip_address)
ip_watch_radio(ctx, ip_of_radio)
@task
def adb_gimbal_stop_shell(ctx, s=""):
"""
Stop gimbal service before adb shell
"""
child = _spawn_adb_shell(ctx, s)
child.sendline("systemctl stop teal-gimbal-conman")
child.interact()
@task
def adb_update_gimbal_fw(ctx, image_file):
"""
Use ADB and the tealflasher script to update the gimbal firmware (only).
Pushes a new image and calls tealflasher.sh
"""
IMAGE_DIR = "/usr/local/tealflasher/images"
SCRIPT_DIR = "/usr/local/tealflasher"
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
print("Removing old gimbal FW image")
ctx.run("adb shell rm {}/teal_gimbal_*\.bin".format(IMAGE_DIR))
print("Copying {} to drone".format(image_file))
ctx.run("adb push {} {}".format(image_file, IMAGE_DIR))
print("Programming gimbal image")
ctx.run("adb shell {}/tealflasher.sh gimbal".format(SCRIPT_DIR))
@task
def adb_pairing_manager_disable(ctx):
"""
ADB to systemctl restart pairing-manager.
"""
ctx.run("adb wait-for-device && adb shell systemctl stop pairing-manager && adb shell systemctl disable pairing-manager")
@task
def adb_pairing_manager_enable(ctx):
"""
ADB to systemclt enable and start pairing-manager
"""
ctx.run("adb wait-for-device && adb shell systemctl enable pairing-manager && adb shell systemctl start pairing-manager")
@task
def adb_dcm_disable(ctx):
"""
ADB to stop and disable DCM.
"""
ctx.run("adb wait-for-device && adb shell systemctl stop dcm && adb shell systemctl disable dcm")
@task
def adb_dcm_enable(ctx):
"""
ADB to enable and start DCM.
"""
ctx.run("adb wait-for-device && adb shell systemctl enable dcm && adb shell systemctl start dcm")
@task
def adb_comment_out_qgc_endpoint(ctx):
"""
Comment out (disable) mavlink-router main.conf QGC Endpoint.
"""
ctx.run("adb wait-for-device")
ctx.run("adb shell {}".format(sed_comment_qgc_endpoint))
@task
def adb_uncomment_out_qgc_endpoint(ctx):
"""
Uncomment (enable) mavlink-router main.conf QGC Endpoint.
"""
ctx.run("adb wait-for-device")
ctx.run("adb shell {}".format(sed_uncomment_qgc_endpoint))
@task
def ssh_comment_out_qgc_endpoint(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH comment out (disable) mavlink-router main.conf QGC Endpoint.
"""
child = _spawn_ssh_shell(ctx, ip_address)
child.sendline(sed_comment_qgc_endpoint)
child.interact()
@task
def ssh_uncomment_out_qgc_endpoint(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH uncomment (enable) mavlink-router main.confg QGC Endpoint.
"""
child = _spawn_ssh_shell(ctx, ip_address)
child.sendline(sed_uncomment_qgc_endpoint)
child.interact()
@task
def ssh_log_while_armed(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH change logging to while armed
"""
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip_address, port = 22, username = 'root', password = 'oelinux123') #connect to drone
ssh.exec_command(sed_comment_log_always)
ssh.exec_command(sed_uncomment_log_while_armed)
ssh.close()
@task
def ssh_log_always(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH change logging to always
"""
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip_address, port = 22, username = 'root', password = 'oelinux123') #connect to drone
ssh.exec_command(sed_uncomment_log_always)
ssh.exec_command(sed_comment_log_while_armed)
ssh.close()
@task
def adb_log_while_armed(ctx):
"""
ADB change logging to while armed
"""
ctx.run("adb wait-for-device")
always = "adb shell {}".format(sed_comment_log_always)
armed = "adb shell {}".format(sed_uncomment_log_while_armed)
ctx.run(always)
ctx.run(armed)
@task
def adb_log_always(ctx, s=""):
"""
ADB change logging to always
"""
ctx.run("adb wait-for-device")
always = "adb shell {}".format(sed_uncomment_log_always)
armed = "adb shell {}".format(sed_comment_log_while_armed)
ctx.run(always)
ctx.run(armed)
@task
def adb_show_version_file(ctx):
"""
ADB to cat /etc/versionfile
"""
print("Waiting for ADB connection...")
ctx.run("adb wait-for-device")
ctx.run("adb shell cat /etc/versionfile")
@task
def adb_push_drone_profile(ctx):
"""
ADB to push ~/util/drone_profile
"""
ctx.run("adb wait-for-device")
ctx.run("adb push ~/util/drone_profile /")
@task
def adb_shell(ctx, s=""):
"""
ADB shell.
"""
child = _spawn_adb_shell(ctx, s)
child.interact()
@task
def adb_gimbal_stop_shell(ctx, s=""):
"""
Stop gimbal service before adb shell
"""
child = _spawn_adb_shell(ctx, s)
child.sendline("systemctl stop teal-gimbal-conman")
child.interact()
@task
def adb_gimbal_stop_run_util(ctx, s=""):
"""
Stop gimbal service before adb shell
"""
child = _spawn_adb_shell(ctx, s)
child.sendline("systemctl stop teal-gimbal-conman && teal_gimbal_util /dev/ttyHS5")
child.interact()
@task
def adb_gimbal_util(ctx, s=""):
"""
Run gimbal util from /home/root
"""
child = _spawn_adb_shell(ctx, s)
child.sendline("teal_gimbal_util /dev/ttyHS5")
child.interact()
@task
def adb_gimbal_screen(ctx, s=""):
"""
Run screen /dev/ttyHS5 460800 to connect to the gimbal uart.
"""
child = _spawn_adb_shell(ctx, s)
child.sendline("screen /dev/ttyHS5 460800")
child.interact()
def _scp(ctx, src_file_path, target_file_path, ip_address=DRONE_DEFAULT_IP):
print("src:", src_file_path)
print("trg:", target_file_path)
print("ip:", ip_address)
cmd = "scp -O -i ~/.ssh/mk1-ssh-dev {} root@{}:{} ".format(src_file_path, ip_address, target_file_path)
print("cmd:", cmd)
child = pexpect.spawn(cmd)
while True:
response = child.expect([pexpect.TIMEOUT, pexpect.EOF, '[#$]'])
#print("==> got response:", response, child.before, child.after)
if response == 0:
print("SCP TIMEOUT")
return None
elif response == 1:
print("SCP EOF")
return None
elif response == 2: #prompt
print("SCP PROMPT")
return child
def _spawn_ssh_shell(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH Shell.
"""
# clear any existing key to facilitate quickly moving from device to device,
# or same device with new keys
#print("Clearing existing SSH keys for {}".format(ip_address))
#ssh_clearkey = 'ssh-keygen -f "' + os.path.expanduser("~") + '/.ssh/known_hosts" -R ' + ip_address + ' &>/dev/null'
#os.system(ssh_clearkey)
new_key = 'Are you sure you want to continue connecting'
host_id_changed = 'HOST IDENTIFICATION HAS CHANGED'
ssh_keygen = 'ssh-keygen -f "{}/.ssh/known_hosts" -R {}'.format(HOME_PATH, ip_address)
ssh_cmd = 'ssh root@{} -i {}/.ssh/mk1-ssh-dev'.format(ip_address, HOME_PATH)
child = pexpect.spawn(ssh_cmd)
x = 0
while True:
response = child.expect([pexpect.TIMEOUT, pexpect.EOF, '[#$]', new_key, 'password:', host_id_changed])
#print("==> got response:", response)
if response == 0:
#print("-->got timeout.")
return None
elif response == 1:
print("EOF detected")
break
elif response == 2: #prompt
#print("-->got prompt")
_set_environment(child)
print(child.before.decode() + child.after.decode(), end='')
break
elif response == 3: # new key
#print("-->got new key")
print(child.before, child.after)
child.sendline('yes')
# time.sleep(0.1)
time.sleep(1)
elif response == 4: # password
#print("-->got password")
child.sendline("oelinux123")
#resp = child.expect([pexpect.TIMEOUT, '[#$]'])
elif response == 5: # host id changed
#print("-->got HOST CHANGED in _ssh_shell")
#print(child.before, child.after)
#print("sending ssh-keygen")
ssh_clear_keys(ctx, ip_address)
child = pexpect.spawn("ssh root@{} -i {}/.ssh/mk1-ssh-dev".format(ip_address, HOME_PATH))
else:
#print("-->break")
break
return child
@task(help={'ip_address':"IP address of drone"})
def ssh_shell(ctx, ip_address=DRONE_DEFAULT_IP):
child = _spawn_ssh_shell(ctx, ip_address)
#_send_alias_commands(child)
#_set_stty(child)
child.interact()
@task
def ssh_enable_adb(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH with mk1-ssh-dev key, enable adb
"""
child = _spawn_ssh_shell(ctx, ip_address)
print("sending {}".format(sed_enable_start_adbd))
child.sendline(sed_enable_start_adbd)
while True:
i = child.expect([pexpect.TIMEOUT, pexpect.EOF, '[#$]', 'fingerprint'])
if i == 0:
print(child.before, child.after)
return None
elif i == 1:
print("EOF")
break
elif i == 2: #prompt
print("PROMPT")
child.sendline()
command_list = "alias ls=\'ls -F\'; alias ll='ls -l'; alias systemctl='systemctl --no-pager'; cd /data/teal; alias pst='ps | grep teal'"
child.sendline(command_list)
break
elif i == 3: # fingerprint
print("got fingerprint request")
print(child.before, child.after)
child.sendline("yes")
break
else:
print(child.before, child.after)
break
@task
def ssh_disable_adb(ctx, ip_address=DRONE_DEFAULT_IP):
"""
SSH with mk1-ssh-dev key, enable adb
"""
child = _spawn_ssh_shell(ctx, ip_address)
print("sending {}".format(sed_disable_start_adbd))
child.sendline(sed_disable_start_adbd)
while True:
i = child.expect([pexpect.TIMEOUT, pexpect.EOF, '[#$]', 'fingerprint'])
if i == 0:
print(child.before, child.after)
return None
elif i == 1:
print("EOF")
break
elif i == 2: #prompt
print("PROMPT")
break
elif i == 3: # fingerprint
print("got fingerprint request")
print(child.before, child.after)
child.sendline("yes")
break
else:
print(child.before, child.after)
break
@task
def adb_radio_telnet(ctx):
"""
ADB shell telnet to radio
"""
ctx.run("adb wait-for-device")
child = pexpect.spawn("adb shell 'telnet 192.168.168.1'")
while True:
response = child.expect([pexpect.TIMEOUT, 'login:', 'assword:', '[#$>]'])
if response == 0:
print(child.before, child.after)
return None
elif response == 1:
child.sendline("admin")
elif response == 2:
child.sendline("teamteal")
elif response == 3:
child.sendline("at+mwstatus")
break
else:
print("What?")
print(child.before, child.after)
break;
child.interact()
@task
def adb_push_conman(ctx):
"""
Push gimbal conman and util to bin after rename
"""
ctx.run("adb wait-for-device")
# stop conman
ctx.run("adb shell systemctl stop teal-gimbal-conman")
ctx.run("adb shell systemctl --no-pager status teal-gimbal-conman")
# archive current versions
# copy new versions into bin dir
ctx.run("adb push ./teal_gimbal_conman /usr/bin/")
ctx.run("adb push ./teal_gimbal_util /usr/bin/")
@task
def adb_watch_radio(ctx, s="", ip_of_radio='192.168.168.1'):
"""
ADB to loop on AT+MWSTATUS
"""
while True:
# print("Wait for adb")
cmd = add_adb_serial_number_param("wait-for-device", s)
ctx.run(cmd)
tcmd = "telnet {}".format(ip_of_radio)