forked from Discriminator/PDW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobitex.cpp
1856 lines (1563 loc) · 51.8 KB
/
mobitex.cpp
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
//
// Mobitex.cpp
//
//
#ifndef STRICT
#define STRICT 1
#endif
#include <windows.h>
#include "headers\pdw.h"
#include "headers\initapp.h"
#include "headers\sound_in.h"
#include "headers\decode.h"
#include "headers\misc.h"
#include "headers\mobitex.h"
#include "headers\helper_funcs.h"
#define MB_MSG_SIZE 5000 // Max length of mobitex message
#define MAX_ERR 10 // was 15 - J.P
#define MPAK_CLASS_PSUBCOM 0
#define MPAK_CLASS_1 1 // ??
#define MPAK_CLASS_2 2 // ??
#define MPAK_CLASS_DTESERV 3
#define FRAMETYPE_MRM 0x01
#define FRAMETYPE_ACK 0x02
#define FRAMETYPE_NACK 0x03
#define FRAMETYPE_REB 0x04
#define FRAMETYPE_RES 0x05
#define FRAMETYPE_ABD 0x06
#define FRAMETYPE_ABL 0x07
#define FRAMETYPE_ATD 0x08
#define FRAMETYPE_ATL 0x09
#define FRAMETYPE_BKD 0x0C
#define FRAMETYPE_BKT 0x0D
#define FRAMETYPE_FRI 0x0E
#define FRAMETYPE_SVP 0x0F
#define FRAMETYPE_TST 0x10
#define FRAMETYPE_AKT 0x11
#define FRAMETYPE_NAT 0x12
#define FRAMETYPE_BBT 0x13
#define MPAKTYPE_TEXT 0x01
#define MPAKTYPE_DATA 0x02
#define MPAKTYPE_STATUS 0x03
#define MPAKTYPE_HPDATA 0x04
#define RX 0
#define TX 1
MOBITEX mb;
char obm[256];
int block[20];
//int blocks;
int frsyncs[18] = { 0xC4D7, 0xB433, 0xA2F7, 0xEB90, 0xEE16, 0x4BCC, 0x3B48, 0x5D08, 0x146F,
0x11E9, 0xEB23, 0xEF45, 0x09D7, 0x6877, 0x14DC, 0x10BA, 0xF628, 0x9788 };
int frevsyncs[18] = { 0x3B28, 0x4BCC, 0x5D08, 0x146F, 0x11E9, 0xB433, 0xC4B7, 0xA2F7, 0xEB90,
0xEE16, 0x14DC, 0x10BA, 0xF628, 0x9788, 0xEB23, 0xEF45, 0x09D7, 0x6877 };
// Variables for displaying message data.
char mb_msg_buf[MB_MSG_SIZE];
BYTE mb_msg_col[MB_MSG_SIZE];
int iBlockNumber, nCharacters=0;
unsigned int ESN_manufacturerscode, ESN_modelnumber, ESN_IDnumber;
bool bBadDestination=false, bBadSender=false, bBadHeader;
bool bFirstMPAK=true, bPrimaryBlock;
char szDestination[10], szSender[10], szType[10], szMPAK[32];
char szNeighbourList[MAX_STR_LEN], szBaseIdName[128]="";
char szLastBaseID[8], szTmpIdName[128];
extern char szWindowText[6][1000];
int aPreviousMAN[2][10]; // This array holds the previous RX & TX MAN's
char *MpakDteserv[32] = { "UNKNOWN MPAK", // 00
"LOGIN REQUEST", // 01
"LOGIN GRANTED", // 02
"LOGIN REFUSED", // 03
"LOGOUT", // 04
"LOGOUT ORDER", // 05
"BORN", // 06
"ACTIVE", // 07
"INACTIVE", // 08
"DIE", // 09
"LIVE", // 10
"ROAMING ORDER", // 11
"ROAMING MESSAGE", // 12
"UNKNOWN MPAK", // 13
"UNKNOWN MPAK", // 14
"GROUPLIST", // 15
"FLEXREQ", // 16
"FLEXLIST", // 17
"INFOREQ", // 18
"INFO", // 19
"TIME", // 20
"AREALIST", // 21
"ESN REQUEST", // 22
"ESN INFO", // 23
"MODE", // 24
"UNKNOWN MPAK", // 25
"UNKNOWN MPAK", // 26
"UNKNOWN MPAK", // 27
"UNKNOWN MPAK", // 28
"UNKNOWN MPAK", // 29
"APPLICATION OPTIONS", // 30
"LOW POWER" }; // 31
char *FrameTypes[16] = { "<???> ", // 00
"<MRM> ", // 01
"<ACK> ", // 02
"<NACK>", // 03
"<REB> ", // 04
"<RES> ", // 05
"<ABD> ", // 06
"<ABL> ", // 07
"<ATD> ", // 08
"<ATL> ", // 09
"<???> ", // 10
"<???> ", // 11
"<BKD> ", // 12
"<BKT> ", // 13
"<FRI> ", // 14
"<SVP> " }; // 15
//FILE *pTest=NULL;
MOBITEX::MOBITEX() // Default Mobitex settings
{
cfs = 1; // system specific frame sync checking status
frsync = 0xEB90; // system specific frame sync
frsync_rev = ~frsync; // In case bit stream wrong way up.
bitsync = 0xCCCC; // bit sync: CCCC = Base, 3333 = Mobile.
bitsync_rev = ~bitsync; // In case bit stream wrong way up.
bitrate = 8000; // for getting baud rate from INI file.
bitscr = 1; // bit scrambling in use ?
ramnet = 1; // ramnet flag
timer = 0; // For restoring title bar msg/signal indicator.
h3 = 0xEC; // matrix variables for block encoding
h2 = 0xD3;
h1 = 0xBA;
h0 = 0x75;
min_msg_len = 5; // Don't display message unless it's at least this length.
show = 0x0F;
nBitCount=0;
}
MOBITEX::~MOBITEX()
{
}
// Called if changing data mode or if switching between soundcard & serial port input
void MOBITEX::reset(void)
{
nBitCount = 0;
timer = 0;
}
// returns the number of ones in the byte passed to routine
int MOBITEX::mb_nOnes(int h)
{
int nb=0;
for (int i=0; i<8; i++)
{
if ((h & 0x01) == 1) nb++;
h = h >> 1;
}
return(nb);
}
// returns number of ones in the integer passed to routine
int MOBITEX::mb_nOnes_int(int h)
{
return(mb_nOnes(h) + mb_nOnes(h >> 8));
}
//***********************************************************************
// RUN THROUGH BLOCK FORWARD ERROR CORRECTION CODE *
//***********************************************************************
// PURPOSE:
// does FEC on the 8 data bits and 4 FEC bits passed to it in goi
// get_fec tries to check and correct errors... If the errors are
// uncorrectable get_fec returns a 1; otherwise 0
// but don't read too much into this - two thirds of the time
// random crap going into this routine will not generate the
// uncorrectable error signal... Please Rely on the CRC check
//
int MOBITEX::get_fec(int *goi)
{
int dab=0, nb=0, bb=0;
int uce=0; // flag that indicates an uncorrectable error
iUnfixedError = 0; // flag that indicates an unfixed error.
// calculate ecc bits from our current info bits
dab = (*goi >> 4);
nb = (mb_nOnes(dab & h3) & 0x01) << 3;
nb += (mb_nOnes(dab & h2) & 0x01) << 2;
nb += (mb_nOnes(dab & h1) & 0x01) << 1;
nb += (mb_nOnes(dab & h0) & 0x01);
// get syndrome
nb = nb ^ (*goi & 0x0F);
if (mb_nOnes(nb) > 0) uce = 1;
else uce = 0;
// if syndrome is not equal to zero try to correct the bad bit
if (mb_nOnes(nb) > 1)
{
if ((nb & 0x08) > 0) bb = h3; else bb = h3 ^ 0xff;
if ((nb & 0x04) > 0) bb &= h2; else bb &= h2 ^ 0xff;
if ((nb & 0x02) > 0) bb &= h1; else bb &= h1 ^ 0xff;
if ((nb & 0x01) > 0) bb &= h0; else bb &= h0 ^ 0xff;
// are we pointing to a single bit? if so we found it
if (mb_nOnes(bb) == 1) *goi = *goi ^ (bb<<4);
else
{
uce++;
iUnfixedError = 1; // unfixed errors (used for COLOR_MB_BITERRORS)
}
}
else *goi = *goi ^ nb;
// single wrong bit in syndrome => error occured in FEC bits
return(uce);
}
//************************************************************************
// BIT SCRAMBLING SEQUENCE GENERATOR *
//************************************************************************
// following is the pseudo-random bit scrambling generator.
// It's the output of a 9 stage linear-feedback shift register with taps
// at position 5 and 9 XORed and fed back into the first stage.
// An input of less than zero resets the scrambler.
// Otherwise it returns one bit at a time each time it is called
//
int MOBITEX::mb_bs(int st)
{
static int rs, ud;
// leave if system isn't supposed to use bit scrambling
if (bitscr == 0) return(0);
if (st < 0) rs = 0x1E; // inputs <0 reset scrambler
else
{
if ((rs & 0x01) > 0) ud = 1;
else ud = 0;
if ((rs & 0x10) > 0) ud = ud ^ 0x01;
rs = rs >> 1;
if (ud > 0) rs ^= 0x100;
}
return(ud);
}
//***********************************************************************
// CRC GENERATING ROUTINE *
//***********************************************************************
// CRC generator - passing a -1 to this routine resets it, otherwise
// pass all 144 data bits in the mobitex data block to it (starting
// with byte 0, LSB bit). The returned value will then be the
// CRC value. Passing any other negative value just returns CRC.
unsigned int MOBITEX::mb_crc(signed int bit)
{
short unsigned int sr = 0x00, cr;
if (bit >= 0)
{
if (bit == 1) cr ^= sr;
if (sr & 0x8000) sr = (sr << 1) ^ 0x0811;
else sr = sr << 1;
}
else if (bit == -1) // -1 resets the crc state
{
sr = 0xF8E7;
cr = 0x2A5D;
}
CountBiterrors((cr == 0xCCCC) ? 0 : 1);
return(cr);
}
/*
unsigned int MOBITEX::mb_crc_1200(unsigned char *input, unsigned int syndrome)
{
unsigned int r=0;
for (int i=0; i<6; i++)
{
unsigned char c = input[i];
for (int nBit=0; nBit<8; nBit++)
{
r = r << 1;
if (c & 0x80) r |= 0x1;
if (r & 0x200000) r = (r&0x1FFFFF) ^ 0x3B9FB5;
c = c << 1;
}
}
// flush register
for (int nBit=0; nBit<21; nBit++)
{
r = r << 1;
if (r & 0x200000) r = (r & 0x1FFFFF) ^ 0x3B9FB5;
}
return (r & 0x1FFFFF) ^ syndrome;
}
*/
//*********************************************************************
// PROCESS RECEIVED 240 BIT MOBITEX DATA BLOCK *
//*********************************************************************
// process MOBITEX data block
//
void MOBITEX::barfrog()
{
int i,j,k,b,cb, nErrors=0;
char c, blocks2resend[10];
unsigned int crcc=0x0000;
// process data block into 20 byte chunk stored in array 'block'
mb_crc(-1); // reset crc routine
for (i=0; i<20; i++) // uninterleave the data into b (holds 8 data bits + 4 FEC bits)
{
for (j=0, b=0; j<12; j++)
{
b = b << 1;
k = (j*20) + i;
if (obm[k] == 1) b ^= 0x01;
}
if (!((iBlockNumber+1 >= LINK_CONTROL_INFO.BlockLength) && // Check if we don't exceed the
(i >= LINK_CONTROL_INFO.BytesLastBlock))) // number of bytes in this frame
{
if (get_fec(&b)) // run through error correction routine
{
nErrors++;
CountBiterrors(1);
}
else CountBiterrors(0);
}
// spit out data bits to CRC routine - LSB data bit first...
cb = b >> 4;
if (i < 18) // Mark bad bits, so they can be highlighted later.
{
blk_err[i] = iUnfixedError ? 1 : 0;
for (j=0; j<8; j++)
{
if ((cb & 0x01) == 1) mb_crc(1);
else mb_crc(0);
cb = cb >> 1;
}
}
else crcc = (crcc << 8) ^ (cb & 0xff);
b = b >> 4;
block[i] = b;
}
iBlockNumber++;
bPrimaryBlock = (iBlockNumber == 1) ? true : false; // Block #1 is the primary block
// at this point array block holds the data; nErrors gives the number of errors detected
// by the FEC code ('corrected' errors count as one, uncorrectable count as 2);
// and crcc gives the received CRC code. We use this info to decide if we got a good block
// if CRC is correct or nErrors < MAX_ERR we'll say it's a good block
i = mb_crc(-2);
if ((i == crcc) || (nErrors < MAX_ERR))
{
if (bPrimaryBlock)
{
GetLinkControlInfo(); // Get link control information
if (LINK_CONTROL_INFO.FrameID == FRAMETYPE_MRM) // MRM DATA
{
bBadHeader = GetMpakHeader(); // Get MPAK header, will return true if we
} // don't want to see this type of MPAK
else if ((LINK_CONTROL_INFO.FrameID != FRAMETYPE_MRM) && (LINK_CONTROL_INFO.FrameID != FRAMETYPE_RES))
{
sprintf(szDestination, "%i", LINK_CONTROL_INFO.Destination); // If no MRM or RES frame,
strcpy(szSender , "NETWORK"); // get destination from frame header
if (LINK_CONTROL_INFO.FrameID == FRAMETYPE_SVP) // SWEEP frames
{
SWEEP.type = block[6]; // Get type of sweep frame
if (LINK_CONTROL_INFO.BlockLength > 1)
{
SWEEP.fbi = block[9]; // Get Frequency Band Information
}
SWEEP.channellist = (LINK_CONTROL_INFO.BlockLength > 1) ? true : false;
}
memset(mb_msg_col, COLOR_MISC, sizeof(mb_msg_col));
}
}
switch (LINK_CONTROL_INFO.FrameID) // Handle the different frame types
{
case FRAMETYPE_RES : // Handle <RES> frames
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show <RES> frames
if (bPrimaryBlock)
{
strcpy(mb_msg_buf, "<RES> ");
nCharacters=strlen(mb_msg_buf);
memset(mb_msg_col, COLOR_MISC, nCharacters);
sprintf(szDestination, "%07i", LINK_CONTROL_INFO.Destination); // Get RX-MAN
for (int i=0; i<10; i++)
{
if (aPreviousMAN[RX][i] = LINK_CONTROL_INFO.Destination)
{
sprintf(szSender, "%07i", aPreviousMAN[TX][i]); // Get TX-MAN
break;
}
strcpy(szSender, "UNKNOWN"); // If Sender not found, show "UNKNOWN"
}
break; // Skip first, data starts in second block
}
// fall trough to display message
case FRAMETYPE_MRM : // Handle <MRM> frames
for (i=0; i<18; i++) // store each character in buffer.
{
if (nCharacters > (MB_MSG_SIZE-2)) break; // enough room in buffer?
if (bPrimaryBlock)
{
i=17; // Actual data starts at last byte of the primary block
}
if (!((iBlockNumber == LINK_CONTROL_INFO.BlockLength) &&
(i >= LINK_CONTROL_INFO.BytesLastBlock)))
{
c = block[i] & 0x7F;
mb_msg_col[nCharacters] = blk_err[i] ? COLOR_BITERRORS : COLOR_MESSAGE;
mb_msg_buf[nCharacters] = c;
nCharacters++;
}
}
break;
case FRAMETYPE_REB : // Handle <REB> frames
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show <REB> frames
if (block[7]) // If the number of blocks != 0
{
strcpy(mb_msg_buf, "[Request to repeat block ");
for (i=1, j=1; i<=block[7]; i*=2, j++) // Loop until we reach the value
{ // which we have found in byte-7
if (block[7] & i) // Check if current block (i) is in byte-7
{
sprintf(blocks2resend, "%s%02i", isdigit(mb_msg_buf[strlen(mb_msg_buf)-1]) ? "/" : "", j);
strcat(mb_msg_buf, blocks2resend);
}
}
strcat(mb_msg_buf, "]");
}
else strcpy(mb_msg_buf, "[Request to repeat block]");
strcpy(szType, "<REB>");
nCharacters=strlen(mb_msg_buf);
break;
case FRAMETYPE_SVP : // Handle <SVP> frames
if (SWEEP.type == 1) // When receiving a SVP-1 frame, let's call
{ // display_showmo() to update the WindowText
display_showmo(MODE_MOBITEX); // to clear rejected/blocked messages
}
if (!(mb.show & MOBITEX_SHOW_SWEEP)) return; // Don't show SWEEP FRAMES
if (SWEEP.channellist && !bPrimaryBlock)
{
if ((iBlockNumber == 2) && !block[1]) // If we are in the second block in
{ // which the second byte should be 0
SWEEP.channels = block[0]; // The first byte tells the number of neighours
}
if (GetNeighbourChannels()) // true if the channellist has been received correctly
{
strcpy(mb_msg_buf, szNeighbourList);
nCharacters=strlen(mb_msg_buf);
}
}
else if (block[3] == 15)
{
if ((block[4] & 0x1F) == 24)
{
if (block[6] == 1)
{
sprintf(mb_msg_buf, "TxPower=%idB RssiProcedure=%s RssiPeriod=%ims TimeToNext=%i MaxRepetitionFactor=%i»BaseStatus=%i ScanTime=%.1fsec BadBase=%idBµV GoodBase=%idBµV BetterBase=%idBµV",
block[7], // TX-POWER
block[8] ? "ContinousMode" : "FrameMode", // RSSI_PROC
block[9]*20, // RSSI_PERIOD
block[10], // TIME_TO_NEXT
block[11], // MAX_REP
block[12], // BASE_STATUS
(double)block[13]/10, // SCAN_TIME
block[14], // BAD_BASE
block[15], // GOOD_BASE
block[16]); // BETTER_BASE
}
else if (block[6] == 6)
{
sprintf(mb_msg_buf, "CycleTime=%isec TimeToNext=%isec RssiPeriod=%i ?=%i EvaluateOthers=%iRSSIperiods»BaseStatus=%i Cycle=%i",
block[7]/4, // Cycle Time
block[8]/4, // TimeToNext
block[9], // RSSI_PERIOD
block[10], // ?
block[11], // Evaluate Others
block[12], // BASE_STATUS
block[13]); // SCAN TIME
}
}
else if ((block[4] & 0x1F) == 23)
{
if (GetSlaveChannels()) // Will return true if the channellist
{ // has been received correctly
strcpy(mb_msg_buf, szNeighbourList); // Put the neighbourlist
} // in the messagebuffer
}
nCharacters=strlen(mb_msg_buf);
}
if (bPrimaryBlock) // Is this the first (primary) block?
{
sprintf(szType, "<SVP%i>", SWEEP.type);
sprintf(szDestination, " ");
sprintf(szSender, " %s ", LINK_CONTROL_INFO.BaseID);
}
break;
case FRAMETYPE_ACK :
case FRAMETYPE_NACK :
case FRAMETYPE_ABD :
case FRAMETYPE_ABL :
case FRAMETYPE_ATD :
case FRAMETYPE_ATL :
case FRAMETYPE_FRI :
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show control messages
strcpy(szType, FrameTypes[LINK_CONTROL_INFO.FrameID]);
break;
case FRAMETYPE_TST :
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show control messages
strcpy(szType, "<TST> ");
break;
case FRAMETYPE_AKT :
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show control messages
strcpy(szType, "<AKT> ");
break;
default :
if (!(mb.show & MOBITEX_VERBOSE)) return; // Don't show unknown frames
sprintf(mb_msg_buf, "<UNKNOWN FrameID=%i>", LINK_CONTROL_INFO.FrameID);
sprintf(szType, " ");
nCharacters=strlen(mb_msg_buf);
break;
}
}
return;
}
//*******************************************************************
// FRAME SYNCHRONIZATION OF RAW BIT STREAM *
//*******************************************************************
// this routine tries to achieve frame sync up in the raw bit stream
//
void MOBITEX::frame_sync(int bit)
{
static int s1=0x0000,s2=0x0000, bc=0, nu;
int fec, og=0, fsb, fsbr;
// int ControlByte1, ControlByte2, BaseID, AreaID, CFlags, Parity;
int ControlByte1, ControlByte2;
extern unsigned long hourly_stat[NUM_STAT][2];
extern unsigned long hourly_char[NUM_STAT][2];
extern unsigned long daily_stat[NUM_STAT][2];
extern unsigned long daily_char[NUM_STAT][2];
// is a bit counter for # of bits left to form into a data block
if (nBitCount == 0)
{
// nBitCount = 0 so we aren't trying to process a data block;
// instead try to sync up with incoming bit stream
// keep sliding buffers up to date
s1 = s1 << 1;
if ((s2 & 0x8000)) s1++;
s2 = s2 << 1;
if (bit) s2++;
/*
// sliding window
for (int i=0; i<blocks; i++)
{
block[i] <<= 1;
block[i] += (block[i+1] & 0x80) ? 1 : 0;
}
// add new bit
block[blocks] <<= 1;
block[blocks] += bit;
if (mb.bitrate == 8000)
{
fsb = mb_nOnes_int((block[0] << 8 | block[1]) ^ bitsync);
fsbr = mb_nOnes_int((block[0] << 8 | block[1]) ^ bitsync_rev);
if (cfs) // Checking frame sync?
{
fsb += mb_nOnes_int((block[2] << 8 | block[3]) ^ frsync);
fsbr += mb_nOnes_int((block[2] << 8 | block[3]) ^ frsync_rev);
be = 2; // was 4
}
if (fsb <= cfs)
{
if (!cfs) // Not checking frame sync
{
for (int i=0; i<18; i++)
{
if (mb_nOnes_int((block[2] << 8 | block[3]) ^ frsyncs[i]) == 0) break;
}
if (i == 18) return;
}
live_frsync = ((block[2] << 8 | block[3]) & 0xFFFF);
bc = 25;
GetFrameHeader();
}
else if (fsbr == 0) // ....is signal reversed?
{
if (!cfs) // Not checking frame sync
{
for (int i=0; i<18; i++)
{
if (mb_nOnes_int((block[2] << 8 | block[3]) ^ frevsyncs[i]) == 0) break;
}
if (i == 18) return;
}
InvertData(); // Invert receive polarity
return;
}
}
else
{
if (block[0] == 0xAA && block[1] == 0xCE && (block[2] >> 6) == 0)
{
GetFrameHeader();
bc = 25;
}
else if (block[0] == 0x55 && block[1] == 0x31 && (block[2] >> 6) == 0x3)
{
InvertData(); // Invert receive polarity
return;
}
}
*/
if (bc == 0) // Not synced, looking for ROSI header
{
fsb = mb_nOnes_int(s1^bitsync);
fsbr = mb_nOnes_int(s1^bitsync_rev);
if (cfs) // Checking frame sync?
{
fsb += mb_nOnes_int(s2^frsync);
fsbr += mb_nOnes_int(s2^frsync_rev);
}
// if first two integers match up within a bit or two then we've gotten frame sync
if (fsb <= cfs)
{
if (!cfs) // Not checking frame sync
{
for (int i=0; i<18; i++)
{
if (mb_nOnes_int(s2^frsyncs[i]) == 0) break;
else if (i == 17) return;
}
}
live_frsync = (s2 & 0xFFFF);
bc = 25;
}
else if (fsbr == 0) // ....is signal reversed?
{
if (!cfs) // Not checking frame sync
{
for (int i=0; i<18; i++)
{
if (mb_nOnes_int(s2^frevsyncs[i]) == 0) break;
else if (i == 17) return;
}
}
InvertData(); // Invert receive polarity
live_frsync = (~s2 & 0xFFFF);
bc = 25;
}
}
// bc is a bit counter used to pick off the two status bytes and the FEC byte in the header.
if (bc > 0)
{
bc--;
if (bc == 0)
{
// strip off control bytes, run them through FEC routine
ControlByte1 = (s1 & 0xFF) << 4;
ControlByte1 += (s2 >> 4) & 0xF;
ControlByte2 = (s2 >> 4) & 0xFF0;
ControlByte2 += (s2 & 0xF);
fec = get_fec(&ControlByte1) + get_fec(&ControlByte2);
CountBiterrors(fec);
if (!timer) // In case we lose Mobitex-sync, clear BaseID and Name to prevent
{ // the wrong ID from being displayed when switching channels
strcpy(LINK_CONTROL_INFO.BaseID, "?");
strcpy(szBaseIdName, "");
}
if (!fec) // Only if ZERO errors
{
FRAME_HEADER.BaseID = (s2 & 0xFC0000) >> 18; // Get Base Identification
FRAME_HEADER.AreaID = (s2 & 0x3F000) >> 12; // Get Area Identification
FRAME_HEADER.CFlags = (s2 & 0xF00) >> 8; // Get Control Flags
FRAME_HEADER.Parity = (s2 & 0xFF); // Get Parity
sprintf(LINK_CONTROL_INFO.BaseID, "%02X%02X", FRAME_HEADER.AreaID, FRAME_HEADER.BaseID);
}
if ((strcmp(LINK_CONTROL_INFO.BaseID, szLastBaseID) != 0) ||
(!szBaseIdName[0])) // Only if we detect an other BaseID
{
GetBaseID(LINK_CONTROL_INFO.BaseID, szBaseIdName); // Get Base Name
strcpy(szLastBaseID, LINK_CONTROL_INFO.BaseID);
if (szBaseIdName[0]) sprintf(szTmpIdName, " (%s)", szBaseIdName);
if (bitsync == 0xCCCC)
{
sprintf(szWindowText[3], "%d (base) - NetworkID: %04X - BaseID: %s%s", mb.bitrate, (cfs ? frsync : live_frsync), LINK_CONTROL_INFO.BaseID, szBaseIdName[0] ? szTmpIdName : "");
}
if (bitsync == 0x3333) sprintf(szWindowText[3], "%d (mobile)", mb.bitrate);
display_showmo(MODE_MOBITEX);
}
timer = 10; // Lets stay in Mobitex mode for another second
// if monitoring a RAM network and CFlags == 0 data block(s) follow
// otherwise just be adventurous and see if we get a valid block
if (ramnet)
{
if (!FRAME_HEADER.CFlags)
{
nBitCount = 1440;
}
else return; // If CFlags != 0 , this frame should be empty
}
else nBitCount = 1440;
MPAK_HEADER.Destination=0;
MPAK_HEADER.Sender=0;
bBadDestination=false;
bBadSender=false;
iBlockNumber=0;
nCharacters=0;
nu = 0;
mb_bs(-1);
} // endof "if bc == 0"
} // endof "if bc > 0"
}
else
{
if (mb_bs(1) == 1) og = bit ^ 0x01;
else og = bit;
obm[nu++] = og;
if (nu == 240)
{
nu = 0;
barfrog();
if (iBlockNumber >= LINK_CONTROL_INFO.BlockLength) nBitCount = 1;
else nBitCount = 241;
// if (barfrog() < MAX_ERR)
// {
// nBitCount = 241;
// }
// else nBitCount = 1;
}
// If end of signal display msg if it's at least 'n' characters long.
nBitCount--;
if (nBitCount == 0)
{
for (nu=nCharacters-1; nu>=0; nu--)
{
if ((mb_msg_buf[nu] < 32) || (mb_msg_buf[nu] > 126)) nCharacters--;
else break;
}
if (!szMPAK[0] && (nCharacters < min_msg_len))
{
if (!(mb.show & MOBITEX_VERBOSE)) return; // Message is too short
}
if (bBadHeader) return; // See GetHeaderInfo()
if ((MPAK_HEADER.Sender > 16777216) ||
(MPAK_HEADER.Destination > 16777216))
{
return; // If MAN is too high (errors)
}
if (strlen(szDestination) != strlen(szSender))
{
return; // If MAN lengths are different (errors)
}
// ********* Destination MAN ***********
strcpy(Current_MSG[MSG_CAPCODE], szDestination);
if (bBadDestination) // Destination byte contains biterrors
{
messageitems_colors[MSG_CAPCODE] = COLOR_BITERRORS;
}
else messageitems_colors[MSG_CAPCODE] = COLOR_ADDRESS;
// ************* Time/Date **************
Get_Date_Time();
strcpy(Current_MSG[MSG_TIME], szCurrentTime);
strcpy(Current_MSG[MSG_DATE], szCurrentDate);
messageitems_colors[MSG_TIME] = COLOR_TIMESTAMP;
messageitems_colors[MSG_DATE] = COLOR_TIMESTAMP;
// ********* Sender MAN *****************
strcpy(Current_MSG[MSG_MODE], szSender);
if (bBadSender) // Sender byte contains biterrors
{
messageitems_colors[MSG_MODE] = COLOR_BITERRORS;
}
else messageitems_colors[MSG_MODE] = COLOR_MB_SENDER;
// ********* Message TYPE ***************
strcpy(Current_MSG[MSG_TYPE], szType);
messageitems_colors[MSG_TYPE] = COLOR_MODETYPEBIT;
// ********* Bitrate ********************
sprintf(Current_MSG[MSG_BITRATE], "%d",bitrate);
messageitems_colors[MSG_BITRATE] = COLOR_MODETYPEBIT;
// ********* Message / MPAK *************
if (szMPAK[0]) // Show MPAK
{
display_color(&Pane1, COLOR_MISC);
display_show_str(&Pane1, szMPAK);
strcpy(szMPAK, "");
}
else // Show Message
{
hourly_stat[STAT_MOBITEX][STAT_ALPHA]++;
daily_stat[STAT_MOBITEX][STAT_ALPHA]++;
if ((nCharacters == 18) && // Coordinates
(mb_msg_buf[0] == '+') &&
(mb_msg_buf[1] == ' ') &&
(mb_msg_buf[15] == ' ') &&
(mb_msg_buf[16] == 0 ) &&
(mb_msg_buf[17] == '"'))
{
for (nu=2; nu<15; nu+=2)
{
if (mb_msg_buf[nu] < 32) mb_msg_buf[nu] += 90;
}
}
for (nu=0; nu<nCharacters; nu++)
{
display_color(&Pane1, mb_msg_col[nu]);
display_show_char(&Pane1, mb_msg_buf[nu]);
hourly_char[STAT_MOBITEX][STAT_ALPHA]++;
daily_char[STAT_MOBITEX][STAT_ALPHA]++;
}
}
ShowMessage(); // Display Message.
if (szWindowText[5][0]) display_showmo(MODE_MOBITEX);
}
}
}
/*
void MOBITEX::GetFrameHeader(void)
{
// int ControlByte1, ControlByte2;
static char szLastBaseID[8], szTmpIdName[128];
if (mb.bitrate == 8000)
{
int fec = get_fec(&block[4]) + get_fec(&block[5]);
CountBiterrors(fec);
if (!timer) // In case we lose Mobitex-sync, clear BaseID and Name to prevent
{ // the wrong ID from being displayed when switching channels
strcpy(LINK_CONTROL_INFO.BaseID, "?");
strcpy(szBaseIdName, "");
}
if (!fec) // Only if ZERO errors
{
// FRAME_HEADER.BaseID = (s2 & 0xFC0000) >> 18; // Get Base Identification
// FRAME_HEADER.AreaID = (s2 & 0x3F000) >> 12; // Get Area Identification
// FRAME_HEADER.CFlags = (s2 & 0xF00) >> 8; // Get Control Flags
// FRAME_HEADER.Parity = (s2 & 0xFF); // Get Parity
sprintf(LINK_CONTROL_INFO.BaseID, "%02X%02X", FRAME_HEADER.AreaID, FRAME_HEADER.BaseID);
}
}
else
{
FRAME_HEADER.BaseID = (block[2] << 4) + (block[3] >> 4);
FRAME_HEADER.CFlags = (block[3] & 0x3);
sprintf(LINK_CONTROL_INFO.BaseID, "%03X", FRAME_HEADER.BaseID);
}
if ((strcmp(LINK_CONTROL_INFO.BaseID, szLastBaseID) != 0) ||
(!szBaseIdName[0])) // Only if we detect an other BaseID
{
GetBaseID(LINK_CONTROL_INFO.BaseID, szBaseIdName); // Get Base Name
strcpy(szLastBaseID, LINK_CONTROL_INFO.BaseID);
if (szBaseIdName[0]) sprintf(szTmpIdName, " (%s)", szBaseIdName);
if (bitsync == 0xCCCC)
{
sprintf(szWindowText[3], "%d (base) - NetworkID: %04X - BaseID: %s%s", mb.bitrate, (cfs ? frsync : live_frsync), LINK_CONTROL_INFO.BaseID, szBaseIdName[0] ? szTmpIdName : "");
}
if (bitsync == 0x3333) sprintf(szWindowText[3], "%d (mobile)", mb.bitrate);
display_showmo(MODE_MOBITEX);