-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.tcl
609 lines (553 loc) · 12 KB
/
helpers.tcl
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
#
#
#
package require fa_sudo
namespace eval ::stash {
}
#
# logger - log a message
#
proc logger {text} {
puts stderr "[clock format [clock seconds] -format "%Y-%m-%d %H:%M:%SZ" -gmt 1] $text"
}
#
# fullscreen - resize the wish top-level window to be full screen
#
proc fullscreen {w} {
wm geometry $w $::fullscreenGeometry
}
# set the target top-level window size
proc size_screen {width height} {
set ::fullscreenGeometry "${width}x${height}"
set ::fullscreenHeight $height
set ::fullscreenWidth $width
if {$width == [winfo screenwidth .] && $height == [winfo screenheight .]} {
append ::fullscreenGeometry "+0+0"
set ::cursorStyle none
} else {
set ::cursorStyle arrow
}
fullscreen .
}
#
# comma - comma-separate a number, like 1234 becomes 1,234
#
proc comma {number} {
while {[regsub {^([-+]?\d+)(\d{3})} $number {\1,\2} number]} {}
return $number
}
#
# prompt_user_for_shutdown - prompts user for whether they want to shut down the
# flight feeder (after the main display program is stopped), shuts down if yes,
# does nothing otherwise.
proc prompt_user_for_shutdown {} {
while 1 {
puts -nonewline "\nWould you like to shut down the flight feeder (y/N)?"
#flush stdout
set response [ gets stdin ]
if {[ regexp -nocase {^y} $response ]} {
puts "okay, we're shutting down now."
::fa_sudo::exec_as -root -- /sbin/shutdown -P
} elseif {[ regexp -nocase {^n} $response ]} {
exit 0
break
}
}
}
# wifi_scan - return a list of wireless accesspoints ordered by highest quality connection
proc wifi_scan {interface} {
set ssidList ""
# interface must be up for scanning to work
try {
::fa_sudo::exec_as -root -- /bin/ip link set dev $interface up
} on error {result} {
puts stderr "caught '$result' running ip link set"
return ""
}
set wifiScan [::fa_sudo::open_as -root "|iwlist $interface scan"]
try {
while {[gets $wifiScan line] >= 0} {
if {[regexp {Cell \d+ - Address: ([0-9a-fA-F:]+)} $line -> newaddress]} {
if {[info exists ssid] && [info exists address]} {
if {[info exists quality]} {
lappend ssidList [list $ssid $address $quality]
} else {
lappend ssidList [list $ssid $address 0]
}
}
unset -nocomplain quality
unset -nocomplain ssid
set address $newaddress
} elseif {[regexp {Quality=(\d+)} $line -> quality]} {
# ok
} elseif {[regexp {ESSID:"(.+)"} $line -> ssid]} {
# ok
}
}
# last cell
if {[info exists ssid] && [info exist quality] && [info exists address]} {
lappend ssidList [list $ssid $address $quality]
}
return [lsort -integer -decreasing -index 2 $ssidList]
} on error {result} {
puts stderr "caught '$result' running wifi scan"
return ""
} finally {
catch {close $wifiscan}
}
}
#
# bgerror - write errors to the stderr instead of to the screen
#
proc bgerror {error} {
puts stderr "GUI error : $error"
puts stderr "$::errorInfo"
}
set ::countryDict [dict create {Afghanistan} AF \
{Åland Islands} AX \
{Albania} AL \
{Algeria} DZ \
{American Samoa} AS \
{Andorra} AD \
{Angola} AO \
{Anguilla} AI \
{Antarctica} AQ \
{Antigua and Barbuda} AG \
{Argentina} AR \
{Armenia} AM \
{Aruba} AW \
{Australia} AU \
{Austria} AT \
{Azerbaijan} AZ \
{Bahamas} BS \
{Bahrain} BH \
{Bangladesh} BD \
{Barbados} BB \
{Belarus} BY \
{Belgium} BE \
{Belize} BZ \
{Benin} BJ \
{Bermuda} BM \
{Bhutan} BT \
{Bolivia, Plurinational State of} BO \
{Bonaire, Sint Eustatius and Saba} BQ \
{Bosnia and Herzegovina} BA \
{Botswana} BW \
{Bouvet Island} BV \
{Brazil} BR \
{British Indian Ocean Territory} IO \
{Brunei Darussalam} BN \
{Bulgaria} BG \
{Burkina Faso} BF \
{Burundi} BI \
{Cabo Verde} CV \
{Cambodia} KH \
{Cameroon} CM \
{Canada} CA \
{Cayman Islands} KY \
{Central African Republic} CF \
{Chad} TD \
{Chile} CL \
{China} CN \
{Christmas Island} CX \
{Cocos (Keeling) Islands} CC \
{Colombia} CO \
{Comoros} KM \
{Congo} CG \
{Congo, the Democratic Republic of the} CD \
{Cook Islands} CK \
{Costa Rica} CR \
{Côte d'Ivoire} CI \
{Croatia} HR \
{Cuba} CU \
{Curaçao} CW \
{Cyprus} CY \
{Czechia} CZ \
{Denmark} DK \
{Djibouti} DJ \
{Dominica} DM \
{Dominican Republic} DO \
{Ecuador} EC \
{Egypt} EG \
{El Salvador} SV \
{Equatorial Guinea} GQ \
{Eritrea} ER \
{Estonia} EE \
{Eswatini} SZ \
{Ethiopia} ET \
{Falkland Islands (Malvinas)} FK \
{Faroe Islands} FO \
{Fiji} FJ \
{Finland} FI \
{France} FR \
{French Guiana} GF \
{French Polynesia} PF \
{French Southern Territories} TF \
{Gabon} GA \
{Gambia} GM \
{Georgia} GE \
{Germany} DE \
{Ghana} GH \
{Gibraltar} GI \
{Greece} GR \
{Greenland} GL \
{Grenada} GD \
{Guadeloupe} GP \
{Guam} GU \
{Guatemala} GT \
{Guernsey} GG \
{Guinea} GN \
{Guinea-Bissau} GW \
{Guyana} GY \
{Haiti} HT \
{Heard Island and McDonald Islands} HM \
{Holy See} VA \
{Honduras} HN \
{Hong Kong} HK \
{Hungary} HU \
{Iceland} IS \
{India} IN \
{Indonesia} ID \
{Iran, Islamic Republic of} IR \
{Iraq} IQ \
{Ireland} IE \
{Isle of Man} IM \
{Israel} IL \
{Italy} IT \
{Jamaica} JM \
{Japan} JP \
{Jersey} JE \
{Jordan} JO \
{Kazakhstan} KZ \
{Kenya} KE \
{Kiribati} KI \
{Korea, Democratic People's Republic of} KP \
{Korea, Republic of} KR \
{Kuwait} KW \
{Kyrgyzstan} KG \
{Lao People's Democratic Republic} LA \
{Latvia} LV \
{Lebanon} LB \
{Lesotho} LS \
{Liberia} LR \
{Libya} LY \
{Liechtenstein} LI \
{Lithuania} LT \
{Luxembourg} LU \
{Macao} MO \
{Macedonia, the former Yugoslav Republic of} MK \
{Madagascar} MG \
{Malawi} MW \
{Malaysia} MY \
{Maldives} MV \
{Mali} ML \
{Malta} MT \
{Marshall Islands} MH \
{Martinique} MQ \
{Mauritania} MR \
{Mauritius} MU \
{Mayotte} YT \
{Mexico} MX \
{Micronesia, Federated States of} FM \
{Moldova, Republic of} MD \
{Monaco} MC \
{Mongolia} MN \
{Montenegro} ME \
{Montserrat} MS \
{Morocco} MA \
{Mozambique} MZ \
{Myanmar} MM \
{Namibia} NA \
{Nauru} NR \
{Nepal} NP \
{Netherlands} NL \
{New Caledonia} NC \
{New Zealand} NZ \
{Nicaragua} NI \
{Niger} NE \
{Nigeria} NG \
{Niue} NU \
{Norfolk Island} NF \
{Northern Mariana Islands} MP \
{Norway} NO \
{Oman} OM \
{Pakistan} PK \
{Palau} PW \
{Palestine, State of} PS \
{Panama} PA \
{Papua New Guinea} PG \
{Paraguay} PY \
{Peru} PE \
{Philippines} PH \
{Pitcairn} PN \
{Poland} PL \
{Portugal} PT \
{Puerto Rico} PR \
{Qatar} QA \
{Réunion} RE \
{Romania} RO \
{Russian Federation} RU \
{Rwanda} RW \
{Saint Barthélemy} BL \
{Saint Helena, Ascension and Tristan da Cunha} SH \
{Saint Kitts and Nevis} KN \
{Saint Lucia} LC \
{Saint Martin (French part)} MF \
{Saint Pierre and Miquelon} PM \
{Saint Vincent and the Grenadines} VC \
{Samoa} WS \
{San Marino} SM \
{Sao Tome and Principe} ST \
{Saudi Arabia} SA \
{Senegal} SN \
{Serbia} RS \
{Seychelles} SC \
{Sierra Leone} SL \
{Singapore} SG \
{Sint Maarten (Dutch part)} SX \
{Slovakia} SK \
{Slovenia} SI \
{Solomon Islands} SB \
{Somalia} SO \
{South Africa} ZA \
{South Georgia and the South Sandwich Islands} GS \
{South Sudan} SS \
{Spain} ES \
{Sri Lanka} LK \
{Sudan} SD \
{Suriname} SR \
{Svalbard and Jan Mayen} SJ \
{Sweden} SE \
{Switzerland} CH \
{Syrian Arab Republic} SY \
{Taiwan, Province of China} TW \
{Tajikistan} TJ \
{Tanzania, United Republic of} TZ \
{Thailand} TH \
{Timor-Leste} TL \
{Togo} TG \
{Tokelau} TK \
{Tonga} TO \
{Trinidad and Tobago} TT \
{Tunisia} TN \
{Turkey} TR \
{Turkmenistan} TM \
{Turks and Caicos Islands} TC \
{Tuvalu} TV \
{Uganda} UG \
{Ukraine} UA \
{United Arab Emirates} AE \
{United Kingdom of Great Britain and Northern Ireland} GB \
{United States Minor Outlying Islands} UM \
{United States of America} US \
{Uruguay} UY \
{Uzbekistan} UZ \
{Vanuatu} VU \
{Venezuela, Bolivarian Republic of} VE \
{Viet Nam} VN \
{Virgin Islands, British} VG \
{Virgin Islands, U.S.} VI \
{Wallis and Futuna} WF \
{Western Sahara} EH \
{Yemen} YE \
{Zambia} ZM \
{Zimbabwe} ZW ]
proc detect_hardware {} {
if {![info exists ::hardware]} {
set ::hardware {}
catch {lappend ::hardware {*}[machine_architecture]}
catch {lappend ::hardware {*}[detect_pi_revision]}
catch {lappend ::hardware {*}[detect_usb_devices]}
catch {lappend ::hardware {*}[detect_display]}
}
return $::hardware
}
# detect machine architecture via uname
proc machine_architecture {} {
try {
return [list [::fa_sudo::exec_as -- uname -m]]
} on error {result} {
return ""
}
}
# detect Pi hardware based on contents of /proc/cpuinfo
# see http://elinux.org/RPi_HardwareHistory#Board_Revision_History
variable rpi_by_revision
array set rpi_by_revision {
0002 rpi_1b
0003 rpi_1b
0004 rpi_1b
0005 rpi_1b
0006 rpi_1b
0007 rpi_1a
0008 rpi_1a
0009 rpi_1a
000d rpi_1b
000e rpi_1b
000f rpi_1b
0010 rpi_1bplus
0011 rpi_compute
0012 rpi_1aplus
0013 rpi_1bplus
0014 rpi_compute
0015 rpi_1aplus
a01041 rpi_2b
a21041 rpi_2b
900092 rpi_zero
900093 rpi_zero
a02082 rpi_3b
a22082 rpi_3b
}
proc detect_pi_revision {} {
variable rpi_by_revision
set f [open "/proc/cpuinfo" "r"]
try {
while {[gets $f line] >= 0} {
if {[regexp {^Revision\s*:\s*(?:1000)?([0-9a-fA-F]+)} $line -> match]} {
set revision $match
}
}
} finally {
catch {close $f}
}
if {![info exists revision]} {
# not a Pi?
return ""
}
if {[info exists rpi_by_revision($revision)]} {
return [list $rpi_by_revision($revision) "rpi_${revision}"]
} else {
# we don't grok the revision
return [list "rpi_unknown" "rpi_${revision}"]
}
}
variable usb_by_id
array set usb_by_id {
"0bda:2832" "rtlsdr"
"0bda:2838" "rtlsdr"
}
variable usb_by_product
array set usb_by_product {
"bladeRF" "bladerf"
"Mode-S Beast GPS" "beastgps"
"Mode-S Beast" "beast"
}
# detect USB connected receivers
# we do this directly in /sys rather than using lsusb
# as lsusb won't give us the product strings unless
# running as root
proc detect_usb_devices {} {
set result ""
variable usb_by_id
variable usb_by_product
foreach pf [glob -nocomplain "/sys/bus/usb/devices/*"] {
if {![file readable "$pf/idVendor"] || ![file readable "$pf/idProduct"]} {
# not a device
continue
}
set f [open "$pf/idVendor" "r"]
try {
gets $f idVendor
} finally {
catch {close $f}
}
set f [open "$pf/idProduct" "r"]
try {
gets $f idProduct
} finally {
catch {close $f}
}
if {[info exists usb_by_id($idVendor:$idProduct)]} {
lappend result $usb_by_id($idVendor:$idProduct)
continue
}
if {[file readable "$pf/product"]} {
set f [open "$pf/product" "r"]
try {
gets $f product
} finally {
catch {close $f}
}
if {[info exists usb_by_product($product)]} {
lappend result $usb_by_product($product)
}
}
}
return $result
}
proc detect_display {} {
set sysfsPath "/sys/class/graphics/fb1/device/of_node/name"
set result ""
if {[file exists $sysfsPath]} {
set f [open $sysfsPath "r"]
try {
gets $f dtName
set dtName [string trimright $dtName "\0"] ;# strip trailing NUL
} finally {
catch {close $f}
}
if {$dtName ne ""} {
lappend result "lcd_$dtName"
}
}
return $result
}
#
# sshd_is_up - return 1 if sshd is active, else 0
#
proc sshd_is_up {} {
set active 1
set fp [open "|service ssh status"]
while {[gets $fp line] >= 0} {
if {[string match "*inactive*" $line]} {
set active 0
break
}
}
catch {close $fp}
return $active
}
#
# is_generating_ssh_keys - return 1 if ssh is currently generating ssh keys, else 0
#
proc is_generating_ssh_keys {} {
try {
set fp [open /var/log/regen_ssh_keys.log]
} trap {POSIX ENOENT} {} {
return 0
}
set generating 1
while {[gets $fp line] >= 0} {
if {[string match "finished*" $line]} {
set generating 0
break
}
}
close $fp
return $generating
}
#
# enable_sshd - turn on sshd to allow remote login via ssh
#
proc enable_sshd {} {
while {[is_generating_ssh_keys]} {
puts "ssh keys still generating, waiting..."
sleep 5
}
if {[sshd_is_up]} {
return
}
::fa_sudo::exec_as -root -- ssh-keygen -A
::fa_sudo::exec_as -root -- update-rc.d ssh enable
::fa_sudo::exec_as -root -- invoke-rc.d ssh start
}
#
# disable_sshd - turn off sshd to disable remote login via ssh
# and kill all active sessions
#
proc disable_sshd {} {
::fa_sudo::exec_as -root -- update-rc.d ssh disable
::fa_sudo::exec_as -root -- invoke-rc.d ssh stop
::fa_sudo::exec_as -root -- pkill --signal HUP sshd
}
# vim: set ts=4 sw=4 sts=4 noet :