This file is indexed.

/usr/lib/gpsman/lowrance.tcl is in gpsman 6.4.4.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
#
# This file is part of:
#
#  gpsman --- GPS Manager: a manager for GPS receiver data
#
# Copyright (c) 1998-2013 Miguel Filgueiras migfilg@t-online.de
#
#    This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 3 of the License, or
#      (at your option) any later version.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      You should have received a copy of the GNU General Public License
#      along with this program.
#
#########################################################################
#
#  This file (lowrance.tcl) - Author - Brian Baulch
#    (baulchb@hotkey.net.au) is based on the file garmin.tcl (Author
#     Miguel Filgueiras)
#  Copyright (c) Brian Baulch 1999, 2000
# 
#  Last change: 26 Jan 2002
#  
# All the text above and including these lines MUST be reproduced in full
#  by any person modifying or copying this file.
##########################################################################

# Includes changes/contributions by Miguel Filgueiras marked
#   "MF change" or "MF contribution"

# Date last modified: 19 June 2007

set SERIALLOG ~/.gpsman-log
set NotLogging 1
set LogMutex 0

proc Log {m} {
    global NotLogging LogFile LogMutex

    if { $NotLogging } { return }
    if { $LogMutex } {
	after 50 "Log {$m}"
	return
    }
    set LogMutex 1
    puts $LogFile $m
    flush $LogFile
    set LogMutex 0
    return
}

# Constants needed by the Mercator conversion routines.

set RADtoDEG 57.29577951308232087684
set PI 3.14159265358979

# Correction constant (in days) to convert GPSMan time to Lowrance time.

set GPSMANDAYS 1461

# The LSI100 command preamble - actually 0x8155, but must be in decimal
# format to allow comparison under TCL.
 
set PREAMBLE 33109

# Lowrance Products & Software

set PRODID {dummy GlobalMap AirMap AccuMap {GlobalNav 310} {Eagle View} \
    {Eagle Explorer} {GlobalNav 200} {Expedition II} {GlobalNav 212} \
    {GlobalMap 12} {Accumap 12}}

set PROTV {{Version 1.0} {Version 2.0} {Version 3}}

# Lowrance Commands:

set CMD(GetMEM) 8
set CMD(GetWP) 771
set CMD(GetRT) 773
set CMD(GetTRp) 775
set CMD(GetTR) 786
set CMD(GetTRd) 787
set CMD(GetICno) 776
set CMD(GetICpos) 777
set CMD(GetInfo) 782
set CMD(PutMEM) 9
set CMD(PutWP) 772
set CMD(PutRT) 774
set CMD(PutTRd) 789
set CMD(PutTR) 788
set CMD(PutICno) 778
set CMD(PutIC) 779

set PDTYPE(Header) {word word word byte}
set PDTYPE(GetMEM) {long word byte}
set PDTYPE(GetWP) {byte word byte byte long long charray=13 long}
set PDTYPE(PutWP) {word byte byte long long charray=13 long}
set PDTYPE(GetRT) {byte word byte charray=13}
set PDTYPE(PutRT) {word byte charray=13 wordarray}
set PDTYPE(GetICno) {byte word}
set PDTYPE(GetICpos) {byte word long long byte}
set PDTYPE(PutICno) {word}
set PDTYPE(PutIC) {word long long byte}
set PDTYPE(GetICgrn) {byte word}
set PDTYPE(GetICgr) {byte byte byte byte long}
set PDTYPE(GPSInfo) {byte word word word word word word word word \
	word byte byte byte long}
set PDTYPE(GetTR) {byte byte long long word}
set PDTYPE(GetTRd) {byte byte word}
set PDTYPE(PutTR) {byte long long word} 
set PDTYPE(PutTRd) {byte word}

set TIMEOUT 1000

#############################################################

proc ErrorMess {mess} {

    global MessSent MESS

    if { ! $MessSent} {
	set MessSent 1
	GMMessage $MESS($mess)
	set ReadState done
	ResetCursor .
	DisableGPS
	GPSOff
    }
    return
}

proc ReadProc {what} {

    global ReadState

    set data [ReadInterface $what]
    ProcessRead $what $data
    set ReadState done
    return
}

proc TimeOutProc {} {

    global Id ReadState

    if { [string compare $ReadState done] } {
	after cancel $Id
	ErrorMess {receiver}
    }
    return
}
	
proc GPSInterface {what data} {

    # what, as in Get?? Put?? etc. as above under CMD(???)

    global Id NotLogging ReadState TIMEOUT SRLFILE \

    set ReadState ""
    if {! $NotLogging} {
	foreach b $data {
	    binary scan $b "c" x
	    lappend log $x
	}
	Log "SP> $log"
    }  
    foreach b $data {
	puts -nonewline $SRLFILE "$b"
    }
    flush $SRLFILE
    set Id [after 0 ReadProc $what]
    after $TIMEOUT TimeOutProc
    while { [ string compare $ReadState "done"] } { update }
    return
}

proc ReadInterface {what} {

    global NotLogging PREAMBLE SRLFILE MessSent PDTYPE CMD

    if { ! $MessSent } {
	set header ""
	for { set i 1 } { $i < 9 } { incr i } {
	    set ch ""
	    while { [string length $ch] == 0 } { 
		set ch [read $SRLFILE 1]
		update
	    }
	    append header $ch
	}
	set inheader [split $header ""]
	if {! $NotLogging} {
	    set log ""
	    foreach b $inheader {
		binary scan $b "c" x
		lappend log $x
	    }
	    Log "SP< $log"
	}
	set sumin [lindex $inheader "end"]
	set inheader [lreplace $inheader "end" "end"]
	set sumcalc [CheckSum $inheader]
	if {$sumcalc != $sumin} {
	    ErrorMess {checksum1}
	    return
	}
	set header [UnPackData $inheader $PDTYPE(Header)]
	if {[lindex $header 0] != $PREAMBLE} {
	    ErrorMess {serial1}
	    return
	}
	if {[lindex $header 1] != ($CMD($what) + 128)} {
	    ErrorMess {serial2}
	    return
	}
	set bytecount [lindex $header 2]
	set data ""
	if {$bytecount > 0} {
 	    set bytecount [incr bytecount 2]
	    for { set i 1 } { $i < $bytecount } { incr i } {
		set ch ""
		while { [string length $ch] == 0 } { 
		    set ch [read $SRLFILE 1]
		    update
		}
		append bdata $ch
	    }
	    set data [split $bdata ""]
	    if {! $NotLogging} {
		set log ""
		foreach b $data {
		    binary scan $b "c" x
		    lappend log $x
		}
		Log $log
	    }
	    set sumin [lindex $data "end"]
	    set data [lreplace $data "end" "end"]
	    set sumcalc [CheckSum $data]
	    if {$sumcalc != $sumin} {
		ErrorMess {checksum2}
		set data ""
		return
	    }
	}
	after cancel TimeOutProc
    } 
    return $data
}

proc ProcessRead {what data} {

    switch $what {
	    GetWP {
		WayPoint $data
	    }
	    GetRT {
		Routes $data
	    }
	    GetTR {
		TrailOrigin $data
	    }
	    GetTRd {
		TrailDelta $data
	    }
	    GetICno {
		IconCount $data
	    }
	    GetICpos {
		IconPosn $data
	    }
	    GetInfo {
		ProductInfo $data
	    }
	    PutTR -
	    PutTRd {
		PutTrail
	    }
	}
return
}

proc WayPoint {data} {

global PDTYPE WNames WName WNum WSymb WLatitude WLongitude WDate

    set data [UnPackData $data $PDTYPE(GetWP)]
    if [lindex $data 2] {
	set name [lindex $data 6]
	set WNum($name) [lindex $data 1]
	lappend WNames $name
	set WName($WNum($name)) $name
	set WSymb($name) [lindex $data 3]
	set merclat [lindex $data 4]
	set WLatitude($name) [Merc2Lat $merclat]
	set merclong [lindex $data 5]
	set WLongitude($name) [Merc2Long $merclong]
	set WDate($name) [lindex $data 7]
#	ListWayPoints $name
    }
    return
}

proc ListWayPoints {} {

global WNames WNum WSymb WLatitude WLongitude WDate UnusedWPInx

    set bulkdata ""
    foreach name $WNames {
	set data $WNum($name)
	lappend data $WSymb($name) $WLatitude($name) $WLongitude($name) \
		$name $WDate($name)                
	lappend bulkdata $data
	set inx [lsearch -exact $UnusedWPInx $WNum($name)] 
	set UnusedWPInx [lreplace $UnusedWPInx $inx $inx]
    }
    return $bulkdata
}

proc Routes {data} {

global PDTYPE RNum RPoints RNames

    set pts ""
    set packdata [lrange $data 0 16]
    set data [lreplace $data 0 16] 
    set rtdata [UnPackData $packdata $PDTYPE(GetRT)]
    set points [lindex $rtdata 2]
    if { $points } {
	if { [string compare [lindex $rtdata 3] ""] } {
	    set name [lindex $rtdata 3]
	} else {
	    set num [expr [lindex $rtdata 1] + 1]
	    set name ROUTE$num
	}
	lappend RNames $name
	set RNum($name) [expr [lindex $rtdata 1] + 1] 
	set i 0 ; set x 0 ; set y 1
 	while {$i < $points} {
	   binary scan [join [list [lindex $data $x] \
		[lindex $data $y]] ""] "s" p  
	   lappend pts $p
	   incr x 2 ; incr y 2 ; incr i
	}
	set RPoints($name) $pts
    }
    return
}

proc ListRoutes {} {

global PDTYPE RNames RNum RPoints

    set bulkdata ""
    foreach n $RNames {
	set name $n
	set data $RNum($name)
	lappend data $name $RPoints($name)
	lappend bulkdata $data
    }
    return $bulkdata
}

proc IconCount {data} {

global PDTYPE BulkData

    set BulkData ""
    set data [UnPackData $data $PDTYPE(GetICno)]
    set count [lindex $data 1]
    for {set i 0} {$i < $count} {incr i} {
	SendData GetICpos $i
    }
    return
}
 
proc IconPosn {data} {

global PDTYPE UnusedICInx BulkData

    set d [UnPackData $data $PDTYPE(GetICpos)]
    set data [lindex $d 1]
    set merclat [lindex $d 2]
    set inx [lsearch -exact $UnusedICInx $data] 
    set UnusedICInx [lreplace $UnusedICInx $inx $inx]
    set merclong [lindex $d 3]
    lappend data [Merc2Lat $merclat] [Merc2Long $merclong] [lindex $d 4]
    lappend BulkData $data
    return
}
    
proc ProductInfo {data} {

global PDTYPE PRODID PROTV MAX Prod Protv

    set data [UnPackData $data $PDTYPE(GPSInfo)]
    set prod [lindex $data 1]
    set Prod [lindex $PRODID $prod]
    set vn [lindex $data 2]
    set Protv [lindex $PROTV $vn]
#    set MAX(WP) [lindex $data 6]
#    set MAX(RT) [lindex $data 8]
    # MF change: using the user defaults
    global MAXWPOINTS MAXROUTES
    set MAX(WP) $MAXWPOINTS
    set MAX(RT) $MAXROUTES
    #--
    set MAX(IC) [lindex $data 7]
    set MAXWPINROUTE) [lindex $data 9]
    set MAX(TR) [lindex $data 10]
    set MAXICG [lindex $data 11]
    return
}

proc PutTrail {} {

global PntCount CurrentTrack

    while {$PntCount > 0} {
	SendData PutTRd $CurrentTrack
    }
    return
}

proc TrailOrigin {data} {

global NIXDAYS PDTYPE TRNumDeltas Time Trail Xcurrent Ycurrent \
		BulkData TrailData

    set TrailData ""
    set Time [clock seconds]
    set data [UnPackData $data $PDTYPE(GetTR)]
    set TRNumDeltas [lindex $data 4]
    if {$TRNumDeltas > 0} then {
	set trail [lindex $data 1]
	set Trail [expr 1 + $trail]
#     set the current global trail points to the received XY origins.
	set Xcurrent [lindex $data 3]
	set Ycurrent [lindex $data 2]
	lappend TrailData [list "Trail$Trail" [TimeStamp $Time] $Time [Merc2Lat \
		$Ycurrent] [Merc2Long $Xcurrent] "1"]
	while {$TRNumDeltas > 0} {
	    SendData GetTRd $trail
	}
	lappend BulkData $TrailData
    }    
    return
}

proc TrailDelta {data} {

global Time PDTYPE TrailData Xcurrent Ycurrent Trail INTERVAL

    set ddata [UnPackData $data $PDTYPE(GetTRd)]
    set pnts [lindex $ddata 2]
    set data [lreplace $data 0 3]
    for {set p 0} {$p < $pnts} {incr p} {
	binary scan [join [list [lindex $data 0] \
		[lindex $data 1]] ""] "s" deltaY  
	set Ycurrent [expr ($Ycurrent + $deltaY)]
	binary scan [join [list [lindex $data 2] \
		[lindex $data 3]] ""] "s" deltaX
	set Xcurrent [expr ($Xcurrent + $deltaX)]
	set Time [expr ($Time + $INTERVAL)]
	lappend TrailData [list "Trail$Trail" [TimeStamp $Time] $Time \
		[Merc2Lat $Ycurrent] [Merc2Long $Xcurrent]]
# delete point just calc'ed & printed, ready for the next iteration.
	set data [lreplace $data 0 3]
    }
    return
}

proc Lat2Merc {data} {

global RADtoDEG PI

    set lat [expr (($data / $RADtoDEG) + $PI/2)/2]
    set tanlat [expr tan($lat)]
    set loglat [expr log($tanlat)]
    return [expr round ($loglat * 6356752.3142)]
}

proc Long2Merc {data} {

global RADtoDEG

    return [expr round (6356752.3142 * $data / $RADtoDEG)]
}

proc Merc2Lat {data} {

global PI RADtoDEG

    set temp [expr ($data / 6356752.3142)]
    set temp [expr exp($temp)]
    set temp [expr (2 * atan($temp)) - ($PI / 2)]
    set x [expr ($temp * $RADtoDEG)]
    return $x
}

proc Merc2Long {data} {

global RADtoDEG

    set temp [expr ($data / 6356752.3142)]
    return [expr ($temp * $RADtoDEG)]
}

proc UnPackData {data types} {
    # convert from list of bytes (as TCL characters) to list of elements
    #  conforming to the types in the list $types

global PDTYPE

    set vals ""
    foreach t $types {
	switch -glob $t {
	    byte {
		set n 1
		binary scan [lindex $data 0] "c" x
		set x [expr ($x+0x100)%0x100]
	    }
	    int {
		set n 2
		binary scan [join [list [lindex $data 0] [lindex $data 1]] \
			          ""] "s" x
	    }
	    word {
		set n 2
		binary scan [join [list [lindex $data 0] [lindex $data 1]] \
			          ""] "s" x
		set x [expr ($x+0x10000)%0x10000]
	    }
	    long -
	    longword {
		       # longword cannot be represented in Tcl as unsigned!
		set n 4
		binary scan [join [list [lindex $data 0] [lindex $data 1] \
                                   [lindex $data 2] [lindex $data 3]] ""] "i" x
	    }
	    charray=* {
		regsub charray= $t "" n
		set x ""
		for { set i 0 } { $i < $n } { incr i } {
		    set c [lindex $data $i]
		    binary scan $c c d
		    if { $d == 0 } { set c " " }
		    append x $c
		}
		set x [string trim $x " "]
		regsub -all {:} $x "" x
	    }
	    ignored {
		return $vals
	    }
	    default {
		Log "unimplemented data type when unpacking: $t"
		set n 1 ; set x 0
	    }
	}
	# MF change: the following comments were taken from the switch body
	#  above
#
#	    double {
#		set n 8
#		binary scan [join [list [lindex $data 0] [lindex $data 1] \
#                                   [lindex $data 2] [lindex $data 3] \
#                                   [lindex $data 4] [lindex $data 5] \
#                                   [lindex $data 6] [lindex $data 7]] ""] "d" x
#	    }
	#--
	lappend vals $x
	set data [lrange $data $n end]
    }
    return $vals
}

proc SendData {command indx} {

    # transfer data to receiver
    # first group sent will be the Command Header. This will
    #  be followed by a data group for PUTS (& for some GETS) or will 
    #  trigger a reply header & data group for GETS.
    #  Second arg is the data index or zero, as appropriate. 

global WPName WPNum UnusedWPInx UnusedICInx RTWPoints RTList TRNumDeltas \
	PntCount NDeltas CMD CurrentTrack

    set data ""
    switch $command {
	GetWP -
	GetICpos - 
	GetRT -
	GetICgr {
	    set data [PacketHeader $CMD($command) 2]
	    lappend data [CheckSum $data]
	    set suffix [binary format "s" $indx]
	    set d [split $suffix ""]
	    lappend data [lindex $d 0] [lindex $d 1] 
	    lappend data [CheckSum $d]
	}    
	GetTR {
	    set data [PacketHeader $CMD($command) 1]
	    lappend data [CheckSum $data]
	    set suffix [binary format "c" $indx]
	    lappend data $suffix
	    lappend data [CheckSum $suffix]
	}
	GetTRd {
	    set deltaslice 20
	    set sdata [PacketHeader $CMD($command) 3]
	    lappend sdata [CheckSum $sdata]
	    set suffix [binary format "c" $indx]
	    if {$TRNumDeltas < $deltaslice} {
		set deltas [binary format "s" $TRNumDeltas]
		set TRNumDeltas 0
	    } else {
		set deltas [binary format "s" $deltaslice]
		set TRNumDeltas [expr ($TRNumDeltas - $deltaslice)]
	    }
	    set d [split $deltas ""]
	    lappend suffix [lindex $d 0] [lindex $d 1]
	    lappend suffix [CheckSum $suffix]
	    set data [concat $sdata $suffix]
	}
	PutWP {
	    if { $WPNum($indx) < 0} {
		set WPNum($indx) [lindex $UnusedWPInx 0]
		set UnusedWPInx [lreplace $UnusedWPInx 0 0]
	    }
	    set sdata [PacketHeader $CMD($command) 29]
	    lappend sdata [CheckSum $sdata]
	    set tvals [PrepData $command $indx]
	    set packdata [PackData $tvals]
	    lappend packdata [CheckSum $packdata]
	    set data [concat $sdata $packdata]
	}
	PutRT {
	    set count [array size WPName]
	    set RTList ""
	    foreach pnt $RTWPoints($indx) {
		set inx [IndexNamed WP $pnt]
		if { ! [string compare WPNum($inx) "" ] } {
		    if { [llength $unusedWPInx] == 0 } {
			GMMessage $MESS(noWPinx)
			ResetCursor .
		    } else {
			set WPNum($inx) [lindex $UnusedWPInx 0]
			set UnusedWPInx [lreplace $UnusedWPInx 0 0]
		    }
		}
		lappend RTList $WPNum($inx)
	    }
	    set PntCount [llength $RTList]
	    set numbytes [expr ($PntCount * 2) + 16]
	    set data [PacketHeader $CMD($command) $numbytes]
	    lappend data [CheckSum $data]
	    set tvals [PrepData $command $indx]
	    set packdata [PackData $tvals]
	    set chk [CheckSum $packdata]
	    set packdata [concat $packdata [list $chk]]
	    set data [concat $data $packdata]
	}
	PutIC {
	    foreach i { $indx } {
		if { ! [string compare WPNum($i) "" ] } {
		    if { [llength $unusedICInx] == 0 } {
			GMMessage $MESS(noICinx)
			ResetCursor .
		    } else {
			set WPNum($i) [lindex $UnusedICInx 0]
			set UnusedICInx [lreplace $UnusedICInx 0 0]
		    }
		}
	    }
	    set count [llength $indx]
	    set data [PacketHeader $CMD(PutICno) 2]
	    lappend data [CheckSum $data]
	    set suffix [binary format "s" $count]
	    set d [split $suffix ""]
	    lappend data [lindex $d 0] [lindex $d 1] [CheckSum $d]
	    GPSInterface PutICno $data
	    for {set i 0} {$i < $count} {incr i} {
		set inx [lindex $indx $i]
 		set data [PacketHeader $CMD(PutIC) 11]
		lappend data [CheckSum $data]
		set tvals [PrepData $command $inx]
		set packdata [PackData $tvals]
		lappend packdata [CheckSum $packdata]
		set data [concat $data $packdata]
		GPSInterface $command $data
	    }
	    return
	}
	PutTR {
	    set CurrentTrack $indx
	    set sdata [PacketHeader $CMD($command) 11]
	    lappend sdata [CheckSum $sdata]
	    set tvals [PrepData $command $indx]
	    set packdata [PackData $tvals]
	    lappend packdata [CheckSum $packdata]
	    set data [concat $sdata $packdata]
	}
	PutTRd {
	    if {$PntCount < 40} {
		set NDeltas $PntCount
		set PntCount 0
	    } else {
		set NDeltas 40
		set PntCount [expr $PntCount - 40]
	    }
	    set n [expr $NDeltas * 4 + 3]
	    set data [PacketHeader $CMD($command) $n]
	    lappend data [CheckSum $data]
	    set tvals [PrepData $command $CurrentTrack]
	    set packdata [PackData $tvals]
	    lappend packdata [CheckSum $packdata]
	    set data [concat $data $packdata]
	}
	default {
	    set data [PacketHeader $CMD($command) 0]
	    lappend data [CheckSum $data]
	}
    }
    GPSInterface $command $data
    return
}

proc PacketHeader {command count} {

# Formats the correct Packet Command header.
global PREAMBLE PDTYPE

    set headdata ""
    lappend headdata $PREAMBLE $command $count 0
    return [PackData [list $PDTYPE(Header) $headdata]]
}

proc CheckSum {data} {

    # Calculates the 2's complement checksum and returns checksum.

    set datasum 0 
    foreach ch $data {
	binary scan $ch "c" v
	incr datasum $v
    }
    set checksum [expr 256 - ($datasum & 255)]
    set chk [binary format "c" $checksum]
    return $chk
}

proc PrepData {cmd inx} {

    # prepare WPs, RTs, or TRs data to be transferred

global GPSMANDAYS PDTYPE WPName WPSymbol WPNum WPCommt WPPosn WPDatum WPDate \
	ICIdNumber ICSymbol ICPfrmt ICPosn ICDatum RTList RTIdNumber \
	RTCommt TRName TRDatum TRTPoints NDeltas TDeltas TRNumDeltas \
	PntCount Choice SYMBOLCODE YEAR0 MONTHNAMES DateFormat CurrentTrack

    switch $cmd {
	PutWP {
	    set p $WPPosn($inx)
	    if { [string compare $WPDatum($inx) "WGS 84"] } {
		set p [ConvertDatum [lindex $p 0] [lindex $p 1] \
			            $WPDatum($inx) "WGS 84" DDD]
	    }
	    set latd [lindex $p 0]
	    set longd [lindex $p 1] 
	    set sym $SYMBOLCODE($WPSymbol($inx)) 
	    if { ! [string compare $WPDate($inx) ""] } {
		set year0 $YEAR0 ; set YEAR0 1992
		set t { %Y %m %d %H %M %S }
		set s [clock format [clock seconds] -format $t]
		scan $s "%d %d %d %d %d %d" y m d h mn s
		set wpdate [DateToSecs $y $m $d $h $mn $s]
		set YEAR0 $year0
	    } else { 
		set date [split $WPDate($inx) ": -/."]
		if { ! [string compare $DateFormat "DDMMMYYYY"] } {
		    scan $date {%d %[a-zA-Z] %d %d %d %d} d mnth y h mn s
		    set m [lsearch -exact $MONTHNAMES $mnth]
		    set month [expr $m + 1]
		}
		if { ! [string compare $DateFormat "YYYY-MM-DD"] } {
		    scan $date {%d-%d%-d %d %d %d} month d y h mn s
		}
		if { ! [string compare $DateFormat "MMDDYYYY"] } {
		    scan $date {%d %d %d %d %d %d} month d y h mn s
		}
		if { ! [string compare $DateFormat "YYYYMMDD"] } {
		    scan $date {%d %d %d %d %d %d} y month d h mn s
		}
		set gpsdate [DateToSecs $y $month $d $h $mn $s]
		set wpdate [expr $gpsdate - ($GPSMANDAYS*24*3600)]
	    }
	    set wpnum [expr $WPNum($inx) - 1]
	    return [list $PDTYPE($cmd) [PrepWPData $WPNum($inx) $WPName($inx) \
		$sym $WPCommt($inx) $latd $longd $wpdate ]]
	}
	PutRT {
	    set pointlist ""
	    for {set i 0} {$i < $PntCount} {incr i} {
		lappend pointlist [lindex $RTList $i]
	    }
	    return [list $PDTYPE(PutRT) [PrepRTData $RTIdNumber($inx) \
		$PntCount $RTCommt($inx) $pointlist]]

	}
	PutIC { 
	    set p $WPPosn($inx)
	    if { [string compare $WPDatum($inx) "WGS 84"] } {
		set p [ConvertDatum [lindex $p 0] [lindex $p 1] \
			            $WPDatum($inx) "WGS 84" DDD]
	    }
	    set num [string range $WPName($inx) 4 end]
	    set sym $SYMBOLCODE($WPSymbol($inx)) 
	    set latd [lindex $p 0]
	    set longd [lindex $p 1] 
	    return [list $PDTYPE($cmd) [PrepICData $num \
		$sym $latd $longd]]
	}
	PutTR {
	    set TDeltas ""
	    set points ""
	    foreach p $TRTPoints($inx) {
		lappend points [list [lindex $p 0] [lindex $p 1]]
	    }
	    set pnts ""
	    if { [string compare $TRDatum($inx) "WGS 84"] } {
		foreach p $points {
		lappend pnts [ConvertDatum [lindex $p 0] \
			[lindex $p 1] $TRDatum($inx) "WGS 84" DDD]
		}
		set points $pnts
	    }
	    foreach p $points {
		set ymerc [Lat2Merc [lindex $p 0]]
		set xmerc [Long2Merc [lindex $p 1]]
		lappend tpoints [list $ymerc $xmerc] 
	    }
	    set PntCount [expr [llength $tpoints] -1]
	    set p [lindex $tpoints 0]
	    set y [lindex $p 0]	; set x [lindex $p 1]
	    set yo $y ; set xo $x
	    for {set i 1} {$i <= $PntCount} {incr i} {
		set p [lindex $tpoints $i]
		set y1 [lindex $p 0] ; set x1 [lindex $p 1]
		set dy [expr $y1 - $y] ; set dx [expr $x1 - $x]
		lappend TDeltas [list $dy $dx]
		set y $y1 ; set x $x1
	    }
	    return [list $PDTYPE($cmd) [list $inx $yo $xo $PntCount]]
	}
	PutTRd {
	    set deltas "" ; set type $PDTYPE($cmd)
	    for {set i 0} {$i < $NDeltas} {incr i} {
		set d [lindex $TDeltas 0]
		lappend deltas [lindex $d 0] [lindex $d 1]
		lappend type "word" "word"
		set TDeltas [lreplace $TDeltas 0 0] 
	    }
	    return [list $type [concat [list $CurrentTrack $NDeltas] $deltas ]]
	}

    }
}

proc PrepWPData {number name sym cmmt lat long date} {

    return [list $number 1 $sym [Lat2Merc $lat] [Long2Merc $long] \
		$name $date]
}

proc PrepICData {number sym lat long } {

    return [list $number [Lat2Merc $lat] [Long2Merc $long] $sym]
}

proc PrepRTData {id wpcount name points} {


    set id [expr ($id - 1)]
    return [list $id $wpcount $name $points]
}

proc PackData {tvals} {
    # convert from a pair with a list of types and a list of values into a
    #  list of bytes (as TCL characters)

    return [split [DataToStr [lindex $tvals 0] [lindex $tvals 1]] ""]
}

proc DataToStr {types vals} {
    # convert from list of elements conforming to the types in $types to
    #  a TCL string

    global PntCount

    set data ""
    foreach t $types v $vals {
	switch -glob $t {
	    byte {
		append data [binary format "c" $v]
	    }
	    word {
		append data [binary format "s" $v]
	    }
	    longword -
	    long {
		append data [binary format "i" $v]
	    }
	    wordarray {
		append data [binary format "s$PntCount" $v]
	    }
	    charray=* {
		regsub charray= $t "" n
		append data [binary format "A$n" $v]
	    }
	    default {
		Log "unimplemented data type when packing: $t"
	    }
	}
	# MF change: the following comments were taken from the switch body
	#  above
#	    double {
#		append data [binary format "d" $v]
#	    }
	#--
    }
    return $data
}


##### upper level

proc EndOutProt {} {
    # deal with end of output protocol

     CloseInProgrWindow
    return
}

proc EndInProt {what data} {
    # deal with end of input protocol

    CloseInProgrWindow
    switch $what {
	IC {InData$what $data}
        TR {InData$what $data}
    } 
    return
}

proc InDataWP {data} {
    # add WPs data from receiver to database

global Index Number WPRoute GetDispl GetSet WPDispl WPSymbol

    foreach d $data {
	set d [ConvWPData $d]
	set name [lindex $d 0]
	set ix [IndexNamed WP $name]
#	set name [lindex $d 0] ; set name [split $name " "]
#	set name [join $name ""] ; set ix [IndexNamed WP $name]
	set d [lreplace $d 0 0 $name]
	if {! [string compare $GetSet(WP) ""] || \
		[lsearch $GetSet(WP) $ix] != -1 } {
	    StoreWP $ix $name $d $GetDispl
	}
    }
return
}

proc ConvWPData {d} {
    # convert WPs data got from receiver into list of lists suitable for
    #  use with SetItem

global PositionFormat DEFAULTDISPOPT DATAFOR

    set t [PosType $PositionFormat]
    set wpnum [lindex $d 0]
    set wpname [lindex $d 4]
    set date [ConvLowrDate [lindex $d 5]]
    set s [lindex $d 1]
    set sym [NameForCodeOf SYMBOL $s]
    set p [CreatePos [lindex $d 2] [lindex $d 3] \
	$PositionFormat $t "WGS 84"]
    set all [list Name Num Posn Date Symbol]
    set r [list $wpname $wpnum $p $date $sym]
    set r [FormData WP $all $r]
    return $r
}

proc InDataIC {data} {
    # add Icon data from receiver to database
    # return list of set Icons

global Index Number GetDispl

    foreach d $data {
	set d [ConvICData $d]
	set id [lindex $d 1]
	set name "ICON$id"
	set d [lreplace $d 0 0 $name]
	set ix [IndexNamed WP $name]
	StoreWP $ix $name $d $GetDispl
    }
return
}

proc ConvICData {d} {
    # convert Icon data got from receiver into list of lists suitable for
    #  use with SetItem

global PositionFormat DEFAULTDISPOPT 

    set t [PosType $PositionFormat]
    set id [lindex $d 0]
    set name [lindex $d 4]
    set p [CreatePos [lindex $d 1] [lindex $d 2] $PositionFormat $t "WGS 84"]
    set s [lindex $d 3]
    set sym [NameForCodeOf SYMBOL $s]
    set all [list Name Num Posn Symbol]
    set r [list $name $id $p $sym]
    set r [FormData WP $all $r]
    return $r
}

proc InDataRT {data} {
    # add RT data from receiver to database

    global Index Number WName RTWPoints RTDispl GetDispl GetSet
    foreach d $data {
	set wps ""
	set id [lindex $d 0] 
	set name [lindex $d 1] ; set ix [IndexNamed RT $id]
 	foreach pnt [lindex $d 2] {
	    lappend wps $WName($pnt)
	}
	if { [string compare $wps ""] } {
	    set all [list IdNumber Commt WPoints]
	    set r [list $id $name $wps]
	    set r [FormData RT $all $r]
	    StoreRT $ix $name $r $wps $GetDispl
	}
        SetWPRoute $id $wps
    }
    return    
}

proc InDataTR {data} {
    # add TRs data from receiver to database

global Index Number GetDispl

    foreach p $data {
	set tps ""
	set f [lindex $p 0]
	set id [lindex  $f 0]
	set ix [IndexNamed TR $id]
	if {$ix == -1} then {
	    foreach d $p {
		set t [CreatePos [lindex $d 3] [lindex $d 4] \
			DMS latlong "WGS 84"]
		lappend t [lindex $d 1] [lindex $d 2]
		lappend tps $t
	    }
	    set trdata [list [lindex $d 0] $tps]
	    set all [list Name TPoints]
	    set r [FormData TR $all $trdata]
	    StoreTR $ix $id $r $GetDispl
	}
    }
    return
}


##### GPSMan interface

proc GPSChangeProtocol {prot} {
    # change current protocol
    #  $prot in {lowrance, nmeal}
    # must change GPSProtocolExt if successful
    global GPSProtocol GPSProtocolExt GPSState TXT 

    if { "$GPSProtocol" == "$prot" } { return }
    if { "$GPSState" == "online" } {
	set Eof 1
	close $SRLFILE
	DisableGPS
    }
    set GPSProtocol $prot ; set GPSProtocolExt $TXT($prot)
    return
}

proc ConvLowrDate {ld} {
    # converts Lowrance date (seconds since 1992.01.01 00:00:00) into
    # seconds from $YEAR0
    #  with date in current format and seconds since beginning of $YEAR0
    #  (assumed a leap year < 1990)

global YEAR0

    set dd 0
    for { set yy $YEAR0 } { $yy < 1990 } { incr yy 4 } { incr dd }
    set secs [expr ((1992-$YEAR0)*365+$dd)*24*3600+$ld]
    return [DateFromSecs $secs]
}

proc GPSConnection {data} {

    EnableGPS
    return
}

proc GPSBye {} {

    return
}

proc InitGPS {} {

    global MAX MESS

    StartGPS
    if { [FailsInProgrWindow $MESS(fillbuffWP)] } { return }
    SendData GetInfo 0
    BulkRead $MAX(WP) GetWP
    EndInProt WP 0
    if { [FailsInProgrWindow $MESS(fillbuffRT)] } { return }
    BulkRead $MAX(RT) GetRT
    EndInProt RT 0
    return
}

proc GetGPSIn {wh ixs} {
    # get data of type $wh (in {WP, RT}), but only to replace items with
    #  given indices $ixs (that may contain -1, meaning that new items
    #  can be created)
    global GetSet

    set GetSet($wh) $ixs
    DoGetGPS $wh
    return
}

proc GetGPS {wh} {
    global GetSet

    set GetSet($wh) ""
    DoGetGPS $wh
    return
}

proc DoGetGPS {wh} {
    global MESS MAX BulkData Choice 

    if { [FailsInProgrWindow $MESS(get$wh)] } { return }
    switch $wh {
	WP {
	    set data [ListWayPoints]
	    InDataWP $data
	    CloseInProgrWindow
	}
	RT {
	    set data [ListRoutes]
	    InDataRT $data
	    CloseInProgrWindow
	}
	TR {
	    BulkRead $MAX(TR) Get$wh
	    EndInProt TR $BulkData
	}
	IC {
	    SendData GetICno 0
	    EndInProt IC $BulkData
	}
	Info {
	    SendData Get$wh 0
	} 
    }
    return
}

proc PutGPS {wh inx} {

global MESS  

    if { [FailsInProgrWindow $MESS(put$wh)] } { return }
    switch $wh {
	WP {
	    set indx [WPSort PutWP $inx]
	    if { [llength $indx] > 0 } {
		SendData PutIC $indx
	    }
	}
	default {
	    foreach p $inx {
		SendData Put$wh $p
	    }
	}
    }
    EndOutProt
    return
}

proc WPSort {what inx} {

# Sorts WP's from Icons and sends them to Receiver appropriately
    global WPName

    set data ""
    foreach p $inx {
	if { [string match "ICON*" $WPName($p)] } {
	    lappend data $p
	} else {
	    SendData PutWP $p
	}	
    }    
    return $data 
}
proc BulkRead {count wh} {

global BulkData GetSet WNames RNames 

    switch $wh {
	GetWP { set WNames "" }
	GetRT { set RNames "" }
    }
    set BulkData ""
    for {set i 0} {$i < $count} {incr i} {
	SendData $wh $i
    }
}

proc StartGPS {} {
   
global NotLogging LogFile MessSent SERIALLOG SRLFILE SERIALPORT

    if {! $NotLogging } {
	set LogFile [open $SERIALLOG w]
    }
    set MessSent 0
    set SRLFILE [open $SERIALPORT r+]
    fconfigure $SRLFILE -blocking 0 -mode 19200,n,8,1 -translation binary
#    fconfigure $SRLFILE -blocking 0 -mode 9600,n,8,1 -translation binary
    LowranceStdSymbols
    return
}

proc GPSOff {} {

    global SRLFILE

#    close $SRLFILE
    CloseInProgrWindow
    return
}

proc CloseInProgrWindow {} {

    destroy .inprogr
    return
}

proc FailsInProgrWindow {mess} {
    # create dialog for signaling operation in progress
    # fail if window already exists
    #  single button: Abort; no bindings

    global COLOUR EPOSX EPOSY TXT

    if { [winfo exists .inprogr] } { Raise .inprogr ; bell ; return 1 }
    toplevel .inprogr
    wm protocol .inprogr WM_DELETE_WINDOW { bell }
    wm title .inprogr "GPS Manager: $TXT(commrec)"
    wm transient .inprogr
    wm geometry .inprogr +$EPOSX+$EPOSY

    frame .inprogr.fr -relief flat -borderwidth 5 -bg $COLOUR(messbg)
    label .inprogr.fr.title -text "..." -relief sunken
    label .inprogr.fr.text -text "$mess"
#    button .inprogr.fr.stop -text "STOP" -default active -command "NMEAOff"
    set fw .inprogr.fr.frc
    frame $fw -relief flat -borderwidth 0
    label $fw.tit -text "Working - please wait"
    button .inprogr.fr.ok -text "Stop" -state normal \
	-command NMEAOff
#    button .inprogr.fr.ok -text $TEXT(abort) -command NMEAOff
    pack $fw.tit -side left -padx 0
    pack .inprogr.fr -side top
    pack .inprogr.fr.title .inprogr.fr.text .inprogr.fr.frc \
	.inprogr.fr.ok -side top -pady 5
#    pack .inprogr.fr.stop -side top -pady 5
    update idletasks
    grab .inprogr.fr.ok
    RaiseWindow .inprogr
    return 0
}